@orkestrel/contract 0.0.8 → 0.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/dist/src/core/index.cjs +217 -0
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +70 -6
- package/dist/src/core/index.d.ts +70 -6
- package/dist/src/core/index.js +216 -1
- package/dist/src/core/index.js.map +1 -1
- package/package.json +4 -3
package/dist/src/core/index.cjs
CHANGED
|
@@ -1475,6 +1475,221 @@ function shapeToKind(shape) {
|
|
|
1475
1475
|
//#endregion
|
|
1476
1476
|
//#region src/core/cloners.ts
|
|
1477
1477
|
/**
|
|
1478
|
+
* Deep-clone exact JSON data into an owned frozen snapshot.
|
|
1479
|
+
*
|
|
1480
|
+
* @remarks
|
|
1481
|
+
* Traverses iteratively so deeply nested input cannot exhaust the call stack.
|
|
1482
|
+
* Repeated noncyclic aliases are duplicated because JSON persistence represents
|
|
1483
|
+
* a tree, while a structural back-edge on the active path is rejected as a
|
|
1484
|
+
* cycle. Arrays are rebuilt as standard dense arrays and records as
|
|
1485
|
+
* null-prototype objects; every produced node is frozen after its children are
|
|
1486
|
+
* wired. Array keys must be exactly `length` plus every canonical index.
|
|
1487
|
+
* Writable and configurable index flags are normalized rather than treated as
|
|
1488
|
+
* JSON data, so frozen arrays remain valid. Property descriptors are inspected
|
|
1489
|
+
* without reading values through accessors, and every hostile reflective
|
|
1490
|
+
* operation is contained before a new clone-coded error is exposed.
|
|
1491
|
+
*
|
|
1492
|
+
* @param value - The unknown value to validate and snapshot
|
|
1493
|
+
* @returns The primitive unchanged, or a deeply cloned and frozen JSON graph
|
|
1494
|
+
* @throws {ContractError} When the value is not exact acyclic JSON data or traversal fails
|
|
1495
|
+
*
|
|
1496
|
+
* @example
|
|
1497
|
+
* ```ts
|
|
1498
|
+
* const source = { settings: { enabled: true } }
|
|
1499
|
+
* const clone = cloneJSONValue(source)
|
|
1500
|
+
* source.settings.enabled = false
|
|
1501
|
+
* clone // { settings: { enabled: true } }
|
|
1502
|
+
* ```
|
|
1503
|
+
*/
|
|
1504
|
+
function cloneJSONValue(value) {
|
|
1505
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return value;
|
|
1506
|
+
if (typeof value === "number") {
|
|
1507
|
+
if (Number.isFinite(value)) return value;
|
|
1508
|
+
throw new ContractError("cloneJSONValue: number is not finite", {
|
|
1509
|
+
code: "clone",
|
|
1510
|
+
context: { shape: "json" }
|
|
1511
|
+
});
|
|
1512
|
+
}
|
|
1513
|
+
if (typeof value !== "object") throw new ContractError("cloneJSONValue: value is not JSON data", {
|
|
1514
|
+
code: "clone",
|
|
1515
|
+
context: { shape: "json" }
|
|
1516
|
+
});
|
|
1517
|
+
const arrayOutcome = attempt(() => Array.isArray(value));
|
|
1518
|
+
if (!arrayOutcome.success) throw new ContractError("cloneJSONValue: value brand could not be inspected", {
|
|
1519
|
+
code: "clone",
|
|
1520
|
+
context: { shape: "json" }
|
|
1521
|
+
});
|
|
1522
|
+
let root;
|
|
1523
|
+
if (arrayOutcome.value) root = [];
|
|
1524
|
+
else if (isRecord(value)) root = Object.create(null);
|
|
1525
|
+
else throw new ContractError("cloneJSONValue: object is not a plain record", {
|
|
1526
|
+
code: "clone",
|
|
1527
|
+
context: { shape: "json" }
|
|
1528
|
+
});
|
|
1529
|
+
const active = /* @__PURE__ */ new WeakSet();
|
|
1530
|
+
active.add(value);
|
|
1531
|
+
const pending = [{
|
|
1532
|
+
source: value,
|
|
1533
|
+
clone: root,
|
|
1534
|
+
array: arrayOutcome.value,
|
|
1535
|
+
entries: void 0,
|
|
1536
|
+
index: 0
|
|
1537
|
+
}];
|
|
1538
|
+
while (pending.length > 0) {
|
|
1539
|
+
const frame = pending[pending.length - 1];
|
|
1540
|
+
if (frame === void 0) continue;
|
|
1541
|
+
if (frame.entries === void 0) {
|
|
1542
|
+
const keysOutcome = attempt(() => Reflect.ownKeys(frame.source));
|
|
1543
|
+
if (!keysOutcome.success) throw new ContractError("cloneJSONValue: own keys could not be inspected", {
|
|
1544
|
+
code: "clone",
|
|
1545
|
+
context: { shape: "json" }
|
|
1546
|
+
});
|
|
1547
|
+
const keys = keysOutcome.value;
|
|
1548
|
+
const entries = [];
|
|
1549
|
+
if (frame.array) {
|
|
1550
|
+
const lengthOutcome = attempt(() => Reflect.getOwnPropertyDescriptor(frame.source, "length"));
|
|
1551
|
+
if (!lengthOutcome.success) throw new ContractError("cloneJSONValue: array length could not be inspected", {
|
|
1552
|
+
code: "clone",
|
|
1553
|
+
context: { shape: "json" }
|
|
1554
|
+
});
|
|
1555
|
+
const lengthDescriptor = lengthOutcome.value;
|
|
1556
|
+
if (lengthDescriptor === void 0 || !("value" in lengthDescriptor) || typeof lengthDescriptor.value !== "number" || !Number.isInteger(lengthDescriptor.value) || lengthDescriptor.value < 0 || lengthDescriptor.value > 4294967295 || lengthDescriptor.enumerable !== false || lengthDescriptor.configurable !== false) throw new ContractError("cloneJSONValue: array is not intrinsic and dense", {
|
|
1557
|
+
code: "clone",
|
|
1558
|
+
context: { shape: "json" }
|
|
1559
|
+
});
|
|
1560
|
+
const remaining = new Set(keys);
|
|
1561
|
+
if (!remaining.delete("length")) throw new ContractError("cloneJSONValue: array own keys are not exact", {
|
|
1562
|
+
code: "clone",
|
|
1563
|
+
context: { shape: "json" }
|
|
1564
|
+
});
|
|
1565
|
+
for (let index = 0; index < lengthDescriptor.value; index += 1) {
|
|
1566
|
+
const key = String(index);
|
|
1567
|
+
if (!remaining.delete(key)) throw new ContractError("cloneJSONValue: array own keys are not exact", {
|
|
1568
|
+
code: "clone",
|
|
1569
|
+
context: { shape: "json" }
|
|
1570
|
+
});
|
|
1571
|
+
const descriptorOutcome = attempt(() => Reflect.getOwnPropertyDescriptor(frame.source, key));
|
|
1572
|
+
if (!descriptorOutcome.success) throw new ContractError("cloneJSONValue: array index could not be inspected", {
|
|
1573
|
+
code: "clone",
|
|
1574
|
+
context: { shape: "json" }
|
|
1575
|
+
});
|
|
1576
|
+
const descriptor = descriptorOutcome.value;
|
|
1577
|
+
if (descriptor === void 0 || !("value" in descriptor) || descriptor.enumerable !== true) throw new ContractError("cloneJSONValue: array index is not enumerable data", {
|
|
1578
|
+
code: "clone",
|
|
1579
|
+
context: { shape: "json" }
|
|
1580
|
+
});
|
|
1581
|
+
entries.push([key, descriptor.value]);
|
|
1582
|
+
}
|
|
1583
|
+
if (remaining.size !== 0) throw new ContractError("cloneJSONValue: array own keys are not exact", {
|
|
1584
|
+
code: "clone",
|
|
1585
|
+
context: { shape: "json" }
|
|
1586
|
+
});
|
|
1587
|
+
} else for (const key of keys) {
|
|
1588
|
+
if (typeof key !== "string") throw new ContractError("cloneJSONValue: record has a symbol property", {
|
|
1589
|
+
code: "clone",
|
|
1590
|
+
context: { shape: "json" }
|
|
1591
|
+
});
|
|
1592
|
+
const descriptorOutcome = attempt(() => Reflect.getOwnPropertyDescriptor(frame.source, key));
|
|
1593
|
+
if (!descriptorOutcome.success) throw new ContractError("cloneJSONValue: record property could not be inspected", {
|
|
1594
|
+
code: "clone",
|
|
1595
|
+
context: { shape: "json" }
|
|
1596
|
+
});
|
|
1597
|
+
const descriptor = descriptorOutcome.value;
|
|
1598
|
+
if (descriptor === void 0 || !("value" in descriptor) || descriptor.enumerable !== true) throw new ContractError("cloneJSONValue: record property is not enumerable data", {
|
|
1599
|
+
code: "clone",
|
|
1600
|
+
context: { shape: "json" }
|
|
1601
|
+
});
|
|
1602
|
+
entries.push([key, descriptor.value]);
|
|
1603
|
+
}
|
|
1604
|
+
frame.entries = entries;
|
|
1605
|
+
}
|
|
1606
|
+
const entry = frame.entries[frame.index];
|
|
1607
|
+
if (entry === void 0) {
|
|
1608
|
+
Object.freeze(frame.clone);
|
|
1609
|
+
active.delete(frame.source);
|
|
1610
|
+
pending.pop();
|
|
1611
|
+
continue;
|
|
1612
|
+
}
|
|
1613
|
+
frame.index += 1;
|
|
1614
|
+
const key = entry[0];
|
|
1615
|
+
const source = entry[1];
|
|
1616
|
+
let clone;
|
|
1617
|
+
if (source === null || typeof source === "string" || typeof source === "boolean") clone = source;
|
|
1618
|
+
else if (typeof source === "number") {
|
|
1619
|
+
if (!Number.isFinite(source)) throw new ContractError("cloneJSONValue: number is not finite", {
|
|
1620
|
+
code: "clone",
|
|
1621
|
+
context: { shape: "json" }
|
|
1622
|
+
});
|
|
1623
|
+
clone = source;
|
|
1624
|
+
} else if (typeof source === "object") {
|
|
1625
|
+
if (active.has(source)) throw new ContractError("cloneJSONValue: cycle detected", {
|
|
1626
|
+
code: "clone",
|
|
1627
|
+
context: { shape: "json" }
|
|
1628
|
+
});
|
|
1629
|
+
const childArrayOutcome = attempt(() => Array.isArray(source));
|
|
1630
|
+
if (!childArrayOutcome.success) throw new ContractError("cloneJSONValue: value brand could not be inspected", {
|
|
1631
|
+
code: "clone",
|
|
1632
|
+
context: { shape: "json" }
|
|
1633
|
+
});
|
|
1634
|
+
let child;
|
|
1635
|
+
if (childArrayOutcome.value) child = [];
|
|
1636
|
+
else if (isRecord(source)) child = Object.create(null);
|
|
1637
|
+
else throw new ContractError("cloneJSONValue: object is not a plain record", {
|
|
1638
|
+
code: "clone",
|
|
1639
|
+
context: { shape: "json" }
|
|
1640
|
+
});
|
|
1641
|
+
clone = child;
|
|
1642
|
+
active.add(source);
|
|
1643
|
+
pending.push({
|
|
1644
|
+
source,
|
|
1645
|
+
clone: child,
|
|
1646
|
+
array: childArrayOutcome.value,
|
|
1647
|
+
entries: void 0,
|
|
1648
|
+
index: 0
|
|
1649
|
+
});
|
|
1650
|
+
} else throw new ContractError("cloneJSONValue: property is not JSON data", {
|
|
1651
|
+
code: "clone",
|
|
1652
|
+
context: { shape: "json" }
|
|
1653
|
+
});
|
|
1654
|
+
Object.defineProperty(frame.clone, key, {
|
|
1655
|
+
value: clone,
|
|
1656
|
+
enumerable: true,
|
|
1657
|
+
configurable: true,
|
|
1658
|
+
writable: true
|
|
1659
|
+
});
|
|
1660
|
+
}
|
|
1661
|
+
return root;
|
|
1662
|
+
}
|
|
1663
|
+
/**
|
|
1664
|
+
* Deep-clone an exact JSON object record into an owned frozen snapshot.
|
|
1665
|
+
*
|
|
1666
|
+
* @remarks
|
|
1667
|
+
* Adds a record-root boundary to {@link cloneJSONValue}. The output is a
|
|
1668
|
+
* deeply frozen null-prototype record, and repeated noncyclic aliases are
|
|
1669
|
+
* duplicated as independent JSON tree branches.
|
|
1670
|
+
*
|
|
1671
|
+
* @param value - The unknown record value to validate and snapshot
|
|
1672
|
+
* @returns A deeply cloned and frozen JSON record
|
|
1673
|
+
* @throws {ContractError} When the root is not a record, nested data is inexact, or traversal fails
|
|
1674
|
+
*
|
|
1675
|
+
* @example
|
|
1676
|
+
* ```ts
|
|
1677
|
+
* cloneJSONRecord({ attempt: 1 }) // frozen null-prototype record
|
|
1678
|
+
* ```
|
|
1679
|
+
*/
|
|
1680
|
+
function cloneJSONRecord(value) {
|
|
1681
|
+
if (!isRecord(value)) throw new ContractError("cloneJSONRecord: value is not a plain record", {
|
|
1682
|
+
code: "clone",
|
|
1683
|
+
context: { shape: "json" }
|
|
1684
|
+
});
|
|
1685
|
+
const clone = cloneJSONValue(value);
|
|
1686
|
+
if (!isRecord(clone)) throw new ContractError("cloneJSONRecord: cloned value is not a record", {
|
|
1687
|
+
code: "clone",
|
|
1688
|
+
context: { shape: "json" }
|
|
1689
|
+
});
|
|
1690
|
+
return clone;
|
|
1691
|
+
}
|
|
1692
|
+
/**
|
|
1478
1693
|
* Deep-clone a JSON Schema graph into an owned frozen snapshot.
|
|
1479
1694
|
*
|
|
1480
1695
|
* @remarks
|
|
@@ -5049,6 +5264,8 @@ exports.buildObjectShape = buildObjectShape;
|
|
|
5049
5264
|
exports.buildShapeFromNode = buildShapeFromNode;
|
|
5050
5265
|
exports.canonicalStringify = canonicalStringify;
|
|
5051
5266
|
exports.canonicalizeValue = canonicalizeValue;
|
|
5267
|
+
exports.cloneJSONRecord = cloneJSONRecord;
|
|
5268
|
+
exports.cloneJSONValue = cloneJSONValue;
|
|
5052
5269
|
exports.cloneSchema = cloneSchema;
|
|
5053
5270
|
exports.cloneShape = cloneShape;
|
|
5054
5271
|
exports.compileGenerator = compileGenerator;
|