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