fix(code-runtime): accept deeply nested JSON

This commit is contained in:
Tianyi Cui
2026-07-22 02:13:37 +08:00
parent 3f93d4ddbd
commit f2d86b232e
7 changed files with 119 additions and 44 deletions
@@ -40,71 +40,114 @@ function enumerableStringKeys(value: object): string[] | undefined {
if (keys.some(key => typeof key !== 'string' || !Object.prototype.propertyIsEnumerable.call(value, key))) return undefined
return keys as string[]
}
/* jscpd:ignore-end */
type SnapshotDestination =
| { kind: 'root' }
| { kind: 'array'; target: CodeJsonValue[]; index: number }
| { kind: 'object'; target: Record<string, CodeJsonValue>; key: string }
type SnapshotTask =
| { kind: 'visit'; value: unknown; destination: SnapshotDestination }
| { kind: 'array-item'; source: unknown[]; index: number; target: CodeJsonValue[] }
| { kind: 'object-property'; source: Record<string, unknown>; key: string; target: Record<string, CodeJsonValue> }
| { kind: 'leave'; source: object }
/**
* Validate and detach one worker-boundary value without loading another
* workspace package at runtime. This mirrors the session-owned canonical
* JSON boundary while remaining safe to import from the unbuilt worker.
* Its iterative traversal adds no JavaScript call-stack depth limit.
*
* @param value - the candidate completion value.
* @returns a detached lossless-JSON snapshot, or `undefined` when invalid.
*/
export function snapshotCodeJsonValue(value: unknown): CodeJsonValue | undefined {
const active = new Set<object>()
const within = <T extends CodeJsonValue>(source: object, build: () => T | undefined): T | undefined => {
if (active.has(source)) return undefined
active.add(source)
try {
return build()
} finally {
active.delete(source)
let root: CodeJsonValue | undefined
const assign = (destination: SnapshotDestination, item: CodeJsonValue): void => {
if (destination.kind === 'root') {
root = item
} else if (destination.kind === 'array') {
destination.target[destination.index] = item
} else {
Object.defineProperty(destination.target, destination.key, {
value: item,
enumerable: true,
configurable: true,
writable: true,
})
}
}
const copy = (candidate: unknown): CodeJsonValue | undefined => {
if (candidate === null) return null
if (typeof candidate === 'boolean' || typeof candidate === 'string') return candidate
const tasks: SnapshotTask[] = [{ kind: 'visit', value, destination: { kind: 'root' } }]
for (let task = tasks.pop(); task !== undefined; task = tasks.pop()) {
if (task.kind === 'leave') {
active.delete(task.source)
continue
}
if (task.kind === 'array-item') {
if (!Object.hasOwn(task.source, task.index)) return undefined
tasks.push({
kind: 'visit',
value: task.source[task.index],
destination: { kind: 'array', target: task.target, index: task.index },
})
continue
}
if (task.kind === 'object-property') {
tasks.push({
kind: 'visit',
value: task.source[task.key],
destination: { kind: 'object', target: task.target, key: task.key },
})
continue
}
const candidate = task.value
if (candidate === null) {
assign(task.destination, null)
continue
}
if (typeof candidate === 'boolean' || typeof candidate === 'string') {
assign(task.destination, candidate)
continue
}
if (typeof candidate === 'number') {
return Number.isFinite(candidate) && !Object.is(candidate, -0) ? candidate : undefined
if (!Number.isFinite(candidate) || Object.is(candidate, -0)) return undefined
assign(task.destination, candidate)
continue
}
if (typeof candidate !== 'object') return undefined
if (active.has(candidate)) return undefined
if (Array.isArray(candidate)) {
if (!hasPlainArrayPrototype(candidate)) return undefined
const length = candidate.length
if (Reflect.ownKeys(candidate).length !== length + 1) return undefined
return within(candidate, () => {
const result: CodeJsonValue[] = []
for (let index = 0; index < length; index++) {
if (!Object.hasOwn(candidate, index)) return undefined
const item = copy(candidate[index])
if (item === undefined) return undefined
result.push(item)
}
return result
})
const target: CodeJsonValue[] = []
assign(task.destination, target)
active.add(candidate)
tasks.push({ kind: 'leave', source: candidate })
for (let index = length - 1; index >= 0; index--) {
tasks.push({ kind: 'array-item', source: candidate, index, target })
}
continue
}
if (!hasPlainObjectPrototype(candidate)) return undefined
const keys = enumerableStringKeys(candidate)
if (keys === undefined) return undefined
return within(candidate, () => {
const result: Record<string, CodeJsonValue> = {}
for (const key of keys) {
const item = copy((candidate as Record<string, unknown>)[key])
if (item === undefined) return undefined
Object.defineProperty(result, key, {
value: item,
enumerable: true,
configurable: true,
writable: true,
})
}
return result
})
const target: Record<string, CodeJsonValue> = {}
assign(task.destination, target)
active.add(candidate)
tasks.push({ kind: 'leave', source: candidate })
for (let index = keys.length - 1; index >= 0; index--) {
const key = keys[index]
/* v8 ignore next -- the loop is bounded by the captured key count. */
if (key === undefined) return undefined
tasks.push({ kind: 'object-property', source: candidate as Record<string, unknown>, key, target })
}
}
return copy(value)
return root
}
/* jscpd:ignore-end */