deno_std
deno_std copied to clipboard
Implement streams toWeb/fromWeb
Is your feature request related to a problem? Please describe.
I want to convert from Node.js streams to Web streams and back. Node.js has the methods toWeb
and fromWeb
to do this.
Describe the solution you'd like
Implement these functions.
Describe alternatives you've considered
I tried implementing the helper functions but got stuck because .pipe()
didn't work. (https://gist.github.com/Industrial/f67723dcb6a2b4b576e7f06707c2ad94)
We have Duplex.fromWeb https://github.com/denoland/deno_std/pull/2086, but yes, Readable.fromWeb/toWeb, Writable.fromWeb/toWeb, Duplex.toWeb are still missing
import { from } from 'https://deno.land/[email protected]/node/internal/streams/readable.mjs';
const nodeReadableStreamToWebReadableStream = (
inputStream: NodeJS.ReadableStream,
): ReadableStream => {
const outputStream = new TransformStream();
const outputStreamWritableWriter = outputStream.writable.getWriter();
(async () => {
for await (const chunk of inputStream) {
outputStreamWritableWriter.write(chunk);
}
})().then(() => {
outputStreamWritableWriter.close();
}).catch((error) => {
outputStreamWritableWriter.abort(error);
});
return outputStream.readable;
};
const webReadableStreamToNodeReadableStream = (
inputStream: ReadableStream,
): NodeJS.ReadableStream => {
return from(inputStream);
};
This worked for me but it's probably not how you'd want to implement it.
cc @crowlKats
Closing as these are now implemented.