@stryke/url 0.3.25 → 0.3.27
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/CHANGELOG.md +14 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -1
- package/dist/helpers.cjs +28 -1
- package/dist/helpers.mjs +26 -1
- package/dist/helpers.mjs.map +1 -1
- package/dist/index.cjs +5 -1
- package/dist/index.mjs +4 -1
- package/dist/json/src/storm-json.cjs +117 -1
- package/dist/json/src/storm-json.mjs +115 -1
- package/dist/json/src/storm-json.mjs.map +1 -1
- package/dist/json/src/utils/code-frames.cjs +77 -3
- package/dist/json/src/utils/code-frames.mjs +76 -3
- package/dist/json/src/utils/code-frames.mjs.map +1 -1
- package/dist/json/src/utils/parse-error.cjs +34 -1
- package/dist/json/src/utils/parse-error.mjs +33 -1
- package/dist/json/src/utils/parse-error.mjs.map +1 -1
- package/dist/json/src/utils/parse.cjs +45 -1
- package/dist/json/src/utils/parse.mjs +45 -1
- package/dist/json/src/utils/parse.mjs.map +1 -1
- package/dist/json/src/utils/stringify.cjs +67 -1
- package/dist/json/src/utils/stringify.mjs +67 -1
- package/dist/json/src/utils/stringify.mjs.map +1 -1
- package/dist/json/src/utils/strip-comments.cjs +86 -4
- package/dist/json/src/utils/strip-comments.mjs +86 -4
- package/dist/json/src/utils/strip-comments.mjs.map +1 -1
- package/dist/path/src/is-type.cjs +28 -1
- package/dist/path/src/is-type.mjs +28 -1
- package/dist/path/src/is-type.mjs.map +1 -1
- package/dist/path/src/join-paths.cjs +106 -1
- package/dist/path/src/join-paths.mjs +106 -1
- package/dist/path/src/join-paths.mjs.map +1 -1
- package/dist/path/src/regex.cjs +12 -1
- package/dist/path/src/regex.mjs +8 -1
- package/dist/path/src/regex.mjs.map +1 -1
- package/dist/path/src/slash.cjs +15 -1
- package/dist/path/src/slash.mjs +14 -1
- package/dist/path/src/slash.mjs.map +1 -1
- package/dist/storm-url.cjs +227 -1
- package/dist/storm-url.mjs +226 -1
- package/dist/storm-url.mjs.map +1 -1
- package/dist/type-checks/src/get-object-tag.cjs +15 -1
- package/dist/type-checks/src/get-object-tag.mjs +14 -1
- package/dist/type-checks/src/get-object-tag.mjs.map +1 -1
- package/dist/type-checks/src/is-empty.cjs +20 -1
- package/dist/type-checks/src/is-empty.mjs +20 -1
- package/dist/type-checks/src/is-empty.mjs.map +1 -1
- package/dist/type-checks/src/is-integer.cjs +14 -1
- package/dist/type-checks/src/is-integer.mjs +14 -1
- package/dist/type-checks/src/is-integer.mjs.map +1 -1
- package/dist/type-checks/src/is-null.cjs +12 -1
- package/dist/type-checks/src/is-null.mjs +11 -1
- package/dist/type-checks/src/is-null.mjs.map +1 -1
- package/dist/type-checks/src/is-number.cjs +18 -1
- package/dist/type-checks/src/is-number.mjs +17 -1
- package/dist/type-checks/src/is-number.mjs.map +1 -1
- package/dist/type-checks/src/is-object.cjs +19 -1
- package/dist/type-checks/src/is-object.mjs +19 -1
- package/dist/type-checks/src/is-object.mjs.map +1 -1
- package/dist/type-checks/src/is-plain-object.cjs +63 -1
- package/dist/type-checks/src/is-plain-object.mjs +63 -1
- package/dist/type-checks/src/is-plain-object.mjs.map +1 -1
- package/dist/type-checks/src/is-set-string.cjs +20 -1
- package/dist/type-checks/src/is-set-string.mjs +20 -1
- package/dist/type-checks/src/is-set-string.mjs.map +1 -1
- package/dist/type-checks/src/is-set.cjs +19 -1
- package/dist/type-checks/src/is-set.mjs +19 -1
- package/dist/type-checks/src/is-set.mjs.map +1 -1
- package/dist/type-checks/src/is-string.cjs +12 -1
- package/dist/type-checks/src/is-string.mjs +11 -1
- package/dist/type-checks/src/is-string.mjs.map +1 -1
- package/dist/type-checks/src/is-undefined.cjs +8 -1
- package/dist/type-checks/src/is-undefined.mjs +7 -1
- package/dist/type-checks/src/is-undefined.mjs.map +1 -1
- package/dist/types/src/base.cjs +6 -1
- package/dist/types/src/base.mjs +5 -1
- package/dist/types/src/base.mjs.map +1 -1
- package/dist/types.cjs +6 -1
- package/dist/types.mjs +5 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1,34 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_code_frames = require('./code-frames.cjs');
|
|
3
|
+
let jsonc_parser = require("jsonc-parser");
|
|
4
|
+
let lines_and_columns = require("lines-and-columns");
|
|
5
|
+
|
|
6
|
+
//#region ../json/src/utils/parse-error.ts
|
|
7
|
+
/**
|
|
8
|
+
* Nicely formats a JSON error with context
|
|
9
|
+
*
|
|
10
|
+
* @param input - JSON content as string
|
|
11
|
+
* @param parseError - jsonc ParseError
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
function formatParseError(input, parseError) {
|
|
15
|
+
const { error, offset, length } = parseError;
|
|
16
|
+
const result = new lines_and_columns.LinesAndColumns(input).locationForIndex(offset);
|
|
17
|
+
let line = result?.line ?? 0;
|
|
18
|
+
let column = result?.column ?? 0;
|
|
19
|
+
line++;
|
|
20
|
+
column++;
|
|
21
|
+
return `${(0, jsonc_parser.printParseErrorCode)(error)} in JSON at ${line}:${column}\n${require_code_frames.codeFrameColumns(input, {
|
|
22
|
+
start: {
|
|
23
|
+
line,
|
|
24
|
+
column
|
|
25
|
+
},
|
|
26
|
+
end: {
|
|
27
|
+
line,
|
|
28
|
+
column: column + length
|
|
29
|
+
}
|
|
30
|
+
})}\n`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
exports.formatParseError = formatParseError;
|
|
@@ -1,2 +1,34 @@
|
|
|
1
|
-
import{codeFrameColumns
|
|
1
|
+
import { codeFrameColumns } from "./code-frames.mjs";
|
|
2
|
+
import { printParseErrorCode } from "jsonc-parser";
|
|
3
|
+
import { LinesAndColumns } from "lines-and-columns";
|
|
4
|
+
|
|
5
|
+
//#region ../json/src/utils/parse-error.ts
|
|
6
|
+
/**
|
|
7
|
+
* Nicely formats a JSON error with context
|
|
8
|
+
*
|
|
9
|
+
* @param input - JSON content as string
|
|
10
|
+
* @param parseError - jsonc ParseError
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
function formatParseError(input, parseError) {
|
|
14
|
+
const { error, offset, length } = parseError;
|
|
15
|
+
const result = new LinesAndColumns(input).locationForIndex(offset);
|
|
16
|
+
let line = result?.line ?? 0;
|
|
17
|
+
let column = result?.column ?? 0;
|
|
18
|
+
line++;
|
|
19
|
+
column++;
|
|
20
|
+
return `${printParseErrorCode(error)} in JSON at ${line}:${column}\n${codeFrameColumns(input, {
|
|
21
|
+
start: {
|
|
22
|
+
line,
|
|
23
|
+
column
|
|
24
|
+
},
|
|
25
|
+
end: {
|
|
26
|
+
line,
|
|
27
|
+
column: column + length
|
|
28
|
+
}
|
|
29
|
+
})}\n`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
export { formatParseError };
|
|
2
34
|
//# sourceMappingURL=parse-error.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-error.mjs","names":[],"sources":["../../../../../json/src/utils/parse-error.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { ParseError } from \"jsonc-parser\";\nimport { printParseErrorCode } from \"jsonc-parser\";\nimport { LinesAndColumns } from \"lines-and-columns\";\nimport { codeFrameColumns } from \"./code-frames\";\n\n/**\n * Nicely formats a JSON error with context\n *\n * @param input - JSON content as string\n * @param parseError - jsonc ParseError\n * @returns\n */\nexport function formatParseError(input: string, parseError: ParseError) {\n const { error, offset, length } = parseError;\n const result = new LinesAndColumns(input).locationForIndex(offset);\n let line = result?.line ?? 0;\n let column = result?.column ?? 0;\n\n line++;\n column++;\n\n return `${printParseErrorCode(error)} in JSON at ${line}:${column}\\n${codeFrameColumns(\n input,\n {\n start: {\n line,\n column\n },\n end: {\n line,\n column: column + length\n }\n }\n )}\\n`;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"parse-error.mjs","names":[],"sources":["../../../../../json/src/utils/parse-error.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { ParseError } from \"jsonc-parser\";\nimport { printParseErrorCode } from \"jsonc-parser\";\nimport { LinesAndColumns } from \"lines-and-columns\";\nimport { codeFrameColumns } from \"./code-frames\";\n\n/**\n * Nicely formats a JSON error with context\n *\n * @param input - JSON content as string\n * @param parseError - jsonc ParseError\n * @returns\n */\nexport function formatParseError(input: string, parseError: ParseError) {\n const { error, offset, length } = parseError;\n const result = new LinesAndColumns(input).locationForIndex(offset);\n let line = result?.line ?? 0;\n let column = result?.column ?? 0;\n\n line++;\n column++;\n\n return `${printParseErrorCode(error)} in JSON at ${line}:${column}\\n${codeFrameColumns(\n input,\n {\n start: {\n line,\n column\n },\n end: {\n line,\n column: column + length\n }\n }\n )}\\n`;\n}\n"],"mappings":";;;;;;;;;;;;AA8BA,SAAgB,iBAAiB,OAAe,YAAwB;CACtE,MAAM,EAAE,OAAO,QAAQ,WAAW;CAClC,MAAM,SAAS,IAAI,gBAAgB,MAAM,CAAC,iBAAiB,OAAO;CAClE,IAAI,OAAO,QAAQ,QAAQ;CAC3B,IAAI,SAAS,QAAQ,UAAU;AAE/B;AACA;AAEA,QAAO,GAAG,oBAAoB,MAAM,CAAC,cAAc,KAAK,GAAG,OAAO,IAAI,iBACpE,OACA;EACE,OAAO;GACL;GACA;GACD;EACD,KAAK;GACH;GACA,QAAQ,SAAS;GAClB;EACF,CACF,CAAC"}
|
|
@@ -1 +1,45 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_strip_comments = require('./strip-comments.cjs');
|
|
2
|
+
|
|
3
|
+
//#region ../json/src/utils/parse.ts
|
|
4
|
+
const suspectProtoRx = /"(?:_|\\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*:/;
|
|
5
|
+
const suspectConstructorRx = /"(?: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*:/;
|
|
6
|
+
const JsonSigRx = /^\s*["[{]|^\s*-?\d{1,16}(?:\.\d{1,17})?(?:E[+-]?\d+)?\s*$/i;
|
|
7
|
+
function jsonParseTransform(key, value) {
|
|
8
|
+
if (key === "__proto__" || key === "constructor" && value && typeof value === "object" && "prototype" in value) {
|
|
9
|
+
console.warn(`Dropping "${key}" key to prevent prototype pollution.`);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
function parse(value, options = {}) {
|
|
15
|
+
if (typeof value !== "string") return value;
|
|
16
|
+
let stripped = require_strip_comments.stripComments(value);
|
|
17
|
+
if (stripped[0] === "\"" && stripped[stripped.length - 1] === "\"" && !stripped.includes("\\")) return stripped.slice(1, -1);
|
|
18
|
+
stripped = stripped.trim();
|
|
19
|
+
if (stripped.length <= 9) switch (stripped.toLowerCase()) {
|
|
20
|
+
case "true": return true;
|
|
21
|
+
case "false": return false;
|
|
22
|
+
case "undefined": return;
|
|
23
|
+
case "null": return null;
|
|
24
|
+
case "nan": return NaN;
|
|
25
|
+
case "infinity": return Number.POSITIVE_INFINITY;
|
|
26
|
+
case "-infinity": return Number.NEGATIVE_INFINITY;
|
|
27
|
+
}
|
|
28
|
+
if (!JsonSigRx.test(stripped)) {
|
|
29
|
+
if (options.strict) throw new Error("Invalid JSON");
|
|
30
|
+
return stripped;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
if (suspectProtoRx.test(stripped) || suspectConstructorRx.test(stripped)) {
|
|
34
|
+
if (options.strict) throw new Error("Possible prototype pollution");
|
|
35
|
+
return JSON.parse(stripped, jsonParseTransform);
|
|
36
|
+
}
|
|
37
|
+
return JSON.parse(stripped);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (options.strict) throw error;
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
exports.parse = parse;
|
|
@@ -1,2 +1,46 @@
|
|
|
1
|
-
import{stripComments
|
|
1
|
+
import { stripComments } from "./strip-comments.mjs";
|
|
2
|
+
|
|
3
|
+
//#region ../json/src/utils/parse.ts
|
|
4
|
+
const suspectProtoRx = /"(?:_|\\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*:/;
|
|
5
|
+
const suspectConstructorRx = /"(?: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*:/;
|
|
6
|
+
const JsonSigRx = /^\s*["[{]|^\s*-?\d{1,16}(?:\.\d{1,17})?(?:E[+-]?\d+)?\s*$/i;
|
|
7
|
+
function jsonParseTransform(key, value) {
|
|
8
|
+
if (key === "__proto__" || key === "constructor" && value && typeof value === "object" && "prototype" in value) {
|
|
9
|
+
console.warn(`Dropping "${key}" key to prevent prototype pollution.`);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
function parse(value, options = {}) {
|
|
15
|
+
if (typeof value !== "string") return value;
|
|
16
|
+
let stripped = stripComments(value);
|
|
17
|
+
if (stripped[0] === "\"" && stripped[stripped.length - 1] === "\"" && !stripped.includes("\\")) return stripped.slice(1, -1);
|
|
18
|
+
stripped = stripped.trim();
|
|
19
|
+
if (stripped.length <= 9) switch (stripped.toLowerCase()) {
|
|
20
|
+
case "true": return true;
|
|
21
|
+
case "false": return false;
|
|
22
|
+
case "undefined": return;
|
|
23
|
+
case "null": return null;
|
|
24
|
+
case "nan": return NaN;
|
|
25
|
+
case "infinity": return Number.POSITIVE_INFINITY;
|
|
26
|
+
case "-infinity": return Number.NEGATIVE_INFINITY;
|
|
27
|
+
}
|
|
28
|
+
if (!JsonSigRx.test(stripped)) {
|
|
29
|
+
if (options.strict) throw new Error("Invalid JSON");
|
|
30
|
+
return stripped;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
if (suspectProtoRx.test(stripped) || suspectConstructorRx.test(stripped)) {
|
|
34
|
+
if (options.strict) throw new Error("Possible prototype pollution");
|
|
35
|
+
return JSON.parse(stripped, jsonParseTransform);
|
|
36
|
+
}
|
|
37
|
+
return JSON.parse(stripped);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (options.strict) throw error;
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
export { parse };
|
|
2
46
|
//# sourceMappingURL=parse.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.mjs","names":[],"sources":["../../../../../json/src/utils/parse.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { stripComments } from \"./strip-comments\";\n\n// https://github.com/fastify/secure-json-parse\n// https://github.com/hapijs/bourne\nconst suspectProtoRx =\n /\"(?:_|\\\\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*:/;\nconst suspectConstructorRx =\n /\"(?: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*:/;\n\nconst JsonSigRx = /^\\s*[\"[{]|^\\s*-?\\d{1,16}(?:\\.\\d{1,17})?(?:E[+-]?\\d+)?\\s*$/i;\n\nfunction jsonParseTransform(key: string, value: any): any {\n if (\n key === \"__proto__\" ||\n (key === \"constructor\" &&\n value &&\n typeof value === \"object\" &&\n \"prototype\" in value)\n ) {\n // eslint-disable-next-line no-console\n console.warn(`Dropping \"${key}\" key to prevent prototype pollution.`);\n return;\n }\n return value;\n}\n\nexport interface Options {\n strict?: boolean;\n}\n\nexport function parse<T = unknown>(value: any, options: Options = {}): T {\n if (typeof value !== \"string\") {\n return value;\n }\n\n let stripped = stripComments(value);\n\n if (\n stripped[0] === '\"' &&\n stripped[stripped.length - 1] === '\"' &&\n !stripped.includes(\"\\\\\")\n ) {\n return stripped.slice(1, -1) as T;\n }\n\n stripped = stripped.trim();\n\n if (stripped.length <= 9) {\n switch (stripped.toLowerCase()) {\n case \"true\": {\n return true as T;\n }\n case \"false\": {\n return false as T;\n }\n case \"undefined\": {\n return undefined as T;\n }\n case \"null\": {\n return null as T;\n }\n case \"nan\": {\n return Number.NaN as T;\n }\n case \"infinity\": {\n return Number.POSITIVE_INFINITY as T;\n }\n case \"-infinity\": {\n return Number.NEGATIVE_INFINITY as T;\n }\n }\n }\n\n if (!JsonSigRx.test(stripped)) {\n if (options.strict) {\n throw new Error(\"Invalid JSON\");\n }\n return stripped as T;\n }\n\n try {\n if (suspectProtoRx.test(stripped) || suspectConstructorRx.test(stripped)) {\n if (options.strict) {\n throw new Error(\"Possible prototype pollution\");\n }\n return JSON.parse(stripped, jsonParseTransform);\n }\n\n return JSON.parse(stripped);\n } catch (error) {\n if (options.strict) {\n throw error;\n }\n return value as T;\n }\n}\n\nexport function safeParse<T = unknown>(value: any, options: Options = {}): T {\n return parse<T>(value, { ...options, strict: true });\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"parse.mjs","names":[],"sources":["../../../../../json/src/utils/parse.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { stripComments } from \"./strip-comments\";\n\n// https://github.com/fastify/secure-json-parse\n// https://github.com/hapijs/bourne\nconst suspectProtoRx =\n /\"(?:_|\\\\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*:/;\nconst suspectConstructorRx =\n /\"(?: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*:/;\n\nconst JsonSigRx = /^\\s*[\"[{]|^\\s*-?\\d{1,16}(?:\\.\\d{1,17})?(?:E[+-]?\\d+)?\\s*$/i;\n\nfunction jsonParseTransform(key: string, value: any): any {\n if (\n key === \"__proto__\" ||\n (key === \"constructor\" &&\n value &&\n typeof value === \"object\" &&\n \"prototype\" in value)\n ) {\n // eslint-disable-next-line no-console\n console.warn(`Dropping \"${key}\" key to prevent prototype pollution.`);\n return;\n }\n return value;\n}\n\nexport interface Options {\n strict?: boolean;\n}\n\nexport function parse<T = unknown>(value: any, options: Options = {}): T {\n if (typeof value !== \"string\") {\n return value;\n }\n\n let stripped = stripComments(value);\n\n if (\n stripped[0] === '\"' &&\n stripped[stripped.length - 1] === '\"' &&\n !stripped.includes(\"\\\\\")\n ) {\n return stripped.slice(1, -1) as T;\n }\n\n stripped = stripped.trim();\n\n if (stripped.length <= 9) {\n switch (stripped.toLowerCase()) {\n case \"true\": {\n return true as T;\n }\n case \"false\": {\n return false as T;\n }\n case \"undefined\": {\n return undefined as T;\n }\n case \"null\": {\n return null as T;\n }\n case \"nan\": {\n return Number.NaN as T;\n }\n case \"infinity\": {\n return Number.POSITIVE_INFINITY as T;\n }\n case \"-infinity\": {\n return Number.NEGATIVE_INFINITY as T;\n }\n }\n }\n\n if (!JsonSigRx.test(stripped)) {\n if (options.strict) {\n throw new Error(\"Invalid JSON\");\n }\n return stripped as T;\n }\n\n try {\n if (suspectProtoRx.test(stripped) || suspectConstructorRx.test(stripped)) {\n if (options.strict) {\n throw new Error(\"Possible prototype pollution\");\n }\n return JSON.parse(stripped, jsonParseTransform);\n }\n\n return JSON.parse(stripped);\n } catch (error) {\n if (options.strict) {\n throw error;\n }\n return value as T;\n }\n}\n\nexport function safeParse<T = unknown>(value: any, options: Options = {}): T {\n return parse<T>(value, { ...options, strict: true });\n}\n"],"mappings":";;;AAsBA,MAAM,iBACJ;AACF,MAAM,uBACJ;AAEF,MAAM,YAAY;AAElB,SAAS,mBAAmB,KAAa,OAAiB;AACxD,KACE,QAAQ,eACP,QAAQ,iBACP,SACA,OAAO,UAAU,YACjB,eAAe,OACjB;AAEA,UAAQ,KAAK,aAAa,IAAI,uCAAuC;AACrE;;AAEF,QAAO;;AAOT,SAAgB,MAAmB,OAAY,UAAmB,EAAE,EAAK;AACvE,KAAI,OAAO,UAAU,SACnB,QAAO;CAGT,IAAI,WAAW,cAAc,MAAM;AAEnC,KACE,SAAS,OAAO,QAChB,SAAS,SAAS,SAAS,OAAO,QAClC,CAAC,SAAS,SAAS,KAAK,CAExB,QAAO,SAAS,MAAM,GAAG,GAAG;AAG9B,YAAW,SAAS,MAAM;AAE1B,KAAI,SAAS,UAAU,EACrB,SAAQ,SAAS,aAAa,EAA9B;EACE,KAAK,OACH,QAAO;EAET,KAAK,QACH,QAAO;EAET,KAAK,YACH;EAEF,KAAK,OACH,QAAO;EAET,KAAK,MACH,QAAO;EAET,KAAK,WACH,QAAO,OAAO;EAEhB,KAAK,YACH,QAAO,OAAO;;AAKpB,KAAI,CAAC,UAAU,KAAK,SAAS,EAAE;AAC7B,MAAI,QAAQ,OACV,OAAM,IAAI,MAAM,eAAe;AAEjC,SAAO;;AAGT,KAAI;AACF,MAAI,eAAe,KAAK,SAAS,IAAI,qBAAqB,KAAK,SAAS,EAAE;AACxE,OAAI,QAAQ,OACV,OAAM,IAAI,MAAM,+BAA+B;AAEjD,UAAO,KAAK,MAAM,UAAU,mBAAmB;;AAGjD,SAAO,KAAK,MAAM,SAAS;UACpB,OAAO;AACd,MAAI,QAAQ,OACV,OAAM;AAER,SAAO"}
|
|
@@ -1 +1,67 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_is_number = require('../../../type-checks/src/is-number.cjs');
|
|
2
|
+
const require_is_undefined = require('../../../type-checks/src/is-undefined.cjs');
|
|
3
|
+
|
|
4
|
+
//#region ../json/src/utils/stringify.ts
|
|
5
|
+
const invalidKeyChars = [
|
|
6
|
+
"@",
|
|
7
|
+
"/",
|
|
8
|
+
"#",
|
|
9
|
+
"$",
|
|
10
|
+
" ",
|
|
11
|
+
":",
|
|
12
|
+
";",
|
|
13
|
+
",",
|
|
14
|
+
".",
|
|
15
|
+
"!",
|
|
16
|
+
"?",
|
|
17
|
+
"&",
|
|
18
|
+
"=",
|
|
19
|
+
"+",
|
|
20
|
+
"-",
|
|
21
|
+
"*",
|
|
22
|
+
"%",
|
|
23
|
+
"^",
|
|
24
|
+
"~",
|
|
25
|
+
"|",
|
|
26
|
+
"\\",
|
|
27
|
+
"\"",
|
|
28
|
+
"'",
|
|
29
|
+
"`",
|
|
30
|
+
"{",
|
|
31
|
+
"}",
|
|
32
|
+
"[",
|
|
33
|
+
"]",
|
|
34
|
+
"(",
|
|
35
|
+
")",
|
|
36
|
+
"<",
|
|
37
|
+
">"
|
|
38
|
+
];
|
|
39
|
+
/**
|
|
40
|
+
* Stringify a value to a JSON-like string.
|
|
41
|
+
*
|
|
42
|
+
* @param value - The value to stringify
|
|
43
|
+
* @param spacing - The spacing to use for the stringification
|
|
44
|
+
* @returns The stringified value
|
|
45
|
+
*/
|
|
46
|
+
const stringify = (value, spacing = 2) => {
|
|
47
|
+
const space = require_is_number.isNumber(spacing) ? " ".repeat(spacing) : spacing;
|
|
48
|
+
switch (value) {
|
|
49
|
+
case null: return "null";
|
|
50
|
+
case void 0: return "\"undefined\"";
|
|
51
|
+
case true: return "true";
|
|
52
|
+
case false: return "false";
|
|
53
|
+
case Number.POSITIVE_INFINITY: return "infinity";
|
|
54
|
+
case Number.NEGATIVE_INFINITY: return "-infinity";
|
|
55
|
+
}
|
|
56
|
+
if (Array.isArray(value)) return `[${space}${value.map((v) => stringify(v, space)).join(`,${space}`)}${space}]`;
|
|
57
|
+
if (value instanceof Uint8Array) return value.toString();
|
|
58
|
+
switch (typeof value) {
|
|
59
|
+
case "number": return `${value}`;
|
|
60
|
+
case "string": return JSON.stringify(value);
|
|
61
|
+
case "object": return `{${space}${Object.keys(value).filter((key) => !require_is_undefined.isUndefined(value[key])).map((key) => `${invalidKeyChars.some((invalidKeyChar) => key.includes(invalidKeyChar)) ? `"${key}"` : key}: ${space}${stringify(value[key], space)}`).join(`,${space}`)}${space}}`;
|
|
62
|
+
default: return "null";
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
exports.stringify = stringify;
|
|
@@ -1,2 +1,68 @@
|
|
|
1
|
-
import{isNumber
|
|
1
|
+
import { isNumber } from "../../../type-checks/src/is-number.mjs";
|
|
2
|
+
import { isUndefined } from "../../../type-checks/src/is-undefined.mjs";
|
|
3
|
+
|
|
4
|
+
//#region ../json/src/utils/stringify.ts
|
|
5
|
+
const invalidKeyChars = [
|
|
6
|
+
"@",
|
|
7
|
+
"/",
|
|
8
|
+
"#",
|
|
9
|
+
"$",
|
|
10
|
+
" ",
|
|
11
|
+
":",
|
|
12
|
+
";",
|
|
13
|
+
",",
|
|
14
|
+
".",
|
|
15
|
+
"!",
|
|
16
|
+
"?",
|
|
17
|
+
"&",
|
|
18
|
+
"=",
|
|
19
|
+
"+",
|
|
20
|
+
"-",
|
|
21
|
+
"*",
|
|
22
|
+
"%",
|
|
23
|
+
"^",
|
|
24
|
+
"~",
|
|
25
|
+
"|",
|
|
26
|
+
"\\",
|
|
27
|
+
"\"",
|
|
28
|
+
"'",
|
|
29
|
+
"`",
|
|
30
|
+
"{",
|
|
31
|
+
"}",
|
|
32
|
+
"[",
|
|
33
|
+
"]",
|
|
34
|
+
"(",
|
|
35
|
+
")",
|
|
36
|
+
"<",
|
|
37
|
+
">"
|
|
38
|
+
];
|
|
39
|
+
/**
|
|
40
|
+
* Stringify a value to a JSON-like string.
|
|
41
|
+
*
|
|
42
|
+
* @param value - The value to stringify
|
|
43
|
+
* @param spacing - The spacing to use for the stringification
|
|
44
|
+
* @returns The stringified value
|
|
45
|
+
*/
|
|
46
|
+
const stringify = (value, spacing = 2) => {
|
|
47
|
+
const space = isNumber(spacing) ? " ".repeat(spacing) : spacing;
|
|
48
|
+
switch (value) {
|
|
49
|
+
case null: return "null";
|
|
50
|
+
case void 0: return "\"undefined\"";
|
|
51
|
+
case true: return "true";
|
|
52
|
+
case false: return "false";
|
|
53
|
+
case Number.POSITIVE_INFINITY: return "infinity";
|
|
54
|
+
case Number.NEGATIVE_INFINITY: return "-infinity";
|
|
55
|
+
}
|
|
56
|
+
if (Array.isArray(value)) return `[${space}${value.map((v) => stringify(v, space)).join(`,${space}`)}${space}]`;
|
|
57
|
+
if (value instanceof Uint8Array) return value.toString();
|
|
58
|
+
switch (typeof value) {
|
|
59
|
+
case "number": return `${value}`;
|
|
60
|
+
case "string": return JSON.stringify(value);
|
|
61
|
+
case "object": return `{${space}${Object.keys(value).filter((key) => !isUndefined(value[key])).map((key) => `${invalidKeyChars.some((invalidKeyChar) => key.includes(invalidKeyChar)) ? `"${key}"` : key}: ${space}${stringify(value[key], space)}`).join(`,${space}`)}${space}}`;
|
|
62
|
+
default: return "null";
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
export { stringify };
|
|
2
68
|
//# sourceMappingURL=stringify.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stringify.mjs","names":[],"sources":["../../../../../json/src/utils/stringify.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isNumber } from \"@stryke/type-checks/is-number\";\nimport { isUndefined } from \"@stryke/type-checks/is-undefined\";\n\nexport const invalidKeyChars = [\n \"@\",\n \"/\",\n \"#\",\n \"$\",\n \" \",\n \":\",\n \";\",\n \",\",\n \".\",\n \"!\",\n \"?\",\n \"&\",\n \"=\",\n \"+\",\n \"-\",\n \"*\",\n \"%\",\n \"^\",\n \"~\",\n \"|\",\n \"\\\\\",\n '\"',\n \"'\",\n \"`\",\n \"{\",\n \"}\",\n \"[\",\n \"]\",\n \"(\",\n \")\",\n \"<\",\n \">\"\n] as const;\n\n/**\n * Stringify a value to a JSON-like string.\n *\n * @param value - The value to stringify\n * @param spacing - The spacing to use for the stringification\n * @returns The stringified value\n */\nexport const stringify = (\n value: unknown,\n spacing: string | number = 2\n): string => {\n const space = isNumber(spacing) ? \" \".repeat(spacing) : spacing;\n\n switch (value) {\n case null: {\n return \"null\";\n }\n case undefined: {\n return '\"undefined\"';\n }\n case true: {\n return \"true\";\n }\n case false: {\n return \"false\";\n }\n case Number.POSITIVE_INFINITY: {\n return \"infinity\";\n }\n case Number.NEGATIVE_INFINITY: {\n return \"-infinity\";\n }\n }\n\n if (Array.isArray(value)) {\n return `[${space}${value.map(v => stringify(v, space)).join(`,${space}`)}${space}]`;\n }\n if (value instanceof Uint8Array) {\n return value.toString();\n }\n\n // eslint-disable-next-line ts/switch-exhaustiveness-check\n switch (typeof value) {\n case \"number\": {\n return `${value}`;\n }\n case \"string\": {\n return JSON.stringify(value);\n }\n case \"object\": {\n const keys = Object.keys(value as object).filter(\n key => !isUndefined((value as any)[key])\n );\n\n return `{${space}${keys\n .map(\n key =>\n `${invalidKeyChars.some(invalidKeyChar => key.includes(invalidKeyChar)) ? `\"${key}\"` : key}: ${space}${stringify((value as any)[key], space)}`\n )\n .join(`,${space}`)}${space}}`;\n }\n default:\n return \"null\";\n }\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"stringify.mjs","names":[],"sources":["../../../../../json/src/utils/stringify.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isNumber } from \"@stryke/type-checks/is-number\";\nimport { isUndefined } from \"@stryke/type-checks/is-undefined\";\n\nexport const invalidKeyChars = [\n \"@\",\n \"/\",\n \"#\",\n \"$\",\n \" \",\n \":\",\n \";\",\n \",\",\n \".\",\n \"!\",\n \"?\",\n \"&\",\n \"=\",\n \"+\",\n \"-\",\n \"*\",\n \"%\",\n \"^\",\n \"~\",\n \"|\",\n \"\\\\\",\n '\"',\n \"'\",\n \"`\",\n \"{\",\n \"}\",\n \"[\",\n \"]\",\n \"(\",\n \")\",\n \"<\",\n \">\"\n] as const;\n\n/**\n * Stringify a value to a JSON-like string.\n *\n * @param value - The value to stringify\n * @param spacing - The spacing to use for the stringification\n * @returns The stringified value\n */\nexport const stringify = (\n value: unknown,\n spacing: string | number = 2\n): string => {\n const space = isNumber(spacing) ? \" \".repeat(spacing) : spacing;\n\n switch (value) {\n case null: {\n return \"null\";\n }\n case undefined: {\n return '\"undefined\"';\n }\n case true: {\n return \"true\";\n }\n case false: {\n return \"false\";\n }\n case Number.POSITIVE_INFINITY: {\n return \"infinity\";\n }\n case Number.NEGATIVE_INFINITY: {\n return \"-infinity\";\n }\n }\n\n if (Array.isArray(value)) {\n return `[${space}${value.map(v => stringify(v, space)).join(`,${space}`)}${space}]`;\n }\n if (value instanceof Uint8Array) {\n return value.toString();\n }\n\n // eslint-disable-next-line ts/switch-exhaustiveness-check\n switch (typeof value) {\n case \"number\": {\n return `${value}`;\n }\n case \"string\": {\n return JSON.stringify(value);\n }\n case \"object\": {\n const keys = Object.keys(value as object).filter(\n key => !isUndefined((value as any)[key])\n );\n\n return `{${space}${keys\n .map(\n key =>\n `${invalidKeyChars.some(invalidKeyChar => key.includes(invalidKeyChar)) ? `\"${key}\"` : key}: ${space}${stringify((value as any)[key], space)}`\n )\n .join(`,${space}`)}${space}}`;\n }\n default:\n return \"null\";\n }\n};\n"],"mappings":";;;;AAqBA,MAAa,kBAAkB;CAC7B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;;;;;;;;AASD,MAAa,aACX,OACA,UAA2B,MAChB;CACX,MAAM,QAAQ,SAAS,QAAQ,GAAG,IAAI,OAAO,QAAQ,GAAG;AAExD,SAAQ,OAAR;EACE,KAAK,KACH,QAAO;EAET,KAAK,OACH,QAAO;EAET,KAAK,KACH,QAAO;EAET,KAAK,MACH,QAAO;EAET,KAAK,OAAO,kBACV,QAAO;EAET,KAAK,OAAO,kBACV,QAAO;;AAIX,KAAI,MAAM,QAAQ,MAAM,CACtB,QAAO,IAAI,QAAQ,MAAM,KAAI,MAAK,UAAU,GAAG,MAAM,CAAC,CAAC,KAAK,IAAI,QAAQ,GAAG,MAAM;AAEnF,KAAI,iBAAiB,WACnB,QAAO,MAAM,UAAU;AAIzB,SAAQ,OAAO,OAAf;EACE,KAAK,SACH,QAAO,GAAG;EAEZ,KAAK,SACH,QAAO,KAAK,UAAU,MAAM;EAE9B,KAAK,SAKH,QAAO,IAAI,QAJE,OAAO,KAAK,MAAgB,CAAC,QACxC,QAAO,CAAC,YAAa,MAAc,KAAK,CACzC,CAGE,KACC,QACE,GAAG,gBAAgB,MAAK,mBAAkB,IAAI,SAAS,eAAe,CAAC,GAAG,IAAI,IAAI,KAAK,IAAI,IAAI,QAAQ,UAAW,MAAc,MAAM,MAAM,GAC/I,CACA,KAAK,IAAI,QAAQ,GAAG,MAAM;EAE/B,QACE,QAAO"}
|
|
@@ -1,4 +1,86 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
const require_base = require('../../../types/src/base.cjs');
|
|
2
|
+
|
|
3
|
+
//#region ../json/src/utils/strip-comments.ts
|
|
4
|
+
const singleComment = Symbol("singleComment");
|
|
5
|
+
const multiComment = Symbol("multiComment");
|
|
6
|
+
function stripWithoutWhitespace() {
|
|
7
|
+
return "";
|
|
8
|
+
}
|
|
9
|
+
function stripWithWhitespace(value, start, end) {
|
|
10
|
+
return value.slice(start, end).replace(/\S/g, " ");
|
|
11
|
+
}
|
|
12
|
+
function isEscaped(value, quotePosition) {
|
|
13
|
+
let index = quotePosition - 1;
|
|
14
|
+
let backslashCount = 0;
|
|
15
|
+
while (value[index] === "\\") {
|
|
16
|
+
index -= 1;
|
|
17
|
+
backslashCount += 1;
|
|
18
|
+
}
|
|
19
|
+
return Boolean(backslashCount % 2);
|
|
20
|
+
}
|
|
21
|
+
function stripComments(value, { whitespace = true, trailingCommas = false } = {}) {
|
|
22
|
+
if (typeof value !== "string") throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof value}\``);
|
|
23
|
+
const strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace;
|
|
24
|
+
let isInsideString = false;
|
|
25
|
+
let isInsideComment = false;
|
|
26
|
+
let offset = 0;
|
|
27
|
+
let buffer = "";
|
|
28
|
+
let result = "";
|
|
29
|
+
let commaIndex = -1;
|
|
30
|
+
for (let index = 0; index < value.length; index++) {
|
|
31
|
+
const currentCharacter = value[index];
|
|
32
|
+
const nextCharacter = value[index + 1];
|
|
33
|
+
if (!isInsideComment && currentCharacter === "\"") {
|
|
34
|
+
if (!isEscaped(value, index)) isInsideString = !isInsideString;
|
|
35
|
+
}
|
|
36
|
+
if (isInsideString) continue;
|
|
37
|
+
if (!isInsideComment && currentCharacter + (nextCharacter ?? require_base.EMPTY_STRING) === "//") {
|
|
38
|
+
buffer += value.slice(offset, index);
|
|
39
|
+
offset = index;
|
|
40
|
+
isInsideComment = singleComment;
|
|
41
|
+
index++;
|
|
42
|
+
} else if (isInsideComment === singleComment && currentCharacter + (nextCharacter ?? require_base.EMPTY_STRING) === "\r\n") {
|
|
43
|
+
index++;
|
|
44
|
+
isInsideComment = false;
|
|
45
|
+
buffer += strip(value, offset, index);
|
|
46
|
+
offset = index;
|
|
47
|
+
} else if (isInsideComment === singleComment && currentCharacter === "\n") {
|
|
48
|
+
isInsideComment = false;
|
|
49
|
+
buffer += strip(value, offset, index);
|
|
50
|
+
offset = index;
|
|
51
|
+
} else if (!isInsideComment && currentCharacter + (nextCharacter ?? require_base.EMPTY_STRING) === "/*") {
|
|
52
|
+
buffer += value.slice(offset, index);
|
|
53
|
+
offset = index;
|
|
54
|
+
isInsideComment = multiComment;
|
|
55
|
+
index++;
|
|
56
|
+
} else if (isInsideComment === multiComment && currentCharacter + (nextCharacter ?? require_base.EMPTY_STRING) === "*/") {
|
|
57
|
+
index++;
|
|
58
|
+
isInsideComment = false;
|
|
59
|
+
buffer += strip(value, offset, index + 1);
|
|
60
|
+
offset = index + 1;
|
|
61
|
+
} else if (trailingCommas && !isInsideComment) {
|
|
62
|
+
if (commaIndex !== -1) {
|
|
63
|
+
if (currentCharacter === "}" || currentCharacter === "]") {
|
|
64
|
+
buffer += value.slice(offset, index);
|
|
65
|
+
result += strip(buffer, 0, 1) + buffer.slice(1);
|
|
66
|
+
buffer = "";
|
|
67
|
+
offset = index;
|
|
68
|
+
commaIndex = -1;
|
|
69
|
+
} else if (currentCharacter !== " " && currentCharacter !== " " && currentCharacter !== "\r" && currentCharacter !== "\n") {
|
|
70
|
+
buffer += value.slice(offset, index);
|
|
71
|
+
offset = index;
|
|
72
|
+
commaIndex = -1;
|
|
73
|
+
}
|
|
74
|
+
} else if (currentCharacter === ",") {
|
|
75
|
+
result += buffer + value.slice(offset, index);
|
|
76
|
+
buffer = "";
|
|
77
|
+
offset = index;
|
|
78
|
+
commaIndex = index;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return result + buffer + (isInsideComment ? strip(value.slice(offset)) : value.slice(offset));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
exports.stripComments = stripComments;
|
|
@@ -1,5 +1,87 @@
|
|
|
1
|
-
import{EMPTY_STRING
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { EMPTY_STRING } from "../../../types/src/base.mjs";
|
|
2
|
+
|
|
3
|
+
//#region ../json/src/utils/strip-comments.ts
|
|
4
|
+
const singleComment = Symbol("singleComment");
|
|
5
|
+
const multiComment = Symbol("multiComment");
|
|
6
|
+
function stripWithoutWhitespace() {
|
|
7
|
+
return "";
|
|
8
|
+
}
|
|
9
|
+
function stripWithWhitespace(value, start, end) {
|
|
10
|
+
return value.slice(start, end).replace(/\S/g, " ");
|
|
11
|
+
}
|
|
12
|
+
function isEscaped(value, quotePosition) {
|
|
13
|
+
let index = quotePosition - 1;
|
|
14
|
+
let backslashCount = 0;
|
|
15
|
+
while (value[index] === "\\") {
|
|
16
|
+
index -= 1;
|
|
17
|
+
backslashCount += 1;
|
|
18
|
+
}
|
|
19
|
+
return Boolean(backslashCount % 2);
|
|
20
|
+
}
|
|
21
|
+
function stripComments(value, { whitespace = true, trailingCommas = false } = {}) {
|
|
22
|
+
if (typeof value !== "string") throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof value}\``);
|
|
23
|
+
const strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace;
|
|
24
|
+
let isInsideString = false;
|
|
25
|
+
let isInsideComment = false;
|
|
26
|
+
let offset = 0;
|
|
27
|
+
let buffer = "";
|
|
28
|
+
let result = "";
|
|
29
|
+
let commaIndex = -1;
|
|
30
|
+
for (let index = 0; index < value.length; index++) {
|
|
31
|
+
const currentCharacter = value[index];
|
|
32
|
+
const nextCharacter = value[index + 1];
|
|
33
|
+
if (!isInsideComment && currentCharacter === "\"") {
|
|
34
|
+
if (!isEscaped(value, index)) isInsideString = !isInsideString;
|
|
35
|
+
}
|
|
36
|
+
if (isInsideString) continue;
|
|
37
|
+
if (!isInsideComment && currentCharacter + (nextCharacter ?? EMPTY_STRING) === "//") {
|
|
38
|
+
buffer += value.slice(offset, index);
|
|
39
|
+
offset = index;
|
|
40
|
+
isInsideComment = singleComment;
|
|
41
|
+
index++;
|
|
42
|
+
} else if (isInsideComment === singleComment && currentCharacter + (nextCharacter ?? EMPTY_STRING) === "\r\n") {
|
|
43
|
+
index++;
|
|
44
|
+
isInsideComment = false;
|
|
45
|
+
buffer += strip(value, offset, index);
|
|
46
|
+
offset = index;
|
|
47
|
+
} else if (isInsideComment === singleComment && currentCharacter === "\n") {
|
|
48
|
+
isInsideComment = false;
|
|
49
|
+
buffer += strip(value, offset, index);
|
|
50
|
+
offset = index;
|
|
51
|
+
} else if (!isInsideComment && currentCharacter + (nextCharacter ?? EMPTY_STRING) === "/*") {
|
|
52
|
+
buffer += value.slice(offset, index);
|
|
53
|
+
offset = index;
|
|
54
|
+
isInsideComment = multiComment;
|
|
55
|
+
index++;
|
|
56
|
+
} else if (isInsideComment === multiComment && currentCharacter + (nextCharacter ?? EMPTY_STRING) === "*/") {
|
|
57
|
+
index++;
|
|
58
|
+
isInsideComment = false;
|
|
59
|
+
buffer += strip(value, offset, index + 1);
|
|
60
|
+
offset = index + 1;
|
|
61
|
+
} else if (trailingCommas && !isInsideComment) {
|
|
62
|
+
if (commaIndex !== -1) {
|
|
63
|
+
if (currentCharacter === "}" || currentCharacter === "]") {
|
|
64
|
+
buffer += value.slice(offset, index);
|
|
65
|
+
result += strip(buffer, 0, 1) + buffer.slice(1);
|
|
66
|
+
buffer = "";
|
|
67
|
+
offset = index;
|
|
68
|
+
commaIndex = -1;
|
|
69
|
+
} else if (currentCharacter !== " " && currentCharacter !== " " && currentCharacter !== "\r" && currentCharacter !== "\n") {
|
|
70
|
+
buffer += value.slice(offset, index);
|
|
71
|
+
offset = index;
|
|
72
|
+
commaIndex = -1;
|
|
73
|
+
}
|
|
74
|
+
} else if (currentCharacter === ",") {
|
|
75
|
+
result += buffer + value.slice(offset, index);
|
|
76
|
+
buffer = "";
|
|
77
|
+
offset = index;
|
|
78
|
+
commaIndex = index;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return result + buffer + (isInsideComment ? strip(value.slice(offset)) : value.slice(offset));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
export { stripComments };
|
|
5
87
|
//# sourceMappingURL=strip-comments.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"strip-comments.mjs","names":["isInsideString: boolean | symbol","isInsideComment: boolean | symbol"],"sources":["../../../../../json/src/utils/strip-comments.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { EMPTY_STRING } from \"@stryke/types/base\";\n\nconst singleComment = Symbol(\"singleComment\");\nconst multiComment = Symbol(\"multiComment\");\n\nfunction stripWithoutWhitespace() {\n return \"\";\n}\nfunction stripWithWhitespace(value: string, start?: number, end?: number) {\n return value.slice(start, end).replace(/\\S/g, \" \");\n}\n\nfunction isEscaped(value: string, quotePosition: number) {\n let index = quotePosition - 1;\n let backslashCount = 0;\n while (value[index] === \"\\\\\") {\n index -= 1;\n backslashCount += 1;\n }\n\n return Boolean(backslashCount % 2);\n}\n\nexport function stripComments(\n value: string,\n { whitespace = true, trailingCommas = false } = {}\n) {\n if (typeof value !== \"string\") {\n throw new TypeError(\n `Expected argument \\`jsonString\\` to be a \\`string\\`, got \\`${typeof value}\\``\n );\n }\n\n const strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace;\n let isInsideString: boolean | symbol = false;\n let isInsideComment: boolean | symbol = false;\n let offset = 0;\n let buffer = \"\";\n let result = \"\";\n let commaIndex = -1;\n for (let index = 0; index < value.length; index++) {\n const currentCharacter = value[index];\n const nextCharacter = value[index + 1];\n if (!isInsideComment && currentCharacter === '\"') {\n const escaped = isEscaped(value, index);\n if (!escaped) {\n isInsideString = !isInsideString;\n }\n }\n if (isInsideString) {\n continue;\n }\n if (\n !isInsideComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"//\"\n ) {\n buffer += value.slice(offset, index);\n offset = index;\n isInsideComment = singleComment;\n index++;\n } else if (\n isInsideComment === singleComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"\\r\\n\"\n ) {\n index++;\n isInsideComment = false;\n buffer += strip(value, offset, index);\n offset = index;\n } else if (isInsideComment === singleComment && currentCharacter === \"\\n\") {\n isInsideComment = false;\n buffer += strip(value, offset, index);\n offset = index;\n } else if (\n !isInsideComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"/*\"\n ) {\n buffer += value.slice(offset, index);\n offset = index;\n isInsideComment = multiComment;\n index++;\n } else if (\n isInsideComment === multiComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"*/\"\n ) {\n index++;\n isInsideComment = false;\n buffer += strip(value, offset, index + 1);\n offset = index + 1;\n } else if (trailingCommas && !isInsideComment) {\n if (commaIndex !== -1) {\n if (currentCharacter === \"}\" || currentCharacter === \"]\") {\n buffer += value.slice(offset, index);\n result += strip(buffer, 0, 1) + buffer.slice(1);\n buffer = \"\";\n offset = index;\n commaIndex = -1;\n } else if (\n currentCharacter !== \" \" &&\n currentCharacter !== \"\t\" &&\n currentCharacter !== \"\\r\" &&\n currentCharacter !== \"\\n\"\n ) {\n buffer += value.slice(offset, index);\n offset = index;\n commaIndex = -1;\n }\n } else if (currentCharacter === \",\") {\n result += buffer + value.slice(offset, index);\n buffer = \"\";\n offset = index;\n commaIndex = index;\n }\n }\n }\n\n return (\n result +\n buffer +\n (isInsideComment ? strip(value.slice(offset)) : value.slice(offset))\n );\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"strip-comments.mjs","names":["isInsideString: boolean | symbol","isInsideComment: boolean | symbol"],"sources":["../../../../../json/src/utils/strip-comments.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { EMPTY_STRING } from \"@stryke/types/base\";\n\nconst singleComment = Symbol(\"singleComment\");\nconst multiComment = Symbol(\"multiComment\");\n\nfunction stripWithoutWhitespace() {\n return \"\";\n}\nfunction stripWithWhitespace(value: string, start?: number, end?: number) {\n return value.slice(start, end).replace(/\\S/g, \" \");\n}\n\nfunction isEscaped(value: string, quotePosition: number) {\n let index = quotePosition - 1;\n let backslashCount = 0;\n while (value[index] === \"\\\\\") {\n index -= 1;\n backslashCount += 1;\n }\n\n return Boolean(backslashCount % 2);\n}\n\nexport function stripComments(\n value: string,\n { whitespace = true, trailingCommas = false } = {}\n) {\n if (typeof value !== \"string\") {\n throw new TypeError(\n `Expected argument \\`jsonString\\` to be a \\`string\\`, got \\`${typeof value}\\``\n );\n }\n\n const strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace;\n let isInsideString: boolean | symbol = false;\n let isInsideComment: boolean | symbol = false;\n let offset = 0;\n let buffer = \"\";\n let result = \"\";\n let commaIndex = -1;\n for (let index = 0; index < value.length; index++) {\n const currentCharacter = value[index];\n const nextCharacter = value[index + 1];\n if (!isInsideComment && currentCharacter === '\"') {\n const escaped = isEscaped(value, index);\n if (!escaped) {\n isInsideString = !isInsideString;\n }\n }\n if (isInsideString) {\n continue;\n }\n if (\n !isInsideComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"//\"\n ) {\n buffer += value.slice(offset, index);\n offset = index;\n isInsideComment = singleComment;\n index++;\n } else if (\n isInsideComment === singleComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"\\r\\n\"\n ) {\n index++;\n isInsideComment = false;\n buffer += strip(value, offset, index);\n offset = index;\n } else if (isInsideComment === singleComment && currentCharacter === \"\\n\") {\n isInsideComment = false;\n buffer += strip(value, offset, index);\n offset = index;\n } else if (\n !isInsideComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"/*\"\n ) {\n buffer += value.slice(offset, index);\n offset = index;\n isInsideComment = multiComment;\n index++;\n } else if (\n isInsideComment === multiComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"*/\"\n ) {\n index++;\n isInsideComment = false;\n buffer += strip(value, offset, index + 1);\n offset = index + 1;\n } else if (trailingCommas && !isInsideComment) {\n if (commaIndex !== -1) {\n if (currentCharacter === \"}\" || currentCharacter === \"]\") {\n buffer += value.slice(offset, index);\n result += strip(buffer, 0, 1) + buffer.slice(1);\n buffer = \"\";\n offset = index;\n commaIndex = -1;\n } else if (\n currentCharacter !== \" \" &&\n currentCharacter !== \"\t\" &&\n currentCharacter !== \"\\r\" &&\n currentCharacter !== \"\\n\"\n ) {\n buffer += value.slice(offset, index);\n offset = index;\n commaIndex = -1;\n }\n } else if (currentCharacter === \",\") {\n result += buffer + value.slice(offset, index);\n buffer = \"\";\n offset = index;\n commaIndex = index;\n }\n }\n }\n\n return (\n result +\n buffer +\n (isInsideComment ? strip(value.slice(offset)) : value.slice(offset))\n );\n}\n"],"mappings":";;;AAoBA,MAAM,gBAAgB,OAAO,gBAAgB;AAC7C,MAAM,eAAe,OAAO,eAAe;AAE3C,SAAS,yBAAyB;AAChC,QAAO;;AAET,SAAS,oBAAoB,OAAe,OAAgB,KAAc;AACxE,QAAO,MAAM,MAAM,OAAO,IAAI,CAAC,QAAQ,OAAO,IAAI;;AAGpD,SAAS,UAAU,OAAe,eAAuB;CACvD,IAAI,QAAQ,gBAAgB;CAC5B,IAAI,iBAAiB;AACrB,QAAO,MAAM,WAAW,MAAM;AAC5B,WAAS;AACT,oBAAkB;;AAGpB,QAAO,QAAQ,iBAAiB,EAAE;;AAGpC,SAAgB,cACd,OACA,EAAE,aAAa,MAAM,iBAAiB,UAAU,EAAE,EAClD;AACA,KAAI,OAAO,UAAU,SACnB,OAAM,IAAI,UACR,8DAA8D,OAAO,MAAM,IAC5E;CAGH,MAAM,QAAQ,aAAa,sBAAsB;CACjD,IAAIA,iBAAmC;CACvC,IAAIC,kBAAoC;CACxC,IAAI,SAAS;CACb,IAAI,SAAS;CACb,IAAI,SAAS;CACb,IAAI,aAAa;AACjB,MAAK,IAAI,QAAQ,GAAG,QAAQ,MAAM,QAAQ,SAAS;EACjD,MAAM,mBAAmB,MAAM;EAC/B,MAAM,gBAAgB,MAAM,QAAQ;AACpC,MAAI,CAAC,mBAAmB,qBAAqB,MAE3C;OAAI,CADY,UAAU,OAAO,MAAM,CAErC,kBAAiB,CAAC;;AAGtB,MAAI,eACF;AAEF,MACE,CAAC,mBACD,oBAAoB,iBAAiB,kBAAkB,MACvD;AACA,aAAU,MAAM,MAAM,QAAQ,MAAM;AACpC,YAAS;AACT,qBAAkB;AAClB;aAEA,oBAAoB,iBACpB,oBAAoB,iBAAiB,kBAAkB,QACvD;AACA;AACA,qBAAkB;AAClB,aAAU,MAAM,OAAO,QAAQ,MAAM;AACrC,YAAS;aACA,oBAAoB,iBAAiB,qBAAqB,MAAM;AACzE,qBAAkB;AAClB,aAAU,MAAM,OAAO,QAAQ,MAAM;AACrC,YAAS;aAET,CAAC,mBACD,oBAAoB,iBAAiB,kBAAkB,MACvD;AACA,aAAU,MAAM,MAAM,QAAQ,MAAM;AACpC,YAAS;AACT,qBAAkB;AAClB;aAEA,oBAAoB,gBACpB,oBAAoB,iBAAiB,kBAAkB,MACvD;AACA;AACA,qBAAkB;AAClB,aAAU,MAAM,OAAO,QAAQ,QAAQ,EAAE;AACzC,YAAS,QAAQ;aACR,kBAAkB,CAAC,iBAC5B;OAAI,eAAe,IACjB;QAAI,qBAAqB,OAAO,qBAAqB,KAAK;AACxD,eAAU,MAAM,MAAM,QAAQ,MAAM;AACpC,eAAU,MAAM,QAAQ,GAAG,EAAE,GAAG,OAAO,MAAM,EAAE;AAC/C,cAAS;AACT,cAAS;AACT,kBAAa;eAEb,qBAAqB,OACrB,qBAAqB,OACrB,qBAAqB,QACrB,qBAAqB,MACrB;AACA,eAAU,MAAM,MAAM,QAAQ,MAAM;AACpC,cAAS;AACT,kBAAa;;cAEN,qBAAqB,KAAK;AACnC,cAAU,SAAS,MAAM,MAAM,QAAQ,MAAM;AAC7C,aAAS;AACT,aAAS;AACT,iBAAa;;;;AAKnB,QACE,SACA,UACC,kBAAkB,MAAM,MAAM,MAAM,OAAO,CAAC,GAAG,MAAM,MAAM,OAAO"}
|
|
@@ -1 +1,28 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_regex = require('./regex.cjs');
|
|
2
|
+
const require_slash = require('./slash.cjs');
|
|
3
|
+
|
|
4
|
+
//#region ../path/src/is-type.ts
|
|
5
|
+
/**
|
|
6
|
+
* Check if the path is an absolute path.
|
|
7
|
+
*
|
|
8
|
+
* @param path - The path to check
|
|
9
|
+
* @returns An indicator specifying if the path is an absolute path
|
|
10
|
+
*/
|
|
11
|
+
function isAbsolutePath(path) {
|
|
12
|
+
return require_regex.ABSOLUTE_PATH_REGEX.test(require_slash.slash(path));
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Check if the path is an absolute path.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* This is an alias for {@link isAbsolutePath}.
|
|
19
|
+
*
|
|
20
|
+
* @param path - The path to check
|
|
21
|
+
* @returns An indicator specifying if the path is an absolute path
|
|
22
|
+
*/
|
|
23
|
+
function isAbsolute(path) {
|
|
24
|
+
return isAbsolutePath(path);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
exports.isAbsolute = isAbsolute;
|