@rocket.chat/houston 0.27.0-next.3 → 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 -194
- package/dist/outputCI.js +0 -59
- package/src/bin.js +0 -4
- package/src/cli.ts +0 -25
- package/src/commands/createBranch.ts +0 -28
- package/src/outputCI.ts +0 -12
- 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,1383 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.BadGzip = exports.TarReadError = exports.UnsafeBundleEntry = void 0;
|
|
37
|
+
exports.requireSafeMember = requireSafeMember;
|
|
38
|
+
exports.readMember = readMember;
|
|
39
|
+
exports.memberNames = memberNames;
|
|
40
|
+
exports.extractTarGz = extractTarGz;
|
|
41
|
+
exports.writeTarGzFromDir = writeTarGzFromDir;
|
|
42
|
+
exports.gzipFailure = gzipFailure;
|
|
43
|
+
/**
|
|
44
|
+
* The small part of tar a backup bundle needs: read a `.tar.gz` member, extract one safely,
|
|
45
|
+
* and write one.
|
|
46
|
+
*
|
|
47
|
+
* WHY NOT A DEPENDENCY. The bundle format is a gzipped tar because earlier releases wrote
|
|
48
|
+
* one, and this build reads and writes the same files. `tar` on PATH is not portable enough
|
|
49
|
+
* to be the contract (GNU and BSD differ on flags and on what a member name may contain),
|
|
50
|
+
* and a third-party tar package would be the only dependency in a tree that ships exactly
|
|
51
|
+
* one. So this is a header parser, and it is deliberately NARROW: ustar/GNU/PAX entries,
|
|
52
|
+
* regular files and directories, which is everything a bundle written from a staging
|
|
53
|
+
* directory houston built itself holds.
|
|
54
|
+
*
|
|
55
|
+
* UNSAFE ENTRIES ARE REFUSED, exactly as they always have been: a bundle can arrive from a
|
|
56
|
+
* colleague, so an absolute path, a `..`, a symlink, a hard link or a device node is
|
|
57
|
+
* untrusted input rather than a curiosity.
|
|
58
|
+
*
|
|
59
|
+
* THE GZIP LAYER IS A LAZY READER, not `zlib.createGunzip()`. The gunzip stream checked the
|
|
60
|
+
* CRC trailer of the whole file, reading ahead of the tar walker to reach it, where the
|
|
61
|
+
* established reader stops at the end-of-archive block and never reads the trailer: a
|
|
62
|
+
* bundle whose trailer alone was damaged restored in earlier releases and was refused here,
|
|
63
|
+
* and a bit-flipped manifest was a "zlib error: ... incorrect data check" where the
|
|
64
|
+
* established report is the malformed JSON it decompressed. `GzipReader` parses the header
|
|
65
|
+
* itself, inflates the raw deflate data only as far as the walker asks, and checks a trailer
|
|
66
|
+
* only when the walker reads past the end of the deflate data.
|
|
67
|
+
*/
|
|
68
|
+
const fs = __importStar(require("node:fs"));
|
|
69
|
+
const os = __importStar(require("node:os"));
|
|
70
|
+
const path = __importStar(require("node:path"));
|
|
71
|
+
const zlib = __importStar(require("node:zlib"));
|
|
72
|
+
const exec_1 = require("../seams/exec");
|
|
73
|
+
const errors_1 = require("./errors");
|
|
74
|
+
const format_1 = require("./format");
|
|
75
|
+
const float_text_1 = require("./float-text");
|
|
76
|
+
const BLOCK = 512;
|
|
77
|
+
/**
|
|
78
|
+
* The unsafe-entry refusal, as a `ReproError`.
|
|
79
|
+
*
|
|
80
|
+
* It EXTENDS `ValidationError` rather than `Error`: the refusal is a `ValidationError`
|
|
81
|
+
* raised straight out of reading the manifest and unpacking the archive, so a caller that
|
|
82
|
+
* only knows the error contract must be able to map it to a status. As a plain `Error` it
|
|
83
|
+
* escaped the `http_status` taxonomy for any caller that did not special-case it. The name
|
|
84
|
+
* is quoted by `quotedText`, so a name containing a quote, a backslash or a control character
|
|
85
|
+
* reads exactly as the established message writes it.
|
|
86
|
+
*/
|
|
87
|
+
class UnsafeBundleEntry extends errors_1.ValidationError {
|
|
88
|
+
constructor(entryName) {
|
|
89
|
+
super(`bundle contains an unsafe entry: ${(0, format_1.quotedText)(entryName)}`);
|
|
90
|
+
this.entryName = entryName;
|
|
91
|
+
this.name = 'UnsafeBundleEntry';
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.UnsafeBundleEntry = UnsafeBundleEntry;
|
|
95
|
+
/**
|
|
96
|
+
* The archive reader's refusal, with its own words for a tar stream that ends early:
|
|
97
|
+
* "empty file" and "truncated header" at the first header, "unexpected end of data" inside
|
|
98
|
+
* a member. The walker said "archive ended inside a member" for all three, and that
|
|
99
|
+
* sentence went into the UNREADABLE line of `backups` and the refusal of `restore`.
|
|
100
|
+
*/
|
|
101
|
+
class TarReadError extends Error {
|
|
102
|
+
constructor(message) {
|
|
103
|
+
super(message);
|
|
104
|
+
this.name = 'UnreadableBundle';
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
exports.TarReadError = TarReadError;
|
|
108
|
+
/**
|
|
109
|
+
* A bad gzip member, which counts as an OS-level error: a member header that is not gzip, or
|
|
110
|
+
* a trailer whose CRC or length disagrees with what was decompressed. It carries a `code` so
|
|
111
|
+
* a caller that sorts failures by "has an errno-like code" words it as the OS error it is.
|
|
112
|
+
*/
|
|
113
|
+
class BadGzip extends Error {
|
|
114
|
+
constructor(message) {
|
|
115
|
+
super(message);
|
|
116
|
+
this.code = 'BadGzip';
|
|
117
|
+
this.name = 'BadGzip';
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.BadGzip = BadGzip;
|
|
121
|
+
/** The error for a file that ended inside a member. `gzipFailure` names it truncation. */
|
|
122
|
+
function endedEarly() {
|
|
123
|
+
const error = new Error('Compressed file ended before the end-of-stream marker was reached');
|
|
124
|
+
error.name = 'TruncatedData';
|
|
125
|
+
error.code = 'Z_BUF_ERROR';
|
|
126
|
+
return error;
|
|
127
|
+
}
|
|
128
|
+
/** The bytes a file starts with, in hex, for the refusal of one that is not gzip. */
|
|
129
|
+
function leadingBytes(data) {
|
|
130
|
+
return [...data].map((byte) => `0x${byte.toString(16).padStart(2, '0')}`).join(' ');
|
|
131
|
+
}
|
|
132
|
+
/** The header flags `readHeader` acts on. */
|
|
133
|
+
const FHCRC = 2;
|
|
134
|
+
const FEXTRA = 4;
|
|
135
|
+
const FNAME = 8;
|
|
136
|
+
const FCOMMENT = 16;
|
|
137
|
+
/** How much of the compressed file is handed to the inflater at a time. */
|
|
138
|
+
const READ_SIZE = 64 * 1024;
|
|
139
|
+
/**
|
|
140
|
+
* A gzip reader over a file: `next()` is its read, one decompressed chunk at a time, and
|
|
141
|
+
* null at the end of the last member.
|
|
142
|
+
*
|
|
143
|
+
* LAZY, as the established reader is. The inflater is paused after every chunk it produces
|
|
144
|
+
* and resumed only when the walker has taken them all, so it never runs more than a chunk
|
|
145
|
+
* or two ahead of what has been asked for: a corrupt byte after the end-of-archive block is
|
|
146
|
+
* never decoded, and the data before a corrupt byte is handed over before its error is. A
|
|
147
|
+
* member's trailer is read only when the walker asks for data past the end of its deflate
|
|
148
|
+
* data, and then in full: the CRC and the length checked against what was decompressed,
|
|
149
|
+
* zero padding skipped, and another member's header, or the end of the file, after it.
|
|
150
|
+
*/
|
|
151
|
+
class GzipReader {
|
|
152
|
+
constructor(handle) {
|
|
153
|
+
this.handle = handle;
|
|
154
|
+
this.position = 0;
|
|
155
|
+
this.state = 'header';
|
|
156
|
+
this.engine = null;
|
|
157
|
+
this.queue = [];
|
|
158
|
+
this.failure = null;
|
|
159
|
+
this.ended = false;
|
|
160
|
+
this.pumping = Promise.resolve();
|
|
161
|
+
this.wake = null;
|
|
162
|
+
this.dataStart = 0;
|
|
163
|
+
this.crc = 0;
|
|
164
|
+
this.size = 0;
|
|
165
|
+
this.closed = false;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* `source` opened for reading, a directory refused at the open, where Node's opens it and
|
|
169
|
+
* fails the first read -- with no path in the error.
|
|
170
|
+
*/
|
|
171
|
+
static async open(source) {
|
|
172
|
+
const handle = await fs.promises.open(source, 'r');
|
|
173
|
+
try {
|
|
174
|
+
if ((await handle.stat()).isDirectory()) {
|
|
175
|
+
throw Object.assign(new Error(`EISDIR: illegal operation on a directory, open '${source}'`), {
|
|
176
|
+
code: 'EISDIR',
|
|
177
|
+
errno: -21,
|
|
178
|
+
syscall: 'open',
|
|
179
|
+
path: source,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
await handle.close();
|
|
185
|
+
throw error;
|
|
186
|
+
}
|
|
187
|
+
return new GzipReader(handle);
|
|
188
|
+
}
|
|
189
|
+
async next() {
|
|
190
|
+
for (;;) {
|
|
191
|
+
if (this.state === 'done')
|
|
192
|
+
return null;
|
|
193
|
+
if (this.state === 'header') {
|
|
194
|
+
if (!(await this.readHeader())) {
|
|
195
|
+
this.state = 'done';
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
this.startMember();
|
|
199
|
+
this.state = 'member';
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (this.state === 'trailer') {
|
|
203
|
+
await this.readEof();
|
|
204
|
+
this.state = 'header';
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
const chunk = this.queue.shift();
|
|
208
|
+
if (chunk !== undefined) {
|
|
209
|
+
if (this.queue.length === 0)
|
|
210
|
+
this.engine?.resume();
|
|
211
|
+
return chunk;
|
|
212
|
+
}
|
|
213
|
+
if (this.failure !== null)
|
|
214
|
+
throw this.failure;
|
|
215
|
+
if (this.ended) {
|
|
216
|
+
await this.pumping;
|
|
217
|
+
this.position = this.dataStart + (this.engine?.bytesWritten ?? 0);
|
|
218
|
+
this.engine = null;
|
|
219
|
+
this.state = 'trailer';
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
await new Promise((resolve) => {
|
|
223
|
+
this.wake = resolve;
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
async close() {
|
|
228
|
+
this.closed = true;
|
|
229
|
+
this.engine?.destroy();
|
|
230
|
+
this.engine = null;
|
|
231
|
+
await this.handle.close();
|
|
232
|
+
}
|
|
233
|
+
/** Up to `count` bytes of the file at the current position, fewer only at its end. */
|
|
234
|
+
async read(count) {
|
|
235
|
+
const buffer = Buffer.alloc(count);
|
|
236
|
+
let have = 0;
|
|
237
|
+
while (have < count) {
|
|
238
|
+
const { bytesRead } = await this.handle.read(buffer, have, count - have, this.position);
|
|
239
|
+
if (bytesRead === 0)
|
|
240
|
+
break;
|
|
241
|
+
have += bytesRead;
|
|
242
|
+
this.position += bytesRead;
|
|
243
|
+
}
|
|
244
|
+
return buffer.subarray(0, have);
|
|
245
|
+
}
|
|
246
|
+
/** Exactly `count` bytes, or the ended-early `TruncatedData` error. */
|
|
247
|
+
async readExact(count) {
|
|
248
|
+
const data = await this.read(count);
|
|
249
|
+
if (data.length < count)
|
|
250
|
+
throw endedEarly();
|
|
251
|
+
return data;
|
|
252
|
+
}
|
|
253
|
+
/** A member's gzip header: false at the end of the file, where a member would start. */
|
|
254
|
+
async readHeader() {
|
|
255
|
+
const magic = await this.read(2);
|
|
256
|
+
if (magic.length === 0)
|
|
257
|
+
return false;
|
|
258
|
+
if (magic.length !== 2 || magic[0] !== 0x1f || magic[1] !== 0x8b) {
|
|
259
|
+
throw new BadGzip(`Not a gzipped file (it starts with ${leadingBytes(magic)})`);
|
|
260
|
+
}
|
|
261
|
+
const fixed = await this.readExact(8);
|
|
262
|
+
if (fixed[0] !== 8)
|
|
263
|
+
throw new BadGzip('Unknown compression method');
|
|
264
|
+
const flag = fixed[1];
|
|
265
|
+
if ((flag & FEXTRA) !== 0)
|
|
266
|
+
await this.readExact((await this.readExact(2)).readUInt16LE(0));
|
|
267
|
+
// A name and a comment end at a NUL -- or at the end of the file, which is not an error.
|
|
268
|
+
for (const bit of [FNAME, FCOMMENT]) {
|
|
269
|
+
if ((flag & bit) === 0)
|
|
270
|
+
continue;
|
|
271
|
+
for (;;) {
|
|
272
|
+
const one = await this.read(1);
|
|
273
|
+
if (one.length === 0 || one[0] === 0)
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if ((flag & FHCRC) !== 0)
|
|
278
|
+
await this.readExact(2);
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
/** A member's trailer: its CRC and length, then any zero padding after it. */
|
|
282
|
+
async readEof() {
|
|
283
|
+
const trailer = await this.readExact(8);
|
|
284
|
+
const crc = trailer.readUInt32LE(0);
|
|
285
|
+
if (crc !== this.crc) {
|
|
286
|
+
throw new BadGzip(`CRC check failed 0x${crc.toString(16)} != 0x${this.crc.toString(16)}`);
|
|
287
|
+
}
|
|
288
|
+
if (trailer.readUInt32LE(4) !== this.size % 2 ** 32) {
|
|
289
|
+
throw new BadGzip('Incorrect length of data produced');
|
|
290
|
+
}
|
|
291
|
+
for (;;) {
|
|
292
|
+
const one = await this.read(1);
|
|
293
|
+
if (one.length === 0)
|
|
294
|
+
break;
|
|
295
|
+
if (one[0] !== 0) {
|
|
296
|
+
this.position -= 1;
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
poke() {
|
|
302
|
+
const wake = this.wake;
|
|
303
|
+
this.wake = null;
|
|
304
|
+
wake?.();
|
|
305
|
+
}
|
|
306
|
+
startMember() {
|
|
307
|
+
const engine = zlib.createInflateRaw();
|
|
308
|
+
this.engine = engine;
|
|
309
|
+
this.queue = [];
|
|
310
|
+
this.failure = null;
|
|
311
|
+
this.ended = false;
|
|
312
|
+
this.dataStart = this.position;
|
|
313
|
+
this.crc = 0;
|
|
314
|
+
this.size = 0;
|
|
315
|
+
engine.on('data', (chunk) => {
|
|
316
|
+
if (this.engine !== engine)
|
|
317
|
+
return;
|
|
318
|
+
this.queue.push(chunk);
|
|
319
|
+
this.crc = zlib.crc32(chunk, this.crc);
|
|
320
|
+
this.size += chunk.length;
|
|
321
|
+
engine.pause();
|
|
322
|
+
this.poke();
|
|
323
|
+
});
|
|
324
|
+
engine.on('end', () => {
|
|
325
|
+
if (this.engine !== engine)
|
|
326
|
+
return;
|
|
327
|
+
this.ended = true;
|
|
328
|
+
this.poke();
|
|
329
|
+
});
|
|
330
|
+
engine.on('error', (error) => {
|
|
331
|
+
if (this.engine !== engine)
|
|
332
|
+
return;
|
|
333
|
+
this.failure ?? (this.failure = error);
|
|
334
|
+
this.poke();
|
|
335
|
+
});
|
|
336
|
+
this.pumping = this.pump(engine, this.position);
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Feed the member's compressed bytes, one read at a time and only as fast as the inflater
|
|
340
|
+
* takes them. It stops where the deflate data ends -- the inflater consumed less than it
|
|
341
|
+
* was given -- so what follows is left for the trailer; at the end of the file it ends the
|
|
342
|
+
* inflater, which is zlib's "unexpected end of file" when the member is incomplete.
|
|
343
|
+
*/
|
|
344
|
+
async pump(engine, start) {
|
|
345
|
+
let position = start;
|
|
346
|
+
let fed = 0;
|
|
347
|
+
try {
|
|
348
|
+
for (;;) {
|
|
349
|
+
const buffer = Buffer.allocUnsafe(READ_SIZE);
|
|
350
|
+
const { bytesRead } = await this.handle.read(buffer, 0, READ_SIZE, position);
|
|
351
|
+
if (this.closed || engine.destroyed)
|
|
352
|
+
return;
|
|
353
|
+
if (bytesRead === 0) {
|
|
354
|
+
engine.end();
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
position += bytesRead;
|
|
358
|
+
fed += bytesRead;
|
|
359
|
+
await new Promise((resolve, reject) => {
|
|
360
|
+
engine.write(buffer.subarray(0, bytesRead), (error) => error === null || error === undefined ? resolve() : reject(error));
|
|
361
|
+
});
|
|
362
|
+
if (engine.bytesWritten < fed)
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
catch (error) {
|
|
367
|
+
if (this.closed || this.engine !== engine)
|
|
368
|
+
return;
|
|
369
|
+
this.failure ?? (this.failure = error);
|
|
370
|
+
engine.destroy();
|
|
371
|
+
this.poke();
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
function field(block, offset, length) {
|
|
376
|
+
const raw = block.subarray(offset, offset + length);
|
|
377
|
+
const end = raw.indexOf(0);
|
|
378
|
+
return (end >= 0 ? raw.subarray(0, end) : raw).toString('utf8');
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* A header the archive reader cannot use, in the established words: reading the next member
|
|
382
|
+
* ends the archive on some and makes a `ReadError` of the others, depending on where the
|
|
383
|
+
* header was.
|
|
384
|
+
*/
|
|
385
|
+
class HeaderError extends Error {
|
|
386
|
+
constructor(kind, message) {
|
|
387
|
+
super(message);
|
|
388
|
+
this.kind = kind;
|
|
389
|
+
this.name = 'HeaderError';
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* A number field, or an `invalid header` HeaderError.
|
|
394
|
+
*
|
|
395
|
+
* GNU BASE-256 when the first byte is 0o200 (or 0o377, negative): the GNU format uses this
|
|
396
|
+
* for a value that does not fit the octal field -- a size at or above 8 GiB -- and reading
|
|
397
|
+
* those bytes as octal text silently answered 0. The PAX format houston's bundles use writes
|
|
398
|
+
* a PAX `size` record instead; see `paxSize` in `walk`. Otherwise ASCII up to the first
|
|
399
|
+
* NUL, stripped, and read as octal, an empty field as 0: a field that is not octal makes
|
|
400
|
+
* the header invalid. It answered 0, so a header garbled by a corrupt byte was walked as a
|
|
401
|
+
* member of size 0 and the reader went on past where the established reader stops.
|
|
402
|
+
*/
|
|
403
|
+
function nti(block, offset, length) {
|
|
404
|
+
const raw = block.subarray(offset, offset + length);
|
|
405
|
+
if (raw[0] === 0o200 || raw[0] === 0o377) {
|
|
406
|
+
let value = 0;
|
|
407
|
+
for (let index = 1; index < raw.length; index += 1) {
|
|
408
|
+
value = value * 256 + raw[index];
|
|
409
|
+
}
|
|
410
|
+
return raw[0] === 0o377 ? -(256 ** (raw.length - 1) - value) : value;
|
|
411
|
+
}
|
|
412
|
+
const end = raw.indexOf(0);
|
|
413
|
+
const bytes = end >= 0 ? raw.subarray(0, end) : raw;
|
|
414
|
+
if (bytes.some((byte) => byte >= 0x80))
|
|
415
|
+
throw new HeaderError('invalid', 'invalid header');
|
|
416
|
+
const text = (0, float_text_1.stripText)(bytes.toString('latin1'));
|
|
417
|
+
try {
|
|
418
|
+
return (0, format_1.intFromText)(text === '' ? '0' : text, 8);
|
|
419
|
+
}
|
|
420
|
+
catch {
|
|
421
|
+
throw new HeaderError('invalid', 'invalid header');
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
/** The header's checksums: summed with the checksum field as spaces, unsigned and signed. */
|
|
425
|
+
function calcChecksums(block) {
|
|
426
|
+
let unsigned = 256;
|
|
427
|
+
let signed = 256;
|
|
428
|
+
for (let index = 0; index < BLOCK; index += 1) {
|
|
429
|
+
if (index >= 148 && index < 156)
|
|
430
|
+
continue;
|
|
431
|
+
const byte = block[index];
|
|
432
|
+
unsigned += byte;
|
|
433
|
+
signed += byte > 127 ? byte - 256 : byte;
|
|
434
|
+
}
|
|
435
|
+
return [unsigned, signed];
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* A header block's checks, in the established order, and the fields the walker uses: the
|
|
439
|
+
* checksum, then every number field, any of which makes the header invalid. The walker
|
|
440
|
+
* checked neither.
|
|
441
|
+
*/
|
|
442
|
+
function parseHeader(block) {
|
|
443
|
+
if (block.length === 0)
|
|
444
|
+
throw new HeaderError('empty', 'empty header');
|
|
445
|
+
if (block.length !== BLOCK)
|
|
446
|
+
throw new HeaderError('truncated', 'truncated header');
|
|
447
|
+
if (isZeroBlock(block))
|
|
448
|
+
throw new HeaderError('eof', 'end of file header');
|
|
449
|
+
const checksum = nti(block, 148, 8);
|
|
450
|
+
if (!calcChecksums(block).includes(checksum))
|
|
451
|
+
throw new HeaderError('invalid', 'bad checksum');
|
|
452
|
+
let name = field(block, 0, 100);
|
|
453
|
+
for (const [offset, length] of [
|
|
454
|
+
[100, 8],
|
|
455
|
+
[108, 8],
|
|
456
|
+
[116, 8],
|
|
457
|
+
]) {
|
|
458
|
+
nti(block, offset, length);
|
|
459
|
+
}
|
|
460
|
+
const size = nti(block, 124, 12);
|
|
461
|
+
nti(block, 136, 12);
|
|
462
|
+
nti(block, 329, 8);
|
|
463
|
+
nti(block, 337, 8);
|
|
464
|
+
const raw = field(block, 156, 1);
|
|
465
|
+
// Old V7 tar marks a directory as a regular file (NUL type) whose name ends in `/`.
|
|
466
|
+
const typeflag = raw === '' && name.endsWith('/') ? '5' : raw || '0';
|
|
467
|
+
const prefix = field(block, 345, 155);
|
|
468
|
+
if (prefix !== '' && !['L', 'K', 'S'].includes(typeflag))
|
|
469
|
+
name = `${prefix}/${name}`;
|
|
470
|
+
return { name, size, typeflag };
|
|
471
|
+
}
|
|
472
|
+
function isZeroBlock(block) {
|
|
473
|
+
for (const byte of block)
|
|
474
|
+
if (byte !== 0)
|
|
475
|
+
return false;
|
|
476
|
+
return true;
|
|
477
|
+
}
|
|
478
|
+
function paxRecords(body) {
|
|
479
|
+
const out = {};
|
|
480
|
+
let at = 0;
|
|
481
|
+
while (at < body.length) {
|
|
482
|
+
const space = body.indexOf(0x20, at);
|
|
483
|
+
if (space < 0)
|
|
484
|
+
break;
|
|
485
|
+
const length = Number.parseInt(body.subarray(at, space).toString('ascii'), 10);
|
|
486
|
+
if (!Number.isFinite(length) || length <= 0)
|
|
487
|
+
break;
|
|
488
|
+
const record = body.subarray(space + 1, at + length).toString('utf8');
|
|
489
|
+
const eq = record.indexOf('=');
|
|
490
|
+
if (eq > 0)
|
|
491
|
+
out[record.slice(0, eq)] = record.slice(eq + 1).replace(/\n$/u, '');
|
|
492
|
+
at += length;
|
|
493
|
+
}
|
|
494
|
+
return out;
|
|
495
|
+
}
|
|
496
|
+
function nameOf(header) {
|
|
497
|
+
return header.paxPath !== ''
|
|
498
|
+
? header.paxPath
|
|
499
|
+
: header.longName !== ''
|
|
500
|
+
? header.longName
|
|
501
|
+
: header.name;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Reject an entry that may not be extracted, before anything is written.
|
|
505
|
+
*
|
|
506
|
+
* The established rule: absolute, `..`, a symlink, a hard link or a device. A
|
|
507
|
+
* directory name ending in `/` is fine; a `..` in ANY part is not.
|
|
508
|
+
*/
|
|
509
|
+
function requireSafeMember(name, typeflag) {
|
|
510
|
+
const parts = name.split('/');
|
|
511
|
+
if (path.isAbsolute(name) ||
|
|
512
|
+
parts.includes('..') ||
|
|
513
|
+
typeflag === '2' ||
|
|
514
|
+
typeflag === '1' ||
|
|
515
|
+
typeflag === '3' ||
|
|
516
|
+
typeflag === '4' ||
|
|
517
|
+
typeflag === '6') {
|
|
518
|
+
throw new UnsafeBundleEntry(name);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* The member types that are regular files, which is not only `'0'`. A CONTTYPE (`'7'`)
|
|
523
|
+
* member is a regular file too, and a bundle whose manifest was one restored in earlier
|
|
524
|
+
* releases and was "has no manifest.json" here. GNU sparse (`'S'`) is read as its stored data.
|
|
525
|
+
*/
|
|
526
|
+
const REGULAR_TYPES = new Set(['0', '7', 'S']);
|
|
527
|
+
/** The member types the archive reader knows; any OTHER type reads as a regular file too. */
|
|
528
|
+
const SUPPORTED_TYPES = new Set(['0', '1', '2', '3', '4', '5', '6', '7', 'L', 'K', 'S']);
|
|
529
|
+
/**
|
|
530
|
+
* Whether reading the member hands back its data: a regular type, or one the archive reader
|
|
531
|
+
* does not know -- a directory, a link or a device has none, which is "has no manifest.json".
|
|
532
|
+
*/
|
|
533
|
+
function readsAsFile(typeflag) {
|
|
534
|
+
return REGULAR_TYPES.has(typeflag) || !SUPPORTED_TYPES.has(typeflag);
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* What a failure is when it happens while a HEADER is read: a zlib error is
|
|
538
|
+
* `ReadError("zlib error: ...")`, and, for the first member, an OS error is
|
|
539
|
+
* `ReadError("not a gzip file")`. A failure while reading or skipping a member's DATA
|
|
540
|
+
* reaches the caller raw: `gzipFailure` names a zlib error there as the truncation that
|
|
541
|
+
* reading the manifest and unpacking the archive report.
|
|
542
|
+
*/
|
|
543
|
+
function headerFailure(error, first) {
|
|
544
|
+
const code = String(error.code ?? '');
|
|
545
|
+
if (code === 'Z_DATA_ERROR') {
|
|
546
|
+
return new TarReadError(`zlib error: ${zlibText(error)}`);
|
|
547
|
+
}
|
|
548
|
+
const osError = error instanceof BadGzip ||
|
|
549
|
+
(typeof error.errno === 'number' && !code.startsWith('Z_'));
|
|
550
|
+
if (first && osError)
|
|
551
|
+
return new TarReadError('not a gzip file');
|
|
552
|
+
return error;
|
|
553
|
+
}
|
|
554
|
+
/** A zlib data error in the established words: `Error -3 while decompressing data: <reason>`. */
|
|
555
|
+
function zlibText(error) {
|
|
556
|
+
const own = error.message;
|
|
557
|
+
return `Error -3 while decompressing data: ${typeof own === 'string' ? own : String(error)}`;
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Iterate the members of `source`, calling `onMember` with a header and a chunk source.
|
|
561
|
+
*
|
|
562
|
+
* The callback must consume the member's data (or ignore it), and returning `false`
|
|
563
|
+
* stops the scan -- which is what lets `readMember` stop after the manifest instead of
|
|
564
|
+
* decompressing a multi-gigabyte archive to reach it.
|
|
565
|
+
*/
|
|
566
|
+
async function walk(source, onMember) {
|
|
567
|
+
const reader = await GzipReader.open(source);
|
|
568
|
+
let buffered = Buffer.alloc(0);
|
|
569
|
+
let longName = '';
|
|
570
|
+
let paxPath = '';
|
|
571
|
+
// A PAX `size` record, for the member after it. The PAX format bundles are written in puts
|
|
572
|
+
// 0 in the ustar size field of a member of 8 GiB or more and the real size here, so a
|
|
573
|
+
// bundle whose mongo.archive.gz reached 8 GiB read as a zero-length member followed by
|
|
574
|
+
// archive bytes parsed as headers: UNREADABLE, "archive ended inside a member", where it
|
|
575
|
+
// must be listed and restored.
|
|
576
|
+
let paxSize = null;
|
|
577
|
+
// Bytes of the tar stream consumed so far, which decides whether a header that is not
|
|
578
|
+
// there is an error (at 0) or the end of the archive (past it).
|
|
579
|
+
let consumed = 0;
|
|
580
|
+
// Members handed over so far: until the first is, a header failure is the opening's.
|
|
581
|
+
let yielded = 0;
|
|
582
|
+
async function take(count) {
|
|
583
|
+
const parts = [];
|
|
584
|
+
let have = 0;
|
|
585
|
+
while (have < count) {
|
|
586
|
+
if (buffered.length === 0) {
|
|
587
|
+
const chunk = await reader.next();
|
|
588
|
+
if (chunk === null) {
|
|
589
|
+
throw new TarReadError('unexpected end of data');
|
|
590
|
+
}
|
|
591
|
+
buffered = chunk;
|
|
592
|
+
}
|
|
593
|
+
const share = Math.min(buffered.length, count - have);
|
|
594
|
+
parts.push(buffered.subarray(0, share));
|
|
595
|
+
buffered = buffered.subarray(share);
|
|
596
|
+
have += share;
|
|
597
|
+
consumed += share;
|
|
598
|
+
}
|
|
599
|
+
return Buffer.concat(parts);
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* The next header block: 512 bytes, or fewer only where the tar stream ends.
|
|
603
|
+
* `parseHeader` says what a short one means, and the loop says what that
|
|
604
|
+
* means where it was read.
|
|
605
|
+
*/
|
|
606
|
+
async function readBlock() {
|
|
607
|
+
return await takeUpTo(BLOCK);
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Up to `count` bytes, fewer only where the stream ends, which is how an extension
|
|
611
|
+
* header's body is read. A body cut short is not an error there; the header after it is.
|
|
612
|
+
*/
|
|
613
|
+
async function takeUpTo(count) {
|
|
614
|
+
const parts = [];
|
|
615
|
+
let have = 0;
|
|
616
|
+
while (have < count) {
|
|
617
|
+
if (buffered.length === 0) {
|
|
618
|
+
const chunk = await reader.next();
|
|
619
|
+
if (chunk === null)
|
|
620
|
+
break;
|
|
621
|
+
buffered = chunk;
|
|
622
|
+
}
|
|
623
|
+
const share = Math.min(buffered.length, count - have);
|
|
624
|
+
parts.push(buffered.subarray(0, share));
|
|
625
|
+
buffered = buffered.subarray(share);
|
|
626
|
+
have += share;
|
|
627
|
+
consumed += share;
|
|
628
|
+
}
|
|
629
|
+
return Buffer.concat(parts);
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* `take`, without building the result.
|
|
633
|
+
*
|
|
634
|
+
* A member's data still has to be consumed to reach the next header, and `take` would
|
|
635
|
+
* concatenate a multi-gigabyte archive member into one buffer. Skipping is the whole
|
|
636
|
+
* reason `memberNames` and a full-stream manifest read can walk a real bundle.
|
|
637
|
+
*/
|
|
638
|
+
async function discard(count) {
|
|
639
|
+
let remaining = count;
|
|
640
|
+
while (remaining > 0) {
|
|
641
|
+
if (buffered.length === 0) {
|
|
642
|
+
const chunk = await reader.next();
|
|
643
|
+
if (chunk === null) {
|
|
644
|
+
throw new TarReadError('unexpected end of data');
|
|
645
|
+
}
|
|
646
|
+
buffered = chunk;
|
|
647
|
+
}
|
|
648
|
+
const share = Math.min(buffered.length, remaining);
|
|
649
|
+
buffered = buffered.subarray(share);
|
|
650
|
+
remaining -= share;
|
|
651
|
+
consumed += share;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
/** A read made while the next header is being read: its failures are `headerFailure`'s. */
|
|
655
|
+
async function inHeader(read) {
|
|
656
|
+
try {
|
|
657
|
+
return await read();
|
|
658
|
+
}
|
|
659
|
+
catch (error) {
|
|
660
|
+
throw headerFailure(error, yielded === 0);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
// Whether the header being read follows a PAX or GNU long-name header, which read it
|
|
664
|
+
// themselves and raise its failure as a `ReadError` of its own.
|
|
665
|
+
let afterExtension = false;
|
|
666
|
+
try {
|
|
667
|
+
for (;;) {
|
|
668
|
+
const at = consumed;
|
|
669
|
+
const block = await inHeader(readBlock);
|
|
670
|
+
let parsed;
|
|
671
|
+
try {
|
|
672
|
+
parsed = parseHeader(block);
|
|
673
|
+
}
|
|
674
|
+
catch (error) {
|
|
675
|
+
if (!(error instanceof HeaderError))
|
|
676
|
+
throw error;
|
|
677
|
+
// After an extension header, any failure is a ReadError in its own words ("empty
|
|
678
|
+
// header", "bad checksum", ...). Otherwise a zero block is the end of the archive,
|
|
679
|
+
// and so is any header past the first that cannot be used -- a stream cut short, a
|
|
680
|
+
// bad checksum, a number field that is not one; at the first they are "empty file",
|
|
681
|
+
// "truncated header" and the rest in their own words. This walker read on through a
|
|
682
|
+
// header a corrupt byte had garbled, and reached the gzip trailer the established
|
|
683
|
+
// reader never reads.
|
|
684
|
+
if (afterExtension)
|
|
685
|
+
throw new TarReadError(error.message);
|
|
686
|
+
if (error.kind === 'eof')
|
|
687
|
+
break;
|
|
688
|
+
if (at === 0)
|
|
689
|
+
throw new TarReadError(error.kind === 'empty' ? 'empty file' : error.message);
|
|
690
|
+
break;
|
|
691
|
+
}
|
|
692
|
+
afterExtension = false;
|
|
693
|
+
const header = {
|
|
694
|
+
name: parsed.name,
|
|
695
|
+
size: paxSize ?? parsed.size,
|
|
696
|
+
typeflag: parsed.typeflag,
|
|
697
|
+
longName,
|
|
698
|
+
paxPath,
|
|
699
|
+
};
|
|
700
|
+
longName = '';
|
|
701
|
+
paxPath = '';
|
|
702
|
+
paxSize = null;
|
|
703
|
+
if (header.typeflag === 'x' || header.typeflag === 'g') {
|
|
704
|
+
// A PAX header is small (a path record), but skipping rather than buffering
|
|
705
|
+
// keeps every non-payload member on one code path.
|
|
706
|
+
const body = await inHeader(async () => await takeUpTo(header.size + pad(header.size)));
|
|
707
|
+
const records = paxRecords(body.subarray(0, header.size));
|
|
708
|
+
if (header.typeflag === 'x' && records.path !== undefined)
|
|
709
|
+
paxPath = records.path;
|
|
710
|
+
if (header.typeflag === 'x' && records.size !== undefined) {
|
|
711
|
+
// The integer grammar (`toInt`), and 0 where the value is not an integer.
|
|
712
|
+
try {
|
|
713
|
+
paxSize = (0, format_1.toInt)(records.size);
|
|
714
|
+
}
|
|
715
|
+
catch {
|
|
716
|
+
paxSize = 0;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
afterExtension = true;
|
|
720
|
+
continue;
|
|
721
|
+
}
|
|
722
|
+
if (header.typeflag === 'L') {
|
|
723
|
+
// The name ends at its first NUL.
|
|
724
|
+
const body = await inHeader(async () => await takeUpTo(header.size + pad(header.size)));
|
|
725
|
+
const end = body.subarray(0, header.size).indexOf(0);
|
|
726
|
+
longName = body.subarray(0, end >= 0 ? end : header.size).toString('utf8');
|
|
727
|
+
afterExtension = true;
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
if (header.typeflag === 'K') {
|
|
731
|
+
// GNU's long LINK name, for the member after it: a link, which is refused anyway.
|
|
732
|
+
await inHeader(async () => await takeUpTo(header.size + pad(header.size)));
|
|
733
|
+
afterExtension = true;
|
|
734
|
+
continue;
|
|
735
|
+
}
|
|
736
|
+
const name = nameOf(header);
|
|
737
|
+
// Through an object: a closure assignment to a plain `let` stays narrowed by
|
|
738
|
+
// the compiler, which then calls the guard unreachable.
|
|
739
|
+
const state = { emitted: false };
|
|
740
|
+
const readData = async () => {
|
|
741
|
+
state.emitted = true;
|
|
742
|
+
const data = await take(header.size);
|
|
743
|
+
await take(pad(header.size));
|
|
744
|
+
return data;
|
|
745
|
+
};
|
|
746
|
+
// STREAMED, so a database archive larger than memory is never buffered. Through a
|
|
747
|
+
// FileHandle opened before the first read, and CLOSED before a failed read is
|
|
748
|
+
// rethrown: a WriteStream was still opening when the gunzip under it failed, the
|
|
749
|
+
// caller's `finally` removed the staging directory, and the stream's own ENOENT was an
|
|
750
|
+
// unhandled 'error' event that killed the process.
|
|
751
|
+
const writeData = async (target) => {
|
|
752
|
+
state.emitted = true;
|
|
753
|
+
const handle = await fs.promises.open(target, 'w');
|
|
754
|
+
try {
|
|
755
|
+
let remaining = header.size;
|
|
756
|
+
while (remaining > 0) {
|
|
757
|
+
const chunk = await take(Math.min(remaining, 64 * 1024));
|
|
758
|
+
await handle.write(chunk);
|
|
759
|
+
remaining -= chunk.length;
|
|
760
|
+
}
|
|
761
|
+
await take(pad(header.size));
|
|
762
|
+
}
|
|
763
|
+
finally {
|
|
764
|
+
await handle.close();
|
|
765
|
+
}
|
|
766
|
+
};
|
|
767
|
+
yielded += 1;
|
|
768
|
+
const goOn = await onMember({ name, typeflag: header.typeflag, size: header.size }, readData, writeData);
|
|
769
|
+
if (!state.emitted)
|
|
770
|
+
await discard(header.size + pad(header.size));
|
|
771
|
+
if (goOn === false)
|
|
772
|
+
break;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
finally {
|
|
776
|
+
await reader.close();
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
function pad(size) {
|
|
780
|
+
const remainder = size % BLOCK;
|
|
781
|
+
return remainder === 0 ? 0 : BLOCK - remainder;
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* One member's bytes, or null -- after the safety check over every member.
|
|
785
|
+
* Streaming: a bundle whose archive member is larger than the requested one is never buffered.
|
|
786
|
+
*
|
|
787
|
+
* THE WHOLE STREAM IS READ, even after the wanted member is found, because every member is
|
|
788
|
+
* enumerated, and the enumeration is what notices a bundle truncated AFTER the manifest.
|
|
789
|
+
* Stopping early made a truncated bundle look healthy to `listBackups`; the decompression
|
|
790
|
+
* cost is the same one earlier releases paid, and only the non-payload bytes are buffered.
|
|
791
|
+
*
|
|
792
|
+
* AND IN THE ESTABLISHED ORDER: every header first, then the safety check over all of them,
|
|
793
|
+
* then the member, searched for from the END, so of two `manifest.json` members the LAST is
|
|
794
|
+
* the one read -- the first was, and a bundle carrying both reached the opposite restore
|
|
795
|
+
* verdict from the established one. Names are compared as stored, not normalised
|
|
796
|
+
* (`./manifest.json` is not the manifest), with the trailing `/` a directory's name loses.
|
|
797
|
+
*/
|
|
798
|
+
async function readMember(source, wanted) {
|
|
799
|
+
const target = wanted.replace(/\/+$/u, '');
|
|
800
|
+
const members = [];
|
|
801
|
+
let found = null;
|
|
802
|
+
await walk(source, async (member, readData) => {
|
|
803
|
+
members.push(member);
|
|
804
|
+
const name = member.typeflag === '5' ? member.name.replace(/\/+$/u, '') : member.name;
|
|
805
|
+
if (name !== target)
|
|
806
|
+
return;
|
|
807
|
+
// A ZERO-LENGTH member still counts: it reads as an empty file, and an empty manifest
|
|
808
|
+
// is the "malformed manifest" branch -- skipping it here reported "has no
|
|
809
|
+
// manifest.json" instead. A directory of that name is null.
|
|
810
|
+
found = readsAsFile(member.typeflag) ? await readData() : null;
|
|
811
|
+
});
|
|
812
|
+
// THE SAME REFUSAL AS BEFORE AN EXTRACT, and for the same reason: a bundle can arrive
|
|
813
|
+
// from a colleague, so a read of one is untrusted input too. Without this,
|
|
814
|
+
// `listBackups` presented a bundle containing an absolute path or a symlink as healthy,
|
|
815
|
+
// where its manifest must not be read at all. Every member is checked, after the whole
|
|
816
|
+
// stream has been read.
|
|
817
|
+
for (const member of members)
|
|
818
|
+
requireSafeMember(member.name, member.typeflag);
|
|
819
|
+
return found;
|
|
820
|
+
}
|
|
821
|
+
/** Member names in order, for a caller that wants to refuse before extracting. */
|
|
822
|
+
async function memberNames(source) {
|
|
823
|
+
const members = [];
|
|
824
|
+
await walk(source, async (member) => {
|
|
825
|
+
members.push(member);
|
|
826
|
+
});
|
|
827
|
+
for (const member of members)
|
|
828
|
+
requireSafeMember(member.name, member.typeflag);
|
|
829
|
+
return members.map((member) => member.name);
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Extract every regular file and directory under `dest`, streaming each file to disk.
|
|
833
|
+
*
|
|
834
|
+
* Refuses an unsafe entry and aborts; the caller's `dest` is a temporary staging
|
|
835
|
+
* directory, so a partial extraction never becomes state. The refusals come in the
|
|
836
|
+
* established order -- a stream that cannot be read, then an unsafe member, then a
|
|
837
|
+
* `SpecialFileError` for a type the extraction filter does not know -- although this walks
|
|
838
|
+
* the archive once: an unsafe member is never written, so extracting the safe ones on the
|
|
839
|
+
* way is invisible.
|
|
840
|
+
*/
|
|
841
|
+
async function extractTarGz(source, dest) {
|
|
842
|
+
// Through an object, as `walk`'s `state` is: the compiler keeps a `let` narrowed to its
|
|
843
|
+
// initial `null` across the callback that assigns it.
|
|
844
|
+
const refused = {
|
|
845
|
+
unsafe: null,
|
|
846
|
+
special: null,
|
|
847
|
+
};
|
|
848
|
+
await walk(source, async (member, _readData, writeData) => {
|
|
849
|
+
try {
|
|
850
|
+
requireSafeMember(member.name, member.typeflag);
|
|
851
|
+
}
|
|
852
|
+
catch (error) {
|
|
853
|
+
if (!(error instanceof UnsafeBundleEntry))
|
|
854
|
+
throw error;
|
|
855
|
+
refused.unsafe ?? (refused.unsafe = error);
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
if (refused.unsafe !== null || refused.special !== null)
|
|
859
|
+
return;
|
|
860
|
+
const target = path.join(dest, member.name);
|
|
861
|
+
if (member.typeflag === '5' || member.name.endsWith('/')) {
|
|
862
|
+
await fs.promises.mkdir(target, { recursive: true });
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
if (!REGULAR_TYPES.has(member.typeflag)) {
|
|
866
|
+
refused.special = member.name;
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
await fs.promises.mkdir(path.dirname(target), { recursive: true });
|
|
870
|
+
await writeData(target);
|
|
871
|
+
});
|
|
872
|
+
if (refused.unsafe !== null)
|
|
873
|
+
throw refused.unsafe;
|
|
874
|
+
if (refused.special !== null)
|
|
875
|
+
throw new TarReadError(`${(0, format_1.quotedText)(refused.special)} is a special file`);
|
|
876
|
+
}
|
|
877
|
+
/** The record size: a written archive is padded with zero blocks to a multiple of it. */
|
|
878
|
+
const RECORDSIZE = BLOCK * 20;
|
|
879
|
+
/** The POSIX magic: `ustar`, NUL, then the version `00`. */
|
|
880
|
+
const POSIX_MAGIC = Buffer.from('ustar\u000000', 'latin1');
|
|
881
|
+
/** A string field in ASCII, NUL-padded to `length`: one `?` per character ASCII cannot hold. */
|
|
882
|
+
function stnAscii(value, length) {
|
|
883
|
+
let text = '';
|
|
884
|
+
for (const ch of value)
|
|
885
|
+
text += ch.codePointAt(0) < 0x80 ? ch : '?';
|
|
886
|
+
const out = Buffer.alloc(length);
|
|
887
|
+
out.write(text.slice(0, length), 0, 'latin1');
|
|
888
|
+
return out;
|
|
889
|
+
}
|
|
890
|
+
/** A ustar number field: octal in `digits - 1` places and a NUL. */
|
|
891
|
+
function itn(value, digits) {
|
|
892
|
+
if (!(value >= 0 && value < 8 ** (digits - 1))) {
|
|
893
|
+
const overflow = new Error('overflow in number field');
|
|
894
|
+
overflow.name = 'InvalidValue';
|
|
895
|
+
throw overflow;
|
|
896
|
+
}
|
|
897
|
+
return Buffer.from(`${Math.trunc(value)
|
|
898
|
+
.toString(8)
|
|
899
|
+
.padStart(digits - 1, '0')}\0`, 'latin1');
|
|
900
|
+
}
|
|
901
|
+
/** A ustar header block, its string fields in ASCII with `?` for what ASCII cannot hold. */
|
|
902
|
+
function createHeader(info) {
|
|
903
|
+
const device = info.type === '3' || info.type === '4';
|
|
904
|
+
const block = Buffer.alloc(BLOCK);
|
|
905
|
+
Buffer.concat([
|
|
906
|
+
stnAscii(info.name, 100),
|
|
907
|
+
itn(info.mode & 0o7777, 8),
|
|
908
|
+
itn(info.uid, 8),
|
|
909
|
+
itn(info.gid, 8),
|
|
910
|
+
itn(info.size, 12),
|
|
911
|
+
itn(info.mtime, 12),
|
|
912
|
+
Buffer.from(' ', 'latin1'),
|
|
913
|
+
Buffer.from(info.type, 'latin1'),
|
|
914
|
+
stnAscii(info.linkname, 100),
|
|
915
|
+
POSIX_MAGIC,
|
|
916
|
+
stnAscii(info.uname, 32),
|
|
917
|
+
stnAscii(info.gname, 32),
|
|
918
|
+
device ? itn(info.devmajor, 8) : Buffer.alloc(8),
|
|
919
|
+
device ? itn(info.devminor, 8) : Buffer.alloc(8),
|
|
920
|
+
Buffer.alloc(155),
|
|
921
|
+
]).copy(block);
|
|
922
|
+
// The checksum field: the unsigned sum as six octal digits and a NUL, then the field's
|
|
923
|
+
// own eighth byte, a space, left as it was.
|
|
924
|
+
block.write(`${calcChecksums(block)[0].toString(8).padStart(6, '0')}\0`, 148, 7, 'latin1');
|
|
925
|
+
return block;
|
|
926
|
+
}
|
|
927
|
+
/** A member's data: the bytes, padded with NULs to the next block. */
|
|
928
|
+
function payload(data) {
|
|
929
|
+
return Buffer.concat([data, Buffer.alloc(pad(data.length))]);
|
|
930
|
+
}
|
|
931
|
+
/**
|
|
932
|
+
* A PAX extended header: `<length> <keyword>=<value>\n` records in UTF-8, each length
|
|
933
|
+
* counting its own digits, under a `././@PaxHeader` member of type `x`.
|
|
934
|
+
*/
|
|
935
|
+
function paxHeader(records) {
|
|
936
|
+
const parts = [];
|
|
937
|
+
for (const [keyword, value] of records) {
|
|
938
|
+
const key = Buffer.from(keyword, 'utf8');
|
|
939
|
+
const text = Buffer.from(value, 'utf8');
|
|
940
|
+
const length = key.length + text.length + 3;
|
|
941
|
+
let p = 0;
|
|
942
|
+
for (;;) {
|
|
943
|
+
const n = length + String(p).length;
|
|
944
|
+
if (n === p)
|
|
945
|
+
break;
|
|
946
|
+
p = n;
|
|
947
|
+
}
|
|
948
|
+
parts.push(Buffer.from(`${String(p)} `, 'latin1'), key, Buffer.from('='), text, Buffer.from('\n'));
|
|
949
|
+
}
|
|
950
|
+
const body = Buffer.concat(parts);
|
|
951
|
+
const header = createHeader({
|
|
952
|
+
name: '././@PaxHeader',
|
|
953
|
+
mode: 0,
|
|
954
|
+
uid: 0,
|
|
955
|
+
gid: 0,
|
|
956
|
+
size: body.length,
|
|
957
|
+
mtime: 0,
|
|
958
|
+
type: 'x',
|
|
959
|
+
linkname: '',
|
|
960
|
+
uname: '',
|
|
961
|
+
gname: '',
|
|
962
|
+
devmajor: 0,
|
|
963
|
+
devminor: 0,
|
|
964
|
+
});
|
|
965
|
+
return Buffer.concat([header, payload(body)]);
|
|
966
|
+
}
|
|
967
|
+
/** Whether every character of `value` is ASCII. */
|
|
968
|
+
function isAscii(value) {
|
|
969
|
+
for (const ch of value)
|
|
970
|
+
if (ch.codePointAt(0) >= 0x80)
|
|
971
|
+
return false;
|
|
972
|
+
return true;
|
|
973
|
+
}
|
|
974
|
+
/**
|
|
975
|
+
* A member's header in the PAX format houston's bundles use: a PAX record for a string field
|
|
976
|
+
* that is not ASCII or too long for its ustar field (`path`, `linkpath`, `uname`,
|
|
977
|
+
* `gname`, in that order), and for a number field that does not fit or is a float (`uid`,
|
|
978
|
+
* `gid`, `size`, `mtime`) -- a float goes in rounded half to even, and its float text in the
|
|
979
|
+
* record.
|
|
980
|
+
*/
|
|
981
|
+
function tarHeaderOf(tarinfo) {
|
|
982
|
+
const info = { ...tarinfo };
|
|
983
|
+
const records = [];
|
|
984
|
+
for (const [field, keyword, length] of [
|
|
985
|
+
['name', 'path', 100],
|
|
986
|
+
['linkname', 'linkpath', 100],
|
|
987
|
+
['uname', 'uname', 32],
|
|
988
|
+
['gname', 'gname', 32],
|
|
989
|
+
]) {
|
|
990
|
+
const value = info[field];
|
|
991
|
+
if (!isAscii(value) || value.length > length)
|
|
992
|
+
records.push([keyword, value]);
|
|
993
|
+
}
|
|
994
|
+
for (const [field, digits] of [
|
|
995
|
+
['uid', 8],
|
|
996
|
+
['gid', 8],
|
|
997
|
+
['size', 12],
|
|
998
|
+
['mtime', 12],
|
|
999
|
+
]) {
|
|
1000
|
+
const value = info[field];
|
|
1001
|
+
const isFloat = field === 'mtime';
|
|
1002
|
+
const whole = isFloat ? (0, format_1.roundHalfEven)(value) : value;
|
|
1003
|
+
let needsPax = false;
|
|
1004
|
+
if (!(whole >= 0 && whole < 8 ** (digits - 1))) {
|
|
1005
|
+
info[field] = 0;
|
|
1006
|
+
needsPax = true;
|
|
1007
|
+
}
|
|
1008
|
+
else if (isFloat) {
|
|
1009
|
+
info[field] = whole;
|
|
1010
|
+
needsPax = true;
|
|
1011
|
+
}
|
|
1012
|
+
if (needsPax)
|
|
1013
|
+
records.push([field, isFloat ? (0, float_text_1.floatString)(value) : String(value)]);
|
|
1014
|
+
}
|
|
1015
|
+
const header = createHeader(info);
|
|
1016
|
+
return records.length > 0 ? Buffer.concat([paxHeader(records), header]) : header;
|
|
1017
|
+
}
|
|
1018
|
+
/** The float mtime from the nanosecond count, built as `sec + nsec * 1e-9`. */
|
|
1019
|
+
function statFloat(nanoseconds) {
|
|
1020
|
+
let sec = nanoseconds / 1000000000n;
|
|
1021
|
+
let nsec = nanoseconds % 1000000000n;
|
|
1022
|
+
if (nsec < 0n) {
|
|
1023
|
+
nsec += 1000000000n;
|
|
1024
|
+
sec -= 1n;
|
|
1025
|
+
}
|
|
1026
|
+
return Number(sec) + Number(nsec) * 1e-9;
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
1029
|
+
* The user name for a uid and the group name for a gid, or '' where the database has none.
|
|
1030
|
+
*
|
|
1031
|
+
* `/etc/passwd` and `/etc/group` first, the first line with that id, as the `files` source
|
|
1032
|
+
* answers; then, for a uid, the running user through `os.userInfo()`, which asks the whole name
|
|
1033
|
+
* service. Node has neither lookup, so ANOTHER user or group known only to a network source is
|
|
1034
|
+
* '' here, where `getpwuid` or `getgrgid` would find it -- the limit `home.ts` records for `~name`.
|
|
1035
|
+
*/
|
|
1036
|
+
class Owners {
|
|
1037
|
+
constructor() {
|
|
1038
|
+
this.cache = new Map();
|
|
1039
|
+
}
|
|
1040
|
+
user(uid) {
|
|
1041
|
+
return this.lookup('/etc/passwd', uid, () => {
|
|
1042
|
+
try {
|
|
1043
|
+
const self = os.userInfo();
|
|
1044
|
+
return self.uid === uid ? self.username : '';
|
|
1045
|
+
}
|
|
1046
|
+
catch {
|
|
1047
|
+
return '';
|
|
1048
|
+
}
|
|
1049
|
+
});
|
|
1050
|
+
}
|
|
1051
|
+
group(gid) {
|
|
1052
|
+
return this.lookup('/etc/group', gid, () => '');
|
|
1053
|
+
}
|
|
1054
|
+
lookup(file, id, otherwise) {
|
|
1055
|
+
const key = `${file}:${String(id)}`;
|
|
1056
|
+
const cached = this.cache.get(key);
|
|
1057
|
+
if (cached !== undefined)
|
|
1058
|
+
return cached;
|
|
1059
|
+
let table = '';
|
|
1060
|
+
try {
|
|
1061
|
+
table = fs.readFileSync(file, 'utf8');
|
|
1062
|
+
}
|
|
1063
|
+
catch {
|
|
1064
|
+
/* no such database: the name service below, or nothing */
|
|
1065
|
+
}
|
|
1066
|
+
let found = null;
|
|
1067
|
+
for (const line of table.split('\n')) {
|
|
1068
|
+
const fields = line.split(':');
|
|
1069
|
+
if (fields.length >= 3 && fields[2] === String(id)) {
|
|
1070
|
+
found = fields[0] ?? '';
|
|
1071
|
+
break;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
const name = found ?? otherwise();
|
|
1075
|
+
this.cache.set(key, name);
|
|
1076
|
+
return name;
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
/** glibc's `gnu_dev_major` / `gnu_dev_minor`: a device number's major and minor. */
|
|
1080
|
+
function deviceNumbers(rdev) {
|
|
1081
|
+
const major = ((rdev >> 8n) & 0xfffn) | ((rdev >> 32n) & ~0xfffn);
|
|
1082
|
+
const minor = (rdev & 0xffn) | ((rdev >> 12n) & ~0xffn);
|
|
1083
|
+
return [Number(major), Number(minor)];
|
|
1084
|
+
}
|
|
1085
|
+
/**
|
|
1086
|
+
* A path's member fields, as the established archiver takes them: `lstat`, so a link is a
|
|
1087
|
+
* link; a second path to an inode already archived under another name a hard-link member;
|
|
1088
|
+
* a socket, or anything else a tar has no type for, null -- the writer skips it.
|
|
1089
|
+
*/
|
|
1090
|
+
function gettarinfo(absolute, arcname, inodes, owners, mtimeNs) {
|
|
1091
|
+
const st = fs.lstatSync(absolute, { bigint: true });
|
|
1092
|
+
let type;
|
|
1093
|
+
let linkname = '';
|
|
1094
|
+
if (st.isFile()) {
|
|
1095
|
+
const inode = `${String(st.ino)}:${String(st.dev)}`;
|
|
1096
|
+
const earlier = inodes.get(inode);
|
|
1097
|
+
if (st.nlink > 1n && earlier !== undefined && arcname !== earlier) {
|
|
1098
|
+
type = '1';
|
|
1099
|
+
linkname = earlier;
|
|
1100
|
+
}
|
|
1101
|
+
else {
|
|
1102
|
+
type = '0';
|
|
1103
|
+
if (st.ino !== 0n)
|
|
1104
|
+
inodes.set(inode, arcname);
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
else if (st.isDirectory()) {
|
|
1108
|
+
type = '5';
|
|
1109
|
+
}
|
|
1110
|
+
else if (st.isFIFO()) {
|
|
1111
|
+
type = '6';
|
|
1112
|
+
}
|
|
1113
|
+
else if (st.isSymbolicLink()) {
|
|
1114
|
+
type = '2';
|
|
1115
|
+
linkname = fs.readlinkSync(absolute);
|
|
1116
|
+
}
|
|
1117
|
+
else if (st.isCharacterDevice()) {
|
|
1118
|
+
type = '3';
|
|
1119
|
+
}
|
|
1120
|
+
else if (st.isBlockDevice()) {
|
|
1121
|
+
type = '4';
|
|
1122
|
+
}
|
|
1123
|
+
else {
|
|
1124
|
+
return null;
|
|
1125
|
+
}
|
|
1126
|
+
const uid = Number(st.uid);
|
|
1127
|
+
const gid = Number(st.gid);
|
|
1128
|
+
const [devmajor, devminor] = type === '3' || type === '4' ? deviceNumbers(st.rdev) : [0, 0];
|
|
1129
|
+
return {
|
|
1130
|
+
// A directory's name ends in `/`.
|
|
1131
|
+
name: type === '5' && !arcname.endsWith('/') ? `${arcname}/` : arcname,
|
|
1132
|
+
mode: Number(st.mode) & 0o7777,
|
|
1133
|
+
uid,
|
|
1134
|
+
gid,
|
|
1135
|
+
size: type === '0' ? Number(st.size) : 0,
|
|
1136
|
+
mtime: statFloat(mtimeNs?.get(absolute) ?? st.mtimeNs),
|
|
1137
|
+
type,
|
|
1138
|
+
linkname,
|
|
1139
|
+
uname: owners.user(uid),
|
|
1140
|
+
gname: owners.group(gid),
|
|
1141
|
+
devmajor,
|
|
1142
|
+
devminor,
|
|
1143
|
+
};
|
|
1144
|
+
}
|
|
1145
|
+
/**
|
|
1146
|
+
* A failed system call as the `FileError` `fileErrorOf` makes of it -- `ro/b.rcbak.partial:
|
|
1147
|
+
* Permission denied` -- for the CLI's unhandled-error line.
|
|
1148
|
+
*/
|
|
1149
|
+
function asOsError(error) {
|
|
1150
|
+
const err = error;
|
|
1151
|
+
const code = typeof err.code === 'string' ? err.code : '';
|
|
1152
|
+
if (typeof err.errno !== 'number' || code === '' || code.startsWith('Z_'))
|
|
1153
|
+
return error;
|
|
1154
|
+
return (0, format_1.fileErrorOf)(error);
|
|
1155
|
+
}
|
|
1156
|
+
/** Write until the whole buffer is down. */
|
|
1157
|
+
function writeAll(fd, data) {
|
|
1158
|
+
let at = 0;
|
|
1159
|
+
while (at < data.length)
|
|
1160
|
+
at += fs.writeSync(fd, data, at, data.length - at);
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1163
|
+
* A gzip writer at level 9 over an open file, in the established framing: the header
|
|
1164
|
+
* written at once -- FNAME the file's base name in Latin-1 (none when it cannot be), MTIME
|
|
1165
|
+
* now, XFL 2 for level 9, OS 255 -- raw deflate at level 9, and the CRC-32 and length
|
|
1166
|
+
* trailer at `close`.
|
|
1167
|
+
* `zlib.createGzip` writes MTIME 0, OS 3 and no name, and cannot be told otherwise.
|
|
1168
|
+
*/
|
|
1169
|
+
class GzipWriter {
|
|
1170
|
+
constructor(fd, name) {
|
|
1171
|
+
this.fd = fd;
|
|
1172
|
+
this.deflate = zlib.createDeflateRaw({ level: 9 });
|
|
1173
|
+
this.failure = null;
|
|
1174
|
+
this.crc = 0;
|
|
1175
|
+
this.size = 0;
|
|
1176
|
+
const base = path.basename(name);
|
|
1177
|
+
let fname = Array.from(base).every((ch) => ch.codePointAt(0) <= 0xff)
|
|
1178
|
+
? Buffer.from(base, 'latin1')
|
|
1179
|
+
: Buffer.alloc(0);
|
|
1180
|
+
if (fname.length >= 3 && fname.subarray(-3).equals(Buffer.from('.gz'))) {
|
|
1181
|
+
fname = fname.subarray(0, -3);
|
|
1182
|
+
}
|
|
1183
|
+
const header = Buffer.from([0x1f, 0x8b, 8, fname.length > 0 ? FNAME : 0, 0, 0, 0, 0, 2, 0xff]);
|
|
1184
|
+
header.writeUInt32LE(Math.floor(Date.now() / 1000), 4);
|
|
1185
|
+
writeAll(fd, fname.length > 0 ? Buffer.concat([header, fname, Buffer.from([0])]) : header);
|
|
1186
|
+
this.deflate.on('data', (chunk) => {
|
|
1187
|
+
if (this.failure !== null)
|
|
1188
|
+
return;
|
|
1189
|
+
try {
|
|
1190
|
+
writeAll(this.fd, chunk);
|
|
1191
|
+
}
|
|
1192
|
+
catch (error) {
|
|
1193
|
+
this.failure = error;
|
|
1194
|
+
this.deflate.destroy();
|
|
1195
|
+
}
|
|
1196
|
+
});
|
|
1197
|
+
this.deflate.on('error', (error) => {
|
|
1198
|
+
this.failure ?? (this.failure = error);
|
|
1199
|
+
});
|
|
1200
|
+
this.done = new Promise((resolve) => {
|
|
1201
|
+
this.deflate.once('end', resolve);
|
|
1202
|
+
this.deflate.once('close', resolve);
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
/**
|
|
1206
|
+
* One piece of the tar stream. Each is where a Ctrl-C lands (`interruptPoint`):
|
|
1207
|
+
* `writeTarGzFromDir`'s `finally` removes the `.partial` and the staging directory goes
|
|
1208
|
+
* with the unwind.
|
|
1209
|
+
*/
|
|
1210
|
+
async write(chunk) {
|
|
1211
|
+
await (0, exec_1.interruptPoint)();
|
|
1212
|
+
if (this.failure !== null)
|
|
1213
|
+
throw this.failure;
|
|
1214
|
+
this.crc = zlib.crc32(chunk, this.crc);
|
|
1215
|
+
this.size += chunk.length;
|
|
1216
|
+
await new Promise((resolve, reject) => {
|
|
1217
|
+
this.deflate.write(chunk, (error) => error === null || error === undefined ? resolve() : reject(this.failure ?? error));
|
|
1218
|
+
});
|
|
1219
|
+
}
|
|
1220
|
+
async close() {
|
|
1221
|
+
this.deflate.end();
|
|
1222
|
+
await this.done;
|
|
1223
|
+
if (this.failure !== null)
|
|
1224
|
+
throw this.failure;
|
|
1225
|
+
const trailer = Buffer.alloc(8);
|
|
1226
|
+
trailer.writeUInt32LE(this.crc, 0);
|
|
1227
|
+
trailer.writeUInt32LE(this.size % 2 ** 32, 4);
|
|
1228
|
+
writeAll(this.fd, trailer);
|
|
1229
|
+
}
|
|
1230
|
+
destroy() {
|
|
1231
|
+
this.deflate.destroy();
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
/**
|
|
1235
|
+
* Write `dir` as a bundle has always been written: a gzipped tar at `<dest>.partial`, each
|
|
1236
|
+
* entry of `dir` added under its own name in sorted order, then renamed onto `dest`.
|
|
1237
|
+
*
|
|
1238
|
+
* It was its own minimal ustar writer: every member mode 0644 --
|
|
1239
|
+
* directories too, so a bundle unpacked by hand had directories nobody could enter -- uid and
|
|
1240
|
+
* gid 0 with no names, one mtime "now", no PAX records, no padding to the 10240-byte record,
|
|
1241
|
+
* a gzip header with no name and no time, and each file read whole into memory, which is a
|
|
1242
|
+
* RangeError for a mongodump archive over 2 GiB. Now each member is what the `lstat` of its
|
|
1243
|
+
* path makes -- mode, owner and group with their names, the float mtime as a PAX record, a
|
|
1244
|
+
* directory's real mode, a PAX `path` for a long or non-ASCII name, a link as a link -- in
|
|
1245
|
+
* the established order (entries sorted, a directory recursed in place), each file's bytes
|
|
1246
|
+
* streamed; then the end blocks and the record padding, under `GzipWriter`'s framing.
|
|
1247
|
+
* `mtimeNs`, when given, is the nanosecond mtime a staged path should carry: a copy that
|
|
1248
|
+
* keeps mode and mtime sets it exactly, and `utimes` here keeps only microseconds (see
|
|
1249
|
+
* `backup.copyWithMetadata`).
|
|
1250
|
+
*
|
|
1251
|
+
* The compressed bytes are not the ones earlier releases wrote -- Node's zlib is not the
|
|
1252
|
+
* system one -- but what they decompress to is: `targz.test.ts` compares the two tar
|
|
1253
|
+
* streams byte for byte.
|
|
1254
|
+
*/
|
|
1255
|
+
async function writeTarGzFromDir(dir, dest, { mtimeNs } = {}) {
|
|
1256
|
+
const temporary = `${dest}.partial`;
|
|
1257
|
+
try {
|
|
1258
|
+
// The partial file opened for writing before anything else: an --out nobody may write
|
|
1259
|
+
// into is a permission error there, not a WriteStream 'error' that rejected a promise
|
|
1260
|
+
// nothing was awaiting yet and escaped the handler.
|
|
1261
|
+
const fd = fs.openSync(temporary, 'w');
|
|
1262
|
+
let gzip = null;
|
|
1263
|
+
try {
|
|
1264
|
+
gzip = new GzipWriter(fd, temporary);
|
|
1265
|
+
const writer = gzip;
|
|
1266
|
+
const inodes = new Map();
|
|
1267
|
+
const owners = new Owners();
|
|
1268
|
+
let offset = 0;
|
|
1269
|
+
const emit = async (chunk) => {
|
|
1270
|
+
await writer.write(chunk);
|
|
1271
|
+
offset += chunk.length;
|
|
1272
|
+
};
|
|
1273
|
+
const add = async (absolute, arcname) => {
|
|
1274
|
+
const info = gettarinfo(absolute, arcname, inodes, owners, mtimeNs);
|
|
1275
|
+
if (info === null)
|
|
1276
|
+
return;
|
|
1277
|
+
if (info.type === '0') {
|
|
1278
|
+
await addFile(absolute, info, emit);
|
|
1279
|
+
return;
|
|
1280
|
+
}
|
|
1281
|
+
await emit(tarHeaderOf(info));
|
|
1282
|
+
if (info.type !== '5')
|
|
1283
|
+
return;
|
|
1284
|
+
for (const child of fs.readdirSync(absolute).sort(format_1.compareCodePoints)) {
|
|
1285
|
+
await add(path.join(absolute, child), `${arcname}/${child}`);
|
|
1286
|
+
}
|
|
1287
|
+
};
|
|
1288
|
+
for (const item of fs.readdirSync(dir).sort(format_1.compareCodePoints)) {
|
|
1289
|
+
await add(path.join(dir, item), item);
|
|
1290
|
+
}
|
|
1291
|
+
// The end of the archive: two zero blocks, then zeros to the end of the record.
|
|
1292
|
+
await emit(Buffer.alloc(BLOCK * 2));
|
|
1293
|
+
const remainder = offset % RECORDSIZE;
|
|
1294
|
+
if (remainder > 0)
|
|
1295
|
+
await emit(Buffer.alloc(RECORDSIZE - remainder));
|
|
1296
|
+
await writer.close();
|
|
1297
|
+
}
|
|
1298
|
+
finally {
|
|
1299
|
+
gzip?.destroy();
|
|
1300
|
+
fs.closeSync(fd);
|
|
1301
|
+
}
|
|
1302
|
+
fs.renameSync(temporary, dest);
|
|
1303
|
+
}
|
|
1304
|
+
catch (error) {
|
|
1305
|
+
throw asOsError(error);
|
|
1306
|
+
}
|
|
1307
|
+
finally {
|
|
1308
|
+
try {
|
|
1309
|
+
fs.rmSync(temporary, { force: true });
|
|
1310
|
+
}
|
|
1311
|
+
catch {
|
|
1312
|
+
/* already renamed away */
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
/**
|
|
1317
|
+
* A regular file as a member: opened first, then the header and exactly `size` bytes -- a
|
|
1318
|
+
* `FileError` `unexpected end of data` if the file is shorter now -- then the padding to the
|
|
1319
|
+
* block.
|
|
1320
|
+
*/
|
|
1321
|
+
async function addFile(absolute, info, emit) {
|
|
1322
|
+
const fd = fs.openSync(absolute, 'r');
|
|
1323
|
+
try {
|
|
1324
|
+
await emit(tarHeaderOf(info));
|
|
1325
|
+
const buffer = Buffer.allocUnsafe(1024 * 1024);
|
|
1326
|
+
let remaining = info.size;
|
|
1327
|
+
while (remaining > 0) {
|
|
1328
|
+
const want = Math.min(remaining, buffer.length);
|
|
1329
|
+
let have = 0;
|
|
1330
|
+
while (have < want) {
|
|
1331
|
+
const read = fs.readSync(fd, buffer, have, want - have, null);
|
|
1332
|
+
if (read === 0)
|
|
1333
|
+
break;
|
|
1334
|
+
have += read;
|
|
1335
|
+
}
|
|
1336
|
+
if (have < want) {
|
|
1337
|
+
const short = new Error('unexpected end of data');
|
|
1338
|
+
short.name = 'FileError';
|
|
1339
|
+
throw short;
|
|
1340
|
+
}
|
|
1341
|
+
await emit(buffer.subarray(0, have));
|
|
1342
|
+
remaining -= have;
|
|
1343
|
+
}
|
|
1344
|
+
if (info.size % BLOCK !== 0)
|
|
1345
|
+
await emit(Buffer.alloc(BLOCK - (info.size % BLOCK)));
|
|
1346
|
+
}
|
|
1347
|
+
finally {
|
|
1348
|
+
fs.closeSync(fd);
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
/**
|
|
1352
|
+
* What a failure to decompress a bundle IS, in the established words.
|
|
1353
|
+
*
|
|
1354
|
+
* Two cases are told apart, where an earlier version did not. A stream that stops early is an
|
|
1355
|
+
* ended-early error -- "truncated, or still being written" -- while a file that is not gzip at all is
|
|
1356
|
+
* a `TarReadError`. Every `Z_` code was read as truncation, so random bytes were reported
|
|
1357
|
+
* as a bundle still being written.
|
|
1358
|
+
*
|
|
1359
|
+
* A zlib data error that reaches a caller is one met while a member's DATA was read or
|
|
1360
|
+
* skipped: `walk` has already made one met in a header `TarReadError("zlib error: ...")`, and
|
|
1361
|
+
* the gzip header is parsed by `GzipReader`, so `incorrect header check` no longer comes
|
|
1362
|
+
* from zlib at all. Outside a header read the zlib error reaches the manifest read and the
|
|
1363
|
+
* unpacking raw, and both report it as the truncation clause they share with the ended-early error.
|
|
1364
|
+
*/
|
|
1365
|
+
function gzipFailure(error) {
|
|
1366
|
+
const code = String(error.code ?? '');
|
|
1367
|
+
// Read off the object, not through `instanceof Error`: a zlib error is built in Node's own
|
|
1368
|
+
// realm, which is not the realm a Jest sandbox (or any vm context) calls `Error`.
|
|
1369
|
+
const own = error.message;
|
|
1370
|
+
const message = typeof own === 'string' ? own : String(error);
|
|
1371
|
+
// A gzip stream cut short is the ended-early error, in its words.
|
|
1372
|
+
if (code === 'Z_BUF_ERROR') {
|
|
1373
|
+
return {
|
|
1374
|
+
truncated: true,
|
|
1375
|
+
text: 'Compressed file ended before the end-of-stream marker was reached',
|
|
1376
|
+
};
|
|
1377
|
+
}
|
|
1378
|
+
if (code === 'Z_DATA_ERROR')
|
|
1379
|
+
return { truncated: true, text: zlibText(error) };
|
|
1380
|
+
if (code.startsWith('Z_'))
|
|
1381
|
+
return { truncated: true, text: message };
|
|
1382
|
+
return null;
|
|
1383
|
+
}
|