upload(): INGEST 1
segment · lines 265–291View on GitHubChecks the body, then derives the kept pages, the skipped pages and the character count.
Calls json() Shared helpers
// INGEST 1 · validate the upload: name, page count, each page's text // The body the page sent; a body that is not JSON reads as null. const body = (await request.json().catch(() => null)) as { name?: unknown; page_count?: unknown; pages?: unknown } | nullParse the body Ingestion
WhatThe stage marker, then request.json(). A body that is not JSON reads as null, and every field is typed unknown until it is checked.
WhyThe browser sends the body, but so can anyone holding a visitor's access key (the random key in the demo cookie, which opens that visitor's demo account), so its shape is not trusted. Typing each field as unknown forces a check before the field is used.
// The name is trimmed and capped at 200 characters; the page count is the PDF's own, as the browser read it. const name = typeof body?.name === 'string' ? body.name.trim().slice(0, 200) : '' const pageCount = Number(body?.page_count)Name and page count Ingestion
WhatThe name must be a string; it is trimmed and cut to 200 characters. page_count goes through Number(), so a numeric string is also accepted.
WhyThe name is the file's key within the demo account (INGEST 2) and is shown on the page, so its length is bounded. pageCount is the PDF's own page count as pdf.js read it in the browser, blank pages included. The 300-page limit (INGEST 3) counts pageCount, so blank pages count toward it too.
name: at most 200 charactersLIMITS.pages = 300
// The pages as the browser sent them, or null if `pages` is not an array. const raw = Array.isArray(body?.pages) ? (body.pages as { page?: unknown; text?: unknown }[]) : nullThe pages, if an array Ingestion
Whatraw is body.pages when it is an array, else null.
WhyThe code does not check that each element is an object. A null element makes the page filter at line 281 throw a TypeError, which createApp's fetch answers with 500; no row has been written at that point. raw is null only when pages is not an array, so an empty array is accepted.
// A missing name, a bad page count or no pages array is refused before a row is written; bad pages are filtered below. if (name === '' || !Number.isInteger(pageCount) || pageCount < 1 || raw === null) return json(400, { error: 'That upload could not be read.' })Refuse a malformed upload Ingestion
WhatAn empty name, a page count that is not an integer of at least 1, or a pages field that is not an array is answered 400 "That upload could not be read."
WhyNothing has been written yet, so this refusal needs no undo. An empty pages array passes: a PDF with no text layer is stored with every page skipped and no chunks, and it still counts toward the demo account's files and pages. Bad pages are filtered at line 281 instead of refused here, although a null element makes that filter throw.
// Only pages with text count; the empty ones (scans, blank pages) are listed as `skipped` so the visitor sees them. const pages = raw .filter((p) => Number.isInteger(p.page) && (p.page as number) >= 1 && (p.page as number) <= pageCount && typeof p.text === 'string') .map((p) => ({ page: p.page as number, text: (p.text as string).trim() })) .filter((p) => p.text.length > 0)Pages with text Ingestion
WhatA page is kept when its number is an integer from 1 to pageCount and its text is a string. The text is trimmed, and pages left empty are dropped.
WhyA page number out of range would produce a citation to a page the PDF does not have, so such pages are dropped without an error. Duplicate page numbers are not removed, because the page never sends them.
// Every page from 1 to the page count that has no text is listed as skipped. const withText = new Set(pages.map((p) => p.page)) const skipped = Array.from({ length: pageCount }, (_, i) => i + 1).filter((page) => !withText.has(page))Skipped pages Ingestion
WhatEvery page number from 1 to pageCount that is not among the kept pages goes into skipped, in order.
WhyThe browser does not send empty pages, so the Worker derives them from the count. skipped is stored with the file row (INGEST 2), returned in the reply, and listed on the page as "skipped, no text". The array is built from the client's page_count before INGEST 3 checks the 300-page limit. A forged count of hundreds of millions exhausts the Worker's memory here, and a count of 2^32 or more throws RangeError: Invalid array length, which createApp's fetch answers with 500; either way the upload fails before INGEST 3 can answer 413.
// The total length of the text is what the demo's character limit counts. const chars = pages.reduce((sum, p) => sum + p.text.length, 0)Total characters Ingestion
Whatchars is the sum of the kept pages' trimmed text lengths.
WhyThe demo account's 500,000 limit (INGEST 3) counts chars. JavaScript's string length counts UTF-16 code units, which differ from both bytes and characters: a character outside the Basic Multilingual Plane, such as most emoji, counts 2. The page's notice and INGEST 3's 413 message both call the limit 500 KB.
LIMITS.chars = 500_000