@agentshouse/mdruntime-adapter 0.1.0-ceda7e87cc6e8 → 0.1.0-cedb8e57837a7

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/read.js CHANGED
@@ -1,13 +1,51 @@
1
- import { readPosition, roomVersion } from './version.js';
1
+ import { patching, ROW_WEIGHT, stepped, } from '@agentshouse/mdruntime';
2
+ import { DECIMAL, readPosition, roomVersion } from './version.js';
3
+ const REVISION_FILE = /^fr_([0-9a-z]+)\.[0-9a-z]+\.[0-9a-z]$/u;
4
+ function fileOf(revision) {
5
+ return REVISION_FILE.exec(revision)?.[1];
6
+ }
7
+ export function answering() {
8
+ return {
9
+ documents: new Map(),
10
+ revisions: new Map(),
11
+ fileIds: new Map(),
12
+ changes: new Map(),
13
+ binaryFiles: new Map(),
14
+ removals: new Set(),
15
+ };
16
+ }
17
+ export function revised(answer, path, revision) {
18
+ answer.revisions.set(path, revision);
19
+ const file = fileOf(revision);
20
+ if (file !== undefined)
21
+ answer.fileIds.set(path, file);
22
+ }
23
+ export function forgotten(answer, path) {
24
+ answer.documents.delete(path);
25
+ answer.revisions.delete(path);
26
+ answer.fileIds.delete(path);
27
+ answer.changes.delete(path);
28
+ answer.binaryFiles.delete(path);
29
+ answer.removals.delete(path);
30
+ }
31
+ export function* answered(answer) {
32
+ const removals = [];
33
+ for (const path of answer.removals) {
34
+ removals.push(path);
35
+ yield ROW_WEIGHT;
36
+ }
37
+ return {
38
+ documents: yield* patching({}, answer.documents),
39
+ revisions: yield* patching({}, answer.revisions),
40
+ ...(answer.fileIds.size === 0 ? {} : { fileIds: yield* patching({}, answer.fileIds) }),
41
+ changes: yield* patching({}, answer.changes),
42
+ binaryFiles: yield* patching({}, answer.binaryFiles),
43
+ removals,
44
+ };
45
+ }
2
46
  export function authorityRoot(handle) {
3
47
  return `/rooms/${handle}`;
4
48
  }
5
- export function contentPrefixes(tracked) {
6
- return [
7
- authorityRoot(tracked.handle),
8
- ...tracked.schemas.map((schema) => schemaPath(tracked.handle, schema)),
9
- ];
10
- }
11
49
  export function schemaPath(handle, schema) {
12
50
  return `${authorityRoot(handle)}/${schema}`;
13
51
  }
@@ -92,11 +130,12 @@ function parseFile(value, mode) {
92
130
  path: file.path,
93
131
  revision: file.revision,
94
132
  version: file.revision,
133
+ ...(typeof file.sha256 === 'string' ? { sha256: file.sha256 } : {}),
95
134
  ...headed(head),
96
135
  ...binaried(binary),
97
136
  };
98
137
  }
99
- function parseScope(value, mode) {
138
+ function* parseScope(value, mode) {
100
139
  if (typeof value !== 'object' || value === null)
101
140
  return null;
102
141
  const held = value;
@@ -120,6 +159,7 @@ function parseScope(value, mode) {
120
159
  if (file === null)
121
160
  return null;
122
161
  files.push(file);
162
+ yield ROW_WEIGHT;
123
163
  }
124
164
  const contents = [];
125
165
  for (const entry of held.contents) {
@@ -129,25 +169,39 @@ function parseScope(value, mode) {
129
169
  if (typeof content.path !== 'string' || typeof content.content !== 'string')
130
170
  return null;
131
171
  contents.push({ path: content.path, content: content.content });
172
+ yield ROW_WEIGHT;
132
173
  }
133
174
  return { anchor: manifest.anchor, ...at, files, contents };
134
175
  }
176
+ function parseCut(value) {
177
+ if (value === undefined)
178
+ return undefined;
179
+ if (typeof value !== 'object' || value === null)
180
+ return null;
181
+ const { position, log_epoch: logEpoch } = value;
182
+ if (typeof position !== 'string' || typeof logEpoch !== 'string')
183
+ return null;
184
+ return { position, log_epoch: logEpoch };
185
+ }
135
186
  function parseCursor(value) {
136
187
  if (typeof value !== 'object' || value === null)
137
188
  return null;
138
- const { room, path } = value;
189
+ const { room, path, cut } = value;
139
190
  if (typeof room !== 'string' || (path !== null && typeof path !== 'string'))
140
191
  return null;
141
- return { room, path };
192
+ const parsed = parseCut(cut);
193
+ if (parsed === null)
194
+ return null;
195
+ return { room, path, ...(parsed === undefined ? {} : { cut: parsed }) };
142
196
  }
143
- export function parseEnvelope(body) {
197
+ export function* parseEnvelope(body) {
144
198
  if (typeof body !== 'object' || body === null)
145
199
  return null;
146
200
  if (!('scopes' in body) || !Array.isArray(body.scopes))
147
201
  return null;
148
202
  const scopes = [];
149
203
  for (const entry of body.scopes) {
150
- const scope = parseScope(entry, 'credential');
204
+ const scope = yield* parseScope(entry, 'credential');
151
205
  if (scope === null)
152
206
  return null;
153
207
  scopes.push(scope);
@@ -157,8 +211,20 @@ export function parseEnvelope(body) {
157
211
  const next = parseCursor(body.next);
158
212
  return next === null ? null : { scopes, next };
159
213
  }
160
- export function parseSiteBootstrap(body) {
161
- return parseScope(body, 'site');
214
+ export function* parseSiteBootstrap(body) {
215
+ return yield* parseScope(body, 'site');
216
+ }
217
+ export function siteBootstrapNext(body) {
218
+ if (typeof body !== 'object' || body === null || !('next' in body))
219
+ return null;
220
+ return typeof body.next === 'string' && body.next.length > 0 ? body.next : null;
221
+ }
222
+ export function siteBootstrapOffset(body) {
223
+ if (typeof body !== 'object' || body === null || !('offset' in body))
224
+ return null;
225
+ return Number.isSafeInteger(body.offset) && body.offset > 0
226
+ ? body.offset
227
+ : null;
162
228
  }
163
229
  function parseChange(value, mode) {
164
230
  if (typeof value !== 'object' || value === null)
@@ -222,16 +288,75 @@ export function parseEnumeration(body, mode) {
222
288
  return null;
223
289
  changes.push(mapped);
224
290
  }
225
- authorities.push({ authority: held.authority, ...at, changes });
291
+ const next = held.next === undefined ? undefined : readPage(held.next);
292
+ if (next === null)
293
+ return null;
294
+ authorities.push({
295
+ authority: held.authority,
296
+ ...at,
297
+ changes,
298
+ ...(next === undefined ? {} : { next }),
299
+ });
226
300
  }
227
301
  return { authorities };
228
302
  }
229
- export function advancedScope(scope, view) {
230
- const files = new Map(scope.files.map((file) => [file.path, file]));
231
- const contents = new Map(scope.contents.map((entry) => [entry.path, entry.content]));
303
+ function readPage(held) {
304
+ if (typeof held !== 'object' || held === null)
305
+ return null;
306
+ const { after, through, read } = held;
307
+ if (typeof after !== 'string' || !DECIMAL.test(after))
308
+ return null;
309
+ if (typeof through !== 'string' || !DECIMAL.test(through))
310
+ return null;
311
+ if (typeof read !== 'number' || !Number.isSafeInteger(read) || read < 0)
312
+ return null;
313
+ return { after, through, read };
314
+ }
315
+ function advances(asked, next) {
316
+ const moved = BigInt(next.through) - BigInt(asked.through);
317
+ return moved > 0n || (moved === 0n && next.after === asked.after && next.read > asked.read);
318
+ }
319
+ export async function wholeView(first, more) {
320
+ const changes = new Map(first.changes.map((change) => [change.path, change]));
321
+ let view = first;
322
+ while (view.next !== undefined) {
323
+ const asked = view.next;
324
+ const page = await more(asked);
325
+ if ('kind' in page)
326
+ return page;
327
+ if (page.position !== asked.through ||
328
+ page.logEpoch !== first.logEpoch ||
329
+ (page.next !== undefined && !advances(asked, page.next))) {
330
+ return { kind: 'transport-uncertainty' };
331
+ }
332
+ for (const change of page.changes) {
333
+ changes.delete(change.path);
334
+ changes.set(change.path, change);
335
+ }
336
+ view = page;
337
+ }
338
+ return {
339
+ authority: first.authority,
340
+ position: view.position,
341
+ logEpoch: first.logEpoch,
342
+ changes: [...changes.values()],
343
+ };
344
+ }
345
+ export function* advancedScope(scope, view) {
346
+ const files = new Map();
347
+ for (const file of scope.files) {
348
+ files.set(file.path, file);
349
+ yield ROW_WEIGHT;
350
+ }
351
+ const contents = new Map();
352
+ for (const entry of scope.contents) {
353
+ contents.set(entry.path, entry.content);
354
+ yield ROW_WEIGHT;
355
+ }
232
356
  for (const change of view.changes) {
233
357
  files.delete(change.path);
234
358
  contents.delete(change.path);
359
+ yield ROW_WEIGHT;
235
360
  if ('removed' in change)
236
361
  continue;
237
362
  files.set(change.path, {
@@ -244,42 +369,43 @@ export function advancedScope(scope, view) {
244
369
  if ('content' in change)
245
370
  contents.set(change.path, change.content);
246
371
  }
372
+ const listed = [];
373
+ for (const [path, content] of contents) {
374
+ listed.push({ path, content });
375
+ yield ROW_WEIGHT;
376
+ }
247
377
  return {
248
378
  ...scope,
249
379
  position: view.position,
250
380
  logEpoch: view.logEpoch,
251
381
  files: [...files.values()],
252
- contents: [...contents].map(([path, content]) => ({ path, content })),
382
+ contents: listed,
253
383
  };
254
384
  }
255
385
  export function matchesScope(scope, tracked) {
256
386
  return scope.anchor === tracked.authority;
257
387
  }
258
- export function bootstrapOf(scope) {
259
- const contents = new Map(scope.contents.map((entry) => [entry.path, entry.content]));
260
- const documents = {};
261
- const revisions = {};
262
- const changes = {};
263
- const binaryFiles = {};
388
+ export function* bootstrapping(scope) {
389
+ const contents = new Map();
390
+ for (const entry of scope.contents) {
391
+ contents.set(entry.path, entry.content);
392
+ yield ROW_WEIGHT;
393
+ }
394
+ const answer = answering();
264
395
  for (const file of scope.files) {
265
- if (file.revision !== undefined)
266
- revisions[file.path] = file.revision;
396
+ yield ROW_WEIGHT;
267
397
  if (file.head !== undefined)
268
- changes[file.path] = file.head;
398
+ answer.changes.set(file.path, file.head);
269
399
  const content = contents.get(file.path);
270
400
  if (file.binary !== undefined)
271
- binaryFiles[file.path] = file.binary;
401
+ answer.binaryFiles.set(file.path, file.binary);
272
402
  else if (content !== undefined)
273
- documents[file.path] = content;
403
+ answer.documents.set(file.path, content);
404
+ if (file.revision !== undefined)
405
+ revised(answer, file.path, file.revision);
274
406
  }
275
- return {
276
- kind: 'bootstrap',
277
- roomVersion: roomVersion(scope),
278
- documents,
279
- revisions,
280
- changes,
281
- binaryFiles,
282
- };
407
+ const { removals: _removals, ...page } = yield* answered(answer);
408
+ return { kind: 'bootstrap', roomVersion: roomVersion(scope), ...page };
283
409
  }
284
410
  export function schemaOf(scope, schema) {
285
411
  const file = scope.files.find((entry) => entry.path === schema);
@@ -293,35 +419,27 @@ export function schemaOf(scope, schema) {
293
419
  export function viewOf(enumeration, tracked) {
294
420
  return enumeration.authorities.find((entry) => entry.authority === tracked.authority);
295
421
  }
296
- export function refreshFrom(view, fromRoomVersion) {
422
+ function* refreshing(view, fromRoomVersion) {
297
423
  if (roomVersion(view) === fromRoomVersion)
298
424
  return { kind: 'still-current' };
299
- const documents = {};
300
- const revisions = {};
301
- const changes = {};
302
- const binaryFiles = {};
303
- const removals = [];
425
+ const answer = answering();
304
426
  for (const change of view.changes) {
427
+ yield ROW_WEIGHT;
305
428
  if ('removed' in change) {
306
- removals.push(change.path);
429
+ answer.removals.add(change.path);
307
430
  continue;
308
431
  }
309
432
  if ('content' in change)
310
- documents[change.path] = change.content;
433
+ answer.documents.set(change.path, change.content);
311
434
  else
312
- binaryFiles[change.path] = change.binary;
313
- if (change.revision !== undefined)
314
- revisions[change.path] = change.revision;
435
+ answer.binaryFiles.set(change.path, change.binary);
315
436
  if (change.head !== undefined)
316
- changes[change.path] = change.head;
437
+ answer.changes.set(change.path, change.head);
438
+ if (change.revision !== undefined)
439
+ revised(answer, change.path, change.revision);
317
440
  }
318
- return {
319
- kind: 'net-advancement',
320
- roomVersion: roomVersion(view),
321
- documents,
322
- revisions,
323
- changes,
324
- binaryFiles,
325
- removals: removals.sort(),
326
- };
441
+ return { kind: 'net-advancement', roomVersion: roomVersion(view), ...(yield* answered(answer)) };
442
+ }
443
+ export async function refreshFrom(view, fromRoomVersion) {
444
+ return stepped(refreshing(view, fromRoomVersion));
327
445
  }
@@ -1 +1 @@
1
- {"version":3,"file":"refusal.d.ts","sourceRoot":"","sources":["../src/refusal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACd,eAAe,EACf,aAAa,EACd,MAAM,wBAAwB,CAAC;AAEhC,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,eAAe,GAAG,IAAI,CAOjE;AAED,wBAAgB,aAAa,CAC3B,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,aAAa,CAAC,OAAO,CAAC,EAC7B,OAAO,EAAE,eAAe,GACvB,aAAa,CAEf;AAmBD,wBAAgB,YAAY,CAC1B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,MAAM,GACd,cAAc,GAAG,IAAI,CAMvB;AAED,wBAAgB,YAAY,CAC1B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,MAAM,GACd,cAAc,GAAG;IAAE,IAAI,EAAE,gBAAgB,CAAA;CAAE,GAAG,IAAI,CAGpD;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,cAAc,GAAG,IAAI,CAQrF"}
1
+ {"version":3,"file":"refusal.d.ts","sourceRoot":"","sources":["../src/refusal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACd,eAAe,EACf,aAAa,EACd,MAAM,wBAAwB,CAAC;AAEhC,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,eAAe,GAAG,IAAI,CAOjE;AAED,wBAAgB,aAAa,CAC3B,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,aAAa,CAAC,OAAO,CAAC,EAC7B,OAAO,EAAE,eAAe,GACvB,aAAa,CAEf;AAqBD,wBAAgB,YAAY,CAC1B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,MAAM,GACd,cAAc,GAAG,IAAI,CAMvB;AAED,wBAAgB,YAAY,CAC1B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,MAAM,GACd,cAAc,GAAG;IAAE,IAAI,EAAE,gBAAgB,CAAA;CAAE,GAAG,IAAI,CAGpD;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,cAAc,GAAG,IAAI,CAQrF"}
package/dist/refusal.js CHANGED
@@ -18,8 +18,9 @@ function doorFailure(status, body) {
18
18
  const refusal = doorRefusal(body);
19
19
  if (status === 401)
20
20
  return { kind: 'revoked-authorization' };
21
- if (refusal?.retryable === true)
21
+ if (refusal?.retryable === true || refusal?.code === 'update_required') {
22
22
  return { kind: 'transport-uncertainty' };
23
+ }
23
24
  if (status === 404 || refusal?.code === 'room_not_found')
24
25
  return { kind: 'room-not-readable' };
25
26
  if (status < 400)
package/dist/site.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import type { Adapter } from '@agentshouse/mdruntime';
2
2
  import { type HouseSiteTransport } from './transport.js';
3
- export declare function createSiteAdapter(transport: HouseSiteTransport): Adapter;
3
+ export declare function createSiteAdapter(transport: HouseSiteTransport, snapshot?: boolean): Adapter;
4
4
  //# sourceMappingURL=site.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"site.d.ts","sourceRoot":"","sources":["../src/site.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EAOR,MAAM,wBAAwB,CAAC;AAsBhC,OAAO,EAOL,KAAK,kBAAkB,EACxB,MAAM,gBAAgB,CAAC;AAiCxB,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,kBAAkB,GAAG,OAAO,CA8KxE"}
1
+ {"version":3,"file":"site.d.ts","sourceRoot":"","sources":["../src/site.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EAUR,MAAM,wBAAwB,CAAC;AA4BhC,OAAO,EAOL,KAAK,kBAAkB,EACxB,MAAM,gBAAgB,CAAC;AAyCxB,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,kBAAkB,EAAE,QAAQ,UAAQ,GAAG,OAAO,CAwR1F"}