Limits, hashing and JSON helpers

Resource limits, canonical JSON and hashes. Template reference in the DocStudio developer reference.

Public from docstudio/core and the root docstudio entry. Everything here is synchronous, pure and platform-neutral: no DOM, no Node built-ins, no runtime dependency. tsconfig.core.json typechecks src/core with no DOM and no Node type definitions to keep it that way.

Diagnostics

Diagnostic { code, severity: 'error' | 'warning' | 'info', message, path }

path is a JSON Pointer into the input ('' is the root). Functions in the core return diagnostics instead of throwing for any JSON input.

Code Severity Meaning
LIMIT_EXCEEDED error An input exceeded a configured limit (depth, size, count)
INVALID_JSON_VALUE error A value is not plain JSON data (undefined, NaN, Infinity, function, symbol, bigint, class instance, array hole, unreadable getter or proxy)
INVALID_LIMIT error or warning A limit override was invalid (error) or unknown (warning)
DIAGNOSTICS_TRUNCATED info The collector dropped further diagnostics after its cap
SCHEMA_VIOLATION error A template does not match the template schema (one diagnostic per violation, at its JSON Pointer)
VALIDATOR_FAILURE error The schema validator threw unexpectedly; the failure is reported instead of propagated
INVALID_EXPRESSION, UNKNOWN_FUNCTION, ARITY, INVALID_PATH, UNSAFE_PATH error Expression parse failures (see expressions.md)
TYPE_ERROR, DIVISION_BY_ZERO, NON_FINITE, UNKNOWN_FORMATTER, FORMATTER_FAILURE warning Expression evaluation problems; the result is undefined
LOCKED_PROPERTY, UNKNOWN_THEME, UNKNOWN_TOKEN, UNKNOWN_STYLE, INVALID_STYLE_VALUE warning Configuration and style resolution problems (see configuration.md)
INVALID_LOCK error or warning A lock pattern is malformed or has an unknown root
MISSING_DATA warning or error A binding found no value and the policy is warn or error
WRONG_TYPE warning or error A binding found a value of the wrong type for text or a collection

createCollector(max) keeps at most max diagnostics plus one truncation marker and tracks whether any error was added. Later milestones add codes to this catalog; every code has a test.

Limits

One Limits object configures the whole phase. DEFAULT_LIMITS is frozen. normalizeLimits(overrides) accepts only own, known, positive safe integers up to the ceiling in LIMIT_CEILINGS, ignores the rest with INVALID_LIMIT diagnostics, and returns a frozen result.

Limit Default Ceiling Used by
maxJsonDepth 128 1,024 canonical JSON (M1)
maxJsonNodes 1,000,000 50,000,000 canonical JSON (M1)
maxJsonChars 16,777,216 536,870,912 canonical JSON (M1)
maxStringLength 1,000,000 100,000,000 canonical JSON (M1)
maxArrayLength 1,000,000 50,000,000 canonical JSON (M1)
maxObjectKeys 10,000 1,000,000 canonical JSON (M1)
maxTemplateNodes 5,000 1,000,000 structural validation (M2), enforced before schema validation
maxExpressionDepth 32 256 expressions (M3)
maxExpressionNodes 2,000 100,000 expressions (M3)
maxEvaluationSteps 1,000,000 1,000,000,000 expressions (M3)
maxRepeaterRows 100,000 10,000,000 compile (M10)
maxCompositeDepth 8 64 composites (M9)
maxDiagnostics 500 100,000 collector
maxTraceEntries 10,000 1,000,000 binding trace (M5)

Limits use counters, not timers, so results are deterministic.

Canonical JSON

canonicalize(value, limits?) returns { ok: true, text } or { ok: false, diagnostics }. canonicalizeTo(value, sink, limits?) streams chunks to a sink and returns diagnostics (an empty array means success).

Rules:

  • Object keys are sorted by UTF-16 code unit order; array order is preserved.
  • Strings and numbers are serialized exactly as JSON.stringify does, which matches RFC 8785 for JSON values: control characters, quote and backslash are escaped, other characters are literal, lone surrogates are written as \uXXXX escapes, -0 becomes 0, numbers use the shortest round-trip form.
  • Only plain objects (prototype Object.prototype or null), arrays, strings, finite numbers, booleans and null are accepted. An own __proto__ key is an ordinary key.
  • Traversal is iterative with an explicit stack, so deep nesting reports LIMIT_EXCEEDED rather than overflowing the call stack, and cyclic structures end at the depth limit.
  • Input is never mutated or cloned. Property access is wrapped so getters or proxies that throw yield INVALID_JSON_VALUE.

Hashing

hashValue(value, limits?) returns { ok: true, hash } where hash is sha256: followed by 64 lowercase hex characters, or { ok: false, diagnostics }. The hash is SHA-256 over the UTF-8 bytes of the canonical text, computed by streaming canonical chunks into an incremental hasher, so the full canonical string is never built. Sha256 (incremental) and sha256Hex(text) are also exported. Lone surrogates encode as U+FFFD, matching the platform encoder.

SHA-256 is implemented in TypeScript so that the core stays synchronous and needs no platform crypto. It is tested against the NIST vectors and against the platform implementation for random input.

Two templates with the same content in any key order have the same hash. The hash identifies content, not intent: it is the basis for immutable published versions and reproducible compilation.

JSON Pointer helpers

appendPointer(base, segment), escapePointerSegment, unescapePointerSegment and parsePointer implement RFC 6901 for diagnostic paths.