Няма описание

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. var util = require('util');
  3. var Transform = require('stream').Transform;
  4. function ctor(options, transform) {
  5. util.inherits(FirstChunk, Transform);
  6. if (typeof options === 'function') {
  7. transform = options;
  8. options = {};
  9. }
  10. if (typeof transform !== 'function') {
  11. throw new Error('transform function required');
  12. }
  13. function FirstChunk(options2) {
  14. if (!(this instanceof FirstChunk)) {
  15. return new FirstChunk(options2);
  16. }
  17. Transform.call(this, options2);
  18. this._firstChunk = true;
  19. this._transformCalled = false;
  20. this._minSize = options.minSize;
  21. }
  22. FirstChunk.prototype._transform = function (chunk, enc, cb) {
  23. this._enc = enc;
  24. if (this._firstChunk) {
  25. this._firstChunk = false;
  26. if (this._minSize == null) {
  27. transform.call(this, chunk, enc, cb);
  28. this._transformCalled = true;
  29. return;
  30. }
  31. this._buffer = chunk;
  32. cb();
  33. return;
  34. }
  35. if (this._minSize == null) {
  36. this.push(chunk);
  37. cb();
  38. return;
  39. }
  40. if (this._buffer.length < this._minSize) {
  41. this._buffer = Buffer.concat([this._buffer, chunk]);
  42. cb();
  43. return;
  44. }
  45. if (this._buffer.length >= this._minSize) {
  46. transform.call(this, this._buffer.slice(), enc, function () {
  47. this.push(chunk);
  48. cb();
  49. }.bind(this));
  50. this._transformCalled = true;
  51. this._buffer = false;
  52. return;
  53. }
  54. this.push(chunk);
  55. cb();
  56. };
  57. FirstChunk.prototype._flush = function (cb) {
  58. if (!this._buffer) {
  59. cb();
  60. return;
  61. }
  62. if (this._transformCalled) {
  63. this.push(this._buffer);
  64. cb();
  65. } else {
  66. transform.call(this, this._buffer.slice(), this._enc, cb);
  67. }
  68. };
  69. return FirstChunk;
  70. }
  71. module.exports = function () {
  72. return ctor.apply(ctor, arguments)();
  73. };
  74. module.exports.ctor = ctor;