@hatk/hatk 0.0.1-alpha.94 → 0.0.1-alpha.96
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/classifiers.d.ts +168 -0
- package/dist/classifiers.d.ts.map +1 -0
- package/dist/classifiers.js +370 -0
- package/dist/cli.js +1 -0
- package/dist/config.d.ts +14 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +11 -0
- package/dist/database/db.d.ts +78 -1
- package/dist/database/db.d.ts.map +1 -1
- package/dist/database/db.js +253 -1
- package/dist/database/index.d.ts +1 -1
- package/dist/database/index.d.ts.map +1 -1
- package/dist/database/index.js +1 -1
- package/dist/database/schema.d.ts +27 -0
- package/dist/database/schema.d.ts.map +1 -1
- package/dist/database/schema.js +38 -21
- package/dist/dev-entry.d.ts.map +1 -1
- package/dist/dev-entry.js +3 -4
- package/dist/hydrate.d.ts +8 -2
- package/dist/hydrate.d.ts.map +1 -1
- package/dist/main.js +8 -5
- package/dist/oauth/server.d.ts +36 -0
- package/dist/oauth/server.d.ts.map +1 -1
- package/dist/oauth/server.js +78 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +95 -6
- package/dist/spaces/blob.d.ts +67 -6
- package/dist/spaces/blob.d.ts.map +1 -1
- package/dist/spaces/blob.js +371 -22
- package/dist/spaces/index.d.ts +1 -1
- package/dist/spaces/index.d.ts.map +1 -1
- package/dist/spaces/index.js +1 -1
- package/dist/xrpc.d.ts +21 -0
- package/dist/xrpc.d.ts.map +1 -1
- package/dist/xrpc.js +40 -25
- package/package.json +10 -2
- package/public/admin.html +467 -0
package/dist/spaces/blob.js
CHANGED
|
@@ -8,9 +8,12 @@
|
|
|
8
8
|
* public URL would turn that URL into the capability the credential exists to
|
|
9
9
|
* replace.
|
|
10
10
|
*
|
|
11
|
-
* So the bytes come through here,
|
|
12
|
-
* viewer's
|
|
13
|
-
*
|
|
11
|
+
* So the bytes come through here, and every request is authorized before any
|
|
12
|
+
* of them move: the viewer's credential for the space, or the same 404 a
|
|
13
|
+
* missing blob gets. What that authorization guards is served two ways —
|
|
14
|
+
* `private` so the viewer's own browser may hold it and no shared cache may,
|
|
15
|
+
* and out of a bounded in-memory cache so the same photo is fetched from its
|
|
16
|
+
* repo once rather than once per member looking at it.
|
|
14
17
|
*/
|
|
15
18
|
import { emit } from "../logger.js";
|
|
16
19
|
import { repoEndpoint } from "./identity.js";
|
|
@@ -24,15 +27,74 @@ import { viewerCredential } from "./viewer.js";
|
|
|
24
27
|
* what a browser executes in this origin.
|
|
25
28
|
*/
|
|
26
29
|
const SERVABLE = new Set(['image/jpeg', 'image/png', 'image/webp', 'image/avif', 'image/gif']);
|
|
30
|
+
/**
|
|
31
|
+
* How long a browser may reuse a blob it has before it asks again.
|
|
32
|
+
*
|
|
33
|
+
* Every request that does arrive is authorized, so this is the window in which
|
|
34
|
+
* a viewer who has just lost access still sees what they had. A minute is
|
|
35
|
+
* small against what the surrounding design already accepts: the readable set
|
|
36
|
+
* is cached for five, and the space host lets a minted credential outlive a
|
|
37
|
+
* revocation by up to two hours.
|
|
38
|
+
*/
|
|
39
|
+
const MAX_AGE_S = 60;
|
|
40
|
+
/**
|
|
41
|
+
* Bytes held across every cached blob, and the largest single one worth
|
|
42
|
+
* holding. A blob past the entry limit streams straight through instead —
|
|
43
|
+
* caching it would evict a great many small ones to save one fetch.
|
|
44
|
+
*/
|
|
45
|
+
const CACHE_BYTES = 64 * 1024 * 1024;
|
|
46
|
+
const MAX_ENTRY_BYTES = 4 * 1024 * 1024;
|
|
47
|
+
/**
|
|
48
|
+
* How much of an original we will read in order to make a smaller one.
|
|
49
|
+
*
|
|
50
|
+
* Larger than what we hold, and deliberately: a photograph straight off a
|
|
51
|
+
* camera is exactly the case where resizing pays, and refusing to read it
|
|
52
|
+
* would be refusing the request that needs answering most. What comes back out
|
|
53
|
+
* is a derivative small enough to keep.
|
|
54
|
+
*/
|
|
55
|
+
const MAX_SOURCE_BYTES = 20 * 1024 * 1024;
|
|
56
|
+
/**
|
|
57
|
+
* The sizes a space blob is served at.
|
|
58
|
+
*
|
|
59
|
+
* The same words hatk's public path uses for imgproxy, with the same numbers,
|
|
60
|
+
* so an app naming a size says it once and means it either side of the line
|
|
61
|
+
* between a public blob and a permissioned one. What differs is who resizes:
|
|
62
|
+
* a space blob may not pass through an image CDN, so it happens here.
|
|
63
|
+
*
|
|
64
|
+
* A request naming none gets the original, which is what every caller written
|
|
65
|
+
* before presets existed asks for.
|
|
66
|
+
*/
|
|
67
|
+
const PRESETS = {
|
|
68
|
+
avatar: { width: 1000, height: 1000, fit: 'cover', quality: 85 },
|
|
69
|
+
avatar_thumbnail: { width: 128, height: 128, fit: 'cover', quality: 70 },
|
|
70
|
+
feed_thumbnail: { width: 1000, fit: 'inside', quality: 70 },
|
|
71
|
+
feed_fullsize: { quality: 90 },
|
|
72
|
+
banner: { width: 3000, height: 1000, fit: 'cover', quality: 85 },
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Only a name from that list is a preset.
|
|
76
|
+
*
|
|
77
|
+
* Free-form dimensions would let anyone mint unlimited distinct cache keys and
|
|
78
|
+
* evict everything real, and would put the cost of an arbitrary resize behind
|
|
79
|
+
* a URL. An unrecognised name is treated as none rather than refused: a broken
|
|
80
|
+
* image is a worse answer than a large one.
|
|
81
|
+
*/
|
|
82
|
+
export function asPreset(name) {
|
|
83
|
+
return name && name in PRESETS ? name : undefined;
|
|
84
|
+
}
|
|
85
|
+
/** Resizing is skipped for these: one frame of an animation is not the animation. */
|
|
86
|
+
const ANIMATED = new Set(['image/gif']);
|
|
27
87
|
/**
|
|
28
88
|
* The URL this appview serves a space blob at, for whoever is asking.
|
|
29
89
|
*
|
|
30
90
|
* Built here rather than by the app, because the route is hatk's: a blob in a
|
|
31
91
|
* space has no public address by design, and this is the one address it does
|
|
32
|
-
* have — answered
|
|
92
|
+
* have — answered only to a viewer who may read the space it names.
|
|
33
93
|
*/
|
|
34
|
-
export function spaceBlobUrl(space, repo, cid) {
|
|
94
|
+
export function spaceBlobUrl(space, repo, cid, preset) {
|
|
35
95
|
const params = new URLSearchParams({ space, repo, cid });
|
|
96
|
+
if (preset)
|
|
97
|
+
params.set('preset', preset);
|
|
36
98
|
return `/space-blob?${params}`;
|
|
37
99
|
}
|
|
38
100
|
/**
|
|
@@ -61,7 +123,114 @@ export function parseSpaceBlobRequest(params) {
|
|
|
61
123
|
return null;
|
|
62
124
|
if (!parseSpaceRef(space) || !repo.startsWith('did:'))
|
|
63
125
|
return null;
|
|
64
|
-
|
|
126
|
+
const preset = asPreset(params.get('preset'));
|
|
127
|
+
return { space, repo, cid, ...(preset ? { preset } : {}) };
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Blobs held in memory, least recently used first.
|
|
131
|
+
*
|
|
132
|
+
* In memory and not on disk on purpose. These are members' private photos, and
|
|
133
|
+
* a cache that survives a restart is a store of them: this one is bounded,
|
|
134
|
+
* forgotten when the process ends, and never written anywhere a backup would
|
|
135
|
+
* find it.
|
|
136
|
+
*/
|
|
137
|
+
const cache = new Map();
|
|
138
|
+
let cachedBytes = 0;
|
|
139
|
+
/**
|
|
140
|
+
* Fetches in flight, so a page opening with the same image in it twenty times
|
|
141
|
+
* fetches it once. Keyed like the cache.
|
|
142
|
+
*/
|
|
143
|
+
const inFlight = new Map();
|
|
144
|
+
/**
|
|
145
|
+
* The space is part of the key, and has to be.
|
|
146
|
+
*
|
|
147
|
+
* A CID names bytes, so caching by CID alone would hit across spaces — and the
|
|
148
|
+
* check that a blob is actually referenced from the space being named lives
|
|
149
|
+
* upstream, in `com.atproto.space.getBlob`. A viewer who may read one space
|
|
150
|
+
* could then name any CID they had heard of and be served it out of the cache
|
|
151
|
+
* without that check ever running.
|
|
152
|
+
*/
|
|
153
|
+
const keyFor = (request) => `${request.space}\n${request.cid}\n${request.preset ?? ''}`;
|
|
154
|
+
function cacheGet(key) {
|
|
155
|
+
const entry = cache.get(key);
|
|
156
|
+
if (!entry)
|
|
157
|
+
return undefined;
|
|
158
|
+
// Re-inserted so iteration order is least-recently-used first.
|
|
159
|
+
cache.delete(key);
|
|
160
|
+
cache.set(key, entry);
|
|
161
|
+
return entry;
|
|
162
|
+
}
|
|
163
|
+
function cachePut(key, entry) {
|
|
164
|
+
if (entry.bytes.byteLength > MAX_ENTRY_BYTES)
|
|
165
|
+
return;
|
|
166
|
+
const existing = cache.get(key);
|
|
167
|
+
if (existing)
|
|
168
|
+
cachedBytes -= existing.bytes.byteLength;
|
|
169
|
+
cache.set(key, entry);
|
|
170
|
+
cachedBytes += entry.bytes.byteLength;
|
|
171
|
+
while (cachedBytes > CACHE_BYTES) {
|
|
172
|
+
const oldest = cache.keys().next();
|
|
173
|
+
if (oldest.done)
|
|
174
|
+
break;
|
|
175
|
+
cachedBytes -= cache.get(oldest.value).bytes.byteLength;
|
|
176
|
+
cache.delete(oldest.value);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/** Drop everything held. For tests, and for an operator who wants it gone. */
|
|
180
|
+
export function resetSpaceBlobCache() {
|
|
181
|
+
cache.clear();
|
|
182
|
+
inFlight.clear();
|
|
183
|
+
cachedBytes = 0;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* A CID is a hash of the bytes it names, which is exactly what an ETag is for.
|
|
187
|
+
* Strong, free, and stable for as long as the blob exists. The preset is part
|
|
188
|
+
* of it because it is part of what comes back.
|
|
189
|
+
*/
|
|
190
|
+
const etagFor = (request) => request.preset ? `"${request.cid}-${request.preset}"` : `"${request.cid}"`;
|
|
191
|
+
/** Whether the browser already holds this blob. `*` matches anything it has. */
|
|
192
|
+
function matches(ifNoneMatch, etag) {
|
|
193
|
+
if (!ifNoneMatch)
|
|
194
|
+
return false;
|
|
195
|
+
return ifNoneMatch
|
|
196
|
+
.split(',')
|
|
197
|
+
.map((candidate) => candidate.trim().replace(/^W\//, ''))
|
|
198
|
+
.some((candidate) => candidate === '*' || candidate === etag);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Headers every answer carries, whether it has a body or not.
|
|
202
|
+
*
|
|
203
|
+
* `private` is the whole of the caching posture: the viewer's own browser may
|
|
204
|
+
* keep what it was shown, and no cache between here and there may. `vary`
|
|
205
|
+
* keeps it honest on a browser where two people sign in — the response turns
|
|
206
|
+
* on who asked, and who asked is the cookie or the token.
|
|
207
|
+
*/
|
|
208
|
+
function cacheHeaders(request) {
|
|
209
|
+
return {
|
|
210
|
+
etag: etagFor(request),
|
|
211
|
+
'cache-control': `private, max-age=${MAX_AGE_S}, must-revalidate`,
|
|
212
|
+
vary: 'cookie, authorization',
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function serve(entry, request) {
|
|
216
|
+
return new Response(entry.bytes, {
|
|
217
|
+
status: 200,
|
|
218
|
+
headers: {
|
|
219
|
+
'content-type': entry.contentType,
|
|
220
|
+
...cacheHeaders(request),
|
|
221
|
+
'content-disposition': 'inline',
|
|
222
|
+
'content-security-policy': "default-src 'none'; sandbox",
|
|
223
|
+
'x-content-type-options': 'nosniff',
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
/** A refusal from the repo host, carried out through the in-flight promise. */
|
|
228
|
+
class UpstreamRefusal extends Error {
|
|
229
|
+
status;
|
|
230
|
+
constructor(status) {
|
|
231
|
+
super(`upstream refused with ${status}`);
|
|
232
|
+
this.status = status;
|
|
233
|
+
}
|
|
65
234
|
}
|
|
66
235
|
/**
|
|
67
236
|
* Fetch a space blob as this viewer, or answer why not.
|
|
@@ -70,42 +239,222 @@ export function parseSpaceBlobRequest(params) {
|
|
|
70
239
|
* there. The distinction between "you may not" and "it does not exist" is
|
|
71
240
|
* itself information about a private space, and the space host draws the line
|
|
72
241
|
* the same way when it reports a non-member's repo as simply absent.
|
|
242
|
+
*
|
|
243
|
+
* The credential is checked on every request, including the ones answered from
|
|
244
|
+
* the cache and the ones answered with a 304. Nothing here is ever served to
|
|
245
|
+
* somebody the authority has not just said yes to.
|
|
73
246
|
*/
|
|
74
|
-
export async function serveSpaceBlob(oauth, viewer, request) {
|
|
247
|
+
export async function serveSpaceBlob(oauth, viewer, request, ifNoneMatch) {
|
|
75
248
|
if (!viewer)
|
|
76
249
|
return new Response('Unauthorized', { status: 401 });
|
|
77
250
|
const credential = await viewerCredential(oauth, viewer, request.space);
|
|
78
251
|
if (!credential)
|
|
79
252
|
return new Response('Not found', { status: 404 });
|
|
253
|
+
// The browser has it already. Authorized above, so this is a viewer who may
|
|
254
|
+
// go on looking at what they hold — and it costs neither of us the bytes.
|
|
255
|
+
if (matches(ifNoneMatch, etagFor(request))) {
|
|
256
|
+
return new Response(null, { status: 304, headers: cacheHeaders(request) });
|
|
257
|
+
}
|
|
258
|
+
const key = keyFor(request);
|
|
259
|
+
const hit = cacheGet(key);
|
|
260
|
+
if (hit)
|
|
261
|
+
return serve(hit, request);
|
|
262
|
+
let entry;
|
|
263
|
+
try {
|
|
264
|
+
entry = await (inFlight.get(key) ?? fetchOnce(key, credential, request));
|
|
265
|
+
}
|
|
266
|
+
catch (err) {
|
|
267
|
+
if (err instanceof UpstreamRefusal)
|
|
268
|
+
return new Response('Not found', { status: err.status });
|
|
269
|
+
emit('spaces', 'blob_error', { space: request.space, repo: request.repo, error: err.message });
|
|
270
|
+
return new Response('Blob unavailable', { status: 502 });
|
|
271
|
+
}
|
|
272
|
+
// Too large to hold, which the read above discovered from its headers and
|
|
273
|
+
// then dropped. Asking again costs a round trip and no bytes, and buys a
|
|
274
|
+
// body that is streamed rather than gathered into memory to be served once.
|
|
275
|
+
if (!entry)
|
|
276
|
+
return streamThrough(credential, request);
|
|
277
|
+
return serve(entry, request);
|
|
278
|
+
}
|
|
279
|
+
function blobUrlFor(endpoint, request) {
|
|
280
|
+
const url = new URL(`${endpoint}/xrpc/com.atproto.space.getBlob`);
|
|
281
|
+
url.searchParams.set('space', request.space);
|
|
282
|
+
// `repo`, not `did`: a space read names the repo holding the record the
|
|
283
|
+
// same way everywhere. A server hosting one account falls back to it,
|
|
284
|
+
// which is what makes `did` ever appear to work.
|
|
285
|
+
url.searchParams.set('repo', request.repo);
|
|
286
|
+
url.searchParams.set('cid', request.cid);
|
|
287
|
+
return url;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* The upstream read, shared by everyone who asked for this blob at once.
|
|
291
|
+
*
|
|
292
|
+
* Resolves to null for a blob too large to hold, which the caller streams
|
|
293
|
+
* instead. A failure rejects, and rejects for all of them: one refusal is
|
|
294
|
+
* every caller's refusal, because the credential each of them presented had
|
|
295
|
+
* already been accepted for this space.
|
|
296
|
+
*/
|
|
297
|
+
function fetchOnce(key, credential, request) {
|
|
298
|
+
const task = (async () => {
|
|
299
|
+
const upstream = await credential.fetch(blobUrlFor(await repoEndpoint(request.repo), request));
|
|
300
|
+
if (!upstream.ok) {
|
|
301
|
+
// A credential the repo host rejects reads as absent, not as
|
|
302
|
+
// unauthorized: the viewer holds one, so what they are being told is
|
|
303
|
+
// that this blob is not theirs to see.
|
|
304
|
+
throw new UpstreamRefusal(upstream.status === 401 ? 404 : upstream.status);
|
|
305
|
+
}
|
|
306
|
+
const contentType = servableType(upstream);
|
|
307
|
+
const limit = request.preset ? MAX_SOURCE_BYTES : MAX_ENTRY_BYTES;
|
|
308
|
+
const declared = Number(upstream.headers.get('content-length') ?? NaN);
|
|
309
|
+
if (declared > limit) {
|
|
310
|
+
await upstream.body?.cancel();
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
// Read against the limit rather than trusting the length, which a repo host
|
|
314
|
+
// is free not to send and free to get wrong.
|
|
315
|
+
const source = await readBounded(upstream, limit);
|
|
316
|
+
if (!source)
|
|
317
|
+
return null;
|
|
318
|
+
// A resize that cannot happen is not a failure to serve. Sharp missing, an
|
|
319
|
+
// animation, or an image it cannot read all end here with the original,
|
|
320
|
+
// which is the answer this route gave before presets existed.
|
|
321
|
+
const bytes = request.preset ? ((await resize(source, contentType, request.preset)) ?? source) : source;
|
|
322
|
+
const entry = { bytes, contentType };
|
|
323
|
+
cachePut(key, entry);
|
|
324
|
+
return entry;
|
|
325
|
+
})().finally(() => {
|
|
326
|
+
if (inFlight.get(key) === task)
|
|
327
|
+
inFlight.delete(key);
|
|
328
|
+
});
|
|
329
|
+
inFlight.set(key, task);
|
|
330
|
+
return task;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* The whole body, or null once it is plainly too big to hold.
|
|
334
|
+
*
|
|
335
|
+
* Bounded as it reads: a body that goes past the limit is abandoned there, so
|
|
336
|
+
* the most this ever holds is one blob's worth over the limit by one chunk.
|
|
337
|
+
*/
|
|
338
|
+
async function readBounded(upstream, limit) {
|
|
339
|
+
if (!upstream.body)
|
|
340
|
+
return new Uint8Array(0);
|
|
341
|
+
const reader = upstream.body.getReader();
|
|
342
|
+
const chunks = [];
|
|
343
|
+
let total = 0;
|
|
344
|
+
try {
|
|
345
|
+
while (true) {
|
|
346
|
+
const { done, value } = await reader.read();
|
|
347
|
+
if (done)
|
|
348
|
+
break;
|
|
349
|
+
total += value.byteLength;
|
|
350
|
+
if (total > limit) {
|
|
351
|
+
await reader.cancel();
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
chunks.push(value);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
finally {
|
|
358
|
+
reader.releaseLock();
|
|
359
|
+
}
|
|
360
|
+
const bytes = new Uint8Array(total);
|
|
361
|
+
let at = 0;
|
|
362
|
+
for (const chunk of chunks) {
|
|
363
|
+
bytes.set(chunk, at);
|
|
364
|
+
at += chunk.byteLength;
|
|
365
|
+
}
|
|
366
|
+
return bytes;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Sharp, if this deployment has it.
|
|
370
|
+
*
|
|
371
|
+
* An optional peer dependency: hatk is a library, and most of what installs it
|
|
372
|
+
* never serves a permissioned space at all. Resolved once — a missing module
|
|
373
|
+
* is answered from the same settled promise rather than re-attempted per
|
|
374
|
+
* image.
|
|
375
|
+
*/
|
|
376
|
+
let sharpModule;
|
|
377
|
+
function loadSharp() {
|
|
378
|
+
sharpModule ??= import('sharp')
|
|
379
|
+
.then((m) => m.default)
|
|
380
|
+
.catch(() => {
|
|
381
|
+
emit('spaces', 'resize_unavailable', { reason: 'sharp is not installed' });
|
|
382
|
+
return null;
|
|
383
|
+
});
|
|
384
|
+
return sharpModule;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* The blob at the size it is drawn, or null to serve the original instead.
|
|
388
|
+
*
|
|
389
|
+
* Orientation is applied before the resize and the metadata that carried it is
|
|
390
|
+
* dropped with everything else — which also means the GPS in a ride photo's
|
|
391
|
+
* EXIF stops being served to everyone the space admits.
|
|
392
|
+
*
|
|
393
|
+
* The format is kept rather than normalised to JPEG. Re-encoding a PNG that
|
|
394
|
+
* way silently flattens its transparency, and no size is worth that.
|
|
395
|
+
*
|
|
396
|
+
* Null when the original is the better answer — an animation, an image sharp
|
|
397
|
+
* cannot read, sharp absent, or a derivative that came out no smaller.
|
|
398
|
+
*/
|
|
399
|
+
async function resize(source, contentType, preset) {
|
|
400
|
+
if (ANIMATED.has(contentType))
|
|
401
|
+
return null;
|
|
402
|
+
const sharp = await loadSharp();
|
|
403
|
+
if (!sharp)
|
|
404
|
+
return null;
|
|
405
|
+
const { width, height, fit, quality } = PRESETS[preset];
|
|
406
|
+
try {
|
|
407
|
+
let pipeline = sharp(source).rotate();
|
|
408
|
+
if (width)
|
|
409
|
+
pipeline = pipeline.resize({ width, height, fit, withoutEnlargement: true });
|
|
410
|
+
// Named explicitly rather than left to sharp, which keeps the input format
|
|
411
|
+
// but not the quality — without this a preset that only sets quality, like
|
|
412
|
+
// a full-size one, would re-encode at sharp's default and do nothing.
|
|
413
|
+
if (contentType === 'image/jpeg')
|
|
414
|
+
pipeline = pipeline.jpeg({ quality, mozjpeg: true });
|
|
415
|
+
else if (contentType === 'image/webp')
|
|
416
|
+
pipeline = pipeline.webp({ quality });
|
|
417
|
+
else if (contentType === 'image/avif')
|
|
418
|
+
pipeline = pipeline.avif({ quality });
|
|
419
|
+
else if (contentType === 'image/png')
|
|
420
|
+
pipeline = pipeline.png({ compressionLevel: 9 });
|
|
421
|
+
const out = await pipeline.toBuffer();
|
|
422
|
+
// A derivative bigger than what it came from is a worse answer than the
|
|
423
|
+
// original — small images re-encoded at a higher quality can land there.
|
|
424
|
+
if (out.byteLength >= source.byteLength)
|
|
425
|
+
return null;
|
|
426
|
+
return new Uint8Array(out.buffer, out.byteOffset, out.byteLength);
|
|
427
|
+
}
|
|
428
|
+
catch (err) {
|
|
429
|
+
emit('spaces', 'resize_failed', { preset, content_type: contentType, error: err?.message ?? String(err) });
|
|
430
|
+
return null;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
/** A blob too large to cache, passed through as it arrives. */
|
|
434
|
+
async function streamThrough(credential, request) {
|
|
80
435
|
let upstream;
|
|
81
436
|
try {
|
|
82
|
-
|
|
83
|
-
url.searchParams.set('space', request.space);
|
|
84
|
-
// `repo`, not `did`: a space read names the repo holding the record the
|
|
85
|
-
// same way everywhere. A server hosting one account falls back to it,
|
|
86
|
-
// which is what makes `did` ever appear to work.
|
|
87
|
-
url.searchParams.set('repo', request.repo);
|
|
88
|
-
url.searchParams.set('cid', request.cid);
|
|
89
|
-
upstream = await credential.fetch(url);
|
|
437
|
+
upstream = await credential.fetch(blobUrlFor(await repoEndpoint(request.repo), request));
|
|
90
438
|
}
|
|
91
439
|
catch (err) {
|
|
92
440
|
emit('spaces', 'blob_error', { space: request.space, repo: request.repo, error: err.message });
|
|
93
441
|
return new Response('Blob unavailable', { status: 502 });
|
|
94
442
|
}
|
|
95
|
-
if (!upstream.ok)
|
|
443
|
+
if (!upstream.ok) {
|
|
96
444
|
return new Response('Not found', { status: upstream.status === 401 ? 404 : upstream.status });
|
|
97
|
-
|
|
98
|
-
const contentType = SERVABLE.has(claimed) ? claimed : 'application/octet-stream';
|
|
445
|
+
}
|
|
99
446
|
return new Response(upstream.body, {
|
|
100
447
|
status: 200,
|
|
101
448
|
headers: {
|
|
102
|
-
'content-type':
|
|
103
|
-
|
|
104
|
-
// request for the same URL may be somebody with no right to it.
|
|
105
|
-
'cache-control': 'private, no-store',
|
|
449
|
+
'content-type': servableType(upstream),
|
|
450
|
+
...cacheHeaders(request),
|
|
106
451
|
'content-disposition': 'inline',
|
|
107
452
|
'content-security-policy': "default-src 'none'; sandbox",
|
|
108
453
|
'x-content-type-options': 'nosniff',
|
|
109
454
|
},
|
|
110
455
|
});
|
|
111
456
|
}
|
|
457
|
+
function servableType(upstream) {
|
|
458
|
+
const claimed = (upstream.headers.get('content-type') ?? '').split(';')[0].trim();
|
|
459
|
+
return SERVABLE.has(claimed) ? claimed : 'application/octet-stream';
|
|
460
|
+
}
|
package/dist/spaces/index.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ export declare function serverDpopProof(method: string, url: string, accessToken
|
|
|
22
22
|
export { handleWriteNotice } from './engine.ts';
|
|
23
23
|
export { clearPendingNotices, NoticeError, parseWriteNotice, scheduleNoticeSync, verifyNotice, type WriteNotice, } from './notify.ts';
|
|
24
24
|
export * from './uri.ts';
|
|
25
|
-
export { blobCid, spaceBlobUrl } from './blob.ts';
|
|
25
|
+
export { asPreset, blobCid, resetSpaceBlobCache, type SpaceBlobPreset, spaceBlobUrl } from './blob.ts';
|
|
26
26
|
export { records, requireSpace, spaceRecords } from './records.ts';
|
|
27
27
|
export declare function spaceServiceId(): string | null;
|
|
28
28
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/spaces/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAG7D,OAAO,EAA+D,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAOnH,OAAO,EACL,uBAAuB,EACvB,YAAY,EACZ,cAAc,EACd,aAAa,EACb,YA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AG,EAAE,MAAM,CAAA;IACX,6CAA6C;IAC7C,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;CACzB;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,kBAAkB,GAAG,IAAI,CAqD1D;AAED,wBAAgB,UAAU,IAAI,IAAI,CAMjC;AAED,+FAA+F;AAC/F,OAAO,EAAE,UAAU,IAAI,WAAW,EAAE,YAAY,IAAI,aAAa,EAAE,CAAA"}
|
package/dist/spaces/index.js
CHANGED
|
@@ -33,7 +33,7 @@ export async function serverDpopProof(method, url, accessToken, nonce) {
|
|
|
33
33
|
export { handleWriteNotice } from "./engine.js";
|
|
34
34
|
export { clearPendingNotices, NoticeError, parseWriteNotice, scheduleNoticeSync, verifyNotice, } from "./notify.js";
|
|
35
35
|
export * from "./uri.js";
|
|
36
|
-
export { blobCid, spaceBlobUrl } from "./blob.js";
|
|
36
|
+
export { asPreset, blobCid, resetSpaceBlobCache, spaceBlobUrl } from "./blob.js";
|
|
37
37
|
export { records, requireSpace, spaceRecords } from "./records.js";
|
|
38
38
|
let sweepTimer = null;
|
|
39
39
|
/**
|
package/dist/xrpc.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { BaseContext } from './hydrate.ts';
|
|
|
2
2
|
import type { Row, FlatRow } from './lex-types.ts';
|
|
3
3
|
import type { OAuthConfig, CdnConfig } from './config.ts';
|
|
4
4
|
import type { PdsXrpcOptions } from './pds-proxy.ts';
|
|
5
|
+
import { type ObtainedSession } from './oauth/server.ts';
|
|
5
6
|
export type { Row, FlatRow };
|
|
6
7
|
/** Set the OAuth config used for record write helpers. Called once during boot. */
|
|
7
8
|
export declare function configureOAuth(config: OAuthConfig | null): void;
|
|
@@ -81,7 +82,27 @@ export interface XrpcContext<P = Record<string, string>, Records extends Record<
|
|
|
81
82
|
* the granted scopes still apply. See {@link pdsXrpc}.
|
|
82
83
|
*/
|
|
83
84
|
pds: (nsid: string, options?: PdsXrpcOptions) => Promise<Record<string, unknown>>;
|
|
85
|
+
/**
|
|
86
|
+
* The record helpers and `pds` above, acting as another account this app
|
|
87
|
+
* holds a session for — one it obtained with {@link obtainSession}, say.
|
|
88
|
+
* Nothing here checks that the viewer may act for that account: that is the
|
|
89
|
+
* app's decision, made before calling.
|
|
90
|
+
*/
|
|
91
|
+
asAccount: (did: string) => AccountHelpers;
|
|
92
|
+
/**
|
|
93
|
+
* Obtain a session for another account straight from its authorization
|
|
94
|
+
* server, outside the redirect flow — a group host creating an account for
|
|
95
|
+
* this app, for instance. POSTs `body` to `url` as this app's OAuth client,
|
|
96
|
+
* with DPoP, and stores the session the response carries for its `sub`, so
|
|
97
|
+
* {@link asAccount} works for it afterwards. See `obtainSession` in
|
|
98
|
+
* `oauth/server.ts`.
|
|
99
|
+
*/
|
|
100
|
+
obtainSession: (url: string, body: Record<string, unknown>, opts?: {
|
|
101
|
+
headers?: Record<string, string>;
|
|
102
|
+
}) => Promise<ObtainedSession>;
|
|
84
103
|
}
|
|
104
|
+
/** The PDS helpers on {@link XrpcContext}, for one account. */
|
|
105
|
+
export type AccountHelpers = Pick<XrpcContext, 'createRecord' | 'putRecord' | 'deleteRecord' | 'applyWrites' | 'pds'>;
|
|
85
106
|
/** Set the relay URL used for blob URL generation. Called once during boot. */
|
|
86
107
|
/** True when the relay is a loopback URL — a dev PDS rather than the network. */
|
|
87
108
|
export declare function isLocalRelay(): boolean;
|
package/dist/xrpc.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"xrpc.d.ts","sourceRoot":"","sources":["../src/xrpc.ts"],"names":[],"mappings":"AAuCA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAE/C,OAAO,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAElD,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAEzD,OAAO,KAAK,EAAE,cAAc,EAAE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CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,EAC/C,KAAK,CAAC,EAAE,OAAO,KACZ,OAAO,CAAC,GAAG,CAAC,GAChB,IAAI,CAEN;AAED,+CAA+C;AAC/C,wBAAgB,QAAQ,IAAI,MAAM,EAAE,CAEnC"}
|
package/dist/xrpc.js
CHANGED
|
@@ -35,6 +35,7 @@ import { resolveRecords, buildBaseContext } from "./hydrate.js";
|
|
|
35
35
|
import { getLexicon } from "./database/schema.js";
|
|
36
36
|
import { createHmac } from 'node:crypto';
|
|
37
37
|
import { pdsCreateRecord, pdsPutRecord, pdsDeleteRecord, pdsApplyWrites, pdsXrpc } from "./pds-proxy.js";
|
|
38
|
+
import { obtainSession, ObtainSessionError } from "./oauth/server.js";
|
|
38
39
|
let _oauthConfig = null;
|
|
39
40
|
/** Set the OAuth config used for record write helpers. Called once during boot. */
|
|
40
41
|
export function configureOAuth(config) {
|
|
@@ -136,40 +137,54 @@ export function buildXrpcContext(params, cursor, limit, viewer, input) {
|
|
|
136
137
|
const uri = await findUriByFields(collection, conditions);
|
|
137
138
|
return uri !== null;
|
|
138
139
|
},
|
|
139
|
-
|
|
140
|
+
...accountHelpers(viewer),
|
|
141
|
+
asAccount: (did) => accountHelpers({ did }),
|
|
142
|
+
obtainSession: async (url, body, opts) => {
|
|
140
143
|
if (!_oauthConfig)
|
|
141
|
-
throw new Error('No OAuth config — cannot
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
144
|
+
throw new Error('No OAuth config — cannot obtain a session');
|
|
145
|
+
try {
|
|
146
|
+
return await obtainSession(_oauthConfig, url, body, opts);
|
|
147
|
+
}
|
|
148
|
+
catch (err) {
|
|
149
|
+
if (err instanceof ObtainSessionError)
|
|
150
|
+
throw new InvalidRequestError(err.message, err.error);
|
|
151
|
+
throw err;
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* The PDS helpers for one account, with the session this app holds for it: the
|
|
158
|
+
* viewer's, or another account's through {@link XrpcContext.asAccount}.
|
|
159
|
+
*/
|
|
160
|
+
function accountHelpers(account) {
|
|
161
|
+
const ready = (what) => {
|
|
162
|
+
if (!_oauthConfig)
|
|
163
|
+
throw new Error(`No OAuth config — cannot ${what}`);
|
|
164
|
+
if (!account)
|
|
165
|
+
throw new Error(`Authentication required to ${what}`);
|
|
166
|
+
return { config: _oauthConfig, account };
|
|
167
|
+
};
|
|
168
|
+
return {
|
|
169
|
+
createRecord: async (collection, record, opts) => {
|
|
170
|
+
const { config, account } = ready('write records');
|
|
171
|
+
return pdsCreateRecord(config, account, { collection, record, rkey: opts?.rkey });
|
|
145
172
|
},
|
|
146
173
|
putRecord: async (collection, rkey, record) => {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if (!viewer)
|
|
150
|
-
throw new Error('Authentication required to write records');
|
|
151
|
-
return pdsPutRecord(_oauthConfig, viewer, { collection, rkey, record });
|
|
174
|
+
const { config, account } = ready('write records');
|
|
175
|
+
return pdsPutRecord(config, account, { collection, rkey, record });
|
|
152
176
|
},
|
|
153
177
|
deleteRecord: async (collection, rkey) => {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (!viewer)
|
|
157
|
-
throw new Error('Authentication required to write records');
|
|
158
|
-
await pdsDeleteRecord(_oauthConfig, viewer, { collection, rkey });
|
|
178
|
+
const { config, account } = ready('write records');
|
|
179
|
+
await pdsDeleteRecord(config, account, { collection, rkey });
|
|
159
180
|
},
|
|
160
181
|
applyWrites: async (writes) => {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (!viewer)
|
|
164
|
-
throw new Error('Authentication required to write records');
|
|
165
|
-
return pdsApplyWrites(_oauthConfig, viewer, { writes });
|
|
182
|
+
const { config, account } = ready('write records');
|
|
183
|
+
return pdsApplyWrites(config, account, { writes });
|
|
166
184
|
},
|
|
167
185
|
pds: async (nsid, options) => {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (!viewer)
|
|
171
|
-
throw new Error('Authentication required to call the PDS');
|
|
172
|
-
return pdsXrpc(_oauthConfig, viewer, nsid, options);
|
|
186
|
+
const { config, account } = ready('call the PDS');
|
|
187
|
+
return pdsXrpc(config, account, nsid, options);
|
|
173
188
|
},
|
|
174
189
|
};
|
|
175
190
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hatk/hatk",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.96",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"bin": {
|
|
6
6
|
"hatk": "dist/cli.js"
|
|
@@ -26,7 +26,8 @@
|
|
|
26
26
|
"./config": "./dist/config.js",
|
|
27
27
|
"./vite-plugin": "./dist/vite-plugin.js",
|
|
28
28
|
"./renderer": "./dist/renderer.js",
|
|
29
|
-
"./spaces": "./dist/spaces/index.js"
|
|
29
|
+
"./spaces": "./dist/spaces/index.js",
|
|
30
|
+
"./classifiers": "./dist/classifiers.js"
|
|
30
31
|
},
|
|
31
32
|
"scripts": {
|
|
32
33
|
"clean": "rm -rf dist",
|
|
@@ -50,9 +51,16 @@
|
|
|
50
51
|
"@types/better-sqlite3": "^7.6.13",
|
|
51
52
|
"@types/react": "^19.2.14",
|
|
52
53
|
"@vitest/coverage-v8": "^4.1.11",
|
|
54
|
+
"sharp": "^0.35.4",
|
|
53
55
|
"vite": "^8.0.0"
|
|
54
56
|
},
|
|
55
57
|
"peerDependencies": {
|
|
58
|
+
"sharp": "^0.35.4",
|
|
56
59
|
"vite": "^8.0.0"
|
|
60
|
+
},
|
|
61
|
+
"peerDependenciesMeta": {
|
|
62
|
+
"sharp": {
|
|
63
|
+
"optional": true
|
|
64
|
+
}
|
|
57
65
|
}
|
|
58
66
|
}
|