source: node_modules/end-of-stream/index.js@ 81bc7da

finki-main main
Last change on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 2.7 KB
RevLine 
[81bc7da]1var once = require('once');
2
3var noop = function() {};
4
5var qnt = global.Bare ? queueMicrotask : process.nextTick.bind(process);
6
7var isRequest = function(stream) {
8 return stream.setHeader && typeof stream.abort === 'function';
9};
10
11var isChildProcess = function(stream) {
12 return stream.stdio && Array.isArray(stream.stdio) && stream.stdio.length === 3
13};
14
15var eos = function(stream, opts, callback) {
16 if (typeof opts === 'function') return eos(stream, null, opts);
17 if (!opts) opts = {};
18
19 callback = once(callback || noop);
20
21 var ws = stream._writableState;
22 var rs = stream._readableState;
23 var readable = opts.readable || (opts.readable !== false && stream.readable);
24 var writable = opts.writable || (opts.writable !== false && stream.writable);
25 var cancelled = false;
26
27 var onlegacyfinish = function() {
28 if (!stream.writable) onfinish();
29 };
30
31 var onfinish = function() {
32 writable = false;
33 if (!readable) callback.call(stream);
34 };
35
36 var onend = function() {
37 readable = false;
38 if (!writable) callback.call(stream);
39 };
40
41 var onexit = function(exitCode) {
42 callback.call(stream, exitCode ? new Error('exited with error code: ' + exitCode) : null);
43 };
44
45 var onerror = function(err) {
46 callback.call(stream, err);
47 };
48
49 var onclose = function() {
50 qnt(onclosenexttick);
51 };
52
53 var onclosenexttick = function() {
54 if (cancelled) return;
55 if (readable && !(rs && (rs.ended && !rs.destroyed))) return callback.call(stream, new Error('premature close'));
56 if (writable && !(ws && (ws.ended && !ws.destroyed))) return callback.call(stream, new Error('premature close'));
57 };
58
59 var onrequest = function() {
60 stream.req.on('finish', onfinish);
61 };
62
63 if (isRequest(stream)) {
64 stream.on('complete', onfinish);
65 stream.on('abort', onclose);
66 if (stream.req) onrequest();
67 else stream.on('request', onrequest);
68 } else if (writable && !ws) { // legacy streams
69 stream.on('end', onlegacyfinish);
70 stream.on('close', onlegacyfinish);
71 }
72
73 if (isChildProcess(stream)) stream.on('exit', onexit);
74
75 stream.on('end', onend);
76 stream.on('finish', onfinish);
77 if (opts.error !== false) stream.on('error', onerror);
78 stream.on('close', onclose);
79
80 return function() {
81 cancelled = true;
82 stream.removeListener('complete', onfinish);
83 stream.removeListener('abort', onclose);
84 stream.removeListener('request', onrequest);
85 if (stream.req) stream.req.removeListener('finish', onfinish);
86 stream.removeListener('end', onlegacyfinish);
87 stream.removeListener('close', onlegacyfinish);
88 stream.removeListener('finish', onfinish);
89 stream.removeListener('exit', onexit);
90 stream.removeListener('end', onend);
91 stream.removeListener('error', onerror);
92 stream.removeListener('close', onclose);
93 };
94};
95
96module.exports = eos;
Note: See TracBrowser for help on using the repository browser.