@exodus/stasis 1.0.0-beta.9 → 1.0.0-rc.1

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 +6 -2
  2. package/bin/stasis.js +222 -334
  3. package/package.json +38 -14
  4. package/src/audit-corrections.js +11 -42
  5. package/src/audit.js +256 -212
  6. package/src/babel.js +119 -82
  7. package/src/bundle-from-lockfile.js +9 -21
  8. package/src/bundle-reader.js +24 -8
  9. package/src/bundle.js +0 -3
  10. package/src/cmd/build.js +211 -117
  11. package/src/cmd/bundle-options.js +347 -0
  12. package/src/cmd/bundle.js +1664 -1001
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +48 -21
  15. package/src/cmd/output.js +25 -12
  16. package/src/cmd/sbom.js +6 -9
  17. package/src/cmd/tokens.js +60 -0
  18. package/src/define.js +218 -0
  19. package/src/diff.js +73 -129
  20. package/src/esbuild.js +0 -4
  21. package/src/flow.js +27 -0
  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 +43 -120
  27. package/src/loaders/cargo.js +301 -808
  28. package/src/loaders/foundry.js +112 -290
  29. package/src/loaders/paths.js +15 -26
  30. package/src/loaders/php.js +211 -492
  31. package/src/loaders/rust.js +518 -1461
  32. package/src/loaders/solidity-ownership.js +51 -142
  33. package/src/loaders/solidity.js +42 -188
  34. package/src/loaders/toml.js +7 -17
  35. package/src/lockfile.js +0 -3
  36. package/src/metro-assets.js +41 -0
  37. package/src/metro-resolver.js +58 -133
  38. package/src/metro-transformer.js +0 -6
  39. package/src/metro.js +0 -4
  40. package/src/mock.js +32 -80
  41. package/src/parse.js +8 -7
  42. package/src/prune.js +0 -3
  43. package/src/resolve-fields.js +152 -249
  44. package/src/resolve-node.js +29 -45
  45. package/src/resolve-typescript.js +166 -298
  46. package/src/rollup.js +0 -4
  47. package/src/run-installed.js +10 -0
  48. package/src/sbom.js +70 -97
  49. package/src/scan.js +240 -282
  50. package/src/sixel.js +310 -0
  51. package/src/table.js +95 -0
  52. package/src/validate-notes.d.ts +19 -0
  53. package/src/validate-notes.js +72 -0
  54. package/src/validate.d.ts +159 -0
  55. package/src/validate.js +1315 -0
  56. package/src/vfs-bundle/entries.js +50 -46
  57. package/src/vfs-bundle/github.js +114 -94
  58. package/src/vfs-bundle/glob.js +379 -0
  59. package/src/vfs-bundle/lazy.js +99 -0
  60. package/src/vfs-bundle/prisma.js +235 -105
  61. package/src/vfs-bundle/soldeer.js +28 -49
  62. package/src/vfs-bundle/tree.js +418 -277
  63. package/src/vfs-bundle/yarnclean.js +79 -0
  64. package/src/vfs-bundle.js +24 -55
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +79 -231
  67. package/src/installed-commits.js +0 -182
@@ -0,0 +1,1315 @@
1
+ import { createHash } from 'node:crypto'
2
+ import { readFileSync } from 'node:fs'
3
+ import { readFile, readdir } from 'node:fs/promises'
4
+ import { join, parse as parsePath, posix, relative, sep } from 'node:path'
5
+
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'
7
+ import { decompress } from '@preventive/archive/compression.js'
8
+ import { unpack } from '@preventive/archive/tar.js'
9
+ import { getCrate } from '@preventive/upstream/cargo.js'
10
+ import { HttpError, getTarball } from '@preventive/upstream/npm.js'
11
+ import { parseCargoChecksum, parseCargoLock } from '@preventive/lockfile/cargo.js'
12
+ import { parseComposerLock } from '@preventive/lockfile/composer.js'
13
+ import { parseNpmLockfile } from '@preventive/lockfile/npm.js'
14
+ import { parsePnpmLockfile } from '@preventive/lockfile/pnpm.js'
15
+ import { parseSoldeerLockfile } from '@preventive/lockfile/soldeer.js'
16
+ import { parseYarn1Lockfile } from '@preventive/lockfile/yarn1.js'
17
+ import { createVfs } from '@preventive/vfs'
18
+ import { Lockfile } from '@exodus/stasis-core/lockfile'
19
+ import { bundledIntegrity, sha512integrity } from '@exodus/stasis-core/state-util'
20
+
21
+ import { nameAt, STORE_HASH, resolutionCommit } from './installed.js'
22
+ import { findCargoLock } from './loaders/cargo.js'
23
+ import { parseFileWithKind } from './parse.js'
24
+ import { DIM, formatTable, painter } from './table.js'
25
+ import { FILE_REASONS, LOCKFILE_ERRORS, OWN_REASONS, generatedBy, noteText } from './validate-notes.js'
26
+ import { lockfileOf, lockfileRoot, vfsHost } from './vfs-bundle/tree.js'
27
+
28
+ export const MAX_TOLD = 500
29
+ const PACKAGES_AT_ONCE = 6
30
+ // As patch-package looks for a hunk, this many lines either side of where its header says.
31
+ const FUZZ = 20
32
+ // As upstream's tree verification bounds a tarball.
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())
37
+
38
+ const utf8 = new TextEncoder()
39
+ // A BOM kept, as patch-package keeps it: a patch applied changes no byte but those its hunks change.
40
+ const asText = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
41
+
42
+ export class PatchError extends Error {
43
+ constructor(code) {
44
+ super(code)
45
+ this.name = 'PatchError'
46
+ this.code = code
47
+ }
48
+ }
49
+
50
+ export class ValidateError extends Error {
51
+ constructor(code, message) {
52
+ super(message)
53
+ this.name = 'ValidateError'
54
+ this.code = code
55
+ }
56
+ }
57
+
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.
64
+ const moduleEcosystem = (dir, ecosystem) => (dir === '.' || (ecosystem === 'npm' && !hasNodeModulesSegment(dir)) ? undefined : dependencyEcosystem(dir, ecosystem))
65
+
66
+ export function validationPlan(bundle, { lockfile = false } = {}) {
67
+ const formats = bundle.formats ?? new Map()
68
+ const own = []
69
+ const patches = new Map()
70
+ const packages = new Map()
71
+ for (const [dir, info] of bundle.modules) {
72
+ const ecosystem = moduleEcosystem(dir, info.ecosystem)
73
+ const key = `${ecosystem}\0${info.name ?? ''}\0${info.version ?? ''}`
74
+ for (const [rel, content] of Object.entries(info.files)) {
75
+ const path = moduleFileKey(dir, rel)
76
+ const format = formats.get(path)
77
+ if (typeof content !== 'string' || format === 'directory' || isStatFormat(format)) continue
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)) }
80
+ if (ecosystem !== undefined) {
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() })
82
+ packages.get(key).files.push(file)
83
+ } else if (path !== EMPTY_MODULE_PATH || content !== (lockfile ? EMPTY_INTEGRITY : '')) {
84
+ if (!lockfile && (format === 'patch' || path.endsWith('.patch'))) patches.set(path, content)
85
+ own.push(file)
86
+ }
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)
94
+ }
95
+ const listed = [...packages.values()]
96
+ return { files: listed.reduce((sum, pkg) => sum + pkg.files.length, own.length), own, packages: listed, patches }
97
+ }
98
+
99
+ export const gitBlobId = (bytes) => createHash('sha1').update(`blob ${bytes.byteLength}\0`).update(bytes).digest('hex')
100
+
101
+ // Its one top directory left out, as npm unpacks a package (crates.io's crates and GitHub's trees have one too).
102
+ export async function tarballFiles(tarball) {
103
+ const files = new Map()
104
+ for (const entry of unpack(await decompress(tarball, 'gzip', { limit: MAX_TAR_BYTES }), { symlinksAbove: Number.MAX_SAFE_INTEGER })) {
105
+ const name = entry.name.slice(entry.name.indexOf('/') + 1)
106
+ if (!entry.name.includes('/') || name === '' || entry.type === 'directory') continue
107
+ files.set(name, entry.type === 'symlink' ? { symlink: entry.linkname } : entry.data)
108
+ }
109
+ return files
110
+ }
111
+
112
+ export const FETCHED = new Set(['npm', 'cargo'])
113
+ const sha256 = (bytes) => createHash('sha256').update(bytes).digest('hex')
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.
116
+ export async function fetchPackage(ecosystem, name, version, integrity) {
117
+ const tarball = ecosystem === 'npm'
118
+ ? await getTarball(name, version, integrity === undefined ? undefined : { tarball: `https://registry.npmjs.org${tarballPath(name, version)}`, integrity })
119
+ : await getCrate(name, version, integrity)
120
+ const files = new Map([...await tarballFiles(tarball)].filter(([, data]) => data instanceof Uint8Array))
121
+ if (ecosystem === 'cargo') files.checksum = integrity ?? sha256(tarball)
122
+ return files
123
+ }
124
+
125
+ function checksumOf(bytes, crate) {
126
+ let parsed
127
+ try {
128
+ parsed = parseCargoChecksum(asText.decode(bytes))
129
+ } catch {
130
+ return false
131
+ }
132
+ return (crate.checksum === undefined || parsed.checksum === crate.checksum)
133
+ && Object.entries(parsed.files).every(([path, listed]) => crate.has(path) && sha256(crate.get(path)) === listed)
134
+ }
135
+
136
+ // Re-exported for a caller's `fetch` to throw, as fetchFailure reads it.
137
+ export { HttpError }
138
+ export function fetchFailure(error) {
139
+ if (error instanceof HttpError) return error.status === 404 ? 'not-published' : [401, 403].includes(error.status) ? 'unreadable' : 'failed'
140
+ if (error?.code === 'ERR_ASSERTION' && /integrity mismatch/u.test(error.message)) return 'integrity-mismatch'
141
+ return 'failed'
142
+ }
143
+
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))
146
+ export const isPatchOf = (text, hash) => patchHashes(text).has(hash)
147
+
148
+ // git quotes a path as C writes a string, bytes past ASCII in octal; null where it is quoted otherwise.
149
+ const ESCAPES = { a: 7, b: 8, t: 9, n: 10, v: 11, f: 12, r: 13, '"': 34, '\\': 92 }
150
+ function unquoted(path) {
151
+ if (!path.startsWith('"')) return path
152
+ const bytes = []
153
+ for (let at = 1; at < path.length; at++) {
154
+ const char = path[at]
155
+ if (char === '"') return at === path.length - 1 ? asText.decode(Uint8Array.from(bytes)) : null
156
+ if (char !== '\\') {
157
+ bytes.push(...utf8.encode(char))
158
+ continue
159
+ }
160
+ const next = path[++at] ?? ''
161
+ const octal = /^[0-3][0-7]{2}/u.exec(path.slice(at))?.[0]
162
+ if (octal) {
163
+ bytes.push(Number.parseInt(octal, 8))
164
+ at += 2
165
+ } else if (Object.hasOwn(ESCAPES, next)) bytes.push(ESCAPES[next])
166
+ else return null
167
+ }
168
+ return null
169
+ }
170
+
171
+ const unreadable = () => new PatchError('patch-unreadable')
172
+ const inPackage = (path) => path.length > 0 && path.split('/').every((name) => name !== '' && name !== '.' && name !== '..')
173
+
174
+ // git writes a tab after a path with a space in it.
175
+ function sidePath(named, prefix) {
176
+ const path = unquoted(named.endsWith('\t') ? named.slice(0, -1) : named)
177
+ if (path === '/dev/null') return null
178
+ if (path === null || !path.startsWith(prefix)) throw unreadable()
179
+ return path.slice(prefix.length)
180
+ }
181
+
182
+ function gitPath(named) {
183
+ if (named.startsWith('"')) {
184
+ const quoted = /^("(?:[^"\\]|\\.)*") ("(?:[^"\\]|\\.)*")$/u.exec(named)
185
+ const [a, b] = quoted ? [unquoted(quoted[1]), unquoted(quoted[2])] : []
186
+ return a?.startsWith('a/') && b?.startsWith('b/') && a.slice(2) === b.slice(2) ? a.slice(2) : null
187
+ }
188
+ const length = (named.length - 5) / 2
189
+ const path = named.slice(2, 2 + length)
190
+ return Number.isInteger(length) && named.startsWith('a/') && named.slice(2 + length) === ` b/${path}` ? path : null
191
+ }
192
+
193
+ function readHunk(lines, start, end) {
194
+ let at = start
195
+ const range = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/u.exec(lines[at++])
196
+ if (!range) throw unreadable()
197
+ let news = Number(range[4] ?? 1)
198
+ let olds = Number(range[2] ?? 1)
199
+ const hunk = { at: Math.max(Number(range[1]), 1) - 1, length: olds, parts: [] }
200
+ while (olds > 0 || news > 0) {
201
+ if (at >= end) throw unreadable()
202
+ const line = lines[at++]
203
+ const kind = line === '' || line === '\r' || line[0] === ' ' ? 'context' : line[0] === '-' ? 'deletion' : line[0] === '+' ? 'insertion' : null
204
+ if (!kind) throw unreadable()
205
+ let part = hunk.parts.at(-1)
206
+ if (part?.kind !== kind) hunk.parts.push(part = { kind, lines: [] })
207
+ part.lines.push(line.slice(1))
208
+ if (kind !== 'insertion') olds--
209
+ if (kind !== 'deletion') news--
210
+ if (olds < 0 || news < 0) throw unreadable()
211
+ if (lines[at]?.startsWith('\\')) {
212
+ if (!lines[at++].startsWith('\')) throw unreadable()
213
+ part.unended = true
214
+ }
215
+ }
216
+ return { hunk, next: at }
217
+ }
218
+
219
+ // As pnpm writes a patch; a CRLF one's headers read without their CRs, as patch-package reads them.
220
+ export function parsePatch(patch) {
221
+ const lines = patch.split('\n')
222
+ const end = lines.length - (patch.endsWith('\n') ? 1 : 0)
223
+ const headerAt = (at) => lines[at]?.replace(/\r$/u, '') ?? ''
224
+ const files = []
225
+ for (let at = 0; at < lines.length;) {
226
+ const head = headerAt(at++)
227
+ if (!head.startsWith('diff --git ')) continue
228
+ let created = false
229
+ let deleted = false
230
+ let from = null
231
+ let to = null
232
+ let sides = null
233
+ for (; at < lines.length && !/^(?:diff --git |@@ |--- )/u.test(lines[at]); at++) {
234
+ const header = headerAt(at)
235
+ if (header === 'GIT binary patch' || header.startsWith('Binary files ') || header.startsWith('copy from ') || header.startsWith('copy to ')) {
236
+ throw new PatchError('patch-unsupported')
237
+ }
238
+ if (header.startsWith('deleted file mode ')) deleted = true
239
+ else if (header.startsWith('new file mode ')) created = true
240
+ else if (header.startsWith('rename from ')) from = unquoted(header.slice('rename from '.length))
241
+ else if (header.startsWith('rename to ')) to = unquoted(header.slice('rename to '.length))
242
+ }
243
+ if (lines[at]?.startsWith('--- ')) {
244
+ if (!lines[at + 1]?.startsWith('+++ ')) throw unreadable()
245
+ sides = [sidePath(headerAt(at).slice(4), 'a/'), sidePath(headerAt(at + 1).slice(4), 'b/')]
246
+ at += 2
247
+ }
248
+ const named = gitPath(head.slice('diff --git '.length))
249
+ const after = to ?? (sides ? sides[1] : named)
250
+ const before = from ?? (sides ? sides[0] : named)
251
+ if ((from === null) !== (to === null) || (deleted && created)) throw unreadable()
252
+ const path = deleted ? before : after
253
+ if (path == null || (!created && before == null) || (!deleted && after == null) || ![before, after].every((side) => side == null || inPackage(side))
254
+ || (from === null && !deleted && !created && before !== after)) throw unreadable()
255
+ const hunks = []
256
+ while (lines[at]?.startsWith('@@ ')) {
257
+ const { hunk, next } = readHunk(lines, at, end)
258
+ hunks.push(hunk)
259
+ at = next
260
+ }
261
+ files.push({ path, ...(from !== null && { from: before }), ...(deleted && { deleted: true }), ...(created && { created: true }), hunks: deleted ? [] : hunks })
262
+ }
263
+ return files
264
+ }
265
+
266
+ const doesNotApply = () => new PatchError('patch-does-not-apply')
267
+
268
+ // Lines the same but for the space they end in match, as patch-package finds a hunk.
269
+ function changesAt(hunk, lines, start) {
270
+ let at = start
271
+ if (at < 0 || lines.length - at < hunk.length) return null
272
+ const changes = []
273
+ for (const part of hunk.parts) {
274
+ if (part.kind === 'insertion') {
275
+ changes.push({ splice: at, remove: 0, insert: part.lines })
276
+ if (part.unended) changes.push('pop')
277
+ continue
278
+ }
279
+ for (const line of part.lines) if (lines[at++].trimEnd() !== line.trimEnd()) return null
280
+ if (part.kind === 'deletion') {
281
+ changes.push({ splice: at - part.lines.length, remove: part.lines.length, insert: [] })
282
+ if (part.unended) changes.push('push')
283
+ }
284
+ }
285
+ return changes
286
+ }
287
+
288
+ function applied(text, hunks) {
289
+ const lines = text.split('\n')
290
+ const changes = hunks.flatMap((hunk) => {
291
+ for (let offset = 0; Math.abs(offset) <= FUZZ; offset = offset < 0 ? -offset : -offset - 1) {
292
+ const found = changesAt(hunk, lines, hunk.at + offset)
293
+ if (found) return found
294
+ }
295
+ throw doesNotApply()
296
+ })
297
+ let shift = 0
298
+ for (const change of changes) {
299
+ if (change === 'push') lines.push('')
300
+ else if (change === 'pop') lines.pop()
301
+ else {
302
+ lines.splice(change.splice + shift, change.remove, ...change.insert)
303
+ shift += change.insert.length - change.remove
304
+ }
305
+ }
306
+ return lines.join('\n')
307
+ }
308
+
309
+ // A file it creates holds its first hunk's first lines, as patch-package writes it.
310
+ export function patchedFiles(files, patch) {
311
+ const next = new Map(files)
312
+ for (const file of patch) {
313
+ if (file.created) {
314
+ const first = file.hunks[0]?.parts[0]
315
+ next.set(file.path, utf8.encode(first ? `${first.lines.join('\n')}${first.unended ? '' : '\n'}` : ''))
316
+ continue
317
+ }
318
+ const source = file.from ?? file.path
319
+ const bytes = next.get(source)
320
+ if (!bytes) throw doesNotApply()
321
+ if (file.deleted) {
322
+ next.delete(source)
323
+ continue
324
+ }
325
+ let changed = bytes
326
+ if (file.hunks.length > 0) {
327
+ let text
328
+ try {
329
+ text = asText.decode(bytes)
330
+ } catch {
331
+ throw doesNotApply()
332
+ }
333
+ changed = utf8.encode(applied(text, file.hunks))
334
+ }
335
+ if (file.from !== undefined) {
336
+ if (next.has(file.path)) throw doesNotApply()
337
+ next.delete(source)
338
+ }
339
+ next.set(file.path, changed)
340
+ }
341
+ return next
342
+ }
343
+
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)
347
+ // Formats a CR before each line's end changes the meaning of: a binary's bytes, which no checkout
348
+ // converts, a shell script's commands and an env file's values, each its last word's own.
349
+ const CR_CHANGES = new Set(['resource:base64', 'shell', 'env'])
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) {
352
+ if (CR_CHANGES.has(format)) return 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
356
+ }
357
+ const packageKey = ({ ecosystem, name, version }) => `${ecosystem}:${name}@${version}`
358
+ // A registry's version, as pnpm names one in its store's directories (no `file+lib`, say).
359
+ const VERSION = /^\d+\.\d+\.\d+(?:-[\w.-]+)?(?:\+[\w.-]+)?$/u
360
+ // pnpm's store directory names a version `<name, its / a +>@<version>`, but where it hashed a long one or one with uppercase.
361
+ function installedAt(dir) {
362
+ const segments = dir.split('/')
363
+ const at = segments.lastIndexOf('node_modules')
364
+ const as = segments.slice(at + 1).join('/')
365
+ const store = at >= 3 && segments[at - 2] === '.pnpm' && segments[at - 3] === 'node_modules' ? segments[at - 1] : undefined
366
+ if (store === undefined || STORE_HASH.test(store)) return { as }
367
+ const split = store.indexOf('@', 1)
368
+ if (split < 0) return { as }
369
+ const name = store.slice(0, split)
370
+ const version = store.slice(split + 1).split('_')[0]
371
+ return { as, store: { name: name.startsWith('@') ? name.replace('+', '/') : name, ...(VERSION.test(version) && { version }) } }
372
+ }
373
+ // Undefined for a copy installed under an alias no lockfile read tells of.
374
+ function placed(pkg, dir, locked) {
375
+ const { as, store } = installedAt(dir)
376
+ const named = `${pkg.name}@${pkg.version}`
377
+ if (store && (store.name !== pkg.name || (store.version !== undefined && store.version !== pkg.version) || as !== pkg.name)) return false
378
+ const there = locked?.installed?.(dir)
379
+ if (there !== undefined) return there === named
380
+ if (as === pkg.name) return true
381
+ return locked?.aliased?.(as, named)
382
+ }
383
+ const byText = (key) => (a, b) => (key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0)
384
+ // The worst first, own code's files before a package's, so that as many as MAX_TOLD keep the worst.
385
+ const KINDS = ['differs', 'unexpected', 'unverified']
386
+ const STATUSES = ['invalid', 'unverified', 'valid']
387
+ const byProblem = (a, b) => KINDS.indexOf(a.kind) - KINDS.indexOf(b.kind) || (a.of === 'own' ? 0 : 1) - (b.of === 'own' ? 0 : 1)
388
+ || byText((problem) => problem.of)(a, b) || byText((problem) => problem.path)(a, b)
389
+ const byPackage = (a, b) => STATUSES.indexOf(a.status) - STATUSES.indexOf(b.status) || byText(packageKey)(a, b)
390
+ const pnpmPatchName = (name, version) => `${name.replace('/', '__')}@${version}.patch`
391
+
392
+ function patchesOf(plan) {
393
+ const byHash = new Map()
394
+ const byName = new Map()
395
+ for (const [path, text] of plan.patches) {
396
+ const held = { path, text }
397
+ const name = posix.basename(path)
398
+ for (const hash of patchHashes(text)) if (!byHash.has(hash)) byHash.set(hash, held)
399
+ if (!byName.has(name)) byName.set(name, held)
400
+ }
401
+ return { byHash, byName }
402
+ }
403
+
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) {
406
+ const lock = locked?.of(pkg.name, pkg.version)
407
+ const hash = lock === undefined ? recorded : lock?.hash
408
+ let patch
409
+ let text
410
+ try {
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
+ }
427
+ let unverified = false
428
+ if (patch.from === 'bundle') {
429
+ const verdict = (await verdicts).get(patch.path)
430
+ if (verdict === 'differs' || verdict === 'unexpected') throw new PatchError('patch-not-in-repository')
431
+ unverified = verdict !== 'matched'
432
+ patch.verified = !unverified
433
+ }
434
+ return { patch, patched: patchedFiles(files, parsePatch(text)), unverified }
435
+ } catch (error) {
436
+ return { patch: { ...patch, error: error instanceof PatchError ? error.code : 'patch-unreadable' }, patched: null }
437
+ }
438
+ }
439
+
440
+ // A path git can have in a tree, as upstream's isRepoPath (src/args.js), which it doesn't export, has it.
441
+ const inTree = (path) => path.length <= 4096 && path.isWellFormed() && !/\p{Cc}/u.test(path) && path.split('/').every((part) => !['', '.', '..', '.git'].includes(part.toLowerCase()))
442
+
443
+ const pathIn = (dir, name) => (dir ? `${dir}/${name}` : name)
444
+ const repoDir = (dir) => (dir === '.' ? '' : dir)
445
+
446
+ async function eachOf(items, parallel, run) {
447
+ let next = 0
448
+ await Promise.all(Array.from({ length: Math.min(parallel, items.length) }, async () => {
449
+ // eslint-disable-next-line no-await-in-loop -- a worker takes the next item once its own is done
450
+ while (next < items.length) await run(items[next++])
451
+ }))
452
+ }
453
+
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 }])
456
+ const problems = []
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 }
459
+ const tell = (problem) => {
460
+ counts[problem.kind]++
461
+ problems.push(problem)
462
+ }
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()
465
+
466
+ const told = []
467
+ const bundled = patchesOf(plan)
468
+ const fetching = eachOf(units, PACKAGES_AT_ONCE, async ({ pkg, locked }) => {
469
+ const of = packageKey(pkg)
470
+ const tellPackage = (status, more) => told.push({ ecosystem: pkg.ecosystem, name: pkg.name, version: pkg.version, files: pkg.files.length, status, ...more })
471
+ const unverified = (reason, status = 'unverified', more = {}) => {
472
+ counts.unverified += pkg.files.length
473
+ tellPackage(status, { reason, ...more })
474
+ }
475
+ const fails = (reason, more) => {
476
+ counts.lockFailures++
477
+ unverified(reason, 'invalid', more)
478
+ }
479
+ if (!pkg.name || !pkg.version) return unverified('unnamed')
480
+ const dirs = [...new Set(pkg.files.map((file) => file.dir))]
481
+ const places = new Map(pkg.ecosystem === 'npm' ? dirs.map((dir) => [dir, placed(pkg, dir, locked)]) : [])
482
+ const misplaced = dirs.find((dir) => places.get(dir) === false)
483
+ if (misplaced !== undefined) {
484
+ counts.misplaced++
485
+ return unverified('misplaced', 'invalid', { at: misplaced })
486
+ }
487
+ const aliased = dirs.filter((dir) => places.has(dir) && places.get(dir) === undefined)
488
+ if (aliased.length === dirs.length) return unverified('alias-unverified', 'unverified', { at: aliased[0] })
489
+ const lock = locked?.lock?.(pkg.ecosystem, pkg.name, pkg.version)
490
+ if (lock === null) return fails('not-locked')
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')
521
+ if (lock?.from === 'elsewhere') return unverified('not-registry')
522
+ if (lock?.from === 'local' && !locked.tree) return unverified('local')
523
+ if (lock?.from === 'registry' && lock.integrity === undefined && recordedIntegrity === undefined) return unverified('no-integrity')
524
+ if (now() > deadline) return unverified('timeout')
525
+ // Patched alone where the lockfile locks the patch, as pnpm patches every copy, or the copy records it.
526
+ let candidates
527
+ let published
528
+ let patch
529
+ let patched
530
+ let unverifiedPatch = false
531
+ try {
532
+ if (pinned) candidates = []
533
+ else if (lock?.from === 'local') candidates = await Promise.all(lock.directories.map((dir) => locked.tree(dir)))
534
+ else {
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]
538
+ }
539
+ } catch (error) {
540
+ const reason = fetchFailure(error)
541
+ return reason === 'integrity-mismatch' ? fails(reason) : unverified(reason)
542
+ }
543
+ const checksum = (file) => pkg.ecosystem === 'cargo' && file.rel === CARGO_CHECKSUM
544
+ const matches = (file, bytes, candidate) => (checksum(file) ? checksumOf(bytes, candidate) : candidate.has(file.rel) && isFile(file, candidate.get(file.rel)))
545
+ // Each installed copy apart, as one may be patched and another not.
546
+ let missed = 0
547
+ let asPatched = 0
548
+ let notVerified = 0
549
+ for (const copy of Map.groupBy(pkg.files, (file) => file.dir).values()) {
550
+ if (aliased.includes(copy[0].dir)) {
551
+ notVerified += copy.length
552
+ continue
553
+ }
554
+ const bytes = new Map(copy.map((file) => [file, file.integrity === undefined ? file.bytes() : undefined]))
555
+ const missing = (candidate) => copy.filter((file) => !matches(file, bytes.get(file), candidate))
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
559
+ let misses = missing(held)
560
+ for (const other of heldTo.slice(1)) {
561
+ if (misses.length > 0 && missing(other).length === 0) [held, misses] = [other, []]
562
+ }
563
+ // As a patch not verified against the repository patches them: neither as they should be nor not.
564
+ if (unverifiedPatch && missing(patched).length === 0 && missing(published).length > 0) {
565
+ notVerified += copy.length
566
+ continue
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))
572
+ for (const file of misses) tell({ path: file.path, kind: checksum(file) || held.has(file.rel) ? 'differs' : 'unexpected', of })
573
+ missed += misses.length
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
577
+ }
578
+ counts.matched += pkg.files.length - missed - notVerified
579
+ counts.unverified += notVerified
580
+ const status = missed > 0 ? 'invalid' : notVerified > 0 ? 'unverified' : 'valid'
581
+ if (status === 'valid') counts.validPackages++
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' }
585
+ tellPackage(status, {
586
+ ...why,
587
+ ...(patch && { patch: { ...patch, ...(recordedPatch !== undefined && { recorded: true }), ...(patched && status === 'valid' && { applied: asPatched > 0 }) } }),
588
+ ...(lock?.from === 'local' && { directory: lock.directories[0] }),
589
+ })
590
+ }
591
+ return undefined
592
+ })
593
+
594
+ let read
595
+ let skipped
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
605
+ try {
606
+ read = await own
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))
612
+ for (const file of plan.own) {
613
+ // Own code skipped, the bundle's patches are checked all the same where the repository is read.
614
+ if (skipped && !(read.entries && plan.patches.has(file.path))) {
615
+ counts[read.reason === 'skipped' ? 'skipped' : 'unverified']++
616
+ continue
617
+ }
618
+ const path = pathIn(read.directory, file.path)
619
+ const entry = read.entries.get(path)
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')
626
+ else {
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')
633
+ else {
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')
639
+ }
640
+ }
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
+ }
652
+ } finally {
653
+ checked(found)
654
+ }
655
+ await fetching
656
+
657
+ const patchesChecked = skipped && read.entries ? plan.own.filter((file) => plan.patches.has(file.path)).length : 0
658
+ return {
659
+ validatedAt: now(),
660
+ status: counts.differs + counts.unexpected + counts.lockFailures + counts.misplaced > 0 ? 'invalid' : counts.unverified > 0 ? 'incomplete' : 'valid',
661
+ own: skipped ? { checked: false, reason: read.reason, ...(patchesChecked > 0 && { patches: patchesChecked }) }
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 }) },
663
+ lockfiles: lockfiles?.lockfiles ?? null,
664
+ counts,
665
+ problems: problems.toSorted(byProblem).slice(0, MAX_TOLD),
666
+ moreProblems: Math.max(0, problems.length - MAX_TOLD),
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))
687
+ }
688
+ return [...listed.values()].map((client) => ({ ...client, files: client.files.toSorted() })).toSorted(byText((client) => client.output))
689
+ }
690
+
691
+ export const exitCodeOf = (report, { allowUnverified = false } = {}) => (report.status === 'invalid' ? 1 : report.status === 'incomplete' && !allowUnverified ? 2 : 0)
692
+
693
+ // Its tree's tarball, which upstream holds to the tree's id, its symlinks climbing as far as the repository's root.
694
+ export async function repositoryFiles(client, { github, commit, directory = '' }) {
695
+ const tree = await client.getRepoTreeId({ repo: github, sha: commit, ...(directory && { directory }) })
696
+ const tarball = await client.getRepoTreeTarball({ repo: github, tree, symlinksAbove: directory ? directory.split('/').length - 1 : 0 })
697
+ const entries = new Map()
698
+ for (const [path, value] of await tarballFiles(tarball)) entries.set(pathIn(directory, path), value instanceof Uint8Array ? { data: value } : value)
699
+ return entries
700
+ }
701
+
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']])
710
+ const CRATES_IO = new Set(['registry+https://github.com/rust-lang/crates.io-index', 'sparse+https://index.crates.io/'])
711
+
712
+ // None is, as npm writes one of its registry's where set to.
713
+ function registryTarball(tarball, name, version) {
714
+ if (tarball === undefined) return true
715
+ const url = URL.parse(tarball)
716
+ return url !== null && /^https?:$/u.test(url.protocol) && url.pathname.endsWith(tarballPath(name, version))
717
+ }
718
+
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, '') } }) }
723
+ }
724
+ const fromGit = (resolution) => resolution?.type === 'git' || resolution?.gitHosted === true || resolutionCommit(resolution) !== undefined
725
+
726
+ function localDirectory(spec) {
727
+ const path = /^(?:file|link):(.+)$/u.exec(spec)?.[1]
728
+ return path === undefined || /\.(?:tgz|tar\.gz|tar)$/u.test(path) ? undefined : path
729
+ }
730
+
731
+ // Its BOM passed over but Cargo.lock's, as cargo reads it.
732
+ function readLockfile(name, read) {
733
+ const text = name === 'Cargo.lock' ? read : withoutBom(read)
734
+ const entries = new Map()
735
+ const locals = new Map()
736
+ const add = (pkg, version, entry) => upsert(entries, `${pkg}@${version}`, () => []).push(entry)
737
+ const local = (pkg, dir) => locals.set(pkg, new Set([...(locals.get(pkg) ?? []), posix.normalize(dir)]))
738
+ // `<as>\0<name>@<version>`, and a directory's package's `<as>\0<name>`, any version of it.
739
+ const aliases = new Set()
740
+ const directoryAliases = new Set()
741
+ if (name === 'pnpm-lock.yaml') {
742
+ const { lockfile } = parsePnpmLockfile(text)
743
+ // pnpm locks an alias `foo: ms@2.1.3`, and a directory's `foo: lib@file:lib`, as a `file:` override installs it.
744
+ for (const node of [...Object.values(lockfile?.importers ?? {}), ...Object.values(lockfile?.packages ?? {})]) {
745
+ for (const [as, ref] of Object.entries({ ...node.dependencies, ...node.devDependencies, ...node.optionalDependencies })) {
746
+ const split = typeof ref === 'string' ? ref.indexOf('@', 1) : -1
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}`)
753
+ }
754
+ }
755
+ const hashes = new Map()
756
+ for (const [key, pkg] of Object.entries(lockfile?.packages ?? {})) {
757
+ // A directory has no version in the lockfile: pnpm reads the one it has as it installs it.
758
+ if (pkg.resolution.type === 'directory') local(pkg.name, pkg.resolution.directory)
759
+ if (pkg.version === undefined) continue
760
+ // A registry version's key is its name and version, its peers and patch after them.
761
+ const ref = key.slice(pkg.name.length + 1)
762
+ add(pkg.name, pkg.version, lockedFrom(fromGit(pkg.resolution), ref === pkg.version || ref.startsWith(`${pkg.version}(`), sha512Of(pkg.resolution.integrity), pkg.resolution))
763
+ hashes.set(`${pkg.name}@${pkg.version}`, pkg.patchHash ?? null)
764
+ }
765
+ const paths = new Map()
766
+ for (const [selector, { hash, path }] of Object.entries(lockfile?.patchedDependencies ?? {})) {
767
+ const known = paths.get(hash)
768
+ paths.set(hash, { path: known?.path ?? path, selectors: [...(known?.selectors ?? []), selector] })
769
+ }
770
+ return { entries, locals, aliases, directoryAliases, patches: { hashes, paths } }
771
+ }
772
+ if (name === 'Cargo.lock') {
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))
774
+ return { entries }
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
+ }
788
+ let packages
789
+ let locations
790
+ let links
791
+ let importers
792
+ if (name === 'yarn.lock') packages = new Set(Object.values(parseYarn1Lockfile(text, { checkVersions: false }).packages))
793
+ else {
794
+ const lockfile = parseNpmLockfile(text, { checkVersions: false })
795
+ locations = new Map(Object.entries(lockfile.packages).filter(([, pkg]) => pkg.version !== undefined).map(([location, pkg]) => [location, `${pkg.name}@${pkg.version}`]))
796
+ importers = new Set(Object.keys(lockfile.importers))
797
+ links = new Set(Object.keys(lockfile.links))
798
+ for (const [location, target] of Object.entries(lockfile.links)) {
799
+ if (Object.hasOwn(lockfile.importers, target)) local(nameAt(location), target)
800
+ }
801
+ packages = Object.values(lockfile.packages)
802
+ }
803
+ for (const { name: pkg, version, resolution, patterns } of packages) {
804
+ // yarn locks an alias by its pattern, `foo@npm:ms@^2.1.0`.
805
+ for (const pattern of patterns ?? []) {
806
+ const split = pattern.indexOf('@', 1)
807
+ if (pattern.startsWith('npm:', split + 1)) aliases.add(`${pattern.slice(0, split)}\0${pkg}@${version}`)
808
+ }
809
+ // yarn locks a directory by its spec alone, as it last read the version there.
810
+ const dir = resolution === undefined ? patterns?.map((pattern) => localDirectory(pattern.slice(pattern.indexOf('@', 1) + 1))).find(Boolean) : undefined
811
+ if (dir !== undefined) local(pkg, dir)
812
+ else if (version !== undefined) add(pkg, version, lockedFrom(fromGit(resolution), resolution?.type === 'tarball' && registryTarball(resolution.tarball, pkg, version), sha512Of(resolution?.integrity), resolution))
813
+ }
814
+ return { entries, locals, aliases, ...(locations && { locations, links, importers }) }
815
+ }
816
+
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
+
825
+ export function lockedBy(lockfiles) {
826
+ const by = new Map()
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
831
+ const ecosystem = LOCKFILES.get(posix.basename(path))
832
+ if (ecosystem === undefined) throw new ValidateError('lockfile-unsupported', `${path}: not a lockfile validate reads (${[...LOCKFILES.keys()].join(', ')})`)
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.
838
+ const within = locks.importers === undefined || from !== 'local' || locks.importers.has(root) ? root : ''
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)
843
+ }
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))
852
+ return {
853
+ lockfiles: all.map(({ path }) => path),
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
+ },
861
+ lock(ecosystem, name, version) {
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
868
+ }
869
+ const integrities = new Set(locks.filter((entry) => entry.from === 'registry').map((entry) => entry.integrity))
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
+ }
877
+ return integrities.size === 1 ? { from: 'registry', integrity: [...integrities][0] } : { from: 'registry', conflict: true }
878
+ },
879
+ // Null where the pnpm lockfiles lock the version with no patch, undefined where none of them locks it.
880
+ of(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
891
+ },
892
+ async locate({ hash, names = [] }) {
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 }
897
+ }
898
+ return null
899
+ },
900
+ // Null where it installs none at `dir`; undefined where it locks no location, links it to a directory, or none is read.
901
+ installed(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)
906
+ return npm.links.has(location) ? undefined : npm.locations.get(location) ?? null
907
+ },
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))),
917
+ }
918
+ }
919
+
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`.
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 where = directory || 'the repository\'s root'
955
+ const lists = (...names) => listed.some((inDir) => names.some((name) => inDir.includes(name)))
956
+ const manifests = new Set([
957
+ ...ecosystems.has('npm') && lists('package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock') ? ['package.json'] : [],
958
+ ...ecosystems.has('cargo') ? ['Cargo.toml'] : [],
959
+ ])
960
+ const files = Object.fromEntries(dirs.filter(Boolean).map((dir) => [dir, { type: 'directory' }]))
961
+ await Promise.all(dirs.flatMap((dir, index) => listed[index].map(async (name) => {
962
+ if (manifests.has(name)) files[pathIn(dir, name)] = await read(pathIn(dir, name))
963
+ // npm reads npm-shrinkwrap.json in place of package-lock.json, at the root it finds by either.
964
+ else if (LOCKFILES.has(name) || name === 'pnpm-workspace.yaml') files[pathIn(dir, name === 'npm-shrinkwrap.json' ? 'package-lock.json' : name)] ??= ''
965
+ })))
966
+ const host = vfsHost(createVfs(files))
967
+ const cwd = posix.resolve('/', directory || '.')
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
+ }
976
+
977
+ const chosen = []
978
+ if (ecosystems.has('npm')) {
979
+ const found = []
980
+ for (const pm of ['pnpm', 'npm', 'yarn1']) {
981
+ const root = rootOf(`${pm} root`, () => lockfileRoot(host, pm, cwd))
982
+ if (root === null) continue
983
+ const names = listed[dirs.indexOf(fromRoot(root))]
984
+ found.push(pathIn(fromRoot(root), pm === 'npm' && names.includes('npm-shrinkwrap.json') ? 'npm-shrinkwrap.json' : lockfileOf(pm)))
985
+ }
986
+ if (found.length === 0) {
987
+ const bun = ['bun.lock', 'bun.lockb'].find((name) => lists(name))
988
+ throw bun === undefined
989
+ ? 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`)
990
+ : 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`)
991
+ }
992
+ if (found.length > 1) throw new ValidateError('lockfile-ambiguous', `more than one lockfile installs ${where}: ${found.join(', ')}; give --lockfile=path`)
993
+ chosen.push(found[0])
994
+ }
995
+ if (ecosystems.has('cargo')) {
996
+ const lock = rootOf('Cargo workspace', () => findCargoLock(cwd, host))
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`)
998
+ chosen.push(fromRoot(lock))
999
+ }
1000
+ return Promise.all(chosen.map(async (path) => ({ path, text: await read(path) })))
1001
+ }
1002
+
1003
+ export async function installedNpmPackages(dir) {
1004
+ const { root } = parsePath(dir)
1005
+ const at = (path) => join(root, ...path.split('/'))
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) => {
1012
+ if (!(error instanceof ValidateError)) throw error
1013
+ if (error.code === 'no-lockfile') return null
1014
+ // As repositoryLockfiles tells it, but the flags validate takes in its place.
1015
+ throw new ValidateError(error.code, error.message.replace(/[:;] give --lockfile=path.*$/su, '').replace("which validate doesn't read", "which isn't read"))
1016
+ })
1017
+ if (found === null) return null
1018
+ const [{ path, text }] = found
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]) => {
1024
+ const split = key.lastIndexOf('@')
1025
+ return { name: key.slice(0, split), version: key.slice(split + 1) }
1026
+ })
1027
+ }
1028
+
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
+
1046
+ const MOVED = new Set([301, 302, 307, 308])
1047
+
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.
1049
+ export function followingMoves(client, github) {
1050
+ let name = github
1051
+ let moved
1052
+ const read = (method) => async (options) => {
1053
+ try {
1054
+ return await client[method]({ ...options, repo: name })
1055
+ } catch (error) {
1056
+ if (!(error instanceof HttpError && MOVED.has(error.status))) throw error
1057
+ moved ??= client.getRepo({ repo: github, followRedirect: true }).then((info) => {
1058
+ name = info.full_name
1059
+ return name
1060
+ })
1061
+ await moved
1062
+ if (name === github) throw error
1063
+ return client[method]({ ...options, repo: name })
1064
+ }
1065
+ }
1066
+ const methods = ['getRepoTreeId', 'getRepoTreeTarball', 'listRepoDir', 'getRepoFile', 'getRepoHead', 'listRepoTags', 'listRepoBranches', 'compareCommits']
1067
+ return { name: () => name, ...Object.fromEntries(methods.map((method) => [method, read(method)])) }
1068
+ }
1069
+
1070
+ const BRANCHES_AT_ONCE = 6
1071
+ const MAX_COMPARED = 100
1072
+
1073
+ // Null for none, as of a fork's, which GitHub serves through its name too.
1074
+ export async function historyOf(repository, commit, { maxCompared = MAX_COMPARED, deadline = Number.POSITIVE_INFINITY, now = Date.now } = {}) {
1075
+ const holds = async (ref) => {
1076
+ try {
1077
+ return ['ahead', 'identical'].includes((await repository.compareCommits({ base: commit, head: ref })).status)
1078
+ } catch (error) {
1079
+ // A commit GitHub has none of, or none in common with the ref.
1080
+ if (error instanceof HttpError && error.status === 404) return false
1081
+ throw error
1082
+ }
1083
+ }
1084
+ const head = await repository.getRepoHead({})
1085
+ if (head.oid === commit || (await holds(head.branch))) return { branch: head.branch }
1086
+ const tag = (await repository.listRepoTags({})).find((entry) => entry.oid === commit)
1087
+ if (tag) return { tag: tag.tag }
1088
+ const branches = (await repository.listRepoBranches({})).filter((entry) => entry.branch !== head.branch)
1089
+ const at = branches.find((entry) => entry.oid === commit)
1090
+ if (at) return { branch: at.branch }
1091
+ let found = null
1092
+ let compared = 0
1093
+ await eachOf(branches.slice(0, maxCompared), BRANCHES_AT_ONCE, async ({ branch }) => {
1094
+ if (found !== null || now() > deadline) return
1095
+ compared++
1096
+ if (await holds(branch)) found ??= { branch }
1097
+ })
1098
+ const left = branches.length - compared
1099
+ if (found === null && left > 0) {
1100
+ throw new ValidateError('history-unread', `${commit} is at no tag or branch head of ${repository.name()} nor in its default branch's history, and ${left} of its ${branches.length} other branches were left uncompared${now() > deadline ? ' past the deadline' : ` (${maxCompared} compared at most)`}: give --sha= a commit of its default branch or at a tag`)
1101
+ }
1102
+ return found
1103
+ }
1104
+
1105
+ const fetchedEcosystems = (plan) => new Set(plan.packages.filter((pkg) => FETCHED.has(pkg.ecosystem) && pkg.name && pkg.version).map((pkg) => pkg.ecosystem))
1106
+
1107
+ function listedEntries(listed, dir = '') {
1108
+ const entries = new Map()
1109
+ for (const entry of listed) {
1110
+ const value = entry.type === 'commit' ? { submodule: true } : entry.mode === '120000' ? { symlink: '' } : entry.type === 'blob' ? { sha: entry.sha } : undefined
1111
+ if (value) entries.set(pathIn(dir, entry.path), value)
1112
+ }
1113
+ return entries
1114
+ }
1115
+
1116
+ export async function validateRepository(plan, { repository = null, commit, directory = '' }, { own = true, lockfile = true, listTree, strict = true, fetch, deadline, generated, attested, client } = {}) {
1117
+ const ecosystems = fetchedEcosystems(plan)
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
1121
+ const github = repository?.name()
1122
+ const told = (error) => {
1123
+ if (strict || !(error instanceof ValidateError)) throw error
1124
+ return error.code
1125
+ }
1126
+ let ref
1127
+ try {
1128
+ if ((own || fromRepository) && (repository === null || commit === undefined)) {
1129
+ const [code, what] = repository === null ? ['no-repository', 'repository to read it of'] : ['no-commit', 'commit to read it at']
1130
+ throw new ValidateError(code, `no ${what}: ${own ? 'its own code' : 'its lockfile'} is read of the repository`)
1131
+ }
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
1135
+ if (ref === null) {
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`)
1137
+ }
1138
+ } catch (error) {
1139
+ const code = told(error)
1140
+ const report = await validateBundle(plan, { reason: own ? code : 'skipped' }, { fetch, deadline })
1141
+ return fromRepository ? { ...report, lockfileError: code } : report
1142
+ }
1143
+ // Own code's files, read whole or listed; awaited later, its failure then.
1144
+ const reading = own && !listTree ? repositoryFiles(repository, { commit, directory }) : null
1145
+ const listing = own && listTree
1146
+ ? listTree([...new Set([...plan.own.map((file) => pathIn(directory, file.path)), pathIn(directory, 'package.json')])].filter(inTree)).then(({ entries, cut }) => {
1147
+ const listed = listedEntries(entries)
1148
+ // The directories it lists, any entry in each: where it was cut short, those alone.
1149
+ const dirs = new Set(entries.map((entry) => posix.dirname(entry.path)))
1150
+ return { entries: listed, cut, dirs, names: Map.groupBy([...listed.keys()].filter((path) => !listed.get(path).submodule), (path) => posix.dirname(path)) }
1151
+ })
1152
+ : null
1153
+ reading?.catch(() => {})
1154
+ listing?.catch(() => {})
1155
+ const listDir = (dir) => repository.listRepoDir({ sha: commit, ...(dir && { directory: dir }) })
1156
+ const list = memoized(async (dir) => {
1157
+ const listed = await listing
1158
+ if (listed && (!listed.cut || listed.dirs.has(dir || '.'))) return (listed.names.get(dir || '.') ?? []).map((path) => posix.basename(path))
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
1165
+ }
1166
+ const under = (path) => reading && (directory === '' || path === directory || path.startsWith(`${directory}/`))
1167
+ const tree = memoized((dir) => (under(dir) ? reading : repositoryFiles(repository, { commit, directory: dir }))
1168
+ .catch(absent(new Map()))
1169
+ .then((entries) => filesUnder(entries, dir)))
1170
+ const patchEntries = async () => {
1171
+ const entries = new Map()
1172
+ const dirs = new Set([...plan.patches.keys()].map((path) => repoDir(posix.dirname(pathIn(directory, path)))))
1173
+ await Promise.all([...dirs].map(async (dir) => {
1174
+ for (const [path, entry] of listedEntries(await listDir(dir).catch(absent([])), dir)) entries.set(path, entry)
1175
+ }))
1176
+ return entries
1177
+ }
1178
+ const readRepository = async (path) => {
1179
+ const held = under(path) ? (await reading).get(path)?.data : undefined
1180
+ return held ? asText.decode(held) : repository.getRepoFile({ path, ref: commit })
1181
+ }
1182
+ const git = gitReader(client)
1183
+ let locked = null
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
+ })
1190
+ try {
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) }))
1201
+ const held = files && lockedBy(files)
1202
+ for (const ecosystem of held ? ecosystems : []) {
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`)
1207
+ }
1208
+ locked = held
1209
+ } catch (error) {
1210
+ lockfileError = told(error)
1211
+ }
1212
+ const read = reading?.then((entries) => ({ entries })) ?? listing?.then(({ entries, cut }) => ({ entries, cut }))
1213
+ ?? (repository && commit !== undefined && plan.patches.size > 0 ? patchEntries().then((entries) => ({ reason: 'skipped', entries })) : null)
1214
+ const checked = read ? read.then((found) => ({ ...found, repository: repository.name(), ...(repository.name() !== github && { movedFrom: github }), commit, ref, directory })) : { reason: 'skipped' }
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
+ }
1247
+ }
1248
+
1249
+ export async function validate(file, { client, github, sha, directory, own = true, lockfile = true, fetch, deadline } = {}) {
1250
+ const { kind, artifact } = parseFileWithKind(file)
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))))
1253
+ const repo = artifact.repo ?? {}
1254
+ const where = { github: github ?? repo.github, commit: sha ?? repo.commit, directory: directory ?? repo.directory ?? '' }
1255
+ const readLocal = (path) => {
1256
+ try {
1257
+ return readFileSync(path, 'utf8')
1258
+ } catch (error) {
1259
+ throw unreadableLockfile(path, error)
1260
+ }
1261
+ }
1262
+ // Each read as it is held to, once the commit is: none where no package needs one.
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
1268
+ try {
1269
+ return await validateRepository(plan, { repository, commit: where.commit, directory: where.directory }, { own, lockfile: lockfiles, fetch, deadline, generated, attested, client })
1270
+ } catch (error) {
1271
+ if (!(error instanceof ValidateError) || !['no-repository', 'no-commit'].includes(error.code)) throw error
1272
+ const fromRepository = lockfile === true && fetchedEcosystems(plan).size > 0
1273
+ const or = !own ? 'or --lockfile=path, or --no-lockfile to hold its dependencies to their registry alone'
1274
+ : fromRepository ? 'or, to validate its dependencies alone, --only-deps with --lockfile=path or --no-lockfile' : 'or --only-deps to validate its dependencies alone'
1275
+ const what = own ? 'its own code' : 'its lockfile'
1276
+ throw error.code === 'no-repository'
1277
+ ? new ValidateError('no-repository', `${file} records no repository to read ${what} of: give --github=owner/name and --sha=commit, ${or}`)
1278
+ : new ValidateError('no-commit', `${file} records no commit to read ${what} at: give --sha=commit, ${or}`)
1279
+ }
1280
+ }
1281
+
1282
+ const TONES = { differs: 'red', unexpected: 'red', invalid: 'red', unverified: 'yellow', incomplete: 'yellow', valid: 'green', generated: 'green' }
1283
+
1284
+ export function printValidationReport(report, { out = process.stdout, err = process.stderr, showValid = false, colors } = {}) {
1285
+ const paintOut = painter(out, colors)
1286
+ const paintErr = painter(err, colors)
1287
+ const status = (paint) => (text, column, row) => paint(column === 'status' ? TONES[row.status] : column === 'of' && row.of === 'own code' ? DIM : null, text)
1288
+ const tables = []
1289
+ const files = [...Map.groupBy(report.problems, (problem) => `${problem.kind}\0${problem.of}`).values()].map((group) => ({
1290
+ status: group[0].kind,
1291
+ of: group[0].of === 'own' ? 'own code' : group[0].of.replace(/^npm:/u, ''),
1292
+ files: group.map((problem) => `${problem.path}${problem.reason ? ` (${FILE_REASONS[problem.reason] ?? problem.reason})` : ''}`).join('\n'),
1293
+ }))
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` : ''))
1295
+ const told = report.packages.filter((pkg) => (showValid || pkg.status !== 'valid') && noteText(pkg) !== '')
1296
+ const ecosystems = told.some((pkg) => pkg.ecosystem !== 'npm')
1297
+ const packages = told.map((pkg) => ({ status: pkg.status, ...(ecosystems && { ecosystem: pkg.ecosystem }), package: `${pkg.name}@${pkg.version}`, note: noteText(pkg) }))
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) }))
1301
+ if (tables.length > 0) out.write(`${tables.join('\n\n')}\n`)
1302
+
1303
+ const { counts, own } = report
1304
+ const lines = [
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` : ''}`,
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}` : ''}`
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}`] : []),
1310
+ ...(report.lockfiles?.length > 0 ? [`Dependencies held to ${report.lockfiles.join(', ')}`]
1311
+ : report.lockfiles === null && counts.packages > 0
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`)] : []),
1313
+ ]
1314
+ err.write(`${lines.join('\n')}\n`)
1315
+ }