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