An API for compressing a stream of data.

See

CompressionStream on MDN

Example

In this example a request is made to httpbin.org/html and the response body is compressed using gzip compression.

View this example on Fiddle.

async function app(event) {
const req = event.request;
const backendResponse = await fetch("https://httpbin.org/html", { backend: "origin_0" });
if (!backendResponse.body) {
return backendResponse;
}
let resp = new Response(backendResponse.body.pipeThrough(new CompressionStream("gzip")), backendResponse);
resp.headers.set("Content-Encoding", "gzip");
return resp;
}
addEventListener("fetch", event => event.respondWith(app(event)));

Hierarchy

  • CompressionStream

Constructors

Properties

Constructors

  • Creates a new CompressionStream object which compresses a stream of data.

    Throws

    Throws a TypeError if the format passed to the constructor is not supported.

    Example

    const gzip = new CompressionStream("gzip");
    

    Parameters

    • format: "deflate" | "deflate-raw" | "gzip"

      The compression format to use.

    Returns CompressionStream

Properties

Example

let stream = new CompressionStream("gzip");
console.log(stream.readable instanceof ReadableStream); // true

Example

let stream = new CompressionStream("gzip");
console.log(stream.writable instanceof WritableStream); // true

Generated using TypeDoc