@exodus/stasis-core 1.0.0-beta.9 → 1.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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,64 +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}`)
40
+
41
+ export const fileInBucket = (dir, path) => (dir === '.' ? path : path.slice(dir.length + 1))
56
42
 
57
- // GitHub `owner/name` (owner 1-39, name 1-100 chars).
58
43
  const GITHUB_REPO = /^(?=[A-Za-z0-9-]{1,39}\/)[A-Za-z0-9](?:-?[A-Za-z0-9])*\/(?!\.\.?$)[\w.-]{1,100}$/u
59
- // Non-empty normalized repo-relative path of URL-safe segments.
60
44
  const REPO_DIRECTORY = /^(?!\.\.?(?:\/|$))[\w.~@+-]+(?:\/(?!\.\.?(?:\/|$))[\w.~@+-]+)*$/u
61
- // Full lowercase SHA-1 or SHA-256.
62
45
  const GIT_SHA = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/u
63
- // The fields of a `repo`, the bundle's own or a dependency's, and their checks.
64
46
  export const REPO_FIELDS = {
65
47
  github: (v) => typeof v === 'string' && GITHUB_REPO.test(v),
66
- // `''` is the repository's root.
67
48
  directory: (v) => typeof v === 'string' && v.length <= 1024 && (v === '' || REPO_DIRECTORY.test(v)),
68
49
  commit: (v) => typeof v === 'string' && GIT_SHA.test(v),
69
50
  }
70
51
 
71
- // Whether two GitHub `owner/name`s are one: GitHub's names are case-insensitive.
72
52
  export const sameGithub = (a, b) => a?.toLowerCase() === b?.toLowerCase()
73
53
 
74
- // Whether two `repo`s name the same place: every field alike, `github` in any case.
75
- export const sameRepo = (a, b) => Object.keys(REPO_FIELDS).every((key) => (key === 'github' ? sameGithub(a.github, b.github) : a[key] === b[key]))
76
-
77
- // Whether two records of one dependency agree on its `repo`: an artifact from before the field records none.
78
- export const reposAgree = (a, b) => a === undefined || b === undefined || sameRepo(a, b)
79
-
80
- // Validate a block against its `fields` (each optional: a check, or a nested block's fields), `what`
81
- // naming it in errors; canonical, frozen, undefined if empty. Messages are built on failure alone: a
82
- // 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.
83
55
  export const normalizeBlock = (block, fields, what) => {
84
56
  if (block === undefined) return undefined
85
57
  if (!isPlainObject(block)) assert(false, `${what} must be an object`)
@@ -92,38 +64,168 @@ export const normalizeBlock = (block, fields, what) => {
92
64
  return entries.length === 0 ? undefined : Object.freeze(fromEntries(entries))
93
65
  }
94
66
 
95
- // Validate a `repo` (all fields optional); canonical key order, undefined if empty.
96
67
  export const normalizeRepo = (repo, what = 'bundle repo') => normalizeBlock(repo, REPO_FIELDS, what)
97
68
 
98
- // A bucket's ecosystem as a dependency's: its `ecosystem` tag, or npm for an untagged one under
99
- // 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.
100
70
  export const dependencyEcosystem = (dir, ecosystem) => ecosystem ?? (hasNodeModulesSegment(dir) ? 'npm' : undefined)
101
71
 
102
- // A dependency's `repo`, the one its own manifest names, with the bundle's own fields, `github`
103
- // required: a `directory` or `commit` places nothing without the repository it is in. First-party
104
- // code carries none.
105
- const normalizeModuleRepo = (dir, { ecosystem, repo }, what) => {
106
- if (repo === undefined) return undefined
107
- if (dependencyEcosystem(dir, ecosystem) === undefined) assert(false, `${what}: '${dir}' is no dependency's bucket, and carries no repo`)
108
- const normalized = normalizeRepo(repo, `${what} module '${dir}' repo`)
109
- 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}`)
110
78
  return normalized
111
79
  }
112
80
 
113
- // A module bucket record in canonical key order; `ecosystem` and `repo` are omitted (not undefined) when absent.
114
- export const moduleInfo = ({ name, version, ecosystem, repo, files }) =>
115
- ({ name, version, ...(ecosystem === undefined ? {} : { ecosystem }), ...(repo === undefined ? {} : { repo }), files })
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
+
111
+ export const isPackageString = (v) => typeof v === 'string' && /^[\w.+@/~'!()*-]+$/u.test(v)
112
+ export const PACKAGE_BLOCK = { name: isPackageString, version: isPackageString }
113
+
114
+ // Through no `node_modules`: a package installed there is a bucket of its own.
115
+ const isBucketDir = (dir) => typeof dir === 'string' && dir !== '' && !dir.includes('\\') &&
116
+ dir.split('/').every((segment) => segment !== '' && segment !== '.' && segment !== '..' && segment !== 'node_modules')
117
+
118
+ export const isSubpackage = (manifestName, hostName, dir) =>
119
+ typeof manifestName === 'string' && isBucketDir(dir) && manifestName === `${hostName}/${dir}`
120
+
121
+ const DIR_METADATA = {
122
+ vendored: () => true,
123
+ subpackages: (entry, name, sub) => isSubpackage(entry.name, name, sub),
124
+ }
125
+
126
+ export const entryDirOf = (entries, rel) => {
127
+ for (let slash = rel.lastIndexOf('/'); slash > 0; slash = rel.lastIndexOf('/', slash - 1)) {
128
+ if (Object.hasOwn(entries, rel.slice(0, slash))) return rel.slice(0, slash)
129
+ }
130
+ return undefined
131
+ }
116
132
 
117
- // A parsed bucket `dir` of a `what` artifact: an absent version has one spelling (a literal null folds
118
- // into undefined so identity comparisons and JSON round-trips can't split on it), `repo` is validated,
119
- // and `files` is null-prototype.
120
- export function normalizeModule({ name, version, ecosystem, repo, files }, dir, what) {
133
+ function carriedEntries(entries, files) {
134
+ if (entries === undefined) return undefined
135
+ const carried = new Set(Object.keys(files).map((rel) => entryDirOf(entries, rel)).filter((dir) => dir !== undefined))
136
+ return carried.size === 0 ? undefined : Object.freeze(fromEntries(sortedByPath(carried).map((dir) => [dir, entries[dir]])))
137
+ }
138
+
139
+ const normalizeDirMetadata = (field, dir, { name, ecosystem, files, [field]: entries }, what) => {
140
+ if (entries === undefined) return undefined
141
+ if (dependencyEcosystem(dir, ecosystem) === undefined) assert(false, `${what}: '${dir}' is no dependency's bucket, and carries no ${field}`)
142
+ if (!isPlainObject(entries)) assert(false, `${what} module '${dir}' ${field} must be an object`)
143
+ const normalized = fromEntries(Object.entries(entries).map(([sub, block]) => {
144
+ if (!isBucketDir(sub)) assert(false, `${what} module '${dir}' ${field}: invalid directory ${JSON.stringify(sub)}`)
145
+ const entry = normalizeBlock(block, PACKAGE_BLOCK, `${what} module '${dir}' ${field} '${sub}'`)
146
+ if (entry?.name === undefined) assert(false, `${what} module '${dir}' ${field} '${sub}' has no name`)
147
+ if (!DIR_METADATA[field](entry, name, sub)) assert(false, `${what} module '${dir}' ${field} '${sub}' is named '${entry.name}', not '${name}/${sub}'`)
148
+ return [sub, entry]
149
+ }))
150
+ const carried = carriedEntries(normalized, files)
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`)
153
+ return carried
154
+ }
155
+
156
+ const RECORD_METADATA = { integrity: normalizeModuleIntegrity, patch: normalizeModulePatch, repo: normalizeModuleRepo }
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'})`)
204
+ }
205
+ }
206
+
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
121
217
  assert(ecosystem === undefined || typeof ecosystem === 'string')
