@countinghouse/ch-local 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +361 -2
- package/bin/ch-local.js +472 -0
- package/clients/claude-code/.claude-plugin/plugin.json +6 -0
- package/clients/claude-code/.mcp.json +11 -0
- package/clients/claude-code/bin/launch.js +235 -0
- package/clients/claude-code/bin/win-acl.js +420 -0
- package/clients/claude-code/skills/ci-failure-triage/SKILL.md +21 -0
- package/clients/claude-code/skills/countinghouse-module/SKILL.md +177 -0
- package/clients/claude-code/skills/countinghouse-setup/SKILL.md +74 -0
- package/lib/allowlist.js +65 -0
- package/lib/aside.js +16 -0
- package/lib/atomic.js +45 -0
- package/lib/auto-apply.js +245 -0
- package/lib/auto-upgrade.js +98 -0
- package/lib/availability.js +307 -0
- package/lib/ceiling.js +280 -0
- package/lib/clean-text.js +20 -0
- package/lib/cli-change.js +38 -0
- package/lib/client-conditions-fixtures.js +143 -0
- package/lib/client-conditions.js +656 -0
- package/lib/clients.js +179 -0
- package/lib/command-line.js +202 -0
- package/lib/conditions-command.js +116 -0
- package/lib/control.js +134 -0
- package/lib/credential-store.js +414 -0
- package/lib/credentials-command.js +344 -0
- package/lib/daemon-requests.js +137 -0
- package/lib/daemon.js +888 -0
- package/lib/describe-module.js +137 -0
- package/lib/description-check.js +175 -0
- package/lib/failed-json.js +43 -0
- package/lib/gc.js +274 -0
- package/lib/github-token.js +57 -0
- package/lib/handoff-io.js +73 -0
- package/lib/held-back.js +116 -0
- package/lib/hidden-input.js +64 -0
- package/lib/history.js +49 -0
- package/lib/home.js +372 -0
- package/lib/in-place.js +1036 -0
- package/lib/install-plan.js +142 -0
- package/lib/install.js +846 -0
- package/lib/installed-description.js +153 -0
- package/lib/integrity.js +7 -0
- package/lib/lock.js +456 -0
- package/lib/lockfile-rules.js +98 -0
- package/lib/machine.js +20 -0
- package/lib/module-credentials.js +403 -0
- package/lib/module-record.js +298 -0
- package/lib/module-roles.js +44 -0
- package/lib/module-seams.js +26 -0
- package/lib/module-store.js +202 -0
- package/lib/module-values.js +334 -0
- package/lib/names.js +31 -0
- package/lib/next-steps.js +8 -0
- package/lib/node-check.js +56 -0
- package/lib/npm-fetch.js +123 -0
- package/lib/npm-release.js +99 -0
- package/lib/npm-upgrade.js +146 -0
- package/lib/npm.js +41 -0
- package/lib/outside-edits.js +162 -0
- package/lib/own-applied.js +175 -0
- package/lib/own-command.js +380 -0
- package/lib/own-file.js +120 -0
- package/lib/own-record.js +96 -0
- package/lib/own-tree.js +336 -0
- package/lib/pack-table.js +290 -0
- package/lib/packs-command.js +570 -0
- package/lib/packs.js +219 -0
- package/lib/pins.js +64 -0
- package/lib/pipe-probe.js +82 -0
- package/lib/plain-read.js +63 -0
- package/lib/private-file.js +90 -0
- package/lib/private-write.js +33 -0
- package/lib/provenance.js +234 -0
- package/lib/record.js +82 -0
- package/lib/registry.js +22 -0
- package/lib/release-pins.js +350 -0
- package/lib/release-pins.json +4225 -0
- package/lib/release-tags.js +12 -0
- package/lib/release.js +36 -0
- package/lib/restart-command.js +161 -0
- package/lib/rollback.js +227 -0
- package/lib/run-liveness.js +15 -0
- package/lib/runtime-log.js +287 -0
- package/lib/sandbox-support.js +17 -0
- package/lib/semver-order.js +40 -0
- package/lib/session-socket.js +69 -0
- package/lib/session-table.js +238 -0
- package/lib/session.js +281 -0
- package/lib/settings-command.js +82 -0
- package/lib/settings.js +564 -0
- package/lib/shim-binary.js +152 -0
- package/lib/shim-config.js +159 -0
- package/lib/shim-install.js +365 -0
- package/lib/sigstore/README.md +44 -0
- package/lib/sigstore/npm-keys.json +18 -0
- package/lib/sigstore/trusted-root.json +126 -0
- package/lib/snippets.js +76 -0
- package/lib/start.js +721 -0
- package/lib/store-files.js +48 -0
- package/lib/store-paths.js +33 -0
- package/lib/swap-preload.js +38 -0
- package/lib/tar.js +220 -0
- package/lib/uninstall.js +314 -0
- package/lib/unpack.js +41 -0
- package/lib/upgrade-stamp.js +63 -0
- package/lib/upgrade.js +281 -0
- package/lib/watch.js +40 -0
- package/lib/win-acl.js +420 -0
- package/node_modules/@sigstore/bundle/LICENSE +202 -0
- package/node_modules/@sigstore/bundle/README.md +19 -0
- package/node_modules/@sigstore/bundle/dist/build.d.ts +18 -0
- package/node_modules/@sigstore/bundle/dist/build.js +100 -0
- package/node_modules/@sigstore/bundle/dist/bundle.d.ts +74 -0
- package/node_modules/@sigstore/bundle/dist/bundle.js +24 -0
- package/node_modules/@sigstore/bundle/dist/error.d.ts +4 -0
- package/node_modules/@sigstore/bundle/dist/error.js +25 -0
- package/node_modules/@sigstore/bundle/dist/index.d.ts +8 -0
- package/node_modules/@sigstore/bundle/dist/index.js +43 -0
- package/node_modules/@sigstore/bundle/dist/serialized.d.ts +74 -0
- package/node_modules/@sigstore/bundle/dist/serialized.js +49 -0
- package/node_modules/@sigstore/bundle/dist/utility.d.ts +14 -0
- package/node_modules/@sigstore/bundle/dist/utility.js +2 -0
- package/node_modules/@sigstore/bundle/dist/validate.d.ts +7 -0
- package/node_modules/@sigstore/bundle/dist/validate.js +199 -0
- package/node_modules/@sigstore/bundle/package.json +35 -0
- package/node_modules/@sigstore/core/LICENSE +202 -0
- package/node_modules/@sigstore/core/README.md +10 -0
- package/node_modules/@sigstore/core/dist/asn1/error.d.ts +4 -0
- package/node_modules/@sigstore/core/dist/asn1/error.js +24 -0
- package/node_modules/@sigstore/core/dist/asn1/index.d.ts +1 -0
- package/node_modules/@sigstore/core/dist/asn1/index.js +20 -0
- package/node_modules/@sigstore/core/dist/asn1/length.d.ts +3 -0
- package/node_modules/@sigstore/core/dist/asn1/length.js +62 -0
- package/node_modules/@sigstore/core/dist/asn1/obj.d.ts +14 -0
- package/node_modules/@sigstore/core/dist/asn1/obj.js +155 -0
- package/node_modules/@sigstore/core/dist/asn1/parse.d.ts +6 -0
- package/node_modules/@sigstore/core/dist/asn1/parse.js +124 -0
- package/node_modules/@sigstore/core/dist/asn1/tag.d.ts +16 -0
- package/node_modules/@sigstore/core/dist/asn1/tag.js +89 -0
- package/node_modules/@sigstore/core/dist/crypto.d.ts +6 -0
- package/node_modules/@sigstore/core/dist/crypto.js +69 -0
- package/node_modules/@sigstore/core/dist/dsse.d.ts +1 -0
- package/node_modules/@sigstore/core/dist/dsse.js +29 -0
- package/node_modules/@sigstore/core/dist/encoding.d.ts +2 -0
- package/node_modules/@sigstore/core/dist/encoding.js +27 -0
- package/node_modules/@sigstore/core/dist/index.d.ts +9 -0
- package/node_modules/@sigstore/core/dist/index.js +66 -0
- package/node_modules/@sigstore/core/dist/json.d.ts +1 -0
- package/node_modules/@sigstore/core/dist/json.js +60 -0
- package/node_modules/@sigstore/core/dist/oid.d.ts +3 -0
- package/node_modules/@sigstore/core/dist/oid.js +20 -0
- package/node_modules/@sigstore/core/dist/pem.d.ts +2 -0
- package/node_modules/@sigstore/core/dist/pem.js +43 -0
- package/node_modules/@sigstore/core/dist/rfc3161/error.d.ts +2 -0
- package/node_modules/@sigstore/core/dist/rfc3161/error.js +21 -0
- package/node_modules/@sigstore/core/dist/rfc3161/index.d.ts +1 -0
- package/node_modules/@sigstore/core/dist/rfc3161/index.js +20 -0
- package/node_modules/@sigstore/core/dist/rfc3161/timestamp.d.ts +36 -0
- package/node_modules/@sigstore/core/dist/rfc3161/timestamp.js +212 -0
- package/node_modules/@sigstore/core/dist/rfc3161/tstinfo.d.ts +12 -0
- package/node_modules/@sigstore/core/dist/rfc3161/tstinfo.js +72 -0
- package/node_modules/@sigstore/core/dist/stream.d.ts +21 -0
- package/node_modules/@sigstore/core/dist/stream.js +117 -0
- package/node_modules/@sigstore/core/dist/x509/cert.d.ts +43 -0
- package/node_modules/@sigstore/core/dist/x509/cert.js +245 -0
- package/node_modules/@sigstore/core/dist/x509/ext.d.ts +41 -0
- package/node_modules/@sigstore/core/dist/x509/ext.js +146 -0
- package/node_modules/@sigstore/core/dist/x509/index.d.ts +2 -0
- package/node_modules/@sigstore/core/dist/x509/index.js +23 -0
- package/node_modules/@sigstore/core/dist/x509/sct.d.ts +25 -0
- package/node_modules/@sigstore/core/dist/x509/sct.js +158 -0
- package/node_modules/@sigstore/core/package.json +31 -0
- package/node_modules/@sigstore/protobuf-specs/LICENSE +202 -0
- package/node_modules/@sigstore/protobuf-specs/README.md +5 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/envelope.d.ts +44 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/envelope.js +59 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/events.d.ts +64 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/events.js +174 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/api/field_behavior.d.ts +74 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/api/field_behavior.js +141 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/any.d.ts +74 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/any.js +35 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/descriptor.d.ts +1309 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/descriptor.js +2191 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/timestamp.d.ts +113 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/timestamp.js +29 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/dsse.d.ts +23 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/dsse.js +55 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/entry.d.ts +43 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/entry.js +81 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/hashedrekord.d.ts +22 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/hashedrekord.js +56 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/verifier.d.ts +35 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/verifier.js +74 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_bundle.d.ts +146 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_bundle.js +103 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_common.d.ts +304 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_common.js +612 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_monitor.d.ts +33 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_monitor.js +55 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_rekor.d.ts +144 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_rekor.js +137 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_trustroot.d.ts +386 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_trustroot.js +284 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_verification.d.ts +191 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_verification.js +281 -0
- package/node_modules/@sigstore/protobuf-specs/dist/index.d.ts +6 -0
- package/node_modules/@sigstore/protobuf-specs/dist/index.js +37 -0
- package/node_modules/@sigstore/protobuf-specs/dist/rekor/v2/index.d.ts +4 -0
- package/node_modules/@sigstore/protobuf-specs/dist/rekor/v2/index.js +35 -0
- package/node_modules/@sigstore/protobuf-specs/package.json +35 -0
- package/node_modules/@sigstore/verify/README.md +9 -0
- package/node_modules/@sigstore/verify/dist/bundle/dsse.d.ts +12 -0
- package/node_modules/@sigstore/verify/dist/bundle/dsse.js +44 -0
- package/node_modules/@sigstore/verify/dist/bundle/index.d.ts +4 -0
- package/node_modules/@sigstore/verify/dist/bundle/index.js +59 -0
- package/node_modules/@sigstore/verify/dist/bundle/message.d.ts +13 -0
- package/node_modules/@sigstore/verify/dist/bundle/message.js +54 -0
- package/node_modules/@sigstore/verify/dist/error.d.ts +16 -0
- package/node_modules/@sigstore/verify/dist/error.js +34 -0
- package/node_modules/@sigstore/verify/dist/index.d.ts +6 -0
- package/node_modules/@sigstore/verify/dist/index.js +28 -0
- package/node_modules/@sigstore/verify/dist/key/certificate.d.ts +21 -0
- package/node_modules/@sigstore/verify/dist/key/certificate.js +216 -0
- package/node_modules/@sigstore/verify/dist/key/index.d.ts +10 -0
- package/node_modules/@sigstore/verify/dist/key/index.js +75 -0
- package/node_modules/@sigstore/verify/dist/key/sct.d.ts +4 -0
- package/node_modules/@sigstore/verify/dist/key/sct.js +78 -0
- package/node_modules/@sigstore/verify/dist/policy.d.ts +5 -0
- package/node_modules/@sigstore/verify/dist/policy.js +50 -0
- package/node_modules/@sigstore/verify/dist/shared.types.d.ts +50 -0
- package/node_modules/@sigstore/verify/dist/shared.types.js +2 -0
- package/node_modules/@sigstore/verify/dist/timestamp/index.d.ts +11 -0
- package/node_modules/@sigstore/verify/dist/timestamp/index.js +24 -0
- package/node_modules/@sigstore/verify/dist/timestamp/tsa.d.ts +3 -0
- package/node_modules/@sigstore/verify/dist/timestamp/tsa.js +63 -0
- package/node_modules/@sigstore/verify/dist/tlog/checkpoint.d.ts +11 -0
- package/node_modules/@sigstore/verify/dist/tlog/checkpoint.js +154 -0
- package/node_modules/@sigstore/verify/dist/tlog/dsse.d.ts +6 -0
- package/node_modules/@sigstore/verify/dist/tlog/dsse.js +106 -0
- package/node_modules/@sigstore/verify/dist/tlog/hashedrekord.d.ts +6 -0
- package/node_modules/@sigstore/verify/dist/tlog/hashedrekord.js +94 -0
- package/node_modules/@sigstore/verify/dist/tlog/index.d.ts +5 -0
- package/node_modules/@sigstore/verify/dist/tlog/index.js +92 -0
- package/node_modules/@sigstore/verify/dist/tlog/intoto.d.ts +3 -0
- package/node_modules/@sigstore/verify/dist/tlog/intoto.js +62 -0
- package/node_modules/@sigstore/verify/dist/tlog/merkle.d.ts +3 -0
- package/node_modules/@sigstore/verify/dist/tlog/merkle.js +104 -0
- package/node_modules/@sigstore/verify/dist/tlog/set.d.ts +3 -0
- package/node_modules/@sigstore/verify/dist/tlog/set.js +60 -0
- package/node_modules/@sigstore/verify/dist/trust/filter.d.ts +8 -0
- package/node_modules/@sigstore/verify/dist/trust/filter.js +23 -0
- package/node_modules/@sigstore/verify/dist/trust/index.d.ts +5 -0
- package/node_modules/@sigstore/verify/dist/trust/index.js +90 -0
- package/node_modules/@sigstore/verify/dist/trust/trust.types.d.ts +29 -0
- package/node_modules/@sigstore/verify/dist/trust/trust.types.js +2 -0
- package/node_modules/@sigstore/verify/dist/verifier.d.ts +31 -0
- package/node_modules/@sigstore/verify/dist/verifier.js +160 -0
- package/node_modules/@sigstore/verify/package.json +36 -0
- package/package.json +37 -4
package/lib/uninstall.js
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// ch-local uninstall [--keep-data] (spec §2, "Uninstall"): takes back the client
|
|
3
|
+
// entries it recorded, lists what is in its home and asks once before deleting,
|
|
4
|
+
// keeps the data with --keep-data, and ends by naming the credentials it was
|
|
5
|
+
// given, so they can be revoked. It removes only the names it owns, so a home
|
|
6
|
+
// set to a shared directory by mistake keeps everything else.
|
|
7
|
+
const fs = require('node:fs');
|
|
8
|
+
const path = require('node:path');
|
|
9
|
+
const {layout} = require('./home');
|
|
10
|
+
const {readRecord, removeEntry} = require('./clients');
|
|
11
|
+
const {ALLOWLIST} = require('./allowlist');
|
|
12
|
+
const {readModulesRecord} = require('./module-record');
|
|
13
|
+
const {withLock, withLockFile} = require('./lock');
|
|
14
|
+
const {chLocal} = require('./command-line');
|
|
15
|
+
|
|
16
|
+
// The program and every secret: removed with or without --keep-data.
|
|
17
|
+
// run/ holds each start's own files; modules/ is the runtime's --modulePath;
|
|
18
|
+
// credentials/ the tokens stored with `ch-local credentials add`, never left
|
|
19
|
+
// behind.
|
|
20
|
+
const PROGRAM = ['app', 'app.new', 'app.old', 'bin', 'node', 'auth.json', 'installed.json', 'packs.json', 'modules.json', 'module-store', 'installed-clients.json', 'run', 'modules', 'packs.lock', 'packs.lock.break', 'previous', 'rollback.journal', 'packs', 'upgrade-check.json', 'upgrade-available.json', 'upgrade-check.lock', 'shim.json', 'shim', 'credentials', 'credentials.lock', 'credentials.lock.break',
|
|
21
|
+
// the user's own modules' copies and record (B-own-add; their folders
|
|
22
|
+
// are the user's, elsewhere, and their data is DATA)
|
|
23
|
+
'own', 'own.json'];
|
|
24
|
+
// An interrupted rollback's aside directory (lib/rollback.js), which can hold a whole release.
|
|
25
|
+
// (and run/ as uninstall renames it aside before removing it: a re-run
|
|
26
|
+
// after a removal that failed removes it -- A9)
|
|
27
|
+
const ASIDE = /^(?:previous\.|run\.removing-)[0-9a-f]{8}$/;
|
|
28
|
+
const isProgram = (name) => PROGRAM.includes(name) || ASIDE.test(name);
|
|
29
|
+
// The data: the runtime's store (less its identity secret), module data, usage records, caches.
|
|
30
|
+
// settings.json with the history: a reinstall keeps the user's own
|
|
31
|
+
// tightenings (the review of #18)
|
|
32
|
+
const DATA = ['store', 'module_data', 'usage', 'data', 'history.jsonl', 'settings.json'];
|
|
33
|
+
|
|
34
|
+
// Every recorded pack's credential variables (installed or held), once
|
|
35
|
+
// each: what the running release's pins give the pack, and what its stored
|
|
36
|
+
// modules' remotes declare (the pins take theirs from the same manifests;
|
|
37
|
+
// L5 names the stored credentials by module). Unreadable or absent, every
|
|
38
|
+
// pack this release knows.
|
|
39
|
+
function credentialsOf(where, release) {
|
|
40
|
+
// every variable the store holds a key under (a removed pack's kept key,
|
|
41
|
+
// an earlier name), whether or not the record reads (the Reviewer's cut on
|
|
42
|
+
// #98's round 3; the medium on it, 5)
|
|
43
|
+
let stored = [];
|
|
44
|
+
try {
|
|
45
|
+
const read = require('./credential-store').readStore(where.home);
|
|
46
|
+
stored = read.version === 2 ? [...read.modules.values()].flatMap((vars) => [...vars.keys()]) : [...read.values.keys()];
|
|
47
|
+
} catch { /* no store, or one it cannot read: nothing more to name */ }
|
|
48
|
+
try {
|
|
49
|
+
const record = readModulesRecord(where);
|
|
50
|
+
if (record != null) {
|
|
51
|
+
const {declaredVariables} = require('./describe-module');
|
|
52
|
+
const named = new Set();
|
|
53
|
+
for (const [name, p] of [...Object.entries(record.packs), ...Object.entries(record.held || {})]) {
|
|
54
|
+
for (const v of require('./packs').packVariables(release, name)) named.add(v);
|
|
55
|
+
for (const [m, v] of Object.entries(p.modules)) {
|
|
56
|
+
try {
|
|
57
|
+
// (the store's own reader: the medium on #98's high round, 8)
|
|
58
|
+
const manifest = require('./module-store').readStoredManifest(where, m, v);
|
|
59
|
+
for (const x of declaredVariables(manifest != null && manifest.countinghouse != null ? manifest.countinghouse.remotes : [])) named.add(x);
|
|
60
|
+
} catch { /* not in the store: nothing declared to name */ }
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (const v of stored) named.add(v);
|
|
64
|
+
if (named.size > 0) return [...named];
|
|
65
|
+
}
|
|
66
|
+
} catch { /* unreadable: fall back to every pack's */ }
|
|
67
|
+
// (and the variables an earlier release stored GitHub's token under, which
|
|
68
|
+
// a home may still hold: the medium on L5b, 7)
|
|
69
|
+
return [...new Set([...Object.keys(ALLOWLIST.packs).flatMap((p) => require('./packs').packVariables(ALLOWLIST, p)), ...require('./module-credentials').V1_GITHUB, ...stored])].sort();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function uninstall({keepData = false}, io) {
|
|
73
|
+
const say = (s) => io.stdout.write(s + '\n');
|
|
74
|
+
const where = layout(io.env, io.homedir);
|
|
75
|
+
if (!fs.existsSync(where.home)) { say(`nothing to uninstall: ${where.home} does not exist`); return 0; }
|
|
76
|
+
// Not while an install, add-pack or remove-pack is changing the home; the
|
|
77
|
+
// lock file itself goes with the program.
|
|
78
|
+
// (A9: three phases, so a shared runtime is never removed from under:
|
|
79
|
+
// the listing and the yes under packs.lock; then start.lock, where no
|
|
80
|
+
// daemon is starting, restarting or in its backoff, and a serving one
|
|
81
|
+
// refuses the removal; then, still under start.lock, packs.lock -- the
|
|
82
|
+
// daemon's own order, so a runtime starting under start.lock never waits
|
|
83
|
+
// on this -- and the removal)
|
|
84
|
+
const asked = await withLock(where, io, () => uninstallAsk({keepData}, io, where, say));
|
|
85
|
+
if (asked !== 0) return asked;
|
|
86
|
+
// (packs.lock waited for here: a short holder between the phases, a start's
|
|
87
|
+
// record read, never undoes the yes -- the medium on A9's round, 6)
|
|
88
|
+
// (bounded, as start.lock's wait is: start.lock is held meanwhile, and every
|
|
89
|
+
// daemon start waits on it -- the medium on A9's round 2, 7)
|
|
90
|
+
return underStartLock(where, io, async(bound) => {
|
|
91
|
+
const signal = AbortSignal.timeout(bound);
|
|
92
|
+
let ran = false;
|
|
93
|
+
let gone = false;
|
|
94
|
+
// (the home gone meanwhile refuses, never a removal without the lock:
|
|
95
|
+
// the medium on A9's round 3, 6)
|
|
96
|
+
const code = await withLock(where, io, () => { ran = true; return uninstallLocked({keepData}, io, where, say); }, {wait: true, signal,
|
|
97
|
+
missing: () => { gone = true; io.stderr.write(`ch-local uninstall: ${where.home} is gone; nothing was removed\n`); return 1; }});
|
|
98
|
+
// (only when the wait gave up: never after the removal ran, which says
|
|
99
|
+
// its own lines -- the reviewer's high on #140, L1)
|
|
100
|
+
if (!ran && !gone && signal.aborted && code !== 0) io.stderr.write(`ch-local uninstall: another ch-local was still changing this home after ${bound / 1000} s; nothing was removed. Run \`${chLocal(where, 'uninstall')}\` again when it is done.\n`);
|
|
101
|
+
return code;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// start.lock (<home>/run/daemon/start.lock: the daemon's), waited for at most
|
|
106
|
+
// the daemon's longest hold (a restart's last backoff, its ready wait and an
|
|
107
|
+
// abandon's stop) and 20 s; under it a daemon that may be serving this home
|
|
108
|
+
// (lib/daemon.js daemonServing: its socket proves the secret, or its pid
|
|
109
|
+
// lives) refuses the removal, which it would outlive; a daemon.json whose pid
|
|
110
|
+
// is gone is stale. fn runs under it. -> fn's code, or 1 (said)
|
|
111
|
+
async function underStartLock(where, io, fn) {
|
|
112
|
+
const d = require('./daemon');
|
|
113
|
+
const runDir = path.join(where.home, 'run', 'daemon');
|
|
114
|
+
const refuse = (why) => { io.stderr.write(`ch-local uninstall: ${why}; nothing was removed\n`); return 1; };
|
|
115
|
+
// (the lock needs its directory: made, private, each on its own, never the
|
|
116
|
+
// home -- one removed meanwhile is not made again; one that can't be made
|
|
117
|
+
// refuses, never a check without the lock: the medium on A9's round, 3, 4)
|
|
118
|
+
if (!fs.existsSync(where.home)) return refuse(`${where.home} is gone`);
|
|
119
|
+
// (as the daemon makes them: 0700, or on Windows a private DACL -- the
|
|
120
|
+
// final medium on A9, 5)
|
|
121
|
+
const made = d.ensureRunDir(where.home, {platform: io.platform || process.platform, winAcl: io.winAcl || null});
|
|
122
|
+
if (made.problem != null) return refuse(made.problem);
|
|
123
|
+
const bound = Number.isInteger(io.uninstallLockMs) ? io.uninstallLockMs : uninstallLockBoundMs();
|
|
124
|
+
let ran = false;
|
|
125
|
+
let gone = false;
|
|
126
|
+
const code = await withLockFile(path.join(runDir, 'start.lock'), runDir, io, async() => {
|
|
127
|
+
ran = true;
|
|
128
|
+
let judged = {serving: true, pid: null, by: 'error'};
|
|
129
|
+
// (a check that can't be made counts as serving: refused, nothing removed)
|
|
130
|
+
try { judged = await d.daemonServing(where.home, {platform: io.platform, probePipe: io.probePipe || null, ...(typeof io.alive === 'function' ? {alive: io.alive} : {})}); } catch (err) { judged = {serving: true, pid: null, by: 'error', code: err.code || err.message}; }
|
|
131
|
+
if (judged.serving) {
|
|
132
|
+
// (judged by a live pid alone, or a daemon.json that can't be read: the
|
|
133
|
+
// way out when no client runs, so a reused pid or a damaged file never
|
|
134
|
+
// refuses for good -- the medium on A9's round 2, 1 and 5)
|
|
135
|
+
const file = path.join(runDir, 'daemon.json');
|
|
136
|
+
// (a check that threw gives no advice to remove anything: the medium
|
|
137
|
+
// on A9's round 3, 4)
|
|
138
|
+
// (a check that can name it: the command line, which a countinghouse
|
|
139
|
+
// daemon's contains (`... ch-local.js daemon --home <home>`, then
|
|
140
|
+
// --handoff after a handover) -- ps -p alone prints the process's name, which on
|
|
141
|
+
// Node 24 is MainThread: the reviewer's high on #140, M1)
|
|
142
|
+
const win = io.platform === 'win32' || (io.platform == null && process.platform === 'win32');
|
|
143
|
+
// (-ww: never cut to the terminal's width; and only `ch-local.js
|
|
144
|
+
// daemon` is looked for, never the home, whose quoting differs: a
|
|
145
|
+
// false "not ours" would remove the home under a live daemon -- the
|
|
146
|
+
// medium on the high's round, 1, 2)
|
|
147
|
+
const look = win ? 'Task Manager\'s Details tab, Command line column' : `\`ps -ww -o args= -p ${judged.pid}\``;
|
|
148
|
+
const out = judged.by === 'pid' ? `; if pid ${judged.pid}'s command line (${look}) does not contain \`ch-local.js daemon\`, it is not a countinghouse daemon: remove ${file} and run it again`
|
|
149
|
+
// (the daemon outlives its last client by 10 minutes: L6)
|
|
150
|
+
: judged.by === 'unreadable' ? `; ${file} can't be read: if it still can't be read 10 minutes after the last client closed, remove it and run it again`
|
|
151
|
+
: judged.by === 'error' ? ` (whether it serves could not be checked${judged.code != null ? `: ${judged.code}` : ''})` : '';
|
|
152
|
+
io.stderr.write(`ch-local uninstall: the shared runtime is still serving; nothing was removed. Close the clients that use countinghouse; it stops 10 minutes after the last one closes. Then run \`${chLocal(where, 'uninstall')}\` again${out}.\n`);
|
|
153
|
+
return 1;
|
|
154
|
+
}
|
|
155
|
+
return fn(bound);
|
|
156
|
+
}, {wait: true, what: 'to finish starting or restarting the shared runtime', signal: AbortSignal.timeout(bound),
|
|
157
|
+
missing: () => { gone = true; return refuse(`its run directory ${runDir} is gone`); }});
|
|
158
|
+
// (the bound's line only when the bound fired: the final medium on A9, 3)
|
|
159
|
+
if (!ran && !gone) {
|
|
160
|
+
// (a start.lock no one here can judge -- empty or garbled, as a power
|
|
161
|
+
// loss can leave one, or another host's -- would refuse every run: the
|
|
162
|
+
// way out named, as packs.lock's is -- the reviewer's high, L5)
|
|
163
|
+
const lockFile = path.join(runDir, 'start.lock');
|
|
164
|
+
let holder = null;
|
|
165
|
+
try { holder = JSON.parse(fs.readFileSync(lockFile, 'utf8')); } catch (err) { holder = null; }
|
|
166
|
+
// (and one of this host with no usable pid, which the lock never judges
|
|
167
|
+
// stale: A-140-nits)
|
|
168
|
+
const unjudged = holder == null || typeof holder !== 'object' || holder.host !== require('./lock').lockHost() || !Number.isInteger(holder.pid) || holder.pid <= 0;
|
|
169
|
+
// (named by its host only when it has one: never "on undefined")
|
|
170
|
+
// (and this host's own lock is "there", never "another machine named
|
|
171
|
+
// <this host>" -- the closing pass, L4)
|
|
172
|
+
const named = holder != null && typeof holder === 'object' && typeof holder.host === 'string' && holder.host !== require('./lock').lockHost() ? holder : null;
|
|
173
|
+
io.stderr.write(`ch-local uninstall: the shared runtime has been starting or restarting for ${bound / 1000} s; nothing was removed. Run \`${chLocal(where, 'uninstall')}\` again in a minute${unjudged && fs.existsSync(lockFile) ? require('./lock').removeHint(lockFile, named) : ''}.\n`);
|
|
174
|
+
return 1;
|
|
175
|
+
}
|
|
176
|
+
return code;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function uninstallAsk({keepData}, io, where, say) {
|
|
180
|
+
const record = readRecord(where);
|
|
181
|
+
if (record.entries.length > 0) {
|
|
182
|
+
say('client entries ch-local added:');
|
|
183
|
+
for (const e of record.entries) say(` ${e.client}: ${e.file}`);
|
|
184
|
+
}
|
|
185
|
+
say(`in ${where.home}:`);
|
|
186
|
+
for (const name of fs.readdirSync(where.home).sort()) {
|
|
187
|
+
const dir = fs.statSync(path.join(where.home, name)).isDirectory() ? '/' : '';
|
|
188
|
+
const note = name === 'credentials' ? ' (the stored credentials: removed, with or without --keep-data)' : isProgram(name) ? '' : DATA.includes(name) ? (keepData ? ' (kept: --keep-data)' : '') : " (not ch-local's: left)";
|
|
189
|
+
say(` ${name}${dir}${note}`);
|
|
190
|
+
}
|
|
191
|
+
if (keepData) say(` (the identity secret ${where.identitySecret} is removed with the program)`);
|
|
192
|
+
const answer = String(await io.ask(keepData ? 'Remove the client entries and the program, keeping the data? (yes/no) ' : `Remove the client entries and ch-local's files in ${where.home}? (yes/no) `)).trim().toLowerCase();
|
|
193
|
+
if (answer !== 'yes') { say('nothing was removed'); return 1; }
|
|
194
|
+
return 0;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// after the yes, under start.lock and packs.lock: the home read again (an
|
|
198
|
+
// install may have run between the two), then removed
|
|
199
|
+
async function uninstallLocked({keepData}, io, where, say) {
|
|
200
|
+
const record = readRecord(where);
|
|
201
|
+
const credentials = credentialsOf(where, require('./packs').releaseOf(io));
|
|
202
|
+
// First, under the stored credentials' lock (inside packs.lock: always
|
|
203
|
+
// that order; post-rel cred-race), one short section: the store's
|
|
204
|
+
// variables read again and named, then the store removed, then the record
|
|
205
|
+
// (the keys first; one that can't be removed leaves the record, so the
|
|
206
|
+
// uninstall can be run again: the reviewer's high on #102, 3).
|
|
207
|
+
// So a `credentials add` beside it either lands first, and is removed and
|
|
208
|
+
// named here, or comes after, finds nothing installed and stores nothing.
|
|
209
|
+
// Short, so its heartbeat never goes stale (the medium on cred-race, 1);
|
|
210
|
+
// the rest is removed after, no add able to store anything by then.
|
|
211
|
+
let named = credentials;
|
|
212
|
+
const store = require('./credential-store');
|
|
213
|
+
let first;
|
|
214
|
+
// a removal that failed in the section is said there, with the keys to
|
|
215
|
+
// revoke (some may be gone already, and a second run can no longer read
|
|
216
|
+
// them: the medium on round 1, 1 and 2), and is never an unhandled error
|
|
217
|
+
let said = false;
|
|
218
|
+
// (once the removal has begun, app/ may be gone, and the home's launcher
|
|
219
|
+
// runs from it: the npx form, on this home -- the reviewer's high, L2)
|
|
220
|
+
let removing = false;
|
|
221
|
+
// (on Windows the home said in words beside it: the medium, 3)
|
|
222
|
+
const again = () => (removing ? `${require('./command-line').homePrefix(where, io.platform)}npx ${require('../package.json').name} uninstall` : chLocal(where, 'uninstall'));
|
|
223
|
+
const againNote = () => (removing ? require('./command-line').homeNote(where, io.platform) : '');
|
|
224
|
+
const failed = (what, target, e) => {
|
|
225
|
+
said = true;
|
|
226
|
+
io.stderr.write(`ch-local uninstall: ${what} could not be removed (${e.code || e.message}). Close what has ${target} open and run \`${again()}\`${againNote()} again${named.length > 0 ? `; meanwhile revoke the credentials you gave it: ${named.join(', ')}` : ''}\n`);
|
|
227
|
+
return 1;
|
|
228
|
+
};
|
|
229
|
+
try {
|
|
230
|
+
first = await store.withStoreLock(where.home, io, 'ch-local uninstall', async(held) => {
|
|
231
|
+
named = [...new Set([...credentials, ...credentialsOf(where, require('./packs').releaseOf(io))])];
|
|
232
|
+
store.checkHeld(held, where.home);
|
|
233
|
+
const dir = path.join(where.home, 'credentials');
|
|
234
|
+
// (a reader with the store open, on Windows: a short retry)
|
|
235
|
+
try { fs.rmSync(dir, {recursive: true, force: true, maxRetries: 5}); } catch (e) { return failed('the stored credentials', dir, e); }
|
|
236
|
+
try { fs.rmSync(where.modulesRecord, {force: true, maxRetries: 5}); } catch (e) { return failed('the record (the stored credentials are removed)', where.modulesRecord, e); }
|
|
237
|
+
return 0;
|
|
238
|
+
}, {missing: () => 0});
|
|
239
|
+
} catch (e) {
|
|
240
|
+
if (!(e instanceof store.StoreProblem)) throw e;
|
|
241
|
+
io.stderr.write(`ch-local uninstall: ${e.message.replace(/[;.]? *[Nn]othing was (changed|stored)\.?$/, '')}; nothing was removed\n`);
|
|
242
|
+
return 1;
|
|
243
|
+
}
|
|
244
|
+
if (said) return 1;
|
|
245
|
+
if (first !== 0) { say('nothing was removed'); return 1; }
|
|
246
|
+
for (const e of record.entries) say(removeEntry(e));
|
|
247
|
+
// (the own modules' copies are read-only: writable again before they go)
|
|
248
|
+
try { require('./own-tree').unlockTree(path.join(where.home, 'own')); } catch (e) { /* the removal says it */ }
|
|
249
|
+
const asides = fs.readdirSync(where.home).filter((name) => ASIDE.test(name));
|
|
250
|
+
// (never the store's lock: one there now is a waiter's, which finds
|
|
251
|
+
// nothing installed and removes its own)
|
|
252
|
+
// (a removal that fails -- a file a process still holds open, on
|
|
253
|
+
// Windows -- is said with the way on, never thrown: the medium on A9, 2)
|
|
254
|
+
// (run last: it holds start.lock, which keeps a daemon from starting
|
|
255
|
+
// until everything else is gone -- the medium on A9's round, 2)
|
|
256
|
+
// (and renamed aside first, in one step: start.lock goes with it, so no
|
|
257
|
+
// waiter can take a start.lock in the middle of its removal -- the medium
|
|
258
|
+
// on A9's round 2, 3; one waiting ends, its run directory gone)
|
|
259
|
+
removing = true;
|
|
260
|
+
const order = [...PROGRAM.filter((n) => n !== 'run'), ...asides, ...(keepData ? [] : DATA)];
|
|
261
|
+
for (const name of order) {
|
|
262
|
+
if (name === store.LOCK) continue;
|
|
263
|
+
try { fs.rmSync(path.join(where.home, name), {recursive: true, force: true, maxRetries: 5}); } catch (e) { return failed(name, path.join(where.home, name), e); }
|
|
264
|
+
}
|
|
265
|
+
const runAside = path.join(where.home, `run.removing-${require('node:crypto').randomBytes(4).toString('hex')}`);
|
|
266
|
+
// (retried as the removals are: a scanner holding a file on Windows -- L7)
|
|
267
|
+
let renamed = null;
|
|
268
|
+
for (let i = 0; i < 6; i++) {
|
|
269
|
+
try { fs.renameSync(path.join(where.home, 'run'), runAside); renamed = null; break; } catch (e) {
|
|
270
|
+
renamed = e;
|
|
271
|
+
if (e.code === 'ENOENT') { renamed = null; break; }
|
|
272
|
+
if (i < 5) await new Promise((r) => setTimeout(r, 100 * (i + 1)));
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (renamed != null) return failed('run', path.join(where.home, 'run'), renamed);
|
|
276
|
+
try { fs.rmSync(runAside, {recursive: true, force: true, maxRetries: 5}); } catch (e) { return failed('run', runAside, e); }
|
|
277
|
+
try { fs.rmSync(where.identitySecret, {force: true}); } catch (e) { return failed('the identity secret', where.identitySecret, e); }
|
|
278
|
+
// a waiter's lock, or the temporary file it is made from, goes in a
|
|
279
|
+
// moment (the medium on cred-race, 3)
|
|
280
|
+
const waiter = (name) => name === store.LOCK || name.startsWith(`${store.LOCK}.`);
|
|
281
|
+
for (const until = Date.now() + 2000; Date.now() < until && fs.readdirSync(where.home).some(waiter) && fs.readdirSync(where.home).every(waiter);) {
|
|
282
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
283
|
+
}
|
|
284
|
+
// (a waiter's lock made between the check and the removal leaves the
|
|
285
|
+
// home: said, never an unhandled error after everything else is gone; the
|
|
286
|
+
// reviewer's high on #102, 2)
|
|
287
|
+
let emptied = false;
|
|
288
|
+
let kept = null;
|
|
289
|
+
if (fs.readdirSync(where.home).length === 0) {
|
|
290
|
+
try { fs.rmdirSync(where.home); emptied = true; } catch (e) { kept = e.code || e.message; }
|
|
291
|
+
}
|
|
292
|
+
if (emptied) {
|
|
293
|
+
say(`removed ${where.home}`);
|
|
294
|
+
} else if (kept != null) {
|
|
295
|
+
// (said as what it is: an empty home that could not be removed, the
|
|
296
|
+
// medium on round 1, 7)
|
|
297
|
+
say(`removed ch-local's files; ${where.home} itself could not be removed (${kept}) and is left`);
|
|
298
|
+
} else {
|
|
299
|
+
say(keepData ? `removed the program from ${where.home}; the data is kept` : `removed ch-local's files from ${where.home}; what is not ch-local's is left`);
|
|
300
|
+
}
|
|
301
|
+
if (named.length > 0) say(`Revoke the credentials you gave it: ${named.join(', ')}`);
|
|
302
|
+
// (the plugin is Claude Code's own install: the feature audit, F14)
|
|
303
|
+
say('If you added the Claude Code plugin, remove it in Claude Code too: `claude plugin uninstall ch-packs@ch-packs`');
|
|
304
|
+
return 0;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// start.lock's wait: the daemon's longest hold (a restart's last backoff, its
|
|
308
|
+
// ready wait and an abandon's stop) and 20 s
|
|
309
|
+
function uninstallLockBoundMs() {
|
|
310
|
+
const d = require('./daemon');
|
|
311
|
+
return Math.max(...d.BACKOFF_MS) + d.READY_MS + d.STOP_MS + 20000;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
module.exports = {uninstall, uninstallLockBoundMs};
|
package/lib/unpack.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// A module's package from its tarball, already checked against its pin:
|
|
3
|
+
// unpacked by the strict reader (lib/tar.js: files and directories only, no
|
|
4
|
+
// link) and its package.json checked (the pinned name and version, no npm
|
|
5
|
+
// dependencies). The module store's staging step (lib/module-store.js).
|
|
6
|
+
const fs = require('node:fs');
|
|
7
|
+
const path = require('node:path');
|
|
8
|
+
const {extractPackage} = require('./tar');
|
|
9
|
+
|
|
10
|
+
const tarballName = (name, version) => `${name.replace(/^@/, '').replace('/', '-')}-${version}.tgz`;
|
|
11
|
+
const DEPENDENCY_FIELDS = ['dependencies', 'optionalDependencies', 'peerDependencies', 'bundleDependencies', 'bundledDependencies'];
|
|
12
|
+
|
|
13
|
+
function checkManifest(dir, name, version) {
|
|
14
|
+
let pkg;
|
|
15
|
+
try { pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')); } catch (err) { pkg = null; }
|
|
16
|
+
if (pkg == null || typeof pkg !== 'object') throw new Error(`${name}@${version}: the tarball has no readable package.json`);
|
|
17
|
+
if (pkg.name !== name || pkg.version !== version) throw new Error(`${name}@${version}: the tarball's package.json is ${pkg.name}@${pkg.version}`);
|
|
18
|
+
const declared = DEPENDENCY_FIELDS.filter((f) => pkg[f] != null && (Array.isArray(pkg[f]) ? pkg[f].length > 0 : Object.keys(pkg[f]).length > 0));
|
|
19
|
+
if (declared.length > 0) throw new Error(`${name} declares npm dependencies (${declared.join(', ')}); a pack's packages may have none`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// One package unpacked into dir (which must not exist yet), from its tarball
|
|
23
|
+
// already checked against its pin, and its package.json checked: the pinned
|
|
24
|
+
// name and version, no npm dependencies. The module store's staging step
|
|
25
|
+
// (lib/module-store.js). Throws with a message naming the package; a failure
|
|
26
|
+
// leaves no dir.
|
|
27
|
+
function unpackOne(dir, bytes, name, version) {
|
|
28
|
+
try {
|
|
29
|
+
extractPackage(bytes, dir);
|
|
30
|
+
} catch (err) {
|
|
31
|
+
throw new Error(`${name}@${version}: ${err.message}`);
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
checkManifest(dir, name, version);
|
|
35
|
+
} catch (err) {
|
|
36
|
+
fs.rmSync(dir, {recursive: true, force: true});
|
|
37
|
+
throw err;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = {unpackOne, tarballName, checkManifest};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// <home>/upgrade-check.json (docs/development/auto-upgrade.md): what start's
|
|
3
|
+
// check and the automatic upgrade keep between runs, read as a file only
|
|
4
|
+
// this user wrote (lib/own-file.js) and held to its shape, field by field
|
|
5
|
+
// (the review of #48: a malformed firstSeen let the 72-hour wait fail open).
|
|
6
|
+
// - lastCheck, lastSuccess: ms, a finite number no later than now;
|
|
7
|
+
// - firstSeen: {version: ms}, each a strict version and a finite number no
|
|
8
|
+
// later than now;
|
|
9
|
+
// - refused: [{version, root}], strings;
|
|
10
|
+
// - declined: [version], the versions a rollback left.
|
|
11
|
+
// - announced: [{version, key}], what an automatic upgrade held back and
|
|
12
|
+
// said: each (a version, and a digest of what was said) is said once,
|
|
13
|
+
// never at every check (L4); a version no longer newer is dropped.
|
|
14
|
+
// Anything else in it is dropped. A file that is not private is not read:
|
|
15
|
+
// the record starts again (so the wait starts again), and `problem` says why.
|
|
16
|
+
const path = require('node:path');
|
|
17
|
+
const {readOwnFile, writeOwnFile} = require('./own-file');
|
|
18
|
+
|
|
19
|
+
const STRICT = /^(0|[1-9]\d{0,8})\.(0|[1-9]\d{0,8})\.(0|[1-9]\d{0,8})$/;
|
|
20
|
+
const fileOf = (home) => path.join(home, 'upgrade-check.json');
|
|
21
|
+
const at = (v, now) => (typeof v === 'number' && Number.isFinite(v) && v >= 0 && v <= now ? v : undefined);
|
|
22
|
+
const isObject = (x) => x != null && typeof x === 'object' && !Array.isArray(x);
|
|
23
|
+
|
|
24
|
+
function shaped(doc, now) {
|
|
25
|
+
const out = {firstSeen: {}, refused: [], declined: [], announced: []};
|
|
26
|
+
if (!isObject(doc)) return out;
|
|
27
|
+
for (const k of ['lastCheck', 'lastSuccess']) { const v = at(doc[k], now); if (v !== undefined) out[k] = v; }
|
|
28
|
+
if (isObject(doc.firstSeen)) {
|
|
29
|
+
for (const [v, t] of Object.entries(doc.firstSeen)) { const ok = at(t, now); if (STRICT.test(v) && ok !== undefined) out.firstSeen[v] = ok; }
|
|
30
|
+
}
|
|
31
|
+
if (Array.isArray(doc.refused)) out.refused = doc.refused.filter((r) => isObject(r) && typeof r.version === 'string' && typeof r.root === 'string').map((r) => ({version: r.version, root: r.root}));
|
|
32
|
+
if (Array.isArray(doc.declined)) out.declined = [...new Set(doc.declined.filter((v) => typeof v === 'string' && STRICT.test(v)))];
|
|
33
|
+
if (Array.isArray(doc.announced)) out.announced = doc.announced.filter((e) => isObject(e) && typeof e.version === 'string' && STRICT.test(e.version) && typeof e.key === 'string' && /^[0-9a-f]{16}$/.test(e.key)).map((e) => ({version: e.version, key: e.key}));
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// -> {stamp, problem: null | why it was not read}
|
|
38
|
+
function readStamp(home, {now = Date.now(), platform = process.platform} = {}) {
|
|
39
|
+
let bytes = null;
|
|
40
|
+
try {
|
|
41
|
+
bytes = readOwnFile(fileOf(home), {max: 256 * 1024, platform});
|
|
42
|
+
} catch (e) {
|
|
43
|
+
return {stamp: shaped(null, now), problem: e.message};
|
|
44
|
+
}
|
|
45
|
+
if (bytes == null) return {stamp: shaped(null, now), problem: null};
|
|
46
|
+
let doc = null;
|
|
47
|
+
try { doc = JSON.parse(bytes.toString('utf8')); } catch (e) { return {stamp: shaped(null, now), problem: 'upgrade-check.json is not JSON'}; }
|
|
48
|
+
return {stamp: shaped(doc, now), problem: null};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function writeStamp(home, stamp) {
|
|
52
|
+
writeOwnFile(fileOf(home), JSON.stringify(stamp) + '\n');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// read, changed by fn(stamp), written back
|
|
56
|
+
function updateStamp(home, fn, opts = {}) {
|
|
57
|
+
const {stamp} = readStamp(home, opts);
|
|
58
|
+
fn(stamp);
|
|
59
|
+
writeStamp(home, stamp);
|
|
60
|
+
return stamp;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = {readStamp, writeStamp, updateStamp, shaped};
|