Skip to content

Commit a828ae6

Browse files
add types (#30)
* add types * tsd * downgrade tsd for node 8.x support * exclude standard/tsd from ci on node.js 8.x and below
1 parent cdd4a22 commit a828ae6

File tree

4 files changed

+111
-3
lines changed

4 files changed

+111
-3
lines changed

.github/workflows/ci.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
node-version: [6.x, 8.x, 10.x, 11.x, 12.x, 13.x, 14.x, 15.x]
17+
node-version: [10.x, 11.x, 12.x, 13.x, 14.x, 15.x]
1818
steps:
1919
- uses: actions/checkout@v2
2020
- name: Use Node.js ${{ matrix.node-version }}
@@ -25,3 +25,18 @@ jobs:
2525
run: npm install --ignore-scripts
2626
- name: Test
2727
run: npm run test
28+
test-legacy:
29+
runs-on: ubuntu-latest
30+
strategy:
31+
matrix:
32+
node-version: [6.x, 8.x]
33+
steps:
34+
- uses: actions/checkout@v2
35+
- name: Use Node.js ${{ matrix.node-version }}
36+
uses: actions/setup-node@v2.1.5
37+
with:
38+
node-version: ${{ matrix.node-version }}
39+
- name: Install Dependencies
40+
run: npm install --ignore-scripts
41+
- name: Test
42+
run: npm run test-legacy

index.d.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export type ParseOptions = {
2+
/**
3+
* What to do when a `__proto__` key is found.
4+
* - `'error'` - throw a `SyntaxError` when a `__proto__` key is found. This is the default value.
5+
* - `'remove'` - deletes any `__proto__` keys from the result object.
6+
* - `'ignore'` - skips all validation (same as calling `JSON.parse()` directly).
7+
*/
8+
protoAction?: 'error' | 'remove' | 'ignore',
9+
/**
10+
* What to do when a `constructor` key is found.
11+
* - `'error'` - throw a `SyntaxError` when a `constructor.prototype` key is found. This is the default value.
12+
* - `'remove'` - deletes any `constructor` keys from the result object.
13+
* - `'ignore'` - skips all validation (same as calling `JSON.parse()` directly).
14+
*/
15+
constructorAction?: 'error' | 'remove' | 'ignore',
16+
}
17+
18+
export type ScanOptions = {
19+
/**
20+
* What to do when a `__proto__` key is found.
21+
* - `'error'` - throw a `SyntaxError` when a `__proto__` key is found. This is the default value.
22+
* - `'remove'` - deletes any `__proto__` keys from the input `obj`.
23+
*/
24+
protoAction?: 'error' | 'remove',
25+
/**
26+
* What to do when a `constructor` key is found.
27+
* - `'error'` - throw a `SyntaxError` when a `constructor.prototype` key is found. This is the default value.
28+
* - `'remove'` - deletes any `constructor` keys from the input `obj`.
29+
*/
30+
constructorAction?: 'error' | 'remove',
31+
}
32+
33+
type Reviver = (this: any, key: string, value: any) => any
34+
35+
/**
36+
* Parses a given JSON-formatted text into an object.
37+
*
38+
* @param text The JSON text string.
39+
* @param reviver The `JSON.parse()` optional `reviver` argument.
40+
* @param options Optional configuration object.
41+
* @returns The parsed object.
42+
*/
43+
export function parse(text: string | Buffer, reviver?: Reviver | null, options?: ParseOptions): any
44+
45+
/**
46+
* Parses a given JSON-formatted text into an object.
47+
*
48+
* @param text The JSON text string.
49+
* @param reviver The `JSON.parse()` optional `reviver` argument.
50+
* @returns The parsed object, or `null` if there was an error or if the JSON contained possibly insecure properties.
51+
*/
52+
export function safeParse(text: string | Buffer, reviver?: Reviver | null): any
53+
54+
/**
55+
* Scans a given object for prototype properties.
56+
*
57+
* @param obj The object being scanned.
58+
* @param options Optional configuration object.
59+
*/
60+
export function scan(obj: any, options?: ScanOptions): void

index.test-d.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expectType, expectError } from 'tsd'
2+
import sjson = require('.')
3+
4+
expectError(sjson.parse(null))
5+
expectType<any>(sjson.parse('{"anything":0}'))
6+
7+
sjson.parse('"test"', null, { protoAction: 'remove' })
8+
expectError(sjson.parse('"test"', null, { protoAction: 'incorrect' }))
9+
sjson.parse('"test"', null, { constructorAction: 'ignore' })
10+
expectError(sjson.parse('"test"', null, { constructorAction: 'incorrect' }))
11+
12+
sjson.safeParse('"test"', null)
13+
sjson.safeParse('"test"')
14+
expectError(sjson.safeParse(null))
15+
16+
sjson.scan('"test"', { protoAction: 'remove' })
17+
expectError(sjson.scan('"test"', { protoAction: 'ignore' }))
18+
sjson.scan('"test"', { constructorAction: 'error' })
19+
expectError(sjson.scan('"test"', { constructorAction: 'ignore' }))
20+
21+
declare const input: Buffer
22+
sjson.parse(input)
23+
sjson.safeParse(input)
24+
25+
sjson.parse('{"anything":0}', (key, value) => {
26+
expectType<string>(key)
27+
})
28+
sjson.safeParse('{"anything":0}', (key, value) => {
29+
expectType<string>(key)
30+
})

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
"version": "2.3.2",
44
"description": "JSON parse with prototype poisoning protection",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"scripts": {
78
"benchmark": "cd benchmarks && npm install && npm run all",
8-
"test": "standard && nyc tape test.js",
9+
"test-legacy": "nyc tape test.js",
10+
"test": "standard && tsd && nyc tape test.js",
911
"test-in-browsers": "airtap test.js"
1012
},
1113
"repository": {
@@ -31,6 +33,7 @@
3133
"nyc": "^14.1.1",
3234
"playwright": "^1.7.1",
3335
"standard": "^16.0.0",
34-
"tape": "^5.1.1"
36+
"tape": "^5.1.1",
37+
"tsd": "^0.14.0"
3538
}
3639
}

0 commit comments

Comments
 (0)