@exodus/stasis 1.0.0-beta.15 → 1.0.0-beta.17

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 +1 -1
  2. package/bin/stasis.js +26 -11
  3. package/package.json +10 -9
  4. package/src/audit-corrections.js +11 -42
  5. package/src/audit.js +113 -233
  6. package/src/babel.js +39 -122
  7. package/src/bundle-from-lockfile.js +9 -21
  8. package/src/bundle-reader.js +5 -9
  9. package/src/bundle.js +0 -3
  10. package/src/cmd/build.js +50 -166
  11. package/src/cmd/bundle-options.js +110 -167
  12. package/src/cmd/bundle.js +861 -1344
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +46 -27
  15. package/src/cmd/output.js +17 -12
  16. package/src/cmd/sbom.js +3 -8
  17. package/src/cmd/tokens.js +10 -23
  18. package/src/define.js +34 -97
  19. package/src/diff.js +72 -128
  20. package/src/esbuild.js +0 -4
  21. package/src/flow.js +10 -25
  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 +41 -93
  27. package/src/loaders/cargo.js +298 -805
  28. package/src/loaders/foundry.js +109 -285
  29. package/src/loaders/paths.js +13 -13
  30. package/src/loaders/php.js +210 -489
  31. package/src/loaders/rust.js +507 -1430
  32. package/src/loaders/solidity-ownership.js +48 -128
  33. package/src/loaders/solidity.js +39 -122
  34. package/src/loaders/toml.js +7 -17
  35. package/src/lockfile.js +0 -3
  36. package/src/metro-assets.js +9 -33
  37. package/src/metro-resolver.js +20 -47
  38. package/src/metro-transformer.js +0 -5
  39. package/src/metro.js +0 -4
  40. package/src/mock.js +32 -80
  41. package/src/parse.js +2 -5
  42. package/src/prune.js +0 -3
  43. package/src/resolve-fields.js +90 -220
  44. package/src/resolve-node.js +27 -45
  45. package/src/resolve-typescript.js +109 -313
  46. package/src/rollup.js +0 -4
  47. package/src/run-installed.js +10 -0
  48. package/src/sbom.js +52 -128
  49. package/src/scan.js +136 -283
  50. package/src/sixel.js +18 -41
  51. package/src/table.js +14 -35
  52. package/src/validate-notes.d.ts +3 -2
  53. package/src/validate-notes.js +25 -19
  54. package/src/validate.d.ts +38 -11
  55. package/src/validate.js +478 -512
  56. package/src/vfs-bundle/entries.js +40 -57
  57. package/src/vfs-bundle/github.js +88 -121
  58. package/src/vfs-bundle/glob.js +4 -45
  59. package/src/vfs-bundle/lazy.js +27 -56
  60. package/src/vfs-bundle/prisma.js +167 -110
  61. package/src/vfs-bundle/soldeer.js +28 -49
  62. package/src/vfs-bundle/tree.js +295 -318
  63. package/src/vfs-bundle/yarnclean.js +10 -31
  64. package/src/vfs-bundle.js +23 -64
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +75 -231
  67. package/src/installed-commits.js +0 -184
