| 658 | |
| 659 | // Compressor failures, including a runtime without `deflate-raw`, are defects |
| 660 | const deflate = <E, R>(input: Stream.Stream<Uint8Array, E, R>): Stream.Stream<Uint8Array, E, R> => |
| 661 | Stream.unwrap(Effect.map( |
| 662 | Effect.sync(() => new CompressionStream("deflate-raw")), |
| 663 | (compressor) => { |
| 664 | const writer = compressor.writable.getWriter() |
| 665 | const feed = Stream.runForEach( |
| 666 | input, |
| 667 | (chunk) => Effect.promise(() => writer.write(compressible(chunk))) |
| 668 | ).pipe( |
| 669 | Effect.andThen(Effect.promise(() => writer.close())), |
| 670 | Effect.onError(() => |
| 671 | Effect.sync(() => { |
| 672 | writer.abort().catch(() => {}) |
| 673 | }) |
| 674 | ) |
| 675 | ) |
| 676 | const output = Stream.orDie(Stream.fromReadableStream({ |
| 677 | evaluate: () => compressor.readable, |
| 678 | onError: (cause) => cause |
| 679 | })) |
| 680 | return Stream.merge(output, Stream.fromEffectDrain(feed)) |
| 681 | } |
| 682 | )) |
| 683 | |
| 684 | // CompressionStream rejects views of a SharedArrayBuffer |
| 685 | const compressible = (bytes: Uint8Array): Uint8Array<ArrayBuffer> => |