@exodus/stasis 1.0.0-beta.12 → 1.0.0-beta.14

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.
@@ -0,0 +1,1241 @@
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
+ import { createHash } from 'node:crypto'
30
+ import { readFileSync } from 'node:fs'
31
+ import { posix } from 'node:path'
32
+
33
+ import { toBase32 } from '@exodus/bytes/base32.js'
34
+ import { EMPTY_MODULE_PATH, dependencyEcosystem, hasNodeModulesSegment, isStatFormat, moduleFileKey } from '@exodus/stasis-core/util'
35
+ import { decompress } from '@preventive/archive/compression.js'
36
+ import { unpack } from '@preventive/archive/tar.js'
37
+ import { getCrate } from '@preventive/upstream/cargo.js'
38
+ import { HttpError, getTarball } from '@preventive/upstream/npm.js'
39
+ import { parseCargoChecksum, parseCargoLock } from '@preventive/lockfile/cargo.js'
40
+ import { parseNpmLockfile } from '@preventive/lockfile/npm.js'
41
+ import { parsePnpmLockfile } from '@preventive/lockfile/pnpm.js'
42
+ import { parseYarn1Lockfile } from '@preventive/lockfile/yarn1.js'
43
+ import { createVfs } from '@preventive/vfs'
44
+
45
+ import { STORE_HASH, resolutionCommit } from './installed-commits.js'
46
+ import { findCargoLock } from './loaders/cargo.js'
47
+ import { parseFileWithKind } from './parse.js'
48
+ import { DIM, formatTable, painter } from './table.js'
49
+ import { lockfileOf, lockfileRoot, vfsHost } from './vfs-bundle/tree.js'
50
+
51
+ // The files told failing, and the packages told, past which they are counted.
52
+ export const MAX_TOLD = 500
53
+ // Versions fetched at once.
54
+ const PACKAGES_AT_ONCE = 6
55
+ // How many lines either side of where its header says a hunk is looked for, as patch-package does.
56
+ const FUZZ = 20
57
+ // What a tarball may unpack to, as upstream's tree verification bounds one.
58
+ const MAX_TAR_BYTES = 2 ** 30
59
+
60
+ const utf8 = new TextEncoder()
61
+ // A byte order mark kept, as Node's readFileSync keeps it, and patch-package with it: a patch applied
62
+ // changes no byte but those its hunks change.
63
+ const asText = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
64
+
65
+ // A failure to read or apply a patch, by its `code` (patch-unreadable, patch-unsupported,
66
+ // patch-does-not-apply, and patch-mismatch for one read not of the hash its lockfile names it by).
67
+ export class PatchError extends Error {
68
+ constructor(code) {
69
+ super(code)
70
+ this.name = 'PatchError'
71
+ this.code = code
72
+ }
73
+ }
74
+
75
+ // Why `validate` won't check a file, by its `code`: it is a lockfile (`not-a-bundle`); the bundle's
76
+ // own code, or its lockfile, has no repository (`no-repository`) or commit (`no-commit`) to be read
77
+ // of, given or recorded, and neither its dependencies alone (`own: false`, --only-deps) nor another
78
+ // lockfile or none (`lockfile`, --lockfile, --no-lockfile) were asked for; or no lockfile it can read
79
+ // locks its dependencies (`no-lockfile`, `lockfile-ambiguous`, `lockfile-unsupported`,
80
+ // `lockfile-unreadable`).
81
+ export class ValidateError extends Error {
82
+ constructor(code, message) {
83
+ super(message)
84
+ this.name = 'ValidateError'
85
+ this.code = code
86
+ }
87
+ }
88
+
89
+ // The ecosystem a module's files are a dependency's of, or undefined for own code: the root's and an
90
+ // npm-tagged module's outside node_modules (a workspace package's) are own code whatever they record,
91
+ // as a bundle built before only dependencies were tagged may record one.
92
+ const moduleEcosystem = (dir, ecosystem) => (dir === '.' || (ecosystem === 'npm' && !hasNodeModulesSegment(dir)) ? undefined : dependencyEcosystem(dir, ecosystem))
93
+
94
+ // What a bundle's files are, as validation checks them: its own code, `{ path, dir, rel, bytes() }`
95
+ // each, and each package version's, `{ ecosystem, name, version, files }`, one with no name or
96
+ // version among them; the patches it holds (`patches`, their text by path); and how many files.
97
+ // Directories and stat records, which are no files, and Stasis's empty stub are left out.
98
+ export function validationPlan(bundle) {
99
+ const formats = bundle.formats ?? new Map()
100
+ const own = []
101
+ const patches = new Map()
102
+ const packages = new Map()
103
+ for (const [dir, info] of bundle.modules) {
104
+ const ecosystem = moduleEcosystem(dir, info.ecosystem)
105
+ const key = `${ecosystem}\0${info.name ?? ''}\0${info.version ?? ''}`
106
+ for (const [rel, content] of Object.entries(info.files)) {
107
+ const path = moduleFileKey(dir, rel)
108
+ const format = formats.get(path)
109
+ if (typeof content !== 'string' || format === 'directory' || isStatFormat(format)) continue
110
+ const file = { path, dir, rel, format, bytes: () => (format === 'resource:base64' ? Buffer.from(content, 'base64') : utf8.encode(content)) }
111
+ if (ecosystem !== undefined) {
112
+ if (!packages.has(key)) packages.set(key, { ecosystem, name: info.name ?? '', version: info.version ?? '', files: [] })
113
+ packages.get(key).files.push(file)
114
+ } else if (path !== EMPTY_MODULE_PATH || content !== '') {
115
+ if (format === 'patch' || path.endsWith('.patch')) patches.set(path, content)
116
+ own.push(file)
117
+ }
118
+ }
119
+ }
120
+ const listed = [...packages.values()]
121
+ return { files: listed.reduce((sum, pkg) => sum + pkg.files.length, own.length), own, packages: listed, patches }
122
+ }
123
+
124
+ // Git's id of a blob of `bytes`.
125
+ export const gitBlobId = (bytes) => createHash('sha1').update(`blob ${bytes.byteLength}\0`).update(bytes).digest('hex')
126
+
127
+ // A gzipped tarball's files by their paths, its one top directory left out, as npm unpacks a package
128
+ // (and crates.io's crates, and GitHub's trees, have one too); its directories left out, and a symlink
129
+ // `{ symlink }`.
130
+ export async function tarballFiles(tarball) {
131
+ const files = new Map()
132
+ for (const entry of unpack(await decompress(tarball, 'gzip', { limit: MAX_TAR_BYTES }), { symlinksAbove: Number.MAX_SAFE_INTEGER })) {
133
+ const name = entry.name.slice(entry.name.indexOf('/') + 1)
134
+ if (!entry.name.includes('/') || name === '' || entry.type === 'directory') continue
135
+ files.set(name, entry.type === 'symlink' ? { symlink: entry.linkname } : entry.data)
136
+ }
137
+ return files
138
+ }
139
+
140
+ // Ecosystems upstream fetches a version of by its name and version alone, and that version's files,
141
+ // by their paths, as `fetchPackage` gives them; a crate's with its sha256 in hex (`checksum`), which
142
+ // a vendored copy's `.cargo-checksum.json` names. With `integrity`, the one a lockfile locks it with
143
+ // (an npm version's sha512, a crate's sha256 in hex), upstream holds the tarball to that in place of
144
+ // its registry's, and refuses one that differs (fetchFailure's `integrity-mismatch`).
145
+ export const FETCHED = new Set(['npm', 'cargo'])
146
+ const sha256 = (bytes) => createHash('sha256').update(bytes).digest('hex')
147
+ // A registry's tarball of a version, by its path: npm's, as upstream takes a dist of one, or a mirror's.
148
+ const tarballPath = (name, version) => `/${name}/-/${name.split('/').at(-1)}-${version}.tgz`
149
+ export async function fetchPackage(ecosystem, name, version, integrity) {
150
+ const tarball = ecosystem === 'npm'
151
+ ? await getTarball(name, version, integrity === undefined ? undefined : { tarball: `https://registry.npmjs.org${tarballPath(name, version)}`, integrity })
152
+ : await getCrate(name, version, integrity)
153
+ const files = new Map([...await tarballFiles(tarball)].filter(([, data]) => data instanceof Uint8Array))
154
+ // The checksum upstream held it to, where one was given.
155
+ if (ecosystem === 'cargo') files.checksum = integrity ?? sha256(tarball)
156
+ return files
157
+ }
158
+
159
+ // Whether a vendored crate's `.cargo-checksum.json` (its `bytes`) is of `crate`, its files as fetched:
160
+ // each file it lists the crate's, by its sha256, and its package checksum the crate's where the
161
+ // fetch tells it.
162
+ function checksumOf(bytes, crate) {
163
+ let parsed
164
+ try {
165
+ parsed = parseCargoChecksum(asText.decode(bytes))
166
+ } catch {
167
+ return false
168
+ }
169
+ return (crate.checksum === undefined || parsed.checksum === crate.checksum)
170
+ && Object.entries(parsed.files).every(([path, listed]) => crate.has(path) && sha256(crate.get(path)) === listed)
171
+ }
172
+ const CARGO_CHECKSUM = '.cargo-checksum.json'
173
+
174
+ // Why a version wasn't checked: none published of it (`not-published`), its registry refusing it
175
+ // (`unreadable`), its tarball not the one its lockfile locks (`integrity-mismatch`, as upstream
176
+ // refuses it), or another failure (`failed`). A `fetch` of a caller's throws upstream's HttpError,
177
+ // re-exported, for the first two.
178
+ export { HttpError }
179
+ export function fetchFailure(error) {
180
+ if (error instanceof HttpError) return error.status === 404 ? 'not-published' : [401, 403].includes(error.status) ? 'unreadable' : 'failed'
181
+ if (error?.code === 'ERR_ASSERTION' && /integrity mismatch/u.test(error.message)) return 'integrity-mismatch'
182
+ return 'failed'
183
+ }
184
+
185
+ // Whether `text` is the patch `hash` names, as pnpm hashes one: of its text with its CRLFs as LFs, a
186
+ // sha256 in hex from pnpm 10, an md5 in base32 before; or as it is.
187
+ const patchHashes = (text) => new Set([...new Set([text.replaceAll('\r\n', '\n'), text])]
188
+ .flatMap((read) => [sha256(read), toBase32(createHash('md5').update(read).digest(), { padding: false }).toLowerCase()]))
189
+ export const isPatchOf = (text, hash) => patchHashes(text).has(hash)
190
+
191
+ // A path as git writes it, unquoted where quoted (as C writes a string, bytes past ASCII in octal);
192
+ // null where it is quoted otherwise.
193
+ const ESCAPES = { a: 7, b: 8, t: 9, n: 10, v: 11, f: 12, r: 13, '"': 34, '\\': 92 }
194
+ function unquoted(path) {
195
+ if (!path.startsWith('"')) return path
196
+ const bytes = []
197
+ for (let at = 1; at < path.length; at++) {
198
+ const char = path[at]
199
+ if (char === '"') return at === path.length - 1 ? asText.decode(Uint8Array.from(bytes)) : null
200
+ if (char !== '\\') {
201
+ bytes.push(...utf8.encode(char))
202
+ continue
203
+ }
204
+ const next = path[++at] ?? ''
205
+ const octal = /^[0-3][0-7]{2}/u.exec(path.slice(at))?.[0]
206
+ if (octal) {
207
+ bytes.push(Number.parseInt(octal, 8))
208
+ at += 2
209
+ } else if (Object.hasOwn(ESCAPES, next)) bytes.push(ESCAPES[next])
210
+ else return null
211
+ }
212
+ return null
213
+ }
214
+
215
+ const unreadable = () => new PatchError('patch-unreadable')
216
+ // A path in a package: relative, of names none empty, `.` or `..`.
217
+ const inPackage = (path) => path.length > 0 && path.split('/').every((name) => name !== '' && name !== '.' && name !== '..')
218
+
219
+ // A side's path, as a `---` or `+++` line names it after its prefix, git's tab after one with a space
220
+ // in it left out; null for none (`/dev/null`).
221
+ function sidePath(named, prefix) {
222
+ const path = unquoted(named.endsWith('\t') ? named.slice(0, -1) : named)
223
+ if (path === '/dev/null') return null
224
+ if (path === null || !path.startsWith(prefix)) throw unreadable()
225
+ return path.slice(prefix.length)
226
+ }
227
+
228
+ // The path a `diff --git` line names on both sides, as git writes it of a file neither renamed nor
229
+ // copied.
230
+ function gitPath(named) {
231
+ if (named.startsWith('"')) {
232
+ const quoted = /^("(?:[^"\\]|\\.)*") ("(?:[^"\\]|\\.)*")$/u.exec(named)
233
+ const [a, b] = quoted ? [unquoted(quoted[1]), unquoted(quoted[2])] : []
234
+ return a?.startsWith('a/') && b?.startsWith('b/') && a.slice(2) === b.slice(2) ? a.slice(2) : null
235
+ }
236
+ const length = (named.length - 5) / 2
237
+ const path = named.slice(2, 2 + length)
238
+ return Number.isInteger(length) && named.startsWith('a/') && named.slice(2 + length) === ` b/${path}` ? path : null
239
+ }
240
+
241
+ // The hunk whose `@@` line is at `at` of `lines`, those before `end` read, and the line after it, as
242
+ // patch-package reads one.
243
+ function readHunk(lines, start, end) {
244
+ let at = start
245
+ const range = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/u.exec(lines[at++])
246
+ if (!range) throw unreadable()
247
+ let news = Number(range[4] ?? 1)
248
+ let olds = Number(range[2] ?? 1)
249
+ const hunk = { at: Math.max(Number(range[1]), 1) - 1, length: olds, parts: [] }
250
+ while (olds > 0 || news > 0) {
251
+ if (at >= end) throw unreadable()
252
+ const line = lines[at++]
253
+ const kind = line === '' || line === '\r' || line[0] === ' ' ? 'context' : line[0] === '-' ? 'deletion' : line[0] === '+' ? 'insertion' : null
254
+ if (!kind) throw unreadable()
255
+ let part = hunk.parts.at(-1)
256
+ if (part?.kind !== kind) hunk.parts.push(part = { kind, lines: [] })
257
+ part.lines.push(line.slice(1))
258
+ if (kind !== 'insertion') olds--
259
+ if (kind !== 'deletion') news--
260
+ if (olds < 0 || news < 0) throw unreadable()
261
+ if (lines[at]?.startsWith('\\')) {
262
+ if (!lines[at++].startsWith('\')) throw unreadable()
263
+ part.unended = true
264
+ }
265
+ }
266
+ return { hunk, next: at }
267
+ }
268
+
269
+ // A patch's files as git's diff writes them, as pnpm writes a patch: each one's path, the one it was
270
+ // renamed `from`, whether it is `deleted` or `created`, and its hunks. One whose lines end in CRLFs is
271
+ // read as patch-package reads it: its headers but their CRs, its hunks' lines with them. Throws a
272
+ // PatchError for one it can't read, or of a file's bytes.
273
+ export function parsePatch(patch) {
274
+ const lines = patch.split('\n')
275
+ const end = lines.length - (patch.endsWith('\n') ? 1 : 0)
276
+ const headerAt = (at) => lines[at]?.replace(/\r$/u, '') ?? ''
277
+ const files = []
278
+ for (let at = 0; at < lines.length;) {
279
+ const head = headerAt(at++)
280
+ if (!head.startsWith('diff --git ')) continue
281
+ let created = false
282
+ let deleted = false
283
+ let from = null
284
+ let to = null
285
+ let sides = null
286
+ for (; at < lines.length && !/^(?:diff --git |@@ |--- )/u.test(lines[at]); at++) {
287
+ const header = headerAt(at)
288
+ if (header === 'GIT binary patch' || header.startsWith('Binary files ') || header.startsWith('copy from ') || header.startsWith('copy to ')) {
289
+ throw new PatchError('patch-unsupported')
290
+ }
291
+ if (header.startsWith('deleted file mode ')) deleted = true
292
+ else if (header.startsWith('new file mode ')) created = true
293
+ else if (header.startsWith('rename from ')) from = unquoted(header.slice('rename from '.length))
294
+ else if (header.startsWith('rename to ')) to = unquoted(header.slice('rename to '.length))
295
+ }
296
+ if (lines[at]?.startsWith('--- ')) {
297
+ if (!lines[at + 1]?.startsWith('+++ ')) throw unreadable()
298
+ sides = [sidePath(headerAt(at).slice(4), 'a/'), sidePath(headerAt(at + 1).slice(4), 'b/')]
299
+ at += 2
300
+ }
301
+ const named = gitPath(head.slice('diff --git '.length))
302
+ const after = to ?? (sides ? sides[1] : named)
303
+ const before = from ?? (sides ? sides[0] : named)
304
+ if ((from === null) !== (to === null) || (deleted && created)) throw unreadable()
305
+ const path = deleted ? before : after
306
+ if (path == null || (!created && before == null) || (!deleted && after == null) || ![before, after].every((side) => side == null || inPackage(side))
307
+ || (from === null && !deleted && !created && before !== after)) throw unreadable()
308
+ const hunks = []
309
+ while (lines[at]?.startsWith('@@ ')) {
310
+ const { hunk, next } = readHunk(lines, at, end)
311
+ hunks.push(hunk)
312
+ at = next
313
+ }
314
+ files.push({ path, ...(from !== null && { from: before }), ...(deleted && { deleted: true }), ...(created && { created: true }), hunks: deleted ? [] : hunks })
315
+ }
316
+ return files
317
+ }
318
+
319
+ const doesNotApply = () => new PatchError('patch-does-not-apply')
320
+
321
+ // The changes `hunk` makes where its lines are at `start` of `lines`, as patch-package works them out:
322
+ // those it finds the same but for the space they end in; else null.
323
+ function changesAt(hunk, lines, start) {
324
+ let at = start
325
+ if (at < 0 || lines.length - at < hunk.length) return null
326
+ const changes = []
327
+ for (const part of hunk.parts) {
328
+ if (part.kind === 'insertion') {
329
+ changes.push({ splice: at, remove: 0, insert: part.lines })
330
+ if (part.unended) changes.push('pop')
331
+ continue
332
+ }
333
+ for (const line of part.lines) if (lines[at++].trimEnd() !== line.trimEnd()) return null
334
+ if (part.kind === 'deletion') {
335
+ changes.push({ splice: at - part.lines.length, remove: part.lines.length, insert: [] })
336
+ if (part.unended) changes.push('push')
337
+ }
338
+ }
339
+ return changes
340
+ }
341
+
342
+ // `text` with `hunks` applied as patch-package applies them: each found in the text as it was, where
343
+ // its header says, else as near as FUZZ lines either side, the nearer first; then its changes made in
344
+ // turn, past the lines those before added or removed.
345
+ function applied(text, hunks) {
346
+ const lines = text.split('\n')
347
+ const changes = hunks.flatMap((hunk) => {
348
+ for (let offset = 0; Math.abs(offset) <= FUZZ; offset = offset < 0 ? -offset : -offset - 1) {
349
+ const found = changesAt(hunk, lines, hunk.at + offset)
350
+ if (found) return found
351
+ }
352
+ throw doesNotApply()
353
+ })
354
+ let shift = 0
355
+ for (const change of changes) {
356
+ if (change === 'push') lines.push('')
357
+ else if (change === 'pop') lines.pop()
358
+ else {
359
+ lines.splice(change.splice + shift, change.remove, ...change.insert)
360
+ shift += change.insert.length - change.remove
361
+ }
362
+ }
363
+ return lines.join('\n')
364
+ }
365
+
366
+ // A version's `files` (bytes by path) with `patch` applied; a file it changes the text of must be
367
+ // UTF-8 text. One it creates holds its first hunk's first lines, as patch-package writes it.
368
+ export function patchedFiles(files, patch) {
369
+ const next = new Map(files)
370
+ for (const file of patch) {
371
+ if (file.created) {
372
+ const first = file.hunks[0]?.parts[0]
373
+ next.set(file.path, utf8.encode(first ? `${first.lines.join('\n')}${first.unended ? '' : '\n'}` : ''))
374
+ continue
375
+ }
376
+ const source = file.from ?? file.path
377
+ const bytes = next.get(source)
378
+ if (!bytes) throw doesNotApply()
379
+ if (file.deleted) {
380
+ next.delete(source)
381
+ continue
382
+ }
383
+ let changed = bytes
384
+ if (file.hunks.length > 0) {
385
+ let text
386
+ try {
387
+ text = asText.decode(bytes)
388
+ } catch {
389
+ throw doesNotApply()
390
+ }
391
+ changed = utf8.encode(applied(text, file.hunks))
392
+ }
393
+ if (file.from !== undefined) {
394
+ if (next.has(file.path)) throw doesNotApply()
395
+ next.delete(source)
396
+ }
397
+ next.set(file.path, changed)
398
+ }
399
+ return next
400
+ }
401
+
402
+ const equal = (a, b) => Buffer.from(a.buffer, a.byteOffset, a.byteLength).equals(b)
403
+ // Formats a CR before each line's end changes the meaning of: a binary's bytes, which no checkout
404
+ // converts, a shell script's commands and an env file's values, each its last word's own.
405
+ const CR_CHANGES = new Set(['resource:base64', 'shell', 'env'])
406
+ // `bytes`, of a file of `format`, as a checkout with CRLF line endings writes it, as LF text: every
407
+ // line's end a CRLF, as git converts a text file whole. Else undefined.
408
+ function checkedOutAsLf(bytes, format) {
409
+ if (CR_CHANGES.has(format)) return undefined
410
+ const text = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString('latin1')
411
+ return text.includes('\r\n') && !/(?<!\r)\n/u.test(text) ? Buffer.from(text.replaceAll('\r\n', '\n'), 'latin1') : undefined
412
+ }
413
+ const packageKey = ({ ecosystem, name, version }) => `${ecosystem}:${name}@${version}`
414
+ // A registry's version, as pnpm names one in its store's directories (no `file+lib`, say).
415
+ const VERSION = /^\d+\.\d+\.\d+(?:-[\w.-]+)?(?:\+[\w.-]+)?$/u
416
+ // The name an npm package's copy is installed as, by its path (after its last `node_modules/`, as
417
+ // Node resolves it), and the name and version of pnpm's store directory it is in, where it is
418
+ // (`node_modules/.pnpm/<name>@<version>/node_modules/<name>`, the name's `/` a `+`, its peers or patch
419
+ // after an `_`), as far as it names them: none where pnpm hashed a long one or one with uppercase.
420
+ function installedAt(dir) {
421
+ const segments = dir.split('/')
422
+ const at = segments.lastIndexOf('node_modules')
423
+ const as = segments.slice(at + 1).join('/')
424
+ const store = at >= 3 && segments[at - 2] === '.pnpm' && segments[at - 3] === 'node_modules' ? segments[at - 1] : undefined
425
+ if (store === undefined || STORE_HASH.test(store)) return { as }
426
+ const split = store.indexOf('@', 1)
427
+ if (split < 0) return { as }
428
+ const name = store.slice(0, split)
429
+ const version = store.slice(split + 1).split('_')[0]
430
+ return { as, store: { name: name.startsWith('@') ? name.replace('+', '/') : name, ...(VERSION.test(version) && { version }) } }
431
+ }
432
+ // Whether an npm package's copy at `dir` is installed where the bundle has it: the name its path
433
+ // installs it as its own, but an alias its lockfile has (npm's at that location, yarn's or pnpm's of
434
+ // any); pnpm's store directory above it naming it; and, where its lockfile locks each location (npm's),
435
+ // that version there. `true`, else `false` where its path or lockfile names another, else `undefined`
436
+ // where it is installed as another name no lockfile read tells of (none read, say).
437
+ function placed(pkg, dir, locked) {
438
+ const { as, store } = installedAt(dir)
439
+ const named = `${pkg.name}@${pkg.version}`
440
+ if (store && (store.name !== pkg.name || (store.version !== undefined && store.version !== pkg.version) || as !== pkg.name)) return false
441
+ const there = locked?.installed?.(dir)
442
+ if (there !== undefined) return there === named
443
+ if (as === pkg.name) return true
444
+ return locked?.aliased?.(as, named)
445
+ }
446
+ const byText = (key) => (a, b) => (key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0)
447
+ // Files told by kind, the worst first, then by what they are of, own code first, then by their paths;
448
+ // packages by status, the worst first, then by their keys: as many as MAX_TOLD keep the worst.
449
+ const KINDS = ['differs', 'unexpected', 'unverified']
450
+ const STATUSES = ['invalid', 'unverified', 'valid']
451
+ const byProblem = (a, b) => KINDS.indexOf(a.kind) - KINDS.indexOf(b.kind) || (a.of === 'own' ? 0 : 1) - (b.of === 'own' ? 0 : 1)
452
+ || byText((problem) => problem.of)(a, b) || byText((problem) => problem.path)(a, b)
453
+ const byPackage = (a, b) => STATUSES.indexOf(a.status) - STATUSES.indexOf(b.status) || byText(packageKey)(a, b)
454
+ // The name pnpm gives a version's patch file, `@scope__name@1.0.0.patch`.
455
+ const pnpmPatchName = (name, version) => `${name.replace('/', '__')}@${version}.patch`
456
+
457
+ // The patches a plan holds (`{ path, text }`), by each hash pnpm may name one by and by their file names.
458
+ function patchesOf(plan) {
459
+ const byHash = new Map()
460
+ const byName = new Map()
461
+ for (const [path, text] of plan.patches) {
462
+ const held = { path, text }
463
+ for (const hash of patchHashes(text)) if (!byHash.has(hash)) byHash.set(hash, held)
464
+ if (!byName.has(posix.basename(path))) byName.set(posix.basename(path), held)
465
+ }
466
+ return { byHash, byName }
467
+ }
468
+
469
+ // The patch an npm version is held to, and its files patched with it: the one `locked` locks it with,
470
+ // of the bundle's file of its hash (`held`, patchesOf), else read by `locked.read`; with no lockfile
471
+ // read (`locked.of` answering undefined), the bundle's named for it. None where it is patched by none;
472
+ // told with its `error` where it couldn't be read or applied. A patch the bundle holds is own code,
473
+ // taken as the repository has it alone (`verdicts`, a promise of what checking own code found of each
474
+ // file): refused where it isn't (`patch-not-in-repository`), and where that couldn't be told, its
475
+ // files patched `unverified`.
476
+ async function patchOf(held, pkg, files, locked, verdicts) {
477
+ const lock = locked?.of(pkg.name, pkg.version)
478
+ let patch
479
+ let text
480
+ try {
481
+ if (lock) {
482
+ const bundled = held.byHash.get(lock.hash)
483
+ if (bundled) {
484
+ patch = { hash: lock.hash, path: bundled.path, from: 'bundle' }
485
+ text = bundled.text
486
+ } else if (lock.path) {
487
+ patch = { hash: lock.hash, path: lock.path, from: locked.from }
488
+ text = await locked.read(lock.path)
489
+ // Held to the hash the lockfile names it by, as the bundle's are found by it.
490
+ if (!isPatchOf(text, lock.hash)) throw new PatchError('patch-mismatch')
491
+ } else {
492
+ // Named by its hash alone (pnpm 11): the file of that hash in a patches directory.
493
+ const found = await locked.locate?.(lock)
494
+ if (!found) return { patch: { hash: lock.hash, error: 'patch-not-found' }, patched: null }
495
+ patch = { hash: lock.hash, path: found.path, from: locked.from }
496
+ ;({ text } = found)
497
+ }
498
+ } else if (lock === undefined) {
499
+ const named = held.byName.get(pnpmPatchName(pkg.name, pkg.version))
500
+ if (!named) return { patched: null }
501
+ patch = { path: named.path, from: 'bundle' }
502
+ text = named.text
503
+ } else return { patched: null }
504
+ let unverified = false
505
+ if (patch.from === 'bundle') {
506
+ const verdict = (await verdicts).get(patch.path)
507
+ if (verdict === 'differs' || verdict === 'unexpected') throw new PatchError('patch-not-in-repository')
508
+ unverified = verdict !== 'matched'
509
+ patch.verified = !unverified
510
+ }
511
+ return { patch, patched: patchedFiles(files, parsePatch(text)), unverified }
512
+ } catch (error) {
513
+ return { patch: { ...patch, error: error instanceof PatchError ? error.code : 'patch-unreadable' }, patched: null }
514
+ }
515
+ }
516
+
517
+ // `items` each run through `run`, `parallel` at a time.
518
+ async function eachOf(items, parallel, run) {
519
+ let next = 0
520
+ await Promise.all(Array.from({ length: Math.min(parallel, items.length) }, async () => {
521
+ // eslint-disable-next-line no-await-in-loop -- a worker takes the next item once its own is done
522
+ while (next < items.length) await run(items[next++])
523
+ }))
524
+ }
525
+
526
+ // The bundle `plan` validated (validationPlan): its own code against `own`, the repository's files at
527
+ // its commit (`entries`, by their paths from the repository's root, each `{ data }` (bytes), `{ sha }`
528
+ // (git's blob id), `{ symlink }` or `{ submodule: true }`; `cut` where some were left unread), at
529
+ // `repository`, `commit` and `directory`, the one it was built in; or `{ reason }` it wasn't checked,
530
+ // with `entries` where the patches the bundle holds are checked all the same (`skipped`, --only-deps);
531
+ // or a promise of any, the packages fetched meanwhile.
532
+ // Each package fetched with `fetch` (fetchPackage), PACKAGES_AT_ONCE at a time, those left past
533
+ // `deadline` unverified; each as its lockfile locks it (`locked`, lockedBy): `lock(ecosystem, name,
534
+ // version)`, how (undefined where no lockfile of its ecosystem was read, null where it locks none):
535
+ // one of its registry fetched with its `integrity`, failing where upstream refuses the tarball, or
536
+ // where none is locked; one from a directory of the repository held to that directory's files there
537
+ // (`tree(directory)`, where the repository is read; else unverified); one from git or elsewhere left
538
+ // unverified. An npm version held to its files patched where pnpm patches it (`of(name, version)`,
539
+ // the patch it locks it with, null for none, undefined where no lockfile tells; `read(path)`, the
540
+ // patch's text, read of `from`), a patch the bundle holds taken as the repository has it alone.
541
+ //
542
+ // What it tells: its `status`, `valid` where every file it was to check was checked and is as it
543
+ // should be, `invalid` where one differs (`differs`) or is neither the repository's nor its package's
544
+ // (`unexpected`), a version fails its lockfile (`lockFailures`), or a copy is installed where its path
545
+ // or its lockfile names another (`misplaced`), else `incomplete`; the lockfiles
546
+ // held to (`lockfiles`, null for none); whether its own code was checked (`own`); how many files of
547
+ // each kind and outcome (`counts`, own files not checked as asked `skipped`); each file failing, and
548
+ // each own one left unverified, with what it is of (`own`, or `ecosystem:name@version`), as many as
549
+ // MAX_TOLD (`problems`, how many more `moreProblems`); and each version not wholly valid or patched
550
+ // (`packages`), a patched one's patch with, where it is valid, whether a copy of it holds its files
551
+ // patched (`applied`), and one installed from a directory of the repository its `directory`.
552
+ export async function validateBundle(plan, own, { fetch = fetchPackage, locked = null, deadline = Number.POSITIVE_INFINITY, now = Date.now } = {}) {
553
+ const problems = []
554
+ 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,
555
+ skipped: 0, packages: plan.packages.length, validPackages: 0, lockFailures: 0, misplaced: 0 }
556
+ const tell = (problem) => {
557
+ counts[problem.kind]++
558
+ problems.push(problem)
559
+ }
560
+ // What checking own code found of each own file, by its path, once it is checked: none for one not.
561
+ let checked
562
+ const verdicts = new Promise((resolve) => {
563
+ checked = resolve
564
+ })
565
+
566
+ // The packages fetched while own code is read and checked.
567
+ const told = []
568
+ const bundled = patchesOf(plan)
569
+ const fetching = eachOf(plan.packages, PACKAGES_AT_ONCE, async (pkg) => {
570
+ const of = packageKey(pkg)
571
+ const tellPackage = (status, more) => told.push({ ecosystem: pkg.ecosystem, name: pkg.name, version: pkg.version, files: pkg.files.length, status, ...more })
572
+ // Left unchecked, its files with it: unverified, or failing its lockfile (not what was installed,
573
+ // or not known to be).
574
+ const unverified = (reason, status = 'unverified', more = {}) => {
575
+ counts.unverified += pkg.files.length
576
+ tellPackage(status, { reason, ...more })
577
+ }
578
+ const fails = (reason) => {
579
+ counts.lockFailures++
580
+ unverified(reason, 'invalid')
581
+ }
582
+ if (!pkg.name || !pkg.version) return unverified('unnamed')
583
+ if (!FETCHED.has(pkg.ecosystem)) return unverified('unsupported')
584
+ // Each copy where Node resolves it by its path (a crate by its Cargo.toml alone, wherever it is):
585
+ // the version failing where any is installed where another is; those installed under another
586
+ // name no lockfile read tells of left unverified, the others checked all the same.
587
+ const dirs = [...new Set(pkg.files.map((file) => file.dir))]
588
+ const places = new Map(pkg.ecosystem === 'npm' ? dirs.map((dir) => [dir, placed(pkg, dir, locked)]) : [])
589
+ const misplaced = dirs.find((dir) => places.get(dir) === false)
590
+ if (misplaced !== undefined) {
591
+ counts.misplaced++
592
+ return unverified('misplaced', 'invalid', { at: misplaced })
593
+ }
594
+ const aliased = dirs.filter((dir) => places.has(dir) && places.get(dir) === undefined)
595
+ if (aliased.length === dirs.length) return unverified('alias-unverified', 'unverified', { at: aliased[0] })
596
+ const lock = locked?.lock?.(pkg.ecosystem, pkg.name, pkg.version)
597
+ if (lock === null) return fails('not-locked')
598
+ if (lock?.from === 'git') return unverified('git')
599
+ if (lock?.from === 'elsewhere') return unverified('not-registry')
600
+ if (lock?.from === 'local' && !locked.tree) return unverified('local')
601
+ if (lock?.conflict) return fails('integrity-conflict')
602
+ if (lock?.from === 'registry' && lock.integrity === undefined) return unverified('no-integrity')
603
+ if (now() > deadline) return unverified('timeout')
604
+ // What each copy is held to, the first it matches: the repository's directories one from a
605
+ // directory is installed from; else its files patched, where the patch is verified, then as
606
+ // published.
607
+ let candidates
608
+ let patch
609
+ let patched
610
+ let unverifiedPatch = false
611
+ try {
612
+ if (lock?.from === 'local') candidates = await Promise.all(lock.directories.map((dir) => locked.tree(dir)))
613
+ else {
614
+ const files = await fetch(pkg.ecosystem, pkg.name, pkg.version, lock?.integrity)
615
+ ;({ patch, patched, unverified: unverifiedPatch = false } = pkg.ecosystem === 'npm' ? await patchOf(bundled, pkg, files, locked, verdicts) : {})
616
+ candidates = patched && !unverifiedPatch ? [patched, files] : [files]
617
+ }
618
+ } catch (error) {
619
+ const reason = fetchFailure(error)
620
+ return reason === 'integrity-mismatch' ? fails(reason) : unverified(reason)
621
+ }
622
+ // A crate's `.cargo-checksum.json` held to the crate, as no crate holds one.
623
+ const checksum = (file) => pkg.ecosystem === 'cargo' && file.rel === CARGO_CHECKSUM
624
+ const matches = (file, bytes, candidate) => (checksum(file) ? checksumOf(bytes, candidate) : candidate.has(file.rel) && equal(bytes, candidate.get(file.rel)))
625
+ // Each installed copy apart, as one may be patched and another not.
626
+ let missed = 0
627
+ let asPatched = 0
628
+ let notVerified = 0
629
+ for (const copy of Map.groupBy(pkg.files, (file) => file.dir).values()) {
630
+ if (aliased.includes(copy[0].dir)) {
631
+ notVerified += copy.length
632
+ continue
633
+ }
634
+ const bytes = new Map(copy.map((file) => [file, file.bytes()]))
635
+ const missing = (candidate) => copy.filter((file) => !matches(file, bytes.get(file), candidate))
636
+ let [held] = candidates
637
+ let misses = missing(held)
638
+ for (const other of candidates.slice(1)) {
639
+ if (misses.length > 0 && missing(other).length === 0) [held, misses] = [other, []]
640
+ }
641
+ // As a patch not verified against the repository patches them: neither as they should be nor not.
642
+ if (misses.length > 0 && unverifiedPatch && missing(patched).length === 0) {
643
+ notVerified += copy.length
644
+ continue
645
+ }
646
+ for (const file of misses) tell({ path: file.path, kind: checksum(file) || held.has(file.rel) ? 'differs' : 'unexpected', of })
647
+ missed += misses.length
648
+ if (misses.length === 0 && held === patched) asPatched++
649
+ }
650
+ counts.matched += pkg.files.length - missed - notVerified
651
+ counts.unverified += notVerified
652
+ const status = missed > 0 ? 'invalid' : notVerified > 0 ? 'unverified' : 'valid'
653
+ if (status === 'valid') counts.validPackages++
654
+ if (status !== 'valid' || patch) {
655
+ tellPackage(status, {
656
+ ...(status === 'unverified' && (aliased.length > 0 ? { reason: 'alias-unverified', at: aliased[0] } : { reason: 'patch-unverified' })),
657
+ ...(patch && { patch: { ...patch, ...(patched && status === 'valid' && { applied: asPatched > 0 }) } }),
658
+ ...(lock?.from === 'local' && { directory: lock.directories[0] }),
659
+ })
660
+ }
661
+ return undefined
662
+ })
663
+
664
+ let read
665
+ const found = new Map()
666
+ try {
667
+ read = await own
668
+ const skipped = 'reason' in read
669
+ // The submodules' paths, worked out where a file is not in the tree.
670
+ let submodules
671
+ const inSubmodule = (path) => (submodules ??= [...read.entries].filter(([, entry]) => entry.submodule).map(([at]) => `${at}/`)).some((prefix) => path.startsWith(prefix))
672
+ for (const file of plan.own) {
673
+ // Own code unchecked, but for the patches the bundle holds where the repository is read: skipped
674
+ // as asked (--only-deps), else unverified.
675
+ if (skipped && !(read.entries && plan.patches.has(file.path))) {
676
+ counts[read.reason === 'skipped' ? 'skipped' : 'unverified']++
677
+ continue
678
+ }
679
+ const path = read.directory ? `${read.directory}/${file.path}` : file.path
680
+ const entry = read.entries.get(path)
681
+ const verdict = (kind, reason) => {
682
+ if (kind === 'matched') counts.matched++
683
+ else tell({ path: file.path, kind, of: 'own', ...(reason && { reason }) })
684
+ found.set(file.path, kind)
685
+ }
686
+ if (entry === undefined || entry.submodule) {
687
+ const reason = entry?.submodule || inSubmodule(path) ? 'submodule' : read.cut ? 'tree-cut' : undefined
688
+ verdict(reason === 'tree-cut' ? 'unverified' : 'unexpected', reason)
689
+ } else if (entry.symlink !== undefined) verdict('differs', 'symlink')
690
+ else {
691
+ const bytes = file.bytes()
692
+ const same = (candidate) => (entry.data === undefined ? gitBlobId(candidate) === entry.sha : equal(candidate, entry.data))
693
+ if (same(bytes)) verdict('matched')
694
+ else {
695
+ // As a checkout with CRLF line endings writes it, of the file the repository has with LFs.
696
+ const lf = checkedOutAsLf(bytes, file.format)
697
+ const crlf = lf !== undefined && same(lf)
698
+ if (crlf) counts.eol++
699
+ verdict(crlf ? 'matched' : 'differs')
700
+ }
701
+ }
702
+ }
703
+ } finally {
704
+ checked(found)
705
+ }
706
+ await fetching
707
+
708
+ const skipped = 'reason' in read
709
+ const patchesChecked = skipped && read.entries ? [...plan.own].filter((file) => plan.patches.has(file.path)).length : 0
710
+ return {
711
+ validatedAt: now(),
712
+ status: counts.differs + counts.unexpected + counts.lockFailures + counts.misplaced > 0 ? 'invalid' : counts.unverified > 0 ? 'incomplete' : 'valid',
713
+ own: skipped ? { checked: false, reason: read.reason, ...(patchesChecked > 0 && { patches: patchesChecked }) }
714
+ : { 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 }) },
715
+ lockfiles: locked?.lockfiles ?? null,
716
+ counts,
717
+ problems: problems.toSorted(byProblem).slice(0, MAX_TOLD),
718
+ moreProblems: Math.max(0, problems.length - MAX_TOLD),
719
+ packages: told.toSorted(byPackage).slice(0, MAX_TOLD),
720
+ }
721
+ }
722
+
723
+ // The exit code a report's validation stands for: 0 valid, own code skipped as asked aside; 1
724
+ // invalid; 2 incomplete, where a file it was to check went unverified, unless `allowUnverified`.
725
+ export const exitCodeOf = (report, { allowUnverified = false } = {}) => (report.status === 'invalid' ? 1 : report.status === 'incomplete' && !allowUnverified ? 2 : 0)
726
+
727
+ // The repository's files under `directory` at `commit`, as `validateBundle` takes them (by their paths
728
+ // from the repository's root): its tree's tarball, which upstream holds to the tree's id, its symlinks
729
+ // climbing as far as the repository's root.
730
+ export async function repositoryFiles(client, { github, commit, directory = '' }) {
731
+ const tree = await client.getRepoTreeId({ repo: github, sha: commit, ...(directory && { directory }) })
732
+ const tarball = await client.getRepoTreeTarball({ repo: github, tree, symlinksAbove: directory ? directory.split('/').length - 1 : 0 })
733
+ const entries = new Map()
734
+ for (const [path, value] of await tarballFiles(tarball)) entries.set(directory ? `${directory}/${path}` : path, value instanceof Uint8Array ? { data: value } : value)
735
+ return entries
736
+ }
737
+
738
+ // The lockfiles validation reads, by their names, and the ecosystem each locks the packages of.
739
+ const LOCKFILES = new Map([['pnpm-lock.yaml', 'npm'], ['npm-shrinkwrap.json', 'npm'], ['package-lock.json', 'npm'], ['yarn.lock', 'npm'], ['Cargo.lock', 'cargo']])
740
+ // crates.io's index, by the sources a Cargo.lock names it by.
741
+ const CRATES_IO = new Set(['registry+https://github.com/rust-lang/crates.io-index', 'sparse+https://index.crates.io/'])
742
+
743
+ // Whether `tarball` is a registry's of `name` and `version`, npm's or a mirror's of it, by its path:
744
+ // none, as npm writes one of its registry's where set to, is.
745
+ function registryTarball(tarball, name, version) {
746
+ if (tarball === undefined) return true
747
+ const url = URL.parse(tarball)
748
+ return url !== null && /^https?:$/u.test(url.protocol) && url.pathname.endsWith(tarballPath(name, version))
749
+ }
750
+
751
+ // The one sha512 an SRI holds, as upstream takes one; undefined where it holds none, or two.
752
+ function sha512Of(integrity) {
753
+ const found = new Set((integrity ?? '').split(/\s+/u).filter((token) => token.startsWith('sha512-')).map((token) => token.split('?')[0]))
754
+ return found.size === 1 ? [...found][0] : undefined
755
+ }
756
+
757
+ // A text without the byte order mark it starts with, as npm, pnpm and yarn read their lockfiles.
758
+ const withoutBom = (text) => (text.charCodeAt(0) === 0xFE_FF ? text.slice(1) : text)
759
+
760
+ // Where a lockfile's entry of a version comes from: git (a git repository, or a git host's tarball,
761
+ // installed-commits.js's resolutionCommit), its `registry`, with its `integrity`, or elsewhere.
762
+ const lockedFrom = (git, registry, integrity) => (git ? { from: 'git' } : registry ? { from: 'registry', integrity } : { from: 'elsewhere' })
763
+ const fromGit = (resolution) => resolution?.type === 'git' || resolution?.gitHosted === true || resolutionCommit(resolution) !== undefined
764
+
765
+ // A dependency's spec naming a directory of the project, `file:` or `link:` and its path, a tarball's
766
+ // aside; else undefined.
767
+ function localDirectory(spec) {
768
+ const path = /^(?:file|link):(.+)$/u.exec(spec)?.[1]
769
+ return path === undefined || /\.(?:tgz|tar\.gz|tar)$/u.test(path) ? undefined : path
770
+ }
771
+
772
+ // What the lockfile `name` (its file name) of `text` locks: each version by name and version, an
773
+ // entry for each time it locks it, lockedFrom's (an npm version's sha512, a crate's sha256); each
774
+ // package installed from a directory of the project (a `file:` or `link:` one, an npm link), by its
775
+ // name, the directories from the lockfile's (`locals`), which lock no version; and, of pnpm's, the
776
+ // patch it locks each version with, null for none (`hashes`), and each patch's file (`paths`). Its
777
+ // BOM passed over but Cargo.lock's, as cargo reads that as it is.
778
+ function readLockfile(name, read) {
779
+ const text = name === 'Cargo.lock' ? read : withoutBom(read)
780
+ const entries = new Map()
781
+ const locals = new Map()
782
+ const add = (pkg, version, entry) => {
783
+ const key = `${pkg}@${version}`
784
+ entries.set(key, [...(entries.get(key) ?? []), entry])
785
+ }
786
+ const local = (pkg, dir) => locals.set(pkg, new Set([...(locals.get(pkg) ?? []), posix.normalize(dir)]))
787
+ // Each name a version is installed as in place of its own (an alias, `foo: npm:ms@2.1.3`), as
788
+ // `<name it is installed as>\0<its name>@<its version>`.
789
+ const aliases = new Set()
790
+ if (name === 'pnpm-lock.yaml') {
791
+ const { lockfile } = parsePnpmLockfile(text)
792
+ // pnpm locks an alias's dependency by its name and version (`foo: ms@2.1.3`), a version alone otherwise.
793
+ for (const node of [...Object.values(lockfile?.importers ?? {}), ...Object.values(lockfile?.packages ?? {})]) {
794
+ for (const [as, ref] of Object.entries({ ...node.dependencies, ...node.devDependencies, ...node.optionalDependencies })) {
795
+ const split = typeof ref === 'string' ? ref.indexOf('@', 1) : -1
796
+ if (split > 0 && VERSION.test(ref.slice(split + 1).split('(')[0])) aliases.add(`${as}\0${ref.slice(0, split)}@${ref.slice(split + 1).split('(')[0]}`)
797
+ }
798
+ }
799
+ const hashes = new Map()
800
+ for (const [key, pkg] of Object.entries(lockfile?.packages ?? {})) {
801
+ // A directory has no version in the lockfile: pnpm reads the one it has as it installs it.
802
+ if (pkg.resolution.type === 'directory') local(pkg.name, pkg.resolution.directory)
803
+ if (pkg.version === undefined) continue
804
+ // A registry version's key is its name and version, its peers and patch after them.
805
+ const ref = key.slice(pkg.name.length + 1)
806
+ add(pkg.name, pkg.version, lockedFrom(fromGit(pkg.resolution), ref === pkg.version || ref.startsWith(`${pkg.version}(`), sha512Of(pkg.resolution.integrity)))
807
+ hashes.set(`${pkg.name}@${pkg.version}`, pkg.patchHash ?? null)
808
+ }
809
+ // Each patch by its hash: its file, where the lockfile names one, and the selectors naming it.
810
+ const paths = new Map()
811
+ for (const [selector, { hash, path }] of Object.entries(lockfile?.patchedDependencies ?? {})) {
812
+ paths.set(hash, { path: paths.get(hash)?.path ?? path, selectors: [...(paths.get(hash)?.selectors ?? []), selector] })
813
+ }
814
+ return { entries, locals, aliases, patches: { hashes, paths } }
815
+ }
816
+ if (name === 'Cargo.lock') {
817
+ 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))
818
+ return { entries }
819
+ }
820
+ let packages
821
+ // npm's, by each location it installs a version at, from its directory: `<name>@<version>`; and
822
+ // the locations it links to a directory, held to that directory (`locals`) in place of a version.
823
+ let locations
824
+ let links
825
+ let importers
826
+ if (name === 'yarn.lock') packages = new Set(Object.values(parseYarn1Lockfile(text, { checkVersions: false }).packages))
827
+ else {
828
+ const lockfile = parseNpmLockfile(text, { checkVersions: false })
829
+ locations = new Map(Object.entries(lockfile.packages).filter(([, pkg]) => pkg.version !== undefined).map(([location, pkg]) => [location, `${pkg.name}@${pkg.version}`]))
830
+ importers = new Set(Object.keys(lockfile.importers))
831
+ links = new Set(Object.keys(lockfile.links))
832
+ // npm links a directory it installs, by its location, to the directory: the package there.
833
+ for (const [location, target] of Object.entries(lockfile.links)) {
834
+ if (Object.hasOwn(lockfile.importers, target)) local(location.slice(location.lastIndexOf('node_modules/') + 'node_modules/'.length), target)
835
+ }
836
+ packages = Object.values(lockfile.packages)
837
+ }
838
+ for (const { name: pkg, version, resolution, patterns } of packages) {
839
+ // yarn locks an alias by its pattern, `foo@npm:ms@^2.1.0`.
840
+ for (const pattern of patterns ?? []) {
841
+ const split = pattern.indexOf('@', 1)
842
+ if (pattern.startsWith('npm:', split + 1)) aliases.add(`${pattern.slice(0, split)}\0${pkg}@${version}`)
843
+ }
844
+ // yarn locks a directory by its spec alone, as it last read the version there.
845
+ const dir = resolution === undefined ? patterns?.map((pattern) => localDirectory(pattern.slice(pattern.indexOf('@', 1) + 1))).find(Boolean) : undefined
846
+ if (dir !== undefined) local(pkg, dir)
847
+ else if (version !== undefined) add(pkg, version, lockedFrom(fromGit(resolution), resolution?.type === 'tarball' && registryTarball(resolution.tarball, pkg, version), sha512Of(resolution?.integrity)))
848
+ }
849
+ return { entries, locals, aliases, ...(locations && { locations, links, importers }) }
850
+ }
851
+
852
+ // What `lockfiles` lock, as validateBundle takes it (`locked`), one of each ecosystem: each `{ path,
853
+ // text, from, read, tree, patches }`, its path (as it is told), what it is read of (`repository`, or
854
+ // `local`), `read(path)` reading a file by a path as it is (a pnpm patch's, by the lockfile's
855
+ // directory and the path it names), each read once, `tree(dir)` the files of a directory of the
856
+ // repository by their paths in it, where the repository is read, and `patches` the directories a
857
+ // patch it names by its hash alone (pnpm 11's) is looked for in, in turn, beside the lockfile's
858
+ // directory where none is given: `locate(lock)` finds it, the file there of pnpm's name for it and of
859
+ // that hash. Their paths (`lockfiles`), and whether one locks an ecosystem's packages
860
+ // (`locks(ecosystem)`), told too.
861
+ export function lockedBy(lockfiles) {
862
+ const by = new Map()
863
+ for (const { path, text, from, read, tree, patches, root = '' } of lockfiles) {
864
+ const ecosystem = LOCKFILES.get(posix.basename(path))
865
+ if (ecosystem === undefined) throw new ValidateError('lockfile-unsupported', `${path}: not a lockfile validate reads (${[...LOCKFILES.keys()].join(', ')})`)
866
+ if (by.has(ecosystem)) throw new ValidateError('lockfile-ambiguous', `${by.get(ecosystem).path} and ${path} both lock ${ecosystem} packages: give one`)
867
+ let locks
868
+ try {
869
+ locks = readLockfile(posix.basename(path), text)
870
+ } catch (error) {
871
+ throw new ValidateError('lockfile-unreadable', `${path}: ${error.message}`)
872
+ }
873
+ // The bundle's root by its path from the lockfile's directory, where npm's locks each location
874
+ // from there: on disk, the one of its importers it names, else the lockfile's own directory.
875
+ const within = locks.importers === undefined || from !== 'local' || locks.importers.has(root) ? root : ''
876
+ by.set(ecosystem, { ...locks, path, from, read, tree, root: within, patchDirs: patches ?? [posix.join(posix.dirname(path), 'patches')] })
877
+ }
878
+ const npm = by.get('npm')
879
+ const reads = new Map()
880
+ return {
881
+ lockfiles: [...by.values()].map(({ path }) => path),
882
+ locks: (ecosystem) => by.has(ecosystem),
883
+ lock(ecosystem, name, version) {
884
+ const entries = by.get(ecosystem)?.entries
885
+ if (entries === undefined) return undefined
886
+ const locks = entries.get(`${name}@${version}`)
887
+ if (locks === undefined) {
888
+ // One of the project's directories, by its path from the repository's root, '' the root.
889
+ const { locals, path } = by.get(ecosystem)
890
+ const dirs = locals?.get(name)
891
+ const fromRoot = (dir) => (dir === '.' ? '' : dir)
892
+ return dirs ? { from: 'local', directories: [...dirs].map((dir) => fromRoot(posix.join(posix.dirname(path), dir))) } : null
893
+ }
894
+ const integrities = new Set(locks.filter((entry) => entry.from === 'registry').map((entry) => entry.integrity))
895
+ if (integrities.size === 0) return { from: locks.every((entry) => entry.from === 'git') ? 'git' : 'elsewhere' }
896
+ return integrities.size === 1 ? { from: 'registry', integrity: [...integrities][0] } : { from: 'registry', conflict: true }
897
+ },
898
+ of(name, version) {
899
+ if (npm?.patches === undefined) return undefined
900
+ const hash = npm.patches.hashes.get(`${name}@${version}`)
901
+ if (hash == null) return hash
902
+ const { path, selectors = [] } = npm.patches.paths.get(hash) ?? {}
903
+ if (path) return { hash, path: posix.join(posix.dirname(npm.path), path) }
904
+ // The names pnpm gives its file, by each selector naming it and by the version it patches.
905
+ return { hash, names: [...new Set([...selectors.map((selector) => `${selector.replace('/', '__')}.patch`), pnpmPatchName(name, version)])] }
906
+ },
907
+ async locate({ hash, names = [] }) {
908
+ for (const path of npm.patchDirs.flatMap((dir) => names.map((name) => (dir ? `${dir}/${name}` : name)))) {
909
+ let text
910
+ try {
911
+ // eslint-disable-next-line no-await-in-loop -- the first of them that is there
912
+ text = await this.read(path)
913
+ } catch {
914
+ continue // none there
915
+ }
916
+ if (isPatchOf(text, hash)) return { path, text }
917
+ }
918
+ return null
919
+ },
920
+ // The version an npm lockfile installs at the bundle's `dir` (`<name>@<version>`), null for none,
921
+ // undefined where its lockfile locks no location (pnpm's, yarn's) or links it to a directory.
922
+ installed(dir) {
923
+ if (npm?.locations === undefined) return undefined
924
+ const location = npm.root ? `${npm.root}/${dir}` : dir
925
+ return npm.links.has(location) ? undefined : npm.locations.get(location) ?? null
926
+ },
927
+ // Whether the npm lockfile installs `named` (`<name>@<version>`) under the name `as`; undefined for none read.
928
+ aliased: (as, named) => npm?.aliases.has(`${as}\0${named}`),
929
+ from: npm?.from,
930
+ tree: npm?.tree,
931
+ read(path) {
932
+ if (!reads.has(path)) reads.set(path, Promise.resolve().then(() => npm.read(path)))
933
+ return reads.get(path)
934
+ },
935
+ }
936
+ }
937
+
938
+ // The lockfiles that install `directory` of a repository, for the packages of `ecosystems`, as
939
+ // lockedBy takes them (but `from` and `read`), as the build finds them: an npm package's of the one
940
+ // package manager whose root (vfs-bundle's tree's lockfileRoot) holds its lockfile, pnpm's
941
+ // (pnpm-lock.yaml), npm's (package-lock.json, or npm-shrinkwrap.json, which npm takes before it) or
942
+ // yarn 1's (yarn.lock); a crate's the Cargo.lock beside its workspace's root (findCargoLock). Those
943
+ // read of a sparse tree of the directories at and above it, their files' names listed and the
944
+ // manifests the roots are found by read. Refused (ValidateError) where none is, or more than one
945
+ // package manager's is. `read(path)` reads a file of the repository, `list(dir)` the names of the
946
+ // files a directory has.
947
+ export async function repositoryLockfiles({ directory = '', ecosystems, list, read }) {
948
+ const dirs = []
949
+ for (let dir = directory; ; dir = dir.includes('/') ? dir.slice(0, dir.lastIndexOf('/')) : '') {
950
+ dirs.push(dir)
951
+ if (dir === '') break
952
+ }
953
+ const listed = await Promise.all(dirs.map(list))
954
+ const at = (dir, name) => (dir ? `${dir}/${name}` : name)
955
+ const where = directory || 'the repository\'s root'
956
+ // The manifests a root is found by, read where a lockfile they could pick is listed: package.json
957
+ // for npm's and yarn's workspaces, Cargo.toml for cargo's.
958
+ const lists = (...names) => listed.some((names_) => names.some((name) => names_.includes(name)))
959
+ const manifests = new Set([
960
+ ...ecosystems.has('npm') && lists('package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock') ? ['package.json'] : [],
961
+ ...ecosystems.has('cargo') ? ['Cargo.toml'] : [],
962
+ ])
963
+ // Each directory there, whatever it holds of those.
964
+ const files = Object.fromEntries(dirs.filter(Boolean).map((dir) => [dir, { type: 'directory' }]))
965
+ await Promise.all(dirs.flatMap((dir, index) => listed[index].map(async (name) => {
966
+ if (manifests.has(name)) files[at(dir, name)] = await read(at(dir, name))
967
+ // npm reads npm-shrinkwrap.json in place of package-lock.json, at the root it finds by either.
968
+ else if (LOCKFILES.has(name) || name === 'pnpm-workspace.yaml') files[at(dir, name === 'npm-shrinkwrap.json' ? 'package-lock.json' : name)] ??= ''
969
+ })))
970
+ const host = vfsHost(createVfs(files))
971
+ const cwd = posix.resolve('/', directory || '.')
972
+ const fromRoot = (path) => path.slice(1)
973
+
974
+ const chosen = []
975
+ if (ecosystems.has('npm')) {
976
+ const found = []
977
+ for (const pm of ['pnpm', 'npm', 'yarn1']) {
978
+ let root
979
+ try {
980
+ root = lockfileRoot(host, pm, cwd)
981
+ } catch (error) {
982
+ throw new ValidateError('lockfile-unreadable', `${where}'s ${pm} root: ${error.message}`)
983
+ }
984
+ if (root === null) continue
985
+ const names = listed[dirs.indexOf(fromRoot(root))]
986
+ found.push(at(fromRoot(root), pm === 'npm' && names.includes('npm-shrinkwrap.json') ? 'npm-shrinkwrap.json' : lockfileOf(pm)))
987
+ }
988
+ if (found.length === 0) {
989
+ const bun = ['bun.lock', 'bun.lockb'].find((name) => lists(name))
990
+ throw bun === undefined
991
+ ? new ValidateError('no-lockfile', `no lockfile at or above ${where} locks its npm packages (pnpm-lock.yaml, package-lock.json, npm-shrinkwrap.json, yarn.lock): give --lockfile=path, or --no-lockfile to hold them to their registry alone`)
992
+ : new ValidateError('lockfile-unsupported', `${bun}, which validate doesn't read, locks its npm packages: give --lockfile=path, or --no-lockfile to hold them to their registry alone`)
993
+ }
994
+ if (found.length > 1) throw new ValidateError('lockfile-ambiguous', `more than one lockfile installs ${where}: ${found.join(', ')}; give --lockfile=path`)
995
+ chosen.push(found[0])
996
+ }
997
+ if (ecosystems.has('cargo')) {
998
+ let lock
999
+ try {
1000
+ lock = findCargoLock(cwd, host)
1001
+ } catch (error) {
1002
+ throw new ValidateError('lockfile-unreadable', `${where}'s Cargo workspace: ${error.message}`)
1003
+ }
1004
+ 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`)
1005
+ chosen.push(fromRoot(lock))
1006
+ }
1007
+ return Promise.all(chosen.map(async (path) => ({ path, text: await read(path) })))
1008
+ }
1009
+
1010
+ // GitHub's redirects for a repository renamed or transferred.
1011
+ const MOVED = new Set([301, 302, 307, 308])
1012
+
1013
+ // `client`'s reads of the repository `github`, followed where GitHub answers it has moved (renamed
1014
+ // or transferred), as its API refuses to follow a redirect itself: the name it has now asked once,
1015
+ // by the id GitHub redirects to (getRepo's followRedirect), and the read made again of it. What is
1016
+ // read is held to the commit's and tree's ids all the same. `name()` the name read of.
1017
+ export function followingMoves(client, github) {
1018
+ let name = github
1019
+ let moved
1020
+ const read = (method) => async (options) => {
1021
+ try {
1022
+ return await client[method]({ ...options, repo: name })
1023
+ } catch (error) {
1024
+ if (!(error instanceof HttpError && MOVED.has(error.status))) throw error
1025
+ moved ??= client.getRepo({ repo: github, followRedirect: true }).then((info) => {
1026
+ name = info.full_name
1027
+ return name
1028
+ })
1029
+ await moved
1030
+ if (name === github) throw error
1031
+ return client[method]({ ...options, repo: name })
1032
+ }
1033
+ }
1034
+ const methods = ['getRepoTreeId', 'getRepoTreeTarball', 'listRepoDir', 'getRepoFile', 'getRepoHead', 'listRepoTags', 'listRepoBranches', 'compareCommits']
1035
+ return { name: () => name, ...Object.fromEntries(methods.map((method) => [method, read(method)])) }
1036
+ }
1037
+
1038
+ // The branches compared at once, where a commit is in neither the default branch's history nor at a
1039
+ // tag or a branch's head.
1040
+ const BRANCHES_AT_ONCE = 6
1041
+
1042
+ // The ref of `repository` (followingMoves's) that has `commit` in its history, the first of: its
1043
+ // default branch, a tag at it, a branch at it, another branch; `{ branch }` or `{ tag }`, null for
1044
+ // none. GitHub answers for a commit of any fork of a repository through the repository's own name
1045
+ // too, its trees and files read there all the same: that one is the repository's own is that a ref
1046
+ // of it holds it (as its compare tells, `ahead` of it or `identical`).
1047
+ export async function historyOf(repository, commit) {
1048
+ const holds = async (ref) => {
1049
+ try {
1050
+ return ['ahead', 'identical'].includes((await repository.compareCommits({ base: commit, head: ref })).status)
1051
+ } catch (error) {
1052
+ // A commit GitHub has none of, or none in common with the ref.
1053
+ if (error instanceof HttpError && error.status === 404) return false
1054
+ throw error
1055
+ }
1056
+ }
1057
+ const head = await repository.getRepoHead({})
1058
+ if (head.oid === commit || (await holds(head.branch))) return { branch: head.branch }
1059
+ const tag = (await repository.listRepoTags({})).find((entry) => entry.oid === commit)
1060
+ if (tag) return { tag: tag.tag }
1061
+ const branches = (await repository.listRepoBranches({})).filter((entry) => entry.branch !== head.branch)
1062
+ const at = branches.find((entry) => entry.oid === commit)
1063
+ if (at) return { branch: at.branch }
1064
+ let found = null
1065
+ await eachOf(branches, BRANCHES_AT_ONCE, async ({ branch }) => {
1066
+ if (found === null && (await holds(branch))) found ??= { branch }
1067
+ })
1068
+ return found
1069
+ }
1070
+
1071
+ // A bundle file validated (validateBundle): its own code against the repository its `repo` names (or
1072
+ // `github`), at its commit (or `sha`), under its directory (or `directory`), read with `client`
1073
+ // (@preventive/upstream/github.js); its dependencies fetched with upstream, held to the lockfile that
1074
+ // installs it there (repositoryLockfiles), or to the lockfiles at the paths `lockfile` gives, or, with
1075
+ // `lockfile` false, to their registry alone. Its dependencies alone only where `own` is false. A
1076
+ // bundle with no repository or commit to read its own code or its lockfile of is refused
1077
+ // (ValidateError), as is one no lockfile it can read locks the dependencies of.
1078
+ export async function validate(file, { client, github, sha, directory, own = true, lockfile = true, fetch, deadline } = {}) {
1079
+ const { kind, artifact } = parseFileWithKind(file)
1080
+ if (kind !== 'bundle') throw new ValidateError('not-a-bundle', `${file} is a lockfile, which holds no file to check; give a bundle`)
1081
+ const plan = validationPlan(artifact)
1082
+ // The ecosystems of the versions fetched, which a lockfile is to lock.
1083
+ const ecosystems = new Set(plan.packages.filter((pkg) => FETCHED.has(pkg.ecosystem) && pkg.name && pkg.version).map((pkg) => pkg.ecosystem))
1084
+ const fromRepository = lockfile === true && ecosystems.size > 0
1085
+ const repo = artifact.repo ?? {}
1086
+ const where = { github: github ?? repo.github, commit: sha ?? repo.commit, directory: directory ?? repo.directory ?? '' }
1087
+ if (own || fromRepository) {
1088
+ const or = !own ? 'or --lockfile=path, or --no-lockfile to hold its dependencies to their registry alone'
1089
+ : fromRepository ? 'or, to validate its dependencies alone, --only-deps with --lockfile=path or --no-lockfile' : 'or --only-deps to validate its dependencies alone'
1090
+ const what = own ? 'its own code' : 'its lockfile'
1091
+ if (where.github === undefined) throw new ValidateError('no-repository', `${file} records no repository to read ${what} of: give --github=owner/name and --sha=commit, ${or}`)
1092
+ if (where.commit === undefined) throw new ValidateError('no-commit', `${file} records no commit to read ${what} at: give --sha=commit, ${or}`)
1093
+ }
1094
+ // The repository read where it has moved too, at a commit of its own history alone: GitHub serves
1095
+ // a commit of any fork of it through its name too.
1096
+ const repository = where.github === undefined ? null : followingMoves(client, where.github)
1097
+ const readsCommit = own || fromRepository || (plan.patches.size > 0 && where.commit !== undefined)
1098
+ const ref = repository && readsCommit && where.commit !== undefined ? await historyOf(repository, where.commit) : undefined
1099
+ if (ref === null) {
1100
+ throw new ValidateError('commit-not-in-history', `${where.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`)
1101
+ }
1102
+ const reading = own ? repositoryFiles(repository, where) : null
1103
+ // Awaited later, its failure then: none meanwhile.
1104
+ reading?.catch(() => {})
1105
+ const list = async (dir) => (await repository.listRepoDir({ sha: where.commit, ...(dir && { directory: dir }) }))
1106
+ .filter((entry) => entry.type === 'blob').map((entry) => entry.path)
1107
+ // A directory that isn't there, as upstream tells it: one with no files.
1108
+ const absent = (error) => /has no directory at/u.test(error?.message ?? '')
1109
+ // A directory's files of the repository by their paths in it: of the tree read for own code, where
1110
+ // it is under the bundle's; else its own tree, read once.
1111
+ const trees = new Map()
1112
+ const tree = (dir) => {
1113
+ if (!trees.has(dir)) {
1114
+ const prefix = dir ? `${dir}/` : ''
1115
+ const under = reading && (where.directory === '' || dir === where.directory || dir.startsWith(`${where.directory}/`))
1116
+ trees.set(dir, (under ? reading : repositoryFiles(repository, { ...where, directory: dir }))
1117
+ .catch((error) => {
1118
+ if (absent(error)) return new Map()
1119
+ throw error
1120
+ })
1121
+ .then((entries) => new Map([...entries].filter(([path, entry]) => path.startsWith(prefix) && entry.data).map(([path, entry]) => [path.slice(prefix.length), entry.data]))))
1122
+ }
1123
+ return trees.get(dir)
1124
+ }
1125
+ // The repository's entries at the patches the bundle holds, by git's blob ids its listings give.
1126
+ const patchEntries = async () => {
1127
+ const entries = new Map()
1128
+ const paths = [...plan.patches.keys()].map((path) => (where.directory ? `${where.directory}/${path}` : path))
1129
+ await Promise.all([...new Set(paths.map((path) => posix.dirname(path)))].map(async (dir) => {
1130
+ let listed
1131
+ try {
1132
+ listed = await repository.listRepoDir({ sha: where.commit, ...(dir !== '.' && { directory: dir }) })
1133
+ } catch (error) {
1134
+ if (absent(error)) return
1135
+ throw error
1136
+ }
1137
+ for (const entry of listed) {
1138
+ const value = entry.type === 'commit' ? { submodule: true } : entry.mode === '120000' ? { symlink: '' } : entry.type === 'blob' ? { sha: entry.sha } : undefined
1139
+ if (value) entries.set(dir === '.' ? entry.path : `${dir}/${entry.path}`, value)
1140
+ }
1141
+ }))
1142
+ return entries
1143
+ }
1144
+ // A file under the directory read of its tree, where that is being fetched whole; another alone.
1145
+ const readRepository = async (path) => {
1146
+ const under = where.directory === '' || path.startsWith(`${where.directory}/`)
1147
+ const held = reading && under ? (await reading).get(path)?.data : undefined
1148
+ return held ? asText.decode(held) : repository.getRepoFile({ path, ref: where.commit })
1149
+ }
1150
+ const readLocal = (path) => {
1151
+ try {
1152
+ return readFileSync(path, 'utf8')
1153
+ } catch (error) {
1154
+ throw new ValidateError('lockfile-unreadable', `${path}: ${error.message}`)
1155
+ }
1156
+ }
1157
+ const lockfiles = lockfile === false ? null : ecosystems.size === 0 ? []
1158
+ : Array.isArray(lockfile) ? lockfile.map((path) => ({ path, text: readLocal(path), from: 'local', read: readLocal, root: where.directory }))
1159
+ : repositoryLockfiles({ directory: where.directory, ecosystems, list, read: readRepository }).then((files) => files.map((one) => ({
1160
+ ...one, from: 'repository', read: readRepository, tree, root: posix.relative(posix.dirname(one.path), where.directory),
1161
+ // A patch the lockfile names by its hash alone, looked for in the project's patches directory, then the root's.
1162
+ patches: [...new Set([where.directory ? `${where.directory}/patches` : 'patches', 'patches'])],
1163
+ })))
1164
+ const files = await lockfiles
1165
+ const locked = files && lockedBy(files)
1166
+ for (const ecosystem of locked ? ecosystems : []) {
1167
+ if (!locked.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`)
1168
+ }
1169
+ // The packages fetched as the repository's tree is read, where it is; own code skipped, the patches
1170
+ // the bundle holds checked all the same, where a repository and commit are given or recorded.
1171
+ // Where it was read, the name it has now where it has moved.
1172
+ const at = () => ({ repository: repository.name(), ...(repository.name() !== where.github && { movedFrom: where.github }), commit: where.commit, ...(ref && { ref }), directory: where.directory })
1173
+ const checked = own ? reading.then((entries) => ({ entries, ...at() }))
1174
+ : where.github !== undefined && where.commit !== undefined && plan.patches.size > 0 ? patchEntries().then((entries) => ({ reason: 'skipped', entries, ...at() }))
1175
+ : { reason: 'skipped' }
1176
+ return validateBundle(plan, checked, { fetch, locked, deadline })
1177
+ }
1178
+
1179
+ // Why own code or a package wasn't checked, or a file is as it is, as a report tells it.
1180
+ const OWN_UNCHECKED = { skipped: 'skipped (--only-deps): its dependencies alone validated' }
1181
+ // A package's reason, unverified (the first) or failing its lockfile (the last three).
1182
+ const PACKAGE_REASONS = { local: 'installed from a directory of the repository, which isn\'t read', 'patch-unverified': 'as patched by the bundle\'s patch, which isn\'t verified against the repository', git: 'locked from git, not a registry', 'not-registry': 'locked from elsewhere than its registry', 'no-integrity': 'locked with no sha512 to hold its tarball to', unsupported: 'no upstream package for its ecosystem', unnamed: 'no name or version recorded', 'not-published': 'not published', unreadable: 'its registry refused it', timeout: 'not reached in time', failed: 'could not be fetched',
1183
+ 'alias-unverified': 'installed under another name, an alias no lockfile read tells of',
1184
+ misplaced: 'installed where its path or its lockfile names another package or version',
1185
+ 'not-locked': 'not locked by the lockfile', 'integrity-mismatch': 'its registry\'s tarball is not the one the lockfile locks', 'integrity-conflict': 'locked with two integrities' }
1186
+ // Why a patch wasn't applied, told after `its patch` and its path.
1187
+ const PATCH_ERRORS = { 'patch-not-in-repository': 'is the bundle\'s, not the repository\'s', 'patch-not-found': 'is in no file of its hash (the bundle\'s, or the patches directory\'s)', 'patch-mismatch': 'is not of the hash its lockfile locks' }
1188
+ const FILE_REASONS = { symlink: 'a symlink in the repository', submodule: 'in a submodule', 'tree-cut': 'the repository\'s tree was too large to list whole' }
1189
+
1190
+ // Each status's colour: failing red, unverified yellow, valid green.
1191
+ const TONES = { differs: 'red', unexpected: 'red', invalid: 'red', unverified: 'yellow', incomplete: 'yellow', valid: 'green' }
1192
+
1193
+ // What a package told is of, as a table names it: an npm one by its name and version alone.
1194
+ const packageName = (of) => of.replace(/^npm:/u, '')
1195
+
1196
+ // What a report tells of a package beside its status: why it is unverified or fails its lockfile,
1197
+ // its patch, or the directory it is held to; '' where nothing more.
1198
+ function noteOf(pkg) {
1199
+ const { patch } = pkg
1200
+ if (pkg.reason) return `${PACKAGE_REASONS[pkg.reason] ?? pkg.reason}${pkg.at ? `: ${pkg.at}` : ''}`
1201
+ if (patch?.error) return `its patch${patch.path ? ` ${patch.path}` : ''} ${PATCH_ERRORS[patch.error] ?? `could not be applied (${patch.error})`}`
1202
+ // A patch the bundle holds, as checked against the repository.
1203
+ const whose = patch?.from !== 'bundle' ? '' : patch.verified ? ' (the bundle\'s, as the repository has it)' : ' (the bundle\'s, not verified against the repository)'
1204
+ if (patch) return `${patch.applied === false ? 'locked patched, bundled as published' : 'patched'}${patch.path ? ` by ${patch.path}${whose}` : ''}`
1205
+ return pkg.directory ? `held to ${pkg.directory} of the repository` : ''
1206
+ }
1207
+
1208
+ // The report as tables on `out`: the files failing, and own ones unverified, one row for each kind of
1209
+ // what they are of (own code, or a package), the worst first; then the packages with something to
1210
+ // tell (why they are unverified or fail, a patch, a directory), those valid only with `showValid`.
1211
+ // The summary on `err`: its status and counts, where own code was read or why not, and the lockfiles
1212
+ // held to. Their statuses in colour, where each stream takes colours (`colors`, else takesColors).
1213
+ export function printValidationReport(report, { out = process.stdout, err = process.stderr, showValid = false, colors } = {}) {
1214
+ const paintOut = painter(out, colors)
1215
+ const paintErr = painter(err, colors)
1216
+ // A row's status in its colour, and own code dimmed beside a package.
1217
+ const status = (paint) => (text, column, row) => paint(column === 'status' ? TONES[row.status] : column === 'of' && row.of === 'own code' ? DIM : null, text)
1218
+ const tables = []
1219
+ const files = [...Map.groupBy(report.problems, (problem) => `${problem.kind}\0${problem.of}`).values()].map((group) => ({
1220
+ status: group[0].kind,
1221
+ of: group[0].of === 'own' ? 'own code' : packageName(group[0].of),
1222
+ files: group.map((problem) => `${problem.path}${problem.reason ? ` (${FILE_REASONS[problem.reason] ?? problem.reason})` : ''}`).join('\n'),
1223
+ }))
1224
+ 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` : ''))
1225
+ const told = report.packages.filter((pkg) => (showValid || pkg.status !== 'valid') && noteOf(pkg) !== '')
1226
+ // The ecosystem a column of its own only where one is not npm's.
1227
+ const ecosystems = told.some((pkg) => pkg.ecosystem !== 'npm')
1228
+ const packages = told.map((pkg) => ({ status: pkg.status, ...(ecosystems && { ecosystem: pkg.ecosystem }), package: `${pkg.name}@${pkg.version}`, note: noteOf(pkg) }))
1229
+ if (packages.length > 0) tables.push(formatTable(packages, ['status', ...(ecosystems ? ['ecosystem'] : []), 'package', 'note'], { paint: status(paintOut) }))
1230
+ if (tables.length > 0) out.write(`${tables.join('\n\n')}\n`)
1231
+
1232
+ const { counts, own } = report
1233
+ const lines = [
1234
+ `${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` : ''}`,
1235
+ 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}` : ''}`
1236
+ : paintErr('yellow', `Own code not verified: ${OWN_UNCHECKED[own.reason] ?? own.reason}${own.patches ? `; the ${own.patches === 1 ? 'patch' : `${own.patches} patches`} the bundle holds checked all the same` : ''}`),
1237
+ ...(report.lockfiles?.length > 0 ? [`Dependencies held to ${report.lockfiles.join(', ')}`]
1238
+ : report.lockfiles === null && counts.packages > 0 ? [paintErr('yellow', 'Dependencies held to no lockfile (--no-lockfile): each to its registry\'s integrity alone')] : []),
1239
+ ]
1240
+ err.write(`${lines.join('\n')}\n`)
1241
+ }