From cf28ffccb63642d24b0806e0e377352e7540a97c Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 11 Nov 2026 11:43:03 -0800 Subject: [PATCH] refactor(push): organize provider, delivery, and host authentication owners --- .../push/src/apns-session-replacement.test.ts | 45 ------- cloud/apps/push/src/config.ts | 2 +- .../src/{ => database}/durable-push-schema.ts | 0 .../push-background-database.ts | 2 +- .../push/src/{ => database}/push-database.ts | 10 +- .../push/src/{ => database}/push-schema.ts | 0 .../{ => delivery}/durable-push-claim.test.ts | 81 ++----------- .../durable-push-retention.test.ts | 21 ---- .../durable-push-store.test-fixture.ts | 5 +- .../{ => delivery}/durable-push-store.test.ts | 4 +- .../src/{ => delivery}/durable-push-store.ts | 10 +- .../durable-push-worker.test.ts | 95 +-------------- .../src/{ => delivery}/durable-push-worker.ts | 0 .../{ => delivery}/push-delivery-message.ts | 2 +- .../{ => delivery}/push-delivery-payload.ts | 2 +- .../src/{ => delivery}/push-dispatcher.ts | 10 +- .../{ => delivery}/push-queued-dismissal.ts | 4 +- .../{ => delivery}/push-worker-concurrency.ts | 0 .../device-registration-delete-race.test.ts | 92 --------------- .../device-registry-store.test.ts | 27 +---- .../{ => devices}/device-registry-store.ts | 17 +-- .../durable-push-claim-mixed-revision.test.ts | 103 ---------------- .../src/{ => host-auth}/canonical-base64.ts | 0 .../host-challenge-answering.test-fixture.ts | 29 +++-- .../host-challenge-store.test.ts | 17 +-- .../{ => host-auth}/host-challenge-store.ts | 8 +- .../src/{ => host-auth}/host-fingerprint.ts | 2 +- .../host-proof-concurrency.test.ts | 2 +- .../host-session-store.test.ts | 19 +-- .../src/{ => host-auth}/host-session-store.ts | 4 +- .../push-session-concurrency.test.ts | 25 +--- .../{ => http}/client-ip-rate-limit.test.ts | 14 +-- .../src/{ => http}/client-ip-rate-limit.ts | 2 +- .../src/{ => http}/push-auth-admission.ts | 0 .../push-authenticated-ip-limit.test.ts | 4 +- .../push/src/{ => http}/push-readiness.ts | 2 +- .../push/src/{ => http}/push-request-drain.ts | 0 .../{ => http}/push-send-idempotency.test.ts | 30 +---- .../src/{ => http}/push-server-auth.test.ts | 8 +- .../push-server-harness.test-fixture.ts | 14 ++- .../src/{ => http}/push-server-limits.test.ts | 56 +-------- .../src/{ => http}/push-server-send.test.ts | 6 +- .../push/src/http/push-server-wake.test.ts | 44 +++++++ cloud/apps/push/src/index.ts | 2 +- .../apns}/apns-authentication-token.ts | 2 +- .../{ => providers/apns}/apns-client.test.ts | 37 +----- .../src/{ => providers/apns}/apns-client.ts | 8 +- .../apns}/apns-http2-transport.ts | 1 - .../apns}/apns-stream-response.test.ts | 10 -- .../apns}/apns-stream-response.ts | 2 +- .../apns}/apns-topic-recovery.test.ts | 4 +- .../{ => providers/fcm}/fcm-access-token.ts | 0 .../{ => providers/fcm}/fcm-client.test.ts | 38 +----- .../src/{ => providers/fcm}/fcm-client.ts | 8 +- .../{ => providers}/provider-retry-delay.ts | 0 .../{ => providers}/push-provider-outcome.ts | 0 cloud/apps/push/src/push-background.ts | 9 +- .../push-database-postgres-startup.test.ts | 101 ---------------- .../push/src/push-delivery-lifecycle.test.ts | 15 +-- .../push/src/push-dismissal-provider.test.ts | 31 ----- .../push/src/push-notification-sound.test.ts | 33 ------ .../push/src/push-schema-boot-lock.test.ts | 56 --------- cloud/apps/push/src/push-server-wake.test.ts | 111 ------------------ cloud/apps/push/src/push-server.ts | 54 +++++---- cloud/apps/push/src/push-validation.test.ts | 90 -------------- .../push/src/sqlite-transaction-queue.test.ts | 101 ---------------- cloud/packages/cloud-database/package.json | 12 +- cloud/packages/push-contract/package.json | 32 ++++- 68 files changed, 264 insertions(+), 1311 deletions(-) delete mode 100644 cloud/apps/push/src/apns-session-replacement.test.ts rename cloud/apps/push/src/{ => database}/durable-push-schema.ts (100%) rename cloud/apps/push/src/{ => database}/push-background-database.ts (92%) rename cloud/apps/push/src/{ => database}/push-database.ts (66%) rename cloud/apps/push/src/{ => database}/push-schema.ts (100%) rename cloud/apps/push/src/{ => delivery}/durable-push-claim.test.ts (67%) rename cloud/apps/push/src/{ => delivery}/durable-push-retention.test.ts (68%) rename cloud/apps/push/src/{ => delivery}/durable-push-store.test-fixture.ts (96%) rename cloud/apps/push/src/{ => delivery}/durable-push-store.test.ts (99%) rename cloud/apps/push/src/{ => delivery}/durable-push-store.ts (97%) rename cloud/apps/push/src/{ => delivery}/durable-push-worker.test.ts (66%) rename cloud/apps/push/src/{ => delivery}/durable-push-worker.ts (100%) rename cloud/apps/push/src/{ => delivery}/push-delivery-message.ts (96%) rename cloud/apps/push/src/{ => delivery}/push-delivery-payload.ts (95%) rename cloud/apps/push/src/{ => delivery}/push-dispatcher.ts (81%) rename cloud/apps/push/src/{ => delivery}/push-queued-dismissal.ts (96%) rename cloud/apps/push/src/{ => delivery}/push-worker-concurrency.ts (100%) delete mode 100644 cloud/apps/push/src/device-registration-delete-race.test.ts rename cloud/apps/push/src/{ => devices}/device-registry-store.test.ts (79%) rename cloud/apps/push/src/{ => devices}/device-registry-store.ts (95%) delete mode 100644 cloud/apps/push/src/durable-push-claim-mixed-revision.test.ts rename cloud/apps/push/src/{ => host-auth}/canonical-base64.ts (100%) rename cloud/apps/push/src/{ => host-auth}/host-challenge-answering.test-fixture.ts (90%) rename cloud/apps/push/src/{ => host-auth}/host-challenge-store.test.ts (89%) rename cloud/apps/push/src/{ => host-auth}/host-challenge-store.ts (95%) rename cloud/apps/push/src/{ => host-auth}/host-fingerprint.ts (96%) rename cloud/apps/push/src/{ => host-auth}/host-proof-concurrency.test.ts (95%) rename cloud/apps/push/src/{ => host-auth}/host-session-store.test.ts (72%) rename cloud/apps/push/src/{ => host-auth}/host-session-store.ts (93%) rename cloud/apps/push/src/{ => host-auth}/push-session-concurrency.test.ts (60%) rename cloud/apps/push/src/{ => http}/client-ip-rate-limit.test.ts (97%) rename cloud/apps/push/src/{ => http}/client-ip-rate-limit.ts (97%) rename cloud/apps/push/src/{ => http}/push-auth-admission.ts (100%) rename cloud/apps/push/src/{ => http}/push-authenticated-ip-limit.test.ts (91%) rename cloud/apps/push/src/{ => http}/push-readiness.ts (90%) rename cloud/apps/push/src/{ => http}/push-request-drain.ts (100%) rename cloud/apps/push/src/{ => http}/push-send-idempotency.test.ts (56%) rename cloud/apps/push/src/{ => http}/push-server-auth.test.ts (93%) rename cloud/apps/push/src/{ => http}/push-server-harness.test-fixture.ts (89%) rename cloud/apps/push/src/{ => http}/push-server-limits.test.ts (78%) rename cloud/apps/push/src/{ => http}/push-server-send.test.ts (97%) create mode 100644 cloud/apps/push/src/http/push-server-wake.test.ts rename cloud/apps/push/src/{ => providers/apns}/apns-authentication-token.ts (96%) rename cloud/apps/push/src/{ => providers/apns}/apns-client.test.ts (64%) rename cloud/apps/push/src/{ => providers/apns}/apns-client.ts (91%) rename cloud/apps/push/src/{ => providers/apns}/apns-http2-transport.ts (98%) rename cloud/apps/push/src/{ => providers/apns}/apns-stream-response.test.ts (99%) rename cloud/apps/push/src/{ => providers/apns}/apns-stream-response.ts (96%) rename cloud/apps/push/src/{ => providers/apns}/apns-topic-recovery.test.ts (90%) rename cloud/apps/push/src/{ => providers/fcm}/fcm-access-token.ts (100%) rename cloud/apps/push/src/{ => providers/fcm}/fcm-client.test.ts (74%) rename cloud/apps/push/src/{ => providers/fcm}/fcm-client.ts (93%) rename cloud/apps/push/src/{ => providers}/provider-retry-delay.ts (100%) rename cloud/apps/push/src/{ => providers}/push-provider-outcome.ts (100%) delete mode 100644 cloud/apps/push/src/push-database-postgres-startup.test.ts delete mode 100644 cloud/apps/push/src/push-dismissal-provider.test.ts delete mode 100644 cloud/apps/push/src/push-notification-sound.test.ts delete mode 100644 cloud/apps/push/src/push-schema-boot-lock.test.ts delete mode 100644 cloud/apps/push/src/push-server-wake.test.ts delete mode 100644 cloud/apps/push/src/push-validation.test.ts delete mode 100644 cloud/apps/push/src/sqlite-transaction-queue.test.ts diff --git a/cloud/apps/push/src/apns-session-replacement.test.ts b/cloud/apps/push/src/apns-session-replacement.test.ts deleted file mode 100644 index 2678732ca94..00000000000 --- a/cloud/apps/push/src/apns-session-replacement.test.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { EventEmitter } from 'node:events' -import { expect, it, vi } from 'vitest' -const mocks = vi.hoisted(() => ({ - connect: vi.fn(), - read: vi.fn(async () => ({ status: 200, body: '' })) -})) -vi.mock('node:http2', async (original) => ({ - ...(await original()), - connect: mocks.connect -})) -vi.mock('./apns-stream-response.js', () => ({ readApnsStreamResponse: mocks.read })) -import { createApnsHttp2Transport } from './apns-http2-transport.js' - -it('keeps the replacement cached when the draining session closes later', async () => { - const sessions: Array< - EventEmitter & { - closed: boolean - destroyed: boolean - request: ReturnType - close: ReturnType - } - > = [] - mocks.connect.mockImplementation(() => { - const session = Object.assign(new EventEmitter(), { - closed: false, - destroyed: false, - request: vi.fn(() => ({})), - close: vi.fn() - }) - sessions.push(session) - return session - }) - const transport = createApnsHttp2Transport() - const request = { host: 'api.push.apple.com', path: '/synthetic', headers: {}, body: '{}' } - await transport(request) - sessions[0]!.closed = true - await transport(request) - sessions[0]!.emit('close') - sessions[0]!.emit('error', new Error('old-session')) - await transport(request) - expect(sessions).toHaveLength(2) - expect(sessions[1]!.request).toHaveBeenCalledTimes(2) - transport.close() - expect(sessions[1]!.close).toHaveBeenCalledOnce() -}) diff --git a/cloud/apps/push/src/config.ts b/cloud/apps/push/src/config.ts index 2ed0d2111f1..3a885edc22c 100644 --- a/cloud/apps/push/src/config.ts +++ b/cloud/apps/push/src/config.ts @@ -1,4 +1,4 @@ -import { PUSH_DEFAULTS } from '@orca-cloud/push-contract' +import { PUSH_DEFAULTS } from '@orca-cloud/push-contract/push-limits' import { z } from 'zod' export const PUSH_DATABASE_POOL_MAX = 10 diff --git a/cloud/apps/push/src/durable-push-schema.ts b/cloud/apps/push/src/database/durable-push-schema.ts similarity index 100% rename from cloud/apps/push/src/durable-push-schema.ts rename to cloud/apps/push/src/database/durable-push-schema.ts diff --git a/cloud/apps/push/src/push-background-database.ts b/cloud/apps/push/src/database/push-background-database.ts similarity index 92% rename from cloud/apps/push/src/push-background-database.ts rename to cloud/apps/push/src/database/push-background-database.ts index 8a6b759badf..d3d26b9bf31 100644 --- a/cloud/apps/push/src/push-background-database.ts +++ b/cloud/apps/push/src/database/push-background-database.ts @@ -1,4 +1,4 @@ -import type { PushDatabase } from './push-database.js' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' // Caps worker and prune traffic at poolMax - 1 connections so request-path lookups always find one. export function reserveRequestConnection(database: PushDatabase, poolMax: number): PushDatabase { diff --git a/cloud/apps/push/src/push-database.ts b/cloud/apps/push/src/database/push-database.ts similarity index 66% rename from cloud/apps/push/src/push-database.ts rename to cloud/apps/push/src/database/push-database.ts index 0684d1a3f66..fa6e375f9df 100644 --- a/cloud/apps/push/src/push-database.ts +++ b/cloud/apps/push/src/database/push-database.ts @@ -3,15 +3,17 @@ import { openInMemoryCloudDatabase, type CloudDatabase, type OpenCloudDatabaseInput -} from '@orca-cloud/cloud-database' +} from '@orca-cloud/cloud-database/cloud-database' import { pushSchemaStatements } from './push-schema.js' -export type { CloudDatabase as PushDatabase, SqlRow } from '@orca-cloud/cloud-database' - export async function openPushDatabase( input: Omit ): Promise { - return await openCloudDatabase({ ...input, service: 'push', schemaStatements: pushSchemaStatements() }) + return await openCloudDatabase({ + ...input, + service: 'push', + schemaStatements: pushSchemaStatements() + }) } export async function openInMemoryPushDatabase(): Promise { diff --git a/cloud/apps/push/src/push-schema.ts b/cloud/apps/push/src/database/push-schema.ts similarity index 100% rename from cloud/apps/push/src/push-schema.ts rename to cloud/apps/push/src/database/push-schema.ts diff --git a/cloud/apps/push/src/durable-push-claim.test.ts b/cloud/apps/push/src/delivery/durable-push-claim.test.ts similarity index 67% rename from cloud/apps/push/src/durable-push-claim.test.ts rename to cloud/apps/push/src/delivery/durable-push-claim.test.ts index 2d5e91d4000..93f65135504 100644 --- a/cloud/apps/push/src/durable-push-claim.test.ts +++ b/cloud/apps/push/src/delivery/durable-push-claim.test.ts @@ -1,73 +1,19 @@ import { afterEach, expect, it } from 'vitest' -import type { PushDatabase } from './push-database.js' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' import { DELIVERY_LEASE_MS, DurablePushStore } from './durable-push-store.js' -import { reserveRequestConnection } from './push-background-database.js' -import { cleanupDurablePushFixtures, DEVICE_HEAD_SQL, durablePushTestDatabaseUrl, fixture, notification, pauseAfter, within } from './durable-push-store.test-fixture.js' +import { + cleanupDurablePushFixtures, + DEVICE_HEAD_SQL, + durablePushTestDatabaseUrl, + fixture, + notification, + pauseAfter, + within +} from './durable-push-store.test-fixture.js' afterEach(cleanupDurablePushFixtures) -it.skipIf(!durablePushTestDatabaseUrl)( - 'leaves completion, renewal, requests and another store independent of queued claims', - async () => { - const { db, store, clock, advance } = await fixture({ poolMax: 6 }) - await store.accept('host', 'finished', notification(1)) - const finished = await store.claim() - await store.accept('host', 'renewed', notification(2)) - const renewed = await store.claim() - if (!finished || !renewed) throw new Error('setup_claim_missing') - await store.accept('host', 'phone-a', notification(3)) - advance(1) - await store.accept('host', 'phone-b', notification(4)) - advance(1) - await store.accept('host', 'phone-c', notification(5)) - advance(1) - await store.accept('host', 'phone-d', notification(6)) - const background = reserveRequestConnection(db, 6) - const held = [pauseAfter(background), pauseAfter(background), pauseAfter(background)] as const - let admitted = 0 - const observed: PushDatabase = { - ...background, - transaction: (run) => (held[admitted++]?.wrapped ?? background).transaction(run) - } - const limited = new DurablePushStore(db, clock, observed) - const first = limited.claim() - await held[0].atCandidate - const second = limited.claim() - await held[1].atCandidate - const third = limited.claim() - await held[2].atCandidate - const waiting = Array.from({ length: 3 }, () => limited.claim()) - try { - advance(10) - const [, , rows, other] = await within( - Promise.all([ - limited.finish(finished), - limited.renew(renewed), - db.query('SELECT COUNT(*) AS total FROM push_devices'), - store.claim() - ]), - 2_000 - ) - expect(Number(rows[0]?.total)).toBe(0) - expect(other?.registrationId).toBe('phone-d') - const [lease] = await db.query( - 'SELECT lease_until FROM push_delivery_batches WHERE batch_id = ?', - [renewed.id] - ) - expect(Number(lease?.lease_until)).toBe(clock() + DELIVERY_LEASE_MS) - expect(await store.pendingCount('finished')).toBe(0) - } finally { - for (const pause of held) pause.release() - await Promise.allSettled([first, second, third, ...waiting]) - } - expect((await first)?.registrationId).toBe('phone-a') - expect((await second)?.registrationId).toBe('phone-b') - expect((await third)?.registrationId).toBe('phone-c') - expect(await Promise.all(waiting)).toEqual([null, null, null]) - } -) - it.skipIf(!durablePushTestDatabaseUrl)( 'rolls back a failed claim and admits a fourth waiter while the other claims stay held', async () => { @@ -117,8 +63,6 @@ it.skipIf(!durablePushTestDatabaseUrl)( } ) - - it('leases exactly one delivery per device across concurrent claimers', async () => { const { db, store, clock, advance } = await fixture() for (let seq = 1; seq <= 5; seq++) { @@ -197,11 +141,6 @@ it.skipIf(!durablePushTestDatabaseUrl)( // Only the TTL term keeps the candidate scan off an unpruned backlog; expires_at is not indexed first. - - - - - it('frees a device held by a crashed worker once its lease lapses', async () => { const { db, store, clock, advance } = await fixture() await store.accept('host', 'phone', notification(1)) diff --git a/cloud/apps/push/src/durable-push-retention.test.ts b/cloud/apps/push/src/delivery/durable-push-retention.test.ts similarity index 68% rename from cloud/apps/push/src/durable-push-retention.test.ts rename to cloud/apps/push/src/delivery/durable-push-retention.test.ts index 72dcc225b79..4304bcce3f4 100644 --- a/cloud/apps/push/src/durable-push-retention.test.ts +++ b/cloud/apps/push/src/delivery/durable-push-retention.test.ts @@ -5,7 +5,6 @@ import { fixture, within } from './durable-push-store.test-fixture.js' -import { PRUNE_BATCH_ROWS } from './durable-push-store.js' afterEach(cleanupDurablePushFixtures) @@ -27,26 +26,6 @@ const retentionTables = [ } ] as const -it.each(retentionTables)( - 'prunes expired $table rows across batches and preserves the cutoff', - async ({ table, columns, values }) => { - const { db, store, clock } = await fixture() - const cutoff = clock() - 24 * 60 * 60_000 - const expired = PRUNE_BATCH_ROWS + 3 - await db.query( - `INSERT INTO ${table} (${columns}) - WITH RECURSIVE seq(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM seq WHERE n < ?), - rows AS (SELECT n, CASE WHEN n <= ? THEN ? ELSE ? + n - ? - 1 END AS created_at FROM seq) - SELECT ${values} FROM rows`, - [expired + 2, expired, cutoff - 1, cutoff, expired] - ) - expect(await store.prune()).toEqual({ deleted: expired, saturated: false }) - const preserved = await db.query(`SELECT created_at FROM ${table} ORDER BY created_at`) - expect(preserved.map((row) => Number(row.created_at))).toEqual([cutoff, cutoff + 1]) - expect(await store.prune()).toEqual({ deleted: 0, saturated: false }) - } -) - it.skipIf(!durablePushTestDatabaseUrl).each(retentionTables)( 'skips a locked oldest $table row until its transaction releases it', async ({ table, columns, values }) => { diff --git a/cloud/apps/push/src/durable-push-store.test-fixture.ts b/cloud/apps/push/src/delivery/durable-push-store.test-fixture.ts similarity index 96% rename from cloud/apps/push/src/durable-push-store.test-fixture.ts rename to cloud/apps/push/src/delivery/durable-push-store.test-fixture.ts index 67ef6999db1..c9aa565db9b 100644 --- a/cloud/apps/push/src/durable-push-store.test-fixture.ts +++ b/cloud/apps/push/src/delivery/durable-push-store.test-fixture.ts @@ -1,7 +1,8 @@ import { randomUUID } from 'node:crypto' import pg from 'pg' -import type { PushNotification } from '@orca-cloud/push-contract' -import { openInMemoryPushDatabase, openPushDatabase, type PushDatabase } from './push-database.js' +import type { PushNotification } from '@orca-cloud/push-contract/send-messages' +import { openInMemoryPushDatabase, openPushDatabase } from '../database/push-database.js' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' import { DurablePushStore } from './durable-push-store.js' export const durablePushTestDatabaseUrl = diff --git a/cloud/apps/push/src/durable-push-store.test.ts b/cloud/apps/push/src/delivery/durable-push-store.test.ts similarity index 99% rename from cloud/apps/push/src/durable-push-store.test.ts rename to cloud/apps/push/src/delivery/durable-push-store.test.ts index d8387793d45..9be0e32ec8f 100644 --- a/cloud/apps/push/src/durable-push-store.test.ts +++ b/cloud/apps/push/src/delivery/durable-push-store.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it } from 'vitest' -import { openPushDatabase } from './push-database.js' +import { openPushDatabase } from '../database/push-database.js' import { DurablePushStore, DELIVERY_LEASE_MS } from './durable-push-store.js' import { cleanupDurablePushFixtures, @@ -89,8 +89,6 @@ describe('durable push acceptance', () => { await store.finish(fresh!) }) - - it('rolls quota and payload back together if persistence fails', async () => { const { db, store } = await fixture() await db.query('ALTER TABLE push_delivery_batches RENAME TO push_delivery_batches_unavailable') diff --git a/cloud/apps/push/src/durable-push-store.ts b/cloud/apps/push/src/delivery/durable-push-store.ts similarity index 97% rename from cloud/apps/push/src/durable-push-store.ts rename to cloud/apps/push/src/delivery/durable-push-store.ts index 1bcdda0bdc7..9d9151e584a 100644 --- a/cloud/apps/push/src/durable-push-store.ts +++ b/cloud/apps/push/src/delivery/durable-push-store.ts @@ -1,10 +1,14 @@ import { isDismissedAlert, reconcileQueuedDismissal } from './push-queued-dismissal.js' import { parsePushDeliveryPayload } from './push-delivery-payload.js' import { createHash, randomUUID } from 'node:crypto' -import { PUSH_LIMITS, type PushNotification } from '@orca-cloud/push-contract' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' +import type { PushNotification } from '@orca-cloud/push-contract/send-messages' import { WORKER_DRAINS } from './push-worker-concurrency.js' -import type { PushDatabase, SqlRow } from './push-database.js' -import { limitPushDatabaseConcurrency } from './push-background-database.js' +import type { + CloudDatabase as PushDatabase, + SqlRow +} from '@orca-cloud/cloud-database/cloud-database' +import { limitPushDatabaseConcurrency } from '../database/push-background-database.js' const RETENTION_MS = 24 * 60 * 60_000 // accept() caps expires_at at due_at + TTL, so older due_at is expired: scans skip an unpruned backlog. diff --git a/cloud/apps/push/src/durable-push-worker.test.ts b/cloud/apps/push/src/delivery/durable-push-worker.test.ts similarity index 66% rename from cloud/apps/push/src/durable-push-worker.test.ts rename to cloud/apps/push/src/delivery/durable-push-worker.test.ts index 5eb84f35110..78303de18e6 100644 --- a/cloud/apps/push/src/durable-push-worker.test.ts +++ b/cloud/apps/push/src/delivery/durable-push-worker.test.ts @@ -1,13 +1,12 @@ - import { afterEach, expect, it, vi } from 'vitest' -import type { PushNotification } from '@orca-cloud/push-contract' +import type { PushNotification } from '@orca-cloud/push-contract/send-messages' import { DurablePushStore } from './durable-push-store.js' import { DurablePushWorker } from './durable-push-worker.js' import { PushDispatcher } from './push-dispatcher.js' -import { PushDeviceRegistryStore } from './device-registry-store.js' -import { openInMemoryPushDatabase } from './push-database.js' +import { PushDeviceRegistryStore } from '../devices/device-registry-store.js' +import { openInMemoryPushDatabase } from '../database/push-database.js' import type { PushDelivery } from './push-delivery-message.js' -import type { PushProviderOutcome } from './push-provider-outcome.js' +import type { PushProviderOutcome } from '../providers/push-provider-outcome.js' import { pauseAfter, within } from './durable-push-store.test-fixture.js' @@ -69,52 +68,6 @@ async function fixture() { } } -it('preserves provider retry delay when waking a restarted worker', async () => { - const h = await fixture() - vi.useFakeTimers() - h.send.mockResolvedValueOnce({ - status: 'error', - reason: 'busy', - retryable: true, - retryAfterMs: 10000 - }) - await h.accept(note(1)) - await h.worker.runDue() - expect(h.send).toHaveBeenCalledOnce() - await h.worker.stop() - const restarted = new DurablePushWorker(h.store, h.dispatcher, { now: h.now, onRetry: h.onRetry }) - restarted.start() - h.advance(9999) - restarted.wake() - await restarted.runDue() - expect(h.send).toHaveBeenCalledOnce() - h.advance(1) - restarted.wake() - await restarted.runDue() - expect(h.send).toHaveBeenCalledTimes(2) - expect(h.send.mock.calls.map(([delivery]) => delivery.expiresAt)).toEqual([1_300_000, 1_300_000]) - expect(h.onRetry).toHaveBeenCalledOnce() - expect(await h.store.pendingCount(h.registrationId)).toBe(0) - await restarted.stop() -}) - -it('expires instead of shortening a provider delay beyond the delivery lifetime', async () => { - const h = await fixture() - h.send.mockResolvedValue({ - status: 'error', - reason: 'busy', - retryable: true, - retryAfterMs: 600000 - }) - await h.accept(note(1)) - await h.worker.runDue() - h.advance(600000) - await h.worker.runDue() - expect(h.send).toHaveBeenCalledOnce() - expect(await h.store.pendingCount(h.registrationId)).toBe(0) - expect(h.onRetry).not.toHaveBeenCalled() -}) - it('rechecks the device before a persisted retry and does not send after unregistration', async () => { const h = await fixture() h.send.mockResolvedValue({ status: 'error', reason: 'timeout', retryable: true }) @@ -127,46 +80,6 @@ it('rechecks the device before a persisted retry and does not send after unregis expect(await h.store.pendingCount(h.registrationId)).toBe(0) }) -it('joins active work on shutdown and leaves unclaimed work for the next instance', async () => { - const h = await fixture() - vi.useFakeTimers() - let finish!: (outcome: PushProviderOutcome) => void - let started!: () => void - const entered = new Promise((resolve) => { - started = resolve - }) - h.send.mockImplementationOnce(() => { - started() - return new Promise((resolve) => { - finish = resolve - }) - }) - await h.accept(note(1)) - h.worker.start() - h.worker.wake() - const pending = h.worker.runDue() - await entered - await h.accept(note(2)) - h.worker.wake() - let stopped = false - const stopping = h.worker.stop().then(() => { - stopped = true - }) - await Promise.resolve() - expect(stopped).toBe(false) - finish({ status: 'sent' }) - await Promise.all([pending, stopping]) - expect(stopped).toBe(true) - expect(h.send).toHaveBeenCalledOnce() - expect(vi.getTimerCount()).toBe(0) - const resumed = new DurablePushWorker(h.store, h.dispatcher, { now: h.now }) - resumed.start() - await vi.advanceTimersByTimeAsync(1000) - await resumed.runDue() - expect(h.send).toHaveBeenCalledTimes(2) - await resumed.stop() -}) - it('stops queued claim admissions without leasing the remaining device', async () => { const h = await fixture() const other = await h.devices.upsert({ diff --git a/cloud/apps/push/src/durable-push-worker.ts b/cloud/apps/push/src/delivery/durable-push-worker.ts similarity index 100% rename from cloud/apps/push/src/durable-push-worker.ts rename to cloud/apps/push/src/delivery/durable-push-worker.ts diff --git a/cloud/apps/push/src/push-delivery-message.ts b/cloud/apps/push/src/delivery/push-delivery-message.ts similarity index 96% rename from cloud/apps/push/src/push-delivery-message.ts rename to cloud/apps/push/src/delivery/push-delivery-message.ts index 1d37f621573..200d4c78908 100644 --- a/cloud/apps/push/src/push-delivery-message.ts +++ b/cloud/apps/push/src/delivery/push-delivery-message.ts @@ -1,5 +1,5 @@ import { createHash } from 'node:crypto' -import { type PushNotification } from '@orca-cloud/push-contract' +import type { PushNotification } from '@orca-cloud/push-contract/send-messages' export type PushOrcaData = { kind?: 'alert' | 'dismiss' diff --git a/cloud/apps/push/src/push-delivery-payload.ts b/cloud/apps/push/src/delivery/push-delivery-payload.ts similarity index 95% rename from cloud/apps/push/src/push-delivery-payload.ts rename to cloud/apps/push/src/delivery/push-delivery-payload.ts index 825286125b7..3ce1541facf 100644 --- a/cloud/apps/push/src/push-delivery-payload.ts +++ b/cloud/apps/push/src/delivery/push-delivery-payload.ts @@ -1,4 +1,4 @@ -import type { PushNotification } from '@orca-cloud/push-contract' +import type { PushNotification } from '@orca-cloud/push-contract/send-messages' export function parsePushDeliveryPayload(payload: string): PushNotification { const value: unknown = JSON.parse(payload) diff --git a/cloud/apps/push/src/push-dispatcher.ts b/cloud/apps/push/src/delivery/push-dispatcher.ts similarity index 81% rename from cloud/apps/push/src/push-dispatcher.ts rename to cloud/apps/push/src/delivery/push-dispatcher.ts index ca9006bc435..68ae5ae5b1d 100644 --- a/cloud/apps/push/src/push-dispatcher.ts +++ b/cloud/apps/push/src/delivery/push-dispatcher.ts @@ -1,9 +1,9 @@ -import type { ApnsClient } from './apns-client.js' -import type { PushDeviceRegistryStore } from './device-registry-store.js' -import type { FcmClient } from './fcm-client.js' -import { fingerprintLogPrefix } from './host-fingerprint.js' +import type { ApnsClient } from '../providers/apns/apns-client.js' +import type { PushDeviceRegistryStore } from '../devices/device-registry-store.js' +import type { FcmClient } from '../providers/fcm/fcm-client.js' +import { fingerprintLogPrefix } from '../host-auth/host-fingerprint.js' import type { PushDelivery } from './push-delivery-message.js' -import type { PushProviderOutcome } from './push-provider-outcome.js' +import type { PushProviderOutcome } from '../providers/push-provider-outcome.js' export type PushDispatcherOptions = { devices: PushDeviceRegistryStore diff --git a/cloud/apps/push/src/push-queued-dismissal.ts b/cloud/apps/push/src/delivery/push-queued-dismissal.ts similarity index 96% rename from cloud/apps/push/src/push-queued-dismissal.ts rename to cloud/apps/push/src/delivery/push-queued-dismissal.ts index e1b48bcac53..6056974b0e4 100644 --- a/cloud/apps/push/src/push-queued-dismissal.ts +++ b/cloud/apps/push/src/delivery/push-queued-dismissal.ts @@ -1,5 +1,5 @@ -import type { PushNotification } from '@orca-cloud/push-contract' -import type { PushDatabase } from './push-database.js' +import type { PushNotification } from '@orca-cloud/push-contract/send-messages' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' import { parsePushDeliveryPayload } from './push-delivery-payload.js' export async function reconcileQueuedDismissal( diff --git a/cloud/apps/push/src/push-worker-concurrency.ts b/cloud/apps/push/src/delivery/push-worker-concurrency.ts similarity index 100% rename from cloud/apps/push/src/push-worker-concurrency.ts rename to cloud/apps/push/src/delivery/push-worker-concurrency.ts diff --git a/cloud/apps/push/src/device-registration-delete-race.test.ts b/cloud/apps/push/src/device-registration-delete-race.test.ts deleted file mode 100644 index 2e2bdbdc7fa..00000000000 --- a/cloud/apps/push/src/device-registration-delete-race.test.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { expect, it, vi } from 'vitest' -import { openPushDatabase, type PushDatabase } from './push-database.js' -import { PushDeviceRegistryStore } from './device-registry-store.js' - -const databaseUrl = process.env.ORCA_PUSH_TEST_DATABASE_URL -it.skipIf(!databaseUrl)( - 'serializes deletion with a registration that has already read its row', - async () => { - if (!process.env.CI && new URL(databaseUrl!).port !== '55440') - throw new Error('isolated_postgres_port_required') - const database = await openPushDatabase({ - databaseUrl, - dataDir: '', - poolMax: 4, - applicationName: 'push-delete-race' - }) - let release!: () => void - const paused = new Promise((resolve) => { - release = resolve - }) - let read = false - let pause = false - const wrapped: PushDatabase = { - dialect: database.dialect, - query: database.query.bind(database), - close: database.close.bind(database), - lockQuotaScope: database.lockQuotaScope.bind(database), - tryLockScope: database.tryLockScope.bind(database), - tryLockSharedScope: database.tryLockSharedScope.bind(database), - transaction: (run) => - database.transaction((tx) => - run({ - dialect: tx.dialect, - close: tx.close.bind(tx), - transaction: tx.transaction.bind(tx), - lockQuotaScope: tx.lockQuotaScope.bind(tx), - tryLockScope: tx.tryLockScope.bind(tx), - tryLockSharedScope: tx.tryLockSharedScope.bind(tx), - query: async (sql, params) => { - const rows = await tx.query(sql, params) - if (pause && sql.startsWith('SELECT registration_id FROM push_devices')) { - read = true - await paused - } - return rows - } - }) - ) - } - const devices = new PushDeviceRegistryStore(wrapped) - const input = { - hostFingerprint: 'delete-race-host', - deviceId: 'phone', - platform: 'android' as const, - token: 'synthetic' - } - let registration: Promise | undefined - let deletion: Promise | undefined - try { - await database.query('DELETE FROM push_devices WHERE host_fingerprint = ?', [ - input.hostFingerprint - ]) - const first = await devices.upsert(input) - if (!first.ok) throw new Error('registration refused') - pause = true - registration = devices.upsert(input) - await vi.waitFor(() => expect(read).toBe(true)) - let deleted = false - deletion = devices.deleteOwned(input.hostFingerprint, first.registrationId).then((value) => { - deleted = true - return value - }) - await vi.waitFor(async () => { - const rows = await database.query( - "SELECT 1 FROM pg_stat_activity WHERE application_name = 'push-delete-race' AND wait_event_type = 'Lock'" - ) - expect(deleted || rows.length > 0).toBe(true) - }) - expect(deleted).toBe(false) - release() - expect(await registration).toEqual(first) - expect(await deletion).toBe(true) - } finally { - release() - await Promise.allSettled([registration, deletion]) - await database.query('DELETE FROM push_devices WHERE host_fingerprint = ?', [ - input.hostFingerprint - ]) - await database.close() - } - } -) diff --git a/cloud/apps/push/src/device-registry-store.test.ts b/cloud/apps/push/src/devices/device-registry-store.test.ts similarity index 79% rename from cloud/apps/push/src/device-registry-store.test.ts rename to cloud/apps/push/src/devices/device-registry-store.test.ts index 97f3e47acc5..7639ed974b9 100644 --- a/cloud/apps/push/src/device-registry-store.test.ts +++ b/cloud/apps/push/src/devices/device-registry-store.test.ts @@ -1,7 +1,8 @@ -import { PUSH_LIMITS } from '@orca-cloud/push-contract' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { PushDeviceRegistryStore, type PushDeviceUpsert } from './device-registry-store.js' -import { openInMemoryPushDatabase, type PushDatabase } from './push-database.js' +import { openInMemoryPushDatabase } from '../database/push-database.js' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' const OWNER = 'abcdefghijklmnop' const OTHER = 'ponmlkjihgfedcba' @@ -117,28 +118,6 @@ describe('push device registry store', () => { expect(await devices.findOwned(OWNER, [])).toEqual(new Map()) }) - it('refuses a new device once the host reaches its registration cap', async () => { - for (let index = 0; index < PUSH_LIMITS.maxDevicesPerHost; index++) { - await upsertOk(androidDevice(`device-${index}`)) - } - expect(await devices.upsert(androidDevice('one-too-many'))).toEqual({ - ok: false, - reason: 'too_many_devices' - }) - expect(await devices.list(OWNER)).toHaveLength(PUSH_LIMITS.maxDevicesPerHost) - }) - - it('still lets a capped host re-register a device it already owns', async () => { - for (let index = 0; index < PUSH_LIMITS.maxDevicesPerHost; index++) { - await upsertOk(androidDevice(`device-${index}`)) - } - const rotated = await devices.upsert({ ...androidDevice('device-0'), token: 'rotated-token' }) - expect(rotated.ok).toBe(true) - expect(await devices.list(OWNER)).toHaveLength(PUSH_LIMITS.maxDevicesPerHost) - }) - - - it('counts the cap per host, not across the whole table', async () => { for (let index = 0; index < PUSH_LIMITS.maxDevicesPerHost; index++) { await upsertOk(androidDevice(`device-${index}`)) diff --git a/cloud/apps/push/src/device-registry-store.ts b/cloud/apps/push/src/devices/device-registry-store.ts similarity index 95% rename from cloud/apps/push/src/device-registry-store.ts rename to cloud/apps/push/src/devices/device-registry-store.ts index a4061189e35..1db88954d62 100644 --- a/cloud/apps/push/src/device-registry-store.ts +++ b/cloud/apps/push/src/devices/device-registry-store.ts @@ -1,11 +1,14 @@ import { randomUUID } from 'node:crypto' -import { - PUSH_LIMITS, - type ApnsEnvironment, - type PushDeviceSummary, - type PushPlatform -} from '@orca-cloud/push-contract' -import type { PushDatabase, SqlRow } from './push-database.js' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' +import type { + ApnsEnvironment, + PushDeviceSummary, + PushPlatform +} from '@orca-cloud/push-contract/device-registration-messages' +import type { + CloudDatabase as PushDatabase, + SqlRow +} from '@orca-cloud/cloud-database/cloud-database' const DEVICE_CAP_LOCK_PREFIX = 'orca-push-device-cap:' diff --git a/cloud/apps/push/src/durable-push-claim-mixed-revision.test.ts b/cloud/apps/push/src/durable-push-claim-mixed-revision.test.ts deleted file mode 100644 index 869160c3591..00000000000 --- a/cloud/apps/push/src/durable-push-claim-mixed-revision.test.ts +++ /dev/null @@ -1,103 +0,0 @@ -import { afterEach, expect, it, vi } from 'vitest' -import type { PushDatabase } from './push-database.js' -import { DELIVERY_LEASE_MS, DurablePushStore } from './durable-push-store.js' -import { - cleanupDurablePushFixtures, - durablePushTestDatabaseUrl, - fixture, - notification, - pauseAfter, - within -} from './durable-push-store.test-fixture.js' - -afterEach(cleanupDurablePushFixtures) - -const LEASE_SQL = 'UPDATE push_delivery_batches SET lease_token' - -// The previous revision's claim statements, in order, with a hook between its candidate scan and re-read. -function previousRevisionClaim( - db: PushDatabase, - clock: () => number, - afterCandidate: () => Promise -): Promise { - return db.transaction(async (tx) => { - await tx.lockQuotaScope('push-worker-claim') - const now = clock() - const [candidate] = await tx.query( - `SELECT * FROM push_delivery_batches WHERE state = 'pending' AND lease_until <= ? AND expires_at > ? AND due_at <= ? AND NOT EXISTS (SELECT 1 FROM push_delivery_batches busy WHERE busy.registration_id = push_delivery_batches.registration_id AND busy.lease_until > ?) - ORDER BY due_at, created_at, batch_id LIMIT 1`, - [now, now, now, now] - ) - await afterCandidate() - if (!candidate) return null - await tx.lockQuotaScope(`push-events:${String(candidate.host_fingerprint)}`) - const [row] = await tx.query('SELECT * FROM push_delivery_batches WHERE batch_id = ?', [ - candidate.batch_id - ]) - if (!row || row.state !== 'pending' || Number(row.expires_at) <= now) return null - await tx.query( - 'UPDATE push_delivery_batches SET lease_token = ?, lease_until = ?, attempts = attempts + 1 WHERE batch_id = ?', - ['previous', now + DELIVERY_LEASE_MS, row.batch_id] - ) - return 'previous' - }) -} - -it.skipIf(!durablePushTestDatabaseUrl)( - 'keeps a previous-revision claim from re-leasing a delivery this revision leased', - async () => { - const { db, store, clock } = await fixture({ ownDatabase: true }) - await store.accept('host', 'phone', notification(1)) - const paused = pauseAfter(db, LEASE_SQL) - const current = new DurablePushStore(paused.wrapped, clock).claim() - await paused.atCandidate - let committed!: () => void - const currentCommitted = new Promise((resolve) => (committed = resolve)) - const previous = previousRevisionClaim(db, clock, () => currentCommitted) - try { - await vi.waitFor(async () => { - const waiting = await db.query( - "SELECT 1 FROM pg_stat_activity WHERE datname = current_database() AND wait_event_type = 'Lock' AND wait_event = 'advisory'" - ) - expect(waiting).toHaveLength(1) - }) - } finally { - paused.release() - try { - await current - } finally { - committed() - await previous - } - } - const leased = await current - expect(await previous).toBeNull() - const [row] = await db.query('SELECT lease_token FROM push_delivery_batches') - expect(leased?.lease).toBeTruthy() - expect(row?.lease_token).toBe(leased?.lease) - } -) - -it.skipIf(!durablePushTestDatabaseUrl)( - 'yields to a previous-revision claim that already holds the claim lock', - async () => { - const { db, store, clock } = await fixture({ ownDatabase: true }) - await store.accept('host', 'phone', notification(1)) - let scanned!: () => void - const atCandidate = new Promise((resolve) => (scanned = resolve)) - let release!: () => void - const released = new Promise((resolve) => (release = resolve)) - const previous = previousRevisionClaim(db, clock, async () => { - scanned() - await released - }) - await atCandidate - try { - expect(await within(store.claim(), 2_000)).toBeNull() - } finally { - release() - } - expect(await previous).toBe('previous') - expect(await store.claim()).toBeNull() - } -) diff --git a/cloud/apps/push/src/canonical-base64.ts b/cloud/apps/push/src/host-auth/canonical-base64.ts similarity index 100% rename from cloud/apps/push/src/canonical-base64.ts rename to cloud/apps/push/src/host-auth/canonical-base64.ts diff --git a/cloud/apps/push/src/host-challenge-answering.test-fixture.ts b/cloud/apps/push/src/host-auth/host-challenge-answering.test-fixture.ts similarity index 90% rename from cloud/apps/push/src/host-challenge-answering.test-fixture.ts rename to cloud/apps/push/src/host-auth/host-challenge-answering.test-fixture.ts index ac5320cd1ab..e84a49bc632 100644 --- a/cloud/apps/push/src/host-challenge-answering.test-fixture.ts +++ b/cloud/apps/push/src/host-auth/host-challenge-answering.test-fixture.ts @@ -2,9 +2,9 @@ import { createHmac, timingSafeEqual } from 'node:crypto' import { PUSH_HOST_CHALLENGE_PLAINTEXT_DOMAIN, PUSH_HOST_PROOF_TRANSCRIPT_DOMAIN, - PUSH_HOST_PROOF_TRANSCRIPT_FIELD_COUNT, - PUSH_LIMITS -} from '@orca-cloud/push-contract' + PUSH_HOST_PROOF_TRANSCRIPT_FIELD_COUNT +} from '@orca-cloud/push-contract/push-host-proof-transcript' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' import nacl from 'tweetnacl' import { decodeCanonicalBase64 } from './canonical-base64.js' import { deriveHostFingerprint } from './host-fingerprint.js' @@ -63,7 +63,10 @@ function parseTranscript(transcript: Uint8Array): Map | null function readUint64(value: Uint8Array | undefined): number | null { if (!value || value.byteLength !== 8) return null - const parsed = new DataView(value.buffer, value.byteOffset, value.byteLength).getBigUint64(0, false) + const parsed = new DataView(value.buffer, value.byteOffset, value.byteLength).getBigUint64( + 0, + false + ) return parsed <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(parsed) : null } @@ -98,14 +101,17 @@ function validateTranscript( ], ['not-expired', now - PUSH_LIMITS.clockSkewToleranceMs <= challenge.expiresAt], ['issuedAt-before-expiry', issuedAt === null || issuedAt <= challenge.expiresAt], - [ - 'window', - issuedAt === null || challenge.expiresAt - issuedAt <= PUSH_LIMITS.challengeTtlMs - ], + ['window', issuedAt === null || challenge.expiresAt - issuedAt <= PUSH_LIMITS.challengeTtlMs], ['expiry-consistent', expiresAt === challenge.expiresAt], - ['protocol', equal(fields.get('protocol'), textEncoder.encode(PUSH_HOST_PROOF_TRANSCRIPT_DOMAIN))], + [ + 'protocol', + equal(fields.get('protocol'), textEncoder.encode(PUSH_HOST_PROOF_TRANSCRIPT_DOMAIN)) + ], ['version', equal(fields.get('version'), new Uint8Array([1]))], - ['gatewayOrigin', equal(fields.get('gatewayOrigin'), textEncoder.encode(context.gatewayOrigin))], + [ + 'gatewayOrigin', + equal(fields.get('gatewayOrigin'), textEncoder.encode(context.gatewayOrigin)) + ], ['gatewayEphemeralPublicKey', equal(fields.get('gatewayEphemeralPublicKey'), gatewayKey)], ['challengeNonce', equal(fields.get('challengeNonce'), nonce)], ['challengeId', equal(fields.get('challengeId'), textEncoder.encode(challenge.challengeId))], @@ -125,7 +131,8 @@ export function answerPushHostChallenge( const gatewayKey = decodeCanonicalBase64(challenge.gatewayEphemeralPublicKeyB64, 32) const nonce = decodeCanonicalBase64(challenge.nonceB64, 24) const ciphertext = Buffer.from(challenge.ciphertextB64, 'base64') - if (!gatewayKey || !nonce || ciphertext.toString('base64') !== challenge.ciphertextB64) return null + if (!gatewayKey || !nonce || ciphertext.toString('base64') !== challenge.ciphertextB64) + return null const plaintext = nacl.box.open(ciphertext, nonce, gatewayKey, context.keypair.secretKey) if (!plaintext) { context.onInvalid?.('challenge-box-open') diff --git a/cloud/apps/push/src/host-challenge-store.test.ts b/cloud/apps/push/src/host-auth/host-challenge-store.test.ts similarity index 89% rename from cloud/apps/push/src/host-challenge-store.test.ts rename to cloud/apps/push/src/host-auth/host-challenge-store.test.ts index f73a4f53ea8..a5b4e5574ea 100644 --- a/cloud/apps/push/src/host-challenge-store.test.ts +++ b/cloud/apps/push/src/host-auth/host-challenge-store.test.ts @@ -1,4 +1,4 @@ -import { PUSH_LIMITS } from '@orca-cloud/push-contract' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { answerPushHostChallenge, @@ -7,7 +7,8 @@ import { } from './host-challenge-answering.test-fixture.js' import { PushHostChallengeStore } from './host-challenge-store.js' -import { openInMemoryPushDatabase, type PushDatabase } from './push-database.js' +import { openInMemoryPushDatabase } from '../database/push-database.js' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' const GATEWAY_ORIGIN = 'https://push.onorca.dev' @@ -26,12 +27,6 @@ describe('push host challenge store', () => { await database.close() }) - - - - - - it('rejects a challenge the moment its own ttl elapses', async () => { const host = createPushHostKeypair(4) const challenge = await store.issue(hostPublicKeyB64(host)) @@ -47,10 +42,6 @@ describe('push host challenge store', () => { }) }) - - - - it('refuses a wrong host: the box will not open and a foreign proof will not match', async () => { const owner = createPushHostKeypair(6) const intruder = createPushHostKeypair(7) @@ -74,6 +65,4 @@ describe('push host challenge store', () => { reason: 'proof_mismatch' }) }) - - }) diff --git a/cloud/apps/push/src/host-challenge-store.ts b/cloud/apps/push/src/host-auth/host-challenge-store.ts similarity index 95% rename from cloud/apps/push/src/host-challenge-store.ts rename to cloud/apps/push/src/host-auth/host-challenge-store.ts index fd12af85983..5cfb68d2f12 100644 --- a/cloud/apps/push/src/host-challenge-store.ts +++ b/cloud/apps/push/src/host-auth/host-challenge-store.ts @@ -2,13 +2,13 @@ import { createHash, createHmac, randomBytes, randomUUID, timingSafeEqual } from import { buildPushHostChallengePlaintext, buildPushHostProofMacInput, - buildPushHostProofTranscript, - PUSH_LIMITS -} from '@orca-cloud/push-contract' + buildPushHostProofTranscript +} from '@orca-cloud/push-contract/push-host-proof-transcript' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' import nacl from 'tweetnacl' import { decodeCanonicalBase64 } from './canonical-base64.js' import { deriveHostFingerprint } from './host-fingerprint.js' -import type { PushDatabase } from './push-database.js' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' export type IssuedPushChallenge = { challengeId: string diff --git a/cloud/apps/push/src/host-fingerprint.ts b/cloud/apps/push/src/host-auth/host-fingerprint.ts similarity index 96% rename from cloud/apps/push/src/host-fingerprint.ts rename to cloud/apps/push/src/host-auth/host-fingerprint.ts index 955b1ac8ecb..b0cee6ca15b 100644 --- a/cloud/apps/push/src/host-fingerprint.ts +++ b/cloud/apps/push/src/host-auth/host-fingerprint.ts @@ -1,5 +1,5 @@ import { createHash } from 'node:crypto' -import { PUSH_HOST_FINGERPRINT_LENGTH } from '@orca-cloud/push-contract' +import { PUSH_HOST_FINGERPRINT_LENGTH } from '@orca-cloud/push-contract/push-limits' // Identical derivation to deriveRelayHostId on the desktop, so a host and a // phone reach the same fingerprint from the same X25519 public key. diff --git a/cloud/apps/push/src/host-proof-concurrency.test.ts b/cloud/apps/push/src/host-auth/host-proof-concurrency.test.ts similarity index 95% rename from cloud/apps/push/src/host-proof-concurrency.test.ts rename to cloud/apps/push/src/host-auth/host-proof-concurrency.test.ts index 031ca61c7fd..8e64ab10165 100644 --- a/cloud/apps/push/src/host-proof-concurrency.test.ts +++ b/cloud/apps/push/src/host-auth/host-proof-concurrency.test.ts @@ -1,5 +1,5 @@ import { expect, it } from 'vitest' -import { openInMemoryPushDatabase, openPushDatabase } from './push-database.js' +import { openInMemoryPushDatabase, openPushDatabase } from '../database/push-database.js' import { PushHostChallengeStore } from './host-challenge-store.js' import { answerPushHostChallenge, diff --git a/cloud/apps/push/src/host-session-store.test.ts b/cloud/apps/push/src/host-auth/host-session-store.test.ts similarity index 72% rename from cloud/apps/push/src/host-session-store.test.ts rename to cloud/apps/push/src/host-auth/host-session-store.test.ts index ad90407164d..4389d310f4a 100644 --- a/cloud/apps/push/src/host-session-store.test.ts +++ b/cloud/apps/push/src/host-auth/host-session-store.test.ts @@ -1,7 +1,8 @@ -import { PUSH_LIMITS } from '@orca-cloud/push-contract' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { PushHostSessionStore } from './host-session-store.js' -import { openInMemoryPushDatabase, type PushDatabase } from './push-database.js' +import { openInMemoryPushDatabase } from '../database/push-database.js' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' const HOST = 'abcdefghijklmnop' @@ -20,18 +21,6 @@ describe('push host session store', () => { await database.close() }) - it('mints a 24 hour session and stores only its hash', async () => { - const session = await sessions.create(HOST) - expect(session.expiresAt).toBe(clock + PUSH_LIMITS.sessionTtlMs) - expect(Buffer.from(session.sessionToken, 'base64url').byteLength).toBe(32) - const [row] = await database.query('SELECT token_hash FROM push_sessions') - expect(String(row?.token_hash)).not.toBe(session.sessionToken) - await expect(sessions.resolve(session.sessionToken)).resolves.toMatchObject({ - ok: true, - hostFingerprint: HOST - }) - }) - it('reports expiry separately from an unknown token', async () => { const session = await sessions.create(HOST) clock += PUSH_LIMITS.sessionTtlMs + 1 @@ -45,8 +34,6 @@ describe('push host session store', () => { }) }) - - it('keeps one live session per host and prunes it once expired', async () => { const first = await sessions.create(HOST) const second = await sessions.create(HOST) diff --git a/cloud/apps/push/src/host-session-store.ts b/cloud/apps/push/src/host-auth/host-session-store.ts similarity index 93% rename from cloud/apps/push/src/host-session-store.ts rename to cloud/apps/push/src/host-auth/host-session-store.ts index 5ee1f2aa6a0..6841bb4c633 100644 --- a/cloud/apps/push/src/host-session-store.ts +++ b/cloud/apps/push/src/host-auth/host-session-store.ts @@ -1,6 +1,6 @@ import { createHash, randomBytes } from 'node:crypto' -import { PUSH_LIMITS } from '@orca-cloud/push-contract' -import type { PushDatabase } from './push-database.js' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' export type IssuedPushSession = { sessionToken: string diff --git a/cloud/apps/push/src/push-session-concurrency.test.ts b/cloud/apps/push/src/host-auth/push-session-concurrency.test.ts similarity index 60% rename from cloud/apps/push/src/push-session-concurrency.test.ts rename to cloud/apps/push/src/host-auth/push-session-concurrency.test.ts index 4c6944255f8..221a5c91ab4 100644 --- a/cloud/apps/push/src/push-session-concurrency.test.ts +++ b/cloud/apps/push/src/host-auth/push-session-concurrency.test.ts @@ -1,7 +1,8 @@ import { randomUUID } from 'node:crypto' import { tmpdir } from 'node:os' import { afterEach, describe, expect, it } from 'vitest' -import { openInMemoryPushDatabase, openPushDatabase, type PushDatabase } from './push-database.js' +import { openInMemoryPushDatabase, openPushDatabase } from '../database/push-database.js' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' import { PushHostSessionStore } from './host-session-store.js' const databases: PushDatabase[] = [] afterEach(async () => { @@ -31,16 +32,6 @@ it('serializes sessions on SQLite', async () => { await concurrentSessions(await openInMemoryPushDatabase()) }) -it('enforces one session per host directly in the schema', async () => { - const db = await openInMemoryPushDatabase() - databases.push(db) - await db.query('INSERT INTO push_sessions VALUES (?, ?, ?, ?)', ['first', 'host', 100, 1]) - await expect( - db.query('INSERT INTO push_sessions VALUES (?, ?, ?, ?)', ['second', 'host', 100, 2]) - ).rejects.toThrow() - expect(await db.query('SELECT token_hash FROM push_sessions')).toEqual([{ token_hash: 'first' }]) -}) - describe.skipIf(!process.env.ORCA_PUSH_TEST_DATABASE_URL)('PostgreSQL push sessions', () => { it('leaves exactly one live token after concurrent creates', async () => { await concurrentSessions( @@ -50,16 +41,4 @@ describe.skipIf(!process.env.ORCA_PUSH_TEST_DATABASE_URL)('PostgreSQL push sessi }) ) }) - it('allows concurrent schema startup', async () => { - const opened = await Promise.all( - Array.from({ length: 4 }, () => - openPushDatabase({ - databaseUrl: process.env.ORCA_PUSH_TEST_DATABASE_URL!, - dataDir: tmpdir() - }) - ) - ) - databases.push(...opened) - for (const db of opened) expect(await db.query('SELECT 1 AS ok')).toEqual([{ ok: 1 }]) - }) }) diff --git a/cloud/apps/push/src/client-ip-rate-limit.test.ts b/cloud/apps/push/src/http/client-ip-rate-limit.test.ts similarity index 97% rename from cloud/apps/push/src/client-ip-rate-limit.test.ts rename to cloud/apps/push/src/http/client-ip-rate-limit.test.ts index 11baec26394..041b1577f08 100644 --- a/cloud/apps/push/src/client-ip-rate-limit.test.ts +++ b/cloud/apps/push/src/http/client-ip-rate-limit.test.ts @@ -1,4 +1,4 @@ -import { PUSH_LIMITS } from '@orca-cloud/push-contract' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' import { Hono } from 'hono' import { describe, expect, it } from 'vitest' import { ClientIpRateLimiter, clientIpRateLimit } from './client-ip-rate-limit.js' @@ -14,18 +14,6 @@ function limiterApp(limiter: ClientIpRateLimiter, trustedProxyHops = 0): Hono { } describe('client ip rate limiter', () => { - - - - - - - - - - - - it('buckets on the last forwarded hop, the only one the platform appended', async () => { const app = limiterApp(new ClientIpRateLimiter({ now: () => 1_000 })) for (let index = 0; index < CAPACITY; index++) { diff --git a/cloud/apps/push/src/client-ip-rate-limit.ts b/cloud/apps/push/src/http/client-ip-rate-limit.ts similarity index 97% rename from cloud/apps/push/src/client-ip-rate-limit.ts rename to cloud/apps/push/src/http/client-ip-rate-limit.ts index 6ac3e4d3c15..d13794b7095 100644 --- a/cloud/apps/push/src/client-ip-rate-limit.ts +++ b/cloud/apps/push/src/http/client-ip-rate-limit.ts @@ -1,4 +1,4 @@ -import { PUSH_LIMITS } from '@orca-cloud/push-contract' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' import type { Context, MiddlewareHandler } from 'hono' const REFILL_WINDOW_MS = 60_000 diff --git a/cloud/apps/push/src/push-auth-admission.ts b/cloud/apps/push/src/http/push-auth-admission.ts similarity index 100% rename from cloud/apps/push/src/push-auth-admission.ts rename to cloud/apps/push/src/http/push-auth-admission.ts diff --git a/cloud/apps/push/src/push-authenticated-ip-limit.test.ts b/cloud/apps/push/src/http/push-authenticated-ip-limit.test.ts similarity index 91% rename from cloud/apps/push/src/push-authenticated-ip-limit.test.ts rename to cloud/apps/push/src/http/push-authenticated-ip-limit.test.ts index a626b80060a..01b68073dad 100644 --- a/cloud/apps/push/src/push-authenticated-ip-limit.test.ts +++ b/cloud/apps/push/src/http/push-authenticated-ip-limit.test.ts @@ -1,6 +1,6 @@ -import { PUSH_LIMITS } from '@orca-cloud/push-contract' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' import { expect, it } from 'vitest' -import { createPushHostKeypair } from './host-challenge-answering.test-fixture.js' +import { createPushHostKeypair } from '../host-auth/host-challenge-answering.test-fixture.js' import { createPushServerHarness, FCM_TOKEN } from './push-server-harness.test-fixture.js' it('bounds valid hosts together across routes without letting key rotation reset the IP budget', async () => { diff --git a/cloud/apps/push/src/push-readiness.ts b/cloud/apps/push/src/http/push-readiness.ts similarity index 90% rename from cloud/apps/push/src/push-readiness.ts rename to cloud/apps/push/src/http/push-readiness.ts index c071a0750e4..c1751ef72f6 100644 --- a/cloud/apps/push/src/push-readiness.ts +++ b/cloud/apps/push/src/http/push-readiness.ts @@ -1,4 +1,4 @@ -import type { PushDatabase } from './push-database.js' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' export type PushReadinessOptions = { cacheMs?: number diff --git a/cloud/apps/push/src/push-request-drain.ts b/cloud/apps/push/src/http/push-request-drain.ts similarity index 100% rename from cloud/apps/push/src/push-request-drain.ts rename to cloud/apps/push/src/http/push-request-drain.ts diff --git a/cloud/apps/push/src/push-send-idempotency.test.ts b/cloud/apps/push/src/http/push-send-idempotency.test.ts similarity index 56% rename from cloud/apps/push/src/push-send-idempotency.test.ts rename to cloud/apps/push/src/http/push-send-idempotency.test.ts index 1a687a35449..b061eef3040 100644 --- a/cloud/apps/push/src/push-send-idempotency.test.ts +++ b/cloud/apps/push/src/http/push-send-idempotency.test.ts @@ -1,6 +1,6 @@ import { afterEach, expect, it } from 'vitest' import { createPushServerHarness, notification } from './push-server-harness.test-fixture.js' -import { createPushHostKeypair } from './host-challenge-answering.test-fixture.js' +import { createPushHostKeypair } from '../host-auth/host-challenge-answering.test-fixture.js' const harnesses: Awaited>[] = [] afterEach(async () => { await Promise.all(harnesses.splice(0).map((h) => h.close())) @@ -34,31 +34,3 @@ it('returns queued for concurrent retries without double quota or delivery', asy await h.flushDeliveries() expect(h.fcmRequests).toHaveLength(2) }) - -it.each([false, true])( - 'accepts default alert kind equivalently through the API (explicit first: %s)', - async (explicitFirst) => { - const h = await createPushServerHarness() - harnesses.push(h) - const token = await h.signIn(createPushHostKeypair(3)) - const registrationId = await h.registerAndroid(token) - const implicit = notification() - const explicit = { kind: 'alert', ...implicit } - for (const event of explicitFirst ? [explicit, implicit] : [implicit, explicit]) { - const response = await h.post( - '/v1/send', - { v: 1, registrationIds: [registrationId], notification: event }, - token - ) - expect(await response.json()).toEqual({ results: [{ registrationId, status: 'queued' }] }) - } - await h.flushDeliveries() - expect(h.fcmRequests).toHaveLength(1) - const changed = await h.post( - '/v1/send', - { v: 1, registrationIds: [registrationId], notification: { ...explicit, body: 'changed' } }, - token - ) - expect(await changed.json()).toEqual({ results: [{ registrationId, status: 'error' }] }) - } -) diff --git a/cloud/apps/push/src/push-server-auth.test.ts b/cloud/apps/push/src/http/push-server-auth.test.ts similarity index 93% rename from cloud/apps/push/src/push-server-auth.test.ts rename to cloud/apps/push/src/http/push-server-auth.test.ts index 50ad8add4f9..ad1989955f7 100644 --- a/cloud/apps/push/src/push-server-auth.test.ts +++ b/cloud/apps/push/src/http/push-server-auth.test.ts @@ -1,8 +1,8 @@ -import { PUSH_LIMITS } from '@orca-cloud/push-contract' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' import { afterEach, beforeEach, describe, expect, it } from 'vitest' -import { createPushHostKeypair } from './host-challenge-answering.test-fixture.js' -import type { PushDatabase } from './push-database.js' -import { createPushServer } from './push-server.js' +import { createPushHostKeypair } from '../host-auth/host-challenge-answering.test-fixture.js' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' +import { createPushServer } from '../push-server.js' import { createPushServerHarness, testPushConfig } from './push-server-harness.test-fixture.js' describe('push gateway authentication and device routes', () => { diff --git a/cloud/apps/push/src/push-server-harness.test-fixture.ts b/cloud/apps/push/src/http/push-server-harness.test-fixture.ts similarity index 89% rename from cloud/apps/push/src/push-server-harness.test-fixture.ts rename to cloud/apps/push/src/http/push-server-harness.test-fixture.ts index f1567dd9ab7..2d7c403c19a 100644 --- a/cloud/apps/push/src/push-server-harness.test-fixture.ts +++ b/cloud/apps/push/src/http/push-server-harness.test-fixture.ts @@ -1,15 +1,17 @@ import { generateKeyPairSync } from 'node:crypto' import { expect } from 'vitest' -import type { ApnsRequest, ApnsResponse } from './apns-http2-transport.js' -import type { PushConfig } from './config.js' -import type { FcmRequest, FcmResponse } from './fcm-client.js' +import type { ApnsRequest } from '../providers/apns/apns-http2-transport.js' +import type { ApnsResponse } from '../providers/apns/apns-stream-response.js' +import type { PushConfig } from '../config.js' +import type { FcmRequest, FcmResponse } from '../providers/fcm/fcm-client.js' import { answerPushHostChallenge, hostPublicKeyB64, type PushHostKeypair -} from './host-challenge-answering.test-fixture.js' -import { openInMemoryPushDatabase, type PushDatabase } from './push-database.js' -import { createPushServer } from './push-server.js' +} from '../host-auth/host-challenge-answering.test-fixture.js' +import { openInMemoryPushDatabase } from '../database/push-database.js' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' +import { createPushServer } from '../push-server.js' export const GATEWAY_ORIGIN = 'https://push.onorca.dev' export const APNS_TOKEN = 'a'.repeat(64) diff --git a/cloud/apps/push/src/push-server-limits.test.ts b/cloud/apps/push/src/http/push-server-limits.test.ts similarity index 78% rename from cloud/apps/push/src/push-server-limits.test.ts rename to cloud/apps/push/src/http/push-server-limits.test.ts index a46384d0d3c..465684353d4 100644 --- a/cloud/apps/push/src/push-server-limits.test.ts +++ b/cloud/apps/push/src/http/push-server-limits.test.ts @@ -1,11 +1,10 @@ -import { PUSH_LIMITS } from '@orca-cloud/push-contract' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { createPushHostKeypair, hostPublicKeyB64 } from './host-challenge-answering.test-fixture.js' import { - createPushServerHarness, - FCM_TOKEN, - notification -} from './push-server-harness.test-fixture.js' + createPushHostKeypair, + hostPublicKeyB64 +} from '../host-auth/host-challenge-answering.test-fixture.js' +import { createPushServerHarness, notification } from './push-server-harness.test-fixture.js' const CLIENT_IP = '203.0.113.7' const OTHER_CLIENT_IP = '198.51.100.9' @@ -177,51 +176,6 @@ describe('push gateway request limits', () => { expect(challenge.status).toBe(200) }) - it('stops repeated forged bearers after the invalid-auth budget is exhausted', async () => { - const headers = { 'x-forwarded-for': CLIENT_IP } - const [before] = await harness.database.query('SELECT COUNT(*) AS sessions FROM push_sessions') - for (let index = 0; index < PUSH_LIMITS.unauthenticatedRequestsPerMinutePerIp; index++) { - const refused = await harness.authorized('/v1/send', { method: 'POST', headers }, 'forged') - expect(refused.status).toBe(401) - } - const limited = await harness.authorized('/v1/send', { method: 'POST', headers }, 'forged') - expect(limited.status).toBe(429) - expect(await limited.json()).toEqual({ error: 'rate_limited' }) - expect(harness.server.unauthenticatedIps.trackedIpCount()).toBe(0) - const [after] = await harness.database.query('SELECT COUNT(*) AS sessions FROM push_sessions') - expect(Number(after?.sessions)).toBe(Number(before?.sessions)) - }) - - it('answers 409 once a host has registered its device allowance', async () => { - const sessionToken = await harness.signIn(createPushHostKeypair(66)) - for (let index = 0; index < PUSH_LIMITS.maxDevicesPerHost; index++) { - const accepted = await harness.post( - '/v1/devices', - { - v: 1, - deviceId: `device-${index}`, - platform: 'android', - token: FCM_TOKEN - }, - sessionToken - ) - expect(accepted.status).toBe(200) - } - - const refused = await harness.post( - '/v1/devices', - { v: 1, deviceId: 'one-too-many', platform: 'android', token: FCM_TOKEN }, - sessionToken - ) - expect(refused.status).toBe(409) - expect(await refused.json()).toEqual({ error: 'too_many_devices' }) - - const listed = await harness.authorized('/v1/devices', {}, sessionToken) - expect(((await listed.json()) as { devices: unknown[] }).devices).toHaveLength( - PUSH_LIMITS.maxDevicesPerHost - ) - }) - // Why: a database error carries the failing row in its message. The response // and the log must both stop at the error's name. it('answers an unexpected route failure with a bare 500 and logs only the name', async () => { diff --git a/cloud/apps/push/src/push-server-send.test.ts b/cloud/apps/push/src/http/push-server-send.test.ts similarity index 97% rename from cloud/apps/push/src/push-server-send.test.ts rename to cloud/apps/push/src/http/push-server-send.test.ts index 9536bed841e..48982f5f697 100644 --- a/cloud/apps/push/src/push-server-send.test.ts +++ b/cloud/apps/push/src/http/push-server-send.test.ts @@ -1,7 +1,7 @@ -import { PushNotificationSchema } from '@orca-cloud/push-contract' -import { PUSH_LIMITS } from '@orca-cloud/push-contract' +import { PushNotificationSchema } from '@orca-cloud/push-contract/send-messages' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' import { afterEach, beforeEach, describe, expect, it } from 'vitest' -import { createPushHostKeypair } from './host-challenge-answering.test-fixture.js' +import { createPushHostKeypair } from '../host-auth/host-challenge-answering.test-fixture.js' import { APNS_TOKEN, createPushServerHarness, diff --git a/cloud/apps/push/src/http/push-server-wake.test.ts b/cloud/apps/push/src/http/push-server-wake.test.ts new file mode 100644 index 00000000000..7a64b68c0d1 --- /dev/null +++ b/cloud/apps/push/src/http/push-server-wake.test.ts @@ -0,0 +1,44 @@ +import { afterEach, expect, it, vi } from 'vitest' +import { startPushBackground } from '../push-background.js' +import { createPushHostKeypair } from '../host-auth/host-challenge-answering.test-fixture.js' +import { createPushServerHarness, notification } from './push-server-harness.test-fixture.js' + +const cleanups: (() => Promise)[] = [] +afterEach(async () => { + for (const cleanup of cleanups.splice(0)) await cleanup() + vi.useRealTimers() + vi.restoreAllMocks() +}) + +async function fixture() { + const h = await createPushServerHarness() + const token = await h.signIn(createPushHostKeypair(31)) + const registrationId = await h.registerAndroid(token) + let stop = async () => {} + cleanups.push(async () => { + await stop() + await h.close() + }) + vi.useFakeTimers() + return { + ...h, + registrationId, + start: () => { + stop = startPushBackground({ mode: 'active' }, h.server) + }, + send: (event = notification(), ids = [registrationId]) => + h.post('/v1/send', { v: 1, registrationIds: ids, notification: event }, token), + invalid: () => h.post('/v1/send', { v: 1 }, token) + } +} + +it('keeps concurrent accepted retries idempotent while automatic delivery runs', async () => { + const h = await fixture() + h.start() + await Promise.all(Array.from({ length: 10 }, () => h.send())) + await vi.waitFor(() => expect(h.fcmRequests).toHaveLength(1)) + await h.send() + await h.server.worker.runDue() + expect(h.fcmRequests).toHaveLength(1) + expect(await h.server.deliveryStore.pendingCount(h.registrationId)).toBe(0) +}) diff --git a/cloud/apps/push/src/index.ts b/cloud/apps/push/src/index.ts index d44148874c7..e9abf51c78a 100644 --- a/cloud/apps/push/src/index.ts +++ b/cloud/apps/push/src/index.ts @@ -1,6 +1,6 @@ import { startPushBackground } from './push-background.js' import { loadPushConfig } from './config.js' -import { openPushDatabase } from './push-database.js' +import { openPushDatabase } from './database/push-database.js' import { createPushServer } from './push-server.js' const config = loadPushConfig() diff --git a/cloud/apps/push/src/apns-authentication-token.ts b/cloud/apps/push/src/providers/apns/apns-authentication-token.ts similarity index 96% rename from cloud/apps/push/src/apns-authentication-token.ts rename to cloud/apps/push/src/providers/apns/apns-authentication-token.ts index 34def16e86e..751c7279f17 100644 --- a/cloud/apps/push/src/apns-authentication-token.ts +++ b/cloud/apps/push/src/providers/apns/apns-authentication-token.ts @@ -1,5 +1,5 @@ import { createPrivateKey, type KeyObject, sign } from 'node:crypto' -import type { ApnsCredentials } from './config.js' +import type { ApnsCredentials } from '../../config.js' // Apple rejects a provider token older than an hour and throttles reissue // under about 20 minutes, so 50 minutes is the safe rotation point. diff --git a/cloud/apps/push/src/apns-client.test.ts b/cloud/apps/push/src/providers/apns/apns-client.test.ts similarity index 64% rename from cloud/apps/push/src/apns-client.test.ts rename to cloud/apps/push/src/providers/apns/apns-client.test.ts index 6e17085506d..7c76aeda694 100644 --- a/cloud/apps/push/src/apns-client.test.ts +++ b/cloud/apps/push/src/providers/apns/apns-client.test.ts @@ -2,9 +2,10 @@ import { generateKeyPairSync } from 'node:crypto' import { expect, it } from 'vitest' import { ApnsClient } from './apns-client.js' -import type { ApnsRequest, ApnsResponse } from './apns-http2-transport.js' -import type { ApnsCredentials } from './config.js' -import { buildPushDelivery } from './push-delivery-message.js' +import type { ApnsRequest } from './apns-http2-transport.js' +import type { ApnsResponse } from './apns-stream-response.js' +import type { ApnsCredentials } from '../../config.js' +import { buildPushDelivery } from '../../delivery/push-delivery-message.js' const HOST = 'abcdefghijklmnop' @@ -46,10 +47,6 @@ function fakeTransport(response: ApnsResponse) { } } - - - - it('does not collapse background dismissals with visible alerts', async () => { const fake = fakeTransport({ status: 200, body: '' }) const apns = new ApnsClient({ @@ -72,29 +69,3 @@ it('does not collapse background dismissals with visible alerts', async () => { }) expect(JSON.parse(fake.requests[0]!.body).aps).toEqual({ 'content-available': 1 }) }) - -it('keeps the absolute deadline across retries and refuses expired delivery', async () => { - let now = 1_700_000_000_000 - const fake = fakeTransport({ status: 503, body: '{}' }) - const client = new ApnsClient({ - topic: 'test', - credentials: credentials(), - now: () => now, - transport: fake.transport - }) - const pending = delivery(now) - const device = { token: 'test', apnsEnvironment: 'sandbox' as const } - await client.send(pending, device) - now += 60_000 - await client.send(pending, device) - expect(fake.requests.map((request) => request.headers['apns-expiration'])).toEqual([ - String(pending.expiresAt / 1000), - String(pending.expiresAt / 1000) - ]) - now = pending.expiresAt - await expect(client.send(pending, device)).resolves.toEqual({ - status: 'error', - reason: 'expired' - }) - expect(fake.requests).toHaveLength(2) -}) diff --git a/cloud/apps/push/src/apns-client.ts b/cloud/apps/push/src/providers/apns/apns-client.ts similarity index 91% rename from cloud/apps/push/src/apns-client.ts rename to cloud/apps/push/src/providers/apns/apns-client.ts index 827efdfea79..e49f593b83b 100644 --- a/cloud/apps/push/src/apns-client.ts +++ b/cloud/apps/push/src/providers/apns/apns-client.ts @@ -1,9 +1,9 @@ -import type { ApnsEnvironment } from '@orca-cloud/push-contract' +import type { ApnsEnvironment } from '@orca-cloud/push-contract/device-registration-messages' import { ApnsAuthenticationToken } from './apns-authentication-token.js' import type { ApnsTransport } from './apns-http2-transport.js' -import type { ApnsCredentials } from './config.js' -import type { PushDelivery } from './push-delivery-message.js' -import type { PushProviderOutcome } from './push-provider-outcome.js' +import type { ApnsCredentials } from '../../config.js' +import type { PushDelivery } from '../../delivery/push-delivery-message.js' +import type { PushProviderOutcome } from '../push-provider-outcome.js' const APNS_HOSTS: Record = { production: 'api.push.apple.com', diff --git a/cloud/apps/push/src/apns-http2-transport.ts b/cloud/apps/push/src/providers/apns/apns-http2-transport.ts similarity index 98% rename from cloud/apps/push/src/apns-http2-transport.ts rename to cloud/apps/push/src/providers/apns/apns-http2-transport.ts index d6cbc9b0b13..bf37ece50b7 100644 --- a/cloud/apps/push/src/apns-http2-transport.ts +++ b/cloud/apps/push/src/providers/apns/apns-http2-transport.ts @@ -8,7 +8,6 @@ export type ApnsRequest = { body: string } -export type { ApnsResponse } export type ApnsTransport = (request: ApnsRequest) => Promise // Cache APNs HTTP/2 sessions per host until socket loss; long-lived connections avoid reconnect overhead. diff --git a/cloud/apps/push/src/apns-stream-response.test.ts b/cloud/apps/push/src/providers/apns/apns-stream-response.test.ts similarity index 99% rename from cloud/apps/push/src/apns-stream-response.test.ts rename to cloud/apps/push/src/providers/apns/apns-stream-response.test.ts index 0cf8b3554be..e8fb3ad6f6c 100644 --- a/cloud/apps/push/src/apns-stream-response.test.ts +++ b/cloud/apps/push/src/providers/apns/apns-stream-response.test.ts @@ -28,8 +28,6 @@ function fakeApnsStream(): FakeStream { } describe('apns stream response', () => { - - it('rejects when the peer resets the stream without an end or an error', async () => { const stream = fakeApnsStream() const pending = readApnsStreamResponse(stream, 'body') @@ -38,12 +36,4 @@ describe('apns stream response', () => { stream.emit('close') await expect(pending).rejects.toThrow('apns_stream_closed') }) - - - - - - - - }) diff --git a/cloud/apps/push/src/apns-stream-response.ts b/cloud/apps/push/src/providers/apns/apns-stream-response.ts similarity index 96% rename from cloud/apps/push/src/apns-stream-response.ts rename to cloud/apps/push/src/providers/apns/apns-stream-response.ts index 8ea0c1b1a21..18f27c425c8 100644 --- a/cloud/apps/push/src/apns-stream-response.ts +++ b/cloud/apps/push/src/providers/apns/apns-stream-response.ts @@ -1,5 +1,5 @@ import type { EventEmitter } from 'node:events' -import { providerRetryAfter } from './provider-retry-delay.js' +import { providerRetryAfter } from '../provider-retry-delay.js' import { constants } from 'node:http2' export type ApnsResponse = { status: number; body: string; retryAfterMs?: number } diff --git a/cloud/apps/push/src/apns-topic-recovery.test.ts b/cloud/apps/push/src/providers/apns/apns-topic-recovery.test.ts similarity index 90% rename from cloud/apps/push/src/apns-topic-recovery.test.ts rename to cloud/apps/push/src/providers/apns/apns-topic-recovery.test.ts index 583d4536b5a..2697564792c 100644 --- a/cloud/apps/push/src/apns-topic-recovery.test.ts +++ b/cloud/apps/push/src/providers/apns/apns-topic-recovery.test.ts @@ -1,10 +1,10 @@ import { expect, it } from 'vitest' -import { createPushHostKeypair } from './host-challenge-answering.test-fixture.js' +import { createPushHostKeypair } from '../../host-auth/host-challenge-answering.test-fixture.js' import { APNS_TOKEN, createPushServerHarness, notification -} from './push-server-harness.test-fixture.js' +} from '../../http/push-server-harness.test-fixture.js' it('delivers again after a topic error without re-registering the phone', async () => { const h = await createPushServerHarness() diff --git a/cloud/apps/push/src/fcm-access-token.ts b/cloud/apps/push/src/providers/fcm/fcm-access-token.ts similarity index 100% rename from cloud/apps/push/src/fcm-access-token.ts rename to cloud/apps/push/src/providers/fcm/fcm-access-token.ts diff --git a/cloud/apps/push/src/fcm-client.test.ts b/cloud/apps/push/src/providers/fcm/fcm-client.test.ts similarity index 74% rename from cloud/apps/push/src/fcm-client.test.ts rename to cloud/apps/push/src/providers/fcm/fcm-client.test.ts index 107ef8c648d..8aeeba07af6 100644 --- a/cloud/apps/push/src/fcm-client.test.ts +++ b/cloud/apps/push/src/providers/fcm/fcm-client.test.ts @@ -1,7 +1,6 @@ - import { describe, expect, it } from 'vitest' import { FcmClient, type FcmRequest, type FcmResponse } from './fcm-client.js' -import { buildPushDelivery } from './push-delivery-message.js' +import { buildPushDelivery } from '../../delivery/push-delivery-message.js' const NOW = 1_700_000_000_000 const HOST = 'abcdefghijklmnop' @@ -51,9 +50,6 @@ function client(response: FcmResponse) { } describe('fcm client', () => { - - - it('marks an invalid-argument that names the token dead, and others an error', async () => { const named = client({ status: 400, @@ -78,8 +74,6 @@ describe('fcm client', () => { retryAfterMs: 10000 }) }) - - }) it('does not send when credential refresh crosses the absolute expiry', async () => { @@ -100,33 +94,3 @@ it('does not send when credential refresh crosses the absolute expiry', async () }) expect(fake.requests).toHaveLength(0) }) - -it('decreases retry TTL and refuses expired delivery before refreshing credentials', async () => { - let now = NOW - let refreshes = 0 - const fake = fakeTransport({ status: 503, body: '{}' }) - const fcm = new FcmClient({ - projectId: 'test', - now: () => now, - accessToken: async () => { - refreshes++ - return 'test-token' - }, - transport: fake.transport - }) - const pending = delivery() - await fcm.send(pending, { token: TOKEN }) - now += 60_000 - await fcm.send(pending, { token: TOKEN }) - expect(fake.requests.map((request) => JSON.parse(request.body).message.android.ttl)).toEqual([ - '300s', - '240s' - ]) - now = pending.expiresAt - await expect(fcm.send(pending, { token: TOKEN })).resolves.toEqual({ - status: 'error', - reason: 'expired' - }) - expect(fake.requests).toHaveLength(2) - expect(refreshes).toBe(2) -}) diff --git a/cloud/apps/push/src/fcm-client.ts b/cloud/apps/push/src/providers/fcm/fcm-client.ts similarity index 93% rename from cloud/apps/push/src/fcm-client.ts rename to cloud/apps/push/src/providers/fcm/fcm-client.ts index 0b58aae80f5..66ef292bd18 100644 --- a/cloud/apps/push/src/fcm-client.ts +++ b/cloud/apps/push/src/providers/fcm/fcm-client.ts @@ -1,7 +1,7 @@ -import { providerRetryAfter } from './provider-retry-delay.js' -import { PUSH_DEFAULTS } from '@orca-cloud/push-contract' -import { orcaDataStrings, type PushDelivery } from './push-delivery-message.js' -import type { PushProviderOutcome } from './push-provider-outcome.js' +import { providerRetryAfter } from '../provider-retry-delay.js' +import { PUSH_DEFAULTS } from '@orca-cloud/push-contract/push-limits' +import { orcaDataStrings, type PushDelivery } from '../../delivery/push-delivery-message.js' +import type { PushProviderOutcome } from '../push-provider-outcome.js' export const FCM_SCOPE = 'https://www.googleapis.com/auth/firebase.messaging' diff --git a/cloud/apps/push/src/provider-retry-delay.ts b/cloud/apps/push/src/providers/provider-retry-delay.ts similarity index 100% rename from cloud/apps/push/src/provider-retry-delay.ts rename to cloud/apps/push/src/providers/provider-retry-delay.ts diff --git a/cloud/apps/push/src/push-provider-outcome.ts b/cloud/apps/push/src/providers/push-provider-outcome.ts similarity index 100% rename from cloud/apps/push/src/push-provider-outcome.ts rename to cloud/apps/push/src/providers/push-provider-outcome.ts diff --git a/cloud/apps/push/src/push-background.ts b/cloud/apps/push/src/push-background.ts index 25b98d0d467..fec2f121117 100644 --- a/cloud/apps/push/src/push-background.ts +++ b/cloud/apps/push/src/push-background.ts @@ -1,5 +1,5 @@ import type { PushConfig } from './config.js' -import type { PushPruneSweep } from './durable-push-store.js' +import type { PushPruneSweep } from './delivery/durable-push-store.js' import type { createPushServer } from './push-server.js' const CHALLENGE_PRUNE_INTERVAL_MS = 60_000 @@ -45,9 +45,10 @@ function prune(label: string, run: () => Promise, intervalMs: nu } // One DELETE under a statement timeout: there is no batch budget for it to exhaust. -const unbatchedSweep = - (run: () => Promise) => - async (): Promise => ({ deleted: await run(), saturated: false }) +const unbatchedSweep = (run: () => Promise) => async (): Promise => ({ + deleted: await run(), + saturated: false +}) export function startPushBackground( config: Pick, diff --git a/cloud/apps/push/src/push-database-postgres-startup.test.ts b/cloud/apps/push/src/push-database-postgres-startup.test.ts deleted file mode 100644 index 08be1dad766..00000000000 --- a/cloud/apps/push/src/push-database-postgres-startup.test.ts +++ /dev/null @@ -1,101 +0,0 @@ -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { randomUUID } from 'node:crypto' - -const fakes = vi.hoisted(() => ({ - configs: [] as Array>, - lifecycle: [] as string[], - query: vi.fn(async (_sql: string) => ({ rows: [], rowCount: 0 })), - release: vi.fn() -})) - -vi.mock('pg', () => ({ - default: { - Pool: class { - on = vi.fn() - connect = vi.fn(async () => ({ query: fakes.query, release: fakes.release })) - private readonly label: string - - constructor(config: Record) { - fakes.configs.push(config) - this.label = `max=${String(config.max)} statement_timeout=${String(config.statement_timeout)}` - fakes.lifecycle.push(`open ${this.label}`) - } - - async end(): Promise { - fakes.lifecycle.push(`end ${this.label}`) - } - } - } -})) - -import { openPushDatabase } from './push-database.js' -import { pushSchemaStatements } from './push-schema.js' - -describe('PostgreSQL push gateway startup', () => { - beforeEach(() => { - fakes.configs.length = 0 - fakes.lifecycle.length = 0 - fakes.query.mockClear() - }) - - afterEach(() => { - vi.restoreAllMocks() - }) - - const socketPassword = `${randomUUID()}@/` - const socketUrl = `postgresql://push:${encodeURIComponent(socketPassword)}@/orca_push?host=/cloudsql/test:region:instance` - - it('passes the Terraform socket URL unchanged to both active pools', async () => { - const database = await openPushDatabase({ databaseUrl: socketUrl, dataDir: '/unused' }) - expect(fakes.configs.map((config) => config.connectionString)).toEqual([socketUrl, socketUrl]) - await database.close() - }) - - it('parses socket credentials and query options before enforcing validation read-only', async () => { - const options = '-c search_path=validation -c default_transaction_read_only=off' - const database = await openPushDatabase({ - databaseUrl: `${socketUrl}&port=5433&sslmode=disable&options=${encodeURIComponent(options)}`, - dataDir: '/unused', - readOnly: true - }) - expect(fakes.configs).toHaveLength(1) - expect(fakes.configs[0]).toMatchObject({ - host: '/cloudsql/test:region:instance', - user: 'push', - password: socketPassword, - database: 'orca_push', - port: 5433, - ssl: false, - options: `${options} -c default_transaction_read_only=on` - }) - expect(fakes.configs[0]).not.toHaveProperty('connectionString') - expect(fakes.query).not.toHaveBeenCalled() - await database.close() - }) - - // Growing-table CREATE INDEX can exceed request deadlines; inheriting them makes startup fail forever at the same DDL. - it('applies the schema on an untimed pool that is gone before the serving pool opens', async () => { - const database = await openPushDatabase({ - databaseUrl: 'postgresql://push@localhost:55440/orca_push', - dataDir: '/unused', - poolMax: 2, - applicationName: 'orca-push' - }) - expect(fakes.lifecycle).toEqual([ - 'open max=1 statement_timeout=0', - 'end max=1 statement_timeout=0', - 'open max=2 statement_timeout=5000' - ]) - expect(fakes.configs[0]).toMatchObject({ - application_name: 'orca-push/schema', - lock_timeout: 1_000, - idle_in_transaction_session_timeout: 5_000 - }) - expect( - fakes.query.mock.calls.map(([sql]) => sql).slice(0, pushSchemaStatements().length) - ).toEqual(pushSchemaStatements()) - await database.close() - }) - - -}) diff --git a/cloud/apps/push/src/push-delivery-lifecycle.test.ts b/cloud/apps/push/src/push-delivery-lifecycle.test.ts index c94108e459c..ef845cc0811 100644 --- a/cloud/apps/push/src/push-delivery-lifecycle.test.ts +++ b/cloud/apps/push/src/push-delivery-lifecycle.test.ts @@ -1,12 +1,13 @@ import { afterEach, expect, it, vi } from 'vitest' import { Hono } from 'hono' -import { PushRequestDrain } from './push-request-drain.js' -import { PushDispatcher } from './push-dispatcher.js' -import { PushDeviceRegistryStore } from './device-registry-store.js' -import { openInMemoryPushDatabase, type PushDatabase } from './push-database.js' -import { buildPushDelivery } from './push-delivery-message.js' -import { PushNotificationSchema } from '@orca-cloud/push-contract' -import { notification } from './push-server-harness.test-fixture.js' +import { PushRequestDrain } from './http/push-request-drain.js' +import { PushDispatcher } from './delivery/push-dispatcher.js' +import { PushDeviceRegistryStore } from './devices/device-registry-store.js' +import { openInMemoryPushDatabase } from './database/push-database.js' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' +import { buildPushDelivery } from './delivery/push-delivery-message.js' +import { PushNotificationSchema } from '@orca-cloud/push-contract/send-messages' +import { notification } from './http/push-server-harness.test-fixture.js' const databases: PushDatabase[] = [] afterEach(async () => { diff --git a/cloud/apps/push/src/push-dismissal-provider.test.ts b/cloud/apps/push/src/push-dismissal-provider.test.ts deleted file mode 100644 index 65572e8401c..00000000000 --- a/cloud/apps/push/src/push-dismissal-provider.test.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { expect, it } from 'vitest' -import { apnsBody } from './apns-client.js' -import { fcmMessageBody } from './fcm-client.js' -import { buildPushDelivery } from './push-delivery-message.js' - -it('dismissal provider payloads cannot display a new alert or play a sound', () => { - const delivery = buildPushDelivery({ - expiresAt: Date.now() + 300_000, - registrationId: 'reg', - hostFingerprint: 'host', - notification: { - kind: 'dismiss', - notificationId: 'note', - notificationSeq: 2, - notificationEpoch: 'epoch', - source: 'agent-task-complete', - agentState: null, - title: 'Orca', - body: '' - } - }) - expect(JSON.parse(apnsBody(delivery)).aps).toEqual({ 'content-available': 1 }) - const android = JSON.parse(fcmMessageBody({ delivery, token: 'test', channelId: 'test' })).message - expect(android).not.toHaveProperty('notification') - expect(android.android).not.toHaveProperty('notification') - expect(android.android).not.toHaveProperty('collapse_key') - expect(android.data).not.toHaveProperty('title') - expect(android.data).not.toHaveProperty('message') - expect(android.data).not.toHaveProperty('sound') - expect(android.data.kind).toBe('dismiss') -}) diff --git a/cloud/apps/push/src/push-notification-sound.test.ts b/cloud/apps/push/src/push-notification-sound.test.ts deleted file mode 100644 index ede4dcd3291..00000000000 --- a/cloud/apps/push/src/push-notification-sound.test.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { expect, it } from 'vitest' -import { apnsBody } from './apns-client.js' -import { fcmMessageBody } from './fcm-client.js' -import { buildPushDelivery } from './push-delivery-message.js' -import { PushNotificationSchema } from '@orca-cloud/push-contract' - -it('carries a silent preference through validation to APNs and Android payloads', () => { - const notification = PushNotificationSchema.parse({ - notificationSeq: 1, - notificationEpoch: 'epoch', - source: 'terminal-bell', - agentState: null, - title: 'Bell', - body: '', - sound: false - }) - const delivery = buildPushDelivery({ - expiresAt: Date.now() + 300_000, - registrationId: 'reg', - hostFingerprint: 'host', - notification - }) - expect(JSON.parse(apnsBody(delivery)).aps).not.toHaveProperty('sound') - expect( - JSON.parse(fcmMessageBody({ delivery, token: 'test-token', channelId: 'orca-desktop' })).message - .data.channelId - ).toBe('orca-desktop-silent') - expect( - JSON.parse(fcmMessageBody({ delivery, token: 'test-token', channelId: 'orca-desktop' })).message - .data.sound - ).toBe('') - expect(JSON.parse(apnsBody({ ...delivery, sound: undefined })).aps.sound).toBe('default') -}) diff --git a/cloud/apps/push/src/push-schema-boot-lock.test.ts b/cloud/apps/push/src/push-schema-boot-lock.test.ts deleted file mode 100644 index 1c08fab56d1..00000000000 --- a/cloud/apps/push/src/push-schema-boot-lock.test.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { randomUUID } from 'node:crypto' -import pg from 'pg' -import { afterEach, expect, it, vi } from 'vitest' -import { openPushDatabase } from './push-database.js' -import { durablePushTestDatabaseUrl } from './durable-push-store.test-fixture.js' - -const QUEUE_INDEXES = ['push_batches_pending_device'] -const cleanups: (() => Promise)[] = [] -afterEach(async () => { - vi.restoreAllMocks() - for (const cleanup of cleanups.splice(0).reverse()) await cleanup() -}) - -// Push applies its schema with retryLockTimeout, so a lock timeout is retried, never deferred: -// the new revision either boots with every index or does not boot at all. -it.skipIf(!durablePushTestDatabaseUrl)( - 'creates the queue index after waiting out a writer that holds the table', - async () => { - const admin = new pg.Client({ connectionString: durablePushTestDatabaseUrl }) - await admin.connect() - const schema = `boot_${randomUUID().replaceAll('-', '')}` - cleanups.push(async () => { - await admin.query(`DROP SCHEMA IF EXISTS ${schema} CASCADE`) - await admin.end() - }) - await admin.query(`CREATE SCHEMA ${schema}`) - const url = new URL(durablePushTestDatabaseUrl!) - url.searchParams.set('options', `-c search_path=${schema}`) - await (await openPushDatabase({ databaseUrl: url.toString(), dataDir: '' })).close() - await admin.query( - `DROP INDEX ${QUEUE_INDEXES.map((name) => `${schema}.${name}`).join(', ')}` - ) - - const writer = new pg.Client({ connectionString: url.toString() }) - await writer.connect() - cleanups.push(() => writer.end()) - await writer.query('BEGIN') - await writer.query('LOCK TABLE push_delivery_batches IN ROW EXCLUSIVE MODE') - const warnings: string[] = [] - vi.spyOn(console, 'warn').mockImplementation((line: string) => warnings.push(line)) - vi.spyOn(console, 'log').mockImplementation(() => undefined) - const held = setTimeout(() => void writer.query('COMMIT'), 2_500) - cleanups.push(async () => clearTimeout(held)) - - const booted = await openPushDatabase({ databaseUrl: url.toString(), dataDir: '' }) - await booted.close() - const { rows } = await admin.query( - 'SELECT indexname FROM pg_indexes WHERE schemaname = $1 AND indexname = ANY($2) ORDER BY 1', - [schema, QUEUE_INDEXES] - ) - expect(rows.map((row) => row.indexname)).toEqual(QUEUE_INDEXES) - const events = warnings.map((line) => JSON.parse(line).event) - expect(events).toContain('orca_push_postgres_schema_retry') - expect(events).not.toContain('orca_push_postgres_schema_object_deferred') - } -) diff --git a/cloud/apps/push/src/push-server-wake.test.ts b/cloud/apps/push/src/push-server-wake.test.ts deleted file mode 100644 index 8a5442df9c5..00000000000 --- a/cloud/apps/push/src/push-server-wake.test.ts +++ /dev/null @@ -1,111 +0,0 @@ -import { afterEach, expect, it, vi } from 'vitest' -import { startPushBackground } from './push-background.js' -import { createPushHostKeypair } from './host-challenge-answering.test-fixture.js' -import { createPushServerHarness, notification } from './push-server-harness.test-fixture.js' - -const cleanups: (() => Promise)[] = [] -afterEach(async () => { - for (const cleanup of cleanups.splice(0)) await cleanup() - vi.useRealTimers() - vi.restoreAllMocks() -}) - -async function fixture() { - const h = await createPushServerHarness() - const token = await h.signIn(createPushHostKeypair(31)) - const registrationId = await h.registerAndroid(token) - let stop = async () => {} - cleanups.push(async () => { - await stop() - await h.close() - }) - vi.useFakeTimers() - return { - ...h, - registrationId, - start: () => { - stop = startPushBackground({ mode: 'active' }, h.server) - }, - send: (event = notification(), ids = [registrationId]) => - h.post('/v1/send', { v: 1, registrationIds: ids, notification: event }, token), - invalid: () => h.post('/v1/send', { v: 1 }, token) - } -} - -it('delivers a committed send before the next poll without waiting in the HTTP handler', async () => { - const h = await fixture() - h.start() - const startedAt = Date.now() - const claim = h.server.deliveryStore.claim.bind(h.server.deliveryStore) - const release = Promise.withResolvers() - vi.spyOn(h.server.deliveryStore, 'claim').mockImplementation(async () => { - await release.promise - return claim() - }) - try { - const response = await h.send() - expect(await response.json()).toEqual({ - results: [{ registrationId: h.registrationId, status: 'queued' }] - }) - expect(h.fcmRequests).toHaveLength(0) - release.resolve() - await vi.waitFor(() => expect(h.fcmRequests).toHaveLength(1)) - expect(Date.now() - startedAt).toBeLessThan(1000) - expect(JSON.parse(h.fcmRequests[0]!.body).message.data.notificationSeq).toBe('1') - } finally { - release.resolve() - } -}) - -it('keeps concurrent accepted retries idempotent while automatic delivery runs', async () => { - const h = await fixture() - h.start() - await Promise.all(Array.from({ length: 10 }, () => h.send())) - await vi.waitFor(() => expect(h.fcmRequests).toHaveLength(1)) - await h.send() - await h.server.worker.runDue() - expect(h.fcmRequests).toHaveLength(1) - expect(await h.server.deliveryStore.pendingCount(h.registrationId)).toBe(0) -}) - -it('does not wake for invalid, unowned or dead registrations', async () => { - const h = await fixture() - h.start() - const wake = vi.spyOn(h.server.worker, 'wake') - expect((await h.invalid()).status).toBe(400) - expect(await (await h.send(notification(), ['unknown'])).json()).toEqual({ - results: [{ registrationId: 'unknown', status: 'error' }] - }) - const device = await h.server.devices.findById(h.registrationId) - if (!device) throw new Error('registration missing') - await h.server.devices.markDead(device) - expect(await (await h.send()).json()).toEqual({ - results: [{ registrationId: h.registrationId, status: 'dead' }] - }) - expect(wake).not.toHaveBeenCalled() - expect(h.fcmRequests).toHaveLength(0) -}) - -it.each(['queued', 'sent'] as const)( - 'preserves dismissal of an already %s alert', - async (phase) => { - const h = await fixture() - if (phase === 'sent') h.start() - await h.send() - if (phase === 'sent') await vi.waitFor(() => expect(h.fcmRequests).toHaveLength(1)) - await h.send(notification({ kind: 'dismiss', notificationSeq: 2, sound: false })) - if (phase === 'queued') { - h.start() - await vi.advanceTimersByTimeAsync(1000) - } - await vi.waitFor(() => expect(h.fcmRequests).toHaveLength(phase === 'sent' ? 2 : 1)) - const last = h.fcmRequests.at(-1) - if (!last) throw new Error('dismissal missing') - const message = JSON.parse(last.body).message - expect(message.data.kind).toBe('dismiss') - expect(message.data.notificationId).toBe('note-1') - expect(message.data.notificationSeq).toBe('2') - expect(message.data.title).toBeUndefined() - expect(message.data.sound).toBeUndefined() - } -) diff --git a/cloud/apps/push/src/push-server.ts b/cloud/apps/push/src/push-server.ts index a174a3994f1..5889f811df9 100644 --- a/cloud/apps/push/src/push-server.ts +++ b/cloud/apps/push/src/push-server.ts @@ -1,32 +1,42 @@ -import { PushAuthAdmission } from './push-auth-admission.js' +import { PushAuthAdmission } from './http/push-auth-admission.js' import { createAdaptorServer } from '@hono/node-server' +import { PUSH_LIMITS } from '@orca-cloud/push-contract/push-limits' +import { PushDeviceRegistrationRequestSchema } from '@orca-cloud/push-contract/device-registration-messages' import { - PUSH_LIMITS, - PushDeviceRegistrationRequestSchema, PushHostChallengeRequestSchema, - PushHostSessionRequestSchema, - PushSendRequestSchema, - type PushSendResult -} from '@orca-cloud/push-contract' + PushHostSessionRequestSchema +} from '@orca-cloud/push-contract/host-auth-messages' +import { PushSendRequestSchema, type PushSendResult } from '@orca-cloud/push-contract/send-messages' import { Hono, type MiddlewareHandler } from 'hono' import { bodyLimit } from 'hono/body-limit' -import { ApnsClient } from './apns-client.js' -import { createApnsHttp2Transport, type ApnsTransport } from './apns-http2-transport.js' -import { clientIpRateLimit, ClientIpRateLimiter, readClientIp } from './client-ip-rate-limit.js' -import { DurablePushStore } from './durable-push-store.js' -import { DurablePushWorker } from './durable-push-worker.js' +import { ApnsClient } from './providers/apns/apns-client.js' +import { + createApnsHttp2Transport, + type ApnsTransport +} from './providers/apns/apns-http2-transport.js' +import { + clientIpRateLimit, + ClientIpRateLimiter, + readClientIp +} from './http/client-ip-rate-limit.js' +import { DurablePushStore } from './delivery/durable-push-store.js' +import { DurablePushWorker } from './delivery/durable-push-worker.js' import type { PushConfig } from './config.js' -import { PushDeviceRegistryStore } from './device-registry-store.js' -import { createFcmAccessTokenProvider } from './fcm-access-token.js' -import { createFcmFetchTransport, FcmClient, type FcmTransport } from './fcm-client.js' -import { PushHostChallengeStore } from './host-challenge-store.js' -import { PushHostSessionStore } from './host-session-store.js' -import type { PushDatabase } from './push-database.js' -import { PushDispatcher } from './push-dispatcher.js' +import { PushDeviceRegistryStore } from './devices/device-registry-store.js' +import { createFcmAccessTokenProvider } from './providers/fcm/fcm-access-token.js' +import { + createFcmFetchTransport, + FcmClient, + type FcmTransport +} from './providers/fcm/fcm-client.js' +import { PushHostChallengeStore } from './host-auth/host-challenge-store.js' +import { PushHostSessionStore } from './host-auth/host-session-store.js' +import type { CloudDatabase as PushDatabase } from '@orca-cloud/cloud-database/cloud-database' +import { PushDispatcher } from './delivery/push-dispatcher.js' import { PushObservability } from './push-observability.js' -import { reserveRequestConnection } from './push-background-database.js' -import { createPushReadiness } from './push-readiness.js' -import { PushRequestDrain } from './push-request-drain.js' +import { reserveRequestConnection } from './database/push-background-database.js' +import { createPushReadiness } from './http/push-readiness.js' +import { PushRequestDrain } from './http/push-request-drain.js' export type PushServerOptions = { now?: () => number diff --git a/cloud/apps/push/src/push-validation.test.ts b/cloud/apps/push/src/push-validation.test.ts deleted file mode 100644 index afb6906e423..00000000000 --- a/cloud/apps/push/src/push-validation.test.ts +++ /dev/null @@ -1,90 +0,0 @@ -import { expect, it, vi } from 'vitest' -import { loadPushConfig } from './config.js' -import { openPushDatabase } from './push-database.js' -import { createPushServer } from './push-server.js' -import { startPushBackground } from './push-background.js' - -it('fails closed on an invalid validation mode', () => { - for (const mode of ['typo', '', ' ']) { - expect(() => - loadPushConfig({ - ORCA_PUSH_FCM_PROJECT_ID: 'onorca-cloud', - ORCA_PUSH_PUBLIC_URL: 'https://push.onorca.dev', - ORCA_PUSH_MODE: mode - }) - ).toThrow() - } -}) - -const databaseUrl = process.env.ORCA_PUSH_TEST_DATABASE_URL -it.skipIf(!databaseUrl)( - 'validation cannot write PostgreSQL and starts no consumers or pruners', - async () => { - if (!process.env.CI && new URL(databaseUrl!).port !== '55440') - throw new Error('isolated_postgres_port_required') - const active = await openPushDatabase({ databaseUrl, dataDir: '' }) - const schema = `validation_${Date.now()}` - await active.query(`CREATE SCHEMA ${schema}`) - const isolatedUrl = new URL(databaseUrl!) - isolatedUrl.searchParams.set( - 'options', - `-c search_path=${schema} -c default_transaction_read_only=off` - ) - isolatedUrl.searchParams.set('host', isolatedUrl.hostname) - isolatedUrl.searchParams.set('port', isolatedUrl.port) - const hostlessUrl = `postgresql://${isolatedUrl.username}:${isolatedUrl.password}@${isolatedUrl.pathname}?${isolatedUrl.searchParams}` - const database = await openPushDatabase({ - databaseUrl: hostlessUrl, - dataDir: '', - readOnly: true - }) - const config = loadPushConfig({ - ORCA_PUSH_FCM_PROJECT_ID: 'onorca-cloud', - ORCA_PUSH_PUBLIC_URL: 'https://push.onorca.dev', - ORCA_PUSH_MODE: 'validation' - }) - const runtime = createPushServer(config, database) - let stop: (() => Promise) | undefined - try { - const [setting] = await database.query( - "SELECT current_setting('default_transaction_read_only') AS default_transaction_read_only" - ) - expect(setting!.default_transaction_read_only).toBe('on') - await expect( - database.query(`CREATE TABLE ${schema}.forbidden (id integer)`) - ).rejects.toMatchObject({ code: '25006' }) - // An empty schema stays empty: validation must not run startup DDL. - expect( - await active.query('SELECT tablename FROM pg_tables WHERE schemaname = ?', [schema]) - ).toEqual([]) - const calls = vi.spyOn(database, 'query') - const claim = vi.spyOn(runtime.deliveryStore, 'claim') - const send = vi.spyOn(runtime.worker, 'start') - vi.useFakeTimers() - stop = startPushBackground(config, runtime) - await vi.advanceTimersByTimeAsync(31 * 60_000) - expect(calls).not.toHaveBeenCalled() - expect(claim).not.toHaveBeenCalled() - expect(send).not.toHaveBeenCalled() - vi.useRealTimers() - expect(await (await runtime.app.request('/health')).json()).toMatchObject({ - mode: 'validation' - }) - expect((await runtime.app.request('/ready')).status).toBe(200) - expect((await runtime.app.request('/v1/host/challenge', { method: 'POST' })).status).toBe(503) - expect((await runtime.app.request('/v1/send', { method: 'POST' })).status).toBe(503) - expect(calls.mock.calls.map(([sql]) => sql)).toEqual(['SELECT 1 AS ready']) - await expect( - database.query('DELETE FROM public.push_challenges WHERE false') - ).rejects.toMatchObject({ code: '25006' }) - } finally { - vi.useRealTimers() - await stop?.() - runtime.closeTransports() - await database.close() - await active.query(`DROP SCHEMA ${schema} CASCADE`) - await active.close() - vi.restoreAllMocks() - } - } -) diff --git a/cloud/apps/push/src/sqlite-transaction-queue.test.ts b/cloud/apps/push/src/sqlite-transaction-queue.test.ts deleted file mode 100644 index 1ee5e8b3d52..00000000000 --- a/cloud/apps/push/src/sqlite-transaction-queue.test.ts +++ /dev/null @@ -1,101 +0,0 @@ -import { mkdtempSync, rmSync } from 'node:fs' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { DatabaseSync } from 'node:sqlite' -import { setImmediate } from 'node:timers/promises' -import { expect, it, vi } from 'vitest' -import { openPushDatabase } from './push-database.js' - -it('releases queued work and close after a SQLite transaction cannot acquire its lock', async () => { - const dataDir = mkdtempSync(join(tmpdir(), 'orca-push-sqlite-queue-')) - let connection: DatabaseSync | undefined - const prepare = DatabaseSync.prototype.prepare - // Keep the native handle reachable for cleanup even if a queue regression strands close(). - const capture = vi - .spyOn(DatabaseSync.prototype, 'prepare') - .mockImplementation(function (this: DatabaseSync, sql) { - connection = this - return prepare.call(this, sql) - }) - const database = await openPushDatabase({ dataDir }) - capture.mockRestore() - const blocker = new DatabaseSync(join(dataDir, 'orca-push.sqlite')) - try { - await database.query('CREATE TABLE queue_progress (value INTEGER)') - await database.query('INSERT INTO queue_progress VALUES (0)') - blocker.exec('BEGIN IMMEDIATE') - const operation = vi.fn(async () => undefined) - await expect(database.transaction(operation)).rejects.toThrow('database is locked') - let lockFailures = 0 - const blocked = Array.from({ length: 5 }, () => - database.transaction(operation).catch(() => { - lockFailures += 1 - }) - ) - await setImmediate() - expect(lockFailures).toBe(5) - await Promise.all(blocked) - expect(operation).not.toHaveBeenCalled() - blocker.exec('ROLLBACK') - let completed = 0 - const pending = Array.from({ length: 100 }, () => - database.transaction(async (transaction) => { - await transaction.query('UPDATE queue_progress SET value = value + 1') - completed += 1 - }) - ) - for (const request of pending) void request.catch(() => undefined) - await setImmediate() - expect(completed).toBe(100) - await Promise.all(pending) - expect(await database.query('SELECT value FROM queue_progress')).toEqual([{ value: 100 }]) - let closed = false - const closing = database.close().then(() => { - closed = true - }) - await setImmediate() - expect(closed).toBe(true) - await closing - } finally { - capture.mockRestore() - blocker.close() - if (connection?.isOpen) connection.close() - rmSync(dataDir, { recursive: true, force: true }) - } -}) - -it('does not roll back a transaction when BEGIN failed before taking ownership', async () => { - const dataDir = mkdtempSync(join(tmpdir(), 'orca-push-sqlite-owner-')) - let connection: DatabaseSync | undefined - const prepare = DatabaseSync.prototype.prepare - const capture = vi - .spyOn(DatabaseSync.prototype, 'prepare') - .mockImplementation(function (this: DatabaseSync, sql) { - connection = this - return prepare.call(this, sql) - }) - const database = await openPushDatabase({ dataDir }) - capture.mockRestore() - try { - await database.query('CREATE TABLE queue_owner (value INTEGER)') - await database.query('BEGIN IMMEDIATE') - await database.query('INSERT INTO queue_owner VALUES (7)') - await expect(database.transaction(async () => undefined)).rejects.toThrow( - 'cannot start a transaction within a transaction' - ) - expect(connection?.isTransaction).toBe(true) - let rows: unknown - void database.query('SELECT value FROM queue_owner').then((result) => { - rows = result - }) - await setImmediate() - expect(rows).toEqual([{ value: 7 }]) - await database.query('ROLLBACK') - expect(await database.query('SELECT value FROM queue_owner')).toEqual([]) - await database.close() - } finally { - capture.mockRestore() - if (connection?.isOpen) connection.close() - rmSync(dataDir, { recursive: true, force: true }) - } -}) diff --git a/cloud/packages/cloud-database/package.json b/cloud/packages/cloud-database/package.json index 206fd4f6f70..d51b8fb517b 100644 --- a/cloud/packages/cloud-database/package.json +++ b/cloud/packages/cloud-database/package.json @@ -1,10 +1,20 @@ { "name": "@orca-cloud/cloud-database", - "private": true, "version": "0.0.0", + "private": true, "type": "module", "main": "dist/index.js", "types": "dist/index.d.ts", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + }, + "./cloud-database": { + "types": "./dist/cloud-database.d.ts", + "default": "./dist/cloud-database.js" + } + }, "scripts": { "build": "tsc -p tsconfig.build.json", "clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\"", diff --git a/cloud/packages/push-contract/package.json b/cloud/packages/push-contract/package.json index 558f8e970f3..29484abca8d 100644 --- a/cloud/packages/push-contract/package.json +++ b/cloud/packages/push-contract/package.json @@ -1,10 +1,40 @@ { "name": "@orca-cloud/push-contract", - "private": true, "version": "0.0.0", + "private": true, "type": "module", "main": "dist/index.js", "types": "dist/index.d.ts", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + }, + "./device-registration-messages": { + "types": "./dist/device-registration-messages.d.ts", + "default": "./dist/device-registration-messages.js" + }, + "./host-auth-messages": { + "types": "./dist/host-auth-messages.d.ts", + "default": "./dist/host-auth-messages.js" + }, + "./push-host-proof-transcript": { + "types": "./dist/push-host-proof-transcript.d.ts", + "default": "./dist/push-host-proof-transcript.js" + }, + "./push-limits": { + "types": "./dist/push-limits.d.ts", + "default": "./dist/push-limits.js" + }, + "./send-messages": { + "types": "./dist/send-messages.d.ts", + "default": "./dist/send-messages.js" + }, + "./wire-scalars": { + "types": "./dist/wire-scalars.d.ts", + "default": "./dist/wire-scalars.js" + } + }, "scripts": { "build": "pnpm clean && tsc -p tsconfig.build.json", "clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\"",