Skip to content

Commit 6398d06

Browse files
committed
🗜
1 parent 152e3c5 commit 6398d06

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

‎packages/node/src/transports/http-module.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { IncomingHttpHeaders, RequestOptions as HTTPRequestOptions } from 'http';
22
import { RequestOptions as HTTPSRequestOptions } from 'https';
3+
import { Writable } from 'stream';
34
import { URL } from 'url';
45

56
export type HTTPModuleRequestOptions = HTTPRequestOptions | HTTPSRequestOptions | string | URL;
@@ -15,15 +16,6 @@ export interface HTTPModuleRequestIncomingMessage {
1516
setEncoding(encoding: string): void;
1617
}
1718

18-
/**
19-
* Cut version of http.ClientRequest.
20-
* Some transports work in a special Javascript environment where http.IncomingMessage is not available.
21-
*/
22-
export interface HTTPModuleClientRequest {
23-
end(chunk: string | Uint8Array): void;
24-
on(event: 'error', listener: () => void): void;
25-
}
26-
2719
/**
2820
* Internal used interface for typescript.
2921
* @hidden
@@ -34,10 +26,7 @@ export interface HTTPModule {
3426
* @param options These are {@see TransportOptions}
3527
* @param callback Callback when request is finished
3628
*/
37-
request(
38-
options: HTTPModuleRequestOptions,
39-
callback?: (res: HTTPModuleRequestIncomingMessage) => void,
40-
): HTTPModuleClientRequest;
29+
request(options: HTTPModuleRequestOptions, callback?: (res: HTTPModuleRequestIncomingMessage) => void): Writable;
4130

4231
// This is the type for nodejs versions that handle the URL argument
4332
// (v10.9.0+), but we do not use it just yet because we support older node

‎packages/node/src/transports/http.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
} from '@sentry/types';
99
import * as http from 'http';
1010
import * as https from 'https';
11+
import { Readable } from 'stream';
1112
import { URL } from 'url';
13+
import { createGzip } from 'zlib';
1214

1315
import { HTTPModule } from './http-module';
1416

@@ -23,6 +25,22 @@ export interface NodeTransportOptions extends BaseTransportOptions {
2325
httpModule?: HTTPModule;
2426
}
2527

28+
// Estimated maximum size for reasonable standalone event
29+
const GZIP_THRESHOLD = 1024 * 32;
30+
31+
/**
32+
* Gets a stream from a Uint8Array or string
33+
* Readable.from is ideal but was added in node.js v12.3.0 and v10.17.0
34+
*/
35+
function streamFromBody(body: Uint8Array | string): Readable {
36+
return new Readable({
37+
read() {
38+
this.push(body);
39+
this.push(null);
40+
},
41+
});
42+
}
43+
2644
/**
2745
* Creates a Transport that uses native the native 'http' and 'https' modules to send events to Sentry.
2846
*/
@@ -85,6 +103,17 @@ function createRequestExecutor(
85103
const { hostname, pathname, port, protocol, search } = new URL(options.url);
86104
return function makeRequest(request: TransportRequest): Promise<TransportMakeRequestResponse> {
87105
return new Promise((resolve, reject) => {
106+
let body = streamFromBody(request.body);
107+
108+
if (request.body.length > GZIP_THRESHOLD) {
109+
options.headers = {
110+
...options.headers,
111+
'content-encoding': 'gzip',
112+
};
113+
114+
body = body.pipe(createGzip());
115+
}
116+
88117
const req = httpModule.request(
89118
{
90119
method: 'POST',
@@ -123,7 +152,7 @@ function createRequestExecutor(
123152
);
124153

125154
req.on('error', reject);
126-
req.end(request.body);
155+
body.pipe(req);
127156
});
128157
};
129158
}

0 commit comments

Comments
 (0)