refactor(push): organize provider, delivery, and host authentication owners

This commit is contained in:
Neil
2026-11-11 13:48:40 -08:00
parent 1708f1caee
commit cf28ffccb6
68 changed files with 264 additions and 1311 deletions
@@ -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<typeof import('node:http2')>()),
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<typeof vi.fn>
close: ReturnType<typeof vi.fn>
}
> = []
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()
})
+1 -1
View File
@@ -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
@@ -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 {
@@ -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<OpenCloudDatabaseInput, 'service' | 'schemaStatements'>
): Promise<CloudDatabase> {
return await openCloudDatabase({ ...input, service: 'push', schemaStatements: pushSchemaStatements() })
return await openCloudDatabase({
...input,
service: 'push',
schemaStatements: pushSchemaStatements()
})
}
export async function openInMemoryPushDatabase(): Promise<CloudDatabase> {
@@ -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))
@@ -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 }) => {
@@ -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 =
@@ -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')
@@ -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.
@@ -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<void>((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({
@@ -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'
@@ -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)
@@ -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
@@ -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(
@@ -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<void>((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<unknown> | undefined
let deletion: Promise<boolean> | 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()
}
}
)
@@ -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}`))
@@ -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:'
@@ -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<void>
): Promise<string | null> {
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<void>((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<void>((resolve) => (scanned = resolve))
let release!: () => void
const released = new Promise<void>((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()
}
)
@@ -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<string, Uint8Array> | 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')
@@ -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'
})
})
})
@@ -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
@@ -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.
@@ -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,
@@ -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)
@@ -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
@@ -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 }])
})
})
@@ -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++) {
@@ -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
@@ -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 () => {
@@ -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
@@ -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<ReturnType<typeof createPushServerHarness>>[] = []
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' }] })
}
)
@@ -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', () => {
@@ -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)
@@ -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 () => {
@@ -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,
@@ -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<void>)[] = []
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)
})
+1 -1
View File
@@ -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()
@@ -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.
@@ -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)
})
@@ -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<ApnsEnvironment, string> = {
production: 'api.push.apple.com',
@@ -8,7 +8,6 @@ export type ApnsRequest = {
body: string
}
export type { ApnsResponse }
export type ApnsTransport = (request: ApnsRequest) => Promise<ApnsResponse>
// Cache APNs HTTP/2 sessions per host until socket loss; long-lived connections avoid reconnect overhead.
@@ -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')
})
})
@@ -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 }
@@ -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()
@@ -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)
})
@@ -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'
+5 -4
View File
@@ -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<PushPruneSweep>, intervalMs: nu
}
// One DELETE under a statement timeout: there is no batch budget for it to exhaust.
const unbatchedSweep =
(run: () => Promise<number>) =>
async (): Promise<PushPruneSweep> => ({ deleted: await run(), saturated: false })
const unbatchedSweep = (run: () => Promise<number>) => async (): Promise<PushPruneSweep> => ({
deleted: await run(),
saturated: false
})
export function startPushBackground(
config: Pick<PushConfig, 'mode'>,
@@ -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<Record<string, unknown>>,
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<string, unknown>) {
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<void> {
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()
})
})
@@ -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 () => {
@@ -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')
})
@@ -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')
})
@@ -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<void>)[] = []
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')
}
)
@@ -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<void>)[] = []
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<void>()
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()
}
)
+32 -22
View File
@@ -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
@@ -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<void>) | 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()
}
}
)
@@ -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 })
}
})
+11 -1
View File
@@ -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 })\"",
+31 -1
View File
@@ -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 })\"",