Skip to content

Commit b0a1028

Browse files
HttpResponse: add onFullData with zero-copy fast path and lazy allocation (#1225)
* Initial plan * Add onFullData to HttpResponseWrapper for accumulating POST body data Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com> * onFullData: fast paths, zero-copy, lazy allocation 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 a11eb9d commit b0a1028

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

‎docs/index.d.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ export interface HttpResponse {
181181
/** Handler for reading data from POST and such requests. You MUST copy the data of chunk if isLast is not true. We Neuter ArrayBuffers on return, making it zero length.*/
182182
onData(handler: (chunk: ArrayBuffer, isLast: boolean) => void) : HttpResponse;
183183

184+
/** Accumulates all data chunks and calls handler with the complete body as an ArrayBuffer once all data has arrived.
185+
* If the total body size exceeds maxSize bytes, handler is called with null instead. */
186+
onFullData(maxSize: number, handler: (fullBody: ArrayBuffer | null) => void) : HttpResponse;
187+
184188
/** Returns the remote IP address in binary format (4 or 16 bytes). */
185189
getRemoteAddress() : ArrayBuffer;
186190

‎src/HttpResponseWrapper.h‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,88 @@ struct HttpResponseWrapper {
111111
}
112112
}
113113

114+
/* Takes integer maxSize and function of fullData. Accumulates all data chunks and calls handler with the complete
115+
* body as an ArrayBuffer once all data has arrived. If the body exceeds maxSize bytes, handler is called with
116+
* null instead. Fast path: if all data arrives in a single chunk no allocation is made and the ArrayBuffer is
117+
* zero-copy (backed directly by the incoming data, detached after the call). Slow path: chunks are lazily
118+
* accumulated into a std::vector whose memory is transferred zero-copy into the ArrayBuffer backing store.
119+
* Returns this */
120+
template <int SSL>
121+
static void res_onFullData(const FunctionCallbackInfo<Value> &args) {
122+
Isolate *isolate = args.GetIsolate();
123+
auto *res = getHttpResponse<SSL>(args);
124+
if (res) {
125+
size_t maxSize = (size_t) args[0]->NumberValue(isolate->GetCurrentContext()).ToChecked();
126+
127+
/* This thing perfectly fits in with unique_function, and will Reset on destructor */
128+
UniquePersistent<Function> p(isolate, Local<Function>::Cast(args[1]));
129+
130+
/* Lazily allocated; nullptr means not yet started. Separate overflow flag distinguishes
131+
* the "not started" state from the "exceeded maxSize" state. */
132+
std::unique_ptr<std::vector<char>> buffer;
133+
bool overflow = false;
134+
135+
res->onData([p = std::move(p), buffer = std::move(buffer), overflow, maxSize, isolate](std::string_view data, bool last) mutable {
136+
HandleScope hs(isolate);
137+
138+
if (!overflow) {
139+
if (!buffer) {
140+
/* Fast path: this is the very first (and possibly only) chunk */
141+
if (last) {
142+
if (data.size() <= maxSize) {
143+
/* Single-chunk zero-copy: wrap data directly, detach after call like onData */
144+
Local<ArrayBuffer> ab = ArrayBuffer_New(isolate, (void *) data.data(), data.size());
145+
Local<Value> argv[] = {ab};
146+
CallJS(isolate, Local<Function>::New(isolate, p), 1, argv);
147+
ab->Detach();
148+
} else {
149+
Local<Value> argv[] = {Null(isolate)};
150+
CallJS(isolate, Local<Function>::New(isolate, p), 1, argv);
151+
}
152+
return;
153+
}
154+
/* Slow path begins: allocate buffer lazily for first non-terminal chunk */
155+
if (data.size() <= maxSize) {
156+
buffer = std::make_unique<std::vector<char>>(data.begin(), data.end());
157+
} else {
158+
overflow = true;
159+
}
160+
} else {
161+
/* Subsequent chunks: accumulate or mark overflow; guard both sides of subtraction */
162+
if (buffer->size() <= maxSize && data.size() <= maxSize - buffer->size()) {
163+
buffer->insert(buffer->end(), data.begin(), data.end());
164+
} else {
165+
buffer.reset();
166+
overflow = true;
167+
}
168+
}
169+
}
170+
171+
if (last) {
172+
if (!overflow) {
173+
/* Zero-copy: hand V8 the vector's own memory via a custom deleter */
174+
auto *rawBuffer = buffer.release();
175+
auto backingStore = ArrayBuffer::NewBackingStore(
176+
rawBuffer->data(), rawBuffer->size(),
177+
[](void *, size_t, void *deleter_data) {
178+
delete static_cast<std::vector<char> *>(deleter_data);
179+
},
180+
rawBuffer
181+
);
182+
Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, std::move(backingStore));
183+
Local<Value> argv[] = {ab};
184+
CallJS(isolate, Local<Function>::New(isolate, p), 1, argv);
185+
} else {
186+
Local<Value> argv[] = {Null(isolate)};
187+
CallJS(isolate, Local<Function>::New(isolate, p), 1, argv);
188+
}
189+
}
190+
});
191+
192+
args.GetReturnValue().Set(args.This());
193+
}
194+
}
195+
114196
/* Takes nothing, returns nothing. Cb wants nothing returned. */
115197
template <int SSL>
116198
static void res_onAborted(const FunctionCallbackInfo<Value> &args) {
@@ -463,6 +545,7 @@ struct HttpResponseWrapper {
463545
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onWritable", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_onWritable<SSL>));
464546
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onAborted", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_onAborted<SSL>));
465547
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onData", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_onData<SSL>));
548+
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onFullData", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_onFullData<SSL>));
466549

467550
/* QUIC has a lot of functions unimplemented */
468551
if constexpr (SSL != 2) {

0 commit comments

Comments
 (0)