Skip to content

Commit e695e99

Browse files
Always fully initialize the downstream Request (#48)
Until now, we only fully initialized the downstream request object when it's retrieved by script, i.e. when the `FetchEvent#request` accessor was invoked. However, since #38, we're resolving relative URLs based on the downstream request's URL, and hence need to ensure that it's initialized properly. All tests I ran before trying to publish a new SDK, and hence running the full test suite, happened to invoked the accessor, so I didn't realize that things weren't working without doing so. This patch fixes things, and also lightly improves some aspects of base URL handling I found while debugging this.
1 parent 7b6e0ce commit e695e99

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

c-dependencies/js-compute-runtime/js-compute-builtins.cpp

+19-9
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,13 @@ namespace Fastly {
554554

555555
bool baseURL_set(JSContext* cx, unsigned argc, Value* vp) {
556556
CallArgs args = CallArgsFromVp(argc, vp);
557-
if (!URL::is_instance(args.get(0))) {
557+
if (args.get(0).isNullOrUndefined()) {
558+
baseURL.set(nullptr);
559+
}
560+
else if (!URL::is_instance(args.get(0))) {
558561
JS_ReportErrorUTF8(cx,
559-
"Invalid value assigned to fastly.baseURL, must be an instance of URL");
562+
"Invalid value assigned to fastly.baseURL, must be an instance of "
563+
"URL, null, or undefined");
560564
return false;
561565
}
562566

@@ -3290,6 +3294,8 @@ static PersistentRooted<JSObject*> INSTANCE;
32903294
}
32913295

32923296
static bool init_downstream_request(JSContext* cx, HandleObject request) {
3297+
MOZ_RELEASE_ASSERT(Request::request_handle(request).handle == INVALID_HANDLE);
3298+
32933299
RequestHandle request_handle = { INVALID_HANDLE };
32943300
BodyHandle body_handle = { INVALID_HANDLE };
32953301
if (!HANDLE_RESULT(cx, xqd_req_body_downstream_get(&request_handle, &body_handle)))
@@ -3342,13 +3348,7 @@ static PersistentRooted<JSObject*> INSTANCE;
33423348
bool request_get(JSContext* cx, unsigned argc, Value* vp) {
33433349
METHOD_HEADER(0)
33443350

3345-
RootedObject request(cx, &JS::GetReservedSlot(self, Slots::Request).toObject());
3346-
if (Request::request_handle(request).handle == INVALID_HANDLE) {
3347-
if (!init_downstream_request(cx, request))
3348-
return false;
3349-
}
3350-
3351-
args.rval().setObject(*request);
3351+
args.rval().set(JS::GetReservedSlot(self, Slots::Request));
33523352
return true;
33533353
}
33543354

@@ -3716,6 +3716,11 @@ static PersistentRooted<JSObject*> INSTANCE;
37163716
return INSTANCE;
37173717
}
37183718

3719+
bool init_request(JSContext* cx, HandleObject self) {
3720+
RootedObject request(cx, &JS::GetReservedSlot(self, Slots::Request).toObject());
3721+
return init_downstream_request(cx, request);
3722+
}
3723+
37193724
bool is_active(JSObject* self) {
37203725
MOZ_ASSERT(is_instance(self));
37213726
// Note: we also treat the FetchEvent as active if it's in `responseStreaming` state
@@ -3924,6 +3929,11 @@ namespace URL {
39243929
}
39253930

39263931
JSObject* create(JSContext* cx, HandleValue url_val, HandleValue base_val) {
3932+
if (is_instance(base_val)) {
3933+
RootedObject base_obj(cx, &base_val.toObject());
3934+
return create(cx, url_val, base_obj);
3935+
}
3936+
39273937
JSUrl* base = nullptr;
39283938

39293939
if (!base_val.isUndefined()) {

c-dependencies/js-compute-runtime/js-compute-builtins.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace FetchEvent {
2626
};
2727

2828
JSObject* create(JSContext* cx);
29+
bool init_request(JSContext* cx, JS::HandleObject self);
2930

3031
// There can only ever be a single FetchEvent instance in a service, so we can treat it as a
3132
// singleton for easy access.

c-dependencies/js-compute-runtime/js-compute-runtime.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ int main(int argc, const char *argv[]) {
456456
js::ResetMathRandomSeed(cx);
457457

458458
HandleObject fetch_event = FetchEvent::instance();
459+
FetchEvent::init_request(cx, fetch_event);
459460

460461
dispatch_fetch_event(cx, fetch_event, &total_compute);
461462

0 commit comments

Comments
 (0)