Skip to content

Commit

Permalink
fix: support cacheKey in Request init (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed May 7, 2024
1 parent 3042796 commit b64b22e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ import { routes } from "./routes.js";
});
routes.set("/request/constructor/cacheKey", () => {
const request = new Request('https://www.fastly.com', {cacheKey: 'meow'})
request.setCacheKey('meow')
let error = assert(request.headers.get('fastly-xqd-cache-key'), '404CDD7BC109C432F8CC2443B45BCFE95980F5107215C645236E577929AC3E52', `request.headers.get('fastly-xqd-cache-key'`)
if (error) { return error }
return pass()
Expand Down
10 changes: 10 additions & 0 deletions runtime/fastly/builtins/fetch/request-response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,7 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
JS::RootedValue body_val(cx);
JS::RootedValue backend_val(cx);
JS::RootedValue cache_override(cx);
JS::RootedValue cache_key(cx);
JS::RootedValue fastly_val(cx);
JS::RootedValue manualFramingHeaders(cx);
bool hasmanualFramingHeaders;
Expand All @@ -1777,6 +1778,7 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
!JS_GetProperty(cx, init, "body", &body_val) ||
!JS_GetProperty(cx, init, "backend", &backend_val) ||
!JS_GetProperty(cx, init, "cacheOverride", &cache_override) ||
!JS_GetProperty(cx, init, "cacheKey", &cache_key) ||
!JS_GetProperty(cx, init, "fastly", &fastly_val) ||
!JS_HasOwnProperty(cx, init, "manualFramingHeaders", &hasmanualFramingHeaders) ||
!JS_GetProperty(cx, init, "manualFramingHeaders", &manualFramingHeaders)) {
Expand Down Expand Up @@ -2074,6 +2076,14 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
JS::GetReservedSlot(input_request, static_cast<uint32_t>(Slots::CacheOverride)));
}

// Apply the Fastly Compute-proprietary `cacheKey` property.
// (in the input_request case, the header will be copied across normally)
if (!cache_key.isUndefined()) {
if (!set_cache_key(cx, request, cache_key)) {
return nullptr;
}
}

if (fastly_val.isObject()) {
JS::RootedValue decompress_response_val(cx);
JS::RootedObject fastly(cx, fastly_val.toObjectOrNull());
Expand Down
9 changes: 9 additions & 0 deletions runtime/js-compute-runtime/builtins/request-response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,7 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
JS::RootedValue body_val(cx);
JS::RootedValue backend_val(cx);
JS::RootedValue cache_override(cx);
JS::RootedValue cache_key(cx);
JS::RootedValue fastly_val(cx);
JS::RootedValue manualFramingHeaders(cx);
bool hasmanualFramingHeaders;
Expand All @@ -1824,6 +1825,7 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
!JS_GetProperty(cx, init, "body", &body_val) ||
!JS_GetProperty(cx, init, "backend", &backend_val) ||
!JS_GetProperty(cx, init, "cacheOverride", &cache_override) ||
!JS_GetProperty(cx, init, "cacheKey", &cache_key) ||
!JS_GetProperty(cx, init, "fastly", &fastly_val) ||
!JS_HasOwnProperty(cx, init, "manualFramingHeaders", &hasmanualFramingHeaders) ||
!JS_GetProperty(cx, init, "manualFramingHeaders", &manualFramingHeaders)) {
Expand Down Expand Up @@ -2119,6 +2121,13 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
request, static_cast<uint32_t>(Slots::CacheOverride),
JS::GetReservedSlot(input_request, static_cast<uint32_t>(Slots::CacheOverride)));
}
// Apply the Fastly Compute-proprietary `cacheKey` property.
// (in the input_request case, the header will be copied across normally)
if (!cache_key.isUndefined()) {
if (!set_cache_key(cx, request, cache_key)) {
return nullptr;
}
}

if (fastly_val.isObject()) {
JS::RootedValue decompress_response_val(cx);
Expand Down

0 comments on commit b64b22e

Please sign in to comment.