@stryke/json 0.5.8 → 0.6.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.
@@ -10,54 +10,51 @@ var _buffer = require("buffer/");
10
10
  var _jsoncParser = require("jsonc-parser");
11
11
  var _superjson = _interopRequireDefault(require("superjson"));
12
12
  var _parseError = require("./utils/parse-error.cjs");
13
+ var _stringify = require("./utils/stringify.cjs");
13
14
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
14
15
  class StormJSON extends _superjson.default {
15
- static #e;
16
+ static #r;
16
17
  static get instance() {
17
- return StormJSON.#e || (StormJSON.#e = new StormJSON()), StormJSON.#e;
18
+ return StormJSON.#r || (StormJSON.#r = new StormJSON()), StormJSON.#r;
18
19
  }
19
- static deserialize(e) {
20
- return StormJSON.instance.deserialize(e);
20
+ static deserialize(r) {
21
+ return StormJSON.instance.deserialize(r);
21
22
  }
22
- static serialize(e) {
23
- return StormJSON.instance.serialize(e);
23
+ static serialize(r) {
24
+ return StormJSON.instance.serialize(r);
24
25
  }
25
- static parse(e) {
26
- return StormJSON.instance.parse(e);
26
+ static parse(r) {
27
+ return StormJSON.instance.parse(r);
27
28
  }
28
- static stringify(e, r) {
29
- const s = StormJSON.instance.customTransformerRegistry.findApplicable(e);
30
- let t = e;
31
- return s && (t = s.serialize(t)), this.stringifyBase(t);
29
+ static stringify(r, e) {
30
+ const s = StormJSON.instance.customTransformerRegistry.findApplicable(r);
31
+ let t = r;
32
+ return s && (t = s.serialize(t)), (0, _stringify.stringify)(t);
32
33
  }
33
- static stringifyBase(e) {
34
- const r = StormJSON.instance.stringify(e);
35
- return r.startsWith('{"json":') ? r.substring(8, r.length - 1) : r;
36
- }
37
- static parseJson(e, r) {
34
+ static parseJson(r, e) {
38
35
  try {
39
- if (r?.expectComments === !1) return StormJSON.instance.parse(e);
36
+ if (e?.expectComments === !1) return StormJSON.instance.parse(r);
40
37
  } catch {}
41
38
  const s = [],
42
39
  t = {
43
40
  allowTrailingComma: !0,
44
- ...r
41
+ ...e
45
42
  },
46
- n = (0, _jsoncParser.parse)(e, s, t);
47
- if (s.length > 0 && s[0]) throw new Error((0, _parseError.formatParseError)(e, s[0]));
43
+ n = (0, _jsoncParser.parse)(r, s, t);
44
+ if (s.length > 0 && s[0]) throw new Error((0, _parseError.formatParseError)(r, s[0]));
48
45
  return n;
49
46
  }
50
- static register(e, r, s, t) {
47
+ static register(r, e, s, t) {
51
48
  StormJSON.instance.registerCustom({
52
49
  isApplicable: t,
53
- serialize: r,
50
+ serialize: e,
54
51
  deserialize: s
55
- }, e);
52
+ }, r);
56
53
  }
57
- static registerClass(e, r) {
58
- StormJSON.instance.registerClass(e, {
59
- identifier: (0, _isString.isString)(r) ? r : r?.identifier || e.name,
60
- allowProps: r && (0, _isObject.isObject)(r) && r?.allowProps && Array.isArray(r.allowProps) ? r.allowProps : ["__typename"]
54
+ static registerClass(r, e) {
55
+ StormJSON.instance.registerClass(r, {
56
+ identifier: (0, _isString.isString)(e) ? e : e?.identifier || r.name,
57
+ allowProps: e && (0, _isObject.isObject)(e) && e?.allowProps && Array.isArray(e.allowProps) ? e.allowProps : ["__typename"]
61
58
  });
62
59
  }
63
60
  constructor() {
@@ -35,13 +35,6 @@ export declare class StormJSON extends SuperJSON {
35
35
  * @returns the formatted JSON representation of the object
36
36
  */
37
37
  static stringify<T>(value: T, _options?: JsonSerializeOptions): string;
38
- /**
39
- * Stringify the given value with superjson
40
- *
41
- * @param obj - The object to stringify
42
- * @returns The stringified object
43
- */
44
- static stringifyBase(obj: any): string;
45
38
  /**
46
39
  * Parses the given JSON string and returns the object the JSON content represents.
47
40
  * By default javascript-style comments and trailing commas are allowed.
@@ -1 +1 @@
1
- import{isObject as o}from"@stryke/type-checks/is-object";import{isString as l}from"@stryke/type-checks/is-string";import{Buffer as a}from"buffer/";import{parse as c}from"jsonc-parser";import u from"superjson";import{formatParseError as p}from"./utils/parse-error";export class StormJSON extends u{static#e;static get instance(){return StormJSON.#e||(StormJSON.#e=new StormJSON),StormJSON.#e}static deserialize(e){return StormJSON.instance.deserialize(e)}static serialize(e){return StormJSON.instance.serialize(e)}static parse(e){return StormJSON.instance.parse(e)}static stringify(e,r){const s=StormJSON.instance.customTransformerRegistry.findApplicable(e);let t=e;return s&&(t=s.serialize(t)),this.stringifyBase(t)}static stringifyBase(e){const r=StormJSON.instance.stringify(e);return r.startsWith('{"json":')?r.substring(8,r.length-1):r}static parseJson(e,r){try{if(r?.expectComments===!1)return StormJSON.instance.parse(e)}catch{}const s=[],t={allowTrailingComma:!0,...r},n=c(e,s,t);if(s.length>0&&s[0])throw new Error(p(e,s[0]));return n}static register(e,r,s,t){StormJSON.instance.registerCustom({isApplicable:t,serialize:r,deserialize:s},e)}static registerClass(e,r){StormJSON.instance.registerClass(e,{identifier:l(r)?r:r?.identifier||e.name,allowProps:r&&o(r)&&r?.allowProps&&Array.isArray(r.allowProps)?r.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");
1
+ import{isObject as o}from"@stryke/type-checks/is-object";import{isString as l}from"@stryke/type-checks/is-string";import{Buffer as a}from"buffer/";import{parse as c}from"jsonc-parser";import p from"superjson";import{formatParseError as u}from"./utils/parse-error";import{stringify as f}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 StormJSON.instance.parse(r)}static stringify(r,e){const s=StormJSON.instance.customTransformerRegistry.findApplicable(r);let t=r;return s&&(t=s.serialize(t)),f(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(u(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");
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.parse = parse;
7
+ exports.safeParse = safeParse;
8
+ const e = /"(?:_|\\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*:/,
9
+ o = /"(?: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
+ i = /^\s*["[{]|^\s*-?\d{1,16}(?:\.\d{1,17})?(?:E[+-]?\d+)?\s*$/i;
11
+ function u(r, t) {
12
+ if (r === "__proto__" || r === "constructor" && t && typeof t == "object" && "prototype" in t) {
13
+ console.warn(`Dropping "${r}" key to prevent prototype pollution.`);
14
+ return;
15
+ }
16
+ return t;
17
+ }
18
+ function parse(r, t = {}) {
19
+ if (typeof r != "string") return r;
20
+ if (r[0] === '"' && r[r.length - 1] === '"' && !r.includes("\\")) return r.slice(1, -1);
21
+ const n = r.trim();
22
+ if (n.length <= 9) switch (n.toLowerCase()) {
23
+ case "true":
24
+ return !0;
25
+ case "false":
26
+ return !1;
27
+ case "undefined":
28
+ return;
29
+ case "null":
30
+ return null;
31
+ case "nan":
32
+ return Number.NaN;
33
+ case "infinity":
34
+ return Number.POSITIVE_INFINITY;
35
+ case "-infinity":
36
+ return Number.NEGATIVE_INFINITY;
37
+ }
38
+ if (!i.test(r)) {
39
+ if (t.strict) throw new Error("Invalid JSON");
40
+ return r;
41
+ }
42
+ try {
43
+ if (e.test(r) || o.test(r)) {
44
+ if (t.strict) throw new Error("Possible prototype pollution");
45
+ return JSON.parse(r, u);
46
+ }
47
+ return JSON.parse(r);
48
+ } catch (s) {
49
+ if (t.strict) throw s;
50
+ return r;
51
+ }
52
+ }
53
+ function safeParse(r, t = {}) {
54
+ return parse(r, {
55
+ ...t,
56
+ strict: !0
57
+ });
58
+ }
@@ -0,0 +1,5 @@
1
+ export interface Options {
2
+ strict?: boolean;
3
+ }
4
+ export declare function parse<T = unknown>(value: any, options?: Options): T;
5
+ export declare function safeParse<T = unknown>(value: any, options?: Options): T;
@@ -0,0 +1 @@
1
+ const e=/"(?:_|\\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*:/,o=/"(?: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 u(r,t){if(r==="__proto__"||r==="constructor"&&t&&typeof t=="object"&&"prototype"in t){console.warn(`Dropping "${r}" key to prevent prototype pollution.`);return}return t}export function parse(r,t={}){if(typeof r!="string")return r;if(r[0]==='"'&&r[r.length-1]==='"'&&!r.includes("\\"))return r.slice(1,-1);const n=r.trim();if(n.length<=9)switch(n.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(t.strict)throw new Error("Invalid JSON");return r}try{if(e.test(r)||o.test(r)){if(t.strict)throw new Error("Possible prototype pollution");return JSON.parse(r,u)}return JSON.parse(r)}catch(s){if(t.strict)throw s;return r}}export function safeParse(r,t={}){return parse(r,{...t,strict:!0})}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/json",
3
- "version": "0.5.8",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "description": "A package containing JSON parsing/stringify utilities used by Storm Software.",
6
6
  "repository": {
@@ -120,6 +120,20 @@
120
120
  "default": "./dist/utils/stringify.mjs"
121
121
  }
122
122
  },
123
+ "./utils/parse": {
124
+ "import": {
125
+ "types": "./dist/utils/parse.d.ts",
126
+ "default": "./dist/utils/parse.mjs"
127
+ },
128
+ "require": {
129
+ "types": "./dist/utils/parse.d.ts",
130
+ "default": "./dist/utils/parse.cjs"
131
+ },
132
+ "default": {
133
+ "types": "./dist/utils/parse.d.ts",
134
+ "default": "./dist/utils/parse.mjs"
135
+ }
136
+ },
123
137
  "./utils/parse-error": {
124
138
  "import": {
125
139
  "types": "./dist/utils/parse-error.d.ts",