@@ -118,7 +118,7 @@ struct HttpResponseWrapper { |
118 | 118 | * accumulated into a std::vector whose memory is transferred zero-copy into the ArrayBuffer backing store. |
119 | 119 | * Returns this */ |
120 | 120 | template <int SSL> |
121 | | - static void res_onFullData(const FunctionCallbackInfo<Value> &args) { |
| 121 | + static void res_collectBody(const FunctionCallbackInfo<Value> &args) { |
122 | 122 | Isolate *isolate = args.GetIsolate(); |
123 | 123 | auto *res = getHttpResponse<SSL>(args); |
124 | 124 | if (res) { |
@@ -142,13 +142,13 @@ struct HttpResponseWrapper { |
142 | 142 | std::unique_ptr<std::vector<char>> buffer; |
143 | 143 | bool overflow = false; |
144 | 144 |
|
145 | | - res->onData([res, p = std::move(p), buffer = std::move(buffer), overflow, maxSize, isolate](std::string_view data, bool last) mutable { |
| 145 | + res->onDataV2([res, p = std::move(p), buffer = std::move(buffer), overflow, maxSize, isolate](std::string_view data, uint64_t maxRemainingBodyLength) mutable { |
146 | 146 | HandleScope hs(isolate); |
147 | 147 |
|
148 | 148 | if (!overflow) { |
149 | 149 | if (!buffer) { |
150 | 150 | /* Fast path: this is the very first (and possibly only) chunk */ |
151 | | - if (last) { |
| 151 | + if (maxRemainingBodyLength == 0) { |
152 | 152 | if (data.size() <= maxSize) { |
153 | 153 | /* Single-chunk zero-copy: wrap data directly, detach after call like onData */ |
154 | 154 | Local<ArrayBuffer> ab = ArrayBuffer_New(isolate, (void *) data.data(), data.size()); |
@@ -165,8 +165,8 @@ struct HttpResponseWrapper { |
165 | 165 | if (data.size() <= maxSize) { |
166 | 166 | buffer = std::make_unique<std::vector<char>>(); |
167 | 167 | /* Preallocate with hint */ |
168 | | - if (res->maxRemainingBodyLength() <= maxSize) { |
169 | | - buffer->reserve(res->maxRemainingBodyLength()); // this includes the total size on first call (look over this) |
| 168 | + if (maxRemainingBodyLength <= maxSize) { |
| 169 | + buffer->reserve(maxRemainingBodyLength); // this includes the total size on first call (look over this) |
170 | 170 | } |
171 | 171 | buffer->assign(data.begin(), data.end()); |
172 | 172 | } else { |
@@ -183,7 +183,7 @@ struct HttpResponseWrapper { |
183 | 183 | } |
184 | 184 | } |
185 | 185 |
|
186 | | - if (last) { |
| 186 | + if (maxRemainingBodyLength == 0) { |
187 | 187 | if (!overflow) { |
188 | 188 | /* Zero-copy: hand V8 the vector's own memory via a custom deleter */ |
189 | 189 | auto *rawBuffer = buffer.release(); |
@@ -212,7 +212,7 @@ struct HttpResponseWrapper { |
212 | 212 | * If chunk is null, the connection was aborted. If maxRemainingBodyLength is 0, the last chunk has arrived. |
213 | 213 | * The JS object is invalidated before the abort callback is called. Returns this. */ |
214 | 214 | template <int SSL> |
215 | | - static void res_onStream(const FunctionCallbackInfo<Value> &args) { |
| 215 | + static void res_onDataV2(const FunctionCallbackInfo<Value> &args) { |
216 | 216 | Isolate *isolate = args.GetIsolate(); |
217 | 217 | auto *res = getHttpResponse<SSL>(args); |
218 | 218 | if (res) { |
@@ -222,24 +222,13 @@ struct HttpResponseWrapper { |
222 | 222 | /* This is how we capture res (C++ this in invocation of this function) */ |
223 | 223 | UniquePersistent<Object> resObject(isolate, args.This()); |
224 | 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) { |
| 225 | + res->onDataV2([res, sharedP, isolate](std::string_view data, uint64_t maxRemainingBodyLength) { |
237 | 226 | HandleScope hs(isolate); |
238 | 227 |
|
239 | 228 | Local<ArrayBuffer> dataArrayBuffer = ArrayBuffer_New(isolate, (void *) data.data(), data.length()); |
240 | 229 |
|
241 | 230 | /* Pass maxRemainingBodyLength so user can preallocate; 0 signals the last chunk */ |
242 | | - Local<Value> argv[] = {dataArrayBuffer, BigInt::NewFromUnsigned(isolate, res->maxRemainingBodyLength())}; |
| 231 | + Local<Value> argv[] = {dataArrayBuffer, BigInt::NewFromUnsigned(isolate, maxRemainingBodyLength)}; |
243 | 232 | CallJS(isolate, Local<Function>::New(isolate, *sharedP), 2, argv); |
244 | 233 |
|
245 | 234 | dataArrayBuffer->Detach(); |
@@ -368,16 +357,6 @@ struct HttpResponseWrapper { |
368 | 357 | } |
369 | 358 | } |
370 | 359 |
|
371 | | - /* Returns the max remaining body length */ |
372 | | - template <int SSL> |
373 | | - static void res_maxRemainingBodyLength(const FunctionCallbackInfo<Value> &args) { |
374 | | - Isolate *isolate = args.GetIsolate(); |
375 | | - auto *res = getHttpResponse<SSL>(args); |
376 | | - if (res) { |
377 | | - args.GetReturnValue().Set(BigInt::NewFromUnsigned(isolate, res->maxRemainingBodyLength())); |
378 | | - } |
379 | | - } |
380 | | - |
381 | 360 | /* Takes function of bool(int), returns this */ |
382 | 361 | template <int SSL> |
383 | 362 | static void res_onWritable(const FunctionCallbackInfo<Value> &args) { |
@@ -638,10 +617,9 @@ struct HttpResponseWrapper { |
638 | 617 |
|
639 | 618 | /* QUIC has a lot of functions unimplemented */ |
640 | 619 | if constexpr (SSL != 2) { |
641 | | - resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onStream", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_onStream<SSL>)); |
642 | | - resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onFullData", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_onFullData<SSL>)); |
| 620 | + resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onDataV2", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_onDataV2<SSL>)); |
| 621 | + resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "collectBody", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_collectBody<SSL>)); |
643 | 622 | resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getWriteOffset", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getWriteOffset<SSL>)); |
644 | | - resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "maxRemainingBodyLength", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_maxRemainingBodyLength<SSL>)); |
645 | 623 | resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddress", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getRemoteAddress<SSL>)); |
646 | 624 | resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "cork", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_cork<SSL>)); |
647 | 625 | resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "collect", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_cork<SSL>)); |
|