nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
107.33k stars 29.47k forks source link

stream.pipeline does not accept zero-length string as a valid first argument #38721

Closed carlansley closed 3 years ago

carlansley commented 3 years ago

What steps will reproduce the bug?

This corner-case issue is another continuation of #36437. Although Iterables are now supported, zero-length strings are not.

const stream = require("stream");

async function main() {
    await stream.pipeline(
        /*stream1*/ '',
        /*stream2*/ new stream.PassThrough({ objectMode: true }),
        /*callback*/ () => { console.log("done"); });
}

main().catch(e => console.error(e));

How often does it reproduce? Is there a required condition?

Reproduces in all versions of Node tested, 16+.

What is the expected behavior?

For a zero-length string to be able to be used as a source/first argument. Strings with non-zero lengths work correctly.

What do you see instead?

TypeError [ERR_INVALID_ARG_TYPE]: The "source" argument must be of type function or an instance of Stream, Iterable, or AsyncIterable. Received type string ('')
    at Function.pipeline (internal/streams/pipeline.js:202:15)
    at main (REPL8:2:18)
    at REPL10:1:1
    at Script.runInThisContext (vm.js:133:18)
    at REPLServer.defaultEval (repl.js:486:29)
    at bound (domain.js:416:15)
    at REPLServer.runBound [as eval] (domain.js:427:12)
    at REPLServer.onLine (repl.js:819:10)
    at REPLServer.emit (events.js:388:22)
    at REPLServer.emit (domain.js:470:12) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Additional information

Ayase-252 commented 3 years ago

It seems that empty string "" get rejected here, while string is an Iterable by definition.

https://github.com/nodejs/node/blob/910efc2d9a69ac32205d64e4835df481fc057f02/lib/internal/streams/utils.js#L23

Opening a PR to fix.