Skip to content

Sync upstream safe-stable-stringify 3.0.1 / 3.0.2 fixes into the TypeScript fork - #4

Merged
kitschpatrol merged 21 commits into
mainfrom
fork-tender/BridgeAR-safe-stable-stringify
Dec 2, 2026
Merged

kitschpatrol merged 21 commits into
mainfrom
fork-tender/BridgeAR-safe-stable-stringify

Conversation

@claude

@claude claude Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

What's new upstream

The merge brings in 20 upstream commits. The fork had already hand-ported everything through upstream 3.0.0 (bdd585f), so the new runtime changes are the 3.0.1 and 3.0.2 releases plus one types fix:

  • 3.0.1: BigInt typed arrays now respect bigint: false: their entries are omitted, or reported in strict mode. With safe: true, strict-mode failures are reported at each typed-array index. Safe mode now escapes caught error messages, so quotes, backslashes, newlines, control characters and lone surrogates produce valid JSON.
  • 3.0.2: toJSON accessors are read once per value and called via Reflect.apply, so a shadowed .call property has no effect. maximumDepth and maximumBreadth now accept an explicit Infinity. Strings shorter than 10 code units are escaped with a plain loop instead of the regex.
  • eb583b3: a StringifyFunction type is now exported.

How it was ported

All runtime changes were ported by hand into src/index.ts:

  • The short-string loop is in stringifyString. It checks the same code-unit ranges as upstream, using range comparisons instead of a bitmask.
  • stringifyTypedArray handles BigInt arrays when bigint is false.
  • failTypedArray (strict fail wrapped with makeSafe in safe mode) is passed to both typed-array call sites. The separator is reset when every entry was omitted.
  • Every toJSON lookup is cached and called through callToJson(toJson, value, key).
  • getPositiveIntegerOption now accepts Infinity.
  • makeSafe escapes messages through stringifyString.
  • export type StringifyFunction = Stringify was added as an alias. Nothing existing changed: the Stringify, Replacer and StringifyOptions types and all default and named exports are the same as before.

