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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +5 -1
  2. package/bin/stasis.js +191 -149
  3. package/package.json +31 -7
  4. package/src/audit.js +224 -90
  5. package/src/babel.js +44 -37
  6. package/src/bundle-reader.js +24 -4
  7. package/src/cmd/build.js +218 -8
  8. package/src/cmd/bundle-options.js +31 -25
  9. package/src/cmd/bundle.js +1147 -311
  10. package/src/cmd/github-bundle.js +17 -9
  11. package/src/cmd/output.js +10 -3
  12. package/src/cmd/tokens.js +73 -0
  13. package/src/define.js +5 -0
  14. package/src/diff.js +2 -2
  15. package/src/flow.js +42 -0
  16. package/src/installed-commits.js +4 -2
  17. package/src/loaders/bash.js +2 -27
  18. package/src/loaders/cargo.js +6 -6
  19. package/src/loaders/foundry.js +4 -6
  20. package/src/loaders/paths.js +4 -15
  21. package/src/loaders/php.js +3 -5
  22. package/src/loaders/rust.js +19 -39
  23. package/src/loaders/solidity-ownership.js +4 -15
  24. package/src/loaders/solidity.js +4 -67
  25. package/src/metro-assets.js +65 -0
  26. package/src/metro-resolver.js +76 -123
  27. package/src/parse.js +7 -3
  28. package/src/resolve-fields.js +133 -366
  29. package/src/resolve-node.js +9 -7
  30. package/src/resolve-typescript.js +102 -30
  31. package/src/scan.js +154 -54
  32. package/src/sixel.js +333 -0
  33. package/src/table.js +116 -0
  34. package/src/validate-notes.d.ts +18 -0
  35. package/src/validate-notes.js +66 -0
  36. package/src/validate.d.ts +132 -0
  37. package/src/validate.js +1349 -0
  38. package/src/vfs-bundle/entries.js +5 -2
  39. package/src/vfs-bundle/github.js +68 -19
  40. package/src/vfs-bundle/glob.js +420 -0
  41. package/src/vfs-bundle/lazy.js +128 -0
  42. package/src/vfs-bundle/prisma.js +102 -29
  43. package/src/vfs-bundle/tree.js +238 -75
  44. package/src/vfs-bundle/yarnclean.js +100 -0
  45. package/src/vfs-bundle.js +16 -7
  46. package/src/why.js +9 -7
@@ -3,7 +3,9 @@ import { resolve } from 'node:path'
3
3
 
4
4
  import { createClient } from '@preventive/upstream/github.js'
5
5
  import { buildGitHubBundle } from '../vfs-bundle/github.js'
6
- import { bundledSummary, writeBundle, writeFile } from './output.js'
6
+ import { debugTimed } from './bundle.js'
7
+ import { githubToken, withNpmToken } from './tokens.js'
8
+ import { bundledSummary, unpackedSummary, writeBundle, writeFile } from './output.js'
7
9
 
8
10
  // The longest file name ext4, APFS and NTFS take: 255 bytes, or characters, ours being ASCII.
9
11
  const NAME_MAX = 255
@@ -27,13 +29,19 @@ export function githubBundleFile({ github, directory, commit }) {
27
29
 
28
30
  // Run `stasis github-bundle`: the bundle of a GitHub repo at a commit, or the one a tag names (the
29
31
  // default branch's head without either), as buildGitHubBundle builds it from `options`, written
30
- // brotli-compressed to `output` (githubBundleFile's name for it by default, `-` for stdout), and a
31
- // JS bundle's lockfile to `lockfile` where given.
32
- // The tree is fetched with GITHUB_TOKEN from `env` where it is set; nothing else of `env` is read.
33
- export async function githubBundleCommand({ cwd = process.cwd(), env = process.env, output, lockfile, brotliQuality, client, ...options } = {}) {
34
- const built = await buildGitHubBundle({ ...options, lockfile, client: client ?? createClient({ token: env.GITHUB_TOKEN || null }) })
35
- const dest = writeBundle(cwd, output ?? githubBundleFile({ github: options.github, directory: options.directory, commit: built.bundle.repo.commit }), built.bundle.serialize(), brotliQuality)
36
- if (lockfile !== undefined) writeFile(resolve(cwd, lockfile), built.lockfile.serialize())
37
- const from = `${options.github}@${built.bundle.repo.commit}${options.directory ? `/${options.directory}` : ''}`
32
+ // brotli-compressed to `output` (githubBundleFile's name for it by default, of the directory
33
+ // bundled, the one a `path` gives too; `-` for stdout), and a JS bundle's lockfile to `lockfile`
34
+ // where given; then it says how many packages it unpacked, and what it bundled.
35
+ // GitHub is asked with githubToken's token, `env`'s GITHUB_TOKEN where it is set, nothing else of
36
+ // `env` being read; npm, with withNpmToken's (tokens.js).
37
+ export async function githubBundleCommand({ cwd = process.cwd(), env = process.env, home, output, lockfile, brotliQuality, client, ...options } = {}) {
38
+ const built = await withNpmToken(home, () => buildGitHubBundle({ ...options, lockfile, client: client ?? createClient({ token: githubToken({ env, home }) }) }))
39
+ const { directory } = built
40
+ // Built for --lockfile alone, before anything is written, as `stasis bundle` builds it.
41
+ const lockData = lockfile === undefined ? undefined : built.lockfile.serialize()
42
+ const dest = await debugTimed('Wrote the bundle', () => writeBundle(cwd, output ?? githubBundleFile({ github: options.github, directory, commit: built.bundle.repo.commit }), built.bundle.serialize(), brotliQuality))
43
+ if (lockData !== undefined) writeFile(resolve(cwd, lockfile), lockData)
44
+ const from = `${options.github}@${built.bundle.repo.commit}${directory ? `/${directory}` : ''}`
45
+ console.warn(unpackedSummary(built.stats))
38
46
  console.warn(bundledSummary(built.bundle.sources.size, built.bundle.modules, from, dest))
39
47
  }
package/src/cmd/output.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import { mkdirSync, writeFileSync } from 'node:fs'
2
2
  import { dirname, resolve } from 'node:path'
3
- import { brotliCompressSync } from 'node:zlib'
4
3
 
5
- import { brotliOptions } from '@exodus/stasis-core/brotli'
4
+ import { compressBundleText } from '@exodus/stasis-core/brotli'
6
5
 
7
6
  // Default bundle output: stasis.code.br, the same name `stasis run --bundle=load` discovers, so the two round-trip with no flags.
8
7
  export const DEFAULT_BUNDLE_FILE = 'stasis.code.br'
@@ -24,7 +23,7 @@ export function writeOutput(cwd, target, data) {
24
23
  }
25
24
 
26
25
  // Write a bundle's `serialized` text brotli-compressed (at `brotliQuality`) as writeOutput does. -> where it went
27
- export const writeBundle = (cwd, target, serialized, brotliQuality) => writeOutput(cwd, target, brotliCompressSync(serialized, brotliOptions(brotliQuality)))
26
+ export const writeBundle = (cwd, target, serialized, brotliQuality) => writeOutput(cwd, target, compressBundleText(serialized, brotliQuality))
28
27
 
29
28
  // `N package(s)`: every non-empty bucket of a bundle's `modules` (the "." workspace bucket counts as one).
30
29
  export function packagesLabel(modules) {
@@ -32,5 +31,13 @@ export function packagesLabel(modules) {
32
31
  return `${packages} package${packages === 1 ? '' : 's'}`
33
32
  }
34
33
 
34
+ // The summary of what a tree buildVfsBundle built from unpacked, by its `stats`: how many of the
35
+ // packages installed (a Soldeer tree's dependencies), the files and their size, and the tarballs
36
+ // fetched, where deptree counts them.
37
+ export function unpackedSummary({ unpacked, installed, dependencies, files, bytes, tarballs }) {
38
+ const fetched = typeof tarballs === 'number' ? `, ${tarballs} tarball${tarballs === 1 ? '' : 's'} fetched` : ''
39
+ return `[stasis] Unpacked ${unpacked} of ${installed ?? dependencies} packages: ${files} files, ${(bytes / 2 ** 20).toFixed(1)} MiB${fetched}`
40
+ }
41
+
35
42
  // The summary of a bundle of `count` files in `modules` written from `from` to `dest`.
36
43
  export const bundledSummary = (count, modules, from, dest) => `[stasis] Bundled ${count} files in ${packagesLabel(modules)} from ${from} to ${dest}`
@@ -0,0 +1,73 @@
1
+ import { isUtf8 } from 'node:buffer'
2
+ import { readFileSync } from 'node:fs'
3
+ import { homedir } from 'node:os'
4
+ import { join } from 'node:path'
5
+
6
+ import { readJson } from '@exodus/stasis-core/bundle-util'
7
+
8
+ // Tokens for fetching, from the environment, and where it has none, as a fallback, from other tools'
9
+ // files in the user's home, read for compatibility.
10
+
11
+ // The variables @preventive/upstream's npm client takes its token from, the first set and not
12
+ // empty, read from process.env on each request.
13
+ export const NPM_TOKEN_VARS = ['PREVENTIVE_MEASURES_NPM_TOKEN', 'STASIS_NPM_TOKEN', 'NPM_TOKEN']
14
+
15
+ // A token as @preventive/upstream takes one, printable ASCII and no space, around which whitespace
16
+ // is dropped; anything else is none, so that it never reaches an error message.
17
+ const asToken = (value) => (typeof value === 'string' && /^[!-~]+$/u.test(value.trim()) ? value.trim() : undefined)
18
+
19
+ // The GitHub `token` and `npmToken` of `<home>/.audit/config.json` (the user's home by default),
20
+ // each where the file has it as a token. The file is another tool's, read for compatibility: one
21
+ // that is missing, unreadable, not JSON or of another shape gives neither, and nothing is said.
22
+ export function auditTokens(home) {
23
+ const config = readJson(join(home ?? homedir(), '.audit', 'config.json'))
24
+ return config === null ? {} : { token: asToken(config.token), npmToken: asToken(config.npmToken) }
25
+ }
26
+
27
+ // The GitHub token: `env`'s GITHUB_TOKEN where it is set, else, as a fallback, auditTokens(home)'s,
28
+ // only read then. -> it, or null where there is none
29
+ export function githubToken({ env = process.env, home } = {}) {
30
+ return env.GITHUB_TOKEN || auditTokens(home).token || null
31
+ }
32
+
33
+ // A line of an .npmrc giving npm's own registry an npm token, exactly as npm writes it: no other
34
+ // registry's, no variable for npm to expand, and nothing else on the line.
35
+ const NPMRC_LINE = /^\/\/registry\.npmjs\.org\/:_authToken=(npm_[\dA-Za-z]+)\r?$/u
36
+
37
+ // The npm token `<home>/.npmrc` (the user's home by default) gives npm's registry: that of the last
38
+ // line of it that is NPMRC_LINE, as npm takes the last of a setting given twice. Another registry's
39
+ // token, or one in any other form, is none. The file is npm's, read for compatibility: one that is
40
+ // missing or unreadable gives none, and nothing is said; one that is not valid UTF-8 is refused.
41
+ export function npmrcToken(home) {
42
+ const file = join(home ?? homedir(), '.npmrc')
43
+ let bytes
44
+ try {
45
+ bytes = readFileSync(file)
46
+ } catch {
47
+ return undefined
48
+ }
49
+ if (!isUtf8(bytes)) throw new Error(`${file} is not valid UTF-8`)
50
+ let token
51
+ for (const line of bytes.toString('utf8').split('\n')) token = NPMRC_LINE.exec(line)?.[1] ?? token
52
+ return token
53
+ }
54
+
55
+ // Run `fn`, with a token for the npm client where process.env gives it none (none of
56
+ // NPM_TOKEN_VARS set and not empty), as a fallback: auditTokens(home)'s `npmToken`, else
57
+ // npmrcToken(home)'s, each file only read then. That token is STASIS_NPM_TOKEN in process.env while
58
+ // `fn` runs, for the client to read, and process.env is as it was after, unless something else set
59
+ // STASIS_NPM_TOKEN meanwhile. -> what `fn` gives
60
+ export async function withNpmToken(home, fn) {
61
+ const token = NPM_TOKEN_VARS.some((name) => process.env[name]) ? undefined : (auditTokens(home).npmToken ?? npmrcToken(home))
62
+ if (token === undefined) return fn()
63
+ const before = process.env.STASIS_NPM_TOKEN
64
+ process.env.STASIS_NPM_TOKEN = token
65
+ try {
66
+ return await fn()
67
+ } finally {
68
+ if (process.env.STASIS_NPM_TOKEN === token) {
69
+ if (before === undefined) delete process.env.STASIS_NPM_TOKEN
70
+ else process.env.STASIS_NPM_TOKEN = before
71
+ }
72
+ }
73
+ }
package/src/define.js CHANGED
@@ -9,6 +9,11 @@ import { isPlainObject } from '@exodus/stasis-core/util'
9
9
  // no test Metro wouldn't, after React Native's Babel transforms, so a --metro bundle never lacks a
10
10
  // file Metro's graph holds.
11
11
 
12
+ // The values Metro's release build (`dev: false`, `react-native bundle --dev false`) inlines, and its
13
+ // prelude gives the globals: what `stasis bundle --metro` and `stasis build --metro` define by
14
+ // default, for each name no define given (a flag or the entry's `define`) gives a value of its own.
15
+ export const METRO_RELEASE_DEFINE = Object.freeze({ __DEV__: false, 'process.env.NODE_ENV': 'production' })
16
+
12
17
  // A defined name: an identifier, or a dotted path of them (`process.env.NODE_ENV`).
13
18
  const NAME = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/u
14
19
 
package/src/diff.js CHANGED
@@ -107,7 +107,7 @@ export function diffArtifacts(left, right, { imports = false, hash } = {}) {
107
107
 
108
108
  // Execute-bit changes, reported for every path either side marks (not just files in shared
109
109
  // modules): gaining or losing +x is a real change to the extracted tree, whatever bucket it sits in.
110
- const onlyIn = (a, b) => [...a].filter((file) => !b.has(file)).toSorted(sortPaths)
110
+ const onlyIn = (a, b) => [...a.difference(b)].toSorted(sortPaths)
111
111
  const executableAdded = onlyIn(R.executable, L.executable)
112
112
  const executableRemoved = onlyIn(L.executable, R.executable)
113
113
 
@@ -151,7 +151,7 @@ function resolutionsOf(imports) {
151
151
  const cmpStr = (a, b) => (a < b ? -1 : a > b ? 1 : 0)
152
152
  const cmpPair = (a, b) => sortPaths(a.parent, b.parent) || cmpStr(a.specifier, b.specifier)
153
153
  const sortedTargets = (set) => [...set].toSorted(sortPaths)
154
- const sameTargets = (a, b) => a.size === b.size && [...a].every((t) => b.has(t))
154
+ const sameTargets = (a, b) => a.size === b.size && a.isSubsetOf(b)
155
155
 
156
156
  // Diff resolution graphs at the (parent, specifier) level (conditions folded away — see
157
157
  // resolutionsOf). Reports added/removed and changed (resolved on both sides to different targets —
package/src/flow.js ADDED
@@ -0,0 +1,42 @@
1
+ // --flow, for `stasis bundle` (the scan) and `stasis build`: Flow type syntax stripped from a JS-family
2
+ // file, as Metro's Babel strips it (@react-native/babel-preset): one marked `@flow` (hasFlowPragma)
3
+ // whatever else it holds, any other where the parser can't read it as it is. By flow-remove-types --
4
+ // stasis's own dependency, resolved from stasis itself (never from the project) and loaded lazily,
5
+ // only under --flow, which --metro turns on.
6
+ import { createRequire } from 'node:module'
7
+
8
+ // Flow type syntax lives in the JS (non-TS) family: flow-remove-types over a .ts/.tsx file would
9
+ // corrupt TS-only constructs. .jsx is included (JS + JSX, which can carry Flow types like .js);
10
+ // flow-remove-types preserves JSX.
11
+ export const FLOW_EXTS = new Set(['.js', '.cjs', '.mjs', '.jsx'])
12
+
13
+ // Whether `source` holds `@flow`, the pragma marking a Flow file, anywhere in it, as React Native's
14
+ // Babel preset finds it (/@flow/), to parse the file with hermes-parser, as Flow throughout: --flow
15
+ // strips such a file whether or not it parses as it is.
16
+ export const hasFlowPragma = (source) => source.includes('@flow')
17
+
18
+ let flowRemoveTypes
19
+ // A comment marking a file `@noflow`, in which flow-remove-types reads syntax both Flow and JS can
20
+ // read (`f<T>(x)`, `a < b > (c)`) as JS (hermes-parser's `flow: 'detect'`), and none other; and
21
+ // what it leaves of it, the pragma blanked.
22
+ const NOFLOW = '\n/* @noflow */'
23
+ const NOFLOW_STRIPPED = `\n/* ${' '.repeat('@noflow'.length)} */`
24
+
25
+ // `source` with its Flow type syntax blanked to whitespace in place, so byte offsets and every import
26
+ // specifier are kept, read as Metro's Babel reads it: a file marked `@flow` (hasFlowPragma) as Flow
27
+ // throughout (`all: true`), as hermes-parser parses it there, so `f<T>(x)` is a call with a type
28
+ // argument; any other as Babel's own Flow parser reads it (`all: false`), its annotations Flow's and
29
+ // syntax JS can read too JS's (`a < b > (c)`, comparisons). flow-remove-types reads it so only in a
30
+ // file marked `@noflow`, so one not marked yet is, past its end, and cut off again.
31
+ // Types only: Flow syntax that is code -- `component` and `hook` declarations, `enum`s -- stays. An
32
+ // import listing types alone (`import { type A } from`) stays as `import {} from`, or with
33
+ // `removeEmptyImports` goes whole, as Babel's Flow transform drops it. Throws where flow-remove-types
34
+ // can't parse `source`.
35
+ export function stripFlowTypes(source, { removeEmptyImports = false } = {}) {
36
+ flowRemoveTypes ??= createRequire(import.meta.url)('flow-remove-types')
37
+ if (hasFlowPragma(source)) return flowRemoveTypes(source, { all: true, removeEmptyImports }).toString()
38
+ if (source.includes('@noflow')) return flowRemoveTypes(source, { removeEmptyImports }).toString()
39
+ const stripped = flowRemoveTypes(source + NOFLOW, { removeEmptyImports }).toString()
40
+ if (!stripped.endsWith(NOFLOW_STRIPPED)) throw new Error('flow-remove-types left the @noflow comment stasis appended')
41
+ return stripped.slice(0, -NOFLOW_STRIPPED.length)
42
+ }
@@ -30,7 +30,7 @@ const githubTarball = (url) => {
30
30
  }
31
31
 
32
32
  // githubCommit of a git or tarball resolution @preventive/lockfile reads.
33
- const resolutionCommit = (resolution) => {
33
+ export const resolutionCommit = (resolution) => {
34
34
  if (resolution?.type === 'git') return githubCommit(resolution.repo, resolution.commit)
35
35
  return resolution?.type === 'tarball' ? githubTarball(resolution.tarball ?? '') : undefined
36
36
  }
@@ -98,7 +98,9 @@ const storeNames = (key) => STORE_NAMES.flatMap(([unsafe, digest]) => {
98
98
  if (name.includes('(')) name = name.replace(/\)$/u, '').replace(/\)\(|\(|\)/gu, '_')
99
99
  return [name, `_${digest(name)}`]
100
100
  })
101
- const STORE_HASH = /_(?:[\da-f]{32}|[a-z2-7]{26})$/u
101
+ // The hash pnpm ends a store directory's name with where it cuts a long one short: pnpm 10's hex,
102
+ // or the base32 of those before.
103
+ export const STORE_HASH = /_(?:[\da-f]{32}|[a-z2-7]{26})$/u
102
104
 
103
105
  // pnpm's: lock.yaml in `store`, a node_modules/.pnpm, the lockfile of what pnpm 9 to 12 installed
104
106
  // there, by the directory there each snapshot is in. A git dependency's resolution is the repository
@@ -7,11 +7,11 @@
7
7
  import { isUtf8 } from 'node:buffer'
8
8
  import { realpathSync } from 'node:fs'
9
9
  import { readFile } from 'node:fs/promises'
10
- import { dirname, extname, join, resolve } from 'node:path'
10
+ import { join } from 'node:path'
11
11
 
12
12
  import { assertRealPathWithinBase } from '@exodus/stasis-core/util'
13
13
  import { isFile } from '../resolve-typescript.js'
14
- import { applyToDir, assertWithinBase } from './paths.js'
14
+ import { applyToDir } from './paths.js'
15
15
 
16
16
  // The capture class excludes backtick/`(`/`)` so a ref inside a command
17
17
  // substitution (`` `source x.sh` ``, `$(bash x.sh)`) doesn't swallow the closing delimiter.
@@ -163,28 +163,3 @@ export async function collectBashFilesFromDisk(baseDir, entries) {
163
163
  await processWave(entries)
164
164
  return sources
165
165
  }
166
-
167
- // High-level entry: reads the scripts named in a `.sh.txt` listing (relative to
168
- // it). Unlike loadRust, the listing is authoritative — bash sourcing is too
169
- // dynamic to walk — so exactly the listed files are read, resolutions computed among them.
170
- export async function loadBash(shTxtFile) {
171
- const baseDir = dirname(resolve(shTxtFile))
172
- const listing = await readFile(shTxtFile, 'utf8')
173
- const lines = listing.split('\n').map((l) => l.trim()).filter(Boolean)
174
- .map((l) => l.replace(/^\.\//u, ''))
175
- if (lines.length === 0) throw new Error(`Empty bash listing: ${shTxtFile}`)
176
-
177
- if (!lines.every((line) => ['', '.sh', '.bash'].includes(extname(line)))) {
178
- throw new Error(`Bash listing must only contain .sh/.bash files: ${shTxtFile}`)
179
- }
180
- for (const line of lines) assertWithinBase(baseDir, line, 'Entry path')
181
-
182
- const realBase = realpathSync(baseDir)
183
- const reads = await Promise.all(
184
- lines.map(async (relPath) => {
185
- assertRealPathWithinBase(realBase, baseDir, relPath)
186
- return [relPath, await readScript(baseDir, relPath)]
187
- }),
188
- )
189
- return buildBashTree(new Map(reads))
190
- }
@@ -14,7 +14,7 @@ import { dirname, isAbsolute, join, parse, posix, relative, resolve, sep } from
14
14
  import { readText } from '@exodus/stasis-core/bundle-util'
15
15
  import { diskHost } from '@exodus/stasis-core/host'
16
16
  import { canonicalizePath } from '@exodus/stasis-core/state-util'
17
- import { relativeEscapes, toPosix } from '@exodus/stasis-core/util'
17
+ import { posixPathEscapes, relativeEscapes, toPosix } from '@exodus/stasis-core/util'
18
18
 
19
19
  import { LockfileError, linkCargo, parseCargoConfig, parseCargoLock, parseCargoManifest as readCargoManifest, readCargoVendor, resolveCargoFeatures } from '@preventive/lockfile/cargo.js'
20
20
  import { matches, parseVersion, parseVersionReq } from '@preventive/lockfile/rust-semver.js'
@@ -66,7 +66,7 @@ const table = (v) => (isTomlTable(v) ? v : null)
66
66
  export function normalizeRel(dir, sub) {
67
67
  if (isAbsolute(sub) || posix.isAbsolute(sub)) return null
68
68
  const rel = posix.normalize(posix.join(dir === '.' ? '' : dir, sub))
69
- if (rel === '..' || rel.startsWith('../') || posix.isAbsolute(rel)) return null
69
+ if (posixPathEscapes(rel)) return null
70
70
  return rel
71
71
  }
72
72
 
@@ -165,19 +165,19 @@ const satisfies = (version, req) => {
165
165
  // The Cargo.lock governing `baseDir`: beside the workspace root's Cargo.toml -- the nearest manifest
166
166
  // at or above `baseDir` with a `[workspace]` table, as cargo finds it -- else beside `baseDir`'s
167
167
  // own. A lock further up belongs to some other project: passing `--locked` for it would make
168
- // cargo refuse to create the one it needs.
169
- export function findCargoLock(baseDir) {
168
+ // cargo refuse to create the one it needs. Read of `host`, the disk's unless given.
169
+ export function findCargoLock(baseDir, host = diskHost) {
170
170
  let root = baseDir
171
171
  for (const dir of ancestors(baseDir)) {
172
172
  const file = join(dir, 'Cargo.toml')
173
- const text = readText(diskHost, file)
173
+ const text = readText(host, file)
174
174
  if (text !== null && 'workspace' in readToml(text, file)) {
175
175
  root = dir
176
176
  break
177
177
  }
178
178
  }
179
179
  const candidate = join(root, 'Cargo.lock')
180
- return existsSync(candidate) ? candidate : null
180
+ return isFile(candidate, host) ? candidate : null
181
181
  }
182
182
 
183
183
  // --- cfg predicates ---------------------------------------------------------------------
@@ -16,7 +16,7 @@
16
16
  import { posix, resolve } from 'node:path'
17
17
 
18
18
  import { diskHost } from '@exodus/stasis-core/host'
19
- import { toPosix } from '@exodus/stasis-core/util'
19
+ import { nestedMap, toPosix } from '@exodus/stasis-core/util'
20
20
  import { isDir } from '../resolve-typescript.js'
21
21
  import { projectOwnership, projectRelative, readUtf8OrNull, realpathOrNull } from './solidity-ownership.js'
22
22
  import { isTomlTable, readToml } from './toml.js'
@@ -409,8 +409,7 @@ export function findRemappingsWithContext(dir, host = diskHost) {
409
409
  const key = `${name}/`
410
410
  const owner = dependencyOwner(dir, c.windowStart)
411
411
  if (owner !== null) {
412
- if (!contextual.has(owner)) contextual.set(owner, new Map())
413
- insertPrioritized(contextual.get(owner), key, c.sourceDir)
412
+ insertPrioritized(nestedMap(contextual, owner), key, c.sourceDir)
414
413
  }
415
414
  insertPrioritized(global, key, c.sourceDir)
416
415
  }
@@ -445,7 +444,7 @@ function parseFoundryToml(text, file = null) {
445
444
  const topLevel = new Map()
446
445
  const read = (map, name, table) => {
447
446
  const profile = name.toLowerCase()
448
- const dict = map.get(profile) ?? map.set(profile, new Map()).get(profile)
447
+ const dict = nestedMap(map, profile)
449
448
  for (const [key, value] of Object.entries(table)) {
450
449
  const k = snakeCase(key)
451
450
  if (k !== 'extends' || map === current) dict.set(k, value) // forge reads `extends` from `[profile.<name>]` only
@@ -467,8 +466,7 @@ function parseFoundryToml(text, file = null) {
467
466
  function mergeExtended(base, local, strategy) {
468
467
  const out = new Map([...base].map(([p, dict]) => [p, new Map(dict)]))
469
468
  for (const [p, dict] of local) {
470
- if (!out.has(p)) out.set(p, new Map())
471
- const merged = out.get(p)
469
+ const merged = nestedMap(out, p)
472
470
  for (const [k, v] of dict) {
473
471
  const prev = merged.get(k)
474
472
  merged.set(k, strategy === 'extend-arrays' && Array.isArray(prev) && Array.isArray(v) ? [...prev, ...v] : v)
@@ -1,18 +1,4 @@
1
- // Path checks shared by the source-language loaders.
2
-
3
- import { isAbsolute, relative, resolve } from 'node:path'
4
-
5
- import { relativeEscapes, toPosix } from '@exodus/stasis-core/util'
6
-
7
- // Reject absolute and `..`-escaping paths in a `.<lang>.txt` listing so a malicious or sloppy
8
- // listing can't read files outside the listing's own directory.
9
- export function assertWithinBase(baseDir, candidate, label) {
10
- if (isAbsolute(candidate)) throw new Error(`${label} must not be absolute: ${candidate}`)
11
- const rel = toPosix(relative(baseDir, resolve(baseDir, candidate)))
12
- if (relativeEscapes(rel)) {
13
- throw new Error(`${label} escapes baseDir: ${candidate}`)
14
- }
15
- }
1
+ // Path arithmetic shared by the source-language loaders.
16
2
 
17
3
  // Apply `segments` on `fromFile`'s directory, resolving '.'/'..'. Returns a baseDir-relative POSIX
18
4
  // path ('' for baseDir itself), or null when traversal escapes the root.
@@ -31,3 +17,6 @@ export function applyToDir(fromFile, segments) {
31
17
  }
32
18
  return resolved.join('/')
33
19
  }
20
+
21
+ // A `./`/`../` specifier, first segment `.` or `..` (a `.`-led NAME like `..cache/a.php` is a bare one).
22
+ export const isDotRelative = (spec) => spec === '.' || spec === '..' || spec.startsWith('./') || spec.startsWith('../')
@@ -11,10 +11,10 @@ import { isDeepStrictEqual } from 'node:util'
11
11
 
12
12
  import { isValidRepoField } from '@exodus/stasis-core/bundle'
13
13
  import { checkoutCommit, parseGithubRepository } from '@exodus/stasis-core/bundle-util'
14
- import { assertRealPathWithinBase, isPlainObject, relativeEscapes, toPosix } from '@exodus/stasis-core/util'
14
+ import { assertRealPathWithinBase, fileInBucket, isPlainObject, relativeEscapes, toPosix } from '@exodus/stasis-core/util'
15
15
  import { LockfileError, parseComposerLock } from '@preventive/lockfile/composer.js'
16
16
  import { isDir, isFile } from '../resolve-typescript.js'
17
- import { applyToDir } from './paths.js'
17
+ import { applyToDir, isDotRelative } from './paths.js'
18
18
  import { readUtf8OrNull } from './solidity-ownership.js'
19
19
  import { nameErrors } from './toml.js'
20
20
 
@@ -178,8 +178,6 @@ function matchDirAnchor(code, pos) {
178
178
  return { end: PHP_DIR_ANCHOR_RE.lastIndex, up }
179
179
  }
180
180
 
181
- // A `./`/`../` specifier, as PHP tells one (a `.`-led NAME like `..cache/a.php` is a bare one).
182
- const isDotRelative = (spec) => spec === '.' || spec === '..' || spec.startsWith('./') || spec.startsWith('../')
183
181
 
184
182
  // Normalise an include path to a specifier. Dir-relative paths get a `./` prefix
185
183
  // with the leading separator stripped; empty means the file's own dir (`.`).
@@ -744,7 +742,7 @@ export function bucketizePhpSources(baseDir, sources, fallbackName, fallbackVers
744
742
  for (const [path, content] of sources) {
745
743
  const pkg = findComposerPackage(baseDir, path, installed, fallbackVersion)
746
744
  if (pkg) {
747
- const rel = pkg.pkgDir === '.' ? path : path.slice(pkg.pkgDir.length + 1)
745
+ const rel = fileInBucket(pkg.pkgDir, path)
748
746
  // Below the root package = an installed Composer dependency, tagged
749
747
  // `composer`; the root "." bucket is workspace code and stays ecosystem-less.
750
748
  const ecosystem = pkg.pkgDir === '.' ? undefined : 'composer'
@@ -11,12 +11,11 @@
11
11
  import { isUtf8 } from 'node:buffer'
12
12
  import { realpathSync } from 'node:fs'
13
13
  import { readFile } from 'node:fs/promises'
14
- import { dirname, extname, join, posix, relative, resolve } from 'node:path'
14
+ import { join, posix, relative } from 'node:path'
15
15
 
16
- import { assertRealPathWithinBase, relativeEscapes, toPosix } from '@exodus/stasis-core/util'
16
+ import { assertRealPathWithinBase, nestedMap, relativeEscapes, toPosix } from '@exodus/stasis-core/util'
17
17
  import { isFile } from '../resolve-typescript.js'
18
18
  import { TARGET_CFG_KEYS, TARGET_UNIT, VENDOR_DIR, cached, cfgName, createCargoContext, evalCfg, evalCfgKey, isTestTargetPath, normName, normalizeCfg, normalizeRel } from './cargo.js'
19
- import { assertWithinBase } from './paths.js'
20
19
  import { matchClose, splitTopLevel } from './toml.js'
21
20
 
22
21
  // Leads of the expression-position paths anchored on the module tree rather than on a name.
@@ -1789,7 +1788,7 @@ function withAlternatives(found, asker) {
1789
1788
  // answer. A list built while the closure was still being resolved (an import cycle, see
1790
1789
  // memoized) is not kept.
1791
1790
  function providedAll(root, at, name, seeing, ctx) {
1792
- const memo = ctx.provided.get(root) ?? ctx.provided.set(root, new Map()).get(root)
1791
+ const memo = nestedMap(ctx.provided, root)
1793
1792
  const key = `${at}\0${name}\0${seeing}`
1794
1793
  let list = memo.get(key)
1795
1794
  if (list === undefined) {
@@ -1949,7 +1948,7 @@ function providedFrom(root, at, name, ctx, file, asker, ns = null) {
1949
1948
  // The files holding the module's imports, gathered once (they are all in place by now).
1950
1949
  const files = of === undefined ? null : (of.files ??= new Set([...of.globs, ...[...of.named.values()].flat()].map((im) => im.file)))
1951
1950
  if (files === null || files.size === (files.has(file) ? 1 : 0)) return provided(root, at, name, at, ctx, asker, ns)
1952
- const memo = ctx.provided.get(root) ?? ctx.provided.set(root, new Map()).get(root)
1951
+ const memo = nestedMap(ctx.provided, root)
1953
1952
  const seeing = at.split('::').length
1954
1953
  const key = `${at}\0${name}\0@${file}`
1955
1954
  let list = memo.get(key)
@@ -2009,7 +2008,7 @@ function importsOf(root, at, name, seeing, ctx, onlyFile) {
2009
2008
  // on from it; a closure asked for while it is being computed (a glob into a module whose own
2010
2009
  // imports name the glob's source) is a cycle, and reads as empty.
2011
2010
  function globClosure(root, at, seeing, ctx) {
2012
- const memo = ctx.closures.get(root) ?? ctx.closures.set(root, new Map()).get(root)
2011
+ const memo = nestedMap(ctx.closures, root)
2013
2012
  const key = `${at}\0${seeing}`
2014
2013
  return memoized(memo, key, ctx, () => {
2015
2014
  const modules = []
@@ -2291,7 +2290,7 @@ function opaqueGlobsOf(root, ctx) {
2291
2290
  }
2292
2291
 
2293
2292
  function globMayProvide(root, at, lead, ctx) {
2294
- const memo = ctx.opaque.get(root) ?? ctx.opaque.set(root, new Map()).get(root)
2293
+ const memo = nestedMap(ctx.opaque, root)
2295
2294
  const key = `${at}\0${lead}`
2296
2295
  if (memo.has(key)) return memo.get(key)
2297
2296
  memo.set(key, false)
@@ -2379,7 +2378,7 @@ function resolvePathRef(ref, file, bindings, ctx) {
2379
2378
 
2380
2379
  // `map`: root → name → Set<module>, noting `module` under (root, name).
2381
2380
  const note = (map, root, name, module) => {
2382
- const byName = map.get(root) ?? map.set(root, new Map()).get(root)
2381
+ const byName = nestedMap(map, root)
2383
2382
  ;(byName.get(name) ?? byName.set(name, new Set()).get(name)).add(module)
2384
2383
  }
2385
2384
 
@@ -2474,7 +2473,7 @@ function buildOf(rel, ctx, units = null) {
2474
2473
  const test = isTestTarget(rel, ctx)
2475
2474
  const set = unitsOf(units, rel)
2476
2475
  const key = `${ctx?.packageInfo(rel)?.dir ?? ''}\0${test}\0${[...set].toSorted().join()}`
2477
- const memo = buildMemo.get(ctx ?? NO_CTX) ?? buildMemo.set(ctx ?? NO_CTX, new Map()).get(ctx ?? NO_CTX)
2476
+ const memo = nestedMap(buildMemo, ctx ?? NO_CTX)
2478
2477
  return cached(memo, key, () => {
2479
2478
  const one = (unit) => ({ features: ctx?.featuresFor(rel, unit) ?? null, maybeFeatures: ctx?.maybeFeaturesFor(rel, unit) ?? null, target: ctx?.platformOf(unit)?.cfgs ?? null })
2480
2479
  const each = [...set].map(one)
@@ -2633,8 +2632,8 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2633
2632
  }
2634
2633
  continue
2635
2634
  }
2636
- const keyed = byCfg.get(spec) ?? byCfg.set(spec, new Map()).get(spec)
2637
- const byFile = cfgsHere.get(spec) ?? cfgsHere.set(spec, new Map()).get(spec)
2635
+ const keyed = nestedMap(byCfg, spec)
2636
+ const byFile = nestedMap(cfgsHere, spec)
2638
2637
  for (const t of targets) {
2639
2638
  const cfg = t.cfg ?? decl.cfg ?? null
2640
2639
  const base = cfgKey(cfg ?? (decl.macro === null ? null : `${decl.macro}!`))
@@ -2726,7 +2725,7 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2726
2725
  // build may compile (walkPath asks whether one of them may be there at all).
2727
2726
  const moduleFiles = new Map()
2728
2727
  for (const [path, f] of files) {
2729
- const byModule = moduleFiles.get(f.root) ?? moduleFiles.set(f.root, new Map()).get(f.root)
2728
+ const byModule = nestedMap(moduleFiles, f.root)
2730
2729
  ;(byModule.get(f.modulePath) ?? byModule.set(f.modulePath, []).get(f.modulePath)).push(path)
2731
2730
  }
2732
2731
  // Per crate root, what a path may continue along where a segment names no module: the imports
@@ -2748,7 +2747,6 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2748
2747
  const exportedMacros = new Map() // root -> name -> every `#[macro_export]` definition, `{ file, leaves }`
2749
2748
  const fileMacros = new Map()
2750
2749
  const externPrelude = new Map()
2751
- const perRoot = (map, root) => map.get(root) ?? map.set(root, new Map()).get(root)
2752
2750
  // An import's or item's leaves: those its file (`here`) was mounted under, its own cfg's, and
2753
2751
  // its gate macro's.
2754
2752
  const itemLeaves = (here, x, path) => unionLeaves(here.leaves, leafSet([...cfgLeaves(x.cfg), ...gateLeaves(x.macro, gatesOf(path))], leafSets), leafSets)
@@ -2775,7 +2773,7 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2775
2773
  // `mod lib { pub use core::fmt; … }` is what every file's `use crate::lib::*` names.)
2776
2774
  if (im.template === true) continue
2777
2775
  const modulePath = [here.modulePath, ...im.inlinePath].join('::')
2778
- const byModule = perRoot(imports, here.root)
2776
+ const byModule = nestedMap(imports, here.root)
2779
2777
  const of = byModule.get(modulePath) ?? byModule.set(modulePath, { named: new Map(), globs: [] }).get(modulePath)
2780
2778
  const entry = { ...im, file: path, module: modulePath, scopeDepth: scopeDepthOf(im.vis, modulePath), leaves: itemLeaves(here, im, path) }
2781
2779
  if (im.glob) of.globs.push(entry)
@@ -2783,11 +2781,11 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2783
2781
  }
2784
2782
  for (const d of items.defined) {
2785
2783
  const modulePath = [here.modulePath, ...d.inlinePath].join('::')
2786
- const byModule = perRoot(defined, here.root)
2787
- const names = byModule.get(modulePath) ?? byModule.set(modulePath, new Map()).get(modulePath)
2784
+ const byModule = nestedMap(defined, here.root)
2785
+ const names = nestedMap(byModule, modulePath)
2788
2786
  ;(names.get(d.name) ?? names.set(d.name, []).get(d.name)).push({ file: path, scopeDepth: scopeDepthOf(d.vis, modulePath), ns: d.ns, leaves: itemLeaves(here, d, path) })
2789
2787
  }
2790
- const scopes = perRoot(modScope, here.root)
2788
+ const scopes = nestedMap(modScope, here.root)
2791
2789
  const specMap = resolutions.get(path)
2792
2790
  for (const decl of modsOf(path)) {
2793
2791
  const parent = [here.modulePath, ...decl.inlinePath].join('::')
@@ -2810,13 +2808,13 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2810
2808
  fileMacros.set(path, new Set(items.macros.map((m) => m.name)))
2811
2809
  for (const m of items.macros) {
2812
2810
  if (!m.exported) continue
2813
- const byName = perRoot(macros, here.root)
2811
+ const byName = nestedMap(macros, here.root)
2814
2812
  if (!byName.has(m.name)) byName.set(m.name, path)
2815
- const defs = perRoot(exportedMacros, here.root)
2813
+ const defs = nestedMap(exportedMacros, here.root)
2816
2814
  ;(defs.get(m.name) ?? defs.set(m.name, []).get(m.name)).push({ file: path, leaves: itemLeaves(here, m, path) })
2817
2815
  }
2818
2816
  if (here.modulePath === 'crate') {
2819
- const prelude = perRoot(externPrelude, here.root)
2817
+ const prelude = nestedMap(externPrelude, here.root)
2820
2818
  for (const e of items.externCrates) {
2821
2819
  // `extern crate self as x;` puts this crate's root in the prelude as `x` (kept as `self`).
2822
2820
  const named = e.name === 'self' ? e.alias !== null : !NON_CRATE_LEADS.has(e.name)
@@ -3231,7 +3229,7 @@ export async function collectRustFilesFromDisk(baseDir, entries, { cargo = null,
3231
3229
  // A template's `mod`s wait for the files invoking it (expandTemplates).
3232
3230
  for (const decl of mods) {
3233
3231
  if (decl.template === null) modFrom(decl, relPath)
3234
- else (templateMods.get(decl.template) ?? templateMods.set(decl.template, new Map()).get(decl.template)).set(`${relPath}\0${decl.at}`, { decl, from: relPath, exported: macros.some((m) => m.name === decl.template && m.exported) })
3232
+ else nestedMap(templateMods, decl.template).set(`${relPath}\0${decl.at}`, { decl, from: relPath, exported: macros.some((m) => m.name === decl.template && m.exported) })
3235
3233
  }
3236
3234
  // `include!("x.rs")` splices Rust source (scanned next wave, under this module); the asset
3237
3235
  // macros name a file to carry as it is. A build-output path (`concat!(env!("OUT_DIR"), …)`)
@@ -3328,21 +3326,3 @@ export async function collectRustBundle(baseDir, entries, { cargo, buildScripts
3328
3326
  }
3329
3327
  return { sources, formats, tree: await complete() }
3330
3328
  }
3331
-
3332
- // High-level entry: reads a `.rs.txt` listing of crate roots (relative to the listing), then
3333
- // walks `mod` declarations and in-tree crate references from there.
3334
- export async function loadRust(rsTxtFile) {
3335
- const baseDir = dirname(resolve(rsTxtFile))
3336
- const listing = await readFile(rsTxtFile, 'utf8')
3337
- const lines = listing.split('\n').map((l) => l.trim()).filter(Boolean)
3338
- .map((l) => l.replace(/^\.\//u, ''))
3339
- if (lines.length === 0) throw new Error(`Empty Rust listing: ${rsTxtFile}`)
3340
-
3341
- if (!lines.every((line) => extname(line) === '.rs')) {
3342
- throw new Error(`Rust listing must only contain .rs files: ${rsTxtFile}`)
3343
- }
3344
- for (const line of lines) assertWithinBase(baseDir, line, 'Entry path')
3345
-
3346
- const { tree, formats } = await collectRustBundle(baseDir, lines, { cargo: createCargoContext(baseDir, { entries: lines }) })
3347
- return { ...tree, formats }
3348
- }