@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,1361 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The established YAML 1.1 emission, reproduced: a safe representer, a serializer, a
|
|
4
|
+
* resolver and an emitter, for the values the two writers here hand it.
|
|
5
|
+
*
|
|
6
|
+
* Both YAML files earlier releases wrote -- a workspace's `docker-compose.yml` (`compose.ts`)
|
|
7
|
+
* and `config.yaml` (`config.ts`) -- were written here by the `yaml` package, whose emitter
|
|
8
|
+
* is a different program with different choices: it double-quotes where the established
|
|
9
|
+
* files single-quote (`PORT: "3000"` against `'3000'`), never folds a long scalar at the
|
|
10
|
+
* 80th column, writes non-ASCII raw where the established files escape it, quotes `1e8`,
|
|
11
|
+
* `y` and `N` that this module's resolver leaves plain, spells an empty key `""` where the
|
|
12
|
+
* established files hold the complex `? ''`, indents a list under its key, names anchors
|
|
13
|
+
* `&a1` where the Serializer names them `&id001`, and escapes in lower case. Every such
|
|
14
|
+
* file read back to the same value, but every `env --set` of a workspace an earlier release
|
|
15
|
+
* made rewrote 7 to 16 of its lines, and every `config set` the whole of config.yaml.
|
|
16
|
+
* Options were tried against the package first and cannot close it: the quote rules, the
|
|
17
|
+
* fold points and the complex-key rule are the emitter's own code.
|
|
18
|
+
*
|
|
19
|
+
* So this is that emitter, state for state and writer for writer, with the one
|
|
20
|
+
* configuration both call sites use: block style, keys in their own order, indent 2, width
|
|
21
|
+
* 80, non-ASCII escaped, no explicit start or end, no version or tag directives. The flow
|
|
22
|
+
* and canonical branches the emitter has are kept, since an empty collection is written in
|
|
23
|
+
* flow style (`[]`, `{}`) and the checks share them. The emitter measures a string in code
|
|
24
|
+
* points, so every length, index and column here counts code points, not UTF-16 units.
|
|
25
|
+
*
|
|
26
|
+
* The tag a JavaScript value is represented with:
|
|
27
|
+
* - `null` and `undefined` are `!!null`; a mapping entry whose value is `undefined` is
|
|
28
|
+
* left out, as the package left it out;
|
|
29
|
+
* - a string is a `!!str`, a boolean a `!!bool`, a bigint an `!!int`;
|
|
30
|
+
* - a number is an `!!int` when it is integral, and a `!!float` otherwise or when it is -0
|
|
31
|
+
* (the int readers here return 0 for `-0`, so a -0 can only be a float's). A float that
|
|
32
|
+
* holds an integral value (`1.0`) is not recoverable from a number, and is written as
|
|
33
|
+
* the int, unless it arrives as a `JsonFloat`, which is always the `!!float` it wraps;
|
|
34
|
+
* - a `Uint8Array` is `!!binary`, a `Set` a `!!set`, a `Date` a `!!timestamp`, a date or
|
|
35
|
+
* a datetime as `dateText` spells it;
|
|
36
|
+
* - an array is a `!!seq`, a `Map` a `!!map` with its keys as they are, and a plain
|
|
37
|
+
* object a `!!map` of its own enumerable string keys in their order.
|
|
38
|
+
* An object met twice is ONE node, which the Serializer anchors; a string, number, boolean
|
|
39
|
+
* or `Uint8Array` never is.
|
|
40
|
+
*/
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.safeDump = safeDump;
|
|
43
|
+
const node_util_1 = require("node:util");
|
|
44
|
+
const float_text_1 = require("./float-text");
|
|
45
|
+
/** A `RepresenterError` or an `EmitterError`, named as the error line shows it. */
|
|
46
|
+
function yamlError(name, message) {
|
|
47
|
+
const error = new Error(message);
|
|
48
|
+
error.name = name;
|
|
49
|
+
return error;
|
|
50
|
+
}
|
|
51
|
+
// ---------------------------------------------------------------------------------------
|
|
52
|
+
// The resolver: the implicit tag a plain scalar's text would read as.
|
|
53
|
+
//
|
|
54
|
+
// A pattern's `$` also matches before a final line feed, so `yes\n` is a bool; `\n?$` is
|
|
55
|
+
// that `$` here. The verbose-mode whitespace is taken out; the character classes keep theirs.
|
|
56
|
+
const IMPLICIT = [
|
|
57
|
+
[
|
|
58
|
+
'tag:yaml.org,2002:bool',
|
|
59
|
+
/^(?:yes|Yes|YES|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)\n?$/,
|
|
60
|
+
'yYnNtTfFoO',
|
|
61
|
+
],
|
|
62
|
+
[
|
|
63
|
+
'tag:yaml.org,2002:float',
|
|
64
|
+
/^(?:[-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+][0-9]+)?|\.[0-9][0-9_]*(?:[eE][-+][0-9]+)?|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*|[-+]?\.(?:inf|Inf|INF)|\.(?:nan|NaN|NAN))\n?$/,
|
|
65
|
+
'-+0123456789.',
|
|
66
|
+
],
|
|
67
|
+
[
|
|
68
|
+
'tag:yaml.org,2002:int',
|
|
69
|
+
/^(?:[-+]?0b[0-1_]+|[-+]?0[0-7_]+|[-+]?(?:0|[1-9][0-9_]*)|[-+]?0x[0-9a-fA-F_]+|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)\n?$/,
|
|
70
|
+
'-+0123456789',
|
|
71
|
+
],
|
|
72
|
+
['tag:yaml.org,2002:merge', /^(?:<<)\n?$/, '<'],
|
|
73
|
+
// `first` is `['~', 'n', 'N', '']`: the empty string is keyed by itself.
|
|
74
|
+
['tag:yaml.org,2002:null', /^(?:~|null|Null|NULL|)\n?$/, '~nN\0'],
|
|
75
|
+
[
|
|
76
|
+
'tag:yaml.org,2002:timestamp',
|
|
77
|
+
/^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]|[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?(?:[Tt]|[ \t]+)[0-9][0-9]?:[0-9][0-9]:[0-9][0-9](?:\.[0-9]*)?(?:[ \t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)\n?$/,
|
|
78
|
+
'0123456789',
|
|
79
|
+
],
|
|
80
|
+
['tag:yaml.org,2002:value', /^(?:=)\n?$/, '='],
|
|
81
|
+
['tag:yaml.org,2002:yaml', /^(?:!|&|\*)\n?$/, '!&*'],
|
|
82
|
+
];
|
|
83
|
+
/**
|
|
84
|
+
* The implicit resolvers by first character: first character -> the (tag, regexp) pairs in
|
|
85
|
+
* registration order, the resolver's own entries and then `extra` appended after them.
|
|
86
|
+
*/
|
|
87
|
+
function resolverTable(extra) {
|
|
88
|
+
const table = new Map();
|
|
89
|
+
for (const [tag, regexp, first] of [...IMPLICIT, ...extra]) {
|
|
90
|
+
for (const ch of first) {
|
|
91
|
+
const key = ch === '\0' ? '' : ch;
|
|
92
|
+
const list = table.get(key) ?? [];
|
|
93
|
+
list.push([tag, regexp]);
|
|
94
|
+
table.set(key, list);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return table;
|
|
98
|
+
}
|
|
99
|
+
/** The resolver's own table, the one a plain safe dump resolves with. */
|
|
100
|
+
const RESOLVERS = resolverTable([]);
|
|
101
|
+
const STR = 'tag:yaml.org,2002:str';
|
|
102
|
+
/** The tag a plain scalar `value` reads as. */
|
|
103
|
+
function resolvePlain(table, value) {
|
|
104
|
+
const first = value === '' ? '' : Array.from(value)[0];
|
|
105
|
+
for (const [tag, regexp] of table.get(first) ?? []) {
|
|
106
|
+
if (regexp.test(value))
|
|
107
|
+
return tag;
|
|
108
|
+
}
|
|
109
|
+
return STR;
|
|
110
|
+
}
|
|
111
|
+
// ---------------------------------------------------------------------------------------
|
|
112
|
+
// The representer.
|
|
113
|
+
/** A float's text lowered, with `.nan`, `.inf` and `-.inf`, and a `.0` before an `e`. */
|
|
114
|
+
function floatText(value) {
|
|
115
|
+
if (Number.isNaN(value))
|
|
116
|
+
return '.nan';
|
|
117
|
+
if (value === Infinity)
|
|
118
|
+
return '.inf';
|
|
119
|
+
if (value === -Infinity)
|
|
120
|
+
return '-.inf';
|
|
121
|
+
const text = (0, float_text_1.floatString)(value).toLowerCase();
|
|
122
|
+
return !text.includes('.') && text.includes('e') ? text.replace('e', '.0e') : text;
|
|
123
|
+
}
|
|
124
|
+
/** An integer's digits. `String` spells an integral number from 1e21 up with an exponent. */
|
|
125
|
+
function intText(value) {
|
|
126
|
+
if (typeof value === 'bigint')
|
|
127
|
+
return value.toString();
|
|
128
|
+
return Math.abs(value) < 1e21 ? String(value) : BigInt(value).toString();
|
|
129
|
+
}
|
|
130
|
+
/** Base64 in lines of 76 characters, each ended by a line feed. */
|
|
131
|
+
function encodeBytes(data) {
|
|
132
|
+
const text = Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString('base64');
|
|
133
|
+
let out = '';
|
|
134
|
+
for (let at = 0; at < text.length; at += 76)
|
|
135
|
+
out += `${text.slice(at, at + 76)}\n`;
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
/** The ISO text (a space between) of the UTC datetime at the instant a Date holds. */
|
|
139
|
+
function utcDatetimeText(value) {
|
|
140
|
+
const pad = (n, width = 2) => String(n).padStart(width, '0');
|
|
141
|
+
let out = `${pad(value.getUTCFullYear(), 4)}-${pad(value.getUTCMonth() + 1)}-`;
|
|
142
|
+
out += `${pad(value.getUTCDate())} ${pad(value.getUTCHours())}:${pad(value.getUTCMinutes())}:`;
|
|
143
|
+
out += pad(value.getUTCSeconds());
|
|
144
|
+
const micro = value.getUTCMilliseconds() * 1000;
|
|
145
|
+
if (micro !== 0)
|
|
146
|
+
out += `.${pad(micro, 6)}`;
|
|
147
|
+
return `${out}+00:00`;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* An object of no class: its prototype is null or SOME realm's `Object.prototype`. Compared
|
|
151
|
+
* by shape rather than to this realm's, because `structuredClone` -- which `compose.ts`'s
|
|
152
|
+
* deep copy is -- hands back objects of the realm Node's own globals live in, and under
|
|
153
|
+
* jest that is not the realm the module runs in. The built-ins are asked of `util.types`
|
|
154
|
+
* for the same reason, where `instanceof` would answer for this realm only.
|
|
155
|
+
*/
|
|
156
|
+
function isPlainObject(value) {
|
|
157
|
+
const prototype = Object.getPrototypeOf(value);
|
|
158
|
+
return prototype === null || Object.getPrototypeOf(prototype) === null;
|
|
159
|
+
}
|
|
160
|
+
class Representer {
|
|
161
|
+
constructor(options) {
|
|
162
|
+
this.options = options;
|
|
163
|
+
/** The node made for each object already represented, keyed by the object. */
|
|
164
|
+
this.represented = new Map();
|
|
165
|
+
/** Which object the node being made stands for, when it may be aliased. */
|
|
166
|
+
this.aliasKey = null;
|
|
167
|
+
}
|
|
168
|
+
representData(data) {
|
|
169
|
+
// Null, a string, a Uint8Array, a boolean and a number are never aliased -- a
|
|
170
|
+
// `JsonFloat` included, which is an object here and stands for a float.
|
|
171
|
+
this.aliasKey =
|
|
172
|
+
data !== null &&
|
|
173
|
+
typeof data === 'object' &&
|
|
174
|
+
!node_util_1.types.isUint8Array(data) &&
|
|
175
|
+
!(data instanceof float_text_1.JsonFloat)
|
|
176
|
+
? data
|
|
177
|
+
: null;
|
|
178
|
+
if (this.aliasKey !== null) {
|
|
179
|
+
const seen = this.represented.get(this.aliasKey);
|
|
180
|
+
if (seen !== undefined)
|
|
181
|
+
return seen;
|
|
182
|
+
}
|
|
183
|
+
if (data === null || data === undefined) {
|
|
184
|
+
return this.scalar('tag:yaml.org,2002:null', 'null');
|
|
185
|
+
}
|
|
186
|
+
// A `!!float` for a float a number cannot say is one: config.yaml's `ratio: 3.0`
|
|
187
|
+
// and a `3.0:` key were rewritten `ratio: 3` and `3:`, where the file must keep what it
|
|
188
|
+
// held.
|
|
189
|
+
if (data instanceof float_text_1.JsonFloat) {
|
|
190
|
+
return this.scalar('tag:yaml.org,2002:float', floatText(data.value));
|
|
191
|
+
}
|
|
192
|
+
switch (typeof data) {
|
|
193
|
+
case 'string':
|
|
194
|
+
return this.scalar(STR, data);
|
|
195
|
+
case 'boolean':
|
|
196
|
+
return this.scalar('tag:yaml.org,2002:bool', data ? 'true' : 'false');
|
|
197
|
+
case 'bigint':
|
|
198
|
+
return this.scalar('tag:yaml.org,2002:int', intText(data));
|
|
199
|
+
case 'number':
|
|
200
|
+
return Number.isInteger(data) && !Object.is(data, -0)
|
|
201
|
+
? this.scalar('tag:yaml.org,2002:int', intText(data))
|
|
202
|
+
: this.scalar('tag:yaml.org,2002:float', floatText(data));
|
|
203
|
+
default:
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
if (node_util_1.types.isUint8Array(data)) {
|
|
207
|
+
return this.scalar('tag:yaml.org,2002:binary', encodeBytes(data), '|');
|
|
208
|
+
}
|
|
209
|
+
if (node_util_1.types.isDate(data)) {
|
|
210
|
+
const text = this.options.dateText?.(data) ?? utcDatetimeText(data);
|
|
211
|
+
return this.scalar('tag:yaml.org,2002:timestamp', text);
|
|
212
|
+
}
|
|
213
|
+
if (Array.isArray(data))
|
|
214
|
+
return this.sequence('tag:yaml.org,2002:seq', data);
|
|
215
|
+
if (node_util_1.types.isSet(data)) {
|
|
216
|
+
// A `!!set`: a mapping of each member to null, in the order the set holds them.
|
|
217
|
+
return this.mapping('tag:yaml.org,2002:set', [...data].map((member) => [member, null]));
|
|
218
|
+
}
|
|
219
|
+
if (node_util_1.types.isMap(data)) {
|
|
220
|
+
return this.mapping('tag:yaml.org,2002:map', [...data]);
|
|
221
|
+
}
|
|
222
|
+
if (typeof data === 'object' && isPlainObject(data)) {
|
|
223
|
+
return this.mapping('tag:yaml.org,2002:map', Object.keys(data).map((key) => [key, data[key]]));
|
|
224
|
+
}
|
|
225
|
+
// Anything else cannot be represented.
|
|
226
|
+
throw yamlError('RepresenterError', `cannot represent an object: ${String(data)}`);
|
|
227
|
+
}
|
|
228
|
+
scalar(tag, value, style = null) {
|
|
229
|
+
const node = { kind: 'scalar', tag, value, style };
|
|
230
|
+
if (this.aliasKey !== null)
|
|
231
|
+
this.represented.set(this.aliasKey, node);
|
|
232
|
+
return node;
|
|
233
|
+
}
|
|
234
|
+
sequence(tag, items) {
|
|
235
|
+
const node = { kind: 'sequence', tag, value: [], flowStyle: false };
|
|
236
|
+
if (this.aliasKey !== null)
|
|
237
|
+
this.represented.set(this.aliasKey, node);
|
|
238
|
+
for (const item of items)
|
|
239
|
+
node.value.push(this.representData(item));
|
|
240
|
+
// Block style decides, whatever the best style found would be.
|
|
241
|
+
return node;
|
|
242
|
+
}
|
|
243
|
+
mapping(tag, pairs) {
|
|
244
|
+
const node = { kind: 'mapping', tag, value: [], flowStyle: false };
|
|
245
|
+
if (this.aliasKey !== null)
|
|
246
|
+
this.represented.set(this.aliasKey, node);
|
|
247
|
+
for (const [key, value] of pairs) {
|
|
248
|
+
// The package left a key whose value is `undefined` out, as JSON does; so does this.
|
|
249
|
+
if (value === undefined)
|
|
250
|
+
continue;
|
|
251
|
+
const keyNode = this.representData(key);
|
|
252
|
+
const valueNode = this.representData(value);
|
|
253
|
+
node.value.push([keyNode, valueNode]);
|
|
254
|
+
}
|
|
255
|
+
return node;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
// ---------------------------------------------------------------------------------------
|
|
259
|
+
// The serializer.
|
|
260
|
+
class Serializer {
|
|
261
|
+
constructor(emit, resolvers) {
|
|
262
|
+
this.emit = emit;
|
|
263
|
+
this.resolvers = resolvers;
|
|
264
|
+
this.anchors = new Map();
|
|
265
|
+
this.serialized = new Set();
|
|
266
|
+
this.lastAnchorId = 0;
|
|
267
|
+
}
|
|
268
|
+
serialize(node) {
|
|
269
|
+
this.emit({ type: 'DocumentStart', explicit: false });
|
|
270
|
+
this.anchorNode(node);
|
|
271
|
+
this.serializeNode(node);
|
|
272
|
+
this.emit({ type: 'DocumentEnd', explicit: false });
|
|
273
|
+
}
|
|
274
|
+
anchorNode(node) {
|
|
275
|
+
if (this.anchors.has(node)) {
|
|
276
|
+
if (this.anchors.get(node) === null) {
|
|
277
|
+
this.lastAnchorId += 1;
|
|
278
|
+
// An anchor is `id` and three digits: `id001`.
|
|
279
|
+
this.anchors.set(node, `id${String(this.lastAnchorId).padStart(3, '0')}`);
|
|
280
|
+
}
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
this.anchors.set(node, null);
|
|
284
|
+
if (node.kind === 'sequence') {
|
|
285
|
+
for (const item of node.value)
|
|
286
|
+
this.anchorNode(item);
|
|
287
|
+
}
|
|
288
|
+
else if (node.kind === 'mapping') {
|
|
289
|
+
for (const [key, value] of node.value) {
|
|
290
|
+
this.anchorNode(key);
|
|
291
|
+
this.anchorNode(value);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
serializeNode(node) {
|
|
296
|
+
const alias = this.anchors.get(node) ?? null;
|
|
297
|
+
if (this.serialized.has(node)) {
|
|
298
|
+
this.emit({ type: 'Alias', anchor: alias });
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
this.serialized.add(node);
|
|
302
|
+
if (node.kind === 'scalar') {
|
|
303
|
+
const implicit = [
|
|
304
|
+
node.tag === resolvePlain(this.resolvers, node.value),
|
|
305
|
+
node.tag === STR,
|
|
306
|
+
];
|
|
307
|
+
this.emit({
|
|
308
|
+
type: 'Scalar',
|
|
309
|
+
anchor: alias,
|
|
310
|
+
tag: node.tag,
|
|
311
|
+
implicit,
|
|
312
|
+
value: node.value,
|
|
313
|
+
style: node.style,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
else if (node.kind === 'sequence') {
|
|
317
|
+
this.emit({
|
|
318
|
+
type: 'SequenceStart',
|
|
319
|
+
anchor: alias,
|
|
320
|
+
tag: node.tag,
|
|
321
|
+
implicit: node.tag === 'tag:yaml.org,2002:seq',
|
|
322
|
+
flowStyle: node.flowStyle,
|
|
323
|
+
});
|
|
324
|
+
for (const item of node.value)
|
|
325
|
+
this.serializeNode(item);
|
|
326
|
+
this.emit({ type: 'SequenceEnd' });
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
this.emit({
|
|
330
|
+
type: 'MappingStart',
|
|
331
|
+
anchor: alias,
|
|
332
|
+
tag: node.tag,
|
|
333
|
+
implicit: node.tag === 'tag:yaml.org,2002:map',
|
|
334
|
+
flowStyle: node.flowStyle,
|
|
335
|
+
});
|
|
336
|
+
for (const [key, value] of node.value) {
|
|
337
|
+
this.serializeNode(key);
|
|
338
|
+
this.serializeNode(value);
|
|
339
|
+
}
|
|
340
|
+
this.emit({ type: 'MappingEnd' });
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
/** The four line breaks the emitter knows: LF, NEL, U+2028 and U+2029. */
|
|
345
|
+
const BREAKS = '\n\u0085\u2028\u2029';
|
|
346
|
+
/** What counts as whitespace around an indicator: NUL, space, tab, CR and the four breaks. */
|
|
347
|
+
const WHITESPACE = '\0 \t\r\n\u0085\u2028\u2029';
|
|
348
|
+
const ESCAPE_REPLACEMENTS = new Map([
|
|
349
|
+
['\0', '0'],
|
|
350
|
+
['\u0007', 'a'],
|
|
351
|
+
['\b', 'b'],
|
|
352
|
+
['\t', 't'],
|
|
353
|
+
['\n', 'n'],
|
|
354
|
+
['\u000b', 'v'],
|
|
355
|
+
['\f', 'f'],
|
|
356
|
+
['\r', 'r'],
|
|
357
|
+
['\u001b', 'e'],
|
|
358
|
+
['"', '"'],
|
|
359
|
+
['\\', '\\'],
|
|
360
|
+
['\u0085', 'N'],
|
|
361
|
+
['\u00a0', '_'],
|
|
362
|
+
['\u2028', 'L'],
|
|
363
|
+
['\u2029', 'P'],
|
|
364
|
+
]);
|
|
365
|
+
const DEFAULT_TAG_PREFIXES = new Map([
|
|
366
|
+
['!', '!'],
|
|
367
|
+
['tag:yaml.org,2002:', '!!'],
|
|
368
|
+
]);
|
|
369
|
+
function code(ch) {
|
|
370
|
+
return ch.codePointAt(0) ?? 0;
|
|
371
|
+
}
|
|
372
|
+
function hex(value, width) {
|
|
373
|
+
return value.toString(16).toUpperCase().padStart(width, '0');
|
|
374
|
+
}
|
|
375
|
+
/** A tag character the emitter writes as it is, not percent-escaped. */
|
|
376
|
+
function tagSafe(ch) {
|
|
377
|
+
return /^[0-9A-Za-z]$/.test(ch) || "-;/?:@&=+$,_.~*'()[]".includes(ch);
|
|
378
|
+
}
|
|
379
|
+
class Emitter {
|
|
380
|
+
constructor(stream) {
|
|
381
|
+
this.stream = stream;
|
|
382
|
+
this.states = [];
|
|
383
|
+
this.state = () => this.expectStreamStart();
|
|
384
|
+
this.events = [];
|
|
385
|
+
this.event = null;
|
|
386
|
+
this.indents = [];
|
|
387
|
+
this.indent = null;
|
|
388
|
+
this.flowLevel = 0;
|
|
389
|
+
this.rootContext = false;
|
|
390
|
+
this.sequenceContext = false;
|
|
391
|
+
this.mappingContext = false;
|
|
392
|
+
this.simpleKeyContext = false;
|
|
393
|
+
this.column = 0;
|
|
394
|
+
this.whitespace = true;
|
|
395
|
+
this.indention = true;
|
|
396
|
+
this.openEnded = false;
|
|
397
|
+
this.canonical = false;
|
|
398
|
+
this.allowUnicode = false;
|
|
399
|
+
this.bestIndent = 2;
|
|
400
|
+
this.bestWidth = 80;
|
|
401
|
+
this.bestLineBreak = '\n';
|
|
402
|
+
this.tagPrefixes = new Map();
|
|
403
|
+
this.preparedAnchor = null;
|
|
404
|
+
this.preparedTag = null;
|
|
405
|
+
this.analysis = null;
|
|
406
|
+
this.style = null;
|
|
407
|
+
}
|
|
408
|
+
emit(event) {
|
|
409
|
+
this.events.push(event);
|
|
410
|
+
while (!this.needMoreEvents()) {
|
|
411
|
+
this.event = this.events.shift() ?? null;
|
|
412
|
+
this.state();
|
|
413
|
+
this.event = null;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
current() {
|
|
417
|
+
if (this.event === null)
|
|
418
|
+
throw yamlError('EmitterError', 'no current event');
|
|
419
|
+
return this.event;
|
|
420
|
+
}
|
|
421
|
+
needMoreEvents() {
|
|
422
|
+
const [event] = this.events;
|
|
423
|
+
if (event === undefined)
|
|
424
|
+
return true;
|
|
425
|
+
if (event.type === 'DocumentStart')
|
|
426
|
+
return this.needEvents(1);
|
|
427
|
+
if (event.type === 'SequenceStart')
|
|
428
|
+
return this.needEvents(2);
|
|
429
|
+
if (event.type === 'MappingStart')
|
|
430
|
+
return this.needEvents(3);
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
needEvents(count) {
|
|
434
|
+
let level = 0;
|
|
435
|
+
for (const event of this.events.slice(1)) {
|
|
436
|
+
if (['DocumentStart', 'SequenceStart', 'MappingStart'].includes(event.type))
|
|
437
|
+
level += 1;
|
|
438
|
+
else if (['DocumentEnd', 'SequenceEnd', 'MappingEnd'].includes(event.type))
|
|
439
|
+
level -= 1;
|
|
440
|
+
else if (event.type === 'StreamEnd')
|
|
441
|
+
level = -1;
|
|
442
|
+
if (level < 0)
|
|
443
|
+
return false;
|
|
444
|
+
}
|
|
445
|
+
return this.events.length < count + 1;
|
|
446
|
+
}
|
|
447
|
+
increaseIndent(flow = false, indentless = false) {
|
|
448
|
+
this.indents.push(this.indent);
|
|
449
|
+
if (this.indent === null)
|
|
450
|
+
this.indent = flow ? this.bestIndent : 0;
|
|
451
|
+
else if (!indentless)
|
|
452
|
+
this.indent += this.bestIndent;
|
|
453
|
+
}
|
|
454
|
+
popState() {
|
|
455
|
+
const next = this.states.pop();
|
|
456
|
+
if (next === undefined)
|
|
457
|
+
throw yamlError('EmitterError', 'the state stack is empty');
|
|
458
|
+
this.state = next;
|
|
459
|
+
}
|
|
460
|
+
popIndent() {
|
|
461
|
+
this.indent = this.indents.pop() ?? null;
|
|
462
|
+
}
|
|
463
|
+
// Stream handlers.
|
|
464
|
+
expectStreamStart() {
|
|
465
|
+
if (this.current().type !== 'StreamStart') {
|
|
466
|
+
throw yamlError('EmitterError', 'expected StreamStartEvent');
|
|
467
|
+
}
|
|
468
|
+
// A stream start writes a BOM only for a UTF-16 encoding; there is none.
|
|
469
|
+
this.state = () => this.expectDocumentStart(true);
|
|
470
|
+
}
|
|
471
|
+
expectNothing() {
|
|
472
|
+
throw yamlError('EmitterError', 'expected nothing');
|
|
473
|
+
}
|
|
474
|
+
// Document handlers.
|
|
475
|
+
expectDocumentStart(first = false) {
|
|
476
|
+
const event = this.current();
|
|
477
|
+
if (event.type === 'DocumentStart') {
|
|
478
|
+
// No version and no tag directives: the dump is given neither.
|
|
479
|
+
this.tagPrefixes = new Map(DEFAULT_TAG_PREFIXES);
|
|
480
|
+
const implicit = first && !event.explicit && !this.canonical && !this.checkEmptyDocument();
|
|
481
|
+
if (!implicit) {
|
|
482
|
+
this.writeIndent();
|
|
483
|
+
this.writeIndicator('---', true);
|
|
484
|
+
if (this.canonical)
|
|
485
|
+
this.writeIndent();
|
|
486
|
+
}
|
|
487
|
+
this.state = () => this.expectDocumentRoot();
|
|
488
|
+
}
|
|
489
|
+
else if (event.type === 'StreamEnd') {
|
|
490
|
+
if (this.openEnded) {
|
|
491
|
+
this.writeIndicator('...', true);
|
|
492
|
+
this.writeIndent();
|
|
493
|
+
}
|
|
494
|
+
this.state = () => this.expectNothing();
|
|
495
|
+
}
|
|
496
|
+
else {
|
|
497
|
+
throw yamlError('EmitterError', 'expected DocumentStartEvent');
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
expectDocumentEnd() {
|
|
501
|
+
const event = this.current();
|
|
502
|
+
if (event.type !== 'DocumentEnd') {
|
|
503
|
+
throw yamlError('EmitterError', 'expected DocumentEndEvent');
|
|
504
|
+
}
|
|
505
|
+
this.writeIndent();
|
|
506
|
+
if (event.explicit) {
|
|
507
|
+
this.writeIndicator('...', true);
|
|
508
|
+
this.writeIndent();
|
|
509
|
+
}
|
|
510
|
+
this.state = () => this.expectDocumentStart();
|
|
511
|
+
}
|
|
512
|
+
expectDocumentRoot() {
|
|
513
|
+
this.states.push(() => this.expectDocumentEnd());
|
|
514
|
+
this.expectNode(true);
|
|
515
|
+
}
|
|
516
|
+
// Node handlers.
|
|
517
|
+
expectNode(root = false, sequence = false, mapping = false, simpleKey = false) {
|
|
518
|
+
this.rootContext = root;
|
|
519
|
+
this.sequenceContext = sequence;
|
|
520
|
+
this.mappingContext = mapping;
|
|
521
|
+
this.simpleKeyContext = simpleKey;
|
|
522
|
+
const event = this.current();
|
|
523
|
+
if (event.type === 'Alias') {
|
|
524
|
+
this.expectAlias();
|
|
525
|
+
}
|
|
526
|
+
else if (event.type === 'Scalar' ||
|
|
527
|
+
event.type === 'SequenceStart' ||
|
|
528
|
+
event.type === 'MappingStart') {
|
|
529
|
+
this.processAnchor('&');
|
|
530
|
+
this.processTag();
|
|
531
|
+
if (event.type === 'Scalar') {
|
|
532
|
+
this.expectScalar();
|
|
533
|
+
}
|
|
534
|
+
else if (event.type === 'SequenceStart') {
|
|
535
|
+
if (this.flowLevel || this.canonical || event.flowStyle || this.checkEmptySequence()) {
|
|
536
|
+
this.expectFlowSequence();
|
|
537
|
+
}
|
|
538
|
+
else {
|
|
539
|
+
this.expectBlockSequence();
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
else if (this.flowLevel || this.canonical || event.flowStyle || this.checkEmptyMapping()) {
|
|
543
|
+
this.expectFlowMapping();
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
this.expectBlockMapping();
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
else {
|
|
550
|
+
throw yamlError('EmitterError', 'expected NodeEvent');
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
expectAlias() {
|
|
554
|
+
const event = this.current();
|
|
555
|
+
if (event.type !== 'Alias' || event.anchor === null) {
|
|
556
|
+
throw yamlError('EmitterError', 'anchor is not specified for alias');
|
|
557
|
+
}
|
|
558
|
+
this.processAnchor('*');
|
|
559
|
+
this.popState();
|
|
560
|
+
}
|
|
561
|
+
expectScalar() {
|
|
562
|
+
this.increaseIndent(true);
|
|
563
|
+
this.processScalar();
|
|
564
|
+
this.popIndent();
|
|
565
|
+
this.popState();
|
|
566
|
+
}
|
|
567
|
+
// Flow sequence handlers.
|
|
568
|
+
expectFlowSequence() {
|
|
569
|
+
this.writeIndicator('[', true, true);
|
|
570
|
+
this.flowLevel += 1;
|
|
571
|
+
this.increaseIndent(true);
|
|
572
|
+
this.state = () => this.expectFirstFlowSequenceItem();
|
|
573
|
+
}
|
|
574
|
+
expectFirstFlowSequenceItem() {
|
|
575
|
+
if (this.current().type === 'SequenceEnd') {
|
|
576
|
+
this.popIndent();
|
|
577
|
+
this.flowLevel -= 1;
|
|
578
|
+
this.writeIndicator(']', false);
|
|
579
|
+
this.popState();
|
|
580
|
+
}
|
|
581
|
+
else {
|
|
582
|
+
if (this.canonical || this.column > this.bestWidth)
|
|
583
|
+
this.writeIndent();
|
|
584
|
+
this.states.push(() => this.expectFlowSequenceItem());
|
|
585
|
+
this.expectNode(false, true);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
expectFlowSequenceItem() {
|
|
589
|
+
if (this.current().type === 'SequenceEnd') {
|
|
590
|
+
this.popIndent();
|
|
591
|
+
this.flowLevel -= 1;
|
|
592
|
+
if (this.canonical) {
|
|
593
|
+
this.writeIndicator(',', false);
|
|
594
|
+
this.writeIndent();
|
|
595
|
+
}
|
|
596
|
+
this.writeIndicator(']', false);
|
|
597
|
+
this.popState();
|
|
598
|
+
}
|
|
599
|
+
else {
|
|
600
|
+
this.writeIndicator(',', false);
|
|
601
|
+
if (this.canonical || this.column > this.bestWidth)
|
|
602
|
+
this.writeIndent();
|
|
603
|
+
this.states.push(() => this.expectFlowSequenceItem());
|
|
604
|
+
this.expectNode(false, true);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
// Flow mapping handlers.
|
|
608
|
+
expectFlowMapping() {
|
|
609
|
+
this.writeIndicator('{', true, true);
|
|
610
|
+
this.flowLevel += 1;
|
|
611
|
+
this.increaseIndent(true);
|
|
612
|
+
this.state = () => this.expectFirstFlowMappingKey();
|
|
613
|
+
}
|
|
614
|
+
expectFirstFlowMappingKey() {
|
|
615
|
+
if (this.current().type === 'MappingEnd') {
|
|
616
|
+
this.popIndent();
|
|
617
|
+
this.flowLevel -= 1;
|
|
618
|
+
this.writeIndicator('}', false);
|
|
619
|
+
this.popState();
|
|
620
|
+
}
|
|
621
|
+
else {
|
|
622
|
+
if (this.canonical || this.column > this.bestWidth)
|
|
623
|
+
this.writeIndent();
|
|
624
|
+
this.flowMappingKey();
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
expectFlowMappingKey() {
|
|
628
|
+
if (this.current().type === 'MappingEnd') {
|
|
629
|
+
this.popIndent();
|
|
630
|
+
this.flowLevel -= 1;
|
|
631
|
+
if (this.canonical) {
|
|
632
|
+
this.writeIndicator(',', false);
|
|
633
|
+
this.writeIndent();
|
|
634
|
+
}
|
|
635
|
+
this.writeIndicator('}', false);
|
|
636
|
+
this.popState();
|
|
637
|
+
}
|
|
638
|
+
else {
|
|
639
|
+
this.writeIndicator(',', false);
|
|
640
|
+
if (this.canonical || this.column > this.bestWidth)
|
|
641
|
+
this.writeIndent();
|
|
642
|
+
this.flowMappingKey();
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
/** The shared tail of the two flow-mapping key handlers. */
|
|
646
|
+
flowMappingKey() {
|
|
647
|
+
if (!this.canonical && this.checkSimpleKey()) {
|
|
648
|
+
this.states.push(() => this.expectFlowMappingSimpleValue());
|
|
649
|
+
this.expectNode(false, false, true, true);
|
|
650
|
+
}
|
|
651
|
+
else {
|
|
652
|
+
this.writeIndicator('?', true);
|
|
653
|
+
this.states.push(() => this.expectFlowMappingValue());
|
|
654
|
+
this.expectNode(false, false, true);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
expectFlowMappingSimpleValue() {
|
|
658
|
+
this.writeIndicator(':', false);
|
|
659
|
+
this.states.push(() => this.expectFlowMappingKey());
|
|
660
|
+
this.expectNode(false, false, true);
|
|
661
|
+
}
|
|
662
|
+
expectFlowMappingValue() {
|
|
663
|
+
if (this.canonical || this.column > this.bestWidth)
|
|
664
|
+
this.writeIndent();
|
|
665
|
+
this.writeIndicator(':', true);
|
|
666
|
+
this.states.push(() => this.expectFlowMappingKey());
|
|
667
|
+
this.expectNode(false, false, true);
|
|
668
|
+
}
|
|
669
|
+
// Block sequence handlers.
|
|
670
|
+
expectBlockSequence() {
|
|
671
|
+
const indentless = this.mappingContext && !this.indention;
|
|
672
|
+
this.increaseIndent(false, indentless);
|
|
673
|
+
this.state = () => this.expectBlockSequenceItem(true);
|
|
674
|
+
}
|
|
675
|
+
expectBlockSequenceItem(first = false) {
|
|
676
|
+
if (!first && this.current().type === 'SequenceEnd') {
|
|
677
|
+
this.popIndent();
|
|
678
|
+
this.popState();
|
|
679
|
+
}
|
|
680
|
+
else {
|
|
681
|
+
this.writeIndent();
|
|
682
|
+
this.writeIndicator('-', true, false, true);
|
|
683
|
+
this.states.push(() => this.expectBlockSequenceItem());
|
|
684
|
+
this.expectNode(false, true);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
// Block mapping handlers.
|
|
688
|
+
expectBlockMapping() {
|
|
689
|
+
this.increaseIndent(false);
|
|
690
|
+
this.state = () => this.expectBlockMappingKey(true);
|
|
691
|
+
}
|
|
692
|
+
expectBlockMappingKey(first = false) {
|
|
693
|
+
if (!first && this.current().type === 'MappingEnd') {
|
|
694
|
+
this.popIndent();
|
|
695
|
+
this.popState();
|
|
696
|
+
}
|
|
697
|
+
else {
|
|
698
|
+
this.writeIndent();
|
|
699
|
+
if (this.checkSimpleKey()) {
|
|
700
|
+
this.states.push(() => this.expectBlockMappingSimpleValue());
|
|
701
|
+
this.expectNode(false, false, true, true);
|
|
702
|
+
}
|
|
703
|
+
else {
|
|
704
|
+
this.writeIndicator('?', true, false, true);
|
|
705
|
+
this.states.push(() => this.expectBlockMappingValue());
|
|
706
|
+
this.expectNode(false, false, true);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
expectBlockMappingSimpleValue() {
|
|
711
|
+
this.writeIndicator(':', false);
|
|
712
|
+
this.states.push(() => this.expectBlockMappingKey());
|
|
713
|
+
this.expectNode(false, false, true);
|
|
714
|
+
}
|
|
715
|
+
expectBlockMappingValue() {
|
|
716
|
+
this.writeIndent();
|
|
717
|
+
this.writeIndicator(':', true, false, true);
|
|
718
|
+
this.states.push(() => this.expectBlockMappingKey());
|
|
719
|
+
this.expectNode(false, false, true);
|
|
720
|
+
}
|
|
721
|
+
// Checkers.
|
|
722
|
+
checkEmptySequence() {
|
|
723
|
+
return this.current().type === 'SequenceStart' && this.events[0]?.type === 'SequenceEnd';
|
|
724
|
+
}
|
|
725
|
+
checkEmptyMapping() {
|
|
726
|
+
return this.current().type === 'MappingStart' && this.events[0]?.type === 'MappingEnd';
|
|
727
|
+
}
|
|
728
|
+
checkEmptyDocument() {
|
|
729
|
+
const [next] = this.events;
|
|
730
|
+
if (this.current().type !== 'DocumentStart' || next === undefined)
|
|
731
|
+
return false;
|
|
732
|
+
// The scalar's implicit flag is a pair, which always counts as set, so it is not tested.
|
|
733
|
+
return next.type === 'Scalar' && next.anchor === null && next.tag === null && next.value === '';
|
|
734
|
+
}
|
|
735
|
+
checkSimpleKey() {
|
|
736
|
+
const event = this.current();
|
|
737
|
+
let length = 0;
|
|
738
|
+
if ((event.type === 'Alias' ||
|
|
739
|
+
event.type === 'Scalar' ||
|
|
740
|
+
event.type === 'SequenceStart' ||
|
|
741
|
+
event.type === 'MappingStart') &&
|
|
742
|
+
event.anchor !== null) {
|
|
743
|
+
this.preparedAnchor ?? (this.preparedAnchor = this.prepareAnchor(event.anchor));
|
|
744
|
+
length += Array.from(this.preparedAnchor).length;
|
|
745
|
+
}
|
|
746
|
+
if ((event.type === 'Scalar' ||
|
|
747
|
+
event.type === 'SequenceStart' ||
|
|
748
|
+
event.type === 'MappingStart') &&
|
|
749
|
+
event.tag !== null) {
|
|
750
|
+
this.preparedTag ?? (this.preparedTag = this.prepareTag(event.tag));
|
|
751
|
+
length += Array.from(this.preparedTag).length;
|
|
752
|
+
}
|
|
753
|
+
let analysis = null;
|
|
754
|
+
if (event.type === 'Scalar') {
|
|
755
|
+
this.analysis ?? (this.analysis = this.analyzeScalar(event.value));
|
|
756
|
+
analysis = this.analysis;
|
|
757
|
+
length += Array.from(analysis.scalar).length;
|
|
758
|
+
}
|
|
759
|
+
return (length < 128 &&
|
|
760
|
+
(event.type === 'Alias' ||
|
|
761
|
+
(analysis !== null && !analysis.empty && !analysis.multiline) ||
|
|
762
|
+
this.checkEmptySequence() ||
|
|
763
|
+
this.checkEmptyMapping()));
|
|
764
|
+
}
|
|
765
|
+
// Anchor, Tag, and Scalar processors.
|
|
766
|
+
processAnchor(indicator) {
|
|
767
|
+
const event = this.current();
|
|
768
|
+
const anchor = 'anchor' in event ? event.anchor : null;
|
|
769
|
+
if (anchor === null) {
|
|
770
|
+
this.preparedAnchor = null;
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
773
|
+
this.preparedAnchor ?? (this.preparedAnchor = this.prepareAnchor(anchor));
|
|
774
|
+
if (this.preparedAnchor)
|
|
775
|
+
this.writeIndicator(indicator + this.preparedAnchor, true);
|
|
776
|
+
this.preparedAnchor = null;
|
|
777
|
+
}
|
|
778
|
+
processTag() {
|
|
779
|
+
const event = this.current();
|
|
780
|
+
let tag = 'tag' in event ? event.tag : null;
|
|
781
|
+
if (event.type === 'Scalar') {
|
|
782
|
+
this.style ?? (this.style = this.chooseScalarStyle());
|
|
783
|
+
if ((!this.canonical || tag === null) &&
|
|
784
|
+
((this.style === '' && event.implicit[0]) || (this.style !== '' && event.implicit[1]))) {
|
|
785
|
+
this.preparedTag = null;
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
if (event.implicit[0] && tag === null) {
|
|
789
|
+
tag = '!';
|
|
790
|
+
this.preparedTag = null;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
else if (event.type === 'SequenceStart' || event.type === 'MappingStart') {
|
|
794
|
+
if ((!this.canonical || tag === null) && event.implicit) {
|
|
795
|
+
this.preparedTag = null;
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
if (tag === null)
|
|
800
|
+
throw yamlError('EmitterError', 'tag is not specified');
|
|
801
|
+
this.preparedTag ?? (this.preparedTag = this.prepareTag(tag));
|
|
802
|
+
if (this.preparedTag)
|
|
803
|
+
this.writeIndicator(this.preparedTag, true);
|
|
804
|
+
this.preparedTag = null;
|
|
805
|
+
}
|
|
806
|
+
chooseScalarStyle() {
|
|
807
|
+
const event = this.current();
|
|
808
|
+
if (event.type !== 'Scalar')
|
|
809
|
+
throw yamlError('EmitterError', 'expected ScalarEvent');
|
|
810
|
+
this.analysis ?? (this.analysis = this.analyzeScalar(event.value));
|
|
811
|
+
const analysis = this.analysis;
|
|
812
|
+
if (event.style === '"' || this.canonical)
|
|
813
|
+
return '"';
|
|
814
|
+
if (!event.style && event.implicit[0]) {
|
|
815
|
+
if (!(this.simpleKeyContext && (analysis.empty || analysis.multiline)) &&
|
|
816
|
+
((this.flowLevel && analysis.allowFlowPlain) ||
|
|
817
|
+
(!this.flowLevel && analysis.allowBlockPlain))) {
|
|
818
|
+
return '';
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
if (event.style && '|>'.includes(event.style)) {
|
|
822
|
+
if (!this.flowLevel && !this.simpleKeyContext && analysis.allowBlock)
|
|
823
|
+
return event.style;
|
|
824
|
+
}
|
|
825
|
+
if (!event.style || event.style === "'") {
|
|
826
|
+
if (analysis.allowSingleQuoted && !(this.simpleKeyContext && analysis.multiline)) {
|
|
827
|
+
return "'";
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
return '"';
|
|
831
|
+
}
|
|
832
|
+
processScalar() {
|
|
833
|
+
const event = this.current();
|
|
834
|
+
if (event.type !== 'Scalar')
|
|
835
|
+
throw yamlError('EmitterError', 'expected ScalarEvent');
|
|
836
|
+
this.analysis ?? (this.analysis = this.analyzeScalar(event.value));
|
|
837
|
+
this.style ?? (this.style = this.chooseScalarStyle());
|
|
838
|
+
const split = !this.simpleKeyContext;
|
|
839
|
+
const text = this.analysis.scalar;
|
|
840
|
+
if (this.style === '"')
|
|
841
|
+
this.writeDoubleQuoted(text, split);
|
|
842
|
+
else if (this.style === "'")
|
|
843
|
+
this.writeSingleQuoted(text, split);
|
|
844
|
+
else if (this.style === '>')
|
|
845
|
+
this.writeFolded(text);
|
|
846
|
+
else if (this.style === '|')
|
|
847
|
+
this.writeLiteral(text);
|
|
848
|
+
else
|
|
849
|
+
this.writePlain(text, split);
|
|
850
|
+
this.analysis = null;
|
|
851
|
+
this.style = null;
|
|
852
|
+
}
|
|
853
|
+
// Analyzers.
|
|
854
|
+
prepareTag(tag) {
|
|
855
|
+
if (!tag)
|
|
856
|
+
throw yamlError('EmitterError', 'tag must not be empty');
|
|
857
|
+
if (tag === '!')
|
|
858
|
+
return tag;
|
|
859
|
+
let handle = null;
|
|
860
|
+
let suffix = tag;
|
|
861
|
+
const prefixes = [...this.tagPrefixes.keys()].sort();
|
|
862
|
+
for (const prefix of prefixes) {
|
|
863
|
+
if (tag.startsWith(prefix) && (prefix === '!' || prefix.length < tag.length)) {
|
|
864
|
+
handle = this.tagPrefixes.get(prefix) ?? null;
|
|
865
|
+
suffix = tag.slice(prefix.length);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
let text = '';
|
|
869
|
+
for (const ch of suffix) {
|
|
870
|
+
if (tagSafe(ch) || (ch === '!' && handle !== '!')) {
|
|
871
|
+
text += ch;
|
|
872
|
+
}
|
|
873
|
+
else {
|
|
874
|
+
for (const byte of Buffer.from(ch, 'utf8'))
|
|
875
|
+
text += `%${hex(byte, 2)}`;
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
return handle ? `${handle}${text}` : `!<${text}>`;
|
|
879
|
+
}
|
|
880
|
+
prepareAnchor(anchor) {
|
|
881
|
+
if (!anchor)
|
|
882
|
+
throw yamlError('EmitterError', 'anchor must not be empty');
|
|
883
|
+
for (const ch of anchor) {
|
|
884
|
+
if (!/^[0-9A-Za-z_-]$/.test(ch)) {
|
|
885
|
+
throw yamlError('EmitterError', `invalid character in the anchor: ${anchor}`);
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
return anchor;
|
|
889
|
+
}
|
|
890
|
+
analyzeScalar(scalar) {
|
|
891
|
+
// Empty scalar is a special case.
|
|
892
|
+
if (!scalar) {
|
|
893
|
+
return {
|
|
894
|
+
scalar,
|
|
895
|
+
empty: true,
|
|
896
|
+
multiline: false,
|
|
897
|
+
allowFlowPlain: false,
|
|
898
|
+
allowBlockPlain: true,
|
|
899
|
+
allowSingleQuoted: true,
|
|
900
|
+
allowDoubleQuoted: true,
|
|
901
|
+
allowBlock: false,
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
const text = Array.from(scalar);
|
|
905
|
+
const length = text.length;
|
|
906
|
+
// Indicators and special characters.
|
|
907
|
+
let blockIndicators = false;
|
|
908
|
+
let flowIndicators = false;
|
|
909
|
+
let lineBreaks = false;
|
|
910
|
+
let specialCharacters = false;
|
|
911
|
+
// Important whitespace combinations.
|
|
912
|
+
let leadingSpace = false;
|
|
913
|
+
let leadingBreak = false;
|
|
914
|
+
let trailingSpace = false;
|
|
915
|
+
let trailingBreak = false;
|
|
916
|
+
let breakSpace = false;
|
|
917
|
+
let spaceBreak = false;
|
|
918
|
+
// Check document indicators.
|
|
919
|
+
if (scalar.startsWith('---') || scalar.startsWith('...')) {
|
|
920
|
+
blockIndicators = true;
|
|
921
|
+
flowIndicators = true;
|
|
922
|
+
}
|
|
923
|
+
// First character or preceded by a whitespace.
|
|
924
|
+
let precededByWhitespace = true;
|
|
925
|
+
// Last character or followed by a whitespace.
|
|
926
|
+
let followedByWhitespace = length === 1 || WHITESPACE.includes(text[1]);
|
|
927
|
+
// The previous character is a space.
|
|
928
|
+
let previousSpace = false;
|
|
929
|
+
// The previous character is a break.
|
|
930
|
+
let previousBreak = false;
|
|
931
|
+
for (let index = 0; index < length;) {
|
|
932
|
+
const ch = text[index];
|
|
933
|
+
const point = code(ch);
|
|
934
|
+
// Check for indicators.
|
|
935
|
+
if (index === 0) {
|
|
936
|
+
// Leading indicators are special characters.
|
|
937
|
+
if ('#,[]{}&*!|>\'"%@`'.includes(ch)) {
|
|
938
|
+
flowIndicators = true;
|
|
939
|
+
blockIndicators = true;
|
|
940
|
+
}
|
|
941
|
+
if ('?:'.includes(ch)) {
|
|
942
|
+
flowIndicators = true;
|
|
943
|
+
if (followedByWhitespace)
|
|
944
|
+
blockIndicators = true;
|
|
945
|
+
}
|
|
946
|
+
if (ch === '-' && followedByWhitespace) {
|
|
947
|
+
flowIndicators = true;
|
|
948
|
+
blockIndicators = true;
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
else {
|
|
952
|
+
// Some indicators cannot appear within a scalar as well.
|
|
953
|
+
if (',?[]{}'.includes(ch))
|
|
954
|
+
flowIndicators = true;
|
|
955
|
+
if (ch === ':') {
|
|
956
|
+
flowIndicators = true;
|
|
957
|
+
if (followedByWhitespace)
|
|
958
|
+
blockIndicators = true;
|
|
959
|
+
}
|
|
960
|
+
if (ch === '#' && precededByWhitespace) {
|
|
961
|
+
flowIndicators = true;
|
|
962
|
+
blockIndicators = true;
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
// Check for line breaks, special, and unicode characters.
|
|
966
|
+
if (BREAKS.includes(ch))
|
|
967
|
+
lineBreaks = true;
|
|
968
|
+
if (!(ch === '\n' || (point >= 0x20 && point <= 0x7e))) {
|
|
969
|
+
const unicode = (point === 0x85 ||
|
|
970
|
+
(point >= 0xa0 && point <= 0xd7ff) ||
|
|
971
|
+
(point >= 0xe000 && point <= 0xfffd) ||
|
|
972
|
+
(point >= 0x10000 && point < 0x10ffff)) &&
|
|
973
|
+
point !== 0xfeff;
|
|
974
|
+
// With non-ASCII escaped, a printable non-ASCII character is as special as a
|
|
975
|
+
// control character.
|
|
976
|
+
if (!unicode || !this.allowUnicode)
|
|
977
|
+
specialCharacters = true;
|
|
978
|
+
}
|
|
979
|
+
// Detect important whitespace combinations.
|
|
980
|
+
if (ch === ' ') {
|
|
981
|
+
if (index === 0)
|
|
982
|
+
leadingSpace = true;
|
|
983
|
+
if (index === length - 1)
|
|
984
|
+
trailingSpace = true;
|
|
985
|
+
if (previousBreak)
|
|
986
|
+
breakSpace = true;
|
|
987
|
+
previousSpace = true;
|
|
988
|
+
previousBreak = false;
|
|
989
|
+
}
|
|
990
|
+
else if (BREAKS.includes(ch)) {
|
|
991
|
+
if (index === 0)
|
|
992
|
+
leadingBreak = true;
|
|
993
|
+
if (index === length - 1)
|
|
994
|
+
trailingBreak = true;
|
|
995
|
+
if (previousSpace)
|
|
996
|
+
spaceBreak = true;
|
|
997
|
+
previousSpace = false;
|
|
998
|
+
previousBreak = true;
|
|
999
|
+
}
|
|
1000
|
+
else {
|
|
1001
|
+
previousSpace = false;
|
|
1002
|
+
previousBreak = false;
|
|
1003
|
+
}
|
|
1004
|
+
// Prepare for the next character.
|
|
1005
|
+
index += 1;
|
|
1006
|
+
precededByWhitespace = WHITESPACE.includes(ch);
|
|
1007
|
+
followedByWhitespace = index + 1 >= length || WHITESPACE.includes(text[index + 1]);
|
|
1008
|
+
}
|
|
1009
|
+
// Let's decide what styles are allowed.
|
|
1010
|
+
let allowFlowPlain = true;
|
|
1011
|
+
let allowBlockPlain = true;
|
|
1012
|
+
let allowSingleQuoted = true;
|
|
1013
|
+
const allowDoubleQuoted = true;
|
|
1014
|
+
let allowBlock = true;
|
|
1015
|
+
// Leading and trailing whitespaces are bad for plain scalars.
|
|
1016
|
+
if (leadingSpace || leadingBreak || trailingSpace || trailingBreak) {
|
|
1017
|
+
allowFlowPlain = allowBlockPlain = false;
|
|
1018
|
+
}
|
|
1019
|
+
// We do not permit trailing spaces for block scalars.
|
|
1020
|
+
if (trailingSpace)
|
|
1021
|
+
allowBlock = false;
|
|
1022
|
+
// Spaces at the beginning of a new line are only acceptable for block scalars.
|
|
1023
|
+
if (breakSpace)
|
|
1024
|
+
allowFlowPlain = allowBlockPlain = allowSingleQuoted = false;
|
|
1025
|
+
// Spaces followed by breaks, as well as special character are only allowed for double
|
|
1026
|
+
// quoted scalars.
|
|
1027
|
+
if (spaceBreak || specialCharacters) {
|
|
1028
|
+
allowFlowPlain = allowBlockPlain = allowSingleQuoted = allowBlock = false;
|
|
1029
|
+
}
|
|
1030
|
+
// Although the plain scalar writer supports breaks, we never emit multiline plain scalars.
|
|
1031
|
+
if (lineBreaks)
|
|
1032
|
+
allowFlowPlain = allowBlockPlain = false;
|
|
1033
|
+
// Flow indicators are forbidden for flow plain scalars.
|
|
1034
|
+
if (flowIndicators)
|
|
1035
|
+
allowFlowPlain = false;
|
|
1036
|
+
// Block indicators are forbidden for block plain scalars.
|
|
1037
|
+
if (blockIndicators)
|
|
1038
|
+
allowBlockPlain = false;
|
|
1039
|
+
return {
|
|
1040
|
+
scalar,
|
|
1041
|
+
empty: false,
|
|
1042
|
+
multiline: lineBreaks,
|
|
1043
|
+
allowFlowPlain,
|
|
1044
|
+
allowBlockPlain,
|
|
1045
|
+
allowSingleQuoted,
|
|
1046
|
+
allowDoubleQuoted,
|
|
1047
|
+
allowBlock,
|
|
1048
|
+
};
|
|
1049
|
+
}
|
|
1050
|
+
// Writers.
|
|
1051
|
+
write(data) {
|
|
1052
|
+
this.stream.push(data);
|
|
1053
|
+
}
|
|
1054
|
+
writeIndicator(indicator, needWhitespace, whitespace = false, indention = false) {
|
|
1055
|
+
const data = this.whitespace || !needWhitespace ? indicator : ` ${indicator}`;
|
|
1056
|
+
this.whitespace = whitespace;
|
|
1057
|
+
this.indention = this.indention && indention;
|
|
1058
|
+
this.column += Array.from(data).length;
|
|
1059
|
+
this.openEnded = false;
|
|
1060
|
+
this.write(data);
|
|
1061
|
+
}
|
|
1062
|
+
writeIndent() {
|
|
1063
|
+
const indent = this.indent ?? 0;
|
|
1064
|
+
if (!this.indention || this.column > indent || (this.column === indent && !this.whitespace)) {
|
|
1065
|
+
this.writeLineBreak();
|
|
1066
|
+
}
|
|
1067
|
+
if (this.column < indent) {
|
|
1068
|
+
this.whitespace = true;
|
|
1069
|
+
this.write(' '.repeat(indent - this.column));
|
|
1070
|
+
this.column = indent;
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
writeLineBreak(data = null) {
|
|
1074
|
+
this.whitespace = true;
|
|
1075
|
+
this.indention = true;
|
|
1076
|
+
this.column = 0;
|
|
1077
|
+
this.write(data ?? this.bestLineBreak);
|
|
1078
|
+
}
|
|
1079
|
+
/** The run from `start` to `end` written and counted, the step every writer repeats. */
|
|
1080
|
+
writeRun(text, start, end) {
|
|
1081
|
+
const data = text.slice(start, end).join('');
|
|
1082
|
+
this.column += end - start;
|
|
1083
|
+
this.write(data);
|
|
1084
|
+
}
|
|
1085
|
+
/** The line breaks from `start` to `end`, each written as itself or as the default break. */
|
|
1086
|
+
writeBreaks(text, start, end) {
|
|
1087
|
+
for (const br of text.slice(start, end))
|
|
1088
|
+
this.writeLineBreak(br === '\n' ? null : br);
|
|
1089
|
+
}
|
|
1090
|
+
// Scalar streams.
|
|
1091
|
+
writeSingleQuoted(scalar, split = true) {
|
|
1092
|
+
const text = Array.from(scalar);
|
|
1093
|
+
this.writeIndicator("'", true);
|
|
1094
|
+
let spaces = false;
|
|
1095
|
+
let breaks = false;
|
|
1096
|
+
let start = 0;
|
|
1097
|
+
let end = 0;
|
|
1098
|
+
while (end <= text.length) {
|
|
1099
|
+
const ch = end < text.length ? text[end] : null;
|
|
1100
|
+
if (spaces) {
|
|
1101
|
+
if (ch === null || ch !== ' ') {
|
|
1102
|
+
if (start + 1 === end &&
|
|
1103
|
+
this.column > this.bestWidth &&
|
|
1104
|
+
split &&
|
|
1105
|
+
start !== 0 &&
|
|
1106
|
+
end !== text.length) {
|
|
1107
|
+
this.writeIndent();
|
|
1108
|
+
}
|
|
1109
|
+
else {
|
|
1110
|
+
this.writeRun(text, start, end);
|
|
1111
|
+
}
|
|
1112
|
+
start = end;
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
else if (breaks) {
|
|
1116
|
+
if (ch === null || !BREAKS.includes(ch)) {
|
|
1117
|
+
if (text[start] === '\n')
|
|
1118
|
+
this.writeLineBreak();
|
|
1119
|
+
this.writeBreaks(text, start, end);
|
|
1120
|
+
this.writeIndent();
|
|
1121
|
+
start = end;
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
else if (ch === null || ` ${BREAKS}`.includes(ch) || ch === "'") {
|
|
1125
|
+
if (start < end) {
|
|
1126
|
+
this.writeRun(text, start, end);
|
|
1127
|
+
start = end;
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
if (ch === "'") {
|
|
1131
|
+
this.column += 2;
|
|
1132
|
+
this.write("''");
|
|
1133
|
+
start = end + 1;
|
|
1134
|
+
}
|
|
1135
|
+
if (ch !== null) {
|
|
1136
|
+
spaces = ch === ' ';
|
|
1137
|
+
breaks = BREAKS.includes(ch);
|
|
1138
|
+
}
|
|
1139
|
+
end += 1;
|
|
1140
|
+
}
|
|
1141
|
+
this.writeIndicator("'", false);
|
|
1142
|
+
}
|
|
1143
|
+
writeDoubleQuoted(scalar, split = true) {
|
|
1144
|
+
const text = Array.from(scalar);
|
|
1145
|
+
this.writeIndicator('"', true);
|
|
1146
|
+
let start = 0;
|
|
1147
|
+
let end = 0;
|
|
1148
|
+
while (end <= text.length) {
|
|
1149
|
+
const ch = end < text.length ? text[end] : null;
|
|
1150
|
+
const point = ch === null ? 0 : code(ch);
|
|
1151
|
+
if (ch === null ||
|
|
1152
|
+
'"\\\u0085\u2028\u2029\ufeff'.includes(ch) ||
|
|
1153
|
+
!((point >= 0x20 && point <= 0x7e) ||
|
|
1154
|
+
(this.allowUnicode &&
|
|
1155
|
+
((point >= 0xa0 && point <= 0xd7ff) || (point >= 0xe000 && point <= 0xfffd))))) {
|
|
1156
|
+
if (start < end) {
|
|
1157
|
+
this.writeRun(text, start, end);
|
|
1158
|
+
start = end;
|
|
1159
|
+
}
|
|
1160
|
+
if (ch !== null) {
|
|
1161
|
+
const replacement = ESCAPE_REPLACEMENTS.get(ch);
|
|
1162
|
+
let data;
|
|
1163
|
+
if (replacement !== undefined)
|
|
1164
|
+
data = `\\${replacement}`;
|
|
1165
|
+
else if (point <= 0xff)
|
|
1166
|
+
data = `\\x${hex(point, 2)}`;
|
|
1167
|
+
else if (point <= 0xffff)
|
|
1168
|
+
data = `\\u${hex(point, 4)}`;
|
|
1169
|
+
else
|
|
1170
|
+
data = `\\U${hex(point, 8)}`;
|
|
1171
|
+
this.column += data.length;
|
|
1172
|
+
this.write(data);
|
|
1173
|
+
start = end + 1;
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
if (end > 0 &&
|
|
1177
|
+
end < text.length - 1 &&
|
|
1178
|
+
(ch === ' ' || start >= end) &&
|
|
1179
|
+
this.column + (end - start) > this.bestWidth &&
|
|
1180
|
+
split) {
|
|
1181
|
+
const data = `${text.slice(start, end).join('')}\\`;
|
|
1182
|
+
const counted = end - start + 1;
|
|
1183
|
+
if (start < end)
|
|
1184
|
+
start = end;
|
|
1185
|
+
this.column += counted;
|
|
1186
|
+
this.write(data);
|
|
1187
|
+
this.writeIndent();
|
|
1188
|
+
this.whitespace = false;
|
|
1189
|
+
this.indention = false;
|
|
1190
|
+
if (text[start] === ' ') {
|
|
1191
|
+
this.column += 1;
|
|
1192
|
+
this.write('\\');
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
end += 1;
|
|
1196
|
+
}
|
|
1197
|
+
this.writeIndicator('"', false);
|
|
1198
|
+
}
|
|
1199
|
+
determineBlockHints(text) {
|
|
1200
|
+
let hints = '';
|
|
1201
|
+
if (text.length > 0) {
|
|
1202
|
+
if (` ${BREAKS}`.includes(text[0]))
|
|
1203
|
+
hints += String(this.bestIndent);
|
|
1204
|
+
if (!BREAKS.includes(text[text.length - 1]))
|
|
1205
|
+
hints += '-';
|
|
1206
|
+
else if (text.length === 1 || BREAKS.includes(text[text.length - 2]))
|
|
1207
|
+
hints += '+';
|
|
1208
|
+
}
|
|
1209
|
+
return hints;
|
|
1210
|
+
}
|
|
1211
|
+
writeFolded(scalar) {
|
|
1212
|
+
const text = Array.from(scalar);
|
|
1213
|
+
const hints = this.determineBlockHints(text);
|
|
1214
|
+
this.writeIndicator(`>${hints}`, true);
|
|
1215
|
+
if (hints.endsWith('+'))
|
|
1216
|
+
this.openEnded = true;
|
|
1217
|
+
this.writeLineBreak();
|
|
1218
|
+
let leadingSpace = true;
|
|
1219
|
+
let spaces = false;
|
|
1220
|
+
let breaks = true;
|
|
1221
|
+
let start = 0;
|
|
1222
|
+
let end = 0;
|
|
1223
|
+
while (end <= text.length) {
|
|
1224
|
+
const ch = end < text.length ? text[end] : null;
|
|
1225
|
+
if (breaks) {
|
|
1226
|
+
if (ch === null || !BREAKS.includes(ch)) {
|
|
1227
|
+
if (!leadingSpace && ch !== null && ch !== ' ' && text[start] === '\n') {
|
|
1228
|
+
this.writeLineBreak();
|
|
1229
|
+
}
|
|
1230
|
+
leadingSpace = ch === ' ';
|
|
1231
|
+
this.writeBreaks(text, start, end);
|
|
1232
|
+
if (ch !== null)
|
|
1233
|
+
this.writeIndent();
|
|
1234
|
+
start = end;
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
else if (spaces) {
|
|
1238
|
+
if (ch !== ' ') {
|
|
1239
|
+
if (start + 1 === end && this.column > this.bestWidth)
|
|
1240
|
+
this.writeIndent();
|
|
1241
|
+
else
|
|
1242
|
+
this.writeRun(text, start, end);
|
|
1243
|
+
start = end;
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
else if (ch === null || ` ${BREAKS}`.includes(ch)) {
|
|
1247
|
+
this.writeRun(text, start, end);
|
|
1248
|
+
if (ch === null)
|
|
1249
|
+
this.writeLineBreak();
|
|
1250
|
+
start = end;
|
|
1251
|
+
}
|
|
1252
|
+
if (ch !== null) {
|
|
1253
|
+
breaks = BREAKS.includes(ch);
|
|
1254
|
+
spaces = ch === ' ';
|
|
1255
|
+
}
|
|
1256
|
+
end += 1;
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
writeLiteral(scalar) {
|
|
1260
|
+
const text = Array.from(scalar);
|
|
1261
|
+
const hints = this.determineBlockHints(text);
|
|
1262
|
+
this.writeIndicator(`|${hints}`, true);
|
|
1263
|
+
if (hints.endsWith('+'))
|
|
1264
|
+
this.openEnded = true;
|
|
1265
|
+
this.writeLineBreak();
|
|
1266
|
+
let breaks = true;
|
|
1267
|
+
let start = 0;
|
|
1268
|
+
let end = 0;
|
|
1269
|
+
while (end <= text.length) {
|
|
1270
|
+
const ch = end < text.length ? text[end] : null;
|
|
1271
|
+
if (breaks) {
|
|
1272
|
+
if (ch === null || !BREAKS.includes(ch)) {
|
|
1273
|
+
this.writeBreaks(text, start, end);
|
|
1274
|
+
if (ch !== null)
|
|
1275
|
+
this.writeIndent();
|
|
1276
|
+
start = end;
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
else if (ch === null || BREAKS.includes(ch)) {
|
|
1280
|
+
// A literal scalar's run is written without counting it: the line break after it
|
|
1281
|
+
// resets the column.
|
|
1282
|
+
this.write(text.slice(start, end).join(''));
|
|
1283
|
+
if (ch === null)
|
|
1284
|
+
this.writeLineBreak();
|
|
1285
|
+
start = end;
|
|
1286
|
+
}
|
|
1287
|
+
if (ch !== null)
|
|
1288
|
+
breaks = BREAKS.includes(ch);
|
|
1289
|
+
end += 1;
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
writePlain(scalar, split = true) {
|
|
1293
|
+
if (this.rootContext)
|
|
1294
|
+
this.openEnded = true;
|
|
1295
|
+
if (!scalar)
|
|
1296
|
+
return;
|
|
1297
|
+
const text = Array.from(scalar);
|
|
1298
|
+
if (!this.whitespace) {
|
|
1299
|
+
this.column += 1;
|
|
1300
|
+
this.write(' ');
|
|
1301
|
+
}
|
|
1302
|
+
this.whitespace = false;
|
|
1303
|
+
this.indention = false;
|
|
1304
|
+
let spaces = false;
|
|
1305
|
+
let breaks = false;
|
|
1306
|
+
let start = 0;
|
|
1307
|
+
let end = 0;
|
|
1308
|
+
while (end <= text.length) {
|
|
1309
|
+
const ch = end < text.length ? text[end] : null;
|
|
1310
|
+
if (spaces) {
|
|
1311
|
+
if (ch !== ' ') {
|
|
1312
|
+
if (start + 1 === end && this.column > this.bestWidth && split) {
|
|
1313
|
+
this.writeIndent();
|
|
1314
|
+
this.whitespace = false;
|
|
1315
|
+
this.indention = false;
|
|
1316
|
+
}
|
|
1317
|
+
else {
|
|
1318
|
+
this.writeRun(text, start, end);
|
|
1319
|
+
}
|
|
1320
|
+
start = end;
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
else if (breaks) {
|
|
1324
|
+
if (ch === null || !BREAKS.includes(ch)) {
|
|
1325
|
+
if (text[start] === '\n')
|
|
1326
|
+
this.writeLineBreak();
|
|
1327
|
+
this.writeBreaks(text, start, end);
|
|
1328
|
+
this.writeIndent();
|
|
1329
|
+
this.whitespace = false;
|
|
1330
|
+
this.indention = false;
|
|
1331
|
+
start = end;
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
else if (ch === null || ` ${BREAKS}`.includes(ch)) {
|
|
1335
|
+
this.writeRun(text, start, end);
|
|
1336
|
+
start = end;
|
|
1337
|
+
}
|
|
1338
|
+
if (ch !== null) {
|
|
1339
|
+
spaces = ch === ' ';
|
|
1340
|
+
breaks = BREAKS.includes(ch);
|
|
1341
|
+
}
|
|
1342
|
+
end += 1;
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
/**
|
|
1347
|
+
* `data` as one YAML document, in block style with its keys in their own order; given
|
|
1348
|
+
* `implicitResolvers`, with those consulted after the resolver's own.
|
|
1349
|
+
*/
|
|
1350
|
+
function safeDump(data, options = {}) {
|
|
1351
|
+
const stream = [];
|
|
1352
|
+
const emitter = new Emitter(stream);
|
|
1353
|
+
emitter.emit({ type: 'StreamStart' });
|
|
1354
|
+
const node = new Representer(options).representData(data);
|
|
1355
|
+
const extra = options.implicitResolvers ?? [];
|
|
1356
|
+
new Serializer((event) => {
|
|
1357
|
+
emitter.emit(event);
|
|
1358
|
+
}, extra.length === 0 ? RESOLVERS : resolverTable(extra)).serialize(node);
|
|
1359
|
+
emitter.emit({ type: 'StreamEnd' });
|
|
1360
|
+
return stream.join('');
|
|
1361
|
+
}
|