@@ -0,0 +1,265 @@
1
+ import { hash } from 'node:crypto'
2
+ import { basename, dirname, join, relative, resolve } from 'node:path'
3
+
4
+ import { isValidRepoField } from '@exodus/stasis-core/bundle'
5
+ import { parseGithubRepository, readJson, readText } from '@exodus/stasis-core/bundle-util'
6
+ import { diskHost } from '@exodus/stasis-core/host'
7
+ import { hexIntegrity, isNpmDependency, isPlainObject, relativeEscapes, tarballIntegrityOf, toPosix, upsert, withoutBom } from '@exodus/stasis-core/util'
8
+ import { parsePnpmLockfile } from '@preventive/lockfile/pnpm.js'
9
+ import { parseYarn1Lockfile } from '@preventive/lockfile/yarn1.js'
10
+
11
+ // What the package manager that laid out a node_modules recorded there; one missing or unreadable names nothing.
12
+
13
+ function githubCommit(repo, commit) {
14
+ const github = parseGithubRepository(repo)
15
+ return github !== null && isValidRepoField('commit', commit) ? { github, commit } : undefined
16
+ }
17
+
18
+ const CODELOAD = /^https:\/\/codeload\.github\.com\/([^/]+\/[^/]+)\/tar\.gz\/([^/]+)$/u
19
+ const githubTarball = (url) => {
20
+ const [, github, commit] = CODELOAD.exec(url) ?? []
21
+ return isValidRepoField('github', github) ? githubCommit(github, commit) : undefined
22
+ }
23
+
24
+ export const resolutionCommit = (resolution) => {
25
+ if (resolution?.type === 'git') return githubCommit(resolution.repo, resolution.commit)
26
+ return resolution?.type === 'tarball' ? githubTarball(resolution.tarball ?? '') : undefined
27
+ }
28
+
29
+ export const patchIntegrity = (patchHash) => (/^[\da-f]{64}$/u.test(patchHash ?? '') ? hexIntegrity('sha256', patchHash) : undefined)
30
+
31
+ const repoAt = (commit, path = '') => {
32
+ const directory = path.replace(/^\/+|\/+$/gu, '')
33
+ return commit && { github: commit.github, ...(isValidRepoField('directory', directory) && { directory }), commit: commit.commit }
34
+ }
35
+
36
+ const NONE = () => undefined
37
+
38
+ // A git dependency's `resolved` is the URL npm clones, `#` and the commit.
39
+ const npmCommit = (resolved) => {
40
+ if (typeof resolved !== 'string') return undefined
41
+ if (!/^git(?:\+[\w.-]+)?:/u.test(resolved)) return githubTarball(resolved)
42
+ const at = resolved.lastIndexOf('#')
43
+ return at === -1 ? undefined : githubCommit(resolved.slice(0, at), resolved.slice(at + 1))
44
+ }
45
+
46
+ const unreadable = (file, cause) => new Error(`${file}: ${cause?.message ?? 'not a lockfile'}`, { cause })
47
+
48
+ function parseLock(file, host, strict, parse) {
49
+ const text = readText(host, file)
50
+ if (text === null && strict && host.stat(file) !== null) throw new Error(`${file}: can't be read`)
51
+ if (text === null) return undefined
52
+ try {
53
+ return parse(withoutBom(text))
54
+ } catch (cause) {
55
+ if (strict) throw unreadable(file, cause)
56
+ return undefined
57
+ }
58
+ }
59
+
60
+ export const nameAt = (location) => location.slice(location.lastIndexOf('node_modules/') + 'node_modules/'.length)
61
+
62
+ // By name and version, or `name@` (a directory's): of two that differ, either may be the one installed, so its version
63
+ // alone, `ambiguous`. Of `location`'s package by its own `name` (an alias's), else the name it is installed as.
64
+ function byId(entries) {
65
+ const records = new Map()
66
+ for (const [id, record] of entries) {
67
+ const known = records.get(id)
68
+ records.set(id, known === undefined || JSON.stringify(known) === JSON.stringify(record) ? record : { version: record.version, ambiguous: true })
69
+ }
70
+ const find = (name, version) => records.get(`${name}@${version}`) ?? records.get(`${name}@`)
71
+ return (location, version, name) => (name === undefined ? undefined : find(name, version)) ?? find(nameAt(location), version)
72
+ }
73
+
74
+ function npmLocks(file, host, strict) {
75
+ const parsed = parseLock(file, host, strict, (text) => ({ packages: JSON.parse(text).packages }))
76
+ if (parsed === undefined) return undefined
77
+ const { packages } = parsed
78
+ if (!isPlainObject(packages)) {
79
+ if (strict) throw unreadable(file)
80
+ return undefined
81
+ }
82
+ return (location) => {
83
+ const entry = Object.hasOwn(packages, location) ? packages[location] : undefined
84
+ return isPlainObject(entry) ? { version: entry.version, repo: repoAt(npmCommit(entry.resolved)), integrity: entry.integrity } : undefined
85
+ }
86
+ }
87
+
88
+ const resolutionOf = (resolution) => (resolution?.type === 'git' ? `${resolution.repo}#${resolution.commit}` : resolution?.sha1 === undefined ? resolution?.tarball : `${resolution.tarball}#${resolution.sha1}`)
89
+
90
+ const yarnEntries = (file, host, strict) => parseLock(file, host, strict, (text) => {
91
+ const entries = new Set(Object.values(parseYarn1Lockfile(text, { checkVersions: false }).packages))
92
+ return [...entries].map(({ patterns, name, version, resolution }) => ({
93
+ id: `${name}@${version}`,
94
+ patterns,
95
+ record: { version, resolved: resolutionOf(resolution), repo: repoAt(resolutionCommit(resolution)), integrity: resolution?.integrity },
96
+ }))
97
+ })
98
+
99
+ // As pnpm 10 to 12's depPathToFilename names `key`'s store directory, and the `_<hash>` one cut short ends with.
100
+ const storeNames = (key) => {
101
+ let name = key.replace(/[\\/:*?"<>|#]/gu, '+')
102
+ if (name.includes('(')) name = name.replace(/\)$/u, '').replace(/\)\(|\(|\)/gu, '_')
103
+ return [name, `_${hash('sha256', name).slice(0, 32)}`]
104
+ }
105
+ export const STORE_HASH = /_[\da-f]{32}$/u
106
+
107
+ function pnpmLocks(file, host, strict) {
108
+ const parsed = parseLock(file, host, strict, (text) => ({ lockfile: parsePnpmLockfile(text).lockfile }))
109
+ if (parsed === undefined) return undefined
110
+ const { lockfile } = parsed
111
+ const stored = new Map()
112
+ const keyed = new Map()
113
+ const ids = []
114
+ 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 }
116
+ for (const store of storeNames(key)) stored.set(store, record)
117
+ keyed.set(key, record)
118
+ ids.push([`${name}@${version ?? ''}`, record])
119
+ }
120
+ // By the name it is installed as at a top-level location (an alias's or an override's too); none where two differ.
121
+ const top = new Map()
122
+ for (const importer of Object.values(lockfile?.importers ?? {})) {
123
+ for (const [as, key] of [importer.dependencies, importer.devDependencies, importer.optionalDependencies].flatMap((deps) => Object.entries(deps ?? {}))) {
124
+ const record = keyed.get(key)
125
+ if (record) top.set(as, top.has(as) && top.get(as) !== record ? undefined : record)
126
+ }
127
+ }
128
+ const named = byId(ids)
129
+ return {
130
+ stored: (name) => stored.get(name) ?? stored.get(STORE_HASH.exec(name)?.[0]),
131
+ named: (location, ...of) => (location === `node_modules/${nameAt(location)}` ? top.get(nameAt(location)) : undefined) ?? named(location, ...of),
132
+ }
133
+ }
134
+
135
+ const MANAGERS = [
136
+ {
137
+ marker: '.package-lock.json',
138
+ lockfiles: ['npm-shrinkwrap.json', 'package-lock.json'],
139
+ installed: (dir, host) => {
140
+ const locks = npmLocks(join(dir, 'node_modules', '.package-lock.json'), host)
141
+ return locks && ((location, version) => {
142
+ const record = locks(location)
143
+ return record?.version === version ? record : undefined
144
+ })
145
+ },
146
+ locks: (file, host) => npmLocks(file, host, true),
147
+ none: () => 'it locks none there',
148
+ },
149
+ {
150
+ marker: '.yarn-integrity',
151
+ lockfiles: ['yarn.lock'],
152
+ installed: (dir, host) => {
153
+ const integrity = readJson(join(dir, 'node_modules', '.yarn-integrity'), host)?.lockfileEntries
154
+ const entries = isPlainObject(integrity) && yarnEntries(join(dir, 'yarn.lock'), host)
155
+ if (!entries) return undefined
156
+ // What .yarn-integrity has of an entry installed otherwise: a changed yarn.lock often moves a git dependency's commit.
157
+ const installedAs = ({ patterns, record }) => patterns.map((pattern) => (Object.hasOwn(integrity, pattern) ? integrity[pattern] : 'none')).find((resolved) => resolved !== record.resolved)
158
+ return byId(entries.map((entry) => {
159
+ const resolved = installedAs(entry)
160
+ return [entry.id, resolved === undefined ? entry.record : { version: entry.record.version, resolved }]
161
+ }))
162
+ },
163
+ locks: (file, host) => byId(yarnEntries(file, host, true).map(({ id, record }) => [id, record])),
164
+ none: (version) => `${version}; it locks none`,
165
+ },
166
+ {
167
+ marker: '.modules.yaml',
168
+ lockfiles: ['pnpm-lock.yaml'],
169
+ installed: (dir, host) => pnpmLocks(join(dir, 'node_modules', '.pnpm', 'lock.yaml'), host)?.named,
170
+ locks: (file, host) => pnpmLocks(file, host, true).named,
171
+ none: (version) => `${version}; it locks none`,
172
+ },
173
+ ]
174
+
175
+ // Null where there is neither record nor lockfile. Of several lockfiles, the one whose package manager alone left its
176
+ // record, else refused; of several records, none is read, as which was last is not known.
177
+ function layoutOf(dir, host) {
178
+ const marked = MANAGERS.filter(({ marker }) => host.stat(join(dir, 'node_modules', marker)) !== null)
179
+ const locking = MANAGERS.flatMap((manager) => manager.lockfiles.map((name) => join(dir, name)).filter((file) => host.stat(file) !== null).slice(0, 1).map((file) => [manager, file]))
180
+ if (marked.length === 0 && locking.length === 0) return null
181
+ const held = locking.length > 1 ? locking.filter(([manager]) => marked.length === 1 && marked[0] === manager) : locking
182
+ if (locking.length > 1 && held.length === 0) throw new Error(`${dir}: ${locking.map(([, file]) => basename(file)).join(' and ')} are both there, and which installed its node_modules isn't known`)
183
+ const [manager = marked.length === 1 ? marked[0] : undefined, lockfile] = held[0] ?? []
184
+ const installed = (marked.length === 1 && marked[0] === manager && manager.installed(dir, host)) || NONE
185
+ return { installed, lockfile, locks: lockfile && manager.locks(lockfile, host), none: manager?.none }
186
+ }
187
+
188
+ // pnpm's, where it isolates: `store`'s lock.yaml held to the pnpm-lock.yaml of the directory the store is in.
189
+ function storeOf(store, host) {
190
+ const lockfile = join(dirname(dirname(store)), 'pnpm-lock.yaml')
191
+ return { installed: pnpmLocks(join(store, 'lock.yaml'), host)?.stored ?? NONE, lockfile, locks: pnpmLocks(lockfile, host, true)?.stored, none: () => 'it locks none there' }
192
+ }
193
+
194
+ const commitLabel = (repo) => (repo ? `${repo.github}${repo.directory ? `/${repo.directory}` : ''}@${repo.commit}` : 'none')
195
+
196
+ function lockDiffers(version, installed, locked, none) {
197
+ if (locked === undefined) return none(version)
198
+ if (locked.version !== undefined && locked.version !== version) return `${version}; it locks ${locked.version}`
199
+ if (installed === undefined) return undefined
200
+ if (installed.resolved !== undefined && installed.resolved !== locked.resolved) return `installed from ${installed.resolved}; it locks ${locked.resolved ?? 'none'}`
201
+ if (commitLabel(installed.repo) !== commitLabel(locked.repo)) return `at ${commitLabel(installed.repo)}; it locks ${commitLabel(locked.repo)}`
202
+ if (tarballIntegrityOf(installed.integrity) !== tarballIntegrityOf(locked.integrity)) return `installed with ${installed.integrity ?? 'no integrity'}; it locks ${locked.integrity ?? 'none'}`
203
+ if (installed.patchHash !== locked.patchHash) return `installed with the patch ${installed.patchHash ?? 'none'}; it locks ${locked.patchHash ?? 'none'}`
204
+ return undefined
205
+ }
206
+
207
+ const PNPM_PACKAGE = /^(.*\/node_modules\/\.pnpm)\/([^/]+)\/node_modules\/(?:@[^/]+\/)?[^/]+$/u
208
+
209
+ // An npm tarball's integrity is its sha512 alone; only an npm dependency has a patch.
210
+ export function installedMetadata(dir, ecosystem, { repo, integrity, patchHash } = {}) {
211
+ const npm = isNpmDependency(dir, ecosystem)
212
+ const tarball = npm ? tarballIntegrityOf(integrity) : integrity
213
+ const patch = npm ? patchIntegrity(patchHash) : undefined
214
+ return { ...(repo && { repo }), ...(tarball && { integrity: tarball }), ...(patch && { patch: { integrity: patch } }) }
215
+ }
216
+
217
+ export function installedRecords(modules, root, host = diskHost, { stores = new Map(), layouts = new Map() } = {}) {
218
+ const accounts = new Map()
219
+ let top // the bundle root's real path, as each package's is compared with it
220
+ const layoutAt = (dir) => upsert(layouts, dir, () => layoutOf(dir, host))
221
+ for (const [dir, info] of modules) {
222
+ // An untagged bucket under node_modules is npm's too (an artifact from before the tag).
223
+ if (!isNpmDependency(dir, info.ecosystem)) continue
224
+ let real
225
+ try {
226
+ real = toPosix(host.realpath(resolve(root, dir)))
227
+ } catch {
228
+ continue
229
+ }
230
+ let layout
231
+ let at
232
+ const pnpm = PNPM_PACKAGE.exec(real)
233
+ if (pnpm !== null) {
234
+ const [, store, name] = pnpm
235
+ ;[layout, at] = [upsert(stores, store, () => storeOf(store, host)), [name]]
236
+ } else {
237
+ try {
238
+ top ??= resolve(host.realpath(resolve(root)))
239
+ } catch {
240
+ return accounts
241
+ }
242
+ const rel = relative(top, real)
243
+ const parts = toPosix(rel).split('/')
244
+ const segment = parts.indexOf('node_modules')
245
+ if (relativeEscapes(rel) || segment === -1) continue
246
+ let from = resolve(top, ...parts.slice(0, segment))
247
+ while (layoutAt(from) === null && from !== top) from = dirname(from)
248
+ ;[layout, at] = [layoutAt(from) ?? { installed: NONE }, [toPosix(relative(from, real)), info.version, info.name]]
249
+ }
250
+ const { installed, lockfile, locks, none } = layout
251
+ const record = installed(...at)
252
+ const locked = locks?.(...at)
253
+ const differs = locks && lockDiffers(info.version, record, locked, none)
254
+ if (differs) throw new Error(`${lockfile} doesn't lock ${dir} as installed (${differs}): install from it again`)
255
+ const told = record ?? locked
256
+ if (told !== undefined && !told.ambiguous) accounts.set(dir, { ...installedMetadata(dir, info.ecosystem, told), ...(record === undefined && { locked: true }) })
257
+ }
258
+ return accounts
259
+ }
260
+
261
+ export function pinInstalled(bundle, root, host = diskHost) {
262
+ const accounts = installedRecords(bundle.modules, root, host)
263
+ for (const [dir, { locked, ...metadata }] of accounts) if (!locked) bundle.modules.set(dir, { ...bundle.modules.get(dir), ...metadata })
264
+ return accounts
265
+ }
@@ -1,6 +1,5 @@
1
- // Incremental JSON parser over UTF-8 bytes, holding only the value being built and one token in flight.
2
- // Exactly JSON.parse(Buffer.concat(chunks).toString('utf8')) for any chunking (json-stream.test.js checks it),
3
- // so a bundle read in a stream can't parse differently from one read whole. Pure: no Node builtins.
1
+ // Incremental JSON parser over UTF-8 bytes, holding only the value being built and one token in flight: exactly
2
+ // JSON.parse(Buffer.concat(chunks).toString('utf8')) for any chunking (json-stream.test.js checks it), no Node builtins.
4
3
  // Not mirrored: Node 24+'s JSON.parse can return a stale escaped key cached by an earlier call (a V8 bug).
5
4
 
6
5
  const QUOTE = 0x22
@@ -127,7 +126,8 @@ export class JsonStreamParser {
127
126
  const state = this.#state
128
127
  const isArray = Array.isArray(this.#stack.at(-1))
129
128
  if ((state === AFTER || state === ARRAY_FIRST || state === OBJECT_FIRST) && b === (isArray ? 0x5d : 0x7d)) {
130
- this.#close()
129
+ this.#path.pop()
130
+ this.#value(this.#stack.pop())
131
131
  return i + 1
132
132
  }
133
133
  if (state === AFTER) {
@@ -256,11 +256,6 @@ export class JsonStreamParser {
256
256
  }
257
257
  }
258
258
 
259
- #close() {
260
- this.#path.pop()
261
- this.#value(this.#stack.pop())
262
- }
263
-
264
259
  #value(value) {
265
260
  const depth = this.#stack.length
266
261
  if (depth === 0) {
@@ -1,12 +1,7 @@
1
- // stasis's `--mock` loader entry. The import order is load-bearing:
2
- // 1. the core hooks lib -- evaluating it snapshots stasis's own fs bindings off the *real*
3
- // builtins, so stasis keeps reading/writing through real fs;
4
- // 2. ./mock.js -- neutralizes the network/timer builtins and fs writers, then calls
5
- // syncBuiltinESMExports() so user code's later ESM imports resolve to the mocks.
6
- // Only THEN install() the hooks (so mock.js never goes through them / becomes the bundle entry).
1
+ // `--mock`'s loader, in load-bearing order: the hooks lib, snapshotting stasis's own fs bindings off the real builtins;
2
+ // ./mock.js, mocking them for user code; then install(), so mock.js neither goes through the hooks nor is the entry.
7
3
  import { install } from '@exodus/stasis-core/hooks'
8
- // `export *` (mock.js has no exports) rather than a bare side-effect import, to satisfy the
9
- // no-unassigned-import lint rule; ESM still evaluates it here in source order.
4
+ // `export *` (mock.js has no exports), not a bare import, for the no-unassigned-import lint rule.
10
5
  export * from './mock.js'
11
6
 
12
7
  install()
package/src/loader.js CHANGED
@@ -1,6 +1,2 @@
1
- // Public-export adapter: backs `@exodus/stasis/loader` (and `node --import
2
- // @exodus/stasis/loader`). The loader lives in @exodus/stasis-core; importing
3
- // this evaluates it (registering Node's module hooks) for its side effect.
4
- // Internal code uses `@exodus/stasis-core/loader` directly, so this file exists
5
- // only to back the package's `exports` map.
1
+ // `node --import @exodus/stasis/loader`: importing the core loader registers Node's module hooks.
6
2
  export * from '@exodus/stasis-core/loader'
@@ -1,8 +1,6 @@
1
- // Bash loader: `{ sources, resolutions, missing }` from a set of scripts. A
2
- // .sh/.bash reference landing INSIDE the bundle root must resolve to an in-set
3
- // script, else it lands in `missing`; everything else (PATH commands, `$VAR`
4
- // paths, absolute/escaping paths) is best-effort and dropped. A dynamically
5
- // sourced path can be pinned by a `# shellcheck source=...` directive.
1
+ // Bash loader: `{ sources, resolutions, missing }` from a set of scripts. A .sh/.bash reference landing
2
+ // inside the bundle root must resolve to an in-set script, else it is `missing`; everything else (PATH
3
+ // commands, `$VAR` paths, absolute or escaping paths) is best-effort and dropped.
6
4
 
7
5
  import { isUtf8 } from 'node:buffer'
8
6
  import { realpathSync } from 'node:fs'
@@ -13,83 +11,53 @@ import { assertRealPathWithinBase } from '@exodus/stasis-core/util'
13
11
  import { isFile } from '../resolve-typescript.js'
14
12
  import { applyToDir } from './paths.js'
15
13
 
16
- // The capture class excludes backtick/`(`/`)` so a ref inside a command
17
- // substitution (`` `source x.sh` ``, `$(bash x.sh)`) doesn't swallow the closing delimiter.
18
-
19
- // `source script.sh`, `. script.sh`, `. ./script.sh`
14
+ // The capture class excludes backtick, `(` and `)` so a ref inside a command substitution
15
+ // (`` `source x.sh` ``, `$(bash x.sh)`) doesn't swallow the closing delimiter.
20
16
  const SOURCE_RE = /(?:^|\n|;|&&|\|\||`|\$\()\s*(?:source|\.)\s+["']?([^\s"';&|#`()]+)["']?/gu
21
-
22
- // `bash`/`sh script.sh`, incl. `/usr/bin/env bash`, `/bin/sh`, `/usr/bin/bash`.
23
17
  const EXEC_RE = /(?:^|\n|;|&&|\|\||`|\$\()\s*(?:\/usr\/bin\/env\s+|\/bin\/|\/usr\/bin\/)?(?:ba)?sh\s+["']?([^\s"';&|#`()]+)["']?/gu
24
-
25
- // Direct invocation: `./script.sh`, `../lib/helper.bash`, `/path/to/script.sh`
26
18
  const DIRECT_RE = /(?:^|\n|;|&&|\|\||`|\$\()\s*["']?(\.\.?\/[^\s"';&|#`()]+\.(?:sh|bash))["']?/gu
27
-
28
- // Dependency hint comment: `# Depends on: example.sh`
29
19
  const COMMENT_RE = /^# Depends on: ([^ ]+\.(?:sh|bash))( |$)/gmu
30
-
31
- // shellcheck `source=` directive: `# shellcheck source=../lib/config.sh`, the
32
- // explicit way to point at a dynamically sourced (`$VAR`) file. Resolved relative
33
- // to the script. `source=/dev/null` is shellcheck's "don't follow" sentinel, ignored below.
20
+ // Pins a dynamically sourced (`$VAR`) file; `source=/dev/null` is shellcheck's "don't follow".
34
21
  const SHELLCHECK_SOURCE_RE = /#\s*shellcheck\s[^\n]*?\bsource=["']?([^\s"'#]+)["']?/gu
35
22
 
36
- // Extract candidate file references. `$VAR` expansions and `-flags` are dropped
37
- // (never bundle-able); resolveBashCall decides which of the rest name a real file.
23
+ // `$VAR` expansions and `-flags` are never bundle-able.
38
24
  export function extractBashCalls(content) {
39
25
  const calls = new Set()
40
26
  for (const re of [SOURCE_RE, EXEC_RE, DIRECT_RE, COMMENT_RE, SHELLCHECK_SOURCE_RE]) {
41
- for (const m of content.matchAll(re)) {
42
- const ref = m[1]
43
- if (ref.includes('$') || ref.startsWith('-') || ref === '/dev/null') continue
44
- calls.add(ref)
27
+ for (const [, ref] of content.matchAll(re)) {
28
+ if (!ref.includes('$') && !ref.startsWith('-') && ref !== '/dev/null') calls.add(ref)
45
29
  }
46
30
  }
47
31
  return [...calls]
48
32
  }
49
33
 
50
- // Resolve a bash reference to a baseDir-relative POSIX path. Existence is checked
51
- // against `knownFiles` (tree mode) or the filesystem under `baseDir` (walk mode);
52
- // with neither, only path computation happens. Handling differs by kind (below).
34
+ // A bash reference as a baseDir-relative POSIX path. Existence is checked against `knownFiles`
35
+ // (tree mode) or the filesystem under `baseDir` (walk mode); with neither, nothing exists.
53
36
  export function resolveBashCall(ref, fromFile, { knownFiles, baseDir } = {}) {
54
37
  const present = (p) => {
55
38
  if (!p) return false
56
39
  if (knownFiles) return knownFiles.has(p)
57
- if (baseDir) return isFile(join(baseDir, p))
58
- return false
40
+ return Boolean(baseDir) && isFile(join(baseDir, p))
59
41
  }
60
-
61
42
  // Null when it escapes baseDir or names baseDir itself.
62
43
  const resolveRel = (r) => applyToDir(fromFile, r.split('/')) || null
63
44
 
64
- // Absolute → project-relative, accepted only if it exists (so `/etc/...` is dropped).
45
+ // An absolute path is accepted only if it exists in the project (so `/etc/...` is dropped).
65
46
  if (ref.startsWith('/')) {
66
47
  const stripped = ref.replace(/^\/+/u, '')
67
48
  return present(stripped) ? stripped : null
68
49
  }
69
-
70
- // Path with a slash → computed against the caller's dir; existence deferred to the caller.
50
+ // Existence deferred to the caller.
71
51
  if (ref.includes('/')) return resolveRel(ref)
72
52
 
73
- // Bare name → only accept a real file.
74
53
  const sibling = resolveRel(ref)
75
54
  if (present(sibling)) return sibling
76
55
  if (sibling !== ref && present(ref)) return ref
77
- if (knownFiles) {
78
- for (const known of knownFiles) {
79
- const base = known.includes('/') ? known.slice(known.lastIndexOf('/') + 1) : known
80
- if (base === ref) return known
81
- }
82
- }
83
- return null
56
+ return knownFiles?.values().find((known) => known.slice(known.lastIndexOf('/') + 1) === ref) ?? null
84
57
  }
85
58
 
86
- // True for `.sh`/`.bash` refs; an extensionless `source ./env` isn't flagged missing when absent.
87
- const hasShellScriptExt = (ref) => ref.endsWith('.sh') || ref.endsWith('.bash')
88
-
89
- // Build the triple from already-loaded scripts. A ref is `missing` (fatal) only
90
- // when it resolves INSIDE the bundle root but wasn't collected (a dangling
91
- // `source ./lib.sh`); null-resolving refs are external and tolerated, and
92
- // self-refs/non-script exts are dropped silently.
59
+ // Null-resolving refs are external and tolerated; self-refs and an extensionless `source ./env`
60
+ // that isn't there are dropped silently.
93
61
  export function buildBashTree(sources) {
94
62
  const knownFiles = new Set(sources.keys())
95
63
  const resolutions = new Map()
@@ -97,12 +65,11 @@ export function buildBashTree(sources) {
97
65
  for (const [path, content] of sources) {
98
66
  const specMap = new Map()
99
67
  for (const ref of extractBashCalls(content)) {
100
- // Slash branch returns an unchecked in-bundle path, so a non-null target not
101
- // in knownFiles is a genuine dangling dep; a null target is external.
68
+ // A slash ref resolves unchecked, so a target not in knownFiles is a genuine dangling dep.
102
69
  const target = resolveBashCall(ref, path, { knownFiles })
103
70
  if (target && knownFiles.has(target)) {
104
- if (target !== path) specMap.set(ref, target) // drop self-references
105
- } else if (target && hasShellScriptExt(ref)) {
71
+ if (target !== path) specMap.set(ref, target)
72
+ } else if (target && (ref.endsWith('.sh') || ref.endsWith('.bash'))) {
106
73
  console.warn(`[loader.bash] Missing file: ${ref} from ${path}`)
107
74
  missing.push({ spec: ref, from: path })
108
75
  }
@@ -112,54 +79,35 @@ export function buildBashTree(sources) {
112
79
  return { sources, resolutions, missing }
113
80
  }
114
81
 
115
- // A script's text. One that isn't UTF-8 is refused rather than carried with U+FFFD in it.
116
- async function readScript(baseDir, relPath) {
117
- const bytes = await readFile(join(baseDir, relPath))
118
- if (!isUtf8(bytes)) throw new Error(`Shell script is not valid UTF-8: ${relPath}`)
119
- return bytes.toString('utf8')
120
- }
121
-
122
- // Walk from `entries` following resolved refs, reading each script once; a ref
123
- // missing on disk warns and is skipped without aborting the walk.
82
+ // A ref missing on disk warns and is skipped without aborting the walk.
124
83
  export async function collectBashFilesFromDisk(baseDir, entries) {
125
84
  const sources = new Map()
126
85
  const realBase = realpathSync(baseDir)
127
86
 
87
+ const read = async (relPath) => {
88
+ try {
89
+ assertRealPathWithinBase(realBase, baseDir, relPath)
90
+ const bytes = await readFile(join(baseDir, relPath))
91
+ if (!isUtf8(bytes)) throw new Error(`Shell script is not valid UTF-8: ${relPath}`)
92
+ return [relPath, bytes.toString('utf8')]
93
+ } catch (err) {
94
+ // A slash ref can resolve to a directory (`source ./lib`).
95
+ if (err.code !== 'ENOENT' && err.code !== 'EISDIR') throw err
96
+ console.warn(`[loader.bash] ${err.code === 'ENOENT' ? 'Missing file' : 'Skipping directory reference'}: ${relPath}`)
97
+ return null
98
+ }
99
+ }
100
+
128
101
  const processWave = async (wave) => {
129
- const toLoad = [...new Set(wave)].filter((p) => !sources.has(p))
130
- if (toLoad.length === 0) return
131
- const reads = await Promise.all(
132
- toLoad.map(async (relPath) => {
133
- try {
134
- assertRealPathWithinBase(realBase, baseDir, relPath)
135
- return [relPath, await readScript(baseDir, relPath)]
136
- } catch (err) {
137
- if (err.code === 'ENOENT') {
138
- console.warn(`[loader.bash] Missing file: ${relPath}`)
139
- return null
140
- }
141
- // A slash ref can resolve to a directory (`source ./lib`) → EISDIR; skip, don't abort.
142
- if (err.code === 'EISDIR') {
143
- console.warn(`[loader.bash] Skipping directory reference: ${relPath}`)
144
- return null
145
- }
146
- throw err
147
- }
148
- })
149
- )
150
- const next = []
151
- for (const entry of reads) {
152
- if (!entry) continue
153
- const [relPath, content] = entry
102
+ if (wave.size === 0) return
103
+ const next = new Set()
104
+ for (const [relPath, content] of (await Promise.all([...wave].map(read))).filter(Boolean)) {
154
105
  sources.set(relPath, content)
155
- for (const ref of extractBashCalls(content)) {
156
- const resolved = resolveBashCall(ref, relPath, { baseDir })
157
- if (resolved && !sources.has(resolved)) next.push(resolved)
158
- }
106
+ for (const ref of extractBashCalls(content)) next.add(resolveBashCall(ref, relPath, { baseDir }))
159
107
  }
160
- await processWave(next)
108
+ await processWave(new Set([...next].filter((p) => p && !sources.has(p))))
161
109
  }
162
110
 
163
- await processWave(entries)
111
+ await processWave(new Set(entries))
164
112
  return sources
165
113
  }