bun
bun copied to clipboard
`readline` not available
Trying to use spinnies as a dependency:
error: Could not resolve: "readline". Maybe you need to "bun install"?
const readline = require('readline');
/Users/{OMITTED}/node_modules/spinnies/index.js:3:26 40%
Feel free to open a separate issue for readline - from here
If a JS implementation is possible/acceptable based on streams I could try to help with that. I'm too unfamiliar with Zig to do anything else unfortunately...
This should be possible now actually
It would use Web Streams, but you could wrap that. You would do Bun.stdin.stream(), which returns a ReadableStream and then you read from that with readline. It would need a polyfill to work with node streams.
Okay, let me have a look at Bun to see if I can find where it'd be good to inject the JS module readline, I'll try to come back at this tonight, thanks! :)
You can implement separately just to test and then once it works, it can be added to the repo
On Wed, Jul 6, 2022 at 6:28 PM Francisco Presencia @.***> wrote:
Okay, let me have a look at Bun to see if I can find where it'd be good to inject the JS module readline, I'll try to come back at this tonight, thanks! :)
— Reply to this email directly, view it on GitHub https://github.com/Jarred-Sumner/bun/issues/311#issuecomment-1176940937, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFNGSYFGAKP6HSMXQP3SD3VSYXERANCNFSM523QDMFQ . You are receiving this because you commented.Message ID: @.***>
Probably a silly question, but is there any update on this issue? I use readline as part of my project to provide a custom command interface and really need keypress events, but so far have been unable to implement.
Really excited to be using Bun btw, great work everyone!
Started working on readline-polyfill here:
https://github.com/franciscop/readline-polyfill
It's still pretty basic but it can run some of the Node.js examples. I wanted to check in the direction before committing many more hours. WDYT @Jarred-Sumner? Does it look like the right direction to move forward? It depends on node:events and node:tty (stdin/stdout) indirectly. Few of the examples already work, but it's lacking some events and many options.
How about the promises node:readline/promises? I thought of putting those just in a promises.js file and then put the proper exports field in package.json.
It would use Web Streams, but you could wrap that. You would do Bun.stdin.stream(), which returns a ReadableStream and then you read from that with readline. It would need a polyfill to work with node streams.
Agreed, but so far that's not been part of readline, but an argument to readline as input. Readline is pretty agnostic about that IMHO. It'll still need the polyfill of stdin/out for the examples to run raw with bun, since bun doesn't use Node Streams for process.stdin nor process.stdout.
Any progress on this? Can we add readline-polyfill to Bun? Ro add an alias to Bun config to avoid such an error:
error: Could not resolve: "readline". Maybe you need to "bun install"?
const readline = require('readline');
And keep compatibility with node.js
Yes, unfortunately I am a bit stuck here. I found two issues so far, which I believe are probably the same as what was already mentioned by @Jarred-Sumner:
console.log(process.stdin, process.stdout);
// undefined undefined
fs.createReadStream("./input.txt"),
// SystemError
It would use Web Streams, but you could wrap that. You would do Bun.stdin.stream(), which returns a ReadableStream and then you read from that with readline. It would need a polyfill to work with node streams.
I believe the conversion from WebStreams => NodeStream is going to be a lot trickier than this attempt for a readline polyfill. I will attempt it at some point, but I'm not too confident on me being capable of doing it.
Ideally we should polyfill Bun.stdin => process.stdin and Bun.stdout => process.stdout so that any other program using those works by default as well.
For reference, I found 3 major parts on the readline core library to be implemented:
- Low-level stdin/stdout manipulation. This would needs the NodeStreams mentioned above. I believe I implemented this part on itself, but now need to implement the NodeStreams polyfill for the examples.
- Some helpers built on top of these, like
prompt()andquestion(). These should be easy to make once the low-level part is fixed! - Some methods that call the underlying
WriteStreamfor a TTY terminal. This would be the hardest for me, since you probably need to know your TTY terminal pretty well and how to do these things manually. Even googling this is difficult because it tells you to usereadlineor theTTY WriteStream, which is what we are trying to implement here. This are things like moving the cursor to a specific point in the terminal, clear the line, clear the screen, etc.
(I believe I could, given enough time, polyfill the part 1 of the WebStream => NodeStream for the low level operations, but the part 3 is the one that I think I would not be able to do).
puppeteerjs package also depends on this one.
I thought I'd be able to hack in runtime, yet Bun resolves requires during packaging during bun run I suppose, so no runtime hacks..
But process.stdin and Bun.stdin.stream() are not the same, the former is a NodeStream and the latter a WebStream. So you'd need to at least convert those to NodeStreams for them to work properly.
I'm working on a compatibility layer there, I have been able to make the ReadStream work but when I try this:
console.log(Bun.stdin.stream(), Bun.stdout.stream());
// ReadableStream { } ReadableStream { }
I expected the second one to be WritableStream and I'm confused why it's a ReadableStream instead! I am still in [email protected] FWIW
@franciscop I suppose those should be ReadStream and WriteStream, not ReadableStream and WritableStream
@franciscop I would also be able to handle both NodeStream and WebStream in library, yet I can't use Bun as long as there is a require('readline') in the app, don't know how to make it ignore that
Ah sorry if it wasn't clear, this polyfill is not a drop-in. I understand I'm writing it as a separated project, and if I get it working to certain reliability then I'll be able to add the code to bun itself somehow, which means you wouldn't need to do anything until this is working and merged (you can help in the repo though!).
ReadStream doesn't exist. createReadStream() creates a Readable instance (on Node.js, or "NodeStreams" for simplicity), see types of streams. While in WebStreams they are called ReadableStream and WritableStream. I am not sure what you mean exactly with this sentence then, so could you please clarify?
@franciscop I suppose those should be ReadStream and WriteStream, not ReadableStream and WritableStream
@Jarred-Sumner sorry I'm going to need a bit of guidance here. Do you know why this happens?
console.log(Bun.stdin.stream(), Bun.stdout.stream());
// ReadableStream { } ReadableStream { }
I need to access the stdout WritableStream, but somehow with .stream() it returns a ReadableStream instead. Do you know why this might be? And how can I access the WritableStream from Bun,stdout?
Both stdin and stdout are defined in bun-types as FileBlob, which is read-only. Also, I couldn't find a way to pipe a ReadableStream to a file (like Node's createWriteStream)
This is fixed by @ThatOneBro