@exodus/stasis-core 1.0.0-beta.3 → 1.0.0-beta.5
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 +17 -3
- package/package.json +7 -4
- package/src/add.js +35 -46
- package/src/artifact-util.js +487 -0
- package/src/bundle-util.js +220 -33
- package/src/bundle.js +176 -111
- package/src/config.js +163 -223
- package/src/extract.js +7 -15
- package/src/fs.js +180 -296
- package/src/hooks.js +228 -108
- package/src/host.js +45 -0
- package/src/lockfile.js +22 -75
- package/src/prune.js +63 -130
- package/src/shard.js +13 -5
- package/src/state-util.js +10 -6
- package/src/state.js +438 -477
- package/src/util.js +37 -348
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
// The pure half of the util split: the artifact data model shared by bundle.js, lockfile.js and
|
|
2
|
+
// shard.js (the format universe, flat file keys, strict merges, the executable-set rules and the
|
|
3
|
+
// JSON<->Map converters). No Node builtin imports, so `./bundle` and `./lockfile` load in any JS
|
|
4
|
+
// runtime. util.js re-exports this module, so `@exodus/stasis-core/util` serves the full set.
|
|
5
|
+
|
|
6
|
+
// KNOWN_FORMATS is the closed universe of `format` strings; parsers reject anything outside it.
|
|
7
|
+
export const NODE_FORMATS = new Set(['module', 'commonjs', 'json', 'module-typescript', 'commonjs-typescript'])
|
|
8
|
+
export const SOURCE_LANGUAGE_FORMATS = new Set(['solidity', 'php', 'shell', 'rust'])
|
|
9
|
+
export const NATIVE_BUILD_FORMATS = new Set([
|
|
10
|
+
'java', 'kotlin', 'gradle', 'objc', 'objcpp', 'swift', 'c', 'cpp', 'c-header', 'cpp-header',
|
|
11
|
+
'ruby', 'cmake', 'podspec', 'podfile', 'podfile-lock', 'template', 'xml', 'env', 'fastlane', 'pbxproj',
|
|
12
|
+
])
|
|
13
|
+
export const RESOURCE_FORMATS = new Set(['resource', 'resource:base64'])
|
|
14
|
+
export const STAT_FORMATS = new Set(['stat:file', 'stat:directory'])
|
|
15
|
+
export const KNOWN_FORMATS = new Set([
|
|
16
|
+
...NODE_FORMATS,
|
|
17
|
+
...SOURCE_LANGUAGE_FORMATS,
|
|
18
|
+
...NATIVE_BUILD_FORMATS,
|
|
19
|
+
...RESOURCE_FORMATS,
|
|
20
|
+
'patch', // a `.patch` unified diff (pnpm patchedDependencies, patch-package): UTF-8 text applied by a patch step
|
|
21
|
+
'directory',
|
|
22
|
+
...STAT_FORMATS,
|
|
23
|
+
])
|
|
24
|
+
|
|
25
|
+
// Payload-free stat records: attest a path's KIND, no content, and yield to a real format.
|
|
26
|
+
export const isStatFormat = (format) => STAT_FORMATS.has(format)
|
|
27
|
+
|
|
28
|
+
// The post-erasure family of a '-typescript' loader format ('module-typescript' -> 'module'): a
|
|
29
|
+
// transforming preload (`stasis run --import tsx`) resolves/serves a TypeScript file as the JS
|
|
30
|
+
// family it transpiles to, while the attestation keeps the on-disk format. undefined otherwise.
|
|
31
|
+
export const erasedTypeScriptFormat = (format) =>
|
|
32
|
+
format === 'module-typescript' ? 'module' : format === 'commonjs-typescript' ? 'commonjs' : undefined
|
|
33
|
+
|
|
34
|
+
const formatKind = (format) => (format === 'directory' || format === 'stat:directory' ? 'directory' : 'file')
|
|
35
|
+
|
|
36
|
+
// A weak 'stat:*' yields to a real format of the SAME kind and never displaces one; anything else throws.
|
|
37
|
+
export function reconcileFormat(format, currentFormat, name) {
|
|
38
|
+
if (format === currentFormat) return format
|
|
39
|
+
const stat = isStatFormat(format)
|
|
40
|
+
if (stat !== isStatFormat(currentFormat) && formatKind(format) === formatKind(currentFormat)) {
|
|
41
|
+
return stat ? currentFormat : format
|
|
42
|
+
}
|
|
43
|
+
throw new Error(`format conflict for '${name}' ('${currentFormat}' vs '${format}')`)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Flat project-relative key. `rel === ''` (a `directory` capture whose path IS a module root) keys the
|
|
47
|
+
// bare dir, never `${dir}/` -- a trailing slash breaks the round-trip.
|
|
48
|
+
export function moduleFileKey(dir, rel) {
|
|
49
|
+
if (rel === '') return dir
|
|
50
|
+
return dir === '.' ? rel : `${dir}/${rel}`
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// A module bucket record in canonical key order; `ecosystem` is omitted (not undefined) when absent.
|
|
54
|
+
export const moduleInfo = ({ name, version, ecosystem, files }) =>
|
|
55
|
+
({ name, version, ...(ecosystem === undefined ? {} : { ecosystem }), files })
|
|
56
|
+
|
|
57
|
+
// A parsed bucket: an absent version has one spelling (a literal null folds into undefined so
|
|
58
|
+
// identity comparisons and JSON round-trips can't split on it), and `files` is null-prototype.
|
|
59
|
+
export function normalizeModule({ name, version, ecosystem, files }) {
|
|
60
|
+
assert(ecosystem === undefined || typeof ecosystem === 'string')
|
|
61
|
+
return moduleInfo({ name, version: version ?? undefined, ecosystem, files: fromEntries(Object.entries(files)) })
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// The keys a module map records -- the set an artifact's `executable` must be a subset of. `scope` MUST
|
|
65
|
+
// be the artifact's own: a non-full-scope artifact records only its node_modules buckets.
|
|
66
|
+
export function moduleFileKeys(modules, { scope = 'full' } = {}) {
|
|
67
|
+
const keys = new Set()
|
|
68
|
+
for (const [dir, { files }] of modules) {
|
|
69
|
+
if (scope !== 'full' && !hasNodeModulesSegment(dir)) continue
|
|
70
|
+
for (const rel of Object.keys(files)) keys.add(moduleFileKey(dir, rel))
|
|
71
|
+
}
|
|
72
|
+
return keys
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// THE rule an artifact's `executable` entry must satisfy; returns the problem, or null when legal.
|
|
76
|
+
// parseExecutable (read), assertExecutable (write) and narrowExecutable share it so they cannot drift.
|
|
77
|
+
function executableEntryProblem(file, { what, files, formats, scope }) {
|
|
78
|
+
if (file.includes('\\')) return "holds a '\\'"
|
|
79
|
+
if (posixPathEscapes(file)) return 'escapes the root'
|
|
80
|
+
if (scope !== 'full' && !hasNodeModulesSegment(file)) {
|
|
81
|
+
return `is outside node_modules, which a '${scope}'-scope ${what} does not record`
|
|
82
|
+
}
|
|
83
|
+
if (!files.has(file)) return `names no file the ${what} records`
|
|
84
|
+
const format = formats.get(file)
|
|
85
|
+
if (format === 'directory') return 'is a directory capture, not a file'
|
|
86
|
+
if (isStatFormat(format)) return `is a payload-free '${format}' record, not a file`
|
|
87
|
+
return null
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Applied at every write site, so the in-memory artifact is honest before it is ever serialized.
|
|
91
|
+
export function narrowExecutable(executable, { modules, formats, scope }) {
|
|
92
|
+
const out = new Set()
|
|
93
|
+
if (executable.size === 0) return out
|
|
94
|
+
const files = moduleFileKeys(modules, { scope })
|
|
95
|
+
for (const file of executable) {
|
|
96
|
+
if (executableEntryProblem(file, { what: 'artifact', files, formats, scope }) === null) out.add(file)
|
|
97
|
+
}
|
|
98
|
+
return out
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function assertExecutable(executable, { what, files, formats, scope }) {
|
|
102
|
+
for (const file of executable) {
|
|
103
|
+
const problem = executableEntryProblem(file, { what, files, formats, scope })
|
|
104
|
+
assert(problem === null, `${what}: executable entry '${file}' ${problem}`)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function parseExecutable(list, { what, files, formats, scope = 'full' }) {
|
|
109
|
+
if (list === undefined) return new Set()
|
|
110
|
+
const at = `${what}: executable`
|
|
111
|
+
assert(Array.isArray(list), `${at} must be an array of file paths`)
|
|
112
|
+
const out = new Set()
|
|
113
|
+
for (const file of list) {
|
|
114
|
+
assert(typeof file === 'string' && file !== '', `${at} entry must be a non-empty string`)
|
|
115
|
+
// A dupe would silently collapse in out.add() and round-trip to different bytes.
|
|
116
|
+
assert(!out.has(file), `${at} entry '${file}' is listed twice`)
|
|
117
|
+
out.add(file)
|
|
118
|
+
}
|
|
119
|
+
assertExecutable(out, { what, files, formats, scope })
|
|
120
|
+
return out
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// The one choke point every producer goes through, so a write site that forgot to narrow fails HERE, not
|
|
124
|
+
// on the next read. Omitted when empty: keeps every pre-`executable` artifact byte-identical when rewritten.
|
|
125
|
+
export function serializeExecutable(executable, { what, modules, formats, scope }) {
|
|
126
|
+
if (executable.size === 0) return undefined
|
|
127
|
+
assertExecutable(executable, { what, files: moduleFileKeys(modules, { scope }), formats, scope })
|
|
128
|
+
return fileSetToObject(executable)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function assert(condition, msg) {
|
|
132
|
+
if (!condition) throw new Error(msg)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function sortPaths(a, b) {
|
|
136
|
+
const [al, bl] = [a.split('/'), b.split('/')]
|
|
137
|
+
while (al.length > 0 && al[0] === bl[0]) {
|
|
138
|
+
al.shift()
|
|
139
|
+
bl.shift()
|
|
140
|
+
}
|
|
141
|
+
if (al.length === 0 && bl.length === 0) return 0
|
|
142
|
+
|
|
143
|
+
// First process each file in dir, then subdirs
|
|
144
|
+
if (al.length < 2) return bl.length < 2 && al > bl ? 1 : -1
|
|
145
|
+
if (bl.length < 2) return 1
|
|
146
|
+
|
|
147
|
+
if (al[0] === '*') return -1
|
|
148
|
+
if (bl[0] === '*') return 1
|
|
149
|
+
|
|
150
|
+
if (al[0] === 'node_modules') return 1
|
|
151
|
+
if (bl[0] === 'node_modules') return -1
|
|
152
|
+
|
|
153
|
+
// Prefer example/ over example-something/
|
|
154
|
+
const [an, bn] = [al, bl].map((list) => list.join(String.fromCodePoint(0)))
|
|
155
|
+
if (an < bn) return -1
|
|
156
|
+
if (an > bn) return 1
|
|
157
|
+
throw new Error('Unreachable')
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const byPath = (a, b) => sortPaths(a[0], b[0])
|
|
161
|
+
|
|
162
|
+
export const isPlainObject = (x) => x && [null, Object.prototype].includes(Object.getPrototypeOf(x))
|
|
163
|
+
|
|
164
|
+
export const fromEntries = (entries) => Object.setPrototypeOf(Object.fromEntries(entries), null)
|
|
165
|
+
|
|
166
|
+
export const fileSetToObject = (set) => [...set].toSorted(sortPaths)
|
|
167
|
+
|
|
168
|
+
// `sorted: false` only for a machine-only payload whose reader does not care about ordering (shard.js).
|
|
169
|
+
export const fileMapToObject = (map, { sorted = true } = {}) => {
|
|
170
|
+
const entries = sorted ? [...map].toSorted(byPath) : [...map]
|
|
171
|
+
return fromEntries(entries.map(([k, v]) => [k, v instanceof Map ? fileMapToObject(v, { sorted }) : v]))
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// The Map at `map.get(key)`, created on first use.
|
|
175
|
+
export const nestedMap = (map, key) => map.get(key) ?? map.set(key, new Map()).get(key)
|
|
176
|
+
|
|
177
|
+
export const objectToMaps = (obj) => new Map(
|
|
178
|
+
Object.entries(obj).map(([k, v]) => [k, isPlainObject(v) ? objectToMaps(v) : v])
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
// True for an absolute path or any `..` hop that pops above the root, INCLUDING a mid-path one
|
|
182
|
+
// (`a/../../x`) that plain `startsWith('..')` would miss. A segment walk (not posix.normalize, whose
|
|
183
|
+
// verdict it matches -- see posix-path-escapes.test.js) so this module stays free of node:path:
|
|
184
|
+
// `.` and empty segments are skipped exactly as normalize collapses them, a real segment pushes, and
|
|
185
|
+
// a `..` with nothing left to pop is an escape -- normalize would keep it as a leading `..` forever.
|
|
186
|
+
export function posixPathEscapes(path) {
|
|
187
|
+
if (path.startsWith('/')) return true
|
|
188
|
+
// The walk can only return true via a literal '..' segment, so the ordinary keys skip the split.
|
|
189
|
+
if (!path.includes('..')) return false
|
|
190
|
+
let depth = 0
|
|
191
|
+
for (const segment of path.split('/')) {
|
|
192
|
+
if (segment === '.' || segment === '') continue
|
|
193
|
+
if (segment !== '..') depth++
|
|
194
|
+
else if (--depth < 0) return true
|
|
195
|
+
}
|
|
196
|
+
return false
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// THE rule every path an artifact records must satisfy (a file key's too, see canonicalFileKey): in
|
|
200
|
+
// the root, and free of `\` -- part of a name off Windows, which stasis refuses everywhere rather than
|
|
201
|
+
// take for another path, and on Windows a separator a `..\x` would climb out by.
|
|
202
|
+
export const isRefusedPath = (path) => path.includes('\\') || posixPathEscapes(path)
|
|
203
|
+
|
|
204
|
+
// Throws unless `path`, one of `what`'s, is a path an artifact may record (see isRefusedPath).
|
|
205
|
+
export const assertArtifactPath = (path, what) =>
|
|
206
|
+
assert(!isRefusedPath(path), `${what}: path '${path}' escapes the root or holds a '\\'`)
|
|
207
|
+
|
|
208
|
+
// THE rule an artifact's entry must satisfy, on read (parseEntries) and on write (serializeEntries).
|
|
209
|
+
const assertEntry = (entry, what) =>
|
|
210
|
+
assert(typeof entry === 'string' && entry !== '' && !isRefusedPath(entry), `${what}: invalid entry ${JSON.stringify(entry)}`)
|
|
211
|
+
|
|
212
|
+
// An artifact's `entries` list as a Set: each a non-empty in-root path, listed once (a dupe would
|
|
213
|
+
// collapse in the Set and round-trip to different bytes).
|
|
214
|
+
export function parseEntries(list, what) {
|
|
215
|
+
assert(Array.isArray(list), `${what}: entries must be an array of file paths`)
|
|
216
|
+
const out = new Set()
|
|
217
|
+
for (const entry of list) {
|
|
218
|
+
assertEntry(entry, what)
|
|
219
|
+
assert(!out.has(entry), `${what}: entry '${entry}' is listed twice`)
|
|
220
|
+
out.add(entry)
|
|
221
|
+
}
|
|
222
|
+
return out
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// The `entries` list an artifact writes: the same rule as on read, so an in-memory construct can't
|
|
226
|
+
// serialize what parse would reject; path-sorted.
|
|
227
|
+
export function serializeEntries(entries, what) {
|
|
228
|
+
for (const entry of entries) assertEntry(entry, what)
|
|
229
|
+
return fileSetToObject(entries)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// THE rule an artifact's `formats` entry must satisfy, on read (parseFormats) and on write
|
|
233
|
+
// (serializeFormats): a path an artifact may record, and a known format.
|
|
234
|
+
function assertFormat(file, format) {
|
|
235
|
+
assertArtifactPath(file, 'formats')
|
|
236
|
+
assert(KNOWN_FORMATS.has(format), `unknown format '${format}' for ${file}`)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// An artifact's `formats` object as a validated Map. '' and '.' alias to the same key (older
|
|
240
|
+
// artifacts keyed the root listing ''); normalized, failing closed on dupes, and an unknown format
|
|
241
|
+
// is rejected at the schema boundary so a tampered artifact fails closed.
|
|
242
|
+
export function parseFormats(json) {
|
|
243
|
+
assert(isPlainObject(json))
|
|
244
|
+
const formats = new Map()
|
|
245
|
+
for (const [file, format] of Object.entries(json)) {
|
|
246
|
+
assertFormat(file, format)
|
|
247
|
+
const key = file === '' ? '.' : file
|
|
248
|
+
assert(!formats.has(key), `duplicate format key '${key}'`)
|
|
249
|
+
formats.set(key, format)
|
|
250
|
+
}
|
|
251
|
+
return formats
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// The `formats` an artifact writes: the same rule as on read, so an in-memory construct can't serialize
|
|
255
|
+
// what parse would reject; path-sorted.
|
|
256
|
+
export function serializeFormats(formats) {
|
|
257
|
+
for (const [file, format] of formats) assertFormat(file, format)
|
|
258
|
+
return fileMapToObject(formats)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// THE rules an artifact's `imports` (conditions -> parent -> specifier -> target, as nested Maps)
|
|
262
|
+
// must satisfy, on read (parseImports) and on write (serializeImports). Paths escaping the root (incl.
|
|
263
|
+
// mid-path `a/../../x`) or holding a `\` are rejected: getImport resolves against the root at load. A
|
|
264
|
+
// target is a file, or (--metro) a non-empty { platform: file } map.
|
|
265
|
+
function assertImports(imports) {
|
|
266
|
+
for (const [, byParent] of imports) {
|
|
267
|
+
assert(byParent instanceof Map)
|
|
268
|
+
for (const [parent, specifiers] of byParent) {
|
|
269
|
+
assertArtifactPath(parent, 'imports')
|
|
270
|
+
assert(specifiers instanceof Map)
|
|
271
|
+
for (const [, target] of specifiers) {
|
|
272
|
+
if (typeof target === 'string') {
|
|
273
|
+
assertArtifactPath(target, 'imports')
|
|
274
|
+
continue
|
|
275
|
+
}
|
|
276
|
+
assert(target instanceof Map && target.size > 0, 'import target must be a file or a non-empty {platform: file} map')
|
|
277
|
+
for (const [platform, file] of target) {
|
|
278
|
+
assert(typeof platform === 'string' && platform.length > 0 && !platform.includes('/'), `invalid platform key '${platform}'`)
|
|
279
|
+
assert(typeof file === 'string')
|
|
280
|
+
assertArtifactPath(file, 'imports')
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// An artifact's `imports` object as nested Maps (see assertImports).
|
|
288
|
+
export function parseImports(json) {
|
|
289
|
+
assert(isPlainObject(json))
|
|
290
|
+
const imports = objectToMaps(json)
|
|
291
|
+
assertImports(imports)
|
|
292
|
+
return imports
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// The `imports` an artifact writes: the same rules as on read (see assertImports); path-sorted.
|
|
296
|
+
export function serializeImports(imports) {
|
|
297
|
+
assertImports(imports)
|
|
298
|
+
return fileMapToObject(imports)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// A module map as the serialized `modules` (node_modules buckets) and `sources` (the rest) objects,
|
|
302
|
+
// buckets and files path-sorted so the bytes are canonical.
|
|
303
|
+
export function groupModules(modules, { skipEmpty = false } = {}) {
|
|
304
|
+
const grouped = { modules: [], sources: [] }
|
|
305
|
+
for (const [dir, info] of modules) {
|
|
306
|
+
if (skipEmpty && Object.keys(info.files).length === 0) continue
|
|
307
|
+
const inNodeModules = hasNodeModulesSegment(dir)
|
|
308
|
+
if (inNodeModules) assert(info.name && info.version && info.files)
|
|
309
|
+
const files = fromEntries(Object.entries(info.files).toSorted(byPath))
|
|
310
|
+
grouped[inNodeModules ? 'modules' : 'sources'].push([dir, moduleInfo({ ...info, files })])
|
|
311
|
+
}
|
|
312
|
+
return { modules: fromEntries(grouped.modules.toSorted(byPath)), sources: fromEntries(grouped.sources.toSorted(byPath)) }
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// A target is a resolved-file string, or a { platform: file } Map under --metro.
|
|
316
|
+
function importTargetsEqual(a, b) {
|
|
317
|
+
const aMap = a instanceof Map
|
|
318
|
+
if (aMap !== (b instanceof Map)) return false
|
|
319
|
+
if (!aMap) return a === b
|
|
320
|
+
if (a.size !== b.size) return false
|
|
321
|
+
for (const [platform, file] of a) if (b.get(platform) !== file) return false
|
|
322
|
+
return true
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const fmtTarget = (t) => (t instanceof Map ? `{${[...t].map(([p, f]) => `${p}: ${f}`).join(', ')}}` : t)
|
|
326
|
+
|
|
327
|
+
// Merge two import maps (conditions -> parent -> specifier -> target); a redirect conflict throws.
|
|
328
|
+
export function mergeImportMaps(a, b, label) {
|
|
329
|
+
const out = new Map()
|
|
330
|
+
const absorb = (imports) => {
|
|
331
|
+
for (const [conditions, byParent] of imports) {
|
|
332
|
+
const outByParent = nestedMap(out, conditions)
|
|
333
|
+
for (const [parent, specs] of byParent) {
|
|
334
|
+
const outSpecs = nestedMap(outByParent, parent)
|
|
335
|
+
for (const [spec, target] of specs) {
|
|
336
|
+
if (outSpecs.has(spec)) {
|
|
337
|
+
assert(importTargetsEqual(outSpecs.get(spec), target),
|
|
338
|
+
`${label}: import '${spec}' from '${parent}' resolves differently ('${fmtTarget(outSpecs.get(spec))}' vs '${fmtTarget(target)}')`)
|
|
339
|
+
} else {
|
|
340
|
+
outSpecs.set(spec, target instanceof Map ? new Map(target) : target)
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
absorb(a)
|
|
347
|
+
absorb(b)
|
|
348
|
+
return out
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export function mergeFormatMaps(a, b, label) {
|
|
352
|
+
const out = new Map()
|
|
353
|
+
const absorb = (formats) => {
|
|
354
|
+
for (const [file, format] of formats) {
|
|
355
|
+
const currentFormat = out.get(file)
|
|
356
|
+
if (currentFormat === undefined) {
|
|
357
|
+
out.set(file, format)
|
|
358
|
+
continue
|
|
359
|
+
}
|
|
360
|
+
try {
|
|
361
|
+
out.set(file, reconcileFormat(format, currentFormat, file))
|
|
362
|
+
} catch (cause) {
|
|
363
|
+
throw new Error(`${label}: ${cause.message}`, { cause })
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
absorb(a)
|
|
368
|
+
absorb(b)
|
|
369
|
+
return out
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// A union, EXCEPT that `b` (the INCOMING, newer artifact -- every call site passes it on the right) is
|
|
373
|
+
// authoritative for its own files: a since-lost execute bit is cleared, not resurrected by the union.
|
|
374
|
+
export function mergeExecutableSets(a, b, bModules, scope) {
|
|
375
|
+
if (a.size === 0) return new Set(b)
|
|
376
|
+
const bFiles = moduleFileKeys(bModules, { scope })
|
|
377
|
+
const out = new Set(b)
|
|
378
|
+
for (const file of a) if (!bFiles.has(file)) out.add(file)
|
|
379
|
+
return out
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// An empty, '.' or '..' path segment.
|
|
383
|
+
const NON_CANONICAL_SEGMENT = /(?:^|\/)\.{0,2}(?:\/|$)/u
|
|
384
|
+
|
|
385
|
+
// The flat key of `rel` in bucket `dir`; throws unless canonical ('.' only as the root listing, rel '')
|
|
386
|
+
// and free of `\`, which no artifact path holds: off Windows it is part of a name, refused everywhere.
|
|
387
|
+
export function canonicalFileKey(dir, rel, what) {
|
|
388
|
+
const key = moduleFileKey(dir, rel)
|
|
389
|
+
// Message built only on failure: this runs for every file.
|
|
390
|
+
if ((key !== '.' || rel !== '') && NON_CANONICAL_SEGMENT.test(key)) assert(false, `${what}: non-canonical file key ${JSON.stringify(key)}`)
|
|
391
|
+
if (key.includes('\\')) assert(false, `${what}: file key '${key}' holds a '\\', which no path may`)
|
|
392
|
+
return key
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Maps each file's flat key to its bucket; rejects non-canonical keys and refused bucket dirs (an empty
|
|
396
|
+
// bucket's too, which no file key would reach), reports duplicates to onDuplicate.
|
|
397
|
+
export function flatFileKeys(modules, what, onDuplicate) {
|
|
398
|
+
const owners = new Map()
|
|
399
|
+
for (const [dir, { files }] of modules) {
|
|
400
|
+
if (typeof dir !== 'string') assert(false, `${what}: bucket dir ${String(dir)} is not a string`)
|
|
401
|
+
assertArtifactPath(dir, what)
|
|
402
|
+
for (const rel of Object.keys(files)) {
|
|
403
|
+
const key = canonicalFileKey(dir, rel, what)
|
|
404
|
+
const owner = owners.get(key)
|
|
405
|
+
if (owner !== undefined) onDuplicate(key, owner, dir)
|
|
406
|
+
owners.set(key, dir)
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return owners
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// The flatFileKeys duplicate handler for an artifact's parse/serialize: two bucket splits flattening
|
|
413
|
+
// to one path means module bucketing changed between writes.
|
|
414
|
+
export const duplicateKeyError = (what, regenerate) => (key) => assert(false,
|
|
415
|
+
`duplicate file key '${key}' across ${what} buckets -- module bucketing changed between writes ` +
|
|
416
|
+
`(a workspace package without a version now owns its own bucket); regenerate the ${regenerate}`)
|
|
417
|
+
|
|
418
|
+
// Result `files` objects are null-prototype, so a `__proto__` file name is a plain own key.
|
|
419
|
+
export function mergeModuleMaps(a, b, label) {
|
|
420
|
+
const out = new Map()
|
|
421
|
+
const absorb = (modules) => {
|
|
422
|
+
for (const [dir, info] of modules) {
|
|
423
|
+
const existing = out.get(dir)
|
|
424
|
+
if (existing === undefined) {
|
|
425
|
+
out.set(dir, moduleInfo({ ...info, files: Object.assign(Object.create(null), info.files) }))
|
|
426
|
+
continue
|
|
427
|
+
}
|
|
428
|
+
assert(existing.name === info.name,
|
|
429
|
+
`${label}: module '${dir}' name mismatch ('${existing.name}' vs '${info.name}')`)
|
|
430
|
+
// A one-sided absent version is usually a migration skew: older stasis fabricated '0.0.0'
|
|
431
|
+
// for a workspace package whose package.json has no version, newer stasis records none.
|
|
432
|
+
assert(existing.version === info.version,
|
|
433
|
+
`${label}: module '${dir}' version mismatch ('${existing.version}' vs '${info.version}')` +
|
|
434
|
+
((existing.version == null) === (info.version == null) ? '' :
|
|
435
|
+
` -- an artifact from an older stasis may record a placeholder version for a workspace ` +
|
|
436
|
+
`package without one; regenerate it (bundle=replace / lock=replace)`))
|
|
437
|
+
assert(existing.ecosystem === info.ecosystem,
|
|
438
|
+
`${label}: module '${dir}' ecosystem mismatch ('${existing.ecosystem ?? '(none)'}' vs '${info.ecosystem ?? '(none)'}')`)
|
|
439
|
+
for (const [rel, value] of Object.entries(info.files)) {
|
|
440
|
+
if (Object.hasOwn(existing.files, rel)) {
|
|
441
|
+
assert(existing.files[rel] === value, `${label}: content mismatch for '${moduleFileKey(dir, rel)}'`)
|
|
442
|
+
} else {
|
|
443
|
+
existing.files[rel] = value
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
absorb(a)
|
|
449
|
+
absorb(b)
|
|
450
|
+
// One project-relative path must live in exactly one bucket. Bucketing can change between
|
|
451
|
+
// releases (a versionless workspace package used to fall through to a parent bucket and now owns
|
|
452
|
+
// its own), and per-dir absorption cannot see that: without this check the merge would WRITE an
|
|
453
|
+
// artifact that then fails its own next parse on the duplicate-file-key guard.
|
|
454
|
+
flatFileKeys(out, label, (key, owner, dir) => assert(false,
|
|
455
|
+
`${label}: file '${key}' is bucketed under both '${owner}' and '${dir}' -- module bucketing ` +
|
|
456
|
+
`changed between the artifacts (a workspace package without a version now owns its own ` +
|
|
457
|
+
`bucket); regenerate the artifact (bundle=replace / lock=replace)`))
|
|
458
|
+
return out
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// A full path SEGMENT, NOT a bare substring (`foo_node_modules/dep` is a source dir, not a bucket).
|
|
462
|
+
export function hasNodeModulesSegment(path) {
|
|
463
|
+
return path.split('/').includes('node_modules')
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// Deepest segment-aligned `node_modules/` marker, or -1: `lastIndexOf` alone would also match a bare
|
|
467
|
+
// substring (`foo_node_modules/`), so a candidate only counts at the start or after a `/`.
|
|
468
|
+
function lastNodeModulesMarker(path) {
|
|
469
|
+
const marker = 'node_modules/'
|
|
470
|
+
let idx = path.length
|
|
471
|
+
while ((idx = path.lastIndexOf(marker, idx - 1)) !== -1) {
|
|
472
|
+
if (idx === 0 || path[idx - 1] === '/') return idx
|
|
473
|
+
}
|
|
474
|
+
return -1
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
export function splitNodeModulesPath(path) {
|
|
478
|
+
const marker = 'node_modules/'
|
|
479
|
+
const idx = lastNodeModulesMarker(path)
|
|
480
|
+
if (idx === -1) return null
|
|
481
|
+
const after = idx + marker.length
|
|
482
|
+
const parts = path.slice(after).split('/')
|
|
483
|
+
const pkgLen = parts[0].startsWith('@') ? 2 : 1
|
|
484
|
+
if (parts.length <= pkgLen || parts.slice(0, pkgLen).some((p) => !p)) return null
|
|
485
|
+
const name = parts.slice(0, pkgLen).join('/')
|
|
486
|
+
return { dir: path.slice(0, after) + name, rel: parts.slice(pkgLen).join('/'), name }
|
|
487
|
+
}
|