From 045794b04f880112c9ea03374ca30a4fbf686339 Mon Sep 17 00:00:00 2001 From: AussieScorcher Date: Tue, 4 Aug 2026 23:53:00 +0800 Subject: [PATCH 01/15] chore: streamline project tooling --- benchmarks/backend-behavior.ts | 431 ------------------------------- benchmarks/backend.ts | 196 -------------- benchmarks/compute.ts | 334 ------------------------ benchmarks/database.ts | 170 ------------- benchmarks/realtime-behavior.ts | 367 -------------------------- benchmarks/realtime.ts | 438 -------------------------------- benchmarks/routes.ts | 114 --------- benchmarks/smoke.ts | 96 ------- bun.lock | 22 +- package.json | 34 +-- 10 files changed, 25 insertions(+), 2177 deletions(-) delete mode 100644 benchmarks/backend-behavior.ts delete mode 100644 benchmarks/backend.ts delete mode 100644 benchmarks/compute.ts delete mode 100644 benchmarks/database.ts delete mode 100644 benchmarks/realtime-behavior.ts delete mode 100644 benchmarks/realtime.ts delete mode 100644 benchmarks/routes.ts delete mode 100644 benchmarks/smoke.ts diff --git a/benchmarks/backend-behavior.ts b/benchmarks/backend-behavior.ts deleted file mode 100644 index dc6d56c..0000000 --- a/benchmarks/backend-behavior.ts +++ /dev/null @@ -1,431 +0,0 @@ -import assert from 'node:assert/strict'; -import { mock } from 'bun:test'; -import { AirportService, parseAirportCreateInput, parseAirportUpdateInput, type AirportRecord } from '../src/services/airport'; -import { AuthService } from '../src/services/auth'; -import { CacheService, withCache } from '../src/services/cache'; -import { ContactService } from '../src/services/contact'; -import { ContributionService, type Contribution } from '../src/services/contributions'; -import { StorageService } from '../src/services/storage'; -import { VatsimService } from '../src/services/vatsim'; - -mock.module('cloudflare:workers', () => ({ - DurableObject: class {}, - waitUntil: () => undefined, -})); - -const parsedAirport = parseAirportCreateInput({ - icao: ' yxyz ', - latitude: -31.25, - longitude: 115.75, - name: 'New Test Airport', - continent: 'oc', - country_code: 'au', - elevation_ft: 100, - runways: [ - { - length_ft: 5000, - width_ft: '150', - le_ident: '09', - le_latitude_deg: -31.26, - le_longitude_deg: 115.7, - he_ident: '27', - he_latitude_deg: -31.24, - he_longitude_deg: 115.8, - }, - ], -}); -assert.equal(parsedAirport.icao, 'YXYZ'); -assert.equal(parsedAirport.continent, 'OC'); -assert.equal(parsedAirport.country_code, 'AU'); -assert.equal(parsedAirport.elevation_m, 30.48); -assert.equal(parsedAirport.runways[0].length_ft, '5000'); -assert.throws(() => parseAirportUpdateInput({ bbox_min_lat: -31 }), /All four bounding-box fields must be supplied together/); -assert.throws(() => parseAirportUpdateInput({ icao: 'YSSY' }), /Unknown or immutable field: icao/); -assert.throws( - () => - parseAirportUpdateInput({ - runways: [ - { - length_ft: 5000, - width_ft: 150, - le_ident: '09', - le_latitude_deg: -31, - le_longitude_deg: 115, - he_ident: '27', - he_latitude_deg: -31.1, - he_longitude_deg: 115.1, - typo: true, - }, - ], - }), - /Unknown field: runways\[0\]\.typo/, -); -assert.throws( - () => - parseAirportCreateInput({ - icao: 'YXYZ', - latitude: 91, - longitude: 115.75, - name: 'Invalid Airport', - continent: 'OC', - }), - /latitude must be a number between -90 and 90/, -); - -interface FakeAirportStatement { - query: string; - params: unknown[]; -} - -const airportBatches: FakeAirportStatement[][] = []; -const airportRow: AirportRecord = { - icao: 'YXYZ', - latitude: -31.25, - longitude: 115.75, - name: 'New Test Airport', - continent: 'OC', - country_code: 'AU', - country_name: null, - region_name: null, - elevation_ft: 100, - elevation_m: 30.48, - bbox_min_lat: null, - bbox_min_lon: null, - bbox_max_lat: null, - bbox_max_lon: null, -}; -const airportReadRow: AirportRecord = { - ...airportRow, - country_name: 'Australia', - region_name: 'Western Australia', - bbox_min_lat: -31.3, - bbox_min_lon: 115.6, - bbox_max_lat: -31.2, - bbox_max_lon: 115.9, -}; -const fakeAirportDb = { - withSession: () => ({ - prepare: (query: string) => { - let params: unknown[] = []; - const statement = { - query, - get params() { - return params; - }, - bind: (...values: unknown[]) => { - params = values; - return statement; - }, - }; - return statement; - }, - batch: async (statements: FakeAirportStatement[]) => { - airportBatches.push(statements.map((statement) => ({ query: statement.query, params: statement.params }))); - return statements.map((statement, index) => { - let results: unknown[] = []; - if (statement.query.includes('SELECT id') && statement.query.includes('FROM division_airports')) { - results = [{ id: 42 }]; - } else if (index === 0) { - results = statement.query.trimStart().startsWith('SELECT') - ? [{ ...airportReadRow, ...(statement.query.includes(') AS id') ? { id: 42 } : {}) }] - : [airportRow]; - } else if (statement.query.includes('SELECT length_ft')) { - results = parsedAirport.runways; - } - return { success: true, results, meta: {} }; - }); - }, - getBookmark: () => null, - }), -} as unknown as D1Database; -const airportService = new AirportService(fakeAirportDb, ''); -assert.deepEqual(await airportService.createAirport(parsedAirport), { ...airportRow, runways: parsedAirport.runways }); -assert.equal(airportBatches.length, 1, 'airport and runways should be created in one D1 batch'); -assert.equal(airportBatches[0].length, 2); -assert(airportBatches[0][1].query.includes('WHERE EXISTS')); - -const parsedUpdate = parseAirportUpdateInput({ name: 'Renamed Airport', elevation_ft: 200, runways: [] }); -const updatedAirport = await airportService.updateAirport('YXYZ', parsedUpdate); -assert.equal(updatedAirport.name, airportRow.name); -assert.equal(airportBatches.length, 2, 'airport patch and runway replacement should use one D1 batch'); -assert(airportBatches[1][0].query.includes('UPDATE airports SET name = ?, elevation_ft = ?, elevation_m = ?')); -assert(airportBatches[1].some((statement) => statement.query.includes('DELETE FROM runways'))); -assert(airportBatches[1].some((statement) => statement.query.includes('SELECT length_ft'))); - -const fetchedAirport = await airportService.getAirport('YXYZ'); -assert.equal(fetchedAirport?.id, 42, 'ICAO airport lookup should expose the approved division airport ID'); -assert.equal(airportBatches.length, 3); -assert(airportBatches[2].some((statement) => statement.query.includes("status = 'approved'"))); -const fetchedAirports = await airportService.getAirports(['YXYZ']); -assert.equal((fetchedAirports.YXYZ as { id: number }).id, 42, 'batch ICAO lookups should expose the same numeric ID'); - -class FakeCache { - readonly entries = new Map(); - matchCalls: string[] = []; - putCalls: string[] = []; - - async match(request: Request): Promise { - this.matchCalls.push(request.url); - return this.entries.get(request.url)?.clone(); - } - - async put(request: Request, response: Response): Promise { - this.putCalls.push(request.url); - this.entries.set(request.url, response.clone()); - } - - async delete(request: Request): Promise { - return this.entries.delete(request.url); - } -} - -const fakeCache = new FakeCache(); -Object.defineProperty(globalThis, 'caches', { - configurable: true, - value: { default: fakeCache }, -}); - -const originalDateNow = Date.now; -let currentTime = 1_000_000; -Date.now = () => currentTime; -try { - const namespace = `backend-behavior-${crypto.randomUUID()}`; - const cache = new CacheService({} as Env); - const misses = await Promise.all(Array.from({ length: 100 }, (_, index) => cache.getResponse(`missing-${index}`, namespace))); - assert(misses.every((value) => value === null)); - assert.equal(fakeCache.matchCalls.filter((url) => url.includes('/__version/')).length, 1); - assert.equal(fakeCache.matchCalls.filter((url) => url.includes(`/${namespace}/v1/`)).length, 100); - - await cache.getResponse('another-miss', namespace); - assert.equal(fakeCache.matchCalls.filter((url) => url.includes('/__version/')).length, 1, 'fresh hints avoid marker reads'); - - currentTime += 5_001; - await cache.getResponse('after-revalidation-window', namespace); - assert.equal(fakeCache.matchCalls.filter((url) => url.includes('/__version/')).length, 2, 'stale hints revalidate'); - - const nextVersion = await cache.bumpNamespaceVersion(namespace); - assert.equal(nextVersion, 2); - await cache.set('versioned-hit', { ok: true }, { namespace }); - assert.deepEqual(await cache.get<{ ok: boolean }>('versioned-hit', namespace), { ok: true }); - assert(fakeCache.putCalls.some((url) => url.includes(`/${namespace}/v2/versioned-hit`))); - - const xmlNamespace = `xml-behavior-${crypto.randomUUID()}`; - const middleware = withCache(() => 'latest-map', 60, xmlNamespace); - const background: Promise[] = []; - let handlerCalls = 0; - const makeContext = () => ({ - req: { method: 'GET', raw: new Request('https://api.stopbars.test/maps/YSSY/latest') }, - env: {} as Env, - res: new Response(null), - header: () => undefined, - executionCtx: { waitUntil: (promise: Promise) => void background.push(promise) }, - }); - const firstContext = makeContext(); - await middleware(firstContext as never, async () => { - handlerCalls += 1; - firstContext.res = new Response('', { headers: { 'Content-Type': 'application/xml' } }); - }); - await Promise.all(background.splice(0)); - const secondContext = makeContext(); - const cachedXml = await middleware(secondContext as never, async () => { - handlerCalls += 1; - }); - assert(cachedXml instanceof Response); - assert.equal(await cachedXml.text(), ''); - assert.equal(cachedXml.headers.get('X-Cache'), 'HIT'); - assert.equal(handlerCalls, 1, 'XML responses must be served from Cache API after the first request'); -} finally { - Date.now = originalDateNow; -} - -const originalFetch = globalThis.fetch; -let connectionFetches = 0; -globalThis.fetch = (async () => { - connectionFetches += 1; - await Promise.resolve(); - return new Response('1234567,YSSY_TWR,atc'); -}) as typeof fetch; -try { - const vatsim = new VatsimService('client', 'secret'); - const [status, csv] = await Promise.all([vatsim.getUserStatus('1234567'), vatsim.getUserConnectionsCsv('1234567')]); - assert.deepEqual(status, { cid: '1234567', callsign: 'YSSY_TWR', type: 'atc' }); - assert.equal(csv, '1234567,YSSY_TWR,atc'); - assert.equal(vatsim.isController(status), true); - assert.equal(vatsim.isController({ callsign: 'YSSY_APP_TWR', type: 'atc' }), true); - assert.equal(vatsim.isController({ callsign: 'YSSY_', type: 'atc' }), false); - assert.equal(vatsim.isObserver({ callsign: 'YSSY_OBS', type: 'atc' }), true); - assert.equal(connectionFetches, 1, 'concurrent status consumers must share one upstream request'); - await vatsim.getUserStatus('1234567'); - assert.equal(connectionFetches, 2, 'completed requests are not cached or made stale'); - assert.equal(await vatsim.getUserStatus('invalid'), null); - assert.equal(connectionFetches, 2); -} finally { - globalThis.fetch = originalFetch; -} - -const rangeRequests: Array<{ key: string; options?: R2GetOptions }> = []; -const fakeBucket = { - get: async (key: string, options?: R2GetOptions) => { - rangeRequests.push({ key, options }); - const ranged = Boolean(options?.range); - return { - body: new Response(ranged ? '2345' : '0123456789').body, - size: 10, - etag: 'etag-value', - httpEtag: '"etag-value"', - range: ranged ? { offset: 2, length: 4, suffix: undefined } : undefined, - writeHttpMetadata: (headers: Headers) => { - headers.set('Content-Type', 'application/octet-stream'); - headers.set('Cache-Control', 'public, max-age=60'); - }, - }; - }, -} as unknown as R2Bucket; -const storage = new StorageService(fakeBucket); -const ranged = await storage.getFile('/large.bin', new Headers({ Range: 'bytes=2-5' })); -assert(ranged); -assert.equal(rangeRequests[0].key, 'large.bin'); -assert(rangeRequests[0].options?.range instanceof Headers); -assert.equal(ranged.status, 206); -assert.equal(ranged.headers.get('Content-Range'), 'bytes 2-5/10'); -assert.equal(ranged.headers.get('Content-Length'), '4'); -assert.equal(ranged.headers.get('ETag'), '"etag-value"'); -assert.equal(await ranged.text(), '2345'); -const full = await storage.getFile('large.bin'); -assert(full); -assert.equal(rangeRequests[1].options, undefined); -assert.equal(full.status, 200); -assert.equal(await full.text(), '0123456789'); - -const worker = (await import('../src/index')).default; -const routeResponse = await worker.fetch( - new Request('https://api.stopbars.test/cdn/files/maps/YSSY.xml', { headers: { Range: 'bytes=2-5' } }), - { BARS_STORAGE: fakeBucket } as Env, - { waitUntil: () => undefined } as unknown as ExecutionContext, -); -assert.equal(routeResponse.status, 206); -assert.equal(rangeRequests.at(-1)?.key, 'maps/YSSY.xml', 'named Hono route must preserve nested R2 keys'); -assert.equal(routeResponse.headers.get('Content-Range'), 'bytes 2-5/10'); -assert.equal(await routeResponse.text(), '2345'); - -const queries: Array<{ query: string; params: unknown[] }> = []; -const queuedResults: unknown[][] = []; -const fakeDb = { - withSession: () => ({ - prepare: (query: string) => { - let params: unknown[] = []; - const statement = { - bind: (...values: unknown[]) => { - params = values; - return statement; - }, - all: async () => { - queries.push({ query, params }); - return { results: queuedResults.shift() ?? [], success: true, meta: {} }; - }, - run: async () => { - queries.push({ query, params }); - return { results: queuedResults.shift() ?? [], success: true, meta: {} }; - }, - }; - return statement; - }, - getBookmark: () => null, - }), -} as unknown as D1Database; -const contributions = new ContributionService(fakeDb, {} as never, '', {} as R2Bucket); -queuedResults.push([{ packageName: 'Sydney Ground', simulator: 'msfs2024' }]); -assert.deepEqual(await contributions.getLatestApprovedMapDescriptor('YSSY', 'Sydney Ground', 'msfs2024'), { - packageName: 'Sydney Ground', - simulator: 'msfs2024', -}); -assert(!queries[0].query.includes('submitted_xml')); -assert(!queries[0].query.includes('datetime(c.decision_date)')); -assert(queries[0].query.includes('ORDER BY c.decision_date DESC')); - -const contribution: Contribution = { - id: 'contribution-id', - userId: '7654321', - userDisplayName: 'Submitter', - airportIcao: 'YSSY', - packageName: 'Sydney Ground', - submittedXml: '', - notes: null, - simulator: 'msfs2024', - submissionDate: '2026-07-19T00:00:00.000Z', - status: 'pending', - rejectionReason: null, - decisionDate: null, -}; -queuedResults.push([{ ...contribution, actorIsProductManager: 1 }]); -type ContributionInternals = { - getContributionActionContext( - vatsimId: string, - contributionId: string, - ): Promise<{ isProductManager: boolean; contribution: Contribution | null } | null>; -}; -const actionContext = await (contributions as unknown as ContributionInternals).getContributionActionContext( - '1234567', - 'contribution-id', -); -assert.deepEqual(actionContext, { isProductManager: true, contribution }); -assert.equal(queries.length, 2, 'actor and contribution context must use one D1 query'); -assert(queries[1].query.includes('LEFT JOIN contributions c ON c.id = ?')); - -queuedResults.push([{ id: null, actorIsProductManager: 0 }]); -assert.deepEqual( - await (contributions as unknown as ContributionInternals).getContributionActionContext('1234567', 'missing'), - { isProductManager: false, contribution: null }, -); -queuedResults.push([]); -assert.equal( - await (contributions as unknown as ContributionInternals).getContributionActionContext('missing-user', 'contribution-id'), - null, -); - -const deleteQueryStart = queries.length; -queuedResults.push([ - { - id: 'delete-id', - userId: '1234567', - status: 'pending', - simulator: 'msfs2024', - actorIsProductManager: 0, - }, -]); -queuedResults.push([]); -assert.equal(await contributions.deleteContribution('delete-id', '1234567'), true); -const deleteQueries = queries.slice(deleteQueryStart); -assert.equal(deleteQueries.length, 2, 'delete authorization and mutation should use two D1 statements'); -assert(!deleteQueries[0].query.includes('submitted_xml'), 'delete authorization must not transfer contribution XML'); - -type AuthInternals = { - fetchLoginState(vatsimId: string): Promise; -}; -const auth = new AuthService(fakeDb, new VatsimService('client', 'secret')); -queuedResults.push([]); -await assert.rejects( - () => (auth as unknown as AuthInternals).fetchLoginState('1234567'), - /Failed to load login state/, - 'an impossible empty anchor result should fail with an explicit invariant error', -); - -const contact = new ContactService(fakeDb); -queuedResults.push([{ id: 'contact-id' }]); -assert.equal(await contact.deleteMessage('contact-id'), true); -assert(queries.at(-1)?.query.includes('DELETE FROM contact_messages WHERE id = ? RETURNING id')); -queuedResults.push([]); -assert.equal(await contact.deleteMessage('missing-contact'), false); - -console.log( - JSON.stringify({ - cacheCoalescing: 'passed', - cacheRevalidation: 'passed', - xmlCache: 'passed', - vatsimCoalescing: 'passed', - r2Range: 'passed', - nestedCdnRoute: 'passed', - contributionQueryShape: 'passed', - returningDelete: 'passed', - }), -); diff --git a/benchmarks/backend.ts b/benchmarks/backend.ts deleted file mode 100644 index b1bbd42..0000000 --- a/benchmarks/backend.ts +++ /dev/null @@ -1,196 +0,0 @@ -import assert from 'node:assert/strict'; -import { performance } from 'node:perf_hooks'; -import { CacheKeys } from '../src/services/cache'; - -type Variant = { name: string; iterations: number; run: () => unknown }; - -function checksum(value: unknown): number { - if (typeof value === 'string') return value.length; - if (typeof value === 'number') return value; - if (value instanceof Response) return value.status + value.headers.get('X-Cache')!.length; - if (value && typeof value === 'object') return Object.keys(value).length; - return 0; -} - -function benchmark(variants: Variant[]): Array> { - return variants.map((variant) => { - for (let index = 0; index < Math.min(2_000, variant.iterations); index++) variant.run(); - const samples: number[] = []; - let resultChecksum = 0; - for (let sample = 0; sample < 11; sample++) { - const startedAt = performance.now(); - for (let index = 0; index < variant.iterations; index++) resultChecksum += checksum(variant.run()); - samples.push(performance.now() - startedAt); - } - const ordered = samples.toSorted((left, right) => left - right); - const medianMs = ordered[5]; - return { - variant: variant.name, - iterations: variant.iterations, - medianMs: Number(medianMs.toFixed(3)), - nsPerOperation: Number(((medianMs * 1_000_000) / variant.iterations).toFixed(1)), - checksum: resultChecksum, - }; - }); -} - -const cacheRequest = new Request( - 'https://api.stopbars.test/contributions?summary=true&status=approved&airport=YSSY&simulator=msfs2024', -); -function currentCacheKey() { - return CacheKeys.fromUrl(cacheRequest); -} -function arrayCacheKey() { - const url = new URL(cacheRequest.url); - const path = url.pathname.replace(/[^A-Za-z0-9/_-]/g, ''); - const params = Array.from(url.searchParams.entries()) - .filter(([key]) => !/^auth(orization)?$/i.test(key)) - .sort(([left], [right]) => left.localeCompare(right)) - .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`) - .join('&'); - return params ? `${path}?${params}` : path; -} -function loopCacheKey() { - const url = new URL(cacheRequest.url); - const path = url.pathname.replace(/[^A-Za-z0-9/_-]/g, ''); - const entries: Array<[string, string]> = []; - for (const [key, value] of url.searchParams) { - if (!/^auth(orization)?$/i.test(key)) entries.push([key, value]); - } - entries.sort(([left], [right]) => left.localeCompare(right)); - let params = ''; - for (const [key, value] of entries) { - if (params) params += '&'; - params += `${encodeURIComponent(key)}=${encodeURIComponent(value)}`; - } - return params ? `${path}?${params}` : path; -} -assert.equal(loopCacheKey(), currentCacheKey()); -assert.equal(arrayCacheKey(), currentCacheKey()); - -const cachedPayload = JSON.stringify({ items: Array.from({ length: 30 }, (_, index) => ({ id: index, active: true })) }); -const cachedHeaders = { 'Content-Type': 'application/json', 'Cache-Control': 'max-age=60', ETag: '"cache-test"' }; -function rewriteCachedResponseWithHeadersClone() { - const cached = new Response(cachedPayload, { headers: cachedHeaders }); - const headers = new Headers(cached.headers); - headers.delete('Cache-Control'); - headers.set('X-Cache', 'HIT'); - return new Response(cached.body, { status: cached.status, statusText: cached.statusText, headers }); -} -function rewriteCachedResponseDirectly() { - const cached = new Response(cachedPayload, { headers: cachedHeaders }); - const response = new Response(cached.body, cached); - response.headers.delete('Cache-Control'); - response.headers.set('X-Cache', 'HIT'); - return response; -} -const currentResponse = rewriteCachedResponseWithHeadersClone(); -const directResponse = rewriteCachedResponseDirectly(); -assert.equal(currentResponse.status, directResponse.status); -assert.deepEqual([...currentResponse.headers], [...directResponse.headers]); - -const submittedXml = `${''.repeat(2_000)}`; -const fullContribution = { - id: 'contribution-id', - userId: '1234567', - userDisplayName: 'Controller', - airportIcao: 'YSSY', - packageName: 'Sydney Ground', - submittedXml, - notes: null, - simulator: 'msfs2024', - submissionDate: '2026-07-19T00:00:00.000Z', - status: 'approved', - rejectionReason: null, - decisionDate: '2026-07-19T01:00:00.000Z', -}; -const mapDescriptor = { packageName: fullContribution.packageName, simulator: fullContribution.simulator }; -const fullContributionBytes = JSON.stringify(fullContribution).length; -const mapDescriptorBytes = JSON.stringify(mapDescriptor).length; -assert.equal(mapDescriptor.packageName, fullContribution.packageName); -assert.equal(mapDescriptor.simulator, fullContribution.simulator); - -const isoDates = [ - '2026-07-18T10:00:00.000Z', - '2026-07-19T01:00:00.000Z', - '2025-12-31T23:59:59.000Z', -]; -assert.deepEqual( - isoDates.toSorted((left, right) => right.localeCompare(left)), - isoDates.toSorted((left, right) => Date.parse(right) - Date.parse(left)), -); - -const connectionCsv = '1234567,YSSY_TWR,atc,120.500,100,-33.946,151.177'; -function repeatedTrimStatusParse() { - if (!connectionCsv.trim()) return null; - const [cid, callsign, type] = connectionCsv.trim().split(','); - return cid && callsign && type ? { cid, callsign, type } : null; -} -function singleTrimStatusParse() { - const trimmed = connectionCsv.trim(); - if (!trimmed) return null; - const [cid, callsign, type] = trimmed.split(','); - return cid && callsign && type ? { cid, callsign, type } : null; -} -assert.deepEqual(singleTrimStatusParse(), repeatedTrimStatusParse()); - -const controllerCallsign = 'YSSY_TWR'; -function splitCallsignSuffix() { - const parts = controllerCallsign.toUpperCase().split('_'); - return parts.length < 2 ? null : (parts[parts.length - 1] || null); -} -function lastIndexCallsignSuffix() { - const upper = controllerCallsign.toUpperCase(); - const separator = upper.lastIndexOf('_'); - return separator < 0 || separator === upper.length - 1 ? null : upper.slice(separator + 1); -} -assert.equal(lastIndexCallsignSuffix(), splitCallsignSuffix()); - -console.table( - benchmark([ - { name: 'cache key/array pipeline', iterations: 100_000, run: arrayCacheKey }, - { name: 'cache key/single collection loop', iterations: 100_000, run: loopCacheKey }, - { name: 'cache hit/clone Headers + Response', iterations: 20_000, run: rewriteCachedResponseWithHeadersClone }, - { name: 'cache hit/clone Response then mutate', iterations: 20_000, run: rewriteCachedResponseDirectly }, - { name: 'VATSIM CSV/repeated trim', iterations: 200_000, run: repeatedTrimStatusParse }, - { name: 'VATSIM CSV/single trim', iterations: 200_000, run: singleTrimStatusParse }, - { name: 'callsign suffix/split', iterations: 500_000, run: splitCallsignSuffix }, - { name: 'callsign suffix/lastIndexOf', iterations: 500_000, run: lastIndexCallsignSuffix }, - { name: 'latest map/full contribution JSON', iterations: 2_000, run: () => JSON.stringify(fullContribution) }, - { name: 'latest map/slim descriptor JSON', iterations: 2_000, run: () => JSON.stringify(mapDescriptor) }, - ]), -); - -const uniqueMisses = 1_000; -const coldBurst = 100; -console.log( - JSON.stringify({ - semanticEquality: true, - cacheApiCalls: { - uniqueMissesCurrent: uniqueMisses * 2, - uniqueMissesWithFreshVersionHint: uniqueMisses + 1, - coldBurstCurrent: coldBurst * 2, - coldBurstWithCoalescedVersionRead: coldBurst + 1, - }, - contributionActionD1CallsAfterRouteAuth: { - decision: { current: 2, batchedContext: 1 }, - delete: { current: 3, batchedContext: 1 }, - regenerate: { current: 4, batchedContext: 1 }, - }, - returningMutationD1CallsAfterAuthorization: { - contactDelete: { readThenDelete: 2, deleteReturning: 1 }, - divisionRename: { readThenUpdate: 2, updateReturning: 1 }, - }, - latestMapD1PayloadBytes: { - fullContribution: fullContributionBytes, - slimDescriptor: mapDescriptorBytes, - reduction: Number((fullContributionBytes / mapDescriptorBytes).toFixed(1)), - }, - latestMapQueryPlan: { - datetimeOrder: 'covering index search + temporary B-tree sort', - isoTextOrder: 'covering index search; no temporary sort', - }, - concurrentVatsimStatusFetches: { current: 2, coalesced: 1 }, - r2BytesForOneMegabyteRangeOfHundredMegabyteObject: { current: 100 * 1024 * 1024, ranged: 1024 * 1024 }, - }), -); diff --git a/benchmarks/compute.ts b/benchmarks/compute.ts deleted file mode 100644 index 95ecfec..0000000 --- a/benchmarks/compute.ts +++ /dev/null @@ -1,334 +0,0 @@ -import assert from 'node:assert/strict'; -import { performance } from 'node:perf_hooks'; -import { calculateDestinationPoint, calculateDistance, calculateHeading, generateEquidistantPoints } from '../src/services/bars/geoUtils'; -import type { GeoPoint } from '../src/services/bars/types'; -import { sanitizeContributionXml } from '../src/services/xml-sanitizer'; - -type Variant = { - name: string; - operationsPerSample: number; - run: () => number; -}; - -type Summary = { - name: string; - samplesMs: number[]; - medianMs: number; - minMs: number; - operationsPerSecond: number; - checksum: number; -}; - -const WARMUP_SAMPLES = 2; -const MEASURED_SAMPLES = 11; -// Mirrors the production XML 1.0 control-character filter being benchmarked. -// eslint-disable-next-line no-control-regex -const DISALLOWED_XML_CONTROL_CHARS = new RegExp('[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]', 'g'); - -function runVariant(variant: Variant): Summary { - const runSample = (): { elapsedMs: number; checksum: number } => { - let checksum = 0; - const started = performance.now(); - for (let index = 0; index < variant.operationsPerSample; index++) checksum += variant.run(); - return { elapsedMs: performance.now() - started, checksum }; - }; - - for (let index = 0; index < WARMUP_SAMPLES; index++) runSample(); - const samples: number[] = []; - let checksum = 0; - for (let index = 0; index < MEASURED_SAMPLES; index++) { - const sample = runSample(); - samples.push(sample.elapsedMs); - checksum += sample.checksum; - } - const ordered = [...samples].sort((a, b) => a - b); - const medianMs = ordered[Math.floor(ordered.length / 2)]; - return { - name: variant.name, - samplesMs: samples.map((sample) => Number(sample.toFixed(3))), - medianMs: Number(medianMs.toFixed(3)), - minMs: Number(ordered[0].toFixed(3)), - operationsPerSecond: Math.round((variant.operationsPerSample * 1000) / medianMs), - checksum, - }; -} - -// Previous implementation: creates an array entry for every Unicode code point, -// filters it, then joins the whole document again. -function stripControlsArrayFrom(value: string): string { - return Array.from(value) - .filter((character) => { - const code = character.charCodeAt(0); - return !(code <= 0x08 || code === 0x0b || code === 0x0c || (code >= 0x0e && code <= 0x1f)); - }) - .join(''); -} - -// Alternative considered: scan once and concatenate only the valid slices. -function stripControlsManualSlices(value: string): string { - let output = ''; - let sliceStart = 0; - for (let index = 0; index < value.length; index++) { - const code = value.charCodeAt(index); - if (code <= 0x08 || code === 0x0b || code === 0x0c || (code >= 0x0e && code <= 0x1f)) { - if (sliceStart < index) output += value.slice(sliceStart, index); - sliceStart = index + 1; - } - } - if (sliceStart === 0) return value; - return sliceStart < value.length ? output + value.slice(sliceStart) : output; -} - -// Selected implementation in xml-sanitizer.ts. -function stripControlsRegex(value: string): string { - return value.replace(DISALLOWED_XML_CONTROL_CHARS, ''); -} - -function generateEquidistantResetScan(points: GeoPoint[], interval: number): GeoPoint[] { - if (points.length < 2) return points; - if (interval <= 0) return points.map((point) => ({ ...point })); - const segmentLengths = new Array(points.length - 1); - const segmentBearings = new Array(points.length - 1); - for (let index = 0; index < points.length - 1; index++) { - segmentLengths[index] = calculateDistance(points[index], points[index + 1]); - segmentBearings[index] = calculateHeading(points[index], points[index + 1]); - } - const totalPathLength = segmentLengths.reduce((sum, length) => sum + length, 0); - if (totalPathLength === 0) return [{ ...points[0] }]; - const pointCount = Math.max(1, Math.floor(totalPathLength / interval) + 1); - const startOffset = Math.max(0, (totalPathLength - (pointCount - 1) * interval) / 2); - const result: GeoPoint[] = []; - for (let outputIndex = 0; outputIndex < pointCount; outputIndex++) { - const distance = Math.min(startOffset + outputIndex * interval, totalPathLength); - if (distance <= 0) { - result.push({ ...points[0] }); - continue; - } - let remaining = distance; - let segmentIndex = 0; - while (segmentIndex < segmentLengths.length) { - const length = segmentLengths[segmentIndex]; - if (remaining <= length) { - result.push(calculateDestinationPoint(points[segmentIndex], remaining, segmentBearings[segmentIndex])); - break; - } - remaining -= length; - segmentIndex++; - } - } - return result; -} - -// Alternative considered: cumulative lengths plus a binary search per output. -function generateEquidistantBinarySearch(points: GeoPoint[], interval: number): GeoPoint[] { - if (points.length < 2) return points; - if (interval <= 0) return points.map((point) => ({ ...point })); - const segmentLengths = new Array(points.length - 1); - const segmentBearings = new Array(points.length - 1); - const cumulative = new Array(points.length); - cumulative[0] = 0; - for (let index = 0; index < segmentLengths.length; index++) { - segmentLengths[index] = calculateDistance(points[index], points[index + 1]); - segmentBearings[index] = calculateHeading(points[index], points[index + 1]); - cumulative[index + 1] = cumulative[index] + segmentLengths[index]; - } - const totalPathLength = cumulative[cumulative.length - 1]; - if (totalPathLength === 0) return [{ ...points[0] }]; - const pointCount = Math.max(1, Math.floor(totalPathLength / interval) + 1); - const startOffset = Math.max(0, (totalPathLength - (pointCount - 1) * interval) / 2); - const result: GeoPoint[] = []; - for (let outputIndex = 0; outputIndex < pointCount; outputIndex++) { - const distance = Math.min(startOffset + outputIndex * interval, totalPathLength); - let low = 0; - let high = segmentLengths.length - 1; - while (low < high) { - const middle = (low + high) >>> 1; - if (distance <= cumulative[middle + 1]) high = middle; - else low = middle + 1; - } - result.push(calculateDestinationPoint(points[low], distance - cumulative[low], segmentBearings[low])); - } - return result; -} - -function generateEquidistantHybrid(points: GeoPoint[], interval: number, maxBinarySegments: number): GeoPoint[] { - if (points.length < 2) return points; - if (interval <= 0) return points.map((point) => ({ ...point })); - const segmentCount = points.length - 1; - const segmentLengths = new Array(segmentCount); - const segmentBearings = new Array(segmentCount); - const useResetScan = segmentCount <= 32; - const useBinarySearch = !useResetScan && segmentCount <= maxBinarySegments; - const cumulative = useBinarySearch ? new Array(points.length) : undefined; - if (cumulative) cumulative[0] = 0; - let totalPathLength = 0; - for (let index = 0; index < segmentCount; index++) { - const length = calculateDistance(points[index], points[index + 1]); - segmentLengths[index] = length; - segmentBearings[index] = calculateHeading(points[index], points[index + 1]); - totalPathLength += length; - if (cumulative) cumulative[index + 1] = totalPathLength; - } - if (totalPathLength === 0) return [{ ...points[0] }]; - const pointCount = Math.max(1, Math.floor(totalPathLength / interval) + 1); - const startOffset = Math.max(0, (totalPathLength - (pointCount - 1) * interval) / 2); - const result: GeoPoint[] = []; - if (useResetScan) { - for (let outputIndex = 0; outputIndex < pointCount; outputIndex++) { - const distance = Math.min(startOffset + outputIndex * interval, totalPathLength); - if (distance <= 0) { - result.push({ ...points[0] }); - continue; - } - let remaining = distance; - let segmentIndex = 0; - while (segmentIndex < segmentCount - 1 && remaining > segmentLengths[segmentIndex]) { - remaining -= segmentLengths[segmentIndex++]; - } - result.push(calculateDestinationPoint(points[segmentIndex], remaining, segmentBearings[segmentIndex])); - } - return result; - } - if (cumulative) { - for (let outputIndex = 0; outputIndex < pointCount; outputIndex++) { - const distance = Math.min(startOffset + outputIndex * interval, totalPathLength); - let low = 0; - let high = segmentCount - 1; - while (low < high) { - const middle = (low + high) >>> 1; - if (distance <= cumulative[middle + 1]) high = middle; - else low = middle + 1; - } - result.push(calculateDestinationPoint(points[low], distance - cumulative[low], segmentBearings[low])); - } - return result; - } - let segmentIndex = 0; - let segmentStartDistance = 0; - for (let outputIndex = 0; outputIndex < pointCount; outputIndex++) { - const distance = Math.min(startOffset + outputIndex * interval, totalPathLength); - if (distance <= 0) { - result.push({ ...points[0] }); - continue; - } - while (segmentIndex < segmentCount - 1 && distance - segmentStartDistance > segmentLengths[segmentIndex]) { - segmentStartDistance += segmentLengths[segmentIndex++]; - } - result.push(calculateDestinationPoint(points[segmentIndex], distance - segmentStartDistance, segmentBearings[segmentIndex])); - } - return result; -} - -function assertPointsEquivalent(actual: GeoPoint[], expected: GeoPoint[]): number { - assert.equal(actual.length, expected.length); - let maxDeltaMeters = 0; - for (let index = 0; index < actual.length; index++) { - const deltaMeters = calculateDistance(actual[index], expected[index]); - maxDeltaMeters = Math.max(maxDeltaMeters, deltaMeters); - assert.ok(deltaMeters <= 0.2, `point differs by ${deltaMeters} meters at ${index}`); - } - return maxDeltaMeters; -} - -const xmlNodes = Array.from({ length: 20_000 }, (_, index) => `value-${index}\u0001🙂`).join('\n'); -const dirtyXml = ` \n\n${xmlNodes} `; -const trimmedXml = dirtyXml.trim().replace(/(<\?)(?!xml)([\s\S]*?\?>)/gi, ''); -const expectedControls = stripControlsArrayFrom(trimmedXml); -assert.equal(stripControlsManualSlices(trimmedXml), expectedControls); -assert.equal(stripControlsRegex(trimmedXml), expectedControls); -assert.equal(sanitizeContributionXml(dirtyXml, { maxBytes: dirtyXml.length + 1 }), stripControlsRegex(trimmedXml).replace(/>\s+<')); - -const makePath = (pointCount: number): GeoPoint[] => - Array.from({ length: pointCount }, (_, index) => ({ - lat: -31.94 + index * 0.000045 + Math.sin(index / 8) * 0.00001, - lon: 115.96 + index * 0.00005, - })); -const geometryScenarios = [ - { name: 'short', points: makePath(16), interval: 11.25, operationsPerSample: 2_000 }, - { name: 'medium', points: makePath(80), interval: 3, operationsPerSample: 1_000 }, - { name: 'long', points: makePath(240), interval: 3, operationsPerSample: 400 }, - { name: 'crossover', points: makePath(400), interval: 3, operationsPerSample: 200 }, - { name: 'very-long', points: makePath(1_000), interval: 3, operationsPerSample: 50 }, -]; -let maxGeometryDeltaMeters = 0; -for (const scenario of geometryScenarios) { - const expectedPoints = generateEquidistantResetScan(scenario.points, scenario.interval); - maxGeometryDeltaMeters = Math.max( - maxGeometryDeltaMeters, - assertPointsEquivalent(generateEquidistantBinarySearch(scenario.points, scenario.interval), expectedPoints), - assertPointsEquivalent(generateEquidistantPoints(scenario.points, scenario.interval), expectedPoints), - assertPointsEquivalent(generateEquidistantHybrid(scenario.points, scenario.interval, 256), expectedPoints), - assertPointsEquivalent(generateEquidistantHybrid(scenario.points, scenario.interval, 512), expectedPoints), - ); -} - -const variants: Variant[] = [ - { name: 'xml/array-from (before)', operationsPerSample: 20, run: () => stripControlsArrayFrom(trimmedXml).length }, - { name: 'xml/manual-slices', operationsPerSample: 20, run: () => stripControlsManualSlices(trimmedXml).length }, - { name: 'xml/regex (current)', operationsPerSample: 20, run: () => stripControlsRegex(trimmedXml).length }, - ...geometryScenarios.flatMap(({ name, points, interval, operationsPerSample }) => [ - { - name: `geometry/${name}/reset-scan (before)`, - operationsPerSample, - run: () => generateEquidistantResetScan(points, interval).length, - }, - { - name: `geometry/${name}/binary-search`, - operationsPerSample, - run: () => generateEquidistantBinarySearch(points, interval).length, - }, - { - name: `geometry/${name}/single-sweep (current)`, - operationsPerSample, - run: () => generateEquidistantPoints(points, interval).length, - }, - { - name: `geometry/${name}/hybrid-256`, - operationsPerSample, - run: () => generateEquidistantHybrid(points, interval, 256).length, - }, - { - name: `geometry/${name}/hybrid-512`, - operationsPerSample, - run: () => generateEquidistantHybrid(points, interval, 512).length, - }, - ]), -]; - -const summaries = variants.map(runVariant); -const byName = new Map(summaries.map((summary) => [summary.name, summary])); -const speedups = { - xml: Number((byName.get('xml/array-from (before)')!.medianMs / byName.get('xml/regex (current)')!.medianMs).toFixed(2)), - geometry: Number( - ( - geometryScenarios.reduce( - (total, scenario) => total + byName.get(`geometry/${scenario.name}/reset-scan (before)`)!.medianMs, - 0, - ) / - geometryScenarios.reduce( - (total, scenario) => total + byName.get(`geometry/${scenario.name}/single-sweep (current)`)!.medianMs, - 0, - ) - ).toFixed(2), - ), -}; -const report = { - generatedAt: new Date().toISOString(), - runtime: process.version, - warmupSamples: WARMUP_SAMPLES, - measuredSamples: MEASURED_SAMPLES, - fixtures: { - xmlCharacters: dirtyXml.length, - geometry: geometryScenarios.map(({ name, points, interval }) => ({ name, pathPoints: points.length, intervalMeters: interval })), - }, - semanticEquality: { - xmlExact: true, - geometryToleranceMeters: 0.2, - maxGeometryDeltaMeters, - }, - speedups, - results: summaries, -}; - -console.table(summaries.map(({ name, medianMs, minMs, operationsPerSecond }) => ({ name, medianMs, minMs, operationsPerSecond }))); -console.log(JSON.stringify(report)); diff --git a/benchmarks/database.ts b/benchmarks/database.ts deleted file mode 100644 index 75a175a..0000000 --- a/benchmarks/database.ts +++ /dev/null @@ -1,170 +0,0 @@ -import assert from 'node:assert/strict'; -import { performance } from 'node:perf_hooks'; - -type PointRow = { id: string; airportId: string; name: string }; -type DownloadCounts = { versionCount: number; productTotal: number }; - -class FakeD1 { - public calls = 0; - public readonly points = new Map(); - public readonly faqOrder = new Map(); - public readonly downloads = new Map(); - - constructor() { - for (let i = 0; i < 200; i += 1) { - const id = `BARS_${String(i).padStart(5, '0')}`; - this.points.set(id, { id, airportId: `Y${String(i % 10).padStart(3, '0')}`, name: `Point ${i}` }); - } - for (let i = 0; i < 50; i += 1) this.faqOrder.set(`faq-${i}`, i); - this.downloads.set('Installer@1.0.0', 7); - this.downloads.set('Installer@0.9.0', 3); - } - - private transportCost(): void { - // Fixed work makes the benchmark deterministic while representing per-call - // binding/serialization overhead. It is intentionally not wall-clock sleep. - let state = 0x9e3779b9; - for (let i = 0; i < 12_000; i += 1) state = Math.imul(state ^ i, 1664525) + 1013904223; - if (state === 0) throw new Error('unreachable'); - } - - async call(operation: () => T): Promise { - this.calls += 1; - this.transportCost(); - return operation(); - } - - async batch(operations: Array<() => T>): Promise { - return this.call(() => operations.map((operation) => operation())); - } -} - -const requestedIds = [ - ...Array.from({ length: 98 }, (_, index) => `BARS_${String(index).padStart(5, '0')}`), - 'BARS_00007', // duplicate: order and duplicates must be preserved - 'BARS_99999', // missing: null position must be preserved -]; - -async function pointsSequential(db: FakeD1): Promise> { - const rows: Array = []; - for (const id of requestedIds) rows.push(await db.call(() => db.points.get(id) ?? null)); - return rows; -} - -async function pointsConcurrent(db: FakeD1): Promise> { - return Promise.all(requestedIds.map((id) => db.call(() => db.points.get(id) ?? null))); -} - -async function pointsInQuery(db: FakeD1): Promise> { - const unique = [...new Set(requestedIds)]; - const rows = await db.call(() => unique.flatMap((id) => (db.points.has(id) ? [db.points.get(id)!] : []))); - const byId = new Map(rows.map((row) => [row.id, row])); - return requestedIds.map((id) => byId.get(id) ?? null); -} - -async function faqSequential(db: FakeD1): Promise { - for (let i = 0; i < 50; i += 1) await db.call(() => db.faqOrder.set(`faq-${i}`, 49 - i)); - return [...db.faqOrder.values()]; -} - -async function faqBatch(db: FakeD1): Promise { - await db.batch(Array.from({ length: 50 }, (_, i) => () => db.faqOrder.set(`faq-${i}`, 49 - i))); - return [...db.faqOrder.values()]; -} - -async function airportSeparate(db: FakeD1): Promise<{ airport: PointRow | null; runways: string[] }> { - const airport = await db.call(() => db.points.get('BARS_00001') ?? null); - const runways = await db.call(() => ['01/19', '09/27']); - return { airport, runways }; -} - -async function airportBatch(db: FakeD1): Promise<{ airport: PointRow | null; runways: string[] }> { - const [airport, runways] = await db.batch([() => db.points.get('BARS_00001') ?? null, () => ['01/19', '09/27']]); - return { airport: airport as PointRow | null, runways: runways as string[] }; -} - -async function downloadLegacy(db: FakeD1): Promise { - const key = 'Installer@1.0.0'; - await db.call(() => db.downloads.has(key)); // ensure row - await db.call(() => null); // read IP hit - await db.call(() => true); // insert/update IP hit - const versionCount = await db.call(() => { - const count = (db.downloads.get(key) ?? 0) + 1; - db.downloads.set(key, count); - return count; - }); - await db.call(() => true); // expired-hit cleanup - const productTotal = await db.call(() => [...db.downloads.values()].reduce((sum, count) => sum + count, 0)); - return { versionCount, productTotal }; -} - -async function downloadBatched(db: FakeD1): Promise { - const key = 'Installer@1.0.0'; - await db.batch([() => db.downloads.has(key), () => true, () => db.downloads.get(key) ?? 0]); - const [versionCount] = await db.batch([ - () => { - const count = (db.downloads.get(key) ?? 0) + 1; - db.downloads.set(key, count); - return count; - }, - () => true, - ]); - return { - versionCount: versionCount as number, - productTotal: [...db.downloads.values()].reduce((sum, count) => sum + count, 0), - }; -} - -type Variant = { name: string; run: (db: FakeD1) => Promise }; - -async function benchmarkGroup(variants: Variant[]): Promise>> { - const expected = await variants[0].run(new FakeD1()); - for (const variant of variants.slice(1)) assert.deepEqual(await variant.run(new FakeD1()), expected, `${variant.name} changed results`); - - for (const variant of variants) { - for (let i = 0; i < 20; i += 1) await variant.run(new FakeD1()); - } - - const rows: Array> = []; - for (const variant of variants) { - const timings: number[] = []; - let calls = 0; - for (let i = 0; i < 100; i += 1) { - const db = new FakeD1(); - const start = performance.now(); - await variant.run(db); - timings.push(performance.now() - start); - calls = db.calls; - } - timings.sort((a, b) => a - b); - rows.push({ - variant: variant.name, - calls, - p50Ms: Number(timings[50].toFixed(3)), - p95Ms: Number(timings[95].toFixed(3)), - }); - } - return rows; -} - -const results = [ - ...(await benchmarkGroup([ - { name: 'points: sequential first()', run: pointsSequential }, - { name: 'points: concurrent first()', run: pointsConcurrent }, - { name: 'points: one IN query', run: pointsInQuery }, - ])), - ...(await benchmarkGroup([ - { name: 'FAQ reorder: sequential writes', run: faqSequential }, - { name: 'FAQ reorder: D1 batch', run: faqBatch }, - ])), - ...(await benchmarkGroup([ - { name: 'airport: separate airport/runway reads', run: airportSeparate }, - { name: 'airport: read batch', run: airportBatch }, - ])), - ...(await benchmarkGroup([ - { name: 'download: legacy fresh-hit path', run: downloadLegacy }, - { name: 'download: batched upsert path', run: downloadBatched }, - ])), -]; - -console.table(results); diff --git a/benchmarks/realtime-behavior.ts b/benchmarks/realtime-behavior.ts deleted file mode 100644 index 97340ec..0000000 --- a/benchmarks/realtime-behavior.ts +++ /dev/null @@ -1,367 +0,0 @@ -import assert from 'node:assert/strict'; -import { mock } from 'bun:test'; -import type { AirportState, ClientType, Packet } from '../src/types'; -import type { AuthService } from '../src/services/auth'; -import type { VatsimService } from '../src/services/vatsim'; - -const cloudflareBackground: Promise[] = []; -mock.module('cloudflare:workers', () => ({ - waitUntil: (promise: Promise) => void cloudflareBackground.push(promise), -})); -const { Connection } = await import('../src/network/connection'); -const { PostHogService } = await import('../src/services/posthog'); - -class FakeSocket { - readyState = WebSocket.OPEN; - sent: string[] = []; - closed?: { code?: number; reason?: string }; - - send(value: string) { - this.sent.push(value); - } - - close(code?: number, reason?: string) { - this.readyState = WebSocket.CLOSED; - this.closed = { code, reason }; - } -} - -class FakeDurableObjectState { - readonly id = { toString: () => '0000000000000000000000000000000000000000000000000000000000000000' }; - readonly values = new Map(); - readonly background: Promise[] = []; - initialization: Promise = Promise.resolve(); - readonly storage = { - list: async ({ prefix }: { prefix: string }): Promise> => - new Map( - Array.from(this.values.entries()).filter(([key]) => key.startsWith(prefix)) as Array<[string, T]>, - ), - put: async (key: string, value: unknown) => void this.values.set(key, value), - delete: async (key: string) => this.values.delete(key), - }; - - blockConcurrencyWhile(callback: () => Promise) { - this.initialization = callback(); - return this.initialization; - } - - waitUntil(promise: Promise) { - this.background.push(promise); - } -} - -type InternalSocketInfo = { - controllerId: string; - type: ClientType; - airport: string; - lastHeartbeat: number; - lastStatusCheck: number; - statusCheckInFlight: boolean; - consecutiveVatsimFailures: number; - sendFailures: number; -}; - -type ConnectionInternals = { - sockets: Map; - airportStates: Map; - offlineStateCache: Map< - string, - { - template?: Array<{ id: string; state: boolean }>; - defaultStates?: ReadonlyMap; - expiresAt: number; - } - >; - registerSocket(socket: WebSocket, info: { controllerId: string; type: ClientType; airport: string; lastHeartbeat: number }): void; - unregisterSocket(socket: WebSocket): InternalSocketInfo | undefined; - validatePacket(packet: unknown): packet is Packet; - broadcast(packet: Packet, sender?: WebSocket, trackAnalytics?: boolean): number; - broadcastToControllers(packet: Packet, sender?: WebSocket, trackAnalytics?: boolean): number; - trackMessage( - details: { clientType: ClientType; messageType: Packet['type']; airport: string; meta?: Record }, - broadcastRecipients?: number, - ): void; - posthog: { - track(event: string, properties: Record): void; - trackBatch(events: readonly { event: string; properties?: Record }[]): void; - }; - handleMultiStateUpdate( - packet: Packet, - controllerId: string, - connectionAirport: string, - ): Promise<{ updates: unknown[]; timestamp: number; airport: string }>; - handleSharedStateUpdate(packet: Packet, controllerId: string, connectionAirport: string): Promise>; - checkSocketStatus(socket: WebSocket, socketInfo: InternalSocketInfo, now: number): Promise | undefined; - enqueueSocketTask(socket: WebSocket, task: () => Promise): Promise; - flushAirportDurableState(airport: string): Promise; -}; - -let banChecks = 0; -let vatsimChecks = 0; -const fakeAuth = { - isVatsimIdBanned: async () => { - banChecks++; - await Promise.resolve(); - return false; - }, -} as unknown as AuthService; -const fakeVatsim = { - getUserStatus: async (controllerId: string) => { - vatsimChecks++; - await Promise.resolve(); - return { cid: controllerId, callsign: 'YSSY_TWR', type: 'atc' }; - }, - isController: (status: { type: string }) => status.type === 'atc', - isPilot: (status: { type: string }) => status.type === 'pilot', - isObserver: () => false, -} as unknown as VatsimService; -const fakeState = new FakeDurableObjectState(); -const fakeEnv = { DB: {}, POSTHOG_HOST: 'https://example.invalid' } as unknown as Env; -const connection = new Connection(fakeEnv, fakeAuth, fakeVatsim, fakeState as unknown as DurableObjectState); -await fakeState.initialization; -const internal = connection as unknown as ConnectionInternals; -const analyticsSingles: Array<{ event: string; properties: Record }> = []; -const analyticsBatches: Array }[]> = []; -internal.posthog = { - track: (event, properties) => void analyticsSingles.push({ event, properties }), - trackBatch: (events) => void analyticsBatches.push(events), -}; - -const template = [ - { id: 'SB1', state: false }, - { id: 'LO1', state: true }, -]; -internal.offlineStateCache.set('YSSY', { - template, - defaultStates: new Map(template.map((point) => [point.id, point.state])), - expiresAt: Date.now() + 60_000, -}); - -const controllerA = new FakeSocket(); -const controllerB = new FakeSocket(); -const pilotA = new FakeSocket(); -const pilotADuplicate = new FakeSocket(); -const observer = new FakeSocket(); -const now = Date.now(); -internal.registerSocket(controllerA as unknown as WebSocket, { - controllerId: '1000001', - type: 'controller', - airport: 'YSSY', - lastHeartbeat: now, -}); -internal.registerSocket(controllerB as unknown as WebSocket, { - controllerId: '1000002', - type: 'controller', - airport: 'YSSY', - lastHeartbeat: now, -}); -for (const socket of [pilotA, pilotADuplicate]) { - internal.registerSocket(socket as unknown as WebSocket, { - controllerId: '2000001', - type: 'pilot', - airport: 'YSSY', - lastHeartbeat: now, - }); -} -internal.registerSocket(observer as unknown as WebSocket, { - controllerId: '3000001', - type: 'observer', - airport: 'YSSY', - lastHeartbeat: now, -}); - -const indexedSnapshot = await connection.getState('YSSY'); -assert.deepEqual(indexedSnapshot.controllers, ['1000001', '1000002']); -assert.deepEqual(indexedSnapshot.pilots, ['2000001']); -assert.equal(indexedSnapshot.offline, false); - -const broadcastRecipients = internal.broadcast( - { type: 'STATE_UPDATE', airport: 'YSSY', data: { objectId: 'SB1', state: true } }, - controllerA as unknown as WebSocket, - false, -); -internal.trackMessage( - { clientType: 'controller', messageType: 'STATE_UPDATE', airport: 'YSSY', meta: { objectId: 'SB1' } }, - broadcastRecipients, -); -assert.equal(controllerA.sent.length, 0); -assert.equal(controllerB.sent.length, 1); -assert.equal(pilotA.sent.length, 1); -assert.equal(pilotADuplicate.sent.length, 1); -assert.equal(observer.sent.length, 1); -assert.equal(broadcastRecipients, 4); -assert.deepEqual( - analyticsBatches[0].map((event) => event.event), - ['ws_broadcast', 'ws_message'], -); -assert.equal(analyticsSingles.length, 0); - -for (const socket of [controllerA, controllerB, pilotA, pilotADuplicate, observer]) socket.sent.length = 0; -const controllerRecipients = internal.broadcastToControllers( - { type: 'STOPBAR_CROSSING', airport: 'YSSY', data: { objectId: 'SB1' } }, - pilotA as unknown as WebSocket, - false, -); -internal.trackMessage( - { clientType: 'pilot', messageType: 'STOPBAR_CROSSING', airport: 'YSSY', meta: { objectId: 'SB1' } }, - controllerRecipients, -); -assert.equal(controllerA.sent.length, 1); -assert.equal(controllerB.sent.length, 1); -assert.equal(pilotA.sent.length, 0); -assert.equal(observer.sent.length, 0); -assert.equal(controllerRecipients, 2); -assert.equal(analyticsBatches.length, 2); - -const validPackets: Packet[] = [ - { type: 'HEARTBEAT' }, - { type: 'GET_STATE' }, - { type: 'STATE_UPDATE', data: { objectId: 'SB1', state: true } }, - { type: 'STATE_UPDATE', data: { objectId: 'SB1', patch: { nested: { enabled: true } } } }, - { type: 'MULTI_STATE_UPDATE', data: { updates: [{ objectId: 'SB1', state: true }] } }, - { type: 'SHARED_STATE_UPDATE', data: { sharedStatePatch: { profile: 'default' } } }, - { type: 'STOPBAR_CROSSING', data: { objectId: 'SB1' } }, -]; -for (const packet of validPackets) assert.equal(internal.validatePacket(packet), true); - -let deepArray: unknown = true; -for (let depth = 0; depth < 30; depth++) deepArray = [deepArray]; -const invalidPackets: unknown[] = [ - { type: 'UNKNOWN' }, - { type: 'STATE_UPDATE', data: { objectId: 'invalid id', state: true } }, - { type: 'STATE_UPDATE', data: { objectId: 'SB1', patch: [] } }, - { type: 'STATE_UPDATE', data: { objectId: 'SB1', patch: { constructor: {} } } }, - { type: 'MULTI_STATE_UPDATE', data: { updates: [] } }, - { type: 'SHARED_STATE_UPDATE', data: { sharedStatePatch: [] } }, - { type: 'SHARED_STATE_UPDATE', data: { sharedStatePatch: { nested: deepArray } } }, - { type: 'STOPBAR_CROSSING', data: { objectId: 'invalid id' } }, -]; -for (const packet of invalidPackets) assert.equal(internal.validatePacket(packet), false); - -await internal.handleMultiStateUpdate( - { - type: 'MULTI_STATE_UPDATE', - data: { updates: [{ objectId: 'SB1', state: true }, { objectId: 'LO1', state: false }] }, - }, - '1000001', - 'YSSY', -); -assert.equal(internal.airportStates.get('YSSY')?.objects.get('SB1')?.state, true); -assert.equal(internal.airportStates.get('YSSY')?.objects.get('LO1')?.state, false); - -for (const socket of [controllerA, controllerB, pilotA, pilotADuplicate, observer]) socket.sent.length = 0; -const sharedState = await internal.handleSharedStateUpdate( - { - type: 'SHARED_STATE_UPDATE', - data: { sharedStatePatch: { profile: 'default', nodes: { A1: true } } }, - }, - '1000001', - 'YSSY', -); -assert.deepEqual(sharedState, { profile: 'default', nodes: { A1: true } }); -for (const socket of [controllerA, controllerB, pilotA, pilotADuplicate, observer]) { - assert.equal(socket.sent.length, 1); - assert.deepEqual(JSON.parse(socket.sent[0]), { - type: 'SHARED_STATE_UPDATE', - airport: 'YSSY', - data: { sharedStatePatch: { profile: 'default', nodes: { A1: true } }, controllerId: '1000001' }, - timestamp: JSON.parse(socket.sent[0]).timestamp, - }); -} - -const firstStatus = internal.checkSocketStatus(controllerA as unknown as WebSocket, internal.sockets.get(controllerA as unknown as WebSocket)!, now + 180_000); -const secondStatus = internal.checkSocketStatus(controllerB as unknown as WebSocket, internal.sockets.get(controllerB as unknown as WebSocket)!, now + 180_000); -assert(firstStatus && secondStatus); -await Promise.all([firstStatus, secondStatus]); -assert.equal(banChecks, 2, 'Different CIDs must be checked independently'); -assert.equal(vatsimChecks, 2, 'Different CIDs must be checked independently'); - -const duplicateController = new FakeSocket(); -internal.registerSocket(duplicateController as unknown as WebSocket, { - controllerId: '1000001', - type: 'controller', - airport: 'YSSY', - lastHeartbeat: now, -}); -const beforeBanChecks = banChecks; -const beforeVatsimChecks = vatsimChecks; -const duplicateStatus = internal.checkSocketStatus( - duplicateController as unknown as WebSocket, - internal.sockets.get(duplicateController as unknown as WebSocket)!, - now + 360_000, -); -const originalStatus = internal.checkSocketStatus( - controllerA as unknown as WebSocket, - internal.sockets.get(controllerA as unknown as WebSocket)!, - now + 360_000, -); -assert(duplicateStatus && originalStatus); -await Promise.all([duplicateStatus, originalStatus]); -assert.equal(banChecks - beforeBanChecks, 1); -assert.equal(vatsimChecks - beforeVatsimChecks, 1); - -const queueOrder: number[] = []; -const firstQueued = internal.enqueueSocketTask(pilotA as unknown as WebSocket, async () => { - await Promise.resolve(); - queueOrder.push(1); -}); -const secondQueued = internal.enqueueSocketTask(pilotA as unknown as WebSocket, async () => void queueOrder.push(2)); -await Promise.all([firstQueued, secondQueued]); -assert.deepEqual(queueOrder, [1, 2]); - -internal.unregisterSocket(pilotA as unknown as WebSocket); -assert.deepEqual((await connection.getState('YSSY')).pilots, ['2000001']); -internal.unregisterSocket(pilotADuplicate as unknown as WebSocket); -assert.deepEqual((await connection.getState('YSSY')).pilots, []); - -await internal.flushAirportDurableState('YSSY'); -await Promise.allSettled(fakeState.background); -assert(fakeState.values.has('airport_state:YSSY')); -assert(fakeState.values.has('airport_shared_state:YSSY')); - -const originalFetch = globalThis.fetch; -let capturedBatchRequest: { input: RequestInfo | URL; init?: RequestInit } | undefined; -try { - globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit) => { - capturedBatchRequest = { input, init }; - return new Response(null, { status: 200 }); - }; - const posthog = new PostHogService({ - POSTHOG_API_KEY: 'ph_test', - POSTHOG_HOST: 'https://analytics.example.test', - } as Env); - posthog.trackBatch([ - { event: 'ws_broadcast', properties: { airport: 'YSSY', recipients: 4 } }, - { event: 'ws_message', properties: { airport: 'YSSY', messageType: 'STATE_UPDATE' } }, - ]); - await Promise.all(cloudflareBackground.splice(0)); -} finally { - globalThis.fetch = originalFetch; -} -assert(capturedBatchRequest); -assert.equal(String(capturedBatchRequest.input), 'https://analytics.example.test/batch/'); -const capturedBatchBody = JSON.parse(String(capturedBatchRequest.init?.body)) as { - api_key: string; - batch: Array<{ event: string; properties: Record; $process_person_profile: boolean }>; -}; -assert.equal(capturedBatchBody.api_key, 'ph_test'); -assert.deepEqual( - capturedBatchBody.batch.map((event) => event.event), - ['ws_broadcast', 'ws_message'], -); -assert(capturedBatchBody.batch.every((event) => event.properties.product === 'Core')); -assert(capturedBatchBody.batch.every((event) => event.properties.distinct_id === 'anonymous')); -assert(capturedBatchBody.batch.every((event) => event.$process_person_profile === false)); - -console.log( - JSON.stringify({ - protocolPacketsValidated: validPackets.length + invalidPackets.length, - indexedParticipantSnapshot: true, - broadcastRouting: true, - sharedStateSerialization: true, - analyticsBatching: true, - statusCheckCoalescing: true, - perSocketQueueOrdering: true, - persistence: true, - }), -); diff --git a/benchmarks/realtime.ts b/benchmarks/realtime.ts deleted file mode 100644 index 6766c11..0000000 --- a/benchmarks/realtime.ts +++ /dev/null @@ -1,438 +0,0 @@ -import assert from 'node:assert/strict'; -import { performance } from 'node:perf_hooks'; - -type SyncVariant = { - name: string; - iterations: number; - run: () => unknown; -}; - -const measuredSamples = 11; - -function checksum(value: unknown): number { - if (typeof value === 'number') return value; - if (typeof value === 'boolean') return value ? 1 : 0; - if (typeof value === 'string') return value.length; - if (Array.isArray(value)) return value.length; - if (value && typeof value === 'object') return Object.keys(value).length; - return 0; -} - -function benchmark(variants: SyncVariant[]): Array> { - return variants.map((variant) => { - for (let index = 0; index < Math.min(2_000, variant.iterations); index++) variant.run(); - const samples: number[] = []; - let resultChecksum = 0; - for (let sample = 0; sample < measuredSamples; sample++) { - const startedAt = performance.now(); - for (let index = 0; index < variant.iterations; index++) resultChecksum += checksum(variant.run()); - samples.push(performance.now() - startedAt); - } - const ordered = samples.toSorted((left, right) => left - right); - const medianMs = ordered[Math.floor(ordered.length / 2)]; - return { - variant: variant.name, - iterations: variant.iterations, - medianMs: Number(medianMs.toFixed(3)), - nsPerOperation: Number(((medianMs * 1_000_000) / variant.iterations).toFixed(1)), - checksum: resultChecksum, - }; - }); -} - -type ClientType = 'controller' | 'pilot' | 'observer'; -type SocketInfo = { controllerId: string; type: ClientType; airport: string; readyState: number }; -const openState = 1; -const socketEntries = Array.from({ length: 1_000 }, (_, index) => { - const type: ClientType = index % 5 === 0 ? 'controller' : index % 7 === 0 ? 'observer' : 'pilot'; - return [ - { id: index }, - { - controllerId: `CID-${index % 220}`, - type, - airport: index < 900 ? 'YSSY' : 'YMML', - readyState: openState, - } satisfies SocketInfo, - ] as const; -}); -const sockets = new Map(socketEntries); - -function snapshotByScan() { - const controllers: string[] = []; - const pilots: string[] = []; - const controllerIds = new Set(); - const pilotIds = new Set(); - for (const info of sockets.values()) { - if (info.airport !== 'YSSY') continue; - if (info.type === 'controller' && !controllerIds.has(info.controllerId)) { - controllerIds.add(info.controllerId); - controllers.push(info.controllerId); - } else if (info.type === 'pilot' && !pilotIds.has(info.controllerId)) { - pilotIds.add(info.controllerId); - pilots.push(info.controllerId); - } - } - return { controllers, pilots }; -} - -const indexedParticipants = new Map; pilots: Map }>(); -for (const info of sockets.values()) { - let airport = indexedParticipants.get(info.airport); - if (!airport) { - airport = { controllers: new Map(), pilots: new Map() }; - indexedParticipants.set(info.airport, airport); - } - const index = info.type === 'controller' ? airport.controllers : info.type === 'pilot' ? airport.pilots : null; - if (index) index.set(info.controllerId, (index.get(info.controllerId) ?? 0) + 1); -} -function snapshotByIndex() { - const airport = indexedParticipants.get('YSSY'); - return { - controllers: airport ? Array.from(airport.controllers.keys()) : [], - pilots: airport ? Array.from(airport.pilots.keys()) : [], - }; -} -assert.deepEqual(snapshotByIndex(), snapshotByScan()); - -function broadcastForEach() { - let recipients = 0; - sockets.forEach((info, socket) => { - if (socket.id !== 1 && info.readyState === openState && info.airport === 'YSSY') recipients++; - }); - return recipients; -} -function broadcastForOf() { - let recipients = 0; - for (const [socket, info] of sockets) { - if (socket.id !== 1 && info.readyState === openState && info.airport === 'YSSY') recipients++; - } - return recipients; -} -assert.equal(broadcastForEach(), broadcastForOf()); - -const controllerSocketIndex = new Map>(); -for (const [socket, info] of sockets) { - if (info.airport !== 'YSSY' || info.type !== 'controller') continue; - let indexedSockets = controllerSocketIndex.get(info.controllerId); - if (!indexedSockets) { - indexedSockets = new Set(); - controllerSocketIndex.set(info.controllerId, indexedSockets); - } - indexedSockets.add(socket); -} -function controllerBroadcastByScan() { - let recipients = 0; - for (const [socket, info] of sockets) { - if (socket.id !== 1 && info.readyState === openState && info.airport === 'YSSY' && info.type === 'controller') recipients++; - } - return recipients; -} -function controllerBroadcastByIndex() { - let recipients = 0; - for (const indexedSockets of controllerSocketIndex.values()) { - for (const socket of indexedSockets) { - if (socket.id !== 1) recipients++; - } - } - return recipients; -} -assert.equal(controllerBroadcastByIndex(), controllerBroadcastByScan()); - -const packetTypes = [ - 'HEARTBEAT', - 'HEARTBEAT_ACK', - 'STATE_UPDATE', - 'MULTI_STATE_UPDATE', - 'CLOSE', - 'SHARED_STATE_UPDATE', - 'INITIAL_STATE', - 'CONTROLLER_CONNECT', - 'CONTROLLER_DISCONNECT', - 'ERROR', - 'GET_STATE', - 'STATE_SNAPSHOT', - 'STOPBAR_CROSSING', -] as const; -const packetTypeSet = new Set(packetTypes); -const packetTypeRecord = Object.assign(Object.create(null) as Record, Object.fromEntries(packetTypes.map((type) => [type, true]))); -const packetTypeSamples: string[] = [...packetTypes, 'UNKNOWN', 'STATE_UPDATE', 'not-a-packet']; -const packetTypeSwitch = (type: string): boolean => { - switch (type) { - case 'HEARTBEAT': - case 'HEARTBEAT_ACK': - case 'STATE_UPDATE': - case 'MULTI_STATE_UPDATE': - case 'CLOSE': - case 'SHARED_STATE_UPDATE': - case 'INITIAL_STATE': - case 'CONTROLLER_CONNECT': - case 'CONTROLLER_DISCONNECT': - case 'ERROR': - case 'GET_STATE': - case 'STATE_SNAPSHOT': - case 'STOPBAR_CROSSING': - return true; - default: - return false; - } -}; -for (const type of packetTypeSamples) { - assert.equal(packetTypeSet.has(type), packetTypeSwitch(type)); - assert.equal(packetTypeRecord[type] === true, packetTypeSwitch(type)); -} -let setTypeIndex = 0; -let recordTypeIndex = 0; -let switchTypeIndex = 0; - -function recursiveSafeValue(value: unknown, maxDepth = 20, maxProperties = 100): boolean { - const seen = new WeakSet(); - const walk = (current: unknown, depth: number): boolean => { - if (current === null || typeof current !== 'object') return true; - if (Array.isArray(current)) return current.length <= 1_000 && current.every((item) => walk(item, depth + 1)); - if (depth > maxDepth || seen.has(current)) return false; - seen.add(current); - const keys = Object.keys(current as Record); - return keys.length <= maxProperties && keys.every((key) => key.length <= 100 && walk((current as Record)[key], depth + 1)); - }; - return walk(value, 0); -} - -function iterativeSafeValue(value: unknown, maxDepth = 20, maxProperties = 100): boolean { - const pending: Array<{ value: unknown; depth: number }> = [{ value, depth: 0 }]; - const seen = new WeakSet(); - while (pending.length > 0) { - const current = pending.pop()!; - if (current.value === null || typeof current.value !== 'object') continue; - if (current.depth > maxDepth || seen.has(current.value)) return false; - seen.add(current.value); - if (Array.isArray(current.value)) { - if (current.value.length > 1_000) return false; - for (let index = current.value.length - 1; index >= 0; index--) pending.push({ value: current.value[index], depth: current.depth + 1 }); - continue; - } - const record = current.value as Record; - const keys = Object.keys(record); - if (keys.length > maxProperties) return false; - for (const key of keys) { - if (key.length > 100) return false; - pending.push({ value: record[key], depth: current.depth + 1 }); - } - } - return true; -} -const nestedFixture = { - profile: 'default', - nodes: Object.fromEntries(Array.from({ length: 80 }, (_, index) => [`TWY_${index}`, (index & 1) === 0])), - blocks: Array.from({ length: 30 }, (_, index) => ({ id: index, route: [`A${index}`, `B${index}`] })), -}; -assert.equal(iterativeSafeValue(nestedFixture), recursiveSafeValue(nestedFixture)); -let excessivelyNested: unknown = true; -for (let depth = 0; depth < 30; depth++) excessivelyNested = [excessivelyNested]; -assert.equal(iterativeSafeValue(excessivelyNested), false); - -const disallowedKeys = new Set(['__proto__', 'constructor', 'prototype']); -const nullObject = (): Record => Object.create(null) as Record; -function mergeWithEntryClone(target: unknown, source: unknown, depth = 0): unknown { - if (depth > 20) throw new Error('depth'); - if (source === null || typeof source !== 'object') return source; - if (Array.isArray(source)) return [...source]; - const sourceKeys = Object.keys(source); - if (sourceKeys.length > 100) throw new Error('properties'); - const targetRecord = target !== null && typeof target === 'object' && !Array.isArray(target) ? (target as Record) : undefined; - let result = targetRecord ?? nullObject(); - let cloned = !targetRecord; - const ensureClone = () => { - if (cloned) return; - const clone = nullObject(); - for (const [key, value] of Object.entries(targetRecord!)) clone[key] = value; - result = clone; - cloned = true; - }; - for (const key of sourceKeys) { - if (disallowedKeys.has(key)) throw new Error('key'); - const sourceValue = (source as Record)[key]; - const targetValue = targetRecord?.[key]; - if (sourceValue && typeof sourceValue === 'object' && !Array.isArray(sourceValue)) { - const merged = mergeWithEntryClone(targetValue, sourceValue, depth + 1); - if (merged !== targetValue) { - ensureClone(); - result[key] = merged; - } - } else { - ensureClone(); - result[key] = sourceValue; - } - } - return result; -} -function mergeWithAssignClone(target: unknown, source: unknown, depth = 0): unknown { - if (depth > 20) throw new Error('depth'); - if (source === null || typeof source !== 'object') return source; - if (Array.isArray(source)) return [...source]; - const sourceKeys = Object.keys(source); - if (sourceKeys.length > 100) throw new Error('properties'); - const targetRecord = target !== null && typeof target === 'object' && !Array.isArray(target) ? (target as Record) : undefined; - let result = targetRecord ?? nullObject(); - let cloned = !targetRecord; - const ensureClone = () => { - if (cloned) return; - result = Object.assign(nullObject(), targetRecord); - cloned = true; - }; - for (const key of sourceKeys) { - if (key === '__proto__' || key === 'constructor' || key === 'prototype') throw new Error('key'); - const sourceValue = (source as Record)[key]; - const targetValue = targetRecord?.[key]; - if (sourceValue && typeof sourceValue === 'object' && !Array.isArray(sourceValue)) { - const merged = mergeWithAssignClone(targetValue, sourceValue, depth + 1); - if (merged !== targetValue) { - ensureClone(); - result[key] = merged; - } - } else if (sourceValue !== targetValue) { - ensureClone(); - result[key] = sourceValue; - } - } - return result; -} -const mergeTarget = { profile: 'default', nodes: Object.fromEntries(Array.from({ length: 80 }, (_, index) => [`N${index}`, false])), untouched: { value: 1 } }; -const mergePatch = { nodes: { N2: true, N17: true, N65: true }, sequence: ['A', 'B', 'C'] }; -assert.deepEqual(mergeWithAssignClone(mergeTarget, mergePatch), mergeWithEntryClone(mergeTarget, mergePatch)); - -const sharedPatch = nestedFixture; -const sharedPacket = { - type: 'SHARED_STATE_UPDATE', - airport: 'YSSY', - data: { sharedStatePatch: sharedPatch, controllerId: '1234567' }, - timestamp: 1_753_000_000_000, -}; -function serializeSharedTwice() { - const patchString = JSON.stringify(sharedPatch); - return patchString.length + JSON.stringify(sharedPacket).length; -} -function serializeSharedOnce() { - const patchString = JSON.stringify(sharedPatch); - const packetString = `{"type":"SHARED_STATE_UPDATE","airport":"YSSY","data":{"sharedStatePatch":${patchString},"controllerId":"1234567"},"timestamp":1753000000000}`; - return patchString.length + packetString.length; -} -assert.equal(serializeSharedOnce(), serializeSharedTwice()); - -const encodedPacket = new TextEncoder().encode(JSON.stringify(sharedPacket)); -const sharedDecoder = new TextDecoder(); -assert.equal(sharedDecoder.decode(encodedPacket), new TextDecoder().decode(encodedPacket)); -const heartbeatPacket = { type: 'HEARTBEAT' }; -const serializedHeartbeat = JSON.stringify(heartbeatPacket); -const connectRequest = new Request('https://api.stopbars.test/connect?airport=YSSY&key=BARS_test', { - headers: { Authorization: 'Bearer BARS_test', Upgrade: 'websocket' }, -}); -function cloneConnectRequest() { - const headers = new Headers(connectRequest.headers); - headers.set('Authorization', 'Bearer BARS_test'); - return new Request(connectRequest.url, { method: connectRequest.method, headers, body: connectRequest.body }); -} -assert.equal(cloneConnectRequest().url, connectRequest.url); - -const dueInfo = { statusCheckInFlight: false, lastStatusCheck: 1_753_000_000_000 }; -const dueNow = dueInfo.lastStatusCheck + 1_000; -async function statusDueAsync() { - if (dueInfo.statusCheckInFlight || dueNow - dueInfo.lastStatusCheck < 120_000) return false; - return true; -} -function statusDueSync() { - return !(dueInfo.statusCheckInFlight || dueNow - dueInfo.lastStatusCheck < 120_000); -} -assert.equal(await statusDueAsync(), statusDueSync()); - -const analyticsEvents = [ - { - event: 'ws_broadcast', - properties: { distinct_id: 'anonymous', product: 'Core', airport: 'YSSY', messageType: 'STATE_UPDATE', recipients: 40 }, - $process_person_profile: false, - }, - { - event: 'ws_message', - properties: { - distinct_id: 'anonymous', - product: 'Core', - airport: 'YSSY', - clientType: 'controller', - messageType: 'STATE_UPDATE', - objectId: 'SB1', - }, - $process_person_profile: false, - }, -] as const; -function serializeAnalyticsSeparately() { - return analyticsEvents.reduce( - (total, event) => total + JSON.stringify({ api_key: 'ph_test', ...event }).length, - 0, - ); -} -function serializeAnalyticsBatch() { - return JSON.stringify({ api_key: 'ph_test', batch: analyticsEvents }).length; -} -const separateAnalyticsPayloads = analyticsEvents.map((event) => JSON.parse(JSON.stringify({ api_key: 'ph_test', ...event }))); -const batchAnalyticsPayload = JSON.parse( - JSON.stringify({ api_key: 'ph_test', batch: analyticsEvents }), -) as { batch: typeof analyticsEvents }; -assert.deepEqual( - batchAnalyticsPayload.batch, - separateAnalyticsPayloads.map((payload) => ({ - event: payload.event, - properties: payload.properties, - $process_person_profile: payload.$process_person_profile, - })), -); - -console.table( - benchmark([ - { name: 'snapshot/scan all sockets', iterations: 10_000, run: snapshotByScan }, - { name: 'snapshot/maintained indexes', iterations: 10_000, run: snapshotByIndex }, - { name: 'broadcast/Map.forEach', iterations: 10_000, run: broadcastForEach }, - { name: 'broadcast/for-of', iterations: 10_000, run: broadcastForOf }, - { name: 'controller broadcast/scan all', iterations: 10_000, run: controllerBroadcastByScan }, - { name: 'controller broadcast/controller index', iterations: 10_000, run: controllerBroadcastByIndex }, - { - name: 'packet type/Set.has', - iterations: 1_000_000, - run: () => packetTypeSet.has(packetTypeSamples[setTypeIndex++ % packetTypeSamples.length]), - }, - { - name: 'packet type/object lookup', - iterations: 1_000_000, - run: () => packetTypeRecord[packetTypeSamples[recordTypeIndex++ % packetTypeSamples.length]] === true, - }, - { - name: 'packet type/switch', - iterations: 1_000_000, - run: () => packetTypeSwitch(packetTypeSamples[switchTypeIndex++ % packetTypeSamples.length]), - }, - { name: 'nested validation/recursive', iterations: 20_000, run: () => recursiveSafeValue(nestedFixture) }, - { name: 'nested validation/iterative', iterations: 20_000, run: () => iterativeSafeValue(nestedFixture) }, - { name: 'merge/entry clone + Set keys', iterations: 50_000, run: () => mergeWithEntryClone(mergeTarget, mergePatch) }, - { name: 'merge/assign clone + direct keys', iterations: 50_000, run: () => mergeWithAssignClone(mergeTarget, mergePatch) }, - { name: 'shared packet/stringify patch twice', iterations: 20_000, run: serializeSharedTwice }, - { name: 'shared packet/reuse patch JSON', iterations: 20_000, run: serializeSharedOnce }, - { name: 'binary decode/new decoder', iterations: 50_000, run: () => new TextDecoder().decode(encodedPacket) }, - { name: 'binary decode/shared decoder', iterations: 50_000, run: () => sharedDecoder.decode(encodedPacket) }, - { name: 'heartbeat/JSON.stringify', iterations: 1_000_000, run: () => JSON.stringify(heartbeatPacket) }, - { name: 'heartbeat/pre-serialized', iterations: 1_000_000, run: () => serializedHeartbeat }, - { name: 'connect/clone headers + request', iterations: 20_000, run: cloneConnectRequest }, - { name: 'connect/forward original request', iterations: 20_000, run: () => connectRequest }, - { name: 'status due/async early return', iterations: 100_000, run: statusDueAsync }, - { name: 'status due/synchronous guard', iterations: 100_000, run: statusDueSync }, - { name: 'analytics/two capture payloads', iterations: 100_000, run: serializeAnalyticsSeparately }, - { name: 'analytics/one batch payload', iterations: 100_000, run: serializeAnalyticsBatch }, - ]), -); - -console.log( - JSON.stringify({ - semanticEquality: true, - excessiveArrayDepthRejected: true, - handshakeDatabaseCalls: { current: 2, combinedPrincipal: 1 }, - statusChecksForDuplicateCid: { current: 'one per socket', coalesced: 'one in-flight request per CID' }, - analyticsRequestsPerBroadcastMessage: { separateCaptures: 2, batched: 1 }, - }), -); diff --git a/benchmarks/routes.ts b/benchmarks/routes.ts deleted file mode 100644 index 3669a32..0000000 --- a/benchmarks/routes.ts +++ /dev/null @@ -1,114 +0,0 @@ -import assert from 'node:assert/strict'; -import { performance } from 'node:perf_hooks'; -import { Hono } from 'hono'; - -type Variant = { name: string; iterations: number; run: () => T | Promise }; - -async function benchmark(variants: Variant[]): Promise>> { - const rows: Array> = []; - for (const variant of variants) { - for (let index = 0; index < Math.min(2_000, variant.iterations); index++) await variant.run(); - const samples: number[] = []; - for (let sample = 0; sample < 11; sample++) { - const started = performance.now(); - for (let index = 0; index < variant.iterations; index++) await variant.run(); - samples.push(performance.now() - started); - } - samples.sort((a, b) => a - b); - const medianMs = samples[5]; - rows.push({ - variant: variant.name, - iterations: variant.iterations, - medianMs: Number(medianMs.toFixed(3)), - nsPerOperation: Number(((medianMs * 1_000_000) / variant.iterations).toFixed(1)), - }); - } - return rows; -} - -const request = new Request('https://api.stopbars.test/airports/YSSY/points?q=1'); -const pathname = '/airports/YSSY/points'; -assert.equal(new URL(request.url).pathname, pathname); - -const icaoRegex = /^[A-Z0-9]{4}$/; -assert.equal(Boolean('YSSY'.match(icaoRegex)), icaoRegex.test('YSSY')); - -const template = Array.from({ length: 4_000 }, (_, index) => ({ id: `BARS_${index}`, state: (index & 1) === 0 })); -const updates = Array.from({ length: 200 }, (_, index) => ({ objectId: `BARS_${index * 17}`, state: (index & 1) === 0 })); -const defaultStates = new Map(template.map((point) => [point.id, point.state])); -const pruneWithFind = () => - updates.filter((update) => template.find((point) => point.id === update.objectId)?.state === update.state).length; -const pruneWithMap = () => updates.filter((update) => defaultStates.get(update.objectId) === update.state).length; -assert.equal(pruneWithFind(), pruneWithMap()); - -const stateObjects = new Map( - template.map((point, index) => [ - point.id, - { id: point.id, state: point.state, controllerId: `controller-${index % 12}`, timestamp: 1_750_000_000_000 + index }, - ]), -); -type PersistedObject = { id: string; state: boolean; controllerId: string; timestamp: number }; -const persistWithIntermediates = () => - Object.fromEntries( - Array.from(stateObjects.entries()).map(([id, object]) => [ - id, - { id: object.id, state: object.state, controllerId: object.controllerId, timestamp: object.timestamp }, - ]), - ); -const persistDirectly = () => { - const result: Record = Object.create(null) as Record; - for (const [id, object] of stateObjects) { - result[id] = { - id: object.id, - state: object.state, - controllerId: object.controllerId, - timestamp: object.timestamp, - }; - } - return result; -}; -assert.equal(JSON.stringify(persistDirectly()), JSON.stringify(persistWithIntermediates())); - -const packet = { type: 'MULTI_STATE_UPDATE', airport: 'YSSY', data: { updates } }; -const typedState = { airport: 'YSSY', objects: updates, offline: false }; -const doFetchEnvelope = async () => { - const internalRequest = new Request('https://internal/state?airport=YSSY', { headers: { 'X-Request-Type': 'get_state' } }); - const airport = new URL(internalRequest.url).searchParams.get('airport'); - return Response.json({ ...typedState, airport }).json(); -}; -assert.deepEqual(await doFetchEnvelope(), typedState); - -const oneMiddlewareApp = new Hono(); -oneMiddlewareApp.use('*', async (_context, next) => next()); -oneMiddlewareApp.get('/health', (context) => context.json({ ok: true })); - -const threeMiddlewareApp = new Hono(); -for (let index = 0; index < 3; index++) threeMiddlewareApp.use('*', async (_context, next) => next()); -threeMiddlewareApp.get('/health', (context) => context.json({ ok: true })); - -const results = await benchmark([ - { name: 'path/new URL', iterations: 100_000, run: () => new URL(request.url).pathname }, - { name: 'path/Hono pre-parsed', iterations: 100_000, run: () => pathname }, - { name: 'ICAO/String.match', iterations: 100_000, run: () => 'YSSY'.match(icaoRegex) }, - { name: 'ICAO/RegExp.test', iterations: 100_000, run: () => icaoRegex.test('YSSY') }, - { name: 'defaults/Array.find x200', iterations: 2_000, run: pruneWithFind }, - { name: 'defaults/Map.get x200', iterations: 2_000, run: pruneWithMap }, - { name: 'packet/redundant stringify', iterations: 20_000, run: () => JSON.stringify(packet).length <= 50_000 }, - { name: 'packet/pre-checked raw length', iterations: 20_000, run: () => true }, - { name: 'persist/intermediate arrays', iterations: 1_000, run: persistWithIntermediates }, - { name: 'persist/direct assignment', iterations: 1_000, run: persistDirectly }, - { name: 'DO/fetch JSON envelope', iterations: 5_000, run: doFetchEnvelope }, - { name: 'DO/typed RPC result', iterations: 5_000, run: () => typedState }, - { - name: 'Hono/one async middleware', - iterations: 10_000, - run: async () => (await oneMiddlewareApp.request('https://bench.test/health')).body?.cancel(), - }, - { - name: 'Hono/three async middleware', - iterations: 10_000, - run: async () => (await threeMiddlewareApp.request('https://bench.test/health')).body?.cancel(), - }, -]); - -console.table(results); diff --git a/benchmarks/smoke.ts b/benchmarks/smoke.ts deleted file mode 100644 index 65a4935..0000000 --- a/benchmarks/smoke.ts +++ /dev/null @@ -1,96 +0,0 @@ -const baseUrl = (process.env.CORE_BASE_URL ?? 'http://127.0.0.1:8790').replace(/\/$/, ''); - -const assert = (condition: unknown, message: string): asserts condition => { - if (!condition) throw new Error(message); -}; - -const request = async (path: string, init?: RequestInit): Promise => fetch(`${baseUrl}${path}`, init); - -const percentile = (sorted: number[], fraction: number): number => - sorted[Math.min(sorted.length - 1, Math.ceil(sorted.length * fraction) - 1)] ?? 0; - -const measure = async (name: string, path: (iteration: number) => string, iterations = 20) => { - const samples: number[] = []; - for (let iteration = 0; iteration < iterations; iteration++) { - const startedAt = performance.now(); - const response = await request(path(iteration)); - await response.arrayBuffer(); - assert(response.ok, `${name} returned ${response.status}`); - samples.push(performance.now() - startedAt); - } - const sorted = samples.toSorted((left, right) => left - right); - return { - name, - meanMs: Number((samples.reduce((sum, sample) => sum + sample, 0) / samples.length).toFixed(3)), - p50Ms: Number(percentile(sorted, 0.5).toFixed(3)), - p95Ms: Number(percentile(sorted, 0.95).toFixed(3)), - p99Ms: Number(percentile(sorted, 0.99).toFixed(3)), - }; -}; - -const staticProbes: Array<[path: string, expectedStatus: number]> = [ - ['/favicon.ico', 200], - ['/stateid/info.json', 200], - ['/state?airport=YBEN', 200], - ['/faqs', 200], - ['/health?service=database', 200], - ['/docs', 302], -]; - -for (const [path, expectedStatus] of staticProbes) { - const response = await request(path, path === '/docs' ? { redirect: 'manual' } : undefined); - await response.arrayBuffer(); - assert(response.status === expectedStatus, `${path} returned ${response.status}; expected ${expectedStatus}`); -} - -const pointResponse = await request('/points?ids=P001,P002,P001,MISSING'); -const pointPayload = (await pointResponse.json()) as { - points: Array<{ id: string }>; - requested: number; - found: number; - notFound?: string[]; -}; -assert(pointResponse.ok, `/points returned ${pointResponse.status}`); -assert(pointPayload.requested === 4 && pointPayload.found === 3, 'Bulk point counts changed'); -assert(pointPayload.points.map((point) => point.id).join(',') === 'P001,P002,P001', 'Bulk point ordering changed'); -assert(pointPayload.notFound?.join(',') === 'MISSING', 'Bulk point missing-ID behavior changed'); - -const probeToken = Date.now(); -const latency = [ - await measure('points/100 IDs uncached', (iteration) => { - const ids = Array.from({ length: 100 }, (_, index) => `P${String(index + 1).padStart(3, '0')}`).join(','); - return `/points?ids=${ids}&probe=${probeToken}-${iteration}`; - }), - await measure('state/typed RPC uncached', (iteration) => `/state?airport=YBEN&probe=${probeToken}-${iteration}`), - await measure('state-id/cached', () => '/stateid/info.json'), -]; - -const downloadIp = `2001:db8::${probeToken.toString(16)}`; -const downloadInit = { method: 'POST', headers: { 'CF-Connecting-IP': downloadIp } } satisfies RequestInit; -const firstDownload = await request('/download?product=Installer', downloadInit); -const firstDownloadPayload = (await firstDownload.json()) as { versionCount: number }; -const secondDownload = await request('/download?product=Installer', downloadInit); -const secondDownloadPayload = (await secondDownload.json()) as { versionCount: number }; -assert(firstDownload.ok && secondDownload.ok, 'Download smoke request failed'); -assert(firstDownloadPayload.versionCount === secondDownloadPayload.versionCount, 'Duplicate download incremented the count'); - -const rateLimitIp = `2001:db8:1::${probeToken.toString(16)}`; -const rateLimitStatuses: number[] = []; -for (let requestNumber = 0; requestNumber < 31; requestNumber++) { - const response = await request('/connect', { headers: { 'CF-Connecting-IP': rateLimitIp } }); - rateLimitStatuses.push(response.status); - await response.arrayBuffer(); -} -assert(rateLimitStatuses.slice(0, 30).every((status) => status === 400), 'Rate limiter rejected a request too early'); -assert(rateLimitStatuses[30] === 429, 'Rate limiter did not reject request 31'); - -console.table(latency); -console.log( - JSON.stringify({ - baseUrl, - functionalProbes: staticProbes.length + 3, - bulkPointOrdering: true, - duplicateDownloadSuppression: true, - rateLimit: { allowed: 30, rejectedStatus: rateLimitStatuses[30] }, - }), -); diff --git a/bun.lock b/bun.lock index 7d34cd6..87b1148 100644 --- a/bun.lock +++ b/bun.lock @@ -11,17 +11,17 @@ "swagger-jsdoc": "^6.2.8", }, "devDependencies": { - "@eslint/js": "^9.39.2", - "@types/node": "^24.10.9", - "@types/swagger-jsdoc": "^6.0.4", - "eslint": "^9.39.2", - "globals": "^16.5.0", - "jiti": "^2.6.1", - "prettier": "^3.8.0", - "ts-node": "^10.9.2", - "typescript": "^5.9.3", - "typescript-eslint": "^8.53.0", - "wrangler": "^4.112.0", + "@eslint/js": "9.39.2", + "@types/node": "24.10.9", + "@types/swagger-jsdoc": "6.0.4", + "eslint": "9.39.2", + "globals": "16.5.0", + "jiti": "2.6.1", + "prettier": "3.8.0", + "ts-node": "10.9.2", + "typescript": "5.9.3", + "typescript-eslint": "8.53.0", + "wrangler": "4.112.0", }, }, }, diff --git a/package.json b/package.json index 29e380a..a7e451b 100644 --- a/package.json +++ b/package.json @@ -2,38 +2,32 @@ "name": "bars-core", "version": "2.0.0", "private": true, + "packageManager": "bun@1.3.14", "scripts": { "deploy": "wrangler deploy", "dev": "wrangler dev", "typegen": "wrangler types", "update-db-local": "wrangler d1 execute bars-db --local --file schema.sql", "update-db": "wrangler d1 execute bars-db --remote --file schema.sql", + "migrate-db-local": "wrangler d1 migrations apply bars-db --local", + "migrate-db": "wrangler d1 migrations apply bars-db --remote", "prettify": "prettier --write .", "build": "wrangler build", - "benchmark": "bun run benchmark:routes && bun run benchmark:database && bun run benchmark:compute && bun run benchmark:realtime && bun run benchmark:backend", - "benchmark:routes": "bun benchmarks/routes.ts", - "benchmark:database": "bun benchmarks/database.ts", - "benchmark:compute": "bun benchmarks/compute.ts", - "benchmark:realtime": "bun benchmarks/realtime.ts", - "benchmark:backend": "bun benchmarks/backend.ts", - "test:realtime": "bun benchmarks/realtime-behavior.ts", - "test:backend": "bun benchmarks/backend-behavior.ts", - "benchmark:smoke": "bun benchmarks/smoke.ts", "openapi": "node scripts/generate-openapi.mjs && prettier --write data/openapi.json", "lint": "eslint . --fix" }, "devDependencies": { - "@eslint/js": "^9.39.2", - "@types/node": "^24.10.9", - "@types/swagger-jsdoc": "^6.0.4", - "eslint": "^9.39.2", - "globals": "^16.5.0", - "jiti": "^2.6.1", - "prettier": "^3.8.0", - "ts-node": "^10.9.2", - "typescript": "^5.9.3", - "typescript-eslint": "^8.53.0", - "wrangler": "^4.112.0" + "@eslint/js": "9.39.2", + "@types/node": "24.10.9", + "@types/swagger-jsdoc": "6.0.4", + "eslint": "9.39.2", + "globals": "16.5.0", + "jiti": "2.6.1", + "prettier": "3.8.0", + "ts-node": "10.9.2", + "typescript": "5.9.3", + "typescript-eslint": "8.53.0", + "wrangler": "4.112.0" }, "dependencies": { "geolib": "^3.3.4", From ea9bac917ea6d5864ec94502767bc6b7d0616c53 Mon Sep 17 00:00:00 2001 From: AussieScorcher Date: Tue, 4 Aug 2026 23:53:50 +0800 Subject: [PATCH 02/15] feat: publish X-Plane contribution artifacts --- ...0001_add_xplane_contribution_simulator.sql | 103 ++++ ..._add_contribution_artifact_publication.sql | 11 + schema.sql | 20 +- src/services/contributions.ts | 498 +++++++++++++----- src/services/lights-cache.ts | 15 + src/services/xml-sanitizer.ts | 6 +- src/services/xplane-removals.ts | 195 +++++++ 7 files changed, 713 insertions(+), 135 deletions(-) create mode 100644 migrations/0001_add_xplane_contribution_simulator.sql create mode 100644 migrations/0002_add_contribution_artifact_publication.sql create mode 100644 src/services/xplane-removals.ts diff --git a/migrations/0001_add_xplane_contribution_simulator.sql b/migrations/0001_add_xplane_contribution_simulator.sql new file mode 100644 index 0000000..22c86c7 --- /dev/null +++ b/migrations/0001_add_xplane_contribution_simulator.sql @@ -0,0 +1,103 @@ +PRAGMA defer_foreign_keys = on; + +CREATE TABLE contributions_xplane_migration ( + id TEXT PRIMARY KEY, + user_id TEXT NOT NULL, + airport_icao TEXT NOT NULL, + package_name TEXT NOT NULL, + submitted_xml TEXT NOT NULL, + notes TEXT, + simulator TEXT CHECK ( + simulator IN ( + 'msfs2020', + 'msfs2024', + 'xplane' + ) + ) NOT NULL DEFAULT 'msfs2024', + submission_date DATETIME DEFAULT CURRENT_TIMESTAMP, + status TEXT CHECK ( + status IN ( + 'pending', + 'approved', + 'rejected', + 'outdated' + ) + ) NOT NULL DEFAULT 'pending', + rejection_reason TEXT, + decision_date DATETIME +); + +INSERT INTO contributions_xplane_migration ( + id, + user_id, + airport_icao, + package_name, + submitted_xml, + notes, + simulator, + submission_date, + status, + rejection_reason, + decision_date +) +SELECT + id, + user_id, + airport_icao, + package_name, + submitted_xml, + notes, + simulator, + submission_date, + status, + rejection_reason, + decision_date +FROM contributions; + +DROP TABLE contributions; + +ALTER TABLE contributions_xplane_migration RENAME TO contributions; + +CREATE INDEX IF NOT EXISTS idx_contributions_user_submission_date ON contributions ( + user_id, + submission_date DESC +); + +CREATE INDEX IF NOT EXISTS idx_contributions_submission_date ON contributions ( + submission_date +); + +CREATE INDEX IF NOT EXISTS idx_contributions_status_submission_date ON contributions ( + status, + submission_date DESC +); + +CREATE INDEX IF NOT EXISTS idx_contributions_airport_submission_date ON contributions ( + airport_icao, + submission_date DESC +); + +CREATE INDEX IF NOT EXISTS idx_contributions_status_package ON contributions ( + status, + package_name +); + +CREATE INDEX IF NOT EXISTS idx_contributions_package_simulator_status ON contributions ( + package_name COLLATE NOCASE, + simulator, + status +); + +CREATE INDEX IF NOT EXISTS idx_contributions_status_user ON contributions ( + status, + user_id +); + +CREATE INDEX IF NOT EXISTS idx_contributions_airport_lowerpkg_status_decision ON contributions ( + airport_icao, + lower(package_name), + status, + decision_date DESC +); + +PRAGMA defer_foreign_keys = off; diff --git a/migrations/0002_add_contribution_artifact_publication.sql b/migrations/0002_add_contribution_artifact_publication.sql new file mode 100644 index 0000000..b5f9d20 --- /dev/null +++ b/migrations/0002_add_contribution_artifact_publication.sql @@ -0,0 +1,11 @@ +ALTER TABLE contributions ADD COLUMN generation_token TEXT; +ALTER TABLE contributions ADD COLUMN generation_hash TEXT; +ALTER TABLE contributions ADD COLUMN artifact_identity TEXT; +ALTER TABLE contributions ADD COLUMN artifact_generation_id TEXT; +ALTER TABLE contributions ADD COLUMN removal_artifact_key TEXT; +ALTER TABLE contributions ADD COLUMN bars_artifact_key TEXT; +ALTER TABLE contribution_generations ADD COLUMN simulator TEXT; +ALTER TABLE contribution_generations ADD COLUMN generation_hash TEXT; + +CREATE INDEX IF NOT EXISTS idx_contributions_artifact_identity +ON contributions (airport_icao, artifact_identity, simulator, status); diff --git a/schema.sql b/schema.sql index 7cbaad9..c8795c0 100644 --- a/schema.sql +++ b/schema.sql @@ -180,7 +180,8 @@ CREATE TABLE IF NOT EXISTS contributions ( simulator TEXT CHECK ( simulator IN ( 'msfs2020', - 'msfs2024' + 'msfs2024', + 'xplane' ) ) NOT NULL DEFAULT 'msfs2024', submission_date DATETIME DEFAULT CURRENT_TIMESTAMP, @@ -193,7 +194,13 @@ CREATE TABLE IF NOT EXISTS contributions ( ) ) NOT NULL DEFAULT 'pending', rejection_reason TEXT, - decision_date DATETIME + decision_date DATETIME, + generation_token TEXT, + generation_hash TEXT, + artifact_identity TEXT, + artifact_generation_id TEXT, + removal_artifact_key TEXT, + bars_artifact_key TEXT ); CREATE TABLE IF NOT EXISTS notams ( @@ -260,6 +267,8 @@ CREATE TABLE IF NOT EXISTS contribution_generations ( icao TEXT NOT NULL, supports_key TEXT NOT NULL, bars_key TEXT NOT NULL, + simulator TEXT, + generation_hash TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, expires_at DATETIME NOT NULL DEFAULT (datetime('now', '+1 day')) ); @@ -382,6 +391,13 @@ CREATE INDEX IF NOT EXISTS idx_contributions_airport_lowerpkg_status_decision ON decision_date DESC ); +CREATE INDEX IF NOT EXISTS idx_contributions_artifact_identity ON contributions ( + airport_icao, + artifact_identity, + simulator, + status +); + CREATE INDEX IF NOT EXISTS idx_points_timestamps ON points ( created_at, updated_at diff --git a/src/services/contributions.ts b/src/services/contributions.ts index 4d7ddd1..0950d98 100644 --- a/src/services/contributions.ts +++ b/src/services/contributions.ts @@ -7,10 +7,11 @@ import { PostHogService } from './posthog'; import { sanitizeContributionXml } from './xml-sanitizer'; import { DivisionService } from './divisions'; import { DatabaseContextFactory } from './database-context'; +import { generateXPlaneRemovalsJson } from './xplane-removals'; -export type Simulator = 'msfs2020' | 'msfs2024'; +export type Simulator = 'msfs2020' | 'msfs2024' | 'xplane'; -export const VALID_SIMULATORS: readonly Simulator[] = ['msfs2020', 'msfs2024'] as const; +export const VALID_SIMULATORS: readonly Simulator[] = ['msfs2020', 'msfs2024', 'xplane'] as const; export interface Contribution { id: string; @@ -25,6 +26,12 @@ export interface Contribution { status: 'pending' | 'approved' | 'rejected' | 'outdated'; rejectionReason: string | null; decisionDate: string | null; + generationToken: string | null; + generationHash: string | null; + artifactIdentity: string | null; + artifactGenerationId: string | null; + removalArtifactKey: string | null; + barsArtifactKey: string | null; } export interface ContributionSubmission { @@ -34,6 +41,8 @@ export interface ContributionSubmission { submittedXml: string; notes?: string; simulator: Simulator; + generationToken: string; + generationHash: string; } export interface ContributionDecision { @@ -56,15 +65,29 @@ export interface ContributionListResult { export interface LatestApprovedMapDescriptor { packageName: string; simulator: Simulator; + artifactIdentity: string | null; + artifactGenerationId: string | null; + removalArtifactKey: string | null; + barsArtifactKey: string | null; } +export interface ContributionPublication { + artifactIdentity: string; + generationId: string; + removal: { key: string; etag: string; contentType: string }; + bars: { key: string; etag: string; contentType: 'application/xml' }; +} + +export type ContributionDecisionResult = Contribution & { publication?: ContributionPublication }; + import { DatabaseSessionService } from './database-session'; export const MAX_CONTRIBUTION_NOTES_CHARS = 1000; export const MAX_CONTRIBUTION_PACKAGE_CHARS = 64; const ICAO_REGEX = /^[A-Z0-9]{4}$/; -/** URL-safe base64 (no padding) of SHA-256(msfsXml|ICAO). */ +/** URL-safe base64 (no padding) of a SHA-256 digest. */ export const CONTRIBUTION_GENERATION_TOKEN_REGEX = /^[A-Za-z0-9_-]{43}$/; +const CONTRIBUTION_GENERATION_CONTRACT_VERSION = 'bars-contribution-generation/v2'; function sha256ToBase64Url(hashBuf: ArrayBuffer): string { const bytes = new Uint8Array(hashBuf); @@ -75,16 +98,50 @@ function sha256ToBase64Url(hashBuf: ArrayBuffer): string { return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); } -export async function deriveContributionGenerationTokenFromMsfsXml(sanitizedXml: string, icaoUpper: string): Promise { +async function sha256Base64Url(value: string): Promise { const enc = new TextEncoder(); - const icao = icaoUpper.trim().toUpperCase(); - const data = enc.encode(`${sanitizedXml}|${icao}`); - const hashBuf = await crypto.subtle.digest('SHA-256', data); + const hashBuf = await crypto.subtle.digest('SHA-256', enc.encode(value)); return sha256ToBase64Url(hashBuf); } +export function normalizeContributionXml(xml: string): string { + return xml + .trim() + .replace(/\r/g, '') + .replace(/[\t ]+/g, ' ') + .replace(/>\s+<'); +} + +export async function deriveContributionGenerationHash(sanitizedXml: string): Promise { + return sha256Base64Url(normalizeContributionXml(sanitizedXml)); +} + +export async function deriveContributionGenerationToken(sanitizedXml: string, icaoUpper: string, simulator: Simulator): Promise { + const icao = icaoUpper.trim().toUpperCase(); + const normalizedXml = normalizeContributionXml(sanitizedXml); + return sha256Base64Url(`${CONTRIBUTION_GENERATION_CONTRACT_VERSION}|${simulator}|${icao}|${normalizedXml}`); +} + +/** @deprecated Use deriveContributionGenerationToken with an explicit simulator. */ +export async function deriveContributionGenerationTokenFromMsfsXml(sanitizedXml: string, icaoUpper: string): Promise { + return deriveContributionGenerationToken(sanitizedXml, icaoUpper, 'msfs2024'); +} + +export async function deriveArtifactIdentity(packageName: string): Promise { + const canonical = packageName.trim().normalize('NFKC').toLocaleLowerCase('en-US'); + const slug = + canonical + .replace(/[^a-z0-9.-]+/g, '-') + .replace(/^-+|-+$/g, '') + .slice(0, 48) || 'package'; + const digest = await sha256Base64Url(canonical); + return `${slug}--${digest.slice(0, 16)}`; +} + interface StoredContributionGenerationRow { icao: string; + simulator?: Simulator; + generation_hash?: string; supports_key?: string; bars_key?: string; supports_xml?: string; @@ -97,6 +154,8 @@ export type ContributionGenerationLookupResult = status: 'ok'; token: string; icao: string; + simulator?: Simulator; + generationHash?: string; supportsXml: string; barsXml: string; expiresAt: string; @@ -139,6 +198,9 @@ export class ContributionService { c.submitted_xml AS submittedXml, c.notes, c.simulator, c.submission_date AS submissionDate, c.status, c.rejection_reason AS rejectionReason, c.decision_date AS decisionDate, + c.generation_token AS generationToken, c.generation_hash AS generationHash, + c.artifact_identity AS artifactIdentity, c.artifact_generation_id AS artifactGenerationId, + c.removal_artifact_key AS removalArtifactKey, c.bars_artifact_key AS barsArtifactKey, CASE WHEN staff.role IN ('LEAD_DEVELOPER', 'PRODUCT_MANAGER') THEN 1 ELSE 0 END AS actorIsProductManager FROM users actor LEFT JOIN staff ON staff.user_id = actor.id @@ -192,29 +254,38 @@ export class ContributionService { icao: string, supportsKey: string, barsKey: string, + simulator: Simulator, + generationHash: string, ): Promise { try { await session.executeWrite( ` - INSERT INTO contribution_generations (token, icao, supports_key, bars_key, expires_at) - VALUES (?, ?, ?, ?, datetime('now', '+1 day')) + INSERT INTO contribution_generations (token, icao, supports_key, bars_key, simulator, generation_hash, expires_at) + VALUES (?, ?, ?, ?, ?, ?, datetime('now', '+1 day')) `, - [token, icao.toUpperCase(), supportsKey, barsKey], + [token, icao.toUpperCase(), supportsKey, barsKey, simulator, generationHash], ); } catch (error) { const message = error instanceof Error ? error.message.toLowerCase() : ''; if (!message.includes('no such column')) { throw error; } - - // Compatibility for the initial production table, which used XML column names. - await session.executeWrite( - ` - INSERT INTO contribution_generations (token, icao, supports_xml, bars_xml, expires_at) - VALUES (?, ?, ?, ?, datetime('now', '+1 day')) - `, - [token, icao.toUpperCase(), supportsKey, barsKey], - ); + try { + await session.executeWrite( + `INSERT INTO contribution_generations (token, icao, supports_key, bars_key, expires_at) + VALUES (?, ?, ?, ?, datetime('now', '+1 day'))`, + [token, icao.toUpperCase(), supportsKey, barsKey], + ); + } catch (legacyError) { + const legacyMessage = legacyError instanceof Error ? legacyError.message.toLowerCase() : ''; + if (!legacyMessage.includes('no such column')) throw legacyError; + // Compatibility for the initial production table, which used XML column names. + await session.executeWrite( + `INSERT INTO contribution_generations (token, icao, supports_xml, bars_xml, expires_at) + VALUES (?, ?, ?, ?, datetime('now', '+1 day'))`, + [token, icao.toUpperCase(), supportsKey, barsKey], + ); + } } } @@ -270,7 +341,14 @@ export class ContributionService { } } - async storeGeneration(icao: string, supportsXml: string, barsXml: string, contentToken: string): Promise { + async storeGeneration( + icao: string, + removalArtifact: string, + barsXml: string, + contentToken: string, + simulator: Simulator, + generationHash: string, + ): Promise { const token = contentToken; const icaoUpper = icao.trim().toUpperCase(); @@ -292,12 +370,13 @@ export class ContributionService { return token; } - const supportsKey = `contribution-generations/${token}/supports.xml`; + const isXPlane = simulator === 'xplane'; + const supportsKey = `contribution-generations/${token}/${isXPlane ? 'removals.json' : 'supports.xml'}`; const barsKey = `contribution-generations/${token}/bars.xml`; await Promise.all([ - this.storage.put(supportsKey, supportsXml, { - httpMetadata: { contentType: 'application/xml' }, + this.storage.put(supportsKey, removalArtifact, { + httpMetadata: { contentType: isXPlane ? 'application/json' : 'application/xml' }, }), this.storage.put(barsKey, barsXml, { httpMetadata: { contentType: 'application/xml' }, @@ -305,7 +384,7 @@ export class ContributionService { ]); try { - await this.insertContributionGenerationRow(session, token, icaoUpper, supportsKey, barsKey); + await this.insertContributionGenerationRow(session, token, icaoUpper, supportsKey, barsKey, simulator, generationHash); } catch (error) { const message = error instanceof Error ? error.message.toLowerCase() : ''; if (!message.includes('unique') && !message.includes('constraint')) { @@ -331,7 +410,7 @@ export class ContributionService { try { result = await session.executeLatest( ` - SELECT icao, supports_key, bars_key, expires_at + SELECT icao, supports_key, bars_key, simulator, generation_hash, expires_at FROM contribution_generations WHERE token = ? AND expires_at > datetime('now') LIMIT 1 @@ -344,15 +423,23 @@ export class ContributionService { throw error; } - result = await session.executeLatest( - ` - SELECT icao, supports_xml, bars_xml, expires_at - FROM contribution_generations - WHERE token = ? AND expires_at > datetime('now') - LIMIT 1 - `, - [token], - ); + try { + result = await session.executeLatest( + `SELECT icao, supports_key, bars_key, expires_at + FROM contribution_generations + WHERE token = ? AND expires_at > datetime('now') LIMIT 1`, + [token], + ); + } catch (legacyError) { + const legacyMessage = legacyError instanceof Error ? legacyError.message.toLowerCase() : ''; + if (!legacyMessage.includes('no such column')) throw legacyError; + result = await session.executeLatest( + `SELECT icao, supports_xml, bars_xml, expires_at + FROM contribution_generations + WHERE token = ? AND expires_at > datetime('now') LIMIT 1`, + [token], + ); + } } const generation = result.results[0]; if (!generation) { @@ -371,6 +458,8 @@ export class ContributionService { status: 'ok', token, icao: generation.icao, + simulator: generation.simulator, + generationHash: generation.generation_hash, supportsXml, barsXml, expiresAt: generation.expires_at, @@ -443,15 +532,37 @@ export class ContributionService { const msg = e instanceof Error ? e.message : 'Invalid XML'; throw new Error(msg); } + const embeddedXPlane = /]*\bsimulator\s*=\s*["']xplane["']/i.test(trimmedXml); + if ((submission.simulator === 'xplane') !== embeddedXPlane) { + throw new Error('Draft simulator metadata does not match the selected simulator'); + } + if (submission.simulator === 'xplane') { + generateXPlaneRemovalsJson(trimmedXml, normalizedAirportIcao); + } - // Normalize XML content for stable hashing and comparison - const normalize = (xml: string) => - xml - .trim() - .replace(/\r/g, '') - .replace(/[\t ]+/g, ' ') - .replace(/>\s+<'); - const normalizedXml = normalize(trimmedXml); + const normalizedXml = normalizeContributionXml(trimmedXml); + if (!CONTRIBUTION_GENERATION_TOKEN_REGEX.test(submission.generationToken || '')) { + throw new Error('generationToken must be a valid tested-generation token'); + } + if (!CONTRIBUTION_GENERATION_TOKEN_REGEX.test(submission.generationHash || '')) { + throw new Error('generationHash must be a valid tested-draft hash'); + } + const [expectedToken, expectedHash] = await Promise.all([ + deriveContributionGenerationToken(trimmedXml, normalizedAirportIcao, submission.simulator), + deriveContributionGenerationHash(trimmedXml), + ]); + if (submission.generationToken !== expectedToken || submission.generationHash !== expectedHash) { + throw new Error('Submitted draft does not match the tested generation'); + } + const storedGeneration = await this.getStoredGeneration(expectedToken); + if ( + storedGeneration.status !== 'ok' || + storedGeneration.icao !== normalizedAirportIcao || + (storedGeneration.simulator !== undefined && storedGeneration.simulator !== submission.simulator) || + (storedGeneration.generationHash !== undefined && storedGeneration.generationHash !== expectedHash) + ) { + throw new Error('Tested generation was not found or has expired; test this exact draft again'); + } // Prevent duplicate or stolen submissions (same package + simulator): const existingForPackage = await this.dbSession.executeRead<{ @@ -465,7 +576,7 @@ export class ContributionService { [sanitizedPackageName, submission.simulator], ); for (const row of existingForPackage.results) { - if (normalize(row.submitted_xml) === normalizedXml) { + if (normalizeContributionXml(row.submitted_xml) === normalizedXml) { throw new Error( 'Duplicate submission detected: XML matches an existing contribution for the same package and simulator. Please submit original work.', ); @@ -481,8 +592,8 @@ export class ContributionService { INSERT INTO contributions ( id, user_id, airport_icao, package_name, submitted_xml, notes, - simulator, submission_date, status - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + simulator, submission_date, status, generation_token, generation_hash + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `, [ id, @@ -494,6 +605,8 @@ export class ContributionService { submission.simulator, now, 'pending', + expectedToken, + expectedHash, ], ); @@ -510,6 +623,12 @@ export class ContributionService { status: 'pending', rejectionReason: null, decisionDate: null, + generationToken: expectedToken, + generationHash: expectedHash, + artifactIdentity: null, + artifactGenerationId: null, + removalArtifactKey: null, + barsArtifactKey: null, }; try { this.posthog?.track('Contribution Submitted', { @@ -531,7 +650,10 @@ export class ContributionService { c.airport_icao as airportIcao, c.package_name as packageName, c.submitted_xml as submittedXml, c.notes, c.simulator, c.submission_date as submissionDate, c.status, - c.rejection_reason as rejectionReason, c.decision_date as decisionDate + c.rejection_reason as rejectionReason, c.decision_date as decisionDate, + c.generation_token as generationToken, c.generation_hash as generationHash, + c.artifact_identity as artifactIdentity, c.artifact_generation_id as artifactGenerationId, + c.removal_artifact_key as removalArtifactKey, c.bars_artifact_key as barsArtifactKey FROM contributions c LEFT JOIN users u ON u.vatsim_id = c.user_id WHERE c.id = ? @@ -561,7 +683,9 @@ export class ContributionService { } const result = await this.dbSession.executeRead( ` - SELECT c.package_name AS packageName, c.simulator + SELECT c.package_name AS packageName, c.simulator, + c.artifact_identity AS artifactIdentity, c.artifact_generation_id AS artifactGenerationId, + c.removal_artifact_key AS removalArtifactKey, c.bars_artifact_key AS barsArtifactKey FROM contributions c WHERE c.airport_icao = ? AND lower(c.package_name) = lower(?) AND c.status = 'approved'${simulatorClause} ORDER BY c.decision_date DESC @@ -599,7 +723,10 @@ export class ContributionService { c.airport_icao as airportIcao, c.package_name as packageName, c.submitted_xml as submittedXml, c.notes, c.simulator, c.submission_date as submissionDate, c.status, - c.rejection_reason as rejectionReason, c.decision_date as decisionDate + c.rejection_reason as rejectionReason, c.decision_date as decisionDate, + c.generation_token as generationToken, c.generation_hash as generationHash, + c.artifact_identity as artifactIdentity, c.artifact_generation_id as artifactGenerationId, + c.removal_artifact_key as removalArtifactKey, c.bars_artifact_key as barsArtifactKey FROM contributions c LEFT JOIN users u ON u.vatsim_id = c.user_id ${whereClause} @@ -614,6 +741,43 @@ export class ContributionService { }; } + async listContributionMetadata(options: ContributionListOptions): Promise<{ + contributions: Array>; + total: number; + }> { + const { status = 'all', airportIcao, userId } = options; + const whereConditions: string[] = []; + const params: string[] = []; + if (status !== 'all') { + whereConditions.push('c.status = ?'); + params.push(status); + } + if (airportIcao) { + whereConditions.push('c.airport_icao = ?'); + params.push(airportIcao); + } + if (userId) { + whereConditions.push('c.user_id = ?'); + params.push(userId); + } + const whereClause = whereConditions.length > 0 ? `WHERE ${whereConditions.join(' AND ')}` : ''; + const result = await this.dbSession.executeRead>( + `SELECT + c.id, c.user_id AS userId, u.display_name AS userDisplayName, + c.airport_icao AS airportIcao, c.package_name AS packageName, c.notes, c.simulator, + c.submission_date AS submissionDate, c.status, + c.rejection_reason AS rejectionReason, c.decision_date AS decisionDate, + c.artifact_identity AS artifactIdentity, c.artifact_generation_id AS artifactGenerationId, + c.removal_artifact_key AS removalArtifactKey, c.bars_artifact_key AS barsArtifactKey + FROM contributions c + LEFT JOIN users u ON u.vatsim_id = c.user_id + ${whereClause} + ORDER BY c.submission_date DESC`, + params, + ); + return { contributions: result.results, total: result.results.length }; + } + async listContributionsSimple(options: ContributionListOptions): Promise<{ contributions: Array<{ id: string; @@ -667,7 +831,98 @@ export class ContributionService { }; } - async processDecision(id: string, userId: string, decision: ContributionDecision): Promise { + private async generateAndPublishArtifacts( + contribution: Contribution, + packageName: string, + operation: 'approval' | 'regeneration', + ): Promise { + const [removalArtifact, barsXml] = await Promise.all([ + contribution.simulator === 'xplane' + ? Promise.resolve(generateXPlaneRemovalsJson(contribution.submittedXml, contribution.airportIcao)) + : this.supportService.generateLightSupportsXML(contribution.submittedXml, contribution.airportIcao), + this.polygonService.processBarsXML(contribution.submittedXml, contribution.airportIcao), + ]); + + const [artifactIdentity, removalHash, barsHash] = await Promise.all([ + deriveArtifactIdentity(packageName), + sha256Base64Url(removalArtifact), + sha256Base64Url(barsXml), + ]); + const generationId = crypto.randomUUID(); + const prefix = `ContributionArtifacts/${contribution.airportIcao}/${artifactIdentity}/${contribution.simulator}/${generationId}`; + const removalContentType = contribution.simulator === 'xplane' ? 'application/json' : 'application/xml'; + const removalKey = `${prefix}/${contribution.simulator === 'xplane' ? 'removals.json' : 'supports.xml'}`; + const barsKey = `${prefix}/bars.xml`; + const commonMetadata = { + airportIcao: contribution.airportIcao, + packageName, + simulator: contribution.simulator, + artifactIdentity, + artifactGenerationId: generationId, + contributionId: contribution.id, + operation, + }; + + try { + const [removal, bars] = await Promise.all([ + this.storageService.uploadFile(removalKey, removalArtifact, removalContentType, { + ...commonMetadata, + type: 'removal', + contentHash: removalHash, + }), + this.storageService.uploadFile(barsKey, barsXml, 'application/xml', { + ...commonMetadata, + type: 'bars', + contentHash: barsHash, + }), + ]); + return { + artifactIdentity, + generationId, + removal: { ...removal, contentType: removalContentType }, + bars: { ...bars, contentType: 'application/xml' }, + }; + } catch (error) { + await Promise.allSettled([this.storageService.deleteFile(removalKey), this.storageService.deleteFile(barsKey)]); + throw error; + } + } + + private async discardPublication(publication: ContributionPublication): Promise { + await Promise.allSettled([ + this.storageService.deleteFile(publication.removal.key), + this.storageService.deleteFile(publication.bars.key), + ]); + } + + private async isPublicationCurrent(id: string, publication: ContributionPublication): Promise { + try { + const current = await this.dbSession.executeLatest<{ + status: Contribution['status']; + artifactGenerationId: string | null; + removalArtifactKey: string | null; + barsArtifactKey: string | null; + }>( + `SELECT status, artifact_generation_id AS artifactGenerationId, + removal_artifact_key AS removalArtifactKey, bars_artifact_key AS barsArtifactKey + FROM contributions WHERE id = ? LIMIT 1`, + [id], + ); + const row = current.results[0]; + return Boolean( + row?.status === 'approved' && + row.artifactGenerationId === publication.generationId && + row.removalArtifactKey === publication.removal.key && + row.barsArtifactKey === publication.bars.key, + ); + } catch { + // An indeterminate D1 result must never trigger deletion of artifacts + // that may already be the committed pair. + return null; + } + } + + async processDecision(id: string, userId: string, decision: ContributionDecision): Promise { const context = await this.getContributionActionContext(userId, id); if (!context) { @@ -706,61 +961,48 @@ export class ContributionService { params: [status, rejectionReason, now, packageName, id], }; - // If approving, atomically outdate older approvals and publish this one. + let publication: ContributionPublication | undefined; + // Generate and publish a complete immutable pair before the D1 pointer moves. if (decision.approved) { - await this.dbSession.executeBatch([ - { - query: `UPDATE contributions - SET status = 'outdated', decision_date = ? - WHERE airport_icao = ? - AND package_name = ? - AND simulator = ? - AND status = 'approved' - AND id != ?`, - params: [now, contribution.airportIcao, packageName, contribution.simulator, id], - }, - updateCurrent, - ]); - - // Generate and upload the XML files to CDN + publication = await this.generateAndPublishArtifacts(contribution, packageName, 'approval'); try { - // Generate both XML files from the submitted XML - const [supportsXml, barsXml] = await Promise.all([ - this.supportService.generateLightSupportsXML(contribution.submittedXml, contribution.airportIcao), - this.polygonService.processBarsXML(contribution.submittedXml), + const [, approvedWrite] = await this.dbSession.executeBatch([ + { + query: `UPDATE contributions + SET status = 'outdated', decision_date = ? + WHERE airport_icao = ? + AND package_name = ? COLLATE NOCASE + AND simulator = ? + AND status = 'approved' + AND id != ? + AND EXISTS (SELECT 1 FROM contributions target WHERE target.id = ? AND target.status = 'pending')`, + params: [now, contribution.airportIcao, packageName, contribution.simulator, id, id], + }, + { + query: `UPDATE contributions + SET status = 'approved', rejection_reason = NULL, decision_date = ?, package_name = ?, + artifact_identity = ?, artifact_generation_id = ?, removal_artifact_key = ?, bars_artifact_key = ? + WHERE id = ? AND status = 'pending'`, + params: [ + now, + packageName, + publication.artifactIdentity, + publication.generationId, + publication.removal.key, + publication.bars.key, + id, + ], + }, ]); - - // Create safe filenames for the uploads (include simulator to keep them separate) - const safePackageName = packageName.replace(/[^a-zA-Z0-9.-]/g, '-'); - - // Filename format: ICAO_PackageName_simulator_type.xml - const supportsFileName = `${contribution.airportIcao}_${safePackageName}_${contribution.simulator}_supports.xml`; - const barsFileName = `${contribution.airportIcao}_${safePackageName}_${contribution.simulator}_bars.xml`; - - // Set folder paths for each file type - const removalObjectsPath = 'RemovalObjects'; - const mapsPath = 'Maps'; - - // Upload the files to CDN - await Promise.all([ - // Upload light supports XML to RemovalObjects folder - this.storageService.uploadFile(`${removalObjectsPath}/${supportsFileName}`, supportsXml, 'application/xml', { - airportIcao: contribution.airportIcao, - packageName: contribution.packageName, - type: 'removal', - generatedFrom: `contribution_${id}`, - }), - - // Upload BARS XML to Maps folder - this.storageService.uploadFile(`${mapsPath}/${barsFileName}`, barsXml, 'application/xml', { - airportIcao: contribution.airportIcao, - packageName: contribution.packageName, - type: 'bars', - generatedFrom: `contribution_${id}`, - }), - ]); - } catch { - // Don't throw the error, as we still want to update the contribution status + if (approvedWrite?.meta?.changes === 0) { + throw new Error('Contribution approval lost a concurrent decision race'); + } + } catch (error) { + const publicationIsCurrent = await this.isPublicationCurrent(id, publication); + if (publicationIsCurrent !== true) { + if (publicationIsCurrent === false) await this.discardPublication(publication); + throw error; + } } } else { await this.dbSession.executeWrite(updateCurrent.query, updateCurrent.params); @@ -772,6 +1014,10 @@ export class ContributionService { status, rejectionReason, decisionDate: now, + artifactIdentity: publication?.artifactIdentity ?? contribution.artifactIdentity, + artifactGenerationId: publication?.generationId ?? contribution.artifactGenerationId, + removalArtifactKey: publication?.removal.key ?? contribution.removalArtifactKey, + barsArtifactKey: publication?.bars.key ?? contribution.barsArtifactKey, }; try { this.posthog?.track(decision.approved ? 'Contribution Approved' : 'Contribution Rejected', { @@ -785,7 +1031,7 @@ export class ContributionService { } catch (e) { console.warn('Posthog track failed (Contribution Decision)', e); } - return updated; + return publication ? { ...updated, publication } : updated; } async getContributionStats(): Promise<{ total: number; @@ -895,32 +1141,24 @@ export class ContributionService { } try { - // Generate both XMLs - const [supportsXml, barsXml] = await Promise.all([ - this.supportService.generateLightSupportsXML(contribution.submittedXml, contribution.airportIcao), - this.polygonService.processBarsXML(contribution.submittedXml), - ]); - - // Safe filename components (include simulator to keep them separate) - const safePackageName = contribution.packageName.replace(/[^a-zA-Z0-9.-]/g, '-'); - const supportsFileName = `${contribution.airportIcao}_${safePackageName}_${contribution.simulator}_supports.xml`; - const barsFileName = `${contribution.airportIcao}_${safePackageName}_${contribution.simulator}_bars.xml`; - - // Upload to the same paths (overwrite) - const [supportsRes, barsRes] = await Promise.all([ - this.storageService.uploadFile(`RemovalObjects/${supportsFileName}`, supportsXml, 'application/xml', { - airportIcao: contribution.airportIcao, - packageName: contribution.packageName, - type: 'removal', - regeneratedFrom: `contribution_${id}`, - }), - this.storageService.uploadFile(`Maps/${barsFileName}`, barsXml, 'application/xml', { - airportIcao: contribution.airportIcao, - packageName: contribution.packageName, - type: 'bars', - regeneratedFrom: `contribution_${id}`, - }), - ]); + const publication = await this.generateAndPublishArtifacts(contribution, contribution.packageName, 'regeneration'); + try { + const switched = await this.dbSession.executeWrite( + `UPDATE contributions + SET artifact_identity = ?, artifact_generation_id = ?, removal_artifact_key = ?, bars_artifact_key = ? + WHERE id = ? AND status = 'approved'`, + [publication.artifactIdentity, publication.generationId, publication.removal.key, publication.bars.key, id], + ); + if (switched.meta?.changes === 0) { + throw new Error('Contribution is no longer approved'); + } + } catch (error) { + const publicationIsCurrent = await this.isPublicationCurrent(id, publication); + if (publicationIsCurrent !== true) { + if (publicationIsCurrent === false) await this.discardPublication(publication); + throw error; + } + } try { this.posthog?.track('Contribution Regenerated', { @@ -937,8 +1175,8 @@ export class ContributionService { return { airportIcao: contribution.airportIcao, packageName: contribution.packageName, - maps: { key: barsRes.key, etag: barsRes.etag }, - supports: { key: supportsRes.key, etag: supportsRes.etag }, + maps: { key: publication.bars.key, etag: publication.bars.etag }, + supports: { key: publication.removal.key, etag: publication.removal.etag }, }; } catch (e) { const msg = e instanceof Error ? e.message : 'Unknown error'; diff --git a/src/services/lights-cache.ts b/src/services/lights-cache.ts index 6c03001..58e5cc9 100644 --- a/src/services/lights-cache.ts +++ b/src/services/lights-cache.ts @@ -55,6 +55,21 @@ export async function getLightsByObject(env: Env, icao: string): Promise maxBytes) { - throw new Error(`Submitted XML too large (> ${maxBytes} chars)`); + // Reject cheaply by code-unit length first, then enforce the real UTF-8 byte contract. + if (raw.length > maxBytes || new TextEncoder().encode(raw).byteLength > maxBytes) { + throw new Error(`Submitted XML too large (> ${maxBytes} bytes)`); } const trimmed = raw.trim(); diff --git a/src/services/xplane-removals.ts b/src/services/xplane-removals.ts new file mode 100644 index 0000000..e2cc63a --- /dev/null +++ b/src/services/xplane-removals.ts @@ -0,0 +1,195 @@ +export interface XPlaneRemovalSelector { + feature: string; + code: number; + run: number; + ranges: Array<[number, number]>; +} + +export interface XPlaneRemovalArtifact { + schema: 'bars-xplane-removals/v1'; + icao: string; + selectors: XPlaneRemovalSelector[]; +} + +const FEATURE_PATTERN = /^[a-f0-9]{16}$/i; +const XPLANE_LIGHT_CODES = new Set([101, 102, 103, 104, 105, 106, 107, 108]); +const FRACTION_BOUNDARY_TOLERANCE = 0.00001; +const MAX_XPLANE_REMOVAL_ELEMENTS = 20_000; +const MAX_XPLANE_XML_CHARS = 5 * 1024 * 1024; +const MAX_TAG_CHARS = 2_048; +const TAG_PATTERN = /<(?:[^>"']|"[^"]*"|'[^']*')+>/g; +const ATTRIBUTE_PATTERN = /\s+([A-Za-z_:][\w:.-]*)\s*=\s*(?:"([^"]*)"|'([^']*)')/y; + +/** + * Parse the compact X-Plane removal section without accepting partial or + * ambiguously quoted attributes. The scanner is deliberately bounded because + * contribution XML is user-controlled and executes inside a Worker isolate. + */ +export function generateXPlaneRemovalsJson(xml: string, icao: string): string { + if (xml.length > MAX_XPLANE_XML_CHARS) throw new Error('Invalid X-Plane draft: XML is too large'); + const grouped = new Map(); + let sawFsData = false; + let sawRemovals = false; + let insideRemovals = false; + let removalElementCount = 0; + + for (const match of xml.matchAll(TAG_PATTERN)) { + const tag = match[0]; + if (tag.length > MAX_TAG_CHARS) { + throw new Error('Invalid X-Plane draft: XML tag is too large'); + } + if (tag.startsWith('