json.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * JSON-serializability validation for session event data.
  3. *
  4. * The session event log is the durable source of truth (the event-sourcing / session-persistence RFCs): every
  5. * `event.data` must round-trip losslessly through JSON so any persistence
  6. * backend can store and reload it byte-identically. This invariant belongs to
  7. * the log itself — `Session.append` enforces it at the source, so a
  8. * non-serializable event never enters `session.events` and the live log can
  9. * never diverge from what a backend can persist. Backends re-use the same
  10. * predicate to validate their own `append(events)` entry point (replay/fork
  11. * paths that do not go through a live `Session`).
  12. *
  13. * @module @deepseek-ai/dsh-session/json
  14. */
  15. /**
  16. * Whether `value` is losslessly JSON-serializable: only `null`, finite numbers,
  17. * booleans, strings, plain arrays, and plain objects of such values. Rejects
  18. * `BigInt`, function, symbol, `undefined`, non-finite numbers (`NaN`/`Infinity`,
  19. * which `JSON.stringify` turns into `null`), and exotic objects (`Map`/`Set`/
  20. * `Date`/class instances) — anything `JSON.stringify` would drop, throw on, or
  21. * convert lossily. Sparse arrays are rejected too: a hole serializes to `null`,
  22. * so `[1, , 3]` would not round-trip. Detects circular references (which would
  23. * throw) and reports them as non-serializable rather than propagating the throw.
  24. *
  25. * Scope — matches `JSON.stringify` exactly: only an object's OWN ENUMERABLE
  26. * STRING-keyed properties are inspected (`Object.values`). Symbol-keyed and
  27. * non-enumerable properties are NOT examined, because `JSON.stringify` likewise
  28. * drops them — they never reach the durable form, so a non-serializable value
  29. * hiding under a symbol/non-enumerable key cannot make the round-trip lossy.
  30. * Getters are invoked during the check (again as `JSON.stringify` would), so the
  31. * contract is for plain data records, not objects with side-effecting accessors.
  32. */
  33. export function isJsonValue(value: unknown, seen: Set<object> = new Set()): boolean {
  34. if (value === null) return true
  35. switch (typeof value) {
  36. case 'boolean':
  37. case 'string':
  38. return true
  39. case 'number':
  40. return Number.isFinite(value)
  41. case 'bigint':
  42. case 'function':
  43. case 'symbol':
  44. case 'undefined':
  45. return false
  46. case 'object':
  47. break // handled below
  48. }
  49. // object
  50. if (seen.has(value)) return false // circular
  51. seen.add(value)
  52. try {
  53. if (Array.isArray(value)) {
  54. // Reject sparse arrays: a hole is skipped by `every`/`forEach` but
  55. // JSON.stringify writes it as `null`, so `[1, , 3]` would round-trip
  56. // lossily. Require every index 0..length-1 to be an OWN property.
  57. for (let i = 0; i < value.length; i++) {
  58. if (!Object.prototype.hasOwnProperty.call(value, i)) return false
  59. if (!isJsonValue(value[i], seen)) return false
  60. }
  61. return true
  62. }
  63. // Plain object only (reject Map/Set/Date/class instances).
  64. const proto = Object.getPrototypeOf(value) as unknown
  65. if (proto !== Object.prototype && proto !== null) return false
  66. return Object.values(value).every(v => isJsonValue(v, seen))
  67. } finally {
  68. seen.delete(value)
  69. }
  70. }