@@ -1829,6 +1829,33 @@ void Http2Session::Consume(Local<Object> stream_obj) {
18291829 Debug (this , " i/o stream consumed" );
18301830}
18311831
1832+ // Allow injecting of data from JS
1833+ // This is used when the socket has already some data received
1834+ // before our listener was attached
1835+ // https://github.com/nodejs/node/issues/35475
1836+ void Http2Session::Receive (const FunctionCallbackInfo<Value>& args) {
1837+ Http2Session* session;
1838+ ASSIGN_OR_RETURN_UNWRAP (&session, args.Holder ());
1839+ CHECK (args[0 ]->IsObject ());
1840+
1841+ ArrayBufferViewContents<char > buffer (args[0 ]);
1842+ const char * data = buffer.data ();
1843+ size_t len = buffer.length ();
1844+ Debug (session, " Receiving %zu bytes injected from JS" , len);
1845+
1846+ // Copy given buffer
1847+ while (len > 0 ) {
1848+ uv_buf_t buf = session->OnStreamAlloc (len);
1849+ size_t copy = buf.len > len ? len : buf.len ;
1850+ memcpy (buf.base , data, copy);
1851+ buf.len = copy;
1852+ session->OnStreamRead (copy, buf);
1853+
1854+ data += copy;
1855+ len -= copy;
1856+ }
1857+ }
1858+
18321859Http2Stream* Http2Stream::New (Http2Session* session,
18331860 int32_t id,
18341861 nghttp2_headers_category category,
@@ -3054,6 +3081,7 @@ void Initialize(Local<Object> target,
30543081 env->SetProtoMethod (session, " altsvc" , Http2Session::AltSvc);
30553082 env->SetProtoMethod (session, " ping" , Http2Session::Ping);
30563083 env->SetProtoMethod (session, " consume" , Http2Session::Consume);
3084+ env->SetProtoMethod (session, " receive" , Http2Session::Receive);
30573085 env->SetProtoMethod (session, " destroy" , Http2Session::Destroy);
30583086 env->SetProtoMethod (session, " goaway" , Http2Session::Goaway);
30593087 env->SetProtoMethod (session, " settings" , Http2Session::Settings);
0 commit comments