feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes
This commit is contained in:
parent
01ef3293cf
commit
66b6b45ba9
399 changed files with 17828 additions and 11330 deletions
|
|
@ -1,6 +1,8 @@
|
|||
import { isRecord } from '../_shared/core-contract.generated.ts'
|
||||
import { createClient } from '@supabase/supabase-js'
|
||||
import { requireUser } from '../_shared/auth.ts'
|
||||
import { corsHeaders, handleCorsPreflightRequest } from '../_shared/cors.ts'
|
||||
import { handleCorsPreflightRequest } from '../_shared/cors.ts'
|
||||
import { createJsonResponder, JsonBodyError, readJsonBody } from '../_shared/json-response.ts'
|
||||
import {
|
||||
buildPushNotification,
|
||||
getFcmAccessToken,
|
||||
|
|
@ -21,15 +23,10 @@ import { readWebPushConfig, sendWebPushMessage } from '../_shared/webpush.ts'
|
|||
import { createServiceRoleClient } from '../_shared/quota.ts'
|
||||
import { presentDispatchOutcome } from './dispatch-response.ts'
|
||||
|
||||
const JSON_HEADERS = {
|
||||
...corsHeaders,
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
Pragma: 'no-cache',
|
||||
}
|
||||
const REQUEST_LIMIT_BYTES = 4 * 1024
|
||||
const SEND_CONCURRENCY = 10
|
||||
const DRAIN_CONCURRENCY = 4
|
||||
const jsonResponse = createJsonResponder({ noStore: true, pragmaNoCache: true })
|
||||
|
||||
type DispatchActor =
|
||||
| { kind: 'user'; userId: string }
|
||||
|
|
@ -82,14 +79,6 @@ interface ProcessResult extends DispatchSummary {
|
|||
transientStatus: number | null
|
||||
}
|
||||
|
||||
function jsonResponse(body: Record<string, unknown>, status = 200): Response {
|
||||
return new Response(JSON.stringify(body), { status, headers: JSON_HEADERS })
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
||||
}
|
||||
|
||||
function readEndpointMode(url: URL): { mode: 'dispatch' } | { mode: 'drain'; batchLimit: number } {
|
||||
const entries = [...url.searchParams.entries()]
|
||||
if (entries.length === 0) return { mode: 'dispatch' }
|
||||
|
|
@ -110,21 +99,13 @@ function readEndpointMode(url: URL): { mode: 'dispatch' } | { mode: 'drain'; bat
|
|||
}
|
||||
|
||||
async function readJson(req: Request): Promise<unknown> {
|
||||
if (req.headers.get('Content-Type')?.split(';', 1)[0].trim().toLowerCase() !== 'application/json') {
|
||||
throw new PushContractError('unsupported_media_type', 415)
|
||||
}
|
||||
const declaredLength = Number(req.headers.get('Content-Length') ?? '0')
|
||||
if (Number.isFinite(declaredLength) && declaredLength > REQUEST_LIMIT_BYTES) {
|
||||
throw new PushContractError('request_too_large', 413)
|
||||
}
|
||||
const text = await req.text()
|
||||
if (new TextEncoder().encode(text).byteLength > REQUEST_LIMIT_BYTES) {
|
||||
throw new PushContractError('request_too_large', 413)
|
||||
}
|
||||
try {
|
||||
return JSON.parse(text)
|
||||
} catch {
|
||||
throw new PushContractError('invalid_json', 400)
|
||||
return await readJsonBody(req, { maxBytes: REQUEST_LIMIT_BYTES, requireContentType: true })
|
||||
} catch (error) {
|
||||
if (error instanceof JsonBodyError) {
|
||||
throw new PushContractError(error.code, error.status)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue