@@ -208,6 +208,47 @@ struct HttpResponseWrapper {
208208 }
209209 }
210210
211+ /* Takes a function of (chunk, maxRemainingBodyLength). Combines onAborted and onData into a single callback.
212+ * If chunk is null, the connection was aborted. If maxRemainingBodyLength is 0, the last chunk has arrived.
213+ * The JS object is invalidated before the abort callback is called. Returns this. */
214+ template <int SSL >
215+ static void res_onStream (const FunctionCallbackInfo<Value> &args) {
216+ Isolate *isolate = args.GetIsolate ();
217+ auto *res = getHttpResponse<SSL >(args);
218+ if (res) {
219+ /* Share the persistent function between both onAborted and onData lambdas */
220+ auto sharedP = std::make_shared<UniquePersistent<Function>>(isolate, Local<Function>::Cast (args[0 ]));
221+
222+ /* This is how we capture res (C++ this in invocation of this function) */
223+ UniquePersistent<Object> resObject (isolate, args.This ());
224+
225+ res->onAborted ([resObject = std::move (resObject), sharedP, isolate]() {
226+ HandleScope hs (isolate);
227+
228+ /* Mark this resObject invalid */
229+ Local<Object>::New (isolate, resObject)->SetAlignedPointerInInternalField (0 , nullptr );
230+
231+ /* Call handler with (null, 0n) to signal abort */
232+ Local<Value> argv[] = {Null (isolate), BigInt::NewFromUnsigned (isolate, 0 )};
233+ CallJS (isolate, Local<Function>::New (isolate, *sharedP), 2 , argv);
234+ });
235+
236+ res->onData ([res, sharedP, isolate](std::string_view data, bool last) {
237+ HandleScope hs (isolate);
238+
239+ Local<ArrayBuffer> dataArrayBuffer = ArrayBuffer_New (isolate, (void *) data.data (), data.length ());
240+
241+ /* Pass maxRemainingBodyLength so user can preallocate; 0 signals the last chunk */
242+ Local<Value> argv[] = {dataArrayBuffer, BigInt::NewFromUnsigned (isolate, res->maxRemainingBodyLength ())};
243+ CallJS (isolate, Local<Function>::New (isolate, *sharedP), 2 , argv);
244+
245+ dataArrayBuffer->Detach ();
246+ });
247+
248+ args.GetReturnValue ().Set (args.This ());
249+ }
250+ }
251+
211252 /* Takes nothing, returns nothing. Cb wants nothing returned. */
212253 template <int SSL >
213254 static void res_onAborted (const FunctionCallbackInfo<Value> &args) {
@@ -597,6 +638,7 @@ struct HttpResponseWrapper {
597638
598639 /* QUIC has a lot of functions unimplemented */
599640 if constexpr (SSL != 2 ) {
641+ resTemplateLocal->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " onStream" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, res_onStream<SSL >));
600642 resTemplateLocal->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " onFullData" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, res_onFullData<SSL >));
601643 resTemplateLocal->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " getWriteOffset" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, res_getWriteOffset<SSL >));
602644 resTemplateLocal->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " maxRemainingBodyLength" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, res_maxRemainingBodyLength<SSL >));
0 commit comments