@@ -3,7 +3,8 @@ import * as http from 'http'
3
3
import * as http2 from 'http2'
4
4
import { EventEmitter } from 'events'
5
5
import type * as https from 'https'
6
- import { type Http2Agent , HTTP2ClientRequestOptions , globalAgent } from './http2Agent'
6
+ import { HTTP2ClientRequestOptions , globalAgent } from './http2Agent'
7
+ import * as tls from "tls" ;
7
8
8
9
export interface RequestOptions extends Omit < https . RequestOptions , 'agent' | 'createConnection' | 'protocol' > {
9
10
agent ?: {
@@ -22,6 +23,8 @@ export class Http2Request extends EventEmitter {
22
23
stream : http2 . ClientHttp2Stream
23
24
private readonly _client : http2 . ClientHttp2Session
24
25
requestHeaders : http2 . OutgoingHttpHeaders = { } ;
26
+ connectionHeaders = [ 'connection' , 'host' ]
27
+ socket : tls . TLSSocket
25
28
26
29
constructor ( options : RequestOptions ) {
27
30
super ( )
@@ -51,12 +54,16 @@ export class Http2Request extends EventEmitter {
51
54
[ http2 . constants . HTTP2_HEADER_AUTHORITY ] : newoptions . host ,
52
55
...headers
53
56
}
57
+ this . socket = this . _client . socket as tls . TLSSocket ;
54
58
55
- this . requestHeaders = Object . fromEntries ( Object . entries ( this . requestHeaders ) . filter ( ( [ key ] ) => ! ( options . blacklistHeaders ?? [ ] ) . includes ( key . toLowerCase ( ) ) ) )
56
-
57
- // Remove blacklisted http/1 headers
58
- delete this . requestHeaders . Connection
59
- delete this . requestHeaders . Host
59
+ this . requestHeaders = Object . fromEntries (
60
+ Object . entries ( this . requestHeaders )
61
+ . map ( ( [ key , value ] ) => ( [ key . toLowerCase ( ) , value ] ) )
62
+ // @ts -ignore
63
+ . filter ( ( [ key ] ) => ! ( options . blacklistHeaders ?? [ ] ) . includes ( key ) )
64
+ // @ts -ignore
65
+ . filter ( ( [ key ] ) => ! this . connectionHeaders . includes ( key ) )
66
+ )
60
67
61
68
this . stream = this . _client . request ( this . requestHeaders )
62
69
this . registerListeners ( )
@@ -80,20 +87,14 @@ export class Http2Request extends EventEmitter {
80
87
this . stream . on ( 'close' , ( ...args ) => {
81
88
this . emit ( 'close' , ...args )
82
89
} )
83
- this . stream . on ( 'socket' , ( ) => this . emit ( 'socket' , this . _client . socket ) )
90
+
84
91
this . _client . once ( 'error' , this . onError )
85
92
this . stream . on ( 'response' , ( response ) => {
86
93
this . emit ( 'response' , new ResponseProxy ( response , this ) )
87
-
88
- // HTTP response events returns a readable stream which has data event that consumers listen on to get the response body
89
- // With HTTP2 the response object is a header object and the body is streamed via the data event.
90
- // To maintain compatibilty with the HTTP API, we need to emit the data after the response event is emitted
91
- // And wait for the data listeners to be attached
92
94
} )
93
- //
95
+
94
96
this . stream . on ( 'end' , ( ) => {
95
97
this . _client . off ( 'error' , this . onError )
96
- this . emit ( 'end' )
97
98
} )
98
99
}
99
100
@@ -120,7 +121,7 @@ export class Http2Request extends EventEmitter {
120
121
121
122
on ( eventName : string | symbol , listener : ( ...args : any [ ] ) => void ) : this {
122
123
if ( eventName === 'socket' ) {
123
- listener ( this . _client . socket )
124
+ listener ( this . socket )
124
125
return this
125
126
}
126
127
@@ -164,7 +165,6 @@ class ResponseProxy extends EventEmitter {
164
165
}
165
166
166
167
registerRequestListeners ( ) {
167
- this . req . stream . on ( 'end' , ( ) => this . emit ( 'end' ) )
168
168
this . req . stream . on ( 'error' , ( e ) => this . emit ( 'error' , e ) )
169
169
this . req . stream . on ( 'close' , ( ) => {
170
170
this . emit ( 'close' )
@@ -183,6 +183,10 @@ class ResponseProxy extends EventEmitter {
183
183
this . emit ( 'data' , chunk )
184
184
} )
185
185
}
186
+
187
+ if ( eventName === 'end' ) {
188
+ this . req . stream . on ( 'end' , listener )
189
+ }
186
190
return this
187
191
}
188
192
@@ -209,6 +213,7 @@ class ResponseProxy extends EventEmitter {
209
213
}
210
214
211
215
pause ( ) {
216
+ console . log ( performance . now ( ) , 'pausing' )
212
217
this . req . stream . pause ( )
213
218
}
214
219
@@ -223,4 +228,8 @@ class ResponseProxy extends EventEmitter {
223
228
setEncoding ( encoding : BufferEncoding ) {
224
229
this . req . stream . setEncoding ( encoding )
225
230
}
231
+
232
+ destroy ( ) {
233
+ this . req . stream . destroy ( ) ;
234
+ }
226
235
}
0 commit comments