Conflicts and how they were resolved

  • Upstream root JS and tooling files (index.js, index.d.ts, test.js, benchmark.js, compare.js, esm/*; new wrapper.cjs, benchmark-utils.js, test-modules.js, test-fixtures/*; .npmignore): kept the fork's deletion. The fork is ESM-only and TypeScript, and its tests are Vitest. Upstream's CommonJS/ESM dual-export work (wrapper.cjs, the shared Symbol.for('safe-stable-stringify@3.0.2') global) is intentionally not adopted.
  • package.json: kept the fork's metadata, version, exports and scripts. The only change is the upstream-comparison devDependency, bumped from safe-stable-stringify ^3.0.0 to ^3.0.1. Upstream's package.json says 3.0.2, but 3.0.2 is not on npm yet (pnpm reports 3.0.1 as the latest). pnpm-lock.yaml was regenerated with pnpm.
  • tsconfig.json, .gitignore: kept the fork's versions. Upstream's changes there (the .tap/ ignore and .cts/.mts includes) only serve upstream's tooling.
  • changelog.md: added a fork entry for the ported fixes and copied upstream's v3.0.1 and v3.0.2 sections into the upstream changelog block.
  • readme.md: the fork-specific readme is kept. Its "Changes" bullet now says the fork matches upstream 3.0.2. Upstream's benchmark-number refresh is not carried over, because the fork's performance section is generated by mdat.
  • Protected path: .github/workflows/test.yml was changed upstream (Node version matrix). The fork deleted this file, and it stays deleted.

Tests

Upstream's new regression tests were ported into test/index.test.ts:

  • safe+strict float typed arrays
  • the BigInt typed-array policy
  • property order and breadth when BigInt entries are omitted
  • the BigInt policy using the intrinsic type (subclass, cross-realm, disguised)
  • toJSON accessors read once
  • a toJSON hook with its own call property
  • Infinity limits
  • escaping of safe-mode getter error messages
  • the StringifyFunction type alias

I also added a short-string escaping boundary test. A small getCombinations helper flattens upstream's deeply nested loops so they pass the fork's lint rules.

One change to test/upstream.test.ts: its "toJSON accessors" parity scenario compared against the published npm package. That package (3.0.1) still reads toJSON getters twice, while 3.0.2 reads them once. I moved the scenario out of the npm-parity loop into a dedicated test that pins the 3.0.2 result in every option and replacer mode. Once upstream publishes 3.0.2, the scenario can go back into the parity loop.

Verify

pnpm ci && pnpm build && pnpm lint && pnpm test passes:

  • tsdown build, attw and publint are clean.
  • ksc lint: 8 of 8 checks pass.
  • Vitest: 129 tests pass.

Benchmarks

I compared before and after on the same machine. I ran pnpm bench:baseline with the fork-main src/index.ts, then pnpm bench:compare with the merged source, and afterwards restored the stored baselines and compare.json from git, so neither is changed in this PR.

  • Most suites are 1–9% faster. For example, simple object is +4%, function replacer deep is +9%, indentation circular is +5%, and the implementation comparison is +2%.
  • Array replacer with indentation is 3–6% slower (simple object, circular, deep, deep circular). Its error margins are ±0.4%, but these are single runs on a shared CI runner. I think this is probably noise, but it's worth re-running locally.

20 new commit(s) from BridgeAR/safe-stable-stringify · upstream compare

Verification Result
Baseline (main) pass
Mechanical pass on this branch skipped

See the report above for post-resolution verification.

Automated by fork-tender · outcome: resolved · confidence: high

spokodev and others added 10 commits October 12, 2026 04:39
`JSON.stringify` treats any numeric `space` below 1 as no indentation, but
`stringify` threw on it:

    JSON.stringify({ a: 1 }, null, -1) // '{"a":1}'
    stringify({ a: 1 }, null, -1)      // RangeError: Invalid count value: -1

The README documents the spacer as behaving "the same as with
JSON.stringify()". `space` was clamped to a maximum of 10 but not a minimum
of 0, so a negative count reached `String.prototype.repeat`, which throws
for counts <= -1. Clamp the lower bound to 0.
Looks like this was accidentally introduced in BridgeAR@132e76c
Circular references to different ancestors currently produce the same marker, which hides where each cycle closes. The new marker identifies the referenced ancestor's depth, starting at 0 for the root.

BREAKING CHANGE: The default marker changes from `[Circular]` to `[Circular: N]`. Set `circularValue: '[Circular]'` to retain the previous output.
…R#73)

* Serialize non-finite typed-array values consistently

Fixes: BridgeAR#68

* perf(typed-array): inline floating-point checks

A formatter closure adds finite checks to integer arrays and an extra call for floating-point values. Inline checks retain direct conversion for integer and BigInt arrays.

---------

Co-authored-by: Ferit <frtcm11@gmail.com>
Array indexes only need strings when they reach toJSON or function replacer
callbacks. A four-number array uses 86 ns instead of 107 ns on Node 26.3.1
(V8 14.6.202.34-node.20), a 20% CPU reduction. Measurements use 50,000 warmup
calls and seven alternating trials, with the fastest and slowest removed.

Drive-by fix: Mark intentional invalid-option assertions for TypeScript.
Disordered objects with up to 200 keys can spend most of their serialization
time shifting keys. A shuffled 200-key object uses 16.6 us instead of 45.6 us
on Node 26.3.1 (V8 14.6.202.34-node.20), a 64% CPU reduction. Measurements
use 50,000 warmup calls and seven alternating trials, with the fastest and
slowest removed. Nearly ordered keys retain the insertion path.

Drive-by fix: Mark intentional invalid-option assertions for TypeScript.
The implementation is cached after the first load, so removing its global
export leaves the other wrapper without an export. Retain a version-specific
symbol so ESM imports, CommonJS requires, and independent copies of one
version receive the same function.

BREAKING CHANGE: Require Node.js 18 or later.
Primitive array elements and object properties enter recursive serializers
even though they need no traversal. Share primitive formatting across
serialization modes while preserving toJSON ordering and per-value error
handling in safe mode.

Native string escaping becomes faster at shorter string lengths on
Node.js 22 and later. Select the cutoff once at module load so older
runtimes retain their existing fast path.
@claude claude Bot added the fork-tender Automated upstream sync by fork-tender label Nov 9, 2026
BridgeAR and others added 5 commits November 10, 2026 23:44
BigInt typed arrays ignore `bigint: false` and strict mode because the
optimized path converts their entries directly. Safe mode also keeps
strict validation errors at their indices rather than replacing the
entire typed array.

Fixes: BridgeAR#78
* fix: escape error messages in safe mode

Use the existing JSON string escaping helper for caught errors.

Assisted-by: LLM

* fix: preserve closing brace indentation

Remove editor auto-indentation from the final brace.

Assisted-by: LLM

* test: cover escaped errors in safe serialization

Cover quotes, newlines, backslashes, NUL and lone surrogates across compact, indented and replacer modes.

Assisted-by: LLM
@claude
claude Bot force-pushed the fork-tender/BridgeAR-safe-stable-stringify branch from 92de954 to 8855094 Compare November 16, 2026 15:32
@claude claude Bot changed the title Record upstream safe-stable-stringify 3.0.0 merge (all changes already ported) Sync upstream safe-stable-stringify 3.0.1 + toJSON accessor fix Nov 16, 2026
bardrop and others added 4 commits November 18, 2026 12:31
Infinity is the documented default of both options, but passing it
explicitly threw "must be an integer". Code that forwards an optional
limit with a fallback, like `maximumDepth: depth ?? Infinity`, broke
on configure().
@claude claude Bot changed the title Sync upstream safe-stable-stringify 3.0.1 + toJSON accessor fix Sync upstream safe-stable-stringify 3.0.1 and 3.0.2 fixes into the TypeScript fork Nov 23, 2026
@claude
claude Bot force-pushed the fork-tender/BridgeAR-safe-stable-stringify branch from 8855094 to 370b133 Compare November 23, 2026 18:02
anubhavlal07 and others added 2 commits November 30, 2026 16:31
…der/BridgeAR-safe-stable-stringify

# Conflicts:
#	.github/workflows/test.yml
#	.gitignore
#	.npmignore
#	benchmark.js
#	changelog.md
#	compare.js
#	esm/package.json
#	esm/wrapper.mjs
#	index.d.ts
#	index.js
#	package.json
#	readme.md
#	test.js
#	tsconfig.json

Fork-Tender: v1
@claude
claude Bot force-pushed the fork-tender/BridgeAR-safe-stable-stringify branch from 370b133 to 72e19fd Compare November 30, 2026 19:01
@claude claude Bot changed the title Sync upstream safe-stable-stringify 3.0.1 and 3.0.2 fixes into the TypeScript fork Sync upstream safe-stable-stringify 3.0.1 / 3.0.2 fixes into the TypeScript fork Nov 30, 2026
@kitschpatrol
kitschpatrol merged commit 44717b5 into main Dec 2, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork-tender Automated upstream sync by fork-tender

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants