@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.
Files changed (262) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +361 -2
  3. package/bin/ch-local.js +472 -0
  4. package/clients/claude-code/.claude-plugin/plugin.json +6 -0
  5. package/clients/claude-code/.mcp.json +11 -0
  6. package/clients/claude-code/bin/launch.js +235 -0
  7. package/clients/claude-code/bin/win-acl.js +420 -0
  8. package/clients/claude-code/skills/ci-failure-triage/SKILL.md +21 -0
  9. package/clients/claude-code/skills/countinghouse-module/SKILL.md +177 -0
  10. package/clients/claude-code/skills/countinghouse-setup/SKILL.md +74 -0
  11. package/lib/allowlist.js +65 -0
  12. package/lib/aside.js +16 -0
  13. package/lib/atomic.js +45 -0
  14. package/lib/auto-apply.js +245 -0
  15. package/lib/auto-upgrade.js +98 -0
  16. package/lib/availability.js +307 -0
  17. package/lib/ceiling.js +280 -0
  18. package/lib/clean-text.js +20 -0
  19. package/lib/cli-change.js +38 -0
  20. package/lib/client-conditions-fixtures.js +143 -0
  21. package/lib/client-conditions.js +656 -0
  22. package/lib/clients.js +179 -0
  23. package/lib/command-line.js +202 -0
  24. package/lib/conditions-command.js +116 -0
  25. package/lib/control.js +134 -0
  26. package/lib/credential-store.js +414 -0
  27. package/lib/credentials-command.js +344 -0
  28. package/lib/daemon-requests.js +137 -0
  29. package/lib/daemon.js +888 -0
  30. package/lib/describe-module.js +137 -0
  31. package/lib/description-check.js +175 -0
  32. package/lib/failed-json.js +43 -0
  33. package/lib/gc.js +274 -0
  34. package/lib/github-token.js +57 -0
  35. package/lib/handoff-io.js +73 -0
  36. package/lib/held-back.js +116 -0
  37. package/lib/hidden-input.js +64 -0
  38. package/lib/history.js +49 -0
  39. package/lib/home.js +372 -0
  40. package/lib/in-place.js +1036 -0
  41. package/lib/install-plan.js +142 -0
  42. package/lib/install.js +846 -0
  43. package/lib/installed-description.js +153 -0
  44. package/lib/integrity.js +7 -0
  45. package/lib/lock.js +456 -0
  46. package/lib/lockfile-rules.js +98 -0
  47. package/lib/machine.js +20 -0
  48. package/lib/module-credentials.js +403 -0
  49. package/lib/module-record.js +298 -0
  50. package/lib/module-roles.js +44 -0
  51. package/lib/module-seams.js +26 -0
  52. package/lib/module-store.js +202 -0
  53. package/lib/module-values.js +334 -0
  54. package/lib/names.js +31 -0
  55. package/lib/next-steps.js +8 -0
  56. package/lib/node-check.js +56 -0
  57. package/lib/npm-fetch.js +123 -0
  58. package/lib/npm-release.js +99 -0
  59. package/lib/npm-upgrade.js +146 -0
  60. package/lib/npm.js +41 -0
  61. package/lib/outside-edits.js +162 -0
  62. package/lib/own-applied.js +175 -0
  63. package/lib/own-command.js +380 -0
  64. package/lib/own-file.js +120 -0
  65. package/lib/own-record.js +96 -0
  66. package/lib/own-tree.js +336 -0
  67. package/lib/pack-table.js +290 -0
  68. package/lib/packs-command.js +570 -0
  69. package/lib/packs.js +219 -0
  70. package/lib/pins.js +64 -0
  71. package/lib/pipe-probe.js +82 -0
  72. package/lib/plain-read.js +63 -0
  73. package/lib/private-file.js +90 -0
  74. package/lib/private-write.js +33 -0
  75. package/lib/provenance.js +234 -0
  76. package/lib/record.js +82 -0
  77. package/lib/registry.js +22 -0
  78. package/lib/release-pins.js +350 -0
  79. package/lib/release-pins.json +4225 -0
  80. package/lib/release-tags.js +12 -0
  81. package/lib/release.js +36 -0
  82. package/lib/restart-command.js +161 -0
  83. package/lib/rollback.js +227 -0
  84. package/lib/run-liveness.js +15 -0
  85. package/lib/runtime-log.js +287 -0
  86. package/lib/sandbox-support.js +17 -0
  87. package/lib/semver-order.js +40 -0
  88. package/lib/session-socket.js +69 -0
  89. package/lib/session-table.js +238 -0
  90. package/lib/session.js +281 -0
  91. package/lib/settings-command.js +82 -0
  92. package/lib/settings.js +564 -0
  93. package/lib/shim-binary.js +152 -0
  94. package/lib/shim-config.js +159 -0
  95. package/lib/shim-install.js +365 -0
  96. package/lib/sigstore/README.md +44 -0
  97. package/lib/sigstore/npm-keys.json +18 -0
  98. package/lib/sigstore/trusted-root.json +126 -0
  99. package/lib/snippets.js +76 -0
  100. package/lib/start.js +721 -0
  101. package/lib/store-files.js +48 -0
  102. package/lib/store-paths.js +33 -0
  103. package/lib/swap-preload.js +38 -0
  104. package/lib/tar.js +220 -0
  105. package/lib/uninstall.js +314 -0
  106. package/lib/unpack.js +41 -0
  107. package/lib/upgrade-stamp.js +63 -0
  108. package/lib/upgrade.js +281 -0
  109. package/lib/watch.js +40 -0
  110. package/lib/win-acl.js +420 -0
  111. package/node_modules/@sigstore/bundle/LICENSE +202 -0
  112. package/node_modules/@sigstore/bundle/README.md +19 -0
  113. package/node_modules/@sigstore/bundle/dist/build.d.ts +18 -0
  114. package/node_modules/@sigstore/bundle/dist/build.js +100 -0
  115. package/node_modules/@sigstore/bundle/dist/bundle.d.ts +74 -0
  116. package/node_modules/@sigstore/bundle/dist/bundle.js +24 -0
  117. package/node_modules/@sigstore/bundle/dist/error.d.ts +4 -0
  118. package/node_modules/@sigstore/bundle/dist/error.js +25 -0
  119. package/node_modules/@sigstore/bundle/dist/index.d.ts +8 -0
  120. package/node_modules/@sigstore/bundle/dist/index.js +43 -0
  121. package/node_modules/@sigstore/bundle/dist/serialized.d.ts +74 -0
  122. package/node_modules/@sigstore/bundle/dist/serialized.js +49 -0
  123. package/node_modules/@sigstore/bundle/dist/utility.d.ts +14 -0
  124. package/node_modules/@sigstore/bundle/dist/utility.js +2 -0
  125. package/node_modules/@sigstore/bundle/dist/validate.d.ts +7 -0
  126. package/node_modules/@sigstore/bundle/dist/validate.js +199 -0
  127. package/node_modules/@sigstore/bundle/package.json +35 -0
  128. package/node_modules/@sigstore/core/LICENSE +202 -0
  129. package/node_modules/@sigstore/core/README.md +10 -0
  130. package/node_modules/@sigstore/core/dist/asn1/error.d.ts +4 -0
  131. package/node_modules/@sigstore/core/dist/asn1/error.js +24 -0
  132. package/node_modules/@sigstore/core/dist/asn1/index.d.ts +1 -0
  133. package/node_modules/@sigstore/core/dist/asn1/index.js +20 -0
  134. package/node_modules/@sigstore/core/dist/asn1/length.d.ts +3 -0
  135. package/node_modules/@sigstore/core/dist/asn1/length.js +62 -0
  136. package/node_modules/@sigstore/core/dist/asn1/obj.d.ts +14 -0
  137. package/node_modules/@sigstore/core/dist/asn1/obj.js +155 -0
  138. package/node_modules/@sigstore/core/dist/asn1/parse.d.ts +6 -0
  139. package/node_modules/@sigstore/core/dist/asn1/parse.js +124 -0
  140. package/node_modules/@sigstore/core/dist/asn1/tag.d.ts +16 -0
  141. package/node_modules/@sigstore/core/dist/asn1/tag.js +89 -0
  142. package/node_modules/@sigstore/core/dist/crypto.d.ts +6 -0
  143. package/node_modules/@sigstore/core/dist/crypto.js +69 -0
  144. package/node_modules/@sigstore/core/dist/dsse.d.ts +1 -0
  145. package/node_modules/@sigstore/core/dist/dsse.js +29 -0
  146. package/node_modules/@sigstore/core/dist/encoding.d.ts +2 -0
  147. package/node_modules/@sigstore/core/dist/encoding.js +27 -0
  148. package/node_modules/@sigstore/core/dist/index.d.ts +9 -0
  149. package/node_modules/@sigstore/core/dist/index.js +66 -0
  150. package/node_modules/@sigstore/core/dist/json.d.ts +1 -0
  151. package/node_modules/@sigstore/core/dist/json.js +60 -0
  152. package/node_modules/@sigstore/core/dist/oid.d.ts +3 -0
  153. package/node_modules/@sigstore/core/dist/oid.js +20 -0
  154. package/node_modules/@sigstore/core/dist/pem.d.ts +2 -0
  155. package/node_modules/@sigstore/core/dist/pem.js +43 -0
  156. package/node_modules/@sigstore/core/dist/rfc3161/error.d.ts +2 -0
  157. package/node_modules/@sigstore/core/dist/rfc3161/error.js +21 -0
  158. package/node_modules/@sigstore/core/dist/rfc3161/index.d.ts +1 -0
  159. package/node_modules/@sigstore/core/dist/rfc3161/index.js +20 -0
  160. package/node_modules/@sigstore/core/dist/rfc3161/timestamp.d.ts +36 -0
  161. package/node_modules/@sigstore/core/dist/rfc3161/timestamp.js +212 -0
  162. package/node_modules/@sigstore/core/dist/rfc3161/tstinfo.d.ts +12 -0
  163. package/node_modules/@sigstore/core/dist/rfc3161/tstinfo.js +72 -0
  164. package/node_modules/@sigstore/core/dist/stream.d.ts +21 -0
  165. package/node_modules/@sigstore/core/dist/stream.js +117 -0
  166. package/node_modules/@sigstore/core/dist/x509/cert.d.ts +43 -0
  167. package/node_modules/@sigstore/core/dist/x509/cert.js +245 -0
  168. package/node_modules/@sigstore/core/dist/x509/ext.d.ts +41 -0
  169. package/node_modules/@sigstore/core/dist/x509/ext.js +146 -0
  170. package/node_modules/@sigstore/core/dist/x509/index.d.ts +2 -0
  171. package/node_modules/@sigstore/core/dist/x509/index.js +23 -0
  172. package/node_modules/@sigstore/core/dist/x509/sct.d.ts +25 -0
  173. package/node_modules/@sigstore/core/dist/x509/sct.js +158 -0
  174. package/node_modules/@sigstore/core/package.json +31 -0
  175. package/node_modules/@sigstore/protobuf-specs/LICENSE +202 -0
  176. package/node_modules/@sigstore/protobuf-specs/README.md +5 -0
  177. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/envelope.d.ts +44 -0
  178. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/envelope.js +59 -0
  179. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/events.d.ts +64 -0
  180. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/events.js +174 -0
  181. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/api/field_behavior.d.ts +74 -0
  182. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/api/field_behavior.js +141 -0
  183. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/any.d.ts +74 -0
  184. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/any.js +35 -0
  185. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/descriptor.d.ts +1309 -0
  186. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/descriptor.js +2191 -0
  187. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/timestamp.d.ts +113 -0
  188. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/timestamp.js +29 -0
  189. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/dsse.d.ts +23 -0
  190. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/dsse.js +55 -0
  191. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/entry.d.ts +43 -0
  192. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/entry.js +81 -0
  193. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/hashedrekord.d.ts +22 -0
  194. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/hashedrekord.js +56 -0
  195. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/verifier.d.ts +35 -0
  196. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/verifier.js +74 -0
  197. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_bundle.d.ts +146 -0
  198. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_bundle.js +103 -0
  199. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_common.d.ts +304 -0
  200. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_common.js +612 -0
  201. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_monitor.d.ts +33 -0
  202. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_monitor.js +55 -0
  203. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_rekor.d.ts +144 -0
  204. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_rekor.js +137 -0
  205. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_trustroot.d.ts +386 -0
  206. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_trustroot.js +284 -0
  207. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_verification.d.ts +191 -0
  208. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_verification.js +281 -0
  209. package/node_modules/@sigstore/protobuf-specs/dist/index.d.ts +6 -0
  210. package/node_modules/@sigstore/protobuf-specs/dist/index.js +37 -0
  211. package/node_modules/@sigstore/protobuf-specs/dist/rekor/v2/index.d.ts +4 -0
  212. package/node_modules/@sigstore/protobuf-specs/dist/rekor/v2/index.js +35 -0
  213. package/node_modules/@sigstore/protobuf-specs/package.json +35 -0
  214. package/node_modules/@sigstore/verify/README.md +9 -0
  215. package/node_modules/@sigstore/verify/dist/bundle/dsse.d.ts +12 -0
  216. package/node_modules/@sigstore/verify/dist/bundle/dsse.js +44 -0
  217. package/node_modules/@sigstore/verify/dist/bundle/index.d.ts +4 -0
  218. package/node_modules/@sigstore/verify/dist/bundle/index.js +59 -0
  219. package/node_modules/@sigstore/verify/dist/bundle/message.d.ts +13 -0
  220. package/node_modules/@sigstore/verify/dist/bundle/message.js +54 -0
  221. package/node_modules/@sigstore/verify/dist/error.d.ts +16 -0
  222. package/node_modules/@sigstore/verify/dist/error.js +34 -0
  223. package/node_modules/@sigstore/verify/dist/index.d.ts +6 -0
  224. package/node_modules/@sigstore/verify/dist/index.js +28 -0
  225. package/node_modules/@sigstore/verify/dist/key/certificate.d.ts +21 -0
  226. package/node_modules/@sigstore/verify/dist/key/certificate.js +216 -0
  227. package/node_modules/@sigstore/verify/dist/key/index.d.ts +10 -0
  228. package/node_modules/@sigstore/verify/dist/key/index.js +75 -0
  229. package/node_modules/@sigstore/verify/dist/key/sct.d.ts +4 -0
  230. package/node_modules/@sigstore/verify/dist/key/sct.js +78 -0
  231. package/node_modules/@sigstore/verify/dist/policy.d.ts +5 -0
  232. package/node_modules/@sigstore/verify/dist/policy.js +50 -0
  233. package/node_modules/@sigstore/verify/dist/shared.types.d.ts +50 -0
  234. package/node_modules/@sigstore/verify/dist/shared.types.js +2 -0
  235. package/node_modules/@sigstore/verify/dist/timestamp/index.d.ts +11 -0
  236. package/node_modules/@sigstore/verify/dist/timestamp/index.js +24 -0
  237. package/node_modules/@sigstore/verify/dist/timestamp/tsa.d.ts +3 -0
  238. package/node_modules/@sigstore/verify/dist/timestamp/tsa.js +63 -0
  239. package/node_modules/@sigstore/verify/dist/tlog/checkpoint.d.ts +11 -0
  240. package/node_modules/@sigstore/verify/dist/tlog/checkpoint.js +154 -0
  241. package/node_modules/@sigstore/verify/dist/tlog/dsse.d.ts +6 -0
  242. package/node_modules/@sigstore/verify/dist/tlog/dsse.js +106 -0
  243. package/node_modules/@sigstore/verify/dist/tlog/hashedrekord.d.ts +6 -0
  244. package/node_modules/@sigstore/verify/dist/tlog/hashedrekord.js +94 -0
  245. package/node_modules/@sigstore/verify/dist/tlog/index.d.ts +5 -0
  246. package/node_modules/@sigstore/verify/dist/tlog/index.js +92 -0
  247. package/node_modules/@sigstore/verify/dist/tlog/intoto.d.ts +3 -0
  248. package/node_modules/@sigstore/verify/dist/tlog/intoto.js +62 -0
  249. package/node_modules/@sigstore/verify/dist/tlog/merkle.d.ts +3 -0
  250. package/node_modules/@sigstore/verify/dist/tlog/merkle.js +104 -0
  251. package/node_modules/@sigstore/verify/dist/tlog/set.d.ts +3 -0
  252. package/node_modules/@sigstore/verify/dist/tlog/set.js +60 -0
  253. package/node_modules/@sigstore/verify/dist/trust/filter.d.ts +8 -0
  254. package/node_modules/@sigstore/verify/dist/trust/filter.js +23 -0
  255. package/node_modules/@sigstore/verify/dist/trust/index.d.ts +5 -0
  256. package/node_modules/@sigstore/verify/dist/trust/index.js +90 -0
  257. package/node_modules/@sigstore/verify/dist/trust/trust.types.d.ts +29 -0
  258. package/node_modules/@sigstore/verify/dist/trust/trust.types.js +2 -0
  259. package/node_modules/@sigstore/verify/dist/verifier.d.ts +31 -0
  260. package/node_modules/@sigstore/verify/dist/verifier.js +160 -0
  261. package/node_modules/@sigstore/verify/package.json +36 -0
  262. package/package.json +37 -4
