@rocket.chat/houston 0.27.0-next.2 → 2.1.8
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/LICENSE +1 -1
- package/README.md +108 -15
- package/bin/houston +62 -0
- package/data/docs/ARCHITECTURE.md +387 -0
- package/data/docs/automation.md +107 -0
- package/data/docs/e2ee.md +76 -0
- package/data/docs/eks.md +258 -0
- package/data/docs/gui.md +282 -0
- package/data/docs/guide.md +669 -0
- package/data/docs/kubernetes.md +138 -0
- package/data/docs/troubleshooting.md +193 -0
- package/data/docs/upgrading.md +84 -0
- package/data/docs/voice.md +253 -0
- package/data/iam/engineer-policy.json +11 -0
- package/data/iam/engineer-rbac.yaml +42 -0
- package/data/iam/operator-policy.json +211 -0
- package/data/loadtest/badbot.js +22 -0
- package/data/loadtest/common.js +168 -0
- package/data/loadtest/custom.js +20 -0
- package/data/loadtest/journey.js +58 -0
- package/data/loadtest/login.js +18 -0
- package/data/loadtest/messages.js +13 -0
- package/data/loadtest/mixed.js +39 -0
- package/data/loadtest/read.js +12 -0
- package/data/loadtest/webhook.js +20 -0
- package/data/monitoring/houston-logs.json +98 -0
- package/data/monitoring/k6-loadtest.json +610 -0
- package/data/monitoring/mongodb-exporter.json +3007 -0
- package/data/monitoring/node-exporter-full.json +16114 -0
- package/data/monitoring/rocketchat-metrics.json +6471 -0
- package/data/presets/airgapped.yaml +16 -0
- package/data/presets/default.yaml +9 -0
- package/data/seed/avatar-0.png +0 -0
- package/data/seed/avatar-1.png +0 -0
- package/data/seed/avatar-2.png +0 -0
- package/data/seed/avatar-3.png +0 -0
- package/data/seed/avatar-4.png +0 -0
- package/data/seed/avatar-5.png +0 -0
- package/data/seed/avatar-6.png +0 -0
- package/data/seed/avatar-7.png +0 -0
- package/data/seed/seed-metrics.csv +3 -0
- package/data/seed/seed-preview.png +0 -0
- package/data/seed/seed-report.pdf +32 -0
- package/data/seed/seed-runbook.txt +3 -0
- package/data/skill/SKILL.md +399 -0
- package/data/versions.yaml +55 -0
- package/data/voice/freeswitch/drachtio.xml +52 -0
- package/data/voice/freeswitch/public.xml +87 -0
- package/data/webui/app.css +2003 -0
- package/data/webui/index.html +678 -0
- package/data/webui/js/api.js +561 -0
- package/data/webui/js/boot.js +44 -0
- package/data/webui/js/clusters-tail.js +1377 -0
- package/data/webui/js/clusters.js +896 -0
- package/data/webui/js/create.js +276 -0
- package/data/webui/js/detail.js +507 -0
- package/data/webui/js/jobs.js +636 -0
- package/data/webui/js/logs.js +634 -0
- package/data/webui/js/main.js +14 -0
- package/data/webui/js/options.js +408 -0
- package/data/webui/js/people.js +438 -0
- package/data/webui/js/perf.js +303 -0
- package/data/webui/js/render.js +438 -0
- package/data/webui/js/shared.js +3 -0
- package/data/webui/js/state.js +583 -0
- package/data/webui/js/theme.js +123 -0
- package/data/webui/js/util.js +584 -0
- package/dist/cli/app.js +593 -0
- package/dist/cli/capabilities.js +76 -0
- package/dist/cli/cell-widths.js +495 -0
- package/dist/cli/entry.js +10 -0
- package/dist/cli/error-panel.js +492 -0
- package/dist/cli/handlers/actor.js +84 -0
- package/dist/cli/handlers/https.js +614 -0
- package/dist/cli/handlers/prompt.js +299 -0
- package/dist/cli/handlers/users.js +226 -0
- package/dist/cli/handlers.js +3678 -0
- package/dist/cli/help.js +183 -0
- package/dist/cli/index.js +15 -0
- package/dist/cli/perf.js +810 -0
- package/dist/cli/render.js +254 -0
- package/dist/cli/serve.js +858 -0
- package/dist/cli/spec-data.js +2593 -0
- package/dist/cli/spec-types.js +8 -0
- package/dist/cli/usage-error.js +520 -0
- package/dist/cli/wire.js +70 -0
- package/dist/core/audit.js +346 -0
- package/dist/core/backup-compat.js +85 -0
- package/dist/core/backup.js +1639 -0
- package/dist/core/cluster.js +1398 -0
- package/dist/core/configimport.js +344 -0
- package/dist/core/data.js +267 -0
- package/dist/core/database-of.js +82 -0
- package/dist/core/diagnose.js +159 -0
- package/dist/core/doctor.js +1518 -0
- package/dist/core/edge/acme.js +338 -0
- package/dist/core/edge/engine.js +500 -0
- package/dist/core/edge/networks.js +174 -0
- package/dist/core/edge/paths.js +107 -0
- package/dist/core/edge/routes.js +251 -0
- package/dist/core/edge/routeyml.js +195 -0
- package/dist/core/edge/runtime.js +493 -0
- package/dist/core/edge/spec.js +305 -0
- package/dist/core/edge/types.js +202 -0
- package/dist/core/eks-docs.js +135 -0
- package/dist/core/eks.js +2240 -0
- package/dist/core/envvars.js +562 -0
- package/dist/core/eventlog.js +541 -0
- package/dist/core/firstrun.js +153 -0
- package/dist/core/journal.js +691 -0
- package/dist/core/kube/ask.js +231 -0
- package/dist/core/kube/capabilities.js +269 -0
- package/dist/core/kube/certmanager.js +452 -0
- package/dist/core/kube/chart.js +413 -0
- package/dist/core/kube/cluster.js +196 -0
- package/dist/core/kube/context.js +545 -0
- package/dist/core/kube/endpoints.js +448 -0
- package/dist/core/kube/exec.js +195 -0
- package/dist/core/kube/forward.js +732 -0
- package/dist/core/kube/grafana-ingress.js +110 -0
- package/dist/core/kube/ingress.js +218 -0
- package/dist/core/kube/manifests.js +361 -0
- package/dist/core/kube/monitoring.js +768 -0
- package/dist/core/kube/names.js +237 -0
- package/dist/core/kube/namespace.js +678 -0
- package/dist/core/kube/operator.js +941 -0
- package/dist/core/kube/probe.js +202 -0
- package/dist/core/kube/reachability.js +163 -0
- package/dist/core/kube/release.js +439 -0
- package/dist/core/kube/replicaset.js +180 -0
- package/dist/core/kube/scale.js +398 -0
- package/dist/core/kube/status.js +633 -0
- package/dist/core/kube/tools.js +238 -0
- package/dist/core/kube/transfer.js +173 -0
- package/dist/core/kube/workspace.js +423 -0
- package/dist/core/lifecycle/admin.js +64 -0
- package/dist/core/lifecycle/create-docker.js +1533 -0
- package/dist/core/lifecycle/create-k8s.js +939 -0
- package/dist/core/lifecycle/destroy.js +339 -0
- package/dist/core/lifecycle/inspect.js +635 -0
- package/dist/core/lifecycle/naming.js +298 -0
- package/dist/core/lifecycle/notes.js +726 -0
- package/dist/core/lifecycle/ownership.js +313 -0
- package/dist/core/lifecycle/payload.js +401 -0
- package/dist/core/lifecycle/ports.js +388 -0
- package/dist/core/lifecycle/preconditions.js +367 -0
- package/dist/core/lifecycle/pruning.js +551 -0
- package/dist/core/lifecycle/ready.js +753 -0
- package/dist/core/lifecycle/request.js +525 -0
- package/dist/core/lifecycle/seeding.js +281 -0
- package/dist/core/lifecycle/sizing.js +392 -0
- package/dist/core/lifecycle/state.js +286 -0
- package/dist/core/lifecycle/tlsplan.js +704 -0
- package/dist/core/lifecycle/types.js +232 -0
- package/dist/core/loadtest.js +995 -0
- package/dist/core/migrate.js +612 -0
- package/dist/core/monitor.js +494 -0
- package/dist/core/postready.js +969 -0
- package/dist/core/promote.js +804 -0
- package/dist/core/rcapi.js +1285 -0
- package/dist/core/recovery.js +75 -0
- package/dist/core/runner/engine.js +394 -0
- package/dist/core/runner/host.js +298 -0
- package/dist/core/runner/locks.js +129 -0
- package/dist/core/runner/paths.js +173 -0
- package/dist/core/runner/ports.js +172 -0
- package/dist/core/runner/queries.js +612 -0
- package/dist/core/runner/record.js +563 -0
- package/dist/core/scaleseed.js +271 -0
- package/dist/core/seed-db.js +208 -0
- package/dist/core/sessions.js +472 -0
- package/dist/core/skill.js +326 -0
- package/dist/core/sso-identities.js +172 -0
- package/dist/core/sso.js +703 -0
- package/dist/core/tls-local.js +311 -0
- package/dist/core/tls.js +878 -0
- package/dist/core/topology.js +482 -0
- package/dist/core/upgrade.js +868 -0
- package/dist/core/users.js +739 -0
- package/dist/lib/audit.js +100 -0
- package/dist/lib/close-matches.js +238 -0
- package/dist/lib/compose.js +672 -0
- package/dist/lib/config.js +1398 -0
- package/dist/lib/context.js +18 -0
- package/dist/lib/errors.js +305 -0
- package/dist/lib/events.js +208 -0
- package/dist/lib/float-text.js +126 -0
- package/dist/lib/format.js +1320 -0
- package/dist/lib/home.js +325 -0
- package/dist/lib/journal.js +122 -0
- package/dist/lib/json-text.js +733 -0
- package/dist/lib/jsonout.js +382 -0
- package/dist/lib/lock.js +516 -0
- package/dist/lib/login-name.js +202 -0
- package/dist/lib/map-limit.js +29 -0
- package/dist/lib/mkdir-parents.js +90 -0
- package/dist/lib/mongo-kernel.js +67 -0
- package/dist/lib/monitoring.js +492 -0
- package/dist/lib/platform-name.js +59 -0
- package/dist/lib/presets/common.js +390 -0
- package/dist/lib/presets/composite.js +326 -0
- package/dist/lib/presets/e2ee.js +143 -0
- package/dist/lib/presets/email.js +172 -0
- package/dist/lib/presets/guides.js +27 -0
- package/dist/lib/presets/index.js +490 -0
- package/dist/lib/presets/keycloak.js +369 -0
- package/dist/lib/presets/ldap.js +1285 -0
- package/dist/lib/presets/livechat.js +220 -0
- package/dist/lib/presets/multi-instance.js +124 -0
- package/dist/lib/presets/oidc.js +161 -0
- package/dist/lib/presets/public.js +496 -0
- package/dist/lib/presets/registry.js +64 -0
- package/dist/lib/presets/s3-minio.js +300 -0
- package/dist/lib/presets/saml.js +149 -0
- package/dist/lib/presets/scenario.js +51 -0
- package/dist/lib/presets/types.js +86 -0
- package/dist/lib/presets/voice-pbx.js +320 -0
- package/dist/lib/presets/voice.js +345 -0
- package/dist/lib/presets/webhooks.js +182 -0
- package/dist/lib/redact.js +106 -0
- package/dist/lib/seed-options.js +68 -0
- package/dist/lib/seed-profiles.js +59 -0
- package/dist/lib/targz.js +1383 -0
- package/dist/lib/teammode.js +25 -0
- package/dist/lib/tempdir.js +162 -0
- package/dist/lib/unicode-tables.js +2335 -0
- package/dist/lib/version-spec.js +357 -0
- package/dist/lib/versions.js +463 -0
- package/dist/lib/yaml-dump.js +1361 -0
- package/dist/perf/baseline.js +168 -0
- package/dist/perf/constrain.js +296 -0
- package/dist/perf/k6.js +235 -0
- package/dist/perf/mongoprof.js +171 -0
- package/dist/perf/rcmetrics.js +178 -0
- package/dist/perf/report.js +386 -0
- package/dist/perf/resources.js +224 -0
- package/dist/perf/slo.js +111 -0
- package/dist/perf/timeline.js +187 -0
- package/dist/perf/timings.js +85 -0
- package/dist/perf/verdict.js +201 -0
- package/dist/seams/aws-cli.js +126 -0
- package/dist/seams/docker-cli.js +280 -0
- package/dist/seams/exec.js +861 -0
- package/dist/seams/fs/lock.js +528 -0
- package/dist/seams/kubectl.js +83 -0
- package/dist/seams/rc-http.js +921 -0
- package/dist/seed/apply.js +190 -0
- package/dist/seed/assets.js +76 -0
- package/dist/seed/check.js +391 -0
- package/dist/seed/client.js +172 -0
- package/dist/seed/executor.js +726 -0
- package/dist/seed/history.js +281 -0
- package/dist/seed/kinds.js +91 -0
- package/dist/seed/plan.js +432 -0
- package/dist/seed/receipt.js +339 -0
- package/dist/seed/repairs.js +75 -0
- package/dist/seed/verification.js +363 -0
- package/dist/seed/wording.js +248 -0
- package/dist/web/app.js +326 -0
- package/dist/web/jobs.js +510 -0
- package/dist/web/route-deps.js +211 -0
- package/dist/web/route-table.js +213 -0
- package/dist/web/routes/backup.js +244 -0
- package/dist/web/routes/clusters.js +303 -0
- package/dist/web/routes/data.js +187 -0
- package/dist/web/routes/edge.js +82 -0
- package/dist/web/routes/index.js +63 -0
- package/dist/web/routes/jobs.js +95 -0
- package/dist/web/routes/logs.js +297 -0
- package/dist/web/routes/meta.js +191 -0
- package/dist/web/routes/people.js +151 -0
- package/dist/web/routes/perf.js +95 -0
- package/dist/web/routes/repros.js +550 -0
- package/dist/web/routes/session.js +361 -0
- package/dist/web/routes/spa.js +198 -0
- package/dist/web/security.js +475 -0
- package/dist/web/signin.js +246 -0
- package/package.json +67 -32
- package/.eslintrc +0 -94
- package/.swcrc +0 -18
- package/dist/bin.js +0 -3
- package/dist/cli.js +0 -184
- package/dist/commands/createBranch.js +0 -192
- package/src/bin.js +0 -4
- package/src/cli.ts +0 -25
- package/src/commands/createBranch.ts +0 -25
- package/src-old/logs.js +0 -323
- package/src-old/md.js +0 -191
- package/src-old/release-table.js +0 -60
- package/src-old/set-version.js +0 -614
- package/src-old/utils.js +0 -15
|
@@ -0,0 +1,733 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JsonFloat = exports.JsonNumber = exports.JsonSyntaxError = void 0;
|
|
4
|
+
exports.decodeError = decodeError;
|
|
5
|
+
exports.loads = loads;
|
|
6
|
+
exports.orderedDict = orderedDict;
|
|
7
|
+
exports.spelled = spelled;
|
|
8
|
+
exports.shownLoaded = shownLoaded;
|
|
9
|
+
exports.dumps = dumps;
|
|
10
|
+
/**
|
|
11
|
+
* A JSON reader, and specifically the error it gives when a document fails -- and a JSON
|
|
12
|
+
* writer, the one every state file goes through, in the spelling earlier releases wrote.
|
|
13
|
+
*
|
|
14
|
+
* `JSON.parse` is the right parser for a document that parses; this module exists for the
|
|
15
|
+
* one that does not. A customer support dump is exactly the file that arrives wrong --
|
|
16
|
+
* truncated by a download, an HTML error page from an expired share link, or the whole
|
|
17
|
+
* bundle instead of the settings file inside it -- and `configimport` reports where it went
|
|
18
|
+
* wrong by line and column. The two engines disagree completely about the words:
|
|
19
|
+
*
|
|
20
|
+
* established : line 1, column 1: Expecting value
|
|
21
|
+
* V8 : Unexpected token '<', "<html>x" is not valid JSON
|
|
22
|
+
*
|
|
23
|
+
* so the same bad file would produce a different sentence from the established one. The
|
|
24
|
+
* happy path still goes through `JSON.parse`; this scanner runs only after it has already
|
|
25
|
+
* thrown, and its whole job is to find the FIRST fault and name it in the established words.
|
|
26
|
+
*
|
|
27
|
+
* It does not build values. `JSON.parse` does that, and a second value builder would be a
|
|
28
|
+
* second set of rules about numbers and escapes.
|
|
29
|
+
*
|
|
30
|
+
* The writer is here for the opposite reason: `JSON.stringify` is NOT the established
|
|
31
|
+
* writer, and the difference lands in files (see `dumps`).
|
|
32
|
+
*/
|
|
33
|
+
const format_1 = require("./format");
|
|
34
|
+
const float_text_1 = require("./float-text");
|
|
35
|
+
Object.defineProperty(exports, "JsonFloat", { enumerable: true, get: function () { return float_text_1.JsonFloat; } });
|
|
36
|
+
/** A decoder failure: one of a closed set of messages, with its line, column and char. */
|
|
37
|
+
class JsonSyntaxError extends Error {
|
|
38
|
+
constructor(msg, doc, pos) {
|
|
39
|
+
const lineno = countNewlines(doc, pos) + 1;
|
|
40
|
+
const colno = pos - doc.lastIndexOf('\n', pos - 1);
|
|
41
|
+
super(`${msg}: line ${String(lineno)} column ${String(colno)} (char ${String(pos)})`);
|
|
42
|
+
this.msg = msg;
|
|
43
|
+
this.doc = doc;
|
|
44
|
+
this.pos = pos;
|
|
45
|
+
this.name = 'JsonSyntaxError';
|
|
46
|
+
this.lineno = lineno;
|
|
47
|
+
this.colno = colno;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.JsonSyntaxError = JsonSyntaxError;
|
|
51
|
+
function countNewlines(doc, end) {
|
|
52
|
+
let n = 0;
|
|
53
|
+
for (let i = 0; i < end && i < doc.length; i += 1)
|
|
54
|
+
if (doc[i] === '\n')
|
|
55
|
+
n += 1;
|
|
56
|
+
return n;
|
|
57
|
+
}
|
|
58
|
+
/** JSON's insignificant whitespace: space, tab, LF, CR. */
|
|
59
|
+
const WS = new Set([' ', '\t', '\n', '\r']);
|
|
60
|
+
/**
|
|
61
|
+
* The index after the whitespace that starts at `i`.
|
|
62
|
+
*
|
|
63
|
+
* Clamped to the document's length when `i` is past the end -- and the clamp is
|
|
64
|
+
* load-bearing: `[` walks one past the bracket and must come back to report "Expecting
|
|
65
|
+
* value" at index 1, not at index 2.
|
|
66
|
+
*/
|
|
67
|
+
function skipWs(s, i) {
|
|
68
|
+
let j = Math.min(i, s.length);
|
|
69
|
+
while (j < s.length && WS.has(s[j]))
|
|
70
|
+
j += 1;
|
|
71
|
+
return j;
|
|
72
|
+
}
|
|
73
|
+
/** The character at `i` -- the empty string past the end, never undefined. */
|
|
74
|
+
function at(s, i) {
|
|
75
|
+
return i >= 0 && i < s.length ? s[i] : '';
|
|
76
|
+
}
|
|
77
|
+
/** The JSON number grammar, anchored at `idx`. Returns the end index, or -1 for no match. */
|
|
78
|
+
function scanNumber(s, idx) {
|
|
79
|
+
const re = /(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?/y;
|
|
80
|
+
re.lastIndex = idx;
|
|
81
|
+
const m = re.exec(s);
|
|
82
|
+
return m === null ? -1 : re.lastIndex;
|
|
83
|
+
}
|
|
84
|
+
const BACKSLASH = new Set(['"', '\\', '/', 'b', 'f', 'n', 'r', 't']);
|
|
85
|
+
/** A string, scanned from just after the opening quote. Returns the index after the close. */
|
|
86
|
+
function scanString(s, end) {
|
|
87
|
+
const begin = end - 1;
|
|
88
|
+
let i = end;
|
|
89
|
+
for (;;) {
|
|
90
|
+
// The next terminator of any kind: a quote, a backslash or a control character.
|
|
91
|
+
let terminatorAt = -1;
|
|
92
|
+
for (let j = i; j < s.length; j += 1) {
|
|
93
|
+
const ch = s.charCodeAt(j);
|
|
94
|
+
if (ch === 0x22 || ch === 0x5c || ch < 0x20) {
|
|
95
|
+
terminatorAt = j;
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (terminatorAt < 0)
|
|
100
|
+
throw new JsonSyntaxError('Unterminated string starting at', s, begin);
|
|
101
|
+
const terminator = s[terminatorAt];
|
|
102
|
+
i = terminatorAt + 1;
|
|
103
|
+
if (terminator === '"')
|
|
104
|
+
return i;
|
|
105
|
+
if (terminator !== '\\') {
|
|
106
|
+
throw new JsonSyntaxError('Invalid control character at', s, terminatorAt);
|
|
107
|
+
}
|
|
108
|
+
if (i >= s.length)
|
|
109
|
+
throw new JsonSyntaxError('Unterminated string starting at', s, begin);
|
|
110
|
+
const esc = s[i];
|
|
111
|
+
if (esc !== 'u') {
|
|
112
|
+
if (!BACKSLASH.has(esc))
|
|
113
|
+
throw new JsonSyntaxError('Invalid \\escape', s, terminatorAt);
|
|
114
|
+
i += 1;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const hex = s.slice(i + 1, i + 5);
|
|
118
|
+
if (hex.length !== 4 || !/^[0-9a-fA-F]{4}$/u.test(hex)) {
|
|
119
|
+
throw new JsonSyntaxError('Invalid \\uXXXX escape', s, i);
|
|
120
|
+
}
|
|
121
|
+
i += 5;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** An array, scanned from just after the `[`. Returns the index after the `]`. */
|
|
125
|
+
function scanArray(s, end, tokens) {
|
|
126
|
+
let i = end;
|
|
127
|
+
let nextchar = at(s, i);
|
|
128
|
+
if (nextchar === '' || WS.has(nextchar)) {
|
|
129
|
+
i = skipWs(s, i + 1);
|
|
130
|
+
nextchar = at(s, i);
|
|
131
|
+
}
|
|
132
|
+
if (nextchar === ']')
|
|
133
|
+
return i + 1;
|
|
134
|
+
for (;;) {
|
|
135
|
+
i = scanOnce(s, i, tokens);
|
|
136
|
+
nextchar = at(s, i);
|
|
137
|
+
if (nextchar === '' || WS.has(nextchar)) {
|
|
138
|
+
i = skipWs(s, i + 1);
|
|
139
|
+
nextchar = at(s, i);
|
|
140
|
+
}
|
|
141
|
+
i += 1;
|
|
142
|
+
if (nextchar === ']')
|
|
143
|
+
break;
|
|
144
|
+
if (nextchar !== ',')
|
|
145
|
+
throw new JsonSyntaxError("Expecting ',' delimiter", s, i - 1);
|
|
146
|
+
if (i < s.length && WS.has(s[i])) {
|
|
147
|
+
i += 1;
|
|
148
|
+
if (i < s.length && WS.has(s[i]))
|
|
149
|
+
i = skipWs(s, i + 1);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return i;
|
|
153
|
+
}
|
|
154
|
+
/** An object, scanned from just after the `{`. Returns the index after the `}`. */
|
|
155
|
+
function scanObject(s, end, tokens) {
|
|
156
|
+
let i = end;
|
|
157
|
+
let nextchar = at(s, i);
|
|
158
|
+
if (nextchar !== '"') {
|
|
159
|
+
if (nextchar === '' || WS.has(nextchar)) {
|
|
160
|
+
i = skipWs(s, i);
|
|
161
|
+
nextchar = at(s, i);
|
|
162
|
+
}
|
|
163
|
+
if (nextchar === '}')
|
|
164
|
+
return i + 1;
|
|
165
|
+
if (nextchar !== '"') {
|
|
166
|
+
throw new JsonSyntaxError('Expecting property name enclosed in double quotes', s, i);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
i += 1;
|
|
170
|
+
for (;;) {
|
|
171
|
+
i = scanString(s, i);
|
|
172
|
+
if (at(s, i) !== ':') {
|
|
173
|
+
i = skipWs(s, i);
|
|
174
|
+
if (at(s, i) !== ':')
|
|
175
|
+
throw new JsonSyntaxError("Expecting ':' delimiter", s, i);
|
|
176
|
+
}
|
|
177
|
+
i += 1;
|
|
178
|
+
if (i < s.length && WS.has(s[i])) {
|
|
179
|
+
i += 1;
|
|
180
|
+
if (i < s.length && WS.has(s[i]))
|
|
181
|
+
i = skipWs(s, i + 1);
|
|
182
|
+
}
|
|
183
|
+
i = scanOnce(s, i, tokens);
|
|
184
|
+
nextchar = at(s, i);
|
|
185
|
+
if (nextchar !== '' && WS.has(nextchar)) {
|
|
186
|
+
i = skipWs(s, i + 1);
|
|
187
|
+
nextchar = at(s, i);
|
|
188
|
+
}
|
|
189
|
+
i += 1;
|
|
190
|
+
if (nextchar === '}')
|
|
191
|
+
break;
|
|
192
|
+
if (nextchar !== ',')
|
|
193
|
+
throw new JsonSyntaxError("Expecting ',' delimiter", s, i - 1);
|
|
194
|
+
i = skipWs(s, i);
|
|
195
|
+
nextchar = at(s, i);
|
|
196
|
+
i += 1;
|
|
197
|
+
if (nextchar !== '"') {
|
|
198
|
+
throw new JsonSyntaxError('Expecting property name enclosed in double quotes', s, i - 1);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return i;
|
|
202
|
+
}
|
|
203
|
+
/** One value, scanned. Returns the index after it. */
|
|
204
|
+
function scanOnce(s, idx, tokens) {
|
|
205
|
+
if (idx >= s.length)
|
|
206
|
+
throw new JsonSyntaxError('Expecting value', s, idx);
|
|
207
|
+
const ch = s[idx];
|
|
208
|
+
if (ch === '"')
|
|
209
|
+
return scanString(s, idx + 1);
|
|
210
|
+
if (ch === '{')
|
|
211
|
+
return scanObject(s, idx + 1, tokens);
|
|
212
|
+
if (ch === '[')
|
|
213
|
+
return scanArray(s, idx + 1, tokens);
|
|
214
|
+
if (s.startsWith('null', idx))
|
|
215
|
+
return idx + 4;
|
|
216
|
+
if (s.startsWith('true', idx))
|
|
217
|
+
return idx + 4;
|
|
218
|
+
if (s.startsWith('false', idx))
|
|
219
|
+
return idx + 5;
|
|
220
|
+
const numberEnd = scanNumber(s, idx);
|
|
221
|
+
if (numberEnd >= 0)
|
|
222
|
+
return numberEnd;
|
|
223
|
+
for (const constant of ['NaN', 'Infinity', '-Infinity']) {
|
|
224
|
+
if (s.startsWith(constant, idx)) {
|
|
225
|
+
tokens?.push([idx, constant]);
|
|
226
|
+
return idx + constant.length;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
throw new JsonSyntaxError('Expecting value', s, idx);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Find the first fault, with the message and position the established decoder reports.
|
|
233
|
+
*
|
|
234
|
+
* Only called on a document `JSON.parse` has already rejected. If the scanner somehow
|
|
235
|
+
* finds nothing wrong -- a disagreement between the two parsers, which would be a real
|
|
236
|
+
* finding -- the caller's own error is raised rather than a fabricated one.
|
|
237
|
+
*/
|
|
238
|
+
function decodeError(text, tokens) {
|
|
239
|
+
try {
|
|
240
|
+
const end = skipWs(text, scanOnce(text, skipWs(text, 0), tokens));
|
|
241
|
+
if (end !== text.length)
|
|
242
|
+
return new JsonSyntaxError('Extra data', text, end);
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
catch (error) {
|
|
246
|
+
if (error instanceof JsonSyntaxError)
|
|
247
|
+
return error;
|
|
248
|
+
throw error;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/** Stands in for a constant while `JSON.parse` reads the rest; private-use, so no document's. */
|
|
252
|
+
const CONSTANT_MARK = '\uE000pyjson\uE001';
|
|
253
|
+
const CONSTANTS = {
|
|
254
|
+
NaN: Number.NaN,
|
|
255
|
+
Infinity: Number.POSITIVE_INFINITY,
|
|
256
|
+
'-Infinity': Number.NEGATIVE_INFINITY,
|
|
257
|
+
};
|
|
258
|
+
/**
|
|
259
|
+
* `text` parsed as JSON, raising `JsonSyntaxError` with the established message and
|
|
260
|
+
* position.
|
|
261
|
+
*
|
|
262
|
+
* And ACCEPTING the constants `NaN`, `Infinity` and `-Infinity` as values: `dumps` writes
|
|
263
|
+
* them for a float that is one, so a record or a manifest written that way and read back
|
|
264
|
+
* was, to `JSON.parse`, not JSON at all -- a workspace with a NaN in its record vanished
|
|
265
|
+
* from `list` and crashed `info`. The scanner already knows where each constant stands
|
|
266
|
+
* outside a string; each is swapped for a marker string that `JSON.parse` reads, and the
|
|
267
|
+
* reviver puts the number back.
|
|
268
|
+
*
|
|
269
|
+
* The values are plain -- objects, arrays, doubles -- so every reader reads what it read
|
|
270
|
+
* before. What a plain value cannot hold of the document is kept BESIDE it, for `dumps`
|
|
271
|
+
* (see `spell`): a rewrite of what this read writes back what the document said.
|
|
272
|
+
*
|
|
273
|
+
* And REFUSING before it scans: a text that starts with U+FEFF is `Unexpected UTF-8 BOM`
|
|
274
|
+
* at position 0, before the decoder sees a character. Without
|
|
275
|
+
* that check the BOM reached the scanner as a stray character and was "Expecting value" --
|
|
276
|
+
* so a settings dump a Windows editor had saved lost the one sentence that names what is
|
|
277
|
+
* wrong with it, and a BOM-led manifest or record was refused in other words than the
|
|
278
|
+
* established ones. Only the FIRST character: a BOM anywhere after it is the scanner's to
|
|
279
|
+
* report.
|
|
280
|
+
*/
|
|
281
|
+
function loads(text, options = {}) {
|
|
282
|
+
if (text.startsWith('\uFEFF')) {
|
|
283
|
+
throw new JsonSyntaxError('Unexpected UTF-8 BOM', text, 0);
|
|
284
|
+
}
|
|
285
|
+
let value;
|
|
286
|
+
try {
|
|
287
|
+
value = JSON.parse(text);
|
|
288
|
+
}
|
|
289
|
+
catch (error) {
|
|
290
|
+
const tokens = [];
|
|
291
|
+
const decoded = decodeError(text, tokens);
|
|
292
|
+
if (decoded !== null)
|
|
293
|
+
throw decoded;
|
|
294
|
+
if (tokens.length === 0 || text.includes(CONSTANT_MARK))
|
|
295
|
+
throw error;
|
|
296
|
+
let rebuilt = '';
|
|
297
|
+
let last = 0;
|
|
298
|
+
for (const [position, constant] of tokens) {
|
|
299
|
+
rebuilt += `${text.slice(last, position)}${JSON.stringify(CONSTANT_MARK + constant)}`;
|
|
300
|
+
last = position + constant.length;
|
|
301
|
+
}
|
|
302
|
+
rebuilt += text.slice(last);
|
|
303
|
+
value = JSON.parse(rebuilt, (_key, member) => typeof member === 'string' && member.startsWith(CONSTANT_MARK)
|
|
304
|
+
? (CONSTANTS[member.slice(CONSTANT_MARK.length)] ?? member)
|
|
305
|
+
: member);
|
|
306
|
+
}
|
|
307
|
+
return spell(text, value, options);
|
|
308
|
+
}
|
|
309
|
+
const SPELLINGS = new WeakMap();
|
|
310
|
+
/**
|
|
311
|
+
* `value`, with what its text says beside it (`Spelling`), for `dumps`.
|
|
312
|
+
*
|
|
313
|
+
* `JSON.parse` still builds every value -- the escapes, the numbers and `__proto__` are its
|
|
314
|
+
* rules, as the module comment says they must be. The text is walked a second time only
|
|
315
|
+
* for what the value cannot say, and a walk that disagreed with the parser (which would be
|
|
316
|
+
* a real finding) leaves the value as the parser built it.
|
|
317
|
+
*/
|
|
318
|
+
function spell(text, value, options) {
|
|
319
|
+
let shape;
|
|
320
|
+
try {
|
|
321
|
+
shape = shapeAt(text, 0)[1];
|
|
322
|
+
}
|
|
323
|
+
catch {
|
|
324
|
+
return value;
|
|
325
|
+
}
|
|
326
|
+
annotate(value, shape);
|
|
327
|
+
if (options.spelledRoot === true && shape?.kind === 'number' && typeof value === 'number') {
|
|
328
|
+
return new JsonNumber(value, shape.spelling);
|
|
329
|
+
}
|
|
330
|
+
return value;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* The text `dumps` writes for a number literal, or null where its double already writes
|
|
334
|
+
* it.
|
|
335
|
+
*
|
|
336
|
+
* An integer literal is exact at any size: `String` of the double spells it only below
|
|
337
|
+
* 2**53 (and not at all from 1e21, where it turns to `1e+21`). A literal with a point or an
|
|
338
|
+
* exponent is a float, which `dumps` spells by `floatString` unless it is integral, because
|
|
339
|
+
* only the integral ones are indistinguishable from an int once parsed.
|
|
340
|
+
*/
|
|
341
|
+
function spellingOf(literal) {
|
|
342
|
+
const value = Number(literal);
|
|
343
|
+
if (!/[.eE]/u.test(literal)) {
|
|
344
|
+
const exact = BigInt(literal).toString();
|
|
345
|
+
return String(value) === exact ? null : exact;
|
|
346
|
+
}
|
|
347
|
+
return Number.isInteger(value) ? (0, float_text_1.floatString)(value) : null;
|
|
348
|
+
}
|
|
349
|
+
/** A key JavaScript enumerates before the others: an array index, `0` to `2**32 - 2`. */
|
|
350
|
+
function isIndex(key) {
|
|
351
|
+
return /^(?:0|[1-9]\d{0,9})$/u.test(key) && Number(key) < 4294967295;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* The shape of the value at `start`, and the index after it.
|
|
355
|
+
*
|
|
356
|
+
* Over a document already accepted -- by `JSON.parse`, or by the scanner above for one
|
|
357
|
+
* holding a constant -- so it only walks; anything unexpected is thrown, and `spell`
|
|
358
|
+
* then leaves the value alone. A container nothing in which needs a spelling is `null`,
|
|
359
|
+
* so the common record costs one walk and no table entry.
|
|
360
|
+
*/
|
|
361
|
+
function shapeAt(s, start) {
|
|
362
|
+
const i = skipWs(s, start);
|
|
363
|
+
const ch = at(s, i);
|
|
364
|
+
if (ch === '"')
|
|
365
|
+
return [scanString(s, i + 1), null];
|
|
366
|
+
if (ch === '{')
|
|
367
|
+
return shapeObject(s, i + 1);
|
|
368
|
+
if (ch === '[')
|
|
369
|
+
return shapeArray(s, i + 1);
|
|
370
|
+
for (const word of ['null', 'true', 'false', 'NaN', 'Infinity', '-Infinity']) {
|
|
371
|
+
if (s.startsWith(word, i))
|
|
372
|
+
return [i + word.length, null];
|
|
373
|
+
}
|
|
374
|
+
const end = scanNumber(s, i);
|
|
375
|
+
if (end < 0)
|
|
376
|
+
throw new SyntaxError(`no value at ${String(i)}`);
|
|
377
|
+
const spelling = spellingOf(s.slice(i, end));
|
|
378
|
+
return [end, spelling === null ? null : { kind: 'number', spelling }];
|
|
379
|
+
}
|
|
380
|
+
function shapeObject(s, start) {
|
|
381
|
+
let i = skipWs(s, start);
|
|
382
|
+
if (at(s, i) === '}')
|
|
383
|
+
return [i + 1, null];
|
|
384
|
+
// First occurrence for the position, last for the value: a mapping built from the pairs,
|
|
385
|
+
// as `JSON.parse` builds its object.
|
|
386
|
+
const keys = [];
|
|
387
|
+
const members = new Map();
|
|
388
|
+
for (;;) {
|
|
389
|
+
if (at(s, i) !== '"')
|
|
390
|
+
throw new SyntaxError(`no key at ${String(i)}`);
|
|
391
|
+
const keyEnd = scanString(s, i + 1);
|
|
392
|
+
const token = s.slice(i, keyEnd);
|
|
393
|
+
const key = token.includes('\\') ? JSON.parse(token) : token.slice(1, -1);
|
|
394
|
+
i = skipWs(s, keyEnd);
|
|
395
|
+
if (at(s, i) !== ':')
|
|
396
|
+
throw new SyntaxError(`no ':' at ${String(i)}`);
|
|
397
|
+
const [end, member] = shapeAt(s, i + 1);
|
|
398
|
+
if (!members.has(key))
|
|
399
|
+
keys.push(key);
|
|
400
|
+
members.set(key, member);
|
|
401
|
+
i = skipWs(s, end);
|
|
402
|
+
const next = at(s, i);
|
|
403
|
+
i = skipWs(s, i + 1);
|
|
404
|
+
if (next === '}')
|
|
405
|
+
break;
|
|
406
|
+
if (next !== ',')
|
|
407
|
+
throw new SyntaxError(`no ',' at ${String(i)}`);
|
|
408
|
+
}
|
|
409
|
+
const needed = keys.some(isIndex) || [...members.values()].some((member) => member !== null);
|
|
410
|
+
return [i, needed ? { kind: 'object', keys, members } : null];
|
|
411
|
+
}
|
|
412
|
+
function shapeArray(s, start) {
|
|
413
|
+
let i = skipWs(s, start);
|
|
414
|
+
if (at(s, i) === ']')
|
|
415
|
+
return [i + 1, null];
|
|
416
|
+
const items = [];
|
|
417
|
+
for (;;) {
|
|
418
|
+
const [end, item] = shapeAt(s, i);
|
|
419
|
+
items.push(item);
|
|
420
|
+
i = skipWs(s, end);
|
|
421
|
+
const next = at(s, i);
|
|
422
|
+
i += 1;
|
|
423
|
+
if (next === ']')
|
|
424
|
+
break;
|
|
425
|
+
if (next !== ',')
|
|
426
|
+
throw new SyntaxError(`no ',' at ${String(i - 1)}`);
|
|
427
|
+
}
|
|
428
|
+
return [i, items.some((item) => item !== null) ? { kind: 'array', items } : null];
|
|
429
|
+
}
|
|
430
|
+
/** Record in `SPELLINGS` what `shape` says of `value` that `value` does not. */
|
|
431
|
+
function annotate(value, shape) {
|
|
432
|
+
if (shape === null || shape.kind === 'number' || value === null || typeof value !== 'object') {
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
const numbers = new Map();
|
|
436
|
+
let order;
|
|
437
|
+
const visit = (key, member, sub) => {
|
|
438
|
+
if (sub?.kind !== 'number')
|
|
439
|
+
annotate(member, sub);
|
|
440
|
+
else if (typeof member === 'number')
|
|
441
|
+
numbers.set(key, [member, sub.spelling]);
|
|
442
|
+
};
|
|
443
|
+
if (shape.kind === 'array') {
|
|
444
|
+
if (!Array.isArray(value))
|
|
445
|
+
return;
|
|
446
|
+
shape.items.forEach((sub, index) => visit(String(index), value[index], sub));
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
if (Array.isArray(value))
|
|
450
|
+
return;
|
|
451
|
+
const bag = value;
|
|
452
|
+
const own = Object.keys(bag);
|
|
453
|
+
if (own.length === shape.keys.length && own.some((key, index) => key !== shape.keys[index])) {
|
|
454
|
+
order = shape.keys;
|
|
455
|
+
}
|
|
456
|
+
for (const [key, sub] of shape.members)
|
|
457
|
+
visit(key, bag[key], sub);
|
|
458
|
+
}
|
|
459
|
+
if (order !== undefined || numbers.size > 0)
|
|
460
|
+
SPELLINGS.set(value, { order, numbers });
|
|
461
|
+
}
|
|
462
|
+
/** `names` (an object's own keys) in `order`, then any it gained since, in its own order. */
|
|
463
|
+
function documentOrder(names, order) {
|
|
464
|
+
const present = new Set(names);
|
|
465
|
+
const listed = order.filter((name) => present.has(name));
|
|
466
|
+
const seen = new Set(listed);
|
|
467
|
+
return [...listed, ...names.filter((name) => !seen.has(name))];
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* A copy of an object `loads` built: a new object with the same members, in the
|
|
471
|
+
* same order, with the same spellings -- which a spread `{...extra}` is not. It is a new
|
|
472
|
+
* object in JavaScript's key order, with nothing beside it, so a record copied that way
|
|
473
|
+
* before a rewrite lost what the table above keeps.
|
|
474
|
+
*
|
|
475
|
+
* Otherwise it IS the spread it replaces, so a call site changes in nothing else: the same
|
|
476
|
+
* own keys (of a list or a string too, and none of null), each defined rather than
|
|
477
|
+
* assigned, so a `__proto__` key stays a key.
|
|
478
|
+
*/
|
|
479
|
+
function orderedDict(value) {
|
|
480
|
+
const out = {};
|
|
481
|
+
if (value === null || value === undefined)
|
|
482
|
+
return out;
|
|
483
|
+
const bag = value;
|
|
484
|
+
const spelling = typeof value === 'object' ? SPELLINGS.get(value) : undefined;
|
|
485
|
+
const names = spelling?.order === undefined
|
|
486
|
+
? Object.keys(bag)
|
|
487
|
+
: documentOrder(Object.keys(bag), spelling.order);
|
|
488
|
+
for (const name of names) {
|
|
489
|
+
Object.defineProperty(out, name, {
|
|
490
|
+
value: bag[name],
|
|
491
|
+
enumerable: true,
|
|
492
|
+
writable: true,
|
|
493
|
+
configurable: true,
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
if (spelling !== undefined) {
|
|
497
|
+
SPELLINGS.set(out, {
|
|
498
|
+
order: spelling.order === undefined ? undefined : names,
|
|
499
|
+
numbers: new Map(spelling.numbers),
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
return out;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* A number with the text it was read from, which its double does not spell.
|
|
506
|
+
*
|
|
507
|
+
* Only `loads(text, { spelledRoot: true })` makes one, for a document that is a single
|
|
508
|
+
* number a double does not spell: `api -d 12345678901234567890` sent `12345678901234567000`.
|
|
509
|
+
* A number INSIDE a document needs no wrapper; `loads` keeps its spelling beside the
|
|
510
|
+
* container that holds it.
|
|
511
|
+
*/
|
|
512
|
+
class JsonNumber {
|
|
513
|
+
constructor(value, spelling) {
|
|
514
|
+
this.value = value;
|
|
515
|
+
this.spelling = spelling;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
exports.JsonNumber = JsonNumber;
|
|
519
|
+
/**
|
|
520
|
+
* `container[key]` with the spelling `loads` kept for it, for a reader that takes a member
|
|
521
|
+
* OUT of what `loads` built.
|
|
522
|
+
*
|
|
523
|
+
* The spelling a double loses is kept beside the CONTAINER (`Spelling`), so a value that
|
|
524
|
+
* stays where it was read is written back right -- and one copied out of it is not. That is
|
|
525
|
+
* how repro.json's own fields lost their floats: `fromDocument` copies each top-level field
|
|
526
|
+
* onto the record, and a rewrite wrote `"host_port": 3100.0` back as `3100`. And how a
|
|
527
|
+
* settings dump's values did: each is copied into the import plan, and its `5.0`, `-0.0`
|
|
528
|
+
* and `1e+16` were printed and sent as `5`, `0` and `10000000000000000`.
|
|
529
|
+
*
|
|
530
|
+
* The member itself when nothing needs keeping -- a string, a container (which carries its
|
|
531
|
+
* own table), a double that spells the document's value. Otherwise a `JsonFloat` for a float
|
|
532
|
+
* that is integral, and a `JsonNumber` for an int past 2**53, which `dumps` and
|
|
533
|
+
* `shownLoaded` write in the document's spelling.
|
|
534
|
+
*/
|
|
535
|
+
function spelled(container, key) {
|
|
536
|
+
const member = container[key];
|
|
537
|
+
const kept = SPELLINGS.get(container)?.numbers.get(key);
|
|
538
|
+
if (kept === undefined || typeof member !== 'number' || !Object.is(kept[0], member)) {
|
|
539
|
+
return member;
|
|
540
|
+
}
|
|
541
|
+
return /^-?\d+$/u.test(kept[1]) ? new JsonNumber(member, kept[1]) : new float_text_1.JsonFloat(member);
|
|
542
|
+
}
|
|
543
|
+
/** What `shownSpelled` needs of `loads`'s table, for `format` a rank below it. */
|
|
544
|
+
const LOADED_SPELLINGS = {
|
|
545
|
+
// A `JsonFloat` needs nothing from here: `shownSpelled` writes the float it wraps on its
|
|
546
|
+
// own, as it does for one config.yaml's reader made.
|
|
547
|
+
scalar: (value) => (value instanceof JsonNumber ? value.spelling : undefined),
|
|
548
|
+
member: (container, key, held) => {
|
|
549
|
+
const kept = SPELLINGS.get(container)?.numbers.get(key);
|
|
550
|
+
return kept !== undefined && Object.is(kept[0], held) ? kept[1] : undefined;
|
|
551
|
+
},
|
|
552
|
+
keys: (container) => {
|
|
553
|
+
const order = SPELLINGS.get(container)?.order;
|
|
554
|
+
const names = Object.keys(container);
|
|
555
|
+
return order === undefined ? names : documentOrder(names, order);
|
|
556
|
+
},
|
|
557
|
+
};
|
|
558
|
+
/**
|
|
559
|
+
* What `loads` built as a person reads it (`shownValue`), with what the document said kept: a float that is
|
|
560
|
+
* integral keeps its `.0` (`5.0`, `-0.0`, `1e+16`, nested in a list too), an int past
|
|
561
|
+
* 2**53 its digits, and a mapping its keys in document order.
|
|
562
|
+
*
|
|
563
|
+
* The text `dumps` writes for such a number is the same text, so the one table serves both
|
|
564
|
+
* writers.
|
|
565
|
+
* `shownValue` is the same function without the table, for a value no JSON document built.
|
|
566
|
+
*/
|
|
567
|
+
function shownLoaded(value) {
|
|
568
|
+
return (0, format_1.shownSpelled)(value, LOADED_SPELLINGS);
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* `value` as JSON, byte for byte the established spelling -- the one the state files
|
|
572
|
+
* earlier releases wrote already carry.
|
|
573
|
+
*
|
|
574
|
+
* `JSON.stringify` differs from it in four ways, and each reaches a file:
|
|
575
|
+
*
|
|
576
|
+
* ASCII only every character outside space..tilde is written as `\uXXXX` (an astral
|
|
577
|
+
* one as its surrogate pair, DEL included); V8 writes it raw.
|
|
578
|
+
* constants `NaN`, `Infinity` and `-Infinity` are written bare; V8 writes `null`, so
|
|
579
|
+
* a record rewritten by it read back with a null where the number was.
|
|
580
|
+
* float text a float is spelled by `floatString`: `1e-07`, `5e-05`; V8 `1e-7`, `0.00005`.
|
|
581
|
+
* layout the separators are `', '` and `': '` unless told otherwise.
|
|
582
|
+
*
|
|
583
|
+
* A number that is integral is written as an int, because which of `1.0` and `1` the
|
|
584
|
+
* document held is gone once JSON has been parsed (`floatString`'s doc comment);
|
|
585
|
+
* a caller that knows wraps it in `JsonFloat`. Only a non-integral one is a float for
|
|
586
|
+
* certain, and that is spelled by `floatString`. Except where `loads` read it: a member still
|
|
587
|
+
* holding the double it was read as is written as its document spelled it, and an object's
|
|
588
|
+
* keys in its document's order (`Spelling`), so `3.0` stays `3.0`, an int past 2**53 stays
|
|
589
|
+
* digit for digit, and `{"b": 1, "2": 2}` keeps `b` first.
|
|
590
|
+
*
|
|
591
|
+
* What JSON cannot spell -- `undefined`, a bigint, a function, a symbol -- goes to
|
|
592
|
+
* `default` when one is given, and what it returns is written instead. Without one,
|
|
593
|
+
* `undefined` is left out of an object and written `null` in an array, which is what
|
|
594
|
+
* `JSON.stringify` did for every writer this replaced (this code spells "absent" that way);
|
|
595
|
+
* the others raise the TypeError `Object of type ... is not JSON serializable`.
|
|
596
|
+
*/
|
|
597
|
+
function dumps(value, options = {}) {
|
|
598
|
+
const indent = options.indent ?? null;
|
|
599
|
+
const [item, key] = options.separators ?? (indent === null ? [', ', ': '] : [',', ': ']);
|
|
600
|
+
return encode(value, 0, {
|
|
601
|
+
step: indent === null ? null : ' '.repeat(Math.max(0, indent)),
|
|
602
|
+
item,
|
|
603
|
+
key,
|
|
604
|
+
sortKeys: options.sortKeys ?? false,
|
|
605
|
+
allowNan: options.allowNan ?? true,
|
|
606
|
+
fallback: options.default,
|
|
607
|
+
open: new Set(),
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
/** A bad value, as `format` names one: a RangeError named `InvalidValue`. */
|
|
611
|
+
function valueError(message) {
|
|
612
|
+
const error = new RangeError(message);
|
|
613
|
+
error.name = 'InvalidValue';
|
|
614
|
+
return error;
|
|
615
|
+
}
|
|
616
|
+
/** A float's text (`floatString`), or one of the three constants. */
|
|
617
|
+
function encodeFloat(value, how) {
|
|
618
|
+
if (Number.isFinite(value))
|
|
619
|
+
return (0, float_text_1.floatString)(value);
|
|
620
|
+
if (!how.allowNan) {
|
|
621
|
+
throw valueError(`Out of range float values are not JSON compliant: ${(0, float_text_1.floatString)(value)}`);
|
|
622
|
+
}
|
|
623
|
+
return Number.isNaN(value) ? 'NaN' : value > 0 ? 'Infinity' : '-Infinity';
|
|
624
|
+
}
|
|
625
|
+
function encode(value, depth, how) {
|
|
626
|
+
if (value === null)
|
|
627
|
+
return 'null';
|
|
628
|
+
if (value === true)
|
|
629
|
+
return 'true';
|
|
630
|
+
if (value === false)
|
|
631
|
+
return 'false';
|
|
632
|
+
if (typeof value === 'string')
|
|
633
|
+
return encodeString(value);
|
|
634
|
+
if (typeof value === 'number') {
|
|
635
|
+
return Number.isInteger(value) ? String(value) : encodeFloat(value, how);
|
|
636
|
+
}
|
|
637
|
+
if (value instanceof float_text_1.JsonFloat)
|
|
638
|
+
return encodeFloat(value.value, how);
|
|
639
|
+
if (value instanceof JsonNumber)
|
|
640
|
+
return value.spelling;
|
|
641
|
+
if (typeof value !== 'object') {
|
|
642
|
+
if (how.fallback !== undefined)
|
|
643
|
+
return encode(how.fallback(value), depth, how);
|
|
644
|
+
if (value === undefined)
|
|
645
|
+
return 'null';
|
|
646
|
+
throw new TypeError(`Object of type ${typeof value} is not JSON serializable`);
|
|
647
|
+
}
|
|
648
|
+
if (how.open.has(value))
|
|
649
|
+
throw valueError('Circular reference detected');
|
|
650
|
+
how.open.add(value);
|
|
651
|
+
let members;
|
|
652
|
+
let brackets;
|
|
653
|
+
// What `loads` kept beside this container of what it read (`Spelling`).
|
|
654
|
+
const spelling = SPELLINGS.get(value);
|
|
655
|
+
const member = (item, name) => {
|
|
656
|
+
const spelled = spelling?.numbers.get(name);
|
|
657
|
+
return spelled !== undefined && Object.is(spelled[0], item)
|
|
658
|
+
? spelled[1]
|
|
659
|
+
: encode(item, depth + 1, how);
|
|
660
|
+
};
|
|
661
|
+
if (Array.isArray(value)) {
|
|
662
|
+
members = [];
|
|
663
|
+
for (let i = 0; i < value.length; i += 1)
|
|
664
|
+
members.push(member(value[i], String(i)));
|
|
665
|
+
brackets = '[]';
|
|
666
|
+
}
|
|
667
|
+
else {
|
|
668
|
+
const bag = value;
|
|
669
|
+
let names = Object.keys(bag);
|
|
670
|
+
if (spelling?.order !== undefined)
|
|
671
|
+
names = documentOrder(names, spelling.order);
|
|
672
|
+
if (how.fallback === undefined)
|
|
673
|
+
names = names.filter((name) => bag[name] !== undefined);
|
|
674
|
+
if (how.sortKeys)
|
|
675
|
+
names.sort(compareCodePoints);
|
|
676
|
+
members = names.map((name) => `${encodeString(name)}${how.key}${member(bag[name], name)}`);
|
|
677
|
+
brackets = '{}';
|
|
678
|
+
}
|
|
679
|
+
how.open.delete(value);
|
|
680
|
+
const [opening, closing] = [brackets[0], brackets[1]];
|
|
681
|
+
if (members.length === 0)
|
|
682
|
+
return brackets;
|
|
683
|
+
if (how.step === null)
|
|
684
|
+
return `${opening}${members.join(how.item)}${closing}`;
|
|
685
|
+
const inner = `\n${how.step.repeat(depth + 1)}`;
|
|
686
|
+
return `${opening}${inner}${members.join(how.item + inner)}\n${how.step.repeat(depth)}${closing}`;
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Code point order for strings, not UTF-16 code unit order.
|
|
690
|
+
*
|
|
691
|
+
* The two differ only when a string contains an astral character (`\u{1f680}` sorts above
|
|
692
|
+
* `\uffff` by code point and below it by UTF-16 unit).
|
|
693
|
+
*/
|
|
694
|
+
function compareCodePoints(left, right) {
|
|
695
|
+
const a = [...left];
|
|
696
|
+
const b = [...right];
|
|
697
|
+
for (let i = 0; i < Math.min(a.length, b.length); i += 1) {
|
|
698
|
+
const ac = a[i].codePointAt(0);
|
|
699
|
+
const bc = b[i].codePointAt(0);
|
|
700
|
+
if (ac !== bc)
|
|
701
|
+
return ac < bc ? -1 : 1;
|
|
702
|
+
}
|
|
703
|
+
return a.length - b.length;
|
|
704
|
+
}
|
|
705
|
+
const ESCAPES = Object.freeze({
|
|
706
|
+
'"': '\\"',
|
|
707
|
+
'\\': '\\\\',
|
|
708
|
+
'\n': '\\n',
|
|
709
|
+
'\r': '\\r',
|
|
710
|
+
'\t': '\\t',
|
|
711
|
+
'\b': '\\b',
|
|
712
|
+
'\f': '\\f',
|
|
713
|
+
});
|
|
714
|
+
/**
|
|
715
|
+
* The quoted, ASCII-only spelling of one string.
|
|
716
|
+
*
|
|
717
|
+
* What is escaped is `([\\"]|[^\ -~])`, and it is applied here per UTF-16 unit -- so an
|
|
718
|
+
* astral character is its surrogate pair, escaped one half at a time, and a lone surrogate
|
|
719
|
+
* is escaped as itself.
|
|
720
|
+
*/
|
|
721
|
+
function encodeString(value) {
|
|
722
|
+
let out = '"';
|
|
723
|
+
let plain = 0;
|
|
724
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
725
|
+
const unit = value.charCodeAt(i);
|
|
726
|
+
if (unit >= 0x20 && unit <= 0x7e && unit !== 0x22 && unit !== 0x5c)
|
|
727
|
+
continue;
|
|
728
|
+
out += value.slice(plain, i);
|
|
729
|
+
out += ESCAPES[value[i]] ?? `\\u${unit.toString(16).padStart(4, '0')}`;
|
|
730
|
+
plain = i + 1;
|
|
731
|
+
}
|
|
732
|
+
return `${out}${value.slice(plain)}"`;
|
|
733
|
+
}
|