@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/src/add.js CHANGED
@@ -1,195 +1,183 @@
1
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'
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 { brotliOptions } from './brotli.js'
9
- import { detectRepo, findPackageMetadata, normalizeEntries, packageType, readJson } from './bundle-util.js'
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, moduleFileKey, moduleInfo, parseResourcesOption, pathExt, sortPaths, splitNodeModulesPath, toPosix } from './util.js'
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
- const CONFIG_FILE = 'stasis.config.json'
14
- const LOCK_FILE = 'stasis.lock.json'
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
- // A `.js`/`.ts` follows its package `type` (LIMITATION: none falls back to commonjs -- use `.mjs`/`.cjs`
17
- // or set `type` when the module system matters). Any other name is classifyFormat's call: a format,
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
- // File and non-empty package counts of a bundle or lockfile.
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 ?? 'stasis.code.br'
33
+ const bundleFile = cfg.bundleFile ?? BUNDLE_FILE
50
34
  const resourcesBundleFile = cfg.resourcesBundleFile
51
- // Split targets must be distinct files (else they clobber on write); canonicalized so symlink and relative aliases are caught too.
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
- // 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`.
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
- // `identity`'s name, version, ecosystem and repo (moduleInfo takes those alone).
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
- if (meta) {
84
- const relInBucket = meta.pkgDir === '.' ? rel : rel.slice(meta.pkgDir.length + 1)
85
- bucketFiles(meta.pkgDir, meta)[relInBucket] = content
86
- } else {
87
- bucketFiles('.', { name: workspaceName, version: workspaceVersion })[rel] = content
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('add')
65
+ return new Bundle({ config: { scope: 'full' }, entries: new Set(), modules, formats, imports: new Map(), executable, repo }).withReason(reason)
94
66
  }
95
67
 
96
- // Merge add-if-missing into any existing bundle (divergent bytes for an attested path throw -- see
97
- // mergeModuleMaps), returning the write as a thunk so every target's conflicts surface before the
98
- // first byte lands and an aborted `add` leaves the project untouched. Serializing INSIDE the thunk
99
- // keeps peak memory at one target's bytes rather than every target's at once.
100
- function prepareBundleFile(baseDir, targetPath, bundle, brotliQuality) {
101
- const abs = resolve(baseDir, targetPath)
102
- let mergedFrom
103
- if (existsSync(abs)) {
104
- let existing
105
- try {
106
- existing = Bundle.parse(brotliDecompressSync(readFileSync(abs)).toString('utf8'))
107
- } catch (cause) {
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
- mergedFrom = tally(existing).files
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 { files, packages } = tally(bundle)
114
- return {
115
- write: () => {
116
- mkdirSync(dirname(abs), { recursive: true })
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
- // Companion Lockfile: the bundle's shape with each file's content swapped for its on-disk integrity, so `--lock=frozen` matches.
124
- function bundleToLockfile(bundle, integrities) {
125
- const modules = new Map()
126
- for (const [dir, m] of bundle.modules) {
127
- const files = Object.create(null)
128
- for (const rel of Object.keys(m.files)) files[rel] = integrities.get(moduleFileKey(dir, rel))
129
- modules.set(dir, moduleInfo({ ...m, files }))
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
- // Merge into the project's stasis.lock.json (strict: divergent bytes throw), deferring the write like
135
- // prepareBundleFile so a conflict here doesn't leave the bundles updated.
136
- function prepareLockfile(lockPath, lockAdd) {
137
- let existing
138
- try {
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
- const before = tally(existing).files
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
- // GLOB + FILTER: expand each directory entry into the files under it (recursive glob), dropping the
150
- // auto-excluded set from what a sweep FINDS -- dir rules on the segments BELOW the named root (`add
151
- // src/examples` sweeps the dir it was pointed at, `add src` skips one it merely found), file rules
152
- // by name plus the caller's `outputs` (this run's own write targets). A path the caller NAMED is
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 = statSync(dirAbs, { throwIfNoEntry: false })
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
- for (const match of globSync('**/*', { cwd: dirAbs })) {
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 = statSync(fileAbs, { throwIfNoEntry: false })
173
- if (fileStats?.isFile()) found.push([toPosix(relative(baseDir, fileAbs)), match, fileStats])
160
+ const fileStats = statStrict(host, fileAbs, fileRel) ?? undefined
161
+ if (fileStats?.isFile()) found.push([toPosix(fileRel), match, fileStats])
174
162
  }
175
- // Sorted so the sweep is deterministic regardless of readdir order: artifact bytes are
176
- // canonicalized by serialize() either way, but multi-file diagnostics keep a stable order.
177
- for (const [file, match, fileStats] of found.toSorted(([a], [b]) => sortPaths(a, b))) {
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
- || outputs.has(file) || isAutoExcludedFile(file)) excluded.add(file)
168
+ || isOutput(file, fileStats) || isAutoExcludedFile(file)) excluded.add(file)
180
169
  else files.set(file, fileStats)
181
170
  }
182
171
  }
183
- // A path any entry kept can't count as skipped -- another root's sweep may have re-flagged it.
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) => [...rels].toSorted(sortPaths).join(', ')
177
+ const listPaths = (rels) => sortedByPath(rels).join(', ')
189
178
 
190
- // One error for every file validation rejected, so a directory sweep names all of them at once
191
- // instead of one per re-run. A lone offender keeps its exact standalone message.
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(`add: ${parts.join('; ')}`)
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
- // VALIDATE step: classify the WHOLE set -- each file recognized source or a declared resource --
211
- // before a single target is touched, collecting every offender into one error. `files` is the glob
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 = readFileSync(abs)
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, so it is NOT source: it falls through to the resource branch (opaque base64).
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 (resources.has(pathExt(rel) || basename(rel).toLowerCase())) {
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
- // The CONFIGURED bundle targets, project-relative: a sweep must never attest an artifact this very
269
- // run is writing, and a target can be named anything (`dist/code.br`), which isAutoExcludedFile's
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
- // ADD step: every target's merge is computed first, then the writes run -- see prepareBundleFile.
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 { write, counts } = prepareBundleFile(baseDir, target, assembleBundle(baseDir, entriesForTarget, workspaceName, workspaceVersion, repo), brotliQuality)
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(`+${counts.added} ${kind} (${counts.total} total) -> ${counts.path}`)
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, new Map([...codeFiles, ...resourceFiles]), kind)
302
+ planTarget(bundleFile, allFiles, kind)
301
303
  }
302
304
 
303
- // One lockfile attests both split targets, so merge in every packed file's integrity (code + resources together).
305
+ // One lockfile attests both split targets, so it takes every packed file's integrity (code + resources).
304
306
  if (hasLock) {
305
- const allFiles = new Map([...codeFiles, ...resourceFiles])
306
- const lockAdd = bundleToLockfile(assembleBundle(baseDir, allFiles, workspaceName, workspaceVersion), integrities)
307
- const { write, counts } = prepareLockfile(lockPath, lockAdd)
308
- writes.push(write)
309
- summary.push(`+${counts.added} (${counts.total} total) -> ${LOCK_FILE}`)
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