@agentshouse/mdruntime-adapter 0.1.0-alpha.9 → 0.1.0-c023d3564cb7c

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.
Files changed (69) hide show
  1. package/AGENTS.md +312 -74
  2. package/README.md +32 -0
  3. package/dist/actions.d.ts +57 -13
  4. package/dist/actions.d.ts.map +1 -1
  5. package/dist/actions.js +274 -397
  6. package/dist/adapter.d.ts +1 -0
  7. package/dist/adapter.d.ts.map +1 -1
  8. package/dist/adapter.js +525 -216
  9. package/dist/digest.js +1 -5
  10. package/dist/discovery.d.ts +27 -36
  11. package/dist/discovery.d.ts.map +1 -1
  12. package/dist/discovery.js +173 -228
  13. package/dist/gather.d.ts +4 -0
  14. package/dist/gather.d.ts.map +1 -0
  15. package/dist/gather.js +35 -0
  16. package/dist/index.d.ts +4 -2
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +3 -3
  19. package/dist/map.d.ts +2 -2
  20. package/dist/map.d.ts.map +1 -1
  21. package/dist/map.js +6 -7
  22. package/dist/model.d.ts +7 -18
  23. package/dist/model.d.ts.map +1 -1
  24. package/dist/model.js +60 -57
  25. package/dist/part.d.ts +15 -0
  26. package/dist/part.d.ts.map +1 -0
  27. package/dist/part.js +85 -0
  28. package/dist/read.d.ts +93 -35
  29. package/dist/read.d.ts.map +1 -1
  30. package/dist/read.js +415 -245
  31. package/dist/refusal.d.ts +5 -7
  32. package/dist/refusal.d.ts.map +1 -1
  33. package/dist/refusal.js +47 -68
  34. package/dist/room-types.d.ts +6 -0
  35. package/dist/room-types.d.ts.map +1 -0
  36. package/dist/room-types.js +66 -0
  37. package/dist/site.d.ts +1 -1
  38. package/dist/site.d.ts.map +1 -1
  39. package/dist/site.js +219 -161
  40. package/dist/stream.d.ts +18 -6
  41. package/dist/stream.d.ts.map +1 -1
  42. package/dist/stream.js +556 -214
  43. package/dist/transport.d.ts +131 -38
  44. package/dist/transport.d.ts.map +1 -1
  45. package/dist/transport.js +4 -3
  46. package/dist/upload.d.ts +12 -0
  47. package/dist/upload.d.ts.map +1 -0
  48. package/dist/upload.js +52 -0
  49. package/dist/version.d.ts +3 -0
  50. package/dist/version.d.ts.map +1 -1
  51. package/dist/version.js +32 -18
  52. package/dist/write.d.ts +18 -8
  53. package/dist/write.d.ts.map +1 -1
  54. package/dist/write.js +259 -228
  55. package/package.json +4 -13
  56. package/dist/actions.js.map +0 -1
  57. package/dist/adapter.js.map +0 -1
  58. package/dist/digest.js.map +0 -1
  59. package/dist/discovery.js.map +0 -1
  60. package/dist/index.js.map +0 -1
  61. package/dist/map.js.map +0 -1
  62. package/dist/model.js.map +0 -1
  63. package/dist/read.js.map +0 -1
  64. package/dist/refusal.js.map +0 -1
  65. package/dist/site.js.map +0 -1
  66. package/dist/stream.js.map +0 -1
  67. package/dist/transport.js.map +0 -1
  68. package/dist/version.js.map +0 -1
  69. package/dist/write.js.map +0 -1
package/dist/write.js CHANGED
@@ -1,389 +1,420 @@
1
+ import { departure, ROW_WEIGHT, } from '@agentshouse/mdruntime';
2
+ import { answered, answering, binaryFileOf, headChange, revised } from './read.js';
1
3
  import { sha256Hex, textBytes } from './digest.js';
2
4
  import { nativePath, roomLocalPath } from './map.js';
3
5
  import { classifyDoor, doorRefusal, scopedRefusal } from './refusal.js';
4
6
  import { readPosition, roomVersion } from './version.js';
5
- export const INLINE_BODY_LIMIT_BYTES = 4 * 1024 * 1024;
6
- export const UPLOAD_LINE_BYTES = 37;
7
- export const UPLOAD_BYTES_MAX = 32 * 1024 * 1024 - UPLOAD_LINE_BYTES;
8
7
  function priorField(batch, path, name) {
9
- return batch.preconditions.find((precondition)=>precondition.kind === 'field' && precondition.path === path && precondition.name === name);
8
+ return batch.preconditions.find((precondition) => precondition.kind === 'field' && precondition.path === path && precondition.name === name);
10
9
  }
11
10
  function priorSection(batch, path, name) {
12
- return batch.preconditions.find((precondition)=>precondition.kind === 'section' && precondition.path === path && precondition.name === name);
11
+ return batch.preconditions.find((precondition) => precondition.kind === 'section' && precondition.path === path && precondition.name === name);
13
12
  }
14
13
  function priorPreamble(batch, path) {
15
- return batch.preconditions.find((precondition)=>precondition.kind === 'preamble' && precondition.path === path);
14
+ return batch.preconditions.find((precondition) => precondition.kind === 'preamble' && precondition.path === path);
16
15
  }
17
16
  function priorAbsence(batch, path) {
18
- return batch.preconditions.find((precondition)=>precondition.kind === 'absent' && precondition.path === path);
17
+ return batch.preconditions.find((precondition) => precondition.kind === 'absent' && precondition.path === path);
19
18
  }
20
19
  function priorExistence(batch, path) {
21
- const precondition = batch.preconditions.find((held)=>held.path === path && (held.kind === 'revision' || held.kind === 'present'));
22
- if (precondition === undefined) return undefined;
23
- return precondition.kind === 'revision' ? {
24
- base: precondition.revision
25
- } : {
26
- kind: 'presence'
27
- };
20
+ const precondition = batch.preconditions.find((held) => held.path === path && (held.kind === 'revision' || held.kind === 'present'));
21
+ if (precondition === undefined)
22
+ return undefined;
23
+ return precondition.kind === 'revision' ? { base: precondition.revision } : { kind: 'presence' };
28
24
  }
29
- function typedCreate(change, path) {
25
+ function created(change, path) {
26
+ if ('content' in change)
27
+ return { op: 'create', path, content: change.content };
30
28
  const { type, ...fields } = change.fields;
31
- if (typeof type !== 'string' || type.length === 0) return null;
32
- return {
33
- op: 'create_typed',
34
- path,
35
- type,
36
- fields,
37
- sections: change.sections
38
- };
29
+ if (typeof type !== 'string' || type.length === 0)
30
+ return null;
31
+ return { op: 'create_typed', path, type, fields, sections: change.sections };
39
32
  }
40
33
  function operationOf(batch, change, handle) {
41
34
  const path = nativePath(change.path, handle);
42
35
  if (change.kind === 'field') {
43
36
  const prior = priorField(batch, change.path, change.name);
44
- if (prior === undefined) return {
45
- rule: 'precondition-missing'
46
- };
37
+ if (prior === undefined)
38
+ return { rule: 'precondition-missing' };
47
39
  return {
48
40
  operation: {
49
41
  op: 'set_field',
50
42
  path,
51
43
  field: change.name,
52
44
  value: change.value,
53
- ...prior.value === undefined ? {} : {
54
- base: prior.value
55
- }
56
- }
45
+ ...(prior.value === undefined ? {} : { base: prior.value }),
46
+ },
57
47
  };
58
48
  }
59
49
  if (change.kind === 'section') {
60
50
  const prior = priorSection(batch, change.path, change.name);
61
- if (prior === undefined) return {
62
- rule: 'precondition-missing'
63
- };
51
+ if (prior === undefined)
52
+ return { rule: 'precondition-missing' };
64
53
  return {
65
54
  operation: {
66
55
  op: 'replace_section',
67
56
  path,
68
57
  section: change.name,
69
58
  base: prior.value,
70
- content: change.value
71
- }
59
+ content: change.value,
60
+ },
72
61
  };
73
62
  }
74
63
  if (change.kind === 'preamble') {
75
64
  const prior = priorPreamble(batch, change.path);
76
- if (prior === undefined) return {
77
- rule: 'precondition-missing'
78
- };
65
+ if (prior === undefined)
66
+ return { rule: 'precondition-missing' };
79
67
  return {
80
- operation: {
81
- op: 'replace_preamble',
82
- path,
83
- base: prior.value,
84
- content: change.value
85
- }
68
+ operation: { op: 'replace_preamble', path, base: prior.value, content: change.value },
86
69
  };
87
70
  }
88
71
  if (change.kind === 'create') {
89
- if (priorAbsence(batch, change.path) === undefined) return {
90
- rule: 'precondition-missing'
91
- };
92
- const created = typedCreate(change, path);
93
- return created === null ? {
94
- rule: 'type-missing'
95
- } : {
96
- operation: created
72
+ if (priorAbsence(batch, change.path) === undefined)
73
+ return { rule: 'precondition-missing' };
74
+ const operation = created(change, path);
75
+ return operation === null ? { rule: 'type-missing' } : { operation };
76
+ }
77
+ if (change.kind === 'binary') {
78
+ const written = {
79
+ op: 'upload',
80
+ path,
81
+ local_id: change.localId,
82
+ bytes: change.byteLength,
83
+ sha256: change.sha256,
84
+ media_type: change.mediaType,
97
85
  };
86
+ const from = departure(batch.changes, batch.changes.indexOf(change));
87
+ if (from === change.path && priorAbsence(batch, change.path) !== undefined)
88
+ return { operation: written };
89
+ const replaced = priorExistence(batch, from);
90
+ if (replaced === undefined)
91
+ return { rule: 'precondition-missing' };
92
+ if (!('base' in replaced))
93
+ return { rule: 'presence-base-unrepresentable' };
94
+ return { operation: { ...written, base: replaced.base } };
98
95
  }
99
96
  const existence = priorExistence(batch, change.path);
100
- if (existence === undefined) return {
101
- rule: 'precondition-missing'
102
- };
103
- if (!('base' in existence)) return {
104
- rule: 'presence-base-unrepresentable'
105
- };
97
+ if (existence === undefined)
98
+ return { rule: 'precondition-missing' };
99
+ if (!('base' in existence))
100
+ return { rule: 'presence-base-unrepresentable' };
106
101
  if (change.kind === 'replace') {
107
- return {
108
- operation: {
109
- op: 'replace',
110
- path,
111
- base: existence.base,
112
- content: change.content
113
- }
114
- };
102
+ return { operation: { op: 'replace', path, base: existence.base, content: change.content } };
115
103
  }
116
104
  if (change.kind === 'move') {
117
- if (priorAbsence(batch, change.to) === undefined) return {
118
- rule: 'precondition-missing'
119
- };
105
+ if (priorAbsence(batch, change.to) === undefined)
106
+ return { rule: 'precondition-missing' };
120
107
  return {
121
- operation: {
122
- op: 'rename',
123
- path,
124
- to: nativePath(change.to, handle),
125
- base: existence.base
126
- }
108
+ operation: { op: 'rename', path, to: nativePath(change.to, handle), base: existence.base },
127
109
  };
128
110
  }
129
- return {
130
- operation: {
131
- op: 'remove',
132
- path,
133
- base: existence.base
134
- }
135
- };
111
+ return { operation: { op: 'remove', path, base: existence.base } };
136
112
  }
137
113
  export function houseChanges(batch, handle) {
138
114
  const changes = [];
139
- for (const change of batch.changes){
115
+ for (const change of batch.changes) {
140
116
  const mapped = operationOf(batch, change, handle);
141
- if ('rule' in mapped) return {
142
- kind: 'unmapped',
143
- path: change.path,
144
- rule: mapped.rule
145
- };
117
+ if ('rule' in mapped)
118
+ return { kind: 'unmapped', path: change.path, rule: mapped.rule };
146
119
  changes.push(mapped.operation);
147
120
  }
148
- return {
149
- kind: 'changes',
150
- changes
151
- };
121
+ return { kind: 'changes', changes };
122
+ }
123
+ export function houseMove(batch, source, destination) {
124
+ const changes = [];
125
+ for (const { path, to } of batch.between?.moves ?? []) {
126
+ const existence = priorExistence(batch, path);
127
+ if (existence === undefined)
128
+ return { kind: 'unmapped', path, rule: 'precondition-missing' };
129
+ if (!('base' in existence)) {
130
+ return { kind: 'unmapped', path, rule: 'presence-base-unrepresentable' };
131
+ }
132
+ changes.push({
133
+ op: 'rename',
134
+ path: nativePath(path, source),
135
+ to: nativePath(to, destination),
136
+ base: existence.base,
137
+ });
138
+ }
139
+ return { kind: 'changes', changes };
152
140
  }
153
141
  export function operationHead(batch) {
154
142
  const writer = batch.writtenBy;
155
- return writer === undefined ? {
156
- operation_id: batch.id
157
- } : {
158
- operation_id: batch.id,
159
- app_ref: writer.app,
160
- release: writer.release
161
- };
143
+ return writer === undefined
144
+ ? { operation_id: batch.id }
145
+ : { operation_id: batch.id, app_ref: writer.app, release: writer.release };
162
146
  }
163
147
  export function batchPaths(changes) {
164
148
  return [
165
- ...new Set(changes.flatMap((change)=>change.op === 'rename' ? [
166
- String(change.path),
167
- String(change.to)
168
- ] : [
169
- String(change.path)
170
- ]))
171
- ].sort();
149
+ ...new Set(changes.flatMap((change) => change.op === 'rename' ? [String(change.path), String(change.to)] : [String(change.path)])),
150
+ ];
172
151
  }
173
152
  export function deltaBody(changes) {
174
- return JSON.stringify({
175
- changes
176
- });
153
+ return JSON.stringify({ changes });
177
154
  }
178
155
  export function deltaBytes(delta) {
179
156
  return textBytes(delta);
180
157
  }
181
- export function uploadable(bytes) {
182
- return bytes > INLINE_BODY_LIMIT_BYTES && bytes <= UPLOAD_BYTES_MAX;
158
+ export function uploadable(inline, bytes, bounds) {
159
+ return (textBytes(JSON.stringify(inline)) > bounds.max_inline_body_bytes &&
160
+ bytes <= bounds.max_upload_bytes);
183
161
  }
184
162
  export function deltaDigest(delta) {
185
163
  return sha256Hex(delta);
186
164
  }
187
- function material(value, handle) {
188
- if (!Array.isArray(value)) return null;
165
+ function* material(value, handle, within = () => true) {
166
+ if (!Array.isArray(value))
167
+ return null;
189
168
  const held = [];
190
- for (const entry of value){
191
- if (typeof entry !== 'object' || entry === null) return null;
169
+ for (const entry of value) {
170
+ yield ROW_WEIGHT;
171
+ if (typeof entry !== 'object' || entry === null)
172
+ return null;
192
173
  const file = entry;
193
- if (typeof file.path !== 'string') return null;
194
- if (file.content !== null && typeof file.content !== 'string') return null;
195
- if (file.revision !== null && file.revision !== undefined && typeof file.revision !== 'string') return null;
174
+ if (typeof file.path !== 'string')
175
+ return null;
176
+ if (!within(file.path))
177
+ continue;
178
+ if (file.content !== null && typeof file.content !== 'string')
179
+ return null;
180
+ if (file.revision !== null && file.revision !== undefined && typeof file.revision !== 'string')
181
+ return null;
182
+ const binary = binaryFileOf(file);
183
+ if (binary === null)
184
+ return null;
185
+ const head = headChange(file);
186
+ if (head === null)
187
+ return null;
196
188
  let path;
197
189
  try {
198
190
  path = roomLocalPath(file.path, handle);
199
- } catch {
191
+ }
192
+ catch {
200
193
  return null;
201
194
  }
202
195
  held.push({
203
196
  path,
204
197
  content: file.content,
205
- revision: typeof file.revision === 'string' ? file.revision : null
198
+ revision: typeof file.revision === 'string' ? file.revision : null,
199
+ ...(binary === undefined ? {} : { binary }),
200
+ ...(head === undefined ? {} : { head }),
206
201
  });
207
202
  }
208
203
  return held;
209
204
  }
210
- function affectedOf(held) {
205
+ function* affectedOf(held) {
211
206
  const affected = {};
212
- for (const entry of held){
213
- if (entry.content !== null) affected[entry.path] = entry.content;
207
+ for (const entry of held) {
208
+ if (entry.content !== null)
209
+ affected[entry.path] = entry.content;
210
+ yield ROW_WEIGHT;
214
211
  }
215
212
  return affected;
216
213
  }
217
- export function mutationOutcome(status, body, locator, handle) {
218
- const refusal = doorRefusal(body);
219
- if (refusal !== null && refusal.code === 'rate_limited') {
220
- const reset = refusal.fields.retry_at;
221
- return {
222
- kind: 'retryable-refusal',
223
- code: refusal.code,
224
- fields: refusal.fields,
225
- ...typeof reset === 'string' ? {
226
- reset
227
- } : {}
228
- };
214
+ function* acceptedOf(at, contents) {
215
+ const answer = answering();
216
+ for (const entry of contents) {
217
+ yield ROW_WEIGHT + (entry.content?.length ?? 0);
218
+ if (entry.revision === null) {
219
+ answer.removals.add(entry.path);
220
+ continue;
221
+ }
222
+ if (entry.content !== null)
223
+ answer.documents.set(entry.path, entry.content);
224
+ if (entry.binary !== undefined)
225
+ answer.binaryFiles.set(entry.path, entry.binary);
226
+ revised(answer, entry.path, entry.revision);
227
+ if (entry.head !== undefined)
228
+ answer.changes.set(entry.path, entry.head);
229
229
  }
230
- if (refusal !== null && refusal.code === 'operation_expired') return {
231
- kind: 'expired'
230
+ const unbinaried = answer.binaryFiles.size === 0;
231
+ const { binaryFiles, ...room } = yield* answered(answer);
232
+ return { roomVersion: roomVersion(at), ...room, ...(unbinaried ? {} : { binaryFiles }) };
233
+ }
234
+ function* movedOutcome(held, move) {
235
+ const authorities = Array.isArray(held.authorities) ? held.authorities : [];
236
+ const at = (authority) => {
237
+ const entry = authorities.find((one) => typeof one === 'object' &&
238
+ one !== null &&
239
+ one.authority === authority);
240
+ return entry === undefined ? null : readPosition(entry);
241
+ };
242
+ const left = at(move.source.authority);
243
+ const arrived = at(move.destination.authority);
244
+ if (left === null || arrived === null)
245
+ return { kind: 'transport-uncertainty' };
246
+ const under = (handle) => (path) => path.startsWith(`/rooms/${handle}/`);
247
+ const leaving = yield* material(held.contents, move.source.handle, under(move.source.handle));
248
+ const arriving = yield* material(held.contents, move.destination.handle, under(move.destination.handle));
249
+ if (leaving === null || arriving === null)
250
+ return { kind: 'transport-uncertainty' };
251
+ return {
252
+ kind: 'accepted',
253
+ ...(yield* acceptedOf(left, leaving)),
254
+ destination: yield* acceptedOf(arrived, arriving),
232
255
  };
256
+ }
257
+ export function* mutationOutcome(status, body, locator, handle, move) {
258
+ const refusal = doorRefusal(body);
259
+ if (refusal !== null && refusal.code === 'service_mode_write_halted') {
260
+ return { kind: 'write-halted' };
261
+ }
262
+ if (refusal !== null && refusal.code === 'expired') {
263
+ return refusal.fields.restored === true
264
+ ? { kind: 'expired', restored: true }
265
+ : { kind: 'expired' };
266
+ }
233
267
  if (refusal !== null && refusal.code === 'edit_conflict') {
234
268
  const { current, ...fields } = refusal.fields;
235
- const held = material(current, handle) ?? [];
269
+ const held = (yield* material(current, handle)) ?? [];
236
270
  return {
237
271
  kind: 'conflicted',
238
- affected: affectedOf(held),
272
+ affected: yield* affectedOf(held),
239
273
  code: refusal.code,
240
- fields
274
+ fields,
275
+ retryable: refusal.retryable,
241
276
  };
242
277
  }
243
278
  if (refusal !== null && status === 404) {
244
279
  return scopedRefusal(locator, 'undisclosed', refusal);
245
280
  }
246
281
  const classified = classifyDoor(status, body, locator);
247
- if (classified !== null && classified.kind !== 'scoped-refusal') {
248
- return classified.kind === 'beyond-horizon' ? {
249
- kind: 'expired'
250
- } : classified;
282
+ const reset = refusal?.fields.retry_at;
283
+ if (refusal?.retryable === true &&
284
+ typeof reset === 'string' &&
285
+ classified?.kind === 'transport-uncertainty') {
286
+ return {
287
+ kind: 'retryable-refusal',
288
+ code: refusal.code,
289
+ fields: refusal.fields,
290
+ retryable: true,
291
+ reset,
292
+ };
293
+ }
294
+ if (classified !== null && classified.kind !== 'scoped-refusal')
295
+ return classified;
296
+ if (classified !== null && status === 403 && refusal?.code === 'operation_denied') {
297
+ return classified;
251
298
  }
252
- if (classified !== null && status === 403) return classified;
253
299
  if (refusal !== null && status >= 400) {
254
300
  return {
255
301
  kind: 'rejected',
256
302
  affected: {},
257
303
  code: refusal.code,
258
- fields: refusal.fields
304
+ fields: refusal.fields,
305
+ retryable: refusal.retryable,
259
306
  };
260
307
  }
261
308
  if (status !== 200 || typeof body !== 'object' || body === null) {
262
- return {
263
- kind: 'transport-uncertainty'
264
- };
309
+ return { kind: 'transport-uncertainty' };
265
310
  }
266
311
  const held = body;
312
+ if (move !== undefined)
313
+ return yield* movedOutcome(held, move);
267
314
  const at = readPosition(held);
268
- if (at === null) return {
269
- kind: 'transport-uncertainty'
270
- };
271
- const contents = material(held.contents, handle);
272
- if (contents === null) return {
273
- kind: 'transport-uncertainty'
274
- };
275
- const documents = {};
276
- const revisions = {};
277
- const removals = [];
278
- for (const entry of contents){
279
- if (entry.content === null || entry.revision === null) {
280
- removals.push(entry.path);
281
- continue;
282
- }
283
- documents[entry.path] = entry.content;
284
- revisions[entry.path] = entry.revision;
285
- }
286
- return {
287
- kind: 'accepted',
288
- roomVersion: roomVersion(at),
289
- documents,
290
- revisions,
291
- removals: removals.sort()
292
- };
315
+ if (at === null)
316
+ return { kind: 'transport-uncertainty' };
317
+ const contents = yield* material(held.contents, handle);
318
+ if (contents === null)
319
+ return { kind: 'transport-uncertainty' };
320
+ return { kind: 'accepted', ...(yield* acceptedOf(at, contents)) };
293
321
  }
294
322
  export function rejectedMapping(mapping) {
295
323
  return {
296
324
  kind: 'rejected',
297
325
  affected: {},
298
326
  code: mapping.rule,
299
- fields: {
300
- path: mapping.path
301
- }
327
+ fields: { path: mapping.path },
328
+ retryable: false,
302
329
  };
303
330
  }
304
331
  export function revisionBody(input) {
305
332
  return {
306
333
  path: nativePath(input.path, input.handle),
307
- ...input.revision === undefined ? {} : {
308
- revision: input.revision
309
- },
310
- ...input.pageToken === undefined ? {} : {
311
- before: Number(input.pageToken)
312
- }
334
+ ...(input.revision === undefined ? {} : { revision: input.revision }),
335
+ ...(input.pageToken === undefined ? {} : { before: Number(input.pageToken) }),
313
336
  };
314
337
  }
338
+ function revisionAuthor(entry) {
339
+ const user = entry.author_user;
340
+ const site = entry.author_site;
341
+ const siteName = entry.author_site_name;
342
+ const inbox = entry.author_inbox;
343
+ const inboxName = entry.author_inbox_name;
344
+ const named = [site, siteName, inbox, inboxName].filter((held) => held !== undefined);
345
+ if (typeof user === 'string' && named.length === 0)
346
+ return { kind: 'user', id: user };
347
+ if (user !== null || named.length !== 2)
348
+ return null;
349
+ if (typeof site === 'string' && typeof siteName === 'string') {
350
+ return { kind: 'site', id: site, name: siteName };
351
+ }
352
+ if (typeof inbox === 'string' && typeof inboxName === 'string') {
353
+ return { kind: 'inbox', id: inbox, name: inboxName };
354
+ }
355
+ return null;
356
+ }
315
357
  export function historyOutcome(status, body, locator, request) {
316
358
  const classified = classifyDoor(status, body, locator);
317
- if (classified !== null) {
318
- return classified.kind === 'beyond-horizon' ? {
319
- kind: 'transport-uncertainty'
320
- } : classified;
321
- }
359
+ if (classified !== null)
360
+ return classified;
322
361
  if (status !== 200 || typeof body !== 'object' || body === null) {
323
- return {
324
- kind: 'transport-uncertainty'
325
- };
362
+ return { kind: 'transport-uncertainty' };
326
363
  }
327
364
  const held = body;
328
- if (!Array.isArray(held.revisions)) return {
329
- kind: 'transport-uncertainty'
330
- };
365
+ if (!Array.isArray(held.revisions))
366
+ return { kind: 'transport-uncertainty' };
331
367
  const entries = [];
332
- for (const entry of held.revisions){
333
- if (typeof entry !== 'object' || entry === null) return {
334
- kind: 'transport-uncertainty'
335
- };
368
+ for (const entry of held.revisions) {
369
+ if (typeof entry !== 'object' || entry === null)
370
+ return { kind: 'transport-uncertainty' };
336
371
  entries.push(entry);
337
372
  }
338
373
  if (request.revision !== undefined) {
339
374
  const named = entries[0];
340
- if (named === undefined) return {
341
- kind: 'no-history'
342
- };
375
+ if (named === undefined)
376
+ return { kind: 'no-history' };
343
377
  if (named.unavailable === true) {
344
378
  return {
345
379
  kind: 'scoped-refusal',
346
380
  locator,
347
- scope: 'content',
381
+ scope: 'operation',
348
382
  code: 'retained_history_unavailable',
349
- fields: {
350
- path: request.path,
351
- revision: request.revision
352
- }
383
+ fields: { path: request.path, revision: request.revision },
384
+ retryable: false,
353
385
  };
354
386
  }
355
- if (typeof named.content !== 'string') return {
356
- kind: 'transport-uncertainty'
357
- };
358
- return {
359
- kind: 'revision',
360
- content: named.content
361
- };
387
+ if (typeof named.media_type === 'string')
388
+ return { kind: 'no-history' };
389
+ if (typeof named.content !== 'string')
390
+ return { kind: 'transport-uncertainty' };
391
+ return { kind: 'revision', content: named.content };
362
392
  }
363
- if (entries.length === 0) return {
364
- kind: 'no-history'
365
- };
393
+ if (entries.length === 0)
394
+ return { kind: 'no-history' };
366
395
  const revisions = [];
367
- for (const entry of entries){
368
- if (typeof entry.revision !== 'string' || typeof entry.author_user !== 'string' || typeof entry.created_at !== 'string') {
369
- return {
370
- kind: 'transport-uncertainty'
371
- };
396
+ for (const entry of entries) {
397
+ const author = revisionAuthor(entry);
398
+ if (typeof entry.revision !== 'string' ||
399
+ author === null ||
400
+ typeof entry.created_at !== 'string') {
401
+ return { kind: 'transport-uncertainty' };
372
402
  }
403
+ const binaryFile = binaryFileOf(entry);
404
+ if (binaryFile === null)
405
+ return { kind: 'transport-uncertainty' };
373
406
  revisions.push({
374
407
  revision: entry.revision,
375
- author: entry.author_user,
376
- time: entry.created_at
408
+ author,
409
+ time: entry.created_at,
410
+ ...(binaryFile === undefined ? {} : { binaryFile }),
377
411
  });
378
412
  }
379
413
  const next = held.next_before;
380
414
  return {
381
415
  kind: 'history',
382
416
  revisions,
383
- ...typeof next === 'number' ? {
384
- pageToken: String(next)
385
- } : {}
417
+ ...(typeof next === 'number' ? { pageToken: String(next) } : {}),
418
+ ...(held.deleted === true ? { deleted: true } : {}),
386
419
  };
387
420
  }
388
-
389
- //# sourceMappingURL=write.js.map