// The sites gate in createServer: an experimental.sites.domain option composes the handler // and browser in; absent, foreign hostnames behave as before. import { mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { afterEach, describe, expect, it } from 'vitest'; import { createServer } from '../src/index.js'; /** @type {Array<{close: () => Promise}>} */ const servers = []; /** @type {string[]} */ const dirs = []; /** @param {{experimental?: {sites?: {domain?: string, apexName?: string}}}} [opts] */ async function makeServer(opts = {}) { const dir = mkdtempSync(join(tmpdir(), 'pdsjs-sites-wiring-')); dirs.push(dir); const server = await createServer({ dbPath: join(dir, 'pds.db'), blobsDir: join(dir, 'blobs'), jwtSecret: 'wiring-test-secret', hostname: 'pds.example.com', ...opts, }); servers.push(server); return server; } afterEach(async () => { for (const server of servers.splice(0)) { // close() rejects when listen() was never called; these tests only // construct the server. await server.close().catch(() => {}); } for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); describe('node sites wiring', () => { it('leaves sites off without a domain', async () => { const { pds } = await makeServer(); expect(pds.extensionFeatures.sites).toBe(false); const response = await pds.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 the domain is set', async () => { const { pds } = await makeServer({ experimental: { sites: { domain: 'example.com', apexName: 'www' } }, }); expect(pds.extensionFeatures.sites).toBe(true); const response = await pds.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(); const health = await pds.fetch( new Request('https://pds.example.com/xrpc/_health'), ); expect(health.status).toBe(200); }); });