Repository navigation
Sync upstream safe-stable-stringify 3.0.1 / 3.0.2 fixes into the TypeScript fork - #4
Merged
Merged
Conversation
`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.
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
Bot
force-pushed
the
fork-tender/BridgeAR-safe-stable-stringify
branch
from
November 16, 2026 15:32
92de954 to
8855094
Compare
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
Bot
force-pushed
the
fork-tender/BridgeAR-safe-stable-stringify
branch
from
November 23, 2026 18:02
8855094 to
370b133
Compare
…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
Bot
force-pushed
the
fork-tender/BridgeAR-safe-stable-stringify
branch
from
November 30, 2026 19:01
370b133 to
72e19fd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:bigint: false: their entries are omitted, or reported in strict mode. Withsafe: 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.toJSONaccessors are read once per value and called viaReflect.apply, so a shadowed.callproperty has no effect.maximumDepthandmaximumBreadthnow accept an explicitInfinity. Strings shorter than 10 code units are escaped with a plain loop instead of the regex.StringifyFunctiontype is now exported.How it was ported
All runtime changes were ported by hand into
src/index.ts:stringifyString. It checks the same code-unit ranges as upstream, using range comparisons instead of a bitmask.stringifyTypedArrayhandles BigInt arrays whenbigintisfalse.failTypedArray(strict fail wrapped withmakeSafein safe mode) is passed to both typed-array call sites. The separator is reset when every entry was omitted.toJSONlookup is cached and called throughcallToJson(toJson, value, key).getPositiveIntegerOptionnow acceptsInfinity.makeSafeescapes messages throughstringifyString.export type StringifyFunction = Stringifywas added as an alias. Nothing existing changed: theStringify,ReplacerandStringifyOptionstypes and all default and named exports are the same as before.Conflicts and how they were resolved
index.js,index.d.ts,test.js,benchmark.js,compare.js,esm/*; newwrapper.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 sharedSymbol.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 fromsafe-stable-stringify^3.0.0to^3.0.1. Upstream'spackage.jsonsays 3.0.2, but 3.0.2 is not on npm yet (pnpm reports 3.0.1 as the latest).pnpm-lock.yamlwas regenerated with pnpm.tsconfig.json,.gitignore: kept the fork's versions. Upstream's changes there (the.tap/ignore and.cts/.mtsincludes) 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..github/workflows/test.ymlwas 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:toJSONaccessors read oncetoJSONhook with its owncallpropertyInfinitylimitsStringifyFunctiontype aliasI also added a short-string escaping boundary test. A small
getCombinationshelper 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 readstoJSONgetters 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 testpasses:ksc lint: 8 of 8 checks pass.Benchmarks
I compared before and after on the same machine. I ran
pnpm bench:baselinewith the fork-mainsrc/index.ts, thenpnpm bench:comparewith the merged source, and afterwards restored the stored baselines andcompare.jsonfrom git, so neither is changed in this PR.20 new commit(s) from
BridgeAR/safe-stable-stringify· upstream comparemain)See the report above for post-resolution verification.
Automated by fork-tender · outcome:
resolved· confidence:high