oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.19k stars 2.68k forks source link

readline doesn't fully support node:stream Readable as input #2596

Open controversial opened 1 year ago

controversial commented 1 year ago

What version of Bun is running?

0.6.0 (latest canary)

What platform is your computer?

Darwin 22.4.0 arm64 arm

What steps can reproduce the bug?

import readline from 'readline';
import { Readable } from 'stream';

const path = new URL('path/to/large/file.json', import.meta.url);
const stream = Bun.file(path).stream();
const rl = readline.createInterface({ input: Readable.fromWeb(stream) });

console.log(rl);

What is the expected behavior?

Readline object is created successfully

What do you see instead?

A TypeError undefined is not an object (evaluating 'this[kDecoder].write') is logged to the console from inside node:readline literally thousands of times (in my test, 20k+ lines of output):

Screenshot 2023-04-08 at 4 56 30 PM

Additional information

This error reproduces on a large (~18MB) file for me, but not on a small (~2KB) file.

Using fs.createReadStream(path) instead of Readable.fromWeb(Bun.file(path).stream()), this error does not appear.

paperdave commented 1 year ago

This isnt a bug with large files but the fact of using a Readable at all, though fs.createReadStream is exempt from this:

import { Readable } from "stream";
import readline from "readline";

const readable = new Readable({
  read() {
    this.push("hello world\n");
    this.push("hello world\n");
    this.push(null);
  },
});

const rl = readline.createInterface({
  input: readable,
  crlfDelay: Infinity,
});

rl.on("line", (line) => {
  console.log({ line });
});
controversial commented 1 year ago

I thought I’d tested it and been able to successfully use Readable on smaller files, but now I can’t remember for sure

paperdave commented 1 year ago

that's interesting. i get the same error you have so unsure if things changed or not

lwears commented 1 year ago

I am also having this problem on a small 1.8kb text file.