@dasl/tile-loader 3.0.0 → 3.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/README.md +841 -26
- package/dist/at.d.ts +126 -12
- package/dist/at.d.ts.map +1 -1
- package/dist/at.js +574 -90
- package/dist/at.js.map +1 -1
- package/dist/car.d.ts +21 -2
- package/dist/car.d.ts.map +1 -1
- package/dist/car.js +78 -15
- package/dist/car.js.map +1 -1
- package/dist/content-scheme.d.ts +17 -3
- package/dist/content-scheme.d.ts.map +1 -1
- package/dist/content-scheme.js +32 -6
- package/dist/content-scheme.js.map +1 -1
- package/dist/guard.d.ts +242 -0
- package/dist/guard.d.ts.map +1 -0
- package/dist/guard.js +956 -0
- package/dist/guard.js.map +1 -0
- package/dist/index.d.ts +297 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +917 -78
- package/dist/index.js.map +1 -1
- package/dist/lib/cid.d.ts +25 -0
- package/dist/lib/cid.d.ts.map +1 -0
- package/dist/lib/cid.js +65 -0
- package/dist/lib/cid.js.map +1 -0
- package/dist/lib/defaults.d.ts +8 -0
- package/dist/lib/defaults.d.ts.map +1 -0
- package/dist/lib/defaults.js +14 -0
- package/dist/lib/defaults.js.map +1 -0
- package/dist/lib/destinations.d.ts +25 -0
- package/dist/lib/destinations.d.ts.map +1 -0
- package/dist/lib/destinations.js +94 -0
- package/dist/lib/destinations.js.map +1 -0
- package/dist/lib/drisl.d.ts +10 -0
- package/dist/lib/drisl.d.ts.map +1 -0
- package/dist/lib/drisl.js +169 -0
- package/dist/lib/drisl.js.map +1 -0
- package/dist/lib/errors.d.ts +80 -0
- package/dist/lib/errors.d.ts.map +1 -0
- package/dist/lib/errors.js +44 -0
- package/dist/lib/errors.js.map +1 -0
- package/dist/lib/fetching.d.ts +24 -0
- package/dist/lib/fetching.d.ts.map +1 -0
- package/dist/lib/fetching.js +135 -0
- package/dist/lib/fetching.js.map +1 -0
- package/dist/lib/limits.d.ts +34 -0
- package/dist/lib/limits.d.ts.map +1 -0
- package/dist/lib/limits.js +33 -0
- package/dist/lib/limits.js.map +1 -0
- package/dist/lib/pointer.d.ts +83 -0
- package/dist/lib/pointer.d.ts.map +1 -0
- package/dist/lib/pointer.js +244 -0
- package/dist/lib/pointer.js.map +1 -0
- package/dist/link.d.ts +110 -13
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +336 -43
- package/dist/link.js.map +1 -1
- package/dist/memory.d.ts +1 -0
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +5 -0
- package/dist/memory.js.map +1 -1
- package/dist/mount.d.ts +54 -0
- package/dist/mount.d.ts.map +1 -0
- package/dist/mount.js +286 -0
- package/dist/mount.js.map +1 -0
- package/dist/size.d.ts +43 -2
- package/dist/size.d.ts.map +1 -1
- package/dist/size.js +296 -15
- package/dist/size.js.map +1 -1
- package/package.json +13 -3
- package/src/at.ts +582 -84
- package/src/car.ts +75 -16
- package/src/content-scheme.ts +38 -7
- package/src/guard.ts +1013 -0
- package/src/index.ts +1011 -80
- package/src/lib/cid.ts +65 -0
- package/src/lib/defaults.ts +12 -0
- package/src/lib/destinations.ts +79 -0
- package/src/lib/drisl.ts +160 -0
- package/src/lib/errors.ts +111 -0
- package/src/lib/fetching.ts +138 -0
- package/src/lib/limits.ts +61 -0
- package/src/lib/pointer.ts +290 -0
- package/src/link.ts +365 -48
- package/src/memory.ts +5 -0
- package/src/mount.ts +356 -0
- package/src/size.ts +304 -16
package/dist/at.js
CHANGED
|
@@ -1,28 +1,68 @@
|
|
|
1
1
|
import { Tile } from "./index.js";
|
|
2
2
|
import { NOT_FOUND, maslResponse } from "./lib/masl.js";
|
|
3
|
+
import { publicHost, publicEndpoint } from "./lib/destinations.js";
|
|
4
|
+
import { DRISL, checkBytes, parseCid, recordCid } from "./lib/cid.js";
|
|
5
|
+
import { TileLoadError, failure, onPath } from "./lib/errors.js";
|
|
6
|
+
import { careful, get, parseJSON } from "./lib/fetching.js";
|
|
7
|
+
import { limitsOf } from "./lib/limits.js";
|
|
8
|
+
export { publicHost, publicEndpoint, localEndpoint } from "./lib/destinations.js";
|
|
9
|
+
export { recordCid } from "./lib/cid.js";
|
|
10
|
+
export { DEFAULT_LIMITS } from "./lib/limits.js";
|
|
11
|
+
// at://<a DID or a handle>/<collection>/<record key>, and after a `?` the
|
|
12
|
+
// CIDs that are expected, for a caller that has only a string to say it in.
|
|
13
|
+
const AT_URI = /^at:\/\/([^/?#\s]+)\/([^/?#\s]+)\/([A-Za-z0-9._:~-]{1,512})(?:\?([^#\s]*))?$/;
|
|
14
|
+
function sharing(options) {
|
|
15
|
+
return { fetch: careful(options.fetch), options, limits: limitsOf(options.limits), flights: new Map() };
|
|
16
|
+
}
|
|
17
|
+
// One request for one thing, however many are waiting for it: the same tile
|
|
18
|
+
// twice on a page, ten tiles of one author. What they share is the bytes.
|
|
19
|
+
function once(shared, key, ask) {
|
|
20
|
+
const flying = shared.flights.get(key);
|
|
21
|
+
if (flying)
|
|
22
|
+
return flying;
|
|
23
|
+
const flight = ask().finally(() => { shared.flights.delete(key); });
|
|
24
|
+
shared.flights.set(key, flight);
|
|
25
|
+
return flight;
|
|
26
|
+
}
|
|
3
27
|
/**
|
|
4
28
|
* Where a DID's document is, for the two methods AT uses, or false for a DID
|
|
5
29
|
* we do not resolve. `did:plc` documents come from the PLC directory.
|
|
6
30
|
* `did:web` as AT allows it names a host and nothing more: no path, and no
|
|
7
31
|
* port (AT keeps ports for local development, which a tile host is not), so
|
|
8
|
-
* the document is that host's `/.well-known/did.json`.
|
|
32
|
+
* the document is that host's `/.well-known/did.json`. The host has to be one
|
|
33
|
+
* to fetch from: a public name, unless `options.destinations` says otherwise.
|
|
9
34
|
*/
|
|
10
|
-
export function didDocumentUrl(did) {
|
|
35
|
+
export function didDocumentUrl(did, options = {}) {
|
|
11
36
|
if (/^did:plc:[a-z2-7]{24}$/.test(did))
|
|
12
|
-
return
|
|
37
|
+
return `${(options.plcUrl ?? 'https://plc.directory').replace(/\/$/, '')}/${did}`;
|
|
13
38
|
const web = did.match(/^did:web:([a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+)$/i);
|
|
14
|
-
if (web)
|
|
15
|
-
return `https://${web[1].toLowerCase()}/.well-known/did.json`;
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
async function didDocument(fetch, did) {
|
|
19
|
-
const where = didDocumentUrl(did);
|
|
20
|
-
if (!where)
|
|
39
|
+
if (!web)
|
|
21
40
|
return false;
|
|
22
|
-
|
|
23
|
-
|
|
41
|
+
// The host as the URL parser reads it is the one the request goes to.
|
|
42
|
+
let url;
|
|
43
|
+
try {
|
|
44
|
+
url = new URL(`https://${web[1]}/.well-known/did.json`);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
24
47
|
return false;
|
|
25
|
-
|
|
48
|
+
}
|
|
49
|
+
if (!(options.destinations?.did ?? publicHost)(url.hostname))
|
|
50
|
+
return false;
|
|
51
|
+
return url.href;
|
|
52
|
+
}
|
|
53
|
+
// A DID's document. What stands in the way is a TileLoadError that says what.
|
|
54
|
+
async function didDocument(shared, did) {
|
|
55
|
+
const where = didDocumentUrl(did, shared.options);
|
|
56
|
+
if (!where)
|
|
57
|
+
throw new TileLoadError('destination', `${did} is not a DID this loader resolves`);
|
|
58
|
+
const what = `the DID document of ${did}`;
|
|
59
|
+
const { fetch, limits } = shared;
|
|
60
|
+
const bytes = await once(shared, `GET ${where}`, () => get(fetch, where, { maxBytes: limits.didDocumentBytes, ms: limits.requestMs, what }));
|
|
61
|
+
const doc = parseJSON(bytes, what);
|
|
62
|
+
// A document is the DID's only if it says so itself.
|
|
63
|
+
if (!doc || typeof doc !== 'object' || doc.id !== did)
|
|
64
|
+
throw new TileLoadError('not-found', `${what}: what was found is not its own`);
|
|
65
|
+
return doc;
|
|
26
66
|
}
|
|
27
67
|
/** The resolver handles are looked up with unless a loader is told otherwise. */
|
|
28
68
|
export const DEFAULT_DNS_OVER_HTTPS = 'https://cloudflare-dns.com/dns-query';
|
|
@@ -41,24 +81,46 @@ const DID = /^did:[a-z]+:[A-Za-z0-9._:%-]+$/;
|
|
|
41
81
|
* resolver could.
|
|
42
82
|
*/
|
|
43
83
|
export async function resolveHandle(handle, options = {}) {
|
|
44
|
-
|
|
84
|
+
try {
|
|
85
|
+
return (await handleDid(sharing(options), handle)).did;
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// The DID a handle names, with the document in which the DID says so too,
|
|
92
|
+
// or a TileLoadError that says why there is none.
|
|
93
|
+
async function handleDid(shared, handle) {
|
|
45
94
|
const name = handle.toLowerCase();
|
|
95
|
+
const none = () => new TileLoadError('not-found', `No DID for ${handle}`);
|
|
46
96
|
if (!HANDLE.test(name) || name.length > 253)
|
|
47
|
-
|
|
97
|
+
throw none();
|
|
98
|
+
const { fetch, options, limits } = shared;
|
|
99
|
+
const bounds = (what) => ({ maxBytes: limits.didDocumentBytes, ms: limits.requestMs, what });
|
|
48
100
|
const doh = options.dnsOverHttps === undefined ? DEFAULT_DNS_OVER_HTTPS : options.dnsOverHttps;
|
|
101
|
+
// A lookup that failed in a way that may pass. If nothing else finds the
|
|
102
|
+
// DID, that is what went wrong, and not that there is none.
|
|
103
|
+
let failed;
|
|
104
|
+
const mayPass = (err) => { if (err instanceof TileLoadError && err.retryable)
|
|
105
|
+
failed = err; };
|
|
49
106
|
const claims = [];
|
|
50
107
|
if (doh) {
|
|
51
108
|
claims.push(async () => {
|
|
52
109
|
const url = new URL(doh);
|
|
53
110
|
url.searchParams.set('name', `_atproto.${name}`);
|
|
54
111
|
url.searchParams.set('type', 'TXT');
|
|
55
|
-
const
|
|
56
|
-
|
|
112
|
+
const what = `the DNS record of ${name}`;
|
|
113
|
+
let answer;
|
|
114
|
+
try {
|
|
115
|
+
answer = parseJSON(await get(fetch, url.toString(), { ...bounds(what), headers: { accept: 'application/dns-json' } }), what);
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
mayPass(err);
|
|
57
119
|
return;
|
|
58
|
-
|
|
120
|
+
}
|
|
59
121
|
// TXT data comes quoted, and split into quoted chunks when long.
|
|
60
|
-
const found = (Answer
|
|
61
|
-
.filter(a => a
|
|
122
|
+
const found = (Array.isArray(answer?.Answer) ? answer.Answer : [])
|
|
123
|
+
.filter(a => a?.type === 16 && typeof a.data === 'string')
|
|
62
124
|
.map(a => a.data.replace(/"\s*"/g, '').replace(/^"|"$/g, ''))
|
|
63
125
|
.filter(txt => txt.startsWith('did='))
|
|
64
126
|
.map(txt => txt.slice(4));
|
|
@@ -66,12 +128,14 @@ export async function resolveHandle(handle, options = {}) {
|
|
|
66
128
|
return found.length === 1 ? found[0] : undefined;
|
|
67
129
|
});
|
|
68
130
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
131
|
+
// The handle's own host is one more that a tile's address names, and it is
|
|
132
|
+
// asked only if it is one to fetch from.
|
|
133
|
+
if ((options.destinations?.did ?? publicHost)(name)) {
|
|
134
|
+
claims.push(async () => {
|
|
135
|
+
// Unreachable, or not readable from here: that is most hosts, and no failure.
|
|
136
|
+
return new TextDecoder().decode(await get(fetch, `https://${name}/.well-known/atproto-did`, bounds(`${name}'s own word for its DID`))).trim();
|
|
137
|
+
});
|
|
138
|
+
}
|
|
75
139
|
for (const claim of claims) {
|
|
76
140
|
let did;
|
|
77
141
|
try {
|
|
@@ -79,102 +143,522 @@ export async function resolveHandle(handle, options = {}) {
|
|
|
79
143
|
}
|
|
80
144
|
catch {
|
|
81
145
|
continue;
|
|
82
|
-
} //
|
|
146
|
+
} // try the other
|
|
83
147
|
if (!did || !DID.test(did))
|
|
84
148
|
continue;
|
|
85
149
|
let doc;
|
|
86
150
|
try {
|
|
87
|
-
doc = await didDocument(
|
|
151
|
+
doc = await didDocument(shared, did);
|
|
88
152
|
}
|
|
89
|
-
catch {
|
|
153
|
+
catch (err) {
|
|
154
|
+
mayPass(err);
|
|
90
155
|
continue;
|
|
91
156
|
}
|
|
92
|
-
if (
|
|
93
|
-
return did;
|
|
157
|
+
if (Array.isArray(doc.alsoKnownAs) && doc.alsoKnownAs.some(aka => typeof aka === 'string' && aka.toLowerCase() === `at://${name}`))
|
|
158
|
+
return { did, doc };
|
|
94
159
|
}
|
|
95
|
-
|
|
160
|
+
throw failed ?? none();
|
|
96
161
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
162
|
+
// The origin of a DID's PDS, if its document names one we fetch from. It is
|
|
163
|
+
// asked for once for a tile, and all of the tile then comes from there:
|
|
164
|
+
// whatever the document says later, a running tile's requests go nowhere new.
|
|
165
|
+
// `known` is the document when it was read already, for a handle: the one in
|
|
166
|
+
// which the DID claims the handle is the one its PDS is taken from.
|
|
167
|
+
async function pdsOf(shared, did, known) {
|
|
168
|
+
const doc = known ?? await didDocument(shared, did);
|
|
101
169
|
// The id is "#atproto_pds", or the same spelled out with the DID in front.
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const pds =
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const prm = url.searchParams;
|
|
112
|
-
Object.entries(params).forEach(([k, v]) => prm.set(k, v));
|
|
113
|
-
return await fetch(url.toString());
|
|
114
|
-
}
|
|
115
|
-
async function getRecord(fetch, repo, collection, rkey) {
|
|
116
|
-
const res = await fetchFromPDS(fetch, repo, 'com.atproto.repo.getRecord', { repo, collection, rkey });
|
|
117
|
-
if (!res || !res.ok)
|
|
118
|
-
return false;
|
|
119
|
-
return await res.json();
|
|
170
|
+
const endpoint = (Array.isArray(doc.service) ? doc.service : [])
|
|
171
|
+
.find(s => (s?.id === '#atproto_pds' || s?.id === `${did}#atproto_pds`) && s.type === 'AtprotoPersonalDataServer')?.serviceEndpoint;
|
|
172
|
+
if (typeof endpoint !== 'string')
|
|
173
|
+
throw new TileLoadError('not-found', `${did} names no PDS`);
|
|
174
|
+
const pds = (shared.options.destinations?.pds ?? publicEndpoint)(endpoint);
|
|
175
|
+
// An origin that is no URL to this browser is not one to fetch from either.
|
|
176
|
+
if (!pds || !originOf(pds))
|
|
177
|
+
throw new TileLoadError('destination', `${did} names a PDS this loader does not fetch from (${originOf(endpoint) || 'no URL'})`);
|
|
178
|
+
return pds;
|
|
120
179
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
180
|
+
// Where a URL points: its origin, or its scheme where it has none, and
|
|
181
|
+
// nothing if it is no URL. For an error that says where a DID document
|
|
182
|
+
// points: an error is something a host may show its user, and the rest of
|
|
183
|
+
// the URL is the document's author's to write.
|
|
184
|
+
function originOf(url) {
|
|
185
|
+
try {
|
|
186
|
+
const { origin, protocol } = new URL(url);
|
|
187
|
+
return (origin === 'null' ? protocol : origin).slice(0, 100);
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
return '';
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Where to ask a PDS one of its queries.
|
|
194
|
+
function xrpc(pds, query, params) {
|
|
195
|
+
const url = new URL(`/xrpc/${query}`, pds);
|
|
196
|
+
Object.entries(params).forEach(([k, v]) => url.searchParams.set(k, v));
|
|
197
|
+
return url.toString();
|
|
198
|
+
}
|
|
199
|
+
// A blob, from the PDS, checked against the CID that names it before anyone
|
|
200
|
+
// has a byte of it. `exactly` says that `maxBytes` is the size its record
|
|
201
|
+
// declares for it, which it then has to have.
|
|
202
|
+
async function verifiedBlob(shared, where, cid, maxBytes, exactly) {
|
|
203
|
+
const { fetch, limits } = shared;
|
|
204
|
+
const what = `the blob ${cid}`;
|
|
205
|
+
let bytes;
|
|
206
|
+
for (let asked = 1;; asked++) {
|
|
207
|
+
try {
|
|
208
|
+
bytes = await get(fetch, where, { maxBytes, ms: limits.requestMs, what });
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
catch (err) {
|
|
212
|
+
// A server error or a failed request may pass, and is worth asking
|
|
213
|
+
// again. A server that says it is busy is taken at its word.
|
|
214
|
+
const again = err instanceof TileLoadError && (err.code === 'upstream' || err.code === 'network') && asked <= limits.retries;
|
|
215
|
+
if (!again)
|
|
216
|
+
throw err;
|
|
217
|
+
await new Promise(resolve => setTimeout(resolve, limits.retryMs * (1 + Math.random())));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (exactly && bytes.byteLength !== maxBytes) {
|
|
221
|
+
throw new TileLoadError('integrity', `${what} is ${bytes.byteLength} bytes long, and its record says ${maxBytes}`, { expected: cid });
|
|
222
|
+
}
|
|
223
|
+
const { ok, actual } = await checkBytes(cid, bytes);
|
|
224
|
+
if (!ok)
|
|
225
|
+
throw new TileLoadError('integrity', `${what}: the bytes are not what the CID names`, { expected: cid, actual });
|
|
226
|
+
return bytes;
|
|
126
227
|
}
|
|
127
228
|
// ### AT Loader
|
|
128
229
|
// at://did:plc:izttpdp3l6vss5crelt5kcux/ing.dasl.masl/3mceykgxbkk2r
|
|
129
230
|
// at://did:web:example.com/ing.dasl.masl/3mceykgxbkk2r
|
|
130
231
|
// at://robin.berjon.com/ing.dasl.masl/3mceykgxbkk2r (a handle: see resolveHandle)
|
|
131
232
|
export class ATTileLoader {
|
|
132
|
-
#
|
|
133
|
-
#dnsOverHttps;
|
|
233
|
+
#shared;
|
|
134
234
|
constructor(options = {}) {
|
|
135
|
-
this.#
|
|
136
|
-
this.#
|
|
235
|
+
this.#shared = sharing(options);
|
|
236
|
+
loaders.set(this, this.#shared);
|
|
137
237
|
}
|
|
138
|
-
|
|
139
|
-
|
|
238
|
+
/**
|
|
239
|
+
* The tile at `url`: false for what is not an AT address, undefined for one
|
|
240
|
+
* that is not a tile's. A tile that could not be had is a rejection with a
|
|
241
|
+
* TileLoadError, whose `code` says what stood in the way: the handle or the
|
|
242
|
+
* DID has no answer (`not-found`), the record is not there (`not-found`),
|
|
243
|
+
* is not a tile's that can be run (`record-invalid`) or lists more than a
|
|
244
|
+
* tile may have (`over-budget`), a server is busy (`rate-limited`) or
|
|
245
|
+
* failing (`upstream`), did not answer in time (`timeout`) or could not be
|
|
246
|
+
* reached (`network`), or is not one to fetch from (`destination`).
|
|
247
|
+
*
|
|
248
|
+
* Loading a tile fetches its record and nothing else. Its resources are
|
|
249
|
+
* fetched when the host runs it, all of them and before it runs: see
|
|
250
|
+
* ATPathLoader.
|
|
251
|
+
*
|
|
252
|
+
* `options.cid` and `options.manifestCid` say which tile is expected: the
|
|
253
|
+
* CID of its record, the CID of its manifest alone, or both, of which
|
|
254
|
+
* either will do. A caller with only a string to say it in can give them
|
|
255
|
+
* at the end of the address, as `?cid=…&manifestCid=…`: they are read when
|
|
256
|
+
* the options give neither, and never beside them, since an address is
|
|
257
|
+
* often not the host's own writing. The record's CID is computed here, from
|
|
258
|
+
* the record as it came, and so is the manifest's: what the PDS says a
|
|
259
|
+
* record's CID is plays no part. A tile that is not the one expected is not
|
|
260
|
+
* loaded: the load fails with a TileLoadError, `record-changed`, which
|
|
261
|
+
* carries the CID that was `expected`, the `actual` one, and the
|
|
262
|
+
* `manifestCid` of what is there now. The tile that loads has its CIDs
|
|
263
|
+
* (`cid`, `manifestCid`) for a host to keep.
|
|
264
|
+
*
|
|
265
|
+
* The same content published again is a new record, with a new date and so
|
|
266
|
+
* a new CID, and the same manifest: a host that keeps the manifest's CID
|
|
267
|
+
* does not see a change where there is none.
|
|
268
|
+
*/
|
|
269
|
+
async load(url, mothership, options = {}) {
|
|
270
|
+
const address = url.match(AT_URI);
|
|
271
|
+
if (!address)
|
|
140
272
|
return false;
|
|
141
|
-
const [authority, collection, rkey] =
|
|
273
|
+
const [, authority, collection, rkey, query] = address;
|
|
142
274
|
if (collection !== 'ing.dasl.masl')
|
|
143
275
|
return;
|
|
276
|
+
const asked = new URLSearchParams(query ?? '');
|
|
277
|
+
if ([...asked.keys()].some(key => key !== 'cid' && key !== 'manifestCid'))
|
|
278
|
+
return false;
|
|
279
|
+
// What is expected is the host's to say. An address often comes from
|
|
280
|
+
// whoever posted the tile, so what it says is read only when the host
|
|
281
|
+
// says nothing: it never adds to what the host expects.
|
|
282
|
+
const hosts = options.cid != null || options.manifestCid != null;
|
|
283
|
+
const cid = (hosts ? options.cid : asked.get('cid')) ?? undefined;
|
|
284
|
+
const manifestCid = (hosts ? options.manifestCid : asked.get('manifestCid')) ?? undefined;
|
|
285
|
+
const expected = cid !== undefined || manifestCid !== undefined;
|
|
286
|
+
// What is expected is a record's CID or a manifest's: both are of DRISL.
|
|
287
|
+
for (const given of [cid, manifestCid]) {
|
|
288
|
+
if (given !== undefined && parseCid(given)?.codec !== DRISL)
|
|
289
|
+
throw new TileLoadError('record-invalid', 'The CID that was expected is not one a record or a manifest can have.', { expected: given });
|
|
290
|
+
}
|
|
291
|
+
// A record holds its manifest, so the two never have one CID. A host that
|
|
292
|
+
// gives one for both does not know which it holds, and a manifest's CID
|
|
293
|
+
// taken for a record's is met by a record made of that manifest.
|
|
294
|
+
if (cid !== undefined && cid === manifestCid)
|
|
295
|
+
throw new TileLoadError('record-invalid', 'One CID was given as the record\'s and as the manifest\'s, and they are never the same: give it as the one it is.', { expected: cid });
|
|
296
|
+
// The tile's address is the record's: what was expected is not part of it.
|
|
297
|
+
const bare = `at://${authority}/${collection}/${rkey}`;
|
|
144
298
|
// The repo is a DID; a handle is a name for one. The tile keeps the URL
|
|
145
299
|
// it was asked for by, and everything of it is fetched by DID.
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
300
|
+
const shared = this.#shared;
|
|
301
|
+
const named = authority.startsWith('did:') ? undefined : await handleDid(shared, authority);
|
|
302
|
+
const repo = named?.did ?? authority;
|
|
303
|
+
const pds = await pdsOf(shared, repo, named?.doc);
|
|
304
|
+
const where = xrpc(pds, 'com.atproto.repo.getRecord', { repo, collection, rkey });
|
|
305
|
+
const what = `the record at ${bare}`;
|
|
306
|
+
const res = parseJSON(await once(shared, `GET ${where}`, () => get(shared.fetch, where, { maxBytes: shared.limits.recordBytes, ms: shared.limits.requestMs, what })), what);
|
|
307
|
+
const manifest = res?.value?.tile;
|
|
308
|
+
if (!manifest || typeof manifest !== 'object')
|
|
309
|
+
throw new TileLoadError('record-invalid', `${what} is not a tile's`);
|
|
310
|
+
let found;
|
|
311
|
+
try {
|
|
312
|
+
found = { cid: await recordCid(res.value), manifestCid: await recordCid(manifest) };
|
|
313
|
+
}
|
|
314
|
+
catch (err) {
|
|
315
|
+
// A record that cannot be encoded has no CID to compare: without one
|
|
316
|
+
// expected it is loaded as it always was, and has none to show.
|
|
317
|
+
if (expected)
|
|
318
|
+
throw new TileLoadError('record-invalid', `The record at ${bare} cannot be encoded for its CID to be checked: ${err.message}.`);
|
|
319
|
+
}
|
|
320
|
+
if (found && expected && !(cid !== undefined && found.cid === cid) && !(manifestCid !== undefined && found.manifestCid === manifestCid)) {
|
|
321
|
+
throw new TileLoadError('record-changed', `The tile at ${bare} is not the one expected: its record has changed.`, {
|
|
322
|
+
expected: cid ?? manifestCid,
|
|
323
|
+
actual: cid !== undefined ? found.cid : found.manifestCid,
|
|
324
|
+
manifestCid: found.manifestCid,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
const loader = new ATPathLoader(repo, manifest, this, pds);
|
|
328
|
+
return new ATTile(mothership, bare, manifest, loader, { ...found, pinned: expected });
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
// What each loader shares with the path loaders of the tiles it made.
|
|
332
|
+
const loaders = new WeakMap();
|
|
333
|
+
// A tile from an AT record is fetched whole before it runs. Rendering it
|
|
334
|
+
// starts the fetching, and its runtime is only started once everything it
|
|
335
|
+
// lists is in memory; if that cannot be had, the runtime is not started at
|
|
336
|
+
// all. The host is told which it was: a `ready` event, or an `error` event
|
|
337
|
+
// whose `error` is the TileLoadError that says why. Rendering it again tries
|
|
338
|
+
// again.
|
|
339
|
+
class ATTile extends Tile {
|
|
340
|
+
#paths;
|
|
341
|
+
#whole;
|
|
342
|
+
#starts = 0;
|
|
343
|
+
constructor(mothership, url, manifest, paths, identity) {
|
|
344
|
+
super(mothership, url, manifest, paths, identity);
|
|
345
|
+
this.#paths = paths;
|
|
346
|
+
// A tile that is closed while it is being fetched asks for nothing more.
|
|
347
|
+
this.addEventListener('close', () => paths.stop());
|
|
348
|
+
}
|
|
349
|
+
attachIframe(ifr) {
|
|
350
|
+
super.attachIframe(ifr);
|
|
351
|
+
// Asked for now, while the frame loads the runtime. What is said of it is
|
|
352
|
+
// for the frame the tile is in when it is said.
|
|
353
|
+
const id = this.runtimeId;
|
|
354
|
+
// The time a start is given (startDeadline) does not run while the tile
|
|
355
|
+
// is fetched, which can take longer: it runs from when all of it is
|
|
356
|
+
// here, and again from when its runtime is told to load (see start()).
|
|
357
|
+
// A tile that could not be had says so itself, as its one `error`.
|
|
358
|
+
this.awaits();
|
|
359
|
+
const whole = this.#whole = this.#paths.prepare();
|
|
360
|
+
whole.then(() => {
|
|
361
|
+
if (this.runtimeId !== id)
|
|
362
|
+
return;
|
|
363
|
+
this.watch();
|
|
364
|
+
this.dispatchEvent(new Event('ready'));
|
|
365
|
+
}, (error) => {
|
|
366
|
+
if (error instanceof TileLoadError) {
|
|
367
|
+
if (this.runtimeId === id)
|
|
368
|
+
this.dispatchEvent(Object.assign(new Event('error'), { error }));
|
|
369
|
+
}
|
|
370
|
+
// A tile that was closed meanwhile is no failure. Anything else is ours.
|
|
371
|
+
else if (!(error instanceof Stopped))
|
|
372
|
+
console.error(`Could not fetch ${this.url}:`, error);
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
start() {
|
|
376
|
+
const id = this.runtimeId;
|
|
377
|
+
// A frame can load again while its tile is still being fetched (one that
|
|
378
|
+
// is moved in the document does). The runtime that is in it once the
|
|
379
|
+
// tile is here is started once, for the last of those loads.
|
|
380
|
+
const mine = ++this.#starts;
|
|
381
|
+
this.#whole?.then(() => { if (this.runtimeId === id && this.#starts === mine)
|
|
382
|
+
super.start(); }, () => { });
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
// The CID a resource's `src` names, and the size it declares with it: a blob
|
|
386
|
+
// reference has one, a bare link does not. A record can hold anything there.
|
|
387
|
+
function named(src) {
|
|
388
|
+
if (!src || typeof src !== 'object')
|
|
389
|
+
return {};
|
|
390
|
+
const { $link, ref, size } = src;
|
|
391
|
+
return $link !== undefined ? { cid: $link } : { cid: ref?.$link, size };
|
|
392
|
+
}
|
|
393
|
+
const utf8 = new TextEncoder();
|
|
394
|
+
// DRISL's order for the keys of a map: shorter first, then by bytes. It is
|
|
395
|
+
// the order a record has its paths in, whatever order a server lists them in.
|
|
396
|
+
function recordOrder(a, b) {
|
|
397
|
+
const x = utf8.encode(a);
|
|
398
|
+
const y = utf8.encode(b);
|
|
399
|
+
if (x.length !== y.length)
|
|
400
|
+
return x.length - y.length;
|
|
401
|
+
for (let i = 0; i < x.length; i++) {
|
|
402
|
+
if (x[i] !== y[i])
|
|
403
|
+
return x[i] - y[i];
|
|
404
|
+
}
|
|
405
|
+
return 0;
|
|
406
|
+
}
|
|
407
|
+
// Reads a manifest for what it asks, before any of it is asked for: every
|
|
408
|
+
// resource has to name a CID, and what they add up to has to be within what
|
|
409
|
+
// a tile may have, going by the sizes the record declares. Throws a
|
|
410
|
+
// TileLoadError otherwise (`record-invalid`, `over-budget`).
|
|
411
|
+
function plan(manifest, limits) {
|
|
412
|
+
const invalid = (why) => new TileLoadError('record-invalid', `Not a tile that can be run: ${why}`);
|
|
413
|
+
const resources = manifest?.resources;
|
|
414
|
+
if (!resources || typeof resources !== 'object' || Array.isArray(resources))
|
|
415
|
+
throw invalid('it has no resources');
|
|
416
|
+
const paths = new Map();
|
|
417
|
+
const blobs = new Map();
|
|
418
|
+
for (const path of Object.keys(resources).sort(recordOrder)) {
|
|
419
|
+
const entry = resources[path];
|
|
420
|
+
const shown = JSON.stringify(path.slice(0, 80));
|
|
421
|
+
if (!path.startsWith('/'))
|
|
422
|
+
throw invalid(`the path ${shown} does not start with a slash`);
|
|
423
|
+
if (!entry || typeof entry !== 'object')
|
|
424
|
+
throw invalid(`${shown} is not a resource`);
|
|
425
|
+
const { cid, size } = named(entry.src);
|
|
426
|
+
if (typeof cid !== 'string' || !parseCid(cid))
|
|
427
|
+
throw invalid(`${shown} does not name a CID its bytes can be checked against`);
|
|
428
|
+
if (size !== undefined && !(Number.isSafeInteger(size) && size >= 0))
|
|
429
|
+
throw invalid(`${shown} declares a size that is not one`);
|
|
430
|
+
paths.set(path, { entry, cid });
|
|
431
|
+
const known = blobs.get(cid);
|
|
432
|
+
if (!known)
|
|
433
|
+
blobs.set(cid, { cid, size: size, path });
|
|
434
|
+
else if (known.size === undefined)
|
|
435
|
+
known.size = size;
|
|
436
|
+
else if (size !== undefined && size !== known.size)
|
|
437
|
+
throw invalid(`${cid} is declared with two sizes`);
|
|
438
|
+
}
|
|
439
|
+
if (!paths.has('/'))
|
|
440
|
+
throw invalid('it has no "/"');
|
|
441
|
+
const all = [...blobs.values()];
|
|
442
|
+
const declared = all.reduce((sum, blob) => sum + (blob.size ?? 0), 0);
|
|
443
|
+
const over = (why, limit, amount, path) => new TileLoadError('over-budget', `More than a tile may have: ${why}`, { limit, amount, ...(path ? { path } : {}) });
|
|
444
|
+
if (all.length > limits.resources)
|
|
445
|
+
throw over(`${all.length} resources, and ${limits.resources} are allowed`, limits.resources, all.length);
|
|
446
|
+
const large = all.find(blob => (blob.size ?? 0) > limits.resourceBytes);
|
|
447
|
+
if (large)
|
|
448
|
+
throw over(`${JSON.stringify(large.path.slice(0, 80))} is ${large.size} bytes, and a resource may be ${limits.resourceBytes}`, limits.resourceBytes, large.size, large.path);
|
|
449
|
+
if (declared > limits.tileBytes)
|
|
450
|
+
throw over(`${declared} bytes in all, and ${limits.tileBytes} are allowed`, limits.tileBytes, declared);
|
|
451
|
+
return { paths, blobs: all, declared };
|
|
452
|
+
}
|
|
453
|
+
// What a fetch that was called off is rejected with: the tile was closed.
|
|
454
|
+
class Stopped extends Error {
|
|
455
|
+
constructor() {
|
|
456
|
+
super('The tile was closed before all of it was here.');
|
|
157
457
|
}
|
|
158
458
|
}
|
|
459
|
+
/**
|
|
460
|
+
* The resources of a tile on AT, from its author's PDS, each checked against
|
|
461
|
+
* the CID its record names before any of it is served.
|
|
462
|
+
*
|
|
463
|
+
* What a tile asks for, and when, would be a message to its author's server
|
|
464
|
+
* if each request went there as it was made. So when the host runs the tile
|
|
465
|
+
* everything its record lists is fetched once (`prepare()`), in an order the
|
|
466
|
+
* record alone decides, and the tile is answered from memory: what it asks
|
|
467
|
+
* for after that reaches nobody, and what the PDS is asked depends on the
|
|
468
|
+
* record alone. The fetch is refused, not narrowed, when the record lists
|
|
469
|
+
* more than a tile may have: fetching the rest as the tile asked would bring
|
|
470
|
+
* the message back.
|
|
471
|
+
*
|
|
472
|
+
* Before the tile is run only the host can be asking, for a card's picture:
|
|
473
|
+
* that one resource is fetched by itself, within a size fit for a card.
|
|
474
|
+
*
|
|
475
|
+
* It is the tile the AT loader makes that calls `prepare()` when it is run.
|
|
476
|
+
* A host that makes a path loader of its own and runs it under a Tile of its
|
|
477
|
+
* own has to call `prepare()` itself, and run the tile once that has
|
|
478
|
+
* resolved: until it is called, every resource is fetched as it is asked
|
|
479
|
+
* for, the tile's requests among them.
|
|
480
|
+
*/
|
|
159
481
|
export class ATPathLoader {
|
|
160
482
|
#did;
|
|
161
|
-
#
|
|
162
|
-
#
|
|
163
|
-
|
|
483
|
+
#shared;
|
|
484
|
+
#pds;
|
|
485
|
+
#plan;
|
|
486
|
+
// The resources that are here, checked, by CID.
|
|
487
|
+
#have = new Map();
|
|
488
|
+
// The fetching of all of them, from the moment the host runs the tile:
|
|
489
|
+
// whether all of it is here, whether it could not be had, and how to call
|
|
490
|
+
// it off.
|
|
491
|
+
#attempt;
|
|
492
|
+
/**
|
|
493
|
+
* `from` is the loader that made the tile, whose limits and requests in
|
|
494
|
+
* flight it shares, or the options for one on its own (a function is taken
|
|
495
|
+
* for its `fetch`, which is what this argument used to be). `pds` is the
|
|
496
|
+
* origin of the repo's PDS when it is known already: the loader has just
|
|
497
|
+
* asked, and a tile does not ask again. Throws a TileLoadError for a
|
|
498
|
+
* manifest that cannot be run (`record-invalid`) or that lists more than a
|
|
499
|
+
* tile may have (`over-budget`).
|
|
500
|
+
*/
|
|
501
|
+
constructor(did, manifest, from = {}, pds) {
|
|
164
502
|
this.#did = did;
|
|
165
|
-
this.#
|
|
166
|
-
this.#
|
|
503
|
+
this.#shared = (from instanceof ATTileLoader && loaders.get(from)) || sharing(typeof from === 'function' ? { fetch: from } : from);
|
|
504
|
+
this.#plan = plan(manifest, this.#shared.limits);
|
|
505
|
+
if (pds)
|
|
506
|
+
this.#pds = Promise.resolve(pds);
|
|
167
507
|
}
|
|
508
|
+
/**
|
|
509
|
+
* Fetches everything the tile lists, once, checked, in the order of the
|
|
510
|
+
* record: first what the record declares the size of, `concurrency`
|
|
511
|
+
* requests at a time, then what it declares no size for, one at a time and
|
|
512
|
+
* each no further than what the tile has left. Resolves when all of it is
|
|
513
|
+
* in memory. Rejects with a TileLoadError when any of it could not be had,
|
|
514
|
+
* or was not all here within `prepareMs`; calling again then tries again,
|
|
515
|
+
* for what is still missing. The host's rendering the tile calls this;
|
|
516
|
+
* from then on `resolvePath()` asks nobody.
|
|
517
|
+
*/
|
|
518
|
+
prepare() {
|
|
519
|
+
if (this.#attempt && !this.#attempt.failed)
|
|
520
|
+
return this.#attempt.whole;
|
|
521
|
+
const { limits } = this.#shared;
|
|
522
|
+
let stop;
|
|
523
|
+
const calledOff = new Promise((_, reject) => { stop = reject; });
|
|
524
|
+
const late = setTimeout(() => stop(new TileLoadError('timeout', `The tile was not all here within ${limits.prepareMs} ms`)), Math.min(limits.prepareMs, 2 ** 31 - 1));
|
|
525
|
+
const attempt = { done: false, failed: false, stop, whole: undefined };
|
|
526
|
+
attempt.whole = Promise.race([this.#fetchAll(() => attempt.failed), calledOff])
|
|
527
|
+
.then(() => { attempt.done = true; }, (err) => {
|
|
528
|
+
attempt.failed = true;
|
|
529
|
+
throw err;
|
|
530
|
+
})
|
|
531
|
+
.finally(() => clearTimeout(late));
|
|
532
|
+
// Whoever asked hears of it. Nobody asking is not an error.
|
|
533
|
+
attempt.whole.catch(() => { });
|
|
534
|
+
this.#attempt = attempt;
|
|
535
|
+
return attempt.whole;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Calls off a fetch of everything that has not come to its end: nothing
|
|
539
|
+
* more is asked for. What is here stays, and the requests already made run
|
|
540
|
+
* their course, since another tile may be waiting on them. For a tile that
|
|
541
|
+
* is closed before it could run, which is then as it was before the host
|
|
542
|
+
* ran it. A tile that is all here is left as it is.
|
|
543
|
+
*/
|
|
544
|
+
stop() {
|
|
545
|
+
const attempt = this.#attempt;
|
|
546
|
+
if (!attempt || attempt.done)
|
|
547
|
+
return;
|
|
548
|
+
attempt.failed = true;
|
|
549
|
+
attempt.stop(new Stopped());
|
|
550
|
+
this.#attempt = undefined;
|
|
551
|
+
}
|
|
552
|
+
// Every blob of the plan that is not here yet. `over` says when to stop
|
|
553
|
+
// asking: something failed, the time is up, or the tile was closed.
|
|
554
|
+
async #fetchAll(over) {
|
|
555
|
+
const { limits } = this.#shared;
|
|
556
|
+
const { blobs, declared } = this.#plan;
|
|
557
|
+
let failed;
|
|
558
|
+
const fail = (err, blob) => { failed ??= err instanceof TileLoadError ? onPath(err, blob.path) : err; };
|
|
559
|
+
// What the record declares the size of, which the plan has counted: a
|
|
560
|
+
// few at a time.
|
|
561
|
+
const queue = blobs.filter(blob => blob.size !== undefined);
|
|
562
|
+
const lane = async () => {
|
|
563
|
+
while (queue.length && !failed && !over()) {
|
|
564
|
+
const blob = queue.shift();
|
|
565
|
+
try {
|
|
566
|
+
await this.#blob(blob, 0);
|
|
567
|
+
}
|
|
568
|
+
catch (err) {
|
|
569
|
+
fail(err, blob);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
await Promise.all(Array.from({ length: Math.max(1, Math.min(limits.concurrency, queue.length)) }, lane));
|
|
574
|
+
// A blob that declares no size is counted as it arrives. These come one
|
|
575
|
+
// after the other, each read no further than a resource may be and than
|
|
576
|
+
// the tile has left: several at once would each be read that far, and a
|
|
577
|
+
// record that declares nothing would have the page take more than one
|
|
578
|
+
// that declares it all.
|
|
579
|
+
let left = limits.tileBytes - declared;
|
|
580
|
+
// `amount` is what the tile comes to, where a blob was read to its end.
|
|
581
|
+
const tooMuch = (amount) => new TileLoadError('over-budget', `More than a tile may have: over ${limits.tileBytes} bytes in all`, { limit: limits.tileBytes, ...(amount === undefined ? {} : { amount }) });
|
|
582
|
+
for (const blob of blobs) {
|
|
583
|
+
if (blob.size !== undefined)
|
|
584
|
+
continue;
|
|
585
|
+
if (failed || over())
|
|
586
|
+
break;
|
|
587
|
+
const room = Math.max(0, Math.min(limits.resourceBytes, left));
|
|
588
|
+
try {
|
|
589
|
+
left -= (await this.#blob(blob, room)).byteLength;
|
|
590
|
+
// (What was here already, for a card, was read to a limit of its own.)
|
|
591
|
+
if (left < 0)
|
|
592
|
+
throw tooMuch(limits.tileBytes - left);
|
|
593
|
+
}
|
|
594
|
+
catch (err) {
|
|
595
|
+
// Longer than the tile has left, where a resource may be longer: it
|
|
596
|
+
// is the tile that is too large, and not this one resource.
|
|
597
|
+
const tiles = err instanceof TileLoadError && err.code === 'too-large' && room < limits.resourceBytes;
|
|
598
|
+
fail(tiles ? tooMuch() : err, blob);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
if (failed)
|
|
602
|
+
throw failed;
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* The resource at `path`. A path the tile does not list is a 404, and
|
|
606
|
+
* nobody is asked. Once the host has run the tile the answer comes from
|
|
607
|
+
* memory, when all of the tile is there; if it could not all be had, every
|
|
608
|
+
* path is answered with that failure (a 502, a 503 when the PDS is busy, a
|
|
609
|
+
* 504 when it took too long, a 507 when it is more than a tile may have),
|
|
610
|
+
* which carries the TileLoadError that says why. Before that, the one
|
|
611
|
+
* resource is fetched and checked by itself, if it is no larger than
|
|
612
|
+
* `cardBytes`.
|
|
613
|
+
*/
|
|
168
614
|
async resolvePath(path) {
|
|
169
|
-
const
|
|
170
|
-
if (!
|
|
171
|
-
return NOT_FOUND;
|
|
172
|
-
const src = entry.src;
|
|
173
|
-
const cid = '$link' in src ? src.$link : src.ref.$link;
|
|
174
|
-
const data = await getBlob(this.#fetch, this.#did, cid);
|
|
175
|
-
if (!data)
|
|
615
|
+
const listed = this.#plan.paths.get(path);
|
|
616
|
+
if (!listed)
|
|
176
617
|
return NOT_FOUND;
|
|
177
|
-
|
|
618
|
+
const { entry, cid } = listed;
|
|
619
|
+
try {
|
|
620
|
+
if (this.#attempt) {
|
|
621
|
+
await this.#attempt.whole;
|
|
622
|
+
return maslResponse(entry, this.#have.get(cid));
|
|
623
|
+
}
|
|
624
|
+
// The tile is not running, so it is the host asking, to draw a card.
|
|
625
|
+
const blob = this.#plan.blobs.find(b => b.cid === cid);
|
|
626
|
+
const { cardBytes } = this.#shared.limits;
|
|
627
|
+
if ((blob.size ?? 0) > cardBytes) {
|
|
628
|
+
throw new TileLoadError('too-large', `${JSON.stringify(path.slice(0, 80))} is ${blob.size} bytes, and ${cardBytes} are allowed before a tile is run`, { limit: cardBytes });
|
|
629
|
+
}
|
|
630
|
+
return maslResponse(entry, await this.#blob(blob, cardBytes));
|
|
631
|
+
}
|
|
632
|
+
catch (err) {
|
|
633
|
+
if (!(err instanceof TileLoadError))
|
|
634
|
+
throw err;
|
|
635
|
+
// The error may be one that several are being told of: each gets its own.
|
|
636
|
+
return failure(onPath(err, err.path ?? path));
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
// The bytes of a blob, checked: from memory, or from the PDS, where one
|
|
640
|
+
// request serves all who ask for the same at the same time. `room` is how
|
|
641
|
+
// much of one that declares no size is read.
|
|
642
|
+
async #blob(blob, room) {
|
|
643
|
+
const here = this.#have.get(blob.cid);
|
|
644
|
+
if (here)
|
|
645
|
+
return here;
|
|
646
|
+
const declared = blob.size !== undefined;
|
|
647
|
+
const maxBytes = declared ? blob.size : room;
|
|
648
|
+
const pds = await this.#resolved();
|
|
649
|
+
const where = xrpc(pds, 'com.atproto.sync.getBlob', { did: this.#did, cid: blob.cid });
|
|
650
|
+
const bytes = await once(this.#shared, `GET ${where} ${declared ? 'of' : 'within'} ${maxBytes}`, () => verifiedBlob(this.#shared, where, blob.cid, maxBytes, declared));
|
|
651
|
+
this.#have.set(blob.cid, bytes);
|
|
652
|
+
return bytes;
|
|
653
|
+
}
|
|
654
|
+
// The PDS, asked for once. A lookup that failed is not kept: the next
|
|
655
|
+
// request for a resource asks again.
|
|
656
|
+
#resolved() {
|
|
657
|
+
this.#pds ??= pdsOf(this.#shared, this.#did).catch((err) => {
|
|
658
|
+
this.#pds = undefined;
|
|
659
|
+
throw err;
|
|
660
|
+
});
|
|
661
|
+
return this.#pds;
|
|
178
662
|
}
|
|
179
663
|
}
|
|
180
664
|
//# sourceMappingURL=at.js.map
|