Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/kit/src/codec/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface EncodeState {
entryOffset: number
depth: number
seen: WeakMap<object, ValueHandle>
expanded: WeakSet<object>
handles: ValueHandleRegistry
handleScope?: string
maxDepth: number
Expand All @@ -77,6 +78,7 @@ export function encodeValue(value: unknown, options: ValueCodecOptions = {}): En
entryOffset: options.entryOffset ?? 0,
entryKeys: options.entryKeys,
seen: new WeakMap(),
expanded: new WeakSet(),
handles,
handleScope: options.handleScope,
maxDepth: options.maxDepth ?? 2,
Expand Down Expand Up @@ -138,8 +140,19 @@ function encodeUnsafe(value: unknown, state: EncodeState): EncodedValue {
if (circular) return { kind: 'circular', handle: circular }

const handle = state.handles.getOrCreate(value, state.handleScope)
// Only ancestors on the current path are circular. A value repeated elsewhere
// is encoded again, but its children are walked only once per encodeValue call.
const repeated = state.expanded.has(value)
if (state.depth < state.maxDepth) state.expanded.add(value)
state.seen.set(value, handle)
try {
return encodeReference(value, handle, repeated ? { ...state, maxDepth: state.depth } : state)
} finally {
state.seen.delete(value)
}
}

function encodeReference(value: object, handle: ValueHandle, state: EncodeState): EncodedValue {
if (isCustomValue(value)) return encodeCustomValue(value, handle, state)

if (isDomElement(value)) return encodeDomElement(value)
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/codec/encode-value.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,43 @@ describe('encodeValue', () => {
expect(secondChild.preview.find((entry) => entry.key === 'root')?.value.kind).toBe('circular')
})

it('does not mark repeated non-circular references as circular', () => {
const item = { id: 1 }
const day = new Date(0)

const encoded = encodeValue({ items: [item, item], start: day, end: day }, { maxDepth: 3 })

expect(encoded.kind).toBe('object')
if (encoded.kind !== 'object') return

const items = encoded.preview.find((entry) => entry.key === 'items')?.value
expect(items?.kind).toBe('array')
if (items?.kind !== 'array') return

const [first, second] = items.preview.map((entry) => entry.value)
expect(first).toMatchObject({
kind: 'object',
entries: 1,
preview: [{ key: 'id', value: { kind: 'number', value: 1 } }],
})
if (first?.kind !== 'object') return
// The repeat keeps its handle, so its fields load on expand like a value at the depth limit.
expect(second).toEqual({ ...first, preview: [] })

const date = { kind: 'date', value: '1970-01-01T00:00:00.000Z' }
expect(encoded.preview.find((entry) => entry.key === 'start')?.value).toEqual(date)
expect(encoded.preview.find((entry) => entry.key === 'end')?.value).toEqual(date)
})

it('walks a shared subtree once instead of once per path', () => {
let node: object = { leaf: true }
for (let level = 0; level < 16; level++) node = { left: node, right: node }

const encoded = encodeValue(node, { maxDepth: 20 })

expect(JSON.stringify(encoded).length).toBeLessThan(10_000)
})

it('truncates previews according to the configured state budget', () => {
const encoded = encodeValue({ first: 1, second: 2, third: 3 }, { maxEntries: 2 })

Expand Down
Loading