8
8
} from '@sentry/types' ;
9
9
import * as http from 'http' ;
10
10
import * as https from 'https' ;
11
+ import { Readable } from 'stream' ;
11
12
import { URL } from 'url' ;
13
+ import { createGzip } from 'zlib' ;
12
14
13
15
import { HTTPModule } from './http-module' ;
14
16
@@ -23,6 +25,22 @@ export interface NodeTransportOptions extends BaseTransportOptions {
23
25
httpModule ?: HTTPModule ;
24
26
}
25
27
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
+
26
44
/**
27
45
* Creates a Transport that uses native the native 'http' and 'https' modules to send events to Sentry.
28
46
*/
@@ -85,6 +103,17 @@ function createRequestExecutor(
85
103
const { hostname, pathname, port, protocol, search } = new URL ( options . url ) ;
86
104
return function makeRequest ( request : TransportRequest ) : Promise < TransportMakeRequestResponse > {
87
105
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
+
88
117
const req = httpModule . request (
89
118
{
90
119
method : 'POST' ,
@@ -123,7 +152,7 @@ function createRequestExecutor(
123
152
) ;
124
153
125
154
req . on ( 'error' , reject ) ;
126
- req . end ( request . body ) ;
155
+ body . pipe ( req ) ;
127
156
} ) ;
128
157
} ;
129
158
}
0 commit comments