Skip to content

Commit

Permalink
stream: unify stream utils
Browse files Browse the repository at this point in the history
Unify stream helps into utils.
  • Loading branch information
ronag committed Jul 7, 2021
1 parent 4de6f20 commit f4c1935
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 84 deletions.
22 changes: 6 additions & 16 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const {
const {
Symbol,
} = primordials;
const {
kDestroyed,
isDestroyed,
isFinished,
} = require('internal/streams/utils');

const kDestroy = Symbol('kDestroy');
const kConstruct = Symbol('kConstruct');
Expand Down Expand Up @@ -364,8 +369,6 @@ function isRequest(stream) {
return stream && stream.setHeader && typeof stream.abort === 'function';
}

const kDestroyed = Symbol('kDestroyed');

function emitCloseLegacy(stream) {
stream.emit('close');
}
Expand All @@ -375,25 +378,13 @@ function emitErrorCloseLegacy(stream, err) {
process.nextTick(emitCloseLegacy, stream);
}

function isDestroyed(stream) {
return stream.destroyed || stream[kDestroyed];
}

function isReadable(stream) {
return stream.readable && !stream.readableEnded && !isDestroyed(stream);
}

function isWritable(stream) {
return stream.writable && !stream.writableEnded && !isDestroyed(stream);
}

// Normalize destroy for legacy.
function destroyer(stream, err) {
if (isDestroyed(stream)) {
return;
}

if (!err && (isReadable(stream) || isWritable(stream))) {
if (!err && !isFinished(stream)) {
err = new AbortError();
}

Expand Down Expand Up @@ -422,7 +413,6 @@ function destroyer(stream, err) {

module.exports = {
kDestroy,
isDestroyed,
construct,
destroyer,
destroy,
Expand Down
76 changes: 22 additions & 54 deletions lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,23 @@ const {
validateObject,
} = require('internal/validators');

const {
isClosed,
isReadable,
isReadableStream,
isReadableEnded,
isWritable,
isWritableStream,
isWritableFinished,
willEmitClose: _willEmitClose,
} = require('internal/streams/utils');

function isRequest(stream) {
return stream.setHeader && typeof stream.abort === 'function';
}

function isServerResponse(stream) {
return (
typeof stream._sent100 === 'boolean' &&
typeof stream._removedConnection === 'boolean' &&
typeof stream._removedContLen === 'boolean' &&
typeof stream._removedTE === 'boolean' &&
typeof stream._closed === 'boolean'
);
}

function isReadable(stream) {
return typeof stream.readable === 'boolean' ||
typeof stream.readableEnded === 'boolean' ||
!!stream._readableState;
}

function isWritable(stream) {
return typeof stream.writable === 'boolean' ||
typeof stream.writableEnded === 'boolean' ||
!!stream._writableState;
}

function isWritableFinished(stream) {
if (stream.writableFinished) return true;
const wState = stream._writableState;
if (!wState || wState.errored) return false;
return wState.finished || (wState.ended && wState.length === 0);
}

const nop = () => {};

function isReadableEnded(stream) {
if (stream.readableEnded) return true;
const rState = stream._readableState;
if (!rState || rState.errored) return false;
return rState.endEmitted || (rState.ended && rState.length === 0);
}

function eos(stream, options, callback) {
if (arguments.length === 2) {
callback = options;
Expand All @@ -74,13 +49,12 @@ function eos(stream, options, callback) {
callback = once(callback);

const readable = options.readable ||
(options.readable !== false && isReadable(stream));
(options.readable !== false && isReadableStream(stream));
const writable = options.writable ||
(options.writable !== false && isWritable(stream));
(options.writable !== false && isWritableStream(stream));

const wState = stream._writableState;
const rState = stream._readableState;
const state = wState || rState;

const onlegacyfinish = () => {
if (!stream.writable) onfinish();
Expand All @@ -89,16 +63,13 @@ function eos(stream, options, callback) {
// TODO (ronag): Improve soft detection to include core modules and
// common ecosystem modules that do properly emit 'close' but fail
// this generic check.
let willEmitClose = isServerResponse(stream) || (
state &&
state.autoDestroy &&
state.emitClose &&
state.closed === false &&
isReadable(stream) === readable &&
isWritable(stream) === writable
let willEmitClose = (
_willEmitClose(stream) &&
isReadableStream(stream) === readable &&
isWritableStream(stream) === writable
);

let writableFinished = stream.writableFinished || wState?.finished;
let writableFinished = isWritableFinished(stream);
const onfinish = () => {
writableFinished = true;
// Stream should not be destroyed here. If it is that
Expand All @@ -110,7 +81,7 @@ function eos(stream, options, callback) {
if (!readable || readableEnded) callback.call(stream);
};

let readableEnded = stream.readableEnded || rState?.endEmitted;
let readableEnded = isReadableEnded(stream);
const onend = () => {
readableEnded = true;
// Stream should not be destroyed here. If it is that
Expand All @@ -126,7 +97,7 @@ function eos(stream, options, callback) {
callback.call(stream, err);
};

let closed = wState?.closed || rState?.closed;
let closed = isClosed(stream);

const onclose = () => {
closed = true;
Expand Down Expand Up @@ -185,19 +156,16 @@ function eos(stream, options, callback) {
}
} else if (
!readable &&
(!willEmitClose || stream.readable) &&
(!willEmitClose || isReadable(stream)) &&
writableFinished
) {
process.nextTick(onclose);
} else if (
!writable &&
(!willEmitClose || stream.writable) &&
(!willEmitClose || isWritable(stream)) &&
readableEnded
) {
process.nextTick(onclose);
} else if (!wState && !rState && stream._closed === true) {
// _closed is for OutgoingMessage which is not a proper Writable.
process.nextTick(onclose);
} else if ((rState && stream.req && stream.aborted)) {
process.nextTick(onclose);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const { validateCallback } = require('internal/validators');

const {
isIterable,
isReadable,
isReadableStream,
isStream,
} = require('internal/streams/utils');

Expand Down Expand Up @@ -87,7 +87,7 @@ function popCallback(streams) {
function makeAsyncIterable(val) {
if (isIterable(val)) {
return val;
} else if (isReadable(val)) {
} else if (isReadableStream(val)) {
// Legacy streams are not Iterable.
return fromReadable(val);
}
Expand Down Expand Up @@ -216,7 +216,7 @@ function pipeline(...streams) {
throw new ERR_INVALID_RETURN_VALUE(
'Iterable, AsyncIterable or Stream', 'source', ret);
}
} else if (isIterable(stream) || isReadable(stream)) {
} else if (isIterable(stream) || isReadableStream(stream)) {
ret = stream;
} else {
throw new ERR_INVALID_ARG_TYPE(
Expand Down Expand Up @@ -272,7 +272,7 @@ function pipeline(...streams) {
destroys.push(destroyer(ret, false, true, finish));
}
} else if (isStream(stream)) {
if (isReadable(ret)) {
if (isReadableStream(ret)) {
ret.pipe(stream);

// Compat. Before node v10.12.0 stdio used to throw an error so
Expand Down
Loading

0 comments on commit f4c1935

Please sign in to comment.