@exodus/stasis-core 1.0.0-beta.2 → 1.0.0-beta.3
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 +1 -1
- package/bin/stasis-core.js +26 -30
- package/package.json +8 -1
- package/src/add.js +325 -0
- package/src/brotli.js +4 -12
- package/src/bundle-util.js +66 -0
- package/src/bundle.js +96 -91
- package/src/config.js +47 -122
- package/src/extract.js +159 -0
- package/src/fs.js +86 -347
- package/src/hooks.js +108 -498
- package/src/loader.js +1 -3
- package/src/lockfile.js +82 -68
- package/src/prune.js +37 -152
- package/src/shard.js +60 -0
- package/src/state-util.js +4 -26
- package/src/state.js +510 -1291
- package/src/util.js +534 -116
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# <img src="/stasis/logo.svg" alt="" width="39" height="39" valign="bottom" /> `@exodus/stasis-core`
|
|
2
2
|
|
|
3
|
-
A minimal subset of `@exodus/stasis` with just `run` and `prune` commands.
|
|
3
|
+
A minimal subset of `@exodus/stasis` with just the `run`, `add`, `extract`, and `prune` commands.
|
|
4
4
|
|
|
5
5
|
See main package [GitHub](https://github.com/ExodusOSS/stasis/tree/main/stasis) or [npm](https://npmjs.com/package/@exodus/stasis) for full README.
|
|
6
6
|
|
package/bin/stasis-core.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import { spawn } from 'node:child_process'
|
|
4
|
-
import { parseArgs } from 'node:util'
|
|
5
4
|
import { once } from 'node:events'
|
|
6
5
|
import { fileURLToPath } from 'node:url'
|
|
7
6
|
import { basename, resolve } from 'node:path'
|
|
8
7
|
import { existsSync, realpathSync } from 'node:fs'
|
|
9
8
|
import { constants as osConstants } from 'node:os'
|
|
10
9
|
import assert from 'node:assert/strict'
|
|
11
|
-
import { parseBrotliQuality } from '../src/util.js'
|
|
10
|
+
import { parseBrotliQuality, parseLeadingOptions } from '../src/util.js'
|
|
12
11
|
import pkg from '../package.json' with { type: 'json' }
|
|
13
12
|
|
|
14
13
|
const argv = [...process.argv]
|
|
@@ -16,12 +15,17 @@ assert(['node', 'node.exe'].includes(basename(argv.shift())))
|
|
|
16
15
|
assert(['node', 'node.exe'].includes(basename(process.argv0)))
|
|
17
16
|
|
|
18
17
|
const jsname = argv.shift()
|
|
19
|
-
const pathsEqual = (a, b) => a === b || (existsSync(a) && realpathSync(a) === b)
|
|
18
|
+
const pathsEqual = (a, b) => a === b || (existsSync(a) && realpathSync(a) === b)
|
|
20
19
|
assert(basename(jsname) === 'stasis-core' || pathsEqual(jsname, fileURLToPath(import.meta.url)))
|
|
21
20
|
|
|
22
21
|
function usage(prefix = '') {
|
|
23
22
|
console.error(`${prefix}\nUsage:
|
|
24
23
|
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] [--fs=(sync|async)] [--resources=ext,ext] [--brotli-quality=0..11] path/to/file.js ...
|
|
24
|
+
stasis-core add path/to/(file|dir) ...
|
|
25
|
+
(adds the listed files to the project's bundle(s) with no dependency resolution;
|
|
26
|
+
a directory expands to its files. Requires a stasis.config.json (all fields optional).)
|
|
27
|
+
stasis-core extract [--output=path/to/dir] path/to/bundle.stasis.code.br
|
|
28
|
+
(unpacks a stasis.code.br bundle back onto disk and derives a matching stasis.lock.json.)
|
|
25
29
|
stasis-core prune [path/to/project]
|
|
26
30
|
`.trim())
|
|
27
31
|
process.exit(1)
|
|
@@ -39,12 +43,6 @@ if (command === '-v' || command === '--version') {
|
|
|
39
43
|
console.log(`v${pkg.version}`)
|
|
40
44
|
process.exit(0)
|
|
41
45
|
} else if (command === 'run') {
|
|
42
|
-
const flags = []
|
|
43
|
-
const valueFlags = new Set(['--bundle', '--bundle-file', '--resources-bundle-file', '--lock', '--resources', '--brotli-quality'])
|
|
44
|
-
while (argv.length > 0 && (argv[0].startsWith('-') || valueFlags.has(flags.at(-1)))) {
|
|
45
|
-
flags.push(argv.shift())
|
|
46
|
-
}
|
|
47
|
-
|
|
48
46
|
const options = {
|
|
49
47
|
lock: { type: 'string', default: 'none' },
|
|
50
48
|
bundle: { type: 'string', default: 'none' },
|
|
@@ -57,13 +55,10 @@ if (command === '-v' || command === '--version') {
|
|
|
57
55
|
resources: { type: 'string' },
|
|
58
56
|
'brotli-quality': { type: 'string' },
|
|
59
57
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
} catch (cause) {
|
|
65
|
-
usage(`Error: ${cause.message}`)
|
|
66
|
-
}
|
|
58
|
+
const values = parseLeadingOptions(argv, options, {
|
|
59
|
+
valueFlags: ['--bundle', '--bundle-file', '--resources-bundle-file', '--lock', '--resources', '--brotli-quality'],
|
|
60
|
+
onError: usage,
|
|
61
|
+
})
|
|
67
62
|
if (argv.length === 0) usage('Nothing to run: no path to file given')
|
|
68
63
|
if (!['none', 'ignore', 'add', 'replace', 'frozen'].includes(values.lock)) usage('Error: invalid --lock value')
|
|
69
64
|
const lock = values.lock
|
|
@@ -74,23 +69,13 @@ if (command === '-v' || command === '--version') {
|
|
|
74
69
|
const debug = values.debug ? '1' : ''
|
|
75
70
|
if (!['none', 'ignore', 'add', 'replace', 'load', 'frozen'].includes(bundle)) usage('Error: invalid --bundle value')
|
|
76
71
|
if (bundleFile && bundle === 'none') usage('Error: --bundle-file requires --bundle=(add|replace|load|frozen|ignore)')
|
|
77
|
-
// Unlike --bundle-file (which is inert-but-harmless under --bundle=ignore, so Config allows
|
|
78
|
-
// it), --resources-bundle-file needs an active bundle: Config rejects bundle=none AND ignore
|
|
79
|
-
// (resourcesBundleFile requires an active bundle mode). Front-run that with a clean usage error.
|
|
80
72
|
if (resourcesBundleFile && (bundle === 'none' || bundle === 'ignore')) usage('Error: --resources-bundle-file requires --bundle=(add|replace|load|frozen)')
|
|
81
73
|
if (bundle === 'load' && lock !== 'frozen' && lock !== 'none' && lock !== 'ignore') usage('Error: --bundle=load is incompatible with --lock=(add|replace)')
|
|
82
74
|
if (lock === 'none' && bundle === 'none') usage('Error: stasis needs a lockfile or a bundle: set --lock or --bundle')
|
|
83
|
-
// --fs monkey-patches the fs readers (readFileSync/readdirSync + lstatSync/statSync)
|
|
84
|
-
// to capture them into the bundle (add|replace) or serve them from it (load); nothing
|
|
85
|
-
// to record/read without one. The mode argument is required: `sync` patches the sync
|
|
86
|
-
// readers, `async` adds their async (callback + fs.promises) counterparts on top.
|
|
87
75
|
if (values.fs !== undefined && !['sync', 'async'].includes(values.fs)) usage("Error: --fs must be 'sync' or 'async'")
|
|
88
76
|
if (values.fs !== undefined && !['add', 'replace', 'load'].includes(bundle)) usage('Error: --fs requires --bundle=(add|replace|load)')
|
|
89
77
|
const captureFs = values.fs ?? ''
|
|
90
|
-
// --resources: comma-separated extension/filename allowlist for `--fs` resource captures
|
|
91
|
-
// (e.g. png,svg,LICENSE). The child's Config validates each entry via parseResourcesOption.
|
|
92
78
|
const resources = values.resources ?? ''
|
|
93
|
-
// --brotli-quality: bundle compression quality (0..11; unset = brotli's default 11).
|
|
94
79
|
let brotliQuality
|
|
95
80
|
if (values['brotli-quality'] !== undefined) {
|
|
96
81
|
try {
|
|
@@ -99,8 +84,6 @@ if (command === '-v' || command === '--version') {
|
|
|
99
84
|
usage(`Error: ${cause.message}`)
|
|
100
85
|
}
|
|
101
86
|
}
|
|
102
|
-
// --child-process: forward forked-child (e.g. Metro transform worker) capture to the root
|
|
103
|
-
// via per-pid shards. Opt-in -- it stands up a process-coordination channel, off by default.
|
|
104
87
|
const childProcess = values['child-process'] ? '1' : ''
|
|
105
88
|
console.warn('[stasis-core] Running stasis with config:', { lock, scope, bundle, ...(bundleFile && { bundleFile }), ...(resourcesBundleFile && { resourcesBundleFile }), ...(childProcess && { childProcess: true }), ...(values.fs && { fs: values.fs }), ...(resources && { resources }), ...(brotliQuality !== undefined && { brotliQuality }) })
|
|
106
89
|
if (debug) console.warn(`[stasis-core] Warning: stasis debug mode active`)
|
|
@@ -113,8 +96,7 @@ if (command === '-v' || command === '--version') {
|
|
|
113
96
|
setEnv('EXODUS_STASIS_CHILD_PROCESS', childProcess)
|
|
114
97
|
setEnv('EXODUS_STASIS_FS', captureFs)
|
|
115
98
|
setEnv('EXODUS_STASIS_RESOURCES', resources)
|
|
116
|
-
// Only set when given
|
|
117
|
-
// to the child (an unconditional setEnv('') would reject it as a conflict).
|
|
99
|
+
// Only set when given: an unconditional setEnv('') would reject an ambient EXODUS_STASIS_BROTLI_QUALITY as a conflict.
|
|
118
100
|
if (brotliQuality !== undefined) setEnv('EXODUS_STASIS_BROTLI_QUALITY', String(brotliQuality))
|
|
119
101
|
const nodeArgs = ['--import', import.meta.resolve('../src/loader.js')]
|
|
120
102
|
const child = spawn(process.execPath, [...nodeArgs, ...argv], { stdio: 'inherit' })
|
|
@@ -127,6 +109,20 @@ if (command === '-v' || command === '--version') {
|
|
|
127
109
|
const { prune } = await import('../src/prune.js')
|
|
128
110
|
const { removed, validated, minimized } = prune({ root })
|
|
129
111
|
console.warn(`[stasis-core] prune: validated ${validated.length} file(s), removed ${removed.length} file(s), minimized ${minimized.length} package.json file(s)`)
|
|
112
|
+
} else if (command === 'add') {
|
|
113
|
+
if (argv.length === 0) usage('Nothing to add: no file given')
|
|
114
|
+
if (argv.some((a) => a.startsWith('-'))) usage('Error: add takes no options; its targets and resource allowlist come from stasis.config.json')
|
|
115
|
+
const { addCommand } = await import('../src/add.js')
|
|
116
|
+
addCommand({ cwd: process.cwd(), entries: argv, logLabel: 'stasis-core' })
|
|
117
|
+
} else if (command === 'extract') {
|
|
118
|
+
const options = {
|
|
119
|
+
output: { type: 'string', short: 'o' },
|
|
120
|
+
}
|
|
121
|
+
const values = parseLeadingOptions(argv, options, { valueFlags: ['--output', '-o'], onError: usage })
|
|
122
|
+
if (argv.length === 0) usage('Nothing to extract: no bundle file given')
|
|
123
|
+
if (argv.length > 1) usage('Error: extract takes exactly one bundle file')
|
|
124
|
+
const { extractCommand } = await import('../src/extract.js')
|
|
125
|
+
extractCommand({ cwd: process.cwd(), bundleFile: argv[0], output: values.output, logLabel: 'stasis-core' })
|
|
130
126
|
} else {
|
|
131
127
|
usage()
|
|
132
128
|
}
|
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.3",
|
|
4
4
|
"description": "Zero-dependency stasis runtime",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
"./state": "./src/state.js",
|
|
10
10
|
"./config": "./src/config.js",
|
|
11
11
|
"./bundle": "./src/bundle.js",
|
|
12
|
+
"./add": "./src/add.js",
|
|
13
|
+
"./bundle-util": "./src/bundle-util.js",
|
|
14
|
+
"./extract": "./src/extract.js",
|
|
12
15
|
"./lockfile": "./src/lockfile.js",
|
|
13
16
|
"./util": "./src/util.js",
|
|
14
17
|
"./brotli": "./src/brotli.js",
|
|
@@ -17,15 +20,19 @@
|
|
|
17
20
|
},
|
|
18
21
|
"files": [
|
|
19
22
|
"bin/stasis-core.js",
|
|
23
|
+
"src/add.js",
|
|
20
24
|
"src/brotli.js",
|
|
21
25
|
"src/bundle.js",
|
|
26
|
+
"src/bundle-util.js",
|
|
22
27
|
"src/config.js",
|
|
28
|
+
"src/extract.js",
|
|
23
29
|
"src/fs.js",
|
|
24
30
|
"src/hooks.js",
|
|
25
31
|
"src/loader.js",
|
|
26
32
|
"src/lockfile.js",
|
|
27
33
|
"src/package.cjs",
|
|
28
34
|
"src/prune.js",
|
|
35
|
+
"src/shard.js",
|
|
29
36
|
"src/state.js",
|
|
30
37
|
"src/state-util.js",
|
|
31
38
|
"src/util.js"
|
package/src/add.js
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { isUtf8 } from 'node:buffer'
|
|
2
|
+
import { existsSync, globSync, mkdirSync, readFileSync, realpathSync, statSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { basename, dirname, extname, join, relative, resolve } from 'node:path'
|
|
4
|
+
import { brotliCompressSync, brotliDecompressSync } from 'node:zlib'
|
|
5
|
+
|
|
6
|
+
import { Bundle } from './bundle.js'
|
|
7
|
+
import { Lockfile } from './lockfile.js'
|
|
8
|
+
import { brotliOptions } from './brotli.js'
|
|
9
|
+
import { findPackageMetadata, normalizeEntries, packageType, readJson } from './bundle-util.js'
|
|
10
|
+
import { canonicalizePath, sha512integrity } from './state-util.js'
|
|
11
|
+
import { assertRealPathWithinBase, classifyFormat, hasNodeModulesSegment, isAutoExcludedDir, isAutoExcludedFile, isBinaryPlist, isBrotliQuality, isExecutableMode, moduleFileKey, parseResourcesOption, pathExt, sortPaths, splitNodeModulesPath, toPosix } from './util.js'
|
|
12
|
+
|
|
13
|
+
const CONFIG_FILE = 'stasis.config.json'
|
|
14
|
+
const LOCK_FILE = 'stasis.lock.json'
|
|
15
|
+
|
|
16
|
+
// LIMITATION: a `.js`/`.ts` with no package `type` falls back to commonjs -- use `.mjs`/`.cjs` or set `type` when the module system matters.
|
|
17
|
+
function sourceFormat(absFile, content) {
|
|
18
|
+
const ext = extname(absFile).toLowerCase()
|
|
19
|
+
if (ext === '.js' || ext === '.ts') return `${packageType(absFile) ?? 'commonjs'}${ext === '.ts' ? '-typescript' : ''}`
|
|
20
|
+
return classifyFormat(absFile, { content }) ?? null
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const tally = (bundle) => {
|
|
24
|
+
let files = 0
|
|
25
|
+
let packages = 0
|
|
26
|
+
for (const m of bundle.modules.values()) {
|
|
27
|
+
const n = Object.keys(m.files).length
|
|
28
|
+
if (n > 0) {
|
|
29
|
+
files += n
|
|
30
|
+
packages += 1
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return { files, packages }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function readAddConfig(baseDir) {
|
|
37
|
+
const cfg = readJson(join(baseDir, CONFIG_FILE))
|
|
38
|
+
if (cfg === null) {
|
|
39
|
+
throw new Error(`add requires a ${CONFIG_FILE} (its bundleFile, resourcesBundleFile, and resources are all optional)`)
|
|
40
|
+
}
|
|
41
|
+
const resources = parseResourcesOption(CONFIG_FILE, cfg.resources)
|
|
42
|
+
if (cfg.brotliQuality !== undefined && !isBrotliQuality(cfg.brotliQuality)) {
|
|
43
|
+
throw new Error(`${CONFIG_FILE}: brotliQuality must be an integer 0..11 (got ${JSON.stringify(cfg.brotliQuality)})`)
|
|
44
|
+
}
|
|
45
|
+
const bundleFile = cfg.bundleFile ?? 'stasis.code.br'
|
|
46
|
+
const resourcesBundleFile = cfg.resourcesBundleFile
|
|
47
|
+
// Split targets must be distinct files (else they clobber on write); canonicalized so symlink and relative aliases are caught too.
|
|
48
|
+
if (resourcesBundleFile &&
|
|
49
|
+
canonicalizePath(resolve(baseDir, bundleFile)) === canonicalizePath(resolve(baseDir, resourcesBundleFile))) {
|
|
50
|
+
throw new Error(`${CONFIG_FILE}: bundleFile and resourcesBundleFile must name distinct paths (both resolve to ${resolve(baseDir, bundleFile)})`)
|
|
51
|
+
}
|
|
52
|
+
return { bundleFile, resourcesBundleFile, resources, brotliQuality: cfg.brotliQuality }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 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`.
|
|
56
|
+
function assembleBundle(baseDir, files, workspaceName, workspaceVersion) {
|
|
57
|
+
const modules = new Map()
|
|
58
|
+
const formats = new Map()
|
|
59
|
+
const executable = new Set()
|
|
60
|
+
const ensureBucket = (dir, name, version, ecosystem) => {
|
|
61
|
+
if (!modules.has(dir)) {
|
|
62
|
+
modules.set(dir, ecosystem === undefined
|
|
63
|
+
? { name, version, files: Object.create(null) }
|
|
64
|
+
: { name, version, ecosystem, files: Object.create(null) })
|
|
65
|
+
}
|
|
66
|
+
return modules.get(dir)
|
|
67
|
+
}
|
|
68
|
+
// Memoize per directory: findPackageMetadata's result depends only on the file's directory.
|
|
69
|
+
const metaByDir = new Map()
|
|
70
|
+
const metaFor = (rel) => {
|
|
71
|
+
const dir = dirname(rel)
|
|
72
|
+
if (!metaByDir.has(dir)) metaByDir.set(dir, findPackageMetadata(baseDir, rel))
|
|
73
|
+
return metaByDir.get(dir)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
for (const [rel, { content, format, executable: isExec }] of files) {
|
|
77
|
+
const meta = metaFor(rel)
|
|
78
|
+
const inNodeModules = splitNodeModulesPath(rel) !== null
|
|
79
|
+
if (meta) {
|
|
80
|
+
// A node_modules file whose nearest package.json is the workspace root is a misconfigured dep -- refuse.
|
|
81
|
+
if (inNodeModules && !hasNodeModulesSegment(meta.pkgDir)) {
|
|
82
|
+
throw new Error(`add: no package.json with name+version found for ${rel}`)
|
|
83
|
+
}
|
|
84
|
+
const relInBucket = meta.pkgDir === '.' ? rel : rel.slice(meta.pkgDir.length + 1)
|
|
85
|
+
const ecosystem = hasNodeModulesSegment(meta.pkgDir) ? 'npm' : undefined
|
|
86
|
+
ensureBucket(meta.pkgDir, meta.name, meta.version, ecosystem).files[relInBucket] = content
|
|
87
|
+
} else {
|
|
88
|
+
if (inNodeModules) throw new Error(`add: no package.json with name+version found for ${rel}`)
|
|
89
|
+
ensureBucket('.', workspaceName, workspaceVersion).files[rel] = content
|
|
90
|
+
}
|
|
91
|
+
formats.set(rel, format)
|
|
92
|
+
if (isExec) executable.add(rel)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return new Bundle({
|
|
96
|
+
config: { scope: 'full' },
|
|
97
|
+
entries: new Set(),
|
|
98
|
+
modules,
|
|
99
|
+
formats,
|
|
100
|
+
imports: new Map(),
|
|
101
|
+
executable,
|
|
102
|
+
}).withReason('add')
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Merge add-if-missing into any existing bundle (divergent bytes for an attested path throw -- see
|
|
106
|
+
// mergeModuleMaps), returning the write as a thunk so every target's conflicts surface before the
|
|
107
|
+
// first byte lands and an aborted `add` leaves the project untouched. Serializing INSIDE the thunk
|
|
108
|
+
// keeps peak memory at one target's bytes rather than every target's at once.
|
|
109
|
+
function prepareBundleFile(baseDir, targetPath, bundle, brotliQuality) {
|
|
110
|
+
const abs = resolve(baseDir, targetPath)
|
|
111
|
+
let mergedFrom
|
|
112
|
+
if (existsSync(abs)) {
|
|
113
|
+
let existing
|
|
114
|
+
try {
|
|
115
|
+
existing = Bundle.parse(brotliDecompressSync(readFileSync(abs)).toString('utf8'))
|
|
116
|
+
} catch (cause) {
|
|
117
|
+
throw new Error(`add: failed to read the existing bundle at ${targetPath}`, { cause })
|
|
118
|
+
}
|
|
119
|
+
mergedFrom = tally(existing).files
|
|
120
|
+
bundle = existing.merge(bundle)
|
|
121
|
+
}
|
|
122
|
+
const { files, packages } = tally(bundle)
|
|
123
|
+
return {
|
|
124
|
+
write: () => {
|
|
125
|
+
mkdirSync(dirname(abs), { recursive: true })
|
|
126
|
+
writeFileSync(abs, brotliCompressSync(bundle.serialize(), brotliOptions(brotliQuality)))
|
|
127
|
+
},
|
|
128
|
+
counts: { path: targetPath, total: files, packages, added: mergedFrom === undefined ? files : files - mergedFrom },
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Companion Lockfile: the bundle's shape with each file's content swapped for its on-disk integrity, so `--lock=frozen` matches.
|
|
133
|
+
function bundleToLockfile(bundle, integrities) {
|
|
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, { name: m.name, version: m.version, ...(m.ecosystem === undefined ? {} : { ecosystem: m.ecosystem }), files })
|
|
139
|
+
}
|
|
140
|
+
return new Lockfile({ config: bundle.config, entries: bundle.entries, modules, imports: bundle.imports, formats: bundle.formats, executable: bundle.executable })
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Merge into the project's stasis.lock.json (strict: divergent bytes throw), deferring the write like
|
|
144
|
+
// prepareBundleFile so a conflict here doesn't leave the bundles updated.
|
|
145
|
+
function prepareLockfile(lockPath, lockAdd) {
|
|
146
|
+
let existing
|
|
147
|
+
try {
|
|
148
|
+
existing = Lockfile.parse(readFileSync(lockPath, 'utf8'))
|
|
149
|
+
} catch (cause) {
|
|
150
|
+
throw new Error(`add: failed to read the existing ${LOCK_FILE}`, { cause })
|
|
151
|
+
}
|
|
152
|
+
const before = tally(existing).files
|
|
153
|
+
const merged = existing.merge(lockAdd)
|
|
154
|
+
const total = tally(merged).files
|
|
155
|
+
return { write: () => writeFileSync(lockPath, merged.serialize()), counts: { total, added: total - before } }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// GLOB + FILTER: expand each directory entry into the files under it (recursive glob), dropping the
|
|
159
|
+
// auto-excluded set from what a sweep FINDS -- dir rules on the segments BELOW the named root (`add
|
|
160
|
+
// src/examples` sweeps the dir it was pointed at, `add src` skips one it merely found), file rules
|
|
161
|
+
// by name plus the caller's `outputs` (this run's own write targets). A path the caller NAMED is
|
|
162
|
+
// always kept (naming is the override; a missing one keeps stats undefined so validation reports
|
|
163
|
+
// it), and inclusion is monotone: a nearer root's sweep can only ever see FEWER excluded segments,
|
|
164
|
+
// so a path any entry keeps stays kept -- `add src src/examples` includes the subtree it names in
|
|
165
|
+
// either order. `stats` is the walk's own stat, reused by validation. Gotcha: `**/*` skips dotfiles
|
|
166
|
+
// (.env, .git/...) -- name them explicitly.
|
|
167
|
+
function expandDirectories(baseDir, rels, outputs) {
|
|
168
|
+
const files = new Map() // rel -> stats
|
|
169
|
+
const excluded = new Set()
|
|
170
|
+
for (const rel of rels) {
|
|
171
|
+
const stats = statSync(join(baseDir, rel), { throwIfNoEntry: false })
|
|
172
|
+
if (stats === undefined || !stats.isDirectory()) {
|
|
173
|
+
files.set(rel, stats)
|
|
174
|
+
continue
|
|
175
|
+
}
|
|
176
|
+
const dirAbs = join(baseDir, rel)
|
|
177
|
+
const found = []
|
|
178
|
+
for (const match of globSync('**/*', { cwd: dirAbs })) {
|
|
179
|
+
const fileAbs = join(dirAbs, match)
|
|
180
|
+
// A dangling symlink stats as undefined -- skipped, not a hard ENOENT out of the walk.
|
|
181
|
+
const fileStats = statSync(fileAbs, { throwIfNoEntry: false })
|
|
182
|
+
if (fileStats?.isFile()) found.push([toPosix(relative(baseDir, fileAbs)), match, fileStats])
|
|
183
|
+
}
|
|
184
|
+
// Sorted because serialize() emits `reason` in insertion order (everything else it re-sorts):
|
|
185
|
+
// without this, the attested bundle's BYTES would depend on readdir order.
|
|
186
|
+
for (const [file, match, fileStats] of found.toSorted(([a], [b]) => sortPaths(a, b))) {
|
|
187
|
+
if (toPosix(match).split('/').slice(0, -1).some(isAutoExcludedDir)
|
|
188
|
+
|| outputs.has(file) || isAutoExcludedFile(file)) excluded.add(file)
|
|
189
|
+
else files.set(file, fileStats)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// A path any entry kept can't count as skipped -- another root's sweep may have re-flagged it.
|
|
193
|
+
for (const file of files.keys()) excluded.delete(file)
|
|
194
|
+
return { files, excluded }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const listPaths = (rels) => [...rels].toSorted(sortPaths).join(', ')
|
|
198
|
+
|
|
199
|
+
// One error for every file validation rejected, so a directory sweep names all of them at once
|
|
200
|
+
// instead of one per re-run. A lone offender keeps its exact standalone message.
|
|
201
|
+
function validationError({ missing, undeclared, nonUtf8 }) {
|
|
202
|
+
// `one`/`many` per category; empty categories contribute nothing.
|
|
203
|
+
const phrase = (items, one, many) => (items.length === 1 ? [one(items[0])] : items.length > 1 ? [many(items)] : [])
|
|
204
|
+
const named = ({ rel, format }) => `${rel} (format '${format}')`
|
|
205
|
+
const parts = [
|
|
206
|
+
...phrase(missing,
|
|
207
|
+
(rel) => `file not found: ${rel}`,
|
|
208
|
+
(rels) => `${rels.length} files not found: ${listPaths(rels)}`),
|
|
209
|
+
...phrase(undeclared,
|
|
210
|
+
(rel) => `${rel} is neither a recognized source file nor a declared resource; add its extension to "resources" in ${CONFIG_FILE}`,
|
|
211
|
+
(rels) => `${rels.length} files are neither recognized source files nor declared resources; add their extensions to "resources" in ${CONFIG_FILE}: ${listPaths(rels)}`),
|
|
212
|
+
...phrase(nonUtf8,
|
|
213
|
+
({ rel, format }) => `${rel} is not valid UTF-8 (format '${format}')`,
|
|
214
|
+
(bad) => `${bad.length} files are not valid UTF-8: ${bad.map(named).join(', ')}`),
|
|
215
|
+
]
|
|
216
|
+
return new Error(`add: ${parts.join('; ')}`)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// VALIDATE step: classify the WHOLE set -- each file recognized source or a declared resource --
|
|
220
|
+
// before a single target is touched, collecting every offender into one error. `files` is the glob
|
|
221
|
+
// step's rel -> stats map (stats undefined = not on disk). Containment is the one rule NOT collected:
|
|
222
|
+
// a symlink escaping the root is a security invariant, so it fails closed on the spot. Bytes are
|
|
223
|
+
// hashed only when there's a lockfile to receive the integrities (`withIntegrity`).
|
|
224
|
+
function validateFiles({ baseDir, realBase, files, resources, withIntegrity }) {
|
|
225
|
+
const codeFiles = new Map()
|
|
226
|
+
const resourceFiles = new Map()
|
|
227
|
+
const integrities = new Map()
|
|
228
|
+
const missing = []
|
|
229
|
+
const undeclared = []
|
|
230
|
+
const nonUtf8 = []
|
|
231
|
+
for (const [rel, stats] of files) {
|
|
232
|
+
if (stats === undefined) {
|
|
233
|
+
missing.push(rel)
|
|
234
|
+
continue
|
|
235
|
+
}
|
|
236
|
+
const executable = isExecutableMode(stats)
|
|
237
|
+
// Refuse a symlink whose realpath escapes the project root -- a bundle carries only in-tree bytes.
|
|
238
|
+
assertRealPathWithinBase(realBase, baseDir, rel)
|
|
239
|
+
const abs = join(baseDir, rel)
|
|
240
|
+
const buf = readFileSync(abs)
|
|
241
|
+
if (withIntegrity) integrities.set(rel, sha512integrity(buf))
|
|
242
|
+
const format = sourceFormat(abs, buf)
|
|
243
|
+
// 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).
|
|
244
|
+
if (format !== null && !isBinaryPlist(rel, buf)) {
|
|
245
|
+
// Source is stored as a UTF-8 string, so non-UTF-8 bytes would lossily diverge from the file on disk.
|
|
246
|
+
if (isUtf8(buf)) codeFiles.set(rel, { content: buf.toString('utf8'), format, executable })
|
|
247
|
+
else nonUtf8.push({ rel, format })
|
|
248
|
+
} else if (resources.has(pathExt(rel) || basename(rel).toLowerCase())) {
|
|
249
|
+
const utf8 = isUtf8(buf)
|
|
250
|
+
resourceFiles.set(rel, { content: utf8 ? buf.toString('utf8') : buf.toString('base64'), format: utf8 ? 'resource' : 'resource:base64', executable })
|
|
251
|
+
} else {
|
|
252
|
+
undeclared.push(rel)
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (missing.length + undeclared.length + nonUtf8.length > 0) throw validationError({ missing, undeclared, nonUtf8 })
|
|
256
|
+
return { codeFiles, resourceFiles, integrities }
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// GLOB the listed paths, FILTERING the auto-excluded set out of what a sweep finds (see
|
|
260
|
+
// expandDirectories) -> VALIDATE the whole surviving set -> only then ADD it to the target
|
|
261
|
+
// bundle(s), add-if-missing. A stasis.lock.json is updated only when one already exists.
|
|
262
|
+
export function addCommand({ cwd = process.cwd(), entries, logLabel = 'stasis-core' } = {}) {
|
|
263
|
+
if (!Array.isArray(entries) || entries.length === 0) {
|
|
264
|
+
throw new Error('add: at least one file is required')
|
|
265
|
+
}
|
|
266
|
+
const baseDir = resolve(cwd)
|
|
267
|
+
const realBase = realpathSync(baseDir)
|
|
268
|
+
const { bundleFile, resourcesBundleFile, resources, brotliQuality } = readAddConfig(baseDir)
|
|
269
|
+
|
|
270
|
+
const rootPkg = readJson(join(baseDir, 'package.json')) ?? {}
|
|
271
|
+
const workspaceName = rootPkg.name ?? 'workspace'
|
|
272
|
+
const workspaceVersion = rootPkg.version ?? '0.0.0'
|
|
273
|
+
|
|
274
|
+
// Lockfile updated only if one already exists (never created); hash packed bytes to match a frozen run.
|
|
275
|
+
const lockPath = join(baseDir, LOCK_FILE)
|
|
276
|
+
const hasLock = existsSync(lockPath)
|
|
277
|
+
|
|
278
|
+
// The CONFIGURED bundle targets, project-relative: a sweep must never attest an artifact this very
|
|
279
|
+
// run is writing, and a target can be named anything (`dist/code.br`), which isAutoExcludedFile's
|
|
280
|
+
// by-name rule can't recognize (it does cover the lockfile). One configured outside the project
|
|
281
|
+
// yields a '..' path, which no swept path can equal.
|
|
282
|
+
const outputs = new Set([bundleFile, resourcesBundleFile]
|
|
283
|
+
.filter((file) => file !== undefined)
|
|
284
|
+
.map((file) => toPosix(relative(baseDir, resolve(baseDir, file)))))
|
|
285
|
+
|
|
286
|
+
// `excluded` is counted in the summary below rather than dropped silently.
|
|
287
|
+
const { files, excluded } = expandDirectories(baseDir, normalizeEntries(entries, cwd), outputs)
|
|
288
|
+
if (files.size === 0) {
|
|
289
|
+
throw new Error(`add: no files to add (directory entries matched ${excluded.size === 0
|
|
290
|
+
? 'nothing' : `only auto-excluded files: ${listPaths(excluded)}`})`)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const { codeFiles, resourceFiles, integrities } = validateFiles({ baseDir, realBase, files, resources, withIntegrity: hasLock })
|
|
294
|
+
|
|
295
|
+
// ADD step: every target's merge is computed first, then the writes run -- see prepareBundleFile.
|
|
296
|
+
const summary = []
|
|
297
|
+
const writes = []
|
|
298
|
+
const planTarget = (target, entriesForTarget, kind) => {
|
|
299
|
+
if (entriesForTarget.size === 0) return
|
|
300
|
+
const { write, counts } = prepareBundleFile(baseDir, target, assembleBundle(baseDir, entriesForTarget, workspaceName, workspaceVersion), brotliQuality)
|
|
301
|
+
writes.push(write)
|
|
302
|
+
summary.push(`+${counts.added} ${kind} (${counts.total} total) -> ${counts.path}`)
|
|
303
|
+
}
|
|
304
|
+
if (resourcesBundleFile) {
|
|
305
|
+
planTarget(bundleFile, codeFiles, 'source')
|
|
306
|
+
planTarget(resourcesBundleFile, resourceFiles, 'resource')
|
|
307
|
+
} else {
|
|
308
|
+
const all = new Map([...codeFiles, ...resourceFiles])
|
|
309
|
+
const kind = codeFiles.size > 0 && resourceFiles.size > 0 ? 'file' : resourceFiles.size > 0 ? 'resource' : 'source'
|
|
310
|
+
planTarget(bundleFile, all, kind)
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// One lockfile attests both split targets, so merge in every packed file's integrity (code + resources together).
|
|
314
|
+
if (hasLock) {
|
|
315
|
+
const allFiles = new Map([...codeFiles, ...resourceFiles])
|
|
316
|
+
const lockAdd = bundleToLockfile(assembleBundle(baseDir, allFiles, workspaceName, workspaceVersion), integrities)
|
|
317
|
+
const { write, counts } = prepareLockfile(lockPath, lockAdd)
|
|
318
|
+
writes.push(write)
|
|
319
|
+
summary.push(`+${counts.added} (${counts.total} total) -> ${LOCK_FILE}`)
|
|
320
|
+
}
|
|
321
|
+
if (excluded.size > 0) summary.push(`skipped ${excluded.size} auto-excluded`)
|
|
322
|
+
|
|
323
|
+
for (const write of writes) write()
|
|
324
|
+
console.warn(`[${logLabel}] add: ${summary.join('; ')}`)
|
|
325
|
+
}
|
package/src/brotli.js
CHANGED
|
@@ -2,16 +2,9 @@ import { constants as zlibConstants } from 'node:zlib'
|
|
|
2
2
|
|
|
3
3
|
import { assert, isBrotliQuality, parseBrotliQuality } from './util.js'
|
|
4
4
|
|
|
5
|
-
//
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// env var -- which must agree when both are set; the output is still a valid .br
|
|
9
|
-
// at any quality. Production code pays the default cost for the best ratio.
|
|
10
|
-
//
|
|
11
|
-
// Kept out of `util.js` so that importing the generic helpers there (`sortPaths`,
|
|
12
|
-
// `assertRealPathWithinBase`, …) doesn't transitively load `node:zlib`. Only the
|
|
13
|
-
// code that actually compresses a bundle (`state.js`, `stasis bundle`) reaches
|
|
14
|
-
// for this, and that code already imports zlib directly.
|
|
5
|
+
// Default brotli quality; 11 (max) costs far more for a marginal gain over 9.
|
|
6
|
+
export const DEFAULT_BROTLI_QUALITY = 9
|
|
7
|
+
|
|
15
8
|
export function brotliOptions(quality) {
|
|
16
9
|
if (quality !== undefined) {
|
|
17
10
|
assert(isBrotliQuality(quality), `brotli quality must be an integer 0..11, got ${quality}`)
|
|
@@ -23,6 +16,5 @@ export function brotliOptions(quality) {
|
|
|
23
16
|
`brotli quality ${quality} conflicts with EXODUS_STASIS_BROTLI_QUALITY=${raw}`)
|
|
24
17
|
quality = n
|
|
25
18
|
}
|
|
26
|
-
|
|
27
|
-
return { params: { [zlibConstants.BROTLI_PARAM_QUALITY]: quality } }
|
|
19
|
+
return { params: { [zlibConstants.BROTLI_PARAM_QUALITY]: quality ?? DEFAULT_BROTLI_QUALITY } }
|
|
28
20
|
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { isUtf8 } from 'node:buffer'
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
3
|
+
import { dirname, isAbsolute, join, relative, resolve } from 'node:path'
|
|
4
|
+
import { findPackageJSON } from 'node:module'
|
|
5
|
+
import { pathToFileURL } from 'node:url'
|
|
6
|
+
|
|
7
|
+
import { assertRealPathWithinBase, toPosix } from './util.js'
|
|
8
|
+
|
|
9
|
+
export function packageType(file) {
|
|
10
|
+
const pkg = findPackageJSON(pathToFileURL(file).toString())
|
|
11
|
+
if (!pkg) return null
|
|
12
|
+
try {
|
|
13
|
+
const type = JSON.parse(readFileSync(pkg, 'utf8')).type
|
|
14
|
+
return type === 'module' || type === 'commonjs' ? type : null
|
|
15
|
+
} catch {
|
|
16
|
+
return null
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Nearest package.json (walking up) with both name and version; pkgDir is relative to baseDir ("." at the root). Null if none.
|
|
21
|
+
export function findPackageMetadata(baseDir, fileRelPath) {
|
|
22
|
+
let dir = dirname(fileRelPath)
|
|
23
|
+
while (true) {
|
|
24
|
+
const pkgPath = join(baseDir, dir, 'package.json')
|
|
25
|
+
if (existsSync(pkgPath)) {
|
|
26
|
+
try {
|
|
27
|
+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'))
|
|
28
|
+
if (pkg.name && pkg.version) return { pkgDir: dir, name: pkg.name, version: pkg.version }
|
|
29
|
+
} catch { /* malformed -- keep walking */ }
|
|
30
|
+
}
|
|
31
|
+
if (dir === '.' || dir === '/' || dir === '') return null
|
|
32
|
+
const parent = dirname(dir)
|
|
33
|
+
if (parent === dir) return null
|
|
34
|
+
dir = parent
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function normalizeEntries(entries, cwd) {
|
|
39
|
+
const baseDir = resolve(cwd)
|
|
40
|
+
return entries.map((e) => {
|
|
41
|
+
const abs = resolve(cwd, e)
|
|
42
|
+
const rel = toPosix(relative(baseDir, abs))
|
|
43
|
+
// On Windows path.relative() returns an absolute path across drives (no leading '..'), so reject that form too.
|
|
44
|
+
if (rel.startsWith('..') || isAbsolute(rel)) throw new Error(`Entry escapes baseDir: ${e}`)
|
|
45
|
+
return rel.replace(/^\.\//u, '')
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Bytes of a bundled module's `package.json`, or null to skip when it's absent on disk; non-UTF-8 aborts (never silently skipped).
|
|
50
|
+
export function readModuleManifest({ baseDir, realBase, rel } = {}) {
|
|
51
|
+
const absolute = join(baseDir, rel)
|
|
52
|
+
if (!existsSync(absolute)) return null
|
|
53
|
+
assertRealPathWithinBase(realBase, baseDir, rel)
|
|
54
|
+
const buf = readFileSync(absolute)
|
|
55
|
+
if (!isUtf8(buf)) throw new Error(`package.json is not valid UTF-8: ${rel}`)
|
|
56
|
+
return buf
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Never throws: a missing/unreadable/malformed file yields null.
|
|
60
|
+
export function readJson(file) {
|
|
61
|
+
try {
|
|
62
|
+
return JSON.parse(readFileSync(file, 'utf8'))
|
|
63
|
+
} catch {
|
|
64
|
+
return null
|
|
65
|
+
}
|
|
66
|
+
}
|