index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. 'use strict';
  2. const {Readable, Writable, Duplex, Transform} = require('stream');
  3. function Final(value) {
  4. this.value = value;
  5. }
  6. function Many(values) {
  7. this.values = values;
  8. }
  9. const processData = (result, stream) => {
  10. if (result instanceof Final) {
  11. result = result.value;
  12. } else if (result instanceof Many) {
  13. result = result.values;
  14. }
  15. if (result !== undefined && result !== null) {
  16. if (result instanceof Array) {
  17. result.forEach(value => value !== undefined && value !== null && stream.push(value));
  18. } else {
  19. stream.push(result);
  20. }
  21. }
  22. };
  23. const wrapFunction = fn =>
  24. new Transform({
  25. writableObjectMode: true,
  26. readableObjectMode: true,
  27. transform(chunk, encoding, callback) {
  28. try {
  29. const result = fn.call(this, chunk, encoding);
  30. if (result && typeof result.then == 'function') {
  31. // Promise
  32. result.then(result => (processData(result, this), callback(null)), error => callback(error));
  33. return;
  34. }
  35. if (result && typeof result.next == 'function') {
  36. // generator
  37. while (true) {
  38. const data = result.next();
  39. processData(data.value, this);
  40. if (data.done) break;
  41. }
  42. } else {
  43. processData(result, this);
  44. }
  45. callback(null);
  46. } catch (error) {
  47. callback(error);
  48. }
  49. }
  50. });
  51. const wrapArray = array =>
  52. new Transform({
  53. writableObjectMode: true,
  54. readableObjectMode: true,
  55. transform(chunk, encoding, callback) {
  56. try {
  57. let value = chunk;
  58. for (let i = 0; i < array.length; ++i) {
  59. const result = array[i].call(this, value, encoding);
  60. if (result instanceof Final) {
  61. value = result.value;
  62. break;
  63. }
  64. value = result;
  65. }
  66. processData(value, this);
  67. callback(null);
  68. } catch (error) {
  69. callback(error);
  70. }
  71. }
  72. });
  73. const convertToTransform = fn => {
  74. if (typeof fn === 'function') return wrapFunction(fn);
  75. if (fn instanceof Array) return fn.length ? wrapArray(fn) : 0;
  76. return null;
  77. };
  78. class Chain extends Duplex {
  79. constructor(fns, options) {
  80. super(options || {writableObjectMode: true, readableObjectMode: true});
  81. if (!(fns instanceof Array) || !fns.length) {
  82. throw Error("Chain's argument should be a non-empty array.");
  83. }
  84. this.streams = fns
  85. .map((fn, index) => {
  86. if (typeof fn === 'function' || fn instanceof Array) return Chain.convertToTransform(fn);
  87. if (
  88. fn instanceof Duplex ||
  89. fn instanceof Transform ||
  90. (!index && fn instanceof Readable) ||
  91. (index === fns.length - 1 && fn instanceof Writable)
  92. ) {
  93. return fn;
  94. }
  95. throw Error('Arguments should be functions or streams.');
  96. })
  97. .filter(s => s);
  98. this.input = this.streams[0];
  99. this.output = this.streams.reduce((output, stream) => (output && output.pipe(stream)) || stream);
  100. if (!(this.input instanceof Writable)) {
  101. this._write = (_1, _2, callback) => callback(null);
  102. this._final = callback => callback(null); // unavailable in Node 6
  103. this.input.on('end', () => this.end());
  104. }
  105. if (this.output instanceof Readable) {
  106. this.output.on('data', chunk => !this.push(chunk) && this.output.pause());
  107. this.output.on('end', () => this.push(null));
  108. } else {
  109. this._read = () => {}; // nop
  110. this.resume();
  111. this.output.on('finish', () => this.push(null));
  112. }
  113. // connect events
  114. if (!options || !options.skipEvents) {
  115. this.streams.forEach(stream => stream.on('error', error => this.emit('error', error)));
  116. }
  117. }
  118. _write(chunk, encoding, callback) {
  119. let error = null;
  120. try {
  121. this.input.write(chunk, encoding, e => callback(e || error));
  122. } catch (e) {
  123. error = e;
  124. }
  125. }
  126. _final(callback) {
  127. // unavailable in Node 6
  128. let error = null;
  129. try {
  130. this.input.end(null, null, e => callback(e || error));
  131. } catch (e) {
  132. error = e;
  133. }
  134. }
  135. _read() {
  136. this.output.resume();
  137. }
  138. static chain(fns, options) {
  139. return new Chain(fns, options);
  140. }
  141. static final(value) {
  142. return new Final(value);
  143. }
  144. static many(values) {
  145. return new Many(values);
  146. }
  147. static convertToTransform(fn) {
  148. if (typeof fn === 'function') return wrapFunction(fn);
  149. if (fn instanceof Array) return fn.length ? wrapArray(fn) : 0;
  150. return null;
  151. }
  152. }
  153. Chain.make = Chain.chain;
  154. Chain.make.Constructor = Chain;
  155. module.exports = Chain;