@fedify/vocab-tools 2.0.8 → 2.0.9
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/deno.json +1 -1
- package/dist/mod.cjs +38 -121
- package/dist/mod.d.cts +1 -2
- package/dist/mod.d.ts +1 -2
- package/dist/mod.js +30 -91
- package/package.json +3 -3
- package/tsdown.config.ts +1 -1
package/deno.json
CHANGED
package/dist/mod.cjs
CHANGED
|
@@ -1,70 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
-
key = keys[i];
|
|
11
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
-
get: ((k) => from[k]).bind(null, key),
|
|
13
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
-
value: mod,
|
|
20
|
-
enumerable: true
|
|
21
|
-
}) : target, mod));
|
|
22
|
-
|
|
23
|
-
//#endregion
|
|
24
|
-
const node_fs_promises = __toESM(require("node:fs/promises"));
|
|
25
|
-
const byte_encodings_base58 = __toESM(require("byte-encodings/base58"));
|
|
26
|
-
const __cfworker_json_schema = __toESM(require("@cfworker/json-schema"));
|
|
27
|
-
const node_path = __toESM(require("node:path"));
|
|
28
|
-
const node_url = __toESM(require("node:url"));
|
|
29
|
-
const yaml = __toESM(require("yaml"));
|
|
30
|
-
const es_toolkit = __toESM(require("es-toolkit"));
|
|
31
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let node_fs_promises = require("node:fs/promises");
|
|
3
|
+
let byte_encodings_base58 = require("byte-encodings/base58");
|
|
4
|
+
let _cfworker_json_schema = require("@cfworker/json-schema");
|
|
5
|
+
let node_path = require("node:path");
|
|
6
|
+
let node_url = require("node:url");
|
|
7
|
+
let yaml = require("yaml");
|
|
8
|
+
let es_toolkit = require("es-toolkit");
|
|
32
9
|
//#region deno.json
|
|
33
10
|
var name = "@fedify/vocab-tools";
|
|
34
|
-
var version = "2.0.
|
|
35
|
-
var license = "MIT";
|
|
36
|
-
var exports$1 = { ".": "./src/mod.ts" };
|
|
37
|
-
var author = {
|
|
38
|
-
"name": "Hong Minhee",
|
|
39
|
-
"email": "hong@minhee.org",
|
|
40
|
-
"url": "https://hongminhee.org/"
|
|
41
|
-
};
|
|
42
|
-
var imports = {
|
|
43
|
-
"@cfworker/json-schema": "npm:@cfworker/json-schema@^4.1.1",
|
|
44
|
-
"yaml": "npm:yaml@^2.8.1"
|
|
45
|
-
};
|
|
46
|
-
var exclude = [
|
|
47
|
-
"dist",
|
|
48
|
-
"node_modules",
|
|
49
|
-
"src/schema.yaml"
|
|
50
|
-
];
|
|
51
|
-
var publish = { "exclude": ["**/*.test.ts", "tsdown.config.ts"] };
|
|
52
|
-
var tasks = {
|
|
53
|
-
"check": "deno fmt --check && deno lint && deno check src/*.ts",
|
|
54
|
-
"test": "deno test -A"
|
|
55
|
-
};
|
|
56
|
-
var deno_default = {
|
|
57
|
-
name,
|
|
58
|
-
version,
|
|
59
|
-
license,
|
|
60
|
-
exports: exports$1,
|
|
61
|
-
author,
|
|
62
|
-
imports,
|
|
63
|
-
exclude,
|
|
64
|
-
publish,
|
|
65
|
-
tasks
|
|
66
|
-
};
|
|
67
|
-
|
|
11
|
+
var version = "2.0.9";
|
|
68
12
|
//#endregion
|
|
69
13
|
//#region src/type.ts
|
|
70
14
|
const HEURISTICS_CONTEXTS = [
|
|
@@ -474,7 +418,7 @@ function getTypeNames(typeUris, types, parentheses = false) {
|
|
|
474
418
|
if (typeUris.length < 1) return "never";
|
|
475
419
|
else if (typeUris.length === 1) return getTypeName(typeUris[0], types);
|
|
476
420
|
let typeNames = typeUris.map((typeUri) => getTypeName(typeUri, types));
|
|
477
|
-
typeNames = typeNames.filter((t
|
|
421
|
+
typeNames = typeNames.filter((t, i) => typeNames.indexOf(t) === i);
|
|
478
422
|
const t = typeNames.join(" | ");
|
|
479
423
|
return parentheses && typeNames.length > 1 ? `(${t})` : t;
|
|
480
424
|
}
|
|
@@ -499,10 +443,7 @@ function isCompactableType(typeUri, types) {
|
|
|
499
443
|
}
|
|
500
444
|
function getSubtypes(typeUri, types, excludeSelf = false) {
|
|
501
445
|
const subtypes = excludeSelf ? [] : [typeUri];
|
|
502
|
-
for (const uri in types)
|
|
503
|
-
const type = types[uri];
|
|
504
|
-
if (type.extends === typeUri) subtypes.push(...getSubtypes(uri, types));
|
|
505
|
-
}
|
|
446
|
+
for (const uri in types) if (types[uri].extends === typeUri) subtypes.push(...getSubtypes(uri, types));
|
|
506
447
|
return subtypes.filter((t, i) => subtypes.indexOf(t) === i);
|
|
507
448
|
}
|
|
508
449
|
function getSupertypes(typeUri, types, excludeSelf = false) {
|
|
@@ -572,11 +513,10 @@ function* getDecoders(typeUris, types, variable, optionsVariable, baseUrlVariabl
|
|
|
572
513
|
}
|
|
573
514
|
yield "undefined";
|
|
574
515
|
}
|
|
575
|
-
function emitOverride(typeUri, types) {
|
|
516
|
+
function emitOverride$1(typeUri, types) {
|
|
576
517
|
if (types[typeUri].extends == null) return "";
|
|
577
518
|
return "override";
|
|
578
519
|
}
|
|
579
|
-
|
|
580
520
|
//#endregion
|
|
581
521
|
//#region src/field.ts
|
|
582
522
|
async function getFieldName(propertyUri, prefix = "#") {
|
|
@@ -588,20 +528,15 @@ async function getFieldName(propertyUri, prefix = "#") {
|
|
|
588
528
|
async function generateField(property, types, prefix = "#") {
|
|
589
529
|
const fieldName = await getFieldName(property.uri, prefix);
|
|
590
530
|
if (areAllScalarTypes(property.range, types)) return `${fieldName}: (${getTypeNames(property.range, types, true)})[] = [];\n`;
|
|
591
|
-
else
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
return `
|
|
595
|
-
${fieldName}: (${typeNames} | URL)[] = [];
|
|
596
|
-
${trustFieldName}: Set<number> = new Set();
|
|
531
|
+
else return `
|
|
532
|
+
${fieldName}: (${getTypeNames(property.range, types)} | URL)[] = [];
|
|
533
|
+
${await getFieldName(property.uri, `${prefix}_trust`)}: Set<number> = new Set();
|
|
597
534
|
`;
|
|
598
|
-
}
|
|
599
535
|
}
|
|
600
536
|
async function* generateFields(typeUri, types) {
|
|
601
537
|
const type = types[typeUri];
|
|
602
538
|
for (const property of type.properties) yield await generateField(property, types);
|
|
603
539
|
}
|
|
604
|
-
|
|
605
540
|
//#endregion
|
|
606
541
|
//#region src/fs.ts
|
|
607
542
|
/**
|
|
@@ -616,7 +551,6 @@ async function* readDirRecursive(dir) {
|
|
|
616
551
|
for await (const subentry of readDirRecursive(path)) yield (0, node_path.join)(entry.name, subentry);
|
|
617
552
|
} else yield entry.name;
|
|
618
553
|
}
|
|
619
|
-
|
|
620
554
|
//#endregion
|
|
621
555
|
//#region src/schema.ts
|
|
622
556
|
/**
|
|
@@ -654,17 +588,13 @@ async function loadSchemaValidator() {
|
|
|
654
588
|
const thisFile = require("url").pathToFileURL(__filename).href;
|
|
655
589
|
const schemaFile = new URL(node_path.posix.join(node_path.posix.dirname(thisFile), "schema.yaml"));
|
|
656
590
|
let content;
|
|
657
|
-
if (schemaFile.protocol !== "file:")
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
} else content = await (0, node_fs_promises.readFile)((0, node_url.fileURLToPath)(schemaFile), { encoding: "utf-8" });
|
|
661
|
-
const schemaObject = (0, yaml.parse)(content);
|
|
662
|
-
return new __cfworker_json_schema.Validator(schemaObject);
|
|
591
|
+
if (schemaFile.protocol !== "file:") content = await (await fetch(schemaFile)).text();
|
|
592
|
+
else content = await (0, node_fs_promises.readFile)((0, node_url.fileURLToPath)(schemaFile), { encoding: "utf-8" });
|
|
593
|
+
return new _cfworker_json_schema.Validator((0, yaml.parse)(content));
|
|
663
594
|
}
|
|
664
595
|
let schemaValidator = void 0;
|
|
665
596
|
async function loadSchema(path) {
|
|
666
|
-
const
|
|
667
|
-
const schema = (0, yaml.parse)(content);
|
|
597
|
+
const schema = (0, yaml.parse)(await (0, node_fs_promises.readFile)(path, { encoding: "utf-8" }));
|
|
668
598
|
if (schemaValidator == null) schemaValidator = await loadSchemaValidator();
|
|
669
599
|
const result = schemaValidator.validate(schema);
|
|
670
600
|
const errors = [];
|
|
@@ -704,7 +634,6 @@ async function loadSchemaFiles(dir) {
|
|
|
704
634
|
entries.sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
705
635
|
return Object.fromEntries(entries);
|
|
706
636
|
}
|
|
707
|
-
|
|
708
637
|
//#endregion
|
|
709
638
|
//#region src/codec.ts
|
|
710
639
|
async function* generateEncoder(typeUri, types) {
|
|
@@ -720,7 +649,7 @@ async function* generateEncoder(typeUri, types) {
|
|
|
720
649
|
when \`format\` is set to \`'expand'\`.
|
|
721
650
|
* @returns The JSON-LD representation of this object.
|
|
722
651
|
*/
|
|
723
|
-
${emitOverride(typeUri, types)} async toJsonLd(options: {
|
|
652
|
+
${emitOverride$1(typeUri, types)} async toJsonLd(options: {
|
|
724
653
|
format?: "compact" | "expand",
|
|
725
654
|
contextLoader?: DocumentLoader,
|
|
726
655
|
context?: string | Record<string, string> | (string | Record<string, string>)[],
|
|
@@ -894,7 +823,7 @@ async function* generateEncoder(typeUri, types) {
|
|
|
894
823
|
return compacted;
|
|
895
824
|
}
|
|
896
825
|
|
|
897
|
-
protected ${emitOverride(typeUri, types)} isCompactable(): boolean {
|
|
826
|
+
protected ${emitOverride$1(typeUri, types)} isCompactable(): boolean {
|
|
898
827
|
`;
|
|
899
828
|
for (const property of type.properties) if (!property.range.every((r) => isCompactableType(r, types))) yield `
|
|
900
829
|
if (
|
|
@@ -921,7 +850,7 @@ async function* generateDecoder(typeUri, types) {
|
|
|
921
850
|
* @returns The object of this type.
|
|
922
851
|
* @throws {TypeError} If the given \`json\` is invalid.
|
|
923
852
|
*/
|
|
924
|
-
static ${emitOverride(typeUri, types)} async fromJsonLd(
|
|
853
|
+
static ${emitOverride$1(typeUri, types)} async fromJsonLd(
|
|
925
854
|
json: unknown,
|
|
926
855
|
options: {
|
|
927
856
|
documentLoader?: DocumentLoader,
|
|
@@ -932,8 +861,8 @@ async function* generateDecoder(typeUri, types) {
|
|
|
932
861
|
): Promise<${type.name}> {
|
|
933
862
|
const tracerProvider = options.tracerProvider ?? trace.getTracerProvider();
|
|
934
863
|
const tracer = tracerProvider.getTracer(
|
|
935
|
-
${JSON.stringify(
|
|
936
|
-
${JSON.stringify(
|
|
864
|
+
${JSON.stringify(name)},
|
|
865
|
+
${JSON.stringify(version)},
|
|
937
866
|
);
|
|
938
867
|
return await tracer.startActiveSpan(
|
|
939
868
|
"activitypub.parse_object",
|
|
@@ -1097,7 +1026,6 @@ async function* generateDecoder(typeUri, types) {
|
|
|
1097
1026
|
}
|
|
1098
1027
|
`;
|
|
1099
1028
|
}
|
|
1100
|
-
|
|
1101
1029
|
//#endregion
|
|
1102
1030
|
//#region src/constructor.ts
|
|
1103
1031
|
function generateParameterType(property, types) {
|
|
@@ -1166,8 +1094,7 @@ async function* generateConstructor(typeUri, types) {
|
|
|
1166
1094
|
if (hasSingularAccessor(property)) {
|
|
1167
1095
|
let typeGuards = getTypeGuards(property.range, types, `values.${property.singularName}`);
|
|
1168
1096
|
let typeNames = getTypeNames(property.range, types);
|
|
1169
|
-
|
|
1170
|
-
if (!scalar) {
|
|
1097
|
+
if (!areAllScalarTypes(property.range, types)) {
|
|
1171
1098
|
typeGuards = `${typeGuards} || values.${property.singularName} instanceof URL`;
|
|
1172
1099
|
typeNames = `${typeNames} | URL`;
|
|
1173
1100
|
}
|
|
@@ -1192,8 +1119,7 @@ async function* generateConstructor(typeUri, types) {
|
|
|
1192
1119
|
if (isNonFunctionalProperty(property)) {
|
|
1193
1120
|
let typeGuards = getTypeGuards(property.range, types, `v`);
|
|
1194
1121
|
let typeNames = getTypeNames(property.range, types);
|
|
1195
|
-
|
|
1196
|
-
if (!scalar) {
|
|
1122
|
+
if (!areAllScalarTypes(property.range, types)) {
|
|
1197
1123
|
typeGuards = `${typeGuards} || v instanceof URL`;
|
|
1198
1124
|
typeNames = `${typeNames} | URL`;
|
|
1199
1125
|
}
|
|
@@ -1243,7 +1169,7 @@ async function* generateCloner(typeUri, types) {
|
|
|
1243
1169
|
* @param options The options to use for cloning.
|
|
1244
1170
|
* @returns The cloned instance.
|
|
1245
1171
|
*/
|
|
1246
|
-
${emitOverride(typeUri, types)} clone(
|
|
1172
|
+
${emitOverride$1(typeUri, types)} clone(
|
|
1247
1173
|
values:
|
|
1248
1174
|
`;
|
|
1249
1175
|
for await (const code of generateParametersType(typeUri, types)) yield code;
|
|
@@ -1280,8 +1206,7 @@ async function* generateCloner(typeUri, types) {
|
|
|
1280
1206
|
if (hasSingularAccessor(property)) {
|
|
1281
1207
|
let typeGuards = getTypeGuards(property.range, types, `values.${property.singularName}`);
|
|
1282
1208
|
let typeNames = getTypeNames(property.range, types);
|
|
1283
|
-
|
|
1284
|
-
if (!scalar) {
|
|
1209
|
+
if (!areAllScalarTypes(property.range, types)) {
|
|
1285
1210
|
typeGuards = `${typeGuards} || values.${property.singularName} instanceof URL`;
|
|
1286
1211
|
typeNames = `${typeNames} | URL`;
|
|
1287
1212
|
}
|
|
@@ -1306,8 +1231,7 @@ async function* generateCloner(typeUri, types) {
|
|
|
1306
1231
|
if (isNonFunctionalProperty(property)) {
|
|
1307
1232
|
let typeGuards = getTypeGuards(property.range, types, `v`);
|
|
1308
1233
|
let typeNames = getTypeNames(property.range, types);
|
|
1309
|
-
|
|
1310
|
-
if (!scalar) {
|
|
1234
|
+
if (!areAllScalarTypes(property.range, types)) {
|
|
1311
1235
|
typeGuards = `${typeGuards} || v instanceof URL`;
|
|
1312
1236
|
typeNames = `${typeNames} | URL`;
|
|
1313
1237
|
}
|
|
@@ -1352,13 +1276,12 @@ async function* generateCloner(typeUri, types) {
|
|
|
1352
1276
|
}
|
|
1353
1277
|
`;
|
|
1354
1278
|
}
|
|
1355
|
-
|
|
1356
1279
|
//#endregion
|
|
1357
1280
|
//#region src/inspector.ts
|
|
1358
1281
|
async function* generateInspector(typeUri, types) {
|
|
1359
1282
|
const type = types[typeUri];
|
|
1360
1283
|
yield `
|
|
1361
|
-
protected ${emitOverride(typeUri, types)} _getCustomInspectProxy(): Record<string, unknown> {
|
|
1284
|
+
protected ${emitOverride$1(typeUri, types)} _getCustomInspectProxy(): Record<string, unknown> {
|
|
1362
1285
|
`;
|
|
1363
1286
|
if (type.extends == null) yield `
|
|
1364
1287
|
const proxy: Record<string, unknown> = {};
|
|
@@ -1450,12 +1373,10 @@ async function* generateInspectorPostClass(typeUri, types) {
|
|
|
1450
1373
|
};
|
|
1451
1374
|
`;
|
|
1452
1375
|
}
|
|
1453
|
-
|
|
1454
1376
|
//#endregion
|
|
1455
1377
|
//#region src/property.ts
|
|
1456
|
-
function emitOverride
|
|
1457
|
-
|
|
1458
|
-
let supertypeUri = type.extends;
|
|
1378
|
+
function emitOverride(typeUri, types, property) {
|
|
1379
|
+
let supertypeUri = types[typeUri].extends;
|
|
1459
1380
|
while (supertypeUri != null) {
|
|
1460
1381
|
const st = types[supertypeUri];
|
|
1461
1382
|
if (st.properties.find((p) => p.singularName === property.singularName)) return "override";
|
|
@@ -1464,7 +1385,7 @@ function emitOverride$1(typeUri, types, property) {
|
|
|
1464
1385
|
return "";
|
|
1465
1386
|
}
|
|
1466
1387
|
async function* generateProperty(type, property, types) {
|
|
1467
|
-
const override = emitOverride
|
|
1388
|
+
const override = emitOverride(type.uri, types, property);
|
|
1468
1389
|
const doc = `\n/** ${property.description.replaceAll("\n", "\n * ")}\n */\n`;
|
|
1469
1390
|
if (areAllScalarTypes(property.range, types)) {
|
|
1470
1391
|
if (hasSingularAccessor(property)) {
|
|
@@ -1507,8 +1428,8 @@ async function* generateProperty(type, property, types) {
|
|
|
1507
1428
|
const tracerProvider = options.tracerProvider ??
|
|
1508
1429
|
this._tracerProvider ?? trace.getTracerProvider();
|
|
1509
1430
|
const tracer = tracerProvider.getTracer(
|
|
1510
|
-
${JSON.stringify(
|
|
1511
|
-
${JSON.stringify(
|
|
1431
|
+
${JSON.stringify(name)},
|
|
1432
|
+
${JSON.stringify(version)},
|
|
1512
1433
|
);
|
|
1513
1434
|
return await tracer.startActiveSpan("activitypub.lookup_object", async (span) => {
|
|
1514
1435
|
let fetchResult: RemoteDocument;
|
|
@@ -1600,10 +1521,9 @@ async function* generateProperty(type, property, types) {
|
|
|
1600
1521
|
`;
|
|
1601
1522
|
for (const range of property.range) {
|
|
1602
1523
|
if (!(range in types)) continue;
|
|
1603
|
-
const rangeType = types[range];
|
|
1604
1524
|
yield `
|
|
1605
1525
|
try {
|
|
1606
|
-
return await ${
|
|
1526
|
+
return await ${types[range].name}.fromJsonLd(
|
|
1607
1527
|
jsonLd,
|
|
1608
1528
|
{ documentLoader, contextLoader, tracerProvider, baseUrl },
|
|
1609
1529
|
);
|
|
@@ -1818,7 +1738,6 @@ async function* generateProperties(typeUri, types) {
|
|
|
1818
1738
|
const type = types[typeUri];
|
|
1819
1739
|
for (const property of type.properties) yield* generateProperty(type, property, types);
|
|
1820
1740
|
}
|
|
1821
|
-
|
|
1822
1741
|
//#endregion
|
|
1823
1742
|
//#region src/class.ts
|
|
1824
1743
|
/**
|
|
@@ -1894,7 +1813,7 @@ async function* generateClass(typeUri, types) {
|
|
|
1894
1813
|
/**
|
|
1895
1814
|
* The type URI of {@link ${type.name}}: \`${typeUri}\`.
|
|
1896
1815
|
*/
|
|
1897
|
-
static ${emitOverride(typeUri, types)} get typeId(): URL {
|
|
1816
|
+
static ${emitOverride$1(typeUri, types)} get typeId(): URL {
|
|
1898
1817
|
return new URL(${JSON.stringify(typeUri)});
|
|
1899
1818
|
}
|
|
1900
1819
|
`;
|
|
@@ -1935,7 +1854,6 @@ async function* generateClasses(types) {
|
|
|
1935
1854
|
const sorted = sortTopologically(types);
|
|
1936
1855
|
for (const typeUri of sorted) for await (const code of generateClass(typeUri, types)) yield code;
|
|
1937
1856
|
}
|
|
1938
|
-
|
|
1939
1857
|
//#endregion
|
|
1940
1858
|
//#region src/generate.ts
|
|
1941
1859
|
async function generateVocab(schemaDir, generatedPath) {
|
|
@@ -1950,8 +1868,7 @@ async function generateVocab(schemaDir, generatedPath) {
|
|
|
1950
1868
|
writer.end();
|
|
1951
1869
|
});
|
|
1952
1870
|
}
|
|
1953
|
-
|
|
1954
1871
|
//#endregion
|
|
1955
1872
|
exports.areAllScalarTypes = areAllScalarTypes;
|
|
1956
1873
|
exports.generateVocab = generateVocab;
|
|
1957
|
-
exports.loadSchemaFiles = loadSchemaFiles;
|
|
1874
|
+
exports.loadSchemaFiles = loadSchemaFiles;
|
package/dist/mod.d.cts
CHANGED
|
@@ -174,7 +174,6 @@ type TermDefinition = Uri | Record<string, Uri | "@id">;
|
|
|
174
174
|
/**
|
|
175
175
|
* Type guard to check if a property is not functional (has pluralName).
|
|
176
176
|
*/
|
|
177
|
-
|
|
178
177
|
/**
|
|
179
178
|
* Loads all schema files in the directory.
|
|
180
179
|
* @param dir The path of the directory to load schema files from.
|
|
@@ -187,4 +186,4 @@ declare function loadSchemaFiles(dir: string): Promise<Record<string, TypeSchema
|
|
|
187
186
|
//#region src/type.d.ts
|
|
188
187
|
declare function areAllScalarTypes(typeUris: string[], types: Record<string, TypeSchema>): boolean;
|
|
189
188
|
//#endregion
|
|
190
|
-
export { PropertySchema, TypeSchema, areAllScalarTypes, generateVocab, loadSchemaFiles };
|
|
189
|
+
export { type PropertySchema, type TypeSchema, areAllScalarTypes, generateVocab, loadSchemaFiles };
|
package/dist/mod.d.ts
CHANGED
|
@@ -174,7 +174,6 @@ type TermDefinition = Uri | Record<string, Uri | "@id">;
|
|
|
174
174
|
/**
|
|
175
175
|
* Type guard to check if a property is not functional (has pluralName).
|
|
176
176
|
*/
|
|
177
|
-
|
|
178
177
|
/**
|
|
179
178
|
* Loads all schema files in the directory.
|
|
180
179
|
* @param dir The path of the directory to load schema files from.
|
|
@@ -187,4 +186,4 @@ declare function loadSchemaFiles(dir: string): Promise<Record<string, TypeSchema
|
|
|
187
186
|
//#region src/type.d.ts
|
|
188
187
|
declare function areAllScalarTypes(typeUris: string[], types: Record<string, TypeSchema>): boolean;
|
|
189
188
|
//#endregion
|
|
190
|
-
export { PropertySchema, TypeSchema, areAllScalarTypes, generateVocab, loadSchemaFiles };
|
|
189
|
+
export { type PropertySchema, type TypeSchema, areAllScalarTypes, generateVocab, loadSchemaFiles };
|
package/dist/mod.js
CHANGED
|
@@ -5,43 +5,9 @@ import { join, posix } from "node:path";
|
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
6
|
import { parse } from "yaml";
|
|
7
7
|
import { pascalCase } from "es-toolkit";
|
|
8
|
-
|
|
9
8
|
//#region deno.json
|
|
10
9
|
var name = "@fedify/vocab-tools";
|
|
11
|
-
var version = "2.0.
|
|
12
|
-
var license = "MIT";
|
|
13
|
-
var exports = { ".": "./src/mod.ts" };
|
|
14
|
-
var author = {
|
|
15
|
-
"name": "Hong Minhee",
|
|
16
|
-
"email": "hong@minhee.org",
|
|
17
|
-
"url": "https://hongminhee.org/"
|
|
18
|
-
};
|
|
19
|
-
var imports = {
|
|
20
|
-
"@cfworker/json-schema": "npm:@cfworker/json-schema@^4.1.1",
|
|
21
|
-
"yaml": "npm:yaml@^2.8.1"
|
|
22
|
-
};
|
|
23
|
-
var exclude = [
|
|
24
|
-
"dist",
|
|
25
|
-
"node_modules",
|
|
26
|
-
"src/schema.yaml"
|
|
27
|
-
];
|
|
28
|
-
var publish = { "exclude": ["**/*.test.ts", "tsdown.config.ts"] };
|
|
29
|
-
var tasks = {
|
|
30
|
-
"check": "deno fmt --check && deno lint && deno check src/*.ts",
|
|
31
|
-
"test": "deno test -A"
|
|
32
|
-
};
|
|
33
|
-
var deno_default = {
|
|
34
|
-
name,
|
|
35
|
-
version,
|
|
36
|
-
license,
|
|
37
|
-
exports,
|
|
38
|
-
author,
|
|
39
|
-
imports,
|
|
40
|
-
exclude,
|
|
41
|
-
publish,
|
|
42
|
-
tasks
|
|
43
|
-
};
|
|
44
|
-
|
|
10
|
+
var version = "2.0.9";
|
|
45
11
|
//#endregion
|
|
46
12
|
//#region src/type.ts
|
|
47
13
|
const HEURISTICS_CONTEXTS = [
|
|
@@ -451,7 +417,7 @@ function getTypeNames(typeUris, types, parentheses = false) {
|
|
|
451
417
|
if (typeUris.length < 1) return "never";
|
|
452
418
|
else if (typeUris.length === 1) return getTypeName(typeUris[0], types);
|
|
453
419
|
let typeNames = typeUris.map((typeUri) => getTypeName(typeUri, types));
|
|
454
|
-
typeNames = typeNames.filter((t
|
|
420
|
+
typeNames = typeNames.filter((t, i) => typeNames.indexOf(t) === i);
|
|
455
421
|
const t = typeNames.join(" | ");
|
|
456
422
|
return parentheses && typeNames.length > 1 ? `(${t})` : t;
|
|
457
423
|
}
|
|
@@ -476,10 +442,7 @@ function isCompactableType(typeUri, types) {
|
|
|
476
442
|
}
|
|
477
443
|
function getSubtypes(typeUri, types, excludeSelf = false) {
|
|
478
444
|
const subtypes = excludeSelf ? [] : [typeUri];
|
|
479
|
-
for (const uri in types)
|
|
480
|
-
const type = types[uri];
|
|
481
|
-
if (type.extends === typeUri) subtypes.push(...getSubtypes(uri, types));
|
|
482
|
-
}
|
|
445
|
+
for (const uri in types) if (types[uri].extends === typeUri) subtypes.push(...getSubtypes(uri, types));
|
|
483
446
|
return subtypes.filter((t, i) => subtypes.indexOf(t) === i);
|
|
484
447
|
}
|
|
485
448
|
function getSupertypes(typeUri, types, excludeSelf = false) {
|
|
@@ -549,11 +512,10 @@ function* getDecoders(typeUris, types, variable, optionsVariable, baseUrlVariabl
|
|
|
549
512
|
}
|
|
550
513
|
yield "undefined";
|
|
551
514
|
}
|
|
552
|
-
function emitOverride(typeUri, types) {
|
|
515
|
+
function emitOverride$1(typeUri, types) {
|
|
553
516
|
if (types[typeUri].extends == null) return "";
|
|
554
517
|
return "override";
|
|
555
518
|
}
|
|
556
|
-
|
|
557
519
|
//#endregion
|
|
558
520
|
//#region src/field.ts
|
|
559
521
|
async function getFieldName(propertyUri, prefix = "#") {
|
|
@@ -565,20 +527,15 @@ async function getFieldName(propertyUri, prefix = "#") {
|
|
|
565
527
|
async function generateField(property, types, prefix = "#") {
|
|
566
528
|
const fieldName = await getFieldName(property.uri, prefix);
|
|
567
529
|
if (areAllScalarTypes(property.range, types)) return `${fieldName}: (${getTypeNames(property.range, types, true)})[] = [];\n`;
|
|
568
|
-
else
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
return `
|
|
572
|
-
${fieldName}: (${typeNames} | URL)[] = [];
|
|
573
|
-
${trustFieldName}: Set<number> = new Set();
|
|
530
|
+
else return `
|
|
531
|
+
${fieldName}: (${getTypeNames(property.range, types)} | URL)[] = [];
|
|
532
|
+
${await getFieldName(property.uri, `${prefix}_trust`)}: Set<number> = new Set();
|
|
574
533
|
`;
|
|
575
|
-
}
|
|
576
534
|
}
|
|
577
535
|
async function* generateFields(typeUri, types) {
|
|
578
536
|
const type = types[typeUri];
|
|
579
537
|
for (const property of type.properties) yield await generateField(property, types);
|
|
580
538
|
}
|
|
581
|
-
|
|
582
539
|
//#endregion
|
|
583
540
|
//#region src/fs.ts
|
|
584
541
|
/**
|
|
@@ -593,7 +550,6 @@ async function* readDirRecursive(dir) {
|
|
|
593
550
|
for await (const subentry of readDirRecursive(path)) yield join(entry.name, subentry);
|
|
594
551
|
} else yield entry.name;
|
|
595
552
|
}
|
|
596
|
-
|
|
597
553
|
//#endregion
|
|
598
554
|
//#region src/schema.ts
|
|
599
555
|
/**
|
|
@@ -631,17 +587,13 @@ async function loadSchemaValidator() {
|
|
|
631
587
|
const thisFile = import.meta.url;
|
|
632
588
|
const schemaFile = new URL(posix.join(posix.dirname(thisFile), "schema.yaml"));
|
|
633
589
|
let content;
|
|
634
|
-
if (schemaFile.protocol !== "file:")
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
} else content = await readFile(fileURLToPath(schemaFile), { encoding: "utf-8" });
|
|
638
|
-
const schemaObject = parse(content);
|
|
639
|
-
return new Validator(schemaObject);
|
|
590
|
+
if (schemaFile.protocol !== "file:") content = await (await fetch(schemaFile)).text();
|
|
591
|
+
else content = await readFile(fileURLToPath(schemaFile), { encoding: "utf-8" });
|
|
592
|
+
return new Validator(parse(content));
|
|
640
593
|
}
|
|
641
594
|
let schemaValidator = void 0;
|
|
642
595
|
async function loadSchema(path) {
|
|
643
|
-
const
|
|
644
|
-
const schema = parse(content);
|
|
596
|
+
const schema = parse(await readFile(path, { encoding: "utf-8" }));
|
|
645
597
|
if (schemaValidator == null) schemaValidator = await loadSchemaValidator();
|
|
646
598
|
const result = schemaValidator.validate(schema);
|
|
647
599
|
const errors = [];
|
|
@@ -681,7 +633,6 @@ async function loadSchemaFiles(dir) {
|
|
|
681
633
|
entries.sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
682
634
|
return Object.fromEntries(entries);
|
|
683
635
|
}
|
|
684
|
-
|
|
685
636
|
//#endregion
|
|
686
637
|
//#region src/codec.ts
|
|
687
638
|
async function* generateEncoder(typeUri, types) {
|
|
@@ -697,7 +648,7 @@ async function* generateEncoder(typeUri, types) {
|
|
|
697
648
|
when \`format\` is set to \`'expand'\`.
|
|
698
649
|
* @returns The JSON-LD representation of this object.
|
|
699
650
|
*/
|
|
700
|
-
${emitOverride(typeUri, types)} async toJsonLd(options: {
|
|
651
|
+
${emitOverride$1(typeUri, types)} async toJsonLd(options: {
|
|
701
652
|
format?: "compact" | "expand",
|
|
702
653
|
contextLoader?: DocumentLoader,
|
|
703
654
|
context?: string | Record<string, string> | (string | Record<string, string>)[],
|
|
@@ -871,7 +822,7 @@ async function* generateEncoder(typeUri, types) {
|
|
|
871
822
|
return compacted;
|
|
872
823
|
}
|
|
873
824
|
|
|
874
|
-
protected ${emitOverride(typeUri, types)} isCompactable(): boolean {
|
|
825
|
+
protected ${emitOverride$1(typeUri, types)} isCompactable(): boolean {
|
|
875
826
|
`;
|
|
876
827
|
for (const property of type.properties) if (!property.range.every((r) => isCompactableType(r, types))) yield `
|
|
877
828
|
if (
|
|
@@ -898,7 +849,7 @@ async function* generateDecoder(typeUri, types) {
|
|
|
898
849
|
* @returns The object of this type.
|
|
899
850
|
* @throws {TypeError} If the given \`json\` is invalid.
|
|
900
851
|
*/
|
|
901
|
-
static ${emitOverride(typeUri, types)} async fromJsonLd(
|
|
852
|
+
static ${emitOverride$1(typeUri, types)} async fromJsonLd(
|
|
902
853
|
json: unknown,
|
|
903
854
|
options: {
|
|
904
855
|
documentLoader?: DocumentLoader,
|
|
@@ -909,8 +860,8 @@ async function* generateDecoder(typeUri, types) {
|
|
|
909
860
|
): Promise<${type.name}> {
|
|
910
861
|
const tracerProvider = options.tracerProvider ?? trace.getTracerProvider();
|
|
911
862
|
const tracer = tracerProvider.getTracer(
|
|
912
|
-
${JSON.stringify(
|
|
913
|
-
${JSON.stringify(
|
|
863
|
+
${JSON.stringify(name)},
|
|
864
|
+
${JSON.stringify(version)},
|
|
914
865
|
);
|
|
915
866
|
return await tracer.startActiveSpan(
|
|
916
867
|
"activitypub.parse_object",
|
|
@@ -1074,7 +1025,6 @@ async function* generateDecoder(typeUri, types) {
|
|
|
1074
1025
|
}
|
|
1075
1026
|
`;
|
|
1076
1027
|
}
|
|
1077
|
-
|
|
1078
1028
|
//#endregion
|
|
1079
1029
|
//#region src/constructor.ts
|
|
1080
1030
|
function generateParameterType(property, types) {
|
|
@@ -1143,8 +1093,7 @@ async function* generateConstructor(typeUri, types) {
|
|
|
1143
1093
|
if (hasSingularAccessor(property)) {
|
|
1144
1094
|
let typeGuards = getTypeGuards(property.range, types, `values.${property.singularName}`);
|
|
1145
1095
|
let typeNames = getTypeNames(property.range, types);
|
|
1146
|
-
|
|
1147
|
-
if (!scalar) {
|
|
1096
|
+
if (!areAllScalarTypes(property.range, types)) {
|
|
1148
1097
|
typeGuards = `${typeGuards} || values.${property.singularName} instanceof URL`;
|
|
1149
1098
|
typeNames = `${typeNames} | URL`;
|
|
1150
1099
|
}
|
|
@@ -1169,8 +1118,7 @@ async function* generateConstructor(typeUri, types) {
|
|
|
1169
1118
|
if (isNonFunctionalProperty(property)) {
|
|
1170
1119
|
let typeGuards = getTypeGuards(property.range, types, `v`);
|
|
1171
1120
|
let typeNames = getTypeNames(property.range, types);
|
|
1172
|
-
|
|
1173
|
-
if (!scalar) {
|
|
1121
|
+
if (!areAllScalarTypes(property.range, types)) {
|
|
1174
1122
|
typeGuards = `${typeGuards} || v instanceof URL`;
|
|
1175
1123
|
typeNames = `${typeNames} | URL`;
|
|
1176
1124
|
}
|
|
@@ -1220,7 +1168,7 @@ async function* generateCloner(typeUri, types) {
|
|
|
1220
1168
|
* @param options The options to use for cloning.
|
|
1221
1169
|
* @returns The cloned instance.
|
|
1222
1170
|
*/
|
|
1223
|
-
${emitOverride(typeUri, types)} clone(
|
|
1171
|
+
${emitOverride$1(typeUri, types)} clone(
|
|
1224
1172
|
values:
|
|
1225
1173
|
`;
|
|
1226
1174
|
for await (const code of generateParametersType(typeUri, types)) yield code;
|
|
@@ -1257,8 +1205,7 @@ async function* generateCloner(typeUri, types) {
|
|
|
1257
1205
|
if (hasSingularAccessor(property)) {
|
|
1258
1206
|
let typeGuards = getTypeGuards(property.range, types, `values.${property.singularName}`);
|
|
1259
1207
|
let typeNames = getTypeNames(property.range, types);
|
|
1260
|
-
|
|
1261
|
-
if (!scalar) {
|
|
1208
|
+
if (!areAllScalarTypes(property.range, types)) {
|
|
1262
1209
|
typeGuards = `${typeGuards} || values.${property.singularName} instanceof URL`;
|
|
1263
1210
|
typeNames = `${typeNames} | URL`;
|
|
1264
1211
|
}
|
|
@@ -1283,8 +1230,7 @@ async function* generateCloner(typeUri, types) {
|
|
|
1283
1230
|
if (isNonFunctionalProperty(property)) {
|
|
1284
1231
|
let typeGuards = getTypeGuards(property.range, types, `v`);
|
|
1285
1232
|
let typeNames = getTypeNames(property.range, types);
|
|
1286
|
-
|
|
1287
|
-
if (!scalar) {
|
|
1233
|
+
if (!areAllScalarTypes(property.range, types)) {
|
|
1288
1234
|
typeGuards = `${typeGuards} || v instanceof URL`;
|
|
1289
1235
|
typeNames = `${typeNames} | URL`;
|
|
1290
1236
|
}
|
|
@@ -1329,13 +1275,12 @@ async function* generateCloner(typeUri, types) {
|
|
|
1329
1275
|
}
|
|
1330
1276
|
`;
|
|
1331
1277
|
}
|
|
1332
|
-
|
|
1333
1278
|
//#endregion
|
|
1334
1279
|
//#region src/inspector.ts
|
|
1335
1280
|
async function* generateInspector(typeUri, types) {
|
|
1336
1281
|
const type = types[typeUri];
|
|
1337
1282
|
yield `
|
|
1338
|
-
protected ${emitOverride(typeUri, types)} _getCustomInspectProxy(): Record<string, unknown> {
|
|
1283
|
+
protected ${emitOverride$1(typeUri, types)} _getCustomInspectProxy(): Record<string, unknown> {
|
|
1339
1284
|
`;
|
|
1340
1285
|
if (type.extends == null) yield `
|
|
1341
1286
|
const proxy: Record<string, unknown> = {};
|
|
@@ -1427,12 +1372,10 @@ async function* generateInspectorPostClass(typeUri, types) {
|
|
|
1427
1372
|
};
|
|
1428
1373
|
`;
|
|
1429
1374
|
}
|
|
1430
|
-
|
|
1431
1375
|
//#endregion
|
|
1432
1376
|
//#region src/property.ts
|
|
1433
|
-
function emitOverride
|
|
1434
|
-
|
|
1435
|
-
let supertypeUri = type.extends;
|
|
1377
|
+
function emitOverride(typeUri, types, property) {
|
|
1378
|
+
let supertypeUri = types[typeUri].extends;
|
|
1436
1379
|
while (supertypeUri != null) {
|
|
1437
1380
|
const st = types[supertypeUri];
|
|
1438
1381
|
if (st.properties.find((p) => p.singularName === property.singularName)) return "override";
|
|
@@ -1441,7 +1384,7 @@ function emitOverride$1(typeUri, types, property) {
|
|
|
1441
1384
|
return "";
|
|
1442
1385
|
}
|
|
1443
1386
|
async function* generateProperty(type, property, types) {
|
|
1444
|
-
const override = emitOverride
|
|
1387
|
+
const override = emitOverride(type.uri, types, property);
|
|
1445
1388
|
const doc = `\n/** ${property.description.replaceAll("\n", "\n * ")}\n */\n`;
|
|
1446
1389
|
if (areAllScalarTypes(property.range, types)) {
|
|
1447
1390
|
if (hasSingularAccessor(property)) {
|
|
@@ -1484,8 +1427,8 @@ async function* generateProperty(type, property, types) {
|
|
|
1484
1427
|
const tracerProvider = options.tracerProvider ??
|
|
1485
1428
|
this._tracerProvider ?? trace.getTracerProvider();
|
|
1486
1429
|
const tracer = tracerProvider.getTracer(
|
|
1487
|
-
${JSON.stringify(
|
|
1488
|
-
${JSON.stringify(
|
|
1430
|
+
${JSON.stringify(name)},
|
|
1431
|
+
${JSON.stringify(version)},
|
|
1489
1432
|
);
|
|
1490
1433
|
return await tracer.startActiveSpan("activitypub.lookup_object", async (span) => {
|
|
1491
1434
|
let fetchResult: RemoteDocument;
|
|
@@ -1577,10 +1520,9 @@ async function* generateProperty(type, property, types) {
|
|
|
1577
1520
|
`;
|
|
1578
1521
|
for (const range of property.range) {
|
|
1579
1522
|
if (!(range in types)) continue;
|
|
1580
|
-
const rangeType = types[range];
|
|
1581
1523
|
yield `
|
|
1582
1524
|
try {
|
|
1583
|
-
return await ${
|
|
1525
|
+
return await ${types[range].name}.fromJsonLd(
|
|
1584
1526
|
jsonLd,
|
|
1585
1527
|
{ documentLoader, contextLoader, tracerProvider, baseUrl },
|
|
1586
1528
|
);
|
|
@@ -1795,7 +1737,6 @@ async function* generateProperties(typeUri, types) {
|
|
|
1795
1737
|
const type = types[typeUri];
|
|
1796
1738
|
for (const property of type.properties) yield* generateProperty(type, property, types);
|
|
1797
1739
|
}
|
|
1798
|
-
|
|
1799
1740
|
//#endregion
|
|
1800
1741
|
//#region src/class.ts
|
|
1801
1742
|
/**
|
|
@@ -1871,7 +1812,7 @@ async function* generateClass(typeUri, types) {
|
|
|
1871
1812
|
/**
|
|
1872
1813
|
* The type URI of {@link ${type.name}}: \`${typeUri}\`.
|
|
1873
1814
|
*/
|
|
1874
|
-
static ${emitOverride(typeUri, types)} get typeId(): URL {
|
|
1815
|
+
static ${emitOverride$1(typeUri, types)} get typeId(): URL {
|
|
1875
1816
|
return new URL(${JSON.stringify(typeUri)});
|
|
1876
1817
|
}
|
|
1877
1818
|
`;
|
|
@@ -1912,7 +1853,6 @@ async function* generateClasses(types) {
|
|
|
1912
1853
|
const sorted = sortTopologically(types);
|
|
1913
1854
|
for (const typeUri of sorted) for await (const code of generateClass(typeUri, types)) yield code;
|
|
1914
1855
|
}
|
|
1915
|
-
|
|
1916
1856
|
//#endregion
|
|
1917
1857
|
//#region src/generate.ts
|
|
1918
1858
|
async function generateVocab(schemaDir, generatedPath) {
|
|
@@ -1927,6 +1867,5 @@ async function generateVocab(schemaDir, generatedPath) {
|
|
|
1927
1867
|
writer.end();
|
|
1928
1868
|
});
|
|
1929
1869
|
}
|
|
1930
|
-
|
|
1931
1870
|
//#endregion
|
|
1932
|
-
export { areAllScalarTypes, generateVocab, loadSchemaFiles };
|
|
1871
|
+
export { areAllScalarTypes, generateVocab, loadSchemaFiles };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fedify/vocab-tools",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.9",
|
|
4
4
|
"description": "Code generator for Activity Vocabulary APIs",
|
|
5
5
|
"homepage": "https://fedify.dev/",
|
|
6
6
|
"repository": {
|
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
"license": "MIT",
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/node": "^22.17.0",
|
|
53
|
-
"tsdown": "^0.
|
|
54
|
-
"typescript": "^5.9.
|
|
53
|
+
"tsdown": "^0.21.6",
|
|
54
|
+
"typescript": "^5.9.2"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"@cfworker/json-schema": "^4.1.1",
|