@silverprotocol/core 1.0.0-rc.1 → 1.0.0-rc.2
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/README.md +2 -2
- package/dist/agjson.d.ts +1 -1
- package/dist/agjson.js +1 -1
- package/dist/atomic-push.d.ts +15 -9
- package/dist/atomic-push.d.ts.map +1 -1
- package/dist/atomic-push.js +16 -5
- package/dist/atomic-push.js.map +1 -1
- package/dist/copy-json.js +32 -8
- package/dist/copy-json.js.map +1 -1
- package/package.json +1 -1
- package/src/agjson.ts +1 -1
- package/src/atomic-push.ts +23 -11
- package/src/copy-json.ts +31 -6
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
<p align="center">
|
|
13
13
|
<a href="https://www.npmjs.com/package/@silverprotocol/core"><img src="https://img.shields.io/npm/v/%40silverprotocol%2Fcore?color=0a7"></a>
|
|
14
14
|
<a href="https://github.com/silverprotocol/AgJSON/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue"></a>
|
|
15
|
-
<a href="https://silverprotocol.io/AgJSON"><img src="https://img.shields.io/badge/spec-1.0.0--rc.
|
|
15
|
+
<a href="https://silverprotocol.io/AgJSON"><img src="https://img.shields.io/badge/spec-1.0.0--rc.2-6ee7ff"></a>
|
|
16
16
|
</p>
|
|
17
17
|
|
|
18
18
|
---
|
|
@@ -78,7 +78,7 @@ for a gap. `raw` is for live inspection only; don't persist it. Input that is no
|
|
|
78
78
|
an event at all (not an object, no string `type`, no number `seq`) returns
|
|
79
79
|
nothing and goes to the optional `onReject` callback. `Reducer` folds the
|
|
80
80
|
validated stream into the normative snapshot; `AGJSON_VERSION` is the wire
|
|
81
|
-
version this build implements (`1.0.0-rc.
|
|
81
|
+
version this build implements (`1.0.0-rc.2`).
|
|
82
82
|
|
|
83
83
|
### Stored records and inputs
|
|
84
84
|
|
package/dist/agjson.d.ts
CHANGED
|
@@ -1353,7 +1353,7 @@ export declare const AgRunConfig: z.ZodObject<{
|
|
|
1353
1353
|
}, z.core.$strip>>;
|
|
1354
1354
|
}, z.core.$strip>;
|
|
1355
1355
|
export type AgRunConfig = z.infer<typeof AgRunConfig>;
|
|
1356
|
-
export declare const AGJSON_VERSION = "1.0.0-rc.
|
|
1356
|
+
export declare const AGJSON_VERSION = "1.0.0-rc.2";
|
|
1357
1357
|
export declare const AgInputEnvelope: z.ZodObject<{
|
|
1358
1358
|
protocol: z.ZodLiteral<"agjson">;
|
|
1359
1359
|
version: z.ZodString;
|
package/dist/agjson.js
CHANGED
|
@@ -733,7 +733,7 @@ export const AgRunConfig = z.object({
|
|
|
733
733
|
});
|
|
734
734
|
// The spec version this SDK implements (SPEC.md status line + §12). Consumers
|
|
735
735
|
// reject a MAJOR mismatch and accept any same-major version (§12 negotiation).
|
|
736
|
-
export const AGJSON_VERSION = "1.0.0-rc.
|
|
736
|
+
export const AGJSON_VERSION = "1.0.0-rc.2";
|
|
737
737
|
const agjsonWireVersion = z
|
|
738
738
|
.string()
|
|
739
739
|
.refine((v) => /^\d+\./.test(v) && v.split(".")[0] === AGJSON_VERSION.split(".")[0], {
|
package/dist/atomic-push.d.ts
CHANGED
|
@@ -18,18 +18,22 @@ export declare function normalizerErrorCode(err: unknown): string;
|
|
|
18
18
|
*/
|
|
19
19
|
export interface AtomicPushOptions {
|
|
20
20
|
/** Run {@link toJsonValueSafe} on each native before the inner push (default
|
|
21
|
-
* true). With it the
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
21
|
+
* true). With it the inner is handed a JSON deep copy of the native, taken
|
|
22
|
+
* before the inner sees it, and the journal keeps that same copy: a host
|
|
23
|
+
* mutating a pushed native later changes neither an emitted event nor a
|
|
24
|
+
* rebuild. With `false` a native that is already plain JSON is still
|
|
25
|
+
* copied the same way; any other native (a class instance, a Date, a
|
|
26
|
+
* cycle) is handed over and journaled BY REFERENCE, so the caller must pass
|
|
27
|
+
* values it owns and never mutate one after pushing it. */
|
|
27
28
|
normalize?: boolean;
|
|
28
29
|
}
|
|
29
30
|
/**
|
|
30
31
|
* Wrap a Normalizer factory so each push() (and flush()) is atomic:
|
|
31
32
|
* - Success: the inner's events are returned (by reference; unchanged until a
|
|
32
|
-
* throw has happened), and the native joins the journal.
|
|
33
|
+
* throw has happened), and the native's copy joins the journal. The inner
|
|
34
|
+
* is pushed that copy, never the host's object, so no emitted event shares
|
|
35
|
+
* an object with the native the host passed in (except a non-JSON native
|
|
36
|
+
* under `normalize: false`; see {@link AtomicPushOptions}).
|
|
33
37
|
* - Throw: the partial batch is discarded, the inner is rebuilt and re-driven
|
|
34
38
|
* from the journal with its output dropped (a throw during that re-drive is
|
|
35
39
|
* caught too), and ONE core `error {message: "normalizer error", code}`
|
|
@@ -52,8 +56,10 @@ export interface AtomicPushOptions {
|
|
|
52
56
|
* turn.abort{stream-truncated} for each open turn, innermost first.
|
|
53
57
|
*
|
|
54
58
|
* Costs, by design (caps deferred):
|
|
55
|
-
* - Memory: O(invoke). The journal keeps
|
|
56
|
-
* JSON deep copy when normalizing, never the host's reference
|
|
59
|
+
* - Memory: O(invoke). The journal keeps the COPY each accepted native was
|
|
60
|
+
* pushed as (a JSON deep copy when normalizing, never the host's reference;
|
|
61
|
+
* the inner and the journal share it, and a facet never mutates its input)
|
|
62
|
+
* for the life
|
|
57
63
|
* of the instance, i.e. one invoke, since hosts build a fresh Normalizer per
|
|
58
64
|
* invoke (§8.0 obligation 3).
|
|
59
65
|
* - Time: O(bad × prefix). Each throw re-drives the invoke so far, output
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"atomic-push.d.ts","sourceRoot":"","sources":["../src/atomic-push.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAGxD;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,qBAAqB,CAAC;AAE3D;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAOxD;AAED;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC
|
|
1
|
+
{"version":3,"file":"atomic-push.d.ts","sourceRoot":"","sources":["../src/atomic-push.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAGxD;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,qBAAqB,CAAC;AAE3D;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAOxD;AAED;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;;;;gEAO4D;IAC5D,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAgB,cAAc,CAAC,WAAW,EAAE,MAAM,UAAU,EAAE,IAAI,GAAE,iBAAsB,GAAG,UAAU,CA4ItG"}
|
package/dist/atomic-push.js
CHANGED
|
@@ -23,7 +23,10 @@ export function normalizerErrorCode(err) {
|
|
|
23
23
|
/**
|
|
24
24
|
* Wrap a Normalizer factory so each push() (and flush()) is atomic:
|
|
25
25
|
* - Success: the inner's events are returned (by reference; unchanged until a
|
|
26
|
-
* throw has happened), and the native joins the journal.
|
|
26
|
+
* throw has happened), and the native's copy joins the journal. The inner
|
|
27
|
+
* is pushed that copy, never the host's object, so no emitted event shares
|
|
28
|
+
* an object with the native the host passed in (except a non-JSON native
|
|
29
|
+
* under `normalize: false`; see {@link AtomicPushOptions}).
|
|
27
30
|
* - Throw: the partial batch is discarded, the inner is rebuilt and re-driven
|
|
28
31
|
* from the journal with its output dropped (a throw during that re-drive is
|
|
29
32
|
* caught too), and ONE core `error {message: "normalizer error", code}`
|
|
@@ -46,8 +49,10 @@ export function normalizerErrorCode(err) {
|
|
|
46
49
|
* turn.abort{stream-truncated} for each open turn, innermost first.
|
|
47
50
|
*
|
|
48
51
|
* Costs, by design (caps deferred):
|
|
49
|
-
* - Memory: O(invoke). The journal keeps
|
|
50
|
-
* JSON deep copy when normalizing, never the host's reference
|
|
52
|
+
* - Memory: O(invoke). The journal keeps the COPY each accepted native was
|
|
53
|
+
* pushed as (a JSON deep copy when normalizing, never the host's reference;
|
|
54
|
+
* the inner and the journal share it, and a facet never mutates its input)
|
|
55
|
+
* for the life
|
|
51
56
|
* of the instance, i.e. one invoke, since hosts build a fresh Normalizer per
|
|
52
57
|
* invoke (§8.0 obligation 3).
|
|
53
58
|
* - Time: O(bad × prefix). Each throw re-drives the invoke so far, output
|
|
@@ -166,8 +171,14 @@ export function withAtomicPush(createInner, opts = {}) {
|
|
|
166
171
|
push(native) {
|
|
167
172
|
try {
|
|
168
173
|
const n = normalize ? toJsonValueSafe(native) : native;
|
|
169
|
-
|
|
170
|
-
|
|
174
|
+
// One copy, taken BEFORE the inner sees it: toJsonValueSafe hands plain
|
|
175
|
+
// JSON back as the host's own object, so without it an event the inner
|
|
176
|
+
// builds could share a node with the native, and a host mutating its
|
|
177
|
+
// native after push() would change an event already delivered. The
|
|
178
|
+
// journal keeps the same copy for a rebuild.
|
|
179
|
+
const owned = normalize || isJsonValue(n) ? toJsonValue(n) : n;
|
|
180
|
+
const evs = inner.push(owned);
|
|
181
|
+
journal.push(owned);
|
|
171
182
|
return deliver(evs);
|
|
172
183
|
}
|
|
173
184
|
catch (err) {
|
package/dist/atomic-push.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"atomic-push.js","sourceRoot":"","sources":["../src/atomic-push.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAEtE;;;;GAIG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,kBAAkB,CAAC;AAE3D;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAY;IAC9C,IAAI,CAAC;QACH,IAAI,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CAAC,CAAC;YAChC,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,OAAO,EAAE,CAAC;gBACV,MAAM,GAAG,GAAc,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;gBACzC,KAAK,MAAM,EAAE,IAAI,YAAY,CAAC,IAAI,EAAE;oBAAE,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,GAAG,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC;gBAC5F,KAAK,MAAM,MAAM,IAAI,CAAC,GAAG,SAAS,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC;oBAC9C,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,YAAY,EAAE,GAAG,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,kBAAkB,EAAE,CAAC,CAAC;gBACvF,CAAC;gBACD,YAAY,CAAC,KAAK,EAAE,CAAC;gBACrB,SAAS,CAAC,KAAK,EAAE,CAAC;gBAClB,OAAO,GAAG,CAAC;YACb,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/copy-json.js
CHANGED
|
@@ -91,25 +91,49 @@ export function deleteOwnProtoMember(v) {
|
|
|
91
91
|
export function withoutOwnProtoMember(v) {
|
|
92
92
|
return (hasOwnProtoMember(v) ? rebuild(v) : v);
|
|
93
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* The plain nodes the fast pass visits before it hands the value to the pass
|
|
96
|
+
* that remembers each node. An emitted event is a tree, so remembering costs
|
|
97
|
+
* it a WeakSet entry per node for nothing; a value that shares nodes or holds a
|
|
98
|
+
* cycle can make the fast pass revisit them without end, and the budget turns
|
|
99
|
+
* that into the remembering pass. The fold side's scan (depth.ts) works the
|
|
100
|
+
* same way.
|
|
101
|
+
*/
|
|
102
|
+
const PROTO_SCAN_NODE_BUDGET = 50_000;
|
|
94
103
|
function hasOwnProtoMember(v) {
|
|
95
|
-
|
|
104
|
+
return protoScan(v, false) ?? protoScan(v, true);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* One iterative pass over the arrays and plain (or null-prototype) objects of
|
|
108
|
+
* `v`: whether any such object holds an own `__proto__` member. With
|
|
109
|
+
* `remember`, each node is visited once; without, it returns undefined once it
|
|
110
|
+
* has visited PROTO_SCAN_NODE_BUDGET nodes.
|
|
111
|
+
*/
|
|
112
|
+
function protoScan(v, remember) {
|
|
113
|
+
const seen = remember ? new WeakSet() : undefined;
|
|
114
|
+
let visits = 0;
|
|
96
115
|
const stack = [v];
|
|
97
116
|
while (stack.length > 0) {
|
|
98
117
|
const x = stack.pop();
|
|
99
|
-
if (x === null || typeof x !== "object" ||
|
|
118
|
+
if (x === null || typeof x !== "object" || !isPlainNode(x))
|
|
100
119
|
continue;
|
|
101
|
-
seen
|
|
120
|
+
if (seen !== undefined) {
|
|
121
|
+
if (seen.has(x))
|
|
122
|
+
continue;
|
|
123
|
+
seen.add(x);
|
|
124
|
+
}
|
|
125
|
+
else if (++visits > PROTO_SCAN_NODE_BUDGET) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
102
128
|
if (Array.isArray(x)) {
|
|
103
129
|
for (let i = 0; i < x.length; i++)
|
|
104
130
|
stack.push(x[i]);
|
|
105
131
|
continue;
|
|
106
132
|
}
|
|
107
|
-
|
|
108
|
-
if (Object.hasOwn(o, PROTO))
|
|
133
|
+
if (Object.hasOwn(x, PROTO))
|
|
109
134
|
return true;
|
|
110
|
-
for (const k
|
|
111
|
-
|
|
112
|
-
stack.push(o[k]);
|
|
135
|
+
for (const k of Object.keys(x))
|
|
136
|
+
stack.push(x[k]);
|
|
113
137
|
}
|
|
114
138
|
return false;
|
|
115
139
|
}
|
package/dist/copy-json.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"copy-json.js","sourceRoot":"","sources":["../src/copy-json.ts"],"names":[],"mappings":"AAYA;;;;;;;;GAQG;AACH,MAAM,UAA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C,CAAC,CAAC,CAAC,CAAC,CAAC;YAClF,SAAS;QACX,CAAC;QACD,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;YAC/B,IAAI,CAAC,KAAK,KAAK;gBAAE,SAAS;YAC1B,MAAM,CAAC,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE;gBAC1B,KAAK,EAAE,MAAM,CAAE,CAA6B,CAAC,CAAC,CAAC,CAAC;gBAChD,UAAU,EAAE,IAAI;gBAChB,QAAQ,EAAE,IAAI;gBACd,YAAY,EAAE,IAAI;aACnB,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/package.json
CHANGED
package/src/agjson.ts
CHANGED
|
@@ -949,7 +949,7 @@ export type AgRunConfig = z.infer<typeof AgRunConfig>;
|
|
|
949
949
|
|
|
950
950
|
// The spec version this SDK implements (SPEC.md status line + §12). Consumers
|
|
951
951
|
// reject a MAJOR mismatch and accept any same-major version (§12 negotiation).
|
|
952
|
-
export const AGJSON_VERSION = "1.0.0-rc.
|
|
952
|
+
export const AGJSON_VERSION = "1.0.0-rc.2";
|
|
953
953
|
|
|
954
954
|
const agjsonWireVersion = z
|
|
955
955
|
.string()
|
package/src/atomic-push.ts
CHANGED
|
@@ -50,19 +50,23 @@ export function normalizerErrorCode(err: unknown): string {
|
|
|
50
50
|
*/
|
|
51
51
|
export interface AtomicPushOptions {
|
|
52
52
|
/** Run {@link toJsonValueSafe} on each native before the inner push (default
|
|
53
|
-
* true). With it the
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
53
|
+
* true). With it the inner is handed a JSON deep copy of the native, taken
|
|
54
|
+
* before the inner sees it, and the journal keeps that same copy: a host
|
|
55
|
+
* mutating a pushed native later changes neither an emitted event nor a
|
|
56
|
+
* rebuild. With `false` a native that is already plain JSON is still
|
|
57
|
+
* copied the same way; any other native (a class instance, a Date, a
|
|
58
|
+
* cycle) is handed over and journaled BY REFERENCE, so the caller must pass
|
|
59
|
+
* values it owns and never mutate one after pushing it. */
|
|
59
60
|
normalize?: boolean;
|
|
60
61
|
}
|
|
61
62
|
|
|
62
63
|
/**
|
|
63
64
|
* Wrap a Normalizer factory so each push() (and flush()) is atomic:
|
|
64
65
|
* - Success: the inner's events are returned (by reference; unchanged until a
|
|
65
|
-
* throw has happened), and the native joins the journal.
|
|
66
|
+
* throw has happened), and the native's copy joins the journal. The inner
|
|
67
|
+
* is pushed that copy, never the host's object, so no emitted event shares
|
|
68
|
+
* an object with the native the host passed in (except a non-JSON native
|
|
69
|
+
* under `normalize: false`; see {@link AtomicPushOptions}).
|
|
66
70
|
* - Throw: the partial batch is discarded, the inner is rebuilt and re-driven
|
|
67
71
|
* from the journal with its output dropped (a throw during that re-drive is
|
|
68
72
|
* caught too), and ONE core `error {message: "normalizer error", code}`
|
|
@@ -85,8 +89,10 @@ export interface AtomicPushOptions {
|
|
|
85
89
|
* turn.abort{stream-truncated} for each open turn, innermost first.
|
|
86
90
|
*
|
|
87
91
|
* Costs, by design (caps deferred):
|
|
88
|
-
* - Memory: O(invoke). The journal keeps
|
|
89
|
-
* JSON deep copy when normalizing, never the host's reference
|
|
92
|
+
* - Memory: O(invoke). The journal keeps the COPY each accepted native was
|
|
93
|
+
* pushed as (a JSON deep copy when normalizing, never the host's reference;
|
|
94
|
+
* the inner and the journal share it, and a facet never mutates its input)
|
|
95
|
+
* for the life
|
|
90
96
|
* of the instance, i.e. one invoke, since hosts build a fresh Normalizer per
|
|
91
97
|
* invoke (§8.0 obligation 3).
|
|
92
98
|
* - Time: O(bad × prefix). Each throw re-drives the invoke so far, output
|
|
@@ -204,8 +210,14 @@ export function withAtomicPush(createInner: () => Normalizer, opts: AtomicPushOp
|
|
|
204
210
|
push(native: unknown): AgEvent[] {
|
|
205
211
|
try {
|
|
206
212
|
const n: unknown = normalize ? toJsonValueSafe(native) : native;
|
|
207
|
-
|
|
208
|
-
|
|
213
|
+
// One copy, taken BEFORE the inner sees it: toJsonValueSafe hands plain
|
|
214
|
+
// JSON back as the host's own object, so without it an event the inner
|
|
215
|
+
// builds could share a node with the native, and a host mutating its
|
|
216
|
+
// native after push() would change an event already delivered. The
|
|
217
|
+
// journal keeps the same copy for a rebuild.
|
|
218
|
+
const owned: unknown = normalize || isJsonValue(n) ? toJsonValue(n as JsonValue) : n;
|
|
219
|
+
const evs = inner.push(owned);
|
|
220
|
+
journal.push(owned);
|
|
209
221
|
return deliver(evs);
|
|
210
222
|
} catch (err) {
|
|
211
223
|
rebuild();
|
package/src/copy-json.ts
CHANGED
|
@@ -100,20 +100,45 @@ export function withoutOwnProtoMember<T>(v: T): T {
|
|
|
100
100
|
return (hasOwnProtoMember(v) ? rebuild(v) : v) as T;
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
+
/**
|
|
104
|
+
* The plain nodes the fast pass visits before it hands the value to the pass
|
|
105
|
+
* that remembers each node. An emitted event is a tree, so remembering costs
|
|
106
|
+
* it a WeakSet entry per node for nothing; a value that shares nodes or holds a
|
|
107
|
+
* cycle can make the fast pass revisit them without end, and the budget turns
|
|
108
|
+
* that into the remembering pass. The fold side's scan (depth.ts) works the
|
|
109
|
+
* same way.
|
|
110
|
+
*/
|
|
111
|
+
const PROTO_SCAN_NODE_BUDGET = 50_000;
|
|
112
|
+
|
|
103
113
|
function hasOwnProtoMember(v: unknown): boolean {
|
|
104
|
-
|
|
114
|
+
return protoScan(v, false) ?? protoScan(v, true)!;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* One iterative pass over the arrays and plain (or null-prototype) objects of
|
|
119
|
+
* `v`: whether any such object holds an own `__proto__` member. With
|
|
120
|
+
* `remember`, each node is visited once; without, it returns undefined once it
|
|
121
|
+
* has visited PROTO_SCAN_NODE_BUDGET nodes.
|
|
122
|
+
*/
|
|
123
|
+
function protoScan(v: unknown, remember: boolean): boolean | undefined {
|
|
124
|
+
const seen = remember ? new WeakSet<object>() : undefined;
|
|
125
|
+
let visits = 0;
|
|
105
126
|
const stack: unknown[] = [v];
|
|
106
127
|
while (stack.length > 0) {
|
|
107
128
|
const x = stack.pop();
|
|
108
|
-
if (x === null || typeof x !== "object" ||
|
|
109
|
-
seen
|
|
129
|
+
if (x === null || typeof x !== "object" || !isPlainNode(x)) continue;
|
|
130
|
+
if (seen !== undefined) {
|
|
131
|
+
if (seen.has(x)) continue;
|
|
132
|
+
seen.add(x);
|
|
133
|
+
} else if (++visits > PROTO_SCAN_NODE_BUDGET) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
110
136
|
if (Array.isArray(x)) {
|
|
111
137
|
for (let i = 0; i < x.length; i++) stack.push(x[i]);
|
|
112
138
|
continue;
|
|
113
139
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
for (const k in o) if (Object.hasOwn(o, k)) stack.push(o[k]);
|
|
140
|
+
if (Object.hasOwn(x, PROTO)) return true;
|
|
141
|
+
for (const k of Object.keys(x)) stack.push((x as Record<string, unknown>)[k]);
|
|
117
142
|
}
|
|
118
143
|
return false;
|
|
119
144
|
}
|