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