holepunchto / hypercore

Hypercore is a secure, distributed append-only log.
https://docs.holepunch.to
MIT License
2.57k stars 183 forks source link

pipe hypercore.replicate from webReadableStream to webWritableStream #329

Open sce9sc opened 1 year ago

sce9sc commented 1 year ago

First of all , It is the most amazing project I have ever come across.

I was wondering how can I pipe the hypercore.replicate stream which is a duplex Node.js stream to a webReabable and a webWritable streams in the browser.

I have two distinct streams . A webReadable and a webWritable . Since there is no duplex stream for web does anyone know how to do it or at least point me to the right direction.

Thank you in advance.

sce9sc commented 1 year ago

In node I have accomplished as shown below:

 const { ReadableStream, WritableStream } = require('node:stream/web');
 const streamNode = require('node:stream');

  session.ReadableStream = ReadableStream;
  session.WritableStream = WritableStream;
  let duplex = streamNode.Duplex.fromWeb({
    readable: session.getReadableStream(),
    writable: session.getWritableStream(),
  });

const repStream = core.replicate(true,{ keepAlive: false})
  duplex.pipe(repStream).pipe(duplex)