Skip to content

Byte-range requests, so media can be seeked - #17

Merged
fgatti675 merged 8 commits into
mainfrom
feat/storage-range-requests
Aug 22, 2026
Merged

fgatti675 merged 8 commits into
mainfrom
feat/storage-range-requests

Conversation

@fgatti675

Copy link
Copy Markdown
Member

Completes the delivery half of #7. Stacked on feat/storage-cacheable-delivery
(#13)
— that's the base branch, so this diff shows only the range work. Merge
#13 first.

I deferred this in #13 and said so; here it is.

Why it isn't a nicety

Without Accept-Ranges a browser won't offer to seek in an audio or video
element served from here — it has no way to ask for the middle of a file, so
scrubbing refetches from the start and every seek pays for the whole object
again. Safari goes further and refuses to play a <video> whose first response
isn't a 206. For media this was the difference between a working player and a
broken one.

What it does

Both plain paths advertise Accept-Ranges: bytes and answer a Range with 206
and a Content-Range. Conditional requests still win — a request carrying both
If-None-Match and Range gets the 304, the order RFC 9110 requires.

The parser is deliberately conservative. Malformed, multi-range, or a unit
other than bytes all resolve to "serve the whole object": a range request
answered with 200 is always legal, while a range answered wrongly is a
corrupted download that nothing detects. Multi-range would need a
multipart/byteranges encoder and nothing that matters sends one.

A range starting past the end is a 416 with Content-Range: bytes */<size>,
not a silent whole-file response — a player asking for a byte that doesn't exist
should be told, not handed the file.

Local vs remote, stated plainly

On local storage only the requested slice leaves the disk, via a positioned read.
On S3 and GCS the object is still fetched whole, because StorageController
has no ranged read — so the saving there is the response body, not the upstream
fetch. Worth it anyway (it's the half on the user's connection), but I'm not
going to claim it saves bandwidth it doesn't.

Verification

  • 28 new tests: closed/open/suffix ranges, clamping, single byte, every declined
    form, all four unsatisfiable cases, and the conditional-wins-over-range order
  • Mutation-checked: reading from offset 0 instead of the range start fails
    three of them, so they test the mechanism rather than merely passing
  • packages/server: 147 suites, 2183 passing
  • pnpm typecheck and verify:docs clean

Not verified: an actual browser seeking in a real video. Header and body
semantics are asserted; player behaviour is not.

…blic

Three problems in the served-object headers, one of them a leak.

**`public` on private objects.** Every response said `public, max-age=3600`,
including for an object that had just required credentials to fetch. `public` is
explicit permission for a *shared* cache — a CDN, a corporate proxy — to store
the response and hand it to the next caller. Objects outside the `public/`
prefix are now `private`, and `Vary: Authorization` is sent so a cache that does
key on it cannot merge two callers' answers.

**`immutable` on mutable keys.** The remote path claimed it at an hour and the
transform paths at a year. `immutable` promises the body at this URL will never
change; writing to an existing key is an ordinary operation, so the promise is
false for every object here, and the symptom was an upload that replaced a file
which then did not visibly change until the window lapsed. Gone, everywhere.

**No caching at all on the local path.** It sent no cache headers, so every load
of every image re-read the file and re-sent the body.

All four response paths now carry a weak `ETag` and `Last-Modified` and answer
304 to `If-None-Match` / `If-Modified-Since`. The local path takes them from a
`stat` that replaces the existence-check `access` — one syscall doing two jobs
rather than two doing one each — and the 304 is decided before the file is read,
which is the point.

The validators are weak on purpose: they are size and mtime, not a digest, which
is the trade nginx makes for static files. Hashing every object on every request
would cost more than it saves for bytes this server is already streaming from
somewhere else.

Short max-age plus cheap revalidation replaces the long lie: 60s for objects,
3600s for transforms, `stale-while-revalidate` for public ones so the round trip
is not something a user waits for.

Not included: range requests. Media seeking still fetches whole objects.
The transform cache key named the object but not which version of it —
`source/bucket/path` plus the transform options. Overwriting the source left the
old rendition valid for the cache's full hour, and the HTTP `max-age` sat on top
of that, so a replaced image could stay visibly unchanged for two.

The key now carries the source's validator, so replacing the source produces a
different key rather than shadowing one, and the superseded entry ages out on
its own with no invalidation to get wrong.

Proved by mutation: dropping the version from the key fails the new test.
Without `Accept-Ranges` a browser will not offer to seek in an audio or video
element served from here: it has no way to ask for the middle of a file, so
scrubbing refetches from the start and every seek pays for the whole object
again. Safari goes further and refuses to play a `<video>` whose first response
is not a `206`. For media this was the difference between a player that works and
one that does not.

Both plain paths now advertise `Accept-Ranges: bytes` and answer a `Range` with
`206` and a `Content-Range`. Conditional requests still win — a request carrying
both `If-None-Match` and `Range` gets the 304, the order RFC 9110 requires.

The parser is deliberately conservative. A malformed range, a multi-range header
or a unit other than bytes resolves to "serve the whole object", because a range
request answered with 200 is always legal while a range answered *wrongly* is a
corrupted download that nothing detects. Multi-range would need a
`multipart/byteranges` encoder and nothing that matters sends one.

A range starting past the end is a `416` with `Content-Range: bytes */<size>`,
not a silent whole-file response — a player asking for a byte that does not exist
should be told, not handed the file.

On local storage only the requested slice leaves the disk, via a positioned
read; reading the whole file to answer a range would give up the reason ranges
exist. On S3 and GCS the object is still fetched whole, because a
`StorageController` has no ranged read — the saving there is the response body,
which is the half on the user's connection.

The offset is mutation-checked: reading from 0 rather than the range start fails
three of the new tests.
…r the build

Every pull request has been failing since the portable functions entry point
landed:

    FAIL baas
      backend/functions/hello.ts(7,8): error TS2307:
      Cannot find module '@rebasepro/server/functions'

`check:templates` and `check:eject` compile a *scaffolded project*, which
resolves `@rebasepro/*` through node_modules and its `exports` map. `./functions`
points at `dist/functions/index.d.ts`, so before `pnpm run build` it resolves to
nothing and the baas preset — whose `hello.ts` now imports it — fails to compile.
Both checks ran before the build step.

Nothing caught it locally because a developer's checkout has a `dist` lying
around from the last build; CI installs and has none. Reproduced by deleting
`packages/server/dist/functions` and running the check, which fails identically.

Moved both after `Build packages`, where `check:api-surface` already sits for
exactly this reason — its comment says "Reads dist/index.d.ts, hence after the
build". The cost is that a broken template is reported a few minutes later; the
alternative is a gate that cannot pass on a clean checkout.

Verified the rest of the lane separately: core-types, types-headless, baas-types,
test-scripts, control-chars, untranslated, hooks, unused, api-surface, dts,
deps, headless, names, derived-names and verify:docs all pass on this commit, so
this is the only thing standing between main and a green run.
…o seek to

`Accept-Ranges` on its own is an offer a client has no way to take up. A media
player sends `HEAD` before it seeks, and the response carried the range offer and
a validator but no length — so nothing told it how big the object was, and
nothing told it what byte to ask for.

Both full responses now declare it. The 206 already did, from the slice; this is
the 200 and the HEAD that Hono answers from it.

Covered by asserting the length on HEAD, on a full GET, that a 206 declares the
*slice* length rather than the object's, and that the body is unaffected.
@fgatti675

Copy link
Copy Markdown
Member Author

Added a second commit: declare Content-Length.

Testing HEAD against the route showed it answering 200 with Accept-Ranges and an ETag but no length — which makes the range offer something a client can't act on. A player sends HEAD before it seeks; without a size it can't work out what byte to ask for.

Both full responses now declare it. The 206 already did, from the slice.

Four more tests: the length on HEAD, on a full GET, that a 206 declares the slice length rather than the object's, and that the body is unchanged. packages/server: 147 suites, 2187 passing.

`check:generated` runs the website generators and fails on any diff, so a docs
change that does not carry its regenerated artifact fails CI. Mechanical.
@fgatti675
fgatti675 changed the base branch from feat/storage-cacheable-delivery to main August 22, 2026 01:48
@fgatti675

Copy link
Copy Markdown
Member Author

Retargeted from feat/storage-cacheable-delivery to main, because a stacked PR gets no CI at all.

ci.yml triggers on pull_request: branches: [main], so while this targeted another branch nothing ran on it — it sat with no verification while every other PR went green. That's worse than a noisy diff.

So this diff now carries #13's two commits as well as its own. Merge #13 first and this collapses to just the range work. Merging this one alone would land both, which is also coherent — but #13 is the better-scoped review.

Nothing about the code changed; only the base.

@fgatti675 fgatti675 closed this Aug 22, 2026
@fgatti675 fgatti675 reopened this Aug 22, 2026
`check:unused` is a ratchet on discarded values, and it caught a real one: the
local branch destructured `end` and never used it. A positioned read takes an
offset and a length, and `contentRange` renders the range from the spec rather
than from the pieces — so `end` was computed and thrown away.

Deleted rather than renamed to `_`, which is what that gate's own message asks
for.
@fgatti675
fgatti675 merged commit 6f156e2 into main Aug 22, 2026
2 checks passed
@fgatti675
fgatti675 deleted the feat/storage-range-requests branch August 22, 2026 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant