This repository was archived by the owner on Oct 23, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +39
-6
lines changed
Expand file tree Collapse file tree 4 files changed +39
-6
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ import { CompressionHook } from "./compression.js" ;
12import { 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
1010export 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}
Original file line number Diff line number Diff line change @@ -91,7 +91,7 @@ export type S2OperationConfig = {
9191export 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
Original file line number Diff line number Diff line change 1515 "declarationMap" : true ,
1616 "sourceMap" : true ,
1717 "outDir" : " ." ,
18-
1918
2019 // https://github.com/tsconfig/bases/blob/a1bf7c0fa2e094b068ca3e1448ca2ece4157977e/bases/strictest.json
2120 "strict" : true ,
You can’t perform that action at this time.
0 commit comments