end-of-stream.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Ported from https://github.com/mafintosh/end-of-stream with
  2. // permission from the author, Mathias Buus (@mafintosh).
  3. 'use strict';
  4. var ERR_STREAM_PREMATURE_CLOSE = require('../../../errors').codes.ERR_STREAM_PREMATURE_CLOSE;
  5. function once(callback) {
  6. var called = false;
  7. return function () {
  8. if (called) return;
  9. called = true;
  10. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  11. args[_key] = arguments[_key];
  12. }
  13. callback.apply(this, args);
  14. };
  15. }
  16. function noop() {}
  17. function isRequest(stream) {
  18. return stream.setHeader && typeof stream.abort === 'function';
  19. }
  20. function eos(stream, opts, callback) {
  21. if (typeof opts === 'function') return eos(stream, null, opts);
  22. if (!opts) opts = {};
  23. callback = once(callback || noop);
  24. var readable = opts.readable || opts.readable !== false && stream.readable;
  25. var writable = opts.writable || opts.writable !== false && stream.writable;
  26. var onlegacyfinish = function onlegacyfinish() {
  27. if (!stream.writable) onfinish();
  28. };
  29. var writableEnded = stream._writableState && stream._writableState.finished;
  30. var onfinish = function onfinish() {
  31. writable = false;
  32. writableEnded = true;
  33. if (!readable) callback.call(stream);
  34. };
  35. var readableEnded = stream._readableState && stream._readableState.endEmitted;
  36. var onend = function onend() {
  37. readable = false;
  38. readableEnded = true;
  39. if (!writable) callback.call(stream);
  40. };
  41. var onerror = function onerror(err) {
  42. callback.call(stream, err);
  43. };
  44. var onclose = function onclose() {
  45. var err;
  46. if (readable && !readableEnded) {
  47. if (!stream._readableState || !stream._readableState.ended) err = new ERR_STREAM_PREMATURE_CLOSE();
  48. return callback.call(stream, err);
  49. }
  50. if (writable && !writableEnded) {
  51. if (!stream._writableState || !stream._writableState.ended) err = new ERR_STREAM_PREMATURE_CLOSE();
  52. return callback.call(stream, err);
  53. }
  54. };
  55. var onrequest = function onrequest() {
  56. stream.req.on('finish', onfinish);
  57. };
  58. if (isRequest(stream)) {
  59. stream.on('complete', onfinish);
  60. stream.on('abort', onclose);
  61. if (stream.req) onrequest();else stream.on('request', onrequest);
  62. } else if (writable && !stream._writableState) {
  63. // legacy streams
  64. stream.on('end', onlegacyfinish);
  65. stream.on('close', onlegacyfinish);
  66. }
  67. stream.on('end', onend);
  68. stream.on('finish', onfinish);
  69. if (opts.error !== false) stream.on('error', onerror);
  70. stream.on('close', onclose);
  71. return function () {
  72. stream.removeListener('complete', onfinish);
  73. stream.removeListener('abort', onclose);
  74. stream.removeListener('request', onrequest);
  75. if (stream.req) stream.req.removeListener('finish', onfinish);
  76. stream.removeListener('end', onlegacyfinish);
  77. stream.removeListener('close', onlegacyfinish);
  78. stream.removeListener('finish', onfinish);
  79. stream.removeListener('end', onend);
  80. stream.removeListener('error', onerror);
  81. stream.removeListener('close', onclose);
  82. };
  83. }
  84. module.exports = eos;