CompressionStream

Classin globals@fastly/js-compute 4.0.0View source

An API for compressing a stream of data.

See also

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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](https://fiddle.fastly.dev/fiddle/c1326776).
```js
async function app(event) {
const req = event.request;
const backendResponse = await fetch("https://httpbin.org/html", { backend: "my-backend" });
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)));
```

Constructor

new CompressionStream(format: 'deflate' | 'deflate-raw' | 'gzip')

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

Parameters

format: 'deflate' | 'deflate-raw' | 'gzip'

The compression format to use.

Throws

  • Throws a TypeError if the format passed to the constructor is not supported.
const gzip = new CompressionStream("gzip");

Properties

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