summaryrefslogtreecommitdiff
path: root/demo/components/code-box/stream-adapter.tsx
blob: a97d2ede4643362f53213a00851c1c97adb8d4a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { AsyncDeflate } from '../../..';
export default (stream: AsyncDeflate) => {
  const writable = new WritableStream({
    write(dat: Uint8Array) { stream.push(dat); },
    close() { stream.push(new Uint8Array(0), true); }
  });
  const readable = new ReadableStream({
    start(controller: ReadableStreamDefaultController<Uint8Array>) {
      stream.ondata = (err, chunk, final) => {
        if (err) writable.abort(err.message);
        controller.enqueue(chunk);
        if (final) controller.close();
      }
    }
  });
  return { readable, writable };
}