fix(session): traverse JSON values iteratively

This commit is contained in:
Tianyi Cui
2026-07-22 01:58:38 +08:00
parent 1f7e1765a5
commit 0d851adc3e
8 changed files with 161 additions and 112 deletions

View File

@@ -80,6 +80,19 @@ describe('snapshotJsonValue', () => {
expect(arrayReads).toBe(1)
})
it('accepts deeply nested valid JSON without using the JavaScript call stack', () => {
let value: JsonValue = 'leaf'
for (let depth = 0; depth < 5_000; depth++) value = [value]
expect(isJsonValue(value)).toBe(true)
let cursor: JsonValue | undefined = snapshotJsonValue(value)
for (let depth = 0; depth < 5_000; depth++) {
expect(Array.isArray(cursor)).toBe(true)
cursor = Array.isArray(cursor) ? cursor[0] : undefined
}
expect(cursor).toBe('leaf')
})
it('rejects exotic containers, sparse or decorated arrays, cycles, and invalid children', () => {
class ExoticObject {
readonly value = 1