cursedbelt-server 4.0.0 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server/bench/assert.d.ts +16 -3
- package/dist/server/bench/assert.js +54 -0
- package/dist/server/bench/cpuClock.js +21 -1
- package/dist/server/d1/fakeD1.d.ts +5 -0
- package/dist/server/d1/fakeD1.js +51 -18
- package/dist/server/d1/local.js +48 -3
- package/dist/server/d1/values.d.ts +19 -2
- package/dist/server/d1/values.js +21 -2
- package/dist/server/middleware/bodyLimit.d.ts +152 -0
- package/dist/server/middleware/bodyLimit.js +161 -0
- package/dist/server/storage/binaryStore.d.ts +385 -0
- package/dist/server/storage/binaryStore.js +739 -0
- package/dist/server/storage/binaryStoreFake.d.ts +56 -0
- package/dist/server/storage/binaryStoreFake.js +63 -0
- package/package.json +19 -1
- package/src/leafSubpathsImportNothing.spec.ts +37 -0
- package/src/server/bench/assert.ts +78 -3
- package/src/server/bench/cpuBudget.spec.ts +101 -1
- package/src/server/bench/cpuClock.ts +22 -1
- package/src/server/d1/fakeD1.ts +56 -18
- package/src/server/d1/local.ts +56 -3
- package/src/server/d1/sameShape.spec.ts +73 -0
- package/src/server/d1/values.ts +23 -2
- package/src/server/middleware/bodyLimit.spec.ts +238 -0
- package/src/server/middleware/bodyLimit.ts +210 -0
- package/src/server/storage/binaryStore.spec.ts +908 -0
- package/src/server/storage/binaryStore.ts +1049 -0
- package/src/server/storage/binaryStoreFake.ts +111 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading a request body without first agreeing how big it may be.
|
|
3
|
+
*
|
|
4
|
+
* ── Why this is one function rather than one per app (2026-07-27) ────────────
|
|
5
|
+
* The two apps behind the shared owner-auth were inconsistently hardened, in a
|
|
6
|
+
* way an adversarial review found and neither test suite could:
|
|
7
|
+
*
|
|
8
|
+
* - the companion app (since folded into `apps/orch`) read `c.req.text()`,
|
|
9
|
+
* checked the length, and returned 413 BEFORE parsing — correct.
|
|
10
|
+
* - `apps/roms` called `await c.req.json()` first and checked the size deep
|
|
11
|
+
* inside the save-state store — i.e. it buffered and parsed an arbitrary body
|
|
12
|
+
* into a process whose unit caps at 300 MB of memory before forming an
|
|
13
|
+
* opinion about its size.
|
|
14
|
+
*
|
|
15
|
+
* Both went through {@link readBoundedJson} after that. The guard is one
|
|
16
|
+
* function so the posture cannot diverge again, and it is deliberately the
|
|
17
|
+
* *last* line of defense: a proxy's `client_max_body_size` refuses an oversized
|
|
18
|
+
* body at the edge, and this refuses it if the proxy is absent, misconfigured,
|
|
19
|
+
* or bypassed — which is exactly the case in dev, in `preview:prod`, and for any
|
|
20
|
+
* request that reaches the loopback port directly.
|
|
21
|
+
*
|
|
22
|
+
* ── Where it came from (2026-09-17) ──────────────────────────────────────────
|
|
23
|
+
* Five apps — `roms`, `family`, `music`, `vault`, `collections` — carried this
|
|
24
|
+
* file byte-for-byte identical (`b36d0eedee7a`), which is the posture diverging
|
|
25
|
+
* again, only more slowly and with nothing watching: a repo's gate proves that
|
|
26
|
+
* repo, so five green gates is what five copies of a security guard look like
|
|
27
|
+
* from the outside. Confirmed identical with `shasum -a256` before the move, so
|
|
28
|
+
* there was no fork to reconcile and nothing was dropped.
|
|
29
|
+
*
|
|
30
|
+
* 🔴 It is published as the LEAF subpath `cursedbelt-server/body-limit`, never
|
|
31
|
+
* through the `./middleware` barrel — that barrel drags `hono` and the error
|
|
32
|
+
* envelope, and this module's whole promise is that it imports NOTHING. The
|
|
33
|
+
* source is a `{ req: { text() } }` / `{ req: { arrayBuffer() } }` /
|
|
34
|
+
* `{ req: { header() } }` structural type rather than a Hono context, so a
|
|
35
|
+
* caller needs no framework to use it and a test needs none to exercise it.
|
|
36
|
+
* `src/leafSubpathsImportNothing.spec.ts` is what holds that promise.
|
|
37
|
+
*/
|
|
38
|
+
/**
|
|
39
|
+
* Read a body as TEXT, refusing anything over `maxBytes`.
|
|
40
|
+
*
|
|
41
|
+
* The sibling of {@link readBoundedJson}, for a payload that IS the text rather
|
|
42
|
+
* than a document containing it — `apps/patterns`' chunked sends put their
|
|
43
|
+
* metadata in the query string and the chunk in the raw body, which spares a
|
|
44
|
+
* megabyte-scale caller from JSON-escaping every piece and spares this process
|
|
45
|
+
* from parsing what it is only going to concatenate.
|
|
46
|
+
*
|
|
47
|
+
* Same byte-not-character measurement, and for the same reason: it is the number
|
|
48
|
+
* the proxy in front of this is enforcing.
|
|
49
|
+
*/
|
|
50
|
+
export async function readBoundedText(c, options) {
|
|
51
|
+
const { maxBytes, label = 'body' } = options;
|
|
52
|
+
let raw;
|
|
53
|
+
try {
|
|
54
|
+
raw = await c.req.text();
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return { ok: false, status: 400, error: `${label} could not be read` };
|
|
58
|
+
}
|
|
59
|
+
const bytes = new TextEncoder().encode(raw).length;
|
|
60
|
+
if (bytes > maxBytes) {
|
|
61
|
+
return {
|
|
62
|
+
ok: false,
|
|
63
|
+
status: 413,
|
|
64
|
+
error: `${label} too large (${bytes} bytes; the limit is ${maxBytes})`,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return { ok: true, value: raw };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Read a body as RAW BYTES, refusing anything over `maxBytes`.
|
|
71
|
+
*
|
|
72
|
+
* The third member of the family, for a payload that is neither JSON nor text:
|
|
73
|
+
* roms' ROM-upload chunks are cartridge bytes, and routing them through
|
|
74
|
+
* {@link readBoundedText} would decode arbitrary binary as UTF-8 — which is
|
|
75
|
+
* lossy (every invalid sequence becomes U+FFFD) and measures a size that is not
|
|
76
|
+
* the size the proxy in front is enforcing.
|
|
77
|
+
*
|
|
78
|
+
* A `Content-Length` claim is deliberately NOT trusted here: it is a header, and
|
|
79
|
+
* the guard has to hold against a body that disagrees with it. The measurement is
|
|
80
|
+
* the buffer that actually arrived.
|
|
81
|
+
*/
|
|
82
|
+
export async function readBoundedBytes(c, options) {
|
|
83
|
+
const { maxBytes, label = 'body' } = options;
|
|
84
|
+
let buffer;
|
|
85
|
+
try {
|
|
86
|
+
buffer = await c.req.arrayBuffer();
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return { ok: false, status: 400, error: `${label} could not be read` };
|
|
90
|
+
}
|
|
91
|
+
if (buffer.byteLength > maxBytes) {
|
|
92
|
+
return {
|
|
93
|
+
ok: false,
|
|
94
|
+
status: 413,
|
|
95
|
+
error: `${label} too large (${buffer.byteLength} bytes; the limit is ${maxBytes})`,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
return { ok: true, value: new Uint8Array(buffer) };
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Refuse a body by its DECLARED `Content-Length`, without reading it.
|
|
102
|
+
*
|
|
103
|
+
* The fourth member of the family, and the only one for a body that cannot be
|
|
104
|
+
* buffered first: a `multipart/form-data` upload. The other three measure the
|
|
105
|
+
* bytes that actually arrived, which is strictly better — but they get to,
|
|
106
|
+
* because they buffer. `c.req.formData()` gives no such opportunity: by the time
|
|
107
|
+
* it resolves, the whole upload is already in a process whose unit caps at
|
|
108
|
+
* 300 MB of memory, which is the thing the limit exists to prevent.
|
|
109
|
+
*
|
|
110
|
+
* So this trusts the header, and the docblock says so plainly. A liar gets
|
|
111
|
+
* through to the buffering call — but a liar was always going to, and this still
|
|
112
|
+
* turns the honest 3 GB folder-drop (the actual failure shape) into a refusal
|
|
113
|
+
* the app can explain instead of an OOM or an nginx HTML 413 the app never sees.
|
|
114
|
+
* It is a companion to the proxy's `client_max_body_size`, never a replacement.
|
|
115
|
+
*
|
|
116
|
+
* A missing or unparseable header is NOT a refusal: chunked requests omit it
|
|
117
|
+
* legitimately, and refusing them would break a correct client to catch a
|
|
118
|
+
* dishonest one.
|
|
119
|
+
*/
|
|
120
|
+
export function refuseOversizedBody(c, options) {
|
|
121
|
+
const { maxBytes, label = 'body' } = options;
|
|
122
|
+
const declared = Number(c.req.header('content-length'));
|
|
123
|
+
if (!Number.isFinite(declared) || declared <= maxBytes)
|
|
124
|
+
return null;
|
|
125
|
+
return {
|
|
126
|
+
status: 413,
|
|
127
|
+
error: `${label} too large (${declared} bytes; the limit is ${maxBytes})`,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Read a JSON body, refusing anything over `maxBytes` before parsing it.
|
|
132
|
+
*
|
|
133
|
+
* The size is measured in BYTES, not characters. `String.length` counts UTF-16
|
|
134
|
+
* code units, so a body of 4-byte emoji measures half its true size that way —
|
|
135
|
+
* which would let a caller past a byte limit the proxy is enforcing in real
|
|
136
|
+
* bytes, and produce the mismatch this module exists to close.
|
|
137
|
+
*/
|
|
138
|
+
export async function readBoundedJson(c, options) {
|
|
139
|
+
const { maxBytes, label = 'body' } = options;
|
|
140
|
+
let raw;
|
|
141
|
+
try {
|
|
142
|
+
raw = await c.req.text();
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return { ok: false, status: 400, error: `${label} could not be read` };
|
|
146
|
+
}
|
|
147
|
+
const bytes = new TextEncoder().encode(raw).length;
|
|
148
|
+
if (bytes > maxBytes) {
|
|
149
|
+
return {
|
|
150
|
+
ok: false,
|
|
151
|
+
status: 413,
|
|
152
|
+
error: `${label} too large (${bytes} bytes; the limit is ${maxBytes})`,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
return { ok: true, value: JSON.parse(raw) };
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
return { ok: false, status: 400, error: `${label} must be JSON` };
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import type { FileTokenClaims } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* The claims binary-server verifies, re-exported from `./types` (the fleet's single
|
|
4
|
+
* source of truth — `cursedbelt-server/storage/types`). See there for the per-field
|
|
5
|
+
* docs. Kept exported here so the call sites that took it from the app-side copy of
|
|
6
|
+
* this module read unchanged.
|
|
7
|
+
*/
|
|
8
|
+
export type { FileTokenClaims };
|
|
9
|
+
export interface BinaryStoreConfig {
|
|
10
|
+
/** e.g. `http://127.0.0.1:3099` or `https://binary-server.cursedalchemy.com`. */
|
|
11
|
+
baseUrl: string;
|
|
12
|
+
/** Tenant id — the first path segment and the token scope, e.g. `notes`. */
|
|
13
|
+
appId: string;
|
|
14
|
+
/** Must byte-match the issuer registered in binary-server's `apps` table. */
|
|
15
|
+
issuer: string;
|
|
16
|
+
/** PKCS8 private PEM (raw, `\n`-escaped, or base64-wrapped — all accepted). */
|
|
17
|
+
privateKey: string;
|
|
18
|
+
/** Enables `remove`/`removePrefix` (sent as `x-internal-secret`). */
|
|
19
|
+
internalSecret?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Where THIS PROCESS should reach binary-server, when that is somewhere cheaper than
|
|
22
|
+
* the public name — `http://127.0.0.1:3099` for a deployment sharing the Mac with it.
|
|
23
|
+
* Defaults to {@link BinaryStoreConfig.baseUrl}. Used by `get`, `meta` and `stat`;
|
|
24
|
+
* never by `mediaUrl`, whose output is handed to a browser.
|
|
25
|
+
*/
|
|
26
|
+
internalBaseUrl?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Above this many bytes, {@link BinaryStore.put} takes the CHUNKED path by
|
|
29
|
+
* itself. Defaults to {@link AUTO_CHUNK_THRESHOLD_BYTES}; pass `Infinity` to
|
|
30
|
+
* turn the fallback off and get the pre-2026-08-08 behavior.
|
|
31
|
+
*/
|
|
32
|
+
autoChunkBytes?: number;
|
|
33
|
+
/**
|
|
34
|
+
* How long a "this key is not here" is believed. Defaults to
|
|
35
|
+
* {@link BINARY_ABSENCE_TTL_MS}; the constant carries the owner's reasoning and
|
|
36
|
+
* an app should have a measured reason to differ from it.
|
|
37
|
+
*/
|
|
38
|
+
absenceTtlMs?: number;
|
|
39
|
+
/** Injected so the memo's expiry is testable without a real clock. */
|
|
40
|
+
now?: () => number;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Which keys this process has read from the wire far too often, worst first —
|
|
44
|
+
* empty on every healthy deployment, which is what makes it a usable alarm.
|
|
45
|
+
*
|
|
46
|
+
* Entries older than one window are dropped as they are read, so a burst that
|
|
47
|
+
* stopped stops being reported rather than becoming a permanent accusation.
|
|
48
|
+
*/
|
|
49
|
+
export declare function repeatedBinaryKeys(now?: () => number): RepeatedBinaryKey[];
|
|
50
|
+
/** Test seam — the register is process-wide, so a spec must be able to clear it. */
|
|
51
|
+
export declare function resetRepeatedBinaryKeys(): void;
|
|
52
|
+
/**
|
|
53
|
+
* Did this request ask for a FRESH answer — i.e. is it a hard refresh?
|
|
54
|
+
*
|
|
55
|
+
* 🔴 The owner's first escape hatch, and the reason it is a shared function rather
|
|
56
|
+
* than an inline header read in each app: *"if the no is remembered an hour then I
|
|
57
|
+
* could fix it but not be able to tell for an hour."* A browser hard-refresh sends
|
|
58
|
+
* `Cache-Control: no-cache` (older ones send `Pragma`), so reloading the picture is
|
|
59
|
+
* the repair — as long as every app spells the check the same way.
|
|
60
|
+
*/
|
|
61
|
+
export declare function wantsFresh(req: {
|
|
62
|
+
headers: Headers;
|
|
63
|
+
} | Headers): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* 🔴 The size at which a single-request `put` stops being safe, FLEET-WIDE.
|
|
66
|
+
*
|
|
67
|
+
* binary-server is published through a Cloudflare Tunnel, and the edge refuses a
|
|
68
|
+
* request body over ~100 MB with a **413 before it ever reaches the origin**. So
|
|
69
|
+
* the ceiling is not bs's and not the caller's — it belongs to a hop neither end
|
|
70
|
+
* controls, and every app that stores a large blob inherits it.
|
|
71
|
+
*
|
|
72
|
+
* `apps/roms` hit it on 2026-08-08: eight genuine DS cartridges (128–512 MB)
|
|
73
|
+
* came back `[binary-store] put rom/<digest>: 413` and it read as eight
|
|
74
|
+
* mysterious ingest failures rather than as one limit. roms fixed it locally,
|
|
75
|
+
* which left the trap in place for every other app's attachments and for any
|
|
76
|
+
* future video/media path — so the threshold lives HERE now and `put` applies it
|
|
77
|
+
* itself.
|
|
78
|
+
*
|
|
79
|
+
* 64 MiB, not 100: the edge's number is approximate and includes headers and any
|
|
80
|
+
* transfer encoding, and the chunked path costs one extra request for a blob
|
|
81
|
+
* that only just crosses the line. Being early is free; being late is a 413.
|
|
82
|
+
*/
|
|
83
|
+
export declare const AUTO_CHUNK_THRESHOLD_BYTES: number;
|
|
84
|
+
export interface PutOptions {
|
|
85
|
+
/** The user-facing display name ("My Dog Video") — binary-server stores it in its
|
|
86
|
+
* db ONLY (never on disk), for its inspector/UIs. Optional everywhere. */
|
|
87
|
+
title?: string;
|
|
88
|
+
/**
|
|
89
|
+
* This key IS the digest of these bytes, so they can never change under it.
|
|
90
|
+
* binary-server then serves the object `public, max-age=31536000, immutable`
|
|
91
|
+
* instead of the one-day default — no daily revalidation against the home
|
|
92
|
+
* uplink for bytes that are the same bytes by construction, and cache-busting
|
|
93
|
+
* needs no version segment because different bytes are a different key.
|
|
94
|
+
*
|
|
95
|
+
* Only pass it for a genuinely content-addressed key. A wrong `true` pins stale
|
|
96
|
+
* bytes at the edge and in every browser for a year.
|
|
97
|
+
*/
|
|
98
|
+
immutable?: boolean;
|
|
99
|
+
}
|
|
100
|
+
export interface MediaUrlOptions {
|
|
101
|
+
filename?: string;
|
|
102
|
+
disposition?: 'inline' | 'attachment';
|
|
103
|
+
ttlSeconds?: number;
|
|
104
|
+
/**
|
|
105
|
+
* Mint a URL that is BYTE-IDENTICAL for every call inside the same window of
|
|
106
|
+
* this many seconds (the token's `iat`/`exp` are snapped to the window rather
|
|
107
|
+
* than to the clock), and valid for two windows so one minted at the very end
|
|
108
|
+
* of a window is still good.
|
|
109
|
+
*
|
|
110
|
+
* The problem it solves: a fresh token per call means a fresh URL per call, and
|
|
111
|
+
* a browser's HTTP cache keys on the URL — so an `<img src>` re-rendered or a
|
|
112
|
+
* ROM re-launched re-downloads bytes it already has, however long the
|
|
113
|
+
* Cache-Control says. (The Cloudflare edge is unaffected either way; its cache
|
|
114
|
+
* key strips `token`.) The cost is a longer-lived bearer URL, so keep the
|
|
115
|
+
* window as short as the caching win allows and leave it unset for anything
|
|
116
|
+
* whose leak matters more than its bytes.
|
|
117
|
+
*/
|
|
118
|
+
stableWindowSeconds?: number;
|
|
119
|
+
}
|
|
120
|
+
export interface BlobMeta {
|
|
121
|
+
size: number;
|
|
122
|
+
mime: string | null;
|
|
123
|
+
/** The display name the uploader set (`PutOptions.title`), if any. */
|
|
124
|
+
title: string | null;
|
|
125
|
+
/** sha256 binary-server computed post-write. Audit-only — keys are app-chosen. */
|
|
126
|
+
checksum: string | null;
|
|
127
|
+
createdAt?: string;
|
|
128
|
+
updatedAt?: string;
|
|
129
|
+
}
|
|
130
|
+
export interface PutLargeOptions extends PutOptions {
|
|
131
|
+
/** Bytes per chunk. Default 16 MiB — big enough that a 500 MB upload is ~32 requests,
|
|
132
|
+
* small enough that neither side ever buffers a whole video. */
|
|
133
|
+
chunkBytes?: number;
|
|
134
|
+
}
|
|
135
|
+
/** How a read should treat the memos — see {@link BINARY_ABSENCE_TTL_MS}. */
|
|
136
|
+
export interface ReadOptions {
|
|
137
|
+
/**
|
|
138
|
+
* Ignore both memos: ask the store, and refresh what we believe from its answer.
|
|
139
|
+
*
|
|
140
|
+
* This is the per-request escape hatch, and `wantsFresh(request)` is what turns a
|
|
141
|
+
* browser's hard refresh into it — so "I fixed the picture and cannot tell for an
|
|
142
|
+
* hour" is answered by reloading the picture.
|
|
143
|
+
*/
|
|
144
|
+
fresh?: boolean;
|
|
145
|
+
}
|
|
146
|
+
/** What the memos currently hold — for an owner surface, a smoke, and the tests. */
|
|
147
|
+
export interface BinaryCacheStats {
|
|
148
|
+
/** Keys known to be present. Never expires; see {@link BINARY_ABSENCE_TTL_MS}. */
|
|
149
|
+
present: number;
|
|
150
|
+
/** Keys known to be absent, and not yet lapsed. */
|
|
151
|
+
absent: number;
|
|
152
|
+
}
|
|
153
|
+
/** One key this process has read from the wire far too often — see
|
|
154
|
+
* {@link BINARY_REPEAT_THRESHOLD}. */
|
|
155
|
+
export interface RepeatedBinaryKey {
|
|
156
|
+
/** binary-server tenant, i.e. which app is doing it. */
|
|
157
|
+
tenant: string;
|
|
158
|
+
key: string;
|
|
159
|
+
/** Wire reads inside the current window. Memo hits are NOT counted: the memo
|
|
160
|
+
* working is the fix, so a memoized key must go quiet. */
|
|
161
|
+
reads: number;
|
|
162
|
+
windowMs: number;
|
|
163
|
+
}
|
|
164
|
+
export interface BinaryStore {
|
|
165
|
+
readonly appId: string;
|
|
166
|
+
/**
|
|
167
|
+
* Store an object. Above {@link AUTO_CHUNK_THRESHOLD_BYTES} this delegates to
|
|
168
|
+
* {@link BinaryStore.putLarge} by itself, because the limit that makes a
|
|
169
|
+
* single request fail belongs to the Cloudflare Tunnel in front of
|
|
170
|
+
* binary-server rather than to any caller — see that constant.
|
|
171
|
+
*/
|
|
172
|
+
put(key: string, bytes: Uint8Array, mime?: string, opts?: PutOptions): Promise<void>;
|
|
173
|
+
/**
|
|
174
|
+
* Upload in chunks (`PUT /upload-chunk`), for objects too large to buffer whole.
|
|
175
|
+
*
|
|
176
|
+
* `put` sends one request whose body binary-server reads entirely into memory before
|
|
177
|
+
* writing — fine for an attachment, wrong for a 500 MB video on a machine that is also
|
|
178
|
+
* running everything else. This sends bounded pieces and lets bs assemble them once the
|
|
179
|
+
* last one lands (it counts arrivals, so out-of-order delivery and retries are both safe).
|
|
180
|
+
*/
|
|
181
|
+
putLarge(key: string, source: Uint8Array | Blob, mime?: string, opts?: PutLargeOptions): Promise<void>;
|
|
182
|
+
/** `null` when the key does not exist. */
|
|
183
|
+
get(key: string, opts?: ReadOptions): Promise<Uint8Array | null>;
|
|
184
|
+
/**
|
|
185
|
+
* Everything binary-server knows about a blob (`GET /meta`) — `null` when unknown or
|
|
186
|
+
* deleted. The honest answer to "what IS this?": size, the stored mime, and the TITLE,
|
|
187
|
+
* none of which `stat` could report (it inferred a size from a Range probe and nothing
|
|
188
|
+
* else). Added to bs 2026-07-30 for exactly this.
|
|
189
|
+
*
|
|
190
|
+
* MEMOIZED since 2026-09-09 — a `null` for an hour, a hit for ever. See
|
|
191
|
+
* {@link BINARY_ABSENCE_TTL_MS} for both halves and for the two bypasses.
|
|
192
|
+
*/
|
|
193
|
+
meta(key: string, opts?: ReadOptions): Promise<BlobMeta | null>;
|
|
194
|
+
/** `null` when the key does not exist. Delegates to {@link BinaryStore.meta}. */
|
|
195
|
+
stat(key: string, opts?: ReadOptions): Promise<{
|
|
196
|
+
size: number;
|
|
197
|
+
} | null>;
|
|
198
|
+
/**
|
|
199
|
+
* Does the store hold this key? The cheap question, memoized — and the one every
|
|
200
|
+
* app was asking the expensive way.
|
|
201
|
+
*
|
|
202
|
+
* Prefer it over `stat`/`meta` when the answer is only ever used as a boolean:
|
|
203
|
+
* it says what it means, and it cannot tempt a caller into trusting a memoized
|
|
204
|
+
* `size` for a key whose bytes are replaceable.
|
|
205
|
+
*/
|
|
206
|
+
has(key: string, opts?: ReadOptions): Promise<boolean>;
|
|
207
|
+
/**
|
|
208
|
+
* What the memo ALREADY knows about a key, with no I/O and no promise.
|
|
209
|
+
*
|
|
210
|
+
* 🔴 The one question `has()` cannot answer, and the reason an app would otherwise keep
|
|
211
|
+
* a second copy of this cache. A listing route answers for hundreds of rows inside one
|
|
212
|
+
* request — roms' `/api/roms` draws 300 games — so it cannot await a round trip per row
|
|
213
|
+
* even when every one of them would hit the memo: `await` in a loop over 300 keys is 300
|
|
214
|
+
* microtask turns and a `Promise` each, on the box that also serves everything else.
|
|
215
|
+
* With this the render answers from what is already known and lets the per-card path
|
|
216
|
+
* fill in the rest.
|
|
217
|
+
*
|
|
218
|
+
* `"unknown"` is a real third answer and must not be collapsed into `"absent"`: it means
|
|
219
|
+
* *nobody has asked yet*, and a caller that treats it as "no" turns a cold process into
|
|
220
|
+
* one that permanently draws nothing.
|
|
221
|
+
*/
|
|
222
|
+
known(key: string): 'present' | 'absent' | 'unknown';
|
|
223
|
+
/**
|
|
224
|
+
* Forget every "the store does not have this" — all of them, or those whose key
|
|
225
|
+
* starts with `prefix`. Returns how many were dropped.
|
|
226
|
+
*
|
|
227
|
+
* 🔴 The owner's second escape hatch, and it is deliberately a REPAIR rather than
|
|
228
|
+
* a cache flush: the positive memos are untouched, so proving a fix costs nothing
|
|
229
|
+
* but the keys that were actually missing. An app exposes it behind its owner gate
|
|
230
|
+
* (`POST /api/media/forget-misses`) so "I put the picture back" can be verified in
|
|
231
|
+
* seconds instead of in an hour.
|
|
232
|
+
*/
|
|
233
|
+
forgetMisses(prefix?: string): number;
|
|
234
|
+
/** What the memos hold right now. */
|
|
235
|
+
cacheStats(): BinaryCacheStats;
|
|
236
|
+
remove(key: string): Promise<void>;
|
|
237
|
+
/** Wipes a whole container tree (`?container=1`). */
|
|
238
|
+
removePrefix(prefix: string): Promise<void>;
|
|
239
|
+
/** A signed, directly-fetchable download URL (for redirects / <img src>). */
|
|
240
|
+
mediaUrl(key: string, opts?: MediaUrlOptions): Promise<string>;
|
|
241
|
+
/**
|
|
242
|
+
* A signed URL for `master`, authorized by `prefix` — the only shape a multi-object tree
|
|
243
|
+
* (an HLS ladder) can be played from. See the implementation for why an exact-key token
|
|
244
|
+
* cannot work and why this is a separate method rather than a flag.
|
|
245
|
+
*/
|
|
246
|
+
mediaPrefixUrl(prefix: string, master: string, opts?: MediaUrlOptions): Promise<string>;
|
|
247
|
+
/**
|
|
248
|
+
* The raw `Response` for an object, from the origin THIS PROCESS should use — so an app
|
|
249
|
+
* can SERVE bytes itself, streaming, when the public edge will not.
|
|
250
|
+
*
|
|
251
|
+
* 🔴 The gap {@link BinaryStoreConfig.internalBaseUrl} left open. That note ends
|
|
252
|
+
* "redirected media — videos, large downloads — therefore still goes through the edge",
|
|
253
|
+
* and on 2026-09-08 that was the whole outage: Cloudflare spent its Workers day, every
|
|
254
|
+
* `/media/*` request answered 429, and apps/music could not play a note even though the
|
|
255
|
+
* bytes were on the same Mac as the server the browser was talking to.
|
|
256
|
+
*
|
|
257
|
+
* Two things make this a fallback rather than a second architecture:
|
|
258
|
+
* · It returns the RESPONSE, not the bytes. `get` buffers a whole object into memory,
|
|
259
|
+
* which is wrong for a 30 MB track and unthinkable for a video; the caller pipes
|
|
260
|
+
* `res.body` straight through and holds one chunk at a time.
|
|
261
|
+
* · `range` is forwarded and the origin's `206`/`content-range` come back untouched,
|
|
262
|
+
* because Range IS seeking. A proxy that drops it turns a seekable track into a
|
|
263
|
+
* download.
|
|
264
|
+
*/
|
|
265
|
+
fetchMedia(key: string, init?: {
|
|
266
|
+
range?: string | null;
|
|
267
|
+
method?: 'GET' | 'HEAD';
|
|
268
|
+
signal?: AbortSignal;
|
|
269
|
+
}): Promise<Response>;
|
|
270
|
+
/** Escape hatch for protocols this module doesn't wrap (chunked uploads). */
|
|
271
|
+
signToken(claims: FileTokenClaims, ttlSeconds: number): Promise<string>;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* 🔴 How long "the store does not hold this key" is believed — the NEGATIVE memo.
|
|
275
|
+
*
|
|
276
|
+
* The positive one never expires and needs no constant: a key in this fleet names
|
|
277
|
+
* either a content digest or a timestamped upload, so the bytes under it cannot
|
|
278
|
+
* change and "it is there" cannot stop being true. An ABSENCE is different — it is
|
|
279
|
+
* a fact about right now, and art, web copies and derivatives all genuinely arrive
|
|
280
|
+
* later — so it has to lapse.
|
|
281
|
+
*
|
|
282
|
+
* An hour is the default because the owner named the cost of getting it wrong:
|
|
283
|
+
*
|
|
284
|
+
* > *"potentially we need a shorter time for remembering a 'no' in some
|
|
285
|
+
* > circumstances or at least a way to bypass it occasionally. For instance
|
|
286
|
+
* > collections showed a lot of missing thumbnails and images and if the no is
|
|
287
|
+
* > remembered an hour then I could fix it but not be able to tell for an hour."*
|
|
288
|
+
*
|
|
289
|
+
* So the hour is never the only answer. TWO bypasses ship with it, both cheap and
|
|
290
|
+
* both proven by {@link BinaryStore.forgetMisses}' tests:
|
|
291
|
+
*
|
|
292
|
+
* · a read with `{ fresh: true }` ignores both memos and re-asks. `wantsFresh()`
|
|
293
|
+
* turns a browser's hard refresh (`Cache-Control: no-cache`) into exactly that,
|
|
294
|
+
* so the owner's own reload IS the fix for one picture;
|
|
295
|
+
* · `forgetMisses(prefix?)` drops every negative memo, or a subtree of them, so an
|
|
296
|
+
* app can expose a one-click repair and prove it in seconds rather than in an
|
|
297
|
+
* hour.
|
|
298
|
+
*/
|
|
299
|
+
export declare const BINARY_ABSENCE_TTL_MS: number;
|
|
300
|
+
/**
|
|
301
|
+
* The window and the count that make a key's re-reads an INCIDENT rather than traffic.
|
|
302
|
+
*
|
|
303
|
+
* 🔴 Every quota incident this fleet has had was this exact shape and nothing was
|
|
304
|
+
* watching for it: roms boxart at ~200 reads per key per day (20,626 failed `/media`
|
|
305
|
+
* fetches in one day), family portraits at 2,449 per key per day, music cover art
|
|
306
|
+
* doing a server-side `get` that left the Mac to read a file on the Mac. In all three
|
|
307
|
+
* the giveaway was one key, over and over, from a `Bun/1.3.x` user agent — visible in
|
|
308
|
+
* Cloudflare's top-talker list and in nobody's code.
|
|
309
|
+
*
|
|
310
|
+
* 50 in five minutes is far above any legitimate server-side pattern (a page render
|
|
311
|
+
* asks for a key once; a warm pass asks once) and far below the rate that spends a
|
|
312
|
+
* day's allowance, so it fires early and it does not fire on a normal day.
|
|
313
|
+
*/
|
|
314
|
+
export declare const BINARY_REPEAT_WINDOW_MS: number;
|
|
315
|
+
export declare const BINARY_REPEAT_THRESHOLD = 50;
|
|
316
|
+
export declare const DOWNLOAD_TOKEN_TTL_SECONDS = 60;
|
|
317
|
+
export declare const UPLOAD_TOKEN_TTL_SECONDS = 600;
|
|
318
|
+
/** Deletes are server-to-server and near-instant; a short TTL suffices. The token proves the
|
|
319
|
+
* caller holds THIS tenant's private key and is authorized for THIS path — binary-server
|
|
320
|
+
* requires it alongside the internal secret (defense in depth, owner ruling #10). */
|
|
321
|
+
export declare const DELETE_TOKEN_TTL_SECONDS = 120;
|
|
322
|
+
/** register-app prints the key as base64(PKCS8 PEM); env files sometimes carry
|
|
323
|
+
* raw PEM or PEM with literal `\n` sequences. Accept all three. */
|
|
324
|
+
export declare function normalizePrivateKeyPem(raw: string): string;
|
|
325
|
+
export declare function createBinaryStore(cfg: BinaryStoreConfig): BinaryStore;
|
|
326
|
+
/**
|
|
327
|
+
* Which binary-server TENANT this process is — the app's own key, unless the environment
|
|
328
|
+
* names another.
|
|
329
|
+
*
|
|
330
|
+
* 🔴 It has to be a variable, and 2026-08-21 is the day that stopped being theoretical.
|
|
331
|
+
* A tenant is TWO facts that must agree: the key a request is SIGNED with, and the key the
|
|
332
|
+
* request PATH starts with (`authorize()` in binary-server refuses when
|
|
333
|
+
* `path.split("/")[0] !== token.app_id`). Apps supplied the second as a literal — `const
|
|
334
|
+
* TENANT = "collections"` — while `FILE_TOKEN_ISSUER`/`FILE_TOKEN_PRIVATE_KEY` came from
|
|
335
|
+
* the environment. That is fine on production, where the two happen to match, and it makes
|
|
336
|
+
* an ephemeral STAGE structurally unable to store a byte: `stage up` mints it a real
|
|
337
|
+
* `collections-stage` tenant and hands it that keypair, and every upload then signs as
|
|
338
|
+
* `collections-stage` while addressing `collections/…` and comes back
|
|
339
|
+
* `[binary-store] put file/<sha>: 403`. The stage looked like a working app with an empty
|
|
340
|
+
* gallery, which is exactly what an empty stage is supposed to look like.
|
|
341
|
+
*
|
|
342
|
+
* Reading it here means an app opts in by CALLING this instead of writing a literal, and
|
|
343
|
+
* `BINARY_STORE_TENANT` is already inside an app's stage-env shared-store denylist
|
|
344
|
+
* (`/^BINARY_STORE_(?!.*URL$)/`) — so a stage cannot INHERIT production's tenant, only be
|
|
345
|
+
* given its own. The fallback is the app's key, so an app that never sets it is unchanged.
|
|
346
|
+
*
|
|
347
|
+
* 🔴 **A BLANK tenant now throws, and that is the whole point of the check
|
|
348
|
+
* (2026-08-23).** `undefined` is a perfectly good JavaScript string once it is
|
|
349
|
+
* template-interpolated, so a caller that forgot the argument signed a token for
|
|
350
|
+
* `undefined/<key>` and addressed `PUT /upload/undefined/<key>` — at which point
|
|
351
|
+
* binary-server's `authorize()` refuses it correctly and answers a bare
|
|
352
|
+
* `{"error":"forbidden"}`. That reads as a broken CREDENTIAL, and it was filed as one
|
|
353
|
+
* (*"family.env's FILE_TOKEN_PRIVATE_KEY does not authorize against the family tenant"*).
|
|
354
|
+
* The keypair was correct the whole time and a Mac-side upload works; what was missing was
|
|
355
|
+
* the tenant id. bs cannot tell the two apart — it sees a token for a tenant it does not
|
|
356
|
+
* have — so the check has to live on this side of the wire, where the mistake is legible.
|
|
357
|
+
*/
|
|
358
|
+
export declare function binaryStoreTenant(appId: string, env?: Record<string, string | undefined>): string;
|
|
359
|
+
/**
|
|
360
|
+
* The tenant this process's binary store resolved, or `null` when it has no store.
|
|
361
|
+
*
|
|
362
|
+
* The value an app's `mediaTenant` option wants, and the reason it exists as a named
|
|
363
|
+
* function rather than each app writing `readBinaryStoreEnv(x)?.appId`:
|
|
364
|
+
*
|
|
365
|
+
* 🔴 **`null` and a thrown error are different answers and both are correct.**
|
|
366
|
+
* {@link binaryStoreTenant} throws when it cannot name a tenant, which is right at an upload
|
|
367
|
+
* site — signing for `"undefined/<key>"` earns a bare 403 that reads as a bad credential. It is
|
|
368
|
+
* wrong on `/healthz`, where the same throw would turn "this dev shell has no store" into a
|
|
369
|
+
* 500 and take the app's health probe down with it. So this asks the same question through the
|
|
370
|
+
* same resolution and answers `null` for the store-less case, which the health handler then
|
|
371
|
+
* reports as an explicit "no store" rather than as silence.
|
|
372
|
+
*
|
|
373
|
+
* Pass the app's OWN tenant key — the constant its store is built from (`LIFE_BINARY_TENANT`,
|
|
374
|
+
* `STUDIO_BINARY_TENANT`, …), never the app's display name. They are the same string for most
|
|
375
|
+
* apps and that is precisely why the difference goes unnoticed when it is not.
|
|
376
|
+
*/
|
|
377
|
+
export declare function resolvedMediaTenant(appId: string, env?: Record<string, string | undefined>): string | null;
|
|
378
|
+
/**
|
|
379
|
+
* The standard env surface every tenant's secrets file carries
|
|
380
|
+
* (`$FORGE_STATE/secrets/<app>.env`, written at bs registration):
|
|
381
|
+
* `BINARY_SERVER_URL`, `FILE_TOKEN_ISSUER`, `FILE_TOKEN_PRIVATE_KEY`, and
|
|
382
|
+
* optionally `BINARY_SERVER_INTERNAL_SECRET`. Returns `null` unless the three
|
|
383
|
+
* required vars are all present — callers fall back to their local backend.
|
|
384
|
+
*/
|
|
385
|
+
export declare function readBinaryStoreEnv(appId: string, env?: Record<string, string | undefined>): BinaryStoreConfig | null;
|