@@ -0,0 +1,350 @@
1
+ 'use strict';
2
+ // What a release pins, as data (docs/development/auto-upgrade.md, "The trust
3
+ // chain", step 5): lib/release-pins.json, never code, so a new release's pins
4
+ // are read from its verified tarball's bytes in memory, and nothing of it runs
5
+ // before it is installed. lib/allowlist.js reads this release's own copy.
6
+ //
7
+ // {v: 2,
8
+ // release: {security: {fixes} | null, notes?: [<change>...] | null},
9
+ // self: {repository, repositoryId, ownerId, workflow},
10
+ // shim: {packages: {<platform>-<arch>: "@countinghouse/ch-shim-<platform>-<arch>"}},
11
+ // runtime: {packages: ["@countinghouse/ch-local", "countinghouse"], lockfile: <npm v3 lockfile> | null},
12
+ // packs: {<pack>: {modules: [<package>...]}},
13
+ // modules: {<package>: {credentials: [{variable, origin}], calls: [<package>...],
14
+ // confirmSettings: {<setting>: <its default> | null}}},
15
+ // pins: {<name>: {version, integrity, repository, repositoryId, ownerId, workflow, tag}},
16
+ // floors?: {<package>: <the least version a release may pin>}} (a pack's module or a runtime package)
17
+ //
18
+ // floors (the reviewer on the redaction fixes, A-redact): a home's parts are
19
+ // only as new as its release's pins, so a module whose version carries a fix
20
+ // every release must hold names that version here. No pin may go below it
21
+ // (refused here), and the release step refuses a tarball below it
22
+ // (scripts/describe-release.js). Optional: a document without floors names
23
+ // none. The checklist line (docs/development/release-checklist.md) says
24
+ // why each is there.
25
+ //
26
+ // Version 2 (#413 section 9.3; L5b): packs.<p>.modules replaces packages,
27
+ // and its order means nothing (a module's role is its manifest's). modules
28
+ // DESCRIBES each package of each pack, from its own manifest
29
+ // (lib/describe-module.js), written by the release step
30
+ // (scripts/describe-release.js), never by hand. It is not a list of what is
31
+ // allowed: the store's binding says where a key may go, and the runtime
32
+ // checks calls and settings from the manifests it loads. It exists so that
33
+ // a release can be compared with the one installed before anything is
34
+ // fetched (additions), and install refuses a package that differs from it,
35
+ // so what was shown is what is installed. A pack's credentials and the
36
+ // top-level allowedOrigins are gone. A version 1 document is refused: a
37
+ // breaking change, acceptable only because no release has been published.
38
+ //
39
+ // self is ch-local's own identity. A release can't pin its own digest (its
40
+ // tarball holds this file), so ch-local's pin is the digest its provenance
41
+ // verifies, with this identity and the tag v<version>; no pin may name it.
42
+ // shim names the native shim's package for each of the six targets
43
+ // (${process.platform}-${process.arch}). A pin's tag is exactly its workflow's
44
+ // prefix and its version (lib/provenance.js tagFor), and a package's kind
45
+ // fixes its workflow: the shim's six from shim.yml, every other from
46
+ // publish.yml.
47
+ // The key sets are exact, at every level. A pin names a package of this
48
+ // release (the runtime's or a pack's), a strict MAJOR.MINOR.PATCH version, a
49
+ // sha512, and the identity its provenance must carry (lib/provenance.js).
50
+ // The runtime's lockfile, when present, is npm's v3. Its entries are checked
51
+ // at install, where ch-local's own entry is filled in from its verified
52
+ // digest.
53
+ const MAX_BYTES = 1024 * 1024;
54
+ const STRICT = /^(0|[1-9]\d{0,8})\.(0|[1-9]\d{0,8})\.(0|[1-9]\d{0,8})$/;
55
+ const SHA512 = /^sha512-[A-Za-z0-9+/]{86}==$/;
56
+ const {PACKAGE, PACK} = require('./names');
57
+ const {VARIABLE, isOrigin} = require('./credential-store');
58
+ const SETTING = /^[a-z][a-zA-Z0-9_]{0,63}$/;
59
+ const REPOSITORY = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
60
+ const WORKFLOW = /^\.github\/workflows\/[A-Za-z0-9_.-]+\.ya?ml$/;
61
+ const TAG = /^[A-Za-z0-9_.-]+$/;
62
+ const RUNTIME = ['@countinghouse/ch-local', 'countinghouse'];
63
+ const SHIM_TARGETS = ['darwin-arm64', 'darwin-x64', 'linux-arm64', 'linux-x64', 'win32-arm64', 'win32-x64'];
64
+ const SHIM_WORKFLOW = '.github/workflows/shim.yml';
65
+ const PUBLISH_WORKFLOW = '.github/workflows/publish.yml';
66
+ const {tagFor} = require('./release-tags');
67
+
68
+ const isObject = (x) => x != null && typeof x === 'object' && !Array.isArray(x);
69
+ const keysAre = (o, keys) => isObject(o) && JSON.stringify(Object.keys(o).sort()) === JSON.stringify([...keys].sort());
70
+ const listOf = (a, re, {nonEmpty = false} = {}) => Array.isArray(a) && (!nonEmpty || a.length > 0) && a.every((x) => typeof x === 'string' && re.test(x)) && new Set(a).size === a.length;
71
+
72
+ const deepFreeze = (v) => {
73
+ if (v != null && typeof v === 'object') {
74
+ for (const x of Object.values(v)) deepFreeze(x);
75
+ Object.freeze(v);
76
+ }
77
+ return v;
78
+ };
79
+
80
+ function pinProblem(name, p, shimNames) {
81
+ if (!keysAre(p, ['version', 'integrity', 'repository', 'repositoryId', 'ownerId', 'workflow', 'tag'])) return `the pin for ${name} must be exactly {version, integrity, repository, repositoryId, ownerId, workflow, tag}`;
82
+ if (typeof p.version !== 'string' || !STRICT.test(p.version)) return `the pin for ${name} has no strict MAJOR.MINOR.PATCH version`;
83
+ if (typeof p.integrity !== 'string' || !SHA512.test(p.integrity)) return `the pin for ${name} has no sha512 integrity`;
84
+ if (typeof p.repository !== 'string' || !REPOSITORY.test(p.repository)) return `the pin for ${name} names no repository`;
85
+ for (const k of ['repositoryId', 'ownerId']) if (!Number.isSafeInteger(p[k]) || p[k] <= 0) return `the pin for ${name} has no numeric ${k}`;
86
+ if (typeof p.workflow !== 'string' || !WORKFLOW.test(p.workflow)) return `the pin for ${name} names no workflow`;
87
+ if (typeof p.tag !== 'string' || !TAG.test(p.tag)) return `the pin for ${name} names no tag`;
88
+ // its kind fixes its workflow, and its workflow fixes its tag
89
+ const workflow = shimNames.has(name) ? SHIM_WORKFLOW : PUBLISH_WORKFLOW;
90
+ if (p.workflow !== workflow) return `the pin for ${name} must be built from ${workflow}, not ${p.workflow}`;
91
+ const tag = tagFor(p.workflow, p.version);
92
+ if (p.tag !== tag) return `the pin for ${name}: its tag must be ${tag}, not ${p.tag}`;
93
+ return null;
94
+ }
95
+
96
+ // modules: the descriptions; listed: every package some pack names. Exactly
97
+ // one description for each, none for another; at most one credential (one
98
+ // remote per module in release 1, section 6.1); calls stay inside the
99
+ // release; a confirm setting's default a scalar or null. -> problem | null
100
+ function modulesProblem(modules, listed) {
101
+ if (!isObject(modules)) return 'modules must be an object';
102
+ for (const m of listed) if (!Object.prototype.hasOwnProperty.call(modules, m)) return `modules must describe ${m}`;
103
+ for (const [m, e] of Object.entries(modules)) {
104
+ if (!listed.has(m)) return `modules describes ${m}, which no pack lists`;
105
+ if (!keysAre(e, ['role', 'credentials', 'calls', 'confirmSettings'])) return `the description of ${m} must be exactly {role, credentials, calls, confirmSettings}`;
106
+ if (!(e.role === null || (typeof e.role === 'string' && require('./describe-module').ROLE.test(e.role)))) return `the role in the description of ${m} must be a role or null`;
107
+ if (!Array.isArray(e.credentials) || e.credentials.length > 1) return `the description of ${m} must list at most one credential`;
108
+ for (const c of e.credentials) {
109
+ if (!keysAre(c, ['variable', 'origin']) || typeof c.variable !== 'string' || c.variable.length > 128 || !VARIABLE.test(c.variable) || !isOrigin(c.origin)) {
110
+ return `the credential in the description of ${m} must be {variable, origin} (a variable name, an https origin)`;
111
+ }
112
+ }
113
+ if (!listOf(e.calls, PACKAGE) || JSON.stringify(e.calls) !== JSON.stringify([...e.calls].sort())) return `the description of ${m} must list its calls as package names, each once, sorted`;
114
+ for (const c of e.calls) if (!listed.has(c)) return `${m} calls ${c}, which no pack of this release lists`;
115
+ if (!isObject(e.confirmSettings)) return `the description of ${m} must give its confirm settings as an object`;
116
+ for (const [k, v] of Object.entries(e.confirmSettings)) {
117
+ if (!SETTING.test(k)) return `the description of ${m} names no setting ${JSON.stringify(k)}`;
118
+ if (!(v === null || typeof v === 'boolean' || typeof v === 'string' || (typeof v === 'number' && Number.isFinite(v)))) return `the confirm setting ${k} of ${m} must default to a value or null`;
119
+ }
120
+ }
121
+ return null;
122
+ }
123
+
124
+ // a parsed document, checked -> its problem, or null
125
+ function documentProblem(d) {
126
+ if (isObject(d) && d.v === 1) return 'it is version 1, which this ch-local no longer reads (version 2 describes each module)';
127
+ const KEYS = ['v', 'release', 'self', 'shim', 'runtime', 'packs', 'modules', 'pins'];
128
+ if (!keysAre(d, KEYS) && !keysAre(d, [...KEYS, 'floors'])) return 'must be exactly {v, release, self, shim, runtime, packs, modules, pins}, with an optional floors';
129
+ if (d.v !== 2) return `v must be 2, not ${JSON.stringify(d.v)}`;
130
+ if (!keysAre(d.release, ['security']) && !keysAre(d.release, ['security', 'notes'])) return 'release must be exactly {security}, with an optional notes';
131
+ if (d.release.security !== null && !(keysAre(d.release.security, ['fixes']) && typeof d.release.security.fixes === 'string' && d.release.security.fixes.length <= 500)) {
132
+ return 'release.security must be null or {fixes: <text>}';
133
+ }
134
+ // (the release's own change notes, which upgrade shows before it asks: 1 to
135
+ // 8 lines of plain text, written by the release step from the maintainer's
136
+ // notes file -- scripts/describe-release.js --notes)
137
+ const notes = d.release.notes;
138
+ if (notes !== undefined && notes !== null && !(Array.isArray(notes) && notes.length >= 1 && notes.length <= NOTES_MAX &&
139
+ notes.every((n) => typeof n === 'string' && n.length >= 1 && n.length <= NOTE_MAX && !PLAIN_NOT.test(n)))) {
140
+ return `release.notes must be null or 1 to ${NOTES_MAX} lines of plain text, each at most ${NOTE_MAX} characters`;
141
+ }
142
+ if (!keysAre(d.self, ['repository', 'repositoryId', 'ownerId', 'workflow'])) return 'self must be exactly {repository, repositoryId, ownerId, workflow}';
143
+ if (typeof d.self.repository !== 'string' || !REPOSITORY.test(d.self.repository)) return 'self names no repository';
144
+ for (const k of ['repositoryId', 'ownerId']) if (!Number.isSafeInteger(d.self[k]) || d.self[k] <= 0) return `self has no numeric ${k}`;
145
+ if (typeof d.self.workflow !== 'string' || !WORKFLOW.test(d.self.workflow)) return 'self names no workflow';
146
+ if (d.self.workflow !== PUBLISH_WORKFLOW) return `self must be built from ${PUBLISH_WORKFLOW}`;
147
+ if (!keysAre(d.shim, ['packages']) || !keysAre(d.shim.packages, SHIM_TARGETS)) return `shim must be exactly {packages: {${SHIM_TARGETS.join(', ')}}}`;
148
+ for (const [k, name] of Object.entries(d.shim.packages)) if (name !== `@countinghouse/ch-shim-${k}`) return `shim.packages.${k} must be @countinghouse/ch-shim-${k}`;
149
+ if (!keysAre(d.runtime, ['packages', 'lockfile'])) return 'runtime must be exactly {packages, lockfile}';
150
+ if (JSON.stringify(d.runtime.packages) !== JSON.stringify(RUNTIME)) return `runtime.packages must be ${JSON.stringify(RUNTIME)}`;
151
+ const lock = d.runtime.lockfile;
152
+ if (lock !== null && !(isObject(lock) && lock.lockfileVersion === 3 && isObject(lock.packages) && lock.dependencies === undefined)) {
153
+ return 'runtime.lockfile must be null or an npm v3 lockfile (lockfileVersion 3, packages, no dependencies tree)';
154
+ }
155
+ if (!isObject(d.packs) || Object.keys(d.packs).length === 0) return 'packs must name at least one pack';
156
+ for (const [name, p] of Object.entries(d.packs)) {
157
+ if (!PACK.test(name)) return `the pack name ${JSON.stringify(name)} is not one`;
158
+ if (!keysAre(p, ['modules'])) return `the pack ${name} must be exactly {modules}`;
159
+ if (!listOf(p.modules, PACKAGE, {nonEmpty: true})) return `the pack ${name} must list its modules, each once`;
160
+ }
161
+ const why = modulesProblem(d.modules, new Set(Object.values(d.packs).flatMap((p) => p.modules)));
162
+ if (why != null) return why;
163
+ if (!isObject(d.pins)) return 'pins must be an object';
164
+ const shimNames = new Set(Object.values(d.shim.packages));
165
+ const known = new Set([...d.runtime.packages, ...Object.values(d.packs).flatMap((p) => p.modules), ...shimNames]);
166
+ for (const [name, p] of Object.entries(d.pins)) {
167
+ if (name === RUNTIME[0]) return `no pin may name ${RUNTIME[0]}: its digest is the one its provenance verifies (self)`;
168
+ if (!known.has(name)) return `the pin for ${name} names no package of this release`;
169
+ const why = pinProblem(name, p, shimNames);
170
+ if (why != null) return why;
171
+ }
172
+ if (d.floors !== undefined) {
173
+ if (!isObject(d.floors)) return 'floors must be an object';
174
+ // (a pack's module, or a runtime package: the runtime a home starts
175
+ // carries fixes a release must hold too)
176
+ const listed = new Set([...Object.values(d.packs).flatMap((p) => p.modules), ...d.runtime.packages]);
177
+ for (const [name, v] of Object.entries(d.floors)) {
178
+ if (!listed.has(name)) return `the floor for ${name} names no module of this release's packs, nor its runtime`;
179
+ if (typeof v !== 'string' || !STRICT.test(v)) return `the floor for ${name} is no strict MAJOR.MINOR.PATCH version`;
180
+ const p = d.pins[name];
181
+ if (p != null && belowFloor(p.version, v)) return `the pin for ${name} ${p.version} is below its floor ${v}`;
182
+ }
183
+ }
184
+ return null;
185
+ }
186
+
187
+ // a version below its floor, by semver precedence (lib/semver-order.js: a
188
+ // pre-release comes before its version); what is not a version is below
189
+ // every floor (fail closed: the medium on the floors, 1)
190
+ function belowFloor(version, floor) {
191
+ const c = require('./semver-order').compareVersions(version, floor);
192
+ return c == null || c < 0;
193
+ }
194
+
195
+ const NOTES_MAX = 8;
196
+ const NOTE_MAX = 200;
197
+ const PLAIN_NOT = new RegExp(`[${require('./clean-text').HIDDEN_CLASS}]`, 'u');
198
+ const KNOWN_TOP = new Set(['v', 'release', 'self', 'shim', 'runtime', 'packs', 'modules', 'pins', 'floors']);
199
+ function laterFormat(d) {
200
+ if (!isObject(d)) return false;
201
+ if (typeof d.v === 'number' && Number.isInteger(d.v) && d.v > 2) return true;
202
+ // (a key of release this ch-local doesn't read is a later one's too)
203
+ return d.v === 2 && (Object.keys(d).some((k) => !KNOWN_TOP.has(k)) ||
204
+ (isObject(d.release) && Object.keys(d.release).some((k) => !['security', 'notes'].includes(k))));
205
+ }
206
+
207
+ // a document (an object, or the bytes of one) -> {pins} (frozen) or {problem}
208
+ function parseReleasePins(input) {
209
+ let doc = input;
210
+ if (Buffer.isBuffer(input)) {
211
+ if (input.length > MAX_BYTES) return {problem: `release-pins.json is over ${MAX_BYTES} bytes`};
212
+ try {
213
+ doc = JSON.parse(input.toString('utf8'));
214
+ } catch (e) {
215
+ return {problem: 'release-pins.json is not JSON'};
216
+ }
217
+ }
218
+ const why = documentProblem(doc);
219
+ // later: a format this ch-local doesn't know (a newer v, or a top-level key
220
+ // it doesn't read), so a later ch-local reads it; a broken file is not
221
+ // (the medium on the upgrade fix, A1)
222
+ if (why != null) return {problem: `release-pins.json: ${why}`, later: laterFormat(doc)};
223
+ return {pins: deepFreeze(JSON.parse(JSON.stringify(doc)))};
224
+ }
225
+
226
+ // What `next` adds over `now` that the user must see before it is
227
+ // installed (#413 section 9.3; docs/development/auto-upgrade.md, step 5),
228
+ // for the installed packs; any one holds an automatic upgrade back (section
229
+ // 9.6). now and next are pins-shaped: {packs: {<p>: {modules}}, modules:
230
+ // {<package>: description}} (the running release's, or the home's installed
231
+ // modules, each known only when verifiably the running release's pin:
232
+ // lib/installed-description.js). Anything
233
+ // removed, and a pack not installed, reports nothing.
234
+ // -> [{kind, key, packs, module, line, ...detail}], by pack, module, kind:
235
+ // served a pack the record has withdrawn that next lists again
236
+ // (now.packs.<p>.withdrawn): its modules are added (one per pack)
237
+ // module a module added to an installed pack (one per pack)
238
+ // unknown an installed module now describes as null (not at its
239
+ // verified pin: installed-description), so nothing it changes
240
+ // can be judged: held, and every credential and call of the
241
+ // new version listed, as for a new module
242
+ // role a part no longer one: {was: 'part', now}
243
+ // credential a credential destination new or changed: {variable, origin, was}
244
+ // keyCaller a module newly calls a module that holds a key: {callee}
245
+ // confirm a confirm setting lost its mark, or its default changed:
246
+ // {setting, was: {default}, now: {default} | null}
247
+ // The module-level kinds come once per module, packs naming every installed
248
+ // pack that holds it (C's L4: never one line per pack).
249
+ const KINDS = ['served', 'module', 'unknown', 'role', 'credential', 'keyCaller', 'confirm'];
250
+ // a package's name without its scope
251
+ const short = (m) => String(m).replace(/^@[^/]+\//, '');
252
+ function additions(now, next, installed) {
253
+ const {own} = require('./module-record');
254
+ const out = [];
255
+ const nowModules = (now && now.modules) || {};
256
+ const packsOf = new Map(); // module -> installed packs that hold it in next
257
+ for (const pack of installed) {
258
+ if (!own(next.packs, pack)) continue;
259
+ const had = own(now.packs, pack) ? now.packs[pack].modules : [];
260
+ // a pack the record has withdrawn, listed again: served again, each of
261
+ // its modules new to the running set (the Reviewer's high on L4, 4)
262
+ if (own(now.packs, pack) && now.packs[pack].withdrawn === true) {
263
+ out.push({kind: 'served', key: `served\0${pack}`, packs: [pack], module: '', line: `${pack} is served again: its modules start after the upgrade`});
264
+ }
265
+ for (const m of next.packs[pack].modules) {
266
+ if (!packsOf.has(m)) packsOf.set(m, []);
267
+ packsOf.get(m).push(pack);
268
+ if (!had.includes(m)) out.push({kind: 'module', key: `module\0${pack}\0${m}`, packs: [pack], module: m, line: `a module in ${pack}: ${m}`});
269
+ }
270
+ }
271
+ const S = JSON.stringify;
272
+ for (const [m, packList] of packsOf) {
273
+ const packs = [...packList].sort();
274
+ const b = next.modules[m];
275
+ const a = own(nowModules, m) ? nowModules[m] : null;
276
+ // (installed, but its description unknown: one item, then on as for a
277
+ // new module, so every credential and call of the new version is listed:
278
+ // the medium on L4, 1; the Reviewer's high on L4, 2)
279
+ if (own(nowModules, m) && a === null) {
280
+ out.push({kind: 'unknown', key: `unknown\0${m}`, packs, module: m, line: `${short(m)} as installed can't be verified, so all of what this release has it do is listed`});
281
+ }
282
+ // a part no longer one: its settings, set at a terminal only until now,
283
+ // can be changed in conversation (the Reviewer's high review of #98, 6),
284
+ // those that need no confirmation: a setting marked confirm, or any text
285
+ // setting, needs one, and that is a terminal's while no client is tested
286
+ // to show the form (the operator's ceiling may still keep any at a
287
+ // terminal)
288
+ if (a != null && a.role === 'part' && b.role !== 'part') {
289
+ out.push({kind: 'role', key: `role\0${m}`, packs, module: m, was: 'part', now: b.role, line: `${short(m)} is no longer a part: those of its settings that need no confirmation can now be changed in conversation`});
290
+ }
291
+ const was = a != null && a.credentials.length > 0 ? a.credentials[0] : null;
292
+ for (const c of b.credentials) {
293
+ // (field by field: the Reviewer's high review of #98, 9)
294
+ if (require('./describe-module').sameCredential(was, c)) continue;
295
+ out.push({kind: 'credential', key: `credential\0${m}\0${c.variable}@${c.origin}`, packs, module: m, variable: c.variable, origin: c.origin, was: was == null ? null : {variable: was.variable, origin: was.origin},
296
+ line: was == null ? `${short(m)} sends a key (${c.variable}) to ${c.origin}` : `${short(m)}'s key goes to ${c.origin} as ${c.variable} (was ${was.origin} as ${was.variable})`});
297
+ }
298
+ for (const callee of b.calls) {
299
+ const holds = own(next.modules, callee) && next.modules[callee].credentials.length > 0;
300
+ if (!holds || (a != null && a.calls.includes(callee))) continue;
301
+ out.push({kind: 'keyCaller', key: `keyCaller\0${m}\0${callee}`, packs, module: m, callee, line: `${short(m)} now calls ${short(callee)}, which holds a key`});
302
+ }
303
+ if (a == null) continue;
304
+ for (const setting of Object.keys(a.confirmSettings).sort()) {
305
+ const before = a.confirmSettings[setting];
306
+ if (!own(b.confirmSettings, setting)) {
307
+ out.push({kind: 'confirm', key: `confirm\0${m}\0${setting}`, packs, module: m, setting, was: {default: before}, now: null, line: `${short(m)}'s ${setting} is no longer marked confirm`});
308
+ } else if (S(b.confirmSettings[setting]) !== S(before)) {
309
+ const after = b.confirmSettings[setting];
310
+ out.push({kind: 'confirm', key: `confirm\0${m}\0${setting}`, packs, module: m, setting, was: {default: before}, now: {default: after},
311
+ line: `${short(m)}'s ${setting} (marked confirm) now defaults to ${S(after)} (was ${S(before)})`});
312
+ }
313
+ }
314
+ }
315
+ const order = (x) => [x.packs[0], x.module, KINDS.indexOf(x.kind), x.key];
316
+ return out.sort((x, y) => { const p = order(x); const q = order(y); for (let i = 0; i < p.length; i++) { if (p[i] < q[i]) return -1; if (p[i] > q[i]) return 1; } return 0; });
317
+ }
318
+
319
+ // Identity changes from `now` to `next` (the review of #42): self, or an
320
+ // existing pin's repository, repository or owner ID, or workflow. One for
321
+ // self, the runtime, an installed pack's package or the shim blocks an
322
+ // automatic upgrade (a manual one shows it and asks); one for a pack the
323
+ // user has not installed only is named, since an automatic upgrade never
324
+ // installs that pack. -> {blocking: [line], named: [line]}
325
+ const IDENTITY = ['repository', 'repositoryId', 'ownerId', 'workflow'];
326
+ function identityChanges(now, next, installed) {
327
+ const blocking = [];
328
+ const named = [];
329
+ for (const k of IDENTITY) if (now.self[k] !== next.self[k]) blocking.push(`self's ${k}: ${now.self[k]} -> ${next.self[k]}`);
330
+ const packOf = (name) => Object.keys(next.packs).find((p) => next.packs[p].modules.includes(name));
331
+ const mine = new Set([...next.runtime.packages, ...Object.values(next.shim.packages), ...installed.flatMap((p) => (next.packs[p] != null ? next.packs[p].modules : []))]);
332
+ for (const [name, b] of Object.entries(next.pins)) {
333
+ const a = now.pins[name];
334
+ // a first pin for a name this install uses: who builds it is new, so it
335
+ // blocks as an identity change does (the review of #48)
336
+ if (a == null) {
337
+ if (mine.has(name)) blocking.push(`${name}: pinned for the first time (${b.repository}, ${b.workflow})`);
338
+ continue;
339
+ }
340
+ for (const k of IDENTITY) {
341
+ if (a[k] === b[k]) continue;
342
+ const line = `${name}'s ${k}: ${a[k]} -> ${b[k]}`;
343
+ if (mine.has(name)) blocking.push(line);
344
+ else named.push(`${line} (the pack ${packOf(name)}, not installed)`);
345
+ }
346
+ }
347
+ return {blocking, named};
348
+ }
349
+
350
+ module.exports = {belowFloor, parseReleasePins, additions, identityChanges, modulesProblem, short, MAX_BYTES};