@exodus/stasis 1.0.0-beta.15 → 1.0.0-beta.16

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 (67) hide show
  1. package/README.md +1 -1
  2. package/bin/stasis.js +19 -6
  3. package/package.json +10 -9
  4. package/src/audit-corrections.js +11 -42
  5. package/src/audit.js +113 -233
  6. package/src/babel.js +39 -122
  7. package/src/bundle-from-lockfile.js +9 -21
  8. package/src/bundle-reader.js +5 -9
  9. package/src/bundle.js +0 -3
  10. package/src/cmd/build.js +50 -166
  11. package/src/cmd/bundle-options.js +110 -167
  12. package/src/cmd/bundle.js +853 -1344
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +18 -22
  15. package/src/cmd/output.js +15 -12
  16. package/src/cmd/sbom.js +3 -8
  17. package/src/cmd/tokens.js +10 -23
  18. package/src/define.js +34 -97
  19. package/src/diff.js +72 -128
  20. package/src/esbuild.js +0 -4
  21. package/src/flow.js +10 -25
  22. package/src/installed.js +265 -0
  23. package/src/json-stream.js +4 -9
  24. package/src/loader-mock.js +3 -8
  25. package/src/loader.js +1 -5
  26. package/src/loaders/bash.js +41 -93
  27. package/src/loaders/cargo.js +298 -805
  28. package/src/loaders/foundry.js +109 -285
  29. package/src/loaders/paths.js +13 -13
  30. package/src/loaders/php.js +210 -489
  31. package/src/loaders/rust.js +507 -1430
  32. package/src/loaders/solidity-ownership.js +48 -128
  33. package/src/loaders/solidity.js +39 -122
  34. package/src/loaders/toml.js +7 -17
  35. package/src/lockfile.js +0 -3
  36. package/src/metro-assets.js +9 -33
  37. package/src/metro-resolver.js +20 -47
  38. package/src/metro-transformer.js +0 -5
  39. package/src/metro.js +0 -4
  40. package/src/mock.js +32 -80
  41. package/src/parse.js +2 -5
  42. package/src/prune.js +0 -3
  43. package/src/resolve-fields.js +90 -220
  44. package/src/resolve-node.js +27 -45
  45. package/src/resolve-typescript.js +109 -313
  46. package/src/rollup.js +0 -4
  47. package/src/run-installed.js +10 -0
  48. package/src/sbom.js +52 -128
  49. package/src/scan.js +136 -283
  50. package/src/sixel.js +18 -41
  51. package/src/table.js +14 -35
  52. package/src/validate-notes.d.ts +3 -2
  53. package/src/validate-notes.js +25 -19
  54. package/src/validate.d.ts +38 -11
  55. package/src/validate.js +478 -512
  56. package/src/vfs-bundle/entries.js +40 -57
  57. package/src/vfs-bundle/github.js +77 -121
  58. package/src/vfs-bundle/glob.js +4 -45
  59. package/src/vfs-bundle/lazy.js +27 -56
  60. package/src/vfs-bundle/prisma.js +167 -110
  61. package/src/vfs-bundle/soldeer.js +28 -49
  62. package/src/vfs-bundle/tree.js +295 -318
  63. package/src/vfs-bundle/yarnclean.js +10 -31
  64. package/src/vfs-bundle.js +23 -64
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +75 -231
  67. package/src/installed-commits.js +0 -184
package/src/validate.js CHANGED
@@ -1,71 +1,44 @@
1
- // `stasis validate`: whether a bundle holds what it says it does. Every file of its own code must be
2
- // the repository's at the commit its `repo` names, under the directory it was built in; every file of
3
- // a dependency must be its published package's, fetched with upstream (npm's registry, crates.io),
4
- // which holds each tarball to the integrity the lockfile installing the bundle locks it with; and
5
- // those two must cover every file it holds. A dependency's files a bundle leaves out are expected: it
6
- // holds those its code reaches alone.
7
- //
8
- // Own code is the files of modules naming no ecosystem (dependencyEcosystem): the project's and its
9
- // workspace's packages. The CLI reads the repository's tree with upstream, its tarball held to the
10
- // tree's id; validateBundle takes any reading of it, as file bytes or as git's blob ids (a listing of
11
- // the tree, as a server may read it). A text file whose only difference is a checkout's CRLFs, every
12
- // line's, passes, counted apart (`eol`): not a binary, a shell script or an env file (checkedOutAsLf).
13
- //
14
- // A dependency's version is fetched once, and each installed copy of it (its module) held to its
15
- // files as published, or patched where pnpm patches it (its lockfile's patchedDependencies), as pnpm
16
- // patches them as it installs them: patch-package's applyPatch, which pnpm bundles, each hunk found
17
- // where its header says, else up to FUZZ lines either side, its lines the same but for the space
18
- // they end in. The patch is the lockfile's (`locked`), read of the bundle where it holds a file of its
19
- // hash, else of the repository; with no lockfile read, the one the bundle holds named for the version,
20
- // pnpm's way (`patches/name@version.patch`). Ecosystems upstream fetches no package of by name and
21
- // version alone (Composer's, Soldeer's, a crate from git), versions the lockfile locks from git
22
- // (which npm may build as it installs them) or from elsewhere than their registry, and versions it
23
- // fails to fetch, are left unverified. A version the lockfile doesn't lock, or whose registry's
24
- // tarball isn't the one it locks, fails.
25
- //
26
- // Stasis's empty stub for an import a package disables (EMPTY_MODULE_PATH) is no file of either, and
27
- // is allowed. A vendored crate's `.cargo-checksum.json`, which `cargo vendor` writes and no crate
28
- // holds, is held to the crate instead: its package checksum, and each file's it lists.
29
1
  import { createHash } from 'node:crypto'
30
2
  import { readFileSync } from 'node:fs'
31
3
  import { readFile, readdir } from 'node:fs/promises'
32
4
  import { join, parse as parsePath, posix, relative, sep } from 'node:path'
33
5
 
34
- import { toBase32 } from '@exodus/bytes/base32.js'
35
- import { EMPTY_MODULE_PATH, dependencyEcosystem, hasNodeModulesSegment, isStatFormat, moduleFileKey } from '@exodus/stasis-core/util'
6
+ import { CONFIG_FILE, EMPTY_MODULE_PATH, archiveIntegrity, assertAttested, attestedOf, dependencyEcosystem, hasNodeModulesSegment, integrityHex, isStatFormat, LOCK_FILE, memoized, moduleFileKey, parseLockfilesOption, sha512Of, upsert, withoutBom } from '@exodus/stasis-core/util'
36
7
  import { decompress } from '@preventive/archive/compression.js'
37
8
  import { unpack } from '@preventive/archive/tar.js'
38
9
  import { getCrate } from '@preventive/upstream/cargo.js'
39
10
  import { HttpError, getTarball } from '@preventive/upstream/npm.js'
40
11
  import { parseCargoChecksum, parseCargoLock } from '@preventive/lockfile/cargo.js'
12
+ import { parseComposerLock } from '@preventive/lockfile/composer.js'
41
13
  import { parseNpmLockfile } from '@preventive/lockfile/npm.js'
42
14
  import { parsePnpmLockfile } from '@preventive/lockfile/pnpm.js'
15
+ import { parseSoldeerLockfile } from '@preventive/lockfile/soldeer.js'
43
16
  import { parseYarn1Lockfile } from '@preventive/lockfile/yarn1.js'
44
17
  import { createVfs } from '@preventive/vfs'
18
+ import { Lockfile } from '@exodus/stasis-core/lockfile'
19
+ import { bundledIntegrity, sha512integrity } from '@exodus/stasis-core/state-util'
45
20
 
46
- import { STORE_HASH, resolutionCommit } from './installed-commits.js'
21
+ import { nameAt, STORE_HASH, resolutionCommit } from './installed.js'
47
22
  import { findCargoLock } from './loaders/cargo.js'
48
23
  import { parseFileWithKind } from './parse.js'
49
24
  import { DIM, formatTable, painter } from './table.js'
50
- import { FILE_REASONS, LOCKFILE_ERRORS, OWN_REASONS, noteText } from './validate-notes.js'
25
+ import { FILE_REASONS, LOCKFILE_ERRORS, OWN_REASONS, generatedBy, noteText } from './validate-notes.js'
51
26
  import { lockfileOf, lockfileRoot, vfsHost } from './vfs-bundle/tree.js'
52
27
 
53
- // The files told failing, and the packages told, past which they are counted.
54
28
  export const MAX_TOLD = 500
55
- // Versions fetched at once.
56
29
  const PACKAGES_AT_ONCE = 6
57
- // How many lines either side of where its header says a hunk is looked for, as patch-package does.
30
+ // As patch-package looks for a hunk, this many lines either side of where its header says.
58
31
  const FUZZ = 20
59
- // What a tarball may unpack to, as upstream's tree verification bounds one.
32
+ // As upstream's tree verification bounds a tarball.
60
33
  const MAX_TAR_BYTES = 2 ** 30
34
+ // What `cargo vendor` writes beside a vendored crate, which no crate holds: held to the crate (checksumOf).
35
+ const CARGO_CHECKSUM = '.cargo-checksum.json'
36
+ const EMPTY_INTEGRITY = sha512integrity(new Uint8Array())
61
37
 
62
38
  const utf8 = new TextEncoder()
63
- // A byte order mark kept, as Node's readFileSync keeps it, and patch-package with it: a patch applied
64
- // changes no byte but those its hunks change.
39
+ // A BOM kept, as patch-package keeps it: a patch applied changes no byte but those its hunks change.
65
40
  const asText = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
66
41
 
