node-fetch/fetch-blob

Consider exposing intenal `read` method as separate export

Gozala opened this issue · 2 comments

In our code base we abstract between node & browser via function that reads contents as AsyncIterable<Uint8Array>. This library internally has read that pretty much does that, which then is wrapped in a node Readable.

Maybe instead of providing web incompatible stream() method internal read() could be exposed instead ?

Why don't you use native web blob instead?

I have recently been thinking how to make this Deno compatible...
two think need to happen then.

  1. have to change to es6 module so this can be imported into any Deno code using import statement.
  2. make node-stream a optional dependency
stream () {
  if (globalThis.ReadableStream?.from) {
    return ReadableStream.from(read(...))
  } else {
    import('stream').then(({Readable}) => Readable.from(read(...)))
  }
}

Deno and browser have whatwg - ReadableStream


would it help if this used whatwg streams instead of exposing read?

Edit: just saw that you mention something like it in #51

Why don't you use native web blob instead?

We do on the web, but share the same code with node. So we need a common reading function with little to no overhead.

I have recently been thinking how to make this Deno compatible...
two think need to happen then.

👍

  • have to change to es6 module so this can be imported into any Deno code using import statement.
  • make node-stream a optional dependency
stream () {
 if (globalThis.ReadableStream?.from) {
   return ReadableStream.from(read(...))
 } else {
   import('stream').then(({Readable}) => Readable.from(read(...)))
 }
}

Deno and browser have whatwg - ReadableStream

This is more or less what I end up doing in https://github.com/Gozala/web-blob. It does subclass ReadableStream to provide no overhead access to it as AsyncIterable:

https://github.com/Gozala/web-blob/blob/73a00234e9a8ea25a8b300ca6982ec2b843e3a43/src/lib.js#L203-L208