Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions .baseline/product/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ and how to operate it is in
|---|---|
| The page and the uploader | `apps/web/app/page.tsx`, `apps/web/components/uploader.tsx` |
| The site's name and source link | `apps/web/app/site.ts` |
| Upload (PUT, image/* ≤ 50MB, random suffix) and delivery (GET, private) | `apps/web/app/api/upload/route.ts`, `apps/web/app/api/blob/route.ts` |
| The browser upload helper and the bucket's checks | `apps/web/lib/blob/client.ts`, `apps/web/lib/blob/server.ts` |
| Upload (PUT, PNG/JPEG/GIF/WebP/AVIF ≤ 50MB — no SVG — random suffix) and delivery (GET, private, sandboxed) | `apps/web/app/api/upload/route.ts`, `apps/web/app/api/blob/route.ts` |
| The browser upload helper, the bucket's checks, and the upload types they share | `apps/web/lib/blob/client.ts`, `apps/web/lib/blob/server.ts`, `apps/web/lib/blob/content-types.ts` |
| Workers build, bindings and deploy contract | `apps/web/next.config.mjs`, `open-next.config.ts`, `wrangler.template.jsonc` + `wiring.fixture.json` (→ `wrangler.jsonc`), `component.yaml` |
| The R2 bucket (stage, prod) | `infra/terraform/cloudflare-r2` |
| Tests | `tests/web` (vitest: both routes against an in-memory bucket) |
Expand All @@ -51,11 +51,13 @@ repository it links to in `apps/web/app/site.ts`.
## How uploads work

The browser PUTs the file to `/api/upload?pathname=<name>` with progress;
the route accepts images only, up to 50MB, stores the file in the bucket
under the name plus a random suffix, and answers
`{ pathname, contentType, size }`. `/api/blob?pathname=<stored name>`
streams it back with its content type. The bucket is private: nothing is
served from it except through that route.
the route accepts raster images only — PNG, JPEG, GIF, WebP, AVIF; not SVG,
which can carry script — up to 50MB, stores the file in the bucket under the
name plus a random suffix, and answers `{ pathname, contentType, size }`.
`/api/blob?pathname=<stored name>` streams it back with its content type,
`nosniff`, and `Content-Security-Policy: default-src 'none'; sandbox`, so
nothing it serves can run script on the site's origin. The bucket is
private: nothing is served from it except through that route.

There is no storage key to configure. The Worker binds the bucket as
`BLOB`, and the binding is the credential. Its name is never hand-written:
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/baseline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ jobs:
echo "── $t"
bash "testing/$t.test.sh"
done
# Every component's docs/{overview,architecture,runbook}.md is generated
# from what the component declares (its manifest, Worker config,
# Terraform, migrations, package scripts). A pull request that changes
# one of those facts without regenerating the pages fails here.
- name: Component docs match their manifests
run: python3 tooling/catalog/gen-component-docs.py --check

# Tier 1. THE BLUEPRINT, READ AND PLACED BY THE BINARY A BOOTSTRAP RUNS.
#
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ bucket names. None of this repository's machinery ships.
|---|---|
| The page and the uploader | `apps/web/app/page.tsx`, `apps/web/components/uploader.tsx` |
| The site's name and source link | `apps/web/app/site.ts` |
| Upload (PUT, image/* ≤ 50MB, random suffix) and delivery (GET, private) | `apps/web/app/api/upload/route.ts`, `apps/web/app/api/blob/route.ts` |
| The two halves of what `@vercel/blob` did | `apps/web/lib/blob/client.ts`, `apps/web/lib/blob/server.ts` |
| Upload (PUT, PNG/JPEG/GIF/WebP/AVIF ≤ 50MB — no SVG — random suffix) and delivery (GET, private, sandboxed) | `apps/web/app/api/upload/route.ts`, `apps/web/app/api/blob/route.ts` |
| The two halves of what `@vercel/blob` did, and the upload types they share | `apps/web/lib/blob/client.ts`, `apps/web/lib/blob/server.ts`, `apps/web/lib/blob/content-types.ts` |
| Workers build, bindings and deploy contract | `apps/web/next.config.mjs`, `open-next.config.ts`, `wrangler.template.jsonc` + `wiring.fixture.json` (→ the committed `wrangler.jsonc`), `component.yaml` |
| The R2 bucket (stage, prod) | `infra/terraform/cloudflare-r2` |
| The wrangler renderer | `tooling/wire` |
Expand Down Expand Up @@ -93,8 +93,8 @@ pnpm --filter @app/web preview # wrangler dev over the built Worker, same loc
|---|---|
| Next.js hosting | One Cloudflare Worker (`.open-next/worker.js`) + Static Assets |
| Vercel Blob (`BLOB_READ_WRITE_TOKEN`) | R2 bucket bound as `BLOB`; the binding is the credential |
| `upload()` client token handshake + direct upload | `PUT /api/upload?pathname=` through the Worker, with progress; same checks, same result shape |
| `GET /api/blob` private delivery | Same route, streaming from R2 |
| `upload()` client token handshake + direct upload | `PUT /api/upload?pathname=` through the Worker, with progress; the same checks, with images narrowed to raster formats (an SVG can carry script); same result shape |
| `GET /api/blob` private delivery | Same route, streaming from R2, under `nosniff` and a CSP that sandboxes what it serves |
| A store created in the dashboard | `infra/terraform/cloudflare-r2`, applied by Orun before the Worker deploys |
| Deploy on push | Orun applies the bucket and deploys `<repo>-web-{stage,prod}` on merge to `main` (`apps/web/component.yaml`) |
| "Deploy with Vercel" (clone the template) | `orun new --blueprint repo-blueprint.yaml` — a new repository, branded, deployed |
Expand Down
44 changes: 44 additions & 0 deletions ai/context/current.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# blob-starter — where this repository stands

The entry page for anyone (or any agent) picking this repository up. It is
this repository's own state and does not ship to a product built from it;
the two pages that do are [operations.md](operations.md) and
[deployment.md](deployment.md).

## What it is

Vercel's Blob Next.js Starter — an image uploader with a private delivery
route — on Cloudflare Workers + Static Assets, with the files in an R2 bucket
Terraform provisions, packaged as an Orun baseline: `orun new --blueprint
repo-blueprint.yaml` builds a new product in its own repository and takes it
to live on stage and prod in one landing (`01-product`: the bucket applied,
then the Worker deployed), then records what went live (`02-docs`).

## Release

| | |
|---|---|
| Latest tag | `baseline-v1` (BL4) |
| On `main` since | BL5 — the cirrus standard (no kiox, generated component docs, this context pack) and SVG uploads closed (risk BL-L) |
| Proven live | No. Everything short of the platform is proven offline in Baseline CI, an upload round-tripped through the built Worker included; a live bootstrap from a tag is still to come (risk BL-G) |

`baseline-v1` accepts any `image/*` upload, SVG included, and serves it from
the product's own origin (BL-L). A product built from it needs BL5's change
to `apps/web/lib/blob`, the delivery route and the uploader.

## Where to read

| Question | Page |
|---|---|
| What shipped, and every departure from upstream | `specs/epics/blob-starter/IMPLEMENTATION-STATUS.md` |
| Why it is built this way | [decisions.md](decisions.md), then `specs/epics/blob-starter/design.md` |
| What could still go wrong | [open-risks.md](open-risks.md), then `specs/epics/blob-starter/risks-and-open-questions.md` |
| How to instantiate it | `BOOTSTRAP.md`, `docs/phases/` |
| What each component is | `apps/web/docs/`, `tests/web/docs/`, `infra/terraform/cloudflare-r2/docs/` (generated; `python3 tooling/catalog/gen-component-docs.py`) |

## Next

1. Cut the next `baseline-vN` with `tag.yml`, so the registry can pin BL5 and
no new product starts with SVG uploads open.
2. Bootstrap one product from that tag against a workspace whose Cloudflare
account has R2 enabled (BL-J), and record its timings (closes BL-G).
20 changes: 20 additions & 0 deletions ai/context/decisions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# blob-starter — decisions

The choices that shape this repository, one line each, with where the full
reasoning lives. Superseded decisions stay listed, marked, so the history
reads in order.

| # | Decision | Why | Source |
|---|---|---|---|
| 1 | The upload goes through the Worker (`PUT /api/upload`, buffered, 50MB max), not a presigned R2 URL | One credential (the binding) and the template's checks stay on the server; presigned URLs need an S3 key pair the baseline would have to broker | `specs/epics/blob-starter/design.md` §2, risk BL-A |
| 2 | Delivery stays a private route (`GET /api/blob?pathname=`), as upstream | The bucket is not public; nothing is served from it but through the Worker | design.md §2, epic README |
| 3 | The `BLOB` binding is the storage credential; no storage key | Replaces `BLOB_READ_WRITE_TOKEN`: a product stores and serves files from its first deploy with nothing but the Cloudflare connection | IMPLEMENTATION-STATUS (BL4) |
| 4 | `wrangler.jsonc` is rendered from the R2 root's wiring document per environment, and the fixture render is committed | The deploy lane checks the config exists before the build; Checks diffs the committed render | risk BL-E |
| 5 | dev renders the fixture; stage and prod refuse without the real document | No root applies in dev, which only verifies | risk BL-F |
| 6 | The R2 root applies with its own `r2-data` key (`CLOUDFLARE_R2_TOKEN`) | The `workers-deploy` token cannot touch R2 | risk BL-H |
| 7 | One landing (`01-product`) then the record (`02-docs`); `apps/web` `dependsOn: cloudflare-r2`, enforced on main, advisory on a pull request | The plan, not a landing boundary, orders the bucket before the Worker (cirrus-baseline since baseline-v22) | design.md §7 |
| 8 | `ci.yml` plans nothing while `intent.yaml` names `ws_CHANGE_ME` | A bootstrap cannot set a repository variable (replaced the `ORUN_CI` gate) | IMPLEMENTATION-STATUS (BL4) |
| 9 | stack-tectonic 0.19.0, not 0.20.0 | Next 16's Turbopack refuses 0.20.0's out-of-tree install | IMPLEMENTATION-STATUS (BL4), design.md §7 |
| 10 | Uploads are raster images only (PNG, JPEG, GIF, WebP, AVIF), one list shared by the route and the uploader; the delivery route serves every object under `default-src 'none'; sandbox` | `image/*` admitted SVG, which the route served from the app's origin as script — a stored XSS | risk BL-L, IMPLEMENTATION-STATUS (BL5) |
| 11 | orun is pinned only where it runs (the `orun-action` `version:` in each workflow); no kiox | `kiox.yaml` pinned a version (v2.15.0) nothing ran | IMPLEMENTATION-STATUS (BL5) |
| 12 | Component docs are generated from the manifests and checked in CI | A hand-written copy of the dependency graph and bindings drifts silently | `tooling/catalog/gen-component-docs.py` |
17 changes: 17 additions & 0 deletions ai/context/open-risks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# blob-starter — open risks

What is not yet proven or not yet decided. The full write-up of every risk,
including the accepted and resolved ones, is
`specs/epics/blob-starter/risks-and-open-questions.md`; this page lists only
what is still live, so it should be short.

| ID | Risk | State | What closes it |
|---|---|---|---|
| BL-G | No live bootstrap yet: the repository created and linked, the two tokens minted, a real `terraform apply` of the bucket, the published wiring document, a real `wrangler deploy` and the probes are all unproven | OPEN | One bootstrap from a tag against a workspace whose Cloudflare account has R2 enabled |

Accepted, and worth knowing before you change them: BL-A (the upload is
buffered through the Worker), BL-C (anonymous uploads, as upstream),
BL-I (the bootstrap floor plans every edge enforced), BL-J (R2 is
switched on per account by its owner), BL-K (a custom domain is the owner's
step). Resolved in BL5: BL-L (SVG uploads), for products built from a later
tag than `baseline-v1`.
7 changes: 4 additions & 3 deletions ai/context/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ component-native desired-state repo.
publishes a wiring document — `{r2_bucket_name}` — as the Orun secret
`WIRING_CLOUDFLARE_R2` on that environment.
- **`apps/web`** — the uploader: Next.js built by `@opennextjs/cloudflare`
into one Worker plus Static Assets. `PUT /api/upload?pathname=` accepts an
image (≤ 50MB) and stores it in the `BLOB` R2 binding under the name plus
a random suffix; `GET /api/blob?pathname=` streams it back. The page
into one Worker plus Static Assets. `PUT /api/upload?pathname=` accepts a
raster image (PNG, JPEG, GIF, WebP, AVIF; ≤ 50MB) and stores it in the
`BLOB` R2 binding under the name plus a random suffix; `GET
/api/blob?pathname=` streams it back, sandboxed by its CSP. The page
renders per request (its metadata takes the request's origin).
- **`tests/web`** — both routes against an in-memory bucket (vitest),
verified on the `dev` rung.
Expand Down
12 changes: 8 additions & 4 deletions apps/web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ running on Cloudflare Workers + Static Assets with the files in a
**Cloudflare R2** bucket instead of Vercel Blob:

- `app/page.tsx` — the page around `<Uploader />`
- `components/uploader.tsx` — pick or drop an image (image/* only, 50MB
max), preview, upload with progress, a toast linking the stored file
- `components/uploader.tsx` — pick or drop an image (PNG, JPEG, GIF, WebP
or AVIF, 50MB max), preview, upload with progress, a toast linking the
stored file
- `app/api/upload/route.ts` — `PUT ?pathname=`: the same checks again on the
server, then `BLOB.put` under the name plus a random suffix; answers
`{ pathname, contentType, size }`
- `app/api/blob/route.ts` — `GET ?pathname=`: streams the object with its
content type, or the route's own `Not found` 404
content type, `nosniff` and a CSP that sandboxes it, or the route's own
`Not found` 404
- `lib/blob/client.ts`, `lib/blob/server.ts` — the two halves of what
`@vercel/blob` did
`@vercel/blob` did; `lib/blob/content-types.ts` — the raster types both
accept (no SVG: it can carry script, and the route serves from this
origin)
- `app/site.ts` — the site's name and the repository its links point at

```bash
Expand Down
16 changes: 14 additions & 2 deletions apps/web/app/api/blob/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@ import { type NextRequest, NextResponse } from 'next/server'
import { bucket } from '@/lib/blob/server'

// Delivery route for private blobs.
// The R2 bucket is not public; this route streams the object after
// authenticating the request.
// The R2 bucket is not public; this route is the only way to read it, and it
// streams the object to anyone who asks for its stored name.
//
// ⚠️ Authenticate and authorize users before streaming the object.
// Otherwise, anyone with the name can read it — nothing here checks who asks.
//
// What it serves comes from this app's own origin, so the response is locked
// down whatever the bucket holds: `nosniff` holds the browser to the stored
// content type, and the CSP gives no source to load from and sandboxes the
// object (no script, an opaque origin) — so even a file that is a document,
// such as an SVG stored before the upload route refused them, cannot run.
const CONTENT_SECURITY_POLICY = "default-src 'none'; sandbox"

export async function GET(request: NextRequest) {
// ⚠️ Add your own authentication here.
// For example: await authRequest(request)
Expand All @@ -27,6 +38,7 @@ export async function GET(request: NextRequest) {
'Content-Length': String(object.size),
ETag: object.httpEtag,
'X-Content-Type-Options': 'nosniff',
'Content-Security-Policy': CONTENT_SECURITY_POLICY,
},
})
}
10 changes: 7 additions & 3 deletions apps/web/app/api/upload/route.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { NextResponse } from 'next/server'
import {
MAX_UPLOAD_BYTES,
NOT_ALLOWED_MESSAGE,
bucket,
isAllowedContentType,
putBlob,
} from '@/lib/blob/server'

// Receives the browser's upload and stores it in R2. This is the Worker's
// front door for uploads (Blob's `handleUpload` minted a client token for a
// direct upload instead): the same checks the template declared —
// image/* only, 50MB max, a random suffix on the name.
// direct upload instead): the checks the template declared — images only,
// 50MB max, a random suffix on the name — with "images" narrowed to raster
// formats (PNG, JPEG, GIF, WebP, AVIF). The template's `image/*` admitted
// SVG, which the delivery route would serve from this origin as a document
// that can run script.
//
// ⚠️ Authenticate and authorize users before accepting the upload.
// Otherwise, you're allowing anonymous uploads.
Expand All @@ -23,7 +27,7 @@ export async function PUT(request: Request): Promise<NextResponse> {
return NextResponse.json({ error: 'Missing pathname' }, { status: 400 })
}
if (!isAllowedContentType(contentType)) {
return NextResponse.json({ error: 'We only accept image files' }, { status: 400 })
return NextResponse.json({ error: NOT_ALLOWED_MESSAGE }, { status: 400 })
}
if (length > MAX_UPLOAD_BYTES) {
return NextResponse.json({ error: 'File size too big (max 50MB)' }, { status: 400 })
Expand Down
13 changes: 9 additions & 4 deletions apps/web/components/uploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import { useState, type FormEvent } from 'react'
import toast from 'react-hot-toast'
import { upload } from '@/lib/blob/client'
import {
ALLOWED_CONTENT_TYPES,
NOT_ALLOWED_MESSAGE,
isAllowedContentType,
upload,
} from '@/lib/blob/client'
import ProgressBar from './progress-bar'

export default function Uploader() {
Expand Down Expand Up @@ -41,7 +46,7 @@
const deliveryUrl = `/api/blob?pathname=${encodeURIComponent(blob.pathname)}`

toast(
(t: { id: string }) => (

Check warning on line 49 in apps/web/components/uploader.tsx

View workflow job for this annotation

GitHub Actions / checks

't' is defined but never used
<div className="relative">
<div className="p-2">
<p className="font-semibold text-gray-900">File uploaded!</p>
Expand Down Expand Up @@ -76,8 +81,8 @@
function handleFileChange(file: File) {
toast.dismiss()

if (file.type.split('/')[0] !== 'image') {
toast.error('We only accept image files')
if (!isAllowedContentType(file.type)) {
toast.error(NOT_ALLOWED_MESSAGE)
return
}

Expand Down Expand Up @@ -178,7 +183,7 @@
id="image-upload"
name="image"
type="file"
accept="image/*"
accept={ALLOWED_CONTENT_TYPES.join(',')}
className="sr-only"
onChange={(event) => {
const file = event.currentTarget?.files?.[0]
Expand Down
27 changes: 27 additions & 0 deletions apps/web/docs/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# web — architecture

Built by the turbo pipeline from `apps/web` and deployed with wrangler.

| | |
| ------------------- | ----------------------------------------------- |
| Worker config | `apps/web/wrangler.template.jsonc` |
| Entry | `.open-next/worker.js` |
| Compatibility date | `2026-10-01` |
| Compatibility flags | `nodejs_compat`, `global_fetch_strictly_public` |

## Bindings

| Binding | Kind | Source |
| -------- | ------------- | ------------------------------------- |
| `BLOB` | R2 bucket | `r2_bucket_name` from `cloudflare-r2` |
| `ASSETS` | Static assets | `.open-next/assets` |

Identifiers marked _from_ a component are filled in at deploy time from
that component's published outputs for the environment being deployed.

## Wiring

Resolved at deploy time from secrets the infrastructure lanes publish (names only):

- `WIRING_CLOUDFLARE_R2_PROD`
- `WIRING_CLOUDFLARE_R2_STAGE`
20 changes: 20 additions & 0 deletions apps/web/docs/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# web

The image uploader (Next.js on Cloudflare Workers + Static Assets via @opennextjs/cloudflare, per environment)

A Cloudflare Worker with Static Assets (server rendering plus static files), deployed per environment by its CI lane.

| | |
| ------------ | ------------------------------------------------------------------------------------------------------------------ |
| Type | `cloudflare-workers-assets-turbo` |
| Source | `apps/web` |
| Package | `@app/web` |
| Environments | `dev`: `verify` · `stage`: `verify`, `deploy` on a push to `main` · `prod`: `verify`, `deploy` on a push to `main` |

## Depends on

- **cloudflare-r2** — Provisions the Cloudflare R2 bucket the app binds (stage and prod)

## Depended on by

- (none)
Loading
Loading