Skip to content

Commit

Permalink
Enable cache: no-cache compat flag and enum (#3195)
Browse files Browse the repository at this point in the history
  • Loading branch information
tewaro authored Dec 31, 2024
1 parent f37b4c6 commit 4ff974a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 44 deletions.
58 changes: 31 additions & 27 deletions src/workerd/api/http-test-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,40 +50,56 @@ async function assertFetchCacheRejectsError(

export const cacheMode = {
async test(ctrl: any, env: any, ctx: any) {
const allowedCacheModes: RequestCache[] = [
const allowedCacheModes: Set<RequestCache> = new Set([
'default',
'force-cache',
'no-cache',
'no-store',
'only-if-cached',
'reload',
];
]);
assert.strictEqual('cache' in Request.prototype, env.CACHE_ENABLED);
{
const req = new Request('https://example.org', {});
assert.strictEqual(req.cache, undefined);
}
if (!env.CACHE_ENABLED) {
for (const cacheMode of allowedCacheModes) {

var enabledCacheModes: Set<RequestCache> = new Set();

if (env.CACHE_ENABLED) {
enabledCacheModes.add('no-store');
}
if (env.NO_CACHE_ENABLED) {
enabledCacheModes.add('no-cache');
}

const failureCacheModes = allowedCacheModes.difference(enabledCacheModes);
for (const cacheMode of failureCacheModes) {
if (!env.CACHE_ENABLED) {
await assertRequestCacheThrowsError(cacheMode);
await assertFetchCacheRejectsError(cacheMode);
} else {
await assertRequestCacheThrowsError(
cacheMode,
'TypeError',
'Unsupported cache mode: ' + cacheMode
);
await assertFetchCacheRejectsError(
cacheMode,
'TypeError',
'Unsupported cache mode: ' + cacheMode
);
}
} else {
var failureCacheModes: RequestCache[] = [
'default',
'no-cache',
'force-cache',
'only-if-cached',
'reload',
];
}
for (const cacheMode of enabledCacheModes) {
{
const req = new Request('https://example.org', { cache: 'no-store' });
assert.strictEqual(req.cache, 'no-store');
const req = new Request('https://example.org', { cache: cacheMode });
assert.strictEqual(req.cache, cacheMode);
}
{
const response = await env.SERVICE.fetch(
'http://placeholder/not-found',
{ cache: 'no-store' }
{ cache: cacheMode }
);
assert.strictEqual(
util.inspect(response),
Expand All @@ -106,18 +122,6 @@ export const cacheMode = {
}`
);
}
for (const cacheMode of failureCacheModes) {
await assertRequestCacheThrowsError(
cacheMode,
'TypeError',
'Unsupported cache mode: ' + cacheMode
);
await assertFetchCacheRejectsError(
cacheMode,
'TypeError',
'Unsupported cache mode: ' + cacheMode
);
}
}
},
};
20 changes: 18 additions & 2 deletions src/workerd/api/http-test-ts.ts-wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const unitTests :Workerd.Config = (
bindings = [
( name = "SERVICE", service = "http-test" ),
( name = "CACHE_ENABLED", json = "false" ),
( name = "NO_CACHE_ENABLED", json = "false" ),
],
compatibilityDate = "2023-08-01",
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_option_disabled"],
Expand All @@ -23,9 +24,24 @@ const unitTests :Workerd.Config = (
bindings = [
( name = "SERVICE", service = "http-test-cache-option-enabled" ),
( name = "CACHE_ENABLED", json = "true" ),
( name = "NO_CACHE_ENABLED", json = "false" ),
],
compatibilityDate = "2023-08-01",
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_option_enabled"],
compatibilityDate = "2024-11-11",
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers"],
)
),
( name = "http-test-cache-no-cache",
worker = (
modules = [
( name = "worker-cache-no-cache", esModule = embed "http-test-ts.js" )
],
bindings = [
( name = "SERVICE", service = "http-test-cache-no-cache" ),
( name = "CACHE_ENABLED", json = "true" ),
( name = "NO_CACHE_ENABLED", json = "true" ),
],
compatibilityDate = "2024-11-11",
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_no_cache_enabled"],
)
),
],
Expand Down
9 changes: 6 additions & 3 deletions src/workerd/api/http.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ kj::Maybe<kj::String> Request::serializeCfBlobJson(jsg::Lock& js) {
break;
case CacheMode::NOCACHE:
ttl = 0;
KJ_FALLTHROUGH;
break;
case CacheMode::NONE:
KJ_UNREACHABLE;
}
Expand All @@ -1239,8 +1239,11 @@ void RequestInitializerDict::validate(jsg::Lock& js) {

// Validate that the cache type is valid
auto cacheMode = getCacheModeFromName(c);
JSG_REQUIRE(cacheMode != Request::CacheMode::NOCACHE, TypeError,
kj::str("Unsupported cache mode: ", c));

if (!FeatureFlags::get(js).getCacheNoCache()) {
JSG_REQUIRE(cacheMode != Request::CacheMode::NOCACHE, TypeError,
kj::str("Unsupported cache mode: ", c));
}
}
}

Expand Down
42 changes: 30 additions & 12 deletions src/workerd/api/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -755,12 +755,21 @@ struct RequestInitializerDict {
referrer, referrerPolicy, integrity, signal);
JSG_STRUCT_TS_OVERRIDE_DYNAMIC(CompatibilityFlags::Reader flags) {
if(flags.getCacheOptionEnabled()) {
JSG_TS_OVERRIDE(RequestInit<Cf = CfProperties> {
headers?: HeadersInit;
body?: BodyInit | null;
cache?: 'no-store';
cf?: Cf;
});
if(flags.getCacheNoCache()) {
JSG_TS_OVERRIDE(RequestInit<Cf = CfProperties> {
headers?: HeadersInit;
body?: BodyInit | null;
cache?: 'no-store' | 'no-cache';
cf?: Cf;
});
} else {
JSG_TS_OVERRIDE(RequestInit<Cf = CfProperties> {
headers?: HeadersInit;
body?: BodyInit | null;
cache?: 'no-store';
cf?: Cf;
});
}
} else {
JSG_TS_OVERRIDE(RequestInit<Cf = CfProperties> {
headers?: HeadersInit;
Expand Down Expand Up @@ -929,12 +938,21 @@ class Request final: public Body {
JSG_READONLY_PROTOTYPE_PROPERTY(keepalive, getKeepalive);
if(flags.getCacheOptionEnabled()) {
JSG_READONLY_PROTOTYPE_PROPERTY(cache, getCache);
JSG_TS_OVERRIDE(<CfHostMetadata = unknown, Cf = CfProperties<CfHostMetadata>> {
constructor(input: RequestInfo<CfProperties> | URL, init?: RequestInit<Cf>);
clone(): Request<CfHostMetadata, Cf>;
cache?: "no-store";
get cf(): Cf | undefined;
});
if(flags.getCacheNoCache()) {
JSG_TS_OVERRIDE(<CfHostMetadata = unknown, Cf = CfProperties<CfHostMetadata>> {
constructor(input: RequestInfo<CfProperties> | URL, init?: RequestInit<Cf>);
clone(): Request<CfHostMetadata, Cf>;
cache?: "no-store" | "no-cache";
get cf(): Cf | undefined;
});
} else {
JSG_TS_OVERRIDE(<CfHostMetadata = unknown, Cf = CfProperties<CfHostMetadata>> {
constructor(input: RequestInfo<CfProperties> | URL, init?: RequestInit<Cf>);
clone(): Request<CfHostMetadata, Cf>;
cache?: "no-store";
get cf(): Cf | undefined;
});
}
} else {
JSG_TS_OVERRIDE(<CfHostMetadata = unknown, Cf = CfProperties<CfHostMetadata>> {
constructor(input: RequestInfo<CfProperties> | URL, init?: RequestInit<Cf>);
Expand Down
6 changes: 6 additions & 0 deletions src/workerd/io/compatibility-date.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -673,4 +673,10 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef {
tailWorkerUserSpans @69 :Bool
$compatEnableFlag("tail_worker_user_spans")
$experimental;

cacheNoCache @70 :Bool
$compatEnableFlag("cache_no_cache_enabled")
$compatDisableFlag("cache_no_cache_disabled")
$experimental;
# Enables the use of cache: no-cache in the fetch api.
}

0 comments on commit 4ff974a

Please sign in to comment.