88} from '@sentry/types' ;
99import * as http from 'http' ;
1010import * as https from 'https' ;
11+ import { Readable } from 'stream' ;
1112import { URL } from 'url' ;
13+ import { createGzip } from 'zlib' ;
1214
1315import { 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