Detalhes do pacote

destr

unjs11.7mMIT2.0.5

A faster, secure and convenient alternative for JSON.parse

readme (leia-me)

destr

npm version npm downloads bundle License

A faster, secure and convenient alternative for JSON.parse.

Usage

Node.js

Install dependency:

# npm
npm i destr

# yarn
yarn add destr

# pnpm
pnpm i destr

Import into your Node.js project:

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

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

Deno

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

console.log(destr('{ "deno": "yay" }'));

Why?

✅ Type Safe

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

// Uncaught SyntaxError: Unexpected token u in JSON at position 0
JSON.parse();

// undefined
destr();

✅ Fast lookup for known string values

// Uncaught SyntaxError: Unexpected token T in JSON at position 0
JSON.parse("TRUE");

// true
destr("TRUE");

✅ Fallback to original value if parse fails (empty or any plain string)

// Uncaught SyntaxError: Unexpected token s in JSON at position 0
JSON.parse("salam");

// "salam"
destr("salam");

Note: This fails in safe/strict mode with safeDestr.

✅ Avoid prototype pollution

const input = '{ "user": { "__proto__": { "isAdmin": true } } }';

// { user: { __proto__: { isAdmin: true } } }
JSON.parse(input);

// { user: {} }
destr(input);

✅ Strict Mode

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)

// Returns "[foo"
destr("[foo");

// Throws an error
safeDestr("[foo");

Benchmarks

destr is faster generally for arbitrary inputs but also sometimes little bit slower than JSON.parse when parsing a valid JSON string mainly because of transform to avoid prototype pollution 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.

Check Benchmark Results or run with pnpm run bench:node or pnpm run bench:bun yourself!

License

MIT. Made with 💖

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v2.0.5

compare changes

📦 Build

  • Downgrade unbuild to avoid cjs type regression (09889c2)

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.0.4

compare changes

🔥 Performance

  • Faster plain string and known value checks (#136)

📦 Build

  • Add sideEffects to package.json (#137)

🏡 Chore

❤️ Contributors

v2.0.3

compare changes

🩹 Fixes

  • Improve compatibility with runtimes not supporting String.prototype.at() (#102)

🏡 Chore

✅ Tests

❤️ Contributors

v2.0.2

compare changes

🩹 Fixes

  • Parsing decimals and scientific notation (#94)
  • Avoid fast path with possible escape chars (#89)

📖 Documentation

  • Fix typos (#82)
  • Fix typo (#86)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)
  • Kricsleo
  • Nobkd
  • Alexander Lichter (@manniL)

v2.0.1

compare changes

🔥 Performance

  • Avoid lowercasing long strings (#81)

📖 Documentation

  • Correct safeDestr example usage and tests (#75)

🏡 Chore

🎨 Styles

❤️ Contributors

v2.0.0

compare changes

🚀 Enhancements

  • ⚠️ Support generic type with unkown default (#68)
  • Show warning when dropping unsafe keys (#57)
  • Support minus infinity (#67)
  • Support safeDestr (#70)
  • Parse double-quoted string with fast path (#71)

🔥 Performance

  • Move common check earlier (5be5732)

💅 Refactors

  • ⚠️ Use named destr export (#69)

🏡 Chore

⚠️ Breaking Changes

  • ⚠️ Support generic type with unkown default (#68)
  • ⚠️ Use named destr export (#69)

❤️ Contributors

1.2.2 (2022-12-05)

Bug Fixes

  • only purge constructor.prototype keys (#26) (87918d5)
  • support surrounding whitespaces (resolves #21) (639a5df)

1.2.1 (2022-11-14)

1.2.0 (2022-10-19)

Features

  • add option strict that throws an error if the input is not valid JSON (#11) (36c7121)

1.1.1 (2022-04-07)

1.1.0 (2021-01-21)

Features

1.0.1 (2020-11-08)

Bug Fixes

  • don't parse numbers if potential to exceed 15 digits (bc8c596)

1.0.0 (2020-06-16)

0.1.9 (2020-05-28)

Bug Fixes

0.1.8 (2020-05-28)

Bug Fixes

  • types: remove strict types (1513a48)

0.1.7 (2020-05-27)

Bug Fixes

  • don't throw error on parse fail (65e22c6)

0.1.6 (2020-05-27)

0.1.5 (2020-05-27)

Bug Fixes

  • use JsonSigRx to also match numbers (3023552)

0.1.4 (2020-05-22)

0.1.3 (2020-05-20)

Bug Fixes

0.1.2 (2020-05-20)

0.1.1 (2020-05-20)