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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +6 -2
  2. package/bin/stasis.js +222 -334
  3. package/package.json +38 -14
  4. package/src/audit-corrections.js +11 -42
  5. package/src/audit.js +256 -212
  6. package/src/babel.js +119 -82
  7. package/src/bundle-from-lockfile.js +9 -21
  8. package/src/bundle-reader.js +24 -8
  9. package/src/bundle.js +0 -3
  10. package/src/cmd/build.js +211 -117
  11. package/src/cmd/bundle-options.js +347 -0
  12. package/src/cmd/bundle.js +1664 -1001
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +48 -21
  15. package/src/cmd/output.js +25 -12
  16. package/src/cmd/sbom.js +6 -9
  17. package/src/cmd/tokens.js +60 -0
  18. package/src/define.js +218 -0
  19. package/src/diff.js +73 -129
  20. package/src/esbuild.js +0 -4
  21. package/src/flow.js +27 -0
  22. package/src/installed.js +265 -0
  23. package/src/json-stream.js +4 -9
  24. package/src/loader-mock.js +3 -8
  25. package/src/loader.js +1 -5
  26. package/src/loaders/bash.js +43 -120
  27. package/src/loaders/cargo.js +301 -808
  28. package/src/loaders/foundry.js +112 -290
  29. package/src/loaders/paths.js +15 -26
  30. package/src/loaders/php.js +211 -492
  31. package/src/loaders/rust.js +518 -1461
  32. package/src/loaders/solidity-ownership.js +51 -142
  33. package/src/loaders/solidity.js +42 -188
  34. package/src/loaders/toml.js +7 -17
  35. package/src/lockfile.js +0 -3
  36. package/src/metro-assets.js +41 -0
  37. package/src/metro-resolver.js +58 -133
  38. package/src/metro-transformer.js +0 -6
  39. package/src/metro.js +0 -4
  40. package/src/mock.js +32 -80
  41. package/src/parse.js +8 -7
  42. package/src/prune.js +0 -3
  43. package/src/resolve-fields.js +152 -249
  44. package/src/resolve-node.js +29 -45
  45. package/src/resolve-typescript.js +166 -298
  46. package/src/rollup.js +0 -4
  47. package/src/run-installed.js +10 -0
  48. package/src/sbom.js +70 -97
  49. package/src/scan.js +240 -282
  50. package/src/sixel.js +310 -0
  51. package/src/table.js +95 -0
  52. package/src/validate-notes.d.ts +19 -0
  53. package/src/validate-notes.js +72 -0
  54. package/src/validate.d.ts +159 -0
  55. package/src/validate.js +1315 -0
  56. package/src/vfs-bundle/entries.js +50 -46
  57. package/src/vfs-bundle/github.js +114 -94
  58. package/src/vfs-bundle/glob.js +379 -0
  59. package/src/vfs-bundle/lazy.js +99 -0
  60. package/src/vfs-bundle/prisma.js +235 -105
  61. package/src/vfs-bundle/soldeer.js +28 -49
  62. package/src/vfs-bundle/tree.js +418 -277
  63. package/src/vfs-bundle/yarnclean.js +79 -0
  64. package/src/vfs-bundle.js +24 -55
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +79 -231
  67. package/src/installed-commits.js +0 -182
@@ -1,182 +0,0 @@
1
- import { hash } from 'node:crypto'
2
- import { dirname, join, relative, resolve } from 'node:path'
3
-
4
- import { toBase32 } from '@exodus/bytes/base32.js'
5
- import { isValidRepoField } from '@exodus/stasis-core/bundle'
6
- import { parseGithubRepository, readJson, readText } from '@exodus/stasis-core/bundle-util'
7
- import { diskHost } from '@exodus/stasis-core/host'
8
- import { isPlainObject, relativeEscapes, toPosix } from '@exodus/stasis-core/util'
9
- import { parsePnpmLockfile } from '@preventive/lockfile/pnpm.js'
10
- import { parseYarn1Lockfile } from '@preventive/lockfile/yarn1.js'
11
-
12
- // The commit each git dependency in a node_modules on disk is installed at, as the package manager
13
- // that laid it out recorded it there: npm in its hidden lockfile, pnpm in its copy of the lockfile,
14
- // yarn 1 in yarn.lock, as its .yarn-integrity has it installed. Nothing on disk names a registry
15
- // package's. A record missing, unreadable, or naming no full commit of a GitHub repository records
16
- // nothing, and fails nothing.
17
-
18
- // `{ github, commit }` of `repo`, a git URL, at `commit`, where that is a GitHub repository and a full
19
- // commit id; else undefined.
20
- function githubCommit(repo, commit) {
21
- const github = parseGithubRepository(repo)
22
- return github !== null && isValidRepoField('commit', commit) ? { github, commit } : undefined
23
- }
24
-
25
- // GitHub's tarball of a commit, as yarn 1 and pnpm fetch a GitHub dependency.
26
- const CODELOAD = /^https:\/\/codeload\.github\.com\/([^/]+\/[^/]+)\/tar\.gz\/([^/]+)$/u
27
- const githubTarball = (url) => {
28
- const [, github, commit] = CODELOAD.exec(url) ?? []
29
- return isValidRepoField('github', github) ? githubCommit(github, commit) : undefined
30
- }
31
-
32
- // githubCommit of a git or tarball resolution @preventive/lockfile reads.
33
- const resolutionCommit = (resolution) => {
34
- if (resolution?.type === 'git') return githubCommit(resolution.repo, resolution.commit)
35
- return resolution?.type === 'tarball' ? githubTarball(resolution.tarball ?? '') : undefined
36
- }
37
-
38
- // The lookup of a record that finds nothing: one unreadable, or none.
39
- const NONE = () => undefined
40
-
41
- // npm's, from npm 7: the hidden lockfile it writes into `dir`'s node_modules as it lays it out, by
42
- // each package's location from `dir`, where it records the version installed there. A git
43
- // dependency's `resolved` is the URL npm clones, `#` and the commit.
44
- function npmRecord(dir, host) {
45
- const packages = readJson(join(dir, 'node_modules', '.package-lock.json'), host)?.packages
46
- if (!isPlainObject(packages)) return NONE
47
- return (location, version) => {
48
- const entry = Object.hasOwn(packages, location) ? packages[location] : undefined
49
- if (!isPlainObject(entry) || entry.version !== version || typeof entry.resolved !== 'string') return undefined
50
- const { resolved } = entry
51
- if (!/^git(?:\+[\w.-]+)?:/u.test(resolved)) return githubTarball(resolved)
52
- const at = resolved.lastIndexOf('#')
53
- return at === -1 ? undefined : githubCommit(resolved.slice(0, at), resolved.slice(at + 1))
54
- }
55
- }
56
-
57
- // yarn 1's: yarn.lock in `dir`, by the name a package is installed as, the last of its location, and
58
- // its version, where one entry alone has both. The .yarn-integrity yarn writes into the node_modules
59
- // it lays out has to hold the entry's resolution for each of its patterns, as yarn.lock's `resolved`
60
- // spells it: a yarn.lock changed since often moves a git dependency to another commit of one version.
61
- function yarnRecord(dir, host) {
62
- const installed = readJson(join(dir, 'node_modules', '.yarn-integrity'), host)?.lockfileEntries
63
- const text = readText(host, join(dir, 'yarn.lock'))
64
- if (!isPlainObject(installed) || text === null) return NONE
65
- let packages
66
- try {
67
- packages = parseYarn1Lockfile(text, { checkVersions: false }).packages
68
- } catch {
69
- return NONE // whatever the reader refuses
70
- }
71
- const byVersion = new Map()
72
- for (const { patterns, name, version, resolution } of new Set(Object.values(packages))) {
73
- let commit = resolutionCommit(resolution)
74
- const resolved = resolution?.type === 'git' ? `${resolution.repo}#${resolution.commit}` : resolution?.sha1 === undefined ? resolution?.tarball : `${resolution.tarball}#${resolution.sha1}`
75
- if (!patterns.every((pattern) => Object.hasOwn(installed, pattern) && installed[pattern] === resolved)) commit = undefined
76
- // Of two entries of one name and version, either may be the one installed.
77
- const id = `${name}@${version}`
78
- byVersion.set(id, byVersion.has(id) ? undefined : commit)
79
- }
80
- return (location, version) => byVersion.get(`${location.slice(location.lastIndexOf('node_modules/') + 'node_modules/'.length)}@${version}`)
81
- }
82
-
83
- // What npm and yarn leave in a node_modules they lay out, and the reader of each one's record.
84
- const LAYOUTS = [['.package-lock.json', npmRecord], ['.yarn-integrity', yarnRecord]]
85
-
86
- // The names pnpm 9 to 12 may give snapshot `key`'s directory in node_modules/.pnpm, as its
87
- // depPathToFilename (@pnpm/dependency-path) spells them: each character a path can't hold made `+`,
88
- // `#` too from pnpm 10, and a peer's parentheses `_`; where that is too long or has a capital, cut
89
- // short before `_` and a hash of it, 26 base32 of an MD5 for pnpm 9, 32 hex of a SHA-256 from pnpm 10,
90
- // of which the hash alone is kept, as where it is cut depends on the install's
91
- // virtualStoreDirMaxLength.
92
- const STORE_NAMES = [
93
- [/[\\/:*?"<>|]/gu, (name) => toBase32(hash('md5', name, 'buffer'), { padding: false }).toLowerCase()],
94
- [/[\\/:*?"<>|#]/gu, (name) => hash('sha256', name).slice(0, 32)],
95
- ]
96
- const storeNames = (key) => STORE_NAMES.flatMap(([unsafe, digest]) => {
97
- let name = key.replace(unsafe, '+')
98
- if (name.includes('(')) name = name.replace(/\)$/u, '').replace(/\)\(|\(|\)/gu, '_')
99
- return [name, `_${digest(name)}`]
100
- })
101
- const STORE_HASH = /_(?:[\da-f]{32}|[a-z2-7]{26})$/u
102
-
103
- // pnpm's: lock.yaml in `store`, a node_modules/.pnpm, the lockfile of what pnpm 9 to 12 installed
104
- // there, by the directory there each snapshot is in. A git dependency's resolution is the repository
105
- // and commit, or GitHub's tarball of the commit, and the package's `path` in it (`#path:`) where it is
106
- // not at the root.
107
- function pnpmRecord(store, host) {
108
- const text = readText(host, join(store, 'lock.yaml'))
109
- let packages
110
- try {
111
- packages = text === null ? undefined : parsePnpmLockfile(text).lockfile?.packages
112
- } catch {
113
- return NONE // whatever the reader refuses
114
- }
115
- const byName = new Map()
116
- for (const [key, { resolution }] of Object.entries(packages ?? {})) {
117
- const commit = resolutionCommit(resolution)
118
- if (commit === undefined) continue
119
- const directory = (resolution.path ?? '').replace(/^\/+|\/+$/gu, '')
120
- const repo = { github: commit.github, ...(isValidRepoField('directory', directory) && { directory }), commit: commit.commit }
121
- for (const name of storeNames(key)) byName.set(name, repo)
122
- }
123
- return (name) => byName.get(name) ?? byName.get(STORE_HASH.exec(name)?.[0])
124
- }
125
-
126
- // A pnpm package's real path, <store>/<dir>/node_modules/<name>, its store a node_modules/.pnpm.
127
- const PNPM_PACKAGE = /^(.*\/node_modules\/\.pnpm)\/([^/]+)\/node_modules\/(?:@[^/]+\/)?[^/]+$/u
128
-
129
- // Each npm dependency of `bundle`, its buckets' paths from `root`, that is a git dependency of a GitHub
130
- // repository, recorded as its package manager's record has it installed: that repository, over the
131
- // one its package.json names (a fork's, where that names the one forked), at its root (`''`) or the
132
- // subdirectory pnpm records, at that commit. A dependency is found by its real path, through the link
133
- // pnpm installs it by: in a node_modules/.pnpm, by its directory there; else by its location from the
134
- // nearest directory whose node_modules npm or yarn laid out, from the one its outermost node_modules
135
- // below the bundle root is in up to that root, whatever a directory above the root is named: a
136
- // record outside the root is another project's. Metadata, as `repo` is: never checked against the
137
- // files installed.
138
- export function pinInstalledCommits(bundle, root, host = diskHost) {
139
- const stores = new Map()
140
- const layouts = new Map()
141
- let top // the bundle root's real path, as each package's is compared with it
142
- // The record of `dir`'s node_modules, or null where neither npm nor yarn laid it out; where both
143
- // left theirs, neither's, as which was last is not known.
144
- const layoutAt = (dir) => {
145
- if (!layouts.has(dir)) {
146
- const found = LAYOUTS.filter(([file]) => host.stat(join(dir, 'node_modules', file)) !== null)
147
- layouts.set(dir, found.length === 0 ? null : found.length === 1 ? found[0][1](dir, host) : NONE)
148
- }
149
- return layouts.get(dir)
150
- }
151
- for (const [dir, info] of bundle.modules) {
152
- if (info.ecosystem !== 'npm') continue
153
- let real
154
- try {
155
- real = toPosix(host.realpath(resolve(root, dir)))
156
- } catch {
157
- continue
158
- }
159
- let repo
160
- const pnpm = PNPM_PACKAGE.exec(real)
161
- if (pnpm !== null) {
162
- const [, store, name] = pnpm
163
- if (!stores.has(store)) stores.set(store, pnpmRecord(store, host))
164
- repo = stores.get(store)(name)
165
- } else {
166
- try {
167
- top ??= resolve(host.realpath(resolve(root)))
168
- } catch {
169
- return
170
- }
171
- const rel = relative(top, real)
172
- const parts = toPosix(rel).split('/')
173
- const at = parts.indexOf('node_modules')
174
- if (relativeEscapes(rel) || at === -1) continue
175
- let from = resolve(top, ...parts.slice(0, at))
176
- while (layoutAt(from) === null && from !== top) from = dirname(from)
177
- const commit = (layoutAt(from) ?? NONE)(toPosix(relative(from, real)), info.version)
178
- if (commit !== undefined) repo = { github: commit.github, directory: '', commit: commit.commit }
179
- }
180
- if (repo !== undefined) bundle.modules.set(dir, { ...info, repo })
181
- }
182
- }