---
title: CompressionStream
summary: An API for compressing a stream of data.
url: https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/globals/class.CompressionStream/
---

**Kind:** Class  
**Module:** [globals](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/globals/)  
**Package:** @fastly/js-compute 4.0.1  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/globals.d.ts#L1122

An API for compressing a stream of data.

**See also**

- [CompressionStream on MDN](https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream)

````javascript
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

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/globals.d.ts#L1134

```javascript
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.

```js
const gzip = new CompressionStream("gzip");
```

## Properties

### readable

**Type:** `ReadableStream<Uint8Array<ArrayBuffer>>`  
**Flags:** read-only  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/globals.d.ts#L1143

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

### writable

**Type:** `WritableStream<Uint8Array<ArrayBuffer>>`  
**Flags:** read-only  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/globals.d.ts#L1151

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