@@ -1822,6 +1822,33 @@ void Http2Session::Consume(Local<Object> stream_obj) {
18221822 Debug (this , " i/o stream consumed" );
18231823}
18241824
1825+ // Allow injecting of data from JS
1826+ // This is used when the socket has already some data received
1827+ // before our listener was attached
1828+ // https://github.com/nodejs/node/issues/35475
1829+ void Http2Session::Receive (const FunctionCallbackInfo<Value>& args) {
1830+ Http2Session* session;
1831+ ASSIGN_OR_RETURN_UNWRAP (&session, args.Holder ());
1832+ CHECK (args[0 ]->IsObject ());
1833+
1834+ ArrayBufferViewContents<char > buffer (args[0 ]);
1835+ const char * data = buffer.data ();
1836+ size_t len = buffer.length ();
1837+ Debug (session, " Receiving %zu bytes injected from JS" , len);
1838+
1839+ // Copy given buffer
1840+ while (len > 0 ) {
1841+ uv_buf_t buf = session->OnStreamAlloc (len);
1842+ size_t copy = buf.len > len ? len : buf.len ;
1843+ memcpy (buf.base , data, copy);
1844+ buf.len = copy;
1845+ session->OnStreamRead (copy, buf);
1846+
1847+ data += copy;
1848+ len -= copy;
1849+ }
1850+ }
1851+
18251852Http2Stream* Http2Stream::New (Http2Session* session,
18261853 int32_t id,
18271854 nghttp2_headers_category category,
@@ -3047,6 +3074,7 @@ void Initialize(Local<Object> target,
30473074 env->SetProtoMethod (session, " altsvc" , Http2Session::AltSvc);
30483075 env->SetProtoMethod (session, " ping" , Http2Session::Ping);
30493076 env->SetProtoMethod (session, " consume" , Http2Session::Consume);
3077+ env->SetProtoMethod (session, " receive" , Http2Session::Receive);
30503078 env->SetProtoMethod (session, " destroy" , Http2Session::Destroy);
30513079 env->SetProtoMethod (session, " goaway" , Http2Session::Goaway);
30523080 env->SetProtoMethod (session, " settings" , Http2Session::Settings);
0 commit comments