@@ -38,6 +38,7 @@ const {
38
38
getSettings,
39
39
getStreamState,
40
40
isPayloadMeaningless,
41
+ kSocket,
41
42
mapToHeaders,
42
43
NghttpError,
43
44
sessionName,
@@ -70,10 +71,10 @@ const kOptions = Symbol('options');
70
71
const kOwner = Symbol ( 'owner' ) ;
71
72
const kProceed = Symbol ( 'proceed' ) ;
72
73
const kProtocol = Symbol ( 'protocol' ) ;
74
+ const kProxySocket = Symbol ( 'proxy-socket' ) ;
73
75
const kRemoteSettings = Symbol ( 'remote-settings' ) ;
74
76
const kServer = Symbol ( 'server' ) ;
75
77
const kSession = Symbol ( 'session' ) ;
76
- const kSocket = Symbol ( 'socket' ) ;
77
78
const kState = Symbol ( 'state' ) ;
78
79
const kType = Symbol ( 'type' ) ;
79
80
@@ -672,6 +673,52 @@ function finishSessionDestroy(self, socket) {
672
673
debug ( `[${ sessionName ( self [ kType ] ) } ] nghttp2session destroyed` ) ;
673
674
}
674
675
676
+ const proxySocketHandler = {
677
+ get ( session , prop ) {
678
+ switch ( prop ) {
679
+ case 'setTimeout' :
680
+ return session . setTimeout . bind ( session ) ;
681
+ case 'destroy' :
682
+ case 'emit' :
683
+ case 'end' :
684
+ case 'once' :
685
+ case 'on' :
686
+ case 'pause' :
687
+ case 'read' :
688
+ case 'resume' :
689
+ case 'write' :
690
+ throw new errors . Error ( 'ERR_HTTP2_NO_SOCKET_MANIPULATION' ) ;
691
+ default :
692
+ const socket = session [ kSocket ] ;
693
+ const value = socket [ prop ] ;
694
+ return typeof value === 'function' ? value . bind ( socket ) : value ;
695
+ }
696
+ } ,
697
+ getPrototypeOf ( session ) {
698
+ return Reflect . getPrototypeOf ( session [ kSocket ] ) ;
699
+ } ,
700
+ set ( session , prop , value ) {
701
+ switch ( prop ) {
702
+ case 'setTimeout' :
703
+ session . setTimeout = value ;
704
+ return true ;
705
+ case 'destroy' :
706
+ case 'emit' :
707
+ case 'end' :
708
+ case 'once' :
709
+ case 'on' :
710
+ case 'pause' :
711
+ case 'read' :
712
+ case 'resume' :
713
+ case 'write' :
714
+ throw new errors . Error ( 'ERR_HTTP2_NO_SOCKET_MANIPULATION' ) ;
715
+ default :
716
+ session [ kSocket ] [ prop ] = value ;
717
+ return true ;
718
+ }
719
+ }
720
+ } ;
721
+
675
722
// Upon creation, the Http2Session takes ownership of the socket. The session
676
723
// may not be ready to use immediately if the socket is not yet fully connected.
677
724
class Http2Session extends EventEmitter {
@@ -707,6 +754,7 @@ class Http2Session extends EventEmitter {
707
754
} ;
708
755
709
756
this [ kType ] = type ;
757
+ this [ kProxySocket ] = null ;
710
758
this [ kSocket ] = socket ;
711
759
712
760
// Do not use nagle's algorithm
@@ -756,7 +804,10 @@ class Http2Session extends EventEmitter {
756
804
757
805
// The socket owned by this session
758
806
get socket ( ) {
759
- return this [ kSocket ] ;
807
+ const proxySocket = this [ kProxySocket ] ;
808
+ if ( proxySocket === null )
809
+ return this [ kProxySocket ] = new Proxy ( this , proxySocketHandler ) ;
810
+ return proxySocket ;
760
811
}
761
812
762
813
// The session type
@@ -957,6 +1008,7 @@ class Http2Session extends EventEmitter {
957
1008
// Disassociate from the socket and server
958
1009
const socket = this [ kSocket ] ;
959
1010
// socket.pause();
1011
+ delete this [ kProxySocket ] ;
960
1012
delete this [ kSocket ] ;
961
1013
delete this [ kServer ] ;
962
1014
@@ -2155,30 +2207,6 @@ function socketDestroy(error) {
2155
2207
this . destroy ( error ) ;
2156
2208
}
2157
2209
2158
- function socketOnResume ( ) {
2159
- if ( this . _paused )
2160
- return this . pause ( ) ;
2161
- if ( this . _handle && ! this . _handle . reading ) {
2162
- this . _handle . reading = true ;
2163
- this . _handle . readStart ( ) ;
2164
- }
2165
- }
2166
-
2167
- function socketOnPause ( ) {
2168
- if ( this . _handle && this . _handle . reading ) {
2169
- this . _handle . reading = false ;
2170
- this . _handle . readStop ( ) ;
2171
- }
2172
- }
2173
-
2174
- function socketOnDrain ( ) {
2175
- const needPause = 0 > this . _writableState . highWaterMark ;
2176
- if ( this . _paused && ! needPause ) {
2177
- this . _paused = false ;
2178
- this . resume ( ) ;
2179
- }
2180
- }
2181
-
2182
2210
// When an Http2Session emits an error, first try to forward it to the
2183
2211
// server as a sessionError; failing that, forward it to the socket as
2184
2212
// a sessionError; failing that, destroy, remove the error listener, and
@@ -2267,9 +2295,6 @@ function connectionListener(socket) {
2267
2295
}
2268
2296
2269
2297
socket . on ( 'error' , socketOnError ) ;
2270
- socket . on ( 'resume' , socketOnResume ) ;
2271
- socket . on ( 'pause' , socketOnPause ) ;
2272
- socket . on ( 'drain' , socketOnDrain ) ;
2273
2298
socket . on ( 'close' , socketOnClose ) ;
2274
2299
2275
2300
// Set up the Session
@@ -2426,9 +2451,6 @@ function connect(authority, options, listener) {
2426
2451
}
2427
2452
2428
2453
socket . on ( 'error' , socketOnError ) ;
2429
- socket . on ( 'resume' , socketOnResume ) ;
2430
- socket . on ( 'pause' , socketOnPause ) ;
2431
- socket . on ( 'drain' , socketOnDrain ) ;
2432
2454
socket . on ( 'close' , socketOnClose ) ;
2433
2455
2434
2456
const session = new ClientHttp2Session ( options , socket ) ;
0 commit comments