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