Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement streams toWeb/fromWeb #2310

Closed
Industrial opened this issue Jun 4, 2022 · 4 comments
Closed

Implement streams toWeb/fromWeb #2310

Industrial opened this issue Jun 4, 2022 · 4 comments

Comments

@Industrial
Copy link
Contributor

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)

@kt3k
Copy link
Member

kt3k commented Jun 5, 2022

We have Duplex.fromWeb #2086, but yes, Readable.fromWeb/toWeb, Writable.fromWeb/toWeb, Duplex.toWeb are still missing

@Industrial
Copy link
Contributor Author

import { from } from 'https://deno.land/std@0.143.0/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.

@kt3k
Copy link
Member

kt3k commented Jun 13, 2022

cc @crowlKats

@cjihrig
Copy link
Contributor

cjihrig commented Sep 13, 2022

Closing as these are now implemented.

@cjihrig cjihrig closed this as completed Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants