unjs / destr Goto Github PK
View Code? Open in Web Editor NEWπ Faster, secure and convenient alternative for JSON.parse for artibrary inputs
License: MIT License
π Faster, secure and convenient alternative for JSON.parse for artibrary inputs
License: MIT License
Hey! Based on nuxt/nuxt#15736, we could give out warnings in dev
mode when keys are dropped (constructor
/__proto__
).
Would that make sense?
"node": ">=16",
"pnpm": ">=8.4.0"
Please use a browser with a version below iOS 14.4 to access.
https://stackblitz.com/edit/typescript-hlusqb?file=index.ts
The results displayed by the Chrome browser on iOS 14.4.
This is just a compatibility issue. When in iOS 14, an error occurs due to the incompatibility of the browser caused by the use of the "at" method. I'm not sure whether to fix it in this library, compile this library in my project, or provide a polyfill.
We use Nuxt (v3.8.2) to develop the application. It is possible that the use of renderJsonPayloads: true
in the experimental features causes the data to be parsed with destr. In most cases, this is not a problem, but someone reported a white screen issue on iOS 14.
No response
any
none
Its marketed as sometimes slower but most times faster than JSON.parse. But it is just a wrapper around JSON.parse with better types and quick checks before
No response
No response
TL;DR: After updating "a" laravel package our app broke because destr evaluates all responses as string.
We are using lates laravel as backend with a fairly new plain vue3 frontend.
We are using ofetch (former ohmyfetch) for fetching api responses.
After a update laravel resources responses now have a leading whitespace, thats the reason destr evaluates the response as a string.
How to reproduce:
destr('{}') // type object
destr(' {}') // type text
Is is possible to trim the data before try parsing it?
destr 2.0.1
Node.js 18
JSON.parse('"a\\nb"') == destr('"a\\nb"')
should be true
Line 40 in 769122d
At this line, destr returns string just stripped double quote.
No response
No response
Was searching all over unjs repo and Google to find the JSON.stringify equivalent of destr and didn't find anything.
If there's one please provide the name, otherwise take it as a feature request. :)
High appreciation to the unjs ecosystem. π₯ππ
There are some properties such as in nuxt content hooks that have this syntax and cannot be parsed.
'content:file:beforeParse'
will this be supported or is it not destr's responsibility?
If the string is longer than nine characters it cannot possibly match any of the hard-coded strings. So wrap the lowercasing operation in a conditional that prevents the operation if the string is ten characters or longer.
Hello, @pi0.
I found a memory leak, that affects Nuxt 3 framework, because the destr
package is used as default parseResponse
option in the ohmyfetch
, that used in the Nuxt 3 useFetch()
function as $fetch
.
Create a local TypeScript file (lib/use-fetch.ts
) with copy of useFetch()
function.
Add custom parseResponse()
function:
import type { FetchOptions } from 'ohmyfetch';
/**
* Parse response
* @description Replace of `destr()` function to prevent memory leak
* @param responseText
*/
const parseResponse: FetchOptions['parseResponse'] = (responseText) => {
// Send empty string if zero length
if (responseText.length === 0) {
return '';
}
try {
return JSON.parse(responseText);
} catch (_e) {
return responseText;
}
};
Replace _fetchOptions
constant in the copy of useFetch()
function:
const _fetchOptions = {
...opts,
parseResponse,
cache: typeof opts.cache === 'boolean' ? undefined : opts.cache
}
Use patched useFetch()
function instead of build-in.
Because, right now there is no way to provide custom parseResponse()
function as option of the useFetch()
without error.
Best wishes,
Sergey.
destr: 2.0.1(latest)
nodejs: 18.18.1
const value = Math.random().toString() // => '0.5207845927625354'
destr(value) // => '0.5207845927625354' β should be number
JSON.parse(value) // => 0.5207845927625354 π’ correct
Parsing a string of numbers with more than 14 decimals gives incorrect results, it should return the number but actually returns the original string.
This should have something to do with the regex hereοΌ
Line 8 in 3721051
No response
No response
none
See issues below
Right now, every constructor
key will be removed. Instead, only { "constructor": { "prototype": "<...>" } }
should be removed.
See https://github.com/fastify/secure-json-parse/blob/master/test/index.test.js#L228-L231
No response
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
@vitest/coverage-v8
, eslint-config-unjs
, pnpm
, prettier
, typescript
, vitest
)@vitest/coverage-v8
, vitest
).github/workflows/autofix.yml
actions/checkout v4
actions/setup-node v4
autofix-ci/action ff86a557419858bb967097bfc916833f5647fa8c
.github/workflows/ci.yml
actions/checkout v4
actions/setup-node v4
codecov/codecov-action v4
package.json
@hapi/bourne ^3.0.0
@vitest/coverage-v8 ^1.5.3
changelogen ^0.5.5
eslint ^8.57.0
eslint-config-unjs ^0.2.1
mitata ^0.1.11
prettier ^3.2.5
secure-json-parse ^2.7.0
typescript ^5.4.5
unbuild ^2.0.0
vitest ^1.5.3
pnpm 8.15.8
Many of our json data sources come from the backend, and there is an obvious need to convert the long
type numbers from the backend into strings for reception (otherwise the precision will be lost), this is json-big (or other Some packages) do, it would be great if this could be supported as a built-in configuration item!
Anyway, json+ts is really nice!
"A faster, secure and convenient alternative for JSON.parse.".
It will be interesting versus other JsonParser ...
I dont want to search all tool (maybe twenty) who suggests the same thing (and the latest lol) ...
I have no interest by this package because already 20 or more package do the "same" things and i dont know wich is the best :o .... and why this package is so special ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.