This commit is contained in:
darenhsu
2022-07-17 13:16:16 +08:00
parent 84759556ff
commit befd344ab0
28070 changed files with 4008428 additions and 1 deletions
+48
View File
@@ -0,0 +1,48 @@
# Changelog
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
## [1.0.0](https://github.com/nuxt-contrib/destr/compare/v0.1.9...v1.0.0) (2020-06-16)
### [0.1.9](https://github.com/nuxt-contrib/destr/compare/v0.1.8...v0.1.9) (2020-05-28)
### Bug Fixes
* fix number regex ([886c143](https://github.com/nuxt-contrib/destr/commit/886c1433b388907072528aabaacfd36512a1d6f4))
### [0.1.8](https://github.com/nuxt-contrib/destr/compare/v0.1.7...v0.1.8) (2020-05-28)
### Bug Fixes
* **types:** remove strict types ([1513a48](https://github.com/nuxt-contrib/destr/commit/1513a4809367a4389f2c2f13b79d40f9810aac19))
### [0.1.7](https://github.com/nuxt-contrib/destr/compare/v0.1.6...v0.1.7) (2020-05-27)
### Bug Fixes
* don't throw error on parse fail ([65e22c6](https://github.com/nuxt-contrib/destr/commit/65e22c631ef2757d0b25d77e1270f4656bca7ef8))
### [0.1.6](https://github.com/nuxt-contrib/destr/compare/v0.1.5...v0.1.6) (2020-05-27)
### [0.1.5](https://github.com/nuxt-contrib/destr/compare/v0.1.4...v0.1.5) (2020-05-27)
### Bug Fixes
* use JsonSigRx to also match numbers ([3023552](https://github.com/nuxt-contrib/destr/commit/3023552f98823699ff12382a2c4c510c34bfc60a))
### [0.1.4](https://github.com/nuxt-contrib/destr/compare/v0.1.3...v0.1.4) (2020-05-22)
### [0.1.3](https://github.com/nuxt-contrib/destr/compare/v0.1.2...v0.1.3) (2020-05-20)
### Bug Fixes
* remove unused code ([10ef37d](https://github.com/nuxt-contrib/destr/commit/10ef37d2854ce41534abbcff955c658fa727c459))
### [0.1.2](https://github.com/nuxt-contrib/destr/compare/v0.1.1...v0.1.2) (2020-05-20)
### 0.1.1 (2020-05-20)
Generated Vendored
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 - Pooya Parsa <pyapar@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+117
View File
@@ -0,0 +1,117 @@
# destr
> A faster, secure and convenient alternative for [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse):
[![npm version][npm-v-src]][npm-v-href]
[![npm downloads][npm-d-src]][npm-d-href]
[![bundle phobia][bundlephobia-src]][bundlephobia-href]
## Usage
### Node.js
Install using npm or yarn:
```bash
npm i destr
# or
yarn add destr
```
Import into your Node.js project:
```js
// CommonJS
const destr = require('destr')
// ESM
import destr from 'destr'
```
### Deno
```js
import destr from 'https://deno.land/x/destr/src/index.ts'
console.log(destr('{ "deno": "yay" }'))
```
## Why?
Please note that `destr` is little bit slower when parsing a standard JSON string mainly because of transform to avoid [prototype pollution](https://hueniverse.com/a-tale-of-prototype-poisoning-2610fa170061) which can lead to serious security issues if not being sanetized. In the other words, `destr` is better when input is not always a json string or from untrsuted source like request body.
**Fast fallback to input if is not string:**
```js
// Uncaught SyntaxError: Unexpected token u in JSON at position 0
JSON.parse()
// undefined
destr()
```
```js
// JSON.parse x 5,324,474 ops/sec ±0.65% (94 runs sampled)
JSON.parse(3.14159265359)
// destr x 657,187,095 ops/sec ±0.06% (98 runs sampled)
destr(3.14159265359)
```
**Fast lookup for known string values:**
```js
// Uncaught SyntaxError: Unexpected token T in JSON at position 0
JSON.parse('TRUE')
// true
destr('TRUE')
```
```js
// JSON.parse x 10,407,488 ops/sec ±0.30% (97 runs sampled)
JSON.parse('true')
// destr x 88,634,032 ops/sec ±0.32% (95 runs sampled)
destr('true')
```
**Fallback to original value if parse fails (empty or any plain string):**
```js
// Uncaught SyntaxError: Unexpected token s in JSON at position 0
// JSON.parse (try-catch) x 248,212 ops/sec ±1.22% (84 runs sampled
JSON.parse('salam')
// destr x 30,867,179 ops/sec ±0.49% (94 runs sampled)
destr('salam')
```
**Avoid prototype pollution:**
```js
const input = '{ "user": { "__proto__": { "isAdmin": true } } }'
// { user: { __proto__: { isAdmin: true } } }
JSON.parse(input)
// { user: {} }
destr(input)
```
## License
MIT. Made with 💖
<!-- Refs -->
[npm-v-src]: https://img.shields.io/npm/v/destr?style=flat-square
[npm-v-href]: https://npmjs.com/package/destr
[npm-d-src]: https://img.shields.io/npm/dm/destr?style=flat-square
[npm-d-href]: https://npmjs.com/package/destr
[github-actions-src]: https://img.shields.io/github/workflow/status/nuxt-contrib/destr/ci/master?style=flat-square
[github-actions-href]: https://github.com/nuxt-contrib/destr/actions?query=workflow%3Aci
[bundlephobia-src]: https://img.shields.io/bundlephobia/min/destr?style=flat-square
[bundlephobia-href]: https://bundlephobia.com/result?p=destr
+1
View File
@@ -0,0 +1 @@
export default function destr(val: any): any;
+63
View File
@@ -0,0 +1,63 @@
'use strict';
// https://github.com/fastify/secure-json-parse
// https://github.com/hapijs/bourne
var suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
var suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
var JsonSigRx = /^["{[]|^-?[0-9][0-9.]*$/;
function jsonParseTransform(key, value) {
if (key === '__proto__' || key === 'constructor') {
return;
}
return value;
}
function destr(val) {
if (typeof val !== 'string') {
return val;
}
var _lval = val.toLowerCase();
if (_lval === 'true') {
return true;
}
if (_lval === 'false') {
return false;
}
if (_lval === 'null') {
return null;
}
if (_lval === 'nan') {
return NaN;
}
if (_lval === 'infinity') {
return Infinity;
}
if (_lval === 'undefined') {
return undefined;
}
if (!JsonSigRx.test(val)) {
return val;
}
try {
if (suspectProtoRx.test(val) || suspectConstructorRx.test(val)) {
return JSON.parse(val, jsonParseTransform);
}
return JSON.parse(val);
} catch (_e) {
return val;
}
}
module.exports = destr;
+67
View File
@@ -0,0 +1,67 @@
{
"_args": [
[
"destr@1.0.0",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
]
],
"_from": "destr@1.0.0",
"_id": "destr@1.0.0",
"_inBundle": false,
"_integrity": "sha512-uw0zD4688l00hDftUP76EWyweDtOB/0mVd/8GvmwecYD+Akw5Z4v43pw4onVsz4rC/BsfnZnDMG2rdTGhVIODA==",
"_location": "/destr",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "destr@1.0.0",
"name": "destr",
"escapedName": "destr",
"rawSpec": "1.0.0",
"saveSpec": null,
"fetchSpec": "1.0.0"
},
"_requiredBy": [
"/@nuxt/config",
"/@nuxt/telemetry",
"/rc9"
],
"_resolved": "https://registry.npmjs.org/destr/-/destr-1.0.0.tgz",
"_spec": "1.0.0",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"bugs": {
"url": "https://github.com/nuxt-contrib/destr/issues"
},
"description": "A faster, secure and convenient alternative for JSON.parse",
"devDependencies": {
"@hapi/bourne": "^2.0.0",
"@nuxtjs/eslint-config-typescript": "latest",
"benchmark": "latest",
"bili": "latest",
"eslint": "latest",
"rollup-plugin-typescript2": "latest",
"secure-json-parse": "^2.1.0",
"standard-version": "latest",
"typescript": "latest"
},
"files": [
"dist"
],
"homepage": "https://github.com/nuxt-contrib/destr#readme",
"license": "MIT",
"main": "dist/index.js",
"name": "destr",
"repository": {
"type": "git",
"url": "git+https://github.com/nuxt-contrib/destr.git"
},
"scripts": {
"bench": "yarn build && node ./bench.js",
"build": "bili src/index.ts",
"lint": "eslint --ext .ts .",
"release": "yarn test && yarn build && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint"
},
"types": "dist/index.d.ts",
"version": "1.0.0"
}