Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support safeDestr #70

Merged
merged 3 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ Import into your Node.js project:

```js
// CommonJS
const { destr } = require("destr");
const { destr, safeDestr } = require("destr");

// ESM
import { destr } from "destr";
import { destr, safeDestr } from "destr";
```

### Deno

```js
import { destr } from "https://deno.land/x/destr/src/index.ts";
import { destr, safeDestr } from "https://deno.land/x/destr/src/index.ts";

console.log(destr('{ "deno": "yay" }'));
```
Expand Down Expand Up @@ -98,14 +98,14 @@ destr(input);

### Strict Mode

If `{ strict: true }` passed as second argument, `destr` 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)
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)

```js
// Returns "[foo"
destr("[foo");
safeDestr("[foo");

// Throws an error
destr("[foo", { strict: true });
safeDestr("[foo", { strict: true });
```

## Benchmarks
Expand Down
3 changes: 2 additions & 1 deletion lib/index.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { destr } = require("../dist/index.cjs");
const { destr, safeDestr } = require("../dist/index.cjs");

// Allow mixed default and named exports
destr.destr = destr;
destr.safeDestr = safeDestr;

module.exports = destr;
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ export function destr<T = unknown>(value: any, options: Options = {}): T {

if (!JsonSigRx.test(value)) {
if (options.strict) {
throw new SyntaxError("Invalid JSON");
throw new SyntaxError("[destr] Invalid JSON");
}
return value as T;
}

try {
if (suspectProtoRx.test(value) || suspectConstructorRx.test(value)) {
if (options.strict) {
throw new Error("[destr] Possible prototype pollution");
}
return JSON.parse(value, jsonParseTransform);
}
return JSON.parse(value);
Expand All @@ -78,4 +81,8 @@ export function destr<T = unknown>(value: any, options: Options = {}): T {
}
}

export function safeDestr<T = unknown>(value: any, options: Options = {}): T {
return destr<T>(value, { ...options, strict: true });
}

export default destr;
8 changes: 4 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, it, describe, vi } from "vitest";
import { destr } from "../src";
import { destr, safeDestr } from "../src";

describe("destr", () => {
it("returns the passed value if it's not a string", () => {
Expand Down Expand Up @@ -107,7 +107,7 @@ describe("destr", () => {
}
});

it("returns the passed string if it's a invalid JSON text and `strict` option is set `false`", () => {
it("returns the passed string if it's a invalid JSON text by default", () => {
const testCases = [
{ input: "{ " },
{ input: "[ " },
Expand All @@ -121,7 +121,7 @@ describe("destr", () => {
}
});

it("throws an error if it's a invalid JSON texts and `strict` option is set `true`", () => {
it("throws an error if it's a invalid JSON texts with safeDestr", () => {
const testCases = [
{ input: "{ ", output: "Unexpected end of JSON input" },
{ input: "[ ", output: "Unexpected end of JSON input" },
Expand All @@ -131,7 +131,7 @@ describe("destr", () => {
];

for (const testCase of testCases) {
expect(() => destr(testCase.input, { strict: true })).toThrowError(
expect(() => safeDestr(testCase.input)).toThrowError(
testCase.output || ""
);
}
Expand Down