Skip to content
This repository was archived by the owner on Oct 23, 2025. It is now read-only.

Commit f8b777e

Browse files
authored
feat: Compress request body using gzip (#9)
2 parents 9155074 + 6d1c6fe commit f8b777e

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

src/hooks/compression.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { RequestInput } from "../lib/http";
2+
import { BeforeCreateRequestContext, BeforeCreateRequestHook } from "./types";
3+
4+
export class CompressionHook implements BeforeCreateRequestHook {
5+
beforeCreateRequest(_hookCtx: BeforeCreateRequestContext, input: RequestInput): RequestInput {
6+
const hdrs = new Headers(input.options?.headers ?? {});
7+
const body = input.options?.body;
8+
9+
// We'll need to handle streaming requests differently (per message).
10+
// Currently we only handle unary JSON requests.
11+
if (!hdrs.get("content-type")?.toLowerCase().startsWith("application/json") || typeof body !== "string") {
12+
return input;
13+
}
14+
15+
const stream = new Blob([body]).stream();
16+
const compressedStream = stream.pipeThrough(new CompressionStream("gzip"));
17+
18+
// Set the content encoding.
19+
hdrs.set("content-encoding", "gzip");
20+
21+
const opts = {
22+
...input.options,
23+
body: compressedStream,
24+
headers: hdrs,
25+
duplex: "half", // use HTTP2 because of stream
26+
};
27+
28+
return {
29+
...input,
30+
options: opts,
31+
}
32+
}
33+
}

src/hooks/registration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CompressionHook } from "./compression.js";
12
import { Hooks } from "./types.js";
23

34
/*
@@ -6,9 +7,9 @@ import { Hooks } from "./types.js";
67
* in this file or in separate files in the hooks folder.
78
*/
89

9-
// @ts-expect-error remove this line when you add your first hook and hooks is used
1010
export function initHooks(hooks: Hooks) {
1111
// Add hooks by calling hooks.register{ClientInit/BeforeCreateRequest/BeforeRequest/AfterSuccess/AfterError}Hook
1212
// with an instance of a hook that implements that specific Hook interface
1313
// Hooks are registered per SDK instance, and are valid for the lifetime of the SDK instance
14+
hooks.registerBeforeCreateRequestHook(new CompressionHook());
1415
}

src/index.extras.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export type S2OperationConfig = {
9191
export enum AppendRetryPolicy {
9292
/**
9393
* Retry all eligible failures encountered during an append.
94-
* This could result in append batches being duplicated on the stream.
94+
* This could result in append batches being duplicated on the stream.
9595
*/
9696
All,
9797
/**
@@ -160,7 +160,7 @@ class S2Account {
160160
this.config.httpClient?.addHook("beforeRequest", (request) => {
161161
if (config?.authToken !== undefined) {
162162
request.headers.set("Authorization", `Bearer ${config.authToken}`);
163-
}
163+
}
164164
});
165165
}
166166

@@ -269,7 +269,7 @@ class S2Basin {
269269
this.config.httpClient?.addHook("beforeRequest", (request) => {
270270
if (config?.authToken !== undefined) {
271271
request.headers.set("Authorization", `Bearer ${config.authToken}`);
272-
}
272+
}
273273
});
274274
}
275275

@@ -353,7 +353,7 @@ class Stream {
353353
this.config.httpClient?.addHook("beforeRequest", (request) => {
354354
if (config?.authToken !== undefined) {
355355
request.headers.set("Authorization", `Bearer ${config.authToken}`);
356-
}
356+
}
357357
});
358358
}
359359

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"declarationMap": true,
1616
"sourceMap": true,
1717
"outDir": ".",
18-
1918

2019
// https://github.com/tsconfig/bases/blob/a1bf7c0fa2e094b068ca3e1448ca2ece4157977e/bases/strictest.json
2120
"strict": true,

0 commit comments

Comments
 (0)