10LOC

#web-streams

Web Platformintermediate

CompressionStream for browser-side gzip

export const gzipEncode = async (text: string): Promise<Uint8Array> => {
  const stream = new Blob([text]).stream().pipeThrough(new CompressionStream("gzip"));
  const buffer = await new Response(stream).arrayBuffer();
  return new Uint8Array(buffer);
};

Compress and decompress data in the browser with native Web Streams APIs.

Node.jsadvanced

Bridge Node streams to web streams

import { createReadStream } from "node:fs";
import { Readable } from "node:stream";

export const compressedFileResponse = (path: string) => {
  const nodeStream = createReadStream(path, { highWaterMark: 64 * 1024 });

Turn a Node Readable into a web-compatible stream with Readable.toWeb().