// The sites gate in the Durable Object constructor: PDS_EXPERIMENTAL_SITE_DOMAIN present // composes the handler and browser in; absent, the PDS has no site path and // foreign hostnames behave as before. import { describe, expect, it } from 'vitest'; import { createD1Shim } from '../../../test/helpers/d1-shim.js'; import { PDSDurableObject } from '../src/index.js'; /** A DurableObjectState double with real (in-memory) SQLite behind it. */ async function makeState() { const Database = (await import('better-sqlite3')).default; const { sql, storage } = createD1Shim(new Database(':memory:')); return /** @type {any} */ ({ storage: { sql, ...storage, getAlarm: async () => null, setAlarm: async () => {}, }, blockConcurrencyWhile: (/** @type {() => Promise} */ fn) => fn(), acceptWebSocket: () => {}, getWebSockets: () => [], }); } /** @param {Record} extra */ function makeEnv(extra = {}) { return /** @type {any} */ ({ JWT_SECRET: 'wiring-test-secret', HOSTNAME: 'pds.example.com', BLOBS: { get: async () => null, put: async () => {}, delete: async () => {}, }, ...extra, }); } describe('cloudflare sites wiring', () => { it('leaves sites off without PDS_EXPERIMENTAL_SITE_DOMAIN', async () => { const durable = new PDSDurableObject(await makeState(), makeEnv()); expect(durable.pds.extensionFeatures.sites).toBe(false); // A foreign hostname falls through to normal dispatch const response = await durable.fetch( new Request('https://blog.example.com/nope'), ); expect(response.status).toBe(404); expect(response.headers.get('access-control-allow-origin')).toBe('*'); }); it('composes handler and browser when PDS_EXPERIMENTAL_SITE_DOMAIN is set', async () => { const durable = new PDSDurableObject( await makeState(), makeEnv({ PDS_EXPERIMENTAL_SITE_DOMAIN: 'example.com' }), ); expect(durable.pds.extensionFeatures.sites).toBe(true); // A site hostname gets the site 404 page (nothing deployed yet in this // fresh database), with none of the PDS's header decoration. const response = await durable.fetch( new Request('https://blog.example.com/'), ); expect(response.status).toBe(404); expect(await response.text()).toContain('Nothing is deployed here'); expect(response.headers.get('access-control-allow-origin')).toBeNull(); // The PDS hostname is untouched const health = await durable.fetch( new Request('https://pds.example.com/xrpc/_health'), ); expect(health.status).toBe(200); }); });