122
- return moduleInfo({ name, version: version ?? undefined, ecosystem, repo: normalizeModuleRepo(dir, { ecosystem, repo }, what), 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))
221
+ return moduleInfo({
222
+ name, version: version ?? undefined, ecosystem,
223
+ ...metadataOf(dir, { ...info, files }, what, fields, (field) => info[field]),
224
+ files,
225
+ })
123
226
  }
124
227
 
125
- // The keys a module map records -- the set an artifact's `executable` must be a subset of. `scope` MUST
126
- // 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.
127
229
  export function moduleFileKeys(modules, { scope = 'full' } = {}) {
128
230
  const keys = new Set()
129
231
  for (const [dir, { files }] of modules) {
@@ -133,14 +235,10 @@ export function moduleFileKeys(modules, { scope = 'full' } = {}) {
133
235
  return keys
134
236
  }
135
237
 
136
- // THE rule an artifact's `executable` entry must satisfy; returns the problem, or null when legal.
137
- // parseExecutable (read), assertExecutable (write) and narrowExecutable share it so they cannot drift.
138
238
  function executableEntryProblem(file, { what, files, formats, scope }) {
139
239
  if (file.includes('\\')) return "holds a '\\'"
140
240
  if (posixPathEscapes(file)) return 'escapes the root'
141
- if (scope !== 'full' && !hasNodeModulesSegment(file)) {
142
- return `is outside node_modules, which a '${scope}'-scope ${what} does not record`
143
- }
241
+ if (scope !== 'full' && !hasNodeModulesSegment(file)) return `is outside node_modules, which a '${scope}'-scope ${what} does not record`
144
242
  if (!files.has(file)) return `names no file the ${what} records`
145
243
  const format = formats.get(file)
146
244
  if (format === 'directory') return 'is a directory capture, not a file'
@@ -148,15 +246,10 @@ function executableEntryProblem(file, { what, files, formats, scope }) {
148
246
  return null
149
247
  }
150
248
 
151
- // Applied at every write site, so the in-memory artifact is honest before it is ever serialized.
152
249
  export function narrowExecutable(executable, { modules, formats, scope }) {
153
- const out = new Set()
154
- if (executable.size === 0) return out
250
+ if (executable.size === 0) return new Set()
155
251
  const files = moduleFileKeys(modules, { scope })
156
- for (const file of executable) {
157
- if (executableEntryProblem(file, { what: 'artifact', files, formats, scope }) === null) out.add(file)
158
- }
159
- return out
252
+ return new Set(executable.values().filter((file) => executableEntryProblem(file, { what: 'artifact', files, formats, scope }) === null))
160
253
  }
161
254
 
162
255
  function assertExecutable(executable, { what, files, formats, scope }) {
@@ -166,23 +259,28 @@ function assertExecutable(executable, { what, files, formats, scope }) {
166
259
  }
167
260
  }
168
261
 
169
- export function parseExecutable(list, { what, files, formats, scope = 'full' }) {
170
- if (list === undefined) return new Set()
171
- const at = `${what}: executable`
262
+ function parsePathList(list, at, entry, check) {
172
263
  assert(Array.isArray(list), `${at} must be an array of file paths`)
173
264
  const out = new Set()
174
- for (const file of list) {
175
- assert(typeof file === 'string' && file !== '', `${at} entry must be a non-empty string`)
176
- // A dupe would silently collapse in out.add() and round-trip to different bytes.
177
- assert(!out.has(file), `${at} entry '${file}' is listed twice`)
178
- 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)
179
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`))
180
279
  assertExecutable(out, { what, files, formats, scope })
181
280
  return out
182
281
  }
183
282
 
184
- // The one choke point every producer goes through, so a write site that forgot to narrow fails HERE, not
185
- // 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.
186
284
  export function serializeExecutable(executable, { what, modules, formats, scope }) {
187
285
  if (executable.size === 0) return undefined
188
286
  assertExecutable(executable, { what, files: moduleFileKeys(modules, { scope }), formats, scope })
@@ -193,60 +291,80 @@ export function assert(condition, msg) {
193
291
  if (!condition) throw new Error(msg)
194
292
  }
195
293
 
196
- export function sortPaths(a, b) {
197
- const [al, bl] = [a.split('/'), b.split('/')]
198
- while (al.length > 0 && al[0] === bl[0]) {
199
- al.shift()
200
- bl.shift()
201
- }
202
- if (al.length === 0 && bl.length === 0) return 0
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
303
+
304
+ function compareSegments(al, bl) {
305
+ let i = 0
306
+ while (i < al.length && al[i] === bl[i]) i++
307
+ const ar = al.length - i
308
+ const br = bl.length - i
309
+ if (ar === 0 && br === 0) return 0
203
310
 
204
- // First process each file in dir, then subdirs
205
- if (al.length < 2) return bl.length < 2 && al > bl ? 1 : -1
206
- if (bl.length < 2) return 1
311
+ if (ar < 2) return br < 2 && (al[i] ?? '') > (bl[i] ?? '') ? 1 : -1
312
+ if (br < 2) return 1
207
313
 
208
- if (al[0] === '*') return -1
209
- if (bl[0] === '*') return 1
314
+ if (al[i] === '*') return -1
315
+ if (bl[i] === '*') return 1
210
316
 
211
- if (al[0] === 'node_modules') return 1
212
- if (bl[0] === 'node_modules') return -1
317
+ if (al[i] === 'node_modules') return 1
318
+ if (bl[i] === 'node_modules') return -1
213
319
 
214
320
  // Prefer example/ over example-something/
215
- const [an, bn] = [al, bl].map((list) => list.join(String.fromCodePoint(0)))
321
+ const an = al.slice(i).join('\0')
322
+ const bn = bl.slice(i).join('\0')
216
323
  if (an < bn) return -1
217
324
  if (an > bn) return 1
218
325
  throw new Error('Unreachable')
219
326
  }
220
327
 
221
- const byPath = (a, b) => sortPaths(a[0], b[0])
328
+ export const sortPaths = (a, b) => compareSegments(a.split('/'), b.split('/'))
222
329
 
223
- export const isPlainObject = (x) => x && [null, Object.prototype].includes(Object.getPrototypeOf(x))
330
+ // Each key split once, not at each of the sort's comparisons.
331
+ export const sortedByPath = (items, keyOf = (item) => item) => Array.from(items, (item) => ({ item, segments: keyOf(item).split('/') }))
332
+ .toSorted((a, b) => compareSegments(a.segments, b.segments))
333
+ .map(({ item }) => item)
334
+
335
+ const entryKey = ([key]) => key
336
+
337
+ export function isPlainObject(x) {
338
+ if (!x) return false
339
+ const proto = Object.getPrototypeOf(x)
340
+ return proto === null || proto === Object.prototype
341
+ }
224
342
 
225
343
  export const fromEntries = (entries) => Object.setPrototypeOf(Object.fromEntries(entries), null)
226
344
 
227
- export const fileSetToObject = (set) => [...set].toSorted(sortPaths)
345
+ export const fileSetToObject = (set) => sortedByPath(set)
228
346
 
229
347
  // `sorted: false` only for a machine-only payload whose reader does not care about ordering (shard.js).
230
348
  export const fileMapToObject = (map, { sorted = true } = {}) => {
231
- const entries = sorted ? [...map].toSorted(byPath) : [...map]
349
+ const entries = sorted ? sortedByPath(map, entryKey) : [...map]
232
350
  return fromEntries(entries.map(([k, v]) => [k, v instanceof Map ? fileMapToObject(v, { sorted }) : v]))
233
351
  }
234
352
 
235
- // The Map at `map.get(key)`, created on first use.
236
- 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())
237
360
 
238
361
  export const objectToMaps = (obj) => new Map(
239
362
  Object.entries(obj).map(([k, v]) => [k, isPlainObject(v) ? objectToMaps(v) : v])
240
363
  )
241
364
 
242
- // True for an absolute path or any `..` hop that pops above the root, INCLUDING a mid-path one
243
- // (`a/../../x`) that plain `startsWith('..')` would miss. A segment walk (not posix.normalize, whose
244
- // verdict it matches -- see posix-path-escapes.test.js) so this module stays free of node:path:
245
- // `.` and empty segments are skipped exactly as normalize collapses them, a real segment pushes, and
246
- // 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.
247
366
  export function posixPathEscapes(path) {
248
367
  if (path.startsWith('/')) return true
249
- // The walk can only return true via a literal '..' segment, so the ordinary keys skip the split.
250
368
  if (!path.includes('..')) return false
251
369
  let depth = 0
252
370
  for (const segment of path.split('/')) {
@@ -257,49 +375,28 @@ export function posixPathEscapes(path) {
257
375
  return false
258
376
  }
259
377
 
260
- // THE rule every path an artifact records must satisfy (a file key's too, see canonicalFileKey): in
261
- // the root, and free of `\` -- part of a name off Windows, which stasis refuses everywhere rather than
262
- // 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.
263
379
  export const isRefusedPath = (path) => path.includes('\\') || posixPathEscapes(path)
264
380
 
265
- // Throws unless `path`, one of `what`'s, is a path an artifact may record (see isRefusedPath).
266
381
  export const assertArtifactPath = (path, what) =>
267
382
  assert(!isRefusedPath(path), `${what}: path '${path}' escapes the root or holds a '\\'`)
268
383
 
269
- // THE rule an artifact's entry must satisfy, on read (parseEntries) and on write (serializeEntries).
270
- const assertEntry = (entry, what) =>
271
- 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)}`)
272
386
 
273
- // An artifact's `entries` list as a Set: each a non-empty in-root path, listed once (a dupe would
274
- // collapse in the Set and round-trip to different bytes).
275
- export function parseEntries(list, what) {
276
- assert(Array.isArray(list), `${what}: entries must be an array of file paths`)
277
- const out = new Set()
278
- for (const entry of list) {
279
- assertEntry(entry, what)
280
- assert(!out.has(entry), `${what}: entry '${entry}' is listed twice`)
281
- out.add(entry)
282
- }
283
- return out
284
- }
387
+ export const parseEntries = (list, what) => parsePathList(list, `${what}: entries`, `${what}: entry`, (entry) => assertEntry(entry, what))
285
388
 
286
- // The `entries` list an artifact writes: the same rule as on read, so an in-memory construct can't
287
- // serialize what parse would reject; path-sorted.
288
389
  export function serializeEntries(entries, what) {
289
390
  for (const entry of entries) assertEntry(entry, what)
290
391
  return fileSetToObject(entries)
291
392
  }
292
393
 
293
- // THE rule an artifact's `formats` entry must satisfy, on read (parseFormats) and on write
294
- // (serializeFormats): a path an artifact may record, and a known format.
295
394
  function assertFormat(file, format) {
296
395
  assertArtifactPath(file, 'formats')
297
396
  assert(KNOWN_FORMATS.has(format), `unknown format '${format}' for ${file}`)
298
397
  }
299
398
 
300
- // An artifact's `formats` object as a validated Map. '' and '.' alias to the same key (older
301
- // artifacts keyed the root listing ''); normalized, failing closed on dupes, and an unknown format
302
- // is rejected at the schema boundary so a tampered artifact fails closed.
399
+ // '' and '.' alias to one key: older artifacts keyed the root listing ''.
303
400
  export function parseFormats(json) {
304
401
  assert(isPlainObject(json))
305
402
  const formats = new Map()
@@ -312,17 +409,19 @@ export function parseFormats(json) {
312
409
  return formats
313
410
  }
314
411
 
315
- // The `formats` an artifact writes: the same rule as on read, so an in-memory construct can't serialize
316
- // what parse would reject; path-sorted.
412
+ // A `stat:*` record attests a path's kind alone: one reader would skip content carried for its path, another serve it.
413
+ export function assertPayloadFree(formats, files, what) {
414
+ for (const [file, format] of formats) {
415
+ if (isStatFormat(format) && files.has(file)) assert(false, `${what}: '${file}' is a payload-free '${format}' record, yet carries content`)
416
+ }
417
+ }
418
+
317
419
  export function serializeFormats(formats) {
318
420
  for (const [file, format] of formats) assertFormat(file, format)
319
421
  return fileMapToObject(formats)
320
422
  }
321
423
 
322
- // THE rules an artifact's `imports` (conditions -> parent -> specifier -> target, as nested Maps)
323
- // must satisfy, on read (parseImports) and on write (serializeImports). Paths escaping the root (incl.
324
- // mid-path `a/../../x`) or holding a `\` are rejected: getImport resolves against the root at load. A
325
- // 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.
326
425
  function assertImports(imports) {
327
426
  for (const [, byParent] of imports) {
328
427
  assert(byParent instanceof Map)
@@ -345,7 +444,6 @@ function assertImports(imports) {
345
444
  }
346
445
  }
347
446
 
348
- // An artifact's `imports` object as nested Maps (see assertImports).
349
447
  export function parseImports(json) {
350
448
  assert(isPlainObject(json))
351
449
  const imports = objectToMaps(json)
@@ -353,43 +451,68 @@ export function parseImports(json) {
353
451
  return imports
354
452
  }
355
453
 
356
- // The `imports` an artifact writes: the same rules as on read (see assertImports); path-sorted.
357
454
  export function serializeImports(imports) {
358
455
  assertImports(imports)
359
456
  return fileMapToObject(imports)
360
457
  }
361
458
 
362
- // A module map as the serialized `modules` (node_modules buckets) and `sources` (the rest) objects
363
- // of a `what` artifact, buckets and files path-sorted so the bytes are canonical. `repo: false`
364
- // leaves each record's `repo` out: metadata, which a lockfile never carries.
365
- export function groupModules(modules, { skipEmpty = false, repo = true, what } = {}) {
459
+ export function groupModules(modules, { skipEmpty = false, metadata: written = true, what } = {}) {
460
+ const metadata = written === true ? MODULE_METADATA : written === false ? [] : written
366
461
  const grouped = { modules: [], sources: [] }
367
462
  for (const [dir, info] of modules) {
368
463
  if (skipEmpty && Object.keys(info.files).length === 0) continue
369
464
  const inNodeModules = hasNodeModulesSegment(dir)
370
465
  if (inNodeModules) assert(info.name && info.version && info.files)
371
- const files = fromEntries(Object.entries(info.files).toSorted(byPath))
372
- grouped[inNodeModules ? 'modules' : 'sources'].push([dir, moduleInfo({ ...info, repo: repo ? normalizeModuleRepo(dir, info, what) : undefined, files })])
466
+ const files = fromEntries(sortedByPath(Object.entries(info.files), entryKey))
467
+ grouped[inNodeModules ? 'modules' : 'sources'].push([dir, moduleInfo({
468
+ ...info,
469
+ ...metadataOf(dir, { ...info, files }, what, metadata, (field) => carriedEntries(info[field], files)),
470
+ files,
471
+ })])
472
+ }
473
+ return { modules: fromEntries(sortedByPath(grouped.modules, entryKey)), sources: fromEntries(sortedByPath(grouped.sources, entryKey)) }
474
+ }
475
+
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 }),
373
503
  }
374
- return { modules: fromEntries(grouped.modules.toSorted(byPath)), sources: fromEntries(grouped.sources.toSorted(byPath)) }
375
504
  }
376
505
 
377
- // A target is a resolved-file string, or a { platform: file } Map under --metro.
378
- function importTargetsEqual(a, b) {
379
- const aMap = a instanceof Map
380
- if (aMap !== (b instanceof Map)) return false
381
- if (!aMap) return a === b
382
- if (a.size !== b.size) return false
383
- for (const [platform, file] of a) if (b.get(platform) !== file) return false
384
- return true
506
+ export function importTargetsEqual(a, b) {
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)
385
509
  }
386
510
 
387
511
  const fmtTarget = (t) => (t instanceof Map ? `{${[...t].map(([p, f]) => `${p}: ${f}`).join(', ')}}` : t)
388
512
 
389
- // Merge two import maps (conditions -> parent -> specifier -> target); a redirect conflict throws.
390
513
  export function mergeImportMaps(a, b, label) {
391
514
  const out = new Map()
392
- const absorb = (imports) => {
515
+ for (const imports of [a, b]) {
393
516
  for (const [conditions, byParent] of imports) {
394
517
  const outByParent = nestedMap(out, conditions)
395
518
  for (const [parent, specs] of byParent) {
@@ -405,57 +528,38 @@ export function mergeImportMaps(a, b, label) {
405
528
  }
406
529
  }
407
530
  }
408
- absorb(a)
409
- absorb(b)
410
531
  return out
411
532
  }
412
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
+
413
539
  export function mergeFormatMaps(a, b, label) {
414
540
  const out = new Map()
415
- const absorb = (formats) => {
416
- for (const [file, format] of formats) {
417
- const currentFormat = out.get(file)
418
- if (currentFormat === undefined) {
419
- out.set(file, format)
420
- continue
421
- }
422
- try {
423
- out.set(file, reconcileFormat(format, currentFormat, file))
424
- } catch (cause) {
425
- throw new Error(`${label}: ${cause.message}`, { cause })
426
- }
427
- }
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 })
428
545
  }
429
- absorb(a)
430
- absorb(b)
431
546
  return out
432
547
  }
433
548
 
434
- // A union, EXCEPT that `b` (the INCOMING, newer artifact -- every call site passes it on the right) is
435
- // authoritative for its own files: a since-lost execute bit is cleared, not resurrected by the union.
436
- export function mergeExecutableSets(a, b, bModules, scope) {
437
- if (a.size === 0) return new Set(b)
438
- const bFiles = moduleFileKeys(bModules, { scope })
439
- const out = new Set(b)
440
- for (const file of a) if (!bFiles.has(file)) out.add(file)
441
- return out
442
- }
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 }))))
443
552
 
