@cqrs-ddd/safe-stringify 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -16,6 +16,9 @@ export type StrictJsonValue = null | boolean | number | string | StrictJsonValue
|
|
|
16
16
|
* - Object keys are optionally sorted recursively for deterministic serialization.
|
|
17
17
|
* - Unsupported types (non-finite numbers, `Map`, `Set`, `Error`, `RegExp`,
|
|
18
18
|
* binary buffers, promises, symbols, and sparse arrays) throw a {@link TypeError}.
|
|
19
|
+
* - A boxed primitive, or an iterable object that is not an array and has no
|
|
20
|
+
* `toJSON()` (`URLSearchParams`, `Headers`), throws a {@link TypeError}: its
|
|
21
|
+
* state is not in its own properties.
|
|
19
22
|
* - Cyclic references are detected via a `WeakSet` and throw a {@link TypeError}.
|
|
20
23
|
*
|
|
21
24
|
* @param value The value to normalize.
|
|
@@ -46,7 +49,8 @@ export declare function toStrictJsonValue(value: unknown, sortKeys?: boolean): S
|
|
|
46
49
|
* - Object keys are sorted recursively.
|
|
47
50
|
* - Unsupported types (`undefined`, `bigint`, symbols, functions, non-finite
|
|
48
51
|
* numbers, `Map`, `Set`, `Error`, `RegExp`, binary buffers, promises,
|
|
49
|
-
* symbol-keyed properties
|
|
52
|
+
* symbol-keyed properties, sparse arrays, boxed primitives and iterable
|
|
53
|
+
* objects without `toJSON()`, such as `URLSearchParams`) throw a {@link TypeError}.
|
|
50
54
|
* - Cyclic references are detected via a `WeakSet` and throw a {@link TypeError}.
|
|
51
55
|
*
|
|
52
56
|
* @param value The value to serialize.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stableStringify.d.ts","sourceRoot":"","sources":["../src/stableStringify.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,MAAM,eAAe,GACvB,IAAI,GACJ,OAAO,GACP,MAAM,GACN,MAAM,GACN,eAAe,EAAE,GACjB;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,CAAA;CAAE,CAAC;AAEvC
|
|
1
|
+
{"version":3,"file":"stableStringify.d.ts","sourceRoot":"","sources":["../src/stableStringify.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,MAAM,eAAe,GACvB,IAAI,GACJ,OAAO,GACP,MAAM,GACN,MAAM,GACN,eAAe,EAAE,GACjB;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,CAAA;CAAE,CAAC;AAEvC;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,OAAO,EACd,QAAQ,UAAQ,GACf,eAAe,CAEjB;AAgJD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAStD"}
|
package/dist/stableStringify.js
CHANGED
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
* - Object keys are optionally sorted recursively for deterministic serialization.
|
|
12
12
|
* - Unsupported types (non-finite numbers, `Map`, `Set`, `Error`, `RegExp`,
|
|
13
13
|
* binary buffers, promises, symbols, and sparse arrays) throw a {@link TypeError}.
|
|
14
|
+
* - A boxed primitive, or an iterable object that is not an array and has no
|
|
15
|
+
* `toJSON()` (`URLSearchParams`, `Headers`), throws a {@link TypeError}: its
|
|
16
|
+
* state is not in its own properties.
|
|
14
17
|
* - Cyclic references are detected via a `WeakSet` and throw a {@link TypeError}.
|
|
15
18
|
*
|
|
16
19
|
* @param value The value to normalize.
|
|
@@ -67,6 +70,9 @@ function normalizeObject(value, ancestors, sortKeys) {
|
|
|
67
70
|
ancestors.delete(value);
|
|
68
71
|
}
|
|
69
72
|
}
|
|
73
|
+
if (isOpaqueObject(value)) {
|
|
74
|
+
throw new TypeError(`${value.constructor?.name || 'Object'} is outside the supported JSON domain.`);
|
|
75
|
+
}
|
|
70
76
|
if (Object.getOwnPropertySymbols(value).length > 0) {
|
|
71
77
|
throw new TypeError('Symbol-keyed properties are outside the supported JSON domain.');
|
|
72
78
|
}
|
|
@@ -112,6 +118,18 @@ function isUnsupportedObject(value) {
|
|
|
112
118
|
ArrayBuffer.isView(value) ||
|
|
113
119
|
value instanceof Promise);
|
|
114
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* A boxed primitive or a non-array iterable (`URLSearchParams`, `Headers`, a
|
|
123
|
+
* generator): its state is not in its own properties, so two different values
|
|
124
|
+
* would both normalize to `{}`.
|
|
125
|
+
*/
|
|
126
|
+
function isOpaqueObject(value) {
|
|
127
|
+
return (value instanceof Number ||
|
|
128
|
+
value instanceof Boolean ||
|
|
129
|
+
value instanceof String ||
|
|
130
|
+
value instanceof BigInt ||
|
|
131
|
+
(!Array.isArray(value) && Symbol.iterator in value));
|
|
132
|
+
}
|
|
115
133
|
/**
|
|
116
134
|
* Deterministically serializes a value to a JSON string with lexicographically
|
|
117
135
|
* sorted object keys, for cache keys, idempotency fingerprints and snapshots.
|
|
@@ -128,7 +146,8 @@ function isUnsupportedObject(value) {
|
|
|
128
146
|
* - Object keys are sorted recursively.
|
|
129
147
|
* - Unsupported types (`undefined`, `bigint`, symbols, functions, non-finite
|
|
130
148
|
* numbers, `Map`, `Set`, `Error`, `RegExp`, binary buffers, promises,
|
|
131
|
-
* symbol-keyed properties
|
|
149
|
+
* symbol-keyed properties, sparse arrays, boxed primitives and iterable
|
|
150
|
+
* objects without `toJSON()`, such as `URLSearchParams`) throw a {@link TypeError}.
|
|
132
151
|
* - Cyclic references are detected via a `WeakSet` and throw a {@link TypeError}.
|
|
133
152
|
*
|
|
134
153
|
* @param value The value to serialize.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stableStringify.js","sourceRoot":"","sources":["../src/stableStringify.ts"],"names":[],"mappings":"AAAA,sEAAsE;AAatE
|
|
1
|
+
{"version":3,"file":"stableStringify.js","sourceRoot":"","sources":["../src/stableStringify.ts"],"names":[],"mappings":"AAAA,sEAAsE;AAatE;;;;;;;;;;;;;;;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AC,KAAa;IACnC,OAAO,CACL,KAAK,YAAY,MAAM;QACvB,KAAK,YAAY,OAAO;QACxB,KAAK,YAAY,MAAM;QACvB,KAAK,YAAY,MAAM;QACvB,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,QAAQ,IAAI,KAAK,CAAC,CACpD,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC5C,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;IACxD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,SAAS,CACjB,8DAA8D,EAC9D,EAAE,KAAK,EAAE,CACV,CAAC;IACJ,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cqrs-ddd/safe-stringify",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Deterministic JSON for identity (cache keys, fingerprints) and safe, redacting JSON for logs, with no dependencies and no framework.",
|
|
5
5
|
"author": "Aristotelis",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE",
|