@exodus/stasis-core 1.0.0-beta.9 → 1.0.0-rc.1
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 +6 -2
- package/src/add.js +161 -157
- package/src/artifact-util.js +319 -221
- package/src/brotli.js +21 -6
- package/src/bundle-util.js +142 -100
- package/src/bundle.js +89 -128
- package/src/cli.js +123 -0
- package/src/config.js +74 -87
- package/src/extract.js +140 -67
- package/src/fs.js +57 -106
- package/src/hooks.js +126 -238
- package/src/host.js +9 -1
- package/src/loader.js +1 -1
- package/src/lockfile.js +58 -74
- package/src/policy.js +222 -0
- package/src/prune.js +60 -114
- package/src/shard.js +10 -25
- package/src/state-util.js +24 -9
- package/src/state.js +722 -865
- package/src/util.js +123 -172
package/src/add.js
CHANGED
|
@@ -1,195 +1,183 @@
|
|
|
1
1
|
import { isUtf8 } from 'node:buffer'
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import { brotliCompressSync, brotliDecompressSync } from 'node:zlib'
|
|
2
|
+
import * as fs from 'node:fs'
|
|
3
|
+
import { dirname, extname, join, 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
|
+
import { linksOn, parsePolicy, recordedIn } from './policy.js'
|
|
10
11
|
import { canonicalizePath, sha512integrity } from './state-util.js'
|
|
11
|
-
import { assertRealPathWithinBase, classifyFormat, hasNodeModulesSegment, isAutoExcludedDir, isAutoExcludedFile, isBinaryPlist, isBrotliQuality, isExecutableMode,
|
|
12
|
+
import { assertRealPathWithinBase, attestsAny, BUNDLE_FILE, classifyFormat, CONFIG_FILE, extraPattern, fileInBucket, hasNodeModulesSegment, heldToAccounts, inResources, installedAccounts, isAutoExcludedDir, isAutoExcludedFile, isBinaryPlist, isBrotliQuality, isExecutableMode, LOCK_FILE, memoized, moduleInfo, parseResourcesOption, sortedByPath, splitNodeModulesPath, toPosix, withAttestedOf, withMetadataOf } from './util.js'
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
const
|
|
14
|
+
// Snapshotted before `stasis run --fs` patches node:fs: outputMatcher must read the real disk, not be captured.
|
|
15
|
+
const { existsSync, mkdirSync, readFileSync, realpathSync, statSync, writeFileSync } = fs
|
|
15
16
|
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
// null for a JS-family file (`.jsx`/`.tsx`: source attested with no loader format, as the runtime
|
|
19
|
-
// records one), undefined when unrecognized.
|
|
20
|
-
function sourceFormat(absFile, content) {
|
|
17
|
+
// LIMITATION: a `.js`/`.ts` in a package with no `type` is commonjs; use `.mjs`/`.cjs` or set it.
|
|
18
|
+
function sourceFormat(absFile, content, host, kept) {
|
|
21
19
|
const ext = extname(absFile).toLowerCase()
|
|
22
|
-
if (ext === '.js' || ext === '.ts') return `${packageType(absFile) ?? 'commonjs'}${ext === '.ts' ? '-typescript' : ''}`
|
|
20
|
+
if (ext === '.js' || ext === '.ts') return `${packageType(absFile, host, kept) ?? 'commonjs'}${ext === '.ts' ? '-typescript' : ''}`
|
|
23
21
|
return classifyFormat(absFile, { content })
|
|
24
22
|
}
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
const tally = (artifact) => {
|
|
28
|
-
let files = 0
|
|
29
|
-
let packages = 0
|
|
30
|
-
for (const m of artifact.modules.values()) {
|
|
31
|
-
const n = Object.keys(m.files).length
|
|
32
|
-
if (n > 0) {
|
|
33
|
-
files += n
|
|
34
|
-
packages += 1
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return { files, packages }
|
|
38
|
-
}
|
|
24
|
+
const fileCount = (artifact) => artifact.modules.values().reduce((n, { files }) => n + Object.keys(files).length, 0)
|
|
39
25
|
|
|
40
26
|
function readAddConfig(baseDir) {
|
|
41
27
|
const cfg = readJson(join(baseDir, CONFIG_FILE))
|
|
42
|
-
if (cfg === null) {
|
|
43
|
-
throw new Error(`add requires a ${CONFIG_FILE} (its bundleFile, resourcesBundleFile, and resources are all optional)`)
|
|
44
|
-
}
|
|
28
|
+
if (cfg === null) throw new Error(`add requires a ${CONFIG_FILE} (its bundleFile, resourcesBundleFile, and resources are all optional)`)
|
|
45
29
|
const resources = parseResourcesOption(CONFIG_FILE, cfg.resources)
|
|
46
30
|
if (cfg.brotliQuality !== undefined && !isBrotliQuality(cfg.brotliQuality)) {
|
|
47
31
|
throw new Error(`${CONFIG_FILE}: brotliQuality must be an integer 0..11 (got ${JSON.stringify(cfg.brotliQuality)})`)
|
|
48
32
|
}
|
|
49
|
-
const bundleFile = cfg.bundleFile ??
|
|
33
|
+
const bundleFile = cfg.bundleFile ?? BUNDLE_FILE
|
|
50
34
|
const resourcesBundleFile = cfg.resourcesBundleFile
|
|
51
|
-
//
|
|
35
|
+
// Distinct, else they clobber on write; canonicalized so symlink and relative aliases are caught too.
|
|
52
36
|
if (resourcesBundleFile &&
|
|
53
37
|
canonicalizePath(resolve(baseDir, bundleFile)) === canonicalizePath(resolve(baseDir, resourcesBundleFile))) {
|
|
54
38
|
throw new Error(`${CONFIG_FILE}: bundleFile and resourcesBundleFile must name distinct paths (both resolve to ${resolve(baseDir, bundleFile)})`)
|
|
55
39
|
}
|
|
56
|
-
return { bundleFile, resourcesBundleFile, resources, brotliQuality: cfg.brotliQuality }
|
|
40
|
+
return { bundleFile, resourcesBundleFile, resources, brotliQuality: cfg.brotliQuality, policy: parsePolicy(CONFIG_FILE, cfg) }
|
|
57
41
|
}
|
|
58
42
|
|
|
59
|
-
//
|
|
60
|
-
function assembleBundle(baseDir, files, workspaceName, workspaceVersion, repo) {
|
|
43
|
+
// With NO entries: `add` attests files, not entry points, so nothing in it runs via `--bundle=load`.
|
|
44
|
+
export function assembleBundle(baseDir, files, workspaceName, workspaceVersion, repo, { reason = 'add', buckets = new Map(), host = diskHost } = {}) {
|
|
61
45
|
const modules = new Map()
|
|
62
46
|
const formats = new Map()
|
|
63
47
|
const executable = new Set()
|
|
64
|
-
|
|
65
|
-
const bucketFiles = (dir, identity) => {
|
|
66
|
-
if (!modules.has(dir)) modules.set(dir, moduleInfo({ ...identity, files: Object.create(null) }))
|
|
67
|
-
return modules.get(dir).files
|
|
68
|
-
}
|
|
69
|
-
// findPackageMetadata's result depends only on the file's directory.
|
|
70
|
-
const metaByDir = new Map()
|
|
71
|
-
const metaFor = (rel) => {
|
|
72
|
-
const dir = dirname(rel)
|
|
73
|
-
if (!metaByDir.has(dir)) metaByDir.set(dir, findPackageMetadata(baseDir, rel))
|
|
74
|
-
return metaByDir.get(dir)
|
|
75
|
-
}
|
|
48
|
+
const metaFor = packageLookup(baseDir, { host })
|
|
76
49
|
|
|
77
50
|
for (const [rel, { content, format, executable: isExec }] of files) {
|
|
78
51
|
const meta = metaFor(rel)
|
|
79
|
-
// A node_modules file whose nearest package.json is the workspace root (or none at all) is a misconfigured dep -- refuse.
|
|
80
52
|
if (splitNodeModulesPath(rel) !== null && !(meta && hasNodeModulesSegment(meta.pkgDir))) {
|
|
81
53
|
throw new Error(`add: no package.json with name+version found for ${rel}`)
|
|
82
54
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
|
|
55
|
+
const dir = meta ? meta.pkgDir : '.'
|
|
56
|
+
const identity = buckets.get(dir) ?? meta ?? { name: workspaceName, version: workspaceVersion }
|
|
57
|
+
const bucket = modules.get(dir)
|
|
58
|
+
const info = bucket === undefined ? moduleInfo({ ...identity, files: Object.create(null) }) : withMetadataOf(bucket, identity)
|
|
59
|
+
modules.set(dir, info)
|
|
60
|
+
info.files[fileInBucket(dir, rel)] = content
|
|
89
61
|
if (format != null) formats.set(rel, format)
|
|
90
62
|
if (isExec) executable.add(rel)
|
|
91
63
|
}
|
|
92
64
|
|
|
93
|
-
return new Bundle({ config: { scope: 'full' }, entries: new Set(), modules, formats, imports: new Map(), executable, repo }).withReason(
|
|
65
|
+
return new Bundle({ config: { scope: 'full' }, entries: new Set(), modules, formats, imports: new Map(), executable, repo }).withReason(reason)
|
|
94
66
|
}
|
|
95
67
|
|
|
96
|
-
//
|
|
97
|
-
//
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
throw new Error(`add: failed to read the existing bundle at ${targetPath}`, { cause })
|
|
68
|
+
// Each artifact's attested packages are held to the others' and to the install. The attest(existing, added, companion)
|
|
69
|
+
// returned has `added` attest as `existing`, else its split half `companion`, the others, the install.
|
|
70
|
+
function heldToInstall(baseDir, artifacts) {
|
|
71
|
+
const known = new Map()
|
|
72
|
+
for (const [path, artifact] of artifacts) {
|
|
73
|
+
const attested = new Map([...artifact.modules].filter(([, info]) => attestsAny(info)))
|
|
74
|
+
for (const [dir, info] of attested) known.set(dir, known.has(dir) ? withMetadataOf(known.get(dir), info, `${path}: module '${dir}' (against the other artifacts)`) : info)
|
|
75
|
+
heldToAccounts(attested, installedAccounts(attested, baseDir), `${path}: `)
|
|
76
|
+
}
|
|
77
|
+
return (existing, added, companion) => {
|
|
78
|
+
for (const [dir, account] of installedAccounts(added.modules, baseDir)) {
|
|
79
|
+
if (!account.locked) added.modules.set(dir, withAttestedOf(added.modules.get(dir), existing?.modules.get(dir) ?? companion?.modules.get(dir) ?? known.get(dir) ?? account))
|
|
109
80
|
}
|
|
110
|
-
|
|
81
|
+
return added
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function existingOf(what, parse) {
|
|
86
|
+
try {
|
|
87
|
+
return parse()
|
|
88
|
+
} catch (cause) {
|
|
89
|
+
throw new Error(`add: failed to read the existing ${what}`, { cause })
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// A thunk, so every target's conflicts surface before a byte lands; serializing in it keeps peak memory at one target's.
|
|
94
|
+
function prepareBundleFile(baseDir, targetPath, bundle, brotliQuality, check, existing) {
|
|
95
|
+
check?.(bundle)
|
|
96
|
+
const abs = resolve(baseDir, targetPath)
|
|
97
|
+
let before = 0
|
|
98
|
+
if (existing) {
|
|
99
|
+
check?.(existing, recordedIn(targetPath))
|
|
100
|
+
before = fileCount(existing)
|
|
111
101
|
bundle = existing.merge(bundle)
|
|
112
102
|
}
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
writeFileSync(abs, brotliCompressSync(bundle.serialize(), brotliOptions(brotliQuality)))
|
|
118
|
-
},
|
|
119
|
-
counts: { path: targetPath, total: files, packages, added: mergedFrom === undefined ? files : files - mergedFrom },
|
|
103
|
+
const total = fileCount(bundle)
|
|
104
|
+
const write = () => {
|
|
105
|
+
mkdirSync(dirname(abs), { recursive: true })
|
|
106
|
+
writeFileSync(abs, compressBundleText(bundle.serialize(), brotliQuality))
|
|
120
107
|
}
|
|
108
|
+
return { write, total, added: total - before }
|
|
121
109
|
}
|
|
122
110
|
|
|
123
|
-
//
|
|
124
|
-
function
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
111
|
+
// By name, or dev+inode (a link to one, which a write rewrites in place), confirmed in bigint: a number rounds big inodes.
|
|
112
|
+
function outputMatcher(baseDir, outputs) {
|
|
113
|
+
const exact = new Set()
|
|
114
|
+
const rounded = new Set()
|
|
115
|
+
for (const rel of outputs) {
|
|
116
|
+
const stats = statSync(join(baseDir, rel), { throwIfNoEntry: false, bigint: true })
|
|
117
|
+
if (stats === undefined) continue
|
|
118
|
+
exact.add(`${stats.dev}:${stats.ino}`)
|
|
119
|
+
rounded.add(`${Number(stats.dev)}:${Number(stats.ino)}`)
|
|
120
|
+
}
|
|
121
|
+
return (rel, stats) => {
|
|
122
|
+
if (outputs.has(rel)) return true
|
|
123
|
+
if (stats === undefined || !rounded.has(`${stats.dev}:${stats.ino}`)) return false
|
|
124
|
+
const precise = statSync(join(baseDir, rel), { throwIfNoEntry: false, bigint: true })
|
|
125
|
+
return precise !== undefined && exact.has(`${precise.dev}:${precise.ino}`)
|
|
130
126
|
}
|
|
131
|
-
return new Lockfile({ config: bundle.config, entries: bundle.entries, modules, imports: bundle.imports, formats: bundle.formats, executable: bundle.executable })
|
|
132
127
|
}
|
|
133
128
|
|
|
134
|
-
//
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
existing = Lockfile.parse(readFileSync(lockPath, 'utf8'))
|
|
140
|
-
} catch (cause) {
|
|
141
|
-
throw new Error(`add: failed to read the existing ${LOCK_FILE}`, { cause })
|
|
129
|
+
// Node leaves out a path `exclude` takes only where its `**` meets it, so it is asked of each '/'-joined project path.
|
|
130
|
+
function globPaths(host, pattern, cwd, baseDir, exclude) {
|
|
131
|
+
const projectPath = (path) => {
|
|
132
|
+
const rel = relative(baseDir, path)
|
|
133
|
+
return sep === '\\' ? rel.replaceAll('\\', '/') : rel
|
|
142
134
|
}
|
|
143
|
-
|
|
144
|
-
const merged = existing.merge(lockAdd)
|
|
145
|
-
const total = tally(merged).files
|
|
146
|
-
return { write: () => writeFileSync(lockPath, merged.serialize()), counts: { total, added: total - before } }
|
|
135
|
+
return host.glob(pattern, { cwd, exclude: exclude && ((path) => exclude(projectPath(path))) })
|
|
147
136
|
}
|
|
148
137
|
|
|
149
|
-
//
|
|
150
|
-
//
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
// always kept (naming is the override; a missing one keeps stats undefined so validation reports
|
|
154
|
-
// it), and inclusion is monotone: a nearer root's sweep can only ever see FEWER excluded segments,
|
|
155
|
-
// so a path any entry keeps stays kept -- `add src src/examples` includes the subtree it names in
|
|
156
|
-
// either order. `stats` is the walk's own stat, reused by validation. Gotcha: `**/*` skips dotfiles
|
|
157
|
-
// (.env, .git/...) -- name them explicitly.
|
|
158
|
-
function expandDirectories(baseDir, rels, outputs) {
|
|
159
|
-
const files = new Map() // rel -> stats
|
|
138
|
+
// A sweep drops what it FINDS auto-excluded: dir rules below the named root (`add src/examples` sweeps it, `add src`
|
|
139
|
+
// skips it), file rules by name and to `outputs`. A path `exclude` takes goes, named or swept, with all under it.
|
|
140
|
+
function expandDirectories(baseDir, rels, outputs, { keepNamedOutputs = true, exclude, host = diskHost } = {}) {
|
|
141
|
+
const files = new Map() // rel -> stats, the walk's own, reused by validation
|
|
160
142
|
const excluded = new Set()
|
|
143
|
+
const isOutput = outputMatcher(baseDir, outputs)
|
|
161
144
|
for (const rel of rels) {
|
|
145
|
+
// A walk meets a directory before what's under it, so it checks each path alone; a named one, its directories too.
|
|
146
|
+
if (exclude !== undefined && rel.split('/').some((_, i, segments) => exclude(segments.slice(0, i + 1).join('/')))) continue
|
|
162
147
|
const dirAbs = join(baseDir, rel)
|
|
163
|
-
const stats =
|
|
148
|
+
const stats = statStrict(host, dirAbs, rel) ?? undefined
|
|
149
|
+
// A NAMED path is kept (naming is the override; a missing one keeps undefined stats for validation to report).
|
|
164
150
|
if (stats === undefined || !stats.isDirectory()) {
|
|
165
|
-
files.set(rel, stats)
|
|
151
|
+
if (keepNamedOutputs || !isOutput(rel, stats)) files.set(rel, stats)
|
|
166
152
|
continue
|
|
167
153
|
}
|
|
168
154
|
const found = []
|
|
169
|
-
|
|
155
|
+
// Gotcha: `**/*` skips dotfiles (.env, .git/...); they must be named explicitly.
|
|
156
|
+
for (const match of globPaths(host, '**/*', dirAbs, baseDir, exclude)) {
|
|
170
157
|
const fileAbs = join(dirAbs, match)
|
|
158
|
+
const fileRel = relative(baseDir, fileAbs)
|
|
171
159
|
// A dangling symlink stats as undefined -- skipped, not a hard ENOENT out of the walk.
|
|
172
|
-
const fileStats =
|
|
173
|
-
if (fileStats?.isFile()) found.push([toPosix(
|
|
160
|
+
const fileStats = statStrict(host, fileAbs, fileRel) ?? undefined
|
|
161
|
+
if (fileStats?.isFile()) found.push([toPosix(fileRel), match, fileStats])
|
|
174
162
|
}
|
|
175
|
-
// Sorted
|
|
176
|
-
|
|
177
|
-
|
|
163
|
+
// Sorted, whatever the readdir order, so multi-file diagnostics keep a stable order.
|
|
164
|
+
for (const [file, match, fileStats] of sortedByPath(found, ([path]) => path)) {
|
|
165
|
+
// Node's glob holds `exclude` to its `**` alone: a file below the directory swept still matches the `*` after it.
|
|
166
|
+
if (exclude?.(file)) continue
|
|
178
167
|
if (toPosix(match).split('/').slice(0, -1).some(isAutoExcludedDir)
|
|
179
|
-
||
|
|
168
|
+
|| isOutput(file, fileStats) || isAutoExcludedFile(file)) excluded.add(file)
|
|
180
169
|
else files.set(file, fileStats)
|
|
181
170
|
}
|
|
182
171
|
}
|
|
183
|
-
//
|
|
172
|
+
// Inclusion is monotone: a path any entry kept stays kept, so `add src src/examples` keeps the subtree in either order.
|
|
184
173
|
for (const file of files.keys()) excluded.delete(file)
|
|
185
174
|
return { files, excluded }
|
|
186
175
|
}
|
|
187
176
|
|
|
188
|
-
const listPaths = (rels) =>
|
|
177
|
+
const listPaths = (rels) => sortedByPath(rels).join(', ')
|
|
189
178
|
|
|
190
|
-
// One error for every file validation rejected, so a
|
|
191
|
-
|
|
192
|
-
function validationError({ missing, undeclared, nonUtf8 }) {
|
|
179
|
+
// One error for every file validation rejected, so a sweep names all of them at once; a lone offender keeps its own message.
|
|
180
|
+
export function validationError({ missing, undeclared, nonUtf8 }, label) {
|
|
193
181
|
const phrase = (items, one, many) => (items.length === 1 ? [one(items[0])] : items.length > 1 ? [many(items)] : [])
|
|
194
182
|
const withFormat = (format) => (format == null ? '' : ` (format '${format}')`)
|
|
195
183
|
const named = ({ rel, format }) => `${rel}${withFormat(format)}`
|
|
@@ -204,76 +192,81 @@ function validationError({ missing, undeclared, nonUtf8 }) {
|
|
|
204
192
|
({ rel, format }) => `${rel} is not valid UTF-8${withFormat(format)}`,
|
|
205
193
|
(bad) => `${bad.length} files are not valid UTF-8: ${bad.map(named).join(', ')}`),
|
|
206
194
|
]
|
|
207
|
-
return new Error(
|
|
195
|
+
return new Error(`${label}: ${parts.join('; ')}`)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// As a shell expands `stasis add`'s arguments: Node's glob (its `*` skips dotfiles), a pattern matching nothing kept as
|
|
199
|
+
// itself (no nullglob: a missing path is reported), then each path as `add` takes it, but one of `outputs`.
|
|
200
|
+
export function expandExtra(baseDir, patterns, outputs, host = diskHost) {
|
|
201
|
+
// A `!` entry takes out every match and all under it (never walked), in any entry order.
|
|
202
|
+
const exclusions = patterns.filter((pattern) => pattern.startsWith('!')).map(extraPattern)
|
|
203
|
+
// Memoized: a walk asks of each path, a sweep again, and a named path of every directory above it.
|
|
204
|
+
const exclude = exclusions.length === 0 ? undefined : memoized((rel) => exclusions.some((pattern) => host.matchesGlob(rel, pattern)))
|
|
205
|
+
const entries = patterns.filter((pattern) => !pattern.startsWith('!')).flatMap((pattern) => {
|
|
206
|
+
const matches = globPaths(host, pattern, baseDir, baseDir, exclude)
|
|
207
|
+
return matches.length === 0 ? [pattern] : sortedByPath(matches)
|
|
208
|
+
})
|
|
209
|
+
return expandDirectories(baseDir, normalizeEntries(entries, baseDir), outputs, { keepNamedOutputs: false, exclude, host })
|
|
208
210
|
}
|
|
209
211
|
|
|
210
|
-
//
|
|
211
|
-
|
|
212
|
-
// step's rel -> stats map (stats undefined = not on disk). Containment is the one rule NOT collected:
|
|
213
|
-
// a symlink escaping the root is a security invariant, so it fails closed on the spot. Bytes are
|
|
214
|
-
// hashed only when there's a lockfile to receive the integrities (`withIntegrity`).
|
|
215
|
-
function validateFiles({ baseDir, realBase, files, resources, withIntegrity }) {
|
|
212
|
+
// The WHOLE set is classified before any target is touched; only a symlink escaping the root fails on the spot.
|
|
213
|
+
export function validateFiles({ baseDir, realBase, files, resources, withIntegrity, label = 'add', host = diskHost }) {
|
|
216
214
|
const codeFiles = new Map()
|
|
217
215
|
const resourceFiles = new Map()
|
|
218
216
|
const integrities = new Map()
|
|
219
217
|
const missing = []
|
|
220
218
|
const undeclared = []
|
|
221
219
|
const nonUtf8 = []
|
|
220
|
+
const types = new Map()
|
|
221
|
+
const kept = (path, read) => (types.has(path) ? types.get(path) : types.set(path, read()).get(path))
|
|
222
222
|
for (const [rel, stats] of files) {
|
|
223
223
|
if (stats === undefined) {
|
|
224
224
|
missing.push(rel)
|
|
225
225
|
continue
|
|
226
226
|
}
|
|
227
227
|
const executable = isExecutableMode(stats)
|
|
228
|
-
assertRealPathWithinBase(realBase, baseDir, rel)
|
|
228
|
+
assertRealPathWithinBase(realBase, baseDir, rel, host)
|
|
229
229
|
const abs = join(baseDir, rel)
|
|
230
|
-
const buf =
|
|
230
|
+
const buf = host.readFile(abs)
|
|
231
231
|
if (withIntegrity) integrities.set(rel, sha512integrity(buf))
|
|
232
|
-
const format = sourceFormat(abs, buf)
|
|
233
|
-
// A binary plist can't be stored as the UTF-8 string its 'xml' format implies
|
|
232
|
+
const format = sourceFormat(abs, buf, host, kept)
|
|
233
|
+
// A binary plist can't be stored as the UTF-8 string its 'xml' format implies: a resource (opaque base64).
|
|
234
234
|
if (format !== undefined && !isBinaryPlist(rel, buf)) {
|
|
235
235
|
// Source is stored as a UTF-8 string, so non-UTF-8 bytes would lossily diverge from the file on disk.
|
|
236
236
|
if (isUtf8(buf)) codeFiles.set(rel, { content: buf.toString('utf8'), format, executable })
|
|
237
237
|
else nonUtf8.push({ rel, format })
|
|
238
|
-
} else if (
|
|
238
|
+
} else if (inResources(rel, resources)) {
|
|
239
239
|
const utf8 = isUtf8(buf)
|
|
240
240
|
resourceFiles.set(rel, { content: utf8 ? buf.toString('utf8') : buf.toString('base64'), format: utf8 ? 'resource' : 'resource:base64', executable })
|
|
241
241
|
} else {
|
|
242
242
|
undeclared.push(rel)
|
|
243
243
|
}
|
|
244
244
|
}
|
|
245
|
-
if (missing.length + undeclared.length + nonUtf8.length > 0) throw validationError({ missing, undeclared, nonUtf8 })
|
|
245
|
+
if (missing.length + undeclared.length + nonUtf8.length > 0) throw validationError({ missing, undeclared, nonUtf8 }, label)
|
|
246
246
|
return { codeFiles, resourceFiles, integrities }
|
|
247
247
|
}
|
|
248
248
|
|
|
249
|
-
// GLOB the listed paths, FILTERING the auto-excluded set out of what a sweep finds (see
|
|
250
|
-
// expandDirectories) -> VALIDATE the whole surviving set -> only then ADD it to the target
|
|
251
|
-
// bundle(s), add-if-missing. A stasis.lock.json is updated only when one already exists.
|
|
252
249
|
export function addCommand({ cwd = process.cwd(), entries, logLabel = 'stasis-core' } = {}) {
|
|
253
|
-
if (!Array.isArray(entries) || entries.length === 0)
|
|
254
|
-
throw new Error('add: at least one file is required')
|
|
255
|
-
}
|
|
250
|
+
if (!Array.isArray(entries) || entries.length === 0) throw new Error('add: at least one file is required')
|
|
256
251
|
const baseDir = resolve(cwd)
|
|
257
252
|
const realBase = realpathSync(baseDir)
|
|
258
|
-
const { bundleFile, resourcesBundleFile, resources, brotliQuality } = readAddConfig(baseDir)
|
|
253
|
+
const { bundleFile, resourcesBundleFile, resources, brotliQuality, policy } = readAddConfig(baseDir)
|
|
254
|
+
const links = linksOn(baseDir)
|
|
255
|
+
const check = policy && ((artifact, hint) => policy.assertArtifact(artifact, { hint, links }))
|
|
259
256
|
|
|
260
257
|
const rootPkg = readJson(join(baseDir, 'package.json')) ?? {}
|
|
261
258
|
const workspaceName = rootPkg.name ?? 'workspace'
|
|
262
259
|
const workspaceVersion = rootPkg.version ?? '0.0.0'
|
|
263
260
|
|
|
264
|
-
// Lockfile updated only if one already exists (never created); hash packed bytes to match a frozen run.
|
|
265
261
|
const lockPath = join(baseDir, LOCK_FILE)
|
|
266
262
|
const hasLock = existsSync(lockPath)
|
|
267
263
|
|
|
268
|
-
//
|
|
269
|
-
//
|
|
270
|
-
// by-name rule can't recognize (it does cover the lockfile). One configured outside the project
|
|
271
|
-
// yields a '..' path, which no swept path can equal.
|
|
264
|
+
// A sweep must never attest what this run writes, and isAutoExcludedFile's by-name rule can't know a target named
|
|
265
|
+
// `dist/code.br`. One outside the project yields a '..' path, which no swept path can equal.
|
|
272
266
|
const outputs = new Set([bundleFile, resourcesBundleFile]
|
|
273
267
|
.filter((file) => file !== undefined)
|
|
274
268
|
.map((file) => toPosix(relative(baseDir, resolve(baseDir, file)))))
|
|
275
269
|
|
|
276
|
-
// `excluded` is counted in the summary below rather than dropped silently.
|
|
277
270
|
const { files, excluded } = expandDirectories(baseDir, normalizeEntries(entries, cwd), outputs)
|
|
278
271
|
if (files.size === 0) {
|
|
279
272
|
throw new Error(`add: no files to add (directory entries matched ${excluded.size === 0
|
|
@@ -282,31 +275,42 @@ export function addCommand({ cwd = process.cwd(), entries, logLabel = 'stasis-co
|
|
|
282
275
|
|
|
283
276
|
const { codeFiles, resourceFiles, integrities } = validateFiles({ baseDir, realBase, files, resources, withIntegrity: hasLock })
|
|
284
277
|
|
|
285
|
-
//
|
|
278
|
+
// Every target's merge is computed first, then the writes run -- see prepareBundleFile.
|
|
286
279
|
const summary = []
|
|
287
280
|
const writes = []
|
|
288
281
|
const repo = detectRepo(baseDir)
|
|
282
|
+
// Each artifact is read before a file is added to any: what one attests of a package is held of the others too.
|
|
283
|
+
const bundleAt = (file, abs = resolve(baseDir, file)) => (existsSync(abs) ? existingOf(`bundle at ${file}`, () => parseBundleBytes(readFileSync(abs))) : undefined)
|
|
284
|
+
const bundles = new Map([bundleFile, resourcesBundleFile].filter(Boolean).map((file) => [file, bundleAt(file)]))
|
|
285
|
+
const lock = hasLock ? existingOf(LOCK_FILE, () => Lockfile.parse(readFileSync(lockPath, 'utf8'))) : undefined
|
|
286
|
+
const attest = heldToInstall(baseDir, [...bundles, [LOCK_FILE, lock]].filter(([, artifact]) => artifact))
|
|
289
287
|
const planTarget = (target, entriesForTarget, kind) => {
|
|
290
288
|
if (entriesForTarget.size === 0) return
|
|
291
|
-
const
|
|
289
|
+
const existing = bundles.get(target)
|
|
290
|
+
const companion = bundles.get(target === bundleFile ? resourcesBundleFile : bundleFile)
|
|
291
|
+
const added = attest(existing, assembleBundle(baseDir, entriesForTarget, workspaceName, workspaceVersion, repo), companion)
|
|
292
|
+
const { write, total, added: count } = prepareBundleFile(baseDir, target, added, brotliQuality, check, existing)
|
|
292
293
|
writes.push(write)
|
|
293
|
-
summary.push(`+${
|
|
294
|
+
summary.push(`+${count} ${kind} (${total} total) -> ${target}`)
|
|
294
295
|
}
|
|
296
|
+
const allFiles = new Map([...codeFiles, ...resourceFiles])
|
|
295
297
|
if (resourcesBundleFile) {
|
|
296
298
|
planTarget(bundleFile, codeFiles, 'source')
|
|
297
299
|
planTarget(resourcesBundleFile, resourceFiles, 'resource')
|
|
298
300
|
} else {
|
|
299
301
|
const kind = codeFiles.size > 0 && resourceFiles.size > 0 ? 'file' : resourceFiles.size > 0 ? 'resource' : 'source'
|
|
300
|
-
planTarget(bundleFile,
|
|
302
|
+
planTarget(bundleFile, allFiles, kind)
|
|
301
303
|
}
|
|
302
304
|
|
|
303
|
-
// One lockfile attests both split targets, so
|
|
305
|
+
// One lockfile attests both split targets, so it takes every packed file's integrity (code + resources).
|
|
304
306
|
if (hasLock) {
|
|
305
|
-
const
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
307
|
+
const lockAdd = attest(lock, Lockfile.fromBundle(assembleBundle(baseDir, allFiles, workspaceName, workspaceVersion),
|
|
308
|
+
(file) => integrities.get(file)))
|
|
309
|
+
// `check` sees only what it already records: the files added were checked as the bundles'.
|
|
310
|
+
check?.(lock, recordedIn(LOCK_FILE))
|
|
311
|
+
const merged = lock.merge(lockAdd)
|
|
312
|
+
writes.push(() => writeFileSync(lockPath, merged.serialize()))
|
|
313
|
+
summary.push(`+${fileCount(merged) - fileCount(lock)} (${fileCount(merged)} total) -> ${LOCK_FILE}`)
|
|
310
314
|
}
|
|
311
315
|
if (excluded.size > 0) summary.push(`skipped ${excluded.size} auto-excluded`)
|
|
312
316
|
|