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,5 +1,6 @@
|
|||
import { corsHeaders, handleCorsPreflightRequest } from '../_shared/cors.ts'
|
||||
import { handleCorsPreflightRequest } from '../_shared/cors.ts'
|
||||
import { requireUser, type AuthError } from '../_shared/auth.ts'
|
||||
import { createJsonResponder, JsonBodyError, readJsonBody } from '../_shared/json-response.ts'
|
||||
import { createServiceRoleClient } from '../_shared/quota.ts'
|
||||
import {
|
||||
ContentReportRequestError,
|
||||
|
|
@ -11,35 +12,20 @@ import {
|
|||
|
||||
const MAX_REQUEST_BYTES = 25_000
|
||||
|
||||
function jsonResponse(body: unknown, status: number): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
Pragma: 'no-cache',
|
||||
},
|
||||
})
|
||||
}
|
||||
const jsonResponse = createJsonResponder({ noStore: true, pragmaNoCache: true })
|
||||
|
||||
async function readRequestJson(req: Request): Promise<unknown> {
|
||||
const contentType = req.headers.get('content-type')?.toLowerCase() ?? ''
|
||||
if (contentType.split(';', 1)[0].trim() !== 'application/json') {
|
||||
throw new ContentReportRequestError('content_type_must_be_json', 415)
|
||||
}
|
||||
const declaredLength = Number(req.headers.get('content-length'))
|
||||
if (Number.isFinite(declaredLength) && declaredLength > MAX_REQUEST_BYTES) {
|
||||
throw new ContentReportRequestError('request_too_large', 413)
|
||||
}
|
||||
const rawBody = await req.text()
|
||||
if (new TextEncoder().encode(rawBody).byteLength > MAX_REQUEST_BYTES) {
|
||||
throw new ContentReportRequestError('request_too_large', 413)
|
||||
}
|
||||
try {
|
||||
return JSON.parse(rawBody) as unknown
|
||||
} catch {
|
||||
throw new ContentReportRequestError('invalid_json')
|
||||
return await readJsonBody(req, {
|
||||
maxBytes: MAX_REQUEST_BYTES,
|
||||
requireContentType: true,
|
||||
contentTypeErrorCode: 'content_type_must_be_json',
|
||||
})
|
||||
} catch (error) {
|
||||
if (error instanceof JsonBodyError) {
|
||||
throw new ContentReportRequestError(error.code, error.status)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue