@fedify/vocab-tools 2.5.0-dev.2687 → 2.5.0-dev.2726

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/src/codec.ts DELETED
@@ -1,695 +0,0 @@
1
- import metadata from "../deno.json" with { type: "json" };
2
- import { generateField, getFieldName } from "./field.ts";
3
- import type { PropertyPreprocessorSchema, TypeSchema } from "./schema.ts";
4
- import { isNonFunctionalProperty } from "./schema.ts";
5
- import {
6
- areAllScalarTypes,
7
- emitOverride,
8
- getAllProperties,
9
- getDataCheck,
10
- getDecoder,
11
- getDecoders,
12
- getEncoders,
13
- getSubtypes,
14
- getTypeNames,
15
- isCompactableType,
16
- skipsUnparsable,
17
- } from "./type.ts";
18
-
19
- function* generatePreprocessorBlock(
20
- property: { preprocessors?: PropertyPreprocessorSchema[] },
21
- rangeTypeName: string,
22
- variable: string,
23
- baseUrlExpr: string,
24
- moduleVarNames: ReadonlyMap<string, string>,
25
- ): Iterable<string> {
26
- if (property.preprocessors == null || property.preprocessors.length === 0) {
27
- return;
28
- }
29
- yield `
30
- {
31
- let _handled: ${rangeTypeName} | undefined;
32
- `;
33
- for (const pp of property.preprocessors) {
34
- const varName = moduleVarNames.get(pp.module);
35
- if (varName == null) {
36
- throw new Error(
37
- `Preprocessor module "${pp.module}" is not registered ` +
38
- `in the generated imports. Ensure all preprocessor ` +
39
- `modules used in property schemas are available.`,
40
- );
41
- }
42
- yield `
43
- if (_handled === undefined) {
44
- const _result = await ${varName}[${JSON.stringify(pp.function)}](v, {
45
- documentLoader: options.documentLoader,
46
- contextLoader: options.contextLoader,
47
- tracerProvider: options.tracerProvider,
48
- verifyPortableObject: options.verifyPortableObject,
49
- baseUrl: ${baseUrlExpr},
50
- });
51
- if (_result instanceof Error) throw _result;
52
- if (_result !== undefined) {
53
- _handled = _result as ${rangeTypeName};
54
- }
55
- }
56
- `;
57
- }
58
- yield `
59
- if (_handled !== undefined) {
60
- ${variable}.push(_handled);
61
- continue;
62
- }
63
- }
64
- `;
65
- }
66
-
67
- export async function* generateEncoder(
68
- typeUri: string,
69
- types: Record<string, TypeSchema>,
70
- ): AsyncIterable<string> {
71
- const type = types[typeUri];
72
- yield `
73
- /**
74
- * Converts this object to a JSON-LD structure.
75
- * @param options The options to use.
76
- * - \`format\`: The format of the output: \`compact\` or
77
- \`expand\`.
78
- * - \`contextLoader\`: The loader for remote JSON-LD contexts.
79
- * - \`context\`: The JSON-LD context to use. Not applicable
80
- when \`format\` is set to \`'expand'\`.
81
- * @returns The JSON-LD representation of this object.
82
- */
83
- ${emitOverride(typeUri, types)} async toJsonLd(options: {
84
- format?: "compact" | "expand",
85
- contextLoader?: DocumentLoader,
86
- context?: string | Record<string, string> | (string | Record<string, string>)[],
87
- } = {}): Promise<unknown> {
88
- if (options.format == null && this._cachedJsonLd != null) {
89
- return this._cachedJsonLd;
90
- }
91
- if (options.format !== "compact" && options.context != null) {
92
- throw new TypeError(
93
- "The context option can only be used when the format option is set " +
94
- "to 'compact'."
95
- );
96
- }
97
- options = {
98
- ...options,
99
- contextLoader: options.contextLoader ?? getDocumentLoader(),
100
- };
101
- // Joins the enclosing frame's signed-value scope, or starts one when this
102
- // frame is the outermost. Only the owning frame puts retained signed
103
- // child documents back in place of their placeholders.
104
- const signedValues = enterSignedValueScope(options);
105
- `;
106
- if (isCompactableType(typeUri, types)) {
107
- yield `
108
- if (options.format == null && this.isCompactable()) {
109
- `;
110
- if (type.extends == null) {
111
- yield "const result: Record<string, unknown> = {};";
112
- } else {
113
- yield `
114
- const result = await super.toJsonLd({
115
- ...options,
116
- format: undefined,
117
- context: undefined,
118
- }) as Record<string, unknown>;
119
- `;
120
- const selfProperties = type.properties.map((p) => p.uri);
121
- for (const property of getAllProperties(typeUri, types, true)) {
122
- if (!selfProperties.includes(property.uri)) continue;
123
- yield `delete result[${JSON.stringify(property.compactName)}];`;
124
- }
125
- }
126
- yield `
127
- // deno-lint-ignore no-unused-vars
128
- let compactItems: unknown[];
129
- `;
130
- for (const property of type.properties) {
131
- yield `
132
- compactItems = [];
133
- for (const v of this.${await getFieldName(property.uri)}) {
134
- const item = (
135
- `;
136
- if (!areAllScalarTypes(property.range, types)) {
137
- yield "retainedSignedValueRef(v, signedValues.scope) ?? (";
138
- yield "v instanceof URL ? formatIri(v) : ";
139
- }
140
- const encoders = getEncoders(property.range, types, "v", "options", true);
141
- for (const code of encoders) yield code;
142
- if (!areAllScalarTypes(property.range, types)) yield ")";
143
- yield `
144
- );
145
- compactItems.push(item);
146
- }
147
- if (compactItems.length > 0) {
148
- `;
149
- if (
150
- property.functional ||
151
- (isNonFunctionalProperty(property) && property.container !== "list")
152
- ) {
153
- yield `
154
- result[${JSON.stringify(property.compactName)}]
155
- = compactItems.length > 1
156
- ? compactItems
157
- : compactItems[0];
158
- `;
159
- if (
160
- property.redundantPropertiesWrite !== "canonical" &&
161
- property.redundantProperties != null &&
162
- !(isNonFunctionalProperty(property) && property.container != null)
163
- ) {
164
- for (const prop of property.redundantProperties) {
165
- if (prop.compactName == null) continue;
166
- yield `
167
- result[${JSON.stringify(prop.compactName)}]
168
- = compactItems.length > 1
169
- ? compactItems
170
- : compactItems[0];
171
- `;
172
- }
173
- }
174
- } else {
175
- yield `
176
- result[${JSON.stringify(property.compactName)}] = compactItems;
177
- `;
178
- }
179
- yield `
180
- }
181
- `;
182
- }
183
- yield `
184
- ${
185
- type.typeless
186
- ? ""
187
- : `result["type"] = ${JSON.stringify(type.compactName ?? type.uri)};`
188
- }
189
- if (this.id != null) result["id"] = formatIri(this.id);
190
- result["@context"] = ${JSON.stringify(type.defaultContext)};
191
- return signedValues.owner
192
- ? resolveSignedValues(result, signedValues.scope)
193
- : result;
194
- }
195
- `;
196
- }
197
- yield `
198
- // deno-lint-ignore no-unused-vars prefer-const
199
- let array: unknown[];
200
- `;
201
- if (type.extends == null) {
202
- yield "const values: Record<string, unknown[] | string> = {};";
203
- } else {
204
- yield `
205
- const baseValues = await super.toJsonLd({
206
- ...options,
207
- format: "expand",
208
- context: undefined,
209
- }) as unknown[];
210
- const values = baseValues[0] as Record<
211
- string,
212
- unknown[] | { "@list": unknown[] } | string
213
- >;
214
- `;
215
- }
216
- for (const property of type.properties) {
217
- yield `
218
- array = [];
219
- for (const v of this.${await getFieldName(property.uri)}) {
220
- let element = (
221
- `;
222
- if (!areAllScalarTypes(property.range, types)) {
223
- yield "retainedSignedValueRef(v, signedValues.scope) ?? (";
224
- yield 'v instanceof URL ? { "@id": formatIri(v) } : ';
225
- }
226
- for (const code of getEncoders(property.range, types, "v", "options")) {
227
- yield code;
228
- }
229
- if (!areAllScalarTypes(property.range, types)) yield ")";
230
- yield `
231
- );
232
- if (Array.isArray(element)) {
233
- if (element.length < 1) continue;
234
- if (element.length == 1) element = element[0];
235
- }
236
- if (typeof element === "undefined") continue;
237
- `;
238
- if (isNonFunctionalProperty(property) && property.container === "graph") {
239
- yield `array.push({ "@graph": element });`;
240
- } else {
241
- yield `array.push(element);`;
242
- }
243
- yield `;
244
- }
245
- if (array.length > 0) {
246
- const propValue = (
247
- `;
248
- if (isNonFunctionalProperty(property) && property.container === "list") {
249
- yield `{ "@list": array }`;
250
- } else {
251
- yield `array`;
252
- }
253
- yield `
254
- );
255
- values[${JSON.stringify(property.uri)}] = propValue;
256
- `;
257
- if (
258
- property.redundantPropertiesWrite !== "canonical" &&
259
- property.redundantProperties != null
260
- ) {
261
- for (const prop of property.redundantProperties) {
262
- yield `
263
- values[${JSON.stringify(prop.uri)}] = propValue;
264
- `;
265
- }
266
- }
267
- yield `
268
- }
269
- `;
270
- }
271
- yield `
272
- ${type.typeless ? "" : `values["@type"] = [${JSON.stringify(type.uri)}];`}
273
- if (this.id != null) values["@id"] = formatIri(this.id);
274
- if (options.format === "expand") {
275
- return await jsonld.expand(
276
- values,
277
- {
278
- documentLoader: options.contextLoader,
279
- keepFreeFloatingNodes: true,
280
- },
281
- );
282
- }
283
- let docContext: Parameters<typeof jsonld.compact>[1] = options.context ??
284
- ${JSON.stringify(type.defaultContext)};
285
- `;
286
- const hasExtraContexts = Object.values(types).some((t) =>
287
- t.properties.some((p) => p.extraContext != null),
288
- );
289
- if (hasExtraContexts) {
290
- yield `
291
- if (options.context == null) {
292
- const currentContexts = Array.isArray(docContext) ? docContext : [docContext];
293
- const additionalContexts = getExtraContexts(values).filter(
294
- context => !currentContexts.includes(context)
295
- );
296
- if (additionalContexts.length > 0) {
297
- docContext = [...currentContexts, ...additionalContexts];
298
- }
299
- }
300
- `;
301
- }
302
- yield `
303
- let compacted = await jsonld.compact(
304
- values,
305
- docContext,
306
- { documentLoader: options.contextLoader },
307
- );
308
- `;
309
- if (hasExtraContexts) {
310
- // A cached child may map a property with its own inline context, which
311
- // the scan above cannot recognize; compaction then leaves its full IRI:
312
- yield `
313
- if (options.context == null) {
314
- const currentContexts = Array.isArray(docContext) ? docContext : [docContext];
315
- const additionalContexts = getExtraContexts(compacted).filter(
316
- context => !currentContexts.includes(context)
317
- );
318
- if (additionalContexts.length > 0) {
319
- docContext = [...currentContexts, ...additionalContexts];
320
- compacted = await jsonld.compact(values, docContext, {
321
- documentLoader: options.contextLoader,
322
- });
323
- }
324
- }
325
- `;
326
- }
327
- yield `
328
- if (docContext != null) {
329
- // Embed context
330
- `;
331
- const supertypes: string[] = [];
332
- for (
333
- let uri: string | undefined = typeUri;
334
- uri != null;
335
- uri = types[uri].extends
336
- ) {
337
- supertypes.push(uri);
338
- }
339
- for (const supertype of supertypes) {
340
- for (const property of types[supertype].properties) {
341
- if (property.embedContext == null) continue;
342
- const compactName = property.embedContext.compactName;
343
- yield `
344
- if (${JSON.stringify(compactName)} in compacted &&
345
- compacted.${compactName} != null) {
346
- if (Array.isArray(compacted.${compactName})) {
347
- for (const element of compacted.${compactName}) {
348
- element["@context"] = docContext;
349
- }
350
- } else {
351
- compacted.${compactName}["@context"] = docContext;
352
- }
353
- }
354
- `;
355
- }
356
- }
357
- yield `
358
- }
359
- return signedValues.owner
360
- ? resolveSignedValues(compacted, signedValues.scope)
361
- : compacted;
362
- }
363
-
364
- protected ${emitOverride(typeUri, types)} isCompactable(): boolean {
365
- `;
366
- for (const property of type.properties) {
367
- if (
368
- property.extraContext != null ||
369
- (property.redundantPropertiesWrite !== "canonical" &&
370
- property.redundantProperties != null &&
371
- property.redundantProperties.length > 0 &&
372
- (property.redundantProperties.some((p) => p.compactName == null) ||
373
- (isNonFunctionalProperty(property) && property.container != null))) ||
374
- !property.range.every((r) => isCompactableType(r, types))
375
- ) {
376
- yield `
377
- if (
378
- this.${await getFieldName(property.uri)} != null &&
379
- this.${await getFieldName(property.uri)}.length > 0
380
- ) return false;
381
- `;
382
- }
383
- }
384
- yield `
385
- return ${type.extends == null ? "true" : "super.isCompactable()"};
386
- }
387
- `;
388
- }
389
-
390
- export async function* generateDecoder(
391
- typeUri: string,
392
- types: Record<string, TypeSchema>,
393
- moduleVarNames: ReadonlyMap<string, string>,
394
- ): AsyncIterable<string> {
395
- const type = types[typeUri];
396
- yield `
397
- /**
398
- * Converts a JSON-LD structure to an object of this type.
399
- * @param json The JSON-LD structure to convert.
400
- * @param options The options to use.
401
- * - \`documentLoader\`: The loader for remote JSON-LD documents.
402
- * - \`contextLoader\`: The loader for remote JSON-LD contexts.
403
- * - \`tracerProvider\`: The OpenTelemetry tracer provider to use.
404
- * If omitted, the global tracer provider is used.
405
- * - \`verifyPortableObject\`: The default FEP-ef61 portable
406
- * object verifier for the property accessors of the
407
- * returned object and the objects obtained from it. It
408
- * does not verify the given \`json\` itself.
409
- * @returns The object of this type.
410
- * @throws {TypeError} If the given \`json\` is invalid.
411
- */
412
- static ${emitOverride(typeUri, types)} async fromJsonLd(
413
- json: unknown,
414
- options: {
415
- documentLoader?: DocumentLoader,
416
- contextLoader?: DocumentLoader,
417
- tracerProvider?: TracerProvider,
418
- verifyPortableObject?: PortableObjectVerifier,
419
- baseUrl?: URL,
420
- } = {},
421
- ): Promise<${type.name}> {
422
- const tracerProvider = options.tracerProvider ?? trace.getTracerProvider();
423
- const tracer = tracerProvider.getTracer(
424
- ${JSON.stringify(metadata.name)},
425
- ${JSON.stringify(metadata.version)},
426
- );
427
- return await tracer.startActiveSpan(
428
- "activitypub.parse_object",
429
- async (span) => {
430
- try {
431
- const object = await this.__fromJsonLd__${type.name}__(
432
- json, span, options);
433
- if (object.id != null) {
434
- span.setAttribute("activitypub.object.id", object.id.href);
435
- }
436
- return object;
437
- } catch (error) {
438
- span.setStatus({
439
- code: SpanStatusCode.ERROR,
440
- message: String(error),
441
- });
442
- throw error;
443
- } finally {
444
- span.end();
445
- }
446
- },
447
- );
448
- }
449
-
450
- protected static async __fromJsonLd__${type.name}__(
451
- json: unknown,
452
- span: Span,
453
- options: {
454
- documentLoader?: DocumentLoader,
455
- contextLoader?: DocumentLoader,
456
- tracerProvider?: TracerProvider,
457
- verifyPortableObject?: PortableObjectVerifier,
458
- baseUrl?: URL,
459
- } = {},
460
- ): Promise<${type.name}> {
461
- if (typeof json === "undefined") {
462
- throw new TypeError("Invalid JSON-LD: undefined.");
463
- }
464
- else if (json === null) throw new TypeError("Invalid JSON-LD: null.");
465
- options = {
466
- ...options,
467
- documentLoader: options.documentLoader ?? getDocumentLoader(),
468
- contextLoader: options.contextLoader ?? getDocumentLoader(),
469
- tracerProvider: options.tracerProvider ?? trace.getTracerProvider(),
470
- };
471
- // deno-lint-ignore no-explicit-any
472
- let values: Record<string, any[]> & { "@id"?: string };
473
- let expanded: unknown[];
474
- if (
475
- "_fromSubclass" in options &&
476
- options._fromSubclass &&
477
- typeof json === "object" &&
478
- !Array.isArray(json)
479
- ) {
480
- values =
481
- // deno-lint-ignore no-explicit-any
482
- json as (Record<string, any[]> & { "@id"?: string });
483
- expanded = [values];
484
- } else if (globalThis.Object.keys(json).length == 0) {
485
- values = {};
486
- expanded = [values];
487
- } else {
488
- expanded = await jsonld.expand(json, {
489
- documentLoader: options.contextLoader,
490
- keepFreeFloatingNodes: true,
491
- });
492
- values =
493
- // deno-lint-ignore no-explicit-any
494
- (expanded[0] ?? {}) as (Record<string, any[]> & { "@id"?: string });
495
- }
496
- const id = parseJsonLdId(values["@id"], options.baseUrl);
497
- if (id != null && options.baseUrl == null) {
498
- options = { ...options, baseUrl: id };
499
- }
500
- `;
501
- const subtypes = getSubtypes(typeUri, types, true);
502
- yield `
503
- if ("@type" in values) {
504
- span.setAttribute("activitypub.object.type", values["@type"]);
505
- }
506
- if ("@type" in values &&
507
- !values["@type"].every(t => t.startsWith("_:"))) {
508
- `;
509
- for (const subtypeUri of subtypes) {
510
- yield `
511
- if (values["@type"].includes(${JSON.stringify(subtypeUri)})) {
512
- return await ${types[subtypeUri].name}.fromJsonLd(json, options);
513
- }
514
- `;
515
- }
516
- yield `
517
- if (!values["@type"].includes(${JSON.stringify(typeUri)})) {
518
- throw new TypeError("Invalid type: " + values["@type"]);
519
- }
520
- }
521
- `;
522
- yield `
523
- let cacheJsonLd = !("_fromSubclass" in options) || !options._fromSubclass
524
- ? normalizeJsonLdIris(
525
- expanded,
526
- PORTABLE_IRI_KEYS,
527
- PORTABLE_IRI_PATTERN,
528
- )
529
- : undefined;
530
- if (cacheJsonLd != null && cacheJsonLd !== expanded) {
531
- cacheJsonLd = structuredClone(cacheJsonLd);
532
- }
533
- `;
534
- if (type.extends == null) {
535
- yield `
536
- const instance = new this(
537
- {
538
- id
539
- },
540
- options,
541
- );
542
- `;
543
- } else {
544
- yield `
545
- const superValues = structuredClone(values);
546
- delete superValues["@type"];
547
- const instance = await super.fromJsonLd(superValues, {
548
- ...options,
549
- // @ts-ignore: an internal option
550
- _fromSubclass: true,
551
- });
552
- if (!(instance instanceof ${type.name})) {
553
- throw new TypeError("Unexpected type: " + instance.constructor.name);
554
- }
555
- `;
556
- }
557
- yield `
558
- let shouldCacheJsonLd = instance._shouldCacheJsonLd;
559
- `;
560
- for (const property of type.properties) {
561
- const variable = await getFieldName(property.uri, "");
562
- yield await generateField(property, types, "const ");
563
- const arrayVariable = `${variable}__array`;
564
- const propertyValues =
565
- property.uri === "https://w3id.org/fep/ef61/gateways"
566
- ? "getPortableActorGateways(values) as typeof values[string]"
567
- : `values[${JSON.stringify(property.uri)}]`;
568
- yield `
569
- let ${arrayVariable} = ${propertyValues};
570
- `;
571
- if (property.redundantProperties != null) {
572
- for (const prop of property.redundantProperties) {
573
- yield `
574
- if (${arrayVariable} == null || ${arrayVariable}.length < 1) {
575
- ${arrayVariable} = values[${JSON.stringify(prop.uri)}];
576
- }
577
- `;
578
- }
579
- }
580
- let itemsExpression = `${arrayVariable}.length === 1 && "@list" in ${arrayVariable}[0]
581
- ? ${arrayVariable}[0]["@list"]
582
- : ${arrayVariable}`;
583
- if (isNonFunctionalProperty(property) && property.container === "graph") {
584
- itemsExpression = `(${itemsExpression}).flatMap((item: unknown) =>
585
- item != null && typeof item === "object" && "@graph" in item &&
586
- Array.isArray((item as { "@graph": unknown })["@graph"])
587
- ? (item as { "@graph": unknown[] })["@graph"]
588
- : [item]
589
- )`;
590
- }
591
- yield `
592
- for (
593
- const v of ${arrayVariable} == null
594
- ? []
595
- : ${itemsExpression}
596
- ) {
597
- if (v == null) continue;
598
- `;
599
- const propertyBaseUrl = "options.baseUrl";
600
- yield* generatePreprocessorBlock(
601
- property,
602
- getTypeNames(property.range, types),
603
- variable,
604
- propertyBaseUrl,
605
- moduleVarNames,
606
- );
607
- if (!areAllScalarTypes(property.range, types)) {
608
- yield `
609
- if (typeof v === "object" && "@id" in v && !("@type" in v)
610
- && globalThis.Object.keys(v).length === 1) {
611
- if (v["@id"].startsWith("_:")) continue;
612
- ${variable}.push(parseIri(v["@id"], ${propertyBaseUrl}));
613
- continue;
614
- }
615
- `;
616
- }
617
- yield `
618
- const decoded =
619
- `;
620
- const lenient =
621
- property.range.length == 1 && skipsUnparsable(property.range[0]);
622
- if (property.range.length == 1) {
623
- if (lenient) {
624
- yield getDataCheck(property.range[0], types, "v");
625
- yield " ? ";
626
- }
627
- yield getDecoder(
628
- property.range[0],
629
- types,
630
- "v",
631
- "options",
632
- propertyBaseUrl,
633
- );
634
- if (lenient) yield " : undefined";
635
- } else {
636
- const decoders = getDecoders(
637
- property.range,
638
- types,
639
- "v",
640
- "options",
641
- propertyBaseUrl,
642
- );
643
- for (const code of decoders) yield code;
644
- }
645
- yield `
646
- ;
647
- `;
648
- yield `
649
- if (typeof decoded === "undefined") {
650
- shouldCacheJsonLd = false;
651
- continue;
652
- }
653
- `;
654
- yield `
655
- if (!this._shouldCacheDecodedJsonLd(decoded)) {
656
- shouldCacheJsonLd = false;
657
- }
658
- ${variable}.push(decoded);
659
- `;
660
- yield `
661
- }
662
- instance.${await getFieldName(property.uri)} = ${variable};
663
- `;
664
- }
665
- yield `
666
- instance._shouldCacheJsonLd = shouldCacheJsonLd;
667
- if (
668
- shouldCacheJsonLd &&
669
- (!("_fromSubclass" in options) || !options._fromSubclass)
670
- ) {
671
- try {
672
- if (cacheJsonLd != null && cacheJsonLd !== expanded) {
673
- const compactArray = Array.isArray(json) && json.length === 1;
674
- const jsonLd = compactArray ? json[0] : json;
675
- const normalized = cacheJsonLd;
676
- const cachedJsonLd = await compactJsonLdCache(
677
- normalized,
678
- jsonLd,
679
- options.contextLoader,
680
- );
681
- instance._cachedJsonLd = compactArray ? [cachedJsonLd] : cachedJsonLd;
682
- } else {
683
- instance._cachedJsonLd = structuredClone(json);
684
- }
685
- } catch {
686
- getLogger(["fedify", "vocab"]).warn(
687
- "Failed to cache JSON-LD: {json}",
688
- { json },
689
- );
690
- }
691
- }
692
- return instance;
693
- }
694
- `;
695
- }