nodejs / readable-stream

Node-core streams for userland
https://nodejs.org/api/stream.html
Other
1.03k stars 227 forks source link

Readable.from(buffer) causes a non-ArrayBufferLike content to be streamed #500

Closed cusspvz closed 1 year ago

cusspvz commented 1 year ago

I'm facing a weird issue with the readable-stream and buffer packages on a specific case. Using both of these packages to ensure the expected behavior happens on both NodeJS and browser environments.

Environment

readable-stream@3.6.0
buffer@6.0.3
// snippet to replicate this case

import { Readable } from "readable-stream";
import { Buffer } from "buffer";
import { createWriteStream } from "fs";
import { finished } from "stream/promises";

const buf = Buffer.from("H4sIAAAAAAACE+2U3Y6DIBCFfRTC3laLCGj7NqMOLbv+BXTb2Pjui15ssk32rjHZDd8Fk5wJzEzgoBszDGhntH08QPUBF0zeXd9FL4QxpoQga/Q8R8Y4FyQVWZZLLlXOCUsFU9Lnox2Y3AjWt/KCIX8M90d40A5apGeKd2iHBmOgB/qJ1pm+8ypLWJJ6RZsGHT0/6NFN5eU6H0d047bEkHhpTbW1dFPrN0lQ+lQWUKSoZcVLxcVJFFAC17JWej3PmdkXFfmyHGiNA3Y1dpXZSixLFNiNX+4z2tP/TOZP/s9kKoL/9+CNjFfjyOpvcgNHKoswYk10b8n2HCpwSL4/h2CYQCAQ+Cd8AeQkjmAADAAA", "base64");
const stream = Readable.from(buf);

finished(stream.pipe(createWriteStream('test'))).then(console.log, console.error);

Expected Behavior

The stream to be consumed by the Buffer's ArrayBuffer

Actual Behavior

The readable-stream pushes a number instead of a Buffer or ArrayLike. (seems like it pushes the number 31)

    TypeError: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received type number (31)

      at Readable.ondata (../../node_modules/readable-stream/lib/_stream_readable.js:681:20)
      at addChunk (../../node_modules/readable-stream/lib/_stream_readable.js:298:12)
      at readableAddChunk (../../node_modules/readable-stream/lib/_stream_readable.js:280:11)
      at Readable.Object.<anonymous>.Readable.push (../../node_modules/readable-stream/lib/_stream_readable.js:241:10)
      at ../../node_modules/readable-stream/lib/internal/streams/from.js:49:29
mcollina commented 1 year ago

Please update to readable-stream v4, this should be fixed.