@exodus/stasis-core 1.0.0-beta.3 → 1.0.0-beta.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/stasis-core",
3
- "version": "1.0.0-beta.3",
3
+ "version": "1.0.0-beta.4",
4
4
  "description": "Zero-dependency stasis runtime",
5
5
  "type": "module",
6
6
  "exports": {
@@ -21,6 +21,7 @@
21
21
  "files": [
22
22
  "bin/stasis-core.js",
23
23
  "src/add.js",
24
+ "src/artifact-util.js",
24
25
  "src/brotli.js",
25
26
  "src/bundle.js",
26
27
  "src/bundle-util.js",
@@ -0,0 +1,318 @@
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. Everything here is pure computation over caller-supplied values with no
4
+ // Node builtin imports at all, so the data-model entry points (`./bundle`, `./lockfile`) load in
5
+ // any JS runtime. Byte/name classification, fs observation and CLI parsing live in util.js, which
6
+ // re-exports this module so `@exodus/stasis-core/util` keeps serving the full set.
7
+
8
+ const sep = '/'
9
+
10
+ // KNOWN_FORMATS is the closed universe of `format` strings; parsers reject anything outside it.
11
+ export const NODE_FORMATS = new Set(['module', 'commonjs', 'json', 'module-typescript', 'commonjs-typescript'])
12
+ export const SOURCE_LANGUAGE_FORMATS = new Set(['solidity', 'php', 'shell', 'rust'])
13
+ export const NATIVE_BUILD_FORMATS = new Set([
14
+ 'java', 'kotlin', 'gradle', 'objc', 'objcpp', 'swift', 'c', 'cpp', 'c-header', 'cpp-header',
15
+ 'ruby', 'cmake', 'podspec', 'podfile', 'podfile-lock', 'template', 'xml', 'env', 'fastlane', 'pbxproj',
16
+ ])
17
+ export const RESOURCE_FORMATS = new Set(['resource', 'resource:base64'])
18
+ export const STAT_FORMATS = new Set(['stat:file', 'stat:directory'])
19
+ export const KNOWN_FORMATS = new Set([
20
+ ...NODE_FORMATS,
21
+ ...SOURCE_LANGUAGE_FORMATS,
22
+ ...NATIVE_BUILD_FORMATS,
23
+ ...RESOURCE_FORMATS,
24
+ 'patch', // a `.patch` unified diff (pnpm patchedDependencies, patch-package): UTF-8 text applied by a patch step
25
+ 'directory',
26
+ ...STAT_FORMATS,
27
+ ])
28
+
29
+ // Payload-free stat records: attest a path's KIND, no content, and yield to a real format.
30
+ export const isStatFormat = (format) => STAT_FORMATS.has(format)
31
+
32
+ // Gates reconcileFormat: a stat record only reconciles with a real format of the SAME kind.
33
+ const formatKind = (format) => (format === 'directory' || format === 'stat:directory' ? 'directory' : 'file')
34
+
35
+ // A weak 'stat:*' yields to a real format of the SAME kind and never displaces one; anything else throws.
36
+ export function reconcileFormat(format, currentFormat, name) {
37
+ if (format === currentFormat) return format
38
+ const stat = isStatFormat(format)
39
+ const currentStat = isStatFormat(currentFormat)
40
+ if (stat !== currentStat && 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
+ // The keys a module map records -- the set an artifact's `executable` must be a subset of. `scope` MUST
54
+ // be the artifact's own: a non-full-scope artifact records only its node_modules buckets.
55
+ export function moduleFileKeys(modules, { scope = 'full' } = {}) {
56
+ const keys = new Set()
57
+ for (const [dir, { files }] of modules) {
58
+ if (scope !== 'full' && !hasNodeModulesSegment(dir)) continue
59
+ for (const rel of Object.keys(files)) keys.add(moduleFileKey(dir, rel))
60
+ }
61
+ return keys
62
+ }
63
+
64
+ // THE rule an artifact's `executable` entry must satisfy; returns the problem, or null when legal.
65
+ // parseExecutable (read), assertExecutable (write) and narrowExecutable share it so they cannot drift.
66
+ function executableEntryProblem(file, { what, files, formats, scope }) {
67
+ if (posixPathEscapes(file)) return 'escapes the root'
68
+ if (scope !== 'full' && !hasNodeModulesSegment(file)) {
69
+ return `is outside node_modules, which a '${scope}'-scope ${what} does not record`
70
+ }
71
+ if (!files.has(file)) return `names no file the ${what} records`
72
+ const format = formats.get(file)
73
+ if (format === 'directory') return 'is a directory capture, not a file'
74
+ if (isStatFormat(format)) return `is a payload-free '${format}' record, not a file`
75
+ return null
76
+ }
77
+
78
+ // Applied at every write site, so the in-memory artifact is honest before it is ever serialized.
79
+ export function narrowExecutable(executable, { modules, formats, scope }) {
80
+ const out = new Set()
81
+ if (executable.size === 0) return out
82
+ const files = moduleFileKeys(modules, { scope })
83
+ for (const file of executable) {
84
+ if (executableEntryProblem(file, { what: 'artifact', files, formats, scope }) === null) out.add(file)
85
+ }
86
+ return out
87
+ }
88
+
89
+ function assertExecutable(executable, { what, files, formats, scope }) {
90
+ for (const file of executable) {
91
+ const problem = executableEntryProblem(file, { what, files, formats, scope })
92
+ assert(problem === null, `${what}: executable entry '${file}' ${problem}`)
93
+ }
94
+ }
95
+
96
+ export function parseExecutable(list, { what, files, formats, scope = 'full' }) {
97
+ if (list === undefined) return new Set()
98
+ const at = `${what}: executable`
99
+ assert(Array.isArray(list), `${at} must be an array of file paths`)
100
+ const out = new Set()
101
+ for (const file of list) {
102
+ assert(typeof file === 'string' && file !== '', `${at} entry must be a non-empty string`)
103
+ // A dupe would silently collapse in out.add() and round-trip to different bytes.
104
+ assert(!out.has(file), `${at} entry '${file}' is listed twice`)
105
+ out.add(file)
106
+ }
107
+ assertExecutable(out, { what, files, formats, scope })
108
+ return out
109
+ }
110
+
111
+ // The one choke point every producer goes through, so a write site that forgot to narrow fails HERE, not
112
+ // on the next read. Omitted when empty: keeps every pre-`executable` artifact byte-identical when rewritten.
113
+ export function serializeExecutable(executable, { what, modules, formats, scope }) {
114
+ if (executable.size === 0) return undefined
115
+ assertExecutable(executable, { what, files: moduleFileKeys(modules, { scope }), formats, scope })
116
+ return fileSetToObject(executable)
117
+ }
118
+
119
+ export function assert(condition, msg) {
120
+ if (!condition) throw new Error(msg)
121
+ }
122
+
123
+ export function sortPaths(a, b) {
124
+ const [al, bl] = [a.split(sep), b.split(sep)]
125
+ while (al.length > 0 && al[0] === bl[0]) {
126
+ al.shift()
127
+ bl.shift()
128
+ }
129
+ if (al.length === 0 && bl.length === 0) return 0
130
+
131
+ // First process each file in dir, then subdirs
132
+ if (al.length < 2) return bl.length < 2 && al > bl ? 1 : -1
133
+ if (bl.length < 2) return 1
134
+
135
+ if (al[0] === '*') return -1
136
+ if (bl[0] === '*') return 1
137
+
138
+ if (al[0] === 'node_modules') return 1
139
+ if (bl[0] === 'node_modules') return -1
140
+
141
+ // Prefer example/ over example-something/
142
+ const [an, bn] = [al, bl].map((list) => list.join(String.fromCodePoint(0)))
143
+ if (an < bn) return -1
144
+ if (an > bn) return 1
145
+ throw new Error('Unreachable')
146
+ }
147
+
148
+ export const isPlainObject = (x) => x && [null, Object.prototype].includes(Object.getPrototypeOf(x))
149
+
150
+ export const fromEntries = (entries) => Object.setPrototypeOf(Object.fromEntries(entries), null)
151
+
152
+ export const fileSetToObject = (set) => [...set].toSorted((a, b) => sortPaths(a, b))
153
+
154
+ // `sorted: false` only for a machine-only payload whose reader does not care about ordering (shard.js).
155
+ export const fileMapToObject = (map, { sorted = true } = {}) => {
156
+ if (!sorted) {
157
+ const out = Object.create(null)
158
+ for (const [k, v] of map) out[k] = v instanceof Map ? fileMapToObject(v, { sorted }) : v
159
+ return out
160
+ }
161
+ return fromEntries(
162
+ [...map]
163
+ .toSorted((a, b) => sortPaths(a[0], b[0]))
164
+ .map(([k, v]) => [k, v instanceof Map ? fileMapToObject(v, { sorted }) : v])
165
+ )
166
+ }
167
+
168
+ export const objectToMaps = (obj) => new Map(
169
+ Object.entries(obj).map(([k, v]) => [k, isPlainObject(v) ? objectToMaps(v) : v])
170
+ )
171
+
172
+ // True for an absolute path or any `..` hop that pops above the root, INCLUDING a mid-path one
173
+ // (`a/../../x`) that plain `startsWith('..')` would miss. A segment walk (not posix.normalize, whose
174
+ // verdict it matches -- see posix-path-escapes.test.js) so this module stays free of node:path:
175
+ // `.` and empty segments are skipped exactly as normalize collapses them, a real segment pushes, and
176
+ // a `..` with nothing left to pop is an escape -- normalize would keep it as a leading `..` forever.
177
+ export function posixPathEscapes(path) {
178
+ if (path.startsWith('/')) return true
179
+ let depth = 0
180
+ for (const segment of path.split('/')) {
181
+ if (segment === '.' || segment === '') continue
182
+ if (segment !== '..') depth++
183
+ else if (--depth < 0) return true
184
+ }
185
+ return false
186
+ }
187
+
188
+ // A target is a resolved-file string, or a { platform: file } Map under --metro.
189
+ function importTargetsEqual(a, b) {
190
+ const aMap = a instanceof Map
191
+ if (aMap !== (b instanceof Map)) return false
192
+ if (!aMap) return a === b
193
+ if (a.size !== b.size) return false
194
+ for (const [platform, file] of a) if (b.get(platform) !== file) return false
195
+ return true
196
+ }
197
+
198
+ const fmtTarget = (t) => (t instanceof Map ? `{${[...t].map(([p, f]) => `${p}: ${f}`).join(', ')}}` : t)
199
+
200
+ // Merge two import maps (conditions -> parent -> specifier -> target); a redirect conflict throws.
201
+ export function mergeImportMaps(a, b, label) {
202
+ const out = new Map()
203
+ const absorb = (imports) => {
204
+ for (const [conditions, byParent] of imports) {
205
+ let outByParent = out.get(conditions)
206
+ if (outByParent === undefined) out.set(conditions, (outByParent = new Map()))
207
+ for (const [parent, specs] of byParent) {
208
+ let outSpecs = outByParent.get(parent)
209
+ if (outSpecs === undefined) outByParent.set(parent, (outSpecs = new Map()))
210
+ for (const [spec, target] of specs) {
211
+ if (outSpecs.has(spec)) {
212
+ assert(importTargetsEqual(outSpecs.get(spec), target),
213
+ `${label}: import '${spec}' from '${parent}' resolves differently ('${fmtTarget(outSpecs.get(spec))}' vs '${fmtTarget(target)}')`)
214
+ } else {
215
+ outSpecs.set(spec, target instanceof Map ? new Map(target) : target)
216
+ }
217
+ }
218
+ }
219
+ }
220
+ }
221
+ absorb(a)
222
+ absorb(b)
223
+ return out
224
+ }
225
+
226
+ export function mergeFormatMaps(a, b, label) {
227
+ const out = new Map()
228
+ const absorb = (formats) => {
229
+ for (const [file, format] of formats) {
230
+ const currentFormat = out.get(file)
231
+ if (currentFormat === undefined) {
232
+ out.set(file, format)
233
+ continue
234
+ }
235
+ try {
236
+ out.set(file, reconcileFormat(format, currentFormat, file))
237
+ } catch (cause) {
238
+ throw new Error(`${label}: ${cause.message}`, { cause })
239
+ }
240
+ }
241
+ }
242
+ absorb(a)
243
+ absorb(b)
244
+ return out
245
+ }
246
+
247
+ // A union, EXCEPT that `b` (the INCOMING, newer artifact -- every call site passes it on the right) is
248
+ // authoritative for its own files: a since-lost execute bit is cleared, not resurrected by the union.
249
+ export function mergeExecutableSets(a, b, bModules, scope) {
250
+ if (a.size === 0) return new Set(b)
251
+ const bFiles = moduleFileKeys(bModules, { scope })
252
+ const out = new Set(b)
253
+ for (const file of a) if (!bFiles.has(file)) out.add(file)
254
+ return out
255
+ }
256
+
257
+ // Result `files` objects are null-prototype, so a `__proto__` file name is a plain own key.
258
+ export function mergeModuleMaps(a, b, label) {
259
+ const out = new Map()
260
+ const absorb = (modules) => {
261
+ for (const [dir, info] of modules) {
262
+ const existing = out.get(dir)
263
+ if (existing === undefined) {
264
+ out.set(dir, {
265
+ name: info.name,
266
+ version: info.version,
267
+ ...(info.ecosystem === undefined ? {} : { ecosystem: info.ecosystem }),
268
+ files: Object.assign(Object.create(null), info.files),
269
+ })
270
+ continue
271
+ }
272
+ assert(existing.name === info.name,
273
+ `${label}: module '${dir}' name mismatch ('${existing.name}' vs '${info.name}')`)
274
+ assert(existing.version === info.version,
275
+ `${label}: module '${dir}' version mismatch ('${existing.version}' vs '${info.version}')`)
276
+ assert(existing.ecosystem === info.ecosystem,
277
+ `${label}: module '${dir}' ecosystem mismatch ('${existing.ecosystem ?? '(none)'}' vs '${info.ecosystem ?? '(none)'}')`)
278
+ for (const [rel, value] of Object.entries(info.files)) {
279
+ if (Object.hasOwn(existing.files, rel)) {
280
+ assert(existing.files[rel] === value, `${label}: content mismatch for '${moduleFileKey(dir, rel)}'`)
281
+ } else {
282
+ existing.files[rel] = value
283
+ }
284
+ }
285
+ }
286
+ }
287
+ absorb(a)
288
+ absorb(b)
289
+ return out
290
+ }
291
+
292
+ // A full path SEGMENT, NOT a bare substring (`foo_node_modules/dep` is a source dir, not a bucket).
293
+ export function hasNodeModulesSegment(path) {
294
+ return path.split('/').includes('node_modules')
295
+ }
296
+
297
+ // Deepest segment-aligned `node_modules/` marker, or -1: `lastIndexOf` alone would also match a bare
298
+ // substring (`foo_node_modules/`), so a candidate only counts at the start or after a `/`.
299
+ function lastNodeModulesMarker(path) {
300
+ const marker = 'node_modules/'
301
+ let idx = path.length
302
+ while ((idx = path.lastIndexOf(marker, idx - 1)) !== -1) {
303
+ if (idx === 0 || path[idx - 1] === '/') return idx
304
+ }
305
+ return -1
306
+ }
307
+
308
+ export function splitNodeModulesPath(path) {
309
+ const marker = 'node_modules/'
310
+ const idx = lastNodeModulesMarker(path)
311
+ if (idx === -1) return null
312
+ const after = idx + marker.length
313
+ const parts = path.slice(after).split('/')
314
+ const pkgLen = parts[0].startsWith('@') ? 2 : 1
315
+ if (parts.length <= pkgLen || parts.slice(0, pkgLen).some((p) => !p)) return null
316
+ const name = parts.slice(0, pkgLen).join('/')
317
+ return { dir: path.slice(0, after) + name, rel: parts.slice(pkgLen).join('/'), name }
318
+ }
package/src/bundle.js CHANGED
@@ -17,7 +17,7 @@ import {
17
17
  posixPathEscapes,
18
18
  sortPaths,
19
19
  splitNodeModulesPath,
20
- } from './util.js'
20
+ } from './artifact-util.js'
21
21
 
22
22
  const VERSION = 1
23
23
  const LEGACY_VERSION = 0
package/src/lockfile.js CHANGED
@@ -1,4 +1,4 @@
1
- import { KNOWN_FORMATS, assert, serializeExecutable, fileMapToObject, fileSetToObject, fromEntries, hasNodeModulesSegment, isPlainObject, mergeExecutableSets, mergeFormatMaps, mergeImportMaps, mergeModuleMaps, moduleFileKeys, parseExecutable, posixPathEscapes, sortPaths } from './util.js'
1
+ import { KNOWN_FORMATS, assert, serializeExecutable, fileMapToObject, fileSetToObject, fromEntries, hasNodeModulesSegment, isPlainObject, mergeExecutableSets, mergeFormatMaps, mergeImportMaps, mergeModuleMaps, moduleFileKeys, parseExecutable, posixPathEscapes, sortPaths } from './artifact-util.js'
2
2
 
3
3
  const VERSION = 0
4
4
 
package/src/shard.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // resolution edges; content, listings, stat kinds and exec bits are re-derived from the root's OWN disk, so
3
3
  // a shard cannot inject content or a forged bit. `files` and `formats` are independent -- don't collapse them.
4
4
 
5
- import { KNOWN_FORMATS, assert, fileMapToObject, isPlainObject, objectToMaps, posixPathEscapes } from './util.js'
5
+ import { KNOWN_FORMATS, assert, fileMapToObject, isPlainObject, objectToMaps, posixPathEscapes } from './artifact-util.js'
6
6
 
7
7
  export const SHARD_VERSION = 1
8
8
 
package/src/state.js CHANGED
@@ -851,7 +851,7 @@ export class State {
851
851
  noupsert(this.hashes, file, integrity)
852
852
  if (this.config.childProcess) this.#observed.add(file) // only a child's shardSnapshot reads it; skip when the channel is off
853
853
  if (this.config.bundle && reason !== null) {
854
- this.#recordReason(reason, file) // provenance for the bundle's `reason` field (null = not a consumer observation, e.g. backfill)
854
+ this.#recordReason(reason, file) // provenance for the bundle's `reason` field (null = caller derives attribution itself, see includePackageJson)
855
855
  // Track HOW 'run' saw this file so #bundleReason can drop files run merely fs-READ post-plugin.
856
856
  if (reason === 'run') {
857
857
  if (!fsRead) this.#runImported.add(file)
@@ -1524,10 +1524,20 @@ export class State {
1524
1524
  let pkg
1525
1525
  try { pkg = JSON.parse(buf.toString()) } catch { continue } // malformed manifest for an untouched bucket: skip
1526
1526
  if (pkg?.name !== module.name || pkg?.version !== module.version) continue
1527
- toAdd.push({ rel, buf })
1527
+ // The manifest rides along BECAUSE some consumer bundled the bucket, so it inherits every
1528
+ // consumer that recorded a file of the bucket -- nothing observed the manifest itself, but an
1529
+ // unattributed file can't be sorted by provenance downstream. Derived BEFORE the adds below,
1530
+ // so one fold's manifest can't count as another bucket's consumer evidence.
1531
+ const files = Object.keys(module.files)
1532
+ const consumers = []
1533
+ for (const [who, recorded] of this.#reasonFiles) {
1534
+ if (files.some((f) => recorded.has(moduleFileKey(dir, f)))) consumers.push(who)
1535
+ }
1536
+ toAdd.push({ rel, buf, consumers })
1528
1537
  }
1529
- for (const { rel, buf } of toAdd) {
1538
+ for (const { rel, buf, consumers } of toAdd) {
1530
1539
  this.addFile(pathToFileURL(resolve(this.root, rel)).toString(), { source: buf, reason: null })
1540
+ for (const who of consumers) this.#recordReason(who, rel)
1531
1541
  }
1532
1542
  }
1533
1543
 
@@ -1748,7 +1758,10 @@ export class State {
1748
1758
  assert.ok(isStasisCoreFile(file),
1749
1759
  `state.write() backfill refused: '${file}' is not a stasis-core source file ` +
1750
1760
  `(expected '.../@exodus/stasis-core/src/<name>.js')`)
1751
- this.addFile(pathToFileURL(resolve(this.root, file)).toString(), { reason: null }) // backfill is a write-time self-containment step, not a consumer observation -- don't attribute it
1761
+ // reason 'run': Node evaluated these before registerHooks could observe it (that blindness is
1762
+ // why this backfill exists), so they're run-loaded modules like any other. A consumer-less
1763
+ // record can't be sorted by provenance downstream (app code vs toolchain).
1764
+ this.addFile(pathToFileURL(resolve(this.root, file)).toString(), { reason: 'run' })
1752
1765
  }
1753
1766
 
1754
1767
  // Seeded from the missing files + every stasis-core file already in sources, to walk their
package/src/util.js CHANGED
@@ -1,33 +1,20 @@
1
1
  import { isUtf8 } from 'node:buffer'
2
2
  import * as fs from 'node:fs'
3
- import { basename, isAbsolute, join, posix, relative } from 'node:path'
3
+ import { basename, isAbsolute, join, relative } from 'node:path'
4
4
  import { parseArgs } from 'node:util'
5
5
 
6
+ import { NODE_FORMATS } from './artifact-util.js'
7
+
8
+ // The Node-side half of the util split: byte/name classification for the capture walks, fs/execute-bit
9
+ // observation and CLI parsing. The pure artifact data model (formats universe, keys, merges,
10
+ // executable-set rules, converters) lives in artifact-util.js; re-exported here so
11
+ // `@exodus/stasis-core/util` keeps serving the full set.
12
+ export * from './artifact-util.js'
13
+
6
14
  // Snapshot before `stasis run --fs` patches fs: the patched realpathSync doesn't throw ENOENT (which
7
15
  // assertRealPathWithinBase relies on) and patched statSync answers with synthetic Stats modes.
8
16
  const { realpathSync, statSync } = fs
9
17
 
10
- const sep = '/'
11
-
12
- // KNOWN_FORMATS is the closed universe of `format` strings; parsers reject anything outside it.
13
- export const NODE_FORMATS = new Set(['module', 'commonjs', 'json', 'module-typescript', 'commonjs-typescript'])
14
- export const SOURCE_LANGUAGE_FORMATS = new Set(['solidity', 'php', 'shell', 'rust'])
15
- export const NATIVE_BUILD_FORMATS = new Set([
16
- 'java', 'kotlin', 'gradle', 'objc', 'objcpp', 'swift', 'c', 'cpp', 'c-header', 'cpp-header',
17
- 'ruby', 'cmake', 'podspec', 'podfile', 'podfile-lock', 'template', 'xml', 'env', 'fastlane', 'pbxproj',
18
- ])
19
- export const RESOURCE_FORMATS = new Set(['resource', 'resource:base64'])
20
- export const STAT_FORMATS = new Set(['stat:file', 'stat:directory'])
21
- export const KNOWN_FORMATS = new Set([
22
- ...NODE_FORMATS,
23
- ...SOURCE_LANGUAGE_FORMATS,
24
- ...NATIVE_BUILD_FORMATS,
25
- ...RESOURCE_FORMATS,
26
- 'patch', // a `.patch` unified diff (pnpm patchedDependencies, patch-package): UTF-8 text applied by a patch step
27
- 'directory',
28
- ...STAT_FORMATS,
29
- ])
30
-
31
18
  // JS_UNRESOLVED_EXTS (.js/.ts type/syntax-dependent, .jsx/.tsx transformed) classify as null, not a format.
32
19
  const NODE_EXT_FORMATS = new Map([
33
20
  ['mjs', 'module'], ['cjs', 'commonjs'], ['json', 'json'],
@@ -36,23 +23,6 @@ const NODE_EXT_FORMATS = new Map([
36
23
  const JS_UNRESOLVED_EXTS = new Set(['js', 'ts', 'jsx', 'tsx'])
37
24
  export const CODE_EXTENSIONS = new Set([...NODE_EXT_FORMATS.keys(), ...JS_UNRESOLVED_EXTS])
38
25
 
39
- // Payload-free stat records: attest a path's KIND, no content, and yield to a real format.
40
- export const isStatFormat = (format) => STAT_FORMATS.has(format)
41
-
42
- // Gates reconcileFormat: a stat record only reconciles with a real format of the SAME kind.
43
- const formatKind = (format) => (format === 'directory' || format === 'stat:directory' ? 'directory' : 'file')
44
-
45
- // A weak 'stat:*' yields to a real format of the SAME kind and never displaces one; anything else throws.
46
- export function reconcileFormat(format, currentFormat, name) {
47
- if (format === currentFormat) return format
48
- const stat = isStatFormat(format)
49
- const currentStat = isStatFormat(currentFormat)
50
- if (stat !== currentStat && formatKind(format) === formatKind(currentFormat)) {
51
- return stat ? currentFormat : format
52
- }
53
- throw new Error(`format conflict for '${name}' ('${currentFormat}' vs '${format}')`)
54
- }
55
-
56
26
  export function pathExt(filePath) {
57
27
  const m = /\.([^./\\]+)$/.exec(filePath)
58
28
  return m ? m[1].toLowerCase() : ''
@@ -356,24 +326,6 @@ export function extSetsEqual(a, b) {
356
326
  return true
357
327
  }
358
328
 
359
- // Flat project-relative key. `rel === ''` (a `directory` capture whose path IS a module root) keys the
360
- // bare dir, never `${dir}/` -- a trailing slash breaks the round-trip.
361
- export function moduleFileKey(dir, rel) {
362
- if (rel === '') return dir
363
- return dir === '.' ? rel : `${dir}/${rel}`
364
- }
365
-
366
- // The keys a module map records -- the set an artifact's `executable` must be a subset of. `scope` MUST
367
- // be the artifact's own: a non-full-scope artifact records only its node_modules buckets.
368
- export function moduleFileKeys(modules, { scope = 'full' } = {}) {
369
- const keys = new Set()
370
- for (const [dir, { files }] of modules) {
371
- if (scope !== 'full' && !hasNodeModulesSegment(dir)) continue
372
- for (const rel of Object.keys(files)) keys.add(moduleFileKey(dir, rel))
373
- }
374
- return keys
375
- }
376
-
377
329
  export const EXECUTE_BITS = 0o111
378
330
 
379
331
  export const isExecutableMode = (stats) => stats.isFile() && (stats.mode & EXECUTE_BITS) !== 0
@@ -398,61 +350,6 @@ export const isExecutableFile = (abs) => observeExecutable(abs) === true
398
350
  // "the bit was removed" and strip what a POSIX capture attested.
399
351
  export const canObserveExecuteBits = ({ win32 = process.platform === 'win32' } = {}) => !win32
400
352
 
401
- // THE rule an artifact's `executable` entry must satisfy; returns the problem, or null when legal.
402
- // parseExecutable (read), assertExecutable (write) and narrowExecutable share it so they cannot drift.
403
- function executableEntryProblem(file, { what, files, formats, scope }) {
404
- if (posixPathEscapes(file)) return 'escapes the root'
405
- if (scope !== 'full' && !hasNodeModulesSegment(file)) {
406
- return `is outside node_modules, which a '${scope}'-scope ${what} does not record`
407
- }
408
- if (!files.has(file)) return `names no file the ${what} records`
409
- const format = formats.get(file)
410
- if (format === 'directory') return 'is a directory capture, not a file'
411
- if (isStatFormat(format)) return `is a payload-free '${format}' record, not a file`
412
- return null
413
- }
414
-
415
- // Applied at every write site, so the in-memory artifact is honest before it is ever serialized.
416
- export function narrowExecutable(executable, { modules, formats, scope }) {
417
- const out = new Set()
418
- if (executable.size === 0) return out
419
- const files = moduleFileKeys(modules, { scope })
420
- for (const file of executable) {
421
- if (executableEntryProblem(file, { what: 'artifact', files, formats, scope }) === null) out.add(file)
422
- }
423
- return out
424
- }
425
-
426
- function assertExecutable(executable, { what, files, formats, scope }) {
427
- for (const file of executable) {
428
- const problem = executableEntryProblem(file, { what, files, formats, scope })
429
- assert(problem === null, `${what}: executable entry '${file}' ${problem}`)
430
- }
431
- }
432
-
433
- export function parseExecutable(list, { what, files, formats, scope = 'full' }) {
434
- if (list === undefined) return new Set()
435
- const at = `${what}: executable`
436
- assert(Array.isArray(list), `${at} must be an array of file paths`)
437
- const out = new Set()
438
- for (const file of list) {
439
- assert(typeof file === 'string' && file !== '', `${at} entry must be a non-empty string`)
440
- // A dupe would silently collapse in out.add() and round-trip to different bytes.
441
- assert(!out.has(file), `${at} entry '${file}' is listed twice`)
442
- out.add(file)
443
- }
444
- assertExecutable(out, { what, files, formats, scope })
445
- return out
446
- }
447
-
448
- // The one choke point every producer goes through, so a write site that forgot to narrow fails HERE, not
449
- // on the next read. Omitted when empty: keeps every pre-`executable` artifact byte-identical when rewritten.
450
- export function serializeExecutable(executable, { what, modules, formats, scope }) {
451
- if (executable.size === 0) return undefined
452
- assertExecutable(executable, { what, files: moduleFileKeys(modules, { scope }), formats, scope })
453
- return fileSetToObject(executable)
454
- }
455
-
456
353
  // Throws on a symlink escaping the bundle root: a crafted `link.sh -> /etc/passwd` must not pull an
457
354
  // external file into an attestable bundle. realpathSync surfaces ENOENT, which loaders treat as "missing".
458
355
  export function assertRealPathWithinBase(realBase, baseDir, relPath) {
@@ -462,197 +359,3 @@ export function assertRealPathWithinBase(realBase, baseDir, relPath) {
462
359
  throw new Error(`Refusing to follow symlink escaping bundle root: ${relPath} -> ${real}`)
463
360
  }
464
361
  }
465
-
466
- export function assert(condition, msg) {
467
- if (!condition) throw new Error(msg)
468
- }
469
-
470
- export function sortPaths(a, b) {
471
- const [al, bl] = [a.split(sep), b.split(sep)]
472
- while (al.length > 0 && al[0] === bl[0]) {
473
- al.shift()
474
- bl.shift()
475
- }
476
- if (al.length === 0 && bl.length === 0) return 0
477
-
478
- // First process each file in dir, then subdirs
479
- if (al.length < 2) return bl.length < 2 && al > bl ? 1 : -1
480
- if (bl.length < 2) return 1
481
-
482
- if (al[0] === '*') return -1
483
- if (bl[0] === '*') return 1
484
-
485
- if (al[0] === 'node_modules') return 1
486
- if (bl[0] === 'node_modules') return -1
487
-
488
- // Prefer example/ over example-something/
489
- const [an, bn] = [al, bl].map((list) => list.join(String.fromCodePoint(0)))
490
- if (an < bn) return -1
491
- if (an > bn) return 1
492
- throw new Error('Unreachable')
493
- }
494
-
495
- export const isPlainObject = (x) => x && [null, Object.prototype].includes(Object.getPrototypeOf(x))
496
-
497
- export const fromEntries = (entries) => Object.setPrototypeOf(Object.fromEntries(entries), null)
498
-
499
- export const fileSetToObject = (set) => [...set].toSorted((a, b) => sortPaths(a, b))
500
-
501
- // `sorted: false` only for a machine-only payload whose reader does not care about ordering (shard.js).
502
- export const fileMapToObject = (map, { sorted = true } = {}) => {
503
- if (!sorted) {
504
- const out = Object.create(null)
505
- for (const [k, v] of map) out[k] = v instanceof Map ? fileMapToObject(v, { sorted }) : v
506
- return out
507
- }
508
- return fromEntries(
509
- [...map]
510
- .toSorted((a, b) => sortPaths(a[0], b[0]))
511
- .map(([k, v]) => [k, v instanceof Map ? fileMapToObject(v, { sorted }) : v])
512
- )
513
- }
514
-
515
- export const objectToMaps = (obj) => new Map(
516
- Object.entries(obj).map(([k, v]) => [k, isPlainObject(v) ? objectToMaps(v) : v])
517
- )
518
-
519
- // Normalizes first: plain `startsWith('..')` would miss a mid-path `..` that pops above the root.
520
- export function posixPathEscapes(path) {
521
- // Exact prefilter: normalize() can only yield a '..'-prefixed result from an input containing '..',
522
- // or an absolute one from an input already absolute.
523
- if (!path.includes('..') && !posix.isAbsolute(path)) return false
524
- const normalized = posix.normalize(path)
525
- return normalized === '..' || normalized.startsWith('../') || posix.isAbsolute(normalized)
526
- }
527
-
528
- // A target is a resolved-file string, or a { platform: file } Map under --metro.
529
- function importTargetsEqual(a, b) {
530
- const aMap = a instanceof Map
531
- if (aMap !== (b instanceof Map)) return false
532
- if (!aMap) return a === b
533
- if (a.size !== b.size) return false
534
- for (const [platform, file] of a) if (b.get(platform) !== file) return false
535
- return true
536
- }
537
-
538
- const fmtTarget = (t) => (t instanceof Map ? `{${[...t].map(([p, f]) => `${p}: ${f}`).join(', ')}}` : t)
539
-
540
- // Merge two import maps (conditions -> parent -> specifier -> target); a redirect conflict throws.
541
- export function mergeImportMaps(a, b, label) {
542
- const out = new Map()
543
- const absorb = (imports) => {
544
- for (const [conditions, byParent] of imports) {
545
- let outByParent = out.get(conditions)
546
- if (outByParent === undefined) out.set(conditions, (outByParent = new Map()))
547
- for (const [parent, specs] of byParent) {
548
- let outSpecs = outByParent.get(parent)
549
- if (outSpecs === undefined) outByParent.set(parent, (outSpecs = new Map()))
550
- for (const [spec, target] of specs) {
551
- if (outSpecs.has(spec)) {
552
- assert(importTargetsEqual(outSpecs.get(spec), target),
553
- `${label}: import '${spec}' from '${parent}' resolves differently ('${fmtTarget(outSpecs.get(spec))}' vs '${fmtTarget(target)}')`)
554
- } else {
555
- outSpecs.set(spec, target instanceof Map ? new Map(target) : target)
556
- }
557
- }
558
- }
559
- }
560
- }
561
- absorb(a)
562
- absorb(b)
563
- return out
564
- }
565
-
566
- export function mergeFormatMaps(a, b, label) {
567
- const out = new Map()
568
- const absorb = (formats) => {
569
- for (const [file, format] of formats) {
570
- const currentFormat = out.get(file)
571
- if (currentFormat === undefined) {
572
- out.set(file, format)
573
- continue
574
- }
575
- try {
576
- out.set(file, reconcileFormat(format, currentFormat, file))
577
- } catch (cause) {
578
- throw new Error(`${label}: ${cause.message}`, { cause })
579
- }
580
- }
581
- }
582
- absorb(a)
583
- absorb(b)
584
- return out
585
- }
586
-
587
- // A union, EXCEPT that `b` (the INCOMING, newer artifact -- every call site passes it on the right) is
588
- // authoritative for its own files: a since-lost execute bit is cleared, not resurrected by the union.
589
- export function mergeExecutableSets(a, b, bModules, scope) {
590
- if (a.size === 0) return new Set(b)
591
- const bFiles = moduleFileKeys(bModules, { scope })
592
- const out = new Set(b)
593
- for (const file of a) if (!bFiles.has(file)) out.add(file)
594
- return out
595
- }
596
-
597
- // Result `files` objects are null-prototype, so a `__proto__` file name is a plain own key.
598
- export function mergeModuleMaps(a, b, label) {
599
- const out = new Map()
600
- const absorb = (modules) => {
601
- for (const [dir, info] of modules) {
602
- const existing = out.get(dir)
603
- if (existing === undefined) {
604
- out.set(dir, {
605
- name: info.name,
606
- version: info.version,
607
- ...(info.ecosystem === undefined ? {} : { ecosystem: info.ecosystem }),
608
- files: Object.assign(Object.create(null), info.files),
609
- })
610
- continue
611
- }
612
- assert(existing.name === info.name,
613
- `${label}: module '${dir}' name mismatch ('${existing.name}' vs '${info.name}')`)
614
- assert(existing.version === info.version,
615
- `${label}: module '${dir}' version mismatch ('${existing.version}' vs '${info.version}')`)
616
- assert(existing.ecosystem === info.ecosystem,
617
- `${label}: module '${dir}' ecosystem mismatch ('${existing.ecosystem ?? '(none)'}' vs '${info.ecosystem ?? '(none)'}')`)
618
- for (const [rel, value] of Object.entries(info.files)) {
619
- if (Object.hasOwn(existing.files, rel)) {
620
- assert(existing.files[rel] === value, `${label}: content mismatch for '${moduleFileKey(dir, rel)}'`)
621
- } else {
622
- existing.files[rel] = value
623
- }
624
- }
625
- }
626
- }
627
- absorb(a)
628
- absorb(b)
629
- return out
630
- }
631
-
632
- // A full path SEGMENT, NOT a bare substring (`foo_node_modules/dep` is a source dir, not a bucket).
633
- export function hasNodeModulesSegment(path) {
634
- return path.split('/').includes('node_modules')
635
- }
636
-
637
- // Deepest segment-aligned `node_modules/` marker, or -1: `lastIndexOf` alone would also match a bare
638
- // substring (`foo_node_modules/`), so a candidate only counts at the start or after a `/`.
639
- function lastNodeModulesMarker(path) {
640
- const marker = 'node_modules/'
641
- let idx = path.length
642
- while ((idx = path.lastIndexOf(marker, idx - 1)) !== -1) {
643
- if (idx === 0 || path[idx - 1] === '/') return idx
644
- }
645
- return -1
646
- }
647
-
648
- export function splitNodeModulesPath(path) {
649
- const marker = 'node_modules/'
650
- const idx = lastNodeModulesMarker(path)
651
- if (idx === -1) return null
652
- const after = idx + marker.length
653
- const parts = path.slice(after).split('/')
654
- const pkgLen = parts[0].startsWith('@') ? 2 : 1
655
- if (parts.length <= pkgLen || parts.slice(0, pkgLen).some((p) => !p)) return null
656
- const name = parts.slice(0, pkgLen).join('/')
657
- return { dir: path.slice(0, after) + name, rel: parts.slice(pkgLen).join('/'), name }
658
- }