through2.js 957 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const Transform = require('stream').Transform || require('readable-stream/transform')
  2. , inherits = require('util').inherits
  3. , xtend = require('xtend')
  4. function noop (chunk, enc, callback) {
  5. callback(null, chunk)
  6. }
  7. function ctor (options, transform, flush) {
  8. if (typeof options == 'function') {
  9. flush = transform
  10. transform = options
  11. options = {}
  12. }
  13. if (typeof transform != 'function')
  14. transform = noop
  15. function Through2 (override) {
  16. if (!(this instanceof Through2))
  17. return new Through2(override)
  18. this.options = xtend(options, override)
  19. Transform.call(this, this.options)
  20. }
  21. inherits(Through2, Transform)
  22. Through2.prototype._transform = transform
  23. if (typeof flush == 'function')
  24. Through2.prototype._flush = flush
  25. return Through2
  26. }
  27. function make (options, transform, flush) {
  28. return ctor(options, transform, flush)()
  29. }
  30. module.exports = make
  31. module.exports.ctor = ctor