Joel Stevick

Ingestion · INGEST 1 · stage 1 of 7

Validate the upload

Before anything is written, upload() parses the JSON body and checks the name, the page count and the pages array. It keeps the pages whose number is in range and whose trimmed text is not empty, lists every other page from 1 to the page count as skipped, and totals the characters. INGEST 2 receives name, pageCount, skipped and chars; INGEST 4 receives the kept pages.

Runs in: Demo Worker, the private Worker behind the DEMO service binding, demo/src/. New to Cloudflare? How these fit together
demo/src/app.ts27 / 27 lines on this page explainedlines 265-291 of 1124; the rest are on other stage pages
In the browser
runs in the visitor's browser, on the demo page
Ingestion
runs when a PDF's text is uploaded
Query time
runs when the Worker answers a question or a search_documents call from the MCP server, and in the request handling every route shares, in the Worker and in the site's functions, which uploads and removals pass through too
MCP request
runs on a request to the MCP server or its OAuth endpoints
Site request
runs on a site request that is not a question, an upload, an MCP call or the cron: a page load, a session read, the demo page's /api/grants calls, an admin read, a file's removal
Scheduled
runs in the Worker's daily cron job
Eval run
runs when the eval harness runs on a developer's machine
MCP host
runs in an MCP host outside the Worker: the search loop example, on a developer's machine (npm run search) or in its test
Module load
runs once, when the module is loaded
Type only
erased by the TypeScript compiler; never runs
Never runs
a comment, a licence or markup with no behaviour of its own

Pinned to joelstevick-demo-rag-pipeline at 6a9c6c7.

demo/src/app.ts The demo Worker.

upload(): INGEST 1

segment · lines 265–291View on GitHub

Checks 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 } | null

Parse 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 }[]) : null

The 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