@exodus/stasis 1.0.0-rc.1 → 1.0.0-rc.2

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/bin/stasis.js CHANGED
@@ -1,9 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import { fileURLToPath } from 'node:url'
4
- import { basename, dirname, isAbsolute, join, resolve } from 'node:path'
4
+ import { basename, dirname, resolve } from 'node:path'
5
5
  import { existsSync, realpathSync } from 'node:fs'
6
- import { homedir } from 'node:os'
7
6
  import assert from 'node:assert/strict'
8
7
  import { parseLeadingOptions, valueFlagsOf } from '@exodus/stasis-core/util'
9
8
  import { addCli, brotliQualityOption, extractCli, parseRunOptions, pruneCli, runProgram, setEnv, setRunEnv } from '@exodus/stasis-core/cli'
@@ -68,12 +67,14 @@ async function warningsOnFailure(build) {
68
67
  }
69
68
  }
70
69
 
71
- // The per-user cache directory, as each platform names it (XDG ignores a relative path).
72
- function userCacheDir() {
73
- if (isAbsolute(process.env.XDG_CACHE_HOME ?? '')) return join(process.env.XDG_CACHE_HOME, 'stasis')
74
- if (process.platform === 'darwin') return join(homedir(), 'Library', 'Caches', 'stasis')
75
- if (process.platform === 'win32') return join(process.env.LOCALAPPDATA || join(homedir(), 'AppData', 'Local'), 'stasis', 'Cache')
76
- return join(homedir(), '.cache', 'stasis')
70
+ // The cache directory (STASIS_CACHE_DIR, else the platform's), refused where it is relative or there is none.
71
+ async function cacheDir() {
72
+ const { userCacheDir } = await import('../src/cmd/cache-dir.js')
73
+ try {
74
+ return userCacheDir()
75
+ } catch (cause) {
76
+ fail(`Error: ${cause.message}`)
77
+ }
77
78
  }
78
79
 
79
80
  const command = argv.shift()
@@ -219,7 +220,7 @@ if (command === '-v' || command === '--version') {
219
220
  }
220
221
  // Tarballs, zips and GitHub trees are cached where `stasis audit` caches.
221
222
  const { setCacheDir } = await import('../src/vfs-bundle.js')
222
- setCacheDir(userCacheDir())
223
+ setCacheDir(await cacheDir())
223
224
  const { githubBundleCommand } = await import('../src/cmd/github-bundle.js')
224
225
  await githubBundleCommand({
225
226
  github,
@@ -384,7 +385,7 @@ if (command === '-v' || command === '--version') {
384
385
  // Upstream caches what it looks up (a package's GitHub repo, a month each) only where told to, and
385
386
  // answers from it without asking: a per-user directory, never one shared with less trusted jobs.
386
387
  const { setCacheDir } = await import('@preventive/upstream/npm.js')
387
- setCacheDir(userCacheDir())
388
+ setCacheDir(await cacheDir())
388
389
  const { AuditError, audit, printAuditReport } = await import('../src/audit.js')
389
390
  // With no file, the package manager's lockfile installing the project too, or each JS one its
390
391
  // stasis.config.json names in its `lockfiles`: their packages the bundle doesn't hold checked for
@@ -441,7 +442,7 @@ if (command === '-v' || command === '--version') {
441
442
  // `stasis audit` caches, each held to its id or integrity as it is read.
442
443
  const { createClient } = await import('@preventive/upstream/github.js')
443
444
  const { setCacheDir } = await import('@preventive/upstream/npm.js')
444
- setCacheDir(userCacheDir())
445
+ setCacheDir(await cacheDir())
445
446
  const { githubToken, withNpmToken } = await import('../src/cmd/tokens.js')
446
447
  const { ValidateError, exitCodeOf, validate, printValidationReport } = await import('../src/validate.js')
447
448
  let report
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/stasis",
3
- "version": "1.0.0-rc.1",
3
+ "version": "1.0.0-rc.2",
4
4
  "description": "File-level source bundles and lockfiles",
5
5
  "type": "module",
6
6
  "exports": {
@@ -43,6 +43,7 @@
43
43
  "src/bundle-reader.js",
44
44
  "src/cmd/bundle.js",
45
45
  "src/cmd/bundle-options.js",
46
+ "src/cmd/cache-dir.js",
46
47
  "src/cmd/build.js",
47
48
  "src/cmd/diff.js",
48
49
  "src/cmd/github-bundle.js",
@@ -118,13 +119,13 @@
118
119
  "homepage": "https://github.com/PreventiveMeasures/stasis#readme",
119
120
  "dependencies": {
120
121
  "@exodus/bytes": "^1.16.0",
121
- "@exodus/stasis-core": "1.0.0-rc.1",
122
- "@exodus/stasis-plugins": "1.0.0-rc.1",
123
- "@preventive/archive": "^1.4.0",
122
+ "@exodus/stasis-core": "1.0.0-rc.2",
123
+ "@exodus/stasis-plugins": "1.0.0-rc.2",
124
+ "@preventive/archive": "^1.4.1",
124
125
  "@preventive/deptree": "^1.5.0",
125
126
  "@preventive/lockfile": "^1.3.0",
126
- "@preventive/rn": "1.0.0-alpha.6",
127
- "@preventive/upstream": "^1.6.0",
127
+ "@preventive/rn": "1.0.0-alpha.8",
128
+ "@preventive/upstream": "^1.7.0",
128
129
  "@preventive/vfs": "^1.1.0",
129
130
  "flow-remove-types": "^2.322.0",
130
131
  "oxc-parser": "^0.152.0"
package/src/cmd/build.js CHANGED
@@ -23,7 +23,7 @@ export { bundleFromLockfile }
23
23
 
24
24
  // An allowlist, so that a bundle format added later fails closed here rather than in a cryptic esbuild error.
25
25
  const BUILDABLE_FORMATS = new Set([
26
- 'module', 'commonjs', 'json', 'module-typescript', 'commonjs-typescript',
26
+ 'module', 'commonjs', 'json', 'module-typescript', 'commonjs-typescript', 'generic-javascript', 'generic-typescript',
27
27
  'resource', 'resource:base64', 'directory', 'stat:file', 'stat:directory',
28
28
  ])
29
29
 
package/src/cmd/bundle.js CHANGED
@@ -20,7 +20,7 @@ import { installedRecords, pinInstalled } from '../installed.js'
20
20
  import { State } from '@exodus/stasis-core/state'
21
21
  import { bundledIntegrity, readFileSyncMaybe } from '@exodus/stasis-core/state-util'
22
22
  import { checkoutCommit, detectRepo, normalizeEntries, packageLookup, packageJSONStat, packageType, parseGithubRepository, readJson, readModuleManifest, readPackageJson, readRegularFileOrNull, scopeManifestOf } from '@exodus/stasis-core/bundle-util'
23
- import { attestsAny, BUNDLE_FILE, CONFIG_FILE, EMPTY_MODULE_PATH, assertRealPathWithinBase, classifyFormat, dependencyEcosystem, fileInBucket, archiveIntegrity, hasNodeModulesSegment, heldToAccounts, isDotEnvFile, isPathWithin, isPlainObject, JS_EXTENSIONS, LOCK_FILE, memoized, moduleFileKey, moduleFileKeys, moduleInfo, nestedMap, nodeExtFormat, observeExecutable, parseExtraOption, parseLockfilesOption, parseResourcesOption, posixPathEscapes, relativeEscapes, splitNodeModulesPath, toPosix, upsert, withAttestedOf, withMetadataOf, sameGithub } from '@exodus/stasis-core/util'
23
+ import { attestsAny, BUNDLE_FILE, CONFIG_FILE, EMPTY_MODULE_PATH, assertRealPathWithinBase, classifyFormat, dependencyEcosystem, fileInBucket, archiveIntegrity, hasNodeModulesSegment, heldToAccounts, isDotEnvFile, isPathWithin, isPlainObject, JS_EXTENSIONS, LOCK_FILE, memoized, metroFormat, moduleFileKey, moduleFileKeys, moduleInfo, nestedMap, observeExecutable, parseExtraOption, parseLockfilesOption, parseResourcesOption, posixPathEscapes, relativeEscapes, splitNodeModulesPath, toPosix, upsert, withAttestedOf, withMetadataOf, sameGithub } from '@exodus/stasis-core/util'
24
24
  import { diskHost } from '@exodus/stasis-core/host'
25
25
  import { linksOn, parsePolicy, recordedIn } from '@exodus/stasis-core/policy'
26
26
  import { parseBundleBytes } from '@exodus/stasis-core/brotli'
@@ -1151,8 +1151,8 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1151
1151
  reached.add(abs)
1152
1152
  if (restrict !== null && !isTransformInput(abs, info)) metroTaken.add(abs)
1153
1153
  if (info.resource) resourceRels.add(rel)
1154
- // Under --metro a code file has only the format its name gives it, as the Metro plugin records it.
1155
- formatsByRel.set(rel, metro && !info.resource ? nodeExtFormat(abs) : info.format)
1154
+ // Under --metro a code file has the format Metro builds it in, as the Metro plugin records it.
1155
+ formatsByRel.set(rel, metro && !info.resource ? metroFormat(abs) : info.format)
1156
1156
  for (const e of info.edges) {
1157
1157
  if (e.builtin || e.dynamic) continue
1158
1158
  let target
@@ -1338,8 +1338,11 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1338
1338
  put(rel, buf.toString('utf8'), format)
1339
1339
  }
1340
1340
  const realBase = host.realpath(baseDir)
1341
- // Metro's empty module is carried as the file it is, a code file of Metro's (no format).
1342
- if (usesEmpty && metroEmpty !== null) reached.add(metroEmpty)
1341
+ // Metro's empty module is carried as the file it is, a code file of Metro's.
1342
+ if (usesEmpty && metroEmpty !== null) {
1343
+ reached.add(metroEmpty)
1344
+ if (formatsByRel.get(toRel(metroEmpty)) === undefined) formatsByRel.set(toRel(metroEmpty), metroFormat(metroEmpty))
1345
+ }
1343
1346
  for (const abs of reached) {
1344
1347
  const rel = toRel(abs)
1345
1348
  // Security: an entry is taken as spelled (and --metro-resolver keeps a path it can't realpath), so a
@@ -1353,7 +1356,7 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1353
1356
  if (formatsByRel.has(EMPTY_MODULE_PATH)) {
1354
1357
  throw new Error(`Bundle needs the reserved empty-module path ${EMPTY_MODULE_PATH}, but the project has a real file there`)
1355
1358
  }
1356
- put(EMPTY_MODULE_PATH, '', metro ? undefined : 'commonjs')
1359
+ put(EMPTY_MODULE_PATH, '', metro ? metroFormat(EMPTY_MODULE_PATH) : 'commonjs')
1357
1360
  }
1358
1361
 
1359
1362
  const packageOf = packageLookup(baseDir, { host })
@@ -0,0 +1,19 @@
1
+ import { isAbsolute } from 'node:path'
2
+
3
+ import { cacheDirFor } from '@preventive/upstream/npm.js'
4
+
5
+ // The directory the commands cache in: STASIS_CACHE_DIR itself where it is set, else the platform's
6
+ // cache directory for stasis as @preventive/upstream names it, the one it reads as stasis's (an
7
+ // absolute XDG_CACHE_HOME's only on Linux and the BSDs). STASIS_CACHE_DIR is the user's own choice,
8
+ // so a relative one is refused rather than ignored: it would resolve against whatever directory a
9
+ // command runs in, a project's checkout as likely as not. Both read the process's environment.
10
+ export function userCacheDir() {
11
+ const own = process.env.STASIS_CACHE_DIR
12
+ if (own) {
13
+ if (!isAbsolute(own)) throw new Error(`STASIS_CACHE_DIR must be an absolute path, not ${JSON.stringify(own)}`)
14
+ return own
15
+ }
16
+ const dir = cacheDirFor('stasis')
17
+ if (dir === undefined) throw new Error('there is no cache directory, as no absolute home directory is known: set STASIS_CACHE_DIR')
18
+ return dir
19
+ }
package/src/diff.js CHANGED
@@ -1,4 +1,4 @@
1
- import { ATTESTED_METADATA, extSetsEqual, moduleFileKey, REPO_FIELDS, sameGithub, sortPaths, upsert } from '@exodus/stasis-core/util'
1
+ import { ATTESTED_METADATA, extSetsEqual, moduleFileKey, REPO_FIELDS, sameAttested, sameGithub, sortPaths, upsert } from '@exodus/stasis-core/util'
2
2
 
3
3
  // Two parsed artifacts compared by module and file, never reading disk: each file as an SRI digest, a bundle's bytes
4
4
  // re-hashed by the injected `hash`.
@@ -62,7 +62,7 @@ export function diffArtifacts(left, right, { imports = false, hash } = {}) {
62
62
  // A dependency's repo picks the GitHub advisories audit asks, so a change to it is one to review.
63
63
  if (!reposAgree(l.repo, r.repo)) changes.repoChange = { from: l.repo, to: r.repo }
64
64
  for (const field of ATTESTED_METADATA) {
65
- if (JSON.stringify(l[field]) !== JSON.stringify(r[field])) changes[`${field}Change`] = { from: l[field], to: r[field] }
65
+ if (!sameAttested(field, l[field], r[field])) changes[`${field}Change`] = { from: l[field], to: r[field] }
66
66
  }
67
67
  if (Object.keys(changes).length > 0) modulesChanged.push({ dir, name: r.name ?? l.name, ...changes })
68
68
 
@@ -158,13 +158,15 @@ const CHANGED = '*'
158
158
  // `owner/name`, its commit and directory where recorded: `owner/name@0123abc (packages/x)`.
159
159
  const repoLabel = ({ github, commit, directory }) => `${github ?? '?'}${commit ? `@${commit.slice(0, 7)}` : ''}${directory ? ` (${directory})` : ''}`
160
160
 
161
+ const patchLabel = (patch) => (patch ? `${patch.integrity}${patch.file ? ` (${patch.file})` : ''}` : 'none')
162
+
161
163
  function describeModuleChange({ name, versionChange, nameChange, repoChange, integrityChange, patchChange }) {
162
164
  const segs = []
163
165
  if (versionChange) segs.push(`${versionChange.from} -> ${versionChange.to}`)
164
166
  if (nameChange) segs.push(`name ${nameChange.from} -> ${nameChange.to}`)
165
167
  if (repoChange) segs.push(`repo ${repoLabel(repoChange.from)} -> ${repoLabel(repoChange.to)}`)
166
168
  if (integrityChange) segs.push(`integrity ${integrityChange.from ?? 'none'} -> ${integrityChange.to ?? 'none'}`)
167
- if (patchChange) segs.push(`patch ${patchChange.from?.integrity ?? 'none'} -> ${patchChange.to?.integrity ?? 'none'}`)
169
+ if (patchChange) segs.push(`patch ${patchLabel(patchChange.from)} -> ${patchLabel(patchChange.to)}`)
168
170
  return `${name ? `${name} ` : ''}${segs.join(', ')}`
169
171
  }
170
172
 
package/src/installed.js CHANGED
@@ -4,8 +4,9 @@ import { basename, dirname, join, relative, resolve } from 'node:path'
4
4
  import { isValidRepoField } from '@exodus/stasis-core/bundle'
5
5
  import { parseGithubRepository, readJson, readText } from '@exodus/stasis-core/bundle-util'
6
6
  import { diskHost } from '@exodus/stasis-core/host'
7
- import { hexIntegrity, isNpmDependency, isPlainObject, relativeEscapes, tarballIntegrityOf, toPosix, upsert, withoutBom } from '@exodus/stasis-core/util'
7
+ import { hexIntegrity, isNpmDependency, isPlainObject, memoized, relativeEscapes, tarballIntegrityOf, toPosix, upsert, withoutBom } from '@exodus/stasis-core/util'
8
8
  import { parsePnpmLockfile } from '@preventive/lockfile/pnpm.js'
9
+ import { parseYaml } from '@preventive/lockfile/yaml.js'
9
10
  import { parseYarn1Lockfile } from '@preventive/lockfile/yarn1.js'
10
11
 
11
12
  // What the package manager that laid out a node_modules recorded there; one missing or unreadable names nothing.
@@ -26,6 +27,10 @@ export const resolutionCommit = (resolution) => {
26
27
  return resolution?.type === 'tarball' ? githubTarball(resolution.tarball ?? '') : undefined
27
28
  }
28
29
 
30
+ // As pnpm hashes a patch: a sha256 in hex of its text with its CRLFs as LFs, or as it is.
31
+ export const patchHashes = (text) => new Set([text.replaceAll('\r\n', '\n'), text].map((one) => hash('sha256', one)))
32
+ export const isPatchOf = (text, patchHash) => patchHashes(text).has(patchHash)
33
+
29
34
  export const patchIntegrity = (patchHash) => (/^[\da-f]{64}$/u.test(patchHash ?? '') ? hexIntegrity('sha256', patchHash) : undefined)
30
35
 
31
36
  const repoAt = (commit, path = '') => {
@@ -104,15 +109,44 @@ const storeNames = (key) => {
104
109
  }
105
110
  export const STORE_HASH = /_[\da-f]{32}$/u
106
111
 
112
+ // Of the lockfile `file` and the `patchers` (`[selector, { hash, path }]`) it locks: the file each hash's patch is in.
113
+ function patchLocator(file, patchers, host) {
114
+ // node_modules/.pnpm/lock.yaml copies the pnpm-lock.yaml beside node_modules, its paths from there.
115
+ const base = basename(file) === 'lock.yaml' ? resolve(file, '../../..') : dirname(file)
116
+ const settings = join(base, 'pnpm-workspace.yaml')
117
+ const named = memoized(() => {
118
+ const patches = parseLock(settings, host, true, parseYaml)?.patchedDependencies
119
+ return isPlainObject(patches) ? patches : {}
120
+ })
121
+ // Every file named for the hash held to it; of two of one text, the first named, as deptree records it.
122
+ return memoized((patchHash) => {
123
+ const [first] = patchers.filter(([, patch]) => patch.hash === patchHash).map(([selector, patch]) => {
124
+ const refused = (why) => new Error(`${file} patches ${selector} with ${patchHash}: ${why}`)
125
+ // pnpm 11's lockfile names a patch by its hash alone, its file in pnpm-workspace.yaml.
126
+ const path = patch.path ?? named()[selector]
127
+ if (typeof path !== 'string') throw refused(`neither it nor ${settings} names the patch's file`)
128
+ const at = resolve(base, path)
129
+ const text = readText(host, at)
130
+ if (text === null) throw refused(`${at} can't be read`)
131
+ if (!isPatchOf(text, patchHash)) throw refused(`${at} is a patch of another hash`)
132
+ return at
133
+ })
134
+ return first
135
+ })
136
+ }
137
+
107
138
  function pnpmLocks(file, host, strict) {
108
139
  const parsed = parseLock(file, host, strict, (text) => ({ lockfile: parsePnpmLockfile(text).lockfile }))
109
140
  if (parsed === undefined) return undefined
110
141
  const { lockfile } = parsed
142
+ // Of its patches alone: the cache keeps a run's locators, which must not hold the lockfile.
143
+ const patchers = Object.entries(lockfile?.patchedDependencies ?? {})
144
+ const patchAt = patchers.length === 0 ? undefined : patchLocator(file, patchers, host)
111
145
  const stored = new Map()
112
146
  const keyed = new Map()
113
147
  const ids = []
114
148
  for (const [key, { name, version, resolution, patchHash }] of Object.entries(lockfile?.packages ?? {})) {
115
- const record = { version, repo: repoAt(resolutionCommit(resolution), resolution?.path), integrity: resolution?.integrity, patchHash }
149
+ const record = { version, repo: repoAt(resolutionCommit(resolution), resolution?.path), integrity: resolution?.integrity, patchHash, ...(patchHash !== undefined && { patchAt }) }
116
150
  for (const store of storeNames(key)) stored.set(store, record)
117
151
  keyed.set(key, record)
118
152
  ids.push([`${name}@${version ?? ''}`, record])
@@ -214,9 +248,11 @@ export function installedMetadata(dir, ecosystem, { repo, integrity, patchHash }
214
248
  return { ...(repo && { repo }), ...(tarball && { integrity: tarball }), ...(patch && { patch: { integrity: patch } }) }
215
249
  }
216
250
 
217
- export function installedRecords(modules, root, host = diskHost, { stores = new Map(), layouts = new Map() } = {}) {
251
+ // With `locate`, a patch's record carries its file, from `root`: found and held to its hash, else refused.
252
+ export function installedRecords(modules, root, host = diskHost, { stores = new Map(), layouts = new Map(), locate = false } = {}) {
218
253
  const accounts = new Map()
219
254
  let top // the bundle root's real path, as each package's is compared with it
255
+ const rootReal = () => (top ??= resolve(host.realpath(resolve(root))))
220
256
  const layoutAt = (dir) => upsert(layouts, dir, () => layoutOf(dir, host))
221
257
  for (const [dir, info] of modules) {
222
258
  // An untagged bucket under node_modules is npm's too (an artifact from before the tag).
@@ -235,7 +271,7 @@ export function installedRecords(modules, root, host = diskHost, { stores = new
235
271
  ;[layout, at] = [upsert(stores, store, () => storeOf(store, host)), [name]]
236
272
  } else {
237
273
  try {
238
- top ??= resolve(host.realpath(resolve(root)))
274
+ rootReal()
239
275
  } catch {
240
276
  return accounts
241
277
  }
@@ -253,13 +289,17 @@ export function installedRecords(modules, root, host = diskHost, { stores = new
253
289
  const differs = locks && lockDiffers(info.version, record, locked, none)
254
290
  if (differs) throw new Error(`${lockfile} doesn't lock ${dir} as installed (${differs}): install from it again`)
255
291
  const told = record ?? locked
256
- if (told !== undefined && !told.ambiguous) accounts.set(dir, { ...installedMetadata(dir, info.ecosystem, told), ...(record === undefined && { locked: true }) })
292
+ if (told === undefined || told.ambiguous) continue
293
+ const metadata = installedMetadata(dir, info.ecosystem, told)
294
+ // A record of the lockfile alone is held to, never recorded: its patch is not looked for.
295
+ if (locate && record !== undefined && metadata.patch) metadata.patch.file = toPosix(relative(rootReal(), told.patchAt(told.patchHash)))
296
+ accounts.set(dir, { ...metadata, ...(record === undefined && { locked: true }) })
257
297
  }
258
298
  return accounts
259
299
  }
260
300
 
261
301
  export function pinInstalled(bundle, root, host = diskHost) {
262
- const accounts = installedRecords(bundle.modules, root, host)
302
+ const accounts = installedRecords(bundle.modules, root, host, { locate: true })
263
303
  for (const [dir, { locked, ...metadata }] of accounts) if (!locked) bundle.modules.set(dir, { ...bundle.modules.get(dir), ...metadata })
264
304
  return accounts
265
305
  }
@@ -3,8 +3,8 @@ import { installedRecords } from './installed.js'
3
3
 
4
4
  // `stasis run` preloads this before the loader; what it reads is kept across a run's calls.
5
5
  const cache = { stores: new Map(), layouts: new Map() }
6
- globalThis[INSTALLED] = (modules, root) => {
7
- const accounts = installedRecords(modules, root, undefined, cache)
6
+ globalThis[INSTALLED] = (modules, root, { locate } = {}) => {
7
+ const accounts = installedRecords(modules, root, undefined, { ...cache, locate })
8
8
  for (const [dir, { repo: _repo, ...account }] of accounts) accounts.set(dir, account)
9
9
  return accounts
10
10
  }
package/src/scan.js CHANGED
@@ -169,7 +169,7 @@ export class Scan {
169
169
 
170
170
  // classifyExtension gives 'resource' only for a non-code name.
171
171
  #isResource(file) {
172
- return this.resources.size > 0 && classifyExtension(file, this.resources) === 'resource'
172
+ return classifyExtension(file, this.resources) === 'resource'
173
173
  }
174
174
 
175
175
  walk(entries) {
@@ -32,6 +32,7 @@ export const PACKAGE_REASONS = {
32
32
  'patch-not-locked': 'records a patch its lockfile doesn\'t patch it with',
33
33
  'patch-not-recorded': 'records an integrity and no patch, where its lockfile patches it',
34
34
  'patch-conflict': 'its copies record different patches, or one of them none, where pnpm patches every copy with one',
35
+ 'patch-file-mismatch': 'records its patch in another file than the one it is found in',
35
36
  }
36
37
 
37
38
  // Why a patch wasn't applied, told after `its patch` and its path.
package/src/validate.d.ts CHANGED
@@ -24,7 +24,7 @@ export interface PlanPackage { ecosystem: string; name: string; version: string;
24
24
  export interface ValidationPlan { files: number; own: PlanFile[]; packages: PlanPackage[]; patches: Map<string, string> }
25
25
  // A parsed bundle (Bundle.parse's, or parseFileWithKind's artifact).
26
26
  export interface PlannedBundle {
27
- modules: Map<string, { name?: string; version?: string; ecosystem?: string; integrity?: string; repo?: { github: string; directory?: string; commit?: string }; patch?: { integrity: string }; files: Record<string, unknown> }>
27
+ modules: Map<string, { name?: string; version?: string; ecosystem?: string; integrity?: string; repo?: { github: string; directory?: string; commit?: string }; patch?: { integrity: string; file?: string }; files: Record<string, unknown> }>
28
28
  formats?: Map<string, string>
29
29
  }
30
30
  export function validationPlan(bundle: PlannedBundle, options?: { lockfile?: boolean }): ValidationPlan
package/src/validate.js CHANGED
@@ -18,7 +18,7 @@ import { createVfs } from '@preventive/vfs'
18
18
  import { Lockfile } from '@exodus/stasis-core/lockfile'
19
19
  import { bundledIntegrity, sha512integrity } from '@exodus/stasis-core/state-util'
20
20
 
21
- import { nameAt, STORE_HASH, resolutionCommit } from './installed.js'
21
+ import { isPatchOf, nameAt, patchHashes, STORE_HASH, resolutionCommit } from './installed.js'
22
22
  import { findCargoLock } from './loaders/cargo.js'
23
23
  import { parseFileWithKind } from './parse.js'
24
24
  import { DIM, formatTable, painter } from './table.js'
@@ -78,7 +78,7 @@ export function validationPlan(bundle, { lockfile = false } = {}) {
78
78
  if (lockfile && rel === CARGO_CHECKSUM && ecosystem === 'cargo') continue
79
79
  const file = lockfile ? { path, dir, rel, format, integrity: content } : { path, dir, rel, format, bytes: () => (format === 'resource:base64' ? Buffer.from(content, 'base64') : utf8.encode(content)) }
80
80
  if (ecosystem !== undefined) {
81
- if (!packages.has(key)) packages.set(key, { ecosystem, name: info.name ?? '', version: info.version ?? '', files: [], recorded: new Map(), integrities: new Map(), repos: new Map() })
81
+ if (!packages.has(key)) packages.set(key, { ecosystem, name: info.name ?? '', version: info.version ?? '', files: [], recorded: new Map(), patchFiles: new Map(), integrities: new Map(), repos: new Map() })
82
82
  packages.get(key).files.push(file)
83
83
  } else if (path !== EMPTY_MODULE_PATH || content !== (lockfile ? EMPTY_INTEGRITY : '')) {
84
84
  if (!lockfile && (format === 'patch' || path.endsWith('.patch'))) patches.set(path, content)
@@ -89,6 +89,7 @@ export function validationPlan(bundle, { lockfile = false } = {}) {
89
89
  if (pkg === undefined) continue
90
90
  // In hex, as pnpm's and Cargo's lockfiles spell them.
91
91
  if (info.patch) pkg.recorded.set(dir, integrityHex(info.patch.integrity))
92
+ if (info.patch?.file) pkg.patchFiles.set(dir, info.patch.file)
92
93
  if (info.integrity) pkg.integrities.set(dir, ecosystem === 'cargo' ? integrityHex(info.integrity) : info.integrity)
93
94
  if (info.repo) pkg.repos.set(dir, info.repo)
94
95
  }
@@ -141,9 +142,7 @@ export function fetchFailure(error) {
141
142
  return 'failed'
142
143
  }
143
144
 
144
- // As pnpm hashes a patch: a sha256 in hex of its text with its CRLFs as LFs, or as it is.
145
- const patchHashes = (text) => new Set([text.replaceAll('\r\n', '\n'), text].map(sha256))
146
- export const isPatchOf = (text, hash) => patchHashes(text).has(hash)
145
+ export { isPatchOf }
147
146
 
148
147
  // git quotes a path as C writes a string, bytes past ASCII in octal; null where it is quoted otherwise.
149
148
  const ESCAPES = { a: 7, b: 8, t: 9, n: 10, v: 11, f: 12, r: 13, '"': 34, '\\': 92 }
@@ -398,22 +397,28 @@ function patchesOf(plan) {
398
397
  for (const hash of patchHashes(text)) if (!byHash.has(hash)) byHash.set(hash, held)
399
398
  if (!byName.has(name)) byName.set(name, held)
400
399
  }
401
- return { byHash, byName }
400
+ return { byHash, byName, byPath: plan.patches }
402
401
  }
403
402
 
404
403
  // 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) {
404
+ async function patchOf(held, pkg, files, locked, verdicts, recorded, recordedFile) {
406
405
  const lock = locked?.of(pkg.name, pkg.version)
407
406
  const hash = lock === undefined ? recorded : lock?.hash
408
407
  let patch
409
408
  let text
409
+ // Where it is found, from the bundle root, as a record's `file` names it.
410
+ let file
410
411
  try {
411
- const bundled = hash !== undefined ? held.byHash.get(hash) : lock === undefined && held.byName.get(pnpmPatchName(pkg.name, pkg.version))
412
+ // Of the bundle's of its hash, the one its copies record, where it holds that.
413
+ const own = hash !== undefined && held.byPath.get(recordedFile)
414
+ const bundled = own && isPatchOf(own, hash) ? { path: recordedFile, text: own }
415
+ : hash !== undefined ? held.byHash.get(hash) : lock === undefined && held.byName.get(pnpmPatchName(pkg.name, pkg.version))
412
416
  if (bundled) {
413
417
  patch = { ...(hash !== undefined && { hash }), path: bundled.path, from: 'bundle' }
414
- text = bundled.text
418
+ ;({ text, path: file } = bundled)
415
419
  } else if (lock?.path) {
416
420
  patch = { hash, path: lock.path, from: locked.from }
421
+ file = lock.file
417
422
  text = await locked.read(lock.path)
418
423
  // Held to the hash the lockfile names it by, as the bundle's are found by it.
419
424
  if (!isPatchOf(text, hash)) throw new PatchError('patch-mismatch')
@@ -422,7 +427,7 @@ async function patchOf(held, pkg, files, locked, verdicts, recorded) {
422
427
  const found = lock ? await locked.locate?.(lock) : undefined
423
428
  if (!found) return hash === undefined ? { patched: null } : { patch: { hash, error: 'patch-not-found' }, patched: null }
424
429
  patch = { hash, path: found.path, from: locked.from }
425
- ;({ text } = found)
430
+ ;({ text, file } = found)
426
431
  }
427
432
  let unverified = false
428
433
  if (patch.from === 'bundle') {
@@ -431,7 +436,7 @@ async function patchOf(held, pkg, files, locked, verdicts, recorded) {
431
436
  unverified = verdict !== 'matched'
432
437
  patch.verified = !unverified
433
438
  }
434
- return { patch, patched: patchedFiles(files, parsePatch(text)), unverified }
439
+ return { patch, patched: patchedFiles(files, parsePatch(text)), unverified, file }
435
440
  } catch (error) {
436
441
  return { patch: { ...patch, error: error instanceof PatchError ? error.code : 'patch-unreadable' }, patched: null }
437
442
  }
@@ -527,19 +532,23 @@ export async function validateBundle(plan, own, { fetch = fetchPackage, locked:
527
532
  let published
528
533
  let patch
529
534
  let patched
535
+ let patchFile
530
536
  let unverifiedPatch = false
531
537
  try {
532
538
  if (pinned) candidates = []
533
539
  else if (lock?.from === 'local') candidates = await Promise.all(lock.directories.map((dir) => locked.tree(dir)))
534
540
  else {
535
541
  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) : {})
542
+ ;({ patch, patched, unverified: unverifiedPatch = false, file: patchFile } = pkg.ecosystem === 'npm' && recordedPatch !== null ? await patchOf(bundled, pkg, published, locked, verdicts, recordedPatch, dirs.map((dir) => pkg.patchFiles.get(dir)).find(Boolean)) : {})
537
543
  candidates = patched && !unverifiedPatch ? [patched, published] : [published]
538
544
  }
539
545
  } catch (error) {
540
546
  const reason = fetchFailure(error)
541
547
  return reason === 'integrity-mismatch' ? fails(reason) : unverified(reason)
542
548
  }
549
+ // A copy recording where its patch is records where validation finds it.
550
+ const elsewhere = patchFile === undefined ? undefined : dirs.map((dir) => pkg.patchFiles.get(dir)).find((file) => file !== undefined && file !== patchFile)
551
+ if (elsewhere !== undefined) return fails('patch-file-mismatch', { at: elsewhere, patch: { ...patch, recorded: true } })
543
552
  const checksum = (file) => pkg.ecosystem === 'cargo' && file.rel === CARGO_CHECKSUM
544
553
  const matches = (file, bytes, candidate) => (checksum(file) ? checksumOf(bytes, candidate) : candidate.has(file.rel) && isFile(file, candidate.get(file.rel)))
545
554
  // Each installed copy apart, as one may be patched and another not.
@@ -767,7 +776,7 @@ function readLockfile(name, read) {
767
776
  const known = paths.get(hash)
768
777
  paths.set(hash, { path: known?.path ?? path, selectors: [...(known?.selectors ?? []), selector] })
769
778
  }
770
- return { entries, locals, aliases, directoryAliases, patches: { hashes, paths } }
779
+ return { entries, locals, aliases, directoryAliases, patches: { hashes, paths }, importers: new Set(Object.keys(lockfile?.importers ?? {})) }
771
780
  }
772
781
  if (name === 'Cargo.lock') {
773
782
  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))
@@ -834,9 +843,13 @@ export function lockedBy(lockfiles) {
834
843
  const other = same.find((one) => one.at === at)
835
844
  if (other !== undefined) throw new ValidateError('lockfile-ambiguous', `${other.path} and ${path} both lock ${ecosystem} packages: give one`)
836
845
  const locks = lockOf(path, text)
837
- // npm's locks each location from its directory: the bundle's root from there is one of its importers on disk.
838
- const within = locks.importers === undefined || from !== 'local' || locks.importers.has(root) ? root : ''
839
- const one = { ...locks, path, from, read, tree, git, root: within, at, patchDirs: patches ?? [posix.join(posix.dirname(path), 'patches')] }
846
+ // Given on disk, a lockfile's place in the repository isn't known: the bundle's root from its directory is the
847
+ // longest tail of `root` that is one of its importers, else its own.
848
+ const tails = root.split('/').map((_, start, parts) => parts.slice(start).join('/'))
849
+ const within = locks.importers === undefined || from !== 'local' ? root : tails.find((tail) => locks.importers.has(tail)) ?? ''
850
+ // The bundle's root, in its paths' terms, as a recorded patch's `file` is from there.
851
+ const bundleDir = posix.join(posix.dirname(path), within)
852
+ const one = { ...locks, path, from, read, tree, git, root: within, at, bundleDir, patchDirs: patches ?? [posix.join(posix.dirname(path), 'patches')] }
840
853
  by.set(ecosystem, [...same, one])
841
854
  all.push(one)
842
855
  given.set(one, lockfile)
@@ -884,16 +897,22 @@ export function lockedBy(lockfiles) {
884
897
  if (hash === null) found = null
885
898
  if (hash == null) continue
886
899
  const { path, selectors = [] } = npm.patches.paths.get(hash) ?? {}
887
- if (path) return { hash, path: posix.join(posix.dirname(npm.path), path) }
900
+ const at = path && posix.join(posix.dirname(npm.path), path)
901
+ if (at) return { hash, path: at, file: posix.relative(npm.bundleDir, at) }
888
902
  return { hash, names: [...new Set([...selectors.map((selector) => `${selector.replace('/', '__')}.patch`), pnpmPatchName(name, version)])] }
889
903
  }
890
904
  return found
891
905
  },
892
906
  async locate({ hash, names = [] }) {
893
- for (const path of [...new Set(npms.flatMap((npm) => npm.patchDirs))].flatMap((dir) => names.map((name) => pathIn(dir, name)))) {
894
- // eslint-disable-next-line no-await-in-loop -- the first of them that is there
895
- const text = await Promise.try(() => this.read(path)).catch(() => null)
896
- if (text !== null && isPatchOf(text, hash)) return { path, text }
907
+ const tried = new Set()
908
+ for (const npm of npms) {
909
+ for (const path of npm.patchDirs.flatMap((dir) => names.map((name) => pathIn(dir, name)))) {
910
+ if (tried.has(path)) continue
911
+ tried.add(path)
912
+ // eslint-disable-next-line no-await-in-loop -- the first of them that is there
913
+ const text = await Promise.try(() => this.read(path)).catch(() => null)
914
+ if (text !== null && isPatchOf(text, hash)) return { path, text, file: posix.relative(npm.bundleDir, path) }
915
+ }
897
916
  }
898
917
  return null
899
918
  },
@@ -135,10 +135,28 @@ async function subtreeEntries(client, list, { github, directory, packageManager,
135
135
  return files === null || refersAbove(files, lockfileOf(packageManager)) ? null : files
136
136
  }
137
137
 
138
- async function repoTree(name, { github, sha, tag, directory, path, client, packageManager }, options) {
138
+ // GitHub serves a commit pushed to any fork of a repo through the repo's name too, its tarball and trees alike: one
139
+ // given by its sha is held to the repo's own history, as `stasis validate` holds it (historyOf). A tag's and the
140
+ // default branch's head are the repo's own already.
141
+ async function assertOwnCommit(name, client, github, sha) {
142
+ const { ValidateError, followingMoves, historyOf } = await import('../validate.js')
143
+ const repository = followingMoves(client, github)
144
+ let ref
145
+ try {
146
+ ref = await historyOf(repository, sha)
147
+ } catch (error) {
148
+ if (error instanceof ValidateError) throw new Error(`${name}: ${error.message}`, { cause: error })
149
+ throw error
150
+ }
151
+ if (ref === null) throw new Error(`${name}: ${sha} is in the history of no branch or tag of ${repository.name()} (a fork's, say, which GitHub serves through its name too): give a commit of its own`)
152
+ }
153
+
154
+ // `own`: a given sha is held to the repo's history (assertOwnCommit), before anything else is fetched.
155
+ async function repoTree(name, { github, sha, tag, directory, path, client, packageManager, own = false }, options) {
139
156
  checkAhead(name, packageManager, options)
140
157
  checkRepo(name, { github, sha, tag, directory, path })
141
158
  client ??= createClient({ token: null })
159
+ if (own && sha !== undefined) await debugTimed('Held the commit to the repo\'s history', () => assertOwnCommit(name, client, github, sha))
142
160
  sha ??= tag === undefined ? (await client.getRepoHead({ repo: github })).oid : (await client.getRepoTag({ repo: github, tag })).oid
143
161
  const where = `${name}: ${github}@${sha}`
144
162
  const list = memoized((dir) => client.listRepoDir({ repo: github, sha, directory: dir }))
@@ -181,7 +199,7 @@ async function repoTree(name, { github, sha, tag, directory, path, client, packa
181
199
  export async function buildGitHubBundle({ github, sha, tag, directory, path, client, packageManager, ...options } = {}) {
182
200
  checkTarget('buildGitHubBundle', options)
183
201
  checkCache('buildGitHubBundle', options.cache)
184
- const tree = await debugTimed('Fetched the repo', () => repoTree('buildGitHubBundle', { github, sha, tag, directory, path, client, packageManager }, options))
202
+ const tree = await debugTimed('Fetched the repo', () => repoTree('buildGitHubBundle', { github, sha, tag, directory, path, client, packageManager, own: true }, options))
185
203
  let { entries } = tree
186
204
  if (entries === undefined) {
187
205
  const suggested = defaultEntries('buildGitHubBundle', tree.host, tree.cwd, tree.packageManagers, options)
@@ -565,7 +565,8 @@ const githubRoot = (url) => {
565
565
  export function pinFromTree(bundle, root, tree) {
566
566
  if (tree.trees !== undefined) return new Map(tree.trees.flatMap((one) => [...pinFromTree(bundle, root, one)]))
567
567
  const { root: from, installed, packageManager } = tree
568
- const byDir = new Map(installed.map((pkg) => [posix.relative(root, posix.join(from, pkg.path)), pkg]))
568
+ const fromRoot = (path) => posix.relative(root, posix.join(from, path))
569
+ const byDir = new Map(installed.map((pkg) => [fromRoot(pkg.path), pkg]))
569
570
  const integrities = tree.integrities?.()
570
571
  const locked = ({ integrity, checksum }) => (PACKAGE_MANAGERS[packageManager].kind !== 'js' ? archiveIntegrity('soldeer', checksum) : (integrities?.has(tarballIntegrityOf(integrity)) ?? true) ? integrity : undefined)
571
572
  const accounts = new Map()
@@ -574,6 +575,8 @@ export function pinFromTree(bundle, root, tree) {
574
575
  if (pkg === undefined) continue
575
576
  const at = isValidRepoField('commit', pkg.commit) ? info.repo ?? githubRoot(pkg.git) : undefined
576
577
  const pinned = installedMetadata(dir, info.ecosystem, { repo: at && { ...at, commit: pkg.commit }, integrity: locked(pkg), patchHash: pkg.patch?.hash })
578
+ // deptree read it at the path pnpm's settings name, from the lockfile's directory, and held it to its hash.
579
+ if (pinned.patch) pinned.patch.file = fromRoot(pkg.patch.path)
577
580
  bundle.modules.set(dir, { ...info, ...pinned })
578
581
  accounts.set(dir, { integrity: pinned.integrity, patch: pinned.patch })
579
582
  }