Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a WriteSteam and WritableStream?

const response = await fetch('https://www.google.com')
response.body.pipeThrough(new TextDecoderStream()).pipeTo(process.stdout)

TypeError [ERR_INVALID_ARG_TYPE]: The "transform.writable" property must be an instance of WritableStream. Received an instance of WriteStream

Questions

  1. What is the difference between Writable and Write steams?
  2. Can the WriteSteam (process.stdout) be converted to a writable stream?

Attempts

I tried to make stdout writable to no avail:

// fails
process.stdout.writable = true
response.body.pipeTo(process.stdout)

Some methods that work include:

// works
const response = await fetch('http://www.google.com')
const reader = response.body.pipeThrough(new TextDecoderStream())
for await (const chunk of reader){
  process.stdout.write(chunk)
}
// works
Readable.fromWeb(response.body).pipe(process.stdout)

I don't exactly understand the difference between a "web" stream and a "node" stream, which I'm guessing are just source and destination, but I thought every I/O stream was able to redirect to each other. It seems like using the Readable.fromWeb works, but what is it doing? It seems inefficient.

Can the reverse be done to process.stdout? Something like...

response.body.pipeTo(new WritableStream(process.stdout)
like image 306
Neo Phyte Avatar asked Sep 20 '25 13:09

Neo Phyte


2 Answers

WriteStream is a stream that is writable to a file.

WritableStream is a stream that is writable to a destination. It can be a file, a socket, a pipe, etc.

like image 194
shawnjb Avatar answered Sep 22 '25 09:09

shawnjb


Can the reverse be done to process.stdout? Something like...

Yes, see Stream.Writable.toWeb.

like image 21
Jan Joneš Avatar answered Sep 22 '25 08:09

Jan Joneš



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!