Skip to content

Commit 8a1c215

Browse files
fix: ensure CacheOverride bitflags are the same value as defined in xqd (#386)
Co-authored-by: Trevor Elliott <[email protected]>
1 parent 517ae62 commit 8a1c215

File tree

13 files changed

+306
-32
lines changed

13 files changed

+306
-32
lines changed

.github/workflows/main.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ jobs:
143143
path: "/home/runner/.cargo/bin/viceroy"
144144
key: crate-cache-viceroy-${{ env.viceroy_version }}
145145

146-
- run: yarn
146+
- run: yarn install --frozen-lockfile
147147

148148
- name: Build WPT runtime
149149
run: |
@@ -167,6 +167,7 @@ jobs:
167167
runs-on: ubuntu-latest
168168
needs: [build]
169169
strategy:
170+
fail-fast: false
170171
matrix:
171172
fixture:
172173
- async-select
@@ -224,7 +225,7 @@ jobs:
224225
uses: actions/download-artifact@v3
225226
with:
226227
name: ${{ matrix.fixture == 'regex' && 'engine-release' || 'engine-debug' }}
227-
- run: yarn
228+
- run: yarn install --frozen-lockfile
228229

229230
- name: js-compute Integration Tests Job
230231
timeout-minutes: 20
@@ -301,10 +302,12 @@ jobs:
301302
runs-on: ubuntu-latest
302303
needs: [build]
303304
strategy:
305+
fail-fast: false
304306
matrix:
305307
fixture:
306308
- 'async-select'
307309
- 'byte-repeater'
310+
- 'cache-override'
308311
- 'edge-dictionary'
309312
- 'error'
310313
- 'geoip'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ tests/wpt-harness/wpt-test-runner.js
2020
wpt-runtime.wasm
2121
docs-app/bin/main.wasm
2222
docs-app/pkg/*.tar.gz
23+
c-dependencies/js-compute-runtime/xqd-world/xqd_world_component_type.o

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

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,64 @@ CacheOverride::CacheOverrideMode CacheOverride::mode(JSObject *self) {
1818
}
1919

2020
void CacheOverride::set_mode(JSObject *self, CacheOverride::CacheOverrideMode mode) {
21+
MOZ_ASSERT(is_instance(self));
2122
JS::SetReservedSlot(self, CacheOverride::Slots::Mode, JS::Int32Value((int32_t)mode));
2223
}
2324

2425
JS::Value CacheOverride::ttl(JSObject *self) {
26+
MOZ_ASSERT(is_instance(self));
2527
if (CacheOverride::mode(self) != CacheOverride::CacheOverrideMode::Override)
2628
return JS::UndefinedValue();
2729
return JS::GetReservedSlot(self, Slots::TTL);
2830
}
2931

3032
void CacheOverride::set_ttl(JSObject *self, uint32_t ttl) {
33+
MOZ_ASSERT(is_instance(self));
3134
MOZ_RELEASE_ASSERT(mode(self) == CacheOverride::CacheOverrideMode::Override);
3235
JS::SetReservedSlot(self, CacheOverride::Slots::TTL, JS::Int32Value((int32_t)ttl));
3336
}
3437

3538
JS::Value CacheOverride::swr(JSObject *self) {
39+
MOZ_ASSERT(is_instance(self));
3640
if (CacheOverride::mode(self) != CacheOverride::CacheOverrideMode::Override)
3741
return JS::UndefinedValue();
3842
return JS::GetReservedSlot(self, Slots::SWR);
3943
}
4044

4145
void CacheOverride::set_swr(JSObject *self, uint32_t swr) {
46+
MOZ_ASSERT(is_instance(self));
4247
MOZ_RELEASE_ASSERT(CacheOverride::mode(self) == CacheOverride::CacheOverrideMode::Override);
4348
JS::SetReservedSlot(self, CacheOverride::Slots::SWR, JS::Int32Value((int32_t)swr));
4449
}
4550

4651
JS::Value CacheOverride::surrogate_key(JSObject *self) {
52+
MOZ_ASSERT(is_instance(self));
4753
if (CacheOverride::mode(self) != CacheOverride::CacheOverrideMode::Override)
4854
return JS::UndefinedValue();
4955
return JS::GetReservedSlot(self, Slots::SurrogateKey);
5056
}
5157

5258
void CacheOverride::set_surrogate_key(JSObject *self, JSString *key) {
59+
MOZ_ASSERT(is_instance(self));
5360
MOZ_RELEASE_ASSERT(CacheOverride::mode(self) == CacheOverride::CacheOverrideMode::Override);
5461
JS::SetReservedSlot(self, CacheOverride::Slots::SurrogateKey, JS::StringValue(key));
5562
}
5663

5764
JS::Value CacheOverride::pci(JSObject *self) {
65+
MOZ_ASSERT(is_instance(self));
5866
if (CacheOverride::mode(self) != CacheOverride::CacheOverrideMode::Override)
5967
return JS::UndefinedValue();
6068
return JS::GetReservedSlot(self, Slots::PCI);
6169
}
6270

6371
void CacheOverride::set_pci(JSObject *self, bool pci) {
72+
MOZ_ASSERT(is_instance(self));
6473
MOZ_RELEASE_ASSERT(CacheOverride::mode(self) == CacheOverride::CacheOverrideMode::Override);
6574
JS::SetReservedSlot(self, CacheOverride::Slots::PCI, JS::BooleanValue(pci));
6675
}
6776

6877
uint8_t CacheOverride::abi_tag(JSObject *self) {
78+
MOZ_ASSERT(is_instance(self));
6979
switch (CacheOverride::mode(self)) {
7080
case CacheOverride::CacheOverrideMode::None:
7181
return (uint8_t)CacheOverrideTag::None;
@@ -86,6 +96,11 @@ uint8_t CacheOverride::abi_tag(JSObject *self) {
8696
}
8797

8898
bool CacheOverride::mode_get(JSContext *cx, JS::HandleObject self, JS::MutableHandleValue rval) {
99+
if (self == proto_obj) {
100+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INVALID_INTERFACE, "mode get",
101+
"CacheOverride");
102+
return false;
103+
}
89104
const char *mode_chars;
90105
switch (CacheOverride::mode(self)) {
91106
case CacheOverride::CacheOverrideMode::None:
@@ -120,6 +135,11 @@ bool CacheOverride::ensure_override(JSContext *cx, JS::HandleObject self, const
120135

121136
bool CacheOverride::mode_set(JSContext *cx, JS::HandleObject self, JS::HandleValue val,
122137
JS::MutableHandleValue rval) {
138+
if (self == proto_obj) {
139+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INVALID_INTERFACE, "mode set",
140+
"CacheOverride");
141+
return false;
142+
}
123143
size_t mode_len;
124144
JS::UniqueChars mode_chars = encode(cx, val, &mode_len);
125145
if (!mode_chars)
@@ -133,10 +153,8 @@ bool CacheOverride::mode_set(JSContext *cx, JS::HandleObject self, JS::HandleVal
133153
} else if (!strcmp(mode_chars.get(), "override")) {
134154
mode = CacheOverride::CacheOverrideMode::Override;
135155
} else {
136-
JS_ReportErrorUTF8(cx,
137-
"'mode' has to be \"none\", \"pass\", or \"override\", "
138-
"but got %s",
139-
mode_chars.get());
156+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CACHE_OVERRIDE_MODE_INVALID,
157+
mode_chars.get());
140158
return false;
141159
}
142160

@@ -145,12 +163,22 @@ bool CacheOverride::mode_set(JSContext *cx, JS::HandleObject self, JS::HandleVal
145163
}
146164

147165
bool CacheOverride::ttl_get(JSContext *cx, JS::HandleObject self, JS::MutableHandleValue rval) {
166+
if (self == proto_obj) {
167+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INVALID_INTERFACE, "ttl get",
168+
"CacheOverride");
169+
return false;
170+
}
148171
rval.set(CacheOverride::ttl(self));
149172
return true;
150173
}
151174

152175
bool CacheOverride::ttl_set(JSContext *cx, JS::HandleObject self, JS::HandleValue val,
153176
JS::MutableHandleValue rval) {
177+
if (self == proto_obj) {
178+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INVALID_INTERFACE, "ttl set",
179+
"CacheOverride");
180+
return false;
181+
}
154182
if (!CacheOverride::ensure_override(cx, self, "a TTL"))
155183
return false;
156184

@@ -168,12 +196,22 @@ bool CacheOverride::ttl_set(JSContext *cx, JS::HandleObject self, JS::HandleValu
168196
}
169197

170198
bool CacheOverride::swr_get(JSContext *cx, JS::HandleObject self, JS::MutableHandleValue rval) {
199+
if (self == proto_obj) {
200+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INVALID_INTERFACE, "swr get",
201+
"CacheOverride");
202+
return false;
203+
}
171204
rval.set(CacheOverride::swr(self));
172205
return true;
173206
}
174207

175208
bool CacheOverride::swr_set(JSContext *cx, JS::HandleObject self, JS::HandleValue val,
176209
JS::MutableHandleValue rval) {
210+
if (self == proto_obj) {
211+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INVALID_INTERFACE, "swr set",
212+
"CacheOverride");
213+
return false;
214+
}
177215
if (!CacheOverride::ensure_override(cx, self, "SWR"))
178216
return false;
179217

@@ -192,12 +230,22 @@ bool CacheOverride::swr_set(JSContext *cx, JS::HandleObject self, JS::HandleValu
192230

193231
bool CacheOverride::surrogate_key_get(JSContext *cx, JS::HandleObject self,
194232
JS::MutableHandleValue rval) {
233+
if (self == proto_obj) {
234+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INVALID_INTERFACE,
235+
"surrogate_key get", "CacheOverride");
236+
return false;
237+
}
195238
rval.set(CacheOverride::surrogate_key(self));
196239
return true;
197240
}
198241

199242
bool CacheOverride::surrogate_key_set(JSContext *cx, JS::HandleObject self, JS::HandleValue val,
200243
JS::MutableHandleValue rval) {
244+
if (self == proto_obj) {
245+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INVALID_INTERFACE,
246+
"surrogate_key set", "CacheOverride");
247+
return false;
248+
}
201249
if (!CacheOverride::ensure_override(cx, self, "a surrogate key"))
202250
return false;
203251

@@ -215,12 +263,22 @@ bool CacheOverride::surrogate_key_set(JSContext *cx, JS::HandleObject self, JS::
215263
}
216264

217265
bool CacheOverride::pci_get(JSContext *cx, JS::HandleObject self, JS::MutableHandleValue rval) {
266+
if (self == proto_obj) {
267+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INVALID_INTERFACE, "pci get",
268+
"CacheOverride");
269+
return false;
270+
}
218271
rval.set(CacheOverride::pci(self));
219272
return true;
220273
}
221274

222275
bool CacheOverride::pci_set(JSContext *cx, JS::HandleObject self, JS::HandleValue val,
223276
JS::MutableHandleValue rval) {
277+
if (self == proto_obj) {
278+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INVALID_INTERFACE, "pci set",
279+
"CacheOverride");
280+
return false;
281+
}
224282
if (!CacheOverride::ensure_override(cx, self, "PCI"))
225283
return false;
226284

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ class CacheOverride : public BuiltinImpl<CacheOverride> {
3939
PCI = 1 << 3,
4040
};
4141

42+
static_assert((int)CacheOverrideTag::Pass == FASTLY_HTTP_CACHE_OVERRIDE_TAG_PASS);
43+
static_assert((int)CacheOverrideTag::TTL == FASTLY_HTTP_CACHE_OVERRIDE_TAG_TTL);
44+
static_assert((int)CacheOverrideTag::SWR ==
45+
FASTLY_HTTP_CACHE_OVERRIDE_TAG_STALE_WHILE_REVALIDATE);
46+
static_assert((int)CacheOverrideTag::PCI == FASTLY_HTTP_CACHE_OVERRIDE_TAG_PCI);
47+
4248
static const JSFunctionSpec methods[];
4349
static const JSPropertySpec properties[];
4450
static uint8_t abi_tag(JSObject *self);

c-dependencies/js-compute-runtime/error-numbers.msg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ MSG_DEF(JSMSG_BACKEND_TLS_MIN_INVALID, 0, JSEXN_RANGEERR
9393
MSG_DEF(JSMSG_BACKEND_TLS_MAX_INVALID, 0, JSEXN_RANGEERR, "Backend constructor: tlsMaxVersion must be either 1, 1.1, 1.2, or 1.3")
9494
MSG_DEF(JSMSG_BACKEND_TLS_MIN_GREATER_THAN_TLS_MAX, 0, JSEXN_RANGEERR, "Backend constructor: tlsMinVersion must be less than or equal to tlsMaxVersion")
9595
MSG_DEF(JSMSG_BACKEND_PORT_INVALID, 0, JSEXN_RANGEERR, "Backend constructor: port must be more than 0 and less than 2^16 (65,536)")
96+
MSG_DEF(JSMSG_CACHE_OVERRIDE_MODE_INVALID, 1, JSEXN_TYPEERR, "CacheOverride constructor: 'mode' has to be \"none\", \"pass\", or \"override\", but got \"{0}\"")
9697
MSG_DEF(JSMSG_RESPONSE_VALUE_NOT_UINT8ARRAY, 0, JSEXN_TYPEERR, "Can't convert value to Uint8Array while consuming Body")
9798
MSG_DEF(JSMSG_RESPONSE_BODY_DISTURBED_OR_LOCKED, 0, JSEXN_TYPEERR, "Response body object should not be disturbed or locked")
9899
//clang-format on

c-dependencies/js-compute-runtime/xqd-world/xqd_world.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ typedef struct {
6565

6666
typedef uint8_t fastly_http_cache_override_tag_t;
6767

68-
#define FASTLY_HTTP_CACHE_OVERRIDE_TAG_NONE (1 << 0)
69-
#define FASTLY_HTTP_CACHE_OVERRIDE_TAG_PASS (1 << 1)
70-
#define FASTLY_HTTP_CACHE_OVERRIDE_TAG_TTL (1 << 2)
71-
#define FASTLY_HTTP_CACHE_OVERRIDE_TAG_STALE_WHILE_REVALIDATE (1 << 3)
72-
#define FASTLY_HTTP_CACHE_OVERRIDE_TAG_PCI (1 << 4)
68+
#define FASTLY_HTTP_CACHE_OVERRIDE_TAG_PASS (1 << 0)
69+
#define FASTLY_HTTP_CACHE_OVERRIDE_TAG_TTL (1 << 1)
70+
#define FASTLY_HTTP_CACHE_OVERRIDE_TAG_STALE_WHILE_REVALIDATE (1 << 2)
71+
#define FASTLY_HTTP_CACHE_OVERRIDE_TAG_PCI (1 << 3)
7372

7473
typedef uint8_t fastly_http_version_t;
7574

c-dependencies/js-compute-runtime/xqd-world/xqd_world_adapter.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,6 @@ bool xqd_fastly_http_req_body_downstream_get(fastly_request_t *ret, fastly_error
127127
return convert_result(xqd_req_body_downstream_get(&ret->f0, &ret->f1), err);
128128
}
129129

130-
int convert_tag(fastly_http_cache_override_tag_t tag) {
131-
switch (tag) {
132-
case FASTLY_HTTP_CACHE_OVERRIDE_TAG_NONE:
133-
return CACHE_OVERRIDE_NONE;
134-
case FASTLY_HTTP_CACHE_OVERRIDE_TAG_PASS:
135-
return CACHE_OVERRIDE_PASS;
136-
case FASTLY_HTTP_CACHE_OVERRIDE_TAG_TTL:
137-
return CACHE_OVERRIDE_TTL;
138-
case FASTLY_HTTP_CACHE_OVERRIDE_TAG_STALE_WHILE_REVALIDATE:
139-
return CACHE_OVERRIDE_STALE_WHILE_REVALIDATE;
140-
case FASTLY_HTTP_CACHE_OVERRIDE_TAG_PCI:
141-
default:
142-
return CACHE_OVERRIDE_PCI;
143-
}
144-
}
145-
146130
bool xqd_fastly_http_req_cache_override_set(fastly_request_handle_t h,
147131
fastly_http_cache_override_tag_t tag,
148132
uint32_t *maybe_ttl,
@@ -157,7 +141,7 @@ bool xqd_fastly_http_req_cache_override_set(fastly_request_handle_t h,
157141
}
158142
return convert_result(
159143
xqd_req_cache_override_v2_set(
160-
h, convert_tag(tag), maybe_ttl == NULL ? 0 : *maybe_ttl,
144+
h, static_cast<int>(tag), maybe_ttl == NULL ? 0 : *maybe_ttl,
161145
maybe_stale_while_revalidate == NULL ? 0 : *maybe_stale_while_revalidate,
162146
reinterpret_cast<char *>(sk_str.ptr), sk_str.len),
163147
err);

c-dependencies/js-compute-runtime/xqd.wit

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ world xqd-world {
111111
http-req-body-downstream-get: func() -> result<request, error>
112112

113113
flags http-cache-override-tag {
114-
/// Do not override the behavior specified in the origin response's cache control headers.
115-
none,
116114
/// Do not cache the response to this request, regardless of the origin response's headers.
117115
pass,
118116
ttl,

0 commit comments

Comments
 (0)