fix(javascript): honor cancellation before evidence publication (#1098)
This commit is contained in:
+78
-25
@@ -1,6 +1,10 @@
|
||||
import type { JsonValue } from "./jsonValue.js";
|
||||
|
||||
const immutableSnapshots = new WeakSet<object>();
|
||||
interface SnapshotEntry {
|
||||
readonly value: unknown;
|
||||
readonly exiting: boolean;
|
||||
}
|
||||
|
||||
/** Recognize an owned JSON snapshot sealed by this module, or an immutable scalar. */
|
||||
export const isImmutableJsonSnapshot = (value: unknown): value is JsonValue =>
|
||||
@@ -12,9 +16,7 @@ export const isImmutableJsonSnapshot = (value: unknown): value is JsonValue =>
|
||||
|
||||
/** Seal an already parsed, owned JSON snapshot for immutable zero-copy reuse. */
|
||||
export const freezeJsonSnapshot = <T extends JsonValue>(value: T): T => {
|
||||
const pending: { value: unknown; exiting: boolean }[] = [
|
||||
{ value, exiting: false },
|
||||
];
|
||||
const pending: SnapshotEntry[] = [{ value, exiting: false }];
|
||||
// These sets exist only during traversal, while the owned root already
|
||||
// retains every child. Strong sets avoid per-node ephemeron GC work.
|
||||
const visited = new Set<object>();
|
||||
@@ -33,13 +35,6 @@ export const freezeJsonSnapshot = <T extends JsonValue>(value: T): T => {
|
||||
if (isImmutableJsonSnapshot(item)) continue;
|
||||
if (typeof item !== "object" || item === null)
|
||||
throw new TypeError("Immutable JSON snapshots require JSON values");
|
||||
const prototype: unknown = Object.getPrototypeOf(item);
|
||||
if (
|
||||
!Array.isArray(item) &&
|
||||
prototype !== Object.prototype &&
|
||||
prototype !== null
|
||||
)
|
||||
throw new TypeError("Immutable JSON snapshots require ordinary objects");
|
||||
if (ancestors.has(item))
|
||||
throw new TypeError(
|
||||
"Immutable JSON snapshots cannot contain circular references",
|
||||
@@ -47,21 +42,7 @@ export const freezeJsonSnapshot = <T extends JsonValue>(value: T): T => {
|
||||
if (visited.has(item)) continue;
|
||||
ancestors.add(item);
|
||||
pending.push({ value: item, exiting: true });
|
||||
if (Array.isArray(item))
|
||||
for (let index = 0; index < item.length; index += 1)
|
||||
if (!Object.hasOwn(item, index))
|
||||
throw new TypeError(
|
||||
"Immutable JSON snapshots cannot contain sparse arrays",
|
||||
);
|
||||
for (const key of Object.getOwnPropertyNames(item)) {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(item, key);
|
||||
if (descriptor === undefined || !Object.hasOwn(descriptor, "value"))
|
||||
throw new TypeError(
|
||||
"Immutable JSON snapshots cannot contain accessors",
|
||||
);
|
||||
const child: unknown = descriptor.value;
|
||||
pending.push({ value: child, exiting: false });
|
||||
}
|
||||
pushSnapshotChildren(item, pending);
|
||||
}
|
||||
// Register only after every value is checked and every object is frozen.
|
||||
// An arbitrary Object.freeze call cannot forge this snapshot identity.
|
||||
@@ -72,3 +53,75 @@ export const freezeJsonSnapshot = <T extends JsonValue>(value: T): T => {
|
||||
immutableSnapshots.add(value);
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Seal exclusively owned JSON in bounded steps, authenticating only completion.
|
||||
* Abandonment or failure can leave the provisional input partly frozen.
|
||||
*/
|
||||
export function* freezeOwnedJsonSnapshotSteps<T extends JsonValue>(
|
||||
value: T,
|
||||
): Generator<void, T> {
|
||||
const pending: SnapshotEntry[] = [{ value, exiting: false }];
|
||||
const visited = new Set<object>();
|
||||
const ancestors = new Set<object>();
|
||||
let examined = 0;
|
||||
try {
|
||||
while (pending.length > 0) {
|
||||
if (examined++ === 4096) {
|
||||
examined = 0;
|
||||
yield;
|
||||
}
|
||||
const entry = pending.pop();
|
||||
if (entry === undefined) break;
|
||||
const item = entry.value;
|
||||
if (entry.exiting && typeof item === "object" && item !== null) {
|
||||
ancestors.delete(item);
|
||||
visited.add(item);
|
||||
continue;
|
||||
}
|
||||
if (isImmutableJsonSnapshot(item)) continue;
|
||||
if (typeof item !== "object" || item === null)
|
||||
throw new TypeError("Immutable JSON snapshots require JSON values");
|
||||
if (ancestors.has(item))
|
||||
throw new TypeError(
|
||||
"Immutable JSON snapshots cannot contain circular references",
|
||||
);
|
||||
if (visited.has(item)) continue;
|
||||
ancestors.add(item);
|
||||
pending.push({ value: item, exiting: true });
|
||||
pushSnapshotChildren(item, pending);
|
||||
// Lock every inspected edge before the scheduler can release control.
|
||||
Object.freeze(item);
|
||||
}
|
||||
if (typeof value === "object" && value !== null)
|
||||
immutableSnapshots.add(value);
|
||||
return value;
|
||||
} finally {
|
||||
pending.length = 0;
|
||||
visited.clear();
|
||||
ancestors.clear();
|
||||
}
|
||||
}
|
||||
|
||||
const pushSnapshotChildren = (item: object, pending: SnapshotEntry[]): void => {
|
||||
const prototype: unknown = Object.getPrototypeOf(item);
|
||||
if (
|
||||
!Array.isArray(item) &&
|
||||
prototype !== Object.prototype &&
|
||||
prototype !== null
|
||||
)
|
||||
throw new TypeError("Immutable JSON snapshots require ordinary objects");
|
||||
if (Array.isArray(item))
|
||||
for (let index = 0; index < item.length; index += 1)
|
||||
if (!Object.hasOwn(item, index))
|
||||
throw new TypeError(
|
||||
"Immutable JSON snapshots cannot contain sparse arrays",
|
||||
);
|
||||
for (const key of Object.getOwnPropertyNames(item)) {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(item, key);
|
||||
if (descriptor === undefined || !Object.hasOwn(descriptor, "value"))
|
||||
throw new TypeError("Immutable JSON snapshots cannot contain accessors");
|
||||
const child: unknown = descriptor.value;
|
||||
pending.push({ value: child, exiting: false });
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user