Skip to content

Commit 64003ed

Browse files
committed
Enable cache: no-cache compat flag and enum
1 parent 847693f commit 64003ed

File tree

5 files changed

+91
-44
lines changed

5 files changed

+91
-44
lines changed

src/workerd/api/http-test-ts.ts

+31-27
Original file line numberDiff line numberDiff line change
@@ -50,40 +50,56 @@ async function assertFetchCacheRejectsError(
5050

5151
export const cacheMode = {
5252
async test(ctrl: any, env: any, ctx: any) {
53-
const allowedCacheModes: RequestCache[] = [
53+
const allowedCacheModes: Set<RequestCache> = new Set([
5454
'default',
5555
'force-cache',
5656
'no-cache',
5757
'no-store',
5858
'only-if-cached',
5959
'reload',
60-
];
60+
]);
6161
assert.strictEqual('cache' in Request.prototype, env.CACHE_ENABLED);
6262
{
6363
const req = new Request('https://example.org', {});
6464
assert.strictEqual(req.cache, undefined);
6565
}
66-
if (!env.CACHE_ENABLED) {
67-
for (const cacheMode of allowedCacheModes) {
66+
67+
var enabledCacheModes: Set<RequestCache> = new Set();
68+
69+
if (env.CACHE_ENABLED) {
70+
enabledCacheModes.add('no-store');
71+
}
72+
if (env.NO_CACHE_ENABLED) {
73+
enabledCacheModes.add('no-cache');
74+
}
75+
76+
const failureCacheModes = allowedCacheModes.difference(enabledCacheModes);
77+
for (const cacheMode of failureCacheModes) {
78+
if (!env.CACHE_ENABLED) {
6879
await assertRequestCacheThrowsError(cacheMode);
6980
await assertFetchCacheRejectsError(cacheMode);
81+
} else {
82+
await assertRequestCacheThrowsError(
83+
cacheMode,
84+
'TypeError',
85+
'Unsupported cache mode: ' + cacheMode
86+
);
87+
await assertFetchCacheRejectsError(
88+
cacheMode,
89+
'TypeError',
90+
'Unsupported cache mode: ' + cacheMode
91+
);
7092
}
71-
} else {
72-
var failureCacheModes: RequestCache[] = [
73-
'default',
74-
'no-cache',
75-
'force-cache',
76-
'only-if-cached',
77-
'reload',
78-
];
93+
}
94+
for (const cacheMode of enabledCacheModes) {
7995
{
80-
const req = new Request('https://example.org', { cache: 'no-store' });
81-
assert.strictEqual(req.cache, 'no-store');
96+
const req = new Request('https://example.org', { cache: cacheMode });
97+
assert.strictEqual(req.cache, cacheMode);
8298
}
8399
{
84100
const response = await env.SERVICE.fetch(
85101
'http://placeholder/not-found',
86-
{ cache: 'no-store' }
102+
{ cache: cacheMode }
87103
);
88104
assert.strictEqual(
89105
util.inspect(response),
@@ -106,18 +122,6 @@ export const cacheMode = {
106122
}`
107123
);
108124
}
109-
for (const cacheMode of failureCacheModes) {
110-
await assertRequestCacheThrowsError(
111-
cacheMode,
112-
'TypeError',
113-
'Unsupported cache mode: ' + cacheMode
114-
);
115-
await assertFetchCacheRejectsError(
116-
cacheMode,
117-
'TypeError',
118-
'Unsupported cache mode: ' + cacheMode
119-
);
120-
}
121125
}
122126
},
123127
};

src/workerd/api/http-test-ts.ts-wd-test

+18-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const unitTests :Workerd.Config = (
1010
bindings = [
1111
( name = "SERVICE", service = "http-test" ),
1212
( name = "CACHE_ENABLED", json = "false" ),
13+
( name = "NO_CACHE_ENABLED", json = "false" ),
1314
],
1415
compatibilityDate = "2023-08-01",
1516
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_option_disabled"],
@@ -23,9 +24,24 @@ const unitTests :Workerd.Config = (
2324
bindings = [
2425
( name = "SERVICE", service = "http-test-cache-option-enabled" ),
2526
( name = "CACHE_ENABLED", json = "true" ),
27+
( name = "NO_CACHE_ENABLED", json = "false" ),
2628
],
27-
compatibilityDate = "2023-08-01",
28-
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_option_enabled"],
29+
compatibilityDate = "2024-11-11",
30+
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers"],
31+
)
32+
),
33+
( name = "http-test-cache-no-cache",
34+
worker = (
35+
modules = [
36+
( name = "worker-cache-no-cache", esModule = embed "http-test-ts.js" )
37+
],
38+
bindings = [
39+
( name = "SERVICE", service = "http-test-cache-no-cache" ),
40+
( name = "CACHE_ENABLED", json = "true" ),
41+
( name = "NO_CACHE_ENABLED", json = "true" ),
42+
],
43+
compatibilityDate = "2024-11-11",
44+
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_no_cache_enabled"],
2945
)
3046
),
3147
],

src/workerd/api/http.c++

+6-3
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ kj::Maybe<kj::String> Request::serializeCfBlobJson(jsg::Lock& js) {
12081208
break;
12091209
case CacheMode::NOCACHE:
12101210
ttl = 0;
1211-
KJ_FALLTHROUGH;
1211+
break;
12121212
case CacheMode::NONE:
12131213
KJ_UNREACHABLE;
12141214
}
@@ -1233,8 +1233,11 @@ void RequestInitializerDict::validate(jsg::Lock& js) {
12331233

12341234
// Validate that the cache type is valid
12351235
auto cacheMode = getCacheModeFromName(c);
1236-
JSG_REQUIRE(cacheMode != Request::CacheMode::NOCACHE, TypeError,
1237-
kj::str("Unsupported cache mode: ", c));
1236+
1237+
if (!FeatureFlags::get(js).getCacheNoCache()) {
1238+
JSG_REQUIRE(cacheMode != Request::CacheMode::NOCACHE, TypeError,
1239+
kj::str("Unsupported cache mode: ", c));
1240+
}
12381241
}
12391242
}
12401243

src/workerd/api/http.h

+30-12
Original file line numberDiff line numberDiff line change
@@ -753,12 +753,21 @@ struct RequestInitializerDict {
753753
referrer, referrerPolicy, integrity, signal);
754754
JSG_STRUCT_TS_OVERRIDE_DYNAMIC(CompatibilityFlags::Reader flags) {
755755
if(flags.getCacheOptionEnabled()) {
756-
JSG_TS_OVERRIDE(RequestInit<Cf = CfProperties> {
757-
headers?: HeadersInit;
758-
body?: BodyInit | null;
759-
cache?: 'no-store';
760-
cf?: Cf;
761-
});
756+
if(flags.getCacheNoCache()) {
757+
JSG_TS_OVERRIDE(RequestInit<Cf = CfProperties> {
758+
headers?: HeadersInit;
759+
body?: BodyInit | null;
760+
cache?: 'no-store' | 'no-cache';
761+
cf?: Cf;
762+
});
763+
} else {
764+
JSG_TS_OVERRIDE(RequestInit<Cf = CfProperties> {
765+
headers?: HeadersInit;
766+
body?: BodyInit | null;
767+
cache?: 'no-store';
768+
cf?: Cf;
769+
});
770+
}
762771
} else {
763772
JSG_TS_OVERRIDE(RequestInit<Cf = CfProperties> {
764773
headers?: HeadersInit;
@@ -927,12 +936,21 @@ class Request final: public Body {
927936
JSG_READONLY_PROTOTYPE_PROPERTY(keepalive, getKeepalive);
928937
if(flags.getCacheOptionEnabled()) {
929938
JSG_READONLY_PROTOTYPE_PROPERTY(cache, getCache);
930-
JSG_TS_OVERRIDE(<CfHostMetadata = unknown, Cf = CfProperties<CfHostMetadata>> {
931-
constructor(input: RequestInfo<CfProperties> | URL, init?: RequestInit<Cf>);
932-
clone(): Request<CfHostMetadata, Cf>;
933-
cache?: "no-store";
934-
get cf(): Cf | undefined;
935-
});
939+
if(flags.getCacheNoCache()) {
940+
JSG_TS_OVERRIDE(<CfHostMetadata = unknown, Cf = CfProperties<CfHostMetadata>> {
941+
constructor(input: RequestInfo<CfProperties> | URL, init?: RequestInit<Cf>);
942+
clone(): Request<CfHostMetadata, Cf>;
943+
cache?: "no-store" | "no-cache";
944+
get cf(): Cf | undefined;
945+
});
946+
} else {
947+
JSG_TS_OVERRIDE(<CfHostMetadata = unknown, Cf = CfProperties<CfHostMetadata>> {
948+
constructor(input: RequestInfo<CfProperties> | URL, init?: RequestInit<Cf>);
949+
clone(): Request<CfHostMetadata, Cf>;
950+
cache?: "no-store";
951+
get cf(): Cf | undefined;
952+
});
953+
}
936954
} else {
937955
JSG_TS_OVERRIDE(<CfHostMetadata = unknown, Cf = CfProperties<CfHostMetadata>> {
938956
constructor(input: RequestInfo<CfProperties> | URL, init?: RequestInit<Cf>);

src/workerd/io/compatibility-date.capnp

+6
Original file line numberDiff line numberDiff line change
@@ -668,4 +668,10 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef {
668668
# A bug in the original implementation of TransformStream failed to apply backpressure
669669
# correctly. The fix, however, can break existing implementations that don't account
670670
# for the bug so we need to put the fix behind a compat flag.
671+
672+
cacheNoCache @69 :Bool
673+
$compatEnableFlag("cache_no_cache_enabled")
674+
$compatDisableFlag("cache_no_cache_disabled")
675+
$experimental;
676+
# Enables the use of cache: no-cache in the fetch api.
671677
}

0 commit comments

Comments
 (0)