index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict';
  2. var Readable = require('readable-stream').Readable;
  3. var inherits = require('inherits');
  4. inherits(Noms, Readable);
  5. function Noms (options) {
  6. Readable.call(this,options);
  7. this.inProgress = false;
  8. this.lastPush = void 0;
  9. this.started = false;
  10. this.errored = false;
  11. }
  12. Noms.prototype.push = function(chunk, encoding) {
  13. this.lastPush = Readable.prototype.push.call(this, chunk, encoding);
  14. return this.lastPush;
  15. };
  16. Noms.prototype.nom = function (callback) {
  17. callback(null, null);
  18. };
  19. Noms.prototype._read = function (size) {
  20. if (this.inProgress || this.errored) {
  21. return;
  22. }
  23. if (this.started === false) {
  24. this.inProgress = true;
  25. this.callStart(size);
  26. return;
  27. }
  28. this.inProgress = true;
  29. this.callRead(size);
  30. };
  31. Noms.prototype._before = function (next) {
  32. next();
  33. };
  34. Noms.prototype.callRead = function (size) {
  35. var useSize = this.nom.length > 1;
  36. // so if nothing is pushed, we'll go agian
  37. this.lastPush = true;
  38. var self = this;
  39. function cb(err, chunk) {
  40. if (err) {
  41. self.errored = true;
  42. self.inProgress = false;
  43. self.emit('error', err);
  44. return;
  45. }
  46. if (chunk !== undefined) {
  47. self.push(chunk);
  48. }
  49. if (self.lastPush) {
  50. return self.callRead(size);
  51. } else {
  52. self.inProgress = false;
  53. }
  54. }
  55. if (useSize) {
  56. this.nom(size, cb);
  57. } else {
  58. this.nom(cb);
  59. }
  60. };
  61. Noms.prototype.callStart = function (size) {
  62. var self = this;
  63. function cb(err, chunk) {
  64. self.started = true;
  65. if (err) {
  66. self.errored = true;
  67. self.inProgress = false;
  68. self.emit('error', err);
  69. return;
  70. }
  71. if (chunk !== undefined) {
  72. self.push(chunk);
  73. }
  74. self.callRead(size);
  75. }
  76. this._before(cb);
  77. };
  78. function ctor(read, before) {
  79. inherits(YourStream, Noms);
  80. function YourStream (opts) {
  81. Noms.call(this, opts);
  82. }
  83. YourStream.prototype.nom = read;
  84. if (typeof before === 'function') {
  85. YourStream.prototype._before = before;
  86. }
  87. return YourStream;
  88. }
  89. module.exports = exports = function(options, read, before) {
  90. if (typeof options === 'function') {
  91. before = read;
  92. read = options;
  93. options = {};
  94. }
  95. return new (ctor(read, before))(options);
  96. };
  97. exports.ctor = ctor;
  98. exports.obj = function(options, read, before) {
  99. var out = {};
  100. if (typeof options === 'function') {
  101. before = read;
  102. read = options;
  103. options = undefined;
  104. }
  105. options = options || {};
  106. Object.keys(options).forEach(function (key) {
  107. out[key] = options[key];
  108. });
  109. out.objectMode = true;
  110. return new (ctor(read, before))(out);
  111. };