@exodus/stasis-core 1.0.0-beta.12 → 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/bin/stasis-core.js +9 -98
- package/package.json +4 -2
- package/src/add.js +68 -70
- package/src/artifact-util.js +48 -21
- package/src/brotli.js +23 -3
- package/src/bundle-util.js +65 -10
- package/src/bundle.js +14 -2
- package/src/cli.js +153 -0
- package/src/config.js +14 -4
- package/src/extract.js +148 -41
- package/src/fs.js +1 -19
- package/src/hooks.js +4 -3
- package/src/host.js +12 -1
- package/src/lockfile.js +15 -1
- package/src/state-util.js +26 -0
- package/src/state.js +155 -146
- package/src/util.js +48 -70
package/bin/stasis-core.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import { spawn } from 'node:child_process'
|
|
4
|
-
import { once } from 'node:events'
|
|
5
3
|
import { fileURLToPath } from 'node:url'
|
|
6
|
-
import { basename
|
|
4
|
+
import { basename } from 'node:path'
|
|
7
5
|
import { existsSync, realpathSync } from 'node:fs'
|
|
8
|
-
import { constants as osConstants } from 'node:os'
|
|
9
6
|
import assert from 'node:assert/strict'
|
|
10
|
-
import {
|
|
7
|
+
import { addCli, extractCli, parseRunOptions, pruneCli, runProgram, setRunEnv } from '../src/cli.js'
|
|
11
8
|
import pkg from '../package.json' with { type: 'json' }
|
|
12
9
|
|
|
13
10
|
const argv = [...process.argv]
|
|
@@ -21,7 +18,7 @@ assert(basename(jsname) === 'stasis-core' || pathsEqual(jsname, fileURLToPath(im
|
|
|
21
18
|
const HELP = `Usage:
|
|
22
19
|
stasis-core run --lock=(add|replace|frozen|ignore) [--bundle=(add|replace|load|frozen|ignore)] [--bundle-file=path/to/bundle.br] [--resources-bundle-file=path/to/resources.br] [--dependencies] [--child-process] [--import=module ...] [--fs=(sync|async)] [--resources=ext,ext] [--brotli-quality=0..11] path/to/file.js ...
|
|
23
20
|
stasis-core add path/to/(file|dir) ...
|
|
24
|
-
stasis-core extract [--output=path/to/dir] path/to/bundle.stasis.code.br
|
|
21
|
+
stasis-core extract [--output=path/to/dir] [--lock=(frozen|add|replace|ignore|none)] path/to/bundle.stasis.code.br
|
|
25
22
|
stasis-core prune [path/to/project]
|
|
26
23
|
stasis-core --version
|
|
27
24
|
|
|
@@ -40,12 +37,6 @@ function fail(message) {
|
|
|
40
37
|
process.exit(1)
|
|
41
38
|
}
|
|
42
39
|
|
|
43
|
-
function setEnv(name, value) {
|
|
44
|
-
const env = process.env[name]
|
|
45
|
-
if (env && env !== value) throw new Error(`env conflict: ${name}="${env}", effective: "${value}"`)
|
|
46
|
-
process.env[name] = value === undefined ? '' : value
|
|
47
|
-
}
|
|
48
|
-
|
|
49
40
|
const command = argv.shift()
|
|
50
41
|
|
|
51
42
|
if (command === '-v' || command === '--version') {
|
|
@@ -54,95 +45,15 @@ if (command === '-v' || command === '--version') {
|
|
|
54
45
|
} else if (command === '-h' || command === '--help') {
|
|
55
46
|
usage(true)
|
|
56
47
|
} else if (command === 'run') {
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
'bundle-file': { type: 'string' },
|
|
61
|
-
'resources-bundle-file': { type: 'string' },
|
|
62
|
-
debug: { type: 'boolean' },
|
|
63
|
-
dependencies: { type: 'boolean' },
|
|
64
|
-
'child-process': { type: 'boolean' },
|
|
65
|
-
import: { type: 'string', multiple: true },
|
|
66
|
-
fs: { type: 'string' },
|
|
67
|
-
resources: { type: 'string' },
|
|
68
|
-
'brotli-quality': { type: 'string' },
|
|
69
|
-
}
|
|
70
|
-
const values = parseLeadingOptions(argv, options, {
|
|
71
|
-
valueFlags: ['--bundle', '--bundle-file', '--resources-bundle-file', '--lock', '--import', '--resources', '--brotli-quality'],
|
|
72
|
-
onError: fail,
|
|
73
|
-
})
|
|
74
|
-
if (argv.length === 0) fail('Nothing to run: no path to file given')
|
|
75
|
-
if (!['none', 'ignore', 'add', 'replace', 'frozen'].includes(values.lock)) fail('Error: invalid --lock value')
|
|
76
|
-
const lock = values.lock
|
|
77
|
-
const scope = values.dependencies ? 'node_modules' : 'full'
|
|
78
|
-
const bundle = values.bundle
|
|
79
|
-
const bundleFile = values['bundle-file'] ? resolve(values['bundle-file']) : ''
|
|
80
|
-
const resourcesBundleFile = values['resources-bundle-file'] ? resolve(values['resources-bundle-file']) : ''
|
|
81
|
-
const debug = values.debug ? '1' : ''
|
|
82
|
-
if (!['none', 'ignore', 'add', 'replace', 'load', 'frozen'].includes(bundle)) fail('Error: invalid --bundle value')
|
|
83
|
-
if (bundleFile && bundle === 'none') fail('Error: --bundle-file requires --bundle=(add|replace|load|frozen|ignore)')
|
|
84
|
-
if (resourcesBundleFile && (bundle === 'none' || bundle === 'ignore')) fail('Error: --resources-bundle-file requires --bundle=(add|replace|load|frozen)')
|
|
85
|
-
if (bundle === 'load' && lock !== 'frozen' && lock !== 'none' && lock !== 'ignore') fail('Error: --bundle=load is incompatible with --lock=(add|replace)')
|
|
86
|
-
if (lock === 'none' && bundle === 'none') fail('Error: stasis needs a lockfile or a bundle: set --lock or --bundle')
|
|
87
|
-
if (values.fs !== undefined && !['sync', 'async'].includes(values.fs)) fail("Error: --fs must be 'sync' or 'async'")
|
|
88
|
-
if (values.fs !== undefined && !['add', 'replace', 'load'].includes(bundle)) fail('Error: --fs requires --bundle=(add|replace|load)')
|
|
89
|
-
const captureFs = values.fs ?? ''
|
|
90
|
-
const resources = values.resources ?? ''
|
|
91
|
-
let brotliQuality
|
|
92
|
-
if (values['brotli-quality'] !== undefined) {
|
|
93
|
-
try {
|
|
94
|
-
brotliQuality = parseBrotliQuality('--brotli-quality', values['brotli-quality'])
|
|
95
|
-
} catch (cause) {
|
|
96
|
-
fail(`Error: ${cause.message}`)
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
const childProcess = values['child-process'] ? '1' : ''
|
|
100
|
-
// --import: extra preload module(s) passed through to the spawned node. Node resolves each
|
|
101
|
-
// against the project cwd; they ride AFTER stasis's own loader import.
|
|
102
|
-
const imports = values.import ?? []
|
|
103
|
-
if (imports.some((s) => s === '')) fail('Error: --import requires a module specifier (e.g. --import=./instrument.mjs)')
|
|
104
|
-
console.warn('[stasis-core] Running stasis with config:', { lock, scope, bundle, ...(bundleFile && { bundleFile }), ...(resourcesBundleFile && { resourcesBundleFile }), ...(childProcess && { childProcess: true }), ...(imports.length > 0 && { import: imports }), ...(values.fs && { fs: values.fs }), ...(resources && { resources }), ...(brotliQuality !== undefined && { brotliQuality }) })
|
|
105
|
-
if (debug) console.warn(`[stasis-core] Warning: stasis debug mode active`)
|
|
106
|
-
setEnv('EXODUS_STASIS_LOCK', lock)
|
|
107
|
-
setEnv('EXODUS_STASIS_SCOPE', scope)
|
|
108
|
-
setEnv('EXODUS_STASIS_BUNDLE', bundle)
|
|
109
|
-
setEnv('EXODUS_STASIS_BUNDLE_FILE', bundleFile)
|
|
110
|
-
setEnv('EXODUS_STASIS_RESOURCES_BUNDLE_FILE', resourcesBundleFile)
|
|
111
|
-
setEnv('EXODUS_STASIS_DEBUG', debug)
|
|
112
|
-
setEnv('EXODUS_STASIS_CHILD_PROCESS', childProcess)
|
|
113
|
-
setEnv('EXODUS_STASIS_FS', captureFs)
|
|
114
|
-
setEnv('EXODUS_STASIS_RESOURCES', resources)
|
|
115
|
-
// Only set when given: an unconditional setEnv('') would reject an ambient EXODUS_STASIS_BROTLI_QUALITY as a conflict.
|
|
116
|
-
if (brotliQuality !== undefined) setEnv('EXODUS_STASIS_BROTLI_QUALITY', String(brotliQuality))
|
|
117
|
-
const nodeArgs = ['--import', import.meta.resolve('../src/loader.js')]
|
|
118
|
-
// Passthrough preloads ride after the loader, so they evaluate under its hooks; their module
|
|
119
|
-
// graphs pass through uncaptured unless the app graph reaches them -- then they're promoted
|
|
120
|
-
// into the capture (see src/hooks.js).
|
|
121
|
-
for (const specifier of imports) nodeArgs.push('--import', specifier)
|
|
122
|
-
const child = spawn(process.execPath, [...nodeArgs, ...argv], { stdio: 'inherit' })
|
|
123
|
-
const [code, signal] = await once(child, 'close')
|
|
124
|
-
// code is null when the child died from a signal; report 128+signo (shell convention) instead of the implicit 0
|
|
125
|
-
process.exitCode = code ?? 128 + (osConstants.signals[signal] ?? 0)
|
|
48
|
+
const run = parseRunOptions(argv, { fail })
|
|
49
|
+
setRunEnv('stasis-core', run)
|
|
50
|
+
await runProgram(argv, { loader: import.meta.resolve('../src/loader.js'), imports: run.imports })
|
|
126
51
|
} else if (command === 'prune') {
|
|
127
|
-
|
|
128
|
-
const root = argv[0] ? resolve(argv[0]) : process.cwd()
|
|
129
|
-
const { prune } = await import('../src/prune.js')
|
|
130
|
-
const { removed, validated, minimized } = prune({ root })
|
|
131
|
-
console.warn(`[stasis-core] prune: validated ${validated.length} file(s), removed ${removed.length} file(s), minimized ${minimized.length} package.json file(s)`)
|
|
52
|
+
await pruneCli(argv, { label: 'stasis-core', fail })
|
|
132
53
|
} else if (command === 'add') {
|
|
133
|
-
|
|
134
|
-
if (argv.some((a) => a.startsWith('-'))) fail('Error: add takes no options; its targets and resource allowlist come from stasis.config.json')
|
|
135
|
-
const { addCommand } = await import('../src/add.js')
|
|
136
|
-
addCommand({ cwd: process.cwd(), entries: argv, logLabel: 'stasis-core' })
|
|
54
|
+
await addCli(argv, { label: 'stasis-core', fail })
|
|
137
55
|
} else if (command === 'extract') {
|
|
138
|
-
|
|
139
|
-
output: { type: 'string', short: 'o' },
|
|
140
|
-
}
|
|
141
|
-
const values = parseLeadingOptions(argv, options, { valueFlags: ['--output', '-o'], onError: fail })
|
|
142
|
-
if (argv.length === 0) fail('Nothing to extract: no bundle file given')
|
|
143
|
-
if (argv.length > 1) fail('Error: extract takes exactly one bundle file')
|
|
144
|
-
const { extractCommand } = await import('../src/extract.js')
|
|
145
|
-
extractCommand({ cwd: process.cwd(), bundleFile: argv[0], output: values.output, logLabel: 'stasis-core' })
|
|
56
|
+
await extractCli(argv, { label: 'stasis-core', fail })
|
|
146
57
|
} else if (command === undefined) {
|
|
147
58
|
usage()
|
|
148
59
|
} else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/stasis-core",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.14",
|
|
4
4
|
"description": "Zero-dependency stasis runtime",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"./util": "./src/util.js",
|
|
18
18
|
"./brotli": "./src/brotli.js",
|
|
19
19
|
"./state-util": "./src/state-util.js",
|
|
20
|
-
"./prune": "./src/prune.js"
|
|
20
|
+
"./prune": "./src/prune.js",
|
|
21
|
+
"./cli": "./src/cli.js"
|
|
21
22
|
},
|
|
22
23
|
"files": [
|
|
23
24
|
"bin/stasis-core.js",
|
|
@@ -26,6 +27,7 @@
|
|
|
26
27
|
"src/brotli.js",
|
|
27
28
|
"src/bundle.js",
|
|
28
29
|
"src/bundle-util.js",
|
|
30
|
+
"src/cli.js",
|
|
29
31
|
"src/config.js",
|
|
30
32
|
"src/extract.js",
|
|
31
33
|
"src/fs.js",
|
package/src/add.js
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import { isUtf8 } from 'node:buffer'
|
|
2
2
|
import * as fs from 'node:fs'
|
|
3
|
-
import { basename, dirname, extname, join,
|
|
4
|
-
import { brotliCompressSync, brotliDecompressSync } from 'node:zlib'
|
|
3
|
+
import { basename, dirname, extname, join, posix, relative, resolve, sep } from 'node:path'
|
|
5
4
|
|
|
6
5
|
import { Bundle } from './bundle.js'
|
|
7
6
|
import { Lockfile } from './lockfile.js'
|
|
8
|
-
import {
|
|
9
|
-
import { detectRepo,
|
|
7
|
+
import { compressBundleText, parseBundleBytes } from './brotli.js'
|
|
8
|
+
import { detectRepo, normalizeEntries, packageLookup, packageType, readJson, statStrict } from './bundle-util.js'
|
|
9
|
+
import { diskHost } from './host.js'
|
|
10
10
|
import { canonicalizePath, sha512integrity } from './state-util.js'
|
|
11
|
-
import { assertRealPathWithinBase, classifyFormat, hasNodeModulesSegment, isAutoExcludedDir, isAutoExcludedFile, isBinaryPlist, isBrotliQuality, isExecutableMode,
|
|
11
|
+
import { assertRealPathWithinBase, classifyFormat, fileInBucket, hasNodeModulesSegment, isAutoExcludedDir, isAutoExcludedFile, isBinaryPlist, isBrotliQuality, isExecutableMode, moduleInfo, parseResourcesOption, pathExt, sortPaths, splitNodeModulesPath, toPosix, withMetadataOf } from './util.js'
|
|
12
12
|
|
|
13
|
-
// Snapshotted before `stasis run --fs` patches node:fs: State#includeExtra
|
|
14
|
-
// at write, which must read the real disk, not be captured as the program's
|
|
15
|
-
|
|
13
|
+
// Snapshotted before `stasis run --fs` patches node:fs: State#includeExtra matches its outputs with
|
|
14
|
+
// these at write (outputMatcher), which must read the real disk, not be captured as the program's
|
|
15
|
+
// reads. It sweeps `extra` through its host, which the disk's snapshots too.
|
|
16
|
+
const { existsSync, mkdirSync, readFileSync, realpathSync, statSync, writeFileSync } = fs
|
|
16
17
|
|
|
17
18
|
const CONFIG_FILE = 'stasis.config.json'
|
|
18
19
|
const LOCK_FILE = 'stasis.lock.json'
|
|
@@ -20,10 +21,10 @@ const LOCK_FILE = 'stasis.lock.json'
|
|
|
20
21
|
// A `.js`/`.ts` follows its package `type` (LIMITATION: none falls back to commonjs -- use `.mjs`/`.cjs`
|
|
21
22
|
// or set `type` when the module system matters). Any other name is classifyFormat's call: a format,
|
|
22
23
|
// null for a JS-family file (`.jsx`/`.tsx`: source attested with no loader format, as the runtime
|
|
23
|
-
// records one), undefined when unrecognized.
|
|
24
|
-
function sourceFormat(absFile, content) {
|
|
24
|
+
// records one), undefined when unrecognized. Its package.json is read through `host`.
|
|
25
|
+
function sourceFormat(absFile, content, host) {
|
|
25
26
|
const ext = extname(absFile).toLowerCase()
|
|
26
|
-
if (ext === '.js' || ext === '.ts') return `${packageType(absFile) ?? 'commonjs'}${ext === '.ts' ? '-typescript' : ''}`
|
|
27
|
+
if (ext === '.js' || ext === '.ts') return `${packageType(absFile, host) ?? 'commonjs'}${ext === '.ts' ? '-typescript' : ''}`
|
|
27
28
|
return classifyFormat(absFile, { content })
|
|
28
29
|
}
|
|
29
30
|
|
|
@@ -63,7 +64,8 @@ function readAddConfig(baseDir) {
|
|
|
63
64
|
// Assemble a Bundle bucketed per package.json. `add` records NO entries -- its files are attested, not entry points -- so nothing in an add bundle is runnable via `--bundle=load`.
|
|
64
65
|
// `buckets` (dir -> module) are identities to take for a dir over the one found there: those of the
|
|
65
66
|
// bundle the files will merge into, which may name its buckets otherwise (`solidity-bundle`, say).
|
|
66
|
-
|
|
67
|
+
// Their package.json files are read through `host`.
|
|
68
|
+
function assembleBundle(baseDir, files, workspaceName, workspaceVersion, repo, { reason = 'add', buckets = new Map(), host = diskHost } = {}) {
|
|
67
69
|
const modules = new Map()
|
|
68
70
|
const formats = new Map()
|
|
69
71
|
const executable = new Set()
|
|
@@ -75,13 +77,7 @@ function assembleBundle(baseDir, files, workspaceName, workspaceVersion, repo, {
|
|
|
75
77
|
modules.set(dir, bucket === undefined ? moduleInfo({ ...identity, files: Object.create(null) }) : withMetadataOf(bucket, identity))
|
|
76
78
|
return modules.get(dir).files
|
|
77
79
|
}
|
|
78
|
-
|
|
79
|
-
const metaByDir = new Map()
|
|
80
|
-
const metaFor = (rel) => {
|
|
81
|
-
const dir = dirname(rel)
|
|
82
|
-
if (!metaByDir.has(dir)) metaByDir.set(dir, findPackageMetadata(baseDir, rel))
|
|
83
|
-
return metaByDir.get(dir)
|
|
84
|
-
}
|
|
80
|
+
const metaFor = packageLookup(baseDir, { host })
|
|
85
81
|
|
|
86
82
|
for (const [rel, { content, format, executable: isExec }] of files) {
|
|
87
83
|
const meta = metaFor(rel)
|
|
@@ -89,12 +85,8 @@ function assembleBundle(baseDir, files, workspaceName, workspaceVersion, repo, {
|
|
|
89
85
|
if (splitNodeModulesPath(rel) !== null && !(meta && hasNodeModulesSegment(meta.pkgDir))) {
|
|
90
86
|
throw new Error(`add: no package.json with name+version found for ${rel}`)
|
|
91
87
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
bucketFiles(meta.pkgDir, meta)[relInBucket] = content
|
|
95
|
-
} else {
|
|
96
|
-
bucketFiles('.', { name: workspaceName, version: workspaceVersion })[rel] = content
|
|
97
|
-
}
|
|
88
|
+
const [dir, identity] = meta ? [meta.pkgDir, meta] : ['.', { name: workspaceName, version: workspaceVersion }]
|
|
89
|
+
bucketFiles(dir, identity)[fileInBucket(dir, rel)] = content
|
|
98
90
|
if (format != null) formats.set(rel, format)
|
|
99
91
|
if (isExec) executable.add(rel)
|
|
100
92
|
}
|
|
@@ -112,7 +104,7 @@ function prepareBundleFile(baseDir, targetPath, bundle, brotliQuality) {
|
|
|
112
104
|
if (existsSync(abs)) {
|
|
113
105
|
let existing
|
|
114
106
|
try {
|
|
115
|
-
existing =
|
|
107
|
+
existing = parseBundleBytes(readFileSync(abs))
|
|
116
108
|
} catch (cause) {
|
|
117
109
|
throw new Error(`add: failed to read the existing bundle at ${targetPath}`, { cause })
|
|
118
110
|
}
|
|
@@ -123,22 +115,14 @@ function prepareBundleFile(baseDir, targetPath, bundle, brotliQuality) {
|
|
|
123
115
|
return {
|
|
124
116
|
write: () => {
|
|
125
117
|
mkdirSync(dirname(abs), { recursive: true })
|
|
126
|
-
writeFileSync(abs,
|
|
118
|
+
writeFileSync(abs, compressBundleText(bundle.serialize(), brotliQuality))
|
|
127
119
|
},
|
|
128
120
|
counts: { path: targetPath, total: files, packages, added: mergedFrom === undefined ? files : files - mergedFrom },
|
|
129
121
|
}
|
|
130
122
|
}
|
|
131
123
|
|
|
132
124
|
// Companion Lockfile: the bundle's shape with each file's content swapped for its on-disk integrity, so `--lock=frozen` matches.
|
|
133
|
-
|
|
134
|
-
const modules = new Map()
|
|
135
|
-
for (const [dir, m] of bundle.modules) {
|
|
136
|
-
const files = Object.create(null)
|
|
137
|
-
for (const rel of Object.keys(m.files)) files[rel] = integrities.get(moduleFileKey(dir, rel))
|
|
138
|
-
modules.set(dir, moduleInfo({ ...m, files }))
|
|
139
|
-
}
|
|
140
|
-
return new Lockfile({ config: bundle.config, entries: bundle.entries, modules, imports: bundle.imports, formats: bundle.formats, executable: bundle.executable })
|
|
141
|
-
}
|
|
125
|
+
const bundleToLockfile = (bundle, integrities) => Lockfile.fromBundle(bundle, (file) => integrities.get(file))
|
|
142
126
|
|
|
143
127
|
// Merge into the project's stasis.lock.json (strict: divergent bytes throw), deferring the write like
|
|
144
128
|
// prepareBundleFile so a conflict here doesn't leave the bundles updated.
|
|
@@ -158,7 +142,8 @@ function prepareLockfile(lockPath, lockAdd) {
|
|
|
158
142
|
// Whether a project-relative path, with its stats (undefined when missing), is one of `outputs`
|
|
159
143
|
// (project-relative): by name, or as the same file under another, a symlink or hard link to it -- its
|
|
160
144
|
// device and inode, which a write rewrites in place. Stats as numbers can round a large inode
|
|
161
|
-
// (Windows file ids), so a match on those is settled exactly (bigint), never taken on its own.
|
|
145
|
+
// (Windows file ids), so a match on those is settled exactly (bigint), never taken on its own. The
|
|
146
|
+
// outputs are the disk's, where a run writes them: a sweep of another host's tree has none.
|
|
162
147
|
function outputMatcher(baseDir, outputs) {
|
|
163
148
|
const exact = new Set()
|
|
164
149
|
const rounded = new Set()
|
|
@@ -176,19 +161,16 @@ function outputMatcher(baseDir, outputs) {
|
|
|
176
161
|
}
|
|
177
162
|
}
|
|
178
163
|
|
|
179
|
-
//
|
|
180
|
-
//
|
|
181
|
-
//
|
|
182
|
-
//
|
|
183
|
-
function globPaths(pattern, cwd, baseDir, exclude) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
const projectPath = (dirent) => {
|
|
187
|
-
const rel = relative(baseDir, pathOf(dirent))
|
|
164
|
+
// `host`'s glob of `pattern` under `cwd` (Node's fs.globSync, the disk's or a Vfs's), its matches
|
|
165
|
+
// relative to it, walking into none `exclude` takes (given the path relative to `baseDir`,
|
|
166
|
+
// '/'-joined), which leaves one out where its `**` meets it alone. A visited name is checked as
|
|
167
|
+
// spelled: one holding `\` is for the caller to refuse where it is kept, not here.
|
|
168
|
+
function globPaths(host, pattern, cwd, baseDir, exclude) {
|
|
169
|
+
const projectPath = (path) => {
|
|
170
|
+
const rel = relative(baseDir, path)
|
|
188
171
|
return sep === '\\' ? rel.replaceAll('\\', '/') : rel
|
|
189
172
|
}
|
|
190
|
-
return
|
|
191
|
-
.map((dirent) => relative(cwd, pathOf(dirent)))
|
|
173
|
+
return host.glob(pattern, { cwd, exclude: exclude && ((path) => exclude(projectPath(path))) })
|
|
192
174
|
}
|
|
193
175
|
|
|
194
176
|
// GLOB + FILTER: expand each directory entry into the files under it (recursive glob), dropping the
|
|
@@ -202,8 +184,8 @@ function globPaths(pattern, cwd, baseDir, exclude) {
|
|
|
202
184
|
// either order. `stats` is the walk's own stat, reused by validation. Gotcha: `**/*` skips dotfiles
|
|
203
185
|
// (.env, .git/...) -- name them explicitly. A project-relative path `exclude` takes is dropped, named
|
|
204
186
|
// or swept, with everything under it: a named path below one too, and a directory it takes is never
|
|
205
|
-
// walked (globPaths).
|
|
206
|
-
function expandDirectories(baseDir, rels, outputs, { keepNamedOutputs = true, exclude } = {}) {
|
|
187
|
+
// walked (globPaths). Paths are read through `host`, stat'ed as real stat answers (statStrict).
|
|
188
|
+
function expandDirectories(baseDir, rels, outputs, { keepNamedOutputs = true, exclude, host = diskHost } = {}) {
|
|
207
189
|
const files = new Map() // rel -> stats
|
|
208
190
|
const excluded = new Set()
|
|
209
191
|
const isOutput = outputMatcher(baseDir, outputs)
|
|
@@ -211,21 +193,25 @@ function expandDirectories(baseDir, rels, outputs, { keepNamedOutputs = true, ex
|
|
|
211
193
|
// A walk meets a directory before what's under it, so it checks each path alone; a named one, its directories too.
|
|
212
194
|
if (exclude !== undefined && rel.split('/').some((_, i, segments) => exclude(segments.slice(0, i + 1).join('/')))) continue
|
|
213
195
|
const dirAbs = join(baseDir, rel)
|
|
214
|
-
const stats =
|
|
196
|
+
const stats = statStrict(host, dirAbs, rel) ?? undefined
|
|
215
197
|
if (stats === undefined || !stats.isDirectory()) {
|
|
216
198
|
if (keepNamedOutputs || !isOutput(rel, stats)) files.set(rel, stats)
|
|
217
199
|
continue
|
|
218
200
|
}
|
|
219
201
|
const found = []
|
|
220
|
-
for (const match of globPaths('**/*', dirAbs, baseDir, exclude)) {
|
|
202
|
+
for (const match of globPaths(host, '**/*', dirAbs, baseDir, exclude)) {
|
|
221
203
|
const fileAbs = join(dirAbs, match)
|
|
204
|
+
const fileRel = relative(baseDir, fileAbs)
|
|
222
205
|
// A dangling symlink stats as undefined -- skipped, not a hard ENOENT out of the walk.
|
|
223
|
-
const fileStats =
|
|
224
|
-
if (fileStats?.isFile()) found.push([toPosix(
|
|
206
|
+
const fileStats = statStrict(host, fileAbs, fileRel) ?? undefined
|
|
207
|
+
if (fileStats?.isFile()) found.push([toPosix(fileRel), match, fileStats])
|
|
225
208
|
}
|
|
226
209
|
// Sorted so the sweep is deterministic regardless of readdir order: artifact bytes are
|
|
227
210
|
// canonicalized by serialize() either way, but multi-file diagnostics keep a stable order.
|
|
228
211
|
for (const [file, match, fileStats] of found.toSorted(([a], [b]) => sortPaths(a, b))) {
|
|
212
|
+
// Node's glob holds `exclude` to its `**` alone: a file below the directory swept still
|
|
213
|
+
// matches the `*` after it (a directory it takes is never walked).
|
|
214
|
+
if (exclude?.(file)) continue
|
|
229
215
|
if (toPosix(match).split('/').slice(0, -1).some(isAutoExcludedDir)
|
|
230
216
|
|| isOutput(file, fileStats) || isAutoExcludedFile(file)) excluded.add(file)
|
|
231
217
|
else files.set(file, fileStats)
|
|
@@ -267,15 +253,23 @@ export function validationError({ missing, undeclared, nonUtf8 }, label) {
|
|
|
267
253
|
// takes out every path its pattern matches, in any order of the entries, along with everything under
|
|
268
254
|
// one that is a directory, which is then never walked, whether by a pattern's expansion or a sweep:
|
|
269
255
|
// a pattern whose every match is excluded matches nothing. One matching nothing is no error.
|
|
270
|
-
|
|
256
|
+
// `baseDir` is read through `host`, globbed and matched as Node globs and matches the disk
|
|
257
|
+
// (fs.globSync, path.matchesGlob), whichever host's it is.
|
|
258
|
+
export function expandExtra(baseDir, patterns, outputs, host = diskHost) {
|
|
271
259
|
// Normalized as the paths they're matched against are: no `./`, no trailing `/`.
|
|
272
260
|
const exclusions = patterns.filter((pattern) => pattern.startsWith('!')).map((pattern) => posix.normalize(pattern.slice(1)).replace(/\/+$/u, ''))
|
|
273
|
-
|
|
261
|
+
// Each path once: a walk asks of it, a sweep again, and a named path of every directory above it,
|
|
262
|
+
// where the disk's path.matchesGlob parses each pattern anew.
|
|
263
|
+
const excluded = new Map()
|
|
264
|
+
const exclude = exclusions.length === 0 ? undefined : (rel) => {
|
|
265
|
+
if (!excluded.has(rel)) excluded.set(rel, exclusions.some((pattern) => host.matchesGlob(rel, pattern)))
|
|
266
|
+
return excluded.get(rel)
|
|
267
|
+
}
|
|
274
268
|
const entries = patterns.filter((pattern) => !pattern.startsWith('!')).flatMap((pattern) => {
|
|
275
|
-
const matches = globPaths(pattern, baseDir, baseDir, exclude)
|
|
269
|
+
const matches = globPaths(host, pattern, baseDir, baseDir, exclude)
|
|
276
270
|
return matches.length === 0 ? [pattern] : matches.toSorted(sortPaths)
|
|
277
271
|
})
|
|
278
|
-
return expandDirectories(baseDir, normalizeEntries(entries, baseDir), outputs, { keepNamedOutputs: false, exclude })
|
|
272
|
+
return expandDirectories(baseDir, normalizeEntries(entries, baseDir), outputs, { keepNamedOutputs: false, exclude, host })
|
|
279
273
|
}
|
|
280
274
|
|
|
281
275
|
// VALIDATE step: classify the WHOLE set -- each file recognized source or a declared resource --
|
|
@@ -283,7 +277,8 @@ export function expandExtra(baseDir, patterns, outputs) {
|
|
|
283
277
|
// step's rel -> stats map (stats undefined = not on disk). Containment is the one rule NOT collected:
|
|
284
278
|
// a symlink escaping the root is a security invariant, so it fails closed on the spot. Bytes are
|
|
285
279
|
// hashed only when there's a lockfile to receive the integrities (`withIntegrity`). `label` prefixes the error.
|
|
286
|
-
|
|
280
|
+
// Files are read through `host`.
|
|
281
|
+
function validateFiles({ baseDir, realBase, files, resources, withIntegrity, label = 'add', host = diskHost }) {
|
|
287
282
|
const codeFiles = new Map()
|
|
288
283
|
const resourceFiles = new Map()
|
|
289
284
|
const integrities = new Map()
|
|
@@ -296,11 +291,11 @@ function validateFiles({ baseDir, realBase, files, resources, withIntegrity, lab
|
|
|
296
291
|
continue
|
|
297
292
|
}
|
|
298
293
|
const executable = isExecutableMode(stats)
|
|
299
|
-
assertRealPathWithinBase(realBase, baseDir, rel)
|
|
294
|
+
assertRealPathWithinBase(realBase, baseDir, rel, host)
|
|
300
295
|
const abs = join(baseDir, rel)
|
|
301
|
-
const buf =
|
|
296
|
+
const buf = host.readFile(abs)
|
|
302
297
|
if (withIntegrity) integrities.set(rel, sha512integrity(buf))
|
|
303
|
-
const format = sourceFormat(abs, buf)
|
|
298
|
+
const format = sourceFormat(abs, buf, host)
|
|
304
299
|
// A binary plist can't be stored as the UTF-8 string its 'xml' format implies, so it is NOT source: it falls through to the resource branch (opaque base64).
|
|
305
300
|
if (format !== undefined && !isBinaryPlist(rel, buf)) {
|
|
306
301
|
// Source is stored as a UTF-8 string, so non-UTF-8 bytes would lossily diverge from the file on disk.
|
|
@@ -320,17 +315,20 @@ function validateFiles({ baseDir, realBase, files, resources, withIntegrity, lab
|
|
|
320
315
|
// The files stasis.config.json's `extra` names under `baseDir` (expandExtra), packed as `add` packs
|
|
321
316
|
// them, for `stasis bundle` to merge into `bundle`, one built there without a State (always full
|
|
322
317
|
// scope): those it doesn't carry already (its own record of a file stands), in its buckets where it
|
|
323
|
-
// has them. `outputs` are the project-relative paths the build writes, never swept in.
|
|
318
|
+
// has them. `outputs` are the project-relative paths the build writes, never swept in. The files
|
|
319
|
+
// are read through `host`, the disk by default.
|
|
324
320
|
// -> { bundle, lockfile } of the `extra` consumer (null when there is nothing to add), and
|
|
325
321
|
// `carried`: the files named that `bundle` carries already, the `extra` consumer's too
|
|
326
|
-
export function extraArtifacts({ baseDir, patterns, resources, bundle, outputs = new Set() }) {
|
|
327
|
-
const named = expandExtra(baseDir, patterns, outputs).files
|
|
328
|
-
|
|
329
|
-
const
|
|
322
|
+
export function extraArtifacts({ baseDir, patterns, resources, bundle, outputs = new Set(), host = diskHost }) {
|
|
323
|
+
const named = expandExtra(baseDir, patterns, outputs, host).files
|
|
324
|
+
// Bundle#sources builds its Map afresh on each read: once.
|
|
325
|
+
const bundled = bundle.sources
|
|
326
|
+
const carried = [...named.keys()].filter((rel) => bundled.has(rel))
|
|
327
|
+
const files = new Map([...named].filter(([rel]) => !bundled.has(rel)))
|
|
330
328
|
if (files.size === 0) return { bundle: null, lockfile: null, carried }
|
|
331
|
-
const { codeFiles, resourceFiles, integrities } = validateFiles({ baseDir, realBase:
|
|
332
|
-
const rootPkg = readJson(join(baseDir, 'package.json')) ?? {}
|
|
333
|
-
const extra = assembleBundle(baseDir, new Map([...codeFiles, ...resourceFiles]), rootPkg.name ?? 'workspace', rootPkg.version ?? '0.0.0', undefined, { reason: 'extra', buckets: bundle.modules })
|
|
329
|
+
const { codeFiles, resourceFiles, integrities } = validateFiles({ baseDir, realBase: host.realpath(baseDir), files, resources, withIntegrity: true, label: 'extra', host })
|
|
330
|
+
const rootPkg = readJson(join(baseDir, 'package.json'), host) ?? {}
|
|
331
|
+
const extra = assembleBundle(baseDir, new Map([...codeFiles, ...resourceFiles]), rootPkg.name ?? 'workspace', rootPkg.version ?? '0.0.0', undefined, { reason: 'extra', buckets: bundle.modules, host })
|
|
334
332
|
return { bundle: extra, lockfile: bundleToLockfile(extra, integrities), carried }
|
|
335
333
|
}
|
|
336
334
|
|
package/src/artifact-util.js
CHANGED
|
@@ -54,6 +54,9 @@ export function moduleFileKey(dir, rel) {
|
|
|
54
54
|
return dir === '.' ? rel : `${dir}/${rel}`
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
// A file's key inside its bucket `dir`, of its flat project-relative `path` below it: the inverse of moduleFileKey.
|
|
58
|
+
export const fileInBucket = (dir, path) => (dir === '.' ? path : path.slice(dir.length + 1))
|
|
59
|
+
|
|
57
60
|
// GitHub `owner/name` (owner 1-39, name 1-100 chars).
|
|
58
61
|
const GITHUB_REPO = /^(?=[A-Za-z0-9-]{1,39}\/)[A-Za-z0-9](?:-?[A-Za-z0-9])*\/(?!\.\.?$)[\w.-]{1,100}$/u
|
|
59
62
|
// Non-empty normalized repo-relative path of URL-safe segments.
|
|
@@ -152,7 +155,7 @@ function carriedEntries(entries, files) {
|
|
|
152
155
|
const dir = entryDirOf(entries, rel)
|
|
153
156
|
if (dir !== undefined) carried.add(dir)
|
|
154
157
|
}
|
|
155
|
-
return carried.size === 0 ? undefined : Object.freeze(fromEntries(
|
|
158
|
+
return carried.size === 0 ? undefined : Object.freeze(fromEntries(sortedByPath(carried).map((dir) => [dir, entries[dir]])))
|
|
156
159
|
}
|
|
157
160
|
|
|
158
161
|
// A dependency's `field` of DIR_METADATA, validated: each entry holds one of the record's `files`. First-party
|
|
@@ -312,42 +315,56 @@ export function assert(condition, msg) {
|
|
|
312
315
|
if (!condition) throw new Error(msg)
|
|
313
316
|
}
|
|
314
317
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
318
|
+
// stasis's lockfile, by name at any depth and in any case: a case-insensitive disk resolves `STASIS.LOCK.JSON` to it too.
|
|
319
|
+
export const isLockfileName = (path) => path.slice(path.lastIndexOf('/') + 1).toLowerCase() === 'stasis.lock.json'
|
|
320
|
+
|
|
321
|
+
// sortPaths' order, of two paths as split at '/' (read, not changed).
|
|
322
|
+
function compareSegments(al, bl) {
|
|
323
|
+
// The segments from `i` on are where the two differ.
|
|
324
|
+
let i = 0
|
|
325
|
+
while (i < al.length && al[i] === bl[i]) i++
|
|
326
|
+
const ar = al.length - i
|
|
327
|
+
const br = bl.length - i
|
|
328
|
+
if (ar === 0 && br === 0) return 0
|
|
322
329
|
|
|
323
330
|
// First process each file in dir, then subdirs
|
|
324
|
-
if (
|
|
325
|
-
if (
|
|
331
|
+
if (ar < 2) return br < 2 && (al[i] ?? '') > (bl[i] ?? '') ? 1 : -1
|
|
332
|
+
if (br < 2) return 1
|
|
326
333
|
|
|
327
|
-
if (al[
|
|
328
|
-
if (bl[
|
|
334
|
+
if (al[i] === '*') return -1
|
|
335
|
+
if (bl[i] === '*') return 1
|
|
329
336
|
|
|
330
|
-
if (al[
|
|
331
|
-
if (bl[
|
|
337
|
+
if (al[i] === 'node_modules') return 1
|
|
338
|
+
if (bl[i] === 'node_modules') return -1
|
|
332
339
|
|
|
333
340
|
// Prefer example/ over example-something/
|
|
334
|
-
const
|
|
341
|
+
const an = al.slice(i).join('\0')
|
|
342
|
+
const bn = bl.slice(i).join('\0')
|
|
335
343
|
if (an < bn) return -1
|
|
336
344
|
if (an > bn) return 1
|
|
337
345
|
throw new Error('Unreachable')
|
|
338
346
|
}
|
|
339
347
|
|
|
340
|
-
|
|
348
|
+
export function sortPaths(a, b) {
|
|
349
|
+
return compareSegments(a.split('/'), b.split('/'))
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// `items` in sortPaths' order of `keyOf(item)`, each key split once, not at each of the sort's comparisons.
|
|
353
|
+
export const sortedByPath = (items, keyOf = (item) => item) => Array.from(items, (item) => ({ item, segments: keyOf(item).split('/') }))
|
|
354
|
+
.toSorted((a, b) => compareSegments(a.segments, b.segments))
|
|
355
|
+
.map(({ item }) => item)
|
|
356
|
+
|
|
357
|
+
const entryKey = ([key]) => key
|
|
341
358
|
|
|
342
359
|
export const isPlainObject = (x) => x && [null, Object.prototype].includes(Object.getPrototypeOf(x))
|
|
343
360
|
|
|
344
361
|
export const fromEntries = (entries) => Object.setPrototypeOf(Object.fromEntries(entries), null)
|
|
345
362
|
|
|
346
|
-
export const fileSetToObject = (set) =>
|
|
363
|
+
export const fileSetToObject = (set) => sortedByPath(set)
|
|
347
364
|
|
|
348
365
|
// `sorted: false` only for a machine-only payload whose reader does not care about ordering (shard.js).
|
|
349
366
|
export const fileMapToObject = (map, { sorted = true } = {}) => {
|
|
350
|
-
const entries = sorted ?
|
|
367
|
+
const entries = sorted ? sortedByPath(map, entryKey) : [...map]
|
|
351
368
|
return fromEntries(entries.map(([k, v]) => [k, v instanceof Map ? fileMapToObject(v, { sorted }) : v]))
|
|
352
369
|
}
|
|
353
370
|
|
|
@@ -431,6 +448,15 @@ export function parseFormats(json) {
|
|
|
431
448
|
return formats
|
|
432
449
|
}
|
|
433
450
|
|
|
451
|
+
// A payload-free `stat:*` record attests a path's kind alone: an artifact that also carries content
|
|
452
|
+
// for its path is malformed, rejected on read and never written, as nothing would hold those bytes
|
|
453
|
+
// (a reader taking the record as content-free skips them, another would serve or write them).
|
|
454
|
+
export function assertPayloadFree(formats, files, what) {
|
|
455
|
+
for (const [file, format] of formats) {
|
|
456
|
+
if (isStatFormat(format) && files.has(file)) assert(false, `${what}: '${file}' is a payload-free '${format}' record, yet carries content`)
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
434
460
|
// The `formats` an artifact writes: the same rule as on read, so an in-memory construct can't serialize
|
|
435
461
|
// what parse would reject; path-sorted.
|
|
436
462
|
export function serializeFormats(formats) {
|
|
@@ -490,7 +516,7 @@ export function groupModules(modules, { skipEmpty = false, metadata: written = t
|
|
|
490
516
|
if (skipEmpty && Object.keys(info.files).length === 0) continue
|
|
491
517
|
const inNodeModules = hasNodeModulesSegment(dir)
|
|
492
518
|
if (inNodeModules) assert(info.name && info.version && info.files)
|
|
493
|
-
const files = fromEntries(Object.entries(info.files)
|
|
519
|
+
const files = fromEntries(sortedByPath(Object.entries(info.files), entryKey))
|
|
494
520
|
grouped[inNodeModules ? 'modules' : 'sources'].push([dir, moduleInfo({
|
|
495
521
|
...info,
|
|
496
522
|
repo: metadata.includes('repo') ? normalizeModuleRepo(dir, info, what) : undefined,
|
|
@@ -498,11 +524,12 @@ export function groupModules(modules, { skipEmpty = false, metadata: written = t
|
|
|
498
524
|
files,
|
|
499
525
|
})])
|
|
500
526
|
}
|
|
501
|
-
return { modules: fromEntries(grouped.modules
|
|
527
|
+
return { modules: fromEntries(sortedByPath(grouped.modules, entryKey)), sources: fromEntries(sortedByPath(grouped.sources, entryKey)) }
|
|
502
528
|
}
|
|
503
529
|
|
|
504
530
|
// A target is a resolved-file string, or a { platform: file } Map under --metro.
|
|
505
|
-
|
|
531
|
+
// Whether two import targets are the same: a file, or a per-platform Map<platform, file> compared structurally.
|
|
532
|
+
export function importTargetsEqual(a, b) {
|
|
506
533
|
const aMap = a instanceof Map
|
|
507
534
|
if (aMap !== (b instanceof Map)) return false
|
|
508
535
|
if (!aMap) return a === b
|
package/src/brotli.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { constants as zlibConstants } from 'node:zlib'
|
|
1
|
+
import { brotliCompressSync, brotliDecompressSync, constants as zlibConstants } from 'node:zlib'
|
|
2
2
|
|
|
3
|
+
import { Bundle } from './bundle.js'
|
|
3
4
|
import { assert, isBrotliQuality, parseBrotliQuality } from './util.js'
|
|
4
5
|
|
|
5
|
-
// Default brotli quality;
|
|
6
|
-
export const DEFAULT_BROTLI_QUALITY =
|
|
6
|
+
// Default brotli quality; the top levels (10, 11) cost far more time for a marginal size gain.
|
|
7
|
+
export const DEFAULT_BROTLI_QUALITY = 6
|
|
7
8
|
|
|
8
9
|
export function brotliOptions(quality) {
|
|
9
10
|
if (quality !== undefined) {
|
|
@@ -18,3 +19,22 @@ export function brotliOptions(quality) {
|
|
|
18
19
|
}
|
|
19
20
|
return { params: { [zlibConstants.BROTLI_PARAM_QUALITY]: quality ?? DEFAULT_BROTLI_QUALITY } }
|
|
20
21
|
}
|
|
22
|
+
|
|
23
|
+
// zlib's brotli decoder stops at the end of the stream and ignores whatever follows it, where
|
|
24
|
+
// DecompressionStream refuses bytes after a gzip or deflate stream; this is Node's error for those.
|
|
25
|
+
export const trailingJunkError = () => Object.assign(
|
|
26
|
+
new TypeError('Trailing junk found after the end of the compressed stream'),
|
|
27
|
+
{ code: 'ERR_TRAILING_JUNK_AFTER_STREAM_END' })
|
|
28
|
+
|
|
29
|
+
// brotliDecompressSync(bytes), refusing bytes after the end of the brotli stream: the engine's
|
|
30
|
+
// bytesWritten counts the input it consumed, which stops where the stream does.
|
|
31
|
+
export function brotliDecompressStrict(bytes) {
|
|
32
|
+
const { buffer, engine } = brotliDecompressSync(bytes, { info: true })
|
|
33
|
+
if (engine.bytesWritten !== Buffer.byteLength(bytes)) throw trailingJunkError()
|
|
34
|
+
return buffer
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// A bundle file's bytes, of a serialized bundle's text (brotliOptions' `quality`), and the Bundle of
|
|
38
|
+
// those bytes, which are one brotli stream and nothing after it.
|
|
39
|
+
export const compressBundleText = (text, quality) => brotliCompressSync(text, brotliOptions(quality))
|
|
40
|
+
export const parseBundleBytes = (bytes) => Bundle.parse(brotliDecompressStrict(bytes).toString('utf8'))
|