deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

Implement streams toWeb/fromWeb

Open Industrial opened this issue 2 years ago • 3 comments

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)

Industrial avatar Jun 04 '22 20:06 Industrial

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

kt3k avatar Jun 05 '22 12:06 kt3k

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.

Industrial avatar Jun 12 '22 19:06 Industrial

cc @crowlKats

kt3k avatar Jun 13 '22 05:06 kt3k

Closing as these are now implemented.

cjihrig avatar Sep 13 '22 22:09 cjihrig