67
- // A failure to read or apply a patch, by its `code` (patch-unreadable, patch-unsupported,
68
- // patch-does-not-apply, and patch-mismatch for one read not of the hash its lockfile names it by).
69
42
  export class PatchError extends Error {
70
43
  constructor(code) {
71
44
  super(code)
@@ -74,13 +47,6 @@ export class PatchError extends Error {
74
47
  }
75
48
  }
76
49
 
77
- // Why `validate` won't check a file, by its `code`: it is a lockfile (`not-a-bundle`); the bundle's
78
- // own code, or its lockfile, has no repository (`no-repository`) or commit (`no-commit`) to be read
79
- // of, given or recorded, and neither its dependencies alone (`own: false`, --only-deps) nor another
80
- // lockfile or none (`lockfile`, --lockfile, --no-lockfile) were asked for; its commit is in none of
81
- // the repository's own history (`commit-not-in-history`), or that can't be told (`history-unread`,
82
- // historyOf); or no lockfile it can read locks its dependencies (`no-lockfile`, `lockfile-ambiguous`,
83
- // `lockfile-unsupported`, `lockfile-unreadable`).
84
50
  export class ValidateError extends Error {
85
51
  constructor(code, message) {
86
52
  super(message)
@@ -89,16 +55,15 @@ export class ValidateError extends Error {
89
55
  }
90
56
  }
91
57
 
92
- // The ecosystem a module's files are a dependency's of, or undefined for own code: the root's and an
93
- // npm-tagged module's outside node_modules (a workspace package's) are own code whatever they record,
94
- // as a bundle built before only dependencies were tagged may record one.
58
+ const unreadableLockfile = (path, error) => new ValidateError('lockfile-unreadable', `${path}: ${error.message}`)
59
+ const readLockfileText = (read, path) => Promise.try(read, path).catch((error) => {
60
+ throw unreadableLockfile(path, error)
61
+ })
62
+
63
+ // A workspace package outside node_modules is own code, though a bundle built before only dependencies were tagged tags it npm.
95
64
  const moduleEcosystem = (dir, ecosystem) => (dir === '.' || (ecosystem === 'npm' && !hasNodeModulesSegment(dir)) ? undefined : dependencyEcosystem(dir, ecosystem))
96
65
 
97
- // What a bundle's files are, as validation checks them: its own code, `{ path, dir, rel, bytes() }`
98
- // each, and each package version's, `{ ecosystem, name, version, files }`, one with no name or
99
- // version among them; the patches it holds (`patches`, their text by path); and how many files.
100
- // Directories and stat records, which are no files, and Stasis's empty stub are left out.
101
- export function validationPlan(bundle) {
66
+ export function validationPlan(bundle, { lockfile = false } = {}) {
102
67
  const formats = bundle.formats ?? new Map()
103
68
  const own = []
104
69
  const patches = new Map()
@@ -110,26 +75,30 @@ export function validationPlan(bundle) {
110
75
  const path = moduleFileKey(dir, rel)
111
76
  const format = formats.get(path)
112
77
  if (typeof content !== 'string' || format === 'directory' || isStatFormat(format)) continue
113
- const file = { path, dir, rel, format, bytes: () => (format === 'resource:base64' ? Buffer.from(content, 'base64') : utf8.encode(content)) }
78
+ if (lockfile && rel === CARGO_CHECKSUM && ecosystem === 'cargo') continue
79
+ const file = lockfile ? { path, dir, rel, format, integrity: content } : { path, dir, rel, format, bytes: () => (format === 'resource:base64' ? Buffer.from(content, 'base64') : utf8.encode(content)) }
114
80
  if (ecosystem !== undefined) {
115
- if (!packages.has(key)) packages.set(key, { ecosystem, name: info.name ?? '', version: info.version ?? '', files: [] })
81
+ if (!packages.has(key)) packages.set(key, { ecosystem, name: info.name ?? '', version: info.version ?? '', files: [], recorded: new Map(), integrities: new Map(), repos: new Map() })
116
82
  packages.get(key).files.push(file)
117
- } else if (path !== EMPTY_MODULE_PATH || content !== '') {
118
- if (format === 'patch' || path.endsWith('.patch')) patches.set(path, content)
83
+ } else if (path !== EMPTY_MODULE_PATH || content !== (lockfile ? EMPTY_INTEGRITY : '')) {
84
+ if (!lockfile && (format === 'patch' || path.endsWith('.patch'))) patches.set(path, content)
119
85
  own.push(file)
120
86
  }
121
87
  }
88
+ const pkg = packages.get(key)
89
+ if (pkg === undefined) continue
90
+ // In hex, as pnpm's and Cargo's lockfiles spell them.
91
+ if (info.patch) pkg.recorded.set(dir, integrityHex(info.patch.integrity))
92
+ if (info.integrity) pkg.integrities.set(dir, ecosystem === 'cargo' ? integrityHex(info.integrity) : info.integrity)
93
+ if (info.repo) pkg.repos.set(dir, info.repo)
122
94
  }
123
95
  const listed = [...packages.values()]
124
96
  return { files: listed.reduce((sum, pkg) => sum + pkg.files.length, own.length), own, packages: listed, patches }
125
97
  }
126
98
 
127
- // Git's id of a blob of `bytes`.
128
99
  export const gitBlobId = (bytes) => createHash('sha1').update(`blob ${bytes.byteLength}\0`).update(bytes).digest('hex')
129
100
 
130
- // A gzipped tarball's files by their paths, its one top directory left out, as npm unpacks a package
131
- // (and crates.io's crates, and GitHub's trees, have one too); its directories left out, and a symlink
132
- // `{ symlink }`.
101
+ // Its one top directory left out, as npm unpacks a package (crates.io's crates and GitHub's trees have one too).
133
102
  export async function tarballFiles(tarball) {
134
103
  const files = new Map()
135
104
  for (const entry of unpack(await decompress(tarball, 'gzip', { limit: MAX_TAR_BYTES }), { symlinksAbove: Number.MAX_SAFE_INTEGER })) {
@@ -140,28 +109,19 @@ export async function tarballFiles(tarball) {
140
109
  return files
141
110
  }
142
111
 
143
- // Ecosystems upstream fetches a version of by its name and version alone, and that version's files,
144
- // by their paths, as `fetchPackage` gives them; a crate's with its sha256 in hex (`checksum`), which
145
- // a vendored copy's `.cargo-checksum.json` names. With `integrity`, the one a lockfile locks it with
146
- // (an npm version's sha512, a crate's sha256 in hex), upstream holds the tarball to that in place of
147
- // its registry's, and refuses one that differs (fetchFailure's `integrity-mismatch`).
148
112
  export const FETCHED = new Set(['npm', 'cargo'])
149
113
  const sha256 = (bytes) => createHash('sha256').update(bytes).digest('hex')
150
- // A registry's tarball of a version, by its path: npm's, as upstream takes a dist of one, or a mirror's.
151
114
  const tarballPath = (name, version) => `/${name}/-/${name.split('/').at(-1)}-${version}.tgz`
115
+ // With `integrity`, upstream refuses a tarball other than the one it locks.
152
116
  export async function fetchPackage(ecosystem, name, version, integrity) {
153
117
  const tarball = ecosystem === 'npm'
154
118
  ? await getTarball(name, version, integrity === undefined ? undefined : { tarball: `https://registry.npmjs.org${tarballPath(name, version)}`, integrity })
155
119
  : await getCrate(name, version, integrity)
156
120
  const files = new Map([...await tarballFiles(tarball)].filter(([, data]) => data instanceof Uint8Array))
157
- // The checksum upstream held it to, where one was given.
158
121
  if (ecosystem === 'cargo') files.checksum = integrity ?? sha256(tarball)
159
122
  return files
160
123
  }
161
124
 
162
- // Whether a vendored crate's `.cargo-checksum.json` (its `bytes`) is of `crate`, its files as fetched:
163
- // each file it lists the crate's, by its sha256, and its package checksum the crate's where the
164
- // fetch tells it.
165
125
  function checksumOf(bytes, crate) {
166
126
  let parsed
167
127
  try {
@@ -172,12 +132,8 @@ function checksumOf(bytes, crate) {
172
132
  return (crate.checksum === undefined || parsed.checksum === crate.checksum)
173
133
  && Object.entries(parsed.files).every(([path, listed]) => crate.has(path) && sha256(crate.get(path)) === listed)
174
134
  }
175
- const CARGO_CHECKSUM = '.cargo-checksum.json'
176
135
 
177
- // Why a version wasn't checked: none published of it (`not-published`), its registry refusing it
178
- // (`unreadable`), its tarball not the one its lockfile locks (`integrity-mismatch`, as upstream
179
- // refuses it), or another failure (`failed`). A `fetch` of a caller's throws upstream's HttpError,
180
- // re-exported, for the first two.
136
+ // Re-exported for a caller's `fetch` to throw, as fetchFailure reads it.
181
137
  export { HttpError }
182
138
  export function fetchFailure(error) {
183
139
  if (error instanceof HttpError) return error.status === 404 ? 'not-published' : [401, 403].includes(error.status) ? 'unreadable' : 'failed'
@@ -185,14 +141,11 @@ export function fetchFailure(error) {
185
141
  return 'failed'
186
142
  }
187
143
 
188
- // Whether `text` is the patch `hash` names, as pnpm hashes one: of its text with its CRLFs as LFs, a
189
- // sha256 in hex from pnpm 10, an md5 in base32 before; or as it is.
190
- const patchHashes = (text) => new Set([...new Set([text.replaceAll('\r\n', '\n'), text])]
191
- .flatMap((read) => [sha256(read), toBase32(createHash('md5').update(read).digest(), { padding: false }).toLowerCase()]))
144
+ // As pnpm hashes a patch: a sha256 in hex of its text with its CRLFs as LFs, or as it is.
145
+ const patchHashes = (text) => new Set([text.replaceAll('\r\n', '\n'), text].map(sha256))
192
146
  export const isPatchOf = (text, hash) => patchHashes(text).has(hash)
193
147
 
194
- // A path as git writes it, unquoted where quoted (as C writes a string, bytes past ASCII in octal);
195
- // null where it is quoted otherwise.
148
+ // git quotes a path as C writes a string, bytes past ASCII in octal; null where it is quoted otherwise.
196
149
  const ESCAPES = { a: 7, b: 8, t: 9, n: 10, v: 11, f: 12, r: 13, '"': 34, '\\': 92 }
197
150
  function unquoted(path) {
198
151
  if (!path.startsWith('"')) return path
@@ -216,11 +169,9 @@ function unquoted(path) {
216
169
  }
217
170
 
218
171
  const unreadable = () => new PatchError('patch-unreadable')
219
- // A path in a package: relative, of names none empty, `.` or `..`.
220
172
  const inPackage = (path) => path.length > 0 && path.split('/').every((name) => name !== '' && name !== '.' && name !== '..')
221
173
 
222
- // A side's path, as a `---` or `+++` line names it after its prefix, git's tab after one with a space
223
- // in it left out; null for none (`/dev/null`).
174
+ // git writes a tab after a path with a space in it.
224
175
  function sidePath(named, prefix) {
225
176
  const path = unquoted(named.endsWith('\t') ? named.slice(0, -1) : named)
226
177
  if (path === '/dev/null') return null
@@ -228,8 +179,6 @@ function sidePath(named, prefix) {
228
179
  return path.slice(prefix.length)
229
180
  }
230
181
 
231
- // The path a `diff --git` line names on both sides, as git writes it of a file neither renamed nor
232
- // copied.
233
182
  function gitPath(named) {
234
183
  if (named.startsWith('"')) {
235
184
  const quoted = /^("(?:[^"\\]|\\.)*") ("(?:[^"\\]|\\.)*")$/u.exec(named)
@@ -241,8 +190,6 @@ function gitPath(named) {
241
190
  return Number.isInteger(length) && named.startsWith('a/') && named.slice(2 + length) === ` b/${path}` ? path : null
242
191
  }
243
192
 
244
- // The hunk whose `@@` line is at `at` of `lines`, those before `end` read, and the line after it, as
245
- // patch-package reads one.
246
193
  function readHunk(lines, start, end) {
247
194
  let at = start
248
195
  const range = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/u.exec(lines[at++])
@@ -269,10 +216,7 @@ function readHunk(lines, start, end) {
269
216
  return { hunk, next: at }
270
217
  }
271
218
 
272
- // A patch's files as git's diff writes them, as pnpm writes a patch: each one's path, the one it was
273
- // renamed `from`, whether it is `deleted` or `created`, and its hunks. One whose lines end in CRLFs is
274
- // read as patch-package reads it: its headers but their CRs, its hunks' lines with them. Throws a
275
- // PatchError for one it can't read, or of a file's bytes.
219
+ // As pnpm writes a patch; a CRLF one's headers read without their CRs, as patch-package reads them.
276
220
  export function parsePatch(patch) {
277
221
  const lines = patch.split('\n')
278
222
  const end = lines.length - (patch.endsWith('\n') ? 1 : 0)
@@ -321,8 +265,7 @@ export function parsePatch(patch) {
321
265
 
322
266
  const doesNotApply = () => new PatchError('patch-does-not-apply')
323
267
 
324
- // The changes `hunk` makes where its lines are at `start` of `lines`, as patch-package works them out:
325
- // those it finds the same but for the space they end in; else null.
268
+ // Lines the same but for the space they end in match, as patch-package finds a hunk.
326
269
  function changesAt(hunk, lines, start) {
327
270
  let at = start
328
271
  if (at < 0 || lines.length - at < hunk.length) return null
@@ -342,9 +285,6 @@ function changesAt(hunk, lines, start) {
342
285
  return changes
343
286
  }
344
287
 
345
- // `text` with `hunks` applied as patch-package applies them: each found in the text as it was, where
346
- // its header says, else as near as FUZZ lines either side, the nearer first; then its changes made in
347
- // turn, past the lines those before added or removed.
348
288
  function applied(text, hunks) {
349
289
  const lines = text.split('\n')
350
290
  const changes = hunks.flatMap((hunk) => {
@@ -366,8 +306,7 @@ function applied(text, hunks) {
366
306
  return lines.join('\n')
367
307
  }
368
308
 
369
- // A version's `files` (bytes by path) with `patch` applied; a file it changes the text of must be
370
- // UTF-8 text. One it creates holds its first hunk's first lines, as patch-package writes it.
309
+ // A file it creates holds its first hunk's first lines, as patch-package writes it.
371
310
  export function patchedFiles(files, patch) {
372
311
  const next = new Map(files)
373
312
  for (const file of patch) {
@@ -402,24 +341,23 @@ export function patchedFiles(files, patch) {
402
341
  return next
403
342
  }
404
343
 
405
- const equal = (a, b) => Buffer.from(a.buffer, a.byteOffset, a.byteLength).equals(b)
344
+ const asBuffer = (bytes) => Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
345
+ const equal = (a, b) => asBuffer(a).equals(b)
346
+ const isFile = (file, bytes) => (file.integrity === undefined ? equal(file.bytes(), bytes) : sha512integrity(bytes) === file.integrity)
406
347
  // Formats a CR before each line's end changes the meaning of: a binary's bytes, which no checkout
407
348
  // converts, a shell script's commands and an env file's values, each its last word's own.
408
349
  const CR_CHANGES = new Set(['resource:base64', 'shell', 'env'])
409
- // `bytes`, of a file of `format`, as a checkout with CRLF line endings writes it, as LF text: every
410
- // line's end a CRLF, as git converts a text file whole. Else undefined.
411
- function checkedOutAsLf(bytes, format) {
350
+ // `bytes` as the other side of a checkout with CRLF line endings has them, as git converts a text file whole.
351
+ function checkedOut(bytes, format, toLf) {
412
352
  if (CR_CHANGES.has(format)) return undefined
413
- const text = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString('latin1')
414
- return text.includes('\r\n') && !/(?<!\r)\n/u.test(text) ? Buffer.from(text.replaceAll('\r\n', '\n'), 'latin1') : undefined
353
+ const text = asBuffer(bytes).toString('latin1')
354
+ if (toLf) return text.includes('\r\n') && !/(?<!\r)\n/u.test(text) ? Buffer.from(text.replaceAll('\r\n', '\n'), 'latin1') : undefined
355
+ return text.includes('\n') && !text.includes('\r\n') ? Buffer.from(text.replaceAll('\n', '\r\n'), 'latin1') : undefined
415
356
  }
416
357
  const packageKey = ({ ecosystem, name, version }) => `${ecosystem}:${name}@${version}`
417
358
  // A registry's version, as pnpm names one in its store's directories (no `file+lib`, say).
418
359
  const VERSION = /^\d+\.\d+\.\d+(?:-[\w.-]+)?(?:\+[\w.-]+)?$/u
419
- // The name an npm package's copy is installed as, by its path (after its last `node_modules/`, as
420
- // Node resolves it), and the name and version of pnpm's store directory it is in, where it is
421
- // (`node_modules/.pnpm/<name>@<version>/node_modules/<name>`, the name's `/` a `+`, its peers or patch
422
- // after an `_`), as far as it names them: none where pnpm hashed a long one or one with uppercase.
360
+ // pnpm's store directory names a version `<name, its / a +>@<version>`, but where it hashed a long one or one with uppercase.
423
361
  function installedAt(dir) {
424
362
  const segments = dir.split('/')
425
363
  const at = segments.lastIndexOf('node_modules')
@@ -432,11 +370,7 @@ function installedAt(dir) {
432
370
  const version = store.slice(split + 1).split('_')[0]
433
371
  return { as, store: { name: name.startsWith('@') ? name.replace('+', '/') : name, ...(VERSION.test(version) && { version }) } }
434
372
  }
435
- // Whether an npm package's copy at `dir` is installed where the bundle has it: the name its path
436
- // installs it as its own, but an alias its lockfile has (npm's at that location, yarn's or pnpm's of
437
- // any); pnpm's store directory above it naming it; and, where its lockfile locks each location (npm's),
438
- // that version there. `true`, else `false` where its path or lockfile names another, else `undefined`
439
- // where it is installed as another name no lockfile read tells of (none read, say).
373
+ // Undefined for a copy installed under an alias no lockfile read tells of.
440
374
  function placed(pkg, dir, locked) {
441
375
  const { as, store } = installedAt(dir)
442
376
  const named = `${pkg.name}@${pkg.version}`
@@ -447,63 +381,49 @@ function placed(pkg, dir, locked) {
447
381
  return locked?.aliased?.(as, named)
448
382
  }
449
383
  const byText = (key) => (a, b) => (key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0)
450
- // Files told by kind, the worst first, then by what they are of, own code first, then by their paths;
451
- // packages by status, the worst first, then by their keys: as many as MAX_TOLD keep the worst.
384
+ // The worst first, own code's files before a package's, so that as many as MAX_TOLD keep the worst.
452
385
  const KINDS = ['differs', 'unexpected', 'unverified']
453
386
  const STATUSES = ['invalid', 'unverified', 'valid']
454
387
  const byProblem = (a, b) => KINDS.indexOf(a.kind) - KINDS.indexOf(b.kind) || (a.of === 'own' ? 0 : 1) - (b.of === 'own' ? 0 : 1)
455
388
  || byText((problem) => problem.of)(a, b) || byText((problem) => problem.path)(a, b)
456
389
  const byPackage = (a, b) => STATUSES.indexOf(a.status) - STATUSES.indexOf(b.status) || byText(packageKey)(a, b)
457
- // The name pnpm gives a version's patch file, `@scope__name@1.0.0.patch`.
458
390
  const pnpmPatchName = (name, version) => `${name.replace('/', '__')}@${version}.patch`
459
391
 
460
- // The patches a plan holds (`{ path, text }`), by each hash pnpm may name one by and by their file names.
461
392
  function patchesOf(plan) {
462
393
  const byHash = new Map()
463
394
  const byName = new Map()
464
395
  for (const [path, text] of plan.patches) {
465
396
  const held = { path, text }
397
+ const name = posix.basename(path)
466
398
  for (const hash of patchHashes(text)) if (!byHash.has(hash)) byHash.set(hash, held)
467
- if (!byName.has(posix.basename(path))) byName.set(posix.basename(path), held)
399
+ if (!byName.has(name)) byName.set(name, held)
468
400
  }
469
401
  return { byHash, byName }
470
402
  }
471
403
 
472
- // The patch an npm version is held to, and its files patched with it: the one `locked` locks it with,
473
- // of the bundle's file of its hash (`held`, patchesOf), else read by `locked.read`; with no lockfile
474
- // read (`locked.of` answering undefined), the bundle's named for it. None where it is patched by none;
475
- // told with its `error` where it couldn't be read or applied. A patch the bundle holds is own code,
476
- // taken as the repository has it alone (`verdicts`, a promise of what checking own code found of each
477
- // file): refused where it isn't (`patch-not-in-repository`), and where that couldn't be told, its
478
- // files patched `unverified`.
479
- async function patchOf(held, pkg, files, locked, verdicts) {
404
+ // A bundle's patch is own code: refused where the repository holds it otherwise (`verdicts`).
405
+ async function patchOf(held, pkg, files, locked, verdicts, recorded) {
480
406
  const lock = locked?.of(pkg.name, pkg.version)
407
+ const hash = lock === undefined ? recorded : lock?.hash
481
408
  let patch
482
409
  let text
483
410
  try {
484
- if (lock) {
485
- const bundled = held.byHash.get(lock.hash)
486
- if (bundled) {
487
- patch = { hash: lock.hash, path: bundled.path, from: 'bundle' }
488
- text = bundled.text
489
- } else if (lock.path) {
490
- patch = { hash: lock.hash, path: lock.path, from: locked.from }
491
- text = await locked.read(lock.path)
492
- // Held to the hash the lockfile names it by, as the bundle's are found by it.
493
- if (!isPatchOf(text, lock.hash)) throw new PatchError('patch-mismatch')
494
- } else {
495
- // Named by its hash alone (pnpm 11): the file of that hash in a patches directory.
496
- const found = await locked.locate?.(lock)
497
- if (!found) return { patch: { hash: lock.hash, error: 'patch-not-found' }, patched: null }
498
- patch = { hash: lock.hash, path: found.path, from: locked.from }
499
- ;({ text } = found)
500
- }
501
- } else if (lock === undefined) {
502
- const named = held.byName.get(pnpmPatchName(pkg.name, pkg.version))
503
- if (!named) return { patched: null }
504
- patch = { path: named.path, from: 'bundle' }
505
- text = named.text
506
- } else return { patched: null }
411
+ const bundled = hash !== undefined ? held.byHash.get(hash) : lock === undefined && held.byName.get(pnpmPatchName(pkg.name, pkg.version))
412
+ if (bundled) {
413
+ patch = { ...(hash !== undefined && { hash }), path: bundled.path, from: 'bundle' }
414
+ text = bundled.text
415
+ } else if (lock?.path) {
416
+ patch = { hash, path: lock.path, from: locked.from }
417
+ text = await locked.read(lock.path)
418
+ // Held to the hash the lockfile names it by, as the bundle's are found by it.
419
+ if (!isPatchOf(text, hash)) throw new PatchError('patch-mismatch')
420
+ } else {
421
+ // Named by its hash alone (pnpm 11): the file of that hash in a patches directory.
422
+ const found = lock ? await locked.locate?.(lock) : undefined
423
+ if (!found) return hash === undefined ? { patched: null } : { patch: { hash, error: 'patch-not-found' }, patched: null }
424
+ patch = { hash, path: found.path, from: locked.from }
425
+ ;({ text } = found)
426
+ }
507
427
  let unverified = false
508
428
  if (patch.from === 'bundle') {
509
429
  const verdict = (await verdicts).get(patch.path)
@@ -517,14 +437,12 @@ async function patchOf(held, pkg, files, locked, verdicts) {
517
437
  }
518
438
  }
519
439
 
520
- // A path git can have in a tree, which upstream lists: no part empty, `.`, `..` or `.git`, as upstream's
521
- // isRepoPath (src/args.js) has it, which it doesn't export.
440
+ // A path git can have in a tree, as upstream's isRepoPath (src/args.js), which it doesn't export, has it.
522
441
  const inTree = (path) => path.length <= 4096 && path.isWellFormed() && !/\p{Cc}/u.test(path) && path.split('/').every((part) => !['', '.', '..', '.git'].includes(part.toLowerCase()))
523
442
 
524
- // `name` in the directory `dir` of the repository, '' its root.
525
443
  const pathIn = (dir, name) => (dir ? `${dir}/${name}` : name)
444
+ const repoDir = (dir) => (dir === '.' ? '' : dir)
526
445
 
527
- // `items` each run through `run`, `parallel` at a time.
528
446
  async function eachOf(items, parallel, run) {
529
447
  let next = 0
530
448
  await Promise.all(Array.from({ length: Math.min(parallel, items.length) }, async () => {
@@ -533,67 +451,32 @@ async function eachOf(items, parallel, run) {
533
451
  }))
534
452
  }
535
453
 
536
- // The bundle `plan` validated (validationPlan): its own code against `own`, the repository's files at
537
- // its commit (`entries`, by their paths from the repository's root, each `{ data }` (bytes), `{ sha }`
538
- // (git's blob id), `{ symlink }` or `{ submodule: true }`; `cut` where some were left unread), at
539
- // `repository`, `commit` and `directory`, the one it was built in; or `{ reason }` it wasn't checked,
540
- // with `entries` where the patches the bundle holds are checked all the same (`skipped`, --only-deps);
541
- // or a promise of any, the packages fetched meanwhile.
542
- // Each package fetched with `fetch` (fetchPackage), PACKAGES_AT_ONCE at a time, those left past
543
- // `deadline` unverified; each as its lockfile locks it (`locked`, lockedBy): `lock(ecosystem, name,
544
- // version)`, how (undefined where no lockfile of its ecosystem was read, null where it locks none):
545
- // one of its registry fetched with its `integrity`, failing where upstream refuses the tarball, or
546
- // where none is locked; one from a directory of the repository held to that directory's files there
547
- // (`tree(directory)`, where the repository is read; else unverified); one from git or elsewhere left
548
- // unverified. An npm version held to its files patched where pnpm patches it (`of(name, version)`,
549
- // the patch it locks it with, null for none, undefined where no lockfile tells; `read(path)`, the
550
- // patch's text, read of `from`), a patch the bundle holds taken as the repository has it alone.
551
- //
552
- // What it tells: its `status`, `valid` where every file it was to check was checked and is as it
553
- // should be, `invalid` where one differs (`differs`) or is neither the repository's nor its package's
554
- // (`unexpected`), a version fails its lockfile (`lockFailures`), or a copy is installed where its path
555
- // or its lockfile names another (`misplaced`), else `incomplete`; the lockfiles
556
- // held to (`lockfiles`, null for none); whether its own code was checked (`own`); how many files of
557
- // each kind and outcome (`counts`, own files not checked as asked `skipped`); each file failing, and
558
- // each own one left unverified, with what it is of (`own`, or `ecosystem:name@version`), as many as
559
- // MAX_TOLD (`problems`, how many more `moreProblems`); and each version not wholly valid or patched
560
- // (`packages`), a patched one's patch with, where it is valid, whether a copy of it holds its files
561
- // patched (`applied`), and one installed from a directory of the repository its `directory`.
562
- export async function validateBundle(plan, own, { fetch = fetchPackage, locked = null, deadline = Number.POSITIVE_INFINITY, now = Date.now } = {}) {
454
+ export async function validateBundle(plan, own, { fetch = fetchPackage, locked: lockfiles = null, generated, submodules, deadline = Number.POSITIVE_INFINITY, now = Date.now } = {}) {
455
+ const units = plan.packages.flatMap((pkg) => lockfiles?.split?.(pkg) ?? [{ pkg, locked: lockfiles }])
563
456
  const problems = []
564
- const counts = { files: plan.files, own: plan.own.length, dependencies: plan.files - plan.own.length, matched: 0, eol: 0, differs: 0, unexpected: 0, unverified: 0,
565
- skipped: 0, packages: plan.packages.length, validPackages: 0, lockFailures: 0, misplaced: 0 }
457
+ const counts = { files: plan.files, own: plan.own.length, dependencies: plan.files - plan.own.length, matched: 0, eol: 0, generated: 0, differs: 0, unexpected: 0, unverified: 0,
458
+ skipped: 0, packages: units.length, validPackages: 0, lockFailures: 0, misplaced: 0 }
566
459
  const tell = (problem) => {
567
460
  counts[problem.kind]++
568
461
  problems.push(problem)
569
462
  }
570
- // What checking own code found of each own file, by its path, once it is checked: none for one not.
571
- let checked
572
- const verdicts = new Promise((resolve) => {
573
- checked = resolve
574
- })
463
+ // Each own file's verdict, once own code is checked, as patchOf awaits a bundle's patch's.
464
+ const { promise: verdicts, resolve: checked } = Promise.withResolvers()
575
465
 
576
- // The packages fetched while own code is read and checked.
577
466
  const told = []
578
467
  const bundled = patchesOf(plan)
579
- const fetching = eachOf(plan.packages, PACKAGES_AT_ONCE, async (pkg) => {
468
+ const fetching = eachOf(units, PACKAGES_AT_ONCE, async ({ pkg, locked }) => {
580
469
  const of = packageKey(pkg)
581
470
  const tellPackage = (status, more) => told.push({ ecosystem: pkg.ecosystem, name: pkg.name, version: pkg.version, files: pkg.files.length, status, ...more })
582
- // Left unchecked, its files with it: unverified, or failing its lockfile (not what was installed,
583
- // or not known to be).
584
471
  const unverified = (reason, status = 'unverified', more = {}) => {
585
472
  counts.unverified += pkg.files.length
586
473
  tellPackage(status, { reason, ...more })
587
474
  }
588
- const fails = (reason) => {
475
+ const fails = (reason, more) => {
589
476
  counts.lockFailures++
590
- unverified(reason, 'invalid')
477
+ unverified(reason, 'invalid', more)
591
478
  }
592
479
  if (!pkg.name || !pkg.version) return unverified('unnamed')
593
- if (!FETCHED.has(pkg.ecosystem)) return unverified('unsupported')
594
- // Each copy where Node resolves it by its path (a crate by its Cargo.toml alone, wherever it is):
595
- // the version failing where any is installed where another is; those installed under another
596
- // name no lockfile read tells of left unverified, the others checked all the same.
597
480
  const dirs = [...new Set(pkg.files.map((file) => file.dir))]
598
481
  const places = new Map(pkg.ecosystem === 'npm' ? dirs.map((dir) => [dir, placed(pkg, dir, locked)]) : [])
599
482
  const misplaced = dirs.find((dir) => places.get(dir) === false)
@@ -605,33 +488,60 @@ export async function validateBundle(plan, own, { fetch = fetchPackage, locked =
605
488
  if (aliased.length === dirs.length) return unverified('alias-unverified', 'unverified', { at: aliased[0] })
606
489
  const lock = locked?.lock?.(pkg.ecosystem, pkg.name, pkg.version)
607
490
  if (lock === null) return fails('not-locked')
608
- if (lock?.from === 'git') return unverified('git')
491
+ if (lock?.conflict) return fails('integrity-conflict')
492
+ // A registry's version locked with no integrity is held to the tarball fetched with the one recorded.
493
+ const recordedBy = (by) => new Set(dirs.map(by).filter((value) => value !== undefined))
494
+ const [recordedIntegrity, otherIntegrity] = recordedBy((dir) => pkg.integrities.get(dir))
495
+ if (otherIntegrity !== undefined) return fails('integrity-conflict')
496
+ const fetchedWith = lock?.from === 'registry' && lock.integrity === undefined && FETCHED.has(pkg.ecosystem)
497
+ if (recordedIntegrity !== undefined && lock !== undefined && !fetchedWith && lock.integrity !== recordedIntegrity) return fails('integrity-not-locked')
498
+ // Each copy's patch as it attests it: null, unpatched, of one recording an integrity and none.
499
+ const [recordedPatch, conflicting] = recordedBy((dir) => attestedOf({ integrity: pkg.integrities.get(dir), patch: pkg.recorded.get(dir) }, 'patch'))
500
+ if (conflicting !== undefined) return fails('patch-conflict')
501
+ const lockedPatch = pkg.ecosystem === 'npm' ? locked?.of(pkg.name, pkg.version) : undefined
502
+ // npm's and yarn's lockfiles lock no patch, as they patch nothing.
503
+ const installedPatch = lockedPatch === undefined && locked?.unpatched ? null : lockedPatch
504
+ const otherwise = recordedPatch !== undefined && installedPatch !== undefined && (installedPatch?.hash ?? null) !== recordedPatch
505
+ if (otherwise) return recordedPatch === null ? fails('patch-not-recorded') : fails('patch-not-locked', { patch: { hash: recordedPatch, recorded: true } })
506
+ if (lock?.from === 'git' && !(lock.at && locked.git)) return unverified('git')
507
+ let pinned
508
+ if (pkg.ecosystem === 'github' && submodules) {
509
+ const at = new Map(await Promise.all(dirs.map(async (dir) => [dir, await submodules.at(dir)])))
510
+ if (dirs.some((dir) => at.get(dir) === null)) return unverified('not-a-submodule')
511
+ if (dirs.some((dir) => pkg.repos.get(dir)?.commit !== undefined && pkg.repos.get(dir).commit !== at.get(dir))) return fails('commit-not-locked')
512
+ if (dirs.every((dir) => pkg.repos.get(dir)?.github !== undefined)) {
513
+ try {
514
+ pinned = new Map(await Promise.all(dirs.map(async (dir) => [dir, await submodules.read({ github: pkg.repos.get(dir).github, commit: at.get(dir), path: '' })])))
515
+ } catch (error) {
516
+ return unverified(fetchFailure(error))
517
+ }
518
+ }
519
+ }
520
+ if (!FETCHED.has(pkg.ecosystem) && lock?.from !== 'git' && !pinned) return unverified('unsupported')
609
521
  if (lock?.from === 'elsewhere') return unverified('not-registry')
610
522
  if (lock?.from === 'local' && !locked.tree) return unverified('local')
611
- if (lock?.conflict) return fails('integrity-conflict')
612
- if (lock?.from === 'registry' && lock.integrity === undefined) return unverified('no-integrity')
523
+ if (lock?.from === 'registry' && lock.integrity === undefined && recordedIntegrity === undefined) return unverified('no-integrity')
613
524
  if (now() > deadline) return unverified('timeout')
614
- // What each copy is held to, the first it matches: the repository's directories one from a
615
- // directory is installed from; else its files patched, where the patch is verified, then as
616
- // published.
525
+ // Patched alone where the lockfile locks the patch, as pnpm patches every copy, or the copy records it.
617
526
  let candidates
527
+ let published
618
528
  let patch
619
529
  let patched
620
530
  let unverifiedPatch = false
621
531
  try {
622
- if (lock?.from === 'local') candidates = await Promise.all(lock.directories.map((dir) => locked.tree(dir)))
532
+ if (pinned) candidates = []
533
+ else if (lock?.from === 'local') candidates = await Promise.all(lock.directories.map((dir) => locked.tree(dir)))
623
534
  else {
624
- const files = await fetch(pkg.ecosystem, pkg.name, pkg.version, lock?.integrity)
625
- ;({ patch, patched, unverified: unverifiedPatch = false } = pkg.ecosystem === 'npm' ? await patchOf(bundled, pkg, files, locked, verdicts) : {})
626
- candidates = patched && !unverifiedPatch ? [patched, files] : [files]
535
+ published = lock?.from === 'git' ? await locked.git(lock.at) : await fetch(pkg.ecosystem, pkg.name, pkg.version, lock?.integrity ?? recordedIntegrity)
536
+ ;({ patch, patched, unverified: unverifiedPatch = false } = pkg.ecosystem === 'npm' && recordedPatch !== null ? await patchOf(bundled, pkg, published, locked, verdicts, recordedPatch) : {})
537
+ candidates = patched && !unverifiedPatch ? [patched, published] : [published]
627
538
  }
628
539
  } catch (error) {
629
540
  const reason = fetchFailure(error)
630
541
  return reason === 'integrity-mismatch' ? fails(reason) : unverified(reason)
631
542
  }
632
- // A crate's `.cargo-checksum.json` held to the crate, as no crate holds one.
633
543
  const checksum = (file) => pkg.ecosystem === 'cargo' && file.rel === CARGO_CHECKSUM
634
- const matches = (file, bytes, candidate) => (checksum(file) ? checksumOf(bytes, candidate) : candidate.has(file.rel) && equal(bytes, candidate.get(file.rel)))
544
+ const matches = (file, bytes, candidate) => (checksum(file) ? checksumOf(bytes, candidate) : candidate.has(file.rel) && isFile(file, candidate.get(file.rel)))
635
545
  // Each installed copy apart, as one may be patched and another not.
636
546
  let missed = 0
637
547
  let asPatched = 0
@@ -641,30 +551,40 @@ export async function validateBundle(plan, own, { fetch = fetchPackage, locked =
641
551
  notVerified += copy.length
642
552
  continue
643
553
  }
644
- const bytes = new Map(copy.map((file) => [file, file.bytes()]))
554
+ const bytes = new Map(copy.map((file) => [file, file.integrity === undefined ? file.bytes() : undefined]))
645
555
  const missing = (candidate) => copy.filter((file) => !matches(file, bytes.get(file), candidate))
646
- let [held] = candidates
556
+ const patching = published !== undefined && Boolean(lockedPatch || pkg.recorded.has(copy[0].dir))
557
+ const heldTo = pinned ? [pinned.get(copy[0].dir)] : patched && patching ? [patched] : candidates
558
+ let [held] = heldTo
647
559
  let misses = missing(held)
648
- for (const other of candidates.slice(1)) {
560
+ for (const other of heldTo.slice(1)) {
649
561
  if (misses.length > 0 && missing(other).length === 0) [held, misses] = [other, []]
650
562
  }
651
563
  // As a patch not verified against the repository patches them: neither as they should be nor not.
652
- if (misses.length > 0 && unverifiedPatch && missing(patched).length === 0) {
564
+ if (unverifiedPatch && missing(patched).length === 0 && missing(published).length > 0) {
653
565
  notVerified += copy.length
654
566
  continue
655
567
  }
568
+ // Of a git dependency, a file its repository hasn't there is one an install may build: unverified.
569
+ const built = lock?.from === 'git' ? misses.filter((file) => !held.has(file.rel)) : []
570
+ notVerified += built.length
571
+ misses = misses.filter((file) => !built.includes(file))
656
572
  for (const file of misses) tell({ path: file.path, kind: checksum(file) || held.has(file.rel) ? 'differs' : 'unexpected', of })
657
573
  missed += misses.length
658
574
  if (misses.length === 0 && held === patched) asPatched++
575
+ // Its patch unread or unapplied, nothing tells what its files should be: those as published are unverified.
576
+ if (patching && !patched) notVerified += copy.length - misses.length - built.length
659
577
  }
660
578
  counts.matched += pkg.files.length - missed - notVerified
661
579
  counts.unverified += notVerified
662
580
  const status = missed > 0 ? 'invalid' : notVerified > 0 ? 'unverified' : 'valid'
663
581
  if (status === 'valid') counts.validPackages++
664
582
  if (status !== 'valid' || patch) {
583
+ let why
584
+ if (status === 'unverified') why = aliased.length > 0 ? { reason: 'alias-unverified', at: aliased[0] } : patch?.error ? {} : { reason: lock?.from === 'git' ? 'git-built' : 'patch-unverified' }
665
585
  tellPackage(status, {
666
- ...(status === 'unverified' && (aliased.length > 0 ? { reason: 'alias-unverified', at: aliased[0] } : { reason: 'patch-unverified' })),
667
- ...(patch && { patch: { ...patch, ...(patched && status === 'valid' && { applied: asPatched > 0 }) } }),
586
+ ...why,
587
+ ...(patch && { patch: { ...patch, ...(recordedPatch !== undefined && { recorded: true }), ...(patched && status === 'valid' && { applied: asPatched > 0 }) } }),
668
588
  ...(lock?.from === 'local' && { directory: lock.directories[0] }),
669
589
  })
670
590
  }
@@ -672,145 +592,164 @@ export async function validateBundle(plan, own, { fetch = fetchPackage, locked =
672
592
  })
673
593
 
674
594
  let read
595
+ let skipped
675
596
  const found = new Map()
597
+ const verdict = (file, kind, reason) => {
598
+ if (kind === 'matched') counts.matched++
599
+ else tell({ path: file.path, kind, of: 'own', ...(reason && { reason }) })
600
+ found.set(file.path, kind)
601
+ }
602
+ const unmatched = []
603
+ let generatedClients = []
604
+ let generatedError
676
605
  try {
677
606
  read = await own
678
- const skipped = 'reason' in read
679
- // The submodules' paths, worked out where a file is not in the tree.
680
- let submodules
681
- const inSubmodule = (path) => (submodules ??= [...read.entries].filter(([, entry]) => entry.submodule).map(([at]) => `${at}/`)).some((prefix) => path.startsWith(prefix))
607
+ skipped = 'reason' in read
608
+ // A patch, which no client generates, told at once.
609
+ const mismatch = (file, kind, reason) => (generated && !skipped && !plan.patches.has(file.path) ? unmatched.push({ file, kind, reason }) : verdict(file, kind, reason))
610
+ let submodulePaths
611
+ const inSubmodule = (path) => (submodulePaths ??= [...read.entries].filter(([, entry]) => entry.submodule).map(([at]) => `${at}/`)).some((prefix) => path.startsWith(prefix))
682
612
  for (const file of plan.own) {
683
- // Own code unchecked, but for the patches the bundle holds where the repository is read: skipped
684
- // as asked (--only-deps), else unverified.
613
+ // Own code skipped, the bundle's patches are checked all the same where the repository is read.
685
614
  if (skipped && !(read.entries && plan.patches.has(file.path))) {
686
615
  counts[read.reason === 'skipped' ? 'skipped' : 'unverified']++
687
616
  continue
688
617
  }
689
- const path = read.directory ? `${read.directory}/${file.path}` : file.path
618
+ const path = pathIn(read.directory, file.path)
690
619
  const entry = read.entries.get(path)
691
- const verdict = (kind, reason) => {
692
- if (kind === 'matched') counts.matched++
693
- else tell({ path: file.path, kind, of: 'own', ...(reason && { reason }) })
694
- found.set(file.path, kind)
695
- }
696
- if (entry === undefined || entry.submodule) {
697
- // One at a path no tree can have is no file left unlisted.
698
- const reason = entry?.submodule || inSubmodule(path) ? 'submodule' : read.cut && inTree(path) ? 'tree-cut' : undefined
699
- verdict(reason === 'tree-cut' ? 'unverified' : 'unexpected', reason)
700
- } else if (entry.symlink !== undefined) verdict('differs', 'symlink')
620
+ // One at a path no tree can have is no file left unlisted.
621
+ if (entry?.submodule || (entry === undefined && inSubmodule(path))) verdict(file, 'unexpected', 'submodule')
622
+ else if (entry === undefined && read.cut && inTree(path)) mismatch(file, 'unverified', 'tree-cut')
623
+ else if (entry === undefined) mismatch(file, 'unexpected')
624
+ else if (entry.symlink !== undefined) verdict(file, 'differs', 'symlink')
625
+ else if (file.integrity !== undefined && entry.data === undefined) verdict(file, 'unverified', 'listed')
701
626
  else {
702
- const bytes = file.bytes()
703
- const same = (candidate) => (entry.data === undefined ? gitBlobId(candidate) === entry.sha : equal(candidate, entry.data))
704
- if (same(bytes)) verdict('matched')
627
+ // Either side may be as a checkout with CRLF line endings writes the other's LF text.
628
+ const byIntegrity = file.integrity !== undefined
629
+ const bytes = byIntegrity ? entry.data : file.bytes()
630
+ const same = (candidate) => (byIntegrity ? isFile(file, candidate)
631
+ : entry.data === undefined ? gitBlobId(candidate) === entry.sha : equal(candidate, entry.data))
632
+ if (same(bytes)) verdict(file, 'matched')
705
633
  else {
706
- // As a checkout with CRLF line endings writes it, of the file the repository has with LFs.
707
- const lf = checkedOutAsLf(bytes, file.format)
708
- const crlf = lf !== undefined && same(lf)
709
- if (crlf) counts.eol++
710
- verdict(crlf ? 'matched' : 'differs')
634
+ const converted = checkedOut(bytes, file.format, !byIntegrity)
635
+ if (converted !== undefined && same(converted)) {
636
+ counts.eol++
637
+ verdict(file, 'matched')
638
+ } else mismatch(file, 'differs')
711
639
  }
712
640
  }
713
641
  }
642
+ // Told before the clients are generated: checking a package's patch the bundle holds awaits it.
643
+ checked(found)
644
+ if (unmatched.length > 0) {
645
+ const clients = await Promise.try(generated).catch((error) => {
646
+ generatedError = error.message
647
+ return []
648
+ })
649
+ generatedClients = heldToClients(unmatched, clients, verdict)
650
+ counts.generated = generatedClients.reduce((sum, client) => sum + client.files.length, 0)
651
+ }
714
652
  } finally {
715
653
  checked(found)
716
654
  }
717
655
  await fetching
718
656
 
719
- const skipped = 'reason' in read
720
- const patchesChecked = skipped && read.entries ? [...plan.own].filter((file) => plan.patches.has(file.path)).length : 0
657
+ const patchesChecked = skipped && read.entries ? plan.own.filter((file) => plan.patches.has(file.path)).length : 0
721
658
  return {
722
659
  validatedAt: now(),
723
660
  status: counts.differs + counts.unexpected + counts.lockFailures + counts.misplaced > 0 ? 'invalid' : counts.unverified > 0 ? 'incomplete' : 'valid',
724
661
  own: skipped ? { checked: false, reason: read.reason, ...(patchesChecked > 0 && { patches: patchesChecked }) }
725
662
  : { checked: true, repository: read.repository, ...(read.movedFrom && { movedFrom: read.movedFrom }), commit: read.commit, ...(read.ref && { ref: read.ref }), directory: read.directory, ...(read.cut && { cut: true }) },
726
- lockfiles: locked?.lockfiles ?? null,
663
+ lockfiles: lockfiles?.lockfiles ?? null,
727
664
  counts,
728
665
  problems: problems.toSorted(byProblem).slice(0, MAX_TOLD),
729
666
  moreProblems: Math.max(0, problems.length - MAX_TOLD),
730
667
  packages: told.toSorted(byPackage).slice(0, MAX_TOLD),
668
+ generated: generatedClients,
669
+ ...(generatedError !== undefined && { generatedError }),
670
+ }
671
+ }
672
+
673
+ const bytesOf = (content) => (typeof content === 'string' ? utf8.encode(content) : content)
674
+ const relIn = (dir, path) => (dir === '' ? path : path.startsWith(`${dir}/`) ? path.slice(dir.length + 1) : undefined)
675
+
676
+ function heldToClients(unmatched, clients, verdict) {
677
+ const listed = new Map()
678
+ for (const { file, kind, reason } of unmatched) {
679
+ const outputs = clients.map((client) => ({ client, rel: relIn(client.dir, file.path) })).filter(({ rel }) => rel !== undefined)
680
+ const generating = outputs.filter(({ client, rel }) => client.files.has(rel))
681
+ const by = generating.find(({ client, rel }) => isFile(file, bytesOf(client.files.get(rel))))?.client
682
+ if (by) {
683
+ verdict(file, 'matched')
684
+ upsert(listed, by, () => ({ output: by.dir, schema: by.schema, generator: by.generator, prisma: by.prisma, files: [] })).files.push(file.path)
685
+ } else if (generating.length > 0 && kind !== 'unverified') verdict(file, 'differs', 'generated')
686
+ else verdict(file, kind, reason ?? (outputs.length > 0 && kind === 'unexpected' ? 'not-generated' : undefined))
731
687
  }
688
+ return [...listed.values()].map((client) => ({ ...client, files: client.files.toSorted() })).toSorted(byText((client) => client.output))
732
689
  }
733
690
 
734
- // The exit code a report's validation stands for: 0 valid, own code skipped as asked aside; 1
735
- // invalid; 2 incomplete, where a file it was to check went unverified, unless `allowUnverified`.
736
691
  export const exitCodeOf = (report, { allowUnverified = false } = {}) => (report.status === 'invalid' ? 1 : report.status === 'incomplete' && !allowUnverified ? 2 : 0)
737
692
 
738
- // The repository's files under `directory` at `commit`, as `validateBundle` takes them (by their paths
739
- // from the repository's root): its tree's tarball, which upstream holds to the tree's id, its symlinks
740
- // climbing as far as the repository's root.
693
+ // Its tree's tarball, which upstream holds to the tree's id, its symlinks climbing as far as the repository's root.
741
694
  export async function repositoryFiles(client, { github, commit, directory = '' }) {
742
695
  const tree = await client.getRepoTreeId({ repo: github, sha: commit, ...(directory && { directory }) })
743
696
  const tarball = await client.getRepoTreeTarball({ repo: github, tree, symlinksAbove: directory ? directory.split('/').length - 1 : 0 })
744
697
  const entries = new Map()
745
- for (const [path, value] of await tarballFiles(tarball)) entries.set(directory ? `${directory}/${path}` : path, value instanceof Uint8Array ? { data: value } : value)
698
+ for (const [path, value] of await tarballFiles(tarball)) entries.set(pathIn(directory, path), value instanceof Uint8Array ? { data: value } : value)
746
699
  return entries
747
700
  }
748
701
 
749
- // The lockfiles validation reads, by their names, and the ecosystem each locks the packages of.
750
- const LOCKFILES = new Map([['pnpm-lock.yaml', 'npm'], ['npm-shrinkwrap.json', 'npm'], ['package-lock.json', 'npm'], ['yarn.lock', 'npm'], ['Cargo.lock', 'cargo']])
751
- // crates.io's index, by the sources a Cargo.lock names it by.
702
+ function filesUnder(entries, dir) {
703
+ const prefix = dir ? `${dir}/` : ''
704
+ return new Map([...entries].filter(([path, entry]) => path.startsWith(prefix) && entry.data).map(([path, entry]) => [path.slice(prefix.length), entry.data]))
705
+ }
706
+
707
+ const LOCKFILES = new Map([['pnpm-lock.yaml', 'npm'], ['npm-shrinkwrap.json', 'npm'], ['package-lock.json', 'npm'], ['yarn.lock', 'npm'], ['Cargo.lock', 'cargo'], ['composer.lock', 'composer'], ['soldeer.lock', 'soldeer']])
708
+ // Those of ecosystems upstream fetches no package of, read where the bundle's directory holds one.
709
+ const BESIDE = new Map([['composer', 'composer.lock'], ['soldeer', 'soldeer.lock']])
752
710
  const CRATES_IO = new Set(['registry+https://github.com/rust-lang/crates.io-index', 'sparse+https://index.crates.io/'])
753
711
 
754
- // Whether `tarball` is a registry's of `name` and `version`, npm's or a mirror's of it, by its path:
755
- // none, as npm writes one of its registry's where set to, is.
712
+ // None is, as npm writes one of its registry's where set to.
756
713
  function registryTarball(tarball, name, version) {
757
714
  if (tarball === undefined) return true
758
715
  const url = URL.parse(tarball)
759
716
  return url !== null && /^https?:$/u.test(url.protocol) && url.pathname.endsWith(tarballPath(name, version))
760
717
  }
761
718
 
762
- // The one sha512 an SRI holds, as upstream takes one; undefined where it holds none, or two.
763
- function sha512Of(integrity) {
764
- const found = new Set((integrity ?? '').split(/\s+/u).filter((token) => token.startsWith('sha512-')).map((token) => token.split('?')[0]))
765
- return found.size === 1 ? [...found][0] : undefined
719
+ function lockedFrom(git, registry, integrity, resolution) {
720
+ if (!git) return { from: registry ? 'registry' : 'elsewhere', integrity }
721
+ const at = resolutionCommit(resolution)
722
+ return { from: 'git', integrity, ...(at && { at: { ...at, path: (resolution.path ?? '').replace(/^\/+|\/+$/gu, '') } }) }
766
723
  }
767
-
768
- // A text without the byte order mark it starts with, as npm, pnpm and yarn read their lockfiles.
769
- const withoutBom = (text) => (text.charCodeAt(0) === 0xFE_FF ? text.slice(1) : text)
770
-
771
- // Where a lockfile's entry of a version comes from: git (a git repository, or a git host's tarball,
772
- // installed-commits.js's resolutionCommit), its `registry`, with its `integrity`, or elsewhere.
773
- const lockedFrom = (git, registry, integrity) => (git ? { from: 'git' } : registry ? { from: 'registry', integrity } : { from: 'elsewhere' })
774
724
  const fromGit = (resolution) => resolution?.type === 'git' || resolution?.gitHosted === true || resolutionCommit(resolution) !== undefined
775
725
 
776
- // A dependency's spec naming a directory of the project, `file:` or `link:` and its path, a tarball's
777
- // aside; else undefined.
778
726
  function localDirectory(spec) {
779
727
  const path = /^(?:file|link):(.+)$/u.exec(spec)?.[1]
780
728
  return path === undefined || /\.(?:tgz|tar\.gz|tar)$/u.test(path) ? undefined : path
781
729
  }
782
730
 
783
- // What the lockfile `name` (its file name) of `text` locks: each version by name and version, an
784
- // entry for each time it locks it, lockedFrom's (an npm version's sha512, a crate's sha256); each
785
- // package installed from a directory of the project (a `file:` or `link:` one, an npm link), by its
786
- // name, the directories from the lockfile's (`locals`), which lock no version; and, of pnpm's, the
787
- // patch it locks each version with, null for none (`hashes`), and each patch's file (`paths`). Its
788
- // BOM passed over but Cargo.lock's, as cargo reads that as it is.
731
+ // Its BOM passed over but Cargo.lock's, as cargo reads it.
789
732
  function readLockfile(name, read) {
790
733
  const text = name === 'Cargo.lock' ? read : withoutBom(read)
791
734
  const entries = new Map()
792
735
  const locals = new Map()
793
- const add = (pkg, version, entry) => {
794
- const key = `${pkg}@${version}`
795
- entries.set(key, [...(entries.get(key) ?? []), entry])
796
- }
736
+ const add = (pkg, version, entry) => upsert(entries, `${pkg}@${version}`, () => []).push(entry)
797
737
  const local = (pkg, dir) => locals.set(pkg, new Set([...(locals.get(pkg) ?? []), posix.normalize(dir)]))
798
- // Each name a version is installed as in place of its own (an alias, `foo: npm:ms@2.1.3`), as
799
- // `<name it is installed as>\0<its name>@<its version>`; and each a directory's package is, any version
800
- // of it, as `<name it is installed as>\0<its name>`.
738
+ // `<as>\0<name>@<version>`, and a directory's package's `<as>\0<name>`, any version of it.
801
739
  const aliases = new Set()
802
740
  const directoryAliases = new Set()
803
741
  if (name === 'pnpm-lock.yaml') {
804
742
  const { lockfile } = parsePnpmLockfile(text)
805
- // pnpm locks an alias's dependency by its name and version (`foo: ms@2.1.3`), a version alone
806
- // otherwise; one of a directory by its name and the directory (`foo: lib@file:lib`, as a `file:`
807
- // override installs it), the version the one it has there.
743
+ // pnpm locks an alias `foo: ms@2.1.3`, and a directory's `foo: lib@file:lib`, as a `file:` override installs it.
808
744
  for (const node of [...Object.values(lockfile?.importers ?? {}), ...Object.values(lockfile?.packages ?? {})]) {
809
745
  for (const [as, ref] of Object.entries({ ...node.dependencies, ...node.devDependencies, ...node.optionalDependencies })) {
810
746
  const split = typeof ref === 'string' ? ref.indexOf('@', 1) : -1
811
- const of = split > 0 ? ref.slice(split + 1) : ''
812
- if (VERSION.test(of.split('(')[0])) aliases.add(`${as}\0${ref.slice(0, split)}@${of.split('(')[0]}`)
813
- else if (of.startsWith('file:') && as !== ref.slice(0, split)) directoryAliases.add(`${as}\0${ref.slice(0, split)}`)
747
+ if (split < 0) continue
748
+ const target = ref.slice(0, split)
749
+ const of = ref.slice(split + 1)
750
+ const version = of.split('(')[0]
751
+ if (VERSION.test(version)) aliases.add(`${as}\0${target}@${version}`)
752
+ else if (of.startsWith('file:') && as !== target) directoryAliases.add(`${as}\0${target}`)
814
753
  }
815
754
  }
816
755
  const hashes = new Map()
@@ -820,13 +759,13 @@ function readLockfile(name, read) {
820
759
  if (pkg.version === undefined) continue
821
760
  // A registry version's key is its name and version, its peers and patch after them.
822
761
  const ref = key.slice(pkg.name.length + 1)
823
- add(pkg.name, pkg.version, lockedFrom(fromGit(pkg.resolution), ref === pkg.version || ref.startsWith(`${pkg.version}(`), sha512Of(pkg.resolution.integrity)))
762
+ add(pkg.name, pkg.version, lockedFrom(fromGit(pkg.resolution), ref === pkg.version || ref.startsWith(`${pkg.version}(`), sha512Of(pkg.resolution.integrity), pkg.resolution))
824
763
  hashes.set(`${pkg.name}@${pkg.version}`, pkg.patchHash ?? null)
825
764
  }
826
- // Each patch by its hash: its file, where the lockfile names one, and the selectors naming it.
827
765
  const paths = new Map()
828
766
  for (const [selector, { hash, path }] of Object.entries(lockfile?.patchedDependencies ?? {})) {
829
- paths.set(hash, { path: paths.get(hash)?.path ?? path, selectors: [...(paths.get(hash)?.selectors ?? []), selector] })
767
+ const known = paths.get(hash)
768
+ paths.set(hash, { path: known?.path ?? path, selectors: [...(known?.selectors ?? []), selector] })
830
769
  }
831
770
  return { entries, locals, aliases, directoryAliases, patches: { hashes, paths } }
832
771
  }
@@ -834,9 +773,19 @@ function readLockfile(name, read) {
834
773
  for (const pkg of Object.values(parseCargoLock(text).packages)) add(pkg.name, pkg.version, lockedFrom(pkg.source?.startsWith('git+'), CRATES_IO.has(pkg.source), pkg.checksum))
835
774
  return { entries }
836
775
  }
776
+ // Composer's: a dist archive, else its git source; a path repository's is a directory's, elsewhere.
777
+ if (name === 'composer.lock') {
778
+ for (const pkg of Object.values(parseComposerLock(text, {}).packages)) {
779
+ const git = pkg.dist == null && pkg.source?.type === 'git'
780
+ add(pkg.name, pkg.version, lockedFrom(git, pkg.dist != null && pkg.dist.type !== 'path', archiveIntegrity('composer', pkg.dist?.shasum), git ? { type: 'git', repo: pkg.source.url, commit: pkg.source.reference } : undefined))
781
+ }
782
+ return { entries }
783
+ }
784
+ if (name === 'soldeer.lock') {
785
+ for (const dep of Object.values(parseSoldeerLockfile(text).dependencies)) add(dep.name, dep.version, lockedFrom(dep.type === 'git', true, archiveIntegrity('soldeer', dep.checksum), dep.type === 'git' ? { type: 'git', repo: dep.git, commit: dep.rev } : undefined))
786
+ return { entries }
787
+ }
837
788
  let packages
838
- // npm's, by each location it installs a version at, from its directory: `<name>@<version>`; and
839
- // the locations it links to a directory, held to that directory (`locals`) in place of a version.
840
789
  let locations
841
790
  let links
842
791
  let importers
@@ -846,9 +795,8 @@ function readLockfile(name, read) {
846
795
  locations = new Map(Object.entries(lockfile.packages).filter(([, pkg]) => pkg.version !== undefined).map(([location, pkg]) => [location, `${pkg.name}@${pkg.version}`]))
847
796
  importers = new Set(Object.keys(lockfile.importers))
848
797
  links = new Set(Object.keys(lockfile.links))
849
- // npm links a directory it installs, by its location, to the directory: the package there.
850
798
  for (const [location, target] of Object.entries(lockfile.links)) {
851
- if (Object.hasOwn(lockfile.importers, target)) local(location.slice(location.lastIndexOf('node_modules/') + 'node_modules/'.length), target)
799
+ if (Object.hasOwn(lockfile.importers, target)) local(nameAt(location), target)
852
800
  }
853
801
  packages = Object.values(lockfile.packages)
854
802
  }
@@ -861,109 +809,141 @@ function readLockfile(name, read) {
861
809
  // yarn locks a directory by its spec alone, as it last read the version there.
862
810
  const dir = resolution === undefined ? patterns?.map((pattern) => localDirectory(pattern.slice(pattern.indexOf('@', 1) + 1))).find(Boolean) : undefined
863
811
  if (dir !== undefined) local(pkg, dir)
864
- else if (version !== undefined) add(pkg, version, lockedFrom(fromGit(resolution), resolution?.type === 'tarball' && registryTarball(resolution.tarball, pkg, version), sha512Of(resolution?.integrity)))
812
+ else if (version !== undefined) add(pkg, version, lockedFrom(fromGit(resolution), resolution?.type === 'tarball' && registryTarball(resolution.tarball, pkg, version), sha512Of(resolution?.integrity), resolution))
865
813
  }
866
814
  return { entries, locals, aliases, ...(locations && { locations, links, importers }) }
867
815
  }
868
816
 
869
- // What `lockfiles` lock, as validateBundle takes it (`locked`), one of each ecosystem: each `{ path,
870
- // text, from, read, tree, patches }`, its path (as it is told), what it is read of (`repository`, or
871
- // `local`), `read(path)` reading a file by a path as it is (a pnpm patch's, by the lockfile's
872
- // directory and the path it names), each read once, `tree(dir)` the files of a directory of the
873
- // repository by their paths in it, where the repository is read, and `patches` the directories a
874
- // patch it names by its hash alone (pnpm 11's) is looked for in, in turn, beside the lockfile's
875
- // directory where none is given: `locate(lock)` finds it, the file there of pnpm's name for it and of
876
- // that hash. Their paths (`lockfiles`), and whether one locks an ecosystem's packages
877
- // (`locks(ecosystem)`), told too.
817
+ function lockOf(path, text) {
818
+ try {
819
+ return readLockfile(posix.basename(path), text)
820
+ } catch (error) {
821
+ throw unreadableLockfile(path, error)
822
+ }
823
+ }
824
+
878
825
  export function lockedBy(lockfiles) {
879
826
  const by = new Map()
880
- for (const { path, text, from, read, tree, patches, root = '' } of lockfiles) {
827
+ const all = []
828
+ const given = new Map()
829
+ for (const lockfile of lockfiles) {
830
+ const { path, text, from, read, tree, git, patches, root = '', at = '' } = lockfile
881
831
  const ecosystem = LOCKFILES.get(posix.basename(path))
882
832
  if (ecosystem === undefined) throw new ValidateError('lockfile-unsupported', `${path}: not a lockfile validate reads (${[...LOCKFILES.keys()].join(', ')})`)
883
- if (by.has(ecosystem)) throw new ValidateError('lockfile-ambiguous', `${by.get(ecosystem).path} and ${path} both lock ${ecosystem} packages: give one`)
884
- let locks
885
- try {
886
- locks = readLockfile(posix.basename(path), text)
887
- } catch (error) {
888
- throw new ValidateError('lockfile-unreadable', `${path}: ${error.message}`)
889
- }
890
- // The bundle's root by its path from the lockfile's directory, where npm's locks each location
891
- // from there: on disk, the one of its importers it names, else the lockfile's own directory.
833
+ const same = by.get(ecosystem) ?? []
834
+ const other = same.find((one) => one.at === at)
835
+ if (other !== undefined) throw new ValidateError('lockfile-ambiguous', `${other.path} and ${path} both lock ${ecosystem} packages: give one`)
836
+ const locks = lockOf(path, text)
837
+ // npm's locks each location from its directory: the bundle's root from there is one of its importers on disk.
892
838
  const within = locks.importers === undefined || from !== 'local' || locks.importers.has(root) ? root : ''
893
- by.set(ecosystem, { ...locks, path, from, read, tree, root: within, patchDirs: patches ?? [posix.join(posix.dirname(path), 'patches')] })
839
+ const one = { ...locks, path, from, read, tree, git, root: within, at, patchDirs: patches ?? [posix.join(posix.dirname(path), 'patches')] }
840
+ by.set(ecosystem, [...same, one])
841
+ all.push(one)
842
+ given.set(one, lockfile)
894
843
  }
895
- const npm = by.get('npm')
896
- const reads = new Map()
844
+ const npms = by.get('npm') ?? []
845
+ // Of an npm lockfile at the bundle's root or above it, every copy; of one below, those under it; of one
846
+ // beside it (`../tools`), none, as no path of the bundle climbs out of its root.
847
+ const above = (at) => at === '' || at.split('/').every((part) => part === '..')
848
+ const depth = (at) => (at === '' ? 0 : above(at) ? -at.split('/').length : at.split('/').length)
849
+ const innermost = (of, dir) => of.filter(({ at }) => above(at) || dir.startsWith(`${at}/`)).reduce((inner, one) => (inner === undefined || depth(one.at) > depth(inner.at) ? one : inner), undefined)
850
+ const aloneOf = memoized((one) => lockedBy([given.get(one)]))
851
+ const locationIn = (one, dir) => (above(one.at) ? pathIn(one.root, dir) : dir.slice(one.at.length + 1))
897
852
  return {
898
- lockfiles: [...by.values()].map(({ path }) => path),
853
+ lockfiles: all.map(({ path }) => path),
899
854
  locks: (ecosystem) => by.has(ecosystem),
855
+ // Each copy held to the innermost lockfile holding it alone; those none holds, to them all.
856
+ split(pkg) {
857
+ const same = by.get(pkg.ecosystem) ?? []
858
+ if (same.length < 2) return undefined
859
+ return [...Map.groupBy(pkg.files, (file) => innermost(same, file.dir))].map(([one, files]) => ({ pkg: { ...pkg, files }, locked: one === undefined ? this : aloneOf(one) }))
860
+ },
900
861
  lock(ecosystem, name, version) {
901
- const entries = by.get(ecosystem)?.entries
902
- if (entries === undefined) return undefined
903
- const locks = entries.get(`${name}@${version}`)
904
- if (locks === undefined) {
905
- // One of the project's directories, by its path from the repository's root, '' the root.
906
- const { locals, path } = by.get(ecosystem)
907
- const dirs = locals?.get(name)
908
- const fromRoot = (dir) => (dir === '.' ? '' : dir)
909
- return dirs ? { from: 'local', directories: [...dirs].map((dir) => fromRoot(posix.join(posix.dirname(path), dir))) } : null
862
+ const same = by.get(ecosystem)
863
+ if (same === undefined) return undefined
864
+ const locks = same.flatMap(({ entries }) => entries.get(`${name}@${version}`) ?? [])
865
+ if (locks.length === 0) {
866
+ const dirs = same.flatMap(({ locals, path }) => [...(locals?.get(name) ?? [])].map((dir) => repoDir(posix.join(posix.dirname(path), dir))))
867
+ return dirs.length > 0 ? { from: 'local', directories: [...new Set(dirs)] } : null
910
868
  }
911
869
  const integrities = new Set(locks.filter((entry) => entry.from === 'registry').map((entry) => entry.integrity))
912
- if (integrities.size === 0) return { from: locks.every((entry) => entry.from === 'git') ? 'git' : 'elsewhere' }
870
+ if (integrities.size === 0) {
871
+ const locking = new Set(locks.map((entry) => entry.integrity))
872
+ const archive = locking.size === 1 && !locking.has(undefined) && { integrity: locks[0].integrity }
873
+ if (!locks.every((entry) => entry.from === 'git')) return { from: 'elsewhere', ...archive }
874
+ const [at, other] = new Set(locks.map((entry) => JSON.stringify(entry.at ?? null)))
875
+ return { from: 'git', ...archive, ...(other === undefined && at !== 'null' && { at: JSON.parse(at) }) }
876
+ }
913
877
  return integrities.size === 1 ? { from: 'registry', integrity: [...integrities][0] } : { from: 'registry', conflict: true }
914
878
  },
879
+ // Null where the pnpm lockfiles lock the version with no patch, undefined where none of them locks it.
915
880
  of(name, version) {
916
- if (npm?.patches === undefined) return undefined
917
- const hash = npm.patches.hashes.get(`${name}@${version}`)
918
- if (hash == null) return hash
919
- const { path, selectors = [] } = npm.patches.paths.get(hash) ?? {}
920
- if (path) return { hash, path: posix.join(posix.dirname(npm.path), path) }
921
- // The names pnpm gives its file, by each selector naming it and by the version it patches.
922
- return { hash, names: [...new Set([...selectors.map((selector) => `${selector.replace('/', '__')}.patch`), pnpmPatchName(name, version)])] }
881
+ let found
882
+ for (const npm of npms) {
883
+ const hash = npm.patches?.hashes.get(`${name}@${version}`)
884
+ if (hash === null) found = null
885
+ if (hash == null) continue
886
+ const { path, selectors = [] } = npm.patches.paths.get(hash) ?? {}
887
+ if (path) return { hash, path: posix.join(posix.dirname(npm.path), path) }
888
+ return { hash, names: [...new Set([...selectors.map((selector) => `${selector.replace('/', '__')}.patch`), pnpmPatchName(name, version)])] }
889
+ }
890
+ return found
923
891
  },
924
892
  async locate({ hash, names = [] }) {
925
- for (const path of npm.patchDirs.flatMap((dir) => names.map((name) => (dir ? `${dir}/${name}` : name)))) {
926
- let text
927
- try {
928
- // eslint-disable-next-line no-await-in-loop -- the first of them that is there
929
- text = await this.read(path)
930
- } catch {
931
- continue // none there
932
- }
933
- if (isPatchOf(text, hash)) return { path, text }
893
+ for (const path of [...new Set(npms.flatMap((npm) => npm.patchDirs))].flatMap((dir) => names.map((name) => pathIn(dir, name)))) {
894
+ // eslint-disable-next-line no-await-in-loop -- the first of them that is there
895
+ const text = await Promise.try(() => this.read(path)).catch(() => null)
896
+ if (text !== null && isPatchOf(text, hash)) return { path, text }
934
897
  }
935
898
  return null
936
899
  },
937
- // The version an npm lockfile installs at the bundle's `dir` (`<name>@<version>`), null for none,
938
- // undefined where its lockfile locks no location (pnpm's, yarn's) or links it to a directory.
900
+ // Null where it installs none at `dir`; undefined where it locks no location, links it to a directory, or none is read.
939
901
  installed(dir) {
940
- if (npm?.locations === undefined) return undefined
941
- const location = npm.root ? `${npm.root}/${dir}` : dir
902
+ const npm = innermost(npms, dir)
903
+ if (npm === undefined) return npms.length > 0 ? null : undefined
904
+ if (npm.locations === undefined) return undefined
905
+ const location = locationIn(npm, dir)
942
906
  return npm.links.has(location) ? undefined : npm.locations.get(location) ?? null
943
907
  },
944
- // Whether the npm lockfile installs `named` (`<name>@<version>`) under the name `as`: that version,
945
- // or a directory's package of that name, where it locks no other of that version, so that its
946
- // files are held to the directory's (locals); undefined for none read.
947
- aliased: (as, named) => npm && (npm.aliases.has(`${as}\0${named}`)
948
- || (!npm.entries.has(named) && Boolean(npm.directoryAliases?.has(`${as}\0${named.slice(0, named.lastIndexOf('@'))}`)))),
949
- from: npm?.from,
950
- tree: npm?.tree,
951
- read(path) {
952
- if (!reads.has(path)) reads.set(path, Promise.resolve().then(() => npm.read(path)))
953
- return reads.get(path)
954
- },
908
+ // A directory's package of that name, where no other of that version is locked, too; undefined for none read.
909
+ aliased: (as, named) => (npms.length === 0 ? undefined : npms.some((npm) => npm.aliases.has(`${as}\0${named}`)
910
+ || (!npm.entries.has(named) && Boolean(npm.directoryAliases?.has(`${as}\0${named.slice(0, named.lastIndexOf('@'))}`))))),
911
+ from: npms[0]?.from,
912
+ tree: npms[0]?.tree,
913
+ git: all.find((one) => one.git)?.git,
914
+ // npm's and yarn's lockfiles alone, which patch nothing as they install.
915
+ unpatched: npms.length > 0 && npms.every((npm) => npm.patches === undefined),
916
+ read: memoized((path) => Promise.resolve().then(() => npms[0].read(path))),
955
917
  }
956
918
  }
957
919
 
958
- // The lockfiles that install `directory` of a repository, for the packages of `ecosystems`, as
959
- // lockedBy takes them (but `from` and `read`), as the build finds them: an npm package's of the one
960
- // package manager whose root (vfs-bundle's tree's lockfileRoot) holds its lockfile, pnpm's
961
- // (pnpm-lock.yaml), npm's (package-lock.json, or npm-shrinkwrap.json, which npm takes before it) or
962
- // yarn 1's (yarn.lock); a crate's the Cargo.lock beside its workspace's root (findCargoLock). Those
963
- // read of a sparse tree of the directories at and above it, their files' names listed and the
964
- // manifests the roots are found by read. Refused (ValidateError) where none is, or more than one
965
- // package manager's is. `read(path)` reads a file of the repository, `list(dir)` the names of the
966
- // files a directory has.
920
+ function lockfilesNamed(path, text) {
921
+ let config
922
+ try {
923
+ config = JSON.parse(text)
924
+ return parseLockfilesOption(path, config?.lockfiles)
925
+ } catch (error) {
926
+ // parseLockfilesOption's message names the file.
927
+ throw config === undefined ? unreadableLockfile(path, error) : new ValidateError('lockfile-unreadable', error.message)
928
+ }
929
+ }
930
+
931
+ export async function configuredLockfiles({ directory = '', list, read }) {
932
+ if (!(await list(directory)).includes(CONFIG_FILE)) return undefined
933
+ const path = pathIn(directory, CONFIG_FILE)
934
+ return lockfilesNamed(path, await readLockfileText(read, path))?.map((one) => {
935
+ const at = posix.normalize(pathIn(directory, one))
936
+ if (at === '..' || at.startsWith('../')) throw new ValidateError('lockfile-unreadable', `${path}: lockfiles names ${one}, which is outside the repository`)
937
+ return at
938
+ })
939
+ }
940
+
941
+ async function besideLockfiles({ directory, names, list, read }) {
942
+ const listed = await list(directory)
943
+ return Promise.all(names.filter((name) => listed.includes(name)).map(async (name) => ({ path: pathIn(directory, name), text: await read(pathIn(directory, name)) })))
944
+ }
945
+
946
+ // As the build finds them, read of a sparse tree of the directories at and above `directory`.
967
947
  export async function repositoryLockfiles({ directory = '', ecosystems, list, read }) {
968
948
  const dirs = []
969
949
  for (let dir = directory; ; dir = dir.includes('/') ? dir.slice(0, dir.lastIndexOf('/')) : '') {
@@ -972,14 +952,11 @@ export async function repositoryLockfiles({ directory = '', ecosystems, list, re
972
952
  }
973
953
  const listed = await Promise.all(dirs.map(list))
974
954
  const where = directory || 'the repository\'s root'
975
- // The manifests a root is found by, read where a lockfile they could pick is listed: package.json
976
- // for npm's and yarn's workspaces, Cargo.toml for cargo's.
977
- const lists = (...names) => listed.some((names_) => names.some((name) => names_.includes(name)))
955
+ const lists = (...names) => listed.some((inDir) => names.some((name) => inDir.includes(name)))
978
956
  const manifests = new Set([
979
957
  ...ecosystems.has('npm') && lists('package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock') ? ['package.json'] : [],
980
958
  ...ecosystems.has('cargo') ? ['Cargo.toml'] : [],
981
959
  ])
982
- // Each directory there, whatever it holds of those.
983
960
  const files = Object.fromEntries(dirs.filter(Boolean).map((dir) => [dir, { type: 'directory' }]))
984
961
  await Promise.all(dirs.flatMap((dir, index) => listed[index].map(async (name) => {
985
962
  if (manifests.has(name)) files[pathIn(dir, name)] = await read(pathIn(dir, name))
@@ -989,17 +966,19 @@ export async function repositoryLockfiles({ directory = '', ecosystems, list, re
989
966
  const host = vfsHost(createVfs(files))
990
967
  const cwd = posix.resolve('/', directory || '.')
991
968
  const fromRoot = (path) => path.slice(1)
969
+ const rootOf = (what, find) => {
970
+ try {
971
+ return find()
972
+ } catch (error) {
973
+ throw new ValidateError('lockfile-unreadable', `${where}'s ${what}: ${error.message}`)
974
+ }
975
+ }
992
976
 
993
977
  const chosen = []
994
978
  if (ecosystems.has('npm')) {
995
979
  const found = []
996
980
  for (const pm of ['pnpm', 'npm', 'yarn1']) {
997
- let root
998
- try {
999
- root = lockfileRoot(host, pm, cwd)
1000
- } catch (error) {
1001
- throw new ValidateError('lockfile-unreadable', `${where}'s ${pm} root: ${error.message}`)
1002
- }
981
+ const root = rootOf(`${pm} root`, () => lockfileRoot(host, pm, cwd))
1003
982
  if (root === null) continue
1004
983
  const names = listed[dirs.indexOf(fromRoot(root))]
1005
984
  found.push(pathIn(fromRoot(root), pm === 'npm' && names.includes('npm-shrinkwrap.json') ? 'npm-shrinkwrap.json' : lockfileOf(pm)))
@@ -1014,61 +993,59 @@ export async function repositoryLockfiles({ directory = '', ecosystems, list, re
1014
993
  chosen.push(found[0])
1015
994
  }
1016
995
  if (ecosystems.has('cargo')) {
1017
- let lock
1018
- try {
1019
- lock = findCargoLock(cwd, host)
1020
- } catch (error) {
1021
- throw new ValidateError('lockfile-unreadable', `${where}'s Cargo workspace: ${error.message}`)
1022
- }
996
+ const lock = rootOf('Cargo workspace', () => findCargoLock(cwd, host))
1023
997
  if (lock === null) throw new ValidateError('no-lockfile', `no Cargo.lock beside ${where}'s workspace root locks its crates: give --lockfile=path, or --no-lockfile to hold them to crates.io alone`)
1024
998
  chosen.push(fromRoot(lock))
1025
999
  }
1026
1000
  return Promise.all(chosen.map(async (path) => ({ path, text: await read(path) })))
1027
1001
  }
1028
1002
 
1029
- // The npm packages the package manager's lockfile installing `dir` (an absolute path) locks of a
1030
- // registry, each `{ name, version }` once, a version locked from git or a directory left out; and the
1031
- // lockfile's absolute `path`. Found as repositoryLockfiles finds it, read of the disk at and above `dir`;
1032
- // null where none is. Refused (ValidateError) where more than one package manager's is, it is bun's, which
1033
- // isn't read, or it can't be read.
1034
1003
  export async function installedNpmPackages(dir) {
1035
1004
  const { root } = parsePath(dir)
1036
1005
  const at = (path) => join(root, ...path.split('/'))
1037
- let found
1038
- try {
1039
- found = await repositoryLockfiles({
1040
- directory: relative(root, dir).split(sep).join('/'),
1041
- ecosystems: new Set(['npm']),
1042
- list: (path) => readdir(at(path)).catch(() => []),
1043
- read: (path) => readFile(at(path), 'utf8'),
1044
- })
1045
- } catch (error) {
1006
+ const found = await repositoryLockfiles({
1007
+ directory: relative(root, dir).split(sep).join('/'),
1008
+ ecosystems: new Set(['npm']),
1009
+ list: (path) => readdir(at(path)).catch(() => []),
1010
+ read: (path) => readFile(at(path), 'utf8'),
1011
+ }).catch((error) => {
1046
1012
  if (!(error instanceof ValidateError)) throw error
1047
1013
  if (error.code === 'no-lockfile') return null
1048
1014
  // As repositoryLockfiles tells it, but the flags validate takes in its place.
1049
1015
  throw new ValidateError(error.code, error.message.replace(/[:;] give --lockfile=path.*$/su, '').replace("which validate doesn't read", "which isn't read"))
1050
- }
1016
+ })
1017
+ if (found === null) return null
1051
1018
  const [{ path, text }] = found
1052
- let locks
1053
- try {
1054
- locks = readLockfile(posix.basename(path), text)
1055
- } catch (error) {
1056
- throw new ValidateError('lockfile-unreadable', `${at(path)}: ${error.message}`)
1057
- }
1058
- const packages = [...locks.entries].filter(([, entries]) => entries.some((entry) => entry.from === 'registry')).map(([key]) => {
1019
+ return { path: at(path), packages: registryPackages(text, at(path)) }
1020
+ }
1021
+
1022
+ function registryPackages(text, path) {
1023
+ return [...lockOf(path, text).entries].filter(([, entries]) => entries.some((entry) => entry.from === 'registry')).map(([key]) => {
1059
1024
  const split = key.lastIndexOf('@')
1060
1025
  return { name: key.slice(0, split), version: key.slice(split + 1) }
1061
1026
  })
1062
- return { path: at(path), packages }
1063
1027
  }
1064
1028
 
1065
- // GitHub's redirects for a repository renamed or transferred.
1029
+ export async function lockedNpmPackages(dir) {
1030
+ const configPath = join(dir, CONFIG_FILE)
1031
+ const text = await readFile(configPath, 'utf8').catch((error) => {
1032
+ if (error.code !== 'ENOENT') throw unreadableLockfile(configPath, error)
1033
+ })
1034
+ const named = text === undefined ? undefined : lockfilesNamed(configPath, text)
1035
+ if (named === undefined) {
1036
+ const one = await installedNpmPackages(dir)
1037
+ return one === null ? [] : [{ ...one, name: posix.basename(one.path.split(sep).join('/')) }]
1038
+ }
1039
+ return Promise.all(named.filter((rel) => LOCKFILES.get(posix.basename(rel)) === 'npm').map(async (rel) => {
1040
+ const path = join(dir, ...rel.split('/'))
1041
+ const lock = await readLockfileText((at) => readFile(at, 'utf8'), path)
1042
+ return { path, name: rel, packages: registryPackages(lock, path) }
1043
+ }))
1044
+ }
1045
+
1066
1046
  const MOVED = new Set([301, 302, 307, 308])
1067
1047
 
1068
- // `client`'s reads of the repository `github`, followed where GitHub answers it has moved (renamed
1069
- // or transferred), as its API refuses to follow a redirect itself: the name it has now asked once,
1070
- // by the id GitHub redirects to (getRepo's followRedirect), and the read made again of it. What is
1071
- // read is held to the commit's and tree's ids all the same. `name()` the name read of.
1048
+ // GitHub's API won't follow a redirect itself; what is read is held to the commit's and tree's ids all the same.
1072
1049
  export function followingMoves(client, github) {
1073
1050
  let name = github
1074
1051
  let moved
@@ -1090,18 +1067,10 @@ export function followingMoves(client, github) {
1090
1067
  return { name: () => name, ...Object.fromEntries(methods.map((method) => [method, read(method)])) }
1091
1068
  }
1092
1069
 
1093
- // The branches compared at once, where a commit is in neither the default branch's history nor at a
1094
- // tag or a branch's head; and at most, beside the default branch.
1095
1070
  const BRANCHES_AT_ONCE = 6
1096
1071
  const MAX_COMPARED = 100
1097
1072
 
1098
- // The ref of `repository` (followingMoves's) that has `commit` in its history, the first of: its
1099
- // default branch, a tag at it, a branch at it, another branch; `{ branch }` or `{ tag }`, null for
1100
- // none. GitHub answers for a commit of any fork of a repository through the repository's own name
1101
- // too, its trees and files read there all the same: that one is the repository's own is that a ref
1102
- // of it holds it (as its compare tells, `ahead` of it or `identical`). Other branches are compared
1103
- // `maxCompared` at most, none past `deadline`: where those left uncompared might hold it, it can't
1104
- // tell (ValidateError, history-unread).
1073
+ // Null for none, as of a fork's, which GitHub serves through its name too.
1105
1074
  export async function historyOf(repository, commit, { maxCompared = MAX_COMPARED, deadline = Number.POSITIVE_INFINITY, now = Date.now } = {}) {
1106
1075
  const holds = async (ref) => {
1107
1076
  try {
@@ -1133,11 +1102,8 @@ export async function historyOf(repository, commit, { maxCompared = MAX_COMPARED
1133
1102
  return found
1134
1103
  }
1135
1104
 
1136
- // The ecosystems of the versions fetched, which a lockfile is to lock.
1137
1105
  const fetchedEcosystems = (plan) => new Set(plan.packages.filter((pkg) => FETCHED.has(pkg.ecosystem) && pkg.name && pkg.version).map((pkg) => pkg.ecosystem))
1138
1106
 
1139
- // git's entries of a listing (upstream's listRepoDir's or listRepoTree's) as validateBundle takes
1140
- // them, by their paths under `dir`: a file by its blob's id.
1141
1107
  function listedEntries(listed, dir = '') {
1142
1108
  const entries = new Map()
1143
1109
  for (const entry of listed) {
@@ -1147,22 +1113,11 @@ function listedEntries(listed, dir = '') {
1147
1113
  return entries
1148
1114
  }
1149
1115
 
1150
- // The bundle `plan` (validationPlan) validated (validateBundle) against `repository` (followingMoves's;
1151
- // null for none, its dependencies alone validated) at `commit`, under `directory`, the one it was built
1152
- // in, the commit one of the repository's own history (historyOf). Its own code, unless `own` is false,
1153
- // held to the repository's files there (repositoryFiles); or, given `listTree(paths)`, to git's blob ids
1154
- // of the commit's tree as upstream's listRepoTree lists it (`{ entries, cut }`), asked for the paths of
1155
- // the bundle's own files and of `directory`'s files, its lockfile's directories' files named by it too.
1156
- // Its dependencies held to the lockfile that installs it there (repositoryLockfiles, `lockfile` true),
1157
- // or to those `lockfile` gives (lockedBy's), or, `lockfile` false, to their registry alone. Refused
1158
- // (ValidateError): no repository or commit (`no-repository`, `no-commit`) where own code or the
1159
- // repository's lockfile is to be read, a commit of none of its own history (`commit-not-in-history`,
1160
- // `history-unread`), and no lockfile to hold the dependencies to. With `strict` false, told instead:
1161
- // own code unverified for the error's code (`own.reason`), and the dependencies held to their registry
1162
- // alone, the code told (`lockfileError`) where their lockfile went unread for it.
1163
- export async function validateRepository(plan, { repository = null, commit, directory = '' }, { own = true, lockfile = true, listTree, strict = true, fetch, deadline } = {}) {
1116
+ export async function validateRepository(plan, { repository = null, commit, directory = '' }, { own = true, lockfile = true, listTree, strict = true, fetch, deadline, generated, attested, client } = {}) {
1164
1117
  const ecosystems = fetchedEcosystems(plan)
1165
1118
  const fromRepository = lockfile === true && ecosystems.size > 0
1119
+ const beside = [...new Set(plan.packages.map((pkg) => BESIDE.get(pkg.ecosystem)).filter(Boolean))]
1120
+ const readsBeside = lockfile === true && beside.length > 0 && repository !== null && commit !== undefined
1166
1121
  const github = repository?.name()
1167
1122
  const told = (error) => {
1168
1123
  if (strict || !(error instanceof ValidateError)) throw error
@@ -1174,8 +1129,9 @@ export async function validateRepository(plan, { repository = null, commit, dire
1174
1129
  const [code, what] = repository === null ? ['no-repository', 'repository to read it of'] : ['no-commit', 'commit to read it at']
1175
1130
  throw new ValidateError(code, `no ${what}: ${own ? 'its own code' : 'its lockfile'} is read of the repository`)
1176
1131
  }
1177
- // Read at a commit of its own history alone: GitHub serves a commit of any fork of it through its name too.
1178
- ref = repository && commit !== undefined && (own || fromRepository || plan.patches.size > 0) ? await historyOf(repository, commit, { deadline }) : undefined
1132
+ // At a commit of its own history alone, as GitHub serves a commit of any fork of it through its name too.
1133
+ const pinsSubmodules = plan.packages.some((pkg) => pkg.ecosystem === 'github')
1134
+ ref = repository && commit !== undefined && (own || fromRepository || readsBeside || pinsSubmodules || plan.patches.size > 0) ? await historyOf(repository, commit, { deadline }) : undefined
1179
1135
  if (ref === null) {
1180
1136
  throw new ValidateError('commit-not-in-history', `${commit} is in the history of no branch or tag of ${repository.name()} (a fork's, say, which GitHub serves through its name too): give --sha= a commit of its own`)
1181
1137
  }
@@ -1184,8 +1140,7 @@ export async function validateRepository(plan, { repository = null, commit, dire
1184
1140
  const report = await validateBundle(plan, { reason: own ? code : 'skipped' }, { fetch, deadline })
1185
1141
  return fromRepository ? { ...report, lockfileError: code } : report
1186
1142
  }
1187
- // Own code's files, read whole (the tree's tarball), or by git's blob ids of a listing, with each
1188
- // directory's files' names; awaited later, its failure then.
1143
+ // Own code's files, read whole or listed; awaited later, its failure then.
1189
1144
  const reading = own && !listTree ? repositoryFiles(repository, { commit, directory }) : null
1190
1145
  const listing = own && listTree
1191
1146
  ? listTree([...new Set([...plan.own.map((file) => pathIn(directory, file.path)), pathIn(directory, 'package.json')])].filter(inTree)).then(({ entries, cut }) => {
@@ -1198,104 +1153,122 @@ export async function validateRepository(plan, { repository = null, commit, dire
1198
1153
  reading?.catch(() => {})
1199
1154
  listing?.catch(() => {})
1200
1155
  const listDir = (dir) => repository.listRepoDir({ sha: commit, ...(dir && { directory: dir }) })
1201
- // A directory's files' names: of the listing, where it lists the directory (or is whole); else its own.
1202
- const list = async (dir) => {
1156
+ const list = memoized(async (dir) => {
1203
1157
  const listed = await listing
1204
1158
  if (listed && (!listed.cut || listed.dirs.has(dir || '.'))) return (listed.names.get(dir || '.') ?? []).map((path) => posix.basename(path))
1205
1159
  return (await listDir(dir)).filter((entry) => entry.type === 'blob').map((entry) => entry.path)
1160
+ })
1161
+ // A directory that isn't there, as upstream tells it, holds `none`.
1162
+ const absent = (none) => (error) => {
1163
+ if (/has no directory at/u.test(error?.message ?? '')) return none
1164
+ throw error
1206
1165
  }
1207
- // A directory that isn't there, as upstream tells it: one with no files.
1208
- const absent = (error) => /has no directory at/u.test(error?.message ?? '')
1209
1166
  const under = (path) => reading && (directory === '' || path === directory || path.startsWith(`${directory}/`))
1210
- // A directory's files of the repository by their paths in it: of the tree read for own code, where
1211
- // it is under the bundle's; else its own tree, read once.
1212
- const trees = new Map()
1213
- const tree = (dir) => {
1214
- if (!trees.has(dir)) {
1215
- const prefix = dir ? `${dir}/` : ''
1216
- trees.set(dir, (under(dir) ? reading : repositoryFiles(repository, { commit, directory: dir }))
1217
- .catch((error) => {
1218
- if (absent(error)) return new Map()
1219
- throw error
1220
- })
1221
- .then((entries) => new Map([...entries].filter(([path, entry]) => path.startsWith(prefix) && entry.data).map(([path, entry]) => [path.slice(prefix.length), entry.data]))))
1222
- }
1223
- return trees.get(dir)
1224
- }
1225
- // The repository's entries at the patches the bundle holds, by git's blob ids its listings give.
1167
+ const tree = memoized((dir) => (under(dir) ? reading : repositoryFiles(repository, { commit, directory: dir }))
1168
+ .catch(absent(new Map()))
1169
+ .then((entries) => filesUnder(entries, dir)))
1226
1170
  const patchEntries = async () => {
1227
1171
  const entries = new Map()
1228
- const dirs = new Set([...plan.patches.keys()].map((path) => posix.dirname(pathIn(directory, path))))
1172
+ const dirs = new Set([...plan.patches.keys()].map((path) => repoDir(posix.dirname(pathIn(directory, path)))))
1229
1173
  await Promise.all([...dirs].map(async (dir) => {
1230
- const of = dir === '.' ? '' : dir
1231
- try {
1232
- for (const [path, entry] of listedEntries(await listDir(of), of)) entries.set(path, entry)
1233
- } catch (error) {
1234
- if (!absent(error)) throw error
1235
- }
1174
+ for (const [path, entry] of listedEntries(await listDir(dir).catch(absent([])), dir)) entries.set(path, entry)
1236
1175
  }))
1237
1176
  return entries
1238
1177
  }
1239
- // A file under the directory read of its tree, where that is being fetched whole; another alone.
1240
1178
  const readRepository = async (path) => {
1241
1179
  const held = under(path) ? (await reading).get(path)?.data : undefined
1242
1180
  return held ? asText.decode(held) : repository.getRepoFile({ path, ref: commit })
1243
1181
  }
1182
+ const git = gitReader(client)
1244
1183
  let locked = null
1245
1184
  let lockfileError
1185
+ // A patch a lockfile names by its hash alone is looked for in the project's patches directory, then the root's.
1186
+ const ofRepository = (path, besides = []) => ({
1187
+ from: 'repository', read: readRepository, tree, root: posix.relative(posix.dirname(path), directory), at: posix.relative(directory, posix.dirname(path)),
1188
+ patches: [...new Set([...besides, pathIn(directory, 'patches'), 'patches'])],
1189
+ })
1246
1190
  try {
1247
- const files = lockfile === false ? null : ecosystems.size === 0 ? [] : lockfile !== true ? lockfile
1248
- : (await repositoryLockfiles({ directory, ecosystems, list, read: readRepository })).map((one) => ({
1249
- ...one, from: 'repository', read: readRepository, tree, root: posix.relative(posix.dirname(one.path), directory),
1250
- // A patch the lockfile names by its hash alone, looked for in the project's patches directory, then the root's.
1251
- patches: [...new Set([pathIn(directory, 'patches'), 'patches'])],
1252
- }))
1191
+ const needed = lockfile === true ? fromRepository || readsBeside : ecosystems.size > 0 || beside.length > 0
1192
+ const configured = lockfile === true && needed ? await configuredLockfiles({ directory, list, read: readRepository }) : undefined
1193
+ const readOf = (path) => readLockfileText(readRepository, path)
1194
+ const files = lockfile === false ? null : !needed ? [] : lockfile !== true ? lockfile.map((one) => ({ git, ...one }))
1195
+ : configured !== undefined
1196
+ ? await Promise.all(configured.filter((path) => LOCKFILES.has(posix.basename(path))).map(async (path) => ({ path, text: await readOf(path), git, ...ofRepository(path, [pathIn(repoDir(posix.dirname(path)), 'patches')]) })))
1197
+ : [
1198
+ ...ecosystems.size > 0 ? await repositoryLockfiles({ directory, ecosystems, list, read: readRepository }) : [],
1199
+ ...readsBeside ? await besideLockfiles({ directory, names: beside, list, read: readRepository }) : [],
1200
+ ].map((one) => ({ ...one, git, ...ofRepository(one.path) }))
1253
1201
  const held = files && lockedBy(files)
1254
1202
  for (const ecosystem of held ? ecosystems : []) {
1255
- if (!held.locks(ecosystem)) throw new ValidateError('no-lockfile', `no lockfile given locks its ${ecosystem} packages: give it with --lockfile=path, or --no-lockfile to hold them to their registry alone`)
1203
+ if (held.locks(ecosystem)) continue
1204
+ throw new ValidateError('no-lockfile', configured === undefined
1205
+ ? `no lockfile given locks its ${ecosystem} packages: give it with --lockfile=path, or --no-lockfile to hold them to their registry alone`
1206
+ : `none of the lockfiles ${pathIn(directory, CONFIG_FILE)} names locks its ${ecosystem} packages: give one with --lockfile=path, or --no-lockfile to hold them to their registry alone`)
1256
1207
  }
1257
1208
  locked = held
1258
1209
  } catch (error) {
1259
1210
  lockfileError = told(error)
1260
1211
  }
1261
- // The packages fetched as the repository's tree is read, where it is; own code skipped, the patches
1262
- // the bundle holds checked all the same, where a repository and commit are given or recorded.
1263
1212
  const read = reading?.then((entries) => ({ entries })) ?? listing?.then(({ entries, cut }) => ({ entries, cut }))
1264
1213
  ?? (repository && commit !== undefined && plan.patches.size > 0 ? patchEntries().then((entries) => ({ reason: 'skipped', entries })) : null)
1265
- // Where it was read, the name it has now where it has moved.
1266
1214
  const checked = read ? read.then((found) => ({ ...found, repository: repository.name(), ...(repository.name() !== github && { movedFrom: github }), commit, ref, directory })) : { reason: 'skipped' }
1267
- const report = await validateBundle(plan, checked, { fetch, locked, deadline })
1268
- return lockfileError ? { ...report, lockfileError } : report
1215
+ const submoduleAt = async (dir) => {
1216
+ const path = pathIn(directory, dir)
1217
+ const entry = (await listDir(repoDir(posix.dirname(path))).catch(() => [])).find((one) => one.path === posix.basename(path) && one.type === 'commit')
1218
+ return entry?.sha ?? null
1219
+ }
1220
+ const submodules = ref && client ? { at: submoduleAt, read: git } : undefined
1221
+ const [report, stasisLockfile] = await Promise.all([validateBundle(plan, checked, { fetch, locked, submodules, deadline, generated }), ref && attested ? heldToCommitted(attested, directory, list, readRepository) : undefined])
1222
+ return { ...report, ...(stasisLockfile && { stasisLockfile, ...(stasisLockfile.error && { status: 'invalid' }) }), ...(lockfileError && { lockfileError }) }
1223
+ }
1224
+
1225
+ const gitReader = (client) => client && (async ({ github, commit, path }) => filesUnder(await repositoryFiles(followingMoves(client, github), { commit, directory: path }), path))
1226
+
1227
+ // Held as `stasis bundle` holds a build to the stasis.lock.json the repository commits.
1228
+ async function heldToCommitted(attested, directory, list, read) {
1229
+ const listed = await list(directory).catch((error) => {
1230
+ if (error instanceof HttpError && error.status === 404) return []
1231
+ throw error
1232
+ })
1233
+ if (!listed.includes(LOCK_FILE)) return undefined
1234
+ const path = pathIn(directory, LOCK_FILE)
1235
+ try {
1236
+ const committed = Lockfile.parse(await read(path))
1237
+ const artifact = attested()
1238
+ committed.mergeAgreeing(artifact, path, { held: true })
1239
+ // A build held to it records what it does of a module: one the artifact has none of contradicts it.
1240
+ for (const [dir, info] of artifact.modules) {
1241
+ if (committed.modules.has(dir)) assertAttested(committed.modules.get(dir), info, `${path}: module '${dir}'`)
1242
+ }
1243
+ return { path }
1244
+ } catch (error) {
1245
+ return { path, error: error.message }
1246
+ }
1269
1247
  }
1270
1248
 
1271
- // A bundle file validated (validateRepository): its own code against the repository its `repo` names
1272
- // (or `github`), at its commit (or `sha`), under its directory (or `directory`), read with `client`
1273
- // (@preventive/upstream/github.js) where it has moved too (followingMoves); its dependencies fetched
1274
- // with upstream, held to the lockfile that installs it there, or to the lockfiles at the paths
1275
- // `lockfile` gives, or, with `lockfile` false, to their registry alone. Its dependencies alone only
1276
- // where `own` is false. A bundle with no repository or commit to read its own code or its lockfile of
1277
- // is refused (ValidateError), as is one no lockfile it can read locks the dependencies of.
1278
1249
  export async function validate(file, { client, github, sha, directory, own = true, lockfile = true, fetch, deadline } = {}) {
1279
1250
  const { kind, artifact } = parseFileWithKind(file)
1280
- if (kind !== 'bundle') throw new ValidateError('not-a-bundle', `${file} is a lockfile, which holds no file to check; give a bundle`)
1281
- const plan = validationPlan(artifact)
1251
+ const plan = validationPlan(artifact, { lockfile: kind === 'lockfile' })
1252
+ const attested = () => (kind === 'lockfile' ? artifact : Lockfile.fromBundle(artifact, (path, content) => bundledIntegrity(content, artifact.formats?.get(path))))
1282
1253
  const repo = artifact.repo ?? {}
1283
1254
  const where = { github: github ?? repo.github, commit: sha ?? repo.commit, directory: directory ?? repo.directory ?? '' }
1284
1255
  const readLocal = (path) => {
1285
1256
  try {
1286
1257
  return readFileSync(path, 'utf8')
1287
1258
  } catch (error) {
1288
- throw new ValidateError('lockfile-unreadable', `${path}: ${error.message}`)
1259
+ throw unreadableLockfile(path, error)
1289
1260
  }
1290
1261
  }
1291
1262
  // Each read as it is held to, once the commit is: none where no package needs one.
1292
1263
  const lockfiles = Array.isArray(lockfile) ? lockfile.map((path) => ({ path, get text() { return readLocal(path) }, from: 'local', read: readLocal, root: where.directory })) : lockfile
1264
+ const repository = where.github === undefined ? null : followingMoves(client, where.github)
1265
+ const generated = own && client && repository && where.commit !== undefined
1266
+ ? async () => (await import('./vfs-bundle/github.js')).repoPrismaClients({ github: repository.name(), sha: where.commit, directory: where.directory, client })
1267
+ : undefined
1293
1268
  try {
1294
- return await validateRepository(plan, { repository: where.github === undefined ? null : followingMoves(client, where.github), commit: where.commit, directory: where.directory },
1295
- { own, lockfile: lockfiles, fetch, deadline })
1269
+ return await validateRepository(plan, { repository, commit: where.commit, directory: where.directory }, { own, lockfile: lockfiles, fetch, deadline, generated, attested, client })
1296
1270
  } catch (error) {
1297
1271
  if (!(error instanceof ValidateError) || !['no-repository', 'no-commit'].includes(error.code)) throw error
1298
- // What the file records none of, and what to give in its place.
1299
1272
  const fromRepository = lockfile === true && fetchedEcosystems(plan).size > 0
1300
1273
  const or = !own ? 'or --lockfile=path, or --no-lockfile to hold its dependencies to their registry alone'
1301
1274
  : fromRepository ? 'or, to validate its dependencies alone, --only-deps with --lockfile=path or --no-lockfile' : 'or --only-deps to validate its dependencies alone'
@@ -1306,41 +1279,34 @@ export async function validate(file, { client, github, sha, directory, own = tru
1306
1279
  }
1307
1280
  }
1308
1281
 
1309
- // Each status's colour: failing red, unverified yellow, valid green.
1310
- const TONES = { differs: 'red', unexpected: 'red', invalid: 'red', unverified: 'yellow', incomplete: 'yellow', valid: 'green' }
1311
-
1312
- // What a package told is of, as a table names it: an npm one by its name and version alone.
1313
- const packageName = (of) => of.replace(/^npm:/u, '')
1282
+ const TONES = { differs: 'red', unexpected: 'red', invalid: 'red', unverified: 'yellow', incomplete: 'yellow', valid: 'green', generated: 'green' }
1314
1283
 
1315
- // The report as tables on `out`: the files failing, and own ones unverified, one row for each kind of
1316
- // what they are of (own code, or a package), the worst first; then the packages with something to
1317
- // tell (why they are unverified or fail, a patch, a directory), those valid only with `showValid`.
1318
- // The summary on `err`: its status and counts, where own code was read or why not, and the lockfiles
1319
- // held to. Their statuses in colour, where each stream takes colours (`colors`, else takesColors).
1320
1284
  export function printValidationReport(report, { out = process.stdout, err = process.stderr, showValid = false, colors } = {}) {
1321
1285
  const paintOut = painter(out, colors)
1322
1286
  const paintErr = painter(err, colors)
1323
- // A row's status in its colour, and own code dimmed beside a package.
1324
1287
  const status = (paint) => (text, column, row) => paint(column === 'status' ? TONES[row.status] : column === 'of' && row.of === 'own code' ? DIM : null, text)
1325
1288
  const tables = []
1326
1289
  const files = [...Map.groupBy(report.problems, (problem) => `${problem.kind}\0${problem.of}`).values()].map((group) => ({
1327
1290
  status: group[0].kind,
1328
- of: group[0].of === 'own' ? 'own code' : packageName(group[0].of),
1291
+ of: group[0].of === 'own' ? 'own code' : group[0].of.replace(/^npm:/u, ''),
1329
1292
  files: group.map((problem) => `${problem.path}${problem.reason ? ` (${FILE_REASONS[problem.reason] ?? problem.reason})` : ''}`).join('\n'),
1330
1293
  }))
1331
1294
  if (files.length > 0) tables.push(formatTable(files, ['status', 'of', 'files'], { multiline: ['files'], paint: status(paintOut) }) + (report.moreProblems > 0 ? `\n... and ${report.moreProblems} more files` : ''))
1332
1295
  const told = report.packages.filter((pkg) => (showValid || pkg.status !== 'valid') && noteText(pkg) !== '')
1333
- // The ecosystem a column of its own only where one is not npm's.
1334
1296
  const ecosystems = told.some((pkg) => pkg.ecosystem !== 'npm')
1335
1297
  const packages = told.map((pkg) => ({ status: pkg.status, ...(ecosystems && { ecosystem: pkg.ecosystem }), package: `${pkg.name}@${pkg.version}`, note: noteText(pkg) }))
1336
1298
  if (packages.length > 0) tables.push(formatTable(packages, ['status', ...(ecosystems ? ['ecosystem'] : []), 'package', 'note'], { paint: status(paintOut) }))
1299
+ const generated = (report.generated ?? []).map((client) => ({ status: 'generated', of: generatedBy(client), files: client.files.join('\n') }))
1300
+ if (generated.length > 0) tables.push(formatTable(generated, ['status', 'of', 'files'], { multiline: ['files'], paint: status(paintOut) }))
1337
1301
  if (tables.length > 0) out.write(`${tables.join('\n\n')}\n`)
1338
1302
 
1339
1303
  const { counts, own } = report
1340
1304
  const lines = [
1341
- `${paintErr(['bold', TONES[report.status]], report.status)}: ${counts.matched} of ${counts.files} files as they should be, ${counts.differs} differ, ${counts.unexpected} unexpected, ${counts.unverified} unverified${counts.skipped ? `, ${counts.skipped} skipped` : ''}; ${counts.validPackages} of ${counts.packages} packages as published${counts.lockFailures ? `, ${counts.lockFailures} failing their lockfile` : ''}${counts.misplaced ? `, ${counts.misplaced} installed where another is` : ''}${counts.eol ? `; ${counts.eol} with a checkout's CRLF line endings` : ''}`,
1305
+ `${paintErr(['bold', TONES[report.status]], report.status)}: ${counts.matched} of ${counts.files} files as they should be, ${counts.differs} differ, ${counts.unexpected} unexpected, ${counts.unverified} unverified${counts.skipped ? `, ${counts.skipped} skipped` : ''}; ${counts.validPackages} of ${counts.packages} packages as published${counts.lockFailures ? `, ${counts.lockFailures} failing their lockfile` : ''}${counts.misplaced ? `, ${counts.misplaced} installed where another is` : ''}${counts.eol ? `; ${counts.eol} with a checkout's CRLF line endings` : ''}${counts.generated ? `; ${counts.generated} generated by the repository's Prisma schemas` : ''}`,
1342
1306
  own.checked ? `Own code held to ${own.repository}${own.movedFrom ? ` (moved from ${own.movedFrom})` : ''} at ${own.commit}${own.ref ? (own.ref.tag ? `, tag ${own.ref.tag}` : ` of ${own.ref.branch}`) : ''}${own.directory ? ` under ${own.directory}` : ''}`
1343
1307
  : paintErr('yellow', `Own code not verified: ${OWN_REASONS[own.reason] ?? own.reason}${own.patches ? `; the ${own.patches === 1 ? 'patch' : `${own.patches} patches`} the bundle holds checked all the same` : ''}`),
1308
+ ...(report.generatedError === undefined ? [] : [paintErr('yellow', `No Prisma Client generated to hold the files the repository doesn't hold to: ${report.generatedError}`)]),
1309
+ ...(report.stasisLockfile ? [report.stasisLockfile.error ? paintErr('red', `Contradicts ${report.stasisLockfile.path}: ${report.stasisLockfile.error}`) : `Held to ${report.stasisLockfile.path}`] : []),
1344
1310
  ...(report.lockfiles?.length > 0 ? [`Dependencies held to ${report.lockfiles.join(', ')}`]
1345
1311
  : report.lockfiles === null && counts.packages > 0
1346
1312
  ? [paintErr('yellow', `Dependencies held to no lockfile (${report.lockfileError ? LOCKFILE_ERRORS[report.lockfileError] ?? report.lockfileError : '--no-lockfile'}): each to its registry's integrity alone`)] : []),