@stryke/json 0.8.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +11 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +1 -1
- package/dist/storm-json.cjs +3 -3
- package/dist/storm-json.mjs +1 -1
- package/dist/utils/index.cjs +11 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.mjs +1 -1
- package/dist/utils/parse.cjs +21 -20
- package/dist/utils/parse.mjs +1 -1
- package/dist/utils/strip-comments.cjs +37 -6
- package/dist/utils/strip-comments.d.ts +4 -1
- package/dist/utils/strip-comments.mjs +4 -1
- package/package.json +4 -5
package/dist/index.cjs
CHANGED
|
@@ -24,4 +24,15 @@ Object.keys(_types).forEach(function (key) {
|
|
|
24
24
|
return _types[key];
|
|
25
25
|
}
|
|
26
26
|
});
|
|
27
|
+
});
|
|
28
|
+
var _utils = require("./utils/index.cjs");
|
|
29
|
+
Object.keys(_utils).forEach(function (key) {
|
|
30
|
+
if (key === "default" || key === "__esModule") return;
|
|
31
|
+
if (key in exports && exports[key] === _utils[key]) return;
|
|
32
|
+
Object.defineProperty(exports, key, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () {
|
|
35
|
+
return _utils[key];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
27
38
|
});
|
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export*from"./storm-json";export*from"./types";
|
|
1
|
+
export*from"./storm-json";export*from"./types";export*from"./utils";
|
package/dist/storm-json.cjs
CHANGED
|
@@ -6,8 +6,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.StormJSON = void 0;
|
|
7
7
|
var _isObject = require("@stryke/type-checks/is-object");
|
|
8
8
|
var _isString = require("@stryke/type-checks/is-string");
|
|
9
|
-
var _buffer = require("buffer/");
|
|
10
9
|
var _jsoncParser = require("jsonc-parser");
|
|
10
|
+
var _nodeBuffer = require("node:buffer");
|
|
11
11
|
var _superjson = _interopRequireDefault(require("superjson"));
|
|
12
12
|
var _parse = require("./utils/parse.cjs");
|
|
13
13
|
var _parseError = require("./utils/parse-error.cjs");
|
|
@@ -66,7 +66,7 @@ class StormJSON extends _superjson.default {
|
|
|
66
66
|
}
|
|
67
67
|
exports.StormJSON = StormJSON;
|
|
68
68
|
StormJSON.instance.registerCustom({
|
|
69
|
-
isApplicable: i =>
|
|
69
|
+
isApplicable: i => _nodeBuffer.Buffer.isBuffer(i),
|
|
70
70
|
serialize: i => i.toString("base64"),
|
|
71
|
-
deserialize: i =>
|
|
71
|
+
deserialize: i => _nodeBuffer.Buffer.from(i, "base64")
|
|
72
72
|
}, "Bytes");
|
package/dist/storm-json.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{isObject as o}from"@stryke/type-checks/is-object";import{isString as l}from"@stryke/type-checks/is-string";import{
|
|
1
|
+
import{isObject as o}from"@stryke/type-checks/is-object";import{isString as l}from"@stryke/type-checks/is-string";import{parse as c}from"jsonc-parser";import{Buffer as a}from"node:buffer";import p from"superjson";import{parse as u}from"./utils/parse";import{formatParseError as f}from"./utils/parse-error";import{stringify as m}from"./utils/stringify";export class StormJSON extends p{static#r;static get instance(){return StormJSON.#r||(StormJSON.#r=new StormJSON),StormJSON.#r}static deserialize(r){return StormJSON.instance.deserialize(r)}static serialize(r){return StormJSON.instance.serialize(r)}static parse(r){return u(r)}static stringify(r,e){const s=StormJSON.instance.customTransformerRegistry.findApplicable(r);let t=r;return s&&s.isApplicable(r)&&(t=s.serialize(t)),m(t)}static parseJson(r,e){try{if(e?.expectComments===!1)return StormJSON.instance.parse(r)}catch{}const s=[],t={allowTrailingComma:!0,...e},n=c(r,s,t);if(s.length>0&&s[0])throw new Error(f(r,s[0]));return n}static register(r,e,s,t){StormJSON.instance.registerCustom({isApplicable:t,serialize:e,deserialize:s},r)}static registerClass(r,e){StormJSON.instance.registerClass(r,{identifier:l(e)?e:e?.identifier||r.name,allowProps:e&&o(e)&&e?.allowProps&&Array.isArray(e.allowProps)?e.allowProps:["__typename"]})}constructor(){super({dedupe:!0})}}StormJSON.instance.registerCustom({isApplicable:i=>a.isBuffer(i),serialize:i=>i.toString("base64"),deserialize:i=>a.from(i,"base64")},"Bytes");
|
package/dist/utils/index.cjs
CHANGED
|
@@ -14,6 +14,17 @@ Object.keys(_codeFrames).forEach(function (key) {
|
|
|
14
14
|
}
|
|
15
15
|
});
|
|
16
16
|
});
|
|
17
|
+
var _parse = require("./parse.cjs");
|
|
18
|
+
Object.keys(_parse).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (key in exports && exports[key] === _parse[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () {
|
|
24
|
+
return _parse[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
17
28
|
var _parseError = require("./parse-error.cjs");
|
|
18
29
|
Object.keys(_parseError).forEach(function (key) {
|
|
19
30
|
if (key === "default" || key === "__esModule") return;
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export*from"./code-frames";export*from"./parse-error";export*from"./stringify";export*from"./strip-comments";
|
|
1
|
+
export*from"./code-frames";export*from"./parse";export*from"./parse-error";export*from"./stringify";export*from"./strip-comments";
|
package/dist/utils/parse.cjs
CHANGED
|
@@ -5,21 +5,22 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.parse = parse;
|
|
7
7
|
exports.safeParse = safeParse;
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
var _stripComments = require("./strip-comments.cjs");
|
|
9
|
+
const o = /"(?:_|\\u0{2}5[Ff]){2}(?:p|\\u0{2}70)(?:r|\\u0{2}72)(?:o|\\u0{2}6[Ff])(?:t|\\u0{2}74)(?:o|\\u0{2}6[Ff])(?:_|\\u0{2}5[Ff]){2}"\s*:/,
|
|
10
|
+
u = /"(?: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*:/,
|
|
10
11
|
i = /^\s*["[{]|^\s*-?\d{1,16}(?:\.\d{1,17})?(?:E[+-]?\d+)?\s*$/i;
|
|
11
|
-
function
|
|
12
|
-
if (
|
|
13
|
-
console.warn(`Dropping "${
|
|
12
|
+
function c(t, n) {
|
|
13
|
+
if (t === "__proto__" || t === "constructor" && n && typeof n == "object" && "prototype" in n) {
|
|
14
|
+
console.warn(`Dropping "${t}" key to prevent prototype pollution.`);
|
|
14
15
|
return;
|
|
15
16
|
}
|
|
16
|
-
return
|
|
17
|
+
return n;
|
|
17
18
|
}
|
|
18
|
-
function parse(
|
|
19
|
-
if (typeof
|
|
19
|
+
function parse(t, n = {}) {
|
|
20
|
+
if (typeof t != "string") return t;
|
|
21
|
+
let r = (0, _stripComments.stripComments)(t);
|
|
20
22
|
if (r[0] === '"' && r[r.length - 1] === '"' && !r.includes("\\")) return r.slice(1, -1);
|
|
21
|
-
|
|
22
|
-
if (n.length <= 9) switch (n.toLowerCase()) {
|
|
23
|
+
if (r = r.trim(), r.length <= 9) switch (r.toLowerCase()) {
|
|
23
24
|
case "true":
|
|
24
25
|
return !0;
|
|
25
26
|
case "false":
|
|
@@ -36,23 +37,23 @@ function parse(r, t = {}) {
|
|
|
36
37
|
return Number.NEGATIVE_INFINITY;
|
|
37
38
|
}
|
|
38
39
|
if (!i.test(r)) {
|
|
39
|
-
if (
|
|
40
|
+
if (n.strict) throw new Error("Invalid JSON");
|
|
40
41
|
return r;
|
|
41
42
|
}
|
|
42
43
|
try {
|
|
43
|
-
if (
|
|
44
|
-
if (
|
|
45
|
-
return JSON.parse(r,
|
|
44
|
+
if (o.test(r) || u.test(r)) {
|
|
45
|
+
if (n.strict) throw new Error("Possible prototype pollution");
|
|
46
|
+
return JSON.parse(r, c);
|
|
46
47
|
}
|
|
47
48
|
return JSON.parse(r);
|
|
48
|
-
} catch (
|
|
49
|
-
if (
|
|
50
|
-
return
|
|
49
|
+
} catch (e) {
|
|
50
|
+
if (n.strict) throw e;
|
|
51
|
+
return t;
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
|
-
function safeParse(
|
|
54
|
-
return parse(
|
|
55
|
-
...
|
|
54
|
+
function safeParse(t, n = {}) {
|
|
55
|
+
return parse(t, {
|
|
56
|
+
...n,
|
|
56
57
|
strict: !0
|
|
57
58
|
});
|
|
58
59
|
}
|
package/dist/utils/parse.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const
|
|
1
|
+
import{stripComments as s}from"./strip-comments";const o=/"(?:_|\\u0{2}5[Ff]){2}(?:p|\\u0{2}70)(?:r|\\u0{2}72)(?:o|\\u0{2}6[Ff])(?:t|\\u0{2}74)(?:o|\\u0{2}6[Ff])(?:_|\\u0{2}5[Ff]){2}"\s*:/,u=/"(?: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*:/,i=/^\s*["[{]|^\s*-?\d{1,16}(?:\.\d{1,17})?(?:E[+-]?\d+)?\s*$/i;function c(t,n){if(t==="__proto__"||t==="constructor"&&n&&typeof n=="object"&&"prototype"in n){console.warn(`Dropping "${t}" key to prevent prototype pollution.`);return}return n}export function parse(t,n={}){if(typeof t!="string")return t;let r=s(t);if(r[0]==='"'&&r[r.length-1]==='"'&&!r.includes("\\"))return r.slice(1,-1);if(r=r.trim(),r.length<=9)switch(r.toLowerCase()){case"true":return!0;case"false":return!1;case"undefined":return;case"null":return null;case"nan":return Number.NaN;case"infinity":return Number.POSITIVE_INFINITY;case"-infinity":return Number.NEGATIVE_INFINITY}if(!i.test(r)){if(n.strict)throw new Error("Invalid JSON");return r}try{if(o.test(r)||u.test(r)){if(n.strict)throw new Error("Possible prototype pollution");return JSON.parse(r,c)}return JSON.parse(r)}catch(e){if(n.strict)throw e;return t}}export function safeParse(t,n={}){return parse(t,{...n,strict:!0})}
|
|
@@ -3,10 +3,41 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
exports.stripComments = stripComments;
|
|
7
|
+
var _base = require("@stryke/types/base");
|
|
8
|
+
const h = Symbol("singleComment"),
|
|
9
|
+
a = Symbol("multiComment");
|
|
10
|
+
function d() {
|
|
11
|
+
return "";
|
|
12
|
+
}
|
|
13
|
+
function C(t, f, c) {
|
|
14
|
+
return t.slice(f, c).replace(/\S/g, " ");
|
|
15
|
+
}
|
|
16
|
+
function y(t, f) {
|
|
17
|
+
let c = f - 1,
|
|
18
|
+
o = 0;
|
|
19
|
+
for (; t[c] === "\\";) c -= 1, o += 1;
|
|
20
|
+
return !!(o % 2);
|
|
21
|
+
}
|
|
22
|
+
function stripComments(t, {
|
|
23
|
+
whitespace: f = !0,
|
|
24
|
+
trailingCommas: c = !1
|
|
25
|
+
} = {}) {
|
|
26
|
+
if (typeof t != "string") throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof t}\``);
|
|
27
|
+
const o = f ? C : d;
|
|
28
|
+
let g = !1,
|
|
29
|
+
i = !1,
|
|
30
|
+
s = 0,
|
|
31
|
+
n = "",
|
|
32
|
+
b = "",
|
|
33
|
+
l = -1;
|
|
34
|
+
for (let e = 0; e < t.length; e++) {
|
|
35
|
+
const r = t[e],
|
|
36
|
+
m = t[e + 1];
|
|
37
|
+
!i && r === '"' && (y(t, e) || (g = !g)), !g && (!i && r + (m ?? _base.EMPTY_STRING) === "//" ? (n += t.slice(s, e), s = e, i = h, e++) : i === h && r + (m ?? _base.EMPTY_STRING) === `\r
|
|
38
|
+
` ? (e++, i = !1, n += o(t, s, e), s = e) : i === h && r === `
|
|
39
|
+
` ? (i = !1, n += o(t, s, e), s = e) : !i && r + (m ?? _base.EMPTY_STRING) === "/*" ? (n += t.slice(s, e), s = e, i = a, e++) : i === a && r + (m ?? _base.EMPTY_STRING) === "*/" ? (e++, i = !1, n += o(t, s, e + 1), s = e + 1) : c && !i && (l !== -1 ? r === "}" || r === "]" ? (n += t.slice(s, e), b += o(n, 0, 1) + n.slice(1), n = "", s = e, l = -1) : r !== " " && r !== " " && r !== "\r" && r !== `
|
|
40
|
+
` && (n += t.slice(s, e), s = e, l = -1) : r === "," && (b += n + t.slice(s, e), n = "", s = e, l = e)));
|
|
10
41
|
}
|
|
11
|
-
|
|
12
|
-
|
|
42
|
+
return b + n + (i ? o(t.slice(s)) : t.slice(s));
|
|
43
|
+
}
|
|
@@ -1 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import{EMPTY_STRING as p}from"@stryke/types/base";const h=Symbol("singleComment"),a=Symbol("multiComment");function d(){return""}function C(t,f,c){return t.slice(f,c).replace(/\S/g," ")}function y(t,f){let c=f-1,o=0;for(;t[c]==="\\";)c-=1,o+=1;return!!(o%2)}export function stripComments(t,{whitespace:f=!0,trailingCommas:c=!1}={}){if(typeof t!="string")throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof t}\``);const o=f?C:d;let g=!1,i=!1,s=0,n="",b="",l=-1;for(let e=0;e<t.length;e++){const r=t[e],m=t[e+1];!i&&r==='"'&&(y(t,e)||(g=!g)),!g&&(!i&&r+(m??p)==="//"?(n+=t.slice(s,e),s=e,i=h,e++):i===h&&r+(m??p)===`\r
|
|
2
|
+
`?(e++,i=!1,n+=o(t,s,e),s=e):i===h&&r===`
|
|
3
|
+
`?(i=!1,n+=o(t,s,e),s=e):!i&&r+(m??p)==="/*"?(n+=t.slice(s,e),s=e,i=a,e++):i===a&&r+(m??p)==="*/"?(e++,i=!1,n+=o(t,s,e+1),s=e+1):c&&!i&&(l!==-1?r==="}"||r==="]"?(n+=t.slice(s,e),b+=o(n,0,1)+n.slice(1),n="",s=e,l=-1):r!==" "&&r!==" "&&r!=="\r"&&r!==`
|
|
4
|
+
`&&(n+=t.slice(s,e),s=e,l=-1):r===","&&(b+=n+t.slice(s,e),n="",s=e,l=e)))}return b+n+(i?o(t.slice(s)):t.slice(s))}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stryke/json",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A package containing JSON parsing/stringify utilities used by Storm Software.",
|
|
6
6
|
"repository": {
|
|
@@ -11,14 +11,13 @@
|
|
|
11
11
|
"private": false,
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"buffer": "6.0.3",
|
|
14
|
-
"jsonc-parser": "3.3.1",
|
|
15
14
|
"lines-and-columns": "2.0.4",
|
|
16
15
|
"superjson": "2.2.2",
|
|
17
|
-
"@stryke/type-checks": "^0.3.
|
|
18
|
-
"@stryke/types": "^0.8.
|
|
16
|
+
"@stryke/type-checks": "^0.3.3",
|
|
17
|
+
"@stryke/types": "^0.8.3"
|
|
19
18
|
},
|
|
19
|
+
"devDependencies": { "@types/node": "^22.14.0" },
|
|
20
20
|
"publishConfig": { "access": "public" },
|
|
21
|
-
"devDependencies": {},
|
|
22
21
|
"sideEffects": false,
|
|
23
22
|
"files": ["dist/**/*"],
|
|
24
23
|
"homepage": "https://stormsoftware.com",
|