From b4a0941e436b0d274610f1d089e1df90d713a3c4 Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Thu, 26 Nov 2026 23:07:11 +0530 Subject: [PATCH 1/2] fix: sort extra typed array keys when using a replacer function With deterministic enabled, extra properties on a typed array were sorted without a replacer, but kept insertion order with a replacer function. Sort them in the replacer function path as well. --- index.js | 10 ++++++++-- test.js | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f71bf98..134637b 100644 --- a/index.js +++ b/index.js @@ -371,8 +371,14 @@ function configure (options) { whitespace = ' ' } const maximumPropertiesToStringify = Math.min(keyLength, maximumBreadth) - if (deterministic && getTypedArrayType(value) === undefined) { - keys = sort(keys, comparator) + if (deterministic) { + if (getTypedArrayType(value) === undefined) { + keys = sort(keys, comparator) + } else if (keyLength > value.length) { + // Keep the indices in order and only sort the extra properties. + const indices = keys.slice(0, value.length) + keys = indices.concat(sort(keys.slice(value.length), comparator)) + } } stack.push(value) for (let i = 0; i < maximumPropertiesToStringify; i++) { diff --git a/test.js b/test.js index 4ad065d..2a34a0d 100644 --- a/test.js +++ b/test.js @@ -203,6 +203,25 @@ test('omitted bigint typed array entries preserve property order and breadth', assert.end() }) +test('replacer functions sort extra typed array properties deterministically', + /** @param {import('tap').Test} assert */ + (assert) => { + const value = Object.assign(new Uint8Array([1, 2]), { z: 1, a: 2 }) + const descending = /** @param {string} left @param {string} right */ (left, right) => right.localeCompare(left) + const replacer = /** @param {string} key @param {unknown} value */ (key, value) => value + for (const [deterministic, expected] of /** @type {const} */ ([ + [true, { 0: 1, 1: 2, a: 2, z: 1 }], + [descending, { 0: 1, 1: 2, z: 1, a: 2 }] + ])) { + const serialize = stringify.configure({ deterministic }) + for (const space of [undefined, 2]) { + assert.equal(serialize(value, replacer, space), JSON.stringify(expected, null, space)) + assert.equal(serialize(value, replacer, space), serialize(value, null, space)) + } + } + assert.end() + }) + test('bigint typed array policy uses the intrinsic type', /** @param {import('tap').Test} assert */ (assert) => { From acd737568c6cbc9e61c5448362c2c268a31eb352 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 30 Nov 2026 16:38:58 +0100 Subject: [PATCH 2/2] fix(replacer): preserve typed array index order A typed array can have a length property that shadows the intrinsic getter. Using that property can pass numeric indices to the comparator. Copying the index keys also costs CPU when maximumBreadth excludes every extra key. Keep the existing index prefix when sorting the extra keys. --- index.js | 23 ++++++++++++++++------- test.js | 25 +++++++++++++++++++++---- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 134637b..7487e6b 100644 --- a/index.js +++ b/index.js @@ -65,6 +65,7 @@ function sort (array, comparator) { return array } +/** @type {() => string | undefined} */ const typedArrayPrototypeGetSymbolToStringTag = // @ts-expect-error Object.getOwnPropertyDescriptor( @@ -75,10 +76,12 @@ const typedArrayPrototypeGetSymbolToStringTag = ), Symbol.toStringTag ).get +const typedArrayPrototypeGetLength = /** @type {{ get: () => number }} */ ( + Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Int8Array.prototype), 'length') +).get /** @param {object & {length?: unknown}} value */ function getTypedArrayType (value) { - // @ts-expect-error const type = typedArrayPrototypeGetSymbolToStringTag.call(value) return type !== undefined && value.length !== 0 ? type : undefined } @@ -371,13 +374,19 @@ function configure (options) { whitespace = ' ' } const maximumPropertiesToStringify = Math.min(keyLength, maximumBreadth) - if (deterministic) { - if (getTypedArrayType(value) === undefined) { + if (deterministic && keyLength > 1) { + let index = typedArrayPrototypeGetSymbolToStringTag.call(value) === undefined + ? 0 + : typedArrayPrototypeGetLength.call(value) + if (index === 0) { keys = sort(keys, comparator) - } else if (keyLength > value.length) { - // Keep the indices in order and only sort the extra properties. - const indices = keys.slice(0, value.length) - keys = indices.concat(sort(keys.slice(value.length), comparator)) + } else if ( + keyLength > index + 1 && + (comparator !== undefined || maximumPropertiesToStringify > index) + ) { + for (const key of sort(keys.slice(index), comparator)) { + keys[index++] = key + } } } stack.push(value) diff --git a/test.js b/test.js index 2a34a0d..c7aaa8e 100644 --- a/test.js +++ b/test.js @@ -206,12 +206,18 @@ test('omitted bigint typed array entries preserve property order and breadth', test('replacer functions sort extra typed array properties deterministically', /** @param {import('tap').Test} assert */ (assert) => { - const value = Object.assign(new Uint8Array([1, 2]), { z: 1, a: 2 }) - const descending = /** @param {string} left @param {string} right */ (left, right) => right.localeCompare(left) + const value = Object.assign(new Uint8Array([1, 2]), { z: 1, a: 2, m: 3 }) + let compared = false + const descending = /** @param {string} left @param {string} right */ (left, right) => { + assert.match(left, /^[amz]$/) + assert.match(right, /^[amz]$/) + compared = true + return right.localeCompare(left) + } const replacer = /** @param {string} key @param {unknown} value */ (key, value) => value for (const [deterministic, expected] of /** @type {const} */ ([ - [true, { 0: 1, 1: 2, a: 2, z: 1 }], - [descending, { 0: 1, 1: 2, z: 1, a: 2 }] + [true, { 0: 1, 1: 2, a: 2, m: 3, z: 1 }], + [descending, { 0: 1, 1: 2, z: 1, m: 3, a: 2 }] ])) { const serialize = stringify.configure({ deterministic }) for (const space of [undefined, 2]) { @@ -219,6 +225,17 @@ test('replacer functions sort extra typed array properties deterministically', assert.equal(serialize(value, replacer, space), serialize(value, null, space)) } } + assert.equal(stringify(new Uint8Array([1]), replacer), '{"0":1}') + assert.equal(stringify(new Uint8Array([1, 2]), replacer), '{"0":1,"1":2}') + for (const deterministic of [true, descending]) { + compared = false + const capped = stringify.configure({ deterministic, maximumBreadth: 2 }) + assert.equal(capped(value, replacer), '{"0":1,"1":2,"...":"3 items not stringified"}') + assert.equal(compared, typeof deterministic === 'function') + } + Object.defineProperty(value, 'length', { value: 1 }) + const limited = stringify.configure({ deterministic: descending, maximumBreadth: 3 }) + assert.equal(limited(value, replacer), '{"0":1,"1":2,"z":1,"...":"2 items not stringified"}') assert.end() })