@drfed/models 0.2.0-dev.651 → 0.2.0-dev.682
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 +308 -0
- package/dist/document.d.mts +200 -0
- package/dist/document.d.mts.map +1 -0
- package/dist/document.mjs +1264 -0
- package/dist/document.mjs.map +1 -0
- package/package.json +13 -6
|
@@ -0,0 +1,1264 @@
|
|
|
1
|
+
import { MIMEType } from "node:util";
|
|
2
|
+
import { Accept, Activity, Add, Announce, Application, Arrive, Article, Block, Collection, CollectionPage, Create, Delete, Dislike, Flag, Follow, Group, Ignore, IntransitiveActivity, Invite, Join, Leave, Like, Link, Listen, Mention, Move, Note, Object as Object$1, Offer, OrderedCollection, OrderedCollectionPage, Organization, Person, Question, Read, Reject, Remove, Service, TentativeAccept, TentativeReject, Travel, Undo, Update, View, getEntityTypeById } from "@fedify/vocab";
|
|
3
|
+
import { preloadedContexts } from "@fedify/vocab-runtime";
|
|
4
|
+
//#region src/document.ts
|
|
5
|
+
/** Stable classifications; human-readable diagnostic messages may change. */
|
|
6
|
+
const diagnosticReasons = {
|
|
7
|
+
invalid_document_root: {
|
|
8
|
+
category: "vocabulary",
|
|
9
|
+
severity: "error"
|
|
10
|
+
},
|
|
11
|
+
non_json_value: {
|
|
12
|
+
category: "vocabulary",
|
|
13
|
+
severity: "error"
|
|
14
|
+
},
|
|
15
|
+
invalid_value_shape: {
|
|
16
|
+
category: "vocabulary",
|
|
17
|
+
severity: "error"
|
|
18
|
+
},
|
|
19
|
+
invalid_iri: {
|
|
20
|
+
category: "vocabulary",
|
|
21
|
+
severity: "error"
|
|
22
|
+
},
|
|
23
|
+
invalid_datetime: {
|
|
24
|
+
category: "vocabulary",
|
|
25
|
+
severity: "error"
|
|
26
|
+
},
|
|
27
|
+
invalid_multiplicity: {
|
|
28
|
+
category: "vocabulary",
|
|
29
|
+
severity: "error"
|
|
30
|
+
},
|
|
31
|
+
property_not_applicable: {
|
|
32
|
+
category: "vocabulary",
|
|
33
|
+
severity: "error"
|
|
34
|
+
},
|
|
35
|
+
invalid_context: {
|
|
36
|
+
category: "vocabulary",
|
|
37
|
+
severity: "error"
|
|
38
|
+
},
|
|
39
|
+
unauthorized_local_identity: {
|
|
40
|
+
category: "authority",
|
|
41
|
+
severity: "error"
|
|
42
|
+
},
|
|
43
|
+
server_owned_field_conflict: {
|
|
44
|
+
category: "authority",
|
|
45
|
+
severity: "error"
|
|
46
|
+
},
|
|
47
|
+
context_unavailable_offline: {
|
|
48
|
+
category: "support",
|
|
49
|
+
severity: "error"
|
|
50
|
+
},
|
|
51
|
+
unsupported_context_form: {
|
|
52
|
+
category: "support",
|
|
53
|
+
severity: "error"
|
|
54
|
+
},
|
|
55
|
+
unsupported_type: {
|
|
56
|
+
category: "support",
|
|
57
|
+
severity: "error"
|
|
58
|
+
},
|
|
59
|
+
unsupported_property: {
|
|
60
|
+
category: "support",
|
|
61
|
+
severity: "error"
|
|
62
|
+
},
|
|
63
|
+
unsupported_value_form: {
|
|
64
|
+
category: "support",
|
|
65
|
+
severity: "error"
|
|
66
|
+
},
|
|
67
|
+
processing_limit_exceeded: {
|
|
68
|
+
category: "support",
|
|
69
|
+
severity: "error"
|
|
70
|
+
},
|
|
71
|
+
diagnostic_limit_reached: {
|
|
72
|
+
category: "support",
|
|
73
|
+
severity: "error"
|
|
74
|
+
},
|
|
75
|
+
implicit_activitystreams_context: {
|
|
76
|
+
category: "compatibility",
|
|
77
|
+
severity: "warning"
|
|
78
|
+
},
|
|
79
|
+
extension_property_unchecked: {
|
|
80
|
+
category: "compatibility",
|
|
81
|
+
severity: "warning"
|
|
82
|
+
},
|
|
83
|
+
empty_property_list: {
|
|
84
|
+
category: "compatibility",
|
|
85
|
+
severity: "warning"
|
|
86
|
+
},
|
|
87
|
+
absent_identifier: {
|
|
88
|
+
category: "compatibility",
|
|
89
|
+
severity: "warning"
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const activityStreamsContext = "https://www.w3.org/ns/activitystreams";
|
|
93
|
+
const maximumDiagnostics = 100;
|
|
94
|
+
const maximumPathDepth = 64;
|
|
95
|
+
const actorTypes = [
|
|
96
|
+
Application,
|
|
97
|
+
Group,
|
|
98
|
+
Organization,
|
|
99
|
+
Person,
|
|
100
|
+
Service
|
|
101
|
+
];
|
|
102
|
+
const addressingProperties = [
|
|
103
|
+
"to",
|
|
104
|
+
"cc",
|
|
105
|
+
"bto",
|
|
106
|
+
"bcc",
|
|
107
|
+
"audience"
|
|
108
|
+
];
|
|
109
|
+
const objectOnlyProperties = /* @__PURE__ */ new Set([
|
|
110
|
+
"attachment",
|
|
111
|
+
"attributedTo",
|
|
112
|
+
"audience",
|
|
113
|
+
"content",
|
|
114
|
+
"contentMap",
|
|
115
|
+
"context",
|
|
116
|
+
"endTime",
|
|
117
|
+
"generator",
|
|
118
|
+
"icon",
|
|
119
|
+
"image",
|
|
120
|
+
"inReplyTo",
|
|
121
|
+
"location",
|
|
122
|
+
"published",
|
|
123
|
+
"replies",
|
|
124
|
+
"startTime",
|
|
125
|
+
"summary",
|
|
126
|
+
"summaryMap",
|
|
127
|
+
"tag",
|
|
128
|
+
"updated",
|
|
129
|
+
"url",
|
|
130
|
+
"to",
|
|
131
|
+
"bto",
|
|
132
|
+
"cc",
|
|
133
|
+
"bcc",
|
|
134
|
+
"duration"
|
|
135
|
+
]);
|
|
136
|
+
const implementedTypes = /* @__PURE__ */ new Set([
|
|
137
|
+
Note,
|
|
138
|
+
Object$1,
|
|
139
|
+
Article,
|
|
140
|
+
Application,
|
|
141
|
+
Group,
|
|
142
|
+
Organization,
|
|
143
|
+
Person,
|
|
144
|
+
Service,
|
|
145
|
+
Collection,
|
|
146
|
+
OrderedCollection,
|
|
147
|
+
CollectionPage,
|
|
148
|
+
OrderedCollectionPage,
|
|
149
|
+
Link,
|
|
150
|
+
Mention,
|
|
151
|
+
Activity,
|
|
152
|
+
Like,
|
|
153
|
+
Create,
|
|
154
|
+
Undo,
|
|
155
|
+
Accept,
|
|
156
|
+
Follow,
|
|
157
|
+
IntransitiveActivity,
|
|
158
|
+
Arrive,
|
|
159
|
+
Travel,
|
|
160
|
+
Add,
|
|
161
|
+
Announce,
|
|
162
|
+
Block,
|
|
163
|
+
Delete,
|
|
164
|
+
Dislike,
|
|
165
|
+
Flag,
|
|
166
|
+
Ignore,
|
|
167
|
+
Invite,
|
|
168
|
+
Join,
|
|
169
|
+
Leave,
|
|
170
|
+
Listen,
|
|
171
|
+
Move,
|
|
172
|
+
Offer,
|
|
173
|
+
Question,
|
|
174
|
+
Read,
|
|
175
|
+
Reject,
|
|
176
|
+
Remove,
|
|
177
|
+
TentativeAccept,
|
|
178
|
+
TentativeReject,
|
|
179
|
+
Update,
|
|
180
|
+
View
|
|
181
|
+
]);
|
|
182
|
+
function createReporter() {
|
|
183
|
+
const diagnostics = [];
|
|
184
|
+
let complete = true;
|
|
185
|
+
let truncated = false;
|
|
186
|
+
const report = (reason, path, message) => {
|
|
187
|
+
if (truncated) return;
|
|
188
|
+
if (diagnostics.length === maximumDiagnostics) {
|
|
189
|
+
complete = false;
|
|
190
|
+
truncated = true;
|
|
191
|
+
diagnostics[99] = {
|
|
192
|
+
reason: "diagnostic_limit_reached",
|
|
193
|
+
...diagnosticReasons.diagnostic_limit_reached,
|
|
194
|
+
path: [],
|
|
195
|
+
message: "Further diagnostics were omitted."
|
|
196
|
+
};
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const classification = diagnosticReasons[reason];
|
|
200
|
+
if (classification.category === "support") complete = false;
|
|
201
|
+
diagnostics.push({
|
|
202
|
+
reason,
|
|
203
|
+
...classification,
|
|
204
|
+
path,
|
|
205
|
+
message
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
return {
|
|
209
|
+
report,
|
|
210
|
+
isFull: () => truncated,
|
|
211
|
+
result: () => ({
|
|
212
|
+
complete,
|
|
213
|
+
diagnostics,
|
|
214
|
+
changes: []
|
|
215
|
+
})
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function readDocument(document, path, report) {
|
|
219
|
+
if (!isJsonValueShape(document)) {
|
|
220
|
+
report("non_json_value", path, "Expected a JSON value.");
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (document == null || typeof document !== "object" || Array.isArray(document)) {
|
|
224
|
+
report("invalid_document_root", path, "Expected a document object.");
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const prototype = Object.getPrototypeOf(document);
|
|
228
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
229
|
+
report("non_json_value", path, "Expected a plain JSON data object.");
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (Reflect.ownKeys(document).some((key) => typeof key !== "string")) {
|
|
233
|
+
report("non_json_value", path, "JSON objects cannot have symbol properties.");
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const descriptors = Object.getOwnPropertyDescriptors(document);
|
|
237
|
+
const values = Object.create(null);
|
|
238
|
+
for (const key of Object.keys(descriptors).sort()) {
|
|
239
|
+
const descriptor = descriptors[key];
|
|
240
|
+
if (!("value" in descriptor) || !descriptor.enumerable) {
|
|
241
|
+
report("non_json_value", [...path, key], "Expected an enumerable data property.");
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const value = descriptor.value;
|
|
245
|
+
if (!isJsonValueShape(value)) {
|
|
246
|
+
report("non_json_value", [...path, key], "Expected a JSON value.");
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
values[key] = value;
|
|
250
|
+
}
|
|
251
|
+
return values;
|
|
252
|
+
}
|
|
253
|
+
function isJsonValueShape(value) {
|
|
254
|
+
return value === null || typeof value === "object" || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value);
|
|
255
|
+
}
|
|
256
|
+
function checkReference(value, path, report) {
|
|
257
|
+
if (value === null) return;
|
|
258
|
+
if (typeof value !== "string") {
|
|
259
|
+
if (typeof value === "object") report("unsupported_value_form", path, "Embedded values are not checked yet.");
|
|
260
|
+
else report("invalid_value_shape", path, "Expected a resource reference, embedded object, or allowed list.");
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const hasControlOrSpace = Array.from(value).some((character) => {
|
|
264
|
+
const point = character.codePointAt(0);
|
|
265
|
+
return point <= 32 || point === 127;
|
|
266
|
+
});
|
|
267
|
+
if (!value.isWellFormed() || hasControlOrSpace || /[<>"{}|\\^`]/u.test(value) || /%(?![\da-f]{2})/iu.test(value)) report("invalid_iri", path, "Expected a syntactically valid IRI reference.");
|
|
268
|
+
else if (!/^[a-z][a-z\d+.-]*:/iu.test(value)) report("unsupported_value_form", path, "Relative references need a base-IRI policy.");
|
|
269
|
+
else if (!URL.canParse(value)) report("unsupported_value_form", path, "This IRI cannot be represented by the current URL parser.");
|
|
270
|
+
}
|
|
271
|
+
function checkReferenceEntry(value, path, report, check) {
|
|
272
|
+
if (value === null) return;
|
|
273
|
+
if (!isJsonValueShape(value)) report("non_json_value", path, "Expected a JSON value.");
|
|
274
|
+
else if (Array.isArray(value)) report("unsupported_value_form", path, "Nested lists are not checked yet.");
|
|
275
|
+
else check(value, path, report);
|
|
276
|
+
}
|
|
277
|
+
function visitList(values, path, report, visit) {
|
|
278
|
+
if (Reflect.ownKeys(values).length > values.length + 1) {
|
|
279
|
+
report("non_json_value", path, "Expected a JSON array without extra properties.");
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
for (let index = 0; index < values.length; index += 1) {
|
|
283
|
+
const entryPath = [...path, index];
|
|
284
|
+
const descriptor = Object.getOwnPropertyDescriptor(values, index);
|
|
285
|
+
if (descriptor == null || !("value" in descriptor) || !descriptor.enumerable) {
|
|
286
|
+
report("non_json_value", entryPath, "Expected an enumerable array entry.");
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
const entry = descriptor.value;
|
|
290
|
+
visit(entry, entryPath);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
function checkReferenceValues(value, path, report, check = checkReference, schedule = (work) => work()) {
|
|
294
|
+
if (!Array.isArray(value)) {
|
|
295
|
+
schedule(() => check(value, path, report));
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (value.length === 0) {
|
|
299
|
+
report("unsupported_value_form", path, "Empty lists are not supported in this value position.");
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
visitList(value, path, (reason, entryPath, message) => schedule(() => report(reason, entryPath, message)), (entry, entryPath) => {
|
|
303
|
+
schedule(() => checkReferenceEntry(entry, entryPath, report, check));
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
function checkSingleReference(value, path, report, check, schedule) {
|
|
307
|
+
checkSingleValue(value, path, (reason, entryPath, message) => schedule(() => report(reason, entryPath, message)), (entry, entryPath) => schedule(() => check(entry, entryPath, report)));
|
|
308
|
+
}
|
|
309
|
+
function sameContextTerm(actual, expected) {
|
|
310
|
+
return actual != null && actual.iri === expected.iri && actual.type === expected.type && actual.container === expected.container && actual.prefix === expected.prefix;
|
|
311
|
+
}
|
|
312
|
+
function isSupportedContextUri(value) {
|
|
313
|
+
return /^[a-z][a-z\d+.-]*:/iu.test(value) && !Array.from(value).some((character) => {
|
|
314
|
+
const point = character.codePointAt(0);
|
|
315
|
+
return point <= 32 || point >= 127 && point <= 159 || "<>\"{}|\\^`".includes(character);
|
|
316
|
+
}) && URL.canParse(value);
|
|
317
|
+
}
|
|
318
|
+
function resolveTerm(value, terms, pending) {
|
|
319
|
+
if (value === "@id" || value === "@type") return value;
|
|
320
|
+
if (pending?.has(value)) return void 0;
|
|
321
|
+
const term = terms.get(value);
|
|
322
|
+
if (term != null) return term.iri;
|
|
323
|
+
const colon = value.indexOf(":");
|
|
324
|
+
if (colon < 0) return void 0;
|
|
325
|
+
if (pending?.has(value.slice(0, colon))) return void 0;
|
|
326
|
+
const prefix = terms.get(value.slice(0, colon));
|
|
327
|
+
if (prefix?.prefix) {
|
|
328
|
+
const expanded = prefix.iri + value.slice(colon + 1);
|
|
329
|
+
return isSupportedContextUri(expanded) ? expanded : void 0;
|
|
330
|
+
}
|
|
331
|
+
return isSupportedContextUri(value) ? value : void 0;
|
|
332
|
+
}
|
|
333
|
+
function readContextTerm(definition, path, terms, report, pending) {
|
|
334
|
+
let values;
|
|
335
|
+
if (typeof definition === "string") values = { "@id": definition };
|
|
336
|
+
else if (definition !== null && typeof definition === "object" && !Array.isArray(definition)) {
|
|
337
|
+
const record = readDocument(definition, path, report);
|
|
338
|
+
if (record == null) return void 0;
|
|
339
|
+
values = record;
|
|
340
|
+
} else {
|
|
341
|
+
report(definition === null ? "unsupported_context_form" : "invalid_context", path, "Only string and simple term definitions are supported.");
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
if (Object.keys(values).some((key) => ![
|
|
345
|
+
"@id",
|
|
346
|
+
"@type",
|
|
347
|
+
"@container",
|
|
348
|
+
"@prefix"
|
|
349
|
+
].includes(key))) {
|
|
350
|
+
report("unsupported_context_form", path, "Scoped contexts, reverse properties, and complex term definitions are not supported.");
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const id = values["@id"];
|
|
354
|
+
if (id === void 0) {
|
|
355
|
+
report("unsupported_context_form", path, "Inline term definitions require an explicit @id in this profile.");
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (typeof id !== "string") {
|
|
359
|
+
report("invalid_context", path, "Expected a string @id mapping.");
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (/^[#/.]/u.test(id) || id.startsWith("_:")) {
|
|
363
|
+
report("unsupported_context_form", path, "Relative and blank-node term mappings are outside the supported profile.");
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (id.startsWith("@") && !["@id", "@type"].includes(id)) {
|
|
367
|
+
report("unsupported_context_form", path, "This keyword alias is outside the supported profile.");
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
return resolveContextTerm(id, values, typeof definition === "string", path, terms, report, pending);
|
|
371
|
+
}
|
|
372
|
+
function resolveContextTerm(id, values, implicitPrefix, path, terms, report, pending) {
|
|
373
|
+
const iri = resolveTerm(id, terms, pending);
|
|
374
|
+
if (iri == null) return void 0;
|
|
375
|
+
if (iri.startsWith("@") && Object.keys(values).some((key) => key !== "@id")) {
|
|
376
|
+
report("unsupported_context_form", path, "Keyword aliases with additional coercion or container options are not supported.");
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
const type = values["@type"];
|
|
380
|
+
if (typeof type === "string" && type.startsWith("@") && type !== "@id") {
|
|
381
|
+
report("unsupported_context_form", path, "This type coercion is outside the supported profile.");
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const expandedType = typeof type === "string" ? resolveTerm(type, terms, pending) : void 0;
|
|
385
|
+
if (type !== void 0 && expandedType === void 0) return void 0;
|
|
386
|
+
const container = values["@container"];
|
|
387
|
+
if (container === "@language" && type !== void 0) {
|
|
388
|
+
report("unsupported_context_form", path, "Language containers with type coercion are not supported.");
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
if (container !== void 0 && container !== "@language" && container !== "@list") {
|
|
392
|
+
report("unsupported_context_form", path, "This container form is not supported.");
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
if (values["@prefix"] !== void 0 && typeof values["@prefix"] !== "boolean") {
|
|
396
|
+
report("invalid_context", path, "Expected a boolean @prefix flag.");
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
return {
|
|
400
|
+
iri,
|
|
401
|
+
type: expandedType,
|
|
402
|
+
container,
|
|
403
|
+
prefix: values["@prefix"] === true || implicitPrefix && /[#/:]$/u.test(iri)
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
function applyContextTerms(values, path, terms, report, protectedTerms) {
|
|
407
|
+
let valid = true;
|
|
408
|
+
const localReport = (...args) => {
|
|
409
|
+
valid = false;
|
|
410
|
+
report(...args);
|
|
411
|
+
};
|
|
412
|
+
const pending = new Set(Object.keys(values));
|
|
413
|
+
for (const name of pending) terms.delete(name);
|
|
414
|
+
while (pending.size > 0) {
|
|
415
|
+
if (!valid) return false;
|
|
416
|
+
let resolved = false;
|
|
417
|
+
for (const name of pending) {
|
|
418
|
+
const fieldPath = [...path, name];
|
|
419
|
+
if (name.length === 0) {
|
|
420
|
+
localReport("invalid_context", fieldPath, "A context term cannot be empty.");
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
if (name.includes(":")) {
|
|
424
|
+
localReport("unsupported_context_form", fieldPath, "IRI-shaped term definitions are outside the supported inline profile.");
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (name.startsWith("@")) {
|
|
428
|
+
if (protectedTerms == null && name === "@vocab" && values[name] === "_:") pending.delete(name);
|
|
429
|
+
else localReport("unsupported_context_form", fieldPath, "This context directive is outside the supported profile.");
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
const term = readContextTerm(values[name], fieldPath, terms, localReport, pending);
|
|
433
|
+
if (term == null) continue;
|
|
434
|
+
const original = protectedTerms?.get(name);
|
|
435
|
+
if (original != null && !sameContextTerm(term, original)) {
|
|
436
|
+
localReport("unsupported_context_form", fieldPath, "The standard ActivityStreams context cannot be overridden.");
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
terms.set(name, term);
|
|
440
|
+
pending.delete(name);
|
|
441
|
+
resolved = true;
|
|
442
|
+
}
|
|
443
|
+
if (!resolved && pending.size > 0 && valid) for (const name of pending) localReport("invalid_context", [...path, name], "The term has an unresolved or cyclic IRI mapping.");
|
|
444
|
+
}
|
|
445
|
+
return valid;
|
|
446
|
+
}
|
|
447
|
+
let contextTables;
|
|
448
|
+
function getContextTables() {
|
|
449
|
+
if (contextTables != null) return contextTables;
|
|
450
|
+
const bundledContexts = /* @__PURE__ */ new Map();
|
|
451
|
+
for (const url of [activityStreamsContext, "https://w3id.org/security/v1"]) {
|
|
452
|
+
const document = preloadedContexts[url];
|
|
453
|
+
const terms = /* @__PURE__ */ new Map();
|
|
454
|
+
applyContextTerms(document["@context"], [], terms, (_reason, _path, message) => {
|
|
455
|
+
throw new Error(message);
|
|
456
|
+
});
|
|
457
|
+
bundledContexts.set(url, terms);
|
|
458
|
+
}
|
|
459
|
+
const standardTerms = bundledContexts.get(activityStreamsContext);
|
|
460
|
+
contextTables = {
|
|
461
|
+
bundledContexts,
|
|
462
|
+
standardTerms,
|
|
463
|
+
standardContext: { terms: standardTerms }
|
|
464
|
+
};
|
|
465
|
+
return contextTables;
|
|
466
|
+
}
|
|
467
|
+
function applyContextEntry(value, path, terms, report) {
|
|
468
|
+
const { bundledContexts, standardTerms } = getContextTables();
|
|
469
|
+
if (typeof value === "string") {
|
|
470
|
+
const url = value === "http://www.w3.org/ns/activitystreams" ? activityStreamsContext : value;
|
|
471
|
+
const bundled = bundledContexts.get(url);
|
|
472
|
+
if (bundled == null) {
|
|
473
|
+
report("context_unavailable_offline", path, "This context is outside the offline allowlist.");
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
for (const [name, term] of bundled) terms.set(name, term);
|
|
477
|
+
return true;
|
|
478
|
+
}
|
|
479
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
480
|
+
report(value === null || Array.isArray(value) ? "unsupported_context_form" : "invalid_context", path, "Expected an allowlisted context or a simple inline context.");
|
|
481
|
+
return false;
|
|
482
|
+
}
|
|
483
|
+
const definitions = readDocument(value, path, report);
|
|
484
|
+
return definitions != null && applyContextTerms(definitions, path, terms, report, standardTerms);
|
|
485
|
+
}
|
|
486
|
+
function checkContext(values, path, report, inherited) {
|
|
487
|
+
const { standardContext, standardTerms } = getContextTables();
|
|
488
|
+
if (!Object.hasOwn(values, "@context")) {
|
|
489
|
+
if (inherited != null) return inherited;
|
|
490
|
+
report("implicit_activitystreams_context", [...path, "@context"], "Assuming the ActivityStreams context for this document.");
|
|
491
|
+
return standardContext;
|
|
492
|
+
}
|
|
493
|
+
const terms = new Map(inherited?.terms);
|
|
494
|
+
let valid = true;
|
|
495
|
+
const localReport = (...args) => {
|
|
496
|
+
valid = false;
|
|
497
|
+
report(...args);
|
|
498
|
+
};
|
|
499
|
+
const context = values["@context"];
|
|
500
|
+
const contextPath = [...path, "@context"];
|
|
501
|
+
if (Array.isArray(context)) visitList(context, contextPath, localReport, (entry, entryPath) => {
|
|
502
|
+
if (!applyContextEntry(entry, entryPath, terms, localReport)) valid = false;
|
|
503
|
+
});
|
|
504
|
+
else if (!applyContextEntry(context, contextPath, terms, localReport)) valid = false;
|
|
505
|
+
if (!valid) return void 0;
|
|
506
|
+
if (![...standardTerms].every(([name, term]) => sameContextTerm(terms.get(name), term))) {
|
|
507
|
+
report("unsupported_context_form", contextPath, "The context must include the standard ActivityStreams vocabulary.");
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
return { terms };
|
|
511
|
+
}
|
|
512
|
+
function resolveProperty(source, context) {
|
|
513
|
+
const { standardTerms } = getContextTables();
|
|
514
|
+
const term = context.terms.get(source);
|
|
515
|
+
const iri = term?.iri ?? resolveTerm(source, context.terms);
|
|
516
|
+
for (const [name, standard] of standardTerms) {
|
|
517
|
+
if (iri !== standard.iri || term?.container !== standard.container) continue;
|
|
518
|
+
return {
|
|
519
|
+
source,
|
|
520
|
+
name,
|
|
521
|
+
iri: standard.iri,
|
|
522
|
+
compatible: term?.type === standard.type
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
if (iri === "@id") return {
|
|
526
|
+
source,
|
|
527
|
+
name: "id",
|
|
528
|
+
iri: "@id",
|
|
529
|
+
compatible: true
|
|
530
|
+
};
|
|
531
|
+
if (iri === "@type") return {
|
|
532
|
+
source,
|
|
533
|
+
name: "type",
|
|
534
|
+
iri: "@type",
|
|
535
|
+
compatible: true
|
|
536
|
+
};
|
|
537
|
+
return {
|
|
538
|
+
source,
|
|
539
|
+
...iri == null ? {} : { iri },
|
|
540
|
+
compatible: false
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
function isOpaqueExtension(field) {
|
|
544
|
+
const { standardTerms } = getContextTables();
|
|
545
|
+
return !field.source.startsWith("@") && !field.iri?.startsWith("@") && !Array.from(standardTerms.values()).some((term) => term.iri === field.iri);
|
|
546
|
+
}
|
|
547
|
+
function isTypeCombination(value) {
|
|
548
|
+
if (!Array.isArray(value) || value.length <= 1) return false;
|
|
549
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
550
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, index);
|
|
551
|
+
if (descriptor == null || !("value" in descriptor) || typeof descriptor.value !== "string") return false;
|
|
552
|
+
}
|
|
553
|
+
return true;
|
|
554
|
+
}
|
|
555
|
+
function checkType(value, path, report, context) {
|
|
556
|
+
const typeIri = typeof value === "string" ? resolveTerm(value, context.terms) : void 0;
|
|
557
|
+
let Type;
|
|
558
|
+
if (typeIri != null) {
|
|
559
|
+
Type = getEntityTypeById(typeIri);
|
|
560
|
+
if (typeIri === Link.typeId.href) Type = Link;
|
|
561
|
+
else if (typeIri === Mention.typeId.href) Type = Mention;
|
|
562
|
+
}
|
|
563
|
+
if (Type == null || !implementedTypes.has(Type)) {
|
|
564
|
+
report(Array.isArray(value) ? "unsupported_value_form" : value == null || typeof value === "string" ? "unsupported_type" : "invalid_value_shape", path, isTypeCombination(value) ? "Type combinations are not supported. Provide a single supported type." : Array.isArray(value) ? "Type arrays are not supported. Provide a single type string." : "This type is not supported by the checker yet.");
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
return Type;
|
|
568
|
+
}
|
|
569
|
+
function isTypeOrSubtype(Type, Base) {
|
|
570
|
+
return Type === Base || Type.prototype instanceof Base;
|
|
571
|
+
}
|
|
572
|
+
function checkEmbeddedFedifyRange(Type, expected, path, report) {
|
|
573
|
+
if (expected?.every((Expected) => !isTypeOrSubtype(Type, Expected))) report("unsupported_value_form", path, `Fedify cannot preserve this embedded type for this property. Use ${expected.map((Expected) => Expected.name).join(", ")} or an IRI reference.`);
|
|
574
|
+
}
|
|
575
|
+
function getEmbeddedFedifyRange(property, collectionRange) {
|
|
576
|
+
if (property === "actor" || property === "attributedTo") return actorTypes;
|
|
577
|
+
return collectionRange?.filter((Expected) => Expected !== Link);
|
|
578
|
+
}
|
|
579
|
+
function checkDocumentType(values, fields, path, context, report) {
|
|
580
|
+
const types = fields.filter(({ name }) => name === "type");
|
|
581
|
+
if (types.length > 1) {
|
|
582
|
+
for (const field of types) report("unsupported_value_form", [...path, field.source], "Multiple type declarations are not supported. Provide a single type field.");
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
const source = types[0]?.source ?? "type";
|
|
586
|
+
return checkType(values[source], [...path, source], report, context);
|
|
587
|
+
}
|
|
588
|
+
function getCollectionRange(Type, property) {
|
|
589
|
+
if (isTypeOrSubtype(Type, Collection)) {
|
|
590
|
+
if ([
|
|
591
|
+
"current",
|
|
592
|
+
"first",
|
|
593
|
+
"last"
|
|
594
|
+
].includes(property)) return [CollectionPage, Link];
|
|
595
|
+
}
|
|
596
|
+
if (isTypeOrSubtype(Type, CollectionPage)) {
|
|
597
|
+
if (property === "partOf") return [Collection, Link];
|
|
598
|
+
if (["next", "prev"].includes(property)) return [CollectionPage, Link];
|
|
599
|
+
}
|
|
600
|
+
if (actorTypes.some((Actor) => isTypeOrSubtype(Type, Actor))) {
|
|
601
|
+
if (["inbox", "outbox"].includes(property)) return [OrderedCollection, OrderedCollectionPage];
|
|
602
|
+
if ([
|
|
603
|
+
"following",
|
|
604
|
+
"followers",
|
|
605
|
+
"liked"
|
|
606
|
+
].includes(property)) return [Collection];
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
function isFunctionalProperty(Type, property) {
|
|
610
|
+
if (["id", "mediaType"].includes(property)) return true;
|
|
611
|
+
if (getCollectionRange(Type, property) != null) return true;
|
|
612
|
+
if (isTypeOrSubtype(Type, Link)) return [
|
|
613
|
+
"href",
|
|
614
|
+
"height",
|
|
615
|
+
"width",
|
|
616
|
+
"hreflang"
|
|
617
|
+
].includes(property);
|
|
618
|
+
if ([
|
|
619
|
+
"published",
|
|
620
|
+
"updated",
|
|
621
|
+
"startTime",
|
|
622
|
+
"endTime"
|
|
623
|
+
].includes(property)) return true;
|
|
624
|
+
if (isTypeOrSubtype(Type, Collection) && property === "totalItems") return true;
|
|
625
|
+
return isTypeOrSubtype(Type, OrderedCollectionPage) && property === "startIndex";
|
|
626
|
+
}
|
|
627
|
+
function countValues(value) {
|
|
628
|
+
if (!Array.isArray(value)) return value === null ? 0 : 1;
|
|
629
|
+
let count = 0;
|
|
630
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
631
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, index);
|
|
632
|
+
if (descriptor != null && "value" in descriptor && descriptor.value !== null) count += 1;
|
|
633
|
+
}
|
|
634
|
+
return count;
|
|
635
|
+
}
|
|
636
|
+
function findMultipleAliases(Type, fields, values) {
|
|
637
|
+
const groups = /* @__PURE__ */ new Map();
|
|
638
|
+
for (const field of fields) {
|
|
639
|
+
if (field.name == null || field.iri == null || !isFunctionalProperty(Type, field.name)) continue;
|
|
640
|
+
if (countValues(values[field.source]) === 0) continue;
|
|
641
|
+
const group = groups.get(field.iri) ?? [];
|
|
642
|
+
group.push(field.source);
|
|
643
|
+
groups.set(field.iri, group);
|
|
644
|
+
}
|
|
645
|
+
const multiple = /* @__PURE__ */ new Set();
|
|
646
|
+
for (const group of groups.values()) {
|
|
647
|
+
if (group.length < 2) continue;
|
|
648
|
+
for (const source of group) if (countValues(values[source]) <= 1) multiple.add(source);
|
|
649
|
+
}
|
|
650
|
+
return multiple;
|
|
651
|
+
}
|
|
652
|
+
function checkNonNegativeInteger(value, path, report) {
|
|
653
|
+
if (value === null) return;
|
|
654
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 0) report("invalid_value_shape", path, "Expected a non-negative integer.");
|
|
655
|
+
}
|
|
656
|
+
function checkSingleValue(value, path, report, check) {
|
|
657
|
+
if (!Array.isArray(value)) {
|
|
658
|
+
check(value, path, report);
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
if (value.length === 0) {
|
|
662
|
+
report("unsupported_value_form", path, "Empty lists are not supported in this value position.");
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
const entries = [];
|
|
666
|
+
visitList(value, path, report, (entry, entryPath) => {
|
|
667
|
+
if (entry !== null) entries.push({
|
|
668
|
+
value: entry,
|
|
669
|
+
path: entryPath
|
|
670
|
+
});
|
|
671
|
+
});
|
|
672
|
+
if (entries.length > 1) report("invalid_multiplicity", path, "Expected at most one non-null value.");
|
|
673
|
+
for (const entry of entries) checkReferenceEntry(entry.value, entry.path, report, check);
|
|
674
|
+
}
|
|
675
|
+
function checkLanguageTag(value, path, report) {
|
|
676
|
+
if (typeof value !== "string" || !/^[a-z]{1,8}(?:-[a-z\d]{1,8})*$/iu.test(value)) {
|
|
677
|
+
report("invalid_value_shape", path, "Expected a language tag.");
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
try {
|
|
681
|
+
new Intl.Locale(value);
|
|
682
|
+
} catch (error) {
|
|
683
|
+
if (!(error instanceof RangeError)) throw error;
|
|
684
|
+
report("unsupported_value_form", path, "This language tag is outside the current locale parser's support.");
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
function checkText(value, path, report, plain) {
|
|
688
|
+
if (value === null) return;
|
|
689
|
+
if (typeof value === "string") {
|
|
690
|
+
if (plain && /<(?:[a-z][^>]*>|\/[a-z][^>]*>|!--)/iu.test(value)) report("invalid_value_shape", path, "Names must be plain text without HTML markup.");
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
if (typeof value !== "object" || Array.isArray(value)) {
|
|
694
|
+
report("invalid_value_shape", path, "Expected text.");
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
const values = readDocument(value, path, report);
|
|
698
|
+
if (values == null) return;
|
|
699
|
+
if (!Object.hasOwn(values, "@value")) {
|
|
700
|
+
report("unsupported_value_form", path, "Use a language-map property or an explicit @value object for localized text.");
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
for (const key of Object.keys(values).sort()) {
|
|
704
|
+
const fieldPath = [...path, key];
|
|
705
|
+
if (key === "@value") {
|
|
706
|
+
if (typeof values[key] === "string") checkText(values[key], fieldPath, report, plain);
|
|
707
|
+
else report("invalid_value_shape", fieldPath, "Expected a text value.");
|
|
708
|
+
} else if (key === "@language") checkLanguageTag(values[key], fieldPath, report);
|
|
709
|
+
else report("unsupported_value_form", fieldPath, "This text-value attribute is not supported yet.");
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
function checkLanguageMap(value, path, report, plain) {
|
|
713
|
+
if (value === null) return;
|
|
714
|
+
if (typeof value !== "object" || Array.isArray(value)) {
|
|
715
|
+
report("invalid_value_shape", path, "Expected a language map.");
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
const values = readDocument(value, path, report);
|
|
719
|
+
if (values == null) return;
|
|
720
|
+
for (const language of Object.keys(values).sort()) {
|
|
721
|
+
const fieldPath = [...path, language];
|
|
722
|
+
checkLanguageTag(language, fieldPath, report);
|
|
723
|
+
if (values[language] === null) continue;
|
|
724
|
+
if (typeof values[language] === "string") checkText(values[language], fieldPath, report, plain);
|
|
725
|
+
else report("invalid_value_shape", fieldPath, "Expected a language-map string.");
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
function checkDateTime(value, path, report) {
|
|
729
|
+
if (value === null) return;
|
|
730
|
+
if (typeof value !== "string") {
|
|
731
|
+
report("invalid_value_shape", path, "Expected a date-time string.");
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
const match = /^(?<minute>\d{4}-\d{2}-\d{2}T\d{2}:\d{2})(?<second>:\d{2})?(?<fraction>\.\d+)?(?<offset>Z|[+-]\d{2}:\d{2})$/u.exec(value);
|
|
735
|
+
if (match == null) {
|
|
736
|
+
report("invalid_datetime", path, "Expected an ActivityStreams date-time with a timezone.");
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
const { minute, second, fraction, offset } = match.groups;
|
|
740
|
+
try {
|
|
741
|
+
Temporal.Instant.from(`${minute}${second ?? ":00"}${offset}`);
|
|
742
|
+
} catch (error) {
|
|
743
|
+
if (!(error instanceof RangeError)) throw error;
|
|
744
|
+
report("invalid_datetime", path, "Expected a valid calendar date, time, and offset.");
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
if (second === ":60") {
|
|
748
|
+
report("unsupported_value_form", path, "Leap-second timestamps are not checked by the current date-time parser.");
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
if (second == null) {
|
|
752
|
+
report("unsupported_value_form", path, fraction != null ? "ActivityStreams permits fractions without seconds, but Fedify requires explicit seconds before a fractional component." : "ActivityStreams permits omitted seconds, but Fedify requires explicit seconds, such as 12:34:00Z.");
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
if (fraction != null && fraction.length > 10) {
|
|
756
|
+
report("unsupported_value_form", path, "Fractional seconds exceeding nine digits cannot be preserved through Fedify parsing; provide at most nine fractional digits.");
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
try {
|
|
760
|
+
Temporal.Instant.from(value);
|
|
761
|
+
} catch (error) {
|
|
762
|
+
if (!(error instanceof RangeError)) throw error;
|
|
763
|
+
report("unsupported_value_form", path, "This date-time matches the ActivityStreams format, but cannot be parsed as an instant without rewriting; provide a timestamp such as 2024-01-01T12:34:00Z.");
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
function getCommonPropertyKind(Type, property) {
|
|
767
|
+
const object = isTypeOrSubtype(Type, Object$1);
|
|
768
|
+
const actor = actorTypes.some((Actor) => isTypeOrSubtype(Type, Actor));
|
|
769
|
+
const textProperties = object ? [
|
|
770
|
+
"name",
|
|
771
|
+
"summary",
|
|
772
|
+
"content"
|
|
773
|
+
] : ["name"];
|
|
774
|
+
if (actor) textProperties.push("preferredUsername");
|
|
775
|
+
if (textProperties.includes(property)) return "text";
|
|
776
|
+
if (textProperties.some((name) => property === `${name}Map` && name !== "preferredUsername")) return "languageMap";
|
|
777
|
+
if (object && [
|
|
778
|
+
"published",
|
|
779
|
+
"updated",
|
|
780
|
+
"startTime",
|
|
781
|
+
"endTime"
|
|
782
|
+
].includes(property)) return "datetime";
|
|
783
|
+
if (property === "id" || property === "mediaType") return property;
|
|
784
|
+
}
|
|
785
|
+
function checkCommonProperty(kind, property, value, path, report) {
|
|
786
|
+
if (kind === "text") checkReferenceValues(value, path, report, (entry, entryPath) => checkText(entry, entryPath, report, property === "name"));
|
|
787
|
+
else if (kind === "languageMap") checkLanguageMap(value, path, report, property === "nameMap");
|
|
788
|
+
else if (kind === "datetime") checkSingleValue(value, path, report, checkDateTime);
|
|
789
|
+
else if (kind === "id") {
|
|
790
|
+
if (typeof value !== "string") report("invalid_value_shape", path, "Expected an IRI identifier string.");
|
|
791
|
+
else checkReference(value, path, report);
|
|
792
|
+
} else if (kind === "mediaType") checkSingleValue(value, path, report, (entry, entryPath) => checkLinkProperty(property, entry, entryPath, report));
|
|
793
|
+
else return value === null;
|
|
794
|
+
return true;
|
|
795
|
+
}
|
|
796
|
+
function proposePropertyOmission(value, path, report, changes, identifier) {
|
|
797
|
+
const emptyArray = Array.isArray(value) && value.length === 0;
|
|
798
|
+
if (!emptyArray && !identifier) return false;
|
|
799
|
+
if (value !== null && !Array.isArray(value)) return false;
|
|
800
|
+
let safe = true;
|
|
801
|
+
let absent = true;
|
|
802
|
+
if (Array.isArray(value)) visitList(value, path, (...args) => {
|
|
803
|
+
safe = false;
|
|
804
|
+
report(...args);
|
|
805
|
+
}, (entry) => {
|
|
806
|
+
if (entry !== null) absent = false;
|
|
807
|
+
});
|
|
808
|
+
if (!safe) return true;
|
|
809
|
+
if (!absent) return false;
|
|
810
|
+
report(emptyArray ? "empty_property_list" : "absent_identifier", path, emptyArray ? "ActivityStreams represents empty property arrays by omission or null; the preview omits this property." : "This identifier is absent; the preview omits it so Fedify receives no non-string identifier.");
|
|
811
|
+
changes.push({
|
|
812
|
+
operation: "remove",
|
|
813
|
+
reason: emptyArray ? "omit_empty_property_list" : "omit_absent_identifier",
|
|
814
|
+
path,
|
|
815
|
+
before: value,
|
|
816
|
+
message: emptyArray ? "Omit an empty property array from the wire representation." : "Omit an absent identifier from the wire representation."
|
|
817
|
+
});
|
|
818
|
+
return true;
|
|
819
|
+
}
|
|
820
|
+
function checkLinkRelation(value, path, report) {
|
|
821
|
+
if (value === null) return;
|
|
822
|
+
if (typeof value !== "string" || value.length === 0 || Array.from(value).some((character) => character.charCodeAt(0) <= 32 || character.charCodeAt(0) === 127 || character === ",")) report("invalid_value_shape", path, "Expected a relation token or absolute IRI without whitespace or commas.");
|
|
823
|
+
else if (/^[a-z][a-z\d+.-]*:/iu.test(value)) checkReference(value, path, report);
|
|
824
|
+
else if (!/^[a-z][a-z\d.-]*$/iu.test(value)) report("invalid_value_shape", path, "Expected a relation token or absolute IRI.");
|
|
825
|
+
}
|
|
826
|
+
function checkLinkProperty(property, value, path, report) {
|
|
827
|
+
if (property === "rel") {
|
|
828
|
+
checkReferenceValues(value, path, report, checkLinkRelation);
|
|
829
|
+
return;
|
|
830
|
+
}
|
|
831
|
+
if (value === null) return;
|
|
832
|
+
if (typeof value !== "string") {
|
|
833
|
+
report("invalid_value_shape", path, "Expected a string.");
|
|
834
|
+
return;
|
|
835
|
+
}
|
|
836
|
+
if (property === "href") checkReference(value, path, report);
|
|
837
|
+
else if (property === "mediaType") try {
|
|
838
|
+
new MIMEType(value);
|
|
839
|
+
} catch (error) {
|
|
840
|
+
if (!(error instanceof TypeError)) throw error;
|
|
841
|
+
report("invalid_value_shape", path, "Expected a MIME media type.");
|
|
842
|
+
}
|
|
843
|
+
else checkLanguageTag(value, path, report);
|
|
844
|
+
}
|
|
845
|
+
function checkExtensionKeywords(values, path, report) {
|
|
846
|
+
if (Object.hasOwn(values, "@id") && typeof values["@id"] !== "string") report("invalid_value_shape", [...path, "@id"], "Expected a string JSON-LD identifier.");
|
|
847
|
+
let jsonLiteral = values["@type"] === "@json";
|
|
848
|
+
if (Object.hasOwn(values, "@type")) {
|
|
849
|
+
const type = values["@type"];
|
|
850
|
+
if (Array.isArray(type)) visitList(type, [...path, "@type"], report, (entry, entryPath) => {
|
|
851
|
+
if (typeof entry !== "string") report("invalid_value_shape", entryPath, "Expected a JSON-LD type string.");
|
|
852
|
+
if (type.length === 1 && entry === "@json") jsonLiteral = true;
|
|
853
|
+
});
|
|
854
|
+
else if (typeof type !== "string") report("invalid_value_shape", [...path, "@type"], "Expected a JSON-LD type string or array of strings.");
|
|
855
|
+
}
|
|
856
|
+
if (Object.hasOwn(values, "@value") && values["@language"] != null) {
|
|
857
|
+
if (typeof values["@language"] !== "string") report("invalid_value_shape", [...path, "@language"], "Expected a string language tag or null.");
|
|
858
|
+
if (values["@value"] !== null && typeof values["@value"] !== "string") report("invalid_value_shape", [...path, "@value"], "A JSON-LD language-tagged value must have a string @value or null absence.");
|
|
859
|
+
}
|
|
860
|
+
return jsonLiteral;
|
|
861
|
+
}
|
|
862
|
+
function getInapplicableProperties(Type) {
|
|
863
|
+
return isTypeOrSubtype(Type, Link) ? objectOnlyProperties : /* @__PURE__ */ new Set();
|
|
864
|
+
}
|
|
865
|
+
function checkPathDepth(path, report) {
|
|
866
|
+
if (path.length <= maximumPathDepth) return true;
|
|
867
|
+
report("processing_limit_exceeded", path, `Nesting exceeds the supported limit of ${maximumPathDepth} path segments.`);
|
|
868
|
+
return false;
|
|
869
|
+
}
|
|
870
|
+
function checkDocumentValue(rootDocument, rootPath, reporter, changes, onDocument, onUnavailableDocument = () => void 0) {
|
|
871
|
+
const { report, isFull } = reporter;
|
|
872
|
+
const pending = [];
|
|
873
|
+
const ancestors = /* @__PURE__ */ new WeakSet();
|
|
874
|
+
function checkExtension(value, path, keywords = true) {
|
|
875
|
+
if (!checkPathDepth(path, report)) return;
|
|
876
|
+
if (!isJsonValueShape(value)) {
|
|
877
|
+
report("non_json_value", path, "Expected a JSON value.");
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
if (value === null || typeof value !== "object") return;
|
|
881
|
+
if (ancestors.has(value)) {
|
|
882
|
+
report("non_json_value", path, "JSON documents cannot contain cycles.");
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
885
|
+
ancestors.add(value);
|
|
886
|
+
pending.push(() => ancestors.delete(value));
|
|
887
|
+
const children = [];
|
|
888
|
+
if (Array.isArray(value)) visitList(value, path, report, (entry, entryPath) => {
|
|
889
|
+
children.push(() => checkExtension(entry, entryPath, keywords));
|
|
890
|
+
});
|
|
891
|
+
else {
|
|
892
|
+
const values = readDocument(value, path, report);
|
|
893
|
+
if (values != null) {
|
|
894
|
+
const jsonLiteral = keywords && checkExtensionKeywords(values, path, report);
|
|
895
|
+
for (const key of Object.keys(values)) {
|
|
896
|
+
const childKeywords = keywords && !(jsonLiteral && key === "@value");
|
|
897
|
+
children.push(() => checkExtension(values[key], [...path, key], childKeywords));
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
for (const child of children.reverse()) pending.push(child);
|
|
902
|
+
}
|
|
903
|
+
function checkDocument(document, path, inherited, expectedTypes, fedifyTypes) {
|
|
904
|
+
if (!checkPathDepth(path, report)) {
|
|
905
|
+
onUnavailableDocument(path);
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
if (document !== null && typeof document === "object" && ancestors.has(document)) {
|
|
909
|
+
report("non_json_value", path, "JSON documents cannot contain cycles.");
|
|
910
|
+
onUnavailableDocument(path);
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
const values = readDocument(document, path, report);
|
|
914
|
+
if (values == null) {
|
|
915
|
+
onUnavailableDocument(path);
|
|
916
|
+
return;
|
|
917
|
+
}
|
|
918
|
+
const sourceDocument = document;
|
|
919
|
+
const context = checkContext(values, path, report, inherited);
|
|
920
|
+
if (context == null) {
|
|
921
|
+
onUnavailableDocument(path);
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
924
|
+
const fields = Object.keys(values).filter((source) => source !== "@context").sort().map((source) => resolveProperty(source, context));
|
|
925
|
+
const Type = checkDocumentType(values, fields, path, context, report);
|
|
926
|
+
if (Type == null) {
|
|
927
|
+
onUnavailableDocument(path);
|
|
928
|
+
return;
|
|
929
|
+
}
|
|
930
|
+
if (expectedTypes?.every((Expected) => !isTypeOrSubtype(Type, Expected))) {
|
|
931
|
+
report("invalid_value_shape", path, "The embedded type is outside this property's collection range.");
|
|
932
|
+
onUnavailableDocument(path);
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
checkEmbeddedFedifyRange(Type, fedifyTypes, path, report);
|
|
936
|
+
onDocument?.(values, path, context);
|
|
937
|
+
const isObject = isTypeOrSubtype(Type, Object$1);
|
|
938
|
+
const isLink = isTypeOrSubtype(Type, Link);
|
|
939
|
+
const isActivity = isTypeOrSubtype(Type, Activity);
|
|
940
|
+
const isIntransitive = isTypeOrSubtype(Type, IntransitiveActivity);
|
|
941
|
+
const isCollection = isTypeOrSubtype(Type, Collection);
|
|
942
|
+
const isOrderedPage = isTypeOrSubtype(Type, OrderedCollectionPage);
|
|
943
|
+
const linkProperties = isLink ? [
|
|
944
|
+
"href",
|
|
945
|
+
"mediaType",
|
|
946
|
+
"hreflang",
|
|
947
|
+
"rel"
|
|
948
|
+
] : [];
|
|
949
|
+
const integerProperties = isLink ? ["height", "width"] : isOrderedPage ? ["totalItems", "startIndex"] : isCollection ? ["totalItems"] : [];
|
|
950
|
+
const referenceProperties = ["preview"];
|
|
951
|
+
if (isObject) referenceProperties.push("attributedTo", "location", ...addressingProperties);
|
|
952
|
+
if (isActivity) referenceProperties.push("actor", "object", "target", "origin", "result", "instrument");
|
|
953
|
+
if (isCollection) referenceProperties.push("items", "orderedItems");
|
|
954
|
+
const omittableProperties = /* @__PURE__ */ new Set([
|
|
955
|
+
...integerProperties,
|
|
956
|
+
...linkProperties,
|
|
957
|
+
...referenceProperties
|
|
958
|
+
]);
|
|
959
|
+
const multipleAliases = findMultipleAliases(Type, fields, values);
|
|
960
|
+
const inapplicableProperties = getInapplicableProperties(Type);
|
|
961
|
+
ancestors.add(sourceDocument);
|
|
962
|
+
pending.push(() => {
|
|
963
|
+
ancestors.delete(sourceDocument);
|
|
964
|
+
});
|
|
965
|
+
for (const field of fields.filter(({ name }) => name !== "type").reverse()) {
|
|
966
|
+
const { source, name: key } = field;
|
|
967
|
+
const intransitiveObject = isIntransitive && key === "object";
|
|
968
|
+
pending.push(() => {
|
|
969
|
+
const value = values[source];
|
|
970
|
+
const fieldPath = [...path, source];
|
|
971
|
+
if (key == null) {
|
|
972
|
+
if (isOpaqueExtension(field)) {
|
|
973
|
+
report("extension_property_unchecked", fieldPath, "This custom property's data is preserved; its semantics were not checked.");
|
|
974
|
+
checkExtension(value, fieldPath);
|
|
975
|
+
return;
|
|
976
|
+
}
|
|
977
|
+
report("unsupported_property", fieldPath, "This property is outside the checker's Fedify vocabulary coverage.");
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
if (!field.compatible) {
|
|
981
|
+
report("unsupported_value_form", fieldPath, "This spelling's type coercion differs from the supported vocabulary term.");
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
if (inapplicableProperties.has(key)) {
|
|
985
|
+
if (value === null) return;
|
|
986
|
+
report("property_not_applicable", fieldPath, "This property applies to Object documents, not Link documents.");
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
if (multipleAliases.has(source)) report("invalid_multiplicity", fieldPath, "Multiple aliases supply values for one functional property.");
|
|
990
|
+
const collectionRange = getCollectionRange(Type, key);
|
|
991
|
+
const commonKind = getCommonPropertyKind(Type, key);
|
|
992
|
+
if ((omittableProperties.has(key) || collectionRange != null || commonKind != null) && proposePropertyOmission(value, fieldPath, report, changes, commonKind === "id")) return;
|
|
993
|
+
if (integerProperties.includes(key)) checkSingleValue(value, fieldPath, report, checkNonNegativeInteger);
|
|
994
|
+
else if (linkProperties.includes(key)) {
|
|
995
|
+
if (key === "rel") checkLinkProperty(key, value, fieldPath, report);
|
|
996
|
+
else checkSingleValue(value, fieldPath, report, (entry, entryPath) => checkLinkProperty(key, entry, entryPath, report));
|
|
997
|
+
} else if (checkCommonProperty(commonKind, key, value, fieldPath, report)) {} else if (intransitiveObject) checkReferenceValues(value, fieldPath, report, (entry, entryPath) => {
|
|
998
|
+
if (entry !== null) report("property_not_applicable", entryPath, "The object property does not apply to intransitive activities.");
|
|
999
|
+
});
|
|
1000
|
+
else if (referenceProperties.includes(key) || collectionRange != null) {
|
|
1001
|
+
const checks = [];
|
|
1002
|
+
(collectionRange == null ? checkReferenceValues : checkSingleReference)(value, fieldPath, report, (entry, entryPath) => {
|
|
1003
|
+
if (entry !== null && typeof entry === "object" && !Array.isArray(entry)) checkDocument(entry, entryPath, context, collectionRange, getEmbeddedFedifyRange(key, collectionRange));
|
|
1004
|
+
else checkReference(entry, entryPath, report);
|
|
1005
|
+
}, (check) => checks.push(check));
|
|
1006
|
+
for (const check of checks.reverse()) pending.push(check);
|
|
1007
|
+
} else report("unsupported_property", fieldPath, "This property is not checked by the initial implementation.");
|
|
1008
|
+
});
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
checkDocument(rootDocument, rootPath);
|
|
1012
|
+
while (pending.length > 0 && !isFull()) pending.pop()();
|
|
1013
|
+
}
|
|
1014
|
+
function createPreview(document, changes) {
|
|
1015
|
+
const root = {};
|
|
1016
|
+
const pending = [{
|
|
1017
|
+
source: document,
|
|
1018
|
+
target: root
|
|
1019
|
+
}];
|
|
1020
|
+
while (pending.length > 0) {
|
|
1021
|
+
const { source, target } = pending.pop();
|
|
1022
|
+
for (const [key, value] of Object.entries(source)) {
|
|
1023
|
+
let copy = value;
|
|
1024
|
+
if (value !== null && typeof value === "object") {
|
|
1025
|
+
copy = Array.isArray(value) ? [] : {};
|
|
1026
|
+
pending.push({
|
|
1027
|
+
source: value,
|
|
1028
|
+
target: copy
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
Object.defineProperty(target, key, {
|
|
1032
|
+
value: copy,
|
|
1033
|
+
writable: true,
|
|
1034
|
+
configurable: true,
|
|
1035
|
+
enumerable: true
|
|
1036
|
+
});
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
for (const change of changes) {
|
|
1040
|
+
let parent = root;
|
|
1041
|
+
for (const key of change.path.slice(0, -1)) parent = parent[key];
|
|
1042
|
+
const key = change.path.at(-1);
|
|
1043
|
+
if (change.operation === "remove") delete parent[key];
|
|
1044
|
+
else Object.defineProperty(parent, key, {
|
|
1045
|
+
value: change.after,
|
|
1046
|
+
writable: true,
|
|
1047
|
+
configurable: true,
|
|
1048
|
+
enumerable: true
|
|
1049
|
+
});
|
|
1050
|
+
}
|
|
1051
|
+
return root;
|
|
1052
|
+
}
|
|
1053
|
+
/**
|
|
1054
|
+
* Checks the implemented Activity Vocabulary types, including nested
|
|
1055
|
+
* activity objects, without network access or writes. Embedded objects inherit
|
|
1056
|
+
* the parent's context. Collections add member, count, and navigation checks.
|
|
1057
|
+
* Question uses shared intransitive rules; poll-specific
|
|
1058
|
+
* fields remain unsupported pending #155.
|
|
1059
|
+
*
|
|
1060
|
+
* Supports allowlisted bundled contexts and simple inline aliases/prefixes,
|
|
1061
|
+
* single types, id, string content,
|
|
1062
|
+
* attribution/location and activity actor, object, target, origin, result,
|
|
1063
|
+
* and instrument values. These can be IRI references or supported
|
|
1064
|
+
* embedded types, singly or in flat lists. Empty supported property arrays
|
|
1065
|
+
* receive omission previews. Intransitive activities
|
|
1066
|
+
* reject a non-null object property. Other forms produce
|
|
1067
|
+
* support diagnostics and `complete: false`. This is not a permission check,
|
|
1068
|
+
* a Fedify parsability check, or full ActivityStreams validation.
|
|
1069
|
+
* The input must be plain JSON data, not a Proxy.
|
|
1070
|
+
* Context interpretation is bounded and synchronous. Unknown
|
|
1071
|
+
* custom properties are preserved as opaque JSON with warnings. Complex
|
|
1072
|
+
* contexts remain unsupported; input is never expanded.
|
|
1073
|
+
* Null list entries mean absence. Functional properties accept at most one
|
|
1074
|
+
* non-null value, preserving array-index paths. Common text/language maps,
|
|
1075
|
+
* timestamps, actor preferredUsername, and addressing are checked.
|
|
1076
|
+
* @returns Findings at original-input paths, with an explicit omission preview
|
|
1077
|
+
* for empty supported property arrays and absent identifiers only when validation
|
|
1078
|
+
* completes without errors.
|
|
1079
|
+
*/
|
|
1080
|
+
function validateDocument(document) {
|
|
1081
|
+
const reporter = createReporter();
|
|
1082
|
+
const changes = [];
|
|
1083
|
+
checkDocumentValue(document, [], reporter, changes);
|
|
1084
|
+
const result = reporter.result();
|
|
1085
|
+
if (!result.complete || result.diagnostics.some(({ severity }) => severity === "error") || changes.length === 0) return result;
|
|
1086
|
+
const preview = createPreview(document, changes);
|
|
1087
|
+
const checked = validateDocument(preview);
|
|
1088
|
+
reportPreviewDiagnostics(result, checked, reporter.report);
|
|
1089
|
+
const previewResult = reporter.result();
|
|
1090
|
+
if (!checked.complete || !previewResult.complete || previewResult.diagnostics.some(({ severity }) => severity === "error")) return {
|
|
1091
|
+
...previewResult,
|
|
1092
|
+
complete: checked.complete && previewResult.complete
|
|
1093
|
+
};
|
|
1094
|
+
return {
|
|
1095
|
+
...previewResult,
|
|
1096
|
+
changes,
|
|
1097
|
+
normalizedDocument: preview
|
|
1098
|
+
};
|
|
1099
|
+
}
|
|
1100
|
+
function reportPreviewDiagnostics(original, checked, report) {
|
|
1101
|
+
for (const diagnostic of checked.diagnostics) if (!original.diagnostics.some((existing) => existing.reason === diagnostic.reason && JSON.stringify(existing.path) === JSON.stringify(diagnostic.path))) report(diagnostic.reason, diagnostic.path, diagnostic.message);
|
|
1102
|
+
}
|
|
1103
|
+
function checkPreparationOptions(options) {
|
|
1104
|
+
const validPath = (path) => Array.isArray(path) && path.every((part) => typeof part === "string" || typeof part === "number" && Number.isSafeInteger(part) && part >= 0);
|
|
1105
|
+
if (options == null || !Array.isArray(options.authority) || options.authority.length === 0 || options.authority.some((outcome) => outcome == null || typeof outcome.allowed !== "boolean" || !validPath(outcome.path)) || !Array.isArray(options.serverOwnedFields) || options.serverOwnedFields.some((field) => field == null || !validPath(field.path) || typeof field.path.at(-1) !== "string" || typeof field.value !== "string")) throw new TypeError("Expected explicit trusted authority outcomes and server-owned fields.");
|
|
1106
|
+
}
|
|
1107
|
+
function isMissingPreparationParent(document, path) {
|
|
1108
|
+
let value = document;
|
|
1109
|
+
for (const part of path) {
|
|
1110
|
+
if (value === null || typeof value !== "object") return true;
|
|
1111
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, part);
|
|
1112
|
+
if (descriptor == null) return true;
|
|
1113
|
+
if (!("value" in descriptor)) return false;
|
|
1114
|
+
({value} = descriptor);
|
|
1115
|
+
}
|
|
1116
|
+
return false;
|
|
1117
|
+
}
|
|
1118
|
+
function checkServerOwnedFields(document, fields, targets, unavailable, truncated) {
|
|
1119
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1120
|
+
const checked = [];
|
|
1121
|
+
for (const field of fields) {
|
|
1122
|
+
const parent = field.path.slice(0, -1);
|
|
1123
|
+
if (isMissingPreparationParent(document, parent)) throw new TypeError("Server-owned field parent path does not exist.");
|
|
1124
|
+
const target = targets.get(JSON.stringify(parent));
|
|
1125
|
+
if (target == null) {
|
|
1126
|
+
if (unavailable.some((path) => path.every((part, index) => parent[index] === part)) || truncated) {
|
|
1127
|
+
const source = field.path.at(-1);
|
|
1128
|
+
const { standardContext } = getContextTables();
|
|
1129
|
+
if (resolveProperty(source, standardContext).name != null) checkServerOwnedField(field, standardContext, seen);
|
|
1130
|
+
continue;
|
|
1131
|
+
}
|
|
1132
|
+
throw new TypeError("Server-owned field parents must be existing supported documents, not references or opaque data.");
|
|
1133
|
+
}
|
|
1134
|
+
const property = checkServerOwnedField(field, target.context, seen);
|
|
1135
|
+
checked.push({
|
|
1136
|
+
field,
|
|
1137
|
+
target,
|
|
1138
|
+
property
|
|
1139
|
+
});
|
|
1140
|
+
}
|
|
1141
|
+
return checked;
|
|
1142
|
+
}
|
|
1143
|
+
function checkServerOwnedField(field, context, seen) {
|
|
1144
|
+
const parentPath = field.path.slice(0, -1);
|
|
1145
|
+
const resolved = resolveProperty(field.path.at(-1), context);
|
|
1146
|
+
const property = resolved.name;
|
|
1147
|
+
if (!resolved.compatible || property == null || ![
|
|
1148
|
+
"id",
|
|
1149
|
+
"actor",
|
|
1150
|
+
"attributedTo",
|
|
1151
|
+
"published",
|
|
1152
|
+
"updated"
|
|
1153
|
+
].includes(property)) throw new TypeError("Server-owned fields must identify a supported identity or timestamp property.");
|
|
1154
|
+
const identity = JSON.stringify([...parentPath, property]);
|
|
1155
|
+
if (seen.has(identity)) throw new TypeError("Specify each server-owned semantic field only once, including aliases.");
|
|
1156
|
+
seen.add(identity);
|
|
1157
|
+
const invalidServerValue = () => {
|
|
1158
|
+
throw new TypeError(`Invalid server-owned value for ${property}.`);
|
|
1159
|
+
};
|
|
1160
|
+
if (property === "published" || property === "updated") checkDateTime(field.value, field.path, invalidServerValue);
|
|
1161
|
+
else checkReference(field.value, field.path, invalidServerValue);
|
|
1162
|
+
return property;
|
|
1163
|
+
}
|
|
1164
|
+
function proposeServerField(field, target, property, report, changes) {
|
|
1165
|
+
const parentPath = field.path.slice(0, -1);
|
|
1166
|
+
const source = field.path.at(-1);
|
|
1167
|
+
const aliases = Object.keys(target.values).filter((key) => resolveProperty(key, target.context).name === property);
|
|
1168
|
+
const supplied = aliases.flatMap((key) => {
|
|
1169
|
+
const value = target.values[key];
|
|
1170
|
+
const path = [...parentPath, key];
|
|
1171
|
+
if (Array.isArray(value)) return value.flatMap((entry, index) => entry === null ? [] : [{
|
|
1172
|
+
entry,
|
|
1173
|
+
path: [...path, index]
|
|
1174
|
+
}]);
|
|
1175
|
+
return value === null ? [] : [{
|
|
1176
|
+
entry: value,
|
|
1177
|
+
path
|
|
1178
|
+
}];
|
|
1179
|
+
});
|
|
1180
|
+
if (supplied.length > 0) {
|
|
1181
|
+
if (supplied.length !== 1 || supplied[0].entry !== field.value) for (const suppliedValue of supplied) report("server_owned_field_conflict", suppliedValue.path, "This field must match the server-owned value for this creation operation.");
|
|
1182
|
+
return;
|
|
1183
|
+
}
|
|
1184
|
+
const key = Object.hasOwn(target.values, source) ? source : aliases[0] ?? source;
|
|
1185
|
+
const path = [...parentPath, key];
|
|
1186
|
+
const omission = changes.findIndex((change) => change.operation === "remove" && (change.reason === "omit_empty_property_list" || change.reason === "omit_absent_identifier") && JSON.stringify(change.path) === JSON.stringify(path));
|
|
1187
|
+
if (omission >= 0) changes.splice(omission, 1);
|
|
1188
|
+
const common = {
|
|
1189
|
+
reason: "assign_server_owned_field",
|
|
1190
|
+
path,
|
|
1191
|
+
after: field.value,
|
|
1192
|
+
message: `Use the server-proposed ${property} for this creation operation.`
|
|
1193
|
+
};
|
|
1194
|
+
changes.push(Object.hasOwn(target.values, key) ? {
|
|
1195
|
+
...common,
|
|
1196
|
+
operation: "replace",
|
|
1197
|
+
before: target.values[key]
|
|
1198
|
+
} : {
|
|
1199
|
+
...common,
|
|
1200
|
+
operation: "add"
|
|
1201
|
+
});
|
|
1202
|
+
}
|
|
1203
|
+
/**
|
|
1204
|
+
* Prepares a creation preview against trusted server values and permission
|
|
1205
|
+
* outcomes. Does not determine authorization, allocate values, or persist.
|
|
1206
|
+
*
|
|
1207
|
+
* Integration boundary: https://github.com/fedify-dev/drfed/issues/152
|
|
1208
|
+
* The write operation constructs options from trusted server state, not client
|
|
1209
|
+
* JSON. It must check every local write, including nested creations, and recheck
|
|
1210
|
+
* permissions at commit. This preview is not an authorization token. The caller
|
|
1211
|
+
* also owns keeping committed IDs/timestamps consistent with the shown preview.
|
|
1212
|
+
* Merely referencing or embedding an actor/resource does not request a write.
|
|
1213
|
+
*
|
|
1214
|
+
* Paths identify original source locations; owned-field aliases are resolved
|
|
1215
|
+
* within the supported context profile. Matching singleton values are retained;
|
|
1216
|
+
* absent/null values receive explicit additions/replacements. Conflicts block
|
|
1217
|
+
* the preview. Throws TypeError for invalid trusted options/target paths.
|
|
1218
|
+
* @returns Original-input diagnostics and any explicit creation preview changes.
|
|
1219
|
+
*/
|
|
1220
|
+
function prepareDocument(document, options) {
|
|
1221
|
+
checkPreparationOptions(options);
|
|
1222
|
+
const reporter = createReporter();
|
|
1223
|
+
const changes = [];
|
|
1224
|
+
const requested = new Set(options.serverOwnedFields.map(({ path }) => JSON.stringify(path.slice(0, -1))));
|
|
1225
|
+
const targets = /* @__PURE__ */ new Map();
|
|
1226
|
+
const unavailable = [];
|
|
1227
|
+
checkDocumentValue(document, [], reporter, changes, (values, path, context) => {
|
|
1228
|
+
const key = JSON.stringify(path);
|
|
1229
|
+
if (requested.has(key)) targets.set(key, {
|
|
1230
|
+
values,
|
|
1231
|
+
context
|
|
1232
|
+
});
|
|
1233
|
+
}, (path) => unavailable.push(path));
|
|
1234
|
+
const structural = reporter.result();
|
|
1235
|
+
const checkedFields = checkServerOwnedFields(document, options.serverOwnedFields, targets, unavailable, structural.diagnostics.some(({ reason }) => reason === "diagnostic_limit_reached"));
|
|
1236
|
+
for (const outcome of options.authority) if (!outcome.allowed) reporter.report("unauthorized_local_identity", [...outcome.path], "The server denied use of this local identity for the requested operation.");
|
|
1237
|
+
let result = reporter.result();
|
|
1238
|
+
if (!result.complete || result.diagnostics.some(({ severity }) => severity === "error")) return result;
|
|
1239
|
+
for (const { field, target, property } of checkedFields) proposeServerField(field, target, property, reporter.report, changes);
|
|
1240
|
+
result = reporter.result();
|
|
1241
|
+
if (!result.complete || result.diagnostics.some(({ severity }) => severity === "error") || changes.length === 0) return result;
|
|
1242
|
+
const replacedPaths = new Set(changes.filter((change) => change.operation === "replace").map(({ path }) => JSON.stringify(path)));
|
|
1243
|
+
const preview = createPreview(document, changes);
|
|
1244
|
+
const checked = validateDocument(preview);
|
|
1245
|
+
reportPreviewDiagnostics(result, checked, reporter.report);
|
|
1246
|
+
result = reporter.result();
|
|
1247
|
+
if (!checked.complete || !result.complete || result.diagnostics.some(({ severity }) => severity === "error")) return {
|
|
1248
|
+
...result,
|
|
1249
|
+
complete: checked.complete && result.complete
|
|
1250
|
+
};
|
|
1251
|
+
return {
|
|
1252
|
+
...result,
|
|
1253
|
+
diagnostics: result.diagnostics.map((diagnostic) => (diagnostic.reason === "empty_property_list" || diagnostic.reason === "absent_identifier") && replacedPaths.has(JSON.stringify(diagnostic.path)) ? {
|
|
1254
|
+
...diagnostic,
|
|
1255
|
+
message: "This absent value is replaced by the trusted server-owned value in the preview."
|
|
1256
|
+
} : diagnostic),
|
|
1257
|
+
changes,
|
|
1258
|
+
normalizedDocument: preview
|
|
1259
|
+
};
|
|
1260
|
+
}
|
|
1261
|
+
//#endregion
|
|
1262
|
+
export { diagnosticReasons, prepareDocument, validateDocument };
|
|
1263
|
+
|
|
1264
|
+
//# sourceMappingURL=document.mjs.map
|