@@ -38,6 +38,7 @@ const {
3838 getSettings,
3939 getStreamState,
4040 isPayloadMeaningless,
41+ kSocket,
4142 mapToHeaders,
4243 NghttpError,
4344 sessionName,
@@ -70,10 +71,10 @@ const kOptions = Symbol('options');
7071const kOwner = Symbol ( 'owner' ) ;
7172const kProceed = Symbol ( 'proceed' ) ;
7273const kProtocol = Symbol ( 'protocol' ) ;
74+ const kProxySocket = Symbol ( 'proxy-socket' ) ;
7375const kRemoteSettings = Symbol ( 'remote-settings' ) ;
7476const kServer = Symbol ( 'server' ) ;
7577const kSession = Symbol ( 'session' ) ;
76- const kSocket = Symbol ( 'socket' ) ;
7778const kState = Symbol ( 'state' ) ;
7879const kType = Symbol ( 'type' ) ;
7980
@@ -672,6 +673,48 @@ function finishSessionDestroy(self, socket) {
672673 debug ( `[${ sessionName ( self [ kType ] ) } ] nghttp2session destroyed` ) ;
673674}
674675
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+
675718// Upon creation, the Http2Session takes ownership of the socket. The session
676719// may not be ready to use immediately if the socket is not yet fully connected.
677720class Http2Session extends EventEmitter {
@@ -707,6 +750,7 @@ class Http2Session extends EventEmitter {
707750 } ;
708751
709752 this [ kType ] = type ;
753+ this [ kProxySocket ] = null ;
710754 this [ kSocket ] = socket ;
711755
712756 // Do not use nagle's algorithm
@@ -756,7 +800,10 @@ class Http2Session extends EventEmitter {
756800
757801 // The socket owned by this session
758802 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 ;
760807 }
761808
762809 // The session type
@@ -957,6 +1004,7 @@ class Http2Session extends EventEmitter {
9571004 // Disassociate from the socket and server
9581005 const socket = this [ kSocket ] ;
9591006 // socket.pause();
1007+ delete this [ kProxySocket ] ;
9601008 delete this [ kSocket ] ;
9611009 delete this [ kServer ] ;
9621010
@@ -2155,30 +2203,6 @@ function socketDestroy(error) {
21552203 this . destroy ( error ) ;
21562204}
21572205
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-
21822206// When an Http2Session emits an error, first try to forward it to the
21832207// server as a sessionError; failing that, forward it to the socket as
21842208// a sessionError; failing that, destroy, remove the error listener, and
@@ -2267,9 +2291,6 @@ function connectionListener(socket) {
22672291 }
22682292
22692293 socket . on ( 'error' , socketOnError ) ;
2270- socket . on ( 'resume' , socketOnResume ) ;
2271- socket . on ( 'pause' , socketOnPause ) ;
2272- socket . on ( 'drain' , socketOnDrain ) ;
22732294 socket . on ( 'close' , socketOnClose ) ;
22742295
22752296 // Set up the Session
@@ -2426,9 +2447,6 @@ function connect(authority, options, listener) {
24262447 }
24272448
24282449 socket . on ( 'error' , socketOnError ) ;
2429- socket . on ( 'resume' , socketOnResume ) ;
2430- socket . on ( 'pause' , socketOnPause ) ;
2431- socket . on ( 'drain' , socketOnDrain ) ;
24322450 socket . on ( 'close' , socketOnClose ) ;
24332451
24342452 const session = new ClientHttp2Session ( options , socket ) ;
0 commit comments