diff --git a/.gitignore b/.gitignore index b34e45f..286e1e3 100644 --- a/.gitignore +++ b/.gitignore @@ -174,4 +174,7 @@ dist # cloudflare project .dev.vars -.wrangler/ \ No newline at end of file +.wrangler/ + +# Local development tests +*.test.ts diff --git a/.oxlintrc.json b/.oxlintrc.json new file mode 100644 index 0000000..f6fefb2 --- /dev/null +++ b/.oxlintrc.json @@ -0,0 +1,45 @@ +{ + "$schema": "./node_modules/oxlint/configuration_schema.json", + "ignorePatterns": [ + ".agent/**", + ".agents/**", + ".claude/**", + ".codex/**", + ".continue/**", + ".cursor/**", + ".gemini/**", + ".git/**", + ".opencode/**", + ".pi/**", + ".roo/**", + ".vscode/**", + ".windsurf/**", + ".wrangler/**", + "build/**", + "coverage/**", + "dist/**", + "node_modules/**", + "tools/oxlint/anti-slop/**", + "worker-configuration.d.ts" + ], + "jsPlugins": [ + { "name": "anti-slop", "specifier": "./tools/oxlint/anti-slop/index.ts" } + ], + "rules": { + "anti-slop/no-chained-type-assertions": "error", + "anti-slop/no-conditional-empty-object-spread": "error", + "anti-slop/no-known-value-widening": "error", + "anti-slop/no-module-mocking": "error", + "anti-slop/no-object-parameters": "error", + "anti-slop/no-reflect-apply": "error", + "anti-slop/no-reflect-get": "error", + "anti-slop/no-runtime-typeof": "error", + "anti-slop/no-shape-in-symbol-names": "error", + "anti-slop/no-unknown-parameters": "error", + "anti-slop/no-unknown-returns": "error", + "anti-slop/no-unknown-type-aliases": "error", + "anti-slop/no-unsafe-dictionary-type": "error", + "anti-slop/no-widen-then-assert": "error", + "anti-slop/require-safety-comment-for-type-assertion": "error" + } +} 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..88595a5 100644 --- a/bun.lock +++ b/bun.lock @@ -11,17 +11,19 @@ "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", + "@oxlint/plugins": "1.78.0", + "@types/node": "24.10.9", + "@types/swagger-jsdoc": "6.0.4", + "eslint": "9.39.2", + "globals": "16.5.0", + "jiti": "2.6.1", + "oxlint": "1.78.0", + "prettier": "3.8.0", + "ts-node": "10.9.2", + "typescript": "5.9.3", + "typescript-eslint": "8.53.0", + "wrangler": "4.112.0", }, }, }, @@ -188,6 +190,46 @@ "@jsdevtools/ono": ["@jsdevtools/ono@7.1.3", "", {}, "sha512-4JQNk+3mVzK3xh2rqd6RB4J46qUR19azEHBneZyTZM+c456qOrbbM/5xcR8huNCCcbVt7+UmizG6GuUvPvKUYg=="], + "@oxlint/binding-android-arm-eabi": ["@oxlint/binding-android-arm-eabi@1.78.0", "", { "os": "android", "cpu": "arm" }, "sha512-Bu819lmAfZMUHErrpe0cEWj3iaefuUODHSU8+UbXy67V/r7/7f4K3FL0NmbD85E+wiFLDYuhP8Zlv0XnVeXshw=="], + + "@oxlint/binding-android-arm64": ["@oxlint/binding-android-arm64@1.78.0", "", { "os": "android", "cpu": "arm64" }, "sha512-CDfxZgB61B7buRdY2FJoAYYPPXCZ1EoC1LKscnC5dg3kjobdxiconvAvvN1BmHyW4PyFT3jRLDag/BY/roSNBQ=="], + + "@oxlint/binding-darwin-arm64": ["@oxlint/binding-darwin-arm64@1.78.0", "", { "os": "darwin", "cpu": "arm64" }, "sha512-2Y2U9Ahrz+OO0Ej88f9SJYq51/jUBp1Mc7iZu0ukrbeeZ3gpRGfzIFnoqfHDY96xr0GEfNrPUBFEy0nN5aD7HA=="], + + "@oxlint/binding-darwin-x64": ["@oxlint/binding-darwin-x64@1.78.0", "", { "os": "darwin", "cpu": "x64" }, "sha512-rpych6eJq6m9jDRypTEaPD1xysaEW5h9+xuxhGK/QhOg+/xaqPZrCrTNoIl/f3nEjuJeCEmstNDlrE9rJi/3/g=="], + + "@oxlint/binding-freebsd-x64": ["@oxlint/binding-freebsd-x64@1.78.0", "", { "os": "freebsd", "cpu": "x64" }, "sha512-IcMGrQT3QizkOESUJd5et+rOhVqSkNDfNik1cvrKDqIbzqx9KMtRswpFgkCuNTSwylCFLKhGUu8KmqY1ZnC0Dg=="], + + "@oxlint/binding-linux-arm-gnueabihf": ["@oxlint/binding-linux-arm-gnueabihf@1.78.0", "", { "os": "linux", "cpu": "arm" }, "sha512-/uLdoJ0IXE6vo/0f0LKjinQAp+re+VMaCWaNT8ENIv2EOCkSsc8SGaflXAuW0Jua2dq5+GLVWm1NQK7P3UFSNQ=="], + + "@oxlint/binding-linux-arm-musleabihf": ["@oxlint/binding-linux-arm-musleabihf@1.78.0", "", { "os": "linux", "cpu": "arm" }, "sha512-7xi4Wb/O8NRJhLoUXmDJMUVpNYvB5kefdhFU1Jb8rtae4QoXlTiLwI14X4YvAXVZLNZChP8m5qO9SQAlWQTbkQ=="], + + "@oxlint/binding-linux-arm64-gnu": ["@oxlint/binding-linux-arm64-gnu@1.78.0", "", { "os": "linux", "cpu": "arm64" }, "sha512-4hFW0+fVXa3OIh1Y4A5SPkmvI4wuuBSrCVKzOyE7PTjhc7yEqZ1pmvEEeS5Lj/MaqvegFxXyF33N+6jkehxdyg=="], + + "@oxlint/binding-linux-arm64-musl": ["@oxlint/binding-linux-arm64-musl@1.78.0", "", { "os": "linux", "cpu": "arm64" }, "sha512-oC0mvsgBJjlMijSDEhx9KuvR9zYeHXceA9MjbuXB1F8NSR78Yj2unOBrstEvTVaq+pko+kuue6DajC00eqvTdg=="], + + "@oxlint/binding-linux-ppc64-gnu": ["@oxlint/binding-linux-ppc64-gnu@1.78.0", "", { "os": "linux", "cpu": "ppc64" }, "sha512-XAllT5SUZS+ohjuZ3/5S0cwe0r7eboiuigeStCZ5DXRYx/2KVM2UvQXvAfyzXEimtQjAB7cDQ2YxDe2Zl2WNQQ=="], + + "@oxlint/binding-linux-riscv64-gnu": ["@oxlint/binding-linux-riscv64-gnu@1.78.0", "", { "os": "linux", "cpu": "none" }, "sha512-trucMER/0QtecoXvc1y/UVqE3kwJipDwrx4oHfj+nNm3dq2zjP44WT0CfHNDPM3G1DXIkx/gY6lAD21NSCZVhA=="], + + "@oxlint/binding-linux-riscv64-musl": ["@oxlint/binding-linux-riscv64-musl@1.78.0", "", { "os": "linux", "cpu": "none" }, "sha512-cm3O4F/HQbdzOUX5mKHqG5KDL6E5w0pnlZ+fbBy2rmLryPOowkuLagFHTopQsEIpjcaZoPOrL+BmmAytAG9HFg=="], + + "@oxlint/binding-linux-s390x-gnu": ["@oxlint/binding-linux-s390x-gnu@1.78.0", "", { "os": "linux", "cpu": "s390x" }, "sha512-33wRf6HqGNsybJ3qX4cGaQN2ODPxNmc1rMa0mrTmx3eFq1VzOnvQooi9bIGVYakW8a/wmqVx1mgsUm8R2xfTiw=="], + + "@oxlint/binding-linux-x64-gnu": ["@oxlint/binding-linux-x64-gnu@1.78.0", "", { "os": "linux", "cpu": "x64" }, "sha512-rRdISSYegj6VganMZ9tjRjijowfHJ09IZU01i0toBAqr6n5LEtwHq2IeS4FjW2RoskOHlb6efB26H5izYb3GEQ=="], + + "@oxlint/binding-linux-x64-musl": ["@oxlint/binding-linux-x64-musl@1.78.0", "", { "os": "linux", "cpu": "x64" }, "sha512-GmsP4rW0xTL6u5CVdcDsaN5Fbc7hBc382Wmar1kttbnwSEviM+rSINKOMQ+UQ6iH+AGwC+8gaAiwu134Tgh6Lg=="], + + "@oxlint/binding-openharmony-arm64": ["@oxlint/binding-openharmony-arm64@1.78.0", "", { "os": "none", "cpu": "arm64" }, "sha512-sy9yeYuADc8a+n4TLBayzMCZiHPW78DcIFVpOXTmdKHWQeM9xe5uzkqIIZmi326D5hY9XVwacipEB1p7tQjPAg=="], + + "@oxlint/binding-win32-arm64-msvc": ["@oxlint/binding-win32-arm64-msvc@1.78.0", "", { "os": "win32", "cpu": "arm64" }, "sha512-rjc2hF1KfMi8fZj1X/m3AmnHbdsF3rL0v6KQg0Uc880Yb2khjz+3U14sfdZ7jWTpRnN1m1NQa/TT7uU9lJWPrA=="], + + "@oxlint/binding-win32-ia32-msvc": ["@oxlint/binding-win32-ia32-msvc@1.78.0", "", { "os": "win32", "cpu": "ia32" }, "sha512-zcuXFVrEFHIafRfkCQT8w/Xe41o07ozl/vwHq7p94vB29xVzsB0sZGYORU1jhcYKv3Lr0J3HbJ2T4fHH5rWmvA=="], + + "@oxlint/binding-win32-x64-msvc": ["@oxlint/binding-win32-x64-msvc@1.78.0", "", { "os": "win32", "cpu": "x64" }, "sha512-Sb5ocmLSuYeOuXd+CFOToGKp/gjXUEWDnvIGwhnh8aq8wY4TMmEnKnvbogSW7RdMZv77JSARduS7/gv+khYEjA=="], + + "@oxlint/plugins": ["@oxlint/plugins@1.78.0", "", {}, "sha512-Ypt8KeRYw+4jUtlPirfcHWMrn5ms12VrrFPD+Mds477/7tJxG1Kcz2Yrg2nVcTQEUx/GdlhS+BUg1kmxNm04Ug=="], + "@poppinss/colors": ["@poppinss/colors@4.1.5", "", { "dependencies": { "kleur": "^4.1.5" } }, "sha512-FvdDqtcRCtz6hThExcFOgW0cWX+xwSMWcRuQe5ZEb2m7cVQOAVZOIMt+/v9RxGiD9/OY16qJBXK4CVKWAPalBw=="], "@poppinss/dumper": ["@poppinss/dumper@0.6.4", "", { "dependencies": { "@poppinss/colors": "^4.1.5", "@sindresorhus/is": "^7.0.2", "supports-color": "^10.0.0" } }, "sha512-iG0TIdqv8xJ3Lt9O8DrPRxw1MRLjNpoqiSGU03P/wNLP/s0ra0udPJ1J2Tx5M0J3H/cVyEgpbn8xUKRY9j59kQ=="], @@ -398,6 +440,8 @@ "optionator": ["optionator@0.9.4", "", { "dependencies": { "deep-is": "^0.1.3", "fast-levenshtein": "^2.0.6", "levn": "^0.4.1", "prelude-ls": "^1.2.1", "type-check": "^0.4.0", "word-wrap": "^1.2.5" } }, "sha512-6IpQ7mKUxRcZNLIObR0hz7lxsapSSIYNZJwXPGeF0mTVqGKFIXj1DQcMoT22S3ROcLyY/rz0PWaWZ9ayWmad9g=="], + "oxlint": ["oxlint@1.78.0", "", { "optionalDependencies": { "@oxlint/binding-android-arm-eabi": "1.78.0", "@oxlint/binding-android-arm64": "1.78.0", "@oxlint/binding-darwin-arm64": "1.78.0", "@oxlint/binding-darwin-x64": "1.78.0", "@oxlint/binding-freebsd-x64": "1.78.0", "@oxlint/binding-linux-arm-gnueabihf": "1.78.0", "@oxlint/binding-linux-arm-musleabihf": "1.78.0", "@oxlint/binding-linux-arm64-gnu": "1.78.0", "@oxlint/binding-linux-arm64-musl": "1.78.0", "@oxlint/binding-linux-ppc64-gnu": "1.78.0", "@oxlint/binding-linux-riscv64-gnu": "1.78.0", "@oxlint/binding-linux-riscv64-musl": "1.78.0", "@oxlint/binding-linux-s390x-gnu": "1.78.0", "@oxlint/binding-linux-x64-gnu": "1.78.0", "@oxlint/binding-linux-x64-musl": "1.78.0", "@oxlint/binding-openharmony-arm64": "1.78.0", "@oxlint/binding-win32-arm64-msvc": "1.78.0", "@oxlint/binding-win32-ia32-msvc": "1.78.0", "@oxlint/binding-win32-x64-msvc": "1.78.0" }, "peerDependencies": { "oxlint-tsgolint": ">=7.0.2001", "vite-plus": "*" }, "optionalPeers": ["oxlint-tsgolint", "vite-plus"], "bin": { "oxlint": "bin/oxlint" } }, "sha512-QgQePuxIqKOzo1KSjG2EnITEeWvWnKAm77eq8nrMtf6AGoA+zyGc4PFYtDNJSD25g/ibOwfQ851hZ4/SPkMVoA=="], + "p-limit": ["p-limit@3.1.0", "", { "dependencies": { "yocto-queue": "^0.1.0" } }, "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ=="], "p-locate": ["p-locate@5.0.0", "", { "dependencies": { "p-limit": "^3.0.2" } }, "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw=="], diff --git a/data/openapi.json b/data/openapi.json index a0a74c8..613b0f5 100644 --- a/data/openapi.json +++ b/data/openapi.json @@ -254,7 +254,7 @@ "tags": ["RealTime"], "description": "Performs a WebSocket upgrade to stream real-time airport state. Requires:\n- GET with `Upgrade: websocket`\n- `airport` (ICAO, 4 chars) and an API key via either:\n - `key` query parameter, or\n - `Authorization: Bearer ` header\nThe Durable Object validates the API key from the query parameter or Bearer header.\n", "x-mint": { - "content": "## Establish a session\n1. Connect to `/connect` with `airport` and your API key.\n2. Wait for `INITIAL_STATE` (this confirms your role and current airport state).\n3. Keep the connection alive by sending `HEARTBEAT` regularly.\n\nThe server sends `HEARTBEAT` every 60 seconds and closes idle sessions after ~70 seconds without inbound client messages.\n\n## Packet envelope\nEvery packet uses this top-level shape:\n\n```json\n{\n \"type\": \"STATE_UPDATE\",\n \"airport\": \"YSSY\",\n \"data\": {},\n \"timestamp\": 1739400000000\n}\n```\n\n- `type` is required.\n- `airport` is optional on most client packets (server uses your connected airport if omitted).\n- `timestamp` is optional on client packets and server-populated on outbound packets.\n\n## Packet permissions by role\n| Packet type | Who can send | What happens |\n| --- | --- | --- |\n| `HEARTBEAT` | controller, pilot, observer | Server replies with `HEARTBEAT_ACK` |\n| `GET_STATE` | controller, pilot, observer | Server replies with `STATE_SNAPSHOT` |\n| `STATE_UPDATE` | controller only | Broadcast to same-airport clients (except sender) |\n| `MULTI_STATE_UPDATE` | controller only | Broadcast to same-airport clients (except sender) |\n| `SHARED_STATE_UPDATE` | controller only | Broadcast to same-airport clients (including sender) |\n| `STOPBAR_CROSSING` | pilot only | Broadcast to same-airport controllers |\n| `CLOSE` | controller, pilot, observer | Server closes the session gracefully |\n\n## Client packets you send\n### HEARTBEAT\n```json\n{ \"type\": \"HEARTBEAT\" }\n```\n\n### GET_STATE\n```json\n{ \"type\": \"GET_STATE\" }\n```\n\n### STATE_UPDATE (controller only)\n```json\n{\n \"type\": \"STATE_UPDATE\",\n \"data\": {\n \"objectId\": \"BARS_7K2QH\",\n \"state\": false\n }\n}\n```\n\n### MULTI_STATE_UPDATE (controller only)\n```json\n{\n \"type\": \"MULTI_STATE_UPDATE\",\n \"data\": {\n \"updates\": [\n { \"objectId\": \"BARS_7K2QH\", \"state\": false },\n { \"objectId\": \"BARS_8R1LP\", \"state\": true }\n ]\n }\n}\n```\n\n### SHARED_STATE_UPDATE (controller only)\n```json\n{\n \"type\": \"SHARED_STATE_UPDATE\",\n \"data\": {\n \"sharedStatePatch\": {\n \"profile\": \"default\",\n \"nodes\": {\n \"TWY_A1\": true,\n \"TWY_B2\": false,\n \"RWY_09L_HOLD\": true\n },\n \"blocks\": {\n \"BLOCK_A\": \"clear\",\n \"BLOCK_B\": \"relax\",\n \"BLOCK_C\": { \"route\": [\"TWY_A1\", \"TWY_B2\"] }\n }\n }\n }\n}\n```\n\n### STOPBAR_CROSSING (pilot only)\n```json\n{\n \"type\": \"STOPBAR_CROSSING\",\n \"data\": {\n \"objectId\": \"BARS_7K2QH\"\n }\n}\n```\n\n### CLOSE\n```json\n{ \"type\": \"CLOSE\" }\n```\n\n## Server packets you should handle\n- `INITIAL_STATE` immediately after connect\n- `HEARTBEAT` every 60 seconds\n- `HEARTBEAT_ACK` in response to client `HEARTBEAT`\n- `STATE_SNAPSHOT` in response to `GET_STATE`\n- `STATE_UPDATE` when a controller changes one object state\n- `MULTI_STATE_UPDATE` when a controller sends a batch state change\n- `CONTROLLER_CONNECT` / `CONTROLLER_DISCONNECT` for controller presence changes\n- `SHARED_STATE_UPDATE` when shared state changes\n- `STOPBAR_CROSSING` (controllers only) when a pilot crosses a stopbar\n- `ERROR` when validation/processing fails\n\n## Validation and limits (avoid malformed packets)\n- `objectId` must match `^[a-zA-Z0-9_-]+$`.\n- `STATE_UPDATE` must include `data.objectId` and one of `data.state` or `data.patch`.\n- `MULTI_STATE_UPDATE` must include `data.updates` (maximum `200` updates).\n- `SHARED_STATE_UPDATE` must include `data.sharedStatePatch` object (maximum `10,240` serialized characters).\n- Maximum packet size is `50,000` characters.\n- Unknown packet `type` values are rejected.\n" + "content": "## Establish a session\n1. Connect to `/connect` with `airport` and your API key.\n2. Wait for `INITIAL_STATE` (this confirms your role and current airport state).\n3. Keep the connection alive by sending `HEARTBEAT` regularly.\n\nThe server sends `HEARTBEAT` every 60 seconds and closes idle sessions after ~70 seconds without inbound client messages.\n\n## Packet envelope\nEvery packet uses this top-level shape:\n\n```json\n{\n \"type\": \"STATE_UPDATE\",\n \"airport\": \"YSSY\",\n \"data\": {},\n \"timestamp\": 1739400000000\n}\n```\n\n- `type` is required.\n- `airport` is optional on most client packets (server uses your connected airport if omitted).\n- `timestamp` is optional on client packets and server-populated on outbound packets.\n\n## Packet permissions by role\n| Packet type | Who can send | What happens |\n| --- | --- | --- |\n| `HEARTBEAT` | controller, pilot, observer | Server replies with `HEARTBEAT_ACK` |\n| `GET_STATE` | controller, pilot, observer | Server replies with `STATE_SNAPSHOT` |\n| `GET_ONLINE_PILOTS` | controller, pilot, observer | Server replies with `ONLINE_PILOTS` for the connected airport |\n| `STATE_UPDATE` | controller only | Broadcast to same-airport clients (except sender) |\n| `MULTI_STATE_UPDATE` | controller only | Broadcast to same-airport clients (except sender) |\n| `SHARED_STATE_UPDATE` | controller only | Broadcast to same-airport clients (including sender) |\n| `STOPBAR_CROSSING` | pilot only | Broadcast to same-airport controllers |\n| `CLOSE` | controller, pilot, observer | Server closes the session gracefully |\n\n## Client packets you send\n### HEARTBEAT\n```json\n{ \"type\": \"HEARTBEAT\" }\n```\n\n### GET_STATE\n```json\n{ \"type\": \"GET_STATE\" }\n```\n\n### GET_ONLINE_PILOTS\n```json\n{ \"type\": \"GET_ONLINE_PILOTS\" }\n```\n\n### STATE_UPDATE (controller only)\n```json\n{\n \"type\": \"STATE_UPDATE\",\n \"data\": {\n \"objectId\": \"BARS_7K2QH\",\n \"state\": false\n }\n}\n```\n\n### MULTI_STATE_UPDATE (controller only)\n```json\n{\n \"type\": \"MULTI_STATE_UPDATE\",\n \"data\": {\n \"updates\": [\n { \"objectId\": \"BARS_7K2QH\", \"state\": false },\n { \"objectId\": \"BARS_8R1LP\", \"state\": true }\n ]\n }\n}\n```\n\n### SHARED_STATE_UPDATE (controller only)\n```json\n{\n \"type\": \"SHARED_STATE_UPDATE\",\n \"data\": {\n \"sharedStatePatch\": {\n \"profile\": \"default\",\n \"nodes\": {\n \"TWY_A1\": true,\n \"TWY_B2\": false,\n \"RWY_09L_HOLD\": true\n },\n \"blocks\": {\n \"BLOCK_A\": \"clear\",\n \"BLOCK_B\": \"relax\",\n \"BLOCK_C\": { \"route\": [\"TWY_A1\", \"TWY_B2\"] }\n }\n }\n }\n}\n```\n\n### STOPBAR_CROSSING (pilot only)\n```json\n{\n \"type\": \"STOPBAR_CROSSING\",\n \"data\": {\n \"objectId\": \"BARS_7K2QH\"\n }\n}\n```\n\n### CLOSE\n```json\n{ \"type\": \"CLOSE\" }\n```\n\n## Server packets you should handle\n- `INITIAL_STATE` immediately after connect\n- `HEARTBEAT` every 60 seconds\n- `HEARTBEAT_ACK` in response to client `HEARTBEAT`\n- `STATE_SNAPSHOT` in response to `GET_STATE`\n- `ONLINE_PILOTS` in response to `GET_ONLINE_PILOTS`:\n ```json\n {\n \"type\": \"ONLINE_PILOTS\",\n \"airport\": \"YSSY\",\n \"data\": {\n \"pilots\": [\n { \"cid\": \"1234567\", \"callsign\": \"QFA12\" },\n { \"cid\": \"7654321\", \"callsign\": \"VOZ456\" }\n ],\n \"requestedAt\": 1739400000000\n },\n \"timestamp\": 1739400000123\n }\n ```\n- `STATE_UPDATE` when a controller changes one object state\n- `MULTI_STATE_UPDATE` when a controller sends a batch state change\n- `CONTROLLER_CONNECT` / `CONTROLLER_DISCONNECT` for controller presence changes\n- `SHARED_STATE_UPDATE` when shared state changes\n- `STOPBAR_CROSSING` (controllers only) when a pilot crosses a stopbar\n- `ERROR` when validation/processing fails\n\n## Validation and limits (avoid malformed packets)\n- `objectId` must match `^[a-zA-Z0-9_-]+$`.\n- `STATE_UPDATE` must include `data.objectId` and one of `data.state` or `data.patch`.\n- `MULTI_STATE_UPDATE` must include `data.updates` (maximum `200` updates).\n- `SHARED_STATE_UPDATE` must include `data.sharedStatePatch` object (maximum `10,240` serialized characters).\n- Maximum packet size is `50,000` characters.\n- Unknown packet `type` values are rejected.\n" }, "parameters": [ { @@ -1929,9 +1929,9 @@ }, "/supports/generate": { "post": { - "summary": "Generate Light Supports and BARS XML", + "summary": "Generate simulator removals and BARS XML", "tags": ["Generation"], - "description": "Upload raw XML and generate both light supports XML and processed BARS XML.", + "description": "Upload a draft and generate processed BARS XML plus MSFS light-support XML or compact X-Plane removal JSON.", "parameters": [ { "in": "query", @@ -1957,6 +1957,10 @@ }, "icao": { "type": "string" + }, + "simulator": { + "type": "string", + "enum": ["msfs2020", "msfs2024", "xplane"] } } } @@ -2117,6 +2121,58 @@ } } }, + "/staff/users/{id}/fast-track": { + "put": { + "x-hidden": true, + "summary": "Update contributor fast-track access", + "tags": ["Staff"], + "security": [ + { + "VatsimToken": [] + } + ], + "parameters": [ + { + "in": "path", + "name": "id", + "required": true, + "schema": { + "type": "integer" + } + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "required": ["enabled"], + "properties": { + "enabled": { + "type": "boolean" + } + } + } + } + } + }, + "responses": { + "200": { + "description": "Fast-track access updated" + }, + "400": { + "description": "Invalid request" + }, + "403": { + "description": "Forbidden" + }, + "404": { + "description": "User not found" + } + } + } + }, "/staff/users/{id}": { "delete": { "x-hidden": true, @@ -2290,6 +2346,15 @@ }, "description": "When true, returns only contribution id, airport ICAO, package name, and simulator." }, + { + "in": "query", + "name": "projection", + "schema": { + "type": "string", + "enum": ["metadata"] + }, + "description": "Bounded contribution metadata including artifact keys, without submitted XML or generation proof." + }, { "in": "query", "name": "summary", @@ -2328,7 +2393,7 @@ "application/json": { "schema": { "type": "object", - "required": ["airportIcao", "packageName", "submittedXml", "simulator"], + "required": ["airportIcao", "packageName", "submittedXml", "simulator", "generationToken", "generationHash"], "properties": { "airportIcao": { "type": "string", @@ -2348,8 +2413,20 @@ }, "simulator": { "type": "string", - "enum": ["msfs2020", "msfs2024"], + "enum": ["msfs2020", "msfs2024", "xplane"], "description": "Target flight simulator for this contribution" + }, + "generationToken": { + "type": "string", + "description": "Token returned by testing this exact normalized draft through /supports/generate." + }, + "generationHash": { + "type": "string", + "description": "Normalized draft hash returned by /supports/generate." + }, + "fastTrackRequested": { + "type": "boolean", + "description": "Opt in to fast-track publishing when the authenticated account is eligible. Defaults to false." } } } @@ -2583,7 +2660,7 @@ "required": false, "schema": { "type": "string", - "enum": ["msfs2020", "msfs2024"] + "enum": ["msfs2020", "msfs2024", "xplane"] }, "description": "Filter by simulator (optional - returns latest across all simulators if not specified)" } 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/migrations/0003_add_contributor_fast_track.sql b/migrations/0003_add_contributor_fast_track.sql new file mode 100644 index 0000000..c7b9305 --- /dev/null +++ b/migrations/0003_add_contributor_fast_track.sql @@ -0,0 +1,26 @@ +CREATE TABLE IF NOT EXISTS contributor_fast_track ( + user_id INTEGER PRIMARY KEY, + enabled BOOLEAN NOT NULL DEFAULT TRUE, + granted_by INTEGER, + granted_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + expires_at DATETIME NOT NULL, + updated_by INTEGER, + updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, + FOREIGN KEY (granted_by) REFERENCES users (id) ON DELETE SET NULL, + FOREIGN KEY (updated_by) REFERENCES users (id) ON DELETE SET NULL +); + +ALTER TABLE contributions ADD COLUMN decision_source TEXT CHECK ( + decision_source IN ( + 'staff', + 'fast_track' + ) +); +ALTER TABLE contributions ADD COLUMN decided_by TEXT; + +CREATE INDEX IF NOT EXISTS idx_contributor_fast_track_active +ON contributor_fast_track (user_id, enabled, expires_at); + +CREATE INDEX IF NOT EXISTS idx_contributions_fast_track_limit +ON contributions (user_id, decision_source, decision_date DESC); diff --git a/outputs/019fdfdf-5e8b-7f90-aedc-aa3e4ba1bd60/BARS_v2_Product_Manager_Permission_Decision.xlsx b/outputs/019fdfdf-5e8b-7f90-aedc-aa3e4ba1bd60/BARS_v2_Product_Manager_Permission_Decision.xlsx new file mode 100644 index 0000000..0011111 Binary files /dev/null and b/outputs/019fdfdf-5e8b-7f90-aedc-aa3e4ba1bd60/BARS_v2_Product_Manager_Permission_Decision.xlsx differ diff --git a/package.json b/package.json index 29e380a..06860e2 100644 --- a/package.json +++ b/package.json @@ -2,38 +2,34 @@ "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" + "lint": "oxlint && 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", + "@oxlint/plugins": "1.78.0", + "@types/node": "24.10.9", + "@types/swagger-jsdoc": "6.0.4", + "eslint": "9.39.2", + "globals": "16.5.0", + "jiti": "2.6.1", + "oxlint": "1.78.0", + "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", diff --git a/schema.sql b/schema.sql index 7cbaad9..ac7c07c 100644 --- a/schema.sql +++ b/schema.sql @@ -29,6 +29,19 @@ CREATE TABLE IF NOT EXISTS staff ( ) ); +CREATE TABLE IF NOT EXISTS contributor_fast_track ( + user_id INTEGER PRIMARY KEY, + enabled BOOLEAN NOT NULL DEFAULT TRUE, + granted_by INTEGER, + granted_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + expires_at DATETIME NOT NULL, + updated_by INTEGER, + updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, + FOREIGN KEY (granted_by) REFERENCES users (id) ON DELETE SET NULL, + FOREIGN KEY (updated_by) REFERENCES users (id) ON DELETE SET NULL +); + CREATE TABLE IF NOT EXISTS points ( id TEXT PRIMARY KEY, airport_id TEXT NOT NULL, @@ -180,7 +193,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 +207,20 @@ 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, + decision_source TEXT CHECK ( + decision_source IN ( + 'staff', + 'fast_track' + ) + ), + decided_by TEXT ); CREATE TABLE IF NOT EXISTS notams ( @@ -260,6 +287,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')) ); @@ -332,6 +361,12 @@ CREATE INDEX IF NOT EXISTS idx_staff_created_at ON staff ( created_at DESC ); +CREATE INDEX IF NOT EXISTS idx_contributor_fast_track_active ON contributor_fast_track ( + user_id, + enabled, + expires_at +); + CREATE INDEX IF NOT EXISTS idx_active_objects_name ON active_objects ( name ); @@ -382,6 +417,19 @@ 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_contributions_fast_track_limit ON contributions ( + user_id, + decision_source, + decision_date DESC +); + CREATE INDEX IF NOT EXISTS idx_points_timestamps ON points ( created_at, updated_at diff --git a/src/index.ts b/src/index.ts index 1c8924a..b824f66 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import type { Context } from 'hono'; +import type { Context, MiddlewareHandler } from 'hono'; import { Hono } from 'hono'; import { cors } from 'hono/cors'; import { DurableObject } from 'cloudflare:workers'; @@ -8,16 +8,19 @@ import { CacheKeys, withCache } from './services/cache'; import { DatabaseContextFactory } from './services/database-context'; import { HttpError } from './services/errors'; import { cancelResponseBody, getClientIp } from './services/http'; -import { getLightsByObject, type RadarLight } from './services/lights-cache'; +import { getLightsByObject } from './services/lights-cache'; import { rateLimit } from './services/rate-limit'; import { InstallerProduct } from './services/releases'; import { StaffRole } from './services/roles'; import { ServicePool } from './services/service-pool'; import { UserService } from './services/users'; import { VatsimService } from './services/vatsim'; -import { deriveContributionGenerationTokenFromMsfsXml } from './services/contributions'; +import { deriveContributionGenerationHash, deriveContributionGenerationToken, type Simulator } from './services/contributions'; import { parseAirportCreateInput, parseAirportUpdateInput } from './services/airport'; import { MAX_CONTRIBUTION_XML_BYTES, sanitizeContributionXml } from './services/xml-sanitizer'; +import { generateXPlaneRemovalsJson } from './services/xplane-removals'; +import { DIVISION_DATA_ACTOR_ID, isDivisionDataAutomationRequest } from './services/division-data-auth'; +import { normalizeIsoDateTime, parseDecimalNumber } from './services/parsing'; import { AirportObject, PointChangeset, PointData, UserRecord, VatsimUser } from './types'; export { RateLimiter } from './services/rate-limit'; const POINT_ID_REGEX = /^[A-Z0-9-_]+$/; @@ -33,7 +36,10 @@ const INSTALLER_PRODUCTS: readonly InstallerProduct[] = [ 'Installer', 'SimConnect.NET', ]; +const parseInstallerProduct = (value: string | undefined): InstallerProduct | undefined => + INSTALLER_PRODUCTS.find((product) => product === value); const CACHE_NAMESPACES = ['airports', 'points', 'divisions', 'auth', 'state', 'health', 'installer', 'github', 'faq'] as const; +const CONTRIBUTION_STATUSES = ['pending', 'approved', 'rejected', 'outdated'] as const; const STATE_ID_INFO = [ { type: 'Uni-Directional', code: 0, direction2: 'OFF', direction1: 'OFF' }, { type: 'Uni-Directional', code: 1, direction2: 'OFF', direction1: 'Red' }, @@ -53,8 +59,36 @@ const STATE_ID_INFO = [ { type: 'Bi-Directional', code: 27, direction2: 'Green', direction1: 'Orange' }, ] as const; -const getHighResTime = - typeof performance !== 'undefined' && typeof performance.now === 'function' ? () => performance.now() : () => Date.now(); +type JsonValue = string | number | boolean | null | JsonObject | JsonValue[]; +interface JsonObject { + [key: string]: JsonValue; +} + +const parseJsonObject = (value: JsonValue | undefined): JsonObject => { + if (value === null || value !== Object(value) || Array.isArray(value)) return {}; + // SAFETY: JSON objects contain only JSON values, and the object/array/null cases were distinguished above. + return value as JsonObject; +}; + +const readString = (value: JsonValue | undefined): string | undefined => { + if (String(value) !== value) return undefined; + // SAFETY: strict equality with String(value) establishes that value is a primitive string. + return value as string; +}; + +const readNumber = (value: JsonValue | undefined): number | undefined => { + if (Number(value) !== value) return undefined; + // SAFETY: strict equality with Number(value) establishes that value is a primitive number. + return value as number; +}; + +const readBoolean = (value: JsonValue | undefined): boolean | undefined => { + if (Boolean(value) !== value) return undefined; + // SAFETY: strict equality with Boolean(value) establishes that value is a primitive boolean. + return value as boolean; +}; + +const getHighResTime = () => performance.now(); const formatServerTiming = (durationMs: number): string => { const normalized = durationMs < 0 ? 0 : durationMs; @@ -195,28 +229,41 @@ interface ApproveAirportPayload { approved: boolean; } -interface UpdateAirportContributionsPayload { - contributionsEnabled: boolean; -} - interface ContributionSubmissionPayload { airportIcao: string; packageName: string; submittedXml: string; notes?: string; - simulator: 'msfs2020' | 'msfs2024'; + simulator: Simulator; + generationToken: string; + generationHash: string; + fastTrackRequested?: boolean; } -interface ContributionDecisionPayload { - approved: boolean; - rejectionReason?: string; - newPackageName?: string; +interface AuthNetworkStatusPayload { + cid: string; + offline: boolean; + vatsim_status?: VatsimConnectionStatus | null; + vatsim_status_raw?: string; +} + +interface UploadedBarsPackage { + type: string; + key: string; + size: number; + sha256: string; + etag: string; + url: string; + version?: string; + protocol?: string; } interface ContributionGenerationCacheEntry { icao: string; + simulator: Simulator; + generationHash: string; expiresAt: string; - supportsXml: string; + removalArtifact: string; barsXml: string; } @@ -224,6 +271,17 @@ const CONTRIBUTION_GENERATION_CACHE_NAMESPACE = 'generation'; const CONTRIBUTION_GENERATION_CACHE_TTL_SECONDS = 86400; const MIN_CONTRIBUTION_GENERATION_CACHE_TTL_SECONDS = 1; +const summarizeContributionStatuses = (items: Array<{ status: string }>, total: number) => + items.reduce( + (summary, item) => { + if (item.status === 'approved' || item.status === 'pending' || item.status === 'rejected' || item.status === 'outdated') { + summary[item.status] += 1; + } + return summary; + }, + { total, approved: 0, pending: 0, rejected: 0, outdated: 0 }, + ); + type VatsimConnectionStatus = { callsign: string; facility_type: 'pilot' | 'atc' | string; @@ -251,8 +309,29 @@ export class BARS extends DurableObject { async getState(airport: string, forceOffline = false) { return this.connection.getState(airport, forceOffline); } + + async invalidatePointStateTemplate(airport: string, removedPointIds: string[] = []) { + await this.connection.invalidatePointStateTemplate(airport, removedPointIds); + } } +const invalidateAirportPointStateCaches = async (env: Env, airport: string, removedPointIds: string[] = []): Promise => { + const normalizedAirport = airport.toUpperCase(); + const results = await Promise.allSettled([ + ServicePool.getCache(env).delete(`offline-template-${normalizedAirport}`, OFFLINE_TEMPLATE_NAMESPACE), + env.BARS.getByName(normalizedAirport).invalidatePointStateTemplate(normalizedAirport, removedPointIds), + ]); + + for (const [index, result] of results.entries()) { + if (result.status === 'fulfilled') continue; + console.warn('Point state cache invalidation failed after a successful point mutation', { + airport: normalizedAirport, + cache: index === 0 ? 'worker-offline-template' : 'durable-object-point-template', + error: result.reason instanceof Error ? result.reason.message : String(result.reason), + }); + } +}; + const app = new Hono<{ Bindings: Env; Variables: { @@ -266,7 +345,13 @@ const app = new Hono<{ }; }>(); -const parseVatsimStatusCsv = (csv: string): { offline: boolean; status: VatsimConnectionStatus | null; raw: string } => { +interface ParsedVatsimStatusCsv { + offline: boolean; + status: VatsimConnectionStatus | null; + raw: string; +} + +const parseVatsimStatusCsv = (csv: string): ParsedVatsimStatusCsv => { const trimmed = (csv || '').trim(); if (!trimmed) { return { offline: true, status: null, raw: '' }; @@ -302,7 +387,8 @@ const parseVatsimStatusCsv = (csv: string): { offline: boolean; status: VatsimCo const status: VatsimConnectionStatus = { callsign, - facility_type: (facilityType as VatsimConnectionStatus['facility_type']) || 'unknown', + // SAFETY: VATSIM supplies facility_type as the third CSV field; the contract permits its string codes. + facility_type: facilityType as VatsimConnectionStatus['facility_type'], frequency: numberOrNull(freq), visual_range: numberOrNull(visRange), latitude: numberOrNull(lat), @@ -337,7 +423,7 @@ app.onError((err, c) => { } const start = c?.get('serverTimingStart'); - if (typeof start === 'number') { + if (start !== undefined) { applyServerTimingHeader(response.headers, getHighResTime() - start); } @@ -365,6 +451,7 @@ app.use('*', async (c, next) => { if (c.req.method === 'OPTIONS') return; if (path === '/favicon.ico') return; if (path.includes('/health')) return; + // SAFETY: ANALYTICS_IGNORE is an optional text binding supplied by the Worker environment. const ignoreRaw = (c.env as { ANALYTICS_IGNORE?: string }).ANALYTICS_IGNORE; if (ignoreRaw) { const ignores = ignoreRaw @@ -415,26 +502,25 @@ app.get('/favicon.ico', () => { }); }); -async function resolveUserFromVatsimOrApi( - c: Context<{ Bindings: Env; Variables: { vatsimUser?: VatsimUser; user?: UserRecord } }>, +export async function resolveUserFromCredentials( + vatsimToken: string | undefined, + authz: string, + vatsim: Pick, + auth: Pick, ): Promise<{ user: UserRecord | null; vatsimUser?: VatsimUser }> { - const vatsimToken = c.req.header('X-Vatsim-Token'); - const authz = c.req.header('Authorization') || ''; - const vatsim = ServicePool.getVatsim(c.env); - const auth = ServicePool.getAuth(c.env); - if (vatsimToken) { try { const vUser = await vatsim.getUser(vatsimToken); const user = await auth.getUserByVatsimId(vUser.id); return { user, vatsimUser: vUser }; } catch { - return { user: null }; + // A stale browser session must not prevent a valid API key from + // authenticating the same request. } } if (authz.toLowerCase().startsWith('bearer ')) { - const apiKey = authz.slice(7); + const apiKey = authz.slice(7).trim(); try { const user = await auth.getUserByApiKey(apiKey); if (user) { @@ -448,6 +534,17 @@ async function resolveUserFromVatsimOrApi( return { user: null }; } +async function resolveUserFromVatsimOrApi( + c: Context<{ Bindings: Env; Variables: { vatsimUser?: VatsimUser; user?: UserRecord } }>, +): Promise<{ user: UserRecord | null; vatsimUser?: VatsimUser }> { + return resolveUserFromCredentials( + c.req.header('X-Vatsim-Token'), + c.req.header('Authorization') || '', + ServicePool.getVatsim(c.env), + ServicePool.getAuth(c.env), + ); +} + /** * @openapi * /contact: @@ -482,16 +579,16 @@ async function resolveUserFromVatsimOrApi( app.post('/contact', async (c) => { const dbContext = DatabaseContextFactory.createRequestContext(c.env.DB, c.req.raw); try { - let body: unknown; + let body: JsonValue; try { body = await c.req.json(); } catch { return dbContext.jsonResponse({ error: 'Invalid JSON body' }, { status: 400 }); } - const b = (body ?? {}) as Record; - const email = typeof b.email === 'string' ? b.email.trim() : ''; - const topic = typeof b.topic === 'string' ? b.topic.trim() : ''; - const message = typeof b.message === 'string' ? b.message.trim() : ''; + const contactInput = parseJsonObject(body); + const email = readString(contactInput.email)?.trim() ?? ''; + const topic = readString(contactInput.topic)?.trim() ?? ''; + const message = readString(contactInput.message)?.trim() ?? ''; const ip = getClientIp(c.req.raw); const emailRegex = /^[^@\s]+@[^@\s]+\.[^@\s]+$/; @@ -611,13 +708,13 @@ app.patch('/contact/:id/status', async (c) => { const id = c.req.param('id'); const dbContext = DatabaseContextFactory.createRequestContext(c.env.DB, c.req.raw); try { - let body: unknown; + let body: JsonValue; try { body = await c.req.json(); } catch { return dbContext.jsonResponse({ error: 'Invalid JSON body' }, { status: 400 }); } - const status = (body as Record)?.status; + const status = parseJsonObject(body).status; if (status !== 'pending' && status !== 'handling' && status !== 'handled') { return dbContext.jsonResponse({ error: 'Invalid status' }, { status: 400 }); } @@ -732,6 +829,7 @@ app.delete('/contact/:id', async (c) => { * | --- | --- | --- | * | `HEARTBEAT` | controller, pilot, observer | Server replies with `HEARTBEAT_ACK` | * | `GET_STATE` | controller, pilot, observer | Server replies with `STATE_SNAPSHOT` | + * | `GET_ONLINE_PILOTS` | controller, pilot, observer | Server replies with `ONLINE_PILOTS` for the connected airport | * | `STATE_UPDATE` | controller only | Broadcast to same-airport clients (except sender) | * | `MULTI_STATE_UPDATE` | controller only | Broadcast to same-airport clients (except sender) | * | `SHARED_STATE_UPDATE` | controller only | Broadcast to same-airport clients (including sender) | @@ -749,6 +847,11 @@ app.delete('/contact/:id', async (c) => { * { "type": "GET_STATE" } * ``` * + * ### GET_ONLINE_PILOTS + * ```json + * { "type": "GET_ONLINE_PILOTS" } + * ``` + * * ### STATE_UPDATE (controller only) * ```json * { @@ -815,6 +918,21 @@ app.delete('/contact/:id', async (c) => { * - `HEARTBEAT` every 60 seconds * - `HEARTBEAT_ACK` in response to client `HEARTBEAT` * - `STATE_SNAPSHOT` in response to `GET_STATE` + * - `ONLINE_PILOTS` in response to `GET_ONLINE_PILOTS`: + * ```json + * { + * "type": "ONLINE_PILOTS", + * "airport": "YSSY", + * "data": { + * "pilots": [ + * { "cid": "1234567", "callsign": "QFA12" }, + * { "cid": "7654321", "callsign": "VOZ456" } + * ], + * "requestedAt": 1739400000000 + * }, + * "timestamp": 1739400000123 + * } + * ``` * - `STATE_UPDATE` when a controller changes one object state * - `MULTI_STATE_UPDATE` when a controller sends a batch state change * - `CONTROLLER_CONNECT` / `CONTROLLER_DISCONNECT` for controller presence changes @@ -893,7 +1011,6 @@ app.get('/connect', rateLimit({ maxRequests: 30 }), async (c) => { return c.env.BARS.getByName(airportId).fetch(c.req.raw); }); -// State endpoint /** * @openapi * /state: @@ -962,7 +1079,7 @@ app.get('/state', withCache(CacheKeys.fromUrl, 1, 'state'), async (c) => { const airportIcaoRaw = metadata?.airport ?? obj.name.split('/')[0] ?? ''; const airportIcao = airportIcaoRaw.toUpperCase(); if (!ICAO_REGEX.test(airportIcao)) { - return null as null; + return null; } const controllerCountFromMetadata = metadata?.controllers ?? 0; @@ -971,7 +1088,7 @@ app.get('/state', withCache(CacheKeys.fromUrl, 1, 'state'), async (c) => { if (controllerCountFromMetadata === 0 && pilotCountFromMetadata === 0 && observerCountFromMetadata === 0) { if (isVatsimRadar) { - return null as null; + return null; } const offlineSnapshot = await getOfflineStateSnapshot(c.env, airportIcao); if (offlineSnapshot) { @@ -999,7 +1116,17 @@ app.get('/state', withCache(CacheKeys.fromUrl, 1, 'state'), async (c) => { objects: DOObject[]; offline?: boolean; }; - const state = (await durableObj.getState(airportIcao)) as DOState; + const stateUrl = new URL(c.req.url); + stateUrl.searchParams.set('airport', airportIcao); + const stateResponse = await durableObj.fetch( + new Request(stateUrl, { headers: { 'X-Request-Type': 'get_state' } }), + ); + if (!stateResponse.ok) { + await cancelResponseBody(stateResponse); + throw new Error(`Durable Object state request failed with status ${stateResponse.status}`); + } + // SAFETY: BARS.fetch handles get_state by serializing Connection.getState, which owns the DOState contract. + const state = JSON.parse(await stateResponse.text()) as DOState; // Determine online/offline status consistently const controllerCount = Array.isArray(state.controllers) ? state.controllers.length : 0; @@ -1007,7 +1134,7 @@ app.get('/state', withCache(CacheKeys.fromUrl, 1, 'state'), async (c) => { if (isVatsimRadar) { // For VATSIM Radar, mirror 'all' behavior by excluding offline airports entirely - if (isOffline) return null as null; + if (isOffline) return null; const lightsByObject = await getLightsByObject(c.env, airportIcao); const allowedIds = new Set(Object.keys(lightsByObject)); @@ -1019,7 +1146,7 @@ app.get('/state', withCache(CacheKeys.fromUrl, 1, 'state'), async (c) => { id: o.id, state: o.state, timestamp: o.timestamp, - lights: (lightsByObject as Record)[o.id] || [], + lights: lightsByObject[o.id] || [], })) : []; @@ -1049,7 +1176,7 @@ app.get('/state', withCache(CacheKeys.fromUrl, 1, 'state'), async (c) => { }; } catch { // If a DO fails to respond, skip this entry - return null as null; + return null; } }), ); @@ -1085,7 +1212,17 @@ app.get('/state', withCache(CacheKeys.fromUrl, 1, 'state'), async (c) => { ); } - return c.json(await c.env.BARS.getByName(airport).getState(airport, offlineRequested)); + const stateUrl = new URL(c.req.url); + stateUrl.searchParams.set('airport', airport); + stateUrl.searchParams.set('offline', String(offlineRequested)); + const stateResponse = await c.env.BARS.getByName(airport).fetch( + new Request(stateUrl, { headers: { 'X-Request-Type': 'get_state' } }), + ); + const serializedState = await stateResponse.text(); + return new Response(serializedState, { + status: stateResponse.status, + headers: { 'Content-Type': 'application/json; charset=UTF-8' }, + }); }); // VATSIM auth callback @@ -1118,7 +1255,7 @@ app.get('/auth/vatsim/callback', async (c) => { const auth = ServicePool.getAuth(c.env); try { const { vatsimToken } = await auth.handleCallback(code, c.executionCtx); - return Response.redirect(`https://preview.stopbars.com/auth/callback?token=${vatsimToken}`, 302); + return Response.redirect(`https://stopbars.com/auth/callback?token=${vatsimToken}`, 302); } catch { return Response.redirect('https://v2.stopbars.com/auth?error=oauth_failed', 302); } @@ -1150,7 +1287,6 @@ app.get('/auth/account', async (c) => { return c.text('Unauthorized', 401); } - // Create database context for this request const dbContext = DatabaseContextFactory.createRequestContext(c.env.DB, c.req.raw); try { @@ -1170,6 +1306,14 @@ app.get('/auth/account', async (c) => { if (!user) { return dbContext.textResponse('User not found', { status: 404 }); } + const fastTrack = await dbContext.db.executeLatest<{ expires_at: string }>( + `SELECT ft.expires_at + FROM contributor_fast_track ft + WHERE ft.user_id = ? AND ft.enabled = 1 AND ft.expires_at > CURRENT_TIMESTAMP + LIMIT 1`, + [user.id], + ); + const fastTrackExpiry = fastTrack.results[0]?.expires_at ?? null; // Fire-and-forget background maintenance so it doesn't block response try { @@ -1214,6 +1358,10 @@ app.get('/auth/account', async (c) => { : (vatsimUser.subdivision ?? null), created_at: user.created_at, last_login: user.last_login, + fast_track: { + enabled: fastTrackExpiry !== null, + expires_at: fastTrackExpiry, + }, }); } finally { dbContext.close(); @@ -1262,7 +1410,7 @@ app.get('/auth/network-status', async (c) => { } const { offline, status, raw } = parseVatsimStatusCsv(vatsimStatusCsv ?? ''); - const payload: Record = { cid: user.vatsim_id, offline }; + const payload: AuthNetworkStatusPayload = { cid: user.vatsim_id, offline }; if (!offline) { payload.vatsim_status = status; payload.vatsim_status_raw = raw; @@ -1299,13 +1447,13 @@ app.put('/auth/display-mode', rateLimit({ maxRequests: 5 }), async (c) => { const vatsimToken = c.req.header('X-Vatsim-Token'); if (!vatsimToken) return c.text('Unauthorized', 401); - let body: unknown; + let body: JsonValue; try { body = await c.req.json(); } catch { return c.json({ error: 'Invalid JSON body' }, 400); } - const rawMode = (body as Record)?.mode as unknown; + const rawMode = parseJsonObject(body).mode; const mode = Number(rawMode); if (!Number.isInteger(mode) || ![0, 1, 2].includes(mode)) { return c.json({ error: 'Invalid mode', message: 'mode must be integer 0,1,2' }, 400); @@ -1355,7 +1503,6 @@ app.post('/auth/regenerate-api-key', async (c) => { return c.text('Unauthorized', 401); } - // Create database context for this request const dbContext = DatabaseContextFactory.createRequestContext(c.env.DB, c.req.raw); try { @@ -1571,25 +1718,29 @@ app.get('/bans', async (c) => { app.post('/bans', async (c) => { const token = c.req.header('X-Vatsim-Token'); if (!token) return c.text('Unauthorized', 401); - let body: unknown; + let body: JsonValue; try { body = await c.req.json(); } catch { return c.json({ error: 'Invalid JSON body' }, 400); } - const b = (body ?? {}) as Record; - const vatsimId = typeof b.vatsimId === 'string' || typeof b.vatsimId === 'number' ? String(b.vatsimId) : ''; - const reason = typeof b.reason === 'string' ? b.reason : null; - const expiresAtRaw = b.expiresAt; - const expiresAt = - expiresAtRaw === null || expiresAtRaw === undefined - ? null - : typeof expiresAtRaw === 'string' - ? expiresAtRaw - : typeof expiresAtRaw === 'number' - ? new Date(expiresAtRaw).toISOString() - : null; + const banInput = parseJsonObject(body); + const vatsimIdValue = readString(banInput.vatsimId) ?? readNumber(banInput.vatsimId); + const vatsimId = vatsimIdValue === undefined ? '' : String(vatsimIdValue); + const reason = readString(banInput.reason) ?? null; + const expiresAtRaw = banInput.expiresAt; if (!vatsimId) return c.json({ error: 'vatsimId required' }, 400); + let expiresAt: string | null = null; + if (expiresAtRaw !== null && expiresAtRaw !== undefined) { + const expiresAtValue = readString(expiresAtRaw) ?? readNumber(expiresAtRaw); + if (expiresAtValue === undefined) { + return c.json({ error: 'expiresAt must be an ISO 8601 date-time with a timezone or null' }, 400); + } + expiresAt = normalizeIsoDateTime(expiresAtValue); + if (expiresAt === null) { + return c.json({ error: 'expiresAt must be an ISO 8601 date-time with a timezone or null' }, 400); + } + } const vatsim = ServicePool.getVatsim(c.env); const auth = ServicePool.getAuth(c.env); const roles = ServicePool.getRoles(c.env); @@ -1692,7 +1843,6 @@ app.get( if (icao) { // Helper to sanitize ICAO: remove non-alphanumerics and uppercase const sanitizeIcao = (code: string) => code.toUpperCase().replace(/[^A-Z0-9]/g, ''); - // Handle batch requests if (icao.includes(',')) { const icaos = icao .split(',') @@ -1703,7 +1853,6 @@ app.get( } data = await airports.getAirports(icaos); } else { - // Single airport request const cleanIcao = sanitizeIcao(icao); if (!ICAO_REGEX.test(cleanIcao)) { return c.text('Invalid ICAO format', 400); @@ -1728,11 +1877,7 @@ app.get( const airportMutationApp = new Hono<{ Bindings: Env }>(); -airportMutationApp.use('*', async (c, next) => { - if (c.req.method !== 'POST' && c.req.method !== 'PATCH') { - await next(); - return; - } +const requireLeadDeveloperForAirportMutation: MiddlewareHandler<{ Bindings: Env }> = async (c, next) => { const vatsimToken = c.req.header('X-Vatsim-Token'); if (!vatsimToken) return c.text('Unauthorized', 401); @@ -1747,7 +1892,7 @@ airportMutationApp.use('*', async (c, next) => { if (!staff) return c.text('Unauthorized', 401); if (staff.role !== StaffRole.LEAD_DEVELOPER) return c.text('Forbidden', 403); await next(); -}); +}; /** * @openapi @@ -1791,8 +1936,8 @@ airportMutationApp.use('*', async (c, next) => { * 403: { description: Lead developer role required } * 409: { description: Airport already exists } */ -airportMutationApp.post('/', async (c) => { - let body: unknown; +airportMutationApp.post('/', requireLeadDeveloperForAirportMutation, async (c) => { + let body: JsonValue; try { body = await c.req.json(); } catch { @@ -1832,10 +1977,10 @@ airportMutationApp.post('/', async (c) => { * 403: { description: Lead developer role required } * 404: { description: Airport not found } */ -airportMutationApp.patch('/:icao', async (c) => { +airportMutationApp.patch('/:icao', requireLeadDeveloperForAirportMutation, async (c) => { const icao = c.req.param('icao').trim().toUpperCase(); if (!ICAO_REGEX.test(icao)) return c.json({ error: 'Invalid airport ICAO format' }, 400); - let body: unknown; + let body: JsonValue; try { body = await c.req.json(); } catch { @@ -1917,12 +2062,12 @@ app.get( (req) => { // Bucket cache key by ~5NM (~9.26km). 1 degree lat ~111km => bucket size deg ≈ 9.26/111 ≈ 0.083 const url = new URL(req.url); - const lat = parseFloat(url.searchParams.get('lat') || '0'); - const lon = parseFloat(url.searchParams.get('lon') || '0'); + const lat = parseDecimalNumber(url.searchParams.get('lat') || '0'); + const lon = parseDecimalNumber(url.searchParams.get('lon') || '0'); const bucketDeg = 0.083; // ~5NM const bucketLat = Math.round(lat / bucketDeg); const bucketLon = Math.round(lon / bucketDeg); - return `/airports/nearest/${bucketLat}_${bucketLon}`; + return `/airports/nearest/v2/${bucketLat}_${bucketLon}`; }, 600, 'airports', @@ -1934,9 +2079,9 @@ app.get( if (!latStr || !lonStr) { return c.text('Missing lat/lon', 400); } - const lat = parseFloat(latStr); - const lon = parseFloat(lonStr); - if (Number.isNaN(lat) || Number.isNaN(lon) || lat < -90 || lat > 90 || lon < -180 || lon > 180) { + const lat = parseDecimalNumber(latStr); + const lon = parseDecimalNumber(lonStr); + if (!Number.isFinite(lat) || !Number.isFinite(lon) || lat < -90 || lat > 90 || lon < -180 || lon > 180) { return c.text('Invalid lat/lon', 400); } @@ -2028,6 +2173,7 @@ divisionsApp.post('/', async (c) => { const allowed = await roles.hasPermission(user.id, StaffRole.PRODUCT_MANAGER); if (!allowed) return c.text('Forbidden', 403); + // SAFETY: DivisionService owns validation and persistence of the documented create-division payload. const { name, headVatsimId } = (await c.req.json()) as CreateDivisionPayload; const division = await divisions.createDivision(name, headVatsimId); return c.json(division); @@ -2086,6 +2232,7 @@ divisionsApp.put('/:id', async (c) => { const existing = await divisions.getDivision(id); if (!existing) return c.text('Division not found', 404); + // SAFETY: name is checked for presence and normalized before it reaches DivisionService. const body = (await c.req.json()) as { name: string }; if (!body.name || !body.name.trim()) return c.text('Invalid name', 400); @@ -2291,6 +2438,7 @@ divisionsApp.post('/:id/members', async (c) => { return c.text('Forbidden', 403); } + // SAFETY: DivisionService.addMember enforces the member identity and role domain contract before persistence. const { vatsimId, role } = (await c.req.json()) as AddMemberPayload; const member = await divisions.addMember(divisionId, vatsimId, role); return c.json(member); @@ -2449,6 +2597,7 @@ divisionsApp.post('/:id/airports', async (c) => { const user = await auth.getUserByVatsimId(vatsimUser.id); const isPM = user ? await roles.hasPermission(user.id, StaffRole.PRODUCT_MANAGER) : false; + // SAFETY: the DivisionService request methods normalize and validate the ICAO before persistence. const { icao } = (await c.req.json()) as RequestAirportPayload; const airport = isPM ? await divisions.requestAirportAsStaff(divisionId, icao, vatsimUser.id) @@ -2576,6 +2725,7 @@ divisionsApp.post('/:id/airports/:airportId/approve', async (c) => { const allowed = await roles.hasPermission(user.id, StaffRole.PRODUCT_MANAGER); if (!allowed) return c.text('Forbidden', 403); + // SAFETY: DivisionService.approveAirport owns validation of the documented approval payload. const { approved } = (await c.req.json()) as ApproveAirportPayload; const airport = await divisions.approveAirport(airportId, vatsimUser.id, approved); // Airport GET responses include the approved division_airports ID. Invalidate @@ -2635,12 +2785,12 @@ divisionsApp.patch('/:id/airports/:airportId/contributions', async (c) => { } const vatsimUser = await vatsim.getUser(vatsimToken); - const payload = (await c.req.json()) as UpdateAirportContributionsPayload; - if (typeof payload.contributionsEnabled !== 'boolean') { + const contributionsEnabled = readBoolean(parseJsonObject(await c.req.json()).contributionsEnabled); + if (contributionsEnabled === undefined) { return c.json({ error: 'contributionsEnabled must be a boolean' }, 400); } - const airport = await divisions.updateAirportContributionsEnabled(divisionId, airportId, vatsimUser.id, payload.contributionsEnabled); + const airport = await divisions.updateAirportContributionsEnabled(divisionId, airportId, vatsimUser.id, contributionsEnabled); return c.json(airport); }); @@ -2722,8 +2872,10 @@ app.post('/airports/:icao/points', async (c) => { const points = ServicePool.getPoints(c.env); + // SAFETY: PointsService.createPoint validates every PointData field before inserting it. const pointData = (await c.req.json()) as PointData; const newPoint = await points.createPoint(airportId, user.vatsim_id, pointData); + await invalidateAirportPointStateCaches(c.env, newPoint.airportId); return c.json(newPoint, 201); }); @@ -2766,15 +2918,27 @@ app.post('/airports/:icao/points/batch', async (c) => { return c.text('Invalid airport ICAO format', 400); } - const { user } = await resolveUserFromVatsimOrApi(c); - if (!user) { - return c.text('Unauthorized', 401); + const isDivisionDataAutomation = isDivisionDataAutomationRequest(c.req.header('Authorization') || '', c.env.DIVISION_DATA_API_KEY); + let actorId = DIVISION_DATA_ACTOR_ID; + if (!isDivisionDataAutomation) { + const { user } = await resolveUserFromVatsimOrApi(c); + if (!user) { + return c.text('Unauthorized', 401); + } + actorId = user.vatsim_id; } const points = ServicePool.getPoints(c.env); + // SAFETY: PointsService.applyChangeset validates each create, update, and delete operation before the transaction. const changeset = (await c.req.json()) as PointChangeset; - const newPoints = await points.applyChangeset(airportId, user.vatsim_id, changeset); + const newPoints = await points.applyChangeset( + airportId, + actorId, + changeset, + isDivisionDataAutomation ? { kind: 'division-data-automation' } : { kind: 'division-member' }, + ); + await invalidateAirportPointStateCaches(c.env, airportId, changeset.delete ?? []); return c.json(newPoints, 201); }); @@ -2829,8 +2993,10 @@ app.put('/airports/:icao/points/:id', async (c) => { const points = ServicePool.getPoints(c.env); + // SAFETY: PointsService.updatePoint validates its supported update fields before writing them. const updates = (await c.req.json()) as Partial; const updatedPoint = await points.updatePoint(pointId, user.vatsim_id, updates); + await invalidateAirportPointStateCaches(c.env, updatedPoint.airportId); return c.json(updatedPoint); }); @@ -2880,7 +3046,8 @@ app.delete('/airports/:icao/points/:id', async (c) => { const points = ServicePool.getPoints(c.env); try { - await points.deletePoint(pointId, user.vatsim_id); + const pointAirportId = await points.deletePoint(pointId, user.vatsim_id); + await invalidateAirportPointStateCaches(c.env, pointAirportId, [pointId]); return c.body(null, 204); } catch (error) { if (error instanceof HttpError) { @@ -3361,19 +3528,24 @@ const getStoredContributionGenerationResponse = async (c: Context): Promise `supports-gen:${token}`; +const getContributionGenerationCacheKey = (token: string): string => `supports-gen:v2:${token}`; const getContributionGenerationExpiresAt = (): string => new Date(Date.now() + CONTRIBUTION_GENERATION_CACHE_TTL_SECONDS * 1000).toISOString(); @@ -3412,10 +3584,10 @@ const cacheContributionGeneration = (c: Context, token: string, entry: Contribut * @openapi * /supports/generate: * post: - * summary: Generate Light Supports and BARS XML + * summary: Generate simulator removals and BARS XML * tags: * - Generation - * description: Upload raw XML and generate both light supports XML and processed BARS XML. + * description: Upload a draft and generate processed BARS XML plus MSFS light-support XML or compact X-Plane removal JSON. * parameters: * - in: query * name: token @@ -3436,6 +3608,9 @@ const cacheContributionGeneration = (c: Context, token: string, entry: Contribut * format: binary * icao: * type: string + * simulator: + * type: string + * enum: [msfs2020, msfs2024, xplane] * responses: * 200: * description: Generated XML returned @@ -3464,6 +3639,7 @@ app.post( const formData = await c.req.formData(); const xmlFile = formData.get('xmlFile'); const icao = formData.get('icao')?.toString(); + const requestedSimulator = formData.get('simulator')?.toString().trim().toLowerCase(); if (!xmlFile || !(xmlFile instanceof File)) { return c.json({ error: 'XML file is required' }, 400); @@ -3471,10 +3647,13 @@ app.post( if (!icao) { return c.json({ error: 'ICAO code is required' }, 400); } + if (requestedSimulator && !['msfs2020', 'msfs2024', 'xplane'].includes(requestedSimulator)) { + return c.json({ error: 'Invalid simulator' }, 400); + } // Enforce XML content type or .xml extension for safety - const fileType = (xmlFile as File).type?.toLowerCase() || ''; - const fileName = (xmlFile as File).name || ''; + const fileType = xmlFile.type?.toLowerCase() || ''; + const fileName = xmlFile.name || ''; const looksXml = fileType === 'application/xml' || fileType === 'text/xml' || fileType === 'application/x-xml' || /\.xml$/i.test(fileName); if (!looksXml) { @@ -3496,8 +3675,22 @@ app.post( return c.json({ error: msg }, 400); } + const embeddedXPlane = /]*\bsimulator\s*=\s*["']xplane["']/i.test(sanitized); + const simulator = requestedSimulator || (embeddedXPlane ? 'xplane' : 'msfs2024'); + if ((simulator === 'xplane') !== embeddedXPlane) { + return c.json({ error: 'Draft simulator metadata does not match the selected simulator' }, 400); + } + const icaoUpper = icao.trim().toUpperCase(); - const contentToken = await deriveContributionGenerationTokenFromMsfsXml(sanitized, icaoUpper); + if (!ICAO_REGEX.test(icaoUpper)) { + return c.json({ error: 'ICAO code must be exactly 4 letters or digits' }, 400); + } + // SAFETY: requestedSimulator was checked against every Simulator value, and the fallback is also a Simulator. + const simulatorTyped = simulator as Simulator; + const [contentToken, generationHash] = await Promise.all([ + deriveContributionGenerationToken(sanitized, icaoUpper, simulatorTyped), + deriveContributionGenerationHash(sanitized), + ]); const cacheService = ServicePool.getCache(c.env); const cacheKey = getContributionGenerationCacheKey(contentToken); @@ -3510,8 +3703,11 @@ app.post( return c.json({ token: contentToken, icao: cachedEdge.icao, + simulator: cachedEdge.simulator, + generationHash: cachedEdge.generationHash, expiresAt: cachedEdge.expiresAt, - supportsXml: cachedEdge.supportsXml, + supportsXml: simulator === 'xplane' ? undefined : cachedEdge.removalArtifact, + removalsJson: simulator === 'xplane' ? cachedEdge.removalArtifact : undefined, barsXml: cachedEdge.barsXml, }); } @@ -3526,8 +3722,10 @@ app.post( storedGen.token, { icao: storedGen.icao, + simulator: storedGen.simulator ?? simulatorTyped, + generationHash: storedGen.generationHash ?? generationHash, expiresAt: storedGen.expiresAt, - supportsXml: storedGen.supportsXml, + removalArtifact: storedGen.supportsXml, barsXml: storedGen.barsXml, }, cacheTtlSeconds, @@ -3535,8 +3733,11 @@ app.post( return c.json({ token: storedGen.token, icao: storedGen.icao, + simulator: storedGen.simulator ?? simulatorTyped, + generationHash: storedGen.generationHash ?? generationHash, expiresAt: storedGen.expiresAt, - supportsXml: storedGen.supportsXml, + supportsXml: simulator === 'xplane' ? undefined : storedGen.supportsXml, + removalsJson: simulator === 'xplane' ? storedGen.supportsXml : undefined, barsXml: storedGen.barsXml, }); } @@ -3548,9 +3749,12 @@ app.post( let supportsDuration = 0; let barsDuration = 0; - const supportsPromise = (async () => { + const removalPromise = (async () => { const start = performance.now(); - const result = await supportService.generateLightSupportsXML(sanitized, icaoUpper); + const result = + simulator === 'xplane' + ? generateXPlaneRemovalsJson(sanitized, icaoUpper) + : await supportService.generateLightSupportsXML(sanitized, icaoUpper, simulatorTyped); supportsDuration = performance.now() - start; return result; })(); @@ -3562,7 +3766,7 @@ app.post( return result; })(); - const [supportsXml, barsXml] = await Promise.all([supportsPromise, barsPromise]); + const [removalArtifact, barsXml] = await Promise.all([removalPromise, barsPromise]); const totalDuration = performance.now() - totalStart; const serverTiming: string[] = []; @@ -3571,15 +3775,25 @@ app.post( serverTiming.push(`total;dur=${totalDuration.toFixed(2)}`); c.header('Server-Timing', serverTiming.join(', ')); - await contributionsService.storeGeneration(icaoUpper, supportsXml, barsXml, contentToken); + await contributionsService.storeGeneration(icaoUpper, removalArtifact, barsXml, contentToken, simulatorTyped, generationHash); const expiresAt = getContributionGenerationExpiresAt(); - cacheContributionGeneration(c, contentToken, { icao: icaoUpper, expiresAt, supportsXml, barsXml }); + cacheContributionGeneration(c, contentToken, { + icao: icaoUpper, + simulator: simulatorTyped, + generationHash, + expiresAt, + removalArtifact, + barsXml, + }); c.header('X-Cache-Gen', 'MISS'); return c.json({ token: contentToken, icao: icaoUpper, + simulator: simulatorTyped, + generationHash, expiresAt, - supportsXml, + supportsXml: simulator === 'xplane' ? undefined : removalArtifact, + removalsJson: simulator === 'xplane' ? removalArtifact : undefined, barsXml, }); } catch (error) { @@ -3596,6 +3810,9 @@ app.post( 'no bars points found within', 'airport with icao', 'invalid xml', + 'invalid x-plane', + 'x-plane draft', + 'x-plane light selector', ]; const matchesUserError = userFacingPatterns.some((pattern) => normalized.includes(pattern)); if (matchesUserError) { @@ -3709,8 +3926,9 @@ app.put('/notam', async (c) => { return c.text('Forbidden', 403); } - // Update the NOTAM - const { content, type } = (await c.req.json()) as { content: string; type?: string }; + const notamInput = parseJsonObject(await c.req.json()); + const content = readString(notamInput.content) ?? ''; + const type = readString(notamInput.type); const notamService = ServicePool.getNotam(c.env); const updated = await notamService.updateGlobalNotam(content, type, user.vatsim_id); @@ -3748,7 +3966,7 @@ staffUsersApp.use('*', async (c, next) => { } c.set('user', user); - c.set('userService', new UserService(c.env.DB, roles, auth)); + c.set('userService', new UserService(c.env.DB, roles, auth, ServicePool.getPostHog(c.env))); await next(); }); @@ -3781,7 +3999,7 @@ staffUsersApp.get('/', async (c) => { } catch (error) { const message = error instanceof Error ? error.message : 'An unknown error occurred'; const status = error instanceof Error && error.message.includes('Unauthorized') ? 403 : 500; - c.status(status as 403 | 500); + c.status(status === 403 ? 403 : 500); return c.json({ error: message }); } }); @@ -3827,7 +4045,7 @@ staffUsersApp.get('/search', async (c) => { } catch (error) { const message = error instanceof Error ? error.message : 'An unknown error occurred'; const status = error instanceof Error && error.message.includes('Unauthorized') ? 403 : 500; - c.status(status as 403 | 500); + c.status(status === 403 ? 403 : 500); return c.json({ error: message }); } }); @@ -3858,7 +4076,7 @@ staffUsersApp.get('/search', async (c) => { */ staffUsersApp.post('/refresh-api-token', async (c) => { try { - const { vatsimId } = (await c.req.json()) as { vatsimId: string }; + const vatsimId = readString(parseJsonObject(await c.req.json()).vatsimId) ?? ''; if (!vatsimId) { return c.json( @@ -3891,11 +4109,70 @@ staffUsersApp.post('/refresh-api-token', async (c) => { } } - c.status(status as 403 | 404 | 500); + c.status(status === 403 ? 403 : status === 404 ? 404 : 500); return c.json({ error: message }); } }); +/** + * @openapi + * /staff/users/{id}/fast-track: + * put: + * x-hidden: true + * summary: Update contributor fast-track access + * tags: [Staff] + * security: + * - VatsimToken: [] + * parameters: + * - in: path + * name: id + * required: true + * schema: { type: integer } + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * required: [enabled] + * properties: + * enabled: { type: boolean } + * responses: + * 200: { description: Fast-track access updated } + * 400: { description: Invalid request } + * 403: { description: Forbidden } + * 404: { description: User not found } + */ +staffUsersApp.put('/:id/fast-track', async (c) => { + const targetUserId = Number(c.req.param('id')); + if (!Number.isSafeInteger(targetUserId) || targetUserId <= 0) { + return c.json({ error: 'User ID must be a positive integer' }, 400); + } + + let body: JsonValue; + try { + body = await c.req.json(); + } catch { + return c.json({ error: 'Invalid JSON' }, 400); + } + const enabled = readBoolean(parseJsonObject(body).enabled); + if (enabled === undefined) return c.json({ error: 'enabled must be a boolean' }, 400); + + try { + const user = c.get('user')!; + const userService = c.get('userService')!; + const fastTrack = await userService.setFastTrackAccess(targetUserId, enabled, user.id); + return c.json({ success: true, fastTrack }); + } catch (error) { + if (error instanceof HttpError) { + const status = error.status === 403 ? 403 : error.status === 404 ? 404 : 400; + return c.json({ error: error.message }, status); + } + console.error('Failed to update contributor fast-track access', error); + return c.json({ error: 'Failed to update fast-track access' }, 500); + } +}); + // DELETE /staff/users/:id - Delete a user /** * @openapi @@ -3927,7 +4204,7 @@ staffUsersApp.delete('/:id', async (c) => { } catch (error) { const message = error instanceof Error ? error.message : 'An unknown error occurred'; const status = error instanceof Error && error.message.includes('Unauthorized') ? 403 : 500; - c.status(status as 403 | 500); + c.status(status === 403 ? 403 : 500); return c.json({ error: message }); } }); @@ -3991,15 +4268,15 @@ staffManageApp.get('/', async (c) => { staffManageApp.post('/', async (c) => { const vatsimToken = c.req.header('X-Vatsim-Token'); if (!vatsimToken) return c.text('Unauthorized', 401); - let body: unknown; + let body: JsonValue; try { body = await c.req.json(); } catch { return c.json({ error: 'Invalid JSON' }, 400); } - const b = (body ?? {}) as Record; - const vatsimId = typeof b.vatsimId === 'string' ? b.vatsimId : ''; - const role = typeof b.role === 'string' ? b.role : ''; + const staffInput = parseJsonObject(body); + const vatsimId = readString(staffInput.vatsimId) ?? ''; + const role = readString(staffInput.role) ?? ''; if (!vatsimId || !role || !(role in StaffRole)) return c.json({ error: 'vatsimId and valid role required' }, 400); const vatsim = ServicePool.getVatsim(c.env); const auth = ServicePool.getAuth(c.env); @@ -4012,6 +4289,7 @@ staffManageApp.post('/', async (c) => { const targetUser = await auth.getUserByVatsimId(vatsimId); if (!targetUser) return c.json({ error: 'Target user not found' }, 404); try { + // SAFETY: membership in StaffRole was established before authorization and persistence. const staff = await roles.addStaff(targetUser.id, role as StaffRole); return c.json({ success: true, staff }); } catch (e) { @@ -4110,7 +4388,7 @@ staffDivisionsApp.use('*', async (c, next) => { staffDivisionsApp.get('/airports', async (c) => { const divisions = ServicePool.getDivisions(c.env); const airports = await divisions.getAllDivisionAirports(); - const shaped = airports.map((airport) => ({ + const airportSummaries = airports.map((airport) => ({ division_id: airport.division_id, division_name: airport.division_name, airport_request_id: airport.id, @@ -4120,7 +4398,7 @@ staffDivisionsApp.get('/airports', async (c) => { approved_by: airport.approved_by ?? null, contributions_enabled: airport.contributions_enabled, })); - return c.json({ airports: shaped }); + return c.json({ airports: airportSummaries }); }); app.route('/staff/divisions', staffDivisionsApp); @@ -4151,6 +4429,12 @@ const contributionsApp = new Hono<{ Bindings: Env }>(); * type: boolean * description: When true, returns only contribution id, airport ICAO, package name, and simulator. * - in: query + * name: projection + * schema: + * type: string + * enum: [metadata] + * description: Bounded contribution metadata including artifact keys, without submitted XML or generation proof. + * - in: query * name: summary * schema: * type: boolean @@ -4176,10 +4460,12 @@ contributionsApp.get( const contributions = ServicePool.getContributions(c.env); // Parse query parameters for filtering - const status = (c.req.query('status') as 'pending' | 'approved' | 'rejected' | 'outdated' | 'all') || 'all'; + const requestedStatus = c.req.query('status'); + const status = CONTRIBUTION_STATUSES.find((candidate) => candidate === requestedStatus) ?? 'all'; const airportIcao = c.req.query('airport') || undefined; const requestedUserId = c.req.query('user') || undefined; const simple = (c.req.query('simple') || 'false').toLowerCase() === 'true'; + const projection = c.req.query('projection')?.trim().toLowerCase(); const authz = c.req.header('Authorization') || ''; let authenticatedUserId: string | undefined; @@ -4201,6 +4487,12 @@ contributionsApp.get( const userId = authenticatedUserId ?? requestedUserId; const includeSummary = authenticatedUserId !== undefined || (c.req.query('summary') || 'false').toLowerCase() === 'true'; + if (projection !== undefined && projection !== 'metadata') { + return c.json({ error: 'projection must be metadata when provided' }, 400); + } + if (simple && projection) { + return c.json({ error: 'simple cannot be combined with projection' }, 400); + } if (simple && includeSummary) { return c.json({ error: 'summary cannot be combined with simple view' }, 400); } @@ -4218,6 +4510,15 @@ contributionsApp.get( }); } + if (projection === 'metadata') { + const result = await contributions.listContributionMetadata({ status, airportIcao, userId }); + if (!includeSummary) { + return c.json({ ...result, projection: 'metadata' as const }); + } + const summary = summarizeContributionStatuses(result.contributions, result.total); + return c.json({ ...result, projection: 'metadata' as const, summary }); + } + const result = await contributions.listContributions({ status, airportIcao, @@ -4228,28 +4529,7 @@ contributionsApp.get( return c.json(result); } - const summary = result.contributions.reduce( - (acc, item) => { - switch (item.status) { - case 'approved': - acc.approved += 1; - break; - case 'pending': - acc.pending += 1; - break; - case 'rejected': - acc.rejected += 1; - break; - case 'outdated': - acc.outdated += 1; - break; - default: - break; - } - return acc; - }, - { total: result.total, approved: 0, pending: 0, rejected: 0, outdated: 0 }, - ); + const summary = summarizeContributionStatuses(result.contributions, result.total); return c.json({ ...result, summary }); }, @@ -4322,7 +4602,7 @@ contributionsApp.get( * application/json: * schema: * type: object - * required: [airportIcao, packageName, submittedXml, simulator] + * required: [airportIcao, packageName, submittedXml, simulator, generationToken, generationHash] * properties: * airportIcao: * type: string @@ -4335,8 +4615,17 @@ contributionsApp.get( * notes: { type: string, maxLength: 1000 } * simulator: * type: string - * enum: [msfs2020, msfs2024] + * enum: [msfs2020, msfs2024, xplane] * description: Target flight simulator for this contribution + * generationToken: + * type: string + * description: Token returned by testing this exact normalized draft through /supports/generate. + * generationHash: + * type: string + * description: Normalized draft hash returned by /supports/generate. + * fastTrackRequested: + * type: boolean + * description: Opt in to fast-track publishing when the authenticated account is eligible. Defaults to false. * responses: * 201: * description: Contribution created @@ -4361,6 +4650,7 @@ contributionsApp.post('/', rateLimit({ maxRequests: 1 }), async (c) => { try { const contributions = ServicePool.getContributions(c.env); + // SAFETY: ContributionsService.createContribution validates the complete submission contract before persistence or storage writes. const payload = (await c.req.json()) as ContributionSubmissionPayload; const result = await contributions.createContribution({ userId: user.vatsim_id, @@ -4369,6 +4659,9 @@ contributionsApp.post('/', rateLimit({ maxRequests: 1 }), async (c) => { submittedXml: payload.submittedXml, notes: payload.notes, simulator: payload.simulator, + generationToken: payload.generationToken, + generationHash: payload.generationHash, + fastTrackRequested: payload.fastTrackRequested === true, }); return c.json(result, 201); @@ -4460,11 +4753,13 @@ contributionsApp.post('/:id/decision', async (c) => { try { const contributionId = c.req.param('id'); const contributions = ServicePool.getContributions(c.env); - const payload = (await c.req.json()) as ContributionDecisionPayload; + const payload = parseJsonObject(await c.req.json()); + const approved = readBoolean(payload.approved); + if (approved === undefined) return c.json({ error: 'approved must be a boolean' }, 400); const result = await contributions.processDecision(contributionId, user.vatsim_id, { - approved: payload.approved, - rejectionReason: payload.rejectionReason, - newPackageName: payload.newPackageName, + approved, + rejectionReason: readString(payload.rejectionReason), + newPackageName: readString(payload.newPackageName), }); return c.json(result); @@ -4613,7 +4908,7 @@ const cdnApp = new Hono<{ Bindings: Env }>(); * required: false * schema: * type: string - * enum: [msfs2020, msfs2024] + * enum: [msfs2020, msfs2024, xplane] * description: Filter by simulator (optional - returns latest across all simulators if not specified) * responses: * 200: @@ -4624,7 +4919,8 @@ const cdnApp = new Hono<{ Bindings: Env }>(); app.get('/maps/:icao/packages/:package/latest', withCache(CacheKeys.fromUrl, 900, 'airports'), async (c) => { const icao = c.req.param('icao').toUpperCase(); const pkg = c.req.param('package'); - const simulatorParam = c.req.query('simulator') as 'msfs2020' | 'msfs2024' | undefined; + const requestedSimulator = c.req.query('simulator'); + const simulatorParam = ['msfs2020', 'msfs2024', 'xplane'].find((simulator): simulator is Simulator => simulator === requestedSimulator); const contributions = ServicePool.getContributions(c.env); const storage = ServicePool.getStorage(c.env); @@ -4633,8 +4929,8 @@ app.get('/maps/:icao/packages/:package/latest', withCache(CacheKeys.fromUrl, 900 return c.text('No approved map found', 404); } - const safePackageName = latest.packageName.replace(/[^a-zA-Z0-9.-]/g, '-'); - const fileKey = `Maps/${icao}_${safePackageName}_${latest.simulator}_bars.xml`; + const legacySafePackageName = latest.packageName.replace(/[^a-zA-Z0-9.-]/g, '-'); + const fileKey = latest.barsArtifactKey ?? `Maps/${icao}_${legacySafePackageName}_${latest.simulator}_bars.xml`; const stored = await storage.getFile(fileKey); if (!stored) { @@ -4837,7 +5133,6 @@ cdnApp.get('/files', async (c) => { const prefix = c.req.query('prefix') || undefined; const limit = Number.MAX_SAFE_INTEGER; - // Get list of files const storage = ServicePool.getStorage(c.env); const result = await storage.listFiles(prefix, limit); @@ -4918,7 +5213,6 @@ cdnApp.delete('/files/:fileKey{.+}', async (c) => { ); } - // Delete the file const storage = ServicePool.getStorage(c.env); const deleted = await storage.deleteFile(fileKey); @@ -4979,7 +5273,6 @@ app.get('/euroscope/files/:icao', async (c) => { } try { - // Get list of files for this ICAO const storage = ServicePool.getStorage(c.env); const result = await storage.listFiles(`EuroScope/${icao}/`, 10); @@ -4988,7 +5281,7 @@ app.get('/euroscope/files/:icao', async (c) => { fileName: obj.key.split('/').pop() || '', size: obj.size, uploaded: obj.uploaded.toISOString(), - url: new URL(`https://dev-cdn.stopbars.com/${obj.key}`, c.req.url).toString(), + url: new URL(`https://cdn.stopbars.com/${obj.key}`, c.req.url).toString(), })); return c.json({ @@ -5012,14 +5305,25 @@ const euroscopeApp = new Hono<{ Variables: { vatsimUser?: VatsimUser; user?: UserRecord; + actorId?: string; + divisionDataAutomation?: boolean; }; }>(); euroscopeApp.use('*', async (c, next) => { + if (isDivisionDataAutomationRequest(c.req.header('Authorization') || '', c.env.DIVISION_DATA_API_KEY)) { + c.set('actorId', DIVISION_DATA_ACTOR_ID); + c.set('divisionDataAutomation', true); + await next(); + return; + } + const { user, vatsimUser } = await resolveUserFromVatsimOrApi(c); if (!user) return c.text('Unauthorized', 401); c.set('vatsimUser', vatsimUser!); c.set('user', user); + c.set('actorId', vatsimUser!.id.toString()); + c.set('divisionDataAutomation', false); await next(); }); @@ -5052,7 +5356,8 @@ euroscopeApp.use('*', async (c, next) => { * description: File uploaded */ euroscopeApp.post('/upload', async (c) => { - const vatsimUser = c.get('vatsimUser')!; + const actorId = c.get('actorId')!; + const isDivisionDataAutomation = c.get('divisionDataAutomation') === true; try { const formData = await c.req.formData(); @@ -5100,7 +5405,7 @@ euroscopeApp.post('/upload', async (c) => { // Check if user has access to upload files for this ICAO const divisions = ServicePool.getDivisions(c.env); - const hasAccess = await divisions.userHasAirportAccess(vatsimUser.id.toString(), icao); + const hasAccess = isDivisionDataAutomation || (await divisions.userHasAirportAccess(actorId, icao)); if (!hasAccess) { return c.json( @@ -5132,7 +5437,7 @@ euroscopeApp.post('/upload', async (c) => { // Stream directly to R2 instead of duplicating the entire upload in memory. const result = await storage.uploadFile(fileKey, file.stream(), file.type, { - uploadedBy: vatsimUser.id.toString(), + uploadedBy: actorId, icao: icao, fileName: file.name, size: file.size.toString(), @@ -5147,7 +5452,7 @@ euroscopeApp.post('/upload', async (c) => { icao: icao, fileName: fileName, size: file.size, - url: new URL(`https://dev-cdn.stopbars.com/${result.key}`, c.req.url).toString(), + url: new URL(`https://cdn.stopbars.com/${result.key}`, c.req.url).toString(), }, }, 201, @@ -5190,7 +5495,8 @@ euroscopeApp.post('/upload', async (c) => { euroscopeApp.delete('/files/:icao/:filename', async (c) => { const icao = c.req.param('icao').toUpperCase(); const filename = c.req.param('filename'); - const vatsimUser = c.get('vatsimUser')!; + const actorId = c.get('actorId')!; + const isDivisionDataAutomation = c.get('divisionDataAutomation') === true; // Validate ICAO format if (!ICAO_REGEX.test(icao)) { @@ -5205,7 +5511,7 @@ euroscopeApp.delete('/files/:icao/:filename', async (c) => { try { // Check if user has access to delete files for this ICAO const divisions = ServicePool.getDivisions(c.env); - const hasAccess = await divisions.userHasAirportAccess(vatsimUser.id.toString(), icao); + const hasAccess = isDivisionDataAutomation || (await divisions.userHasAirportAccess(actorId, icao)); if (!hasAccess) { return c.json( @@ -5219,7 +5525,6 @@ euroscopeApp.delete('/files/:icao/:filename', async (c) => { // Construct the file key const fileKey = `EuroScope/${icao}/${filename}`; - // Delete the file const storage = ServicePool.getStorage(c.env); const deleted = await storage.deleteFile(fileKey); @@ -5269,7 +5574,8 @@ euroscopeApp.delete('/files/:icao/:filename', async (c) => { */ euroscopeApp.get('/:icao/editable', async (c) => { const icao = c.req.param('icao').toUpperCase(); - const vatsimUser = c.get('vatsimUser')!; + const actorId = c.get('actorId')!; + const isDivisionDataAutomation = c.get('divisionDataAutomation') === true; // Validate ICAO format if (!ICAO_REGEX.test(icao)) { @@ -5282,9 +5588,17 @@ euroscopeApp.get('/:icao/editable', async (c) => { } try { + if (isDivisionDataAutomation) { + return c.json({ + icao: icao, + editable: true, + role: 'automation', + }); + } + // Check if user has access to edit files for this ICAO const divisions = ServicePool.getDivisions(c.env); - const userRole = await divisions.getUserRoleForAirport(vatsimUser.id.toString(), icao); + const userRole = await divisions.getUserRoleForAirport(actorId, icao); return c.json({ icao: icao, @@ -5320,7 +5634,7 @@ app.get('/vatsys/profiles', withCache(CacheKeys.fromUrl, 600, 'airports'), async const profiles = items.map((it) => ({ icao: it.icao, name: it.name, - url: new URL(`https://dev-cdn.stopbars.com/${it.key}`, c.req.url).toString(), + url: new URL(`https://cdn.stopbars.com/${it.key}`, c.req.url).toString(), })); return c.json({ profiles }); }); @@ -5366,19 +5680,20 @@ app.post('/vatsys/profiles/generate', async (c) => { return c.json({ error: 'Unauthorized' }, 401); } - let body: { icao?: unknown }; + let body: JsonObject; try { - body = (await c.req.json()) as { icao?: unknown }; + body = parseJsonObject(await c.req.json()); } catch { return c.json({ error: 'Invalid JSON body' }, 400); } - if (typeof body.icao !== 'string') { + const icao = readString(body.icao); + if (icao === undefined) { return c.json({ error: 'icao is required' }, 400); } const generator = ServicePool.getVatSysProfileGenerator(c.env); - const result = await generator.generate(body.icao); + const result = await generator.generate(icao); return c.json(result); }); @@ -5441,7 +5756,7 @@ vatsysStaffApp.post('/profiles/upload', async (c) => { const svc = ServicePool.getVatSysProfiles(c.env); try { const res = await svc.upload(file.name, bytes, user.vatsim_id); - const url = new URL(`https://dev-cdn.stopbars.com/${res.key}`, c.req.url).toString(); + const url = new URL(`https://cdn.stopbars.com/${res.key}`, c.req.url).toString(); return c.json({ success: true, key: res.key, etag: res.etag, url }, 201); } catch (e) { return c.json({ error: e instanceof Error ? e.message : 'Upload failed' }, 400); @@ -5500,7 +5815,9 @@ app.get( '/releases', withCache(CacheKeys.fromUrl, 300, 'installer'), // cache 5m async (c) => { - const product = c.req.query('product') as InstallerProduct | undefined; + const requestedProduct = c.req.query('product'); + const product = parseInstallerProduct(requestedProduct); + if (requestedProduct && !product) return c.json({ releases: [] }); const releasesService = ServicePool.getReleases(c.env); const releases = await releasesService.listReleases(product); return c.json({ releases }); @@ -5526,15 +5843,17 @@ app.get( * description: Not found */ app.get('/releases/latest', withCache(CacheKeys.fromUrl, 120, 'installer'), async (c) => { - const product = c.req.query('product') as InstallerProduct | undefined; - if (!product) return c.text('product required', 400); + const requestedProduct = c.req.query('product'); + if (!requestedProduct) return c.text('product required', 400); + const product = parseInstallerProduct(requestedProduct); + if (!product) return c.text('Not found', 404); const releasesService = ServicePool.getReleases(c.env); const latest = await releasesService.getLatest(product); if (!latest) return c.text('Not found', 404); const downloadUrl = product === 'SimConnect.NET' ? `https://www.nuget.org/packages/SimConnect.NET/${latest.version}` - : new URL(`https://dev-cdn.stopbars.com/${latest.file_key}`, c.req.url).toString(); + : new URL(`https://cdn.stopbars.com/${latest.file_key}`, c.req.url).toString(); const imageUrl = latest.image_url ? new URL(latest.image_url, c.req.url).toString() : undefined; const { image_url: _imageUrlOmitted, ...rest } = latest; void _imageUrlOmitted; @@ -5598,7 +5917,8 @@ app.post('/releases/upload', async (c) => { } const file = formData.get('file'); - const product = formData.get('product')?.toString() as InstallerProduct | undefined; + const requestedProduct = formData.get('product')?.toString(); + const product = parseInstallerProduct(requestedProduct); const version = formData.get('version')?.toString(); const changelog = formData.get('changelog')?.toString(); const image = formData.get('image'); @@ -5613,7 +5933,8 @@ app.post('/releases/upload', async (c) => { const allowed = await roles.hasPermission(user.id, StaffRole.PRODUCT_MANAGER); if (!allowed) return c.text('Forbidden', 403); - if (!product || !version) return c.json({ error: 'product & version required' }, 400); + if (!requestedProduct || !version) return c.json({ error: 'product & version required' }, 400); + if (!product) return c.json({ error: 'invalid product' }, 400); const isSimConnect = product === 'SimConnect.NET'; const isInstallerExe = product === 'Installer'; @@ -5639,7 +5960,8 @@ app.post('/releases/upload', async (c) => { let bytes: ArrayBuffer | undefined; if (!isSimConnect) { // File upload path for normal products - const uploadFile = file as File; // already validated + // SAFETY: the non-SimConnect branch rejected any file value that was not a File. + const uploadFile = file as File; fileKey = `releases/${product}/${version}/${uploadFile.name}`; bytes = await uploadFile.arrayBuffer(); } else { @@ -5677,9 +5999,10 @@ app.post('/releases/upload', async (c) => { product, version, }); - imageUrl = `https://dev-cdn.stopbars.com/${imageKey}`; + imageUrl = `https://cdn.stopbars.com/${imageKey}`; } if (!isSimConnect) { + // SAFETY: the non-SimConnect branch rejected any file value that was not a File. const uploadFile = file as File; const fileUploadPromise = storage.uploadFile(fileKey, bytes!, uploadFile.type || 'application/octet-stream', { uploadedBy: user.vatsim_id, @@ -5698,6 +6021,7 @@ app.post('/releases/upload', async (c) => { product, version, fileKey, + // SAFETY: bytes is populated only after the non-SimConnect File check succeeds. fileSize: bytes ? (file as File).size : 0, fileHash: sha256, changelog, @@ -5705,7 +6029,7 @@ app.post('/releases/upload', async (c) => { }); const downloadUrl = isSimConnect ? `https://www.nuget.org/packages/SimConnect.NET/${version}` - : `https://dev-cdn.stopbars.com/${fileKey}`; + : `https://cdn.stopbars.com/${fileKey}`; return c.json({ success: true, release, downloadUrl, imageUrl }, 201); } catch (err) { console.error('Release upload error', err); @@ -5772,14 +6096,13 @@ app.put('/releases/:id/changelog', async (c) => { const canEdit = await roles.hasPermission(user.id, StaffRole.PRODUCT_MANAGER); if (!canEdit) return c.text('Forbidden', 403); - let body: unknown; + let body: JsonValue; try { body = await c.req.json(); } catch { return c.json({ error: 'Invalid JSON body' }, 400); } - const b = (body ?? {}) as Record; - const changelog = typeof b.changelog === 'string' ? b.changelog.trim() : ''; + const changelog = readString(parseJsonObject(body).changelog)?.trim() ?? ''; if (!changelog) return c.json({ error: 'changelog required' }, 400); if (changelog.length > 20000) return c.json({ error: 'changelog too long (max 20000 chars)' }, 400); @@ -5818,9 +6141,10 @@ app.put('/releases/:id/changelog', async (c) => { * description: Latest release not found when version omitted */ app.post('/download', async (c) => { - const product = c.req.query('product') as InstallerProduct | undefined; - if (!product) return c.json({ error: 'product required' }, 400); - if (!INSTALLER_PRODUCTS.includes(product)) return c.json({ error: 'invalid product' }, 400); + const requestedProduct = c.req.query('product'); + if (!requestedProduct) return c.json({ error: 'product required' }, 400); + const product = parseInstallerProduct(requestedProduct); + if (!product) return c.json({ error: 'invalid product' }, 400); const releases = ServicePool.getReleases(c.env); const latest = await releases.getLatest(product); if (!latest) return c.json({ error: 'No release found for product' }, 404); @@ -5836,11 +6160,11 @@ app.post('/download', async (c) => { * /staff/bars-packages/upload: * post: * x-hidden: true - * summary: Upload BARS installer packages (models/removals) + * summary: Upload BARS installer packages * description: >- - * Staff-only endpoint to upload the two MSFS data packages used by the installer. Accepts a multipart form - * with a single file and a `type` field designating which package is being uploaded. The file is stored in R2 - * under a fixed key and previous version overwritten. SHA-256 is computed server-side and stored in metadata. + * Staff-only endpoint to upload the data packages used by the installer. MSFS packages overwrite fixed R2 keys. + * X-Plane bridge packages require a semantic `version`, are stored under versioned keys, and advance a latest + * pointer only after the archive upload succeeds. SHA-256 is computed server-side and stored in metadata. * tags: * - Staff * - Data @@ -5860,7 +6184,10 @@ app.post('/download', async (c) => { * format: binary * type: * type: string - * enum: [models, models-2020, removals-2020, removals-2024] + * enum: [models, models-2020, removals-2020, removals-2024, xplane-bridge] + * version: + * type: string + * description: Required semantic version when type is xplane-bridge * responses: * 201: * description: Package uploaded successfully @@ -5908,8 +6235,16 @@ app.post('/staff/bars-packages/upload', async (c) => { } const file = form.get('file'); const type = form.get('type')?.toString(); + const version = form.get('version')?.toString().trim(); if (!file || !(file instanceof File)) return c.json({ error: 'file required' }, 400); - if (!type || !['models', 'models-2020', 'removals-2020', 'removals-2024'].includes(type)) return c.json({ error: 'invalid type' }, 400); + if (!type || !['models', 'models-2020', 'removals-2020', 'removals-2024', 'xplane-bridge'].includes(type)) + return c.json({ error: 'invalid type' }, 400); + const isXPlaneBridge = type === 'xplane-bridge'; + const semverPattern = + /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/; + if (isXPlaneBridge && (!version || !semverPattern.test(version))) { + return c.json({ error: 'A valid semantic version is required for xplane-bridge' }, 400); + } // Enforce .zip const lowerName = file.name.toLowerCase(); if (!lowerName.endsWith('.zip')) return c.json({ error: 'file must be a .zip archive' }, 400); @@ -5928,29 +6263,71 @@ app.post('/staff/bars-packages/upload', async (c) => { if (type === 'models') key = 'packages/bars-models-2024.zip'; else if (type === 'models-2020') key = 'packages/bars-models-2020.zip'; else if (type === 'removals-2020') key = 'packages/bars-removals-2020.zip'; - else key = 'packages/bars-removals-2024.zip'; - const uploadRes = await storage.uploadFile(key, bytes, 'application/zip', { - uploadedBy: user.vatsim_id, - type, - size: file.size.toString(), - sha256, - }); - const url = new URL(`https://dev-cdn.stopbars.com/${uploadRes.key}`, c.req.url).toString(); - return c.json( - { - success: true, - package: { + else if (type === 'removals-2024') key = 'packages/bars-removals-2024.zip'; + else key = `packages/xplane-bridge/${version}/BARSXPlaneBridge.zip`; + if (isXPlaneBridge && (await storage.headFile(key))) { + return c.json({ error: `X-Plane bridge ${version} has already been uploaded` }, 409); + } + const uploadedAt = new Date().toISOString(); + const uploadMetadataEntries: Array<[string, string]> = [ + ['uploadedBy', user.vatsim_id], + ['type', type], + ['size', file.size.toString()], + ['sha256', sha256], + ]; + if (isXPlaneBridge) { + uploadMetadataEntries.push(['version', version!], ['protocol', 'BARS.XPlaneBridge.v1']); + } + const uploadMetadata = Object.fromEntries(uploadMetadataEntries); + const uploadRes = await storage.uploadFile( + key, + bytes, + 'application/zip', + uploadMetadata, + isXPlaneBridge ? { onlyIfAbsent: true } : undefined, + ); + if (isXPlaneBridge) { + await storage.uploadFile( + 'packages/xplane-bridge/latest.json', + JSON.stringify({ + schema: 'bars-xplane-bridge-package/v1', type, + version, + protocol: 'BARS.XPlaneBridge.v1', key: uploadRes.key, size: file.size, sha256, etag: uploadRes.etag, - url, - }, + uploaded: uploadedAt, + }), + 'application/json', + { type, version: version!, protocol: 'BARS.XPlaneBridge.v1' }, + ); + } + const url = new URL(`https://cdn.stopbars.com/${uploadRes.key}`, c.req.url).toString(); + const uploadedPackage: UploadedBarsPackage = { + type, + key: uploadRes.key, + size: file.size, + sha256, + etag: uploadRes.etag, + url, + }; + if (isXPlaneBridge) { + uploadedPackage.version = version; + uploadedPackage.protocol = 'BARS.XPlaneBridge.v1'; + } + return c.json( + { + success: true, + package: uploadedPackage, }, 201, ); } catch (err) { + if (isXPlaneBridge && err instanceof Error && err.message === 'Storage object already exists') { + return c.json({ error: `X-Plane bridge ${version} has already been uploaded` }, 409); + } console.error('BARS package upload error', err); return c.json({ error: err instanceof Error ? err.message : 'upload failed' }, 500); } @@ -5961,7 +6338,7 @@ app.post('/staff/bars-packages/upload', async (c) => { * /bars-packages: * get: * summary: List current BARS data packages - * description: Public metadata for installer MSFS data packages (models & removals). Returns size, hash and timestamps. + * description: Public metadata for installer packages. Returns version, protocol, size, hash and timestamps where applicable. * tags: * - Data * responses: @@ -5994,14 +6371,57 @@ app.get('/bars-packages', withCache(CacheKeys.fromUrl, 300, 'data'), async (c) = uploaded: obj.uploaded.toISOString(), sha256: obj.customMetadata?.sha256 || null, type, - url: new URL(`https://dev-cdn.stopbars.com/${key}`, c.req.url).toString(), + url: new URL(`https://cdn.stopbars.com/${key}`, c.req.url).toString(), }; } catch { return null; } }), ); - return c.json({ packages: results.filter(Boolean) }); + let xplaneBridge: JsonObject | null = null; + try { + const pointerResponse = await storage.getFile('packages/xplane-bridge/latest.json'); + if (pointerResponse) { + const pointer = parseJsonObject(await pointerResponse.json()); + const pointerVersion = readString(pointer.version); + const pointerKey = readString(pointer.key); + const pointerSize = readNumber(pointer.size); + const pointerSha256 = readString(pointer.sha256); + const pointerUploaded = readString(pointer.uploaded); + if ( + pointer.schema === 'bars-xplane-bridge-package/v1' && + pointer.type === 'xplane-bridge' && + pointerVersion !== undefined && + pointer.protocol === 'BARS.XPlaneBridge.v1' && + pointerKey !== undefined && + pointerKey === `packages/xplane-bridge/${pointerVersion}/BARSXPlaneBridge.zip` && + pointerSize !== undefined && + Number.isSafeInteger(pointerSize) && + pointerSize > 0 && + pointerSha256 !== undefined && + /^[a-f0-9]{64}$/.test(pointerSha256) && + pointerUploaded !== undefined + ) { + const archive = await storage.headFile(pointerKey); + if ( + archive && + archive.size === pointerSize && + archive.customMetadata?.sha256 === pointerSha256 && + archive.customMetadata?.version === pointerVersion && + archive.customMetadata?.protocol === pointer.protocol + ) { + xplaneBridge = { + ...pointer, + etag: archive.etag, + url: new URL(`https://cdn.stopbars.com/${pointerKey}`, c.req.url).toString(), + }; + } + } + } + } catch (error) { + console.error('X-Plane bridge package pointer is invalid', error); + } + return c.json({ packages: [...results.filter(Boolean), ...(xplaneBridge ? [xplaneBridge] : [])] }); } catch (err) { console.error('BARS package list error', err); return c.json({ error: 'Failed to list packages' }, 500); @@ -6028,15 +6448,16 @@ app.get('/bars-packages', withCache(CacheKeys.fromUrl, 300, 'data'), async (c) = */ app.get('/downloads/stats', withCache(CacheKeys.fromUrl, 300, 'installer'), async (c) => { - const product = c.req.query('product') as InstallerProduct | undefined; + const requestedProduct = c.req.query('product'); + const product = parseInstallerProduct(requestedProduct); const downloads = ServicePool.getDownloads(c.env); - if (!product) { + if (!requestedProduct) { // No product specified -> return stats for all products const all = await downloads.getAllStats(); const combinedTotal = all.reduce((sum, item) => sum + item.total, 0); return c.json({ products: all, combinedTotal }); } - if (!INSTALLER_PRODUCTS.includes(product)) return c.json({ error: 'invalid product' }, 400); + if (!product) return c.json({ error: 'invalid product' }, 400); const stats = await downloads.getStats(product); return c.json(stats); }); @@ -6092,7 +6513,9 @@ app.post('/purge-cache', async (c) => { } try { - const { key, namespace } = (await c.req.json()) as { key: string; namespace?: string }; + const purgeInput = parseJsonObject(await c.req.json()); + const key = readString(purgeInput.key) ?? ''; + const namespace = readString(purgeInput.namespace); if (!key) { return c.json({ error: 'Cache key is required' }, 400); @@ -6159,17 +6582,13 @@ app.post('/purge-cache-all', async (c) => { const cache = ServicePool.getCache(c.env); try { - let body: unknown = undefined; + let body: JsonValue | undefined = undefined; try { body = await c.req.json(); } catch { /* allow empty body */ } - let namespace: string | undefined = undefined; - if (body && typeof body === 'object') { - const maybe = body as Record; - if (typeof maybe.namespace === 'string') namespace = maybe.namespace; - } + const namespace = readString(parseJsonObject(body).namespace); const toPurge: readonly string[] = namespace ? [namespace] : CACHE_NAMESPACES; const versions = await Promise.all(toPurge.map((cacheNamespace) => cache.bumpNamespaceVersion(cacheNamespace))); const results = Object.fromEntries(toPurge.map((cacheNamespace, index) => [cacheNamespace, versions[index]])); @@ -6279,16 +6698,16 @@ faqStaffApp.use('*', async (c, next) => { * description: Created */ faqStaffApp.post('/', async (c) => { - let body: unknown; + let body: JsonValue; try { body = await c.req.json(); } catch { return c.json({ error: 'Invalid JSON' }, 400); } - const b = (body ?? {}) as Record; - const question = typeof b.question === 'string' ? b.question : ''; - const answer = typeof b.answer === 'string' ? b.answer : ''; - let order_position = Number((b as { order_position?: unknown }).order_position); + const faqInput = parseJsonObject(body); + const question = readString(faqInput.question) ?? ''; + const answer = readString(faqInput.answer) ?? ''; + let order_position = Number(faqInput.order_position); if (!question || !answer || !Number.isInteger(order_position)) { return c.json({ error: 'question, answer, order_position required' }, 400); } @@ -6338,20 +6757,19 @@ faqStaffApp.post('/', async (c) => { */ faqStaffApp.put('/:id', async (c) => { const id = c.req.param('id'); - let body: unknown; + let body: JsonValue; try { body = await c.req.json(); } catch { body = {}; } const faqService = ServicePool.getFAQs(c.env); - const b = (body ?? {}) as Record; + const faqInput = parseJsonObject(body); + const orderPosition = readNumber(faqInput.order_position); const updated = await faqService.update(id, { - question: typeof b.question === 'string' ? b.question : undefined, - answer: typeof b.answer === 'string' ? b.answer : undefined, - order_position: Number.isInteger((b as { order_position?: unknown }).order_position as number) - ? (b as { order_position: number }).order_position - : undefined, + question: readString(faqInput.question), + answer: readString(faqInput.answer), + order_position: Number.isInteger(orderPosition) ? orderPosition : undefined, }); if (!updated) return c.text('Not found', 404); try { @@ -6428,26 +6846,23 @@ faqStaffApp.delete('/:id', async (c) => { * description: Reordered */ faqStaffApp.post('/reorder', async (c) => { - let body: unknown; + let body: JsonValue; try { body = await c.req.json(); } catch { return c.json({ error: 'Invalid JSON' }, 400); } - const b = (body ?? {}) as Record; - const updatesRaw = b.updates as unknown; + const updatesRaw = parseJsonObject(body).updates; if (!Array.isArray(updatesRaw)) return c.json({ error: 'updates array required' }, 400); type ReorderUpdate = { id: string; order_position: number }; - const updates: ReorderUpdate[] = (updatesRaw as unknown[]) - .filter((u): u is ReorderUpdate => { - if (!u || typeof u !== 'object') return false; - const obj = u as Record; - return typeof obj.id === 'string' && Number.isInteger(obj.order_position as number); - }) - .map((u) => { - const obj = u as Record; - return { id: obj.id as string, order_position: obj.order_position as number }; - }); + const updates: ReorderUpdate[] = updatesRaw.flatMap((updateValue) => { + const update = parseJsonObject(updateValue); + const id = readString(update.id); + const orderPosition = readNumber(update.order_position); + return id !== undefined && orderPosition !== undefined && Number.isInteger(orderPosition) + ? [{ id, order_position: orderPosition }] + : []; + }); const faqService = ServicePool.getFAQs(c.env); await faqService.reorder(updates); try { @@ -6481,9 +6896,11 @@ app.route('/staff/faqs', faqStaffApp); */ app.get('/health', withCache(CacheKeys.fromUrl, 60, 'health'), async (c) => { const requestedService = c.req.query('service'); - const validServices = ['database', 'storage', 'vatsim']; + const validServices = ['database', 'storage', 'vatsim'] as const; + type HealthService = (typeof validServices)[number]; + const selectedService = validServices.find((service) => service === requestedService); - if (requestedService && !validServices.includes(requestedService)) { + if (requestedService && selectedService === undefined) { return c.json( { error: 'Invalid service', @@ -6493,10 +6910,11 @@ app.get('/health', withCache(CacheKeys.fromUrl, 60, 'health'), async (c) => { ); } - const servicesToCheck = requestedService ? [requestedService] : validServices; - const healthChecks = Object.fromEntries(servicesToCheck.map((service) => [service, 'ok'] as const)) as Record; + const servicesToCheck: readonly HealthService[] = selectedService ? [selectedService] : validServices; + const healthChecks: Partial> = {}; + for (const service of servicesToCheck) healthChecks[service] = 'ok'; - const serviceChecks: Record<(typeof validServices)[number], () => Promise> = { + const serviceChecks = { database: async () => { await c.env.DB.prepare('SELECT 1').first(); }, @@ -6527,7 +6945,7 @@ app.get('/health', withCache(CacheKeys.fromUrl, 60, 'health'), async (c) => { await Promise.all( servicesToCheck.map(async (service) => { try { - await serviceChecks[service as (typeof validServices)[number]](); + await serviceChecks[service](); } catch (error) { if (service === 'vatsim') { console.error('VATSIM health check failed:', error); diff --git a/src/network/connection.ts b/src/network/connection.ts index 6911e47..cb3abe4 100644 --- a/src/network/connection.ts +++ b/src/network/connection.ts @@ -1,16 +1,37 @@ -import { ClientType, Packet, AirportState, AirportObject, MultiStateUpdateItem, HEARTBEAT_INTERVAL, HEARTBEAT_TIMEOUT } from '../types'; +import { + ClientType, + Packet, + AirportState, + AirportObject, + MultiStateUpdateItem, + HEARTBEAT_INTERVAL, + HEARTBEAT_TIMEOUT, + OnlinePilot, + type JsonObject, + type JsonValue, +} from '../types'; import { AuthService } from '../services/auth'; import { VatsimService } from '../services/vatsim'; import { PointsService } from '../services/points'; import { IDService } from '../services/id'; import { DivisionService } from '../services/divisions'; import { DatabaseContextFactory } from '../services/database-context'; -import { PostHogService } from '../services/posthog'; +import { allowsLargeLightingPacket, isEuroScopeGraphPatch, MAX_LIGHTING_MESSAGE_BYTES, MAX_LIGHTING_UPDATES } from './euroscope-packets'; +import { PostHogService, type AnalyticsProperties, type AnalyticsPropertyValue } from '../services/posthog'; +import { + buildPointStateTemplate, + chunkStateUpdates, + getGeneratedAliasUpdates, + selectCanonicalEquivalentState, + type OfflineStateTemplate, +} from './point-state-equivalence'; const MAX_STATE_SIZE = 1000000; // 1MB limit for persisted payloads -const MAX_MESSAGE_SIZE = 50000; +const MAX_MESSAGE_SIZE = 64000; const MAX_SHARED_PATCH_SIZE = 10240; const MAX_MULTI_STATE_UPDATES = 200; +const MAX_REJECTED_PACKET_LOG_CHARACTERS = 4096; +const MAX_REJECTED_PACKET_TYPE_LOG_CHARACTERS = 256; const OBJECT_ID_REGEX = /^[a-zA-Z0-9_-]+$/; const STATE_FLUSH_DEBOUNCE_MS = 1500; const ACTIVE_OBJECT_TOUCH_INTERVAL_MS = 60_000; @@ -21,8 +42,11 @@ const SEND_FAILURE_LIMIT = 3; const EFFECTIVE_HEARTBEAT_TIMEOUT = Math.max(HEARTBEAT_TIMEOUT, HEARTBEAT_INTERVAL * 3); const STALE_STATE_TIMEOUT_MS = 120_000; const PACKET_DECODER = new TextDecoder(); +const PACKET_ENCODER = new TextEncoder(); const SERIALIZED_HEARTBEAT = JSON.stringify({ type: 'HEARTBEAT' }); -const VALID_PACKET_TYPES = new Set([ +const SENSITIVE_LOG_KEY_REGEX = + /^(?:authorization|proxy-authorization|cookie|set-cookie|key|api[_-]?key|token|access[_-]?token|refresh[_-]?token|secret|password)$/i; +const VALID_PACKET_TYPES = new Set([ 'HEARTBEAT', 'HEARTBEAT_ACK', 'STATE_UPDATE', @@ -35,23 +59,126 @@ const VALID_PACKET_TYPES = new Set([ 'ERROR', 'GET_STATE', 'STATE_SNAPSHOT', + 'GET_ONLINE_PILOTS', + 'ONLINE_PILOTS', 'STOPBAR_CROSSING', ]); -const createNullObject = (): Record => Object.create(null) as Record; + +type StructuredValue = JsonValue; +type StructuredObject = JsonObject; +type RuntimeCandidate = StructuredValue | undefined | ArrayBuffer | MultiStateUpdateItem | NonNullable; +type RejectedPacketLog = { + packet?: StructuredValue; + packetPreview?: string; + packetLogTruncated: boolean; +}; +type WebSocketErrorDetails = { + eventType: string; + readyState: number; + controllerId?: string; + clientType?: ClientType; + airport?: string; + message?: string; + filename?: string; + lineno?: number; + colno?: number; + error?: AnalyticsProperties | string; +}; +type SocketDebugDetails = { + connectionId?: string; + controllerId?: string; + callsign?: string; + clientType?: ClientType; + airport?: string; + readyState: number; + socketCount: number; + connectedForMs?: number; + lastMessageAgoMs?: number; + lastHeartbeatAgoMs?: number; + receivedPackets?: number; + sentPackets?: number; + receivedBytes?: number; + sentBytes?: number; + sendFailures?: number; +}; + +const createNullObject = (): StructuredObject => Object.create(null); +const createAirportObjectDictionary = (): Record => Object.create(null); const isDisallowedKey = (key: string): boolean => key === '__proto__' || key === 'constructor' || key === 'prototype'; +function isStructuredObject(value: RuntimeCandidate): value is StructuredObject { + return value !== null && value !== undefined && Object(value) === value && !Array.isArray(value); +} + +function isMergeDictionary(value: RuntimeCandidate): value is StructuredObject { + if (!isStructuredObject(value)) return false; + const prototype = Object.getPrototypeOf(value); + return prototype === Object.prototype || prototype === null; +} + +function isAnalyticsProperties(value: AnalyticsPropertyValue): value is AnalyticsProperties { + return value !== null && value !== undefined && Object(value) === value && !Array.isArray(value); +} + +function isStringValue(value: RuntimeCandidate): value is string { + return value === String(value); +} + +function isNumberValue(value: RuntimeCandidate): value is number { + return Object.prototype.toString.call(value) === '[object Number]' && (value === Number(value) || Number.isNaN(value)); +} + +function isBooleanValue(value: RuntimeCandidate): value is boolean { + return value === true || value === false; +} + +function isSupportedPacketType(value: StructuredValue): value is Packet['type'] { + return isStringValue(value) && VALID_PACKET_TYPES.has(value); +} + +function getStructuredValueKind(value: StructuredValue | undefined): string { + if (value === null) return 'null'; + if (value === undefined) return 'undefined'; + if (Array.isArray(value)) return 'array'; + if (isStringValue(value)) return 'string'; + if (isNumberValue(value)) return 'number'; + if (isBooleanValue(value)) return 'boolean'; + return 'object'; +} + type SocketInfo = { + connectionId: string; controllerId: string; + callsign: string; type: ClientType; airport: string; + connectedAt: number; lastHeartbeat: number; + lastMessageAt: number; lastStatusCheck: number; statusCheckInFlight: boolean; consecutiveVatsimFailures: number; sendFailures: number; + receivedPackets: number; + sentPackets: number; + receivedBytes: number; + sentBytes: number; }; -type OfflineStateTemplate = Array<{ id: string; state: boolean }>; +type SocketTerminationRecord = { + info: SocketInfo; + reason: string; + details: AnalyticsProperties; + clean: boolean; + startedAt: number; + completedAt?: number; + task?: Promise; +}; + +type SocketTerminationOptions = { + clean?: boolean; + details?: AnalyticsProperties; +}; type ConnectionStatus = { banned: boolean; @@ -66,55 +193,100 @@ export type ConnectionStateSnapshot = { offline: boolean; }; -function isSafeNestedValue(value: unknown, maxDepth = 20, maxProperties = 100): boolean { +function isSafeNestedValue(value: StructuredValue, maxDepth = 20, maxProperties = 100): boolean { const seen = new WeakSet(); - const walk = (current: unknown, depth: number): boolean => { - if (current === null || typeof current !== 'object') return true; + const walk = (current: StructuredValue, depth: number): boolean => { + if (!isStructuredObject(current) && !Array.isArray(current)) return true; if (depth > maxDepth || seen.has(current)) return false; seen.add(current); if (Array.isArray(current)) { return current.length <= 1000 && current.every((item) => walk(item, depth + 1)); } - const record = current as Record; - const keys = Object.keys(record); + const keys = Object.keys(current); return ( - keys.length <= maxProperties && - keys.every((key) => key.length <= 100 && !isDisallowedKey(key) && walk(record[key], depth + 1)) + keys.length <= maxProperties && keys.every((key) => key.length <= 100 && !isDisallowedKey(key) && walk(current[key], depth + 1)) ); }; return walk(value, 0); } -function describeErrorForLog(error: unknown): Record | string { - if (error instanceof Error) { +function describeErrorForLog(cause: unknown): AnalyticsProperties | string { + if (cause instanceof Error) { return { - name: error.name, - message: error.message, - stack: error.stack, + name: cause.name, + message: cause.message, + stack: cause.stack, }; } - if (error === null || error === undefined) { - return String(error); + if (cause === null || cause === undefined) { + return String(cause); } - if (typeof error === 'object') { + if (cause instanceof Object) { try { - return JSON.parse(JSON.stringify(error)) as Record; + return JSON.parse(JSON.stringify(cause)); } catch { - return Object.prototype.toString.call(error); + return Object.prototype.toString.call(cause); } } - return String(error); + return String(cause); +} + +function sanitizeForDebugLog(value: AnalyticsProperties, depth?: number, seen?: WeakSet): AnalyticsProperties; +function sanitizeForDebugLog(value: StructuredValue, depth?: number, seen?: WeakSet): StructuredValue; +function sanitizeForDebugLog( + value: AnalyticsPropertyValue, + depth?: number, + seen?: WeakSet, +): AnalyticsPropertyValue; +function sanitizeForDebugLog( + value: AnalyticsPropertyValue, + depth = 0, + seen = new WeakSet(), +): AnalyticsPropertyValue { + if (depth > 20) return '[maximum depth reached]'; + if (!isAnalyticsProperties(value) && !Array.isArray(value)) return value; + if (seen.has(value)) return '[circular]'; + seen.add(value); + + if (Array.isArray(value)) { + return value.map((item) => sanitizeForDebugLog(item, depth + 1, seen)); + } + + const sanitized: AnalyticsProperties = {}; + for (const [key, nestedValue] of Object.entries(value)) { + sanitized[key] = SENSITIVE_LOG_KEY_REGEX.test(key) ? '[redacted]' : sanitizeForDebugLog(nestedValue, depth + 1, seen); + } + return sanitized; +} + +function describeRejectedPacketForLog(packet: StructuredValue): RejectedPacketLog { + const sanitized = sanitizeForDebugLog(packet); + let serialized: string; + try { + serialized = JSON.stringify(sanitized) ?? String(sanitized); + } catch { + serialized = String(sanitized); + } + + if (serialized.length <= MAX_REJECTED_PACKET_LOG_CHARACTERS) { + return { packet: sanitized, packetLogTruncated: false }; + } + + return { + packetPreview: serialized.slice(0, MAX_REJECTED_PACKET_LOG_CHARACTERS), + packetLogTruncated: true, + }; } function describeWebSocketErrorEvent( evt: ErrorEvent, socket: WebSocket, socketInfo?: { controllerId: string; type: ClientType; airport: string }, -): Record { - const details: Record = { +): WebSocketErrorDetails { + const details: WebSocketErrorDetails = { eventType: evt.type, readyState: socket.readyState, }; @@ -135,14 +307,14 @@ function describeWebSocketErrorEvent( } // Add recursive merge utility function with safety checks -function recursivelyMergeObjects(target: unknown, source: unknown, depth = 0): unknown { +function recursivelyMergeObjects(target: StructuredValue, source: StructuredValue, depth = 0, euroScopeGraph = false): StructuredValue { // Prevent infinite recursion and overly deep nesting const MAX_DEPTH = 20; if (depth > MAX_DEPTH) { throw new Error('Maximum recursion depth exceeded in merge operation'); } - if (source === null || typeof source !== 'object') { + if (!isStructuredObject(source) && !Array.isArray(source)) { return source; // Return the source value if it's a primitive } @@ -155,17 +327,19 @@ function recursivelyMergeObjects(target: unknown, source: unknown, depth = 0): u } return [...source]; } + if (!isMergeDictionary(source)) { + throw new Error('Merge source must be a plain or null-prototype object'); + } // Handle objects - lazily clone properties when needed - const MAX_PROPERTIES = 100; + const MAX_PROPERTIES = euroScopeGraph && depth === 1 ? MAX_LIGHTING_UPDATES : 100; const sourceKeys = Object.keys(source); if (sourceKeys.length > MAX_PROPERTIES) { throw new Error(`Object has too many properties (${sourceKeys.length} > ${MAX_PROPERTIES})`); } - const targetIsObject = target !== null && typeof target === 'object' && !Array.isArray(target); - const targetRecord = targetIsObject ? (target as Record) : undefined; - let result: Record; + const targetRecord = isMergeDictionary(target) ? target : undefined; + let result: StructuredObject; let cloned = false; if (targetRecord) { @@ -183,7 +357,7 @@ function recursivelyMergeObjects(target: unknown, source: unknown, depth = 0): u }; for (const key of sourceKeys) { - if (typeof key !== 'string' || key.length > 100) { + if (key.length > 100) { throw new Error('Invalid property key'); } @@ -191,19 +365,19 @@ function recursivelyMergeObjects(target: unknown, source: unknown, depth = 0): u throw new Error('Prototype pollution key rejected'); } - const sv = (source as Record)[key]; - const rv = targetRecord ? targetRecord[key] : undefined; + const sv = source[key]; + const rv = targetRecord && Object.prototype.hasOwnProperty.call(targetRecord, key) ? targetRecord[key] : undefined; - if (sv && typeof sv === 'object' && !Array.isArray(sv)) { - if (rv && typeof rv === 'object' && !Array.isArray(rv)) { - const merged = recursivelyMergeObjects(rv, sv, depth + 1); + if (isMergeDictionary(sv)) { + if (isMergeDictionary(rv)) { + const merged = recursivelyMergeObjects(rv, sv, depth + 1, euroScopeGraph); if (merged !== rv) { ensureClone(); result[key] = merged; } } else { ensureClone(); - result[key] = recursivelyMergeObjects(createNullObject(), sv, depth + 1); + result[key] = recursivelyMergeObjects(createNullObject(), sv, depth + 1, euroScopeGraph); } } else if (sv !== rv) { ensureClone(); @@ -218,7 +392,7 @@ export class Connection { private sockets = new Map(); private airportStates = new Map(); - private airportSharedStates = new Map>(); // New shared state storage + private airportSharedStates = new Map(); // New shared state storage private objectId: string; // Store the DO's ID private lastActiveObjectsUpdate = 0; // Throttle D1 updates private activeObjectTouchInFlight = false; @@ -227,25 +401,28 @@ export class Connection { private dirtyAirportStates = new Set(); private dirtySharedStates = new Set(); private socketQueues = new Map>(); + private socketTerminations = new WeakMap(); private controllerSockets = new Map>>(); - private pilotConnectionCounts = new Map>(); + private pilotConnectionCounts = new Map>(); private pendingConnectionStatusChecks = new Map>(); private offlineStateCache = new Map< string, { template?: OfflineStateTemplate; - defaultStates?: ReadonlyMap; expiresAt: number; inFlight?: Promise; + version: number; } >(); - private connectionCounts: Record<'controllers' | 'pilots' | 'observers', number> = { + private offlineStateTemplateVersions = new Map(); + private connectionCounts = { controllers: 0, pilots: 0, observers: 0, }; private posthog: PostHogService; private lastKnownAirport = 'unknown'; + private readonly debugEnabled: boolean; constructor( private env: Env, @@ -255,18 +432,65 @@ export class Connection { ) { this.objectId = state.id.toString(); this.posthog = new PostHogService(env); + this.debugEnabled = env.DEBUG.trim().toLowerCase() === 'true'; + this.debugLog('durable_object_initialized', { + debugEnabled: this.debugEnabled, + }); this.state.blockConcurrencyWhile(async () => { await this.loadPersistedState(); }); } - private registerSocket(socket: WebSocket, info: { controllerId: string; type: ClientType; airport: string; lastHeartbeat: number }) { + private debugLog(event: string, details: AnalyticsProperties = {}) { + if (!this.debugEnabled) return; + console.log({ + level: 'debug', + subsystem: 'websocket', + event, + timestamp: new Date().toISOString(), + durableObjectId: this.objectId, + ...sanitizeForDebugLog(details), + }); + } + + private getSocketDebugDetails(socket: WebSocket, info = this.sockets.get(socket)): SocketDebugDetails { + return { + connectionId: info?.connectionId, + controllerId: info?.controllerId, + callsign: info?.callsign, + clientType: info?.type, + airport: info?.airport, + readyState: socket.readyState, + socketCount: this.sockets.size, + connectedForMs: info ? Date.now() - info.connectedAt : undefined, + lastMessageAgoMs: info ? Date.now() - info.lastMessageAt : undefined, + lastHeartbeatAgoMs: info ? Date.now() - info.lastHeartbeat : undefined, + receivedPackets: info?.receivedPackets, + sentPackets: info?.sentPackets, + receivedBytes: info?.receivedBytes, + sentBytes: info?.sentBytes, + sendFailures: info?.sendFailures, + }; + } + + private registerSocket( + socket: WebSocket, + info: { controllerId: string; callsign?: string; type: ClientType; airport: string; lastHeartbeat: number }, + ) { const socketInfo: SocketInfo = { ...info, + connectionId: crypto.randomUUID(), + callsign: info.callsign ?? info.controllerId, + connectedAt: Date.now(), + lastMessageAt: info.lastHeartbeat, lastStatusCheck: 0, statusCheckInFlight: false, consecutiveVatsimFailures: 0, sendFailures: 0, + receivedPackets: 0, + sentPackets: 0, + receivedBytes: 0, + sentBytes: 0, }; this.sockets.set(socket, socketInfo); this.adjustConnectionCount(socketInfo.type, 1); @@ -274,8 +498,9 @@ export class Connection { if (socketInfo.type === 'controller') { this.addControllerSocket(socket, socketInfo); } else if (socketInfo.type === 'pilot') { - this.adjustPilotConnectionCount(socketInfo.airport, socketInfo.controllerId, 1); + this.adjustPilotConnectionCount(socketInfo.airport, socketInfo.controllerId, socketInfo.callsign, 1); } + this.debugLog('socket_registered', this.getSocketDebugDetails(socket, socketInfo)); } private unregisterSocket(socket: WebSocket) { @@ -284,13 +509,14 @@ export class Connection { return undefined; } + this.debugLog('socket_unregistering', this.getSocketDebugDetails(socket, info)); this.adjustConnectionCount(info.type, -1); this.sockets.delete(socket); this.socketQueues.delete(socket); if (info.type === 'controller') { this.removeControllerSocket(socket, info); } else if (info.type === 'pilot') { - this.adjustPilotConnectionCount(info.airport, info.controllerId, -1); + this.adjustPilotConnectionCount(info.airport, info.controllerId, info.callsign, -1); } if (this.sockets.size === 0) { this.lastKnownAirport = 'unknown'; @@ -374,7 +600,7 @@ export class Connection { return airportControllers ? Array.from(airportControllers.keys()) : []; } - private adjustPilotConnectionCount(airport: string, pilotId: string, delta: number) { + private adjustPilotConnectionCount(airport: string, pilotId: string, callsign: string, delta: number) { let airportPilots = this.pilotConnectionCounts.get(airport); if (!airportPilots) { if (delta <= 0) return; @@ -382,8 +608,10 @@ export class Connection { this.pilotConnectionCounts.set(airport, airportPilots); } - const nextCount = (airportPilots.get(pilotId) ?? 0) + delta; - if (nextCount > 0) airportPilots.set(pilotId, nextCount); + const current = airportPilots.get(pilotId); + const nextCount = (current?.count ?? 0) + delta; + if (nextCount > 0) + airportPilots.set(pilotId, { count: nextCount, callsign: delta > 0 ? callsign : (current?.callsign ?? callsign) }); else airportPilots.delete(pilotId); if (airportPilots.size === 0) this.pilotConnectionCounts.delete(airport); } @@ -393,7 +621,29 @@ export class Connection { return airportPilots ? Array.from(airportPilots.keys()) : []; } - private emitAnalytics(event: string, properties: Record) { + private getLivePilots(airport: string): OnlinePilot[] { + const airportPilots = this.pilotConnectionCounts.get(airport); + return airportPilots ? Array.from(airportPilots, ([cid, pilot]) => ({ cid, callsign: pilot.callsign })) : []; + } + + private updatePilotCallsign(airport: string, pilotId: string, callsign: string) { + const pilot = this.pilotConnectionCounts.get(airport)?.get(pilotId); + if (pilot) pilot.callsign = callsign; + } + + private createOnlinePilotsPacket(airport: string, requestedAt: number, now = Date.now()): Packet { + return { + type: 'ONLINE_PILOTS', + airport, + data: { + pilots: this.getLivePilots(airport), + requestedAt, + }, + timestamp: now, + }; + } + + private emitAnalytics(event: string, properties: AnalyticsProperties) { const filtered = this.filterAnalyticsProperties(properties); try { this.posthog.track(event, filtered); @@ -402,8 +652,8 @@ export class Connection { } } - private filterAnalyticsProperties(properties: Record): Record { - const filtered: Record = {}; + private filterAnalyticsProperties(properties: AnalyticsProperties): AnalyticsProperties { + const filtered: AnalyticsProperties = {}; for (const [key, value] of Object.entries(properties)) { if (value !== undefined) { filtered[key] = value; @@ -412,7 +662,7 @@ export class Connection { return filtered; } - private emitAnalyticsBatch(events: readonly { event: string; properties: Record }[]) { + private emitAnalyticsBatch(events: readonly { event: string; properties: AnalyticsProperties }[]) { try { this.posthog.trackBatch( events.map((item) => ({ @@ -425,51 +675,52 @@ export class Connection { } } - private deserializeAirportState(airport: string, stored: unknown): AirportState { - const airportState = (stored || {}) as { - objects?: Record; - lastUpdate?: number; - controllers?: string[]; - }; + private deserializeAirportState(airport: string, stored: StructuredValue): AirportState { + const airportState = isStructuredObject(stored) ? stored : {}; + const storedObjects = isStructuredObject(airportState.objects) ? airportState.objects : {}; + const objects = new Map(); + for (const [id, storedObject] of Object.entries(storedObjects)) { + if (!isStructuredObject(storedObject)) continue; + const rawState = storedObject.state; + let normalizedState: boolean | StructuredObject; + if (isBooleanValue(rawState)) { + normalizedState = rawState; + } else if (isStructuredObject(rawState)) { + normalizedState = rawState; + } else { + normalizedState = {}; + } + + objects.set(id, { + id, + state: normalizedState, + controllerId: isStringValue(storedObject.controllerId) ? storedObject.controllerId : undefined, + timestamp: isNumberValue(storedObject.timestamp) ? storedObject.timestamp : Date.now(), + }); + } + + const controllers = Array.isArray(airportState.controllers) + ? airportState.controllers.filter((controller): controller is string => isStringValue(controller)) + : []; return { airport, - objects: new Map( - Object.entries(airportState.objects || {}).map(([id, obj]) => { - const rawState = obj.state; - let normalizedState: boolean | Record; - if (typeof rawState === 'boolean') { - normalizedState = rawState; - } else if (rawState && typeof rawState === 'object' && !Array.isArray(rawState)) { - normalizedState = rawState as Record; - } else { - normalizedState = {}; - } - - return [ - id, - { - id, - state: normalizedState, - controllerId: obj.controllerId, - timestamp: typeof obj.timestamp === 'number' ? obj.timestamp : Date.now(), - }, - ]; - }), - ), - lastUpdate: typeof airportState.lastUpdate === 'number' ? airportState.lastUpdate : Date.now(), - controllers: new Set(Array.isArray(airportState.controllers) ? airportState.controllers : []), + objects, + lastUpdate: isNumberValue(airportState.lastUpdate) ? airportState.lastUpdate : Date.now(), + controllers: new Set(controllers), }; } private async loadPersistedState() { const states = new Map(); - const sharedStates = new Map>(); + const sharedStates = new Map(); + const startedAt = Date.now(); + this.debugLog('persisted_state_load_started'); try { const [stateEntries, sharedEntries] = await Promise.all([ - this.state.storage.list({ prefix: 'airport_state:' }), - this.state.storage.list({ prefix: 'airport_shared_state:' }), + this.state.storage.list({ prefix: 'airport_state:' }), + this.state.storage.list({ prefix: 'airport_shared_state:' }), ]); for (const [key, stored] of stateEntries) { @@ -481,14 +732,24 @@ export class Connection { for (const [key, stored] of sharedEntries) { const airport = key.slice('airport_shared_state:'.length); if (!airport) continue; - if (stored && typeof stored === 'object') { - sharedStates.set(airport, stored as Record); + if (isStructuredObject(stored)) { + sharedStates.set(airport, stored); } } this.airportStates = states; this.airportSharedStates = sharedStates; + this.debugLog('persisted_state_load_completed', { + durationMs: Date.now() - startedAt, + airportStateCount: states.size, + sharedStateCount: sharedStates.size, + objectCount: Array.from(states.values()).reduce((total, airportState) => total + airportState.objects.size, 0), + }); } catch (error) { console.error('Failed to load persisted state:', error); + this.debugLog('persisted_state_load_failed', { + durationMs: Date.now() - startedAt, + error: describeErrorForLog(error), + }); } } @@ -498,14 +759,17 @@ export class Connection { if (!state) { try { + this.debugLog('airport_state_delete_started', { airport, storageKey }); await this.state.storage.delete(storageKey); + this.debugLog('airport_state_delete_completed', { airport, storageKey }); } catch (error) { console.error(`Failed to delete airport state for ${airport}:`, error); + this.debugLog('airport_state_delete_failed', { airport, storageKey, error: describeErrorForLog(error) }); } return; } - const objects: Record = createNullObject() as Record; + const objects = createAirportObjectDictionary(); for (const [id, object] of state.objects) { objects[id] = { id: object.id, @@ -524,16 +788,46 @@ export class Connection { try { const serializedString = JSON.stringify(serialized); + const serializedBytes = PACKET_ENCODER.encode(serializedString).byteLength; + this.debugLog('airport_state_persist_started', { + airport, + storageKey, + objectCount: state.objects.size, + controllerCount: state.controllers.size, + serializedCharacters: serializedString.length, + serializedBytes, + }); if (serializedString.length > MAX_STATE_SIZE) { console.warn( `State size (${serializedString.length}) exceeds maximum (${MAX_STATE_SIZE}), skipping persistence for ${airport}`, ); + this.debugLog('airport_state_persist_skipped_size_limit', { + airport, + storageKey, + objectCount: state.objects.size, + serializedCharacters: serializedString.length, + serializedBytes, + maxCharacters: MAX_STATE_SIZE, + }); return; } await this.state.storage.put(storageKey, serialized); + this.debugLog('airport_state_persist_completed', { + airport, + storageKey, + objectCount: state.objects.size, + serializedCharacters: serializedString.length, + serializedBytes, + }); } catch (error) { console.error(`Failed to persist airport state for ${airport}:`, error); + this.debugLog('airport_state_persist_failed', { + airport, + storageKey, + objectCount: state.objects.size, + error: describeErrorForLog(error), + }); } } @@ -587,8 +881,16 @@ export class Connection { this.airportStateFlushTimers.delete(airport); } if (!this.dirtyAirportStates.has(airport)) return; + this.debugLog('airport_state_flush_started', { + airport, + dirtyAirportCount: this.dirtyAirportStates.size, + }); this.dirtyAirportStates.delete(airport); await this.persistAirportState(airport); + this.debugLog('airport_state_flush_completed', { + airport, + becameDirtyDuringFlush: this.dirtyAirportStates.has(airport), + }); if (this.dirtyAirportStates.has(airport) && !this.airportStateFlushTimers.has(airport)) { this.markAirportStateDirty(airport); } @@ -644,9 +946,44 @@ export class Connection { } if (trackAnalytics) this.trackBroadcast(packet.type, airport, recipients); + this.debugLog('packet_broadcast_completed', { + direction: 'outbound', + packetType: packet.type, + airport, + recipients, + senderConnectionId: sender ? this.sockets.get(sender)?.connectionId : undefined, + }); return recipients; } + private createStateUpdatePackets(updates: readonly MultiStateUpdateItem[], airport: string, timestamp: number): Packet[] { + return chunkStateUpdates(updates, MAX_MULTI_STATE_UPDATES).map((chunk) => + chunk.length === 1 + ? { type: 'STATE_UPDATE', airport, data: chunk[0], timestamp } + : { type: 'MULTI_STATE_UPDATE', airport, data: { updates: chunk }, timestamp }, + ); + } + + private broadcastStateUpdates(updates: readonly MultiStateUpdateItem[], airport: string, timestamp: number, sender: WebSocket): number { + let recipients = 0; + for (const packet of this.createStateUpdatePackets(updates, airport, timestamp)) { + recipients = Math.max(recipients, this.broadcast(packet, sender, false)); + } + return recipients; + } + + private sendStateUpdatesToSocket( + socket: WebSocket, + updates: readonly MultiStateUpdateItem[], + airport: string, + timestamp: number, + ): boolean { + if (updates.length === 0) return false; + return this.createStateUpdatePackets(updates, airport, timestamp).every((packet) => + this.sendPacket(socket, packet, 'duplicate_state_alias'), + ); + } + private getOrCreateAirportState(airport: string): AirportState { let state = this.airportStates.get(airport); if (!state) { @@ -662,18 +999,11 @@ export class Connection { } private applyStateUpdateToAirport( state: AirportState, - update: Record, + update: MultiStateUpdateItem, controllerId: string, now: number, ): MultiStateUpdateItem { - if (!update || typeof update !== 'object') { - throw new Error('Invalid update data structure'); - } - - const objectId = update['objectId']; - if (!objectId || typeof objectId !== 'string') { - throw new Error('Missing or invalid objectId'); - } + const objectId = update.objectId; if (!OBJECT_ID_REGEX.test(objectId)) { throw new Error('Invalid objectId format'); @@ -686,7 +1016,7 @@ export class Connection { throw new Error(`Missing both patch and state data for object ${objectId}`); } - let newState: boolean | Record | null; + let newState: boolean | StructuredObject | null; let normalized: MultiStateUpdateItem = { objectId }; const existingObject = state.objects.get(objectId) || { @@ -697,11 +1027,11 @@ export class Connection { }; if (hasPatch) { - const patch = (update as { patch?: unknown }).patch; + const patch = update.patch; if (patch === undefined) { throw new Error(`Missing patch data for object ${objectId}`); } - if (patch !== null && (typeof patch !== 'object' || Array.isArray(patch))) { + if (patch !== null && !isStructuredObject(patch)) { throw new Error(`Patch data must be an object or null for object ${objectId}`); } @@ -711,16 +1041,19 @@ export class Connection { return { objectId, patch: null }; } - const baseState = typeof existingObject.state === 'object' && existingObject.state !== null ? existingObject.state : {}; - const merged = recursivelyMergeObjects(baseState, patch as Record); - newState = merged as Record; - normalized = { objectId, patch: patch as Record | null }; + const baseState = isStructuredObject(existingObject.state) ? existingObject.state : {}; + const merged = recursivelyMergeObjects(baseState, patch); + if (!isStructuredObject(merged)) { + throw new Error(`Patch merge did not produce an object for object ${objectId}`); + } + newState = merged; + normalized = { objectId, patch }; } else { - const stateValue = (update as { state?: unknown }).state; - if (typeof stateValue === 'boolean') { + const stateValue = update.state; + if (isBooleanValue(stateValue)) { + newState = stateValue; + } else if (isStructuredObject(stateValue)) { newState = stateValue; - } else if (stateValue && typeof stateValue === 'object' && !Array.isArray(stateValue)) { - newState = stateValue as Record; } else { throw new Error('State data must be boolean or object'); } @@ -729,7 +1062,7 @@ export class Connection { state.objects.set(objectId, { id: objectId, - state: newState as boolean | Record, + state: newState, controllerId: controllerId, timestamp: now, }); @@ -739,25 +1072,67 @@ export class Connection { return normalized; } + private applyStateUpdateToEquivalentObjects( + state: AirportState, + update: MultiStateUpdateItem, + controllerId: string, + now: number, + template: OfflineStateTemplate, + ): MultiStateUpdateItem[] { + const objectId = update.objectId; + + const equivalentIds = template.equivalentIdsById.get(objectId) ?? [objectId]; + const normalized = this.applyStateUpdateToAirport(state, update, controllerId, now); + if (equivalentIds.length === 1) return [normalized]; + + if (normalized.patch === null) { + return equivalentIds.map((equivalentId) => + equivalentId === objectId + ? normalized + : this.applyStateUpdateToAirport(state, { objectId: equivalentId, patch: null }, controllerId, now), + ); + } + + const canonicalState = state.objects.get(objectId)?.state; + if (canonicalState === undefined) { + throw new Error(`State update did not produce a value for object ${objectId}`); + } + + return equivalentIds.map((equivalentId) => + this.applyStateUpdateToAirport(state, { objectId: equivalentId, state: canonicalState }, controllerId, now), + ); + } + private async handleStateUpdate(packet: Packet, controllerId: string, connectionAirport: string) { try { - if (!packet?.data || typeof packet.data !== 'object' || Array.isArray(packet.data)) { + const update = this.parseStateUpdate(packet.data); + if (!update) { throw new Error('Invalid packet data structure'); } const airport = this.resolvePacketAirport(packet, connectionAirport); - if (!airport || typeof airport !== 'string' || airport.length === 0) { + if (!airport || airport.length === 0) { throw new Error('Invalid airport identifier'); } - const defaultStates = await this.getOfflineDefaultStates(airport); + const template = await this.getOfflineStateTemplate(airport); const now = Date.now(); const state = this.getOrCreateAirportState(airport); - const normalized = this.applyStateUpdateToAirport(state, packet.data as Record, controllerId, now); - this.pruneDefaultStateOverride(state, normalized.objectId, defaultStates); - + const normalizedUpdates = this.applyStateUpdateToEquivalentObjects( + state, + update, + controllerId, + now, + template, + ); this.markAirportStateDirty(airport); - return now; + const requestedObjectId = update.objectId; + return { + updates: normalizedUpdates, + senderUpdates: getGeneratedAliasUpdates(normalizedUpdates, new Set([requestedObjectId])), + timestamp: now, + airport, + }; } catch (error) { console.error(`State update error for controller ${controllerId}`); if (error instanceof Error) { @@ -769,33 +1144,46 @@ export class Connection { private async handleMultiStateUpdate(packet: Packet, controllerId: string, connectionAirport: string) { const airport = this.resolvePacketAirport(packet, connectionAirport); - if (!airport || typeof airport !== 'string' || airport.length === 0) { + if (!airport || airport.length === 0) { throw new Error('Invalid airport identifier'); } - const updatesPayload = this.extractMultiStateUpdates(packet.data); + const updatesPayload = this.parseMultiStateUpdates(packet.data); if (!updatesPayload || updatesPayload.length === 0) { throw new Error('Missing updates array'); } - if (updatesPayload.length > MAX_MULTI_STATE_UPDATES) { - throw new Error(`Batch update exceeds maximum allowed size of ${MAX_MULTI_STATE_UPDATES}`); + const maximumUpdates = allowsLargeLightingPacket(packet) ? MAX_LIGHTING_UPDATES : MAX_MULTI_STATE_UPDATES; + if (updatesPayload.length > maximumUpdates) { + throw new Error(`Batch update exceeds maximum allowed size of ${maximumUpdates}`); } - const updates: MultiStateUpdateItem[] = updatesPayload; + const updates = updatesPayload; - const defaultStates = await this.getOfflineDefaultStates(airport); + const template = await this.getOfflineStateTemplate(airport); const now = Date.now(); const state = this.getOrCreateAirportState(airport); const normalizedUpdates: MultiStateUpdateItem[] = []; + const senderUpdates = new Map(); for (let index = 0; index < updates.length; index++) { const update = updates[index]; try { - const normalized = this.applyStateUpdateToAirport(state, update as Record, controllerId, now); - this.pruneDefaultStateOverride(state, normalized.objectId, defaultStates); - normalizedUpdates.push(normalized); + const expandedUpdates = this.applyStateUpdateToEquivalentObjects( + state, + update, + controllerId, + now, + template, + ); + normalizedUpdates.push(...expandedUpdates); + for (const expanded of expandedUpdates) { + // An alias can overwrite an explicitly requested ID later in the same batch. + if (expanded.objectId !== update.objectId || senderUpdates.has(expanded.objectId)) { + senderUpdates.set(expanded.objectId, expanded); + } + } } catch (error) { const message = error instanceof Error ? error.message : 'Unknown error'; throw new Error(`Update ${index + 1} failed: ${message}`); @@ -804,7 +1192,12 @@ export class Connection { this.markAirportStateDirty(airport); - return { updates: normalizedUpdates, timestamp: now, airport }; + return { + updates: normalizedUpdates, + senderUpdates: [...senderUpdates.values()], + timestamp: now, + airport, + }; } private async handleControllerDisconnect(socket: WebSocket) { @@ -844,6 +1237,7 @@ export class Connection { const VATSIM_CHECK_FREQUENCY = 2; // Check VATSIM status every 2 heartbeats const interval = setInterval(async () => { + this.debugLog('heartbeat_tick', this.getSocketDebugDetails(socket)); if (socket.readyState !== WebSocket.OPEN) { clearInterval(interval); return; @@ -859,6 +1253,10 @@ export class Connection { // Check if we haven't received a heartbeat in too long const now = Date.now(); if (now - socketInfo.lastHeartbeat > EFFECTIVE_HEARTBEAT_TIMEOUT) { + this.debugLog('heartbeat_timeout', { + ...this.getSocketDebugDetails(socket, socketInfo), + effectiveTimeoutMs: EFFECTIVE_HEARTBEAT_TIMEOUT, + }); socket.close(1000, 'Heartbeat timeout'); clearInterval(interval); return; @@ -877,26 +1275,37 @@ export class Connection { this.sendSerializedPacket(socket, SERIALIZED_HEARTBEAT, 'heartbeat'); } catch (sendError) { console.error(`Failed to send heartbeat to ${socketInfo.controllerId}:`, sendError); + this.debugLog('heartbeat_send_failed', { + ...this.getSocketDebugDetails(socket, socketInfo), + error: describeErrorForLog(sendError), + }); socket.close(1011, 'Failed to send heartbeat'); clearInterval(interval); return; } } catch (e) { console.error('Error in heartbeat:', e); + this.debugLog('heartbeat_internal_error', { + ...this.getSocketDebugDetails(socket), + error: describeErrorForLog(e), + }); socket.close(1011, 'Internal error in heartbeat'); clearInterval(interval); } }, HEARTBEAT_INTERVAL); // Clean up interval on socket close - socket.addEventListener('close', () => { + socket.addEventListener('close', (event) => { + const termination = this.socketTerminations.get(socket); + const socketInfo = this.sockets.get(socket) ?? termination?.info; + this.debugLog('heartbeat_stopped_on_close', { + ...this.getSocketDebugDetails(socket, socketInfo), + code: event.code, + reason: event.reason, + wasClean: event.wasClean, + }); clearInterval(interval); }); - - // Add error handler - socket.addEventListener('error', (evt) => { - console.error('WebSocket error:', describeWebSocketErrorEvent(evt, socket, this.sockets.get(socket))); - }); } private checkSocketStatus(socket: WebSocket, socketInfo: SocketInfo, now: number): Promise | undefined { @@ -928,8 +1337,18 @@ export class Connection { } private async runSocketStatusCheck(socket: WebSocket, socketInfo: SocketInfo, now: number) { + const startedAt = Date.now(); + this.debugLog('vatsim_status_check_started', this.getSocketDebugDetails(socket, socketInfo)); try { const connectionStatus = await this.getConnectionStatus(socketInfo.controllerId); + this.debugLog('vatsim_status_check_result', { + ...this.getSocketDebugDetails(socket, socketInfo), + durationMs: Date.now() - startedAt, + banned: connectionStatus.banned, + connected: connectionStatus.status !== null, + vatsimType: connectionStatus.status?.type, + vatsimCallsign: connectionStatus.status?.callsign, + }); if (connectionStatus.banned) { this.sendPacket(socket, { type: 'ERROR', @@ -957,6 +1376,10 @@ export class Connection { } socketInfo.consecutiveVatsimFailures = 0; + if (socketInfo.type === 'pilot' && status.callsign !== socketInfo.callsign) { + socketInfo.callsign = status.callsign; + this.updatePilotCallsign(socketInfo.airport, socketInfo.controllerId, status.callsign); + } const isController = this.vatsim.isController(status); const isPilot = this.vatsim.isPilot(status); const isObserver = this.vatsim.isObserver(status); @@ -976,6 +1399,11 @@ export class Connection { } } catch (error) { console.warn('Socket status check failed (non-fatal):', error); + this.debugLog('vatsim_status_check_failed', { + ...this.getSocketDebugDetails(socket, socketInfo), + durationMs: Date.now() - startedAt, + error: describeErrorForLog(error), + }); } finally { socketInfo.statusCheckInFlight = false; } @@ -1012,60 +1440,73 @@ export class Connection { private async getOfflineStateTemplate(airport: string): Promise { const normalizedAirport = airport.toUpperCase(); + const version = this.offlineStateTemplateVersions.get(normalizedAirport) ?? 0; const cached = this.offlineStateCache.get(normalizedAirport); const now = Date.now(); - if (cached?.template && cached.expiresAt > now) { + if (cached?.version === version && cached.template && cached.expiresAt > now) { return cached.template; } - if (cached?.inFlight) { - return await cached.inFlight; + if (cached?.version === version && cached.inFlight) { + const template = await cached.inFlight; + if ((this.offlineStateTemplateVersions.get(normalizedAirport) ?? 0) !== version) { + return this.getOfflineStateTemplate(normalizedAirport); + } + return template; } const inFlight = this.loadOfflineStateTemplate(normalizedAirport); - this.offlineStateCache.set(normalizedAirport, { expiresAt: now + OFFLINE_STATE_CACHE_TTL_MS, inFlight }); + this.offlineStateCache.set(normalizedAirport, { + expiresAt: now + OFFLINE_STATE_CACHE_TTL_MS, + inFlight, + version, + }); try { const template = await inFlight; + if ((this.offlineStateTemplateVersions.get(normalizedAirport) ?? 0) !== version) { + return this.getOfflineStateTemplate(normalizedAirport); + } this.offlineStateCache.set(normalizedAirport, { template, - defaultStates: new Map(template.map((point) => [point.id, point.state])), expiresAt: Date.now() + OFFLINE_STATE_CACHE_TTL_MS, + version, }); return template; } catch (error) { console.error(`Error fetching offline state for ${airport}:`, error); - this.offlineStateCache.delete(normalizedAirport); - return []; + if (this.offlineStateCache.get(normalizedAirport)?.inFlight === inFlight) { + this.offlineStateCache.delete(normalizedAirport); + } + return { points: [], equivalentIdsById: new Map() }; } } - private async getOfflineDefaultStates(airport: string): Promise> { + async invalidatePointStateTemplate(airport: string, removedPointIds: readonly string[] = []): Promise { const normalizedAirport = airport.toUpperCase(); + const version = (this.offlineStateTemplateVersions.get(normalizedAirport) ?? 0) + 1; + this.offlineStateTemplateVersions.set(normalizedAirport, version); + this.offlineStateCache.delete(normalizedAirport); + const template = await this.getOfflineStateTemplate(normalizedAirport); - const cached = this.offlineStateCache.get(normalizedAirport); - if (cached?.template === template && cached.defaultStates) { - return cached.defaultStates; - } - return new Map(template.map((point) => [point.id, point.state])); + const state = this.airportStates.get(normalizedAirport) ?? this.airportStates.get(airport); + if (!state) return; + + let removedState = false; + for (const pointId of removedPointIds) removedState = state.objects.delete(pointId) || removedState; + if (removedState) this.markAirportStateDirty(normalizedAirport); + this.synchronizeEquivalentStateOverrides(state, template, normalizedAirport); } private async loadOfflineStateTemplate(airport: string): Promise { - // Create the necessary services to fetch points const idService = new IDService(); const divisions = new DivisionService(this.env.DB); const pointsService = new PointsService(this.env.DB, idService, divisions); - // Fetch all points for this airport - const airportPoints = await pointsService.getAirportPoints(airport); - - return airportPoints.map((point) => ({ - id: point.id, - state: point.type === 'taxiway' || point.type === 'lead_on' || point.type === 'stand', - })); + return buildPointStateTemplate(await pointsService.getAirportPoints(airport)); } private buildOfflineObjectsFromTemplate(template: OfflineStateTemplate): AirportObject[] { const timestamp = Date.now(); - return template.map((point) => ({ + return template.points.map((point) => ({ id: point.id, state: point.state, timestamp, @@ -1073,40 +1514,37 @@ export class Connection { } private async getOnlineStateObjects(airport: string, state: AirportState): Promise { - const defaultStates = await this.getOfflineDefaultStates(airport); - this.pruneDefaultStateOverrides(state, defaultStates, airport); + const template = await this.getOfflineStateTemplate(airport); + this.synchronizeEquivalentStateOverrides(state, template, airport); return Array.from(state.objects.values()); } - private pruneDefaultStateOverride(state: AirportState, objectId: string, defaultStates: ReadonlyMap) { - const object = state.objects.get(objectId); - if (!object || typeof object.state !== 'boolean') { - return; - } + private synchronizeEquivalentStateOverrides(state: AirportState, template: OfflineStateTemplate, airport: string) { + const visited = new Set(); + let changed = false; - if (defaultStates.get(objectId) === object.state) { - state.objects.delete(objectId); - } - } + for (const ids of template.equivalentIdsById.values()) { + if (visited.has(ids)) continue; + visited.add(ids); - private pruneDefaultStateOverrides(state: AirportState, defaultStates: ReadonlyMap, airport: string) { - let pruned = false; - - for (const object of state.objects.values()) { - if (typeof object.state !== 'boolean') { - continue; - } + const latest = selectCanonicalEquivalentState(ids, state.objects); + if (!latest) continue; - const defaultState = defaultStates.get(object.id); - if (defaultState === object.state) { - state.objects.delete(object.id); - pruned = true; + for (const id of ids) { + const existing = state.objects.get(id); + if ( + existing?.timestamp === latest.timestamp && + existing.controllerId === latest.controllerId && + JSON.stringify(existing.state) === JSON.stringify(latest.state) + ) { + continue; + } + state.objects.set(id, { ...latest, id }); + changed = true; } } - if (pruned) { - this.markAirportStateDirty(airport); - } + if (changed) this.markAirportStateDirty(airport); } async handleWebSocket(request: Request) { @@ -1121,25 +1559,37 @@ export class Connection { } } - const deny = async () => { + this.debugLog('connection_attempt', { + airport, + hasCredential: Boolean(apiKey), + credentialSource: url.searchParams.has('key') ? 'query' : apiKey ? 'authorization_header' : 'none', + userAgent: request.headers.get('User-Agent'), + cfRay: request.headers.get('CF-Ray'), + cfCountry: request.headers.get('CF-IPCountry'), + }); + + const deny = async (reason: string) => { + this.debugLog('connection_denied', { airport, reason }); const jitter = Math.floor(Math.random() * 30) + 20; await new Promise((r) => setTimeout(r, jitter)); return new Response('Unauthorized', { status: 401 }); }; - if (!apiKey) return await deny(); - if (!airport) return await deny(); + if (!apiKey) return await deny('missing_credential'); + if (!airport) return await deny('missing_airport'); const { user, banned } = await this.auth.getConnectionPrincipalByApiKey(apiKey); - if (!user) return await deny(); + if (!user) return await deny('invalid_credential'); // Ban enforcement: deny connection if banned if (banned) { + this.debugLog('connection_denied', { airport, controllerId: user.vatsim_id, reason: 'banned' }); return new Response('Banned', { status: 403 }); } const status = await this.vatsim.getUserStatus(user.vatsim_id); if (!status) { + this.debugLog('connection_denied', { airport, controllerId: user.vatsim_id, reason: 'not_connected_to_vatsim' }); return new Response('User not connected to VATSIM', { status: 403 }); } // Auto-determine client type based on VATSIM status @@ -1157,10 +1607,15 @@ export class Connection { // Initialize socket info with the airport and heartbeat this.registerSocket(server, { controllerId: user.vatsim_id, + callsign: status.callsign, type: clientType, airport: airport, lastHeartbeat: Date.now(), }); + this.debugLog('connection_accepted', { + ...this.getSocketDebugDetails(server), + vatsimType: status.type, + }); // Start heartbeat mechanism this.startHeartbeat(server); @@ -1223,6 +1678,7 @@ export class Connection { } server.addEventListener('message', (event) => { + const receivedAt = Date.now(); this.state.waitUntil( this.enqueueSocketTask(server, async () => { const socketInfo = this.sockets.get(server); @@ -1234,49 +1690,93 @@ export class Connection { try { // Parse and validate message data let rawData: string; - if (typeof event.data === 'string') { + let rawBytes: number; + if (isStringValue(event.data)) { rawData = event.data; + rawBytes = PACKET_ENCODER.encode(rawData).byteLength; } else { - if (event.data.byteLength > MAX_MESSAGE_SIZE) { - throw new Error(`Message size exceeds maximum allowed size of ${MAX_MESSAGE_SIZE} characters`); + if (event.data.byteLength > MAX_LIGHTING_MESSAGE_BYTES) { + throw new Error(`Message size exceeds maximum allowed size of ${MAX_LIGHTING_MESSAGE_BYTES} bytes`); } try { rawData = PACKET_DECODER.decode(event.data); + rawBytes = event.data.byteLength; } catch { throw new Error('Failed to decode message data'); } } // Validate message size - if (rawData.length > MAX_MESSAGE_SIZE) { - throw new Error(`Message size exceeds maximum allowed size of ${MAX_MESSAGE_SIZE} characters`); + if (rawBytes > MAX_LIGHTING_MESSAGE_BYTES) { + throw new Error(`Message size exceeds maximum allowed size of ${MAX_LIGHTING_MESSAGE_BYTES} bytes`); } // Parse JSON with error handling - let packet: unknown; + let parsedPacket: StructuredValue; try { - packet = JSON.parse(rawData); + parsedPacket = JSON.parse(rawData); } catch { throw new Error('Invalid JSON format'); } // Validate packet structure - if (!this.validatePacket(packet)) { + if (rawBytes > MAX_MESSAGE_SIZE && !allowsLargeLightingPacket(parsedPacket)) { + throw new Error(`Message size exceeds maximum allowed size of ${MAX_MESSAGE_SIZE} bytes`); + } + const validationFailure = this.getPacketValidationFailure(parsedPacket); + if (validationFailure) { + const packetTypeValue = isStructuredObject(parsedPacket) ? parsedPacket.type : undefined; + const packetType = + isStringValue(packetTypeValue) + ? packetTypeValue.slice(0, MAX_REJECTED_PACKET_TYPE_LOG_CHARACTERS) + : undefined; + console.warn({ + level: 'warn', + subsystem: 'websocket', + event: 'packet_rejected', + timestamp: new Date().toISOString(), + durableObjectId: this.objectId, + ...this.getSocketDebugDetails(server, socketInfo), + direction: 'inbound', + validationFailure, + packetType, + packetTypeKind: getStructuredValueKind(packetTypeValue), + packetTypeLogTruncated: + isStringValue(packetTypeValue) && packetTypeValue.length > MAX_REJECTED_PACKET_TYPE_LOG_CHARACTERS, + bytes: rawBytes, + characters: rawData.length, + queuedForMs: Date.now() - receivedAt, + ...describeRejectedPacketForLog(parsedPacket), + }); throw new Error('Invalid packet structure or type'); } + // The same JSON text was validated above before it is decoded into the domain packet contract. + const packet: Packet = JSON.parse(rawData); const now = Date.now(); + socketInfo.receivedPackets++; + socketInfo.receivedBytes += rawBytes; + socketInfo.lastMessageAt = now; // Update last heartbeat time for any message received socketInfo.lastHeartbeat = now; + this.debugLog('packet_received', { + ...this.getSocketDebugDetails(server, socketInfo), + direction: 'inbound', + sequence: socketInfo.receivedPackets, + bytes: rawBytes, + characters: rawData.length, + queuedForMs: now - receivedAt, + packetType: packet.type, + packet: parsedPacket, + }); // Update object status on each message to keep last_updated current (non-fatal on failure) this.touchActiveObjectStatus(); const statusCheck = this.checkSocketStatus(server, socketInfo, now); if (statusCheck) this.state.waitUntil(statusCheck); - const packetAirport = this.resolvePacketAirport(packet as Packet, socketInfo.airport); - // Handle different packet types - switch ((packet as Packet).type) { + const packetAirport = this.resolvePacketAirport(packet, socketInfo.airport); + switch (packet.type) { case 'HEARTBEAT': // Respond to heartbeat with acknowledgment, adding server timestamp this.sendPacket(server, { @@ -1295,13 +1795,12 @@ export class Connection { throw new Error('Only pilot clients can send STOPBAR_CROSSING'); } - const p = packet as Packet; const airport = socketInfo.airport; - if (!p.data || typeof p.data !== 'object' || Array.isArray(p.data)) { + if (!packet.data || !isStructuredObject(packet.data)) { throw new Error('Invalid payload for STOPBAR_CROSSING'); } - const objectId = (p.data as { objectId?: string }).objectId; - if (!objectId) { + const objectId = packet.data.objectId; + if (!isStringValue(objectId)) { throw new Error('objectId is required'); } @@ -1317,14 +1816,17 @@ export class Connection { }; const recipients = this.broadcastToControllers(broadcastPacket, server, false); - this.trackMessage({ - clientType, - messageType: 'STOPBAR_CROSSING', - airport, - meta: { - objectId, + this.trackMessage( + { + clientType, + messageType: 'STOPBAR_CROSSING', + airport, + meta: { + objectId, + }, }, - }, recipients); + recipients, + ); break; } @@ -1354,7 +1856,7 @@ export class Connection { sharedState: this.getSharedStateSnapshot(airport), controllers: this.getLiveControllerIds(airport), offline, - requestedAt: (packet as Packet).timestamp || now, + requestedAt: packet.timestamp || now, }, timestamp: Date.now(), }; @@ -1362,6 +1864,15 @@ export class Connection { break; } + case 'GET_ONLINE_PILOTS': { + this.sendPacket( + server, + this.createOnlinePilotsPacket(packetAirport, packet.timestamp ?? now, now), + 'online_pilots', + ); + break; + } + case 'MULTI_STATE_UPDATE': if (clientType === 'pilot') { throw new Error('Pilots cannot send state updates'); @@ -1371,24 +1882,22 @@ export class Connection { } try { - const { updates, timestamp, airport } = await this.handleMultiStateUpdate( - packet as Packet, + const { updates, senderUpdates, timestamp, airport } = await this.handleMultiStateUpdate( + packet, user.vatsim_id, packetAirport, ); - const broadcastPacket: Packet = { - type: 'MULTI_STATE_UPDATE', - airport, - data: { updates }, - timestamp, - }; - const recipients = this.broadcast(broadcastPacket, server, false); - this.trackMessage({ - clientType, - messageType: 'MULTI_STATE_UPDATE', - airport: socketInfo.airport, - meta: { count: updates.length }, - }, recipients); + let recipients = this.broadcastStateUpdates(updates, airport, timestamp, server); + if (this.sendStateUpdatesToSocket(server, senderUpdates, airport, timestamp)) recipients += 1; + this.trackMessage( + { + clientType, + messageType: 'MULTI_STATE_UPDATE', + airport: socketInfo.airport, + meta: { count: updates.length }, + }, + recipients, + ); } catch (updateError) { throw new Error( `State batch update failed: ${updateError instanceof Error ? updateError.message : String(updateError)}`, @@ -1405,28 +1914,31 @@ export class Connection { } try { - const timestamp = await this.handleStateUpdate(packet as Packet, user.vatsim_id, packetAirport); - const broadcastPacket = { - ...(packet as Packet), - airport: packetAirport, - timestamp, - }; - const recipients = this.broadcast(broadcastPacket, server, false); - const data = ((packet as Packet).data || {}) as Record; - const patchValue = data.patch as unknown; - const meta: Record = { - objectId: typeof data.objectId === 'string' ? data.objectId : undefined, + const { updates, senderUpdates, timestamp, airport } = await this.handleStateUpdate( + packet, + user.vatsim_id, + packetAirport, + ); + let recipients = this.broadcastStateUpdates(updates, airport, timestamp, server); + if (this.sendStateUpdatesToSocket(server, senderUpdates, airport, timestamp)) recipients += 1; + const data = isStructuredObject(packet.data) ? packet.data : {}; + const patchValue = data.patch; + const meta: AnalyticsProperties = { + objectId: isStringValue(data.objectId) ? data.objectId : undefined, updateMode: patchValue !== undefined ? 'patch' : 'state', }; - if (patchValue && typeof patchValue === 'object' && !Array.isArray(patchValue)) { - meta.patchKeys = Object.keys(patchValue as Record).length; + if (isStructuredObject(patchValue)) { + meta.patchKeys = Object.keys(patchValue).length; } - this.trackMessage({ - clientType, - messageType: 'STATE_UPDATE', - airport: socketInfo.airport, - meta, - }, recipients); + this.trackMessage( + { + clientType, + messageType: updates.length === 1 ? 'STATE_UPDATE' : 'MULTI_STATE_UPDATE', + airport: socketInfo.airport, + meta: { ...meta, equivalentObjectCount: updates.length }, + }, + recipients, + ); } catch (updateError) { throw new Error( `State update failed: ${updateError instanceof Error ? updateError.message : String(updateError)}`, @@ -1436,13 +1948,14 @@ export class Connection { case 'CLOSE': { // Handle graceful disconnection - if (clientType === 'controller') { - await this.handleControllerDisconnect(server); - } - const removed = this.unregisterSocket(server); - if (removed) { - await this.trackDisconnection(removed, 'client_close'); - } + await this.cleanupSocket(server, 'client_close', { + clean: true, + details: { + code: 1000, + reason: 'Client requested disconnection', + initiatedBy: 'client_packet', + }, + }); server.close(1000, 'Client requested disconnection'); break; } @@ -1454,7 +1967,7 @@ export class Connection { } try { - await this.handleSharedStateUpdate(packet as Packet, user.vatsim_id, packetAirport); + await this.handleSharedStateUpdate(packet, user.vatsim_id, packetAirport); } catch (updateError) { throw new Error( `Shared state update failed: ${updateError instanceof Error ? updateError.message : String(updateError)}`, @@ -1466,9 +1979,23 @@ export class Connection { // Reject unknown packet types throw new Error(`Unknown packet type: ${packet.type}`); } + this.debugLog('packet_handled', { + ...this.getSocketDebugDetails(server, socketInfo), + direction: 'inbound', + sequence: socketInfo.receivedPackets, + packetType: packet.type, + processingMs: Date.now() - now, + totalLatencyMs: Date.now() - receivedAt, + }); } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred'; console.error(`Message handling error for ${socketInfo.controllerId}:`, errorMessage); + this.debugLog('packet_handling_failed', { + ...this.getSocketDebugDetails(server, socketInfo), + direction: 'inbound', + queuedForMs: Date.now() - receivedAt, + error: describeErrorForLog(err), + }); // Send error response to client if ( @@ -1485,12 +2012,50 @@ export class Connection { ); }); - server.addEventListener('close', () => { - this.state.waitUntil(this.handleSocketTermination(server, 'close_event')); + server.addEventListener('close', (event) => { + const termination = this.socketTerminations.get(server); + const socketInfo = this.sockets.get(server) ?? termination?.info; + this.debugLog('socket_close_event', { + ...this.getSocketDebugDetails(server, socketInfo), + code: event.code, + reason: event.reason, + wasClean: event.wasClean, + }); + this.state.waitUntil( + this.handleSocketTermination(server, 'close_event', { + clean: event.wasClean, + details: { + code: event.code, + reason: event.reason, + wasClean: event.wasClean, + }, + }), + ); }); - server.addEventListener('error', () => { - this.state.waitUntil(this.handleSocketTermination(server, 'socket_error')); + server.addEventListener('error', (event) => { + const termination = this.socketTerminations.get(server); + const socketInfo = this.sockets.get(server); + const errorDetails = describeWebSocketErrorEvent(event, server, socketInfo ?? termination?.info); + if (termination || !socketInfo) { + this.debugLog('socket_error_after_termination_ignored', { + ...this.getSocketDebugDetails(server, termination?.info), + originalReason: termination?.reason, + terminationCompleted: termination?.completedAt !== undefined, + error: errorDetails, + }); + return; + } + console.error('WebSocket error:', errorDetails); + this.debugLog('socket_error_event', { + ...this.getSocketDebugDetails(server, socketInfo), + error: errorDetails, + }); + this.state.waitUntil( + this.handleSocketTermination(server, 'socket_error', { + details: { error: errorDetails }, + }), + ); }); return new Response(null, { status: 101, webSocket: client }); @@ -1506,6 +2071,10 @@ export class Connection { }) .catch((error) => { console.error('Queued socket task failed:', error instanceof Error ? error.message : error); + this.debugLog('socket_queue_task_failed', { + ...this.getSocketDebugDetails(socket), + error: describeErrorForLog(error), + }); }) .finally(() => { if (this.socketQueues.get(socket) === next) { @@ -1528,6 +2097,12 @@ export class Connection { private sendSerializedPacket(socket: WebSocket, packetString: string, context: string): boolean { if (socket.readyState !== WebSocket.OPEN) { + this.debugLog('packet_send_skipped_socket_not_open', { + ...this.getSocketDebugDetails(socket), + direction: 'outbound', + context, + bytes: PACKET_ENCODER.encode(packetString).byteLength, + }); return false; } try { @@ -1535,6 +2110,27 @@ export class Connection { const info = this.sockets.get(socket); if (info) { info.sendFailures = 0; + info.sentPackets++; + const bytes = PACKET_ENCODER.encode(packetString).byteLength; + info.sentBytes += bytes; + let packet: StructuredValue = packetString; + if (this.debugEnabled) { + try { + packet = JSON.parse(packetString); + } catch { + // Keep the serialized value for diagnostics if a non-JSON frame is ever sent. + } + } + this.debugLog('packet_sent', { + ...this.getSocketDebugDetails(socket, info), + direction: 'outbound', + context, + sequence: info.sentPackets, + bytes, + characters: packetString.length, + packetType: isStructuredObject(packet) ? packet.type : undefined, + packet, + }); } return true; } catch (error) { @@ -1543,9 +2139,15 @@ export class Connection { } } - private handleSendFailure(socket: WebSocket, context: string, error: unknown) { + private handleSendFailure(socket: WebSocket, context: string, cause: unknown) { const info = this.sockets.get(socket); - console.error(`Failed to send WebSocket packet during ${context}:`, error); + console.error(`Failed to send WebSocket packet during ${context}:`, cause); + this.debugLog('packet_send_failed', { + ...this.getSocketDebugDetails(socket, info), + direction: 'outbound', + context, + error: describeErrorForLog(cause), + }); if (!info) return; info.sendFailures++; @@ -1555,44 +2157,105 @@ export class Connection { } } - private async cleanupSocket(socket: WebSocket, reason: string) { + private async cleanupSocket(socket: WebSocket, reason: string, options: SocketTerminationOptions = {}) { + await this.terminateSocket(socket, reason, options); + } + + private async handleSocketTermination( + socket: WebSocket, + reason: 'close_event' | 'socket_error', + options: SocketTerminationOptions = {}, + ) { + await this.terminateSocket(socket, reason, options); + } + + private async terminateSocket(socket: WebSocket, reason: string, options: SocketTerminationOptions = {}) { + const existing = this.socketTerminations.get(socket); + if (existing) { + existing.clean ||= options.clean ?? false; + existing.details = { ...existing.details, ...options.details }; + this.debugLog( + existing.completedAt === undefined ? 'socket_termination_already_in_progress' : 'socket_termination_already_handled', + { + ...this.getSocketDebugDetails(socket, existing.info), + originalReason: existing.reason, + repeatedReason: reason, + clean: existing.clean, + ...existing.details, + }, + ); + if (existing.task) await existing.task; + return; + } + const info = this.sockets.get(socket); - if (!info) return; + if (!info) { + this.debugLog('socket_termination_ignored_unregistered', { + ...this.getSocketDebugDetails(socket), + reason, + clean: options.clean ?? false, + ...options.details, + }); + return; + } + + const record: SocketTerminationRecord = { + info, + reason, + details: options.details ?? {}, + clean: options.clean ?? false, + startedAt: Date.now(), + }; + this.socketTerminations.set(socket, record); + record.task = this.performSocketTermination(socket, record); + await record.task; + } + + private async performSocketTermination(socket: WebSocket, record: SocketTerminationRecord) { + const { info } = record; + this.debugLog('socket_termination_started', { + ...this.getSocketDebugDetails(socket, info), + reason: record.reason, + clean: record.clean, + ...record.details, + }); if (info.type === 'controller') { try { await this.handleControllerDisconnect(socket); } catch (error) { - console.warn('handleControllerDisconnect failed during cleanup (non-fatal):', error); + console.warn('handleControllerDisconnect failed during socket termination (non-fatal):', error); + this.debugLog('controller_disconnect_cleanup_failed', { + ...this.getSocketDebugDetails(socket, info), + reason: record.reason, + error: describeErrorForLog(error), + }); } } const removed = this.unregisterSocket(socket); - if (!removed) return; - - try { - await this.trackDisconnection(removed, reason); - } catch (error) { - console.warn('trackDisconnection failed during cleanup (non-fatal):', error); - } - } - - private async handleSocketTermination(socket: WebSocket, reason: 'close_event' | 'socket_error') { - const info = this.sockets.get(socket); - if (info?.type === 'controller') { + if (removed) { try { - await this.handleControllerDisconnect(socket); + await this.trackDisconnection(removed, record.reason); } catch (error) { - console.warn('handleControllerDisconnect failed during socket termination (non-fatal):', error); + console.warn('trackDisconnection failed during socket termination (non-fatal):', error); + this.debugLog('disconnection_tracking_failed', { + ...this.getSocketDebugDetails(socket, removed), + reason: record.reason, + error: describeErrorForLog(error), + }); } } - const removed = this.unregisterSocket(socket); - if (!removed) return; - try { - await this.trackDisconnection(removed, reason); - } catch (error) { - console.warn('trackDisconnection failed during socket termination (non-fatal):', error); - } + + record.completedAt = Date.now(); + this.debugLog('socket_termination_completed', { + ...this.getSocketDebugDetails(socket, info), + reason: record.reason, + clean: record.clean, + ...record.details, + durationMs: record.completedAt - record.startedAt, + socketCount: this.sockets.size, + }); } private touchActiveObjectStatus() { @@ -1699,13 +2362,16 @@ export class Connection { } } - private trackMessage(details: { - clientType: ClientType; - messageType: Packet['type']; - airport: string; - meta?: Record; - }, broadcastRecipients = 0) { - const props: Record = { + private trackMessage( + details: { + clientType: ClientType; + messageType: Packet['type']; + airport: string; + meta?: StructuredObject; + }, + broadcastRecipients = 0, + ) { + const props = { airport: details.airport, clientType: details.clientType, messageType: details.messageType, @@ -1791,7 +2457,7 @@ export class Connection { return recipients; } - private getOrCreateSharedState(airport: string): Record { + private getOrCreateSharedState(airport: string): StructuredObject { let sharedState = this.airportSharedStates.get(airport); if (!sharedState) { sharedState = {}; // Initialize as empty object as per requirements @@ -1802,21 +2468,24 @@ export class Connection { private async handleSharedStateUpdate(packet: Packet, controllerId: string, connectionAirport: string) { try { // Validate required fields - if (!packet?.data || typeof packet.data !== 'object' || Array.isArray(packet.data)) { + if (!packet?.data || !isStructuredObject(packet.data)) { throw new Error('Invalid packet data structure'); } - if (!packet.data.sharedStatePatch || typeof packet.data.sharedStatePatch !== 'object' || Array.isArray(packet.data.sharedStatePatch)) { + if ( + !packet.data.sharedStatePatch || + !isStructuredObject(packet.data.sharedStatePatch) + ) { throw new Error('Missing or invalid sharedStatePatch'); } // Validate airport parameter const airport = this.resolvePacketAirport(packet, connectionAirport); - if (!airport || typeof airport !== 'string' || airport.length === 0) { + if (!airport || airport.length === 0) { throw new Error('Invalid airport identifier'); } - const patch = packet.data.sharedStatePatch as Record; + const patch = packet.data.sharedStatePatch; const patchKeyCount = Object.keys(patch).length; let patchSize = 0; let serializedPatch = ''; @@ -1824,9 +2493,10 @@ export class Connection { // Validate patch structure and size try { serializedPatch = JSON.stringify(patch); - patchSize = serializedPatch.length; - if (patchSize > MAX_SHARED_PATCH_SIZE) { - throw new Error(`Patch size exceeds maximum allowed size of ${MAX_SHARED_PATCH_SIZE} characters`); + patchSize = PACKET_ENCODER.encode(serializedPatch).byteLength; + const sizeLimit = isEuroScopeGraphPatch(patch) ? MAX_LIGHTING_MESSAGE_BYTES : MAX_SHARED_PATCH_SIZE; + if (patchSize > sizeLimit) { + throw new Error(`Patch size exceeds maximum allowed size of ${sizeLimit} bytes`); } } catch { throw new Error('Patch data is not serializable'); @@ -1836,25 +2506,34 @@ export class Connection { const currentState = this.getOrCreateSharedState(airport); // Apply recursive merge with error handling - const updatedState = recursivelyMergeObjects(currentState, patch); + const updatedState = recursivelyMergeObjects(currentState, patch, 0, isEuroScopeGraphPatch(patch)); // Update the stored state - this.airportSharedStates.set(airport, updatedState as Record); + if (!isStructuredObject(updatedState)) { + throw new Error('Shared-state merge did not produce an object'); + } + if (PACKET_ENCODER.encode(JSON.stringify(updatedState)).byteLength > MAX_STATE_SIZE) { + throw new Error('Merged shared state exceeds the supported persistence size.'); + } + this.airportSharedStates.set(airport, updatedState); this.markSharedStateDirty(airport); // Broadcast to all clients (including sender) const recipients = this.broadcastSharedState(airport, serializedPatch, controllerId); - this.trackMessage({ - clientType: 'controller', - messageType: 'SHARED_STATE_UPDATE', - airport, - meta: { - patchKeys: patchKeyCount, - patchSize, + this.trackMessage( + { + clientType: 'controller', + messageType: 'SHARED_STATE_UPDATE', + airport, + meta: { + patchKeys: patchKeyCount, + patchSize, + }, }, - }, recipients); + recipients, + ); return updatedState; } catch (error) { @@ -1877,172 +2556,134 @@ export class Connection { return recipients; } - private getSharedStateSnapshot(airport: string): Record { + private getSharedStateSnapshot(airport: string): StructuredObject { return this.getOrCreateSharedState(airport); } - private validatePacket(packet: unknown): packet is Packet { + private getPacketValidationFailure(packet: StructuredValue): string | null { // Basic structure validation - if (!packet || typeof packet !== 'object') { - return false; + if (!isStructuredObject(packet)) { + return 'packet_not_object'; } - const obj = packet as Record; - const type = obj['type']; + const type = packet.type; // Required type field - if (typeof type !== 'string') { - return false; + if (!isStringValue(type)) { + return 'missing_or_non_string_type'; } - if (!VALID_PACKET_TYPES.has(type as Packet['type'])) { - return false; + if (!isSupportedPacketType(type)) { + return 'unsupported_type'; } // Optional airport field validation - const airport = obj['airport']; - if (airport !== undefined && (typeof airport !== 'string' || airport.length === 0)) { - return false; + const airport = packet.airport; + if (airport !== undefined && (!isStringValue(airport) || airport.length === 0)) { + return 'invalid_airport'; } // Optional timestamp validation - const timestamp = obj['timestamp']; - if (timestamp !== undefined && (typeof timestamp !== 'number' || timestamp < 0)) { - return false; + const timestamp = packet.timestamp; + if (timestamp !== undefined && (!isNumberValue(timestamp) || timestamp < 0)) { + return 'invalid_timestamp'; } // The raw message length and JSON parse are checked before this method. switch (type) { case 'STATE_UPDATE': - return this.validateStateUpdatePacket(packet); + return this.validateStateUpdatePacket(packet) ? null : 'invalid_state_update_payload'; case 'MULTI_STATE_UPDATE': - return this.validateMultiStateUpdatePacket(packet); + return this.validateMultiStateUpdatePacket(packet) ? null : 'invalid_multi_state_update_payload'; case 'SHARED_STATE_UPDATE': - return this.validateSharedStateUpdatePacket(packet); + return this.validateSharedStateUpdatePacket(packet) ? null : 'invalid_shared_state_update_payload'; case 'STOPBAR_CROSSING': - return this.validateStopbarCrossingPacket(packet); + return this.validateStopbarCrossingPacket(packet) ? null : 'invalid_stopbar_crossing_payload'; default: - return true; // Other types are valid if they pass basic checks + return null; // Other types are valid if they pass basic checks } } - private validateStateUpdatePacket(packet: unknown): boolean { - const obj = packet as { data?: unknown }; - if (!obj.data || typeof obj.data !== 'object' || Array.isArray(obj.data)) { - return false; - } + private validateStateUpdatePacket(packet: StructuredValue): boolean { + return isStructuredObject(packet) && this.parseStateUpdate(packet.data) !== null; + } - const data = obj.data as Record; - // Must have objectId - if (!data.objectId || typeof data.objectId !== 'string' || !OBJECT_ID_REGEX.test(data.objectId)) { - return false; - } + private validateMultiStateUpdatePacket(packet: StructuredValue): boolean { + if (!isStructuredObject(packet)) return false; + const updates = this.parseMultiStateUpdates(packet.data); - // Must have either patch or state - if (data.patch === undefined && data.state === undefined) { + if (!updates || updates.length === 0) { return false; } - if ( - data.patch !== undefined && - (data.patch === null || (typeof data.patch === 'object' && !Array.isArray(data.patch))) && - !isSafeNestedValue(data.patch) - ) { - return false; - } - if (data.patch !== undefined && data.patch !== null && (typeof data.patch !== 'object' || Array.isArray(data.patch))) { + const maximumUpdates = allowsLargeLightingPacket(packet) ? MAX_LIGHTING_UPDATES : MAX_MULTI_STATE_UPDATES; + if (updates.length > maximumUpdates) { return false; } - if ( - data.state !== undefined && - typeof data.state !== 'boolean' && - (typeof data.state !== 'object' || data.state === null || Array.isArray(data.state)) - ) { - return false; - } - if (data.state !== undefined && !isSafeNestedValue(data.state)) return false; return true; } - private validateMultiStateUpdatePacket(packet: unknown): boolean { - const obj = packet as { data?: unknown }; - const updates = this.extractMultiStateUpdates(obj.data); - - if (!updates || updates.length === 0) { - return false; - } - - if (updates.length > MAX_MULTI_STATE_UPDATES) { + private validateSharedStateUpdatePacket(packet: StructuredValue): boolean { + if (!isStructuredObject(packet) || !isStructuredObject(packet.data)) return false; + // Must have sharedStatePatch + if (!isStructuredObject(packet.data.sharedStatePatch)) { return false; } - const typedUpdates: MultiStateUpdateItem[] = updates; - - for (const update of typedUpdates) { - if (!update || typeof update !== 'object') return false; - const data = update as Record; - if (!data.objectId || typeof data.objectId !== 'string') return false; - if (!OBJECT_ID_REGEX.test(data.objectId)) return false; - if (data.patch === undefined && data.state === undefined) return false; - if (data.patch !== undefined && data.patch !== null && (typeof data.patch !== 'object' || Array.isArray(data.patch))) { - return false; - } - if ( - data.state !== undefined && - typeof data.state !== 'boolean' && - (typeof data.state !== 'object' || data.state === null || Array.isArray(data.state)) - ) { - return false; - } - if (data.patch !== undefined && !isSafeNestedValue(data.patch)) return false; - if (data.state !== undefined && !isSafeNestedValue(data.state)) return false; - } - + if (!isSafeNestedValue(packet.data.sharedStatePatch) && !isEuroScopeGraphPatch(packet.data.sharedStatePatch)) return false; return true; } - private validateSharedStateUpdatePacket(packet: unknown): boolean { - const obj = packet as { data?: unknown }; - if (!obj.data || typeof obj.data !== 'object' || Array.isArray(obj.data)) { - return false; - } - - const data = obj.data as Record; - // Must have sharedStatePatch - if (!data.sharedStatePatch || typeof data.sharedStatePatch !== 'object' || Array.isArray(data.sharedStatePatch)) { + private validateStopbarCrossingPacket(packet: StructuredValue): boolean { + if (!isStructuredObject(packet) || !isStructuredObject(packet.data)) return false; + // Must have objectId + if (!isStringValue(packet.data.objectId) || !OBJECT_ID_REGEX.test(packet.data.objectId)) { return false; } - if (!isSafeNestedValue(data.sharedStatePatch)) return false; return true; } - private validateStopbarCrossingPacket(packet: unknown): boolean { - const obj = packet as { data?: unknown }; - if (!obj.data || typeof obj.data !== 'object' || Array.isArray(obj.data)) { - return false; + private parseStateUpdate(data: RuntimeCandidate): MultiStateUpdateItem | null { + if (!isStructuredObject(data)) return null; + + const objectId = data.objectId; + if (!isStringValue(objectId) || !OBJECT_ID_REGEX.test(objectId)) return null; + + const hasPatch = data.patch !== undefined; + const hasState = data.state !== undefined; + if (!hasPatch && !hasState) return null; + + const parsed: MultiStateUpdateItem = { objectId }; + if (hasPatch) { + const patch = data.patch; + if (patch !== null && !isStructuredObject(patch)) return null; + if (!isSafeNestedValue(patch)) return null; + parsed.patch = patch; } - const data = obj.data as Record; - // Must have objectId - if (!data.objectId || typeof data.objectId !== 'string' || !OBJECT_ID_REGEX.test(data.objectId)) { - return false; + if (hasState) { + const state = data.state; + if (!isBooleanValue(state) && !isStructuredObject(state)) return null; + if (!isSafeNestedValue(state)) return null; + parsed.state = state; } - return true; + return parsed; } - // Extracts a typed updates array from either a bare array payload or an object with `updates` - private extractMultiStateUpdates(data: unknown): MultiStateUpdateItem[] | null { - if (Array.isArray(data)) { - return data as MultiStateUpdateItem[]; - } - if (data && typeof data === 'object' && !Array.isArray(data)) { - const maybeUpdates = (data as { updates?: unknown }).updates; - if (Array.isArray(maybeUpdates)) { - return maybeUpdates as MultiStateUpdateItem[]; - } + // Parses updates from either the legacy bare array payload or an object with `updates`. + private parseMultiStateUpdates(data: RuntimeCandidate): MultiStateUpdateItem[] | null { + const embeddedUpdates = isStructuredObject(data) && 'updates' in data ? data.updates : undefined; + const rawUpdates = Array.isArray(data) ? data : Array.isArray(embeddedUpdates) ? embeddedUpdates : null; + if (!rawUpdates) return null; + + const updates: MultiStateUpdateItem[] = []; + for (const rawUpdate of rawUpdates) { + const update = this.parseStateUpdate(rawUpdate); + if (!update) return null; + updates.push(update); } - return null; + return updates; } } diff --git a/src/network/euroscope-packets.ts b/src/network/euroscope-packets.ts new file mode 100644 index 0000000..f6d3217 --- /dev/null +++ b/src/network/euroscope-packets.ts @@ -0,0 +1,61 @@ +export const MAX_LIGHTING_MESSAGE_BYTES = 1_000_000; +export const MAX_LIGHTING_UPDATES = 10_000; + +function dictionary(value: unknown): value is Record { + return value !== null && typeof value === 'object' && !Array.isArray(value); +} + +function identifier(value: unknown): value is string { + return typeof value === 'string' && value.length > 0 && value.length <= 100 && + value !== '__proto__' && value !== 'constructor' && value !== 'prototype'; +} + +// EuroScope serializes a complete profile as flat node/block dictionaries, not arbitrary nested JSON. +export function isEuroScopeGraphPatch(value: unknown): boolean { + if (!dictionary(value) || !dictionary(value.nodes) || !dictionary(value.blocks)) return false; + if (Object.keys(value).some((key) => !['meta', 'profile', 'nodes', 'blocks'].includes(key))) return false; + if (value.profile !== undefined && !identifier(value.profile)) return false; + if (value.meta !== undefined && value.meta !== null) { + if (!dictionary(value.meta) || Object.keys(value.meta).length !== 2) return false; + for (const key of ['client', 'serial']) { + const number = value.meta[key]; + if (typeof number !== 'number' || !Number.isFinite(number) || !Number.isInteger(number) || number < 0) return false; + } + } + const nodes = Object.entries(value.nodes); + const blocks = Object.entries(value.blocks); + if (nodes.length + blocks.length > MAX_LIGHTING_UPDATES) return false; + if (!nodes.every(([key, state]) => identifier(key) && typeof state === 'boolean')) return false; + return blocks.every(([key, state]) => { + if (!identifier(key)) return false; + if (state === 'Clear' || state === 'Relax') return true; + return dictionary(state) && Object.keys(state).length === 1 && + Array.isArray(state.Route) && state.Route.length === 2 && state.Route.every(identifier); + }); +} + +export function allowsLargeLightingPacket(value: unknown): boolean { + if (!dictionary(value)) return false; + if (value.type === 'MULTI_STATE_UPDATE') { + if (Object.keys(value).some((key) => !['type', 'data', 'airport', 'timestamp'].includes(key))) return false; + if (value.airport !== undefined && !identifier(value.airport)) return false; + if (value.timestamp !== undefined && (typeof value.timestamp !== 'number' || !Number.isFinite(value.timestamp))) { + return false; + } + if (dictionary(value.data) && Object.keys(value.data).some((key) => key !== 'updates')) return false; + const updates = Array.isArray(value.data) ? value.data : dictionary(value.data) ? value.data.updates : undefined; + return ( + Array.isArray(updates) && + updates.length > 0 && + updates.length <= MAX_LIGHTING_UPDATES && + updates.every((update) => + dictionary(update) && + Object.keys(update).length === 2 && + identifier(update.objectId) && + /^[a-zA-Z0-9_-]+$/.test(update.objectId) && + typeof update.state === 'boolean', + ) + ); + } + return value.type === 'SHARED_STATE_UPDATE' && dictionary(value.data) && isEuroScopeGraphPatch(value.data.sharedStatePatch); +} diff --git a/src/network/point-state-equivalence.ts b/src/network/point-state-equivalence.ts new file mode 100644 index 0000000..4636c91 --- /dev/null +++ b/src/network/point-state-equivalence.ts @@ -0,0 +1,94 @@ +import type { AirportObject, MultiStateUpdateItem, Point } from '../types'; + +export type OfflineStatePoint = { id: string; state: boolean }; + +export type OfflineStateTemplate = { + points: OfflineStatePoint[]; + equivalentIdsById: ReadonlyMap; +}; + +const getDefaultState = (point: Point): boolean => point.type === 'taxiway' || point.type === 'lead_on' || point.type === 'stand'; + +/** + * State equivalence intentionally follows the intrinsic properties that affect + * the generated simulator lights. IDs and names are metadata, while linkedTo + * describes control relationships rather than the physical light object. + * + * Coordinates are compared exactly and in order. This favors a false negative + * over coupling two merely-nearby light objects. + */ +const getPointStateFingerprint = (point: Point): string | null => { + if (point.coordinates.length === 0) return null; + + return JSON.stringify([ + point.airportId.toUpperCase(), + point.type, + point.coordinates.map(({ lat, lng }) => [lat, lng]), + point.directionality ?? null, + point.color ?? null, + point.elevated ?? false, + point.ihp ?? false, + ]); +}; + +export const buildPointStateTemplate = (airportPoints: readonly Point[]): OfflineStateTemplate => { + const duplicateGroups = new Map(); + + for (const point of airportPoints) { + const fingerprint = getPointStateFingerprint(point); + if (fingerprint === null) continue; + + const ids = duplicateGroups.get(fingerprint); + if (ids) { + ids.push(point.id); + } else { + duplicateGroups.set(fingerprint, [point.id]); + } + } + + const equivalentIdsById = new Map(); + for (const unsortedIds of duplicateGroups.values()) { + if (unsortedIds.length < 2) continue; + const ids = Object.freeze([...unsortedIds].sort()); + for (const id of ids) equivalentIdsById.set(id, ids); + } + + return { + points: airportPoints.map((point) => ({ id: point.id, state: getDefaultState(point) })), + equivalentIdsById, + }; +}; + +export const getGeneratedAliasUpdates = ( + updates: readonly MultiStateUpdateItem[], + requestedObjectIds: ReadonlySet, +): MultiStateUpdateItem[] => updates.filter((update) => !requestedObjectIds.has(update.objectId)); + +export const selectCanonicalEquivalentState = ( + ids: readonly string[], + objects: ReadonlyMap, +): AirportObject | undefined => { + let latest: AirportObject | undefined; + for (const id of ids) { + const candidate = objects.get(id); + if ( + candidate && + (!latest || candidate.timestamp > latest.timestamp || (candidate.timestamp === latest.timestamp && candidate.id < latest.id)) + ) { + latest = candidate; + } + } + return latest; +}; + +export const chunkStateUpdates = (updates: readonly MultiStateUpdateItem[], maximumChunkSize: number): MultiStateUpdateItem[][] => { + if (!Number.isInteger(maximumChunkSize) || maximumChunkSize < 1) { + throw new Error('maximumChunkSize must be a positive integer'); + } + + const chunks: MultiStateUpdateItem[][] = []; + for (let index = 0; index < updates.length; index += maximumChunkSize) { + chunks.push(updates.slice(index, index + maximumChunkSize)); + } + return chunks; +}; diff --git a/src/services/airport.ts b/src/services/airport.ts index 32eaeea..48b2efa 100644 --- a/src/services/airport.ts +++ b/src/services/airport.ts @@ -3,6 +3,8 @@ import { PostHogService } from './posthog'; import { calculateDistance } from './bars/geoUtils'; import { HttpError } from './errors'; import { cancelResponseBody } from './http'; +import { parseDecimalNumber } from './parsing'; +import type { JsonObject, JsonValue } from '../types'; interface AirportData { latitude_deg?: number; @@ -114,43 +116,45 @@ const RUNWAY_FIELDS = new Set([ 'he_longitude_deg', ]); -const requireObject = (value: unknown): Record => { - if (!value || typeof value !== 'object' || Array.isArray(value)) { +const requireObject = (value: JsonValue): JsonObject => { + if (value === null || Array.isArray(value) || !(value instanceof Object)) { throw new HttpError(400, 'Request body must be a JSON object'); } - return value as Record; + return value; }; -const requireNumber = (value: unknown, field: string, min: number, max: number): number => { - if (typeof value !== 'number' || !Number.isFinite(value) || value < min || value > max) { +const requireNumber = (value: JsonValue, field: string, min: number, max: number): number => { + const parsed = Number(value); + if (value?.constructor !== Number || !Number.isFinite(parsed) || parsed < min || parsed > max) { throw new HttpError(400, `${field} must be a number between ${min} and ${max}`); } - return value; + return parsed; }; -const requireString = (value: unknown, field: string, maxLength: number): string => { - if (typeof value !== 'string') throw new HttpError(400, `${field} must be a string`); - const normalized = value.trim(); +const requireString = (value: JsonValue, field: string, maxLength: number): string => { + if (value?.constructor !== String) throw new HttpError(400, `${field} must be a string`); + const normalized = String(value).trim(); if (!normalized || normalized.length > maxLength) { throw new HttpError(400, `${field} must contain 1-${maxLength} characters`); } return normalized; }; -const optionalString = (value: unknown, field: string, maxLength: number): string | null => { +const optionalString = (value: JsonValue, field: string, maxLength: number): string | null => { if (value === null) return null; return requireString(value, field, maxLength); }; -const numericString = (value: unknown, field: string, min: number, max: number): string => { - const parsed = typeof value === 'number' ? value : typeof value === 'string' && value.trim() !== '' ? Number(value) : Number.NaN; +const numericString = (value: JsonValue, field: string, min: number, max: number): string => { + const decimalInput = value?.constructor === String ? String(value) : value?.constructor === Number ? Number(value) : null; + const parsed = parseDecimalNumber(decimalInput); if (!Number.isFinite(parsed) || parsed < min || parsed > max) { throw new HttpError(400, `${field} must be a number between ${min} and ${max}`); } return String(parsed); }; -const parseRunways = (value: unknown): RunwayRecord[] => { +const parseRunways = (value: JsonValue): RunwayRecord[] => { if (!Array.isArray(value)) throw new HttpError(400, 'runways must be an array'); if (value.length > 100) throw new HttpError(400, 'runways cannot contain more than 100 entries'); @@ -178,7 +182,7 @@ const parseRunways = (value: unknown): RunwayRecord[] => { }); }; -const validateKnownFields = (body: Record, allowIcao: boolean) => { +const validateKnownFields = (body: JsonObject, allowIcao: boolean) => { for (const field of Object.keys(body)) { if (!AIRPORT_FIELDS.has(field) || (!allowIcao && field === 'icao')) { throw new HttpError(400, `Unknown or immutable field: ${field}`); @@ -186,7 +190,7 @@ const validateKnownFields = (body: Record, allowIcao: boolean) } }; -const parseOptionalFields = (body: Record, target: AirportUpdateInput) => { +const parseOptionalFields = (body: JsonObject, target: AirportUpdateInput) => { if ('latitude' in body) target.latitude = requireNumber(body.latitude, 'latitude', -90, 90); if ('longitude' in body) target.longitude = requireNumber(body.longitude, 'longitude', -180, 180); if ('name' in body) target.name = requireString(body.name, 'name', 200); @@ -245,7 +249,7 @@ const parseOptionalFields = (body: Record, target: AirportUpdat if ('runways' in body) target.runways = parseRunways(body.runways); }; -export const parseAirportCreateInput = (value: unknown): AirportCreateInput => { +export const parseAirportCreateInput = (value: JsonValue): AirportCreateInput => { const body = requireObject(value); validateKnownFields(body, true); const icao = requireString(body.icao, 'icao', 4).toUpperCase(); @@ -275,7 +279,7 @@ export const parseAirportCreateInput = (value: unknown): AirportCreateInput => { }; }; -export const parseAirportUpdateInput = (value: unknown): AirportUpdateInput => { +export const parseAirportUpdateInput = (value: JsonValue): AirportUpdateInput => { const body = requireObject(value); validateKnownFields(body, false); const parsed: AirportUpdateInput = {}; @@ -356,6 +360,7 @@ export class AirportService { ]; try { const results = await dbSession.executeBatch(statements); + // SAFETY: The first statement returns the airports columns that exactly define AirportRecord. const airport = (results[0]?.results as AirportRecord[] | undefined)?.[0]; if (!airport) throw new Error('Airport insert returned no record'); try { @@ -379,10 +384,26 @@ export class AirportService { return this.withDbSession(async (dbSession) => { const assignments: string[] = []; const params: Array = []; - for (const [field, value] of Object.entries(input)) { - if (field === 'runways') continue; + const editableFields = [ + 'latitude', + 'longitude', + 'name', + 'continent', + 'country_code', + 'country_name', + 'region_name', + 'elevation_ft', + 'elevation_m', + 'bbox_min_lat', + 'bbox_min_lon', + 'bbox_max_lat', + 'bbox_max_lon', + ] as const; + for (const field of editableFields) { + const value = input[field]; + if (value === undefined) continue; assignments.push(`${field} = ?`); - params.push(value as string | number | null); + params.push(value); } const updateQuery = @@ -402,8 +423,10 @@ export class AirportService { }); const results = await dbSession.executeBatch(statements); + // SAFETY: The update statement uses RETURNING * from airports, whose selected row matches AirportRecord. const airport = (results[0]?.results as AirportRecord[] | undefined)?.[0]; if (!airport) throw new HttpError(404, `Airport ${normalizedIcao} not found`); + // SAFETY: The final statement selects exactly the eight string columns required by RunwayRecord. const runways = (results[results.length - 1]?.results as RunwayRecord[] | undefined) ?? []; try { this.posthog?.track('Airport Manually Updated', { @@ -447,8 +470,11 @@ export class AirportService { params: [uppercaseIcao], }, ]); + // SAFETY: Each assertion corresponds to the explicitly selected columns in its immediately preceding query. const airportFromDb = (airportResult.results as AirportRecord[])[0]; + // SAFETY: The runway query selects exactly the eight string columns required by RunwayRecord. const cachedRunways = runwayResult.results as RunwayRecord[]; + // SAFETY: The division-airport query selects only the numeric id column. const divisionAirportId = (divisionAirportResult.results as Array<{ id: number }>)[0]?.id ?? null; if (airportFromDb) { @@ -499,10 +525,11 @@ export class AirportService { if (response.status === 404) return null; throw new HttpError(503, 'Bounding box unavailable'); } - const airportData = (await response.json()) as AirportData; + const airportData: AirportData = await response.json(); - const hasCoords = Number.isFinite(airportData.latitude_deg) && Number.isFinite(airportData.longitude_deg); - if (!hasCoords) { + const latitude = airportData.latitude_deg; + const longitude = airportData.longitude_deg; + if (latitude === undefined || longitude === undefined || !Number.isFinite(latitude) || !Number.isFinite(longitude)) { try { this.posthog?.track('Airport External Fetch MissingCoords', { icao: uppercaseIcao }); } catch { @@ -521,8 +548,8 @@ export class AirportService { const airport = { icao: uppercaseIcao, - latitude: airportData.latitude_deg!, - longitude: airportData.longitude_deg!, + latitude, + longitude, name: airportData.name || '', continent: airportData.continent || 'UNKNOWN', country_code, @@ -637,7 +664,10 @@ export class AirportService { return { ...mergedAirport, id: divisionAirportId }; } catch (e) { try { - this.posthog?.track('Airport External Fetch Failed', { icao: uppercaseIcao, error: (e as Error).message }); + this.posthog?.track('Airport External Fetch Failed', { + icao: uppercaseIcao, + error: e instanceof Error ? e.message : String(e), + }); } catch { /* ignore */ } @@ -686,7 +716,9 @@ export class AirportService { const batchResults = await dbSession.executeReadBatch(statements); for (let index = 0; index < batchResults.length; index += 2) { + // SAFETY: The first query in each pair explicitly selects every AirportReadRecord field. for (const airport of batchResults[index].results as AirportReadRecord[]) cachedAirports.set(airport.icao, airport); + // SAFETY: The second query in each pair selects every RunwayRecord field plus airport_icao. for (const runway of batchResults[index + 1].results as Array) { const list = cachedRunways.get(runway.airport_icao) ?? []; list.push({ @@ -873,7 +905,7 @@ export class AirportService { } throw new HttpError(503, 'Bounding box unavailable'); } - const json = (await res.json()) as OverpassResponse; + const json: OverpassResponse = await res.json(); if (!json.elements || json.elements.length === 0) throw new HttpError(503, 'Bounding box unavailable'); const sorted = [...json.elements].sort((a, b) => { @@ -899,9 +931,15 @@ export class AirportService { if (minLat !== Infinity) { bounds = { minlat: minLat, minlon: minLon, maxlat: maxLat, maxlon: maxLon }; } - } else if (el.type === 'node' && Number.isFinite(el.lat) && Number.isFinite(el.lon)) { + } else if ( + el.type === 'node' && + el.lat !== undefined && + el.lon !== undefined && + Number.isFinite(el.lat) && + Number.isFinite(el.lon) + ) { const pad = 0.002; - bounds = { minlat: el.lat! - pad, minlon: el.lon! - pad, maxlat: el.lat! + pad, maxlon: el.lon! + pad }; + bounds = { minlat: el.lat - pad, minlon: el.lon - pad, maxlat: el.lat + pad, maxlon: el.lon + pad }; } } if (bounds) { @@ -930,7 +968,10 @@ export class AirportService { continue; } try { - this.posthog?.track('Airport Bounding Box Fetch Failed', { icao, error: (e as Error).message }); + this.posthog?.track('Airport Bounding Box Fetch Failed', { + icao, + error: e instanceof Error ? e.message : String(e), + }); } catch { /* ignore analytics errors */ } @@ -954,7 +995,7 @@ export class AirportService { await cancelResponseBody(response); return null; } - const data = (await response.json()) as AirportData; + const data: AirportData = await response.json(); const updates: Partial = {}; const setFragments: string[] = []; const params: Array = []; diff --git a/src/services/auth.ts b/src/services/auth.ts index c98cff5..48ca51e 100644 --- a/src/services/auth.ts +++ b/src/services/auth.ts @@ -4,9 +4,25 @@ import { DatabaseSessionService, type SessionOptions } from './database-session' import { PostHogService } from './posthog'; type DisplayModeUser = Pick; +type DisplayNameFields = Pick; +type PersistedUserRow = Omit; +type LoginStateRow = Omit & { + id: number | null; + ban_vatsim_id: string | null; + ban_expires_at: string | null; +}; + +interface LocationFields { + regionId: string | null; + regionName: string | null; + divisionId: string | null; + divisionName: string | null; + subdivisionId: string | null; + subdivisionName: string | null; +} interface ExistingUserLookup { - user: UserRecord | null; + user: PersistedUserRow | null; bookmark: string | null; } @@ -94,7 +110,10 @@ export class AuthService { return { vatsimToken: auth.access_token }; } - private async getOrCreateUser(vatsimUser: VatsimUser, lookup?: ExistingUserLookup): Promise<{ user: UserRecord; created: boolean }> { + private async getOrCreateUser( + vatsimUser: VatsimUser, + lookup?: ExistingUserLookup, + ): Promise<{ user: Pick; created: boolean }> { if (lookup?.user) { return { user: lookup.user, created: false }; } @@ -116,7 +135,7 @@ export class AuthService { private async createNewUser(vatsimUser: VatsimUser, dbSession: DatabaseSessionService) { // Check for existing VATSIM user using session - const existingVatsimUserResult = await dbSession.executeRead('SELECT id FROM users WHERE vatsim_id = ?', [ + const existingVatsimUserResult = await dbSession.executeRead<{ id: number }>('SELECT id FROM users WHERE vatsim_id = ?', [ vatsimUser.id, ]); @@ -127,7 +146,7 @@ export class AuthService { let apiKey = this.generateApiKey(); while (true) { - const existingKeyResult = await dbSession.executeRead('SELECT id FROM users WHERE api_key = ?', [apiKey]); + const existingKeyResult = await dbSession.executeRead<{ id: number }>('SELECT id FROM users WHERE api_key = ?', [apiKey]); if (!existingKeyResult.results[0]) break; apiKey = this.generateApiKey(); @@ -137,20 +156,14 @@ export class AuthService { const displayMode = 0; const displayName = this.computeDisplayName( { - id: 0, vatsim_id: vatsimUser.id, - api_key: apiKey, - email: vatsimUser.email, full_name: fullName, display_mode: displayMode, - created_at: '', - last_login: '', - vatsimToken: '', }, vatsimUser, ); const { regionId, regionName, divisionId, divisionName, subdivisionId, subdivisionName } = this.normalizeLocationFields(vatsimUser); - const result = await dbSession.executeWrite( + const result = await dbSession.executeWrite( 'INSERT INTO users (vatsim_id, api_key, email, full_name, display_mode, display_name, region_id, region_name, division_id, division_name, subdivision_id, subdivision_name, created_at, last_login) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING *', [ vatsimUser.id, @@ -170,10 +183,9 @@ export class AuthService { ], ); - const rows = result.results as unknown as UserRecord[] | null; - const created = rows && rows[0]; + const created = result.results?.[0]; if (!created) throw new Error('Failed to create user'); - return created as UserRecord; + return created; } async syncUserLocationFields(userId: number, vatsimUser: VatsimUser) { @@ -225,7 +237,7 @@ export class AuthService { async getUserByApiKey(apiKey: string): Promise { return this.withDbSession(async (dbSession) => { - const result = await dbSession.executeRead( + const result = await dbSession.executeLatest( `SELECT u.* FROM users u LEFT JOIN bans b ON b.vatsim_id = u.vatsim_id @@ -257,7 +269,7 @@ export class AuthService { const row = result.results[0]; if (!row) return { user: null, banned: false }; const { is_banned, ...user } = row; - return { user: user as UserRecord, banned: is_banned === 1 }; + return { user, banned: is_banned === 1 }; }); } @@ -286,7 +298,7 @@ export class AuthService { }); } - computeDisplayName(user: UserRecord, vatsimUser?: VatsimUser): string { + computeDisplayName(user: DisplayNameFields, vatsimUser?: VatsimUser): string { const mode = user.display_mode ?? 0; const fullName = user.full_name || [vatsimUser?.first_name, vatsimUser?.last_name].filter(Boolean).join(' ').trim(); if (mode === 2) return user.vatsim_id; @@ -318,9 +330,9 @@ export class AuthService { } if (!user) return; - if (user.display_mode === mode) return; // nothing to do + if (user.display_mode === mode) return; - const displayName = this.computeDisplayName({ ...user, display_mode: mode } as UserRecord); + const displayName = this.computeDisplayName({ ...user, display_mode: mode }); await dbSession.executeWrite('UPDATE users SET display_mode = ?, display_name = ? WHERE id = ?', [ mode, @@ -341,7 +353,7 @@ export class AuthService { ); const user = current.results[0]; if (user) { - const displayName = this.computeDisplayName({ ...user, full_name: fullName } as UserRecord); + const displayName = this.computeDisplayName({ ...user, full_name: fullName }); await dbSession.executeWrite('UPDATE users SET full_name = ?, display_name = ? WHERE id = ?', [ fullName, displayName, @@ -379,15 +391,8 @@ export class AuthService { } } - private normalizeLocationFields(vatsimUser: VatsimUser): { - regionId: string | null; - regionName: string | null; - divisionId: string | null; - divisionName: string | null; - subdivisionId: string | null; - subdivisionName: string | null; - } { - const norm = (s?: string) => (typeof s === 'string' && s.trim().length > 0 ? s.trim() : null); + private normalizeLocationFields(vatsimUser: VatsimUser): LocationFields { + const norm = (value?: string) => value?.trim() || null; return { regionId: norm(vatsimUser.region?.id), regionName: norm(vatsimUser.region?.name), @@ -414,17 +419,17 @@ export class AuthService { } // Update the user's API key in the database - const result = await dbSession.executeWrite('UPDATE users SET api_key = ? WHERE id = ? RETURNING api_key', [ + const result = await dbSession.executeWrite<{ api_key: string }>('UPDATE users SET api_key = ? WHERE id = ? RETURNING api_key', [ newApiKey, userId, ]); - const rows = result.results as unknown as Array<{ api_key: string }> | null; - if (!rows || !rows[0]) { + const updated = result.results?.[0]; + if (!updated) { throw new Error('Failed to update API key'); } - return rows[0].api_key; + return updated.api_key; }, { mode: 'first-primary' }, ); @@ -511,7 +516,7 @@ export class AuthService { }> { return this.withDbSession( async (dbSession) => { - const result = await dbSession.executeRead( + const result = await dbSession.executeRead( `SELECT u.*, b.vatsim_id AS ban_vatsim_id, b.expires_at AS ban_expires_at FROM (SELECT 1) anchor LEFT JOIN users u ON u.vatsim_id = ? @@ -521,10 +526,10 @@ export class AuthService { ); const row = result.results[0]; if (!row) throw new Error('Failed to load login state'); - const { ban_vatsim_id, ban_expires_at, ...userFields } = row; + const { ban_vatsim_id, ban_expires_at, id, ...userFields } = row; const bookmark = dbSession.getSessionInfo().bookmark; return { - existingUser: { user: userFields.id == null ? null : (userFields as UserRecord), bookmark }, + existingUser: { user: id == null ? null : { ...userFields, id }, bookmark }, banRecord: ban_vatsim_id ? { vatsim_id: ban_vatsim_id, expires_at: ban_expires_at } : null, }; }, diff --git a/src/services/bars/geoUtils.ts b/src/services/bars/geoUtils.ts index c033b5e..5ae076c 100644 --- a/src/services/bars/geoUtils.ts +++ b/src/services/bars/geoUtils.ts @@ -18,7 +18,6 @@ export function calculateDistance(point1: GeoPoint, point2: GeoPoint): number { export function calculateHeading(point1: GeoPoint, point2: GeoPoint): number { const heading = getGreatCircleBearing({ latitude: point1.lat, longitude: point1.lon }, { latitude: point2.lat, longitude: point2.lon }); - // Convert to 0-360 range return (heading + 360) % 360; } @@ -72,7 +71,6 @@ export function smoothLine(points: GeoPoint[], segments = 5): GeoPoint[] { } } - // Add the last point result.push(points[points.length - 1]); return result; @@ -89,8 +87,8 @@ export function generateEquidistantPoints( if (interval <= 0) return points.map((p) => ({ ...p })); // Precompute segment lengths and bearings to avoid repeated geodesic calculations - const segmentLengths: number[] = new Array(points.length - 1); - const segmentBearings: number[] = new Array(points.length - 1); + const segmentLengths = Array.from({ length: points.length - 1 }, () => 0); + const segmentBearings = Array.from({ length: points.length - 1 }, () => 0); for (let i = 0; i < points.length - 1; i++) { const a = points[i]; const b = points[i + 1]; diff --git a/src/services/bars/handlers.ts b/src/services/bars/handlers.ts index 63a245e..6eab939 100644 --- a/src/services/bars/handlers.ts +++ b/src/services/bars/handlers.ts @@ -20,7 +20,7 @@ export abstract class BarsTypeHandler { protected addHeadingToPoints(points: GeoPoint[], headingAdjustment: number = 0): BarsLightPoint[] { if (points.length < 2) return []; - const segmentHeadings: Array = new Array(points.length - 1).fill(null); + const segmentHeadings: Array = Array.from({ length: points.length - 1 }, () => null); for (let i = 0; i < points.length - 1; i++) { const heading = calculateHeading(points[i], points[i + 1]); segmentHeadings[i] = Number.isFinite(heading) ? heading : null; @@ -125,7 +125,6 @@ export class StopbarHandler extends BarsTypeHandler { }; }); - // Add properties to base stopbar lights const lightsWithProperties: BarsLightPoint[] = lightsWithHeading.map( (light): BarsLightPoint => ({ ...light, @@ -177,7 +176,6 @@ export class StopbarHandler extends BarsTypeHandler { const IHP_LIGHT_SPACING = 1.4; // meters between IHP lights const IHP_LIGHT_OFFSET = 0.4; - // Find the center point of the stopbar const centerIndex = Math.floor(points.length / 2); const centerPoint = points[centerIndex]; @@ -196,11 +194,9 @@ export class StopbarHandler extends BarsTypeHandler { // Either 90 degrees clockwise or 90 degrees counterclockwise from stopbar direction const offsetDirection = placeOnRightSide ? (stopbarDirection + 90) % 360 : (stopbarDirection + 270) % 360; - // Create the center IHP light // It's offset 0.7m from the center of the stopbar in the offset direction const centerIhpPoint = calculateDestinationPoint(centerPoint, IHP_LIGHT_OFFSET, offsetDirection); - // Create left and right IHP lights // They are positioned ALONG THE STOPBAR DIRECTION (parallel to it) // Left IHP light (1.5m along stopbar direction) const leftIhpPoint = calculateDestinationPoint(centerIhpPoint, IHP_LIGHT_SPACING, stopbarDirection); @@ -208,7 +204,6 @@ export class StopbarHandler extends BarsTypeHandler { // Right IHP light (1.5m in opposite direction along stopbar) const rightIhpPoint = calculateDestinationPoint(centerIhpPoint, IHP_LIGHT_SPACING, (stopbarDirection + 180) % 360); - // Create the three IHP lights with inherited heading from stopbar [leftIhpPoint, centerIhpPoint, rightIhpPoint].forEach((point) => { ihpLights.push({ ...point, @@ -234,7 +229,6 @@ export class StopbarHandler extends BarsTypeHandler { const elevatedLights: BarsLightPoint[] = []; - // Get the first and last lights with their headings already calculated const firstLight = lightsWithHeading[0]; const lastLight = lightsWithHeading[lightsWithHeading.length - 1]; @@ -270,7 +264,6 @@ export class StopbarHandler extends BarsTypeHandler { const firstElevatedHeading = (baseLineHeading - ELEVATED_LIGHT_INWARD_ANGLE) % 360; const lastElevatedHeading = (baseLineHeading - ELEVATED_LIGHT_INWARD_ANGLE + 90) % 360; - // Add the elevated lights with correct positions and inward headings elevatedLights.push({ ...startInwardPoint, heading: firstElevatedHeading, @@ -548,7 +541,7 @@ export function deduplicateTaxiwayPoints(objects: ProcessedBarsObject[]): Proces const pointToGroup = new Map(); const toCellKey = (x: number, y: number): string => `${x}:${y}`; - const toMeters = (point: BarsLightPoint): { x: number; y: number } => { + const toMeters = (point: BarsLightPoint) => { const latRadians = (point.lat * Math.PI) / 180; const lonScale = Math.max(Math.cos(latRadians), 0.000001) * METERS_PER_DEGREE_LAT; return { diff --git a/src/services/cache.ts b/src/services/cache.ts index edcbac6..2510a5e 100644 --- a/src/services/cache.ts +++ b/src/services/cache.ts @@ -45,7 +45,7 @@ export class CacheService { const res = await cache.match(this.versionMetaKey(namespace)); if (!res) return 1; try { - const data = (await res.json()) as { version?: number }; + const data = await res.json<{ version?: number }>(); const v = Number(data?.version); return Number.isFinite(v) && v > 0 ? v : 1; } catch { @@ -136,11 +136,9 @@ export class CacheService { async set(key: string, data: T, options: CacheOptions = {}): Promise { const { ttl = 60, namespace = 'default' } = options; - // Versioned cache key with namespace const ver = await this.resolveNamespaceVersion(namespace); const cacheKey = this.dataKey(key, namespace, ver); - // Create response with the data const response = new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', @@ -148,7 +146,6 @@ export class CacheService { }, }); - // Store in cache const cache = caches.default; await cache.put(cacheKey, response); } @@ -204,6 +201,13 @@ export function withCache( return next(); } + // The Cache API cannot store Worker-generated 206 responses. Bypass + // explicit range requests so a partial response is never read from or + // written to the cache under the full-response key. + if (c.req.raw.headers.has('Range')) { + return next(); + } + if (shouldBypass?.(c.req.raw)) { return next(); } @@ -211,7 +215,6 @@ export function withCache( cacheService ??= new CacheService(c.env); const cacheKey = cacheKeyFn(c.req.raw); - // Try to get from cache const cachedResponse = await cacheService.getResponse(cacheKey, namespace); if (cachedResponse) { const response = new Response(cachedResponse.body, cachedResponse); @@ -222,13 +225,12 @@ export function withCache( return response; } - // Cache miss, proceed to handler c.header('X-Cache', 'MISS'); await next(); - // After handler executes, cache the response if it was successful - // Don't cache error responses (4xx, 5xx) including 404 Not Found - if (c.res && c.res.status >= 200 && c.res.status < 300) { + // Cache only complete responses. In particular, Cache.put rejects 206 + // Partial Content even though it is otherwise a successful response. + if (c.res?.status === 200) { try { const contentType = c.res.headers.get('content-type'); if ( @@ -245,7 +247,6 @@ export function withCache( } } catch { // Silently fail if we can't cache - // console.error('Failed to cache response:', e); } } }; diff --git a/src/services/contact.ts b/src/services/contact.ts index 1690645..2f29372 100644 --- a/src/services/contact.ts +++ b/src/services/contact.ts @@ -39,7 +39,9 @@ export class ContactService { RETURNING id, email, topic, message, ip_address, status, handled_by, handled_at, created_at`, [id, email, topic, message, ipHash], ); - const created = (result.results as unknown as ContactMessageRecord[] | null)?.[0]; + // SAFETY: The INSERT RETURNING list exactly matches ContactMessageRecord and supplies one row on success. + const rows = result.results as ContactMessageRecord[] | null; + const created = rows?.[0]; if (!created) throw new Error('Failed to create contact message'); return created; } @@ -67,12 +69,16 @@ export class ContactService { RETURNING id, email, topic, message, ip_address, status, handled_by, handled_at, created_at`, [status, status, handlerVatsimId, status, id], ); - return (result.results as unknown as ContactMessageRecord[] | null)?.[0] ?? null; + // SAFETY: The UPDATE RETURNING list exactly matches ContactMessageRecord. + const rows = result.results as ContactMessageRecord[] | null; + return rows?.[0] ?? null; } async deleteMessage(id: string): Promise { const result = await this.dbSession.executeWrite(`DELETE FROM contact_messages WHERE id = ? RETURNING id`, [id]); - return Boolean((result.results as unknown as Array<{ id: string }> | null)?.[0]); + // SAFETY: The DELETE statement explicitly returns the string contact_messages.id column. + const rows = result.results as Array<{ id: string }> | null; + return Boolean(rows?.[0]); } async hasRecentSubmissionFromIp(ip: string, withinHours = 24): Promise { diff --git a/src/services/contributions.ts b/src/services/contributions.ts index 4d7ddd1..5402745 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,14 @@ 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; + decisionSource?: 'staff' | 'fast_track' | null; + decidedBy?: string | null; } export interface ContributionSubmission { @@ -34,6 +43,9 @@ export interface ContributionSubmission { submittedXml: string; notes?: string; simulator: Simulator; + generationToken: string; + generationHash: string; + fastTrackRequested: boolean; } export interface ContributionDecision { @@ -56,15 +68,34 @@ 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 }; + +export type ContributionSubmissionResult = ContributionDecisionResult & { + fastTrack?: { status: 'published' | 'review_required' }; +}; + 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'; +const MAX_FAST_TRACK_PUBLICATIONS_PER_DAY = 5; function sha256ToBase64Url(hashBuf: ArrayBuffer): string { const bytes = new Uint8Array(hashBuf); @@ -75,20 +106,57 @@ 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); + const version = simulator === 'xplane' ? CONTRIBUTION_GENERATION_CONTRACT_VERSION + : simulator === 'msfs2020' ? 'bars-contribution-generation/v7' : 'bars-contribution-generation/v6'; + return sha256Base64Url(`${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; bars_xml?: string; + created_at: string; expires_at: string; } @@ -97,8 +165,11 @@ export type ContributionGenerationLookupResult = status: 'ok'; token: string; icao: string; + simulator?: Simulator; + generationHash?: string; supportsXml: string; barsXml: string; + createdAt: string; expiresAt: string; } | { status: 'invalid-token' } @@ -139,6 +210,10 @@ 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, + c.decision_source AS decisionSource, c.decided_by AS decidedBy, 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 +267,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], + ); + } } } @@ -235,12 +319,12 @@ export class ContributionService { try { let expiredKeys: string[] = []; try { - const [expired] = await session.executeBatch([ + const [expired] = await session.executeBatch>([ { query: "SELECT supports_key, bars_key FROM contribution_generations WHERE expires_at <= datetime('now')" }, { query: "DELETE FROM contribution_generations WHERE expires_at <= datetime('now')" }, ]); - expiredKeys = (expired.results as Array>).flatMap( - (row) => [row.supports_key, row.bars_key].filter((key): key is string => Boolean(key)), + expiredKeys = (expired.results ?? []).flatMap((row) => + [row.supports_key, row.bars_key].filter((key): key is string => Boolean(key)), ); } catch (error) { const message = error instanceof Error ? error.message.toLowerCase() : ''; @@ -248,12 +332,12 @@ export class ContributionService { throw error; } - const [expired] = await session.executeBatch([ + const [expired] = await session.executeBatch>([ { query: "SELECT supports_xml, bars_xml FROM contribution_generations WHERE expires_at <= datetime('now')" }, { query: "DELETE FROM contribution_generations WHERE expires_at <= datetime('now')" }, ]); - expiredKeys = (expired.results as Array>).flatMap( - (row) => [row.supports_xml, row.bars_xml].filter((key): key is string => Boolean(key)), + expiredKeys = (expired.results ?? []).flatMap((row) => + [row.supports_xml, row.bars_xml].filter((key): key is string => Boolean(key)), ); } await Promise.all( @@ -270,13 +354,20 @@ 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(); const session = DatabaseContextFactory.createSessionService(this.db); try { - const [, existing] = await session.executeBatch([ + const [, existing] = await session.executeBatch<{ one: number }>([ { query: "DELETE FROM contribution_generations WHERE token = ? AND expires_at <= datetime('now')", params: [token], @@ -288,16 +379,17 @@ export class ContributionService { params: [token], }, ]); - if ((existing.results as Array<{ one: number }> | undefined)?.[0]) { + if (existing.results?.[0]) { 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 +397,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 +423,8 @@ 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, + strftime('%Y-%m-%dT%H:%M:%fZ', created_at) AS created_at FROM contribution_generations WHERE token = ? AND expires_at > datetime('now') LIMIT 1 @@ -344,15 +437,25 @@ 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, + strftime('%Y-%m-%dT%H:%M:%fZ', created_at) AS created_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, + strftime('%Y-%m-%dT%H:%M:%fZ', created_at) AS created_at + FROM contribution_generations + WHERE token = ? AND expires_at > datetime('now') LIMIT 1`, + [token], + ); + } } const generation = result.results[0]; if (!generation) { @@ -371,8 +474,11 @@ export class ContributionService { status: 'ok', token, icao: generation.icao, + simulator: generation.simulator, + generationHash: generation.generation_hash, supportsXml, barsXml, + createdAt: generation.created_at, expiresAt: generation.expires_at, }; } finally { @@ -381,7 +487,7 @@ export class ContributionService { } private normalizeAirportIcao(raw: string): string { - if (typeof raw !== 'string') { + if (String(raw) !== raw) { throw new Error('airportIcao must be a string'); } const normalized = raw.trim().toUpperCase(); @@ -392,7 +498,7 @@ export class ContributionService { } private sanitizePackageName(raw: string, fieldLabel = 'packageName'): string { - if (typeof raw !== 'string') { + if (String(raw) !== raw) { throw new Error(`${fieldLabel} must be a string`); } const trimmed = raw.trim(); @@ -404,7 +510,7 @@ export class ContributionService { } return trimmed; } - async createContribution(submission: ContributionSubmission): Promise { + async createContribution(submission: ContributionSubmission): Promise { const normalizedAirportIcao = this.normalizeAirportIcao(submission.airportIcao); const sanitizedPackageName = this.sanitizePackageName(submission.packageName); @@ -425,7 +531,7 @@ export class ContributionService { let sanitizedNotes: string | null = null; if (submission.notes !== undefined && submission.notes !== null) { - if (typeof submission.notes !== 'string') { + if (String(submission.notes) !== submission.notes) { throw new Error('Notes must be a string'); } const trimmed = submission.notes.trim(); @@ -443,15 +549,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 +593,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.', ); @@ -474,28 +602,51 @@ export class ContributionService { const id = crypto.randomUUID(); const now = new Date().toISOString(); + // Keep the tested pair independently of the one-day generation cache. + const publication = await this.publishArtifacts( + { id, airportIcao: normalizedAirportIcao, simulator: submission.simulator }, + sanitizedPackageName, + 'submission', + storedGeneration.supportsXml, + storedGeneration.barsXml, + ); // Insert without snapshot of display name; we'll always resolve via users table when reading - await this.dbSession.executeWrite( - ` + try { + await this.dbSession.executeWrite( + ` 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, + artifact_identity, artifact_generation_id, removal_artifact_key, bars_artifact_key + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `, - [ - id, - submission.userId, - normalizedAirportIcao, - sanitizedPackageName, - trimmedXml, - sanitizedNotes, - submission.simulator, - now, - 'pending', - ], - ); + [ + id, + submission.userId, + normalizedAirportIcao, + sanitizedPackageName, + trimmedXml, + sanitizedNotes, + submission.simulator, + now, + 'pending', + expectedToken, + expectedHash, + publication.artifactIdentity, + publication.generationId, + publication.removal.key, + publication.bars.key, + ], + ); + } catch (error) { + const publicationIsCurrent = await this.isPublicationCurrent(id, publication, 'pending'); + if (publicationIsCurrent !== true) { + if (publicationIsCurrent === false) await this.discardPublication(publication); + throw error; + } + } const contribution: Contribution = { id, @@ -510,6 +661,14 @@ export class ContributionService { status: 'pending', rejectionReason: null, decisionDate: null, + generationToken: expectedToken, + generationHash: expectedHash, + artifactIdentity: publication.artifactIdentity, + artifactGenerationId: publication.generationId, + removalArtifactKey: publication.removal.key, + barsArtifactKey: publication.bars.key, + decisionSource: null, + decidedBy: null, }; try { this.posthog?.track('Contribution Submitted', { @@ -521,7 +680,42 @@ export class ContributionService { } catch (e) { console.warn('Posthog track failed (Contribution Submitted)', e); } - return contribution; + if (!submission.fastTrackRequested) return contribution; + + try { + const fastTrack = await this.evaluateFastTrack(contribution); + if (!fastTrack.hasActiveGrant) return contribution; + if (!fastTrack.canPublish) return { ...contribution, fastTrack: { status: 'review_required' } }; + + const published = await this.approvePendingContribution( + contribution, + contribution.packageName, + submission.userId, + 'fast_track', + ); + try { + this.posthog?.track('Contribution Fast Track Published', { + id: contribution.id, + airport: contribution.airportIcao, + packageName: contribution.packageName, + simulator: contribution.simulator, + userId: submission.userId, + }); + } catch (error) { + console.warn('Posthog track failed (Contribution Fast Track)', error); + } + return { ...published, fastTrack: { status: 'published' } }; + } catch (error) { + console.warn( + JSON.stringify({ + event: 'contribution_fast_track_fallback', + contributionId: contribution.id, + userId: submission.userId, + error: error instanceof Error ? error.message : 'Unknown error', + }), + ); + return { ...contribution, fastTrack: { status: 'review_required' } }; + } } async getContribution(id: string): Promise { const result = await this.dbSession.executeRead( @@ -531,7 +725,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 +758,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 +798,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 +816,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 +906,288 @@ export class ContributionService { }; } - async processDecision(id: string, userId: string, decision: ContributionDecision): Promise { + private fastTrackGuard(alias: string): string { + return ` + AND EXISTS ( + SELECT 1 + FROM users trusted_user + JOIN contributor_fast_track fast_track ON fast_track.user_id = trusted_user.id + WHERE trusted_user.vatsim_id = ${alias}.user_id + AND fast_track.enabled = 1 + AND fast_track.expires_at > CURRENT_TIMESTAMP + ) + AND NOT EXISTS ( + SELECT 1 FROM contributions other_contribution + WHERE other_contribution.id != ${alias}.id + AND other_contribution.airport_icao = ${alias}.airport_icao + AND other_contribution.package_name = ${alias}.package_name COLLATE NOCASE + AND other_contribution.simulator = ${alias}.simulator + AND other_contribution.status = 'pending' + ) + AND ( + SELECT COUNT(*) FROM contributions recent + WHERE recent.user_id = ${alias}.user_id + AND recent.decision_source = 'fast_track' + AND datetime(recent.decision_date) >= datetime('now', '-1 day') + ) < ${MAX_FAST_TRACK_PUBLICATIONS_PER_DAY}`; + } + + private async evaluateFastTrack(contribution: Contribution): Promise<{ hasActiveGrant: boolean; canPublish: boolean }> { + const result = await this.dbSession.executeLatest<{ hasActiveGrant: number; canPublish: number }>( + `SELECT + CASE WHEN EXISTS ( + SELECT 1 + FROM users trusted_user + JOIN contributor_fast_track fast_track ON fast_track.user_id = trusted_user.id + WHERE trusted_user.vatsim_id = target.user_id + AND fast_track.enabled = 1 + AND fast_track.expires_at > CURRENT_TIMESTAMP + ) THEN 1 ELSE 0 END AS hasActiveGrant, + CASE WHEN target.status = 'pending' ${this.fastTrackGuard('target')} + THEN 1 ELSE 0 END AS canPublish + FROM contributions target + WHERE target.id = ? LIMIT 1`, + [contribution.id], + ); + const row = result.results[0]; + return { + hasActiveGrant: row?.hasActiveGrant === 1, + canPublish: row?.canPublish === 1, + }; + } + + private async approvePendingContribution( + contribution: Contribution, + packageName: string, + decidedBy: string, + decisionSource: 'staff' | 'fast_track', + ): Promise { + const publication = await this.generateAndPublishArtifacts(contribution, packageName, 'approval'); + const now = new Date().toISOString(); + const currentGuard = decisionSource === 'fast_track' ? this.fastTrackGuard('contributions') : ''; + + try { + const [approvedWrite] = await this.dbSession.executeBatch([ + { + query: `UPDATE contributions + SET status = 'approved', rejection_reason = NULL, decision_date = ?, package_name = ?, + artifact_identity = ?, artifact_generation_id = ?, removal_artifact_key = ?, bars_artifact_key = ?, + decision_source = ?, decided_by = ? + WHERE id = ? AND status = 'pending' ${currentGuard}`, + params: [ + now, + packageName, + publication.artifactIdentity, + publication.generationId, + publication.removal.key, + publication.bars.key, + decisionSource, + decidedBy, + contribution.id, + ], + }, + { + // Preserve the approval date: retiring an older fast-track publication + // must not count it against today's publication allowance again. + query: `UPDATE contributions + SET status = 'outdated' + 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 = 'approved' + AND target.artifact_generation_id = ? + )`, + params: [ + contribution.airportIcao, + packageName, + contribution.simulator, + contribution.id, + contribution.id, + publication.generationId, + ], + }, + ]); + if (approvedWrite?.meta?.changes === 0) { + throw new Error( + decisionSource === 'fast_track' + ? 'Contribution no longer qualifies for fast-track publishing' + : 'Contribution approval lost a concurrent decision race', + ); + } + } catch (error) { + const publicationIsCurrent = await this.isPublicationCurrent(contribution.id, publication); + if (publicationIsCurrent !== true) { + if (publicationIsCurrent === false) await this.discardPublication(publication); + throw error; + } + } + + return { + ...contribution, + packageName, + status: 'approved', + rejectionReason: null, + decisionDate: now, + artifactIdentity: publication.artifactIdentity, + artifactGenerationId: publication.generationId, + removalArtifactKey: publication.removal.key, + barsArtifactKey: publication.bars.key, + decisionSource, + decidedBy, + publication, + }; + } + + private async generateAndPublishArtifacts( + contribution: Contribution, + packageName: string, + operation: 'approval' | 'regeneration', + ): Promise { + if (operation === 'approval') { + const [removalArtifact, barsXml] = await this.readTestedArtifacts(contribution); + return this.publishArtifacts(contribution, packageName, operation, removalArtifact, barsXml); + } + const [removalArtifact, barsXml] = await Promise.all([ + contribution.simulator === 'xplane' + ? Promise.resolve(generateXPlaneRemovalsJson(contribution.submittedXml, contribution.airportIcao)) + : this.supportService.generateLightSupportsXML(contribution.submittedXml, contribution.airportIcao, contribution.simulator), + this.polygonService.processBarsXML(contribution.submittedXml, contribution.airportIcao), + ]); + return this.publishArtifacts(contribution, packageName, operation, removalArtifact, barsXml); + } + + private async readTestedArtifacts(contribution: Contribution): Promise<[string, string]> { + const unavailable = 'Tested artifacts are unavailable. The contributor must test and resubmit this draft.'; + if (contribution.removalArtifactKey || contribution.barsArtifactKey) { + if (!contribution.removalArtifactKey || !contribution.barsArtifactKey) throw new Error(unavailable); + const [removal, bars] = await Promise.all([ + this.storage.get(contribution.removalArtifactKey), + this.storage.get(contribution.barsArtifactKey), + ]); + if (!removal || !bars) throw new Error(unavailable); + const contents = await Promise.all([removal.text(), bars.text()]); + if (!contents[0] || !contents[1]) throw new Error(unavailable); + return contents; + } + + // Pending submissions from before snapshots can use their original, unexpired generation. + if (!contribution.generationToken || !contribution.generationHash) throw new Error(unavailable); + const generation = await this.getStoredGeneration(contribution.generationToken); + if (generation.status !== 'ok') throw new Error(unavailable); + const generatedAt = Date.parse(generation.createdAt); + const submittedAt = Date.parse(contribution.submissionDate); + if ( + !Number.isFinite(generatedAt) || + !Number.isFinite(submittedAt) || + generatedAt > submittedAt || + generation.icao !== contribution.airportIcao || + (generation.simulator !== undefined && generation.simulator !== contribution.simulator) || + (generation.generationHash !== undefined && generation.generationHash !== contribution.generationHash) || + (await deriveContributionGenerationHash(contribution.submittedXml)) !== contribution.generationHash + ) { + throw new Error(unavailable); + } + return [generation.supportsXml, generation.barsXml]; + } + + private async publishArtifacts( + contribution: Pick, + packageName: string, + operation: 'submission' | 'approval' | 'regeneration', + removalArtifact: string, + barsXml: string, + ): Promise { + 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.allSettled([ + this.storageService.uploadFile(removalKey, removalArtifact, removalContentType, { + ...commonMetadata, + type: 'removal', + contentHash: removalHash, + }), + this.storageService.uploadFile(barsKey, barsXml, 'application/xml', { + ...commonMetadata, + type: 'bars', + contentHash: barsHash, + }), + ]); + // Wait for both writes before cleanup so a late upload cannot leave an orphan. + if (removal.status === 'rejected') throw removal.reason; + if (bars.status === 'rejected') throw bars.reason; + return { + artifactIdentity, + generationId, + removal: { ...removal.value, contentType: removalContentType }, + bars: { ...bars.value, 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, + expectedStatus: Contribution['status'] = 'approved', + ): 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 === expectedStatus && + 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) { @@ -695,92 +1215,67 @@ export class ContributionService { ? this.sanitizePackageName(decision.newPackageName, 'newPackageName') : contribution.packageName; - // Update contribution with decision - const now = new Date().toISOString(); - const status = decision.approved ? 'approved' : 'rejected'; - const rejectionReason = decision.approved ? null : decision.rejectionReason || 'No reason provided'; - const updateCurrent = { - query: `UPDATE contributions - SET status = ?, rejection_reason = ?, decision_date = ?, package_name = ? - WHERE id = ?`, - params: [status, rejectionReason, now, packageName, id], - }; - - // If approving, atomically outdate older approvals and publish this one. 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 + const approved = await this.approvePendingContribution(contribution, packageName, userId, 'staff'); 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), - ]); - - // 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 + this.posthog?.track('Contribution Approved', { + id, + airport: contribution.airportIcao, + packageName, + simulator: contribution.simulator, + decidedBy: userId, + }); + } catch (error) { + console.warn('Posthog track failed (Contribution Decision)', error); } - } else { - await this.dbSession.executeWrite(updateCurrent.query, updateCurrent.params); + return approved; + } + + const now = new Date().toISOString(); + const rejectionReason = decision.rejectionReason || 'No reason provided'; + const [rejectedWrite] = await this.dbSession.executeBatch([ + { + query: `UPDATE contributions + SET status = 'rejected', rejection_reason = ?, decision_date = ?, package_name = ?, + decision_source = 'staff', decided_by = ? + WHERE id = ? AND status = 'pending'`, + params: [rejectionReason, now, packageName, userId, id], + }, + { + query: `UPDATE contributor_fast_track + SET enabled = 0, + updated_by = (SELECT id FROM users WHERE vatsim_id = ? LIMIT 1), + updated_at = CURRENT_TIMESTAMP + WHERE user_id = (SELECT id FROM users WHERE vatsim_id = ? LIMIT 1) + AND enabled = 1 + AND EXISTS ( + SELECT 1 FROM contributions rejected + WHERE rejected.id = ? AND rejected.status = 'rejected' AND rejected.decided_by = ? + )`, + params: [userId, contribution.userId, id, userId], + }, + ]); + if (rejectedWrite?.meta?.changes === 0) { + throw new Error('Contribution rejection lost a concurrent decision race'); } const updated: Contribution = { ...contribution, packageName, - status, + status: 'rejected', rejectionReason, decisionDate: now, + decisionSource: 'staff', + decidedBy: userId, }; try { - this.posthog?.track(decision.approved ? 'Contribution Approved' : 'Contribution Rejected', { + this.posthog?.track('Contribution Rejected', { id, airport: contribution.airportIcao, packageName, simulator: contribution.simulator, decidedBy: userId, - rejectionReason: decision.approved ? undefined : decision.rejectionReason || 'No reason provided', + rejectionReason, }); } catch (e) { console.warn('Posthog track failed (Contribution Decision)', e); @@ -895,32 +1390,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 +1424,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/database-context.ts b/src/services/database-context.ts index ed994d9..89fb141 100644 --- a/src/services/database-context.ts +++ b/src/services/database-context.ts @@ -26,13 +26,13 @@ export class BookmarkManager { /** * Create a new Response with bookmark header set */ - public static responseWithBookmark(body: unknown, bookmark: string | null, init: ResponseInit = {}): Response { + public static responseWithBookmark(body: string, bookmark: string | null, init: ResponseInit = {}): Response { const headers = new Headers(init.headers); if (bookmark) { headers.set(BookmarkManager.BOOKMARK_HEADER, bookmark); } - return new Response(typeof body === 'string' ? body : JSON.stringify(body), { + return new Response(body, { ...init, headers, }); @@ -58,7 +58,7 @@ export class RequestDatabaseContext { */ public startSession(options: Omit = {}): void { if (this.isStarted) { - return; // Already started + return; } const bookmark = BookmarkManager.getBookmarkFromRequest(this.request); diff --git a/src/services/database-session.ts b/src/services/database-session.ts index 5d3be89..98979ba 100644 --- a/src/services/database-session.ts +++ b/src/services/database-session.ts @@ -42,6 +42,12 @@ export interface DatabaseResponse { meta?: DatabaseMeta; } +export interface DatabaseWriteResponse { + results?: T[] | null; + success: boolean; + meta?: DatabaseMeta; +} + export type DatabaseSerializable = null | number | string | boolean | ArrayBuffer; export type DatabaseBinding = Record; @@ -114,7 +120,6 @@ export class DatabaseSessionService { params: DatabaseSerializable[] = [], options: SessionOptions = {}, ): Promise> { - // Start session if not already started if (!this.session) { this.startSession(options); } @@ -123,7 +128,6 @@ export class DatabaseSessionService { const stmt = this.session!.prepare(query); let boundStmt = stmt; - // Bind parameters if provided if (params.length > 0) { boundStmt = stmt.bind(...params); } @@ -152,7 +156,6 @@ export class DatabaseSessionService { params: DatabaseSerializable[] = [], options: SessionOptions = {}, ): Promise> { - // Start session if not already started if (!this.session) { this.startSession(options); } @@ -161,7 +164,6 @@ export class DatabaseSessionService { const stmt = this.session!.prepare(query); let boundStmt = stmt; - // Bind parameters if provided if (params.length > 0) { boundStmt = stmt.bind(...params); } @@ -186,7 +188,7 @@ export class DatabaseSessionService { * Execute a query that modifies data (INSERT, UPDATE, DELETE) * Always uses primary database for consistency */ - public async executeWrite(query: string, params: DatabaseSerializable[] = []): Promise> { + public async executeWrite(query: string, params: DatabaseSerializable[] = []): Promise> { // Force primary mode for write operations if (!this.session) { this.startSession({ mode: 'first-primary' }); @@ -201,7 +203,7 @@ export class DatabaseSessionService { boundStmt = stmt.bind(...params); } - const result = await boundStmt.run(); + const result = await boundStmt.run(); // Update bookmark after write operation this.getBookmark(); @@ -221,7 +223,7 @@ export class DatabaseSessionService { * Execute multiple statements in a batch * Uses primary database for consistency */ - public async executeBatch( + public async executeBatch( statements: Array< | { query: string; @@ -229,7 +231,7 @@ export class DatabaseSessionService { } | D1PreparedStatement >, - ): Promise[]> { + ): Promise[]> { if (statements.length === 0) return []; // Force primary mode for batch operations @@ -239,16 +241,18 @@ export class DatabaseSessionService { try { const preparedStatements = statements.map((statement) => { - if ('query' in statement && typeof statement.query === 'string') { + if ('query' in statement) { const { query, params = [] } = statement; const stmt = this.session!.prepare(query); return params.length > 0 ? stmt.bind(...params) : stmt; - } else { - return statement as D1PreparedStatement; } + return statement; }); - const results = await this.session!.batch(preparedStatements); + // D1 exposes one generic row type for the whole batch. Callers with + // heterogeneous statements must supply a union or shared optional-field + // contract and narrow by the known statement index. + const results = await this.session!.batch(preparedStatements); // Update bookmark after batch operation this.getBookmark(); @@ -330,11 +334,7 @@ export class DatabaseSessionService { /** * Get current session statistics for observability */ - public getSessionInfo(): { - hasSession: boolean; - hasBookmark: boolean; - bookmark: string | null; - } { + public getSessionInfo() { return { hasSession: this.session !== null, hasBookmark: this.currentBookmark !== null, @@ -368,7 +368,7 @@ export class DatabaseSessionService { db: D1Database, query: string, params: DatabaseSerializable[] = [], - ): Promise> { + ): Promise> { const session = new DatabaseSessionService(db); try { return await session.executeWrite(query, params); diff --git a/src/services/division-data-auth.ts b/src/services/division-data-auth.ts new file mode 100644 index 0000000..d0200c4 --- /dev/null +++ b/src/services/division-data-auth.ts @@ -0,0 +1,33 @@ +export const DIVISION_DATA_ACTOR_ID = 'division-data-ci'; + +type TimingSafeEqual = (left: ArrayBufferView, right: ArrayBufferView) => boolean; + +const encoder = new TextEncoder(); + +export function extractBearerToken(authorization: string): string | null { + const match = authorization.match(/^\s*Bearer\s+(.+?)\s*$/i); + return match?.[1] || null; +} + +/** + * Authenticates the division-data CI credential without exposing a user or + * staff principal. Callers must still explicitly opt into the narrow + * automation permissions supported by their route. + */ +export function isDivisionDataAutomationRequest( + authorization: string, + configuredSecret: string | undefined, + timingSafeEqual: TimingSafeEqual = crypto.subtle.timingSafeEqual.bind(crypto.subtle), +): boolean { + const suppliedToken = extractBearerToken(authorization); + if (!suppliedToken || !configuredSecret) return false; + + const suppliedBytes = encoder.encode(suppliedToken); + const configuredBytes = encoder.encode(configuredSecret); + const sameLength = suppliedBytes.byteLength === configuredBytes.byteLength; + + // timingSafeEqual requires equal-length inputs. Comparing the configured + // value with itself on a length mismatch avoids an early content comparison. + const comparableBytes = sameLength ? suppliedBytes : configuredBytes; + return timingSafeEqual(comparableBytes, configuredBytes) && sameLength; +} diff --git a/src/services/divisions.ts b/src/services/divisions.ts index bae29ef..dd38e99 100644 --- a/src/services/divisions.ts +++ b/src/services/divisions.ts @@ -130,7 +130,7 @@ export class DivisionService { AND da.status IN ('pending', 'approved') `; - if (typeof excludeAirportId === 'number') { + if (excludeAirportId !== undefined) { query += ' AND da.id != ?'; params.push(excludeAirportId); } @@ -156,7 +156,8 @@ export class DivisionService { async createDivision(name: string, headVatsimId: string): Promise { const result = await this.dbSession.executeWrite('INSERT INTO divisions (name) VALUES (?) RETURNING *', [name]); - const rows = result.results as unknown as Division[] | null; + // SAFETY: This INSERT returns every column of the divisions row, whose schema is represented by Division. + const rows = result.results as Division[] | null; const division = rows && rows[0]; if (!division) throw new Error('Failed to create division'); @@ -172,7 +173,8 @@ export class DivisionService { async updateDivisionName(id: number, newName: string): Promise { const result = await this.dbSession.executeWrite('UPDATE divisions SET name = ? WHERE id = ? RETURNING *', [newName, id]); - const rows = result.results as unknown as Division[] | null; + // SAFETY: This UPDATE returns every column of the divisions row, whose schema is represented by Division. + const rows = result.results as Division[] | null; const division = rows && rows[0]; if (!division) throw new Error('Division not found'); try { @@ -185,7 +187,8 @@ export class DivisionService { async deleteDivision(id: number): Promise { const result = await this.dbSession.executeWrite('DELETE FROM divisions WHERE id = ? RETURNING id', [id]); - const rows = result.results as unknown as Array<{ id: number }> | null; + // SAFETY: The DELETE statement returns only the numeric divisions.id column. + const rows = result.results as Array<{ id: number }> | null; const deleted = !!(rows && rows[0]); if (deleted) { try { @@ -209,7 +212,8 @@ export class DivisionService { [divisionId, vatsimId, role], ); - const rows = result.results as unknown as DivisionMember[] | null; + // SAFETY: This INSERT returns every division_members column, whose schema is represented by DivisionMember. + const rows = result.results as DivisionMember[] | null; const member = rows && rows[0]; if (!member) throw new Error('Failed to add member to division'); try { @@ -255,6 +259,7 @@ export class DivisionService { [divisionId, normalizedIcao, requestedBy], ); + // SAFETY: The INSERT statement returns a division_airports row containing its numeric id. const rows = result.results as Array<{ id: number }> | null; const request = rows && rows[0]; if (!request) throw new Error('Failed to create airport request'); @@ -276,6 +281,7 @@ export class DivisionService { [divisionId, normalizedIcao, requestedBy], ); + // SAFETY: The INSERT statement returns a division_airports row containing its numeric id. const rows = result.results as Array<{ id: number }> | null; const request = rows && rows[0]; if (!request) throw new Error('Failed to create airport request'); @@ -314,6 +320,7 @@ export class DivisionService { [approved ? 'approved' : 'rejected', approvedBy, airportId], ); + // SAFETY: The UPDATE statement returns a division_airports row containing its numeric id. const rows = result.results as Array<{ id: number }> | null; const updatedAirport = rows && rows[0]; const airport = updatedAirport ? await this.getDivisionAirportById(existing.division_id, updatedAirport.id) : null; @@ -358,6 +365,7 @@ export class DivisionService { `, [contributionsEnabled ? 1 : 0, airportId, divisionId], ); + // SAFETY: The UPDATE statement explicitly returns the numeric division_airports.id column. const rows = result.results as Array<{ id: number }> | null; const updated = rows?.[0]; if (!updated) { @@ -415,7 +423,9 @@ export class DivisionService { [airportId, 'pending', 'rejected'], ); - const deleted = !!(deleteResult.results && (deleteResult.results as Array<{ id: number }>)[0]); + // SAFETY: The DELETE statement explicitly returns the numeric division_airports.id column. + const deletedRows = deleteResult.results as Array<{ id: number }> | null; + const deleted = Boolean(deletedRows?.[0]); if (deleted) { try { this.posthog?.track('Division Airport Request Deleted', { diff --git a/src/services/downloads.ts b/src/services/downloads.ts index b96a154..c3be12e 100644 --- a/src/services/downloads.ts +++ b/src/services/downloads.ts @@ -10,13 +10,20 @@ interface DownloadRow { updated_at: string; } +interface DownloadBatchRow { + id?: number; + should_increment?: number; + version?: string; + total_count?: number; +} + export interface VersionDownloadStats { version: string; count: number; } -export interface ProductDownloadStats { - product: InstallerProduct; +export interface ProductDownloadStats { + product: TProduct; total: number; versions: VersionDownloadStats[]; } @@ -51,7 +58,7 @@ export class DownloadsService { ip: string, ): Promise<{ versionCount: number; productTotal: number; versions: VersionDownloadStats[] }> { const ipHash = await this.hashIp(ip || '0.0.0.0'); - const initial = await this.dbSession.executeBatch([ + const initial = await this.dbSession.executeBatch([ { query: `INSERT INTO downloads (product, version, total_count) VALUES (?, ?, 0) @@ -75,12 +82,17 @@ export class DownloadsService { params: [product], }, ]); - const shouldIncrement = Boolean((initial[1]?.results as Array<{ should_increment: number }> | undefined)?.[0]); - const initialVersions = (initial[2]?.results as Array<{ version: string; total_count: number }> | undefined) ?? []; - const mapVersions = (rows: Array<{ version: string; total_count: number }>): VersionDownloadStats[] => - rows.map((row) => ({ version: row.version, count: row.total_count })); + const shouldIncrement = Boolean(initial[1]?.results?.[0]?.should_increment); + const mapVersions = (rows: DownloadBatchRow[]): VersionDownloadStats[] => + rows.map((row) => { + if (row.version === undefined || row.total_count === undefined) { + throw new Error('Database returned an invalid download statistics row'); + } + return { version: row.version, count: row.total_count }; + }); + const initialVersions = mapVersions(initial[2]?.results ?? []); if (!shouldIncrement) { - const versions = mapVersions(initialVersions); + const versions = initialVersions; return { versionCount: versions.find((item) => item.version === version)?.count ?? 0, productTotal: versions.reduce((sum, item) => sum + item.count, 0), @@ -88,7 +100,7 @@ export class DownloadsService { }; } - const [, , updatedVersionRows] = await this.dbSession.executeBatch([ + const [, , updatedVersionRows] = await this.dbSession.executeBatch([ { query: `UPDATE downloads SET total_count = total_count + 1, updated_at = CURRENT_TIMESTAMP @@ -108,9 +120,7 @@ export class DownloadsService { params: [product], }, ]); - const versions = mapVersions( - (updatedVersionRows.results as Array<{ version: string; total_count: number }> | undefined) ?? initialVersions, - ); + const versions = updatedVersionRows.results ? mapVersions(updatedVersionRows.results) : initialVersions; return { versionCount: versions.find((item) => item.version === version)?.count ?? 0, productTotal: versions.reduce((sum, item) => sum + item.count, 0), @@ -131,18 +141,18 @@ export class DownloadsService { /** * Returns download stats for all products. */ - async getAllStats(): Promise { + async getAllStats(): Promise[]> { const rowsRes = await this.dbSession.executeRead( 'SELECT product, version, total_count FROM downloads ORDER BY product, created_at DESC', ); - const byProduct = new Map(); + const byProduct = new Map(); for (const r of rowsRes.results) { - const prod = r.product as InstallerProduct; + const prod = r.product; const list = byProduct.get(prod) || []; list.push({ version: r.version, count: r.total_count }); byProduct.set(prod, list); } - const all: ProductDownloadStats[] = []; + const all: ProductDownloadStats[] = []; for (const [product, versions] of byProduct.entries()) { const total = versions.reduce((a, b) => a + b.count, 0); all.push({ product, total, versions }); diff --git a/src/services/errors.ts b/src/services/errors.ts index ba2ae29..15556e6 100644 --- a/src/services/errors.ts +++ b/src/services/errors.ts @@ -1,10 +1,15 @@ // Lightweight HTTP error for controlled responses through Hono's global onError +export interface HttpErrorDetails { + statusText?: string; + cause?: unknown; +} + export class HttpError extends Error { status: number; - details?: unknown; + details?: HttpErrorDetails; expose: boolean; - constructor(status: number, message: string, details?: unknown, expose = true) { + constructor(status: number, message: string, details?: HttpErrorDetails, expose = true) { super(message); this.name = 'HttpError'; this.status = status; diff --git a/src/services/faqs.ts b/src/services/faqs.ts index e555268..784ba2c 100644 --- a/src/services/faqs.ts +++ b/src/services/faqs.ts @@ -33,19 +33,19 @@ export class FAQService { async create(data: { question: string; answer: string; order_position: number }): Promise { const id = crypto.randomUUID(); - const result = await this.dbSession.executeWrite( + const result = await this.dbSession.executeWrite( `INSERT INTO faqs (id, question, answer, order_position, created_at, updated_at) VALUES (?, ?, ?, ?, datetime('now'), datetime('now')) RETURNING id, question, answer, order_position, created_at, updated_at`, [id, data.question, data.answer, data.order_position], ); - const created = (result.results as unknown as FAQRecord[] | null)?.[0]; + const created = result.results?.[0]; if (!created) throw new Error('Failed to create FAQ'); return created; } async update(id: string, data: Partial<{ question: string; answer: string; order_position: number }>): Promise { - const result = await this.dbSession.executeWrite( + const result = await this.dbSession.executeWrite( `UPDATE faqs SET question = CASE WHEN ? THEN ? ELSE question END, answer = CASE WHEN ? THEN ? ELSE answer END, @@ -63,7 +63,7 @@ export class FAQService { id, ], ); - return (result.results as unknown as FAQRecord[] | null)?.[0] ?? null; + return result.results?.[0] ?? null; } async delete(id: string): Promise { diff --git a/src/services/github.ts b/src/services/github.ts index 7f8e60d..baa3e98 100644 --- a/src/services/github.ts +++ b/src/services/github.ts @@ -4,6 +4,10 @@ import { cancelResponseBody } from './http'; +interface GitHubRequestHeaders { + [name: string]: string; +} + interface GitHubContributor { id: number; login: string; @@ -105,8 +109,8 @@ export class GitHubService { /** * Get common headers for GitHub API requests */ - private getHeaders(): Record { - const headers: Record = { + private getHeaders(): GitHubRequestHeaders { + const headers: GitHubRequestHeaders = { 'User-Agent': 'BARS-API', Accept: 'application/vnd.github.v3+json', }; @@ -149,7 +153,6 @@ export class GitHubService { const allContributors = new Map(); const repoData: ContributorsData['repositories'] = []; - // Get all organization repositories const orgRepos = await this.getOrganizationRepositories(); const publicRepos = orgRepos.filter((repo) => !repo.private); const concurrency = 10; @@ -215,10 +218,8 @@ export class GitHubService { }); } - // Convert Map to Array and sort by contributions const contributorList = Array.from(allContributors.values()).sort((a, b) => b.contributions - a.contributions); - // Sort repositories by stars repoData.sort((a, b) => b.stars - a.stars); const totalContributions = contributorList.reduce((sum, contributor) => sum + contributor.contributions, 0); diff --git a/src/services/lights-cache.ts b/src/services/lights-cache.ts index 6c03001..660f20e 100644 --- a/src/services/lights-cache.ts +++ b/src/services/lights-cache.ts @@ -1,15 +1,17 @@ import { ServicePool } from './service-pool'; export type RadarLight = { stateId: number | null; offStateId: number | null; position: [number, number]; heading: number }; +export interface LightsByObject { + [objectId: string]: RadarLight[]; +} const LIGHT_STATE_ID_PATTERN = /stateId\s*=\s*"(\d+)"/i; const LIGHT_OFF_STATE_ID_PATTERN = /offStateId\s*=\s*"(\d+)"/i; const LIGHT_POSITION_PATTERN = /\s*([^<]+)\s*<\/Position>/i; const LIGHT_HEADING_PATTERN = /\s*([^<]+)\s*<\/Heading>/i; -// Parse BARS Lights XML into objectId -> lights[] mapping -export function parseBarsLightsXml(xml: string): Record { - const result: Record = {}; +export function parseBarsLightsXml(xml: string): LightsByObject { + const result: LightsByObject = {}; if (!xml) return result; const objRegex = /]*id="([^"]+)"[^>]*>([\s\S]*?)<\/BarsObject>/gi; @@ -46,15 +48,30 @@ export function parseBarsLightsXml(xml: string): Record { } // Fetch and cache latest lights mapping for an airport (15 minutes TTL) -export async function getLightsByObject(env: Env, icao: string): Promise> { +export async function getLightsByObject(env: Env, icao: string): Promise { const normalizedIcao = icao.toUpperCase(); const cache = ServicePool.getCache(env); const cacheKey = `lights-map-${normalizedIcao}`; - const cached = await cache.get>(cacheKey, 'airports'); + const cached = await cache.get(cacheKey, 'airports'); if (cached) return cached; const storage = ServicePool.getStorage(env); try { + const approved = await ServicePool.getContributions(env).listContributionMetadata({ + status: 'approved', + airportIcao: normalizedIcao, + }); + for (const contribution of approved.contributions) { + if (!contribution.barsArtifactKey) continue; + const published = await storage.getFile(contribution.barsArtifactKey); + if (!published) continue; + const mapping = parseBarsLightsXml(await published.text()); + await cache.set(cacheKey, mapping, { ttl: 900, namespace: 'airports' }); + return mapping; + } + + // Backward compatibility for approved YSCB/YSSY rows published before + // immutable artifact keys were recorded on contributions. const list = await storage.listFiles(`Maps/${normalizedIcao}_`, 50); if (!list.objects || list.objects.length === 0) { await cache.set(cacheKey, {}, { ttl: 300, namespace: 'airports' }); diff --git a/src/services/msfs-removals.ts b/src/services/msfs-removals.ts new file mode 100644 index 0000000..5f54542 --- /dev/null +++ b/src/services/msfs-removals.ts @@ -0,0 +1,824 @@ +export interface PolygonVertex { + lat: number; + lon: number; +} + +interface RemovalTarget extends PolygonVertex { + heading: number; + supportSizeMeters?: number; +} + +interface ExclusionFlags { + excludeLibraryObjects: boolean; + excludeVFX: boolean; + excludeSimPropContainers: boolean; +} + +export interface RemovalPolygon { + id: string; + vertices: PolygonVertex[]; + altitude: number; + targets: RemovalTarget[]; + exclusionFlags: ExclusionFlags; + hasPlan: boolean; + mode: 'targets' | 'polygon'; +} + +interface LocalPoint { + x: number; + y: number; +} + +interface Projection { + origin: PolygonVertex; + metersPerDegreeLon: number; + ring: LocalPoint[]; +} + +export interface RemovalSupport { + latitude: number; + longitude: number; + width: number; + length: number; + heading: number; + altitude: number; +} + +export interface RemovalExclusion { + latitudeMinimum: number; + latitudeMaximum: number; + longitudeMinimum: number; + longitudeMaximum: number; + flags: ExclusionFlags; +} + +export interface RemovalArtifacts { + supports: RemovalSupport[]; + exclusions: RemovalExclusion[]; +} + +interface XmlAttributes { + [name: string]: string | undefined; +} + +const METERS_PER_DEGREE_LAT = 111_320; +const TARGET_SUPPORT_SIZES_METERS = [0.5, 0.35, 0.25, 0.15, 0.1, 0.05, 0.025, 0.01]; +const MAXIMUM_TARGET_SUPPORT_SIZE_METERS = 0.75; +const MANUAL_STRIP_HEIGHT_METERS = 0.25; +const CONTAINMENT_MARGIN_METERS = 0.005; +const MAXIMUM_MANUAL_STRIPS = 50_000; + +export function parseRemovalPolygons(xmlContent: string): RemovalPolygon[] { + const polygons: RemovalPolygon[] = []; + const polygonPattern = /]*)>([\s\S]*?)<\/Polygon>/gi; + let polygonMatch: RegExpExecArray | null; + while ((polygonMatch = polygonPattern.exec(xmlContent)) !== null) { + const attributes = parseAttributes(polygonMatch[1]); + if (String(attributes.displayName ?? '').toLowerCase() !== 'remove') continue; + + const vertices: PolygonVertex[] = []; + const vertexPattern = /]*)\/?\s*>/gi; + let vertexMatch: RegExpExecArray | null; + while ((vertexMatch = vertexPattern.exec(polygonMatch[2])) !== null) { + const vertexAttributes = parseAttributes(vertexMatch[1]); + const lat = Number(vertexAttributes.lat); + const lon = Number(vertexAttributes.lon); + if (Number.isFinite(lat) && Number.isFinite(lon)) vertices.push({ lat, lon }); + } + + const uniqueVertices = removeClosingAndDuplicateVertices(vertices); + if (uniqueVertices.length < 3) continue; + const altitude = Number(attributes.altitude); + polygons.push({ + id: `remove:${polygons.length}`, + vertices: uniqueVertices, + altitude: Number.isFinite(altitude) ? altitude : 0, + targets: [], + exclusionFlags: emptyExclusionFlags(), + hasPlan: false, + mode: 'targets', + }); + } + + const planPattern = /<(Removal|Target|Object)\b([^>]*)\/?\s*>/gi; + let planMatch: RegExpExecArray | null; + while ((planMatch = planPattern.exec(xmlContent)) !== null) { + const attributes = parseAttributes(planMatch[2]); + const removalIndex = Number(attributes.removalIndex); + const polygon = Number.isInteger(removalIndex) ? polygons[removalIndex] : undefined; + if (!polygon) continue; + polygon.hasPlan = true; + const planType = planMatch[1].toLowerCase(); + if (planType === 'removal') { + polygon.mode = attributes.mode === 'polygon' ? 'polygon' : 'targets'; + continue; + } + if (planType === 'target') { + const lat = Number(attributes.lat); + const lon = Number(attributes.lon); + const heading = Number(attributes.heading); + const supportSizeMeters = Number(attributes.supportSizeMeters); + if (!Number.isFinite(lat) || !Number.isFinite(lon)) continue; + polygon.targets.push({ + lat, + lon, + heading: Number.isFinite(heading) ? heading : 0, + ...(Number.isFinite(supportSizeMeters) && supportSizeMeters > 0 + ? { supportSizeMeters: Math.min(supportSizeMeters, MAXIMUM_TARGET_SUPPORT_SIZE_METERS) } + : {}), + }); + continue; + } + polygon.exclusionFlags = { + excludeLibraryObjects: xmlBoolean(attributes.excludeLibraryObjects), + excludeVFX: xmlBoolean(attributes.excludeVFX), + excludeSimPropContainers: xmlBoolean(attributes.excludeSimPropContainers), + }; + } + + for (const polygon of polygons) polygon.targets = deduplicateTargets(polygon.targets); + return polygons; +} + +export function buildRemovalArtifacts(polygons: RemovalPolygon[], options: { alignPolygonSupports?: boolean; coverPolygonEnds?: boolean } = {}): RemovalArtifacts { + const supports: RemovalSupport[] = []; + const exclusions: RemovalExclusion[] = []; + const targetSignatures = new Set(); + + for (const polygon of polygons) { + const polygonTargets = (polygon.mode === 'targets' ? polygon.targets : []).filter((target) => { + const signature = `${target.lat.toFixed(10)}:${target.lon.toFixed(10)}`; + if (targetSignatures.has(signature)) return false; + targetSignatures.add(signature); + return true; + }); + const polygonSupports = + polygon.hasPlan && polygon.mode === 'targets' + ? calculateTargetSupports({ ...polygon, targets: polygonTargets }) + : polygon.hasPlan && polygon.mode === 'polygon' + ? calculatePolygonFallbackSupports(polygon, options.alignPolygonSupports === true, options.coverPolygonEnds === true) + : calculateManualSupports(polygon); + for (const support of polygonSupports) supports.push({ ...support, altitude: polygon.altitude }); + + if (hasExclusionFlags(polygon.exclusionFlags)) { + const exclusion = exclusionForPolygon(polygon); + if (!exclusion) throw new Error(`Object exclusion ${polygon.id} is not safely axis-aligned`); + exclusions.push({ ...exclusion, flags: polygon.exclusionFlags }); + } else if (!polygon.hasPlan) { + for (const support of polygonSupports) { + exclusions.push({ + ...exclusionForAxisAlignedSupport(support), + flags: { ...emptyExclusionFlags(), excludeLibraryObjects: true }, + }); + } + } + } + return { supports, exclusions }; +} + +export function calculateAirportTestRadius(airport: PolygonVertex, supports: RemovalSupport[], exclusions: RemovalExclusion[]): number { + let furthest = 0; + for (const support of supports) { + furthest = Math.max( + furthest, + distanceMeters(airport, { lat: support.latitude, lon: support.longitude }) + Math.hypot(support.width, support.length) / 2, + ); + } + for (const exclusion of exclusions) { + for (const point of [ + { lat: exclusion.latitudeMinimum, lon: exclusion.longitudeMinimum }, + { lat: exclusion.latitudeMaximum, lon: exclusion.longitudeMaximum }, + ]) { + furthest = Math.max(furthest, distanceMeters(airport, point)); + } + } + return Math.max(1000, Math.ceil(furthest + 100)); +} + +function calculateTargetSupports(polygon: RemovalPolygon): Omit[] { + const projection = projectPolygon(polygon.vertices); + if (!projection) return []; + const supports: Omit[] = []; + for (const target of polygon.targets) { + const targetPoint = projectPoint(target, projection); + const radians = (target.heading * Math.PI) / 180; + const along = { x: Math.sin(radians), y: Math.cos(radians) }; + const across = { x: Math.cos(radians), y: -Math.sin(radians) }; + let selected: Omit | undefined; + + for (const size of targetSupportSizes(target)) { + const half = size / 2; + for (const targetAlong of [0, half, -half]) { + for (const targetAcross of [0, half, -half]) { + const center = { + x: targetPoint.x - along.x * targetAlong - across.x * targetAcross, + y: targetPoint.y - along.y * targetAlong - across.y * targetAcross, + }; + if (!rectangleInsideRing(center, along, across, half, half, projection.ring)) continue; + selected = supportFromLocal(center, size, size, target.heading, projection); + break; + } + if (selected) break; + } + if (selected) break; + } + + if (!selected) { + continue; + } + supports.push(selected); + } + return supports; +} + +function targetSupportSizes(target: RemovalTarget): number[] { + const preferred = Number(target.supportSizeMeters); + if (!Number.isFinite(preferred) || preferred <= TARGET_SUPPORT_SIZES_METERS[0]) { + return TARGET_SUPPORT_SIZES_METERS; + } + const capped = Math.min(preferred, MAXIMUM_TARGET_SUPPORT_SIZE_METERS); + return [capped, ...TARGET_SUPPORT_SIZES_METERS.filter((size) => size < capped)]; +} + +function calculatePolygonFallbackSupports(polygon: RemovalPolygon, alignToPolygon: boolean, coverEnds: boolean): Omit[] { + const rectangleProjection = alignToPolygon ? projectPolygon(polygon.vertices) : undefined; + if (rectangleProjection) { + let ring = rectangleProjection.ring; + let changed = true; + while (changed && ring.length > 4) { + changed = false; + for (let index = 0; index < ring.length; index++) { + if (pointToSegmentDistance(ring[index], ring[(index + ring.length - 1) % ring.length], ring[(index + 1) % ring.length]) > 0.0001) continue; + ring = [...ring.slice(0, index), ...ring.slice(index + 1)]; + changed = true; + break; + } + } + rectangleProjection.ring = ring; + } + const rectangle = calculateContainedRectangleSupport(polygon, rectangleProjection ?? undefined); + if (rectangle) return [rectangle]; + const original = calculateManualSupports(polygon); + if (!alignToPolygon) return original; + const originalArea = original.reduce((sum, support) => sum + support.width * support.length, 0); + const best = original; + const sections = corridorSections(polygon); + if (sections) { + const parentProjection = projectPolygon(polygon.vertices)!; + for (let refinement = 0; refinement <= 7; refinement++) { + const fitted = sections.flatMap((section) => { + const projection = { ...parentProjection, ring: section.vertices.map((point) => projectPoint(point, parentProjection)) }; + const rectangle = calculateContainedRectangleSupport(section, projection) ?? calculateCorridorRectangleSupport(projection); + return rectangle ? [rectangle] : calculateManualSupports(section, true, 8 / 2 ** refinement, projection); + }); + fitted.push(...calculateCorridorJoinSupports(parentProjection)); + const area = fitted.reduce((sum, support) => sum + support.width * support.length, 0); + if (area >= originalArea - 1e-6) return coverEnds ? [...fitted, ...calculateCorridorEndSupports(parentProjection)] : fitted; + } + } + for (let refinement = 0; refinement <= 6; refinement++) { + const longitudinal = calculateLongitudinalPolygonSupports(polygon, refinement); + const area = longitudinal.reduce((sum, support) => sum + support.width * support.length, 0); + if (area >= originalArea - 1e-6) return longitudinal; + } + for (let refinement = 0; refinement <= 5; refinement++) { + const aligned = calculateManualSupports(polygon, true, MANUAL_STRIP_HEIGHT_METERS / 2 ** refinement); + if (aligned.length >= best.length) return best; + const area = aligned.reduce((sum, support) => sum + support.width * support.length, 0); + if (area >= originalArea - 1e-6) return aligned; + } + return best; +} + +function calculateLongitudinalPolygonSupports(polygon: RemovalPolygon, refinement: number): Omit[] { + const projection = projectPolygon(polygon.vertices); + if (!projection) return []; + // Cut at changes along the path so each rectangle spans its available width. + const angle = minimumWidthSweepAngle(projection.ring) + Math.PI / 2; + const cos = Math.cos(angle), sin = Math.sin(angle); + const ring = projection.ring.map(point => ({ x: point.x * cos + point.y * sin, y: -point.x * sin + point.y * cos })); + const events = [...new Set(ring.map(point => point.y))].sort((a, b) => a - b); + const supports: Omit[] = []; + const addBand = (y0: number, y1: number) => { + if (y1 <= y0) return; + const middleY = (y0 + y1) / 2; + let intervals = intersectIntervals(intervalsAtY(ring, y0), intervalsAtY(ring, y1)); + intervals = intersectIntervals(intervals, intervalsAtY(ring, middleY)); + for (const y of events) { + if (y > y0 && y < y1) intervals = intersectIntervals(intervals, intervalsAtY(ring, y)); + } + for (const [left, right] of intervals) { + const halfLength = (right - left) / 2 - 0.001; + const halfWidth = (y1 - y0) / 2; + if (halfLength <= 0.005) continue; + const center = { x: (left + right) / 2, y: middleY }; + if (!rectangleInsideRing(center, { x: 1, y: 0 }, { x: 0, y: -1 }, halfLength, halfWidth, ring)) continue; + supports.push(supportFromLocal( + { x: center.x * cos - center.y * sin, y: center.x * sin + center.y * cos }, + halfWidth * 2, halfLength * 2, 90 - angle * 180 / Math.PI, projection, + )); + } + }; + const intervalWidth = (intervals: [number, number][]) => intervals.reduce((sum, [left, right]) => sum + right - left, 0); + for (let event = 1; event < events.length; event++) { + const startY = events[event - 1], endY = events[event]; + if (endY - startY <= 0.002) continue; + const startIntervals = intervalsAtY(ring, startY + 0.001); + const endIntervals = intervalsAtY(ring, endY - 0.001); + const meanWidth = (intervalWidth(startIntervals) + intervalWidth(endIntervals)) / 2; + const containedWidth = intervalWidth(intersectIntervals(startIntervals, endIntervals)); + // Keep nearly rectangular sections whole; refine only the taper at clipped ends. + const subdivisions = containedWidth >= meanWidth * 0.999 ? 1 : 2 ** refinement; + const step = (endY - startY) / subdivisions; + if (step <= 0.002) continue; + for (let index = 0; index < subdivisions; index++) { + const overlap = Math.min(0.01, step / 10); + const y0 = startY + index * step + (index > 0 ? -overlap : 0.001); + const y1 = startY + (index + 1) * step + (index < subdivisions - 1 ? overlap : -0.001); + addBand(y0, y1); + } + } + for (let event = 1; event < events.length - 1; event++) { + const half = Math.min(0.01, (events[event] - events[event - 1]) / 4, (events[event + 1] - events[event]) / 4); + if (half > 0.001) addBand(events[event] - half, events[event] + half); + } + return supports; +} + +function calculateCorridorRectangleSupport(projection: Projection): Omit | undefined { + const ring = projection.ring; + const start = { x: (ring[0].x + ring[3].x) / 2, y: (ring[0].y + ring[3].y) / 2 }; + const end = { x: (ring[1].x + ring[2].x) / 2, y: (ring[1].y + ring[2].y) / 2 }; + const length = Math.hypot(end.x - start.x, end.y - start.y); + if (length < 0.02) return undefined; + const along = { x: (end.x - start.x) / length, y: (end.y - start.y) / length }; + const across = { x: along.y, y: -along.x }; + const center = { x: (start.x + end.x) / 2, y: (start.y + end.y) / 2 }; + const halfWidth = Math.min(...ring.map(point => Math.abs((point.x - center.x) * across.x + (point.y - center.y) * across.y))) - 0.001; + if (halfWidth <= 0.01) return undefined; + let low = 0, high = length / 2; + for (let iteration = 0; iteration < 32; iteration++) { + const middle = (low + high) / 2; + if (rectangleInsideRing(center, along, across, middle, halfWidth, ring)) low = middle; + else high = middle; + } + if (low < length * 0.4) return undefined; + return supportFromLocal(center, halfWidth * 2, Math.max(0, low * 2 - 0.002), Math.atan2(along.x, along.y) * 180 / Math.PI, projection); +} + +function calculateCorridorJoinSupports(projection: Projection): Omit[] { + const supports: Omit[] = []; + const ring = projection.ring; + for (let index = 1; index < ring.length / 2 - 1; index++) { + const left = ring[index], right = ring[ring.length - 1 - index]; + const center = { x: (left.x + right.x) / 2, y: (left.y + right.y) / 2 }; + const width = Math.hypot(left.x - right.x, left.y - right.y); + if (width <= 0.02) continue; + const across = { x: (left.x - right.x) / width, y: (left.y - right.y) / width }; + const along = { x: -across.y, y: across.x }; + let half = width / 2 - 0.001; + while (half >= 0.01 && !rectangleInsideRing(center, along, across, half, half, ring)) half *= 0.8; + if (half < 0.01) continue; + supports.push(supportFromLocal(center, half * 2, half * 2, Math.atan2(along.x, along.y) * 180 / Math.PI, projection)); + } + return supports; +} + +function calculateCorridorEndSupports(projection: Projection): Omit[] { + const supports: Omit[] = []; + const ring = projection.ring; + const last = ring.length / 2 - 1; + for (const index of [0, last]) { + const left = ring[index], right = ring[ring.length - 1 - index]; + const endpoint = { x: (left.x + right.x) / 2, y: (left.y + right.y) / 2 }; + const nextIndex = index === 0 ? 1 : last - 1; + const nextLeft = ring[nextIndex], nextRight = ring[ring.length - 1 - nextIndex]; + const inward = { x: (nextLeft.x + nextRight.x) / 2 - endpoint.x, y: (nextLeft.y + nextRight.y) / 2 - endpoint.y }; + const width = Math.hypot(left.x - right.x, left.y - right.y); + if (width <= 0.02) continue; + const across = { x: (left.x - right.x) / width, y: (left.y - right.y) / width }; + const direction = Math.sign(-across.y * inward.x + across.x * inward.y); + const along = { x: -across.y * direction, y: across.x * direction }; + let halfWidth = width / 2 - 0.001; + let halfLength = Math.min(halfWidth, Math.hypot(inward.x, inward.y) / 2); + while (halfWidth >= 0.01 && halfLength >= 0.01) { + const center = { x: endpoint.x + along.x * (halfLength + 0.001), y: endpoint.y + along.y * (halfLength + 0.001) }; + if (rectangleInsideRing(center, along, across, halfLength, halfWidth, ring)) { + supports.push(supportFromLocal(center, halfWidth * 2, halfLength * 2, Math.atan2(along.x, along.y) * 180 / Math.PI, projection)); + break; + } + halfWidth *= 0.8; + halfLength *= 0.8; + } + } + return supports; +} + +function corridorSections(polygon: RemovalPolygon): RemovalPolygon[] | undefined { + const projection = projectPolygon(polygon.vertices); + if (!projection || projection.ring.length % 2 !== 0) return undefined; + const ring = projection.ring; + const signedArea = (points: LocalPoint[]) => + points.reduce((sum, point, index) => { + const next = points[(index + 1) % points.length]; + return sum + point.x * next.y - next.x * point.y; + }, 0) / 2; + const winding = Math.sign(signedArea(ring)); + if (!winding) return undefined; + const sections: RemovalPolygon[] = []; + for (let index = 0; index < ring.length / 2 - 1; index++) { + const indexes = [index, index + 1, ring.length - 2 - index, ring.length - 1 - index]; + const quad = indexes.map((i) => ring[i]); + if (signedArea(quad) * winding <= 1e-8) return undefined; + for (let edge = 0; edge < 4; edge++) { + const start = quad[edge], + end = quad[(edge + 1) % 4]; + if (orientation(start, end, quad[(edge + 2) % 4]) * winding < -1e-8) return undefined; + if (!pointInRing({ x: (start.x + end.x) / 2, y: (start.y + end.y) / 2 }, ring)) return undefined; + for (let boundary = 0; boundary < ring.length; boundary++) { + if (segmentsProperlyIntersect(start, end, ring[boundary], ring[(boundary + 1) % ring.length])) return undefined; + } + } + sections.push({ ...polygon, vertices: indexes.map((i) => polygon.vertices[i]) }); + } + return sections; +} + +function calculateContainedRectangleSupport( + polygon: RemovalPolygon, + sectionProjection?: Projection, +): Omit | undefined { + const projection = sectionProjection ?? projectPolygon(polygon.vertices); + if (!projection || projection.ring.length !== 4) return undefined; + const ring = projection.ring; + let longestEdgeIndex = 0; + let longestEdgeLength = 0; + for (let index = 0; index < ring.length; index++) { + const next = ring[(index + 1) % ring.length]; + const length = Math.hypot(next.x - ring[index].x, next.y - ring[index].y); + if (length > longestEdgeLength) { + longestEdgeIndex = index; + longestEdgeLength = length; + } + } + if (longestEdgeLength <= CONTAINMENT_MARGIN_METERS * 2) return undefined; + + const edgeStart = ring[longestEdgeIndex]; + const edgeEnd = ring[(longestEdgeIndex + 1) % ring.length]; + const along = { + x: (edgeEnd.x - edgeStart.x) / longestEdgeLength, + y: (edgeEnd.y - edgeStart.y) / longestEdgeLength, + }; + const across = { x: along.y, y: -along.x }; + const center = { + x: ring.reduce((sum, point) => sum + point.x, 0) / ring.length, + y: ring.reduce((sum, point) => sum + point.y, 0) / ring.length, + }; + const alongOffsets = ring.map((point) => (point.x - center.x) * along.x + (point.y - center.y) * along.y); + const acrossOffsets = ring.map((point) => (point.x - center.x) * across.x + (point.y - center.y) * across.y); + const rawHalfLength = (Math.max(...alongOffsets) - Math.min(...alongOffsets)) / 2; + const rawHalfWidth = (Math.max(...acrossOffsets) - Math.min(...acrossOffsets)) / 2; + const rectangleInsetMeters = 0.001; + const halfLength = rawHalfLength - rectangleInsetMeters; + const halfWidth = rawHalfWidth - rectangleInsetMeters; + if (halfLength <= CONTAINMENT_MARGIN_METERS || halfWidth <= CONTAINMENT_MARGIN_METERS) return undefined; + + const expectedCorners = [ + combineAxes(center, along, across, -rawHalfLength, -rawHalfWidth), + combineAxes(center, along, across, rawHalfLength, -rawHalfWidth), + combineAxes(center, along, across, rawHalfLength, rawHalfWidth), + combineAxes(center, along, across, -rawHalfLength, rawHalfWidth), + ]; + const cornerToleranceMeters = 0.02; + if ( + !ring.every((point) => + expectedCorners.some((corner) => Math.hypot(point.x - corner.x, point.y - corner.y) <= cornerToleranceMeters), + ) || + !expectedCorners.every((corner) => + ring.some((point) => Math.hypot(point.x - corner.x, point.y - corner.y) <= cornerToleranceMeters), + ) + ) { + return undefined; + } + if (!rectangleInsideRing(center, along, across, halfLength, halfWidth, ring)) return undefined; + + const heading = ((Math.atan2(along.x, along.y) * 180) / Math.PI + 360) % 360; + return supportFromLocal(center, halfWidth * 2, halfLength * 2, heading, projection); +} + +function calculateManualSupports( + polygon: RemovalPolygon, + alignToPolygon = false, + requestedStripHeight = MANUAL_STRIP_HEIGHT_METERS, + sectionProjection?: Projection, +): Omit[] { + const projection = sectionProjection ?? projectPolygon(polygon.vertices); + if (!projection) return []; + const angle = alignToPolygon ? minimumWidthSweepAngle(projection.ring) : 0; + const cos = Math.cos(angle); + const sin = Math.sin(angle); + const ring = projection.ring.map((point) => ({ x: point.x * cos + point.y * sin, y: -point.x * sin + point.y * cos })); + const minY = Math.min(...ring.map((point) => point.y)); + const maxY = Math.max(...ring.map((point) => point.y)); + const spanY = maxY - minY; + if (spanY <= CONTAINMENT_MARGIN_METERS * 2) return []; + const stripHeight = Math.max(requestedStripHeight, spanY / MAXIMUM_MANUAL_STRIPS); + const rowCount = Math.max(1, Math.ceil(spanY / stripHeight)); + const actualHeight = spanY / rowCount; + // Refined strips need a proportional inset so their gaps do not consume the recovered coverage. + const inset = alignToPolygon ? Math.min(0.001, actualHeight / 1000) : CONTAINMENT_MARGIN_METERS; + const supports: Omit[] = []; + + for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) { + const overlap = alignToPolygon ? Math.min(0.01, actualHeight / 10) : 0; + const y0 = minY + rowIndex * actualHeight + (rowIndex > 0 && alignToPolygon ? -overlap : inset); + const y1 = minY + (rowIndex + 1) * actualHeight + (rowIndex < rowCount - 1 && alignToPolygon ? overlap : -inset); + if (y1 <= y0) continue; + const middleY = (y0 + y1) / 2; + let intervals = intervalsAtY(ring, y0); + intervals = intersectIntervals(intervals, intervalsAtY(ring, middleY)); + intervals = intersectIntervals(intervals, intervalsAtY(ring, y1)); + + for (const [rawStart, rawEnd] of intervals) { + const start = rawStart + CONTAINMENT_MARGIN_METERS; + const end = rawEnd - CONTAINMENT_MARGIN_METERS; + if (end - start <= CONTAINMENT_MARGIN_METERS * 2) continue; + const center = { x: (start + end) / 2, y: middleY }; + const halfLength = (end - start) / 2; + const halfWidth = (y1 - y0) / 2; + if (!rectangleInsideRing(center, { x: 1, y: 0 }, { x: 0, y: -1 }, halfLength, halfWidth, ring)) { + continue; + } + supports.push( + supportFromLocal( + { x: center.x * cos - center.y * sin, y: center.x * sin + center.y * cos }, + halfWidth * 2, + halfLength * 2, + 90 - (angle * 180) / Math.PI, + projection, + ), + ); + } + } + return supports; +} + +function minimumWidthSweepAngle(ring: LocalPoint[]): number { + let bestAngle = 0; + let bestWidth = Math.max(...ring.map((point) => point.y)) - Math.min(...ring.map((point) => point.y)); + // Sweep across the corridor, rather than generating a band for every 25 cm along it. + for (let index = 0; index < ring.length; index++) { + const start = ring[index]; + const end = ring[(index + 1) % ring.length]; + if (Math.hypot(end.x - start.x, end.y - start.y) < CONTAINMENT_MARGIN_METERS) continue; + const angle = Math.atan2(end.y - start.y, end.x - start.x); + const sin = Math.sin(angle); + const cos = Math.cos(angle); + let minimum = Infinity; + let maximum = -Infinity; + for (const point of ring) { + const offset = -point.x * sin + point.y * cos; + minimum = Math.min(minimum, offset); + maximum = Math.max(maximum, offset); + } + if (maximum - minimum < bestWidth - 1e-6) { + bestWidth = maximum - minimum; + bestAngle = angle; + } + } + return bestAngle; +} + +function parseAttributes(source: string): XmlAttributes { + const attributes: XmlAttributes = {}; + const pattern = /([A-Za-z_:][\w:.-]*)\s*=\s*(["'])(.*?)\2/g; + let match: RegExpExecArray | null; + while ((match = pattern.exec(source)) !== null) attributes[match[1]] = match[3]; + return attributes; +} + +function xmlBoolean(value: string | undefined): boolean { + return String(value ?? '').toLowerCase() === 'true'; +} + +function emptyExclusionFlags(): ExclusionFlags { + return { + excludeLibraryObjects: false, + excludeVFX: false, + excludeSimPropContainers: false, + }; +} + +function hasExclusionFlags(flags: ExclusionFlags): boolean { + return flags.excludeLibraryObjects || flags.excludeVFX || flags.excludeSimPropContainers; +} + +function removeClosingAndDuplicateVertices(vertices: PolygonVertex[]): PolygonVertex[] { + const result: PolygonVertex[] = []; + for (const vertex of vertices) { + const previous = result[result.length - 1]; + if (!previous || Math.abs(previous.lat - vertex.lat) > 1e-12 || Math.abs(previous.lon - vertex.lon) > 1e-12) { + result.push(vertex); + } + } + if (result.length > 1) { + const first = result[0]; + const last = result[result.length - 1]; + if (Math.abs(first.lat - last.lat) <= 1e-12 && Math.abs(first.lon - last.lon) <= 1e-12) { + result.pop(); + } + } + return result; +} + +function deduplicateTargets(targets: RemovalTarget[]): RemovalTarget[] { + const seen = new Set(); + return targets.filter((target) => { + const signature = `${target.lat.toFixed(10)}:${target.lon.toFixed(10)}`; + if (seen.has(signature)) return false; + seen.add(signature); + return true; + }); +} + +function projectPolygon(vertices: PolygonVertex[]): Projection | undefined { + if (vertices.length < 3) return undefined; + const origin = { + lat: vertices.reduce((sum, point) => sum + point.lat, 0) / vertices.length, + lon: vertices.reduce((sum, point) => sum + point.lon, 0) / vertices.length, + }; + const metersPerDegreeLon = METERS_PER_DEGREE_LAT * Math.max(0.001, Math.cos((origin.lat * Math.PI) / 180)); + return { + origin, + metersPerDegreeLon, + ring: vertices.map((point) => ({ + x: (point.lon - origin.lon) * metersPerDegreeLon, + y: (point.lat - origin.lat) * METERS_PER_DEGREE_LAT, + })), + }; +} + +function projectPoint(point: PolygonVertex, projection: Projection): LocalPoint { + return { + x: (point.lon - projection.origin.lon) * projection.metersPerDegreeLon, + y: (point.lat - projection.origin.lat) * METERS_PER_DEGREE_LAT, + }; +} + +function supportFromLocal( + center: LocalPoint, + width: number, + length: number, + heading: number, + projection: Projection, +): Omit { + return { + latitude: projection.origin.lat + center.y / METERS_PER_DEGREE_LAT, + longitude: projection.origin.lon + center.x / projection.metersPerDegreeLon, + width, + length, + heading: ((heading % 360) + 360) % 360, + }; +} + +function rectangleInsideRing( + center: LocalPoint, + along: LocalPoint, + across: LocalPoint, + halfLength: number, + halfWidth: number, + ring: LocalPoint[], +): boolean { + const corners = [ + combineAxes(center, along, across, -halfLength, -halfWidth), + combineAxes(center, along, across, halfLength, -halfWidth), + combineAxes(center, along, across, halfLength, halfWidth), + combineAxes(center, along, across, -halfLength, halfWidth), + ]; + if (!corners.every((corner) => pointInRing(corner, ring))) return false; + for (let rectangleIndex = 0; rectangleIndex < corners.length; rectangleIndex++) { + const start = corners[rectangleIndex]; + const end = corners[(rectangleIndex + 1) % corners.length]; + for (let ringIndex = 0; ringIndex < ring.length; ringIndex++) { + if (segmentsProperlyIntersect(start, end, ring[ringIndex], ring[(ringIndex + 1) % ring.length])) { + return false; + } + } + } + return true; +} + +function combineAxes(center: LocalPoint, along: LocalPoint, across: LocalPoint, alongDistance: number, acrossDistance: number): LocalPoint { + return { + x: center.x + along.x * alongDistance + across.x * acrossDistance, + y: center.y + along.y * alongDistance + across.y * acrossDistance, + }; +} + +function pointInRing(point: LocalPoint, ring: LocalPoint[]): boolean { + let inside = false; + for (let index = 0, previous = ring.length - 1; index < ring.length; previous = index++) { + const start = ring[previous]; + const end = ring[index]; + if (pointToSegmentDistance(point, start, end) <= 0.0005) return true; + if (start.y > point.y !== end.y > point.y && point.x < start.x + ((point.y - start.y) * (end.x - start.x)) / (end.y - start.y)) { + inside = !inside; + } + } + return inside; +} + +function pointToSegmentDistance(point: LocalPoint, start: LocalPoint, end: LocalPoint): number { + const dx = end.x - start.x; + const dy = end.y - start.y; + const lengthSquared = dx * dx + dy * dy; + const ratio = + lengthSquared <= 1e-12 ? 0 : Math.max(0, Math.min(1, ((point.x - start.x) * dx + (point.y - start.y) * dy) / lengthSquared)); + return Math.hypot(point.x - (start.x + dx * ratio), point.y - (start.y + dy * ratio)); +} + +function segmentsProperlyIntersect(a: LocalPoint, b: LocalPoint, c: LocalPoint, d: LocalPoint): boolean { + const abC = orientation(a, b, c); + const abD = orientation(a, b, d); + const cdA = orientation(c, d, a); + const cdB = orientation(c, d, b); + const epsilon = 1e-9; + return abC * abD < -epsilon && cdA * cdB < -epsilon; +} + +function orientation(a: LocalPoint, b: LocalPoint, c: LocalPoint): number { + return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); +} + +function intervalsAtY(ring: LocalPoint[], y: number): Array<[number, number]> { + const intersections: number[] = []; + for (let index = 0; index < ring.length; index++) { + const start = ring[index]; + const end = ring[(index + 1) % ring.length]; + if (start.y > y === end.y > y) continue; + intersections.push(start.x + ((y - start.y) * (end.x - start.x)) / (end.y - start.y)); + } + intersections.sort((left, right) => left - right); + const intervals: Array<[number, number]> = []; + for (let index = 0; index + 1 < intersections.length; index += 2) { + intervals.push([intersections[index], intersections[index + 1]]); + } + return intervals; +} + +function intersectIntervals(left: Array<[number, number]>, right: Array<[number, number]>): Array<[number, number]> { + const result: Array<[number, number]> = []; + let leftIndex = 0; + let rightIndex = 0; + while (leftIndex < left.length && rightIndex < right.length) { + const start = Math.max(left[leftIndex][0], right[rightIndex][0]); + const end = Math.min(left[leftIndex][1], right[rightIndex][1]); + if (end > start) result.push([start, end]); + if (left[leftIndex][1] < right[rightIndex][1]) leftIndex++; + else rightIndex++; + } + return result; +} + +function exclusionForPolygon(polygon: RemovalPolygon): Omit | undefined { + const minLat = Math.min(...polygon.vertices.map((point) => point.lat)); + const maxLat = Math.max(...polygon.vertices.map((point) => point.lat)); + const minLon = Math.min(...polygon.vertices.map((point) => point.lon)); + const maxLon = Math.max(...polygon.vertices.map((point) => point.lon)); + const projection = projectPolygon(polygon.vertices); + if (!projection) return undefined; + const corners = [ + { lat: minLat, lon: minLon }, + { lat: minLat, lon: maxLon }, + { lat: maxLat, lon: maxLon }, + { lat: maxLat, lon: minLon }, + ]; + if (!corners.every((corner) => pointInRing(projectPoint(corner, projection), projection.ring))) { + return undefined; + } + return { + latitudeMinimum: minLat, + latitudeMaximum: maxLat, + longitudeMinimum: minLon, + longitudeMaximum: maxLon, + }; +} + +function exclusionForAxisAlignedSupport(support: Omit): Omit { + const halfLat = support.width / (2 * METERS_PER_DEGREE_LAT); + const metersPerDegreeLon = METERS_PER_DEGREE_LAT * Math.max(0.001, Math.cos((support.latitude * Math.PI) / 180)); + const halfLon = support.length / (2 * metersPerDegreeLon); + return { + latitudeMinimum: support.latitude - halfLat, + latitudeMaximum: support.latitude + halfLat, + longitudeMinimum: support.longitude - halfLon, + longitudeMaximum: support.longitude + halfLon, + }; +} + +function distanceMeters(left: PolygonVertex, right: PolygonVertex): number { + const meanLatitude = ((left.lat + right.lat) * Math.PI) / 360; + const north = (right.lat - left.lat) * METERS_PER_DEGREE_LAT; + const east = (right.lon - left.lon) * METERS_PER_DEGREE_LAT * Math.cos(meanLatitude); + return Math.hypot(east, north); +} diff --git a/src/services/parsing.ts b/src/services/parsing.ts new file mode 100644 index 0000000..9bb9cff --- /dev/null +++ b/src/services/parsing.ts @@ -0,0 +1,22 @@ +export type DecimalNumberInput = string | number | null | undefined; + +export const parseDecimalNumber = (value: DecimalNumberInput): number => { + if (value === null || value === undefined) return Number.NaN; + if (Object(value) === value) return Number.NaN; + return Number(value); +}; + +export const normalizeIsoDateTime = (value: string | number): string | null => { + const stringValue = String(value); + if (stringValue !== value) { + const numericValue = Number(value); + if (!Number.isFinite(numericValue)) return null; + const date = new Date(numericValue); + return Number.isFinite(date.getTime()) ? date.toISOString() : null; + } + + const trimmed = stringValue.trim(); + if (!trimmed) return null; + const timestamp = Date.parse(trimmed); + return Number.isFinite(timestamp) ? new Date(timestamp).toISOString() : null; +}; diff --git a/src/services/points.ts b/src/services/points.ts index a0d7a9e..08d8883 100644 --- a/src/services/points.ts +++ b/src/services/points.ts @@ -4,7 +4,7 @@ import { HttpError } from './errors'; import { Point, PointChangeset, PointData } from '../types'; import { PostHogService } from './posthog'; -import { DatabaseSessionService, PreparedStatement, DatabaseSerializable } from './database-session'; +import { DatabaseSessionService, DatabaseSerializable } from './database-session'; type PointRow = { id: string; @@ -23,18 +23,30 @@ type PointRow = { created_by: string; }; +type Coordinate = Point['coordinates'][number]; +type PointValidationInput = Omit & { + coordinates: Coordinate | Coordinate[]; + elevated?: PointData['elevated'] | 0 | 1; + ihp?: PointData['ihp'] | 0 | 1; +}; + +type PointUpdateColumn = 'type' | 'name' | 'coordinates' | 'directionality' | 'color' | 'elevated' | 'ihp'; +type PointUpdateValue = string | number | boolean | null; + +export type PointChangesetAuthorization = { kind: 'division-member' } | { kind: 'division-data-automation' }; + export class PointsService { private dbSession: DatabaseSessionService; private static parseLinkedTo(value: string | null | undefined): string[] { if (!value) return []; try { - const parsed = JSON.parse(value); + const parsed: string | string[] | null = JSON.parse(value); if (Array.isArray(parsed)) { - return parsed.filter((id): id is string => typeof id === 'string'); + return parsed.filter((id) => Object.prototype.toString.call(id) === '[object String]').map(String); } - if (typeof parsed === 'string') { - return [parsed]; + if (Object.prototype.toString.call(parsed) === '[object String]') { + return [String(parsed)]; } } catch { // Legacy rows stored a single ID directly instead of JSON. @@ -48,32 +60,33 @@ export class PointsService { return uniqueIds.length > 0 ? JSON.stringify(uniqueIds) : null; } - private static isCoordinate(value: unknown): value is { lat: number; lng: number } { - if (!value || typeof value !== 'object') return false; - const obj = value as Record; + private static isCoordinate(value: Coordinate): boolean { return ( - Object.prototype.hasOwnProperty.call(obj, 'lat') && - Object.prototype.hasOwnProperty.call(obj, 'lng') && - typeof obj.lat === 'number' && - typeof obj.lng === 'number' && - obj.lat >= -90 && - obj.lat <= 90 && - obj.lng >= -180 && - obj.lng <= 180 + Number.isFinite(value?.lat) && + Number.isFinite(value?.lng) && + value.lat >= -90 && + value.lat <= 90 && + value.lng >= -180 && + value.lng <= 180 ); } - private static isCoordinateArray(value: unknown): value is Array<{ lat: number; lng: number }> { + private static isCoordinateArray(value: Coordinate | Coordinate[]): value is Coordinate[] { return Array.isArray(value) && value.length > 0 && value.every((v) => PointsService.isCoordinate(v)); } - private stmtSelect: PreparedStatement<{ - id: string; - airportId: string; - }>; - private stmtCheckPointId: PreparedStatement<{ - id: string; - }>; + private static parseCoordinates(value: string): Coordinate[] { + let parsed: Coordinate | Coordinate[] | null; + try { + parsed = JSON.parse(value); + } catch { + return []; + } + + if (parsed === null) return []; + if (PointsService.isCoordinateArray(parsed)) return parsed; + return PointsService.isCoordinate(parsed) ? [parsed] : []; + } constructor( private db: D1Database, @@ -83,14 +96,6 @@ export class PointsService { ) { this.dbSession = new DatabaseSessionService(db); - this.stmtSelect = this.dbSession.prepare( - `SELECT - type, name, coordinates, directionality, color, elevated, ihp, linked_to - FROM points - WHERE id = ? AND airport_id = ?;`, - ['id', 'airportId'], - ); - this.stmtCheckPointId = this.dbSession.prepare('SELECT id FROM points WHERE id = ? LIMIT 1;', ['id']); } async createPoint(airportId: string, userId: string, point: PointData): Promise { @@ -145,7 +150,6 @@ export class PointsService { return newPoint; } async updatePoint(pointId: string, userId: string, updates: Partial): Promise { - // Get existing point const point = await this.getPoint(pointId); if (!point) { throw new HttpError(404, 'Point not found'); @@ -158,41 +162,31 @@ export class PointsService { } // Validate updates - const mergedPoint = { ...point, ...updates } as PointData; + const mergedPoint: PointData = { ...point, ...updates }; this.validatePoint(mergedPoint); // Define allowed fields for updates - const allowedFields = ['type', 'name', 'coordinates', 'directionality', 'color', 'elevated', 'ihp']; - const processedUpdates: Record = {}; - Object.entries(updates).forEach(([key, value]) => { - if (allowedFields.includes(key)) { - if (key === 'coordinates') { - processedUpdates[key] = JSON.stringify((mergedPoint as PointData).coordinates); - } else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { - processedUpdates[key] = value; - } else if (value == null) { - processedUpdates[key] = null; - } - } - }); + const processedUpdates: Partial> = {}; + if (Object.prototype.hasOwnProperty.call(updates, 'type')) processedUpdates.type = updates.type ?? null; + if (Object.prototype.hasOwnProperty.call(updates, 'name')) processedUpdates.name = updates.name ?? null; + if (Object.prototype.hasOwnProperty.call(updates, 'coordinates')) { + processedUpdates.coordinates = JSON.stringify(mergedPoint.coordinates); + } + if (Object.prototype.hasOwnProperty.call(updates, 'directionality')) { + processedUpdates.directionality = updates.directionality ?? null; + } + if (Object.prototype.hasOwnProperty.call(updates, 'color')) processedUpdates.color = updates.color ?? null; + if (Object.prototype.hasOwnProperty.call(updates, 'elevated')) processedUpdates.elevated = updates.elevated ?? null; + if (Object.prototype.hasOwnProperty.call(updates, 'ihp')) processedUpdates.ihp = updates.ihp ?? null; if (Object.keys(processedUpdates).length === 0) { return point; } - const fieldMappings: Record = { - type: 'type', - name: 'name', - coordinates: 'coordinates', - directionality: 'directionality', - color: 'color', - elevated: 'elevated', - ihp: 'ihp', - }; const updateFields = Object.keys(processedUpdates) - .map((field) => `${fieldMappings[field]} = ?`) + .map((field) => `${field} = ?`) .join(', '); - const result = await this.dbSession.executeWrite( + const result = await this.dbSession.executeWrite( ` UPDATE points SET ${updateFields}, updated_at = ? @@ -203,7 +197,7 @@ export class PointsService { [...Object.values(processedUpdates), new Date().toISOString(), pointId], ); - const updatedRow = (result.results as unknown as PointRow[] | null)?.[0]; + const updatedRow = result.results?.[0]; if (!updatedRow) throw new HttpError(404, 'Point not found'); const finalPoint = this.mapPointFromDb(updatedRow); try { @@ -219,7 +213,7 @@ export class PointsService { return finalPoint; } - async deletePoint(pointId: string, userId: string): Promise { + async deletePoint(pointId: string, userId: string): Promise { // Get point to check permissions const point = await this.getPoint(pointId); if (!point) { @@ -245,24 +239,40 @@ export class PointsService { } catch (e) { console.warn('Posthog track failed (Point Deleted)', e); } + return point.airportId; } - async applyChangeset(airportId: string, userId: string, changeset: PointChangeset): Promise { - const hasDivisionAccess = await this.divisions.userHasAirportAccess(userId, airportId); - if (!hasDivisionAccess) { - throw new HttpError(403, 'Forbidden: You do not have permission to apply this changeset'); + async applyChangeset( + airportId: string, + userId: string, + changeset: PointChangeset, + authorization: PointChangesetAuthorization = { kind: 'division-member' }, + ): Promise { + if (authorization.kind === 'division-member') { + const hasDivisionAccess = await this.divisions.userHasAirportAccess(userId, airportId); + if (!hasDivisionAccess) { + throw new HttpError(403, 'Forbidden: You do not have permission to apply this changeset'); + } } const modifyEntries = Object.entries(changeset.modify ?? {}); - const selects = modifyEntries.map(([id]) => this.stmtSelect.bindAll({ id, airportId })); - const selectResults = await this.dbSession.executeBatch(selects); - const modifyContexts = modifyEntries.map(([id, patch], index) => { - const rows = selectResults[index]?.results as unknown as PointRow[] | null; - const first = rows && rows[0]; - if (!first) { + const modifyIds = modifyEntries.map(([id]) => id); + const modifyRows: PointRow[] = []; + const maxModifyIdsPerQuery = 99; + for (let index = 0; index < modifyIds.length; index += maxModifyIdsPerQuery) { + const idChunk = modifyIds.slice(index, index + maxModifyIdsPerQuery); + const result = await this.dbSession.executeLatest( + `SELECT * FROM points WHERE airport_id = ? AND id IN (${idChunk.map(() => '?').join(', ')})`, + [airportId, ...idChunk], + ); + modifyRows.push(...result.results); + } + const modifyPointsById = new Map(modifyRows.map((row) => [row.id, this.mapPointFromDb(row)])); + const modifyContexts = modifyEntries.map(([id, patch]) => { + const basePoint = modifyPointsById.get(id); + if (!basePoint) { throw new HttpError(404, 'Point targeted by modify operation does not exist'); } - const basePoint = this.mapPointFromDb(first); const merged: PointData = { type: patch.type ?? basePoint.type, name: patch.name ?? basePoint.name, @@ -400,6 +410,7 @@ export class PointsService { this.posthog?.track('Points Changeset Applied', { airportId, userId, + authorizationKind: authorization.kind, created: createdPoints.length, modified: modifyContexts.length, deleted: deleteIds.length, @@ -473,15 +484,19 @@ export class PointsService { } const candidates = Array.from(candidateSet); - const statements = candidates.map((id) => this.stmtCheckPointId.bindAll({ id })); - const results = await this.dbSession.executeBatch(statements); - - results.forEach((result, index) => { - const rows = (result.results as unknown as Array<{ id: string }> | null) ?? []; - if (rows.length === 0) { - allocatedIds.add(candidates[index]); - } - }); + const existingIds = new Set(); + const maxCandidateIdsPerQuery = 100; + for (let index = 0; index < candidates.length; index += maxCandidateIdsPerQuery) { + const candidateChunk = candidates.slice(index, index + maxCandidateIdsPerQuery); + const existingRows = await this.dbSession.executeLatest<{ id: string }>( + `SELECT id FROM points WHERE id IN (${candidateChunk.map(() => '?').join(', ')})`, + candidateChunk, + ); + for (const { id } of existingRows.results) existingIds.add(id); + } + for (const candidate of candidates) { + if (!existingIds.has(candidate)) allocatedIds.add(candidate); + } } if (allocatedIds.size < count) { @@ -491,17 +506,17 @@ export class PointsService { return Array.from(allocatedIds); } - private validatePoint(point: PointData) { + private validatePoint(point: PointValidationInput): asserts point is PointData { const allowedTypes: Array = ['stopbar', 'lead_on', 'taxiway', 'stand']; if (!point.type || !allowedTypes.includes(point.type)) { throw new HttpError(400, 'Point must have a valid type'); } // Normalize coordinates: accept either legacy single object or array of objects - const rawCoords: unknown = (point as unknown as { coordinates?: unknown }).coordinates; + const rawCoords = point.coordinates; if (rawCoords === undefined || rawCoords === null) throw new HttpError(400, 'Point must have coordinates'); - let arr: Array<{ lat: number; lng: number }>; + let arr: Coordinate[]; if (PointsService.isCoordinateArray(rawCoords)) { arr = rawCoords; } else if (PointsService.isCoordinate(rawCoords)) { @@ -513,7 +528,7 @@ export class PointsService { if (arr.length < 1) { throw new HttpError(400, 'Coordinates must contain at least one point'); } - (point as PointData).coordinates = arr as { lat: number; lng: number }[]; + point.coordinates = arr; // Validate type-specific fields if (point.type === 'stopbar') { @@ -530,26 +545,26 @@ export class PointsService { // elevated is optional for stopbars, defaulting to false if (point.elevated !== undefined) { // Convert numeric 1/0 to boolean if needed - if (point.elevated === true || (point.elevated as unknown as number) === 1) { + if (point.elevated === true || point.elevated === 1) { point.elevated = true; - } else if (point.elevated === false || (point.elevated as unknown as number) === 0) { + } else if (point.elevated === false || point.elevated === 0) { point.elevated = false; } - if (typeof point.elevated !== 'boolean') { + if (point.elevated !== true && point.elevated !== false) { throw new HttpError(400, 'Elevated property must be a boolean when specified'); } } if (point.ihp !== undefined) { // Convert numeric 1/0 to boolean if needed - if (point.ihp === true || (point.ihp as unknown as number) === 1) { + if (point.ihp === true || point.ihp === 1) { point.ihp = true; - } else if (point.ihp === false || (point.ihp as unknown as number) === 0) { + } else if (point.ihp === false || point.ihp === 0) { point.ihp = false; } - if (typeof point.ihp !== 'boolean') { + if (point.ihp !== true && point.ihp !== false) { throw new HttpError(400, 'IHP property must be a boolean when specified'); } } @@ -571,18 +586,7 @@ export class PointsService { } private mapPointFromDb(dbPoint: PointRow): Point { - let raw: unknown; - try { - raw = JSON.parse(dbPoint.coordinates); - } catch { - raw = null; - } - let coordinates: Array<{ lat: number; lng: number }> = []; - if (PointsService.isCoordinateArray(raw)) { - coordinates = raw; - } else if (PointsService.isCoordinate(raw)) { - coordinates = [raw]; - } + const coordinates = PointsService.parseCoordinates(dbPoint.coordinates); const parsedLinkedTo = PointsService.parseLinkedTo(dbPoint.linked_to); const linkedTo = parsedLinkedTo.length > 0 ? parsedLinkedTo : undefined; @@ -753,11 +757,11 @@ export class PointsService { for (const row of results.results) { let stopbarIds: string[] = []; try { - const parsed = JSON.parse(row.linked_to); + const parsed: string | string[] | null = JSON.parse(row.linked_to); if (Array.isArray(parsed)) { - stopbarIds = parsed.filter((id): id is string => typeof id === 'string'); - } else if (typeof parsed === 'string') { - stopbarIds = [parsed]; + stopbarIds = parsed.filter((id) => Object.prototype.toString.call(id) === '[object String]').map(String); + } else if (Object.prototype.toString.call(parsed) === '[object String]') { + stopbarIds = [String(parsed)]; } } catch { // Legacy single ID format diff --git a/src/services/polygons.ts b/src/services/polygons.ts index f89cd2d..e40e3c5 100644 --- a/src/services/polygons.ts +++ b/src/services/polygons.ts @@ -3,6 +3,7 @@ import { processBarsPolygon, deduplicateTaxiwayPoints } from './bars/handlers'; import { BarsPolygon, BarsDBRecord, ProcessedBarsObject, BarsLightPoint, LightProperties, GeoPoint } from './bars/types'; import { calculateDistance } from './bars/geoUtils'; import { PostHogService } from './posthog'; +import { parseDecimalNumber } from './parsing'; import { DatabaseSessionService } from './database-session'; @@ -51,7 +52,7 @@ export class PolygonService { // Check if this polygon has displayName starting with "BARS_" const displayNameMatch = polygonAttributes.match(regex.displayName); if (!displayNameMatch) { - continue; // Skip if not a BARS polygon + continue; } const id = displayNameMatch[1]; // This is the BARS_XXXX id @@ -97,6 +98,7 @@ export class PolygonService { try { const results = await this.dbSession.executeReadBatch(statements); for (const result of results) { + // SAFETY: Every statement uses this method's fixed SELECT list, whose D1 column names match PointRow. for (const row of result.results as PointRow[]) { records.set(row.id, this.mapBarsRecordFromDb(row)); } @@ -152,17 +154,11 @@ export class PolygonService { const converted: GeoPoint[] = []; for (const item of parsed) { if (!item) continue; - const rawLat = - typeof item.lat === 'number' ? item.lat : typeof item.lat === 'string' ? Number(item.lat) : undefined; + const rawLat = parseDecimalNumber(item.lat); const rawLonCandidate = item.lon ?? item.lng; - const rawLon = - typeof rawLonCandidate === 'number' - ? rawLonCandidate - : typeof rawLonCandidate === 'string' - ? Number(rawLonCandidate) - : undefined; + const rawLon = parseDecimalNumber(rawLonCandidate); if (Number.isFinite(rawLat) && Number.isFinite(rawLon)) { - converted.push({ lat: rawLat as number, lon: rawLon as number }); + converted.push({ lat: rawLat, lon: rawLon }); } } if (converted.length >= 2) { @@ -226,14 +222,12 @@ export class PolygonService { out.push(''); out.push(''); - // Add each object for (const obj of processedObjects) { const centerPoint = this.calculateObjectCenter(obj.points); const centerAttributes = centerPoint ? ` centerLat="${centerPoint.lat}" centerLon="${centerPoint.lon}"` : ''; // stateId moved to per-light level (previously on BarsObject) out.push(`\t`); - // Add properties const props = obj.properties; out.push('\t\t'); if (props.color) out.push(`\t\t\t${props.color}`); @@ -242,7 +236,6 @@ export class PolygonService { if (props.intensity !== undefined) out.push(`\t\t\t${props.intensity}`); out.push('\t\t'); - // Add light points for (const point of obj.points) { const lightColor = (point.properties?.color || obj.properties.color || '').toLowerCase(); const isElevatedStopbar = obj.type === 'stopbar' && point.properties?.elevated === true; @@ -436,7 +429,6 @@ export class PolygonService { */ async processBarsXML(inputXml: string, icao?: string): Promise { try { - // Parse BARS polygons from input XML const polygons = this.parseBarsPolygonsFromXML(inputXml); if (polygons.length === 0) { @@ -455,7 +447,7 @@ export class PolygonService { const airportLat = airportData.latitude; const airportLon = airportData.longitude; - if (typeof airportLat === 'number' && typeof airportLon === 'number') { + if (airportLat !== null && airportLon !== null && Number.isFinite(airportLat) && Number.isFinite(airportLon)) { let hasNearbyPoint = false; for (const polygon of polygons) { @@ -486,7 +478,6 @@ export class PolygonService { const barsIds = polygons.map((polygon) => (polygon.id.startsWith('BARS_') ? polygon.id : `BARS_${polygon.id}`)); const barsRecords = await this.getBarsRecordsFromDB(barsIds); - // Process each polygon to generate BARS light locations const processedObjects: ProcessedBarsObject[] = []; for (const polygon of polygons) { @@ -499,7 +490,6 @@ export class PolygonService { const orderedPoints = this.alignPolygonPointOrder(polygon.points, dbRecord.coordinates); const polygonForProcessing = orderedPoints === polygon.points ? polygon : { ...polygon, points: orderedPoints }; - // Process the polygon based on its type const processedObject = await processBarsPolygon(polygonForProcessing, dbRecord); if (processedObject) { @@ -549,12 +539,10 @@ export class PolygonService { let xml = '\n\n'; - // Add each polygon as an Object for (const polygon of polygons) { xml += '\t\n'; xml += `\t\t${polygon.id}\n`; - // Add each point for (const point of polygon.points) { xml += `\t\t${point.lat},${point.lon}\n`; } diff --git a/src/services/posthog.ts b/src/services/posthog.ts index faf7d76..1151e84 100644 --- a/src/services/posthog.ts +++ b/src/services/posthog.ts @@ -3,10 +3,20 @@ import { waitUntil as cfWaitUntil } from 'cloudflare:workers'; import { cancelResponseBody } from './http'; +export type AnalyticsPropertyValue = string | number | boolean | null | undefined | AnalyticsPropertyValue[] | AnalyticsProperties; + +export interface AnalyticsProperties { + [key: string]: AnalyticsPropertyValue; +} + +type BackgroundTaskRuntime = typeof globalThis & { + waitUntil?: (promise: Promise) => void; +}; + interface PostHogCapturePayload { api_key: string; event: string; - properties: Record; + properties: AnalyticsProperties; timestamp?: string; // ISO 8601 $process_person_profile?: boolean; } @@ -20,7 +30,7 @@ export interface TrackOptions { export interface BatchTrackEvent { event: string; - properties?: Record; + properties?: AnalyticsProperties; distinctId?: string; timestamp?: Date | string; } @@ -44,7 +54,7 @@ export class PostHogService { return PII_KEYS.has(lk) || lk.includes('vatsim'); } - private async hashValue(value: unknown): Promise { + private async hashValue(value: AnalyticsPropertyValue): Promise { try { const data = UTF8_ENCODER.encode(String(value)); const digest = await crypto.subtle.digest('SHA-256', data); @@ -62,8 +72,8 @@ export class PostHogService { } } - private async sanitizeProperties(props: Record): Promise> { - const sanitized: Record = {}; + private async sanitizeProperties(props: AnalyticsProperties): Promise { + const sanitized: AnalyticsProperties = {}; const pendingHashes: Promise[] = []; for (const [key, value] of Object.entries(props)) { sanitized[key] = value; @@ -76,11 +86,11 @@ export class PostHogService { } private async prepareProperties( - properties: Record, + properties: AnalyticsProperties, distinctId: string, options: Pick, - ): Promise> { - const mergedProps: Record = { ...properties }; + ): Promise { + const mergedProps: AnalyticsProperties = { ...properties }; if (!options.omitProduct && mergedProps.product === undefined) { mergedProps.product = options.product || 'Core'; } @@ -98,15 +108,14 @@ export class PostHogService { private dispatch(doFetch: () => Promise, inline = false): void | Promise { if (inline) return doFetch(); try { - if (typeof (cfWaitUntil as unknown) === 'function') { - cfWaitUntil(doFetch()); - return; - } + cfWaitUntil(doFetch()); + return; } catch { /* ignore */ } try { - (globalThis as unknown as { waitUntil?: (p: Promise) => void }).waitUntil?.(doFetch()); + const runtimeGlobal: BackgroundTaskRuntime = globalThis; + runtimeGlobal.waitUntil?.(doFetch()); } catch { /* ignore */ } @@ -114,7 +123,7 @@ export class PostHogService { track( event: string, - properties: Record = {}, + properties: AnalyticsProperties = {}, distinctId = 'anonymous', options: TrackOptions = {}, ): void | Promise { @@ -127,7 +136,7 @@ export class PostHogService { $process_person_profile: false, }; if (options.timestamp) { - payload.timestamp = typeof options.timestamp === 'string' ? options.timestamp : options.timestamp.toISOString(); + payload.timestamp = options.timestamp instanceof Date ? options.timestamp.toISOString() : options.timestamp; } return JSON.stringify(payload); }; @@ -161,14 +170,17 @@ export class PostHogService { const doFetch = async () => { const batch = await Promise.all( - events.map(async (item) => ({ - event: item.event, - properties: await this.prepareProperties(item.properties ?? {}, item.distinctId ?? 'anonymous', options), - ...(item.timestamp - ? { timestamp: typeof item.timestamp === 'string' ? item.timestamp : item.timestamp.toISOString() } - : {}), - $process_person_profile: false, - })), + events.map(async (item) => { + const payload: Omit = { + event: item.event, + properties: await this.prepareProperties(item.properties ?? {}, item.distinctId ?? 'anonymous', options), + $process_person_profile: false, + }; + if (item.timestamp) { + payload.timestamp = item.timestamp instanceof Date ? item.timestamp.toISOString() : item.timestamp; + } + return payload; + }), ); return fetch(`${this.host}/batch/`, { diff --git a/src/services/rate-limit.ts b/src/services/rate-limit.ts index f24139f..de1f285 100644 --- a/src/services/rate-limit.ts +++ b/src/services/rate-limit.ts @@ -31,7 +31,7 @@ export class RateLimiter extends DurableObject { async fetch(request: Request): Promise { try { type RLBody = Partial<{ key: string; maxRequests: number; intervalMs: number }>; - const body = await request.json().catch(() => ({}) as RLBody); + const body = await request.json().catch((): RLBody => ({})); const { key, maxRequests, intervalMs } = body; if (!key) return new Response('Missing key', { status: 400 }); return Response.json({ diff --git a/src/services/releases.ts b/src/services/releases.ts index 163355f..5a669ed 100644 --- a/src/services/releases.ts +++ b/src/services/releases.ts @@ -2,6 +2,19 @@ import { DatabaseSessionService } from './database-session'; import { StorageService } from './storage'; export type InstallerProduct = 'Pilot-Client' | 'vatSys-Plugin' | 'EuroScope-Plugin' | 'Installer' | 'SimConnect.NET'; + +export function parseInstallerProduct(value: string): InstallerProduct | null { + switch (value) { + case 'Pilot-Client': + case 'vatSys-Plugin': + case 'EuroScope-Plugin': + case 'Installer': + case 'SimConnect.NET': + return value; + default: + return null; + } +} export interface ReleaseRecord { id: number; product: InstallerProduct; @@ -35,12 +48,11 @@ export class ReleaseService { async createRelease(input: CreateReleaseInput): Promise { const { product, version, fileKey, fileSize, fileHash, changelog, imageUrl } = input; - const result = await this.dbSession.executeWrite( + const result = await this.dbSession.executeWrite( `INSERT INTO installer_releases (product, version, file_key, file_size, file_hash, changelog, image_url) VALUES (?,?,?,?,?,?,?) RETURNING *`, [product, version, fileKey, fileSize, fileHash, changelog || null, imageUrl || null], ); - const rows = result.results as unknown as ReleaseRecord[] | null; - const release = rows && rows[0]; + const release = result.results?.[0]; if (!release) throw new Error('Failed to create release'); return release; } @@ -66,11 +78,10 @@ export class ReleaseService { } async updateChangelog(id: number, changelog: string): Promise { - const res = await this.dbSession.executeWrite(`UPDATE installer_releases SET changelog = ? WHERE id = ? RETURNING *`, [ + const res = await this.dbSession.executeWrite(`UPDATE installer_releases SET changelog = ? WHERE id = ? RETURNING *`, [ changelog, id, ]); - const rows = res.results as unknown as ReleaseRecord[] | null; - return (rows && rows[0]) || null; + return res.results?.[0] ?? null; } } diff --git a/src/services/roles.ts b/src/services/roles.ts index 933300f..d17253b 100644 --- a/src/services/roles.ts +++ b/src/services/roles.ts @@ -3,10 +3,10 @@ export enum StaffRole { PRODUCT_MANAGER = 'PRODUCT_MANAGER', } -export const roleHierarchy: Record = { +export const roleHierarchy = { LEAD_DEVELOPER: 999, PRODUCT_MANAGER: 500, -}; +} satisfies Record; export type Role = 'lead_developer' | 'product_manager' | 'nav_head' | 'nav_member'; @@ -24,28 +24,44 @@ import { StaffRecord } from '../types'; import { DatabaseSessionService } from './database-session'; +type StaffRow = Omit & { role: string }; +type StaffWriteRow = Pick; + +function parseStaffRole(role: string | null | undefined): StaffRole | null { + switch (role) { + case StaffRole.LEAD_DEVELOPER: + case StaffRole.PRODUCT_MANAGER: + return role; + default: + return null; + } +} + +export interface ListedStaffMember { + user_id: number; + role: string; + created_at: string; + vatsim_id: string; + full_name: string | null; +} + export class RoleService { private dbSession: DatabaseSessionService; constructor(private db: D1Database) { this.dbSession = new DatabaseSessionService(db); } - private async fetchStaffRecord(userId: number): Promise { - const staffResult = await this.dbSession.executeRead( + private async fetchStaffRecord(userId: number): Promise { + const staffResult = await this.dbSession.executeRead( 'SELECT id, user_id, role, created_at FROM staff WHERE user_id = ? LIMIT 1', [userId], ); return staffResult.results[0] ?? null; } - private normalizeStaffRole(role?: StaffRecord['role'] | null): StaffRole | null { - if (!role) return null; - return role in roleHierarchy ? (role as StaffRole) : null; - } - async getStaffStatus(userId: number): Promise<{ isStaff: boolean; role: StaffRole | null }> { const staff = await this.fetchStaffRecord(userId); - const role = this.normalizeStaffRole(staff?.role); + const role = parseStaffRole(staff?.role); return { isStaff: !!role, role }; } @@ -60,7 +76,7 @@ export class RoleService { ); const row = result.results[0]; if (!row) return null; - return { userId: row.user_id, role: this.normalizeStaffRole(row.role as StaffRecord['role'] | null) }; + return { userId: row.user_id, role: parseStaffRole(row.role) }; } async isStaff(userId: number): Promise { @@ -80,11 +96,12 @@ export class RoleService { } async hasRole(userId: number, role: StaffRole | Role): Promise { - if (role in StaffRole) { - return this.hasPermission(userId, role as StaffRole); + if (role === StaffRole.LEAD_DEVELOPER || role === StaffRole.PRODUCT_MANAGER) { + return this.hasPermission(userId, role); } + if (role !== 'nav_head' && role !== 'nav_member') return false; const divisionRoles = await this.getDivisionRoles(userId); - return !!divisionRoles[role as keyof DivisionRoles]; + return !!divisionRoles[role]; } async getDivisionRoles(userId: number): Promise { @@ -105,8 +122,8 @@ export class RoleService { } // --- Staff management helpers (write) --- - private async getRoleChangeState(userId: number): Promise<(StaffRecord & { lead_count: number }) | null> { - const result = await this.dbSession.executeLatest( + private async getRoleChangeState(userId: number): Promise<(StaffRow & { lead_count: number }) | null> { + const result = await this.dbSession.executeLatest( `SELECT id, user_id, role, created_at, (SELECT COUNT(*) FROM staff WHERE role = ?) AS lead_count FROM staff @@ -117,10 +134,10 @@ export class RoleService { return result.results[0] ?? null; } - private ensureNotLastLeadDeveloper(current: (StaffRecord & { lead_count: number }) | null, changingToRole?: StaffRole | null) { + private ensureNotLastLeadDeveloper(current: (StaffRow & { lead_count: number }) | null, changingToRole?: StaffRole | null) { if (!current) return; if ( - (current.role as StaffRole) === StaffRole.LEAD_DEVELOPER && + parseStaffRole(current.role) === StaffRole.LEAD_DEVELOPER && (changingToRole == null || changingToRole !== StaffRole.LEAD_DEVELOPER) ) { if (current.lead_count <= 1) throw new Error('Cannot modify or remove the last remaining lead developer'); @@ -131,15 +148,17 @@ export class RoleService { const existing = await this.getRoleChangeState(userId); this.ensureNotLastLeadDeveloper(existing, role); const createdAt = new Date().toISOString(); - const result = await this.dbSession.executeWrite( + const result = await this.dbSession.executeWrite( `INSERT INTO staff (user_id, role, created_at) VALUES (?, ?, ?) ON CONFLICT(user_id) DO UPDATE SET role = excluded.role RETURNING user_id, role, created_at`, [userId, role, createdAt], ); - const row = (result.results as unknown as StaffRecord[] | null)?.[0]; + const row = result.results?.[0]; if (!row) throw new Error('Failed to add staff member'); - return { user_id: row.user_id, role: row.role as StaffRole, created_at: row.created_at }; + const createdRole = parseStaffRole(row.role); + if (!createdRole) throw new Error('Database returned an invalid staff role'); + return { user_id: row.user_id, role: createdRole, created_at: row.created_at }; } async updateStaffRole(userId: number, role: StaffRole): Promise { @@ -154,9 +173,7 @@ export class RoleService { return !!result.success; } - async listStaff(): Promise< - Array<{ user_id: number; role: StaffRole; created_at: string; vatsim_id: string; full_name: string | null }> - > { + async listStaff(): Promise { const res = await this.dbSession.executeRead<{ user_id: number; role: string; @@ -170,12 +187,12 @@ export class RoleService { ORDER BY s.created_at DESC`, [], ); - return res.results.map((r) => ({ - user_id: r.user_id, - role: r.role as StaffRole, - created_at: r.created_at, - vatsim_id: r.vatsim_id, - full_name: r.full_name, + return res.results.map((row) => ({ + user_id: row.user_id, + role: row.role, + created_at: row.created_at, + vatsim_id: row.vatsim_id, + full_name: row.full_name, })); } } diff --git a/src/services/service-pool.ts b/src/services/service-pool.ts index 9feea19..e8e8177 100644 --- a/src/services/service-pool.ts +++ b/src/services/service-pool.ts @@ -1,4 +1,3 @@ -// services/ServicePool.ts import { VatsimService } from './vatsim'; import { AuthService } from './auth'; import { RoleService } from './roles'; diff --git a/src/services/storage.ts b/src/services/storage.ts index 39b0298..73186a1 100644 --- a/src/services/storage.ts +++ b/src/services/storage.ts @@ -27,6 +27,7 @@ export class StorageService { data: ReadableStream | ArrayBuffer | string, contentType: string, metadata: Record = {}, + options: { onlyIfAbsent?: boolean } = {}, ): Promise<{ key: string; etag: string }> { // Normalize key to avoid any issues const normalizedKey = this.normalizeKey(key); @@ -37,16 +38,20 @@ export class StorageService { fileType: contentType.split('/')[0] || 'application', }; - // Upload to R2 - const uploaded = await this.bucket.put(normalizedKey, data, { + const putOptions: R2PutOptions = { httpMetadata: { contentType, cacheControl: `public, max-age=${this.MAX_AGE_DEFAULT}`, }, customMetadata: fileMetadata, - }); + }; + if (options.onlyIfAbsent) { + putOptions.onlyIf = { etagDoesNotMatch: '*' }; + } + const uploaded = await this.bucket.put(normalizedKey, data, putOptions); if (!uploaded) { + if (options.onlyIfAbsent) throw new Error('Storage object already exists'); throw new Error('Failed to upload file to storage'); } @@ -73,7 +78,6 @@ export class StorageService { return null; } - // Create headers const headers = new Headers(); object.writeHttpMetadata(headers); headers.set('etag', object.httpEtag); @@ -81,14 +85,13 @@ export class StorageService { let status = 200; if (object.range) { - // Miniflare may expose all union members with undefined values, so narrow - // by value rather than using the `in` operator. - const suffix = 'suffix' in object.range && typeof object.range.suffix === 'number' ? object.range.suffix : undefined; - const rangeOffset = 'offset' in object.range && typeof object.range.offset === 'number' ? object.range.offset : 0; + // Miniflare may expose union members with undefined values, so preserve + // the same undefined fallbacks after discriminating the range shape. + const suffix = 'suffix' in object.range ? object.range.suffix : undefined; + const rangeOffset = ('offset' in object.range ? object.range.offset : undefined) ?? 0; const offset = suffix === undefined ? rangeOffset : Math.max(0, object.size - suffix); - const rangeLength = 'length' in object.range && typeof object.range.length === 'number' ? object.range.length : undefined; - const length = - suffix === undefined ? (rangeLength ?? Math.max(0, object.size - offset)) : Math.min(object.size, suffix); + const rangeLength = 'length' in object.range ? object.range.length : undefined; + const length = suffix === undefined ? (rangeLength ?? Math.max(0, object.size - offset)) : Math.min(object.size, suffix); headers.set('Content-Range', `bytes ${offset}-${offset + length - 1}/${object.size}`); headers.set('Content-Length', String(length)); status = 206; diff --git a/src/services/support.ts b/src/services/support.ts index 4593fbe..c3d7500 100644 --- a/src/services/support.ts +++ b/src/services/support.ts @@ -1,4 +1,5 @@ import { AirportService } from './airport'; +import { buildRemovalArtifacts, calculateAirportTestRadius, parseRemovalPolygons } from './msfs-removals'; interface PolygonVertex { lat: number; @@ -19,16 +20,6 @@ interface LightSupport { heading: number; } -// Define an interface for airport data to help TypeScript -interface AirportData { - latitude?: number; - longitude?: number; - icao?: string; - name?: string; - continent?: string; - runways?: Record[]; -} - export class SupportService { private airportService: AirportService; private readonly EARTH_RADIUS = 6378137; // Earth radius in meters at equator @@ -64,47 +55,7 @@ export class SupportService { * Parses polygons from XML content that have displayName="remove" */ parsePolygonsFromXML(xmlContent: string): Polygon[] { - const polygons: Polygon[] = []; - const regex = { - polygon: /]*)>([\s\S]*?)<\/Polygon>/g, - vertex: / 0) { - polygons.push({ - id: crypto.randomUUID(), - vertices, - altitude, - }); - } - } - - return polygons; + return parseRemovalPolygons(xmlContent); } /** @@ -239,7 +190,6 @@ export class SupportService { out.push({ latitude: latCenter, longitude: lonCenter, width: meters, length: meters, heading: 0 }); }; - // state const supports: LightSupport[] = []; const covered: Uint8Array[] = Array.from({ length: rows }, () => new Uint8Array(cols)); const scratch: Uint8Array[] = Array.from({ length: rows }, () => new Uint8Array(cols)); @@ -459,22 +409,19 @@ export class SupportService { * Validates XML content before processing with protection against XXE attacks */ private validateXMLContent(xmlContent: string): boolean { - // Basic XML validation - if (!xmlContent.trim().startsWith(']*displayName="remove"[^>]*>/i.test(xmlContent); + const hasRemovePolygon = /]*displayName\s*=\s*(["'])remove\1[^>]*>/i.test(xmlContent); if (!hasRemovePolygon) { throw new Error('No remove polygons found in XML'); } @@ -485,102 +432,27 @@ export class SupportService { /** * Generates light supports XML from input XML containing polygons */ - async generateLightSupportsXML(inputXml: string, icao: string): Promise { + async generateLightSupportsXML(inputXml: string, icao: string, simulator = 'msfs2024'): Promise { try { - // Validate XML content this.validateXMLContent(inputXml); - - // Get airport data to get coordinates - const airportData = (await this.airportService.getAirport(icao)) as AirportData; - if (!airportData) { - throw new Error(`Airport with ICAO ${icao} not found`); - } - - // Parse polygons from input XML - const polygons = this.parsePolygonsFromXML(inputXml); - - if (polygons.length === 0) { - throw new Error('No valid remove polygons found in input XML'); - } - + const airportData = await this.airportService.getAirport(icao); + if (!airportData) throw new Error(`Airport with ICAO ${icao} not found`); + const polygons = parseRemovalPolygons(inputXml); + if (polygons.length === 0) throw new Error('No valid remove polygons found in input XML'); + const { supports, exclusions } = buildRemovalArtifacts(polygons, { alignPolygonSupports: simulator === 'msfs2020' || simulator === 'msfs2024', coverPolygonEnds: simulator === 'msfs2020' }); + const airportLatitude = Number(airportData.latitude) || 0; + const airportLongitude = Number(airportData.longitude) || 0; + const airportTestRadius = calculateAirportTestRadius({ lat: airportLatitude, lon: airportLongitude }, supports, exclusions); let xml = '\n\n'; - - // Start airport tag with coordinates from airport data - // Add explicit nullish coalescing to handle potentially undefined values - xml += `\t\n`; - - // Add light supports - let supportCount = 1; - let exclusionRectangles = ''; // Store exclusion rectangles separately - const exclusionCosCache = new Map(); - - // Helper function to calculate exclusion rectangle coordinates - const calculateExclusionCoords = (center: { latitude: number; longitude: number }, width: number, length: number) => { - // Add a small buffer (35%) to make exclusion rectangle slightly larger - const bufferFactor = 1.35; - const bufferedWidth = width * bufferFactor; - const bufferedLength = length * bufferFactor; - - // Convert width/length from meters to degrees - const halfWidthDeg = bufferedWidth * 0.5 * this.metersToDegreesBase; - const cacheKey = Math.round(center.latitude * 1e6); - let cosLat = exclusionCosCache.get(cacheKey); - if (cosLat === undefined) { - cosLat = this.getCosineLatitude(center.latitude); - exclusionCosCache.set(cacheKey, cosLat); - } - const halfLengthDeg = this.metersToDegreesLonFromCos(bufferedLength * 0.5, cosLat); - - return { - latMin: center.latitude - halfWidthDeg, - latMax: center.latitude + halfWidthDeg, - lonMin: center.longitude - halfLengthDeg, - lonMax: center.longitude + halfLengthDeg, - }; - }; - - // Process each polygon - for (let i = 0; i < polygons.length; i++) { - const polygon = polygons[i]; - - // Get the supports for this polygon - const supports = this.calculateLightSupports(polygon); - - // Add each support and store exclusion rectangle - supports.forEach((support) => { - // Add light support inside Airport tag - xml += `\t\t\n`; - - // Calculate and store exclusion rectangle to add later outside Airport tag - const exclusionCoords = calculateExclusionCoords( - { latitude: support.latitude, longitude: support.longitude }, - support.width, - support.length, - ); - - exclusionRectangles += `\t\n`; - - supportCount++; - }); - - // Count intentionally unused for now; metrics removed + xml += `\t\n`; + for (const [index, support] of supports.entries()) { + xml += `\t\t\n`; } - - // Close airport tag - xml += '\t\t\n'; - xml += '\t\t\n'; - xml += '\t\t\n'; - xml += '\t\n'; - - // Add exclusion rectangles after Airport tag - xml += exclusionRectangles; - - // Close FSData - xml += ''; - - // Stats tracking removed - - return xml; + xml += '\t\t\n\t\t\n\t\t\n\t\n'; + for (const exclusion of exclusions) { + xml += `\t\n`; + } + return `${xml}`; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; throw new Error(`Failed to generate light supports: ${errorMessage}`); diff --git a/src/services/users.ts b/src/services/users.ts index bf28f96..adb9f1c 100644 --- a/src/services/users.ts +++ b/src/services/users.ts @@ -1,4 +1,3 @@ -// import { UserRecord } from '../types'; import { RoleService, StaffRole } from './roles'; import { AuthService } from './auth'; import { PostHogService } from './posthog'; @@ -20,6 +19,77 @@ type StaffUserDTO = { created_at: string; last_login: string; is_staff: boolean; + fast_track: { enabled: boolean; expires_at: string | null }; + contribution_stats: { + approved: number; + airports: number; + rejected: number; + }; +}; + +type StaffUserRow = { + id: number; + vatsim_id: string; + email: string; + full_name: string | null; + display_mode: number | null; + display_name: string | null; + region_id: string | null; + region_name: string | null; + division_id: string | null; + division_name: string | null; + subdivision_id: string | null; + subdivision_name: string | null; + created_at: string; + last_login: string; + is_staff: number; + fast_track_expires_at: string | null; + approved_contributions: number; + approved_airports: number; + rejected_contributions: number; +}; + +type UserCountRow = { count: number }; + +const staffUserProjection = ` + u.id, u.vatsim_id, u.email, u.full_name, u.display_mode, u.display_name, + u.region_id, u.region_name, u.division_id, u.division_name, + u.subdivision_id, u.subdivision_name, u.created_at, u.last_login, + CASE WHEN s.user_id IS NOT NULL THEN 1 ELSE 0 END AS is_staff, + (SELECT ft.expires_at FROM contributor_fast_track ft + WHERE ft.user_id = u.id AND ft.enabled = 1 + AND ft.expires_at > CURRENT_TIMESTAMP LIMIT 1) AS fast_track_expires_at, + (SELECT COUNT(*) FROM contributions c + WHERE c.user_id = u.vatsim_id AND c.status = 'approved') AS approved_contributions, + (SELECT COUNT(DISTINCT c.airport_icao) FROM contributions c + WHERE c.user_id = u.vatsim_id AND c.status = 'approved') AS approved_airports, + (SELECT COUNT(*) FROM contributions c + WHERE c.user_id = u.vatsim_id AND c.status = 'rejected') AS rejected_contributions`; + +const toStaffUserDto = (user: StaffUserRow): StaffUserDTO => { + return { + id: user.id, + vatsim_id: user.vatsim_id, + email: user.email, + full_name: user.full_name, + display_mode: user.display_mode ?? undefined, + display_name: user.display_name, + region: user.region_id || user.region_name ? { id: user.region_id, name: user.region_name } : null, + division: user.division_id || user.division_name ? { id: user.division_id, name: user.division_name } : null, + subdivision: user.subdivision_id || user.subdivision_name ? { id: user.subdivision_id, name: user.subdivision_name } : null, + created_at: user.created_at, + last_login: user.last_login, + is_staff: user.is_staff === 1, + fast_track: { + enabled: user.fast_track_expires_at !== null, + expires_at: user.fast_track_expires_at, + }, + contribution_stats: { + approved: Number(user.approved_contributions) || 0, + airports: Number(user.approved_airports) || 0, + rejected: Number(user.rejected_contributions) || 0, + }, + }; }; export class UserService { @@ -47,10 +117,7 @@ export class UserService { const [usersResult, countResult] = await this.dbSession.executeReadBatch([ { query: ` - SELECT u.id, u.vatsim_id, u.email, u.full_name, u.display_mode, u.display_name, - u.region_id, u.region_name, u.division_id, u.division_name, - u.subdivision_id, u.subdivision_name, u.created_at, u.last_login, - CASE WHEN s.user_id IS NOT NULL THEN 1 ELSE 0 END AS is_staff + SELECT ${staffUserProjection} FROM users u LEFT JOIN staff s ON s.user_id = u.id ORDER BY u.created_at DESC @@ -59,39 +126,12 @@ export class UserService { }, { query: 'SELECT COUNT(*) AS count FROM users' }, ]); - const users = usersResult.results as Array<{ - id: number; - vatsim_id: string; - email: string; - full_name: string | null; - display_mode: number | null; - display_name: string | null; - region_id: string | null; - region_name: string | null; - division_id: string | null; - division_name: string | null; - subdivision_id: string | null; - subdivision_name: string | null; - created_at: string; - last_login: string; - is_staff: number; - }>; - const count = countResult.results as Array<{ count: number }>; + // SAFETY: The first SELECT explicitly projects every StaffUserRow column, and D1 preserves those column names in results. + const users = usersResult.results as StaffUserRow[]; + // SAFETY: The second SELECT aliases its single aggregate column to `count`, matching UserCountRow. + const count = countResult.results as UserCountRow[]; return { - users: users.map((u) => ({ - id: u.id, - vatsim_id: u.vatsim_id, - email: u.email, - full_name: u.full_name, - display_mode: u.display_mode ?? undefined, - display_name: u.display_name, - region: u.region_id || u.region_name ? { id: u.region_id, name: u.region_name } : null, - division: u.division_id || u.division_name ? { id: u.division_id, name: u.division_name } : null, - subdivision: u.subdivision_id || u.subdivision_name ? { id: u.subdivision_id, name: u.subdivision_name } : null, - created_at: u.created_at, - last_login: u.last_login, - is_staff: u.is_staff === 1, - })), + users: users.map(toStaffUserDto), total: count[0]?.count || 0, }; } catch { @@ -108,26 +148,9 @@ export class UserService { } try { - const result = await this.dbSession.executeRead<{ - id: number; - vatsim_id: string; - email: string; - full_name: string | null; - display_mode: number | null; - display_name: string | null; - region_id: string | null; - region_name: string | null; - division_id: string | null; - division_name: string | null; - subdivision_id: string | null; - subdivision_name: string | null; - created_at: string; - last_login: string; - is_staff: number; - }>( + const result = await this.dbSession.executeRead( ` - SELECT u.id, u.vatsim_id, u.email, u.full_name, u.display_mode, u.display_name, u.region_id, u.region_name, u.division_id, u.division_name, u.subdivision_id, u.subdivision_name, u.created_at, u.last_login, - CASE WHEN s.id IS NOT NULL THEN 1 ELSE 0 END as is_staff + SELECT ${staffUserProjection} FROM users u LEFT JOIN staff s ON u.id = s.user_id WHERE u.email LIKE ? OR u.vatsim_id LIKE ? @@ -139,25 +162,74 @@ export class UserService { if (!result) { throw new Error('Failed to search users'); } - return result.results.map((u) => ({ - id: u.id, - vatsim_id: u.vatsim_id, - email: u.email, - full_name: u.full_name, - display_mode: u.display_mode ?? undefined, - display_name: u.display_name, - region: u.region_id || u.region_name ? { id: u.region_id, name: u.region_name } : null, - division: u.division_id || u.division_name ? { id: u.division_id, name: u.division_name } : null, - subdivision: u.subdivision_id || u.subdivision_name ? { id: u.subdivision_id, name: u.subdivision_name } : null, - created_at: u.created_at, - last_login: u.last_login, - is_staff: u.is_staff === 1, - })); + return result.results.map(toStaffUserDto); } catch { throw new HttpError(500, 'Failed to search users'); } } + async setFastTrackAccess( + targetUserId: number, + enabled: boolean, + requestingUserId: number, + ): Promise<{ enabled: boolean; expires_at: string | null }> { + const hasPermission = await this.roles.hasPermission(requestingUserId, StaffRole.PRODUCT_MANAGER); + if (!hasPermission) { + throw new HttpError(403, 'Forbidden: Only product managers and lead developers can manage fast-track access'); + } + + const target = await this.dbSession.executeLatest<{ id: number; vatsim_id: string }>( + 'SELECT id, vatsim_id FROM users WHERE id = ? LIMIT 1', + [targetUserId], + ); + const targetUser = target.results[0]; + if (!targetUser) throw new HttpError(404, 'User not found'); + + if (enabled) { + await this.dbSession.executeWrite( + `INSERT INTO contributor_fast_track ( + user_id, enabled, granted_by, granted_at, expires_at, updated_by, updated_at + ) VALUES (?, 1, ?, CURRENT_TIMESTAMP, datetime('now', '+1 year'), ?, CURRENT_TIMESTAMP) + ON CONFLICT(user_id) DO UPDATE SET + enabled = 1, + granted_by = excluded.granted_by, + granted_at = excluded.granted_at, + expires_at = excluded.expires_at, + updated_by = excluded.updated_by, + updated_at = excluded.updated_at`, + [targetUserId, requestingUserId, requestingUserId], + ); + } else { + await this.dbSession.executeWrite( + `UPDATE contributor_fast_track + SET enabled = 0, updated_by = ?, updated_at = CURRENT_TIMESTAMP + WHERE user_id = ? AND enabled = 1`, + [requestingUserId, targetUserId], + ); + } + + const active = await this.dbSession.executeLatest<{ expires_at: string }>( + `SELECT expires_at + FROM contributor_fast_track + WHERE user_id = ? AND enabled = 1 AND expires_at > CURRENT_TIMESTAMP + LIMIT 1`, + [targetUserId], + ); + const expiresAt = active.results[0]?.expires_at ?? null; + + try { + this.posthog?.track('Contributor Fast Track Updated', { + targetVatsimId: targetUser.vatsim_id, + requestingUserId, + enabled: expiresAt !== null, + }); + } catch (error) { + console.warn('Posthog tracking failed', error); + } + + return { enabled: expiresAt !== null, expires_at: expiresAt }; + } + // Delete user by id async deleteUser(userId: number, requestingUserId: number): Promise { // Check if user has permission diff --git a/src/services/vatsim.ts b/src/services/vatsim.ts index 716e616..3fb07cb 100644 --- a/src/services/vatsim.ts +++ b/src/services/vatsim.ts @@ -87,7 +87,7 @@ export class VatsimService { throw new HttpError(502, `Failed to get VATSIM user (${status})`, { statusText: text }, false); } - const userData = (await res.json()) as VatsimUserResponse; + const userData = await res.json(); const vatsim = userData.data.vatsim || {}; const user: VatsimUser = { id: userData.data.cid, diff --git a/src/services/vatsys-profile-generator.ts b/src/services/vatsys-profile-generator.ts index 1879110..72919e8 100644 --- a/src/services/vatsys-profile-generator.ts +++ b/src/services/vatsys-profile-generator.ts @@ -6,6 +6,54 @@ import { cancelResponseBody } from './http'; type VatSysFormat = 'legacy' | 'intas'; +type JsonObject = { [key: string]: JsonValue }; +type JsonValue = null | boolean | number | string | JsonValue[] | JsonObject; + +type GeoBoundingBox = { + south: number; + west: number; + north: number; + east: number; +}; + +type SignaturePoint = { + lat: string; + lon: string; +}; + +type NormalizedPointSignature = { + id: string; + type: ParsedPoint['type']; + coordinates: SignaturePoint[]; + linkedTo: string[]; +}; + +type NormalizedRunwaySignature = { + id: number; + widthFt: string; + leIdent: string; + lePoint: SignaturePoint; + heIdent: string; + hePoint: SignaturePoint; +}; + +type OrientedRunwayEnds = { + leftEnd: GeoPoint; + rightEnd: GeoPoint; + leftEndName: string; + rightEndName: string; +}; + +type AxisCoordinates = { + x: number; + y: number; +}; + +type LocalCoordinates = { + east: number; + north: number; +}; + type AirportBounds = { bbox_min_lat: number | null; bbox_min_lon: number | null; @@ -17,25 +65,17 @@ type VatSysTaxiwayCache = { version: number; fetched_at: string; source_signature: string; - bbox: { - south: number; - west: number; - north: number; - east: number; - }; + bbox: GeoBoundingBox; lines: GeoPoint[][]; windsocks: GeoPoint[]; }; -type VatSysConfig = { - is_vatsys?: boolean; - is_legacy?: boolean; - is_intas?: boolean; - intas_osm_taxiways?: VatSysTaxiwayCache | null; - intas?: { - osm_taxiways?: VatSysTaxiwayCache | null; - }; - [key: string]: unknown; +type VatSysConfig = JsonObject & { + is_vatsys?: JsonValue; + is_legacy?: JsonValue; + is_intas?: JsonValue; + intas_osm_taxiways?: JsonValue; + intas?: JsonValue; }; type VatSysConfigRecord = { @@ -146,6 +186,19 @@ type IntasOsmData = { windsocks: GeoPoint[]; }; +type OverpassElement = { + type: string; + id: number; + geometry?: Array<{ lat?: JsonValue; lon?: JsonValue }>; + lat?: JsonValue; + lon?: JsonValue; + tags?: Record; +}; + +type OverpassResponse = { + elements: OverpassElement[]; +}; + type GenerationInputs = { configRecord: VatSysConfigRecord; points: ParsedPoint[]; @@ -175,6 +228,18 @@ export class VatSysProfileGeneratorService { constructor(private db: D1Database) {} + private isJsonObject(value: JsonValue): value is JsonObject { + return value !== null && !Array.isArray(value) && Object.prototype.toString.call(value) === '[object Object]'; + } + + private isJsonNumber(value: JsonValue | undefined): value is number { + return Object.prototype.toString.call(value) === '[object Number]' && Number.isFinite(Number(value)); + } + + private isJsonString(value: JsonValue | undefined): value is string { + return Object.prototype.toString.call(value) === '[object String]'; + } + async generate(icao: string): Promise { const normalizedIcao = icao.toUpperCase().replace(/[^A-Z0-9]/g, ''); if (!VatSysProfileGeneratorService.ICAO_REGEX.test(normalizedIcao)) { @@ -287,6 +352,7 @@ export class VatSysProfileGeneratorService { }, ]); + // SAFETY: The fixed SELECT projects id and the non-null vatsys TEXT column from division_airports. const row = configResult.results[0] as { id: number; vatsys: string } | undefined; if (!row) { throw new HttpError(404, `No approved division airport found for ${icao}`); @@ -294,25 +360,29 @@ export class VatSysProfileGeneratorService { let config: VatSysConfig; try { - config = JSON.parse(row.vatsys) as VatSysConfig; - if (!config || typeof config !== 'object') { + const parsed: JsonValue = JSON.parse(row.vatsys); + if (!this.isJsonObject(parsed)) { throw new Error('Invalid vatSys flags'); } + config = parsed; } catch { throw new HttpError(422, `Invalid vatSys configuration for ${icao}`); } const points: ParsedPoint[] = []; for (const rawRow of pointsResult.results) { + // SAFETY: The fixed points SELECT projects every PointRow field and restricts type to the two declared values. const point = this.parsePoint(rawRow as PointRow); if (point) points.push(point); } + // SAFETY: The fixed runways SELECT projects every RunwayRow field from schema-backed columns. const runways = (runwaysResult.results as RunwayRow[]).filter((runway) => this.getRunwayAxis(runway) !== null); return { configRecord: { id: row.id, config }, points, runways, + // SAFETY: The fixed airports SELECT projects exactly the four nullable numeric bounding-box columns. airportBounds: boundsResult.results[0] as AirportBounds | undefined, }; } @@ -451,7 +521,12 @@ export class VatSysProfileGeneratorService { } private parseIntasTaxiwayCache(config: VatSysConfig, sourceSignature: string): VatSysTaxiwayCache | null { - const cache = config.intas_osm_taxiways ?? config.intas?.osm_taxiways; + const nestedIntas = config.intas && this.isJsonObject(config.intas) ? config.intas : null; + const rawCache = config.intas_osm_taxiways ?? nestedIntas?.osm_taxiways; + if (!rawCache || !this.isJsonObject(rawCache)) { + return null; + } + const cache = this.parseTaxiwayCache(rawCache); if ( !cache || cache.version !== VatSysProfileGeneratorService.INTAS_TAXIWAY_CACHE_VERSION || @@ -459,22 +534,43 @@ export class VatSysProfileGeneratorService { ) { return null; } - if (!Array.isArray(cache.lines)) { + if (cache.lines.length === 0) { return null; } - const lines = cache.lines.map((line) => this.normalizeTaxiwayLine(line)).filter((line): line is GeoPoint[] => line !== null); - if (lines.length === 0) { + return cache; + } + + private parseTaxiwayCache(rawCache: JsonObject): VatSysTaxiwayCache | null { + const bbox = rawCache.bbox; + if ( + !this.isJsonNumber(rawCache.version) || + !this.isJsonString(rawCache.fetched_at) || + !this.isJsonString(rawCache.source_signature) || + !bbox || + !this.isJsonObject(bbox) || + !this.isJsonNumber(bbox.south) || + !this.isJsonNumber(bbox.west) || + !this.isJsonNumber(bbox.north) || + !this.isJsonNumber(bbox.east) || + !Array.isArray(rawCache.lines) + ) { return null; } - const windsocks = Array.isArray(cache.windsocks) - ? cache.windsocks.map((point) => this.normalizeGeoPoint(point)).filter((point): point is GeoPoint => point !== null) - : []; + const lines = rawCache.lines + .filter((line): line is JsonValue[] => Array.isArray(line)) + .map((line) => line.map((point) => (this.isJsonObject(point) ? { lat: point.lat, lon: point.lon } : {}))); + const windsocks = Array.isArray(rawCache.windsocks) + ? rawCache.windsocks.map((point) => (this.isJsonObject(point) ? { lat: point.lat, lon: point.lon } : {})) + : []; return { - ...cache, - lines, - windsocks, + version: rawCache.version, + fetched_at: rawCache.fetched_at, + source_signature: rawCache.source_signature, + bbox: { south: bbox.south, west: bbox.west, north: bbox.north, east: bbox.east }, + lines: lines.map((line) => this.normalizeTaxiwayLine(line)).filter((line): line is GeoPoint[] => line !== null), + windsocks: windsocks.map((point) => this.normalizeGeoPoint(point)).filter((point): point is GeoPoint => point !== null), }; } @@ -503,7 +599,7 @@ export class VatSysProfileGeneratorService { return `v1:${this.hashString(JSON.stringify(payload))}`; } - private normalizeBboxForSignature(bbox: { south: number; west: number; north: number; east: number }): Record { + private normalizeBboxForSignature(bbox: GeoBoundingBox) { return { south: this.formatCoordinate(bbox.south), west: this.formatCoordinate(bbox.west), @@ -512,12 +608,7 @@ export class VatSysProfileGeneratorService { }; } - private normalizePointForSignature(point: ParsedPoint): { - id: string; - type: ParsedPoint['type']; - coordinates: Array<{ lat: string; lon: string }>; - linkedTo: string[]; - } { + private normalizePointForSignature(point: ParsedPoint): NormalizedPointSignature { return { id: point.id, type: point.type, @@ -526,14 +617,7 @@ export class VatSysProfileGeneratorService { }; } - private normalizeRunwayForSignature(runway: RunwayRow): { - id: number; - widthFt: string; - leIdent: string; - lePoint: { lat: string; lon: string }; - heIdent: string; - hePoint: { lat: string; lon: string }; - } { + private normalizeRunwayForSignature(runway: RunwayRow): NormalizedRunwaySignature { return { id: runway.id, widthFt: runway.width_ft, @@ -550,7 +634,7 @@ export class VatSysProfileGeneratorService { }; } - private formatPointForSignature(point: GeoPoint): { lat: string; lon: string } { + private formatPointForSignature(point: GeoPoint): SignaturePoint { return { lat: this.formatCoordinate(point.lat), lon: this.formatCoordinate(point.lon), @@ -571,7 +655,7 @@ export class VatSysProfileGeneratorService { configRecord: VatSysConfigRecord, cache: VatSysTaxiwayCache, ): Promise { - const nextConfig: VatSysConfig = { + const nextConfig = { ...configRecord.config, intas_osm_taxiways: cache, }; @@ -588,7 +672,7 @@ export class VatSysProfileGeneratorService { private validateAirportBounds( row: AirportBounds | undefined, icao: string, - ): { south: number; west: number; north: number; east: number } { + ): GeoBoundingBox { if (!row) { throw new HttpError(422, `No airport metadata found for ${icao}`); } @@ -614,20 +698,8 @@ export class VatSysProfileGeneratorService { private async fetchIntasOsmData( icao: string, - bbox: { south: number; west: number; north: number; east: number }, + bbox: GeoBoundingBox, ): Promise { - type OverpassElement = { - type: string; - id: number; - geometry?: Array<{ lat?: number; lon?: number }>; - lat?: number; - lon?: number; - tags?: Record; - }; - type OverpassResponse = { - elements?: OverpassElement[]; - }; - const query = `[out:json][timeout:25];(way["aeroway"="taxiway"](${bbox.south},${bbox.west},${bbox.north},${bbox.east});nwr["aeroway"="windsock"](${bbox.south},${bbox.west},${bbox.north},${bbox.east}););out body geom;`; const body = new URLSearchParams({ data: query }).toString(); const maxAttempts = 3; @@ -652,7 +724,8 @@ export class VatSysProfileGeneratorService { throw new HttpError(503, `OSM taxiway data unavailable for ${icao}`); } - const json = (await response.json()) as OverpassResponse; + const payload: JsonValue = await response.json(); + const json = this.parseOverpassResponse(payload); const taxiwayLines: GeoPoint[][] = []; const windSockPoints: GeoPoint[] = []; for (const element of json.elements ?? []) { @@ -689,6 +762,46 @@ export class VatSysProfileGeneratorService { throw new HttpError(503, `OSM taxiway data unavailable for ${icao}`); } + private parseOverpassResponse(payload: JsonValue): OverpassResponse { + if (!this.isJsonObject(payload)) { + throw new Error('Invalid Overpass response'); + } + if (payload.elements === undefined) return { elements: [] }; + if (!Array.isArray(payload.elements)) throw new Error('Invalid Overpass response elements'); + + const elements: OverpassElement[] = []; + for (const rawElement of payload.elements) { + if ( + !this.isJsonObject(rawElement) || + !this.isJsonString(rawElement.type) || + !this.isJsonNumber(rawElement.id) + ) { + continue; + } + + const geometry = Array.isArray(rawElement.geometry) + ? rawElement.geometry + .filter((point): point is JsonObject => this.isJsonObject(point)) + .map((point) => ({ lat: point.lat, lon: point.lon })) + : undefined; + const tags: Record = {}; + if (rawElement.tags && this.isJsonObject(rawElement.tags)) { + for (const [key, value] of Object.entries(rawElement.tags)) { + if (this.isJsonString(value)) tags[key] = value; + } + } + elements.push({ + type: rawElement.type, + id: rawElement.id, + lat: rawElement.lat, + lon: rawElement.lon, + geometry, + tags, + }); + } + return { elements }; + } + private buildIntasXml(taxiwayLines: GeoPoint[][], windsocks: GeoPoint[], stopbars: ParsedPoint[], leadOns: ParsedPoint[]): string { const linkedLeadOns = this.buildLeadOnLookup(leadOns); const lines = ['', '', ' ']; @@ -1123,7 +1236,7 @@ export class VatSysProfileGeneratorService { }; } - private normalizeTaxiwayLine(rawLine: Array>): GeoPoint[] | null { + private normalizeTaxiwayLine(rawLine: Array<{ lat?: JsonValue; lon?: JsonValue }>): GeoPoint[] | null { const points: GeoPoint[] = []; for (const point of rawLine) { const normalized = this.normalizeGeoPoint(point); @@ -1138,10 +1251,10 @@ export class VatSysProfileGeneratorService { return points.length >= 2 ? points : null; } - private normalizeGeoPoint(point: Partial): GeoPoint | null { + private normalizeGeoPoint(point: { lat?: JsonValue; lon?: JsonValue }): GeoPoint | null { const lat = point.lat; const lon = point.lon; - if (typeof lat !== 'number' || typeof lon !== 'number' || !Number.isFinite(lat) || !Number.isFinite(lon)) { + if (!this.isJsonNumber(lat) || !this.isJsonNumber(lon)) { return null; } return { @@ -1150,7 +1263,7 @@ export class VatSysProfileGeneratorService { }; } - private getOsmElementPoint(element: { lat?: number; lon?: number; geometry?: Array<{ lat?: number; lon?: number }> }): GeoPoint | null { + private getOsmElementPoint(element: OverpassElement): GeoPoint | null { const nodePoint = this.normalizeGeoPoint({ lat: element.lat, lon: element.lon }); if (nodePoint) return nodePoint; let latTotal = 0; @@ -1195,15 +1308,14 @@ export class VatSysProfileGeneratorService { private parseCoordinates(rawCoordinates: string): GeoPoint[] { try { - const parsed = JSON.parse(rawCoordinates) as unknown; + const parsed: JsonValue = JSON.parse(rawCoordinates); const rawPoints = Array.isArray(parsed) ? parsed : [parsed]; const coordinates: GeoPoint[] = []; for (const value of rawPoints) { - if (!value || typeof value !== 'object') continue; - const point = value as { lat?: unknown; lng?: unknown; lon?: unknown }; - const lat = typeof point.lat === 'number' ? point.lat : null; - const lon = typeof point.lng === 'number' ? point.lng : typeof point.lon === 'number' ? point.lon : null; - if (lat === null || lon === null || !Number.isFinite(lat) || !Number.isFinite(lon)) continue; + if (!this.isJsonObject(value)) continue; + const lat = this.isJsonNumber(value.lat) ? value.lat : null; + const lon = this.isJsonNumber(value.lng) ? value.lng : this.isJsonNumber(value.lon) ? value.lon : null; + if (lat === null || lon === null) continue; coordinates.push({ lat, lon }); } return coordinates; @@ -1215,11 +1327,11 @@ export class VatSysProfileGeneratorService { private parseLinkedTo(rawLinkedTo: string | null): string[] { if (!rawLinkedTo) return []; try { - const parsed = JSON.parse(rawLinkedTo) as unknown; + const parsed: JsonValue = JSON.parse(rawLinkedTo); if (Array.isArray(parsed)) { - return parsed.filter((id): id is string => typeof id === 'string' && id.length > 0); + return parsed.filter((id): id is string => this.isJsonString(id) && id.length > 0); } - if (typeof parsed === 'string' && parsed.length > 0) { + if (this.isJsonString(parsed) && parsed.length > 0) { return [parsed]; } } catch { @@ -1254,7 +1366,7 @@ export class VatSysProfileGeneratorService { leEnd: GeoPoint, heEnd: GeoPoint, towerPosition?: GeoPoint, - ): { leftEnd: GeoPoint; rightEnd: GeoPoint; leftEndName: string; rightEndName: string } { + ): OrientedRunwayEnds { const mapOriented = this.orientRunwayEndsByMapAxis(runway, leEnd, heEnd); if (mapOriented) { return mapOriented; @@ -1306,7 +1418,7 @@ export class VatSysProfileGeneratorService { runway: RunwayRow, leEnd: GeoPoint, heEnd: GeoPoint, - ): { leftEnd: GeoPoint; rightEnd: GeoPoint; leftEndName: string; rightEndName: string } | null { + ): OrientedRunwayEnds | null { const heLocal = this.toLocalMeters(heEnd, leEnd); const absEast = Math.abs(heLocal.east); const absNorth = Math.abs(heLocal.north); @@ -1545,7 +1657,7 @@ export class VatSysProfileGeneratorService { return this.clamp(axis.lengthMeters * 0.04, 100, 220); } - private projectToAxis(point: GeoPoint, axis: ProfileAxis): { x: number; y: number } { + private projectToAxis(point: GeoPoint, axis: ProfileAxis): AxisCoordinates { const projection = this.getAxisProjection(axis); const earthRadiusMeters = 6371000; const deltaLat = ((point.lat - axis.start.lat) * Math.PI) / 180; @@ -1576,7 +1688,7 @@ export class VatSysProfileGeneratorService { return projection; } - private toLocalMeters(point: GeoPoint, origin: GeoPoint): { east: number; north: number } { + private toLocalMeters(point: GeoPoint, origin: GeoPoint): LocalCoordinates { const earthRadiusMeters = 6371000; const originLatRad = (origin.lat * Math.PI) / 180; const deltaLat = ((point.lat - origin.lat) * Math.PI) / 180; diff --git a/src/services/vatsys-profiles.ts b/src/services/vatsys-profiles.ts index 795db66..e36d644 100644 --- a/src/services/vatsys-profiles.ts +++ b/src/services/vatsys-profiles.ts @@ -33,10 +33,8 @@ export class VatSysProfilesService { } /** Basic XML safety checks (rejects DOCTYPE/ENTITY and enforces declaration) */ - private static validateXml(xml: ArrayBuffer | string): void { - let text: string; - if (typeof xml === 'string') text = xml; - else text = new TextDecoder().decode(xml); + private static validateXml(xml: ArrayBuffer): void { + const text = new TextDecoder().decode(xml); if (!text || text.length === 0) throw new Error('Empty XML'); if (text.length > VatSysProfilesService.MAX_XML_BYTES) throw new Error('XML too large'); const trimmed = text.trim(); @@ -74,7 +72,7 @@ export class VatSysProfilesService { // Derive ICAO as first 4 alphanumeric chars of the base name const icaoCandidate = (base.substring(0, 4) || '').toUpperCase(); const derivedIcao = /^[A-Z0-9]{4}$/.test(icaoCandidate) ? icaoCandidate : ''; - return { icao: derivedIcao, name: file, key: o.key } as VatSysProfileItem; + return { icao: derivedIcao, name: file, key: o.key }; }); return all; } diff --git a/src/services/xml-sanitizer.ts b/src/services/xml-sanitizer.ts index 375f38a..f2ae9d3 100644 --- a/src/services/xml-sanitizer.ts +++ b/src/services/xml-sanitizer.ts @@ -7,9 +7,9 @@ export function sanitizeContributionXml(raw: string, opts?: { maxBytes?: number if (!raw) throw new Error('Empty XML'); const maxBytes = opts?.maxBytes ?? MAX_CONTRIBUTION_XML_BYTES; - // Size guard (counting UTF-16 code units approximates bytes for ASCII subset typical of these files) - if (raw.length > 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..54cbdb2 --- /dev/null +++ b/src/services/xplane-removals.ts @@ -0,0 +1,254 @@ +export interface XPlaneDsfSelector { + kind: 'dsf-string' | 'dsf-object'; + source: string; + sha256: string; + definition: string; + command: number; + pool: number; + filter: number; + index: number; +} + +export interface XPlaneRemovalSelector { + feature: string; + code: number; + run: number; + ranges: Array<[number, number]>; +} + +export interface XPlaneRemovalArtifact { + schema: 'bars-xplane-removals/v1' | 'bars-xplane-removals/v2'; + dsfSelectors?: XPlaneDsfSelector[]; + 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(); + const dsfSelectors = new Map(); + let version = '1'; + let dsfElementCount = 0; + 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('