数通智联化工云平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.2 KiB

2 years ago
# destr
2 years ago
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![bundle][bundle-src]][bundle-href]
[![License][license-src]][license-href]
2 years ago
2 years ago
A faster, secure and convenient alternative for [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).
2 years ago
## Usage
### Node.js
2 years ago
Install dependency:
2 years ago
```bash
2 years ago
# npm
2 years ago
npm i destr
2 years ago
# yarn
2 years ago
yarn add destr
2 years ago
# pnpm
pnpm i destr
2 years ago
```
Import into your Node.js project:
```js
// ESM
2 years ago
import { destr, safeDestr } from "destr";
// CommonJS
const { destr, safeDestr } = require("destr");
2 years ago
```
### Deno
```js
2 years ago
import { destr, safeDestr } from "https://deno.land/x/destr/src/index.ts";
2 years ago
2 years ago
console.log(destr('{ "deno": "yay" }'));
2 years ago
```
## Why?
2 years ago
### ✅ Type Safe
```ts
const obj = JSON.parse("{}"); // obj type is any
const obj = destr("{}"); // obj type is unknown by default
const obj = destr<MyInterface>("{}"); // obj is well-typed
```
### ✅ Fast fallback to input if is not string
> 🚀 Up to 500 times faster than `JSON.parse`!
2 years ago
```js
// Uncaught SyntaxError: Unexpected token u in JSON at position 0
2 years ago
JSON.parse();
2 years ago
// undefined
2 years ago
destr();
2 years ago
```
2 years ago
### ✅ Fast lookup for known string values
> 🚀 Up to 900 times faster than `JSON.parse`!
2 years ago
```js
// Uncaught SyntaxError: Unexpected token T in JSON at position 0
2 years ago
JSON.parse("TRUE");
2 years ago
// true
2 years ago
destr("TRUE");
2 years ago
```
2 years ago
### ✅ Fallback to original value if parse fails (empty or any plain string)
> 🚀 Up to 900 times faster than `JSON.parse`!
2 years ago
```js
// Uncaught SyntaxError: Unexpected token s in JSON at position 0
2 years ago
JSON.parse("salam");
2 years ago
// "salam"
2 years ago
destr("salam");
2 years ago
```
2 years ago
**Note:** This fails in safe/strict mode with `safeDestr`.
### ✅ Avoid prototype pollution
2 years ago
```js
2 years ago
const input = '{ "user": { "__proto__": { "isAdmin": true } } }';
2 years ago
// { user: { __proto__: { isAdmin: true } } }
2 years ago
JSON.parse(input);
2 years ago
// { user: {} }
2 years ago
destr(input);
2 years ago
```
2 years ago
### ✅ Strict Mode
2 years ago
2 years ago
When using `safeDestr` it will throw an error if the input is not a valid JSON string or parsing fails. (non string values and built-ins will be still returned as-is)
2 years ago
```js
// Returns "[foo"
2 years ago
destr("[foo");
2 years ago
// Throws an error
2 years ago
safeDestr("[foo");
2 years ago
```
## Benchmarks
2 years ago
`destr` is sometimes little bit slower than `JSON.parse` when parsing a valid JSON string mainly because of transform to avoid [prototype pollution](https://learn.snyk.io/lessons/prototype-pollution/javascript/) which can lead to serious security issues if not being sanitized. In the other words, `destr` is better when input is not always a JSON string or from untrusted source like request body.
2 years ago
2 years ago
Check [Benchmarks](./BENCH.md)
2 years ago
## License
MIT. Made with 💖
2 years ago
<!-- Badges -->
2 years ago
2 years ago
[npm-version-src]: https://img.shields.io/npm/v/destr?style=flat&colorA=18181B&colorB=F0DB4F
[npm-version-href]: https://npmjs.com/package/destr
[npm-downloads-src]: https://img.shields.io/npm/dm/destr?style=flat&colorA=18181B&colorB=F0DB4F
[npm-downloads-href]: https://npmjs.com/package/destr
[bundle-src]: https://img.shields.io/bundlephobia/minzip/destr?style=flat&colorA=18181B&colorB=F0DB4F
[bundle-href]: https://bundlephobia.com/result?p=destr
[license-src]: https://img.shields.io/github/license/unjs/destr.svg?style=flat&colorA=18181B&colorB=F0DB4F
[license-href]: https://github.com/unjs/destr/blob/main/LICENSE