@fedify/vocab-tools 2.5.0-dev.2513 → 2.5.0-dev.2516
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 +475 -23
- package/dist/mod.js +475 -23
- package/package.json +2 -2
- package/src/__snapshots__/class.test.ts.deno.snap +18382 -2337
- package/src/__snapshots__/class.test.ts.node.snap +18382 -2337
- package/src/__snapshots__/class.test.ts.snap +18382 -2337
- package/src/class.ts +19 -1
- package/src/constructor.ts +117 -17
- package/src/deep-clone.ts +175 -0
- package/src/field.ts +1 -1
- package/src/property-array.ts +198 -0
- package/src/property.ts +20 -4
package/deno.json
CHANGED
package/dist/mod.cjs
CHANGED
|
@@ -8,7 +8,7 @@ let yaml = require("yaml");
|
|
|
8
8
|
let es_toolkit = require("es-toolkit");
|
|
9
9
|
//#region deno.json
|
|
10
10
|
var name = "@fedify/vocab-tools";
|
|
11
|
-
var version = "2.5.0-dev.
|
|
11
|
+
var version = "2.5.0-dev.2516+42d3ed0b";
|
|
12
12
|
//#endregion
|
|
13
13
|
//#region src/type.ts
|
|
14
14
|
const HEURISTICS_CONTEXTS = [
|
|
@@ -617,7 +617,7 @@ async function generateField(property, types, prefix = "#") {
|
|
|
617
617
|
if (areAllScalarTypes(property.range, types)) return `${fieldName}: (${getTypeNames(property.range, types, true)})[] = [];\n`;
|
|
618
618
|
else return `
|
|
619
619
|
${fieldName}: (${getTypeNames(property.range, types)} | URL)[] = [];
|
|
620
|
-
${await getFieldName(property.uri, `${prefix}_trust`)}:
|
|
620
|
+
${await getFieldName(property.uri, `${prefix}_trust`)}: VocabTrustSet = new Set();
|
|
621
621
|
`;
|
|
622
622
|
}
|
|
623
623
|
async function* generateFields(typeUri, types) {
|
|
@@ -1324,6 +1324,35 @@ async function* generateParametersType(typeUri, types, parentheses = true, exclu
|
|
|
1324
1324
|
}
|
|
1325
1325
|
if (parentheses) yield "}\n";
|
|
1326
1326
|
}
|
|
1327
|
+
function generateValidatedArrayCopy(name, typeNames, typeGuards) {
|
|
1328
|
+
return `
|
|
1329
|
+
let copied!: (${typeNames})[];
|
|
1330
|
+
let valid = false;
|
|
1331
|
+
if (Array.isArray(values.${name})) {
|
|
1332
|
+
const array = values.${name};
|
|
1333
|
+
const test = (v: ${typeNames}): boolean => ${typeGuards};
|
|
1334
|
+
let checked: VocabArrayValidation | undefined;
|
|
1335
|
+
if (array.length < vocabSparseArrayMinimumLength
|
|
1336
|
+
? array.every(test)
|
|
1337
|
+
: (checked = vocabValidatePropertyArray(array, test)).valid) {
|
|
1338
|
+
const input = values.${name};
|
|
1339
|
+
if (input.length < vocabSparseArrayMinimumLength && !checked?.sparse) {
|
|
1340
|
+
copied = input.slice();
|
|
1341
|
+
valid = true;
|
|
1342
|
+
} else {
|
|
1343
|
+
const result = vocabCopyValidatedPropertyArray(
|
|
1344
|
+
input, test, checked ?? { valid: true, sparse: false },
|
|
1345
|
+
);
|
|
1346
|
+
if (result.ok) {
|
|
1347
|
+
copied = result.array;
|
|
1348
|
+
valid = true;
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
if (valid) {
|
|
1354
|
+
`.trimEnd() + "\n";
|
|
1355
|
+
}
|
|
1327
1356
|
async function* generateConstructor(typeUri, types) {
|
|
1328
1357
|
const type = types[typeUri];
|
|
1329
1358
|
yield `
|
|
@@ -1415,16 +1444,13 @@ async function* generateConstructor(typeUri, types) {
|
|
|
1415
1444
|
);
|
|
1416
1445
|
}
|
|
1417
1446
|
`;
|
|
1447
|
+
yield generateValidatedArrayCopy(property.pluralName, typeNames, typeGuards);
|
|
1418
1448
|
yield `
|
|
1419
|
-
if (Array.isArray(values.${property.pluralName}) &&
|
|
1420
|
-
values.${property.pluralName}.every(v => ${typeGuards})) {
|
|
1421
1449
|
// @ts-ignore: type is checked above.
|
|
1422
|
-
this.${fieldName} =
|
|
1450
|
+
this.${fieldName} = copied;
|
|
1423
1451
|
`;
|
|
1424
1452
|
if (!allScalarTypes) yield `
|
|
1425
|
-
|
|
1426
|
-
this.${trustFieldName}.add(i);
|
|
1427
|
-
}
|
|
1453
|
+
vocabSetTrustPrefix(this.${trustFieldName}, this.${fieldName}.length);
|
|
1428
1454
|
`;
|
|
1429
1455
|
yield `
|
|
1430
1456
|
} else {
|
|
@@ -1453,8 +1479,8 @@ async function* generateCloner(typeUri, types) {
|
|
|
1453
1479
|
* object. Hydrating a top-level property of the clone does not update this
|
|
1454
1480
|
* instance's property.
|
|
1455
1481
|
*
|
|
1456
|
-
* To resolve properties without sharing nested objects,
|
|
1457
|
-
*
|
|
1482
|
+
* To resolve properties without sharing nested objects, use
|
|
1483
|
+
* {@link ${type.name}.deepClone} before hydration.
|
|
1458
1484
|
* See the [vocabulary manual](https://fedify.dev/manual/vocab#immutability)
|
|
1459
1485
|
* for details, including how cached JSON-LD affects serialization.
|
|
1460
1486
|
*
|
|
@@ -1503,8 +1529,11 @@ async function* generateCloner(typeUri, types) {
|
|
|
1503
1529
|
const fieldName = await getFieldName(property.uri);
|
|
1504
1530
|
const trustFieldName = await getFieldName(property.uri, "#_trust");
|
|
1505
1531
|
const allScalarTypes = areAllScalarTypes(property.range, types);
|
|
1506
|
-
yield `clone.${fieldName} = this.${fieldName}.
|
|
1507
|
-
|
|
1532
|
+
yield `clone.${fieldName} = this.${fieldName}.length < vocabSparseArrayMinimumLength
|
|
1533
|
+
? this.${fieldName}.slice() : vocabCopyPropertyArray(this.${fieldName});`;
|
|
1534
|
+
if (!allScalarTypes) yield `clone.${trustFieldName} = vocabCopyTrustSet(
|
|
1535
|
+
this.${trustFieldName}, clone.${trustFieldName},
|
|
1536
|
+
);`;
|
|
1508
1537
|
if (hasSingularAccessor(property)) {
|
|
1509
1538
|
let typeGuards = getTypeGuards(property.range, types, `values.${property.singularName}`);
|
|
1510
1539
|
let typeNames = getTypeNames(property.range, types);
|
|
@@ -1519,7 +1548,8 @@ async function* generateCloner(typeUri, types) {
|
|
|
1519
1548
|
// @ts-ignore: type is checked above.
|
|
1520
1549
|
clone.${fieldName} = [values.${property.singularName}];
|
|
1521
1550
|
`;
|
|
1522
|
-
if (!allScalarTypes) yield `clone.${trustFieldName} = new Set(
|
|
1551
|
+
if (!allScalarTypes) yield `clone.${trustFieldName} = new Set();
|
|
1552
|
+
clone.${trustFieldName}[vocabTrustPrefix] = 1;`;
|
|
1523
1553
|
yield `
|
|
1524
1554
|
} else {
|
|
1525
1555
|
throw new TypeError(
|
|
@@ -1550,17 +1580,14 @@ async function* generateCloner(typeUri, types) {
|
|
|
1550
1580
|
);
|
|
1551
1581
|
}
|
|
1552
1582
|
`;
|
|
1583
|
+
yield generateValidatedArrayCopy(property.pluralName, typeNames, typeGuards);
|
|
1553
1584
|
yield `
|
|
1554
|
-
if (Array.isArray(values.${property.pluralName}) &&
|
|
1555
|
-
values.${property.pluralName}.every(v => ${typeGuards})) {
|
|
1556
1585
|
// @ts-ignore: type is checked above.
|
|
1557
|
-
clone.${fieldName} =
|
|
1586
|
+
clone.${fieldName} = copied;
|
|
1558
1587
|
`;
|
|
1559
1588
|
if (!allScalarTypes) yield `
|
|
1560
1589
|
clone.${trustFieldName} = new Set();
|
|
1561
|
-
|
|
1562
|
-
clone.${trustFieldName}.add(i);
|
|
1563
|
-
}
|
|
1590
|
+
vocabSetTrustPrefix(clone.${trustFieldName}, clone.${fieldName}.length);
|
|
1564
1591
|
`;
|
|
1565
1592
|
yield `
|
|
1566
1593
|
} else {
|
|
@@ -1576,7 +1603,223 @@ async function* generateCloner(typeUri, types) {
|
|
|
1576
1603
|
yield `
|
|
1577
1604
|
return clone;
|
|
1578
1605
|
}
|
|
1606
|
+
`.trimEnd() + "\n";
|
|
1607
|
+
}
|
|
1608
|
+
async function* generateDeepCloner(typeUri, types) {
|
|
1609
|
+
const type = types[typeUri];
|
|
1610
|
+
yield `
|
|
1611
|
+
/**
|
|
1612
|
+
* Copies this instance and its nested vocabulary objects, optionally
|
|
1613
|
+
* replacing property values. Replacement values are copied too.
|
|
1614
|
+
*
|
|
1615
|
+
* Property arrays, URLs and byte arrays are independent of the source.
|
|
1616
|
+
* Repeated references remain shared within the copy. References to this
|
|
1617
|
+
* source in replacement values receive a separate copy of its pre-update
|
|
1618
|
+
* values, rather than referring back to the updated root.
|
|
1619
|
+
* Other scalar values, including opaque cryptographic keys, are shared.
|
|
1620
|
+
*
|
|
1621
|
+
* Each copied object retains its own default loaders, tracer, verifier
|
|
1622
|
+
* and base URL. Options override defaults only on the root instance.
|
|
1623
|
+
* This method does not fetch or serialize objects. Cached JSON-LD and
|
|
1624
|
+
* retained signed representations are not copied; unknown JSON-LD
|
|
1625
|
+
* properties and original formatting are not preserved. Sign the copy
|
|
1626
|
+
* again if it needs a valid proof.
|
|
1627
|
+
*
|
|
1628
|
+
* Trust and portable provenance follow clone() rules at every level.
|
|
1629
|
+
* Historical referrers within the graph are copied without replacements;
|
|
1630
|
+
* referrers outside the graph still point at the original external objects.
|
|
1631
|
+
* Custom clone() overrides must return fresh, compatible instances and
|
|
1632
|
+
* remain responsible for their own constructor and cloning side effects.
|
|
1633
|
+
* Large sparse property arrays with ordinary data entries are copied using
|
|
1634
|
+
* a snapshot of their length and present indices. Entries added after a
|
|
1635
|
+
* snapshot are not visited; deleted entries are skipped, and values changed
|
|
1636
|
+
* after the snapshot are read when reached. Custom
|
|
1637
|
+
* clone() overrides must avoid structurally changing a field being copied
|
|
1638
|
+
* if they need all its nested objects to be independent of the source.
|
|
1639
|
+
*
|
|
1640
|
+
* @param values The values to replace before copying the graph.
|
|
1641
|
+
* @param options Root loader and verifier overrides. Undefined options
|
|
1642
|
+
* retain the corresponding defaults.
|
|
1643
|
+
* @returns An independent copy of the stored vocabulary graph.
|
|
1644
|
+
* @since 2.5.0
|
|
1645
|
+
*/
|
|
1646
|
+
${emitOverride$1(typeUri, types)} deepClone(values:
|
|
1579
1647
|
`;
|
|
1648
|
+
for await (const code of generateParametersType(typeUri, types)) yield code;
|
|
1649
|
+
yield `= {}, options: {
|
|
1650
|
+
documentLoader?: DocumentLoader,
|
|
1651
|
+
contextLoader?: DocumentLoader,
|
|
1652
|
+
verifyPortableObject?: PortableObjectVerifier,
|
|
1653
|
+
} = {}): ${type.name} {
|
|
1654
|
+
const cloneOptions = {
|
|
1655
|
+
documentLoader: options.documentLoader ?? this._documentLoader,
|
|
1656
|
+
contextLoader: options.contextLoader ?? this._contextLoader,
|
|
1657
|
+
verifyPortableObject: options.verifyPortableObject ?? this._verifyPortableObject,
|
|
1658
|
+
tracerProvider: this._tracerProvider,
|
|
1659
|
+
baseUrl: this._baseUrl,
|
|
1660
|
+
};
|
|
1661
|
+
return deepCloneGraph(this, this.clone(values, cloneOptions));
|
|
1662
|
+
}\n`;
|
|
1663
|
+
}
|
|
1664
|
+
//#endregion
|
|
1665
|
+
//#region src/deep-clone.ts
|
|
1666
|
+
function needsCopy(property, types) {
|
|
1667
|
+
return property.range.some((uri) => uri in types || ["URL", "Uint8Array"].includes(getTypeName(uri, types)));
|
|
1668
|
+
}
|
|
1669
|
+
/** Emits module-private graph-copy support, shared by all generated classes. */
|
|
1670
|
+
function generateDeepCloneHelpers() {
|
|
1671
|
+
return `
|
|
1672
|
+
interface DeepCloneContext {
|
|
1673
|
+
value<T>(value: T): T;
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
interface DeepCloneDescriptor {
|
|
1677
|
+
create(source: object): object;
|
|
1678
|
+
copy(destination: object, context: DeepCloneContext): void;
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1681
|
+
const deepCloneDescriptors = new Map<object, DeepCloneDescriptor>();
|
|
1682
|
+
|
|
1683
|
+
function getDeepCloneDescriptor(value: object): DeepCloneDescriptor | undefined {
|
|
1684
|
+
let prototype: object | null = globalThis.Object.getPrototypeOf(value);
|
|
1685
|
+
while (prototype != null) {
|
|
1686
|
+
const descriptor = deepCloneDescriptors.get(prototype);
|
|
1687
|
+
if (descriptor != null) return descriptor;
|
|
1688
|
+
prototype = globalThis.Object.getPrototypeOf(prototype);
|
|
1689
|
+
}
|
|
1690
|
+
return undefined;
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
function deepCloneGraph<T extends object>(source: object, root: T): T {
|
|
1694
|
+
const memo = new Map<object, object>();
|
|
1695
|
+
const destinations = new Set<object>();
|
|
1696
|
+
const shells: object[] = [];
|
|
1697
|
+
let next = 0;
|
|
1698
|
+
|
|
1699
|
+
function enqueue(original: object, destination: object): void {
|
|
1700
|
+
if (destination == null || typeof destination !== "object" ||
|
|
1701
|
+
original === destination || destinations.has(destination) ||
|
|
1702
|
+
!globalThis.Object.prototype.isPrototypeOf.call(
|
|
1703
|
+
globalThis.Object.getPrototypeOf(original), destination
|
|
1704
|
+
) || getDeepCloneDescriptor(destination) == null) {
|
|
1705
|
+
throw new TypeError("clone() must return a fresh, compatible instance.");
|
|
1706
|
+
}
|
|
1707
|
+
destinations.add(destination);
|
|
1708
|
+
shells.push(destination);
|
|
1709
|
+
}
|
|
1710
|
+
|
|
1711
|
+
const context: DeepCloneContext = {
|
|
1712
|
+
value<V>(value: V): V {
|
|
1713
|
+
if (value == null || typeof value !== "object") return value;
|
|
1714
|
+
const existing = memo.get(value);
|
|
1715
|
+
if (existing != null) return existing as V;
|
|
1716
|
+
if (value instanceof URL) {
|
|
1717
|
+
const copy = new URL(value.href);
|
|
1718
|
+
memo.set(value, copy);
|
|
1719
|
+
return copy as V;
|
|
1720
|
+
}
|
|
1721
|
+
if (value instanceof Uint8Array) {
|
|
1722
|
+
const copy = new Uint8Array(value);
|
|
1723
|
+
memo.set(value, copy);
|
|
1724
|
+
return copy as V;
|
|
1725
|
+
}
|
|
1726
|
+
const descriptor = getDeepCloneDescriptor(value);
|
|
1727
|
+
// Other supported scalars (Temporal, LanguageString, Intl.Locale,
|
|
1728
|
+
// CryptoKey and class constructors) are shared as immutable values.
|
|
1729
|
+
if (descriptor == null) return value;
|
|
1730
|
+
const copy = descriptor.create(value);
|
|
1731
|
+
enqueue(value, copy);
|
|
1732
|
+
memo.set(value, copy);
|
|
1733
|
+
return copy as V;
|
|
1734
|
+
},
|
|
1735
|
+
};
|
|
1736
|
+
|
|
1737
|
+
function drain(): void {
|
|
1738
|
+
while (next < shells.length) {
|
|
1739
|
+
const shell = shells[next++];
|
|
1740
|
+
getDeepCloneDescriptor(shell)!.copy(shell, context);
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
|
|
1744
|
+
// The effective root includes replacements. References to the source root
|
|
1745
|
+
// instead receive a separate copy of its pre-update values.
|
|
1746
|
+
enqueue(source, root);
|
|
1747
|
+
drain();
|
|
1748
|
+
|
|
1749
|
+
// Discover historical root ancestry without substituting replacement values
|
|
1750
|
+
// into it. The resolver deliberately returns undefined in this first pass.
|
|
1751
|
+
const count = shells.length;
|
|
1752
|
+
for (let i = 0; i < count; i++) {
|
|
1753
|
+
retargetPortableReferrer(shells[i], (original) => {
|
|
1754
|
+
if (original === source) context.value(source);
|
|
1755
|
+
return undefined;
|
|
1756
|
+
});
|
|
1757
|
+
}
|
|
1758
|
+
drain();
|
|
1759
|
+
for (const shell of shells) {
|
|
1760
|
+
retargetPortableReferrer(shell, (original) => memo.get(original));
|
|
1761
|
+
}
|
|
1762
|
+
return root;
|
|
1763
|
+
}
|
|
1764
|
+
`;
|
|
1765
|
+
}
|
|
1766
|
+
/** Emits private-field copiers inside static blocks, keeping them out of d.ts. */
|
|
1767
|
+
async function* generateDeepCloneDescriptor(typeUri, types) {
|
|
1768
|
+
const type = types[typeUri];
|
|
1769
|
+
const properties = type.properties.filter((p) => needsCopy(p, types));
|
|
1770
|
+
if (type.extends != null && properties.length < 1) return;
|
|
1771
|
+
yield `static {\n`;
|
|
1772
|
+
if (type.extends == null) yield `deepCloneDescriptors.set(this.prototype, {
|
|
1773
|
+
create(value: object): object {
|
|
1774
|
+
const source = value as ${type.name};
|
|
1775
|
+
const options = {
|
|
1776
|
+
documentLoader: source.#documentLoader,
|
|
1777
|
+
contextLoader: source.#contextLoader,
|
|
1778
|
+
tracerProvider: source.#tracerProvider,
|
|
1779
|
+
verifyPortableObject: source.#verifyPortableObject,
|
|
1780
|
+
baseUrl: source.#_baseUrl,
|
|
1781
|
+
};
|
|
1782
|
+
return source.clone({}, options);
|
|
1783
|
+
},
|
|
1784
|
+
copy(value: object, context: DeepCloneContext): void {
|
|
1785
|
+
const node = value as ${type.name};
|
|
1786
|
+
const identified = node as { id: URL | null };
|
|
1787
|
+
identified.id = context.value(identified.id);
|
|
1788
|
+
`;
|
|
1789
|
+
else yield `const base = getDeepCloneDescriptor(this.prototype)!;
|
|
1790
|
+
deepCloneDescriptors.set(this.prototype, {
|
|
1791
|
+
create: base.create,
|
|
1792
|
+
copy(value: object, context: DeepCloneContext): void {
|
|
1793
|
+
base.copy(value, context);
|
|
1794
|
+
const node = value as ${type.name};
|
|
1795
|
+
`;
|
|
1796
|
+
for (const property of properties) {
|
|
1797
|
+
const field = await getFieldName(property.uri);
|
|
1798
|
+
yield `
|
|
1799
|
+
{
|
|
1800
|
+
const array = node.${field};
|
|
1801
|
+
const snapshot = array.length < vocabSparseArrayMinimumLength
|
|
1802
|
+
? undefined : vocabSparseArraySnapshot(array);
|
|
1803
|
+
if (snapshot != null) {
|
|
1804
|
+
for (const i of snapshot.indices) {
|
|
1805
|
+
if (!globalThis.Object.hasOwn(array, i)) continue;
|
|
1806
|
+
const value = array[i];
|
|
1807
|
+
if (value != null && typeof value === "object") {
|
|
1808
|
+
array[i] = context.value(value);
|
|
1809
|
+
}
|
|
1810
|
+
}
|
|
1811
|
+
} else {
|
|
1812
|
+
for (let i = 0; i < node.${field}.length; i++) {
|
|
1813
|
+
const value = node.${field}[i];
|
|
1814
|
+
if (value != null && typeof value === "object") {
|
|
1815
|
+
node.${field}[i] = context.value(value);
|
|
1816
|
+
}
|
|
1817
|
+
}
|
|
1818
|
+
}
|
|
1819
|
+
}
|
|
1820
|
+
`;
|
|
1821
|
+
}
|
|
1822
|
+
yield `} }); }\n`;
|
|
1580
1823
|
}
|
|
1581
1824
|
//#endregion
|
|
1582
1825
|
//#region src/inspector.ts
|
|
@@ -2083,7 +2326,8 @@ async function* generateProperty(type, property, types, moduleVarNames) {
|
|
|
2083
2326
|
}
|
|
2084
2327
|
let v = this.${await getFieldName(property.uri)}[0];
|
|
2085
2328
|
if (!(v instanceof URL) &&
|
|
2086
|
-
!this.${await getFieldName(property.uri, "#_trust")}
|
|
2329
|
+
!(0 < (this.${await getFieldName(property.uri, "#_trust")}[vocabTrustPrefix] ?? 0) ||
|
|
2330
|
+
this.${await getFieldName(property.uri, "#_trust")}.has(0))) {
|
|
2087
2331
|
if (v.id == null) {
|
|
2088
2332
|
if (isUnsecuredPortableObject(this)) {
|
|
2089
2333
|
warnUnverifiableEmbeddedObject(this, ${JSON.stringify(property.uri)});
|
|
@@ -2147,7 +2391,8 @@ async function* generateProperty(type, property, types, moduleVarNames) {
|
|
|
2147
2391
|
mustDereferencePortableObject(
|
|
2148
2392
|
this, ${trustOwner}, v.id, options,
|
|
2149
2393
|
)) &&
|
|
2150
|
-
!this.${await getFieldName(property.uri, "#_trust")}
|
|
2394
|
+
!(0 < (this.${await getFieldName(property.uri, "#_trust")}[vocabTrustPrefix] ?? 0) ||
|
|
2395
|
+
this.${await getFieldName(property.uri, "#_trust")}.has(0))) {
|
|
2151
2396
|
if (options.crossOrigin === "throw") {
|
|
2152
2397
|
throw new Error(
|
|
2153
2398
|
"The property object's @id (" + v.id.href + ") has a different " +
|
|
@@ -2236,7 +2481,8 @@ async function* generateProperty(type, property, types, moduleVarNames) {
|
|
|
2236
2481
|
for (let i = 0; i < vs.length; i++) {
|
|
2237
2482
|
let v = vs[i];
|
|
2238
2483
|
if (!(v instanceof URL) &&
|
|
2239
|
-
!this.${await getFieldName(property.uri, "#_trust")}
|
|
2484
|
+
!(i < (this.${await getFieldName(property.uri, "#_trust")}[vocabTrustPrefix] ?? 0) ||
|
|
2485
|
+
this.${await getFieldName(property.uri, "#_trust")}.has(i))) {
|
|
2240
2486
|
if (v.id == null) {
|
|
2241
2487
|
if (isUnsecuredPortableObject(this)) {
|
|
2242
2488
|
warnUnverifiableEmbeddedObject(this, ${JSON.stringify(property.uri)});
|
|
@@ -2304,7 +2550,8 @@ async function* generateProperty(type, property, types, moduleVarNames) {
|
|
|
2304
2550
|
mustDereferencePortableObject(
|
|
2305
2551
|
this, ${trustOwner}, v.id, options,
|
|
2306
2552
|
)) &&
|
|
2307
|
-
!this.${await getFieldName(property.uri, "#_trust")}
|
|
2553
|
+
!(i < (this.${await getFieldName(property.uri, "#_trust")}[vocabTrustPrefix] ?? 0) ||
|
|
2554
|
+
this.${await getFieldName(property.uri, "#_trust")}.has(i))) {
|
|
2308
2555
|
if (options.crossOrigin === "throw") {
|
|
2309
2556
|
throw new Error(
|
|
2310
2557
|
"The property object's @id (" + v.id.href + ") has a different " +
|
|
@@ -2337,6 +2584,206 @@ async function* generateProperties(typeUri, types, moduleVarNames) {
|
|
|
2337
2584
|
for (const property of type.properties) yield* generateProperty(type, property, types, moduleVarNames);
|
|
2338
2585
|
}
|
|
2339
2586
|
//#endregion
|
|
2587
|
+
//#region src/property-array.ts
|
|
2588
|
+
/** Emits module-private sparse property-array validation and copying helpers. */
|
|
2589
|
+
function generatePropertyArrayHelpers() {
|
|
2590
|
+
return `
|
|
2591
|
+
interface VocabSparseArraySnapshot {
|
|
2592
|
+
length: number;
|
|
2593
|
+
indices: number[];
|
|
2594
|
+
}
|
|
2595
|
+
|
|
2596
|
+
interface VocabArrayValidation {
|
|
2597
|
+
valid: boolean;
|
|
2598
|
+
sparse: boolean;
|
|
2599
|
+
}
|
|
2600
|
+
|
|
2601
|
+
type VocabArrayCopy<T> = { ok: true; array: T[] } | { ok: false };
|
|
2602
|
+
|
|
2603
|
+
// Most fields have no trusted prefix. Keep that metadata on the existing Set
|
|
2604
|
+
// so empty fields need neither another object nor another instance slot.
|
|
2605
|
+
const vocabTrustPrefix: unique symbol = Symbol("vocabulary trust prefix");
|
|
2606
|
+
type VocabTrustSet = Set<number> & { [vocabTrustPrefix]?: number };
|
|
2607
|
+
|
|
2608
|
+
function vocabSetTrustPrefix(trust: VocabTrustSet, prefix: number): void {
|
|
2609
|
+
if (prefix > 0) trust[vocabTrustPrefix] = prefix;
|
|
2610
|
+
}
|
|
2611
|
+
|
|
2612
|
+
function vocabCopyTrustSet(
|
|
2613
|
+
trust: VocabTrustSet,
|
|
2614
|
+
destination: VocabTrustSet,
|
|
2615
|
+
): VocabTrustSet {
|
|
2616
|
+
// clone() already constructed the destination. Reuse its empty Set only
|
|
2617
|
+
// when constructor defaults cannot contribute trust to the copied field.
|
|
2618
|
+
const copy: VocabTrustSet = trust !== destination && trust.size === 0 &&
|
|
2619
|
+
destination.size === 0 && destination[vocabTrustPrefix] == null
|
|
2620
|
+
? destination : new Set(trust);
|
|
2621
|
+
const prefix = trust[vocabTrustPrefix];
|
|
2622
|
+
if (prefix != null && prefix > 0) copy[vocabTrustPrefix] = prefix;
|
|
2623
|
+
return copy;
|
|
2624
|
+
}
|
|
2625
|
+
|
|
2626
|
+
// These thresholds select an optimization, not a limit on property arrays.
|
|
2627
|
+
const vocabSparseArrayMinimumLength = 4096;
|
|
2628
|
+
const vocabSparseArrayProbeCount = 8;
|
|
2629
|
+
const vocabSparseArrayPresentProbeLimit = 4;
|
|
2630
|
+
const vocabArrayPrototype = Array.prototype;
|
|
2631
|
+
const vocabObjectPrototype = globalThis.Object.prototype;
|
|
2632
|
+
const vocabArrayDescriptors = ["constructor", "every", "slice"].map(
|
|
2633
|
+
key => globalThis.Object.getOwnPropertyDescriptor(vocabArrayPrototype, key),
|
|
2634
|
+
);
|
|
2635
|
+
const vocabArraySpeciesDescriptor = globalThis.Object.getOwnPropertyDescriptor(
|
|
2636
|
+
Array, Symbol.species,
|
|
2637
|
+
);
|
|
2638
|
+
|
|
2639
|
+
function vocabSameArrayDescriptor(
|
|
2640
|
+
actual: PropertyDescriptor | undefined,
|
|
2641
|
+
expected: PropertyDescriptor | undefined,
|
|
2642
|
+
): boolean {
|
|
2643
|
+
return actual?.value === expected?.value && actual?.get === expected?.get &&
|
|
2644
|
+
actual?.set === expected?.set && actual?.writable === expected?.writable &&
|
|
2645
|
+
actual?.enumerable === expected?.enumerable &&
|
|
2646
|
+
actual?.configurable === expected?.configurable;
|
|
2647
|
+
}
|
|
2648
|
+
|
|
2649
|
+
function vocabArrayIndex(key: string): number | undefined {
|
|
2650
|
+
const index = Number(key);
|
|
2651
|
+
return Number.isInteger(index) && index >= 0 && index < 0xffffffff &&
|
|
2652
|
+
String(index) === key
|
|
2653
|
+
? index
|
|
2654
|
+
: undefined;
|
|
2655
|
+
}
|
|
2656
|
+
|
|
2657
|
+
function vocabSparseArraySnapshot(
|
|
2658
|
+
array: unknown[],
|
|
2659
|
+
): VocabSparseArraySnapshot | undefined {
|
|
2660
|
+
if (!Array.isArray(array)) return undefined;
|
|
2661
|
+
const length = array.length;
|
|
2662
|
+
// Small dense fields take no prototype or key-enumeration overhead.
|
|
2663
|
+
if (length < vocabSparseArrayMinimumLength) return undefined;
|
|
2664
|
+
if (!Number.isInteger(length) || length > 0xffffffff ||
|
|
2665
|
+
globalThis.Object.getPrototypeOf(array) !== vocabArrayPrototype ||
|
|
2666
|
+
globalThis.Object.getPrototypeOf(vocabArrayPrototype) !==
|
|
2667
|
+
vocabObjectPrototype ||
|
|
2668
|
+
globalThis.Object.getPrototypeOf(vocabObjectPrototype) !== null) {
|
|
2669
|
+
return undefined;
|
|
2670
|
+
}
|
|
2671
|
+
const methods = ["constructor", "every", "slice"];
|
|
2672
|
+
for (let i = 0; i < methods.length; i++) {
|
|
2673
|
+
const key = methods[i];
|
|
2674
|
+
if (globalThis.Object.hasOwn(array, key) ||
|
|
2675
|
+
!vocabSameArrayDescriptor(
|
|
2676
|
+
globalThis.Object.getOwnPropertyDescriptor(vocabArrayPrototype, key),
|
|
2677
|
+
vocabArrayDescriptors[i],
|
|
2678
|
+
)) return undefined;
|
|
2679
|
+
}
|
|
2680
|
+
if (!vocabSameArrayDescriptor(
|
|
2681
|
+
globalThis.Object.getOwnPropertyDescriptor(Array, Symbol.species),
|
|
2682
|
+
vocabArraySpeciesDescriptor,
|
|
2683
|
+
)) return undefined;
|
|
2684
|
+
|
|
2685
|
+
// Descriptor probes do not invoke element getters. Plainly dense arrays
|
|
2686
|
+
// keep native iteration without enumerating all their keys.
|
|
2687
|
+
let present = 0;
|
|
2688
|
+
for (let i = 0; i < vocabSparseArrayProbeCount; i++) {
|
|
2689
|
+
const index = Math.floor(i * (length - 1) / (vocabSparseArrayProbeCount - 1));
|
|
2690
|
+
if (globalThis.Object.getOwnPropertyDescriptor(array, index) != null &&
|
|
2691
|
+
++present >= vocabSparseArrayPresentProbeLimit) return undefined;
|
|
2692
|
+
}
|
|
2693
|
+
for (const prototype of [vocabArrayPrototype, vocabObjectPrototype]) {
|
|
2694
|
+
for (const key of globalThis.Object.getOwnPropertyNames(prototype)) {
|
|
2695
|
+
if (vocabArrayIndex(key) != null) return undefined;
|
|
2696
|
+
}
|
|
2697
|
+
}
|
|
2698
|
+
const indices: number[] = [];
|
|
2699
|
+
for (const key of globalThis.Object.getOwnPropertyNames(array)) {
|
|
2700
|
+
const index = vocabArrayIndex(key);
|
|
2701
|
+
if (index != null && index < length) indices.push(index);
|
|
2702
|
+
}
|
|
2703
|
+
if (indices.length >= length / 2) return undefined;
|
|
2704
|
+
for (const index of indices) {
|
|
2705
|
+
const descriptor = globalThis.Object.getOwnPropertyDescriptor(array, index);
|
|
2706
|
+
if (descriptor == null || !("value" in descriptor)) return undefined;
|
|
2707
|
+
}
|
|
2708
|
+
// Ordinary array keys already have canonical index order. This guard is
|
|
2709
|
+
// not a Proxy detector; reflection traps and errors remain observable.
|
|
2710
|
+
return { length, indices };
|
|
2711
|
+
}
|
|
2712
|
+
|
|
2713
|
+
function vocabValidatePropertyArray<T>(
|
|
2714
|
+
array: T[],
|
|
2715
|
+
test: (value: T) => boolean,
|
|
2716
|
+
): VocabArrayValidation {
|
|
2717
|
+
const snapshot = vocabSparseArraySnapshot(array);
|
|
2718
|
+
if (snapshot == null) return { valid: array.every(test), sparse: false };
|
|
2719
|
+
for (const index of snapshot.indices) {
|
|
2720
|
+
if (globalThis.Object.hasOwn(array, index) && !test(array[index])) {
|
|
2721
|
+
return { valid: false, sparse: true };
|
|
2722
|
+
}
|
|
2723
|
+
}
|
|
2724
|
+
return { valid: true, sparse: true };
|
|
2725
|
+
}
|
|
2726
|
+
|
|
2727
|
+
function vocabDefineArrayEntry<T>(array: T[], index: number, value: T): void {
|
|
2728
|
+
globalThis.Object.defineProperty(array, index, {
|
|
2729
|
+
value, writable: true, enumerable: true, configurable: true,
|
|
2730
|
+
});
|
|
2731
|
+
}
|
|
2732
|
+
|
|
2733
|
+
function vocabCopySparsePropertyArray<T>(
|
|
2734
|
+
array: T[],
|
|
2735
|
+
snapshot: VocabSparseArraySnapshot,
|
|
2736
|
+
test?: (value: T) => boolean,
|
|
2737
|
+
): VocabArrayCopy<T> {
|
|
2738
|
+
const copy: T[] = [];
|
|
2739
|
+
// Seed dictionary storage before low indices or the final length can
|
|
2740
|
+
// allocate storage for every hole. The temporary entry stays private.
|
|
2741
|
+
if (snapshot.indices[snapshot.indices.length - 1] !== snapshot.length - 1) {
|
|
2742
|
+
globalThis.Object.defineProperty(copy, snapshot.length - 1, {
|
|
2743
|
+
value: undefined, writable: true, enumerable: true, configurable: true,
|
|
2744
|
+
});
|
|
2745
|
+
delete copy[snapshot.length - 1];
|
|
2746
|
+
}
|
|
2747
|
+
for (const index of snapshot.indices) {
|
|
2748
|
+
if (!globalThis.Object.hasOwn(array, index)) continue;
|
|
2749
|
+
const value = array[index];
|
|
2750
|
+
if (test != null && !test(value)) return { ok: false };
|
|
2751
|
+
vocabDefineArrayEntry(copy, index, value);
|
|
2752
|
+
}
|
|
2753
|
+
// A validation callback may have deleted the last snapshot entry.
|
|
2754
|
+
if (copy.length < snapshot.length) {
|
|
2755
|
+
globalThis.Object.defineProperty(copy, snapshot.length - 1, {
|
|
2756
|
+
value: undefined, writable: true, enumerable: true, configurable: true,
|
|
2757
|
+
});
|
|
2758
|
+
delete copy[snapshot.length - 1];
|
|
2759
|
+
}
|
|
2760
|
+
copy.length = snapshot.length;
|
|
2761
|
+
return { ok: true, array: copy };
|
|
2762
|
+
}
|
|
2763
|
+
|
|
2764
|
+
function vocabCopyPropertyArray<T>(array: T[]): T[] {
|
|
2765
|
+
const snapshot = vocabSparseArraySnapshot(array);
|
|
2766
|
+
if (snapshot == null) return array.slice();
|
|
2767
|
+
const copied = vocabCopySparsePropertyArray(array, snapshot);
|
|
2768
|
+
// Without a predicate the private copy cannot fail validation.
|
|
2769
|
+
return (copied as { ok: true; array: T[] }).array;
|
|
2770
|
+
}
|
|
2771
|
+
|
|
2772
|
+
function vocabCopyValidatedPropertyArray<T>(
|
|
2773
|
+
array: T[],
|
|
2774
|
+
test: (value: T) => boolean,
|
|
2775
|
+
previous: VocabArrayValidation,
|
|
2776
|
+
): VocabArrayCopy<T> {
|
|
2777
|
+
const snapshot = vocabSparseArraySnapshot(array);
|
|
2778
|
+
if (snapshot != null) return vocabCopySparsePropertyArray(array, snapshot, test);
|
|
2779
|
+
// Sparse validation may have changed the next input's density, descriptors
|
|
2780
|
+
// or prototype. Validate that native-copy input before falling back.
|
|
2781
|
+
if (previous.sparse && !array.every(test)) return { ok: false };
|
|
2782
|
+
return { ok: true, array: array.slice() };
|
|
2783
|
+
}
|
|
2784
|
+
`;
|
|
2785
|
+
}
|
|
2786
|
+
//#endregion
|
|
2340
2787
|
//#region src/class.ts
|
|
2341
2788
|
const XSD_ANY_URI = "http://www.w3.org/2001/XMLSchema#anyURI";
|
|
2342
2789
|
const FEDIFY_URL = "fedify:url";
|
|
@@ -2364,6 +2811,7 @@ const PORTABLE_DEREFERENCE_IMPORTS = [
|
|
|
2364
2811
|
"parseCompatibleEf61Reference",
|
|
2365
2812
|
"type PortableResponseClaim",
|
|
2366
2813
|
"recordPortableReferrer",
|
|
2814
|
+
"retargetPortableReferrer",
|
|
2367
2815
|
"rejectMalformedCompatibleReference",
|
|
2368
2816
|
"warnUnverifiableEmbeddedObject"
|
|
2369
2817
|
].join(",\n ");
|
|
@@ -2505,6 +2953,8 @@ async function* generateClass(typeUri, types, moduleVarNames) {
|
|
|
2505
2953
|
for await (const code of generateFields(typeUri, types)) yield code;
|
|
2506
2954
|
for await (const code of generateConstructor(typeUri, types)) yield code;
|
|
2507
2955
|
for await (const code of generateCloner(typeUri, types)) yield code;
|
|
2956
|
+
for await (const code of generateDeepCloner(typeUri, types)) yield code.replace(/[ \t]+$/gm, "");
|
|
2957
|
+
for await (const code of generateDeepCloneDescriptor(typeUri, types)) yield code.replace(/[ \t]+$/gm, "");
|
|
2508
2958
|
for await (const code of generateProperties(typeUri, types, moduleVarNames)) yield code;
|
|
2509
2959
|
for await (const code of generateEncoder(typeUri, types)) yield code;
|
|
2510
2960
|
for await (const code of generateDecoder(typeUri, types, moduleVarNames)) yield code;
|
|
@@ -2602,6 +3052,8 @@ function inspectIri(iri: URL): string {
|
|
|
2602
3052
|
try { return formatIri(iri); } catch { return iri.href; }
|
|
2603
3053
|
}
|
|
2604
3054
|
`;
|
|
3055
|
+
yield generatePropertyArrayHelpers();
|
|
3056
|
+
yield generateDeepCloneHelpers();
|
|
2605
3057
|
yield `
|
|
2606
3058
|
function isValidLanguageTag(language: string): boolean {
|
|
2607
3059
|
try {
|