safe-json-value
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -6,6 +6,10 @@ /** | ||
/** | ||
* Maximum `JSON.stringify(value).length`. | ||
* Big JSON strings can make a process, filesystem operation or network | ||
* request crash. | ||
* `maxSize` prevents it by setting a maximum `JSON.stringify(value).length`. | ||
* | ||
* Additional properties beyond the size limit are omitted. | ||
* They are completely removed, not truncated (including strings). | ||
* | ||
* @default Number.POSITIVE_INFINITY (no maximum size) | ||
* @default 1e7 | ||
* | ||
@@ -48,3 +52,2 @@ * @example | ||
| 'ignoredUndefined' | ||
| 'maxSize' | ||
| 'unresolvedClass' | ||
@@ -55,2 +58,3 @@ | 'unresolvedGetter' | ||
| 'unsafeCycle' | ||
| 'unsafeSize' | ||
| 'unstableInfinite' | ||
@@ -57,0 +61,0 @@ |
@@ -26,6 +26,2 @@ import{safeGetChangeProp}from"./get.js"; | ||
export const addSize=function({ | ||
@@ -55,3 +51,3 @@ type, | ||
newValue:undefined, | ||
reason:"maxSize"}); | ||
reason:"unsafeSize"}); | ||
@@ -64,4 +60,6 @@ return{size,stop}; | ||
export const DEFAULT_MAX_SIZE=SKIP_MAX_SIZE; | ||
export const DEFAULT_MAX_SIZE=1e7; | ||
const SIZED_TYPES={ | ||
@@ -76,3 +74,3 @@ value:{ | ||
2: | ||
JSON.stringify(value).length; | ||
getJsonLength(value); | ||
}, | ||
@@ -91,7 +89,20 @@ getOldValue(value){ | ||
getSize({key,empty}){ | ||
return typeof key==="symbol"? | ||
0: | ||
JSON.stringify(key).length+(empty?1:2); | ||
return typeof key==="symbol"?0:getJsonLength(key)+(empty?1:2); | ||
}, | ||
getOldValue:safeGetChangeProp}}; | ||
const getJsonLength=function(value){ | ||
try{ | ||
return JSON.stringify(value).length; | ||
}catch{ | ||
return Number.POSITIVE_INFINITY; | ||
} | ||
}; | ||
//# sourceMappingURL=size.js.map |
{ | ||
"name": "safe-json-value", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "exports": "./build/src/main.js", |
@@ -14,5 +14,5 @@ [![Codecov](https://img.shields.io/codecov/c/github/ehmicky/safe-json-value.svg?label=tested&logo=codecov)](https://codecov.io/gh/ehmicky/safe-json-value) | ||
- [Throwing](#exceptions) | ||
- [Changing types](#unexpected-types), [filtering](#filtered-values) or | ||
[changing values](#unresolved-values) unexpectedly | ||
- Returning a [very big output](#big-output) | ||
- [Changing types](#unexpected-types) | ||
- [Filtering](#filtered-values) or [transforming values](#unresolved-values) | ||
unexpectedly | ||
@@ -79,7 +79,16 @@ # Example | ||
_Type_: `number`\ | ||
_Default_: `Number.POSITIVE_INFINITY` (no maximum size) | ||
_Default_: `1e7` | ||
Maximum `JSON.stringify(value).length`. Additional properties beyond the size | ||
limit [are omitted](#big-output). | ||
Big JSON strings can make a process, filesystem operation or network request | ||
crash. `maxSize` prevents it by setting a maximum | ||
`JSON.stringify(value).length`. | ||
Additional properties beyond the size limit [are omitted](#big-output). They are | ||
completely removed, not truncated (including strings). | ||
```js | ||
const input = { one: true, two: 'a'.repeat(1e6) } | ||
JSON.stringify(safeJsonValue(input, { maxSize: 1e5 }).value) // '{"one":true}" | ||
``` | ||
### Return value | ||
@@ -139,3 +148,4 @@ | ||
- [Exceptions](#exceptions): [`"unsafeCycle"`](#cycles), | ||
[`"unsafeException"`](#infinite-recursion), [`"unsafeBigInt"`](#bigint), | ||
[`"unsafeBigInt"`](#bigint), [`"unsafeSize"`](#big-output), | ||
[`"unsafeException"`](#infinite-recursion), | ||
[`"unsafeToJSON"`](#exceptions-in-tojson), | ||
@@ -155,3 +165,2 @@ [`"unsafeGetter"`](#exceptions-in-getters) | ||
[`"unresolvedClass"`](#classes), [`"unresolvedGetter"`](#getters) | ||
- [Big output](#big-output): [`"maxSize"`](#big-output) | ||
@@ -202,2 +211,10 @@ ##### changes[*].error | ||
### Big output | ||
```js | ||
const input = { one: true, two: '\n'.repeat(5e8) } | ||
JSON.stringify(input) // Throws due to max string length | ||
JSON.stringify(safeJsonValue(input).value) // '{"one":true}" | ||
``` | ||
### Exceptions in `toJSON()` | ||
@@ -448,15 +465,2 @@ | ||
## Big output | ||
Big JSON strings can make a process, filesystem operation or network request | ||
crash. | ||
When using the [`maxSize` option](#maxsize), properties that are too large are | ||
omitted. Large values (including strings) are completely removed, not truncated. | ||
```js | ||
const input = { one: true, two: 'a'.repeat(1e6) } | ||
JSON.stringify(safeJsonValue(input, { maxSize: 1e5 }).value) // '{"one":true}" | ||
``` | ||
# Support | ||
@@ -463,0 +467,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76547
654
487