Skip to content

Commit

Permalink
Add http cache mode autogate, initialize cache control headers, repea…
Browse files Browse the repository at this point in the history
…t of #2074
  • Loading branch information
AdityaAtulTewari committed Sep 10, 2024
1 parent 50fa4e7 commit f174a78
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 57 deletions.
25 changes: 24 additions & 1 deletion src/workerd/api/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("//:build/kj_test.bzl", "kj_test")
load("//:build/wd_cc_capnp_library.bzl", "wd_cc_capnp_library")
load("//:build/wd_cc_library.bzl", "wd_cc_library")
Expand Down Expand Up @@ -238,6 +239,12 @@ wd_test(
data = ["http-test.js"],
)

wd_test(
src = "http-test-no-autogate.wd-test",
args = ["--experimental"],
data = ["http-test.js"],
)

wd_test(
src = "queue-test.wd-test",
args = ["--experimental"],
Expand Down Expand Up @@ -470,10 +477,26 @@ wd_test(
data = ["tests/js-rpc-test.js"],
)

ts_project(
name = "http-test@ts_project",
srcs = ["http-test-ts.ts"],
allow_js = True,
composite = True,
source_map = True,
tsconfig = "tsconfig.json",
deps = ["//src/node:node.capnp@tsproject"],
)

wd_test(
src = "http-test-ts.ts-wd-test",
args = ["--experimental"],
data = ["http-test-ts.ts"],
data = ["http-test-ts.js"],
)

wd_test(
src = "http-test-ts-no-autogate.ts-wd-test",
args = ["--experimental"],
data = ["http-test-ts.js"],
)

# Enable GPU tests if experimental GPU support is enabled. Unfortunately, this depends on the right
Expand Down
33 changes: 33 additions & 0 deletions src/workerd/api/http-test-no-autogate.wd-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Workerd = import "/workerd/workerd.capnp";

const unitTests :Workerd.Config = (
services = [
( name = "http-test",
worker = (
modules = [
( name = "worker", esModule = embed "http-test.js" )
],
bindings = [
( name = "SERVICE", service = "http-test" ),
( name = "CACHE_ENABLED", json = "false" ),
( name = "CACHE_AUTOGATE_ENABLED", json = "false" ),
],
compatibilityDate = "2023-08-01",
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_option_disabled"],
)
),
( name = "http-test-cache-option-enabled",
worker = (
modules = [
( name = "worker-cache-enabled", esModule = embed "http-test.js" )
],
bindings = [
( name = "SERVICE", service = "http-test-cache-option-enabled" ),
( name = "CACHE_ENABLED", json = "true" ),
( name = "CACHE_AUTOGATE_ENABLED", json = "false" ),
],
compatibilityDate = "2023-08-01",
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_option_enabled"],
))
],
);
34 changes: 34 additions & 0 deletions src/workerd/api/http-test-ts-no-autogate.ts-wd-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Workerd = import "/workerd/workerd.capnp";

const unitTestsRequestCacheNoAutogate :Workerd.Config = (
services = [
( name = "http-test",
worker = (
modules = [
( name = "worker", esModule = embed "http-test-ts.js" )
],
bindings = [
( name = "SERVICE", service = "http-test" ),
( name = "CACHE_ENABLED", json = "false" ),
( name = "CACHE_AUTOGATE_ENABLED", json = "false" ),
],
compatibilityDate = "2023-08-01",
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_option_disabled"],
)
),
( name = "http-test-cache-option-enabled",
worker = (
modules = [
( name = "worker-cache-enabled", esModule = embed "http-test-ts.js" )
],
bindings = [
( name = "SERVICE", service = "http-test-cache-option-enabled" ),
( name = "CACHE_ENABLED", json = "true" ),
( name = "CACHE_AUTOGATE_ENABLED", json = "false" ),
],
compatibilityDate = "2023-08-01",
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_option_enabled"],
)
),
],
);
55 changes: 52 additions & 3 deletions src/workerd/api/http-test-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
// https://opensource.org/licenses/Apache-2.0

import assert from 'node:assert';
import util from 'node:util';

export default {
async fetch(request: any, env: any, ctx: any) {
const { pathname } = new URL(request.url);
return new Response(null, { status: 404 });
},
};

async function assertRequestCacheThrowsError(
cacheHeader: RequestCache,
Expand Down Expand Up @@ -42,7 +50,7 @@ async function assertFetchCacheRejectsError(

export const cacheMode = {
async test(ctrl: any, env: any, ctx: any) {
let allowedCacheModes: Array<RequestCache> = [
const allowedCacheModes: RequestCache[] = [
'default',
'force-cache',
'no-cache',
Expand All @@ -56,12 +64,53 @@ export const cacheMode = {
assert.strictEqual(req.cache, undefined);
}
if (!env.CACHE_ENABLED) {
for (var cacheMode of allowedCacheModes) {
for (const cacheMode of allowedCacheModes) {
await assertRequestCacheThrowsError(cacheMode);
await assertFetchCacheRejectsError(cacheMode);
}
} else {
for (var cacheMode of allowedCacheModes) {
var failureCacheModes: RequestCache[] = [
'default',
'no-cache',
'force-cache',
'only-if-cached',
'reload',
];
if (env.CACHE_AUTOGATE_ENABLED) {
{
const req = new Request('https://example.org', { cache: 'no-store' });
assert.strictEqual(req.cache, 'no-store');
}
{
const response = await env.SERVICE.fetch(
'http://placeholder/not-found',
{ cache: 'no-store' }
);
assert.strictEqual(
util.inspect(response),
`Response {
status: 404,
statusText: 'Not Found',
headers: Headers(0) { [immutable]: true },
ok: false,
redirected: false,
url: 'http://placeholder/not-found',
webSocket: null,
cf: undefined,
body: ReadableStream {
locked: false,
[state]: 'readable',
[supportsBYOB]: true,
[length]: 0n
},
bodyUsed: false
}`
);
}
} else {
failureCacheModes.push('no-store');
}
for (const cacheMode of failureCacheModes) {
await assertRequestCacheThrowsError(
cacheMode,
'TypeError',
Expand Down
7 changes: 6 additions & 1 deletion src/workerd/api/http-test-ts.ts-wd-test
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Workerd = import "/workerd/workerd.capnp";

const unitTests :Workerd.Config = (
const unitTestsRequestCacheAutogate :Workerd.Config = (
services = [
( name = "http-test",
worker = (
Expand All @@ -10,6 +10,7 @@ const unitTests :Workerd.Config = (
bindings = [
( name = "SERVICE", service = "http-test" ),
( name = "CACHE_ENABLED", json = "false" ),
( name = "CACHE_AUTOGATE_ENABLED", json = "true" ),
],
compatibilityDate = "2023-08-01",
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_option_disabled"],
Expand All @@ -23,10 +24,14 @@ const unitTests :Workerd.Config = (
bindings = [
( name = "SERVICE", service = "http-test-cache-option-enabled" ),
( name = "CACHE_ENABLED", json = "true" ),
( name = "CACHE_AUTOGATE_ENABLED", json = "true" ),
],
compatibilityDate = "2023-08-01",
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_option_enabled"],
)
),
],
autogates = [
"workerd-autogate-http-request-cache",
]
);
108 changes: 60 additions & 48 deletions src/workerd/api/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,61 +288,73 @@ async function assertFetchCacheRejectsError(

export const cacheMode = {
async test(ctrl, env, ctx) {
var failureCases = [
'default',
'force-cache',
'no-cache',
'no-store',
'only-if-cached',
'reload',
'unsupported',
];
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) {
await assertRequestCacheThrowsError('no-store');
await assertRequestCacheThrowsError('no-cache');
await assertRequestCacheThrowsError('no-transform');
await assertRequestCacheThrowsError('unsupported');
await assertFetchCacheRejectsError('no-store');
await assertFetchCacheRejectsError('no-cache');
await assertFetchCacheRejectsError('no-transform');
await assertFetchCacheRejectsError('unsupported');
failureCases.push('no-store');
for (const cacheMode in failureCases) {
await assertRequestCacheThrowsError(cacheMode);
await assertFetchCacheRejectsError(cacheMode);
}
} else {
await assertRequestCacheThrowsError(
'no-store',
'TypeError',
'Unsupported cache mode: no-store'
);
await assertRequestCacheThrowsError(
'no-cache',
'TypeError',
'Unsupported cache mode: no-cache'
);
await assertRequestCacheThrowsError(
'no-transform',
'TypeError',
'Unsupported cache mode: no-transform'
);
await assertRequestCacheThrowsError(
'unsupported',
'TypeError',
'Unsupported cache mode: unsupported'
);
await assertFetchCacheRejectsError(
'no-store',
'TypeError',
'Unsupported cache mode: no-store'
);
await assertFetchCacheRejectsError(
'no-cache',
'TypeError',
'Unsupported cache mode: no-cache'
);
await assertFetchCacheRejectsError(
'no-transform',
'TypeError',
'Unsupported cache mode: no-transform'
);
await assertFetchCacheRejectsError(
'unsupported',
'TypeError',
'Unsupported cache mode: unsupported'
);
if (env.CACHE_AUTOGATE_ENABLED) {
{
const req = new Request('https://example.org', { cache: 'no-store' });
assert.strictEqual(req.cache, 'no-store');
}
{
const response = await env.SERVICE.fetch(
'http://placeholder/not-found',
{ cache: 'no-store' }
);
assert.strictEqual(
util.inspect(response),
`Response {
status: 404,
statusText: 'Not Found',
headers: Headers(0) { [immutable]: true },
ok: false,
redirected: false,
url: 'http://placeholder/not-found',
webSocket: null,
cf: undefined,
body: ReadableStream {
locked: false,
[state]: 'readable',
[supportsBYOB]: true,
[length]: 0n
},
bodyUsed: false
}`
);
}
} else {
failureCases.push('no-store');
}
for (const cacheMode in failureCases) {
await assertRequestCacheThrowsError(
cacheMode,
'TypeError',
'Unsupported cache mode: ' + cacheMode
);
await assertFetchCacheRejectsError(
cacheMode,
'TypeError',
'Unsupported cache mode: ' + cacheMode
);
}
}
},
};
5 changes: 5 additions & 0 deletions src/workerd/api/http-test.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 = "CACHE_AUTOGATE_ENABLED", json = "true" ),
],
compatibilityDate = "2023-08-01",
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_option_disabled"],
Expand All @@ -23,9 +24,13 @@ const unitTests :Workerd.Config = (
bindings = [
( name = "SERVICE", service = "http-test-cache-option-enabled" ),
( name = "CACHE_ENABLED", json = "true" ),
( name = "CACHE_AUTOGATE_ENABLED", json = "true" ),
],
compatibilityDate = "2023-08-01",
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_option_enabled"],
))
],
autogates = [
"workerd-autogate-http-request-cache",
]
);
Loading

0 comments on commit f174a78

Please sign in to comment.