Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion platform/packages/dispatch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"deploy": "node ../../scripts/wrangler-deploy.ts",
"dev": "wrangler dev",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json"
},
"dependencies": {
"@void/deploy-core": "workspace:*",
Expand All @@ -15,6 +15,7 @@
},
"devDependencies": {
"@cloudflare/workers-types": "catalog:platform",
"@types/node": "catalog:",
"typescript": "catalog:",
"vite-plus": "catalog:",
"wrangler": "catalog:platform"
Expand Down
4 changes: 3 additions & 1 deletion platform/packages/dispatch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,9 @@ export default {
}
const res = new Response(response.body, response);
res.headers.delete('X-Void-No-Match');
// Cache API and upstream responses have immutable headers. Strip the
// internal cache marker only from this writable client response.
res.headers.delete(SHARED_CACHE_POLICY_HEADER);
res.headers.set('X-Hosted-By', 'void.cloud');
return res;
};
Expand Down Expand Up @@ -862,7 +865,6 @@ export default {
}
}
}
ruled.headers.delete(SHARED_CACHE_POLICY_HEADER);
return meter(ruled, failureKind ? { failureKind } : undefined);
};

Expand Down
111 changes: 111 additions & 0 deletions platform/packages/dispatch/test/runtime/asset-cache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { createRequire } from 'node:module';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { rolldown } from 'rolldown';
import { afterAll, beforeAll, describe, expect, it } from 'vite-plus/test';

type LocalRuntime = {
dispatchFetch(url: string): Promise<Response>;
dispose(): Promise<void>;
};

let runtime: LocalRuntime;

beforeAll(async () => {
const require = createRequire(import.meta.url);
const miniflarePath = createRequire(require.resolve('wrangler')).resolve('miniflare');
const { Miniflare, convertV4MiniflareOptions } = (await import(
pathToFileURL(miniflarePath).href
)) as {
Miniflare: new (options: Record<string, unknown>) => LocalRuntime;
convertV4MiniflareOptions(options: Record<string, unknown>): Record<string, unknown>;
};
const dispatchPath = fileURLToPath(new URL('../../src/index.ts', import.meta.url));
// Use the real Workers Cache API: Map-backed Response mocks have writable
// headers and cannot catch mutations of an immutable cache-hit response.
const fixture = `
import dispatch from ${JSON.stringify(dispatchPath)};
let originCalls = 0;
export default {
async fetch(request) {
const pathname = new URL(request.url).pathname;
const headerRules = pathname.includes('unmatched')
? [{ pattern: '/other/*', headers: [['X-Test', 'unmatched']] }]
: pathname.includes('matched')
? [{ pattern: '/assets/*', headers: [['X-Test', 'matched']] }]
: undefined;
const pending = [];
const routing = {
workerName: 'test-worker', projectId: 'proj-test', ssr: true,
hashedAssetsPrefix: 'assets', headerRules,
};
const response = await dispatch.fetch(request, {
ROUTING: { get: async () => JSON.stringify(routing) },
DISPATCH: { get: () => ({ fetch: async () => {
originCalls++;
return new Response('export const value = 42;', {
headers: { 'Content-Type': 'text/javascript', 'X-Origin-Call': String(originCalls) },
});
} }) },
ANALYTICS: { writeDataPoint() {} },
}, { waitUntil: promise => pending.push(promise) });
await Promise.all(pending);
return response;
},
};
`;
const build = await rolldown({
input: 'virtual:asset-cache-runtime',
plugins: [
{
name: 'asset-cache-runtime',
resolveId: (id) => (id === 'virtual:asset-cache-runtime' ? id : undefined),
load: (id) => (id === 'virtual:asset-cache-runtime' ? fixture : undefined),
},
],
});
try {
const { output } = await build.generate({ format: 'esm' });
const entry = output.find((chunk) => chunk.type === 'chunk' && chunk.isEntry);
if (!entry || entry.type !== 'chunk') {
throw new Error('Dispatch test bundle is missing');
}
runtime = new Miniflare(
convertV4MiniflareOptions({
modules: true,
compatibilityDate: '2026-02-12',
script: entry.code,
}),
);
} finally {
await build.close();
}
});

afterAll(async () => {
await runtime?.dispose();
});

describe('hashed assets in the Workers runtime', () => {
it.each(['no-rules', 'unmatched-rules', 'matched-rules'])(
'serves repeated cache hits with %s',
async (scenario) => {
const url = `https://test.void.app/assets/${scenario}.js`;
const miss = await runtime.dispatchFetch(url);
expect(miss.status).toBe(200);
const originCall = miss.headers.get('X-Origin-Call');
expect(originCall).not.toBeNull();
expect(await miss.text()).toBe('export const value = 42;');

for (let i = 0; i < 2; i++) {
const hit = await runtime.dispatchFetch(url);
expect(hit.status).toBe(200);
expect(await hit.text()).toBe('export const value = 42;');
expect(hit.headers.get('X-Origin-Call')).toBe(originCall);
expect(hit.headers.get('Content-Type')).toBe('text/javascript');
expect(hit.headers.get('Cache-Control')).toBe('public, max-age=31536000, immutable');
expect(hit.headers.get('x-void-shared-cache-policy')).toBeNull();
expect(hit.headers.get('X-Test')).toBe(scenario === 'matched-rules' ? 'matched' : null);
}
},
);
});
3 changes: 2 additions & 1 deletion platform/packages/dispatch/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"types": ["@cloudflare/workers-types"],
"allowImportingTsExtensions": true
},
"include": ["src", "test"]
"include": ["src", "test"],
"exclude": ["test/runtime"]
}
8 changes: 8 additions & 0 deletions platform/packages/dispatch/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"types": ["node"]
},
"include": ["test/runtime"],
"exclude": []
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading