@exodus/stasis 1.0.0-beta.13 → 1.0.0-beta.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/bin/stasis.js +111 -139
- package/package.json +19 -7
- package/src/audit.js +3 -40
- package/src/babel.js +44 -37
- package/src/bundle-reader.js +24 -4
- package/src/cmd/build.js +194 -8
- package/src/cmd/bundle-options.js +21 -18
- package/src/cmd/bundle.js +959 -262
- package/src/cmd/github-bundle.js +17 -8
- package/src/cmd/output.js +10 -3
- package/src/cmd/tokens.js +67 -0
- package/src/diff.js +2 -2
- package/src/flow.js +22 -0
- package/src/installed-commits.js +4 -2
- package/src/loaders/bash.js +2 -27
- package/src/loaders/cargo.js +6 -6
- package/src/loaders/foundry.js +4 -6
- package/src/loaders/paths.js +4 -15
- package/src/loaders/php.js +3 -5
- package/src/loaders/rust.js +19 -39
- package/src/loaders/solidity-ownership.js +4 -15
- package/src/loaders/solidity.js +4 -67
- package/src/metro-assets.js +415 -0
- package/src/metro-resolver.js +5 -3
- package/src/parse.js +4 -3
- package/src/resolve-fields.js +60 -28
- package/src/resolve-node.js +9 -7
- package/src/resolve-typescript.js +3 -3
- package/src/scan.js +137 -42
- package/src/table.js +63 -0
- package/src/validate.js +1241 -0
- package/src/vfs-bundle/entries.js +5 -2
- package/src/vfs-bundle/github.js +67 -20
- package/src/vfs-bundle/glob.js +420 -0
- package/src/vfs-bundle/lazy.js +128 -0
- package/src/vfs-bundle/tree.js +238 -75
- package/src/vfs-bundle/yarnclean.js +100 -0
- package/src/vfs-bundle.js +16 -7
package/src/cmd/github-bundle.js
CHANGED
|
@@ -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 {
|
|
6
|
+
import { debugTimed } from './bundle.js'
|
|
7
|
+
import { auditTokens, 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,20 @@ 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,
|
|
31
|
-
// JS bundle's lockfile to `lockfile`
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
+
// The tree is fetched with GITHUB_TOKEN from `env` where it is set, else with auditTokens(home)'s
|
|
36
|
+
// `token` (tokens.js); nothing else of `env` is read. Where process.env gives the npm client no
|
|
37
|
+
// token, it is given auditTokens(home)'s `npmToken`, else the one `<home>/.npmrc` gives npm's
|
|
38
|
+
// registry, while the bundle is built (tokens.js's withNpmToken).
|
|
39
|
+
export async function githubBundleCommand({ cwd = process.cwd(), env = process.env, home, output, lockfile, brotliQuality, client, ...options } = {}) {
|
|
40
|
+
const audit = auditTokens(home)
|
|
41
|
+
const built = await withNpmToken({ home, audit }, () => buildGitHubBundle({ ...options, lockfile, client: client ?? createClient({ token: env.GITHUB_TOKEN || audit.token || null }) }))
|
|
42
|
+
const { directory } = built
|
|
43
|
+
const dest = await debugTimed('Wrote the bundle', () => writeBundle(cwd, output ?? githubBundleFile({ github: options.github, directory, commit: built.bundle.repo.commit }), built.bundle.serialize(), brotliQuality))
|
|
36
44
|
if (lockfile !== undefined) writeFile(resolve(cwd, lockfile), built.lockfile.serialize())
|
|
37
|
-
const from = `${options.github}@${built.bundle.repo.commit}${
|
|
45
|
+
const from = `${options.github}@${built.bundle.repo.commit}${directory ? `/${directory}` : ''}`
|
|
46
|
+
console.warn(unpackedSummary(built.stats))
|
|
38
47
|
console.warn(bundledSummary(built.bundle.sources.size, built.bundle.modules, from, dest))
|
|
39
48
|
}
|
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 {
|
|
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,
|
|
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,67 @@
|
|
|
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
|
+
// A line of an .npmrc giving npm's own registry an npm token, exactly as npm writes it: no other
|
|
28
|
+
// registry's, no variable for npm to expand, and nothing else on the line.
|
|
29
|
+
const NPMRC_LINE = /^\/\/registry\.npmjs\.org\/:_authToken=(npm_[\dA-Za-z]+)\r?$/u
|
|
30
|
+
|
|
31
|
+
// The npm token `<home>/.npmrc` (the user's home by default) gives npm's registry: that of the last
|
|
32
|
+
// line of it that is NPMRC_LINE, as npm takes the last of a setting given twice. Another registry's
|
|
33
|
+
// token, or one in any other form, is none. The file is npm's, read for compatibility: one that is
|
|
34
|
+
// missing or unreadable gives none, and nothing is said; one that is not valid UTF-8 is refused.
|
|
35
|
+
export function npmrcToken(home) {
|
|
36
|
+
const file = join(home ?? homedir(), '.npmrc')
|
|
37
|
+
let bytes
|
|
38
|
+
try {
|
|
39
|
+
bytes = readFileSync(file)
|
|
40
|
+
} catch {
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
if (!isUtf8(bytes)) throw new Error(`${file} is not valid UTF-8`)
|
|
44
|
+
let token
|
|
45
|
+
for (const line of bytes.toString('utf8').split('\n')) token = NPMRC_LINE.exec(line)?.[1] ?? token
|
|
46
|
+
return token
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Run `fn`, with a token for the npm client where process.env gives it none (none of
|
|
50
|
+
// NPM_TOKEN_VARS set and not empty), as a fallback: auditTokens(home)'s `npmToken` (`audit`, where
|
|
51
|
+
// it is read already), else npmrcToken(home)'s, each file only read then. That token is
|
|
52
|
+
// STASIS_NPM_TOKEN in process.env while `fn` runs, for the client to read, and process.env is as it
|
|
53
|
+
// was after, unless something else set STASIS_NPM_TOKEN meanwhile. -> what `fn` gives
|
|
54
|
+
export async function withNpmToken({ home, audit } = {}, fn) {
|
|
55
|
+
const token = NPM_TOKEN_VARS.some((name) => process.env[name]) ? undefined : ((audit ?? auditTokens(home)).npmToken ?? npmrcToken(home))
|
|
56
|
+
if (token === undefined) return fn()
|
|
57
|
+
const before = process.env.STASIS_NPM_TOKEN
|
|
58
|
+
process.env.STASIS_NPM_TOKEN = token
|
|
59
|
+
try {
|
|
60
|
+
return await fn()
|
|
61
|
+
} finally {
|
|
62
|
+
if (process.env.STASIS_NPM_TOKEN === token) {
|
|
63
|
+
if (before === undefined) delete process.env.STASIS_NPM_TOKEN
|
|
64
|
+
else process.env.STASIS_NPM_TOKEN = before
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
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
|
|
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 &&
|
|
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,22 @@
|
|
|
1
|
+
// --flow, for `stasis bundle` (the scan) and `stasis build`: Flow type syntax stripped from a JS-family
|
|
2
|
+
// file the parser can't read otherwise, by flow-remove-types -- stasis's own dependency, resolved from
|
|
3
|
+
// stasis itself (never from the project) and loaded lazily, only under --flow.
|
|
4
|
+
import { createRequire } from 'node:module'
|
|
5
|
+
|
|
6
|
+
// Flow type syntax lives in the JS (non-TS) family: flow-remove-types over a .ts/.tsx file would
|
|
7
|
+
// corrupt TS-only constructs. .jsx is included (JS + JSX, which can carry Flow types like .js);
|
|
8
|
+
// flow-remove-types preserves JSX.
|
|
9
|
+
export const FLOW_EXTS = new Set(['.js', '.cjs', '.mjs', '.jsx'])
|
|
10
|
+
|
|
11
|
+
let flowRemoveTypes
|
|
12
|
+
|
|
13
|
+
// `source` with its Flow type syntax blanked to whitespace in place, so byte offsets and every import
|
|
14
|
+
// specifier are kept. `all: true` ignores the @flow/@noflow pragma (React Native's sources mix both).
|
|
15
|
+
// Types only: Flow syntax that is code -- `component` and `hook` declarations, `enum`s -- stays. An
|
|
16
|
+
// import listing types alone (`import { type A } from`) stays as `import {} from`, or with
|
|
17
|
+
// `removeEmptyImports` goes whole, as Babel's Flow transform drops it. Throws where flow-remove-types
|
|
18
|
+
// can't parse `source`.
|
|
19
|
+
export function stripFlowTypes(source, { removeEmptyImports = false } = {}) {
|
|
20
|
+
flowRemoveTypes ??= createRequire(import.meta.url)('flow-remove-types')
|
|
21
|
+
return flowRemoveTypes(source, { all: true, removeEmptyImports }).toString()
|
|
22
|
+
}
|
package/src/installed-commits.js
CHANGED
|
@@ -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
|
-
|
|
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
|
package/src/loaders/bash.js
CHANGED
|
@@ -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 {
|
|
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
|
|
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
|
-
}
|
package/src/loaders/cargo.js
CHANGED
|
@@ -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 (
|
|
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(
|
|
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
|
|
180
|
+
return isFile(candidate, host) ? candidate : null
|
|
181
181
|
}
|
|
182
182
|
|
|
183
183
|
// --- cfg predicates ---------------------------------------------------------------------
|
package/src/loaders/foundry.js
CHANGED
|
@@ -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
|
-
|
|
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 =
|
|
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
|
-
|
|
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)
|
package/src/loaders/paths.js
CHANGED
|
@@ -1,18 +1,4 @@
|
|
|
1
|
-
// Path
|
|
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('../')
|
package/src/loaders/php.js
CHANGED
|
@@ -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
|
|
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'
|
package/src/loaders/rust.js
CHANGED
|
@@ -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 {
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
2637
|
-
const byFile =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
2787
|
-
const names =
|
|
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 =
|
|
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 =
|
|
2811
|
+
const byName = nestedMap(macros, here.root)
|
|
2814
2812
|
if (!byName.has(m.name)) byName.set(m.name, path)
|
|
2815
|
-
const defs =
|
|
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 =
|
|
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
|
|
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
|
-
}
|
|
@@ -8,9 +8,9 @@ import { isAbsolute, join, parse, posix, relative, resolve, sep } from 'node:pat
|
|
|
8
8
|
|
|
9
9
|
import { utf8toString } from '@exodus/bytes/utf8.js'
|
|
10
10
|
import { LockfileError, parseGitmodules } from '@preventive/lockfile/foundry.js'
|
|
11
|
-
import { NO_ENTRY, readRegularFileOrNull } from '@exodus/stasis-core/bundle-util'
|
|
11
|
+
import { NO_ENTRY, lexists, readRegularFileOrNull } from '@exodus/stasis-core/bundle-util'
|
|
12
12
|
import { diskHost } from '@exodus/stasis-core/host'
|
|
13
|
-
import { hasNodeModulesSegment } from '@exodus/stasis-core/util'
|
|
13
|
+
import { hasNodeModulesSegment, nestedMap, relativeEscapes } from '@exodus/stasis-core/util'
|
|
14
14
|
import { isDir } from '../resolve-typescript.js'
|
|
15
15
|
|
|
16
16
|
// `/`-separated, as the loader's paths are: only Windows' separator is converted (on POSIX a `\\` is
|
|
@@ -33,7 +33,7 @@ export function realpathOrNull(p, host = diskHost) {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
// A path relative to a dir (slashes) that stays inside it.
|
|
36
|
-
const inRoot = (rel) =>
|
|
36
|
+
const inRoot = (rel) => !relativeEscapes(rel)
|
|
37
37
|
|
|
38
38
|
// `abs`, a file the resolution read, relative to the project `root` (slashes): as spelled when that
|
|
39
39
|
// lies inside it with no `..` to resolve (a linked lib's files keep the lib's path), else by real
|
|
@@ -72,17 +72,6 @@ function osRealpath(p, host) {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
// Whether anything is at `p` itself, a link not followed (`host.readlink` throws when nothing is).
|
|
76
|
-
function lexists(p, host) {
|
|
77
|
-
try {
|
|
78
|
-
host.readlink(p)
|
|
79
|
-
return true
|
|
80
|
-
} catch (err) {
|
|
81
|
-
if (NO_ENTRY.has(err.code)) return false
|
|
82
|
-
throw err
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
75
|
// `bytes` as UTF-8 text, a byte-order mark kept. Bytes that aren't UTF-8 throw, naming them
|
|
87
76
|
// `label`, rather than read with U+FFFD in their place: forge, solc and git refuse such a file, and
|
|
88
77
|
// the text bundled or read must be the file's own.
|
|
@@ -147,7 +136,7 @@ function gitmodulesLeniently(text) {
|
|
|
147
136
|
if (!variable?.startsWith('submodule.')) continue
|
|
148
137
|
const name = variable.slice('submodule.'.length)
|
|
149
138
|
if (section !== 'submodule') notes.push(`${header}, a section git reads as [submodule "${name}"]; reading it as that`)
|
|
150
|
-
const kept =
|
|
139
|
+
const kept = nestedMap(sections, name)
|
|
151
140
|
const given = paths.get(name) ?? paths.set(name, []).get(name)
|
|
152
141
|
for (const entry of keys) {
|
|
153
142
|
if (!SUBMODULE_KEYS.has(entry.key)) continue
|