From 0d9b3feee4ae0addd5e1c7346bb9f0d8a0a84b28 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Tue, 2 Feb 2021 15:13:37 +0100 Subject: [PATCH] Only consider non-null streams --- index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index f08c467..3d3a29a 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,14 @@ "use strict"; module.exports = function pipe(streams) { + // NOTE: We explicitly check for *null* here, not undefined. We still want to detect accidentally-omitted streams, but permit the user to explicitly insert nulls for streams that are not always inserted (eg. optional parallelization in a library). */ + let existentStreams = streams.filter((stream) => stream !== null); + // FIXME: Validate input properly - if (streams.length < 1) { + if (existentStreams.length < 1) { throw new Error("Must specify at least one stream when defining a pipeline"); } else { - let firstStream = streams[0]; + let firstStream = existentStreams[0]; let requiresSource = (firstStream.read.length > 0 && firstStream.__ppstreams_hasSource !== true); /* NOTE: We never clean up the cache, because it's very unlikely that this cache will ever grow big. In the vast majority of cases, it's going to contain at most one item. So instead, we let the garbage collector worry about getting rid of it once the pipeline itself becomes obsolete. */ @@ -13,7 +16,7 @@ module.exports = function pipe(streams) { function getPipeline(source) { if (!boundPipelineCache.has(source)) { - let pipeline = streams.reduce((bound, stream) => { + let pipeline = existentStreams.reduce((bound, stream) => { if (stream.read == null) { throw new Error(`Stream is missing a read handler (stream description: ${stream.description})`); } else if (stream.abort == null) { @@ -47,7 +50,7 @@ module.exports = function pipe(streams) { return { // NOTE: This is set to convey to other `pipe` calls (as well as any other composability tools) that this is explicitly a composed stream that does not require a source, even if the `read` signature suggests otherwise. FIXME: Figure out how and whether to spec this, or whether there is a better way to deal with this. __ppstreams_hasSource: !requiresSource, - description: `piped stream [${streams.map((stream) => stream.description).join(" => ")}]`, + description: `piped stream [${existentStreams.map((stream) => stream.description).join(" => ")}]`, read: function (source) { if(!verifyFullPipeline(source)) { throw new Error("Cannot read from a partial pipeline; maybe you forgot to specify a source stream?");