@cdxgen/cdxgen 13.2.0 → 13.4.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/README.md +33 -14
- package/bin/cdxgen.js +310 -138
- package/bin/evinse.js +22 -0
- package/bin/repl.js +86 -0
- package/bin/sign.js +119 -15
- package/bin/tracebom.js +69 -5
- package/bin/validate.js +107 -19
- package/bin/verify.js +172 -63
- package/data/helpers/init.gradle +46 -21
- package/data/remediations.json +85 -0
- package/data/rules/dependency-sources.yaml +67 -0
- package/data/rules/scala-evidence.yaml +115 -0
- package/lib/audit/index.js +38 -6
- package/lib/cli/bomAssembly.js +105 -15
- package/lib/cli/cliOptions.js +93 -1
- package/lib/cli/index.js +156 -58
- package/lib/cli/jsBom.js +99 -53
- package/lib/cli/jvmBom.js +708 -147
- package/lib/cli/managedBom.js +415 -224
- package/lib/cli/nativeBom.js +264 -44
- package/lib/core/activity.js +12 -0
- package/lib/core/env.js +22 -15
- package/lib/core/fs.js +176 -7
- package/lib/core/httpClient.js +159 -24
- package/lib/core/metadataProperties.js +97 -0
- package/lib/core/pluginRun.js +184 -0
- package/lib/core/processTree.js +100 -0
- package/lib/core/projectConfig.js +31 -4
- package/lib/core/runState.js +39 -0
- package/lib/core/toml.js +72 -0
- package/lib/ecosystems/asarutils.js +4 -2
- package/lib/ecosystems/bunutils.js +274 -108
- package/lib/ecosystems/cargoCache.js +231 -8
- package/lib/ecosystems/caxa.js +434 -2
- package/lib/ecosystems/cmakeResolver.js +76 -21
- package/lib/ecosystems/cppBuildContext.js +631 -0
- package/lib/ecosystems/cppEvidence.js +399 -51
- package/lib/ecosystems/cppIncludeResolver.js +294 -0
- package/lib/ecosystems/denoutils.js +93 -66
- package/lib/ecosystems/ecosystems.js +1983 -793
- package/lib/ecosystems/golem.js +28 -12
- package/lib/ecosystems/gradleutils.js +356 -37
- package/lib/ecosystems/jsEvidence.js +104 -44
- package/lib/ecosystems/kosi.js +31 -7
- package/lib/ecosystems/npmutils.js +174 -9
- package/lib/ecosystems/parsers-dotnet.js +616 -159
- package/lib/ecosystems/parsers-elm.js +46 -28
- package/lib/ecosystems/parsers-gleam.js +3 -4
- package/lib/ecosystems/parsers-go.js +371 -70
- package/lib/ecosystems/parsers-js.js +263 -76
- package/lib/ecosystems/parsers-julia.js +2 -3
- package/lib/ecosystems/parsers-jvm.js +204 -12
- package/lib/ecosystems/parsers-misc.js +929 -216
- package/lib/ecosystems/parsers-python.js +167 -24
- package/lib/ecosystems/parsers-rust.js +28 -22
- package/lib/ecosystems/parsers-terraform.js +1573 -19
- package/lib/ecosystems/pythonutils.js +102 -6
- package/lib/ecosystems/remote/huggingface.js +3 -0
- package/lib/ecosystems/rubyutils.js +1492 -182
- package/lib/ecosystems/rusi.js +54 -10
- package/lib/ecosystems/sbtutils.js +243 -187
- package/lib/ecosystems/scalacliutils.js +342 -0
- package/lib/ecosystems/scalasem.js +2618 -0
- package/lib/ecosystems/terraformRegistry.js +288 -0
- package/lib/ecosystems/utils.js +2 -0
- package/lib/ecosystems/vendoredCode.js +156 -0
- package/lib/evinser/evinser.js +1008 -191
- package/lib/evinser/swiftsem.js +1914 -302
- package/lib/helpers/agenticToolutils.js +2 -2
- package/lib/helpers/bomSigner.js +1101 -222
- package/lib/inventory/aiCollector.js +4 -1
- package/lib/inventory/analyzer.js +110 -127
- package/lib/inventory/atomUtils.js +703 -43
- package/lib/inventory/auditCategories.js +7 -0
- package/lib/inventory/buildToolRateLimit.js +144 -0
- package/lib/inventory/cacheCommands.js +2 -1
- package/lib/inventory/cdxrs.js +103 -12
- package/lib/inventory/cmakeBuildDirs.js +254 -0
- package/lib/inventory/deps.js +501 -124
- package/lib/inventory/depsUtils.js +183 -11
- package/lib/inventory/display.js +4 -5
- package/lib/inventory/dosai.js +798 -57
- package/lib/inventory/dosaiParsers.js +310 -15
- package/lib/inventory/dynamic.js +90 -6
- package/lib/inventory/evidenceUtils.js +166 -113
- package/lib/inventory/fetchBatch.js +690 -55
- package/lib/inventory/fetchDiskCache.js +597 -0
- package/lib/inventory/forgeEnricher.js +14 -5
- package/lib/inventory/jvmLocalRepos.js +917 -0
- package/lib/inventory/mcpConfigParser.js +2 -3
- package/lib/inventory/parentSelection.js +146 -0
- package/lib/inventory/protobom.js +8 -1
- package/lib/inventory/purl.js +152 -3
- package/lib/inventory/rustFormulationParser.js +3 -4
- package/lib/inventory/sbomDocument.js +2 -70
- package/lib/inventory/scalaCoords.js +153 -0
- package/lib/inventory/source.js +45 -27
- package/lib/inventory/spdx.js +101 -0
- package/lib/inventory/traceRunner.js +295 -25
- package/lib/inventory/tracedCrypto.js +294 -0
- package/lib/managers/binary.js +115 -24
- package/lib/managers/docker.js +467 -46
- package/lib/managers/oci.js +63 -3
- package/lib/parsers/cmakeCache.js +69 -0
- package/lib/parsers/cmakeLists.js +466 -0
- package/lib/parsers/cmakePresets.js +400 -0
- package/lib/parsers/compileCommands.js +557 -0
- package/lib/parsers/largeJson.js +454 -0
- package/lib/parsers/npmrc.js +19 -1
- package/lib/parsers/strictJson.js +322 -0
- package/lib/parsers/terraformConfig.js +1073 -0
- package/lib/parsers/terraformSource.js +652 -0
- package/lib/server/server.js +23 -0
- package/lib/stages/postgen/introspection/markers.js +1 -1
- package/lib/stages/postgen/postgen.js +83 -16
- package/lib/stages/postgen/specVersionCompat.js +32 -0
- package/lib/validator/bomValidator.js +6 -16
- package/lib/validator/index.js +64 -11
- package/lib/validator/reporters/console.js +3 -0
- package/lib/validator/reporters/json.js +2 -0
- package/lib/validator/reporters/sarif.js +7 -0
- package/package.json +23 -23
- package/types/lib/audit/index.d.ts.map +1 -1
- package/types/lib/cli/bomAssembly.d.ts +2 -1
- package/types/lib/cli/bomAssembly.d.ts.map +1 -1
- package/types/lib/cli/cliOptions.d.ts +33 -1
- package/types/lib/cli/cliOptions.d.ts.map +1 -1
- package/types/lib/cli/index.d.ts.map +1 -1
- package/types/lib/cli/jsBom.d.ts.map +1 -1
- package/types/lib/cli/jvmBom.d.ts +24 -1
- package/types/lib/cli/jvmBom.d.ts.map +1 -1
- package/types/lib/cli/managedBom.d.ts +7 -4
- package/types/lib/cli/managedBom.d.ts.map +1 -1
- package/types/lib/cli/nativeBom.d.ts +3 -1
- package/types/lib/cli/nativeBom.d.ts.map +1 -1
- package/types/lib/core/activity.d.ts.map +1 -1
- package/types/lib/core/env.d.ts +9 -0
- package/types/lib/core/env.d.ts.map +1 -1
- package/types/lib/core/fs.d.ts +55 -0
- package/types/lib/core/fs.d.ts.map +1 -1
- package/types/lib/core/httpClient.d.ts +13 -8
- package/types/lib/core/httpClient.d.ts.map +1 -1
- package/types/lib/core/metadataProperties.d.ts +39 -0
- package/types/lib/core/metadataProperties.d.ts.map +1 -0
- package/types/lib/core/pluginRun.d.ts +97 -0
- package/types/lib/core/pluginRun.d.ts.map +1 -0
- package/types/lib/core/processTree.d.ts +36 -0
- package/types/lib/core/processTree.d.ts.map +1 -0
- package/types/lib/core/projectConfig.d.ts +6 -2
- package/types/lib/core/projectConfig.d.ts.map +1 -1
- package/types/lib/core/runState.d.ts +29 -0
- package/types/lib/core/runState.d.ts.map +1 -0
- package/types/lib/core/toml.d.ts +20 -0
- package/types/lib/core/toml.d.ts.map +1 -0
- package/types/lib/ecosystems/asarutils.d.ts.map +1 -1
- package/types/lib/ecosystems/bunutils.d.ts +12 -5
- package/types/lib/ecosystems/bunutils.d.ts.map +1 -1
- package/types/lib/ecosystems/cargoCache.d.ts +12 -0
- package/types/lib/ecosystems/cargoCache.d.ts.map +1 -1
- package/types/lib/ecosystems/caxa.d.ts +14 -0
- package/types/lib/ecosystems/caxa.d.ts.map +1 -1
- package/types/lib/ecosystems/cmakeResolver.d.ts +28 -3
- package/types/lib/ecosystems/cmakeResolver.d.ts.map +1 -1
- package/types/lib/ecosystems/cppBuildContext.d.ts +55 -0
- package/types/lib/ecosystems/cppBuildContext.d.ts.map +1 -0
- package/types/lib/ecosystems/cppEvidence.d.ts +17 -1
- package/types/lib/ecosystems/cppEvidence.d.ts.map +1 -1
- package/types/lib/ecosystems/cppIncludeResolver.d.ts +92 -0
- package/types/lib/ecosystems/cppIncludeResolver.d.ts.map +1 -0
- package/types/lib/ecosystems/denoutils.d.ts +9 -3
- package/types/lib/ecosystems/denoutils.d.ts.map +1 -1
- package/types/lib/ecosystems/ecosystems.d.ts +62 -13
- package/types/lib/ecosystems/ecosystems.d.ts.map +1 -1
- package/types/lib/ecosystems/golem.d.ts.map +1 -1
- package/types/lib/ecosystems/gradleutils.d.ts +70 -4
- package/types/lib/ecosystems/gradleutils.d.ts.map +1 -1
- package/types/lib/ecosystems/jsEvidence.d.ts +5 -2
- package/types/lib/ecosystems/jsEvidence.d.ts.map +1 -1
- package/types/lib/ecosystems/kosi.d.ts +13 -0
- package/types/lib/ecosystems/kosi.d.ts.map +1 -1
- package/types/lib/ecosystems/npmutils.d.ts +57 -0
- package/types/lib/ecosystems/npmutils.d.ts.map +1 -1
- package/types/lib/ecosystems/parsers-dotnet.d.ts +118 -4
- package/types/lib/ecosystems/parsers-dotnet.d.ts.map +1 -1
- package/types/lib/ecosystems/parsers-elm.d.ts.map +1 -1
- package/types/lib/ecosystems/parsers-gleam.d.ts.map +1 -1
- package/types/lib/ecosystems/parsers-go.d.ts +54 -2
- package/types/lib/ecosystems/parsers-go.d.ts.map +1 -1
- package/types/lib/ecosystems/parsers-js.d.ts +16 -5
- package/types/lib/ecosystems/parsers-js.d.ts.map +1 -1
- package/types/lib/ecosystems/parsers-julia.d.ts.map +1 -1
- package/types/lib/ecosystems/parsers-jvm.d.ts +46 -1
- package/types/lib/ecosystems/parsers-jvm.d.ts.map +1 -1
- package/types/lib/ecosystems/parsers-misc.d.ts +70 -4
- package/types/lib/ecosystems/parsers-misc.d.ts.map +1 -1
- package/types/lib/ecosystems/parsers-python.d.ts +53 -3
- package/types/lib/ecosystems/parsers-python.d.ts.map +1 -1
- package/types/lib/ecosystems/parsers-rust.d.ts.map +1 -1
- package/types/lib/ecosystems/parsers-terraform.d.ts +38 -21
- package/types/lib/ecosystems/parsers-terraform.d.ts.map +1 -1
- package/types/lib/ecosystems/pythonutils.d.ts +27 -2
- package/types/lib/ecosystems/pythonutils.d.ts.map +1 -1
- package/types/lib/ecosystems/remote/huggingface.d.ts.map +1 -1
- package/types/lib/ecosystems/rubyutils.d.ts +164 -3
- package/types/lib/ecosystems/rubyutils.d.ts.map +1 -1
- package/types/lib/ecosystems/rusi.d.ts +13 -0
- package/types/lib/ecosystems/rusi.d.ts.map +1 -1
- package/types/lib/ecosystems/sbtutils.d.ts +88 -2
- package/types/lib/ecosystems/sbtutils.d.ts.map +1 -1
- package/types/lib/ecosystems/scalacliutils.d.ts +56 -0
- package/types/lib/ecosystems/scalacliutils.d.ts.map +1 -0
- package/types/lib/ecosystems/scalasem.d.ts +311 -0
- package/types/lib/ecosystems/scalasem.d.ts.map +1 -0
- package/types/lib/ecosystems/terraformRegistry.d.ts +9 -0
- package/types/lib/ecosystems/terraformRegistry.d.ts.map +1 -0
- package/types/lib/ecosystems/utils.d.ts +2 -2
- package/types/lib/ecosystems/utils.d.ts.map +1 -1
- package/types/lib/ecosystems/vendoredCode.d.ts +32 -0
- package/types/lib/ecosystems/vendoredCode.d.ts.map +1 -0
- package/types/lib/evinser/evinser.d.ts +132 -15
- package/types/lib/evinser/evinser.d.ts.map +1 -1
- package/types/lib/evinser/swiftsem.d.ts +280 -15
- package/types/lib/evinser/swiftsem.d.ts.map +1 -1
- package/types/lib/helpers/bomSigner.d.ts +182 -13
- package/types/lib/helpers/bomSigner.d.ts.map +1 -1
- package/types/lib/inventory/aiCollector.d.ts.map +1 -1
- package/types/lib/inventory/analyzer.d.ts.map +1 -1
- package/types/lib/inventory/atomUtils.d.ts +219 -5
- package/types/lib/inventory/atomUtils.d.ts.map +1 -1
- package/types/lib/inventory/auditCategories.d.ts +3 -0
- package/types/lib/inventory/auditCategories.d.ts.map +1 -1
- package/types/lib/inventory/buildToolRateLimit.d.ts +38 -0
- package/types/lib/inventory/buildToolRateLimit.d.ts.map +1 -0
- package/types/lib/inventory/cacheCommands.d.ts +2 -1
- package/types/lib/inventory/cacheCommands.d.ts.map +1 -1
- package/types/lib/inventory/cdxrs.d.ts +8 -3
- package/types/lib/inventory/cdxrs.d.ts.map +1 -1
- package/types/lib/inventory/cmakeBuildDirs.d.ts +58 -0
- package/types/lib/inventory/cmakeBuildDirs.d.ts.map +1 -0
- package/types/lib/inventory/deps.d.ts +59 -1
- package/types/lib/inventory/deps.d.ts.map +1 -1
- package/types/lib/inventory/depsUtils.d.ts +15 -0
- package/types/lib/inventory/depsUtils.d.ts.map +1 -1
- package/types/lib/inventory/display.d.ts.map +1 -1
- package/types/lib/inventory/dosai.d.ts +121 -9
- package/types/lib/inventory/dosai.d.ts.map +1 -1
- package/types/lib/inventory/dosaiParsers.d.ts +53 -7
- package/types/lib/inventory/dosaiParsers.d.ts.map +1 -1
- package/types/lib/inventory/dynamic.d.ts +10 -0
- package/types/lib/inventory/dynamic.d.ts.map +1 -1
- package/types/lib/inventory/evidenceUtils.d.ts +19 -4
- package/types/lib/inventory/evidenceUtils.d.ts.map +1 -1
- package/types/lib/inventory/fetchBatch.d.ts +83 -14
- package/types/lib/inventory/fetchBatch.d.ts.map +1 -1
- package/types/lib/inventory/fetchDiskCache.d.ts +169 -0
- package/types/lib/inventory/fetchDiskCache.d.ts.map +1 -0
- package/types/lib/inventory/forgeEnricher.d.ts.map +1 -1
- package/types/lib/inventory/jvmLocalRepos.d.ts +178 -0
- package/types/lib/inventory/jvmLocalRepos.d.ts.map +1 -0
- package/types/lib/inventory/mcpConfigParser.d.ts.map +1 -1
- package/types/lib/inventory/parentSelection.d.ts +34 -0
- package/types/lib/inventory/parentSelection.d.ts.map +1 -0
- package/types/lib/inventory/plugins.d.ts.map +1 -1
- package/types/lib/inventory/protobom.d.ts.map +1 -1
- package/types/lib/inventory/purl.d.ts +71 -2
- package/types/lib/inventory/purl.d.ts.map +1 -1
- package/types/lib/inventory/rustFormulationParser.d.ts.map +1 -1
- package/types/lib/inventory/sbomDocument.d.ts.map +1 -1
- package/types/lib/inventory/scalaCoords.d.ts +80 -0
- package/types/lib/inventory/scalaCoords.d.ts.map +1 -0
- package/types/lib/inventory/source.d.ts.map +1 -1
- package/types/lib/inventory/spdx.d.ts +9 -0
- package/types/lib/inventory/spdx.d.ts.map +1 -1
- package/types/lib/inventory/traceRunner.d.ts +59 -1
- package/types/lib/inventory/traceRunner.d.ts.map +1 -1
- package/types/lib/inventory/tracedCrypto.d.ts +42 -0
- package/types/lib/inventory/tracedCrypto.d.ts.map +1 -0
- package/types/lib/managers/binary.d.ts +30 -1
- package/types/lib/managers/binary.d.ts.map +1 -1
- package/types/lib/managers/docker.d.ts +29 -0
- package/types/lib/managers/docker.d.ts.map +1 -1
- package/types/lib/managers/oci.d.ts +17 -1
- package/types/lib/managers/oci.d.ts.map +1 -1
- package/types/lib/parsers/cmakeCache.d.ts +16 -0
- package/types/lib/parsers/cmakeCache.d.ts.map +1 -1
- package/types/lib/parsers/cmakeLists.d.ts +77 -0
- package/types/lib/parsers/cmakeLists.d.ts.map +1 -0
- package/types/lib/parsers/cmakePresets.d.ts +78 -0
- package/types/lib/parsers/cmakePresets.d.ts.map +1 -0
- package/types/lib/parsers/compileCommands.d.ts +115 -0
- package/types/lib/parsers/compileCommands.d.ts.map +1 -0
- package/types/lib/parsers/largeJson.d.ts +52 -0
- package/types/lib/parsers/largeJson.d.ts.map +1 -0
- package/types/lib/parsers/npmrc.d.ts +5 -0
- package/types/lib/parsers/npmrc.d.ts.map +1 -1
- package/types/lib/parsers/strictJson.d.ts +46 -0
- package/types/lib/parsers/strictJson.d.ts.map +1 -0
- package/types/lib/parsers/terraformConfig.d.ts +70 -0
- package/types/lib/parsers/terraformConfig.d.ts.map +1 -0
- package/types/lib/parsers/terraformSource.d.ts +66 -0
- package/types/lib/parsers/terraformSource.d.ts.map +1 -0
- package/types/lib/server/server.d.ts.map +1 -1
- package/types/lib/stages/postgen/postgen.d.ts.map +1 -1
- package/types/lib/stages/postgen/specVersionCompat.d.ts.map +1 -1
- package/types/lib/validator/bomValidator.d.ts.map +1 -1
- package/types/lib/validator/index.d.ts +18 -1
- package/types/lib/validator/index.d.ts.map +1 -1
- package/types/lib/validator/reporters/console.d.ts.map +1 -1
- package/types/lib/validator/reporters/json.d.ts.map +1 -1
- package/types/lib/validator/reporters/sarif.d.ts.map +1 -1
- package/lib/third-party/arborist/snapshots/calc-dep-flags.test.cjs +0 -462
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strict JSON reading for signed documents.
|
|
3
|
+
*
|
|
4
|
+
* JSF signs the RFC 8785 (JCS) form of a document, and JCS is defined over
|
|
5
|
+
* I-JSON (RFC 7493) input. `JSON.parse` and lenient UTF-8 decoding accept
|
|
6
|
+
* things I-JSON rules out, and each lets two parsers read different values
|
|
7
|
+
* from the same signed bytes:
|
|
8
|
+
*
|
|
9
|
+
* - a member name repeated within one object, where `JSON.parse` keeps the last
|
|
10
|
+
* value and other parsers keep the first or refuse the document;
|
|
11
|
+
* - a number an IEEE 754 double cannot hold exactly, which JavaScript rounds
|
|
12
|
+
* while arbitrary-precision parsers keep every digit;
|
|
13
|
+
* - bytes that are not UTF-8, which lenient decoders turn into U+FFFD while
|
|
14
|
+
* others refuse them or read another encoding;
|
|
15
|
+
* - an unpaired UTF-16 surrogate escape, which some parsers keep and others
|
|
16
|
+
* replace.
|
|
17
|
+
*
|
|
18
|
+
* {@link parseStrictJson} rejects all of them, so the values a verifier checks
|
|
19
|
+
* are the values every consumer of the document reads.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Raised when JSON text is well-formed but breaks one of the rules above.
|
|
24
|
+
*/
|
|
25
|
+
export class StrictJsonError extends Error {
|
|
26
|
+
/**
|
|
27
|
+
* @param {string} message - Description of the problem
|
|
28
|
+
* @param {string} pointer - RFC 6901 JSON Pointer to the offending member or value
|
|
29
|
+
*/
|
|
30
|
+
constructor(message, pointer) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.name = "StrictJsonError";
|
|
33
|
+
this.pointer = pointer;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const QUOTE = 0x22;
|
|
38
|
+
const BACKSLASH = 0x5c;
|
|
39
|
+
const OPEN_OBJECT = 0x7b;
|
|
40
|
+
const CLOSE_OBJECT = 0x7d;
|
|
41
|
+
const OPEN_ARRAY = 0x5b;
|
|
42
|
+
const CLOSE_ARRAY = 0x5d;
|
|
43
|
+
const COMMA = 0x2c;
|
|
44
|
+
const MINUS = 0x2d;
|
|
45
|
+
const PLUS = 0x2b;
|
|
46
|
+
const DOT = 0x2e;
|
|
47
|
+
const DIGIT_0 = 0x30;
|
|
48
|
+
const DIGIT_9 = 0x39;
|
|
49
|
+
const LOWER_E = 0x65;
|
|
50
|
+
const UPPER_E = 0x45;
|
|
51
|
+
const LOWER_U = 0x75;
|
|
52
|
+
|
|
53
|
+
// Integers with at most 15 digits are below 2^53, so a double holds them exactly.
|
|
54
|
+
const EXACT_INTEGER_DIGITS = 15;
|
|
55
|
+
const MAX_SHOWN_NUMBER_LENGTH = 40;
|
|
56
|
+
|
|
57
|
+
function isDigit(code) {
|
|
58
|
+
return code >= DIGIT_0 && code <= DIGIT_9;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function escapePointerToken(token) {
|
|
62
|
+
return token.replaceAll("~", "~0").replaceAll("/", "~1");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function pointerOf(frames, depth) {
|
|
66
|
+
let pointer = "";
|
|
67
|
+
for (let i = 0; i < depth; i++) {
|
|
68
|
+
const frame = frames[i];
|
|
69
|
+
pointer += `/${frame.object ? escapePointerToken(frame.key) : frame.index}`;
|
|
70
|
+
}
|
|
71
|
+
return pointer;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function showNumber(token) {
|
|
75
|
+
return token.length > MAX_SHOWN_NUMBER_LENGTH
|
|
76
|
+
? `${token.slice(0, MAX_SHOWN_NUMBER_LENGTH)}…`
|
|
77
|
+
: token;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Reduces a JSON or ECMAScript number literal to its exact decimal value as a
|
|
82
|
+
* significand without leading or trailing zeros and a power-of-ten exponent.
|
|
83
|
+
* The sign is ignored: a finite non-zero double keeps the sign of its literal,
|
|
84
|
+
* and zero compares equal to negative zero.
|
|
85
|
+
*/
|
|
86
|
+
function decimalValue(literal) {
|
|
87
|
+
let i = literal.charCodeAt(0) === MINUS ? 1 : 0;
|
|
88
|
+
let digits = "";
|
|
89
|
+
let fractionLength = 0;
|
|
90
|
+
while (i < literal.length && isDigit(literal.charCodeAt(i))) {
|
|
91
|
+
digits += literal[i++];
|
|
92
|
+
}
|
|
93
|
+
if (literal.charCodeAt(i) === DOT) {
|
|
94
|
+
i++;
|
|
95
|
+
while (i < literal.length && isDigit(literal.charCodeAt(i))) {
|
|
96
|
+
digits += literal[i++];
|
|
97
|
+
fractionLength++;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
let exponent = 0;
|
|
101
|
+
const marker = literal.charCodeAt(i);
|
|
102
|
+
if (marker === LOWER_E || marker === UPPER_E) {
|
|
103
|
+
exponent = Number(literal.slice(i + 1));
|
|
104
|
+
}
|
|
105
|
+
let start = 0;
|
|
106
|
+
while (start < digits.length && digits.charCodeAt(start) === DIGIT_0) {
|
|
107
|
+
start++;
|
|
108
|
+
}
|
|
109
|
+
let end = digits.length;
|
|
110
|
+
while (end > start && digits.charCodeAt(end - 1) === DIGIT_0) {
|
|
111
|
+
end--;
|
|
112
|
+
}
|
|
113
|
+
if (start === end) {
|
|
114
|
+
return { significand: "", exponent: 0 };
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
significand: digits.slice(start, end),
|
|
118
|
+
exponent: exponent - fractionLength + (digits.length - end),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Returns a description of why a JSON number token does not denote exactly the
|
|
124
|
+
* double that `JSON.parse` produces for it, or undefined when it does.
|
|
125
|
+
*/
|
|
126
|
+
function numberProblem(token) {
|
|
127
|
+
const negative = token.charCodeAt(0) === MINUS;
|
|
128
|
+
const digitCount = token.length - (negative ? 1 : 0);
|
|
129
|
+
if (digitCount <= EXACT_INTEGER_DIGITS) {
|
|
130
|
+
let integer = true;
|
|
131
|
+
for (let i = negative ? 1 : 0; i < token.length; i++) {
|
|
132
|
+
if (!isDigit(token.charCodeAt(i))) {
|
|
133
|
+
integer = false;
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (integer) {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const value = Number(token);
|
|
142
|
+
if (!Number.isFinite(value)) {
|
|
143
|
+
return "is outside the range of an IEEE 754 double";
|
|
144
|
+
}
|
|
145
|
+
const written = decimalValue(token);
|
|
146
|
+
const parsed = decimalValue(String(value));
|
|
147
|
+
if (
|
|
148
|
+
written.significand === parsed.significand &&
|
|
149
|
+
(written.significand === "" || written.exponent === parsed.exponent)
|
|
150
|
+
) {
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
return `cannot be held exactly by an IEEE 754 double; JSON.parse reads it as ${String(value)}`;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function isHighSurrogate(unit) {
|
|
157
|
+
return unit >= 0xd800 && unit <= 0xdbff;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function isLowSurrogate(unit) {
|
|
161
|
+
return unit >= 0xdc00 && unit <= 0xdfff;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Finds the end of the string token starting at `start`, and whether it holds
|
|
166
|
+
* escapes or a UTF-16 surrogate, written raw or as `\u` escape, without its
|
|
167
|
+
* other half.
|
|
168
|
+
*/
|
|
169
|
+
function stringEnd(text, start) {
|
|
170
|
+
let i = start + 1;
|
|
171
|
+
let escaped = false;
|
|
172
|
+
let unpaired = false;
|
|
173
|
+
let pendingHigh = false;
|
|
174
|
+
for (;;) {
|
|
175
|
+
const code = text.charCodeAt(i);
|
|
176
|
+
if (code === QUOTE) {
|
|
177
|
+
return { end: i + 1, escaped, unpaired: unpaired || pendingHigh };
|
|
178
|
+
}
|
|
179
|
+
let unit = code;
|
|
180
|
+
let width = 1;
|
|
181
|
+
if (code === BACKSLASH) {
|
|
182
|
+
escaped = true;
|
|
183
|
+
if (text.charCodeAt(i + 1) === LOWER_U) {
|
|
184
|
+
unit = Number.parseInt(text.slice(i + 2, i + 6), 16);
|
|
185
|
+
width = 6;
|
|
186
|
+
} else {
|
|
187
|
+
unit = 0;
|
|
188
|
+
width = 2;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const low = isLowSurrogate(unit);
|
|
192
|
+
if (pendingHigh !== low) {
|
|
193
|
+
unpaired = true;
|
|
194
|
+
}
|
|
195
|
+
pendingHigh = isHighSurrogate(unit);
|
|
196
|
+
i += width;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function numberEnd(text, start) {
|
|
201
|
+
let i = start;
|
|
202
|
+
while (i < text.length) {
|
|
203
|
+
const code = text.charCodeAt(i);
|
|
204
|
+
if (
|
|
205
|
+
isDigit(code) ||
|
|
206
|
+
code === MINUS ||
|
|
207
|
+
code === PLUS ||
|
|
208
|
+
code === DOT ||
|
|
209
|
+
code === LOWER_E ||
|
|
210
|
+
code === UPPER_E
|
|
211
|
+
) {
|
|
212
|
+
i++;
|
|
213
|
+
} else {
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return i;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Walks well-formed JSON text without recursion and throws a StrictJsonError
|
|
222
|
+
* at the first repeated member name, inexact number, or unpaired surrogate.
|
|
223
|
+
*/
|
|
224
|
+
function checkStrictJson(text) {
|
|
225
|
+
const frames = [];
|
|
226
|
+
let i = 0;
|
|
227
|
+
while (i < text.length) {
|
|
228
|
+
const code = text.charCodeAt(i);
|
|
229
|
+
if (code === OPEN_OBJECT || code === OPEN_ARRAY) {
|
|
230
|
+
frames.push({
|
|
231
|
+
object: code === OPEN_OBJECT,
|
|
232
|
+
names: code === OPEN_OBJECT ? new Set() : undefined,
|
|
233
|
+
key: undefined,
|
|
234
|
+
index: 0,
|
|
235
|
+
expectName: code === OPEN_OBJECT,
|
|
236
|
+
});
|
|
237
|
+
i++;
|
|
238
|
+
} else if (code === CLOSE_OBJECT || code === CLOSE_ARRAY) {
|
|
239
|
+
frames.pop();
|
|
240
|
+
i++;
|
|
241
|
+
} else if (code === COMMA) {
|
|
242
|
+
const top = frames.at(-1);
|
|
243
|
+
if (top.object) {
|
|
244
|
+
top.expectName = true;
|
|
245
|
+
} else {
|
|
246
|
+
top.index++;
|
|
247
|
+
}
|
|
248
|
+
i++;
|
|
249
|
+
} else if (code === QUOTE) {
|
|
250
|
+
const { end, escaped, unpaired } = stringEnd(text, i);
|
|
251
|
+
const top = frames.at(-1);
|
|
252
|
+
const isName = Boolean(top?.object && top.expectName);
|
|
253
|
+
if (unpaired) {
|
|
254
|
+
const pointer = isName
|
|
255
|
+
? `${pointerOf(frames, frames.length - 1)}/${escapePointerToken(JSON.parse(text.slice(i, end)))}`
|
|
256
|
+
: pointerOf(frames, frames.length);
|
|
257
|
+
throw new StrictJsonError(
|
|
258
|
+
`The ${isName ? "member name" : "string"} at ${pointer || "the document root"} holds an unpaired UTF-16 surrogate, which JSON parsers keep, replace, or reject differently.`,
|
|
259
|
+
pointer,
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
if (isName) {
|
|
263
|
+
const name = escaped
|
|
264
|
+
? JSON.parse(text.slice(i, end))
|
|
265
|
+
: text.slice(i + 1, end - 1);
|
|
266
|
+
if (top.names.has(name)) {
|
|
267
|
+
throw new StrictJsonError(
|
|
268
|
+
`Duplicate member name at ${pointerOf(frames, frames.length - 1)}/${escapePointerToken(name)}. JSON parsers disagree on which of the repeated values the document holds.`,
|
|
269
|
+
`${pointerOf(frames, frames.length - 1)}/${escapePointerToken(name)}`,
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
top.names.add(name);
|
|
273
|
+
top.key = name;
|
|
274
|
+
top.expectName = false;
|
|
275
|
+
}
|
|
276
|
+
i = end;
|
|
277
|
+
} else if (code === MINUS || isDigit(code)) {
|
|
278
|
+
const end = numberEnd(text, i);
|
|
279
|
+
const token = text.slice(i, end);
|
|
280
|
+
const problem = numberProblem(token);
|
|
281
|
+
if (problem) {
|
|
282
|
+
const pointer = pointerOf(frames, frames.length);
|
|
283
|
+
throw new StrictJsonError(
|
|
284
|
+
`The number ${showNumber(token)} at ${pointer || "the document root"} ${problem}.`,
|
|
285
|
+
pointer,
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
i = end;
|
|
289
|
+
} else {
|
|
290
|
+
i++;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Parses JSON the way `JSON.parse` does, but refuses bytes that are not UTF-8,
|
|
297
|
+
* repeated member names within an object, numbers that an IEEE 754 double
|
|
298
|
+
* cannot hold exactly, and unpaired UTF-16 surrogates.
|
|
299
|
+
*
|
|
300
|
+
* Pass the raw bytes of a document rather than text decoded elsewhere, so that
|
|
301
|
+
* invalid UTF-8 is detected instead of being replaced by U+FFFD.
|
|
302
|
+
*
|
|
303
|
+
* @param {string|Uint8Array} input - JSON text, or its UTF-8 bytes
|
|
304
|
+
* @returns {any} - The parsed value
|
|
305
|
+
* @throws {SyntaxError} When the text is not JSON
|
|
306
|
+
* @throws {StrictJsonError} When the input breaks one of the rules above
|
|
307
|
+
*/
|
|
308
|
+
export function parseStrictJson(input) {
|
|
309
|
+
let text = input;
|
|
310
|
+
if (typeof input !== "string") {
|
|
311
|
+
try {
|
|
312
|
+
text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
|
|
313
|
+
input,
|
|
314
|
+
);
|
|
315
|
+
} catch {
|
|
316
|
+
throw new StrictJsonError("The document is not valid UTF-8.", "");
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
const value = JSON.parse(text);
|
|
320
|
+
checkStrictJson(text);
|
|
321
|
+
return value;
|
|
322
|
+
}
|