@@ -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,48 @@ 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 'pause' :
685
+ case 'read' :
686
+ case 'resume' :
687
+ case 'write' :
688
+ throw new errors . Error ( 'ERR_HTTP2_NO_SOCKET_MANIPULATION' ) ;
689
+ default :
690
+ const socket = session [ kSocket ] ;
691
+ const value = socket [ prop ] ;
692
+ return typeof value === 'function' ? value . bind ( socket ) : value ;
693
+ }
694
+ } ,
695
+ getPrototypeOf ( session ) {
696
+ return Reflect . getPrototypeOf ( session [ kSocket ] ) ;
697
+ } ,
698
+ set ( session , prop , value ) {
699
+ switch ( prop ) {
700
+ case 'setTimeout' :
701
+ session . setTimeout = value ;
702
+ return true ;
703
+ case 'destroy' :
704
+ case 'emit' :
705
+ case 'end' :
706
+ case 'pause' :
707
+ case 'read' :
708
+ case 'resume' :
709
+ case 'write' :
710
+ throw new errors . Error ( 'ERR_HTTP2_NO_SOCKET_MANIPULATION' ) ;
711
+ default :
712
+ session [ kSocket ] [ prop ] = value ;
713
+ return true ;
714
+ }
715
+ }
716
+ } ;
717
+
675
718
// Upon creation, the Http2Session takes ownership of the socket. The session
676
719
// may not be ready to use immediately if the socket is not yet fully connected.
677
720
class Http2Session extends EventEmitter {
@@ -707,6 +750,7 @@ class Http2Session extends EventEmitter {
707
750
} ;
708
751
709
752
this [ kType ] = type ;
753
+ this [ kProxySocket ] = null ;
710
754
this [ kSocket ] = socket ;
711
755
712
756
// Do not use nagle's algorithm
@@ -756,7 +800,10 @@ class Http2Session extends EventEmitter {
756
800
757
801
// The socket owned by this session
758
802
get socket ( ) {
759
- return this [ kSocket ] ;
803
+ const proxySocket = this [ kProxySocket ] ;
804
+ if ( proxySocket === null )
805
+ return this [ kProxySocket ] = new Proxy ( this , proxySocketHandler ) ;
806
+ return proxySocket ;
760
807
}
761
808
762
809
// The session type
@@ -957,6 +1004,7 @@ class Http2Session extends EventEmitter {
957
1004
// Disassociate from the socket and server
958
1005
const socket = this [ kSocket ] ;
959
1006
// socket.pause();
1007
+ delete this [ kProxySocket ] ;
960
1008
delete this [ kSocket ] ;
961
1009
delete this [ kServer ] ;
962
1010
@@ -2155,30 +2203,6 @@ function socketDestroy(error) {
2155
2203
this . destroy ( error ) ;
2156
2204
}
2157
2205
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
2206
// When an Http2Session emits an error, first try to forward it to the
2183
2207
// server as a sessionError; failing that, forward it to the socket as
2184
2208
// a sessionError; failing that, destroy, remove the error listener, and
@@ -2267,9 +2291,6 @@ function connectionListener(socket) {
2267
2291
}
2268
2292
2269
2293
socket . on ( 'error' , socketOnError ) ;
2270
- socket . on ( 'resume' , socketOnResume ) ;
2271
- socket . on ( 'pause' , socketOnPause ) ;
2272
- socket . on ( 'drain' , socketOnDrain ) ;
2273
2294
socket . on ( 'close' , socketOnClose ) ;
2274
2295
2275
2296
// Set up the Session
@@ -2426,9 +2447,6 @@ function connect(authority, options, listener) {
2426
2447
}
2427
2448
2428
2449
socket . on ( 'error' , socketOnError ) ;
2429
- socket . on ( 'resume' , socketOnResume ) ;
2430
- socket . on ( 'pause' , socketOnPause ) ;
2431
- socket . on ( 'drain' , socketOnDrain ) ;
2432
2450
socket . on ( 'close' , socketOnClose ) ;
2433
2451
2434
2452
const session = new ClientHttp2Session ( options , socket ) ;
0 commit comments