Skip to content

Commit d0a49aa

Browse files
committed
Bump uWS, update wrappers
1 parent 78062d0 commit d0a49aa

2 files changed

Lines changed: 12 additions & 34 deletions

File tree

‎src/HttpResponseWrapper.h‎

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ struct HttpResponseWrapper {
118118
* accumulated into a std::vector whose memory is transferred zero-copy into the ArrayBuffer backing store.
119119
* Returns this */
120120
template <int SSL>
121-
static void res_onFullData(const FunctionCallbackInfo<Value> &args) {
121+
static void res_collectBody(const FunctionCallbackInfo<Value> &args) {
122122
Isolate *isolate = args.GetIsolate();
123123
auto *res = getHttpResponse<SSL>(args);
124124
if (res) {
@@ -142,13 +142,13 @@ struct HttpResponseWrapper {
142142
std::unique_ptr<std::vector<char>> buffer;
143143
bool overflow = false;
144144

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 {
146146
HandleScope hs(isolate);
147147

148148
if (!overflow) {
149149
if (!buffer) {
150150
/* Fast path: this is the very first (and possibly only) chunk */
151-
if (last) {
151+
if (maxRemainingBodyLength == 0) {
152152
if (data.size() <= maxSize) {
153153
/* Single-chunk zero-copy: wrap data directly, detach after call like onData */
154154
Local<ArrayBuffer> ab = ArrayBuffer_New(isolate, (void *) data.data(), data.size());
@@ -165,8 +165,8 @@ struct HttpResponseWrapper {
165165
if (data.size() <= maxSize) {
166166
buffer = std::make_unique<std::vector<char>>();
167167
/* 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)
170170
}
171171
buffer->assign(data.begin(), data.end());
172172
} else {
@@ -183,7 +183,7 @@ struct HttpResponseWrapper {
183183
}
184184
}
185185

186-
if (last) {
186+
if (maxRemainingBodyLength == 0) {
187187
if (!overflow) {
188188
/* Zero-copy: hand V8 the vector's own memory via a custom deleter */
189189
auto *rawBuffer = buffer.release();
@@ -212,7 +212,7 @@ struct HttpResponseWrapper {
212212
* If chunk is null, the connection was aborted. If maxRemainingBodyLength is 0, the last chunk has arrived.
213213
* The JS object is invalidated before the abort callback is called. Returns this. */
214214
template <int SSL>
215-
static void res_onStream(const FunctionCallbackInfo<Value> &args) {
215+
static void res_onDataV2(const FunctionCallbackInfo<Value> &args) {
216216
Isolate *isolate = args.GetIsolate();
217217
auto *res = getHttpResponse<SSL>(args);
218218
if (res) {
@@ -222,24 +222,13 @@ struct HttpResponseWrapper {
222222
/* This is how we capture res (C++ this in invocation of this function) */
223223
UniquePersistent<Object> resObject(isolate, args.This());
224224

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) {
237226
HandleScope hs(isolate);
238227

239228
Local<ArrayBuffer> dataArrayBuffer = ArrayBuffer_New(isolate, (void *) data.data(), data.length());
240229

241230
/* 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)};
243232
CallJS(isolate, Local<Function>::New(isolate, *sharedP), 2, argv);
244233

245234
dataArrayBuffer->Detach();
@@ -368,16 +357,6 @@ struct HttpResponseWrapper {
368357
}
369358
}
370359

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-
381360
/* Takes function of bool(int), returns this */
382361
template <int SSL>
383362
static void res_onWritable(const FunctionCallbackInfo<Value> &args) {
@@ -638,10 +617,9 @@ struct HttpResponseWrapper {
638617

639618
/* QUIC has a lot of functions unimplemented */
640619
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>));
643622
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>));
645623
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddress", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getRemoteAddress<SSL>));
646624
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "cork", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_cork<SSL>));
647625
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "collect", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_cork<SSL>));

0 commit comments

Comments
 (0)