1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { safeParse, array, object, string } from "@atcute/lexicons/validations";
import { manifestSchema, snapshotResponse } from "./snapshot";
import pageSource from "./page.html";
import ui from "./ui.js";
/** the page names its script by content hash so an html/js cache skew on deploy cannot happen. */
function contentHash(text: string): string {
let h = 2166136261;
for (let i = 0; i < text.length; i++)
h = Math.imul(h ^ text.charCodeAt(i), 16777619);
return (h >>> 0).toString(36);
}
const UI_VERSION = contentHash(ui);
const page = pageSource.replace('src="ui.js"', `src="ui.js?v=${UI_VERSION}"`);
interface Env extends Bindings {
readonly STRATA_INGEST_TOKEN: string;
}
const MAX_ARCHIVE_OBJECT_BYTES = 200 << 20;
const ARCHIVE_KEY =
/^(events\/ns=[a-z0-9._-]+\/seg=\d+\.parquet|agg\/seg=\d+\.parquet|manifest\/seg=\d+\.json)$/;
/** the drain uploads one object per (segment, partition). app.bsky raw rows are the only private thing:
* everything else — tail namespaces, marker rows, per-segment aggregates — is public by decision (2026-08-29). */
function bucketFor(key: string, env: Env): R2Bucket {
return key.startsWith("events/ns=app.bsky/") ? env.PRIVATE : env.PUBLIC;
}
async function putArchive(
request: Request,
key: string,
env: Env,
): Promise<Response> {
if (!authorized(request, env)) return json({ error: "unauthorized" }, 401);
if (!ARCHIVE_KEY.test(key))
return json(
{
error:
"key must be events/ns=<ns>/seg=<n>.parquet, agg/seg=<n>.parquet, or manifest/seg=<n>.json",
},
400,
);
const length = Number(request.headers.get("content-length") ?? "0");
if (
!Number.isFinite(length) ||
length <= 0 ||
length > MAX_ARCHIVE_OBJECT_BYTES
)
return json({ error: "content-length required, ≤ 200 MiB" }, 413);
if (request.body === null) return json({ error: "empty body" }, 400);
const bucket = bucketFor(key, env);
try {
const object = await bucket.put(key, request.body, {
httpMetadata: {
contentType: key.endsWith(".json")
? "application/json"
: "application/vnd.apache.parquet",
},
});
return json({
key,
size: object.size,
etag: object.httpEtag,
bucket: bucket === env.PRIVATE ? "private" : "public",
});
} catch (err) {
// an R2 hiccup surfaces here as a 503 with the message, not a bare 1101; the drain retries on 5xx
return json(
{ error: err instanceof Error ? err.message : "put failed" },
503,
);
}
}
/** what this worker answers with: plain JSON built from our own row types (type aliases, so arrays of them fit). */
type JsonValue =
| string
| number
| boolean
| null
| undefined
| readonly JsonValue[]
| { readonly [key: string]: JsonValue };
function json(
body: JsonValue,
status = 200,
headers: Readonly<Record<string, string>> = {},
): Response {
return new Response(JSON.stringify(body), {
status,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*",
...headers,
},
});
}
function timingSafeEqual(a: string, b: string): boolean {
const enc = new TextEncoder();
const ab = enc.encode(a);
const bb = enc.encode(b);
if (ab.byteLength !== bb.byteLength) return false;
return crypto.subtle.timingSafeEqual(ab, bb);
}
function authorized(request: Request, env: Env): boolean {
const header = request.headers.get("authorization") ?? "";
const token = header.startsWith("Bearer ")
? header.slice("Bearer ".length)
: "";
return token.length > 0 && timingSafeEqual(token, env.STRATA_INGEST_TOKEN);
}
const PUBLIC_BASE = "https://pub-735e1688181b45e49c925eed81fe2da7.r2.dev";
/** which drained segments hold rows for a namespace, as public parquet URLs the page can hand to duckdb-wasm.
* app.bsky is private by policy, so it is refused here. */
async function partitions(url: URL, env: Env): Promise<Response> {
const ns = url.searchParams.get("ns");
if (ns === null || !/^[a-z0-9-]+\.[a-z0-9-]+$/i.test(ns))
return json({ error: "ns must be an authority like fm.teal" }, 400);
if (ns === "app.bsky")
return json(
{ error: "app.bsky rows are not public; use the aggregates" },
403,
);
const listed = await env.PUBLIC.list({
prefix: `events/ns=${ns}/`,
limit: 1000,
});
const urls = listed.objects.map((o) => `${PUBLIC_BASE}/${o.key}`);
const bytes = listed.objects.reduce((a, o) => a + o.size, 0);
return json({ ns, files: urls, bytes, truncated: listed.truncated }, 200, {
"cache-control": "public, max-age=120",
});
}
async function publishSnapshot(
request: Request,
env: Env,
key: string,
): Promise<Response> {
if (!authorized(request, env)) return json({ error: "unauthorized" }, 401);
if (key === "checkpoint" && request.method === "GET") {
const saved = await env.PRIVATE.get("snapshots/checkpoint");
return saved ? new Response(saved.body) : json({ error: "not found" }, 404);
}
if (request.method === "DELETE" && /^objects\/[a-f0-9]{64}$/.test(key)) {
const current = await env.PRIVATE.get("snapshots/manifest");
if (current) {
const parsed = safeParse(manifestSchema, await current.json());
if (!parsed.ok) return json({ error: "invalid current manifest" }, 500);
if (parsed.value.objects.some((o) => `objects/${o.digest}` === key))
return json({ error: "object in use" }, 409);
}
await env.PRIVATE.delete(`snapshots/${key}`);
return json({ deleted: key });
}
if (request.method !== "PUT")
return json({ error: "method not allowed" }, 405);
if (key === "batch") {
const length = Number(request.headers.get("content-length") ?? "0");
if (!Number.isSafeInteger(length) || length <= 0 || length > 16 << 20)
return json({ error: "invalid content-length" }, 413);
const parsed = safeParse(
array(object({ digest: string(), body: string() })),
await request.json(),
);
if (!parsed.ok || parsed.value.length > 64)
return json({ error: "invalid batch" }, 400);
for (const item of parsed.value) {
const digest = [
...new Uint8Array(
await crypto.subtle.digest(
"SHA-256",
new TextEncoder().encode(item.body),
),
),
]
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
if (digest !== item.digest)
return json({ error: "digest mismatch" }, 400);
}
await Promise.all(
parsed.value.map((item) =>
env.PRIVATE.put(`snapshots/objects/${item.digest}`, item.body, {
httpMetadata: { contentType: "application/json" },
}),
),
);
return json({ saved: parsed.value.length });
}
if (
key !== "manifest" &&
key !== "checkpoint" &&
!/^objects\/[a-f0-9]{64}$/.test(key)
)
return json({ error: "invalid key" }, 400);
const length = Number(request.headers.get("content-length") ?? "0");
if (
!Number.isSafeInteger(length) ||
length <= 0 ||
length > (key === "checkpoint" ? 128 << 20 : 16 << 20)
)
return json({ error: "invalid content-length" }, 413);
if (key === "checkpoint") {
await env.PRIVATE.put("snapshots/checkpoint", request.body);
return json({ saved: key });
}
const body = await request.arrayBuffer();
if (body.byteLength !== length)
return json({ error: "length mismatch" }, 400);
if (key.startsWith("objects/")) {
const digest = [
...new Uint8Array(await crypto.subtle.digest("SHA-256", body)),
]
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
if (key !== `objects/${digest}`)
return json({ error: "digest mismatch" }, 400);
} else {
const parsed = safeParse(
manifestSchema,
JSON.parse(new TextDecoder().decode(body)),
);
if (
!parsed.ok ||
parsed.value.objects.some((o) => !/^[a-f0-9]{64}$/.test(o.digest))
)
return json({ error: "invalid manifest" }, 400);
for (const name of [
"progress",
"segments",
"checksums",
"collections",
"namespaces",
"heat/namespaces",
"heat/collections",
]) {
const digest = parsed.value.objects.find((o) => o.name === name)?.digest;
if (!digest || !(await env.PRIVATE.head(`snapshots/objects/${digest}`)))
return json({ error: `missing ${name}` }, 400);
}
const previous = await env.PRIVATE.get("snapshots/manifest");
if (previous)
await env.PRIVATE.put("snapshots/previous-manifest", previous.body);
}
await env.PRIVATE.put(`snapshots/${key}`, body, {
httpMetadata: { contentType: "application/json" },
});
return json({ saved: key });
}
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext,
): Promise<Response> {
const url = new URL(request.url);
if (url.pathname.startsWith("/snapshot/"))
return publishSnapshot(
request,
env,
url.pathname.slice("/snapshot/".length),
);
const preview = url.pathname.startsWith("/snapshot-preview/api/");
const apiPath = preview
? url.pathname.slice("/snapshot-preview".length)
: url.pathname;
if (
request.method === "GET" &&
[
"/api/progress",
"/api/checksums",
"/api/collections",
"/api/namespaces",
"/api/heat",
].includes(apiPath)
) {
const cache = caches.default;
const cacheUrl = new URL(request.url);
cacheUrl.searchParams.sort();
const cacheKey = new Request(cacheUrl);
const cached = await cache.match(cacheKey);
if (cached) return cached;
url.pathname = apiPath;
const response = await snapshotResponse(url, env.PRIVATE, ctx);
if (response.ok) ctx.waitUntil(cache.put(cacheKey, response.clone()));
return response;
}
if (request.method === "POST" && url.pathname === "/ingest")
return json({ error: "ingestion moved to HeavyPad" }, 410);
if (request.method === "PUT" && url.pathname.startsWith("/archive/"))
return putArchive(request, url.pathname.slice("/archive/".length), env);
if (request.method !== "GET")
return json({ error: "method not allowed" }, 405);
switch (url.pathname) {
case "/api/partitions":
return partitions(url, env);
case "/":
return new Response(page, {
headers: {
"content-type": "text/html; charset=utf-8",
"cache-control": "public, max-age=60",
},
});
case "/ui.js":
return new Response(ui, {
headers: {
"content-type": "text/javascript; charset=utf-8",
"cache-control":
url.searchParams.get("v") === UI_VERSION
? "public, max-age=31536000, immutable"
: "no-cache",
},
});
case "/api":
return json({
name: "strata",
what: "per-segment, per-collection event counts of the stream.waow.tech archive",
endpoints: [
"/api/progress",
"/api/heat?step=N&top=N&group=namespaces|collections",
"/api/heat?step=N&ns=<authority>",
"/api/namespaces",
"/api/collections?ns=<authority>",
"/api/partitions?ns=<authority>",
],
source: "https://tangled.org/zat.dev/stream/tree/main/strata",
});
default:
return json({ error: "not found" }, 404);
}
},
} satisfies ExportedHandler<Env>;