@exodus/stasis-core 1.0.0-beta.14 → 1.0.0-beta.16

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.
@@ -1,11 +1,7 @@
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.
1
+ // Imports no Node builtin, so `./bundle` and `./lockfile` load in any JS runtime; util.js re-exports it all.
5
2
 
6
- // KNOWN_FORMATS is the closed universe of `format` strings; parsers reject anything outside it.
7
3
  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'])
4
+ export const SOURCE_LANGUAGE_FORMATS = new Set(['solidity', 'php', 'shell', 'rust', 'teal'])
9
5
  export const NATIVE_BUILD_FORMATS = new Set([
10
6
  'java', 'kotlin', 'gradle', 'objc', 'objcpp', 'swift', 'c', 'cpp', 'c-header', 'cpp-header',
11
7
  'ruby', 'python', 'cmake', 'podspec', 'podfile', 'podfile-lock', 'template', 'xml', 'env', 'fastlane', 'pbxproj',
@@ -22,67 +18,40 @@ export const KNOWN_FORMATS = new Set([
22
18
  ...STAT_FORMATS,
23
19
  ])
24
20
 
25
- // Payload-free stat records: attest a path's KIND, no content, and yield to a real format.
26
21
  export const isStatFormat = (format) => STAT_FORMATS.has(format)
27
22
 
28
- // Reserved synthetic file a disabled import (a `browser`/`react-native` field mapping it to `false`)
29
- // points at: never on disk, carried as empty CommonJS so the edge resolves to attested bytes.
23
+ // A disabled import's target (a `browser` field mapping it to `false`): never on disk, carried as empty CommonJS.
30
24
  export const EMPTY_MODULE_PATH = '.stasis/empty-module.js'
31
25
 
32
- // The post-erasure family of a '-typescript' loader format ('module-typescript' -> 'module'): a
33
- // transforming preload (`stasis run --import tsx`) resolves/serves a TypeScript file as the JS
34
- // family it transpiles to, while the attestation keeps the on-disk format. undefined otherwise.
35
26
  export const erasedTypeScriptFormat = (format) =>
36
27
  format === 'module-typescript' ? 'module' : format === 'commonjs-typescript' ? 'commonjs' : undefined
37
28
 
38
29
  const formatKind = (format) => (format === 'directory' || format === 'stat:directory' ? 'directory' : 'file')
39
30
 
40
- // A weak 'stat:*' yields to a real format of the SAME kind and never displaces one; anything else throws.
41
31
  export function reconcileFormat(format, currentFormat, name) {
42
32
  if (format === currentFormat) return format
43
33
  const stat = isStatFormat(format)
44
- if (stat !== isStatFormat(currentFormat) && formatKind(format) === formatKind(currentFormat)) {
45
- return stat ? currentFormat : format
46
- }
34
+ if (stat !== isStatFormat(currentFormat) && formatKind(format) === formatKind(currentFormat)) return stat ? currentFormat : format
47
35
  throw new Error(`format conflict for '${name}' ('${currentFormat}' vs '${format}')`)
48
36
  }
49
37
 
50
- // Flat project-relative key. `rel === ''` (a `directory` capture whose path IS a module root) keys the
51
- // bare dir, never `${dir}/` -- a trailing slash breaks the round-trip.
52
- export function moduleFileKey(dir, rel) {
53
- if (rel === '') return dir
54
- return dir === '.' ? rel : `${dir}/${rel}`
55
- }
38
+ // `rel === ''` (a `directory` capture whose path IS a module root) keys the bare dir: `${dir}/` breaks the round-trip.
39
+ export const moduleFileKey = (dir, rel) => (rel === '' ? dir : dir === '.' ? rel : `${dir}/${rel}`)
56
40
 
57
- // A file's key inside its bucket `dir`, of its flat project-relative `path` below it: the inverse of moduleFileKey.
58
41
  export const fileInBucket = (dir, path) => (dir === '.' ? path : path.slice(dir.length + 1))
59
42
 
60
- // GitHub `owner/name` (owner 1-39, name 1-100 chars).
61
43
  const GITHUB_REPO = /^(?=[A-Za-z0-9-]{1,39}\/)[A-Za-z0-9](?:-?[A-Za-z0-9])*\/(?!\.\.?$)[\w.-]{1,100}$/u
62
- // Non-empty normalized repo-relative path of URL-safe segments.
63
44
  const REPO_DIRECTORY = /^(?!\.\.?(?:\/|$))[\w.~@+-]+(?:\/(?!\.\.?(?:\/|$))[\w.~@+-]+)*$/u
64
- // Full lowercase SHA-1 or SHA-256.
65
45
  const GIT_SHA = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/u
66
- // The fields of a `repo`, the bundle's own or a dependency's, and their checks.
67
46
  export const REPO_FIELDS = {
68
47
  github: (v) => typeof v === 'string' && GITHUB_REPO.test(v),
69
- // `''` is the repository's root.
70
48
  directory: (v) => typeof v === 'string' && v.length <= 1024 && (v === '' || REPO_DIRECTORY.test(v)),
71
49
  commit: (v) => typeof v === 'string' && GIT_SHA.test(v),
72
50
  }
73
51
 
74
- // Whether two GitHub `owner/name`s are one: GitHub's names are case-insensitive.
75
52
  export const sameGithub = (a, b) => a?.toLowerCase() === b?.toLowerCase()
76
53
 
77
- // Whether two `repo`s name the same place: every field alike, `github` in any case.
78
- export const sameRepo = (a, b) => Object.keys(REPO_FIELDS).every((key) => (key === 'github' ? sameGithub(a.github, b.github) : a[key] === b[key]))
79
-
80
- // Whether two records of one dependency agree on its `repo`: an artifact from before the field records none.
81
- export const reposAgree = (a, b) => a === undefined || b === undefined || sameRepo(a, b)
82
-
83
- // Validate a block against its `fields` (each optional: a check, or a nested block's fields), `what`
84
- // naming it in errors; canonical, frozen, undefined if empty. Messages are built on failure alone: a
85
- // dependency's `repo` is checked on every parse and write.
54
+ // Messages are built on failure alone: a dependency's `repo` is checked on every parse and write.
86
55
  export const normalizeBlock = (block, fields, what) => {
87
56
  if (block === undefined) return undefined
88
57
  if (!isPlainObject(block)) assert(false, `${what} must be an object`)
@@ -95,71 +64,78 @@ export const normalizeBlock = (block, fields, what) => {
95
64
  return entries.length === 0 ? undefined : Object.freeze(fromEntries(entries))
96
65
  }
97
66
 
98
- // Validate a `repo` (all fields optional); canonical key order, undefined if empty.
99
67
  export const normalizeRepo = (repo, what = 'bundle repo') => normalizeBlock(repo, REPO_FIELDS, what)
100
68
 
101
- // A bucket's ecosystem as a dependency's: its `ecosystem` tag, or npm for an untagged one under
102
- // node_modules (an artifact from before the tag); undefined for first-party code.
69
+ // An untagged node_modules bucket is npm's: an artifact from before the `ecosystem` tag records none.
103
70
  export const dependencyEcosystem = (dir, ecosystem) => ecosystem ?? (hasNodeModulesSegment(dir) ? 'npm' : undefined)
104
71
 
105
- // A dependency's `repo`, the one its own manifest names, with the bundle's own fields, `github`
106
- // required: a `directory` or `commit` places nothing without the repository it is in. First-party
107
- // code carries none.
108
- const normalizeModuleRepo = (dir, { ecosystem, repo }, what) => {
109
- if (repo === undefined) return undefined
110
- if (dependencyEcosystem(dir, ecosystem) === undefined) assert(false, `${what}: '${dir}' is no dependency's bucket, and carries no repo`)
111
- const normalized = normalizeRepo(repo, `${what} module '${dir}' repo`)
112
- if (normalized?.github === undefined) assert(false, `${what} module '${dir}' repo has no github`)
72
+ // The normalizer of a record's `field` block: only a bucket `carries` takes (a `kind` one) holds it, `required` set.
73
+ const blockMetadata = (field, fields, required, carries, kind) => (dir, { ecosystem, [field]: block }, what) => {
74
+ if (block === undefined) return undefined
75
+ if (!carries(dir, ecosystem)) assert(false, `${what}: '${dir}' is no ${kind} bucket, and carries no ${field}`)
76
+ const normalized = normalizeBlock(block, fields, `${what} module '${dir}' ${field}`)
77
+ if (normalized?.[required] === undefined) assert(false, `${what} module '${dir}' ${field} has no ${required}`)
113
78
  return normalized
114
79
  }
115
80
 
116
- // A package name or version: characters some ecosystem uses there (npm's legacy `~'!()*` too), so not space or `"#$%&,:;<=>?[\]^`{|}`.
81
+ // `github` is required: a `directory` or `commit` places nothing without the repository it is in.
82
+ const normalizeModuleRepo = blockMetadata('repo', REPO_FIELDS, 'github',
83
+ (dir, ecosystem) => dependencyEcosystem(dir, ecosystem) !== undefined, "dependency's")
84
+
85
+ const SRI = { sha1: /^sha1-[A-Za-z\d+/]{27}=$/u, sha256: /^sha256-[A-Za-z\d+/]{43}=$/u, sha512: /^sha512-[A-Za-z\d+/]{86}==$/u }
86
+ const PATCH_FIELDS = { integrity: (v) => typeof v === 'string' && SRI.sha256.test(v) }
87
+
88
+ export function sha512Of(integrity) {
89
+ const found = new Set((integrity ?? '').split(/\s+/u).filter((token) => token.startsWith('sha512-')).map((token) => token.split('?')[0]))
90
+ return found.size === 1 ? [...found][0] : undefined
91
+ }
92
+ export const tarballIntegrityOf = (integrity) => {
93
+ const one = sha512Of(integrity)
94
+ return SRI.sha512.test(one ?? '') ? one : undefined
95
+ }
96
+
97
+ export const isNpmDependency = (dir, ecosystem) => hasNodeModulesSegment(dir) && dependencyEcosystem(dir, ecosystem) === 'npm'
98
+
99
+ export const ARCHIVE_ALGORITHM = { npm: 'sha512', cargo: 'sha256', composer: 'sha1', soldeer: 'sha256' }
100
+
101
+ const normalizeModuleIntegrity = (dir, { ecosystem, integrity }, what) => {
102
+ if (integrity === undefined) return undefined
103
+ const of = dependencyEcosystem(dir, ecosystem)
104
+ if (!Object.hasOwn(ARCHIVE_ALGORITHM, of) || (of === 'npm' && !hasNodeModulesSegment(dir))) assert(false, `${what}: '${dir}' is no npm, cargo, composer or soldeer dependency's bucket, and carries no integrity`)
105
+ if (typeof integrity !== 'string' || !SRI[ARCHIVE_ALGORITHM[of]].test(integrity)) assert(false, `invalid ${what} module '${dir}' integrity: ${JSON.stringify(integrity)}`)
106
+ return integrity
107
+ }
108
+
109
+ const normalizeModulePatch = blockMetadata('patch', PATCH_FIELDS, 'integrity', isNpmDependency, "npm dependency's")
110
+
117
111
  export const isPackageString = (v) => typeof v === 'string' && /^[\w.+@/~'!()*-]+$/u.test(v)
118
112
  export const PACKAGE_BLOCK = { name: isPackageString, version: isPackageString }
119
113
 
120
- // A directory key of a record's `vendored` or `subpackages`: an in-bucket path in canonical form, through
121
- // no `node_modules` (a package installed there is a bucket of its own).
114
+ // Through no `node_modules`: a package installed there is a bucket of its own.
122
115
  const isBucketDir = (dir) => typeof dir === 'string' && dir !== '' && !dir.includes('\\') &&
123
116
  dir.split('/').every((segment) => segment !== '' && segment !== '.' && segment !== '..' && segment !== 'node_modules')
124
117
 
125
- // Whether a package.json naming `manifestName`, at `dir` (package-relative, POSIX) inside the package named
126
- // `hostName`, is one of its subpackages: a subpath entry point with a package.json of its own, named in
127
- // the package's namespace for the very directory it is in (`@hookform/resolvers/zod` at zod/), as
128
- // microbundle lays them out. It is the package's own code, and its `version`, a placeholder (1.0.0), its
129
- // own: held to nothing.
130
118
  export const isSubpackage = (manifestName, hostName, dir) =>
131
119
  typeof manifestName === 'string' && isBucketDir(dir) && manifestName === `${hostName}/${dir}`
132
120
 
133
- // A record's per-directory metadata, each entry the `name` (required) and `version` of the package.json in
134
- // that directory: `vendored`, the packages copied into a dependency (any name), and `subpackages`, its own
135
- // subpath entry points (isSubpackage).
136
121
  const DIR_METADATA = {
137
122
  vendored: () => true,
138
123
  subpackages: (entry, name, sub) => isSubpackage(entry.name, name, sub),
139
124
  }
140
125
 
141
- // The directory of `entries` a bucket file `rel` is in: the deepest one listed that holds it.
142
- const entryDirOf = (entries, rel) => {
126
+ export const entryDirOf = (entries, rel) => {
143
127
  for (let slash = rel.lastIndexOf('/'); slash > 0; slash = rel.lastIndexOf('/', slash - 1)) {
144
128
  if (Object.hasOwn(entries, rel.slice(0, slash))) return rel.slice(0, slash)
145
129
  }
146
130
  return undefined
147
131
  }
148
132
 
149
- // The entries of a `vendored` or `subpackages` that hold one of `files`, each file counted for the deepest
150
- // entry holding it; path-sorted and frozen, undefined if none. An artifact lists those it carries a file of.
151
133
  function carriedEntries(entries, files) {
152
134
  if (entries === undefined) return undefined
153
- const carried = new Set()
154
- for (const rel of Object.keys(files)) {
155
- const dir = entryDirOf(entries, rel)
156
- if (dir !== undefined) carried.add(dir)
157
- }
135
+ const carried = new Set(Object.keys(files).map((rel) => entryDirOf(entries, rel)).filter((dir) => dir !== undefined))
158
136
  return carried.size === 0 ? undefined : Object.freeze(fromEntries(sortedByPath(carried).map((dir) => [dir, entries[dir]])))
159
137
  }
160
138
 
161
- // A dependency's `field` of DIR_METADATA, validated: each entry holds one of the record's `files`. First-party
162
- // code carries none.
163
139
  const normalizeDirMetadata = (field, dir, { name, ecosystem, files, [field]: entries }, what) => {
164
140
  if (entries === undefined) return undefined
165
141
  if (dependencyEcosystem(dir, ecosystem) === undefined) assert(false, `${what}: '${dir}' is no dependency's bucket, and carries no ${field}`)
@@ -172,80 +148,84 @@ const normalizeDirMetadata = (field, dir, { name, ecosystem, files, [field]: ent
172
148
  return [sub, entry]
173
149
  }))
174
150
  const carried = carriedEntries(normalized, files)
175
- for (const sub of Object.keys(normalized)) {
176
- if (!Object.hasOwn(carried ?? {}, sub)) assert(false, `${what} module '${dir}' ${field} '${sub}' holds none of its files`)
177
- }
151
+ const idle = Object.keys(normalized).find((sub) => !Object.hasOwn(carried ?? {}, sub))
152
+ if (idle !== undefined) assert(false, `${what} module '${dir}' ${field} '${idle}' holds none of its files`)
178
153
  return carried
179
154
  }
180
155
 
181
- // A module bucket record in canonical key order; `ecosystem`, `repo`, `vendored` and `subpackages` are omitted (not undefined) when absent.
182
- export const moduleInfo = ({ name, version, ecosystem, repo, vendored, subpackages, files }) => ({
183
- name, version,
184
- ...(ecosystem === undefined ? {} : { ecosystem }),
185
- ...(repo === undefined ? {} : { repo }),
186
- ...(vendored === undefined ? {} : { vendored }),
187
- ...(subpackages === undefined ? {} : { subpackages }),
188
- files,
189
- })
190
-
191
- // `record` with the metadata `other` (of the same bucket) has and it lacks: `repo` where it records none,
192
- // the `vendored` and `subpackages` entries of directories it lists none for. Metadata is held to nothing,
193
- // so where both record one, `record`'s stands. `record` itself when that adds nothing.
194
- export function withMetadataOf(record, other) {
195
- const repo = record.repo ?? other.repo
196
- const fields = Object.keys(DIR_METADATA).filter((field) =>
197
- other[field] !== undefined && Object.keys(other[field]).some((sub) => !Object.hasOwn(record[field] ?? {}, sub)))
198
- if (repo === record.repo && fields.length === 0) return record
199
- const merged = fromEntries(fields.map((field) => [field, Object.freeze(fromEntries([...Object.entries(other[field]), ...Object.entries(record[field] ?? {})]))]))
200
- return moduleInfo({ ...record, repo, ...merged })
201
- }
202
-
203
- // A record's `vendored` and `subpackages` as `what` writes them: the entries holding one of `files`, validated;
204
- // a field not in `fields` left out.
205
- const carriedDirMetadata = (dir, info, files, what, fields) => fromEntries(Object.keys(DIR_METADATA).map((field) =>
206
- [field, fields.includes(field) ? normalizeDirMetadata(field, dir, { ...info, files, [field]: carriedEntries(info[field], files) }, what) : undefined]))
207
-
208
- // The packages a module map records, for what looks at each of them (`stasis audit`): every record, as it is,
209
- // and after a dependency's, each `vendored` copy in it as a record of its own, at the copy's directory in the
210
- // project (`node_modules/next/dist/compiled/react-is`): its `name` and `version`, its host's `ecosystem`,
211
- // `vendoredIn` its host's dir, and `files`, those the copy holds (each counted for the deepest entry holding
212
- // it), relative to that directory. A host keeps all its files, the copies' too: they are shipped as its.
213
- // File values are carried over as they are (a contents-free bundle's locked ones unread).
214
- export function* packageRecords(modules) {
215
- for (const [dir, record] of modules) {
216
- yield [dir, record]
217
- if (record.vendored === undefined) continue
218
- const held = new Map()
219
- for (const rel of Object.keys(record.files)) {
220
- const sub = entryDirOf(record.vendored, rel)
221
- if (sub === undefined) continue
222
- if (!held.has(sub)) held.set(sub, Object.create(null))
223
- Object.defineProperty(held.get(sub), rel.slice(sub.length + 1), Object.getOwnPropertyDescriptor(record.files, rel))
224
- }
225
- for (const [sub, files] of held) {
226
- const { name, version } = record.vendored[sub]
227
- yield [moduleFileKey(dir, sub), { name, version, ecosystem: record.ecosystem, vendoredIn: dir, files }]
228
- }
156
+ const RECORD_METADATA = { integrity: normalizeModuleIntegrity, repo: normalizeModuleRepo, patch: normalizeModulePatch }
157
+ const DIR_FIELDS = Object.keys(DIR_METADATA)
158
+ const MODULE_METADATA = [...Object.keys(RECORD_METADATA), ...DIR_FIELDS]
159
+ const OPTIONAL_KEYS = ['ecosystem', ...MODULE_METADATA]
160
+
161
+ // Canonical key order; `ecosystem` and the metadata are omitted (not undefined) when absent.
162
+ export const moduleInfo = (info) => {
163
+ const record = { name: info.name, version: info.version }
164
+ for (const key of OPTIONAL_KEYS) if (info[key] !== undefined) record[key] = info[key]
165
+ record.files = info.files
166
+ return record
167
+ }
168
+
169
+ // How a record's package was installed: two records of one bucket must agree on it.
170
+ export const ATTESTED_METADATA = ['integrity', 'patch']
171
+
172
+ export const attestsAny = (record) => ATTESTED_METADATA.some((field) => record[field] !== undefined)
173
+ export const withAttestedOf = (record, from) => moduleInfo({ ...record, ...Object.fromEntries(ATTESTED_METADATA.map((field) => [field, from[field]])) })
174
+
175
+ // A record of an integrity and no patch attests the patch null (installed unpatched); undefined: it tells nothing.
176
+ export const attestedOf = (record, field) => record[field] ?? (field === 'patch' && record.integrity !== undefined ? null : undefined)
177
+
178
+ const agree = (field, record, other, what) => {
179
+ const [ours, theirs] = [record[field], other[field]].map((value) => JSON.stringify(value) ?? 'none')
180
+ if (ours !== theirs) assert(false, `${what} ${field} mismatch (${ours} vs ${theirs})`)
181
+ }
182
+
183
+ // What `record` attests must be `other`'s: `other` recording none of it is a mismatch too.
184
+ export function assertAttested(record, other, what) {
185
+ for (const field of ATTESTED_METADATA) if (attestedOf(record, field) !== undefined) agree(field, record, other, what)
186
+ }
187
+
188
+ export function withMetadataOf(record, other, what = 'module') {
189
+ let added
190
+ for (const field of Object.keys(RECORD_METADATA)) {
191
+ if (ATTESTED_METADATA.includes(field) && attestedOf(record, field) !== undefined && attestedOf(other, field) !== undefined) agree(field, record, other, what)
192
+ else if (record[field] === undefined && other[field] !== undefined) (added ??= {})[field] = other[field]
193
+ }
194
+ for (const field of DIR_FIELDS) {
195
+ if (other[field] === undefined || Object.keys(other[field]).every((sub) => Object.hasOwn(record[field] ?? {}, sub))) continue
196
+ ;(added ??= {})[field] = Object.freeze(fromEntries([...Object.entries(other[field]), ...Object.entries(record[field] ?? {})]))
197
+ }
198
+ return added === undefined ? record : moduleInfo({ ...record, ...added })
199
+ }
200
+
201
+ export function heldToAccounts(modules, accounts, prefix = '') {
202
+ for (const [dir, account] of accounts) {
203
+ if (modules.has(dir)) assertAttested(modules.get(dir), account, `${prefix}module '${dir}' (recorded vs ${account.locked ? 'locked' : 'installed'})`)
229
204
  }
230
205
  }
231
206
 
232
- // A parsed bucket `dir` of a `what` artifact: an absent version has one spelling (a literal null folds
233
- // into undefined so identity comparisons and JSON round-trips can't split on it), `repo`, `vendored` and
234
- // `subpackages` are validated, and `files` is null-prototype.
235
- export function normalizeModule({ name, version, ecosystem, repo, vendored, subpackages, files }, dir, what) {
207
+ function metadataOf(dir, info, what, fields, entriesOf) {
208
+ const metadata = {}
209
+ for (const [field, normalize] of Object.entries(RECORD_METADATA)) metadata[field] = fields.includes(field) ? normalize(dir, info, what) : undefined
210
+ for (const field of DIR_FIELDS) metadata[field] = fields.includes(field) ? normalizeDirMetadata(field, dir, { ...info, [field]: entriesOf(field) }, what) : undefined
211
+ return metadata
212
+ }
213
+
214
+ // A literal null version folds into undefined, so identity comparisons and JSON round-trips can't split on it.
215
+ export function normalizeModule(info, dir, what, fields = MODULE_METADATA) {
216
+ const { name, version, ecosystem } = info
236
217
  assert(ecosystem === undefined || typeof ecosystem === 'string')
237
- files = fromEntries(Object.entries(files))
218
+ // An array or a string would read as files named by index ('0', '1'...).
219
+ if (!isPlainObject(info.files)) assert(false, `${what}: module '${dir}' files must be an object`)
220
+ const files = fromEntries(Object.entries(info.files))
238
221
  return moduleInfo({
239
222
  name, version: version ?? undefined, ecosystem,
240
- repo: normalizeModuleRepo(dir, { ecosystem, repo }, what),
241
- vendored: normalizeDirMetadata('vendored', dir, { name, ecosystem, files, vendored }, what),
242
- subpackages: normalizeDirMetadata('subpackages', dir, { name, ecosystem, files, subpackages }, what),
223
+ ...metadataOf(dir, { ...info, files }, what, fields, (field) => info[field]),
243
224
  files,
244
225
  })
245
226
  }
246
227
 
247
- // The keys a module map records -- the set an artifact's `executable` must be a subset of. `scope` MUST
248
- // be the artifact's own: a non-full-scope artifact records only its node_modules buckets.
228
+ // `scope` MUST be the artifact's own: a non-full-scope artifact records only its node_modules buckets.
249
229
  export function moduleFileKeys(modules, { scope = 'full' } = {}) {
250
230
  const keys = new Set()
251
231
  for (const [dir, { files }] of modules) {
@@ -255,14 +235,10 @@ export function moduleFileKeys(modules, { scope = 'full' } = {}) {
255
235
  return keys
256
236
  }
257
237
 
258
- // THE rule an artifact's `executable` entry must satisfy; returns the problem, or null when legal.
259
- // parseExecutable (read), assertExecutable (write) and narrowExecutable share it so they cannot drift.
260
238
  function executableEntryProblem(file, { what, files, formats, scope }) {
261
239
  if (file.includes('\\')) return "holds a '\\'"
262
240
  if (posixPathEscapes(file)) return 'escapes the root'
263
- if (scope !== 'full' && !hasNodeModulesSegment(file)) {
264
- return `is outside node_modules, which a '${scope}'-scope ${what} does not record`
265
- }
241
+ if (scope !== 'full' && !hasNodeModulesSegment(file)) return `is outside node_modules, which a '${scope}'-scope ${what} does not record`
266
242
  if (!files.has(file)) return `names no file the ${what} records`
267
243
  const format = formats.get(file)
268
244
  if (format === 'directory') return 'is a directory capture, not a file'
@@ -270,15 +246,10 @@ function executableEntryProblem(file, { what, files, formats, scope }) {
270
246
  return null
271
247
  }
272
248
 
273
- // Applied at every write site, so the in-memory artifact is honest before it is ever serialized.
274
249
  export function narrowExecutable(executable, { modules, formats, scope }) {
275
- const out = new Set()
276
- if (executable.size === 0) return out
250
+ if (executable.size === 0) return new Set()
277
251
  const files = moduleFileKeys(modules, { scope })
278
- for (const file of executable) {
279
- if (executableEntryProblem(file, { what: 'artifact', files, formats, scope }) === null) out.add(file)
280
- }
281
- return out
252
+ return new Set(executable.values().filter((file) => executableEntryProblem(file, { what: 'artifact', files, formats, scope }) === null))
282
253
  }
283
254
 
284
255
  function assertExecutable(executable, { what, files, formats, scope }) {
@@ -288,23 +259,28 @@ function assertExecutable(executable, { what, files, formats, scope }) {
288
259
  }
289
260
  }
290
261
 
291
- export function parseExecutable(list, { what, files, formats, scope = 'full' }) {
292
- if (list === undefined) return new Set()
293
- const at = `${what}: executable`
262
+ function parsePathList(list, at, entry, check) {
294
263
  assert(Array.isArray(list), `${at} must be an array of file paths`)
295
264
  const out = new Set()
296
- for (const file of list) {
297
- assert(typeof file === 'string' && file !== '', `${at} entry must be a non-empty string`)
298
- // A dupe would silently collapse in out.add() and round-trip to different bytes.
299
- assert(!out.has(file), `${at} entry '${file}' is listed twice`)
300
- out.add(file)
265
+ for (const path of list) {
266
+ check(path)
267
+ // A dupe would silently collapse in the Set and round-trip to different bytes.
268
+ assert(!out.has(path), `${entry} '${path}' is listed twice`)
269
+ out.add(path)
301
270
  }
271
+ return out
272
+ }
273
+
274
+ export function parseExecutable(list, { what, files, formats, scope = 'full' }) {
275
+ if (list === undefined) return new Set()
276
+ const entry = `${what}: executable entry`
277
+ const out = parsePathList(list, `${what}: executable`, entry, (file) =>
278
+ assert(typeof file === 'string' && file !== '', `${entry} must be a non-empty string`))
302
279
  assertExecutable(out, { what, files, formats, scope })
303
280
  return out
304
281
  }
305
282
 
306
- // The one choke point every producer goes through, so a write site that forgot to narrow fails HERE, not
307
- // on the next read. Omitted when empty: keeps every pre-`executable` artifact byte-identical when rewritten.
283
+ // Omitted when empty, so every pre-`executable` artifact rewrites byte-identical.
308
284
  export function serializeExecutable(executable, { what, modules, formats, scope }) {
309
285
  if (executable.size === 0) return undefined
310
286
  assertExecutable(executable, { what, files: moduleFileKeys(modules, { scope }), formats, scope })
@@ -315,19 +291,23 @@ export function assert(condition, msg) {
315
291
  if (!condition) throw new Error(msg)
316
292
  }
317
293
 
318
- // stasis's lockfile, by name at any depth and in any case: a case-insensitive disk resolves `STASIS.LOCK.JSON` to it too.
319
- export const isLockfileName = (path) => path.slice(path.lastIndexOf('/') + 1).toLowerCase() === 'stasis.lock.json'
294
+ export const LOCK_FILE = 'stasis.lock.json'
295
+ export const CONFIG_FILE = 'stasis.config.json'
296
+ export const BUNDLE_FILE = 'stasis.code.br'
297
+
298
+ export const SCOPES = new Set(['node_modules', 'full'])
299
+ export const assertScope = (scope, what) => assert(SCOPES.has(scope), `${what} scope must be node_modules or full, got ${JSON.stringify(scope)}`)
300
+
301
+ // In any case: a case-insensitive disk resolves `STASIS.LOCK.JSON` to it too.
302
+ export const isLockfileName = (path) => path.slice(path.lastIndexOf('/') + 1).toLowerCase() === LOCK_FILE
320
303
 
321
- // sortPaths' order, of two paths as split at '/' (read, not changed).
322
304
  function compareSegments(al, bl) {
323
- // The segments from `i` on are where the two differ.
324
305
  let i = 0
325
306
  while (i < al.length && al[i] === bl[i]) i++
326
307
  const ar = al.length - i
327
308
  const br = bl.length - i
328
309
  if (ar === 0 && br === 0) return 0
329
310
 
330
- // First process each file in dir, then subdirs
331
311
  if (ar < 2) return br < 2 && (al[i] ?? '') > (bl[i] ?? '') ? 1 : -1
332
312
  if (br < 2) return 1
333
313
 
@@ -345,18 +325,20 @@ function compareSegments(al, bl) {
345
325
  throw new Error('Unreachable')
346
326
  }
347
327
 
348
- export function sortPaths(a, b) {
349
- return compareSegments(a.split('/'), b.split('/'))
350
- }
328
+ export const sortPaths = (a, b) => compareSegments(a.split('/'), b.split('/'))
351
329
 
352
- // `items` in sortPaths' order of `keyOf(item)`, each key split once, not at each of the sort's comparisons.
330
+ // Each key split once, not at each of the sort's comparisons.
353
331
  export const sortedByPath = (items, keyOf = (item) => item) => Array.from(items, (item) => ({ item, segments: keyOf(item).split('/') }))
354
332
  .toSorted((a, b) => compareSegments(a.segments, b.segments))
355
333
  .map(({ item }) => item)
356
334
 
357
335
  const entryKey = ([key]) => key
358
336
 
359
- export const isPlainObject = (x) => x && [null, Object.prototype].includes(Object.getPrototypeOf(x))
337
+ export function isPlainObject(x) {
338
+ if (!x) return false
339
+ const proto = Object.getPrototypeOf(x)
340
+ return proto === null || proto === Object.prototype
341
+ }
360
342
 
361
343
  export const fromEntries = (entries) => Object.setPrototypeOf(Object.fromEntries(entries), null)
362
344
 
@@ -368,21 +350,21 @@ export const fileMapToObject = (map, { sorted = true } = {}) => {
368
350
  return fromEntries(entries.map(([k, v]) => [k, v instanceof Map ? fileMapToObject(v, { sorted }) : v]))
369
351
  }
370
352
 
371
- // The Map at `map.get(key)`, created on first use.
372
- export const nestedMap = (map, key) => map.get(key) ?? map.set(key, new Map()).get(key)
353
+ export function upsert(map, key, make) {
354
+ let value = map.get(key)
355
+ if (value === undefined) map.set(key, (value = make()))
356
+ return value
357
+ }
358
+
359
+ export const nestedMap = (map, key) => upsert(map, key, () => new Map())
373
360
 
374
361
  export const objectToMaps = (obj) => new Map(
375
362
  Object.entries(obj).map(([k, v]) => [k, isPlainObject(v) ? objectToMaps(v) : v])
376
363
  )
377
364
 
378
- // True for an absolute path or any `..` hop that pops above the root, INCLUDING a mid-path one
379
- // (`a/../../x`) that plain `startsWith('..')` would miss. A segment walk (not posix.normalize, whose
380
- // verdict it matches -- see posix-path-escapes.test.js) so this module stays free of node:path:
381
- // `.` and empty segments are skipped exactly as normalize collapses them, a real segment pushes, and
382
- // a `..` with nothing left to pop is an escape -- normalize would keep it as a leading `..` forever.
365
+ // A segment walk, not posix.normalize, so this module stays free of node:path; a mid-path `..` (`a/../../x`) counts.
383
366
  export function posixPathEscapes(path) {
384
367
  if (path.startsWith('/')) return true
385
- // The walk can only return true via a literal '..' segment, so the ordinary keys skip the split.
386
368
  if (!path.includes('..')) return false
387
369
  let depth = 0
388
370
  for (const segment of path.split('/')) {
@@ -393,49 +375,28 @@ export function posixPathEscapes(path) {
393
375
  return false
394
376
  }
395
377
 
396
- // THE rule every path an artifact records must satisfy (a file key's too, see canonicalFileKey): in
397
- // the root, and free of `\` -- part of a name off Windows, which stasis refuses everywhere rather than
398
- // take for another path, and on Windows a separator a `..\x` would climb out by.
378
+ // `\` is part of a name off Windows, refused rather than taken for another path; on Windows `..\x` would climb out.
399
379
  export const isRefusedPath = (path) => path.includes('\\') || posixPathEscapes(path)
400
380
 
401
- // Throws unless `path`, one of `what`'s, is a path an artifact may record (see isRefusedPath).
402
381
  export const assertArtifactPath = (path, what) =>
403
382
  assert(!isRefusedPath(path), `${what}: path '${path}' escapes the root or holds a '\\'`)
404
383
 
405
- // THE rule an artifact's entry must satisfy, on read (parseEntries) and on write (serializeEntries).
406
- const assertEntry = (entry, what) =>
407
- assert(typeof entry === 'string' && entry !== '' && !isRefusedPath(entry), `${what}: invalid entry ${JSON.stringify(entry)}`)
384
+ export const isArtifactKey = (key) => typeof key === 'string' && key !== '' && !isRefusedPath(key)
385
+ const assertEntry = (entry, what) => assert(isArtifactKey(entry), `${what}: invalid entry ${JSON.stringify(entry)}`)
408
386
 
409
- // An artifact's `entries` list as a Set: each a non-empty in-root path, listed once (a dupe would
410
- // collapse in the Set and round-trip to different bytes).
411
- export function parseEntries(list, what) {
412
- assert(Array.isArray(list), `${what}: entries must be an array of file paths`)
413
- const out = new Set()
414
- for (const entry of list) {
415
- assertEntry(entry, what)
416
- assert(!out.has(entry), `${what}: entry '${entry}' is listed twice`)
417
- out.add(entry)
418
- }
419
- return out
420
- }
387
+ export const parseEntries = (list, what) => parsePathList(list, `${what}: entries`, `${what}: entry`, (entry) => assertEntry(entry, what))
421
388
 
422
- // The `entries` list an artifact writes: the same rule as on read, so an in-memory construct can't
423
- // serialize what parse would reject; path-sorted.
424
389
  export function serializeEntries(entries, what) {
425
390
  for (const entry of entries) assertEntry(entry, what)
426
391
  return fileSetToObject(entries)
427
392
  }
428
393
 
429
- // THE rule an artifact's `formats` entry must satisfy, on read (parseFormats) and on write
430
- // (serializeFormats): a path an artifact may record, and a known format.
431
394
  function assertFormat(file, format) {
432
395
  assertArtifactPath(file, 'formats')
433
396
  assert(KNOWN_FORMATS.has(format), `unknown format '${format}' for ${file}`)
434
397
  }
435
398
 
436
- // An artifact's `formats` object as a validated Map. '' and '.' alias to the same key (older
437
- // artifacts keyed the root listing ''); normalized, failing closed on dupes, and an unknown format
438
- // is rejected at the schema boundary so a tampered artifact fails closed.
399
+ // '' and '.' alias to one key: older artifacts keyed the root listing ''.
439
400
  export function parseFormats(json) {
440
401
  assert(isPlainObject(json))
441
402
  const formats = new Map()
@@ -448,26 +409,19 @@ export function parseFormats(json) {
448
409
  return formats
449
410
  }
450
411
 
451
- // A payload-free `stat:*` record attests a path's kind alone: an artifact that also carries content
452
- // for its path is malformed, rejected on read and never written, as nothing would hold those bytes
453
- // (a reader taking the record as content-free skips them, another would serve or write them).
412
+ // A `stat:*` record attests a path's kind alone: one reader would skip content carried for its path, another serve it.
454
413
  export function assertPayloadFree(formats, files, what) {
455
414
  for (const [file, format] of formats) {
456
415
  if (isStatFormat(format) && files.has(file)) assert(false, `${what}: '${file}' is a payload-free '${format}' record, yet carries content`)
457
416
  }
458
417
  }
459
418
 
460
- // The `formats` an artifact writes: the same rule as on read, so an in-memory construct can't serialize
461
- // what parse would reject; path-sorted.
462
419
  export function serializeFormats(formats) {
463
420
  for (const [file, format] of formats) assertFormat(file, format)
464
421
  return fileMapToObject(formats)
465
422
  }
466
423
 
467
- // THE rules an artifact's `imports` (conditions -> parent -> specifier -> target, as nested Maps)
468
- // must satisfy, on read (parseImports) and on write (serializeImports). Paths escaping the root (incl.
469
- // mid-path `a/../../x`) or holding a `\` are rejected: getImport resolves against the root at load. A
470
- // target is a file, or (--metro) a non-empty { platform: file } map.
424
+ // getImport resolves each path against the root at load. A target is a file, or (--metro) a { platform: file } map.
471
425
  function assertImports(imports) {
472
426
  for (const [, byParent] of imports) {
473
427
  assert(byParent instanceof Map)
@@ -490,7 +444,6 @@ function assertImports(imports) {
490
444
  }
491
445
  }
492
446
 
493
- // An artifact's `imports` object as nested Maps (see assertImports).
494
447
  export function parseImports(json) {
495
448
  assert(isPlainObject(json))
496
449
  const imports = objectToMaps(json)
@@ -498,19 +451,13 @@ export function parseImports(json) {
498
451
  return imports
499
452
  }
500
453
 
501
- // The `imports` an artifact writes: the same rules as on read (see assertImports); path-sorted.
502
454
  export function serializeImports(imports) {
503
455
  assertImports(imports)
504
456
  return fileMapToObject(imports)
505
457
  }
506
458
 
507
- // A module map as the serialized `modules` (node_modules buckets) and `sources` (the rest) objects
508
- // of a `what` artifact, buckets and files path-sorted so the bytes are canonical. Each record's
509
- // `vendored` and `subpackages` keep the entries holding one of the files it carries. `metadata` names the
510
- // metadata fields written (a lockfile carries `vendored` alone, so it can be audited on its own), or is
511
- // `true` for every one and `false` for none, as the option once was.
512
459
  export function groupModules(modules, { skipEmpty = false, metadata: written = true, what } = {}) {
513
- const metadata = written === true ? ['repo', ...Object.keys(DIR_METADATA)] : written === false ? [] : written
460
+ const metadata = written === true ? MODULE_METADATA : written === false ? [] : written
514
461
  const grouped = { modules: [], sources: [] }
515
462
  for (const [dir, info] of modules) {
516
463
  if (skipEmpty && Object.keys(info.files).length === 0) continue
@@ -519,31 +466,53 @@ export function groupModules(modules, { skipEmpty = false, metadata: written = t
519
466
  const files = fromEntries(sortedByPath(Object.entries(info.files), entryKey))
520
467
  grouped[inNodeModules ? 'modules' : 'sources'].push([dir, moduleInfo({
521
468
  ...info,
522
- repo: metadata.includes('repo') ? normalizeModuleRepo(dir, info, what) : undefined,
523
- ...carriedDirMetadata(dir, info, files, what, metadata),
469
+ ...metadataOf(dir, { ...info, files }, what, metadata, (field) => carriedEntries(info[field], files)),
524
470
  files,
525
471
  })])
526
472
  }
527
473
  return { modules: fromEntries(sortedByPath(grouped.modules, entryKey)), sources: fromEntries(sortedByPath(grouped.sources, entryKey)) }
528
474
  }
529
475
 
530
- // A target is a resolved-file string, or a { platform: file } Map under --metro.
531
- // Whether two import targets are the same: a file, or a per-platform Map<platform, file> compared structurally.
476
+ export function parseBuckets(json, what, fields) {
477
+ const modules = new Map()
478
+ for (const key of json.config.scope === 'full' ? ['modules', 'sources'] : ['modules']) {
479
+ if (key === 'modules' && json.modules === undefined) continue
480
+ assert(isPlainObject(json[key]), `${what}: ${key} must be an object`)
481
+ const inNodeModules = key === 'modules'
482
+ for (const [dir, info] of Object.entries(json[key])) {
483
+ assert(hasNodeModulesSegment(dir) === inNodeModules)
484
+ assertArtifactPath(dir, what)
485
+ assert(info?.name && (info.version || !inNodeModules) && info.files)
486
+ modules.set(dir, normalizeModule(info, dir, what, fields))
487
+ }
488
+ }
489
+ return modules
490
+ }
491
+
492
+ // Each part is undefined where it is none, which JSON.stringify drops. An in-memory lockfile's formats may be null.
493
+ export function serializedParts({ config: { scope }, entries, modules, formats, imports, executable }, what, group) {
494
+ const full = scope === 'full'
495
+ const grouped = groupModules(modules, { ...group, what })
496
+ return {
497
+ entries: full ? serializeEntries(entries, what) : undefined,
498
+ sources: full ? grouped.sources : undefined,
499
+ modules: grouped.modules,
500
+ formats: formats === null ? undefined : serializeFormats(formats),
501
+ imports: imports === null ? undefined : serializeImports(imports),
502
+ executable: serializeExecutable(executable, { what, modules, formats: formats ?? new Map(), scope }),
503
+ }
504
+ }
505
+
532
506
  export function importTargetsEqual(a, b) {
533
- const aMap = a instanceof Map
534
- if (aMap !== (b instanceof Map)) return false
535
- if (!aMap) return a === b
536
- if (a.size !== b.size) return false
537
- for (const [platform, file] of a) if (b.get(platform) !== file) return false
538
- return true
507
+ if (!(a instanceof Map) || !(b instanceof Map)) return a === b
508
+ return a.size === b.size && a.entries().every(([platform, file]) => b.get(platform) === file)
539
509
  }
540
510
 
541
511
  const fmtTarget = (t) => (t instanceof Map ? `{${[...t].map(([p, f]) => `${p}: ${f}`).join(', ')}}` : t)
542
512
 
543
- // Merge two import maps (conditions -> parent -> specifier -> target); a redirect conflict throws.
544
513
  export function mergeImportMaps(a, b, label) {
545
514
  const out = new Map()
546
- const absorb = (imports) => {
515
+ for (const imports of [a, b]) {
547
516
  for (const [conditions, byParent] of imports) {
548
517
  const outByParent = nestedMap(out, conditions)
549
518
  for (const [parent, specs] of byParent) {
@@ -559,57 +528,38 @@ export function mergeImportMaps(a, b, label) {
559
528
  }
560
529
  }
561
530
  }
562
- absorb(a)
563
- absorb(b)
564
531
  return out
565
532
  }
566
533
 
534
+ export function upsertFormat(map, file, format) {
535
+ const currentFormat = map.get(file)
536
+ map.set(file, currentFormat === undefined ? format : reconcileFormat(format, currentFormat, file))
537
+ }
538
+
567
539
  export function mergeFormatMaps(a, b, label) {
568
540
  const out = new Map()
569
- const absorb = (formats) => {
570
- for (const [file, format] of formats) {
571
- const currentFormat = out.get(file)
572
- if (currentFormat === undefined) {
573
- out.set(file, format)
574
- continue
575
- }
576
- try {
577
- out.set(file, reconcileFormat(format, currentFormat, file))
578
- } catch (cause) {
579
- throw new Error(`${label}: ${cause.message}`, { cause })
580
- }
581
- }
541
+ try {
542
+ for (const formats of [a, b]) for (const [file, format] of formats) upsertFormat(out, file, format)
543
+ } catch (cause) {
544
+ throw new Error(`${label}: ${cause.message}`, { cause })
582
545
  }
583
- absorb(a)
584
- absorb(b)
585
546
  return out
586
547
  }
587
548
 
588
- // A union, EXCEPT that `b` (the INCOMING, newer artifact -- every call site passes it on the right) is
589
- // authoritative for its own files: a since-lost execute bit is cleared, not resurrected by the union.
590
- export function mergeExecutableSets(a, b, bModules, scope) {
591
- if (a.size === 0) return new Set(b)
592
- const bFiles = moduleFileKeys(bModules, { scope })
593
- const out = new Set(b)
594
- for (const file of a) if (!bFiles.has(file)) out.add(file)
595
- return out
596
- }
549
+ // `b`, the INCOMING newer artifact, is authoritative for its own files: a since-lost execute bit is cleared.
550
+ export const mergeExecutableSets = (a, b, bModules, scope) =>
551
+ (a.size === 0 ? new Set(b) : new Set(b).union(a.difference(moduleFileKeys(bModules, { scope }))))
597
552
 
598
- // An empty, '.' or '..' path segment.
599
553
  const NON_CANONICAL_SEGMENT = /(?:^|\/)\.{0,2}(?:\/|$)/u
600
554
 
601
- // The flat key of `rel` in bucket `dir`; throws unless canonical ('.' only as the root listing, rel '')
602
- // and free of `\`, which no artifact path holds: off Windows it is part of a name, refused everywhere.
555
+ // '.' is canonical only as the root listing (rel ''). Messages are built only on failure: this runs for every file.
603
556
  export function canonicalFileKey(dir, rel, what) {
604
557
  const key = moduleFileKey(dir, rel)
605
- // Message built only on failure: this runs for every file.
606
558
  if ((key !== '.' || rel !== '') && NON_CANONICAL_SEGMENT.test(key)) assert(false, `${what}: non-canonical file key ${JSON.stringify(key)}`)
607
559
  if (key.includes('\\')) assert(false, `${what}: file key '${key}' holds a '\\', which no path may`)
608
560
  return key
609
561
  }
610
562
 
611
- // Maps each file's flat key to its bucket; rejects non-canonical keys and refused bucket dirs (an empty
612
- // bucket's too, which no file key would reach), reports duplicates to onDuplicate.
613
563
  export function flatFileKeys(modules, what, onDuplicate) {
614
564
  const owners = new Map()
615
565
  for (const [dir, { files }] of modules) {
@@ -625,16 +575,15 @@ export function flatFileKeys(modules, what, onDuplicate) {
625
575
  return owners
626
576
  }
627
577
 
628
- // The flatFileKeys duplicate handler for an artifact's parse/serialize: two bucket splits flattening
629
- // to one path means module bucketing changed between writes.
630
578
  export const duplicateKeyError = (what, regenerate) => (key) => assert(false,
631
579
  `duplicate file key '${key}' across ${what} buckets -- module bucketing changed between writes ` +
632
580
  `(a workspace package without a version now owns its own bucket); regenerate the ${regenerate}`)
633
581
 
634
- // Result `files` objects are null-prototype, so a `__proto__` file name is a plain own key.
635
- export function mergeModuleMaps(a, b, label) {
582
+ // Result `files` objects are null-prototype, so a `__proto__` file name is a plain own key. With
583
+ // `held`, `b` is held to `a`, not merged into it: what `b` alone attests stays a claim of its own files.
584
+ export function mergeModuleMaps(a, b, label, { held = false } = {}) {
636
585
  const out = new Map()
637
- const absorb = (modules) => {
586
+ for (const modules of [a, b]) {
638
587
  for (const [dir, info] of modules) {
639
588
  const existing = out.get(dir)
640
589
  if (existing === undefined) {
@@ -643,8 +592,6 @@ export function mergeModuleMaps(a, b, label) {
643
592
  }
644
593
  assert(existing.name === info.name,
645
594
  `${label}: module '${dir}' name mismatch ('${existing.name}' vs '${info.name}')`)
646
- // A one-sided absent version is usually a migration skew: older stasis fabricated '0.0.0'
647
- // for a workspace package whose package.json has no version, newer stasis records none.
648
595
  assert(existing.version === info.version,
649
596
  `${label}: module '${dir}' version mismatch ('${existing.version}' vs '${info.version}')` +
650
597
  ((existing.version == null) === (info.version == null) ? '' :
@@ -652,24 +599,21 @@ export function mergeModuleMaps(a, b, label) {
652
599
  `package without one; regenerate it (bundle=replace / lock=replace)`))
653
600
  assert(existing.ecosystem === info.ecosystem,
654
601
  `${label}: module '${dir}' ecosystem mismatch ('${existing.ecosystem ?? '(none)'}' vs '${info.ecosystem ?? '(none)'}')`)
655
- // `repo`, `vendored` and `subpackages` are metadata, held to nothing: either side's, where only one records it, else `a`'s.
656
- const merged = withMetadataOf(existing, info)
657
- if (merged !== existing) out.set(dir, merged)
602
+ // An attested value one side alone records is of its files alone: the other may add none.
603
+ out.set(dir, withMetadataOf(existing, info, `${label}: module '${dir}'`))
604
+ for (const field of ATTESTED_METADATA) {
605
+ if ((existing[field] === undefined) === (info[field] === undefined) || (held && existing[field] === undefined)) continue
606
+ const [attests, adds] = existing[field] === undefined ? [info, existing] : [existing, info]
607
+ const added = Object.keys(adds.files).find((rel) => !Object.hasOwn(attests.files, rel))
608
+ if (added !== undefined) assert(false, `${label}: module '${dir}' ${field} is recorded of one side's files, not of '${moduleFileKey(dir, added)}' the other adds`)
609
+ }
658
610
  for (const [rel, value] of Object.entries(info.files)) {
659
- if (Object.hasOwn(existing.files, rel)) {
660
- assert(existing.files[rel] === value, `${label}: content mismatch for '${moduleFileKey(dir, rel)}'`)
661
- } else {
662
- existing.files[rel] = value
663
- }
611
+ if (!Object.hasOwn(existing.files, rel)) existing.files[rel] = value
612
+ else assert(existing.files[rel] === value, `${label}: content mismatch for '${moduleFileKey(dir, rel)}'`)
664
613
  }
665
614
  }
666
615
  }
667
- absorb(a)
668
- absorb(b)
669
- // One project-relative path must live in exactly one bucket. Bucketing can change between
670
- // releases (a versionless workspace package used to fall through to a parent bucket and now owns
671
- // its own), and per-dir absorption cannot see that: without this check the merge would WRITE an
672
- // artifact that then fails its own next parse on the duplicate-file-key guard.
616
+ // Bucketing can change between releases, which per-dir absorption can't see: never WRITE what the next parse rejects.
673
617
  flatFileKeys(out, label, (key, owner, dir) => assert(false,
674
618
  `${label}: file '${key}' is bucketed under both '${owner}' and '${dir}' -- module bucketing ` +
675
619
  `changed between the artifacts (a workspace package without a version now owns its own ` +
@@ -678,29 +622,28 @@ export function mergeModuleMaps(a, b, label) {
678
622
  }
679
623
 
680
624
  // A full path SEGMENT, NOT a bare substring (`foo_node_modules/dep` is a source dir, not a bucket).
681
- export function hasNodeModulesSegment(path) {
682
- return path.split('/').includes('node_modules')
683
- }
625
+ export const hasNodeModulesSegment = (path) =>
626
+ path === 'node_modules' || path.startsWith('node_modules/') || path.endsWith('/node_modules') || path.includes('/node_modules/')
627
+
628
+ const NODE_MODULES_MARKER = 'node_modules/'
684
629
 
685
- // Deepest segment-aligned `node_modules/` marker, or -1: `lastIndexOf` alone would also match a bare
686
- // substring (`foo_node_modules/`), so a candidate only counts at the start or after a `/`.
630
+ // Segment-aligned: `lastIndexOf` alone would also match a bare substring (`foo_node_modules/`).
687
631
  function lastNodeModulesMarker(path) {
688
- const marker = 'node_modules/'
689
632
  let idx = path.length
690
- while ((idx = path.lastIndexOf(marker, idx - 1)) !== -1) {
633
+ while ((idx = path.lastIndexOf(NODE_MODULES_MARKER, idx - 1)) !== -1) {
691
634
  if (idx === 0 || path[idx - 1] === '/') return idx
692
635
  }
693
636
  return -1
694
637
  }
695
638
 
696
639
  export function splitNodeModulesPath(path) {
697
- const marker = 'node_modules/'
698
640
  const idx = lastNodeModulesMarker(path)
699
641
  if (idx === -1) return null
700
- const after = idx + marker.length
642
+ const after = idx + NODE_MODULES_MARKER.length
701
643
  const parts = path.slice(after).split('/')
702
644
  const pkgLen = parts[0].startsWith('@') ? 2 : 1
703
- if (parts.length <= pkgLen || parts.slice(0, pkgLen).some((p) => !p)) return null
704
- const name = parts.slice(0, pkgLen).join('/')
645
+ const nameParts = parts.slice(0, pkgLen)
646
+ if (parts.length <= pkgLen || nameParts.includes('')) return null
647
+ const name = nameParts.join('/')
705
648
  return { dir: path.slice(0, after) + name, rel: parts.slice(pkgLen).join('/'), name }
706
649
  }