Skip to content

Commit 5ff1c54

Browse files
Implement onStream: unified body streaming + abort callback (#1246)
* Initial plan * Implement onStream C++ function and add TypeScript type definition Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com>
1 parent 23d0f7a commit 5ff1c54

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎docs/index.d.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ export interface HttpResponse {
199199
* If the total body size exceeds maxSize bytes, handler is called with null instead. */
200200
onFullData(maxSize: number, handler: (fullBody: ArrayBuffer | null) => void) : HttpResponse;
201201

202+
/** Combined handler for HTTP request body streaming and connection abort events.
203+
* If chunk is null, the connection was aborted. If maxRemainingBodyLength is 0n, the last chunk has arrived.
204+
* You can safely preallocate using maxRemainingBodyLength (it is very large for chunked transfer encoding).
205+
* You MUST copy the data of chunk if maxRemainingBodyLength is not 0n. We Neuter ArrayBuffers on return, making them zero length. */
206+
onStream(handler: (chunk: ArrayBuffer | null, maxRemainingBodyLength: bigint) => void) : HttpResponse;
207+
202208
/** Returns the remote IP address in binary format (4 or 16 bytes). */
203209
getRemoteAddress() : ArrayBuffer;
204210

‎src/HttpResponseWrapper.h‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)