444
- // An empty, '.' or '..' path segment.
445
553
  const NON_CANONICAL_SEGMENT = /(?:^|\/)\.{0,2}(?:\/|$)/u
446
554
 
447
- // The flat key of `rel` in bucket `dir`; throws unless canonical ('.' only as the root listing, rel '')
448
- // 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.
449
556
  export function canonicalFileKey(dir, rel, what) {
450
557
  const key = moduleFileKey(dir, rel)
451
- // Message built only on failure: this runs for every file.
452
558
  if ((key !== '.' || rel !== '') && NON_CANONICAL_SEGMENT.test(key)) assert(false, `${what}: non-canonical file key ${JSON.stringify(key)}`)
453
559
  if (key.includes('\\')) assert(false, `${what}: file key '${key}' holds a '\\', which no path may`)
454
560
  return key
455
561
  }
456
562
 
457
- // Maps each file's flat key to its bucket; rejects non-canonical keys and refused bucket dirs (an empty
458
- // bucket's too, which no file key would reach), reports duplicates to onDuplicate.
459
563
  export function flatFileKeys(modules, what, onDuplicate) {
460
564
  const owners = new Map()
461
565
  for (const [dir, { files }] of modules) {
@@ -471,16 +575,15 @@ export function flatFileKeys(modules, what, onDuplicate) {
471
575
  return owners
472
576
  }
473
577
 
474
- // The flatFileKeys duplicate handler for an artifact's parse/serialize: two bucket splits flattening
475
- // to one path means module bucketing changed between writes.
476
578
  export const duplicateKeyError = (what, regenerate) => (key) => assert(false,
477
579
  `duplicate file key '${key}' across ${what} buckets -- module bucketing changed between writes ` +
478
580
  `(a workspace package without a version now owns its own bucket); regenerate the ${regenerate}`)
479
581
 
480
- // Result `files` objects are null-prototype, so a `__proto__` file name is a plain own key.
481
- 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 } = {}) {
482
585
  const out = new Map()
483
- const absorb = (modules) => {
586
+ for (const modules of [a, b]) {
484
587
  for (const [dir, info] of modules) {
485
588
  const existing = out.get(dir)
486
589
  if (existing === undefined) {
@@ -489,8 +592,6 @@ export function mergeModuleMaps(a, b, label) {
489
592
  }
490
593
  assert(existing.name === info.name,
491
594
  `${label}: module '${dir}' name mismatch ('${existing.name}' vs '${info.name}')`)
492
- // A one-sided absent version is usually a migration skew: older stasis fabricated '0.0.0'
493
- // for a workspace package whose package.json has no version, newer stasis records none.
494
595
  assert(existing.version === info.version,
495
596
  `${label}: module '${dir}' version mismatch ('${existing.version}' vs '${info.version}')` +
496
597
  ((existing.version == null) === (info.version == null) ? '' :
@@ -498,23 +599,21 @@ export function mergeModuleMaps(a, b, label) {
498
599
  `package without one; regenerate it (bundle=replace / lock=replace)`))
499
600
  assert(existing.ecosystem === info.ecosystem,
500
601
  `${label}: module '${dir}' ecosystem mismatch ('${existing.ecosystem ?? '(none)'}' vs '${info.ecosystem ?? '(none)'}')`)
501
- // `repo` is metadata, held to nothing: either side's, where only one records it, else `a`'s.
502
- if (existing.repo === undefined && info.repo !== undefined) out.set(dir, moduleInfo({ ...existing, repo: info.repo }))
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
+ }
503
610
  for (const [rel, value] of Object.entries(info.files)) {
504
- if (Object.hasOwn(existing.files, rel)) {
505
- assert(existing.files[rel] === value, `${label}: content mismatch for '${moduleFileKey(dir, rel)}'`)
506
- } else {
507
- existing.files[rel] = value
508
- }
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)}'`)
509
613
  }
510
614
  }
511
615
  }
512
- absorb(a)
513
- absorb(b)
514
- // One project-relative path must live in exactly one bucket. Bucketing can change between
515
- // releases (a versionless workspace package used to fall through to a parent bucket and now owns
516
- // its own), and per-dir absorption cannot see that: without this check the merge would WRITE an
517
- // 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.
518
617
  flatFileKeys(out, label, (key, owner, dir) => assert(false,
519
618
  `${label}: file '${key}' is bucketed under both '${owner}' and '${dir}' -- module bucketing ` +
520
619
  `changed between the artifacts (a workspace package without a version now owns its own ` +
@@ -523,29 +622,28 @@ export function mergeModuleMaps(a, b, label) {
523
622
  }
524
623
 
525
624
  // A full path SEGMENT, NOT a bare substring (`foo_node_modules/dep` is a source dir, not a bucket).
526
- export function hasNodeModulesSegment(path) {
527
- return path.split('/').includes('node_modules')
528
- }
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/'
529
629
 
530
- // Deepest segment-aligned `node_modules/` marker, or -1: `lastIndexOf` alone would also match a bare
531
- // 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/`).
532
631
  function lastNodeModulesMarker(path) {
533
- const marker = 'node_modules/'
534
632
  let idx = path.length
535
- while ((idx = path.lastIndexOf(marker, idx - 1)) !== -1) {
633
+ while ((idx = path.lastIndexOf(NODE_MODULES_MARKER, idx - 1)) !== -1) {
536
634
  if (idx === 0 || path[idx - 1] === '/') return idx
537
635
  }
538
636
  return -1
539
637
  }
540
638
 
541
639
  export function splitNodeModulesPath(path) {
542
- const marker = 'node_modules/'
543
640
  const idx = lastNodeModulesMarker(path)
544
641
  if (idx === -1) return null
545
- const after = idx + marker.length
642
+ const after = idx + NODE_MODULES_MARKER.length
546
643
  const parts = path.slice(after).split('/')
547
644
  const pkgLen = parts[0].startsWith('@') ? 2 : 1
548
- if (parts.length <= pkgLen || parts.slice(0, pkgLen).some((p) => !p)) return null
549
- 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('/')
550
648
  return { dir: path.slice(0, after) + name, rel: parts.slice(pkgLen).join('/'), name }
551
649
  }