Skip to content

Commit b64b22e

Browse files
author
Guy Bedford
authored
fix: support cacheKey in Request init (#770)
1 parent 3042796 commit b64b22e

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

integration-tests/js-compute/fixtures/app/src/request-cache-key.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ import { routes } from "./routes.js";
6363
});
6464
routes.set("/request/constructor/cacheKey", () => {
6565
const request = new Request('https://www.fastly.com', {cacheKey: 'meow'})
66-
request.setCacheKey('meow')
6766
let error = assert(request.headers.get('fastly-xqd-cache-key'), '404CDD7BC109C432F8CC2443B45BCFE95980F5107215C645236E577929AC3E52', `request.headers.get('fastly-xqd-cache-key'`)
6867
if (error) { return error }
6968
return pass()

runtime/fastly/builtins/fetch/request-response.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,7 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
17671767
JS::RootedValue body_val(cx);
17681768
JS::RootedValue backend_val(cx);
17691769
JS::RootedValue cache_override(cx);
1770+
JS::RootedValue cache_key(cx);
17701771
JS::RootedValue fastly_val(cx);
17711772
JS::RootedValue manualFramingHeaders(cx);
17721773
bool hasmanualFramingHeaders;
@@ -1777,6 +1778,7 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
17771778
!JS_GetProperty(cx, init, "body", &body_val) ||
17781779
!JS_GetProperty(cx, init, "backend", &backend_val) ||
17791780
!JS_GetProperty(cx, init, "cacheOverride", &cache_override) ||
1781+
!JS_GetProperty(cx, init, "cacheKey", &cache_key) ||
17801782
!JS_GetProperty(cx, init, "fastly", &fastly_val) ||
17811783
!JS_HasOwnProperty(cx, init, "manualFramingHeaders", &hasmanualFramingHeaders) ||
17821784
!JS_GetProperty(cx, init, "manualFramingHeaders", &manualFramingHeaders)) {
@@ -2074,6 +2076,14 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
20742076
JS::GetReservedSlot(input_request, static_cast<uint32_t>(Slots::CacheOverride)));
20752077
}
20762078

2079+
// Apply the Fastly Compute-proprietary `cacheKey` property.
2080+
// (in the input_request case, the header will be copied across normally)
2081+
if (!cache_key.isUndefined()) {
2082+
if (!set_cache_key(cx, request, cache_key)) {
2083+
return nullptr;
2084+
}
2085+
}
2086+
20772087
if (fastly_val.isObject()) {
20782088
JS::RootedValue decompress_response_val(cx);
20792089
JS::RootedObject fastly(cx, fastly_val.toObjectOrNull());

runtime/js-compute-runtime/builtins/request-response.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,7 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
18141814
JS::RootedValue body_val(cx);
18151815
JS::RootedValue backend_val(cx);
18161816
JS::RootedValue cache_override(cx);
1817+
JS::RootedValue cache_key(cx);
18171818
JS::RootedValue fastly_val(cx);
18181819
JS::RootedValue manualFramingHeaders(cx);
18191820
bool hasmanualFramingHeaders;
@@ -1824,6 +1825,7 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
18241825
!JS_GetProperty(cx, init, "body", &body_val) ||
18251826
!JS_GetProperty(cx, init, "backend", &backend_val) ||
18261827
!JS_GetProperty(cx, init, "cacheOverride", &cache_override) ||
1828+
!JS_GetProperty(cx, init, "cacheKey", &cache_key) ||
18271829
!JS_GetProperty(cx, init, "fastly", &fastly_val) ||
18281830
!JS_HasOwnProperty(cx, init, "manualFramingHeaders", &hasmanualFramingHeaders) ||
18291831
!JS_GetProperty(cx, init, "manualFramingHeaders", &manualFramingHeaders)) {
@@ -2119,6 +2121,13 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
21192121
request, static_cast<uint32_t>(Slots::CacheOverride),
21202122
JS::GetReservedSlot(input_request, static_cast<uint32_t>(Slots::CacheOverride)));
21212123
}
2124+
// Apply the Fastly Compute-proprietary `cacheKey` property.
2125+
// (in the input_request case, the header will be copied across normally)
2126+
if (!cache_key.isUndefined()) {
2127+
if (!set_cache_key(cx, request, cache_key)) {
2128+
return nullptr;
2129+
}
2130+
}
21222131

21232132
if (fastly_val.isObject()) {
21242133
JS::RootedValue decompress_response_val(cx);

0 commit comments

Comments
 (0)