| 1 |
var once = require('once'); |
| 2 |
|
| 3 |
var noop = function() {}; |
| 4 |
|
| 5 |
var qnt = global.Bare ? queueMicrotask : process.nextTick.bind(process); |
| 6 |
|
| 7 |
var isRequest = function(stream) { |
| 8 |
return stream.setHeader && typeof stream.abort === 'function'; |
| 9 |
}; |
| 10 |
|
| 11 |
var isChildProcess = function(stream) { |
| 12 |
return stream.stdio && Array.isArray(stream.stdio) && stream.stdio.length === 3 |
| 13 |
}; |
| 14 |
|
| 15 |
var 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 |
|
| 96 |
module.exports = eos; |