@exodus/stasis-core 1.0.0-beta.0 → 1.0.0-beta.10
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/README.md +1 -1
- package/bin/stasis-core.js +78 -44
- package/package.json +14 -4
- package/src/add.js +317 -0
- package/src/artifact-util.js +652 -0
- package/src/brotli.js +15 -16
- package/src/bundle-util.js +379 -0
- package/src/bundle.js +217 -161
- package/src/config.js +203 -282
- package/src/extract.js +151 -0
- package/src/fs.js +224 -530
- package/src/hooks.js +327 -527
- package/src/host.js +45 -0
- package/src/loader.js +1 -3
- package/src/lockfile.js +67 -97
- package/src/prune.js +90 -272
- package/src/shard.js +68 -0
- package/src/state-util.js +13 -31
- package/src/state.js +1028 -1561
- package/src/util.js +299 -161
package/src/bundle.js
CHANGED
|
@@ -1,65 +1,138 @@
|
|
|
1
1
|
import {
|
|
2
|
-
KNOWN_FORMATS,
|
|
3
2
|
assert,
|
|
4
|
-
|
|
3
|
+
assertArtifactPath,
|
|
4
|
+
canonicalFileKey,
|
|
5
|
+
duplicateKeyError,
|
|
5
6
|
fileSetToObject,
|
|
6
7
|
fromEntries,
|
|
8
|
+
flatFileKeys,
|
|
9
|
+
groupModules,
|
|
10
|
+
hasNodeModulesSegment,
|
|
7
11
|
isPlainObject,
|
|
12
|
+
mergeFormatMaps,
|
|
13
|
+
mergeImportMaps,
|
|
14
|
+
mergeExecutableSets,
|
|
15
|
+
mergeModuleMaps,
|
|
8
16
|
moduleFileKey,
|
|
9
|
-
|
|
17
|
+
normalizeBlock,
|
|
18
|
+
normalizeModule,
|
|
19
|
+
normalizeRepo,
|
|
20
|
+
PACKAGE_BLOCK,
|
|
21
|
+
parseEntries,
|
|
22
|
+
parseExecutable,
|
|
23
|
+
parseFormats,
|
|
24
|
+
parseImports,
|
|
25
|
+
serializeEntries,
|
|
26
|
+
serializeExecutable,
|
|
27
|
+
serializeFormats,
|
|
28
|
+
serializeImports,
|
|
10
29
|
posixPathEscapes,
|
|
11
|
-
|
|
30
|
+
REPO_FIELDS,
|
|
31
|
+
sameGithub,
|
|
12
32
|
splitNodeModulesPath,
|
|
13
|
-
} from './util.js'
|
|
33
|
+
} from './artifact-util.js'
|
|
14
34
|
|
|
15
35
|
const VERSION = 1
|
|
16
36
|
const LEGACY_VERSION = 0
|
|
17
37
|
|
|
18
|
-
const
|
|
19
|
-
// `ecosystem` (when present) names the package ecosystem a dependency came from
|
|
20
|
-
// (`npm`, `composer`, `soldeer`); the workspace/top-level buckets omit it.
|
|
21
|
-
assert(ecosystem === undefined || typeof ecosystem === 'string')
|
|
22
|
-
return { name, version, ...(ecosystem === undefined ? {} : { ecosystem }), files: fromEntries(Object.entries(files)) }
|
|
23
|
-
}
|
|
38
|
+
const duplicateKey = duplicateKeyError('bundle', 'artifact (bundle=replace)')
|
|
24
39
|
|
|
40
|
+
// A v0 path's bucket split; '' and '.' both spell the root listing (rel '').
|
|
25
41
|
const inferModuleDir = (path) =>
|
|
26
|
-
splitNodeModulesPath(path) ?? { dir: '.', rel: path, name: null }
|
|
42
|
+
splitNodeModulesPath(path) ?? { dir: '.', rel: path === '.' ? '' : path, name: null }
|
|
43
|
+
|
|
44
|
+
function contentsLocked() {
|
|
45
|
+
throw new Error('bundle: file contents are not retained by this contents-free Bundle')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Each file stays an own enumerable key, but reading its contents throws; each value must be a placeholder.
|
|
49
|
+
const lockModule = (dir, { files, ...info }) => {
|
|
50
|
+
const locked = Object.create(null)
|
|
51
|
+
for (const [rel, value] of Object.entries(files)) {
|
|
52
|
+
if (typeof value !== 'symbol') assert(false, `bundle: file '${moduleFileKey(dir, rel)}' is not a placeholder`)
|
|
53
|
+
Object.defineProperty(locked, rel, { get: contentsLocked, enumerable: true })
|
|
54
|
+
}
|
|
55
|
+
return { ...info, files: Object.freeze(locked) }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Union of the informational `reason` maps in canonical form: consumers sorted, each file list
|
|
59
|
+
// deduped and path-sorted. Canonical even when only one side is given, so a fresh withReason()
|
|
60
|
+
// stamp or a parsed artifact can't leak discovery/record order into the map (state's
|
|
61
|
+
// #bundleReason sorts the same way). A non-array list (unvalidated -- informational) is dropped.
|
|
62
|
+
const mergeReason = (a, b) => {
|
|
63
|
+
if (a === undefined && b === undefined) return undefined
|
|
64
|
+
// Accumulated in a Map: on a plain object a '__proto__' consumer key would hit the prototype.
|
|
65
|
+
const merged = new Map()
|
|
66
|
+
for (const src of [a, b]) {
|
|
67
|
+
for (const [consumer, files] of Object.entries(src ?? {})) {
|
|
68
|
+
if (!Array.isArray(files)) continue
|
|
69
|
+
let set = merged.get(consumer)
|
|
70
|
+
if (set === undefined) merged.set(consumer, (set = new Set()))
|
|
71
|
+
for (const file of files) set.add(file)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return fromEntries([...merged.keys()].toSorted().map((c) => [c, fileSetToObject(merged.get(c))]))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Validate one `repo` field without throwing.
|
|
78
|
+
export const isValidRepoField = (key, value) => Object.hasOwn(REPO_FIELDS, key) && REPO_FIELDS[key](value)
|
|
79
|
+
|
|
80
|
+
// `repo` of a bundle plus one added to it: only agreeing fields survive, and none if `github` differs.
|
|
81
|
+
export const mergeRepo = (a, b) => {
|
|
82
|
+
if (a?.github === undefined || !sameGithub(a.github, b?.github)) return undefined
|
|
83
|
+
const kept = Object.keys(REPO_FIELDS).filter((key) => a[key] !== undefined && (key === 'github' || a[key] === b[key]))
|
|
84
|
+
return fromEntries(kept.map((key) => [key, a[key]]))
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// A parsed header `repo`, an older stasis's `root: true` read as unknown: it recorded one from a
|
|
88
|
+
// package.json's claim as well as from a git work tree.
|
|
89
|
+
const parsedRepo = (repo) => {
|
|
90
|
+
if (!isPlainObject(repo) || repo.root !== true || Object.hasOwn(repo, 'directory')) return repo
|
|
91
|
+
const { root: _root, ...rest } = repo
|
|
92
|
+
return rest
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// One block per ecosystem, named as a module's `ecosystem` is.
|
|
96
|
+
const PACKAGE_FIELDS = { npm: PACKAGE_BLOCK, composer: PACKAGE_BLOCK, cargo: PACKAGE_BLOCK }
|
|
27
97
|
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
//
|
|
35
|
-
// v1 — current layout, mirrors stasis.lock.json: per-package buckets under
|
|
36
|
-
// `sources` (workspace) and `modules` (node_modules) with name+version+files,
|
|
37
|
-
// plus `entries`, `formats`, and `imports`.
|
|
38
|
-
// `Bundle.parse` accepts both; offline tooling (stasis extract / diff / audit /
|
|
39
|
-
// sbom) reads v0 -- paths are regrouped by package dir via `inferModuleDir`
|
|
40
|
-
// (best-effort, last-wins); name/version stay null since v0 records neither,
|
|
41
|
-
// which State uses as a marker to skip metadata cross-checks. `stasis run`
|
|
42
|
-
// refuses v0 -- see State's load path. We always serialize v1.
|
|
98
|
+
// `package` of a bundle plus one added to it: per ecosystem, only agreeing fields survive, and none unless `name` does.
|
|
99
|
+
const mergePackage = (a, b) => fromEntries(Object.keys(PACKAGE_FIELDS).map((ecosystem) => {
|
|
100
|
+
const [x, y] = [a?.[ecosystem], b?.[ecosystem]]
|
|
101
|
+
return [ecosystem, x?.name !== undefined && x.name === y?.name ? fromEntries(Object.entries(x).filter(([key, value]) => value === y[key])) : undefined]
|
|
102
|
+
}))
|
|
103
|
+
|
|
104
|
+
// JSON shape of stasis.code.br; callers own the brotli wrap. parse accepts legacy v0 and v1, serialize always writes v1.
|
|
43
105
|
export class Bundle {
|
|
44
106
|
static VERSION = VERSION
|
|
45
107
|
|
|
46
|
-
// The on-disk version this instance represents. Defaults to VERSION; parse*
|
|
47
|
-
// sets it to whatever the file declared (LEGACY_VERSION for v0 input).
|
|
48
|
-
// Serialization always writes the current VERSION regardless — loading v0 and
|
|
49
|
-
// re-saving promotes the bundle to v1.
|
|
50
108
|
version = VERSION
|
|
51
109
|
config
|
|
52
110
|
entries
|
|
53
111
|
modules
|
|
54
112
|
formats
|
|
55
113
|
imports
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
-
//
|
|
59
|
-
// the same bundleFile); a single-consumer bundle omits it. Never consulted for verification.
|
|
114
|
+
// Project-relative paths of bundled files carrying a POSIX execute bit, for `stasis extract`. Files only, never a `directory`.
|
|
115
|
+
executable
|
|
116
|
+
// Informational only, NOT attested -- never consulted for verification.
|
|
60
117
|
reason
|
|
118
|
+
// Informational, not attested, never in a lockfile; validated on every assignment, and frozen.
|
|
119
|
+
#repo
|
|
120
|
+
get repo() {
|
|
121
|
+
return this.#repo
|
|
122
|
+
}
|
|
123
|
+
set repo(repo) {
|
|
124
|
+
this.#repo = normalizeRepo(repo)
|
|
125
|
+
}
|
|
126
|
+
// `{ npm | composer | cargo: { name, version } }`: the package this bundle is; as `repo`, but never set by a build.
|
|
127
|
+
#package
|
|
128
|
+
get package() {
|
|
129
|
+
return this.#package
|
|
130
|
+
}
|
|
131
|
+
set package(pkg) {
|
|
132
|
+
this.#package = normalizeBlock(pkg, PACKAGE_FIELDS, 'bundle package')
|
|
133
|
+
}
|
|
61
134
|
|
|
62
|
-
constructor({ config = { scope: 'full' }, entries, modules, formats, imports, reason, version = VERSION } = {}) {
|
|
135
|
+
constructor({ config = { scope: 'full' }, entries, modules, formats, imports, executable, reason, repo, package: pkg, version = VERSION } = {}) {
|
|
63
136
|
assert([LEGACY_VERSION, VERSION].includes(version))
|
|
64
137
|
assert(['node_modules', 'full'].includes(config.scope))
|
|
65
138
|
this.version = version
|
|
@@ -68,53 +141,61 @@ export class Bundle {
|
|
|
68
141
|
this.modules = modules ?? new Map()
|
|
69
142
|
this.formats = formats ?? new Map()
|
|
70
143
|
this.imports = imports ?? new Map()
|
|
144
|
+
this.executable = executable ?? new Set()
|
|
71
145
|
this.reason = reason
|
|
146
|
+
this.repo = normalizeRepo(repo)
|
|
147
|
+
this.package = pkg
|
|
72
148
|
}
|
|
73
149
|
|
|
74
|
-
// Flat project-relative view of file contents
|
|
75
|
-
// Per-file encoding is signalled by `formats[file]`: 'resource:base64' is a
|
|
76
|
-
// base64 string, everything else (including 'resource' and 'directory') is a
|
|
77
|
-
// raw UTF-8 string.
|
|
150
|
+
// Flat project-relative view of the raw stored file contents (resources stay base64).
|
|
78
151
|
get sources() {
|
|
79
152
|
const m = new Map()
|
|
80
153
|
for (const [dir, { files }] of this.modules) {
|
|
81
|
-
for (const [rel, content] of Object.entries(files))
|
|
82
|
-
m.set(moduleFileKey(dir, rel), content)
|
|
83
|
-
}
|
|
154
|
+
for (const [rel, content] of Object.entries(files)) m.set(moduleFileKey(dir, rel), content)
|
|
84
155
|
}
|
|
85
156
|
return m
|
|
86
157
|
}
|
|
87
158
|
|
|
88
|
-
// True for the resource-like, non-executable payload formats. Their bundle
|
|
89
|
-
// content is a raw UTF-8 string ('resource', and 'directory' — a JSON-serialized
|
|
90
|
-
// `fs.readdirSync` listing) or a base64 blob ('resource:base64'). State stores
|
|
91
|
-
// all three in `this.resources` (vs `this.sources` for code) and the loader's
|
|
92
|
-
// executable allowlist rejects them, so this is the single "is this a code file?"
|
|
93
|
-
// discriminator. Only 'resource:base64' is base64-decoded; the others are raw.
|
|
94
159
|
static isResourceFormat(format) {
|
|
95
160
|
return format === 'resource' || format === 'resource:base64' || format === 'directory'
|
|
96
161
|
}
|
|
97
162
|
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
|
|
163
|
+
// True if any bundled file is non-resource; a full-scope code bundle must declare an entry to be runnable (State#absorbCodeBundle).
|
|
164
|
+
get hasCode() {
|
|
165
|
+
for (const [dir, { files }] of this.modules) {
|
|
166
|
+
for (const rel of Object.keys(files)) {
|
|
167
|
+
if (!Bundle.isResourceFormat(this.formats.get(moduleFileKey(dir, rel)))) return true
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return false
|
|
171
|
+
}
|
|
172
|
+
|
|
101
173
|
static parse(text) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
assert(['node_modules', 'full'].includes(json.config?.scope))
|
|
105
|
-
assert(isPlainObject(json.formats))
|
|
106
|
-
assert(isPlainObject(json.imports))
|
|
174
|
+
return Bundle.fromJSON(JSON.parse(text))
|
|
175
|
+
}
|
|
107
176
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
formats.set(file, format)
|
|
177
|
+
// The `sources` key of the file whose contents sit at JSON key path `path`, else undefined; throws if non-canonical.
|
|
178
|
+
static fileKeyAt(path) {
|
|
179
|
+
const [top, dir, files, rel] = path
|
|
180
|
+
if (typeof dir !== 'string') return undefined
|
|
181
|
+
// v0 `sources.<path>`
|
|
182
|
+
if (path.length === 2 && top === 'sources') {
|
|
183
|
+
const split = inferModuleDir(dir)
|
|
184
|
+
return canonicalFileKey(split.dir, split.rel, 'bundle')
|
|
117
185
|
}
|
|
186
|
+
// v1 `sources|modules.<dir>.files.<rel>`
|
|
187
|
+
if (path.length === 4 && (top === 'sources' || top === 'modules') && files === 'files' && typeof rel === 'string') {
|
|
188
|
+
return canonicalFileKey(dir, rel, 'bundle')
|
|
189
|
+
}
|
|
190
|
+
return undefined
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// parse() on an already-parsed value; `contents: false` takes a symbol placeholder per file and locks contents out.
|
|
194
|
+
static fromJSON(json, { contents = true } = {}) {
|
|
195
|
+
assert(json.version === VERSION || json.version === LEGACY_VERSION)
|
|
196
|
+
assert(['node_modules', 'full'].includes(json.config?.scope))
|
|
197
|
+
// Validated into a typed Map early: a raw-object lookup like `__proto__` would hit the prototype.
|
|
198
|
+
const formats = parseFormats(json.formats)
|
|
118
199
|
|
|
119
200
|
const modules = new Map()
|
|
120
201
|
let entries = new Set()
|
|
@@ -124,91 +205,45 @@ export class Bundle {
|
|
|
124
205
|
if (json.modules !== undefined) {
|
|
125
206
|
assert(typeof json.modules === 'object' && json.modules !== null)
|
|
126
207
|
for (const [dir, info] of Object.entries(json.modules)) {
|
|
127
|
-
assert(dir
|
|
128
|
-
|
|
208
|
+
assert(hasNodeModulesSegment(dir))
|
|
209
|
+
assertArtifactPath(dir, 'bundle')
|
|
129
210
|
assert(info?.name && info.version && info.files)
|
|
130
|
-
modules.set(dir,
|
|
211
|
+
modules.set(dir, normalizeModule(info, dir, 'bundle'))
|
|
131
212
|
}
|
|
132
213
|
}
|
|
133
214
|
if (full) {
|
|
134
215
|
assert(json.sources && typeof json.sources === 'object')
|
|
135
216
|
for (const [dir, info] of Object.entries(json.sources)) {
|
|
136
|
-
assert(!dir
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
// A bundle that carries code in full scope must declare at least one entry:
|
|
142
|
-
// empty entries would let `state.assertEntry` skip its check (it short-circuits
|
|
143
|
-
// on entries.size === 0 for v0+no-lockfile compatibility), which a tampered
|
|
144
|
-
// bundle could exploit. A resources-only bundle has no code files and thus no
|
|
145
|
-
// entries -- allowed, since there is nothing for assertEntry to guard.
|
|
146
|
-
let hasCode = false
|
|
147
|
-
for (const [dir, { files }] of modules) {
|
|
148
|
-
for (const rel of Object.keys(files)) {
|
|
149
|
-
// moduleFileKey (not `${dir}/${rel}`): a `directory` capture at a module
|
|
150
|
-
// root keys at rel === '', whose trailing-slash join would miss its
|
|
151
|
-
// resource-format lookup and miscount the listing as code here.
|
|
152
|
-
const fp = moduleFileKey(dir, rel)
|
|
153
|
-
if (!Bundle.isResourceFormat(formats.get(fp))) { hasCode = true; break }
|
|
154
|
-
}
|
|
155
|
-
if (hasCode) break
|
|
156
|
-
}
|
|
157
|
-
if (hasCode) {
|
|
158
|
-
assert(Array.isArray(json.entries) && json.entries.length > 0,
|
|
159
|
-
'bundle carrying code in scope=full must have at least one entry')
|
|
160
|
-
entries = new Set(json.entries)
|
|
161
|
-
} else {
|
|
162
|
-
assert(json.entries === undefined || (Array.isArray(json.entries) && json.entries.length === 0))
|
|
217
|
+
assert(!hasNodeModulesSegment(dir))
|
|
218
|
+
assertArtifactPath(dir, 'bundle')
|
|
219
|
+
// A workspace bucket may omit version (a private/unpublished package.json can lack one).
|
|
220
|
+
assert(info?.name && info.files)
|
|
221
|
+
modules.set(dir, normalizeModule(info, dir, 'bundle'))
|
|
163
222
|
}
|
|
223
|
+
// Empty entries are valid (`stasis add` attests files without making them entry points); state.assertEntry fails closed on an empty set.
|
|
224
|
+
if (json.entries !== undefined) entries = parseEntries(json.entries, 'bundle')
|
|
164
225
|
} else {
|
|
165
226
|
assert(json.entries === undefined)
|
|
166
227
|
assert(json.sources === undefined)
|
|
167
228
|
}
|
|
168
|
-
for (const [, { files }] of modules) {
|
|
169
|
-
for (const rel of Object.keys(files)) assert(!rel.startsWith('..'))
|
|
170
|
-
}
|
|
171
229
|
} else {
|
|
172
|
-
// v0 legacy: flat sources at top level, regrouped by inferred module dir.
|
|
173
230
|
assert(json.sources)
|
|
174
231
|
for (const [path, content] of Object.entries(json.sources)) {
|
|
175
|
-
|
|
232
|
+
assertArtifactPath(path, 'bundle')
|
|
176
233
|
const { dir, rel, name } = inferModuleDir(path)
|
|
177
|
-
assert(!dir
|
|
234
|
+
assert(!posixPathEscapes(dir) && !posixPathEscapes(rel))
|
|
178
235
|
if (!modules.has(dir)) modules.set(dir, { name, version: null, files: Object.create(null) })
|
|
179
|
-
modules.get(dir)
|
|
236
|
+
const { files } = modules.get(dir)
|
|
237
|
+
assert(!Object.hasOwn(files, rel), `bundle: duplicate file key '.' (v0 '' and '.')`)
|
|
238
|
+
files[rel] = content
|
|
180
239
|
}
|
|
181
240
|
}
|
|
182
241
|
|
|
183
|
-
//
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
const
|
|
188
|
-
// An edge target is normally a file string. A `stasis bundle --metro` edge that
|
|
189
|
-
// resolves differently per platform is instead a { platform: file } map (which
|
|
190
|
-
// objectToMaps has turned into a Map). Validate both shapes; reject anything else
|
|
191
|
-
// (e.g. a deeper nesting) so a tampered artifact fails closed at the schema edge.
|
|
192
|
-
const assertTarget = (target) => {
|
|
193
|
-
if (typeof target === 'string') {
|
|
194
|
-
assert(!posixPathEscapes(target))
|
|
195
|
-
return
|
|
196
|
-
}
|
|
197
|
-
assert(target instanceof Map && target.size > 0, 'import target must be a file or a non-empty {platform: file} map')
|
|
198
|
-
for (const [platform, file] of target) {
|
|
199
|
-
assert(typeof platform === 'string' && platform.length > 0 && !platform.includes('/'), `invalid platform key '${platform}'`)
|
|
200
|
-
assert(typeof file === 'string')
|
|
201
|
-
assert(!posixPathEscapes(file))
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
for (const [, byParent] of imports) {
|
|
205
|
-
assert(byParent instanceof Map)
|
|
206
|
-
for (const [parent, specifiers] of byParent) {
|
|
207
|
-
assert(!posixPathEscapes(parent))
|
|
208
|
-
assert(specifiers instanceof Map)
|
|
209
|
-
for (const [, target] of specifiers) assertTarget(target)
|
|
210
|
-
}
|
|
211
|
-
}
|
|
242
|
+
// Flat keys must be unique across buckets: two different bucket splits can flatten to one path, and the `sources` getter would serve either payload.
|
|
243
|
+
const flatKeys = flatFileKeys(modules, 'bundle', duplicateKey)
|
|
244
|
+
const imports = parseImports(json.imports)
|
|
245
|
+
|
|
246
|
+
if (!contents) for (const [dir, info] of modules) modules.set(dir, lockModule(dir, info))
|
|
212
247
|
|
|
213
248
|
return new Bundle({
|
|
214
249
|
version: json.version,
|
|
@@ -217,40 +252,61 @@ export class Bundle {
|
|
|
217
252
|
modules,
|
|
218
253
|
formats,
|
|
219
254
|
imports,
|
|
220
|
-
|
|
255
|
+
// Every executable must be a file this bundle carries. A v0 `executable` is ignored: with no per-file `formats` it could point `extract` at any path to chmod +x.
|
|
256
|
+
executable: json.version === VERSION
|
|
257
|
+
? parseExecutable(json.executable, { what: 'bundle', files: flatKeys, formats, scope: json.config.scope })
|
|
258
|
+
: new Set(),
|
|
259
|
+
reason: isPlainObject(json.reason) ? json.reason : undefined,
|
|
260
|
+
repo: parsedRepo(json.repo),
|
|
261
|
+
package: json.package,
|
|
221
262
|
})
|
|
222
263
|
}
|
|
223
264
|
|
|
224
|
-
#groupedFromModules() {
|
|
225
|
-
const moduleEntries = []
|
|
226
|
-
const sourceEntries = []
|
|
227
|
-
for (const [dir, { name, version, ecosystem, files }] of this.modules) {
|
|
228
|
-
if (Object.keys(files).length === 0) continue
|
|
229
|
-
const inNodeModules = dir.includes('node_modules')
|
|
230
|
-
if (inNodeModules) assert(name && version && files)
|
|
231
|
-
const sorted = fromEntries(Object.entries(files).toSorted((a, b) => sortPaths(a[0], b[0])))
|
|
232
|
-
const target = inNodeModules ? moduleEntries : sourceEntries
|
|
233
|
-
target.push([dir, { name, version, ...(ecosystem === undefined ? {} : { ecosystem }), files: sorted }])
|
|
234
|
-
}
|
|
235
|
-
moduleEntries.sort((a, b) => sortPaths(a[0], b[0]))
|
|
236
|
-
sourceEntries.sort((a, b) => sortPaths(a[0], b[0]))
|
|
237
|
-
return { modules: fromEntries(moduleEntries), sources: fromEntries(sourceEntries) }
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// One unified on-disk shape for code AND resources. A file is a resource when its
|
|
241
|
-
// `formats` entry is 'resource' (raw UTF-8 content) or 'resource:base64' (binary,
|
|
242
|
-
// base64-encoded); every other format is code carrying a raw UTF-8 source string.
|
|
243
|
-
// A single bundle can therefore hold both; readers split by consulting `formats`.
|
|
244
265
|
serialize() {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
const
|
|
248
|
-
const
|
|
266
|
+
// Never write an artifact that parse would reject.
|
|
267
|
+
flatFileKeys(this.modules, 'bundle', duplicateKey)
|
|
268
|
+
const { modules, sources } = groupModules(this.modules, { skipEmpty: true, what: 'bundle' })
|
|
269
|
+
const full = this.config.scope === 'full'
|
|
249
270
|
const data = { version: VERSION, config: this.config }
|
|
250
|
-
if (this.
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
271
|
+
if (this.repo !== undefined) data.repo = this.repo
|
|
272
|
+
if (this.package !== undefined) data.package = this.package
|
|
273
|
+
if (full) data.entries = serializeEntries(this.entries, 'bundle')
|
|
274
|
+
data.formats = serializeFormats(this.formats)
|
|
275
|
+
data.imports = serializeImports(this.imports)
|
|
276
|
+
const executable = serializeExecutable(this.executable, {
|
|
277
|
+
what: 'bundle', modules: this.modules, formats: this.formats, scope: this.config.scope,
|
|
278
|
+
})
|
|
279
|
+
if (executable !== undefined) data.executable = executable
|
|
280
|
+
// Canonicalized like every other sorted field, so a parsed artifact's order can't leak into the bytes.
|
|
281
|
+
if (this.reason !== undefined) data.reason = mergeReason(this.reason, undefined)
|
|
282
|
+
// File contents are written last, after every other key: sources, then modules.
|
|
283
|
+
if (full) data.sources = sources
|
|
284
|
+
data.modules = modules
|
|
254
285
|
return JSON.stringify(data, undefined, 2)
|
|
255
286
|
}
|
|
287
|
+
|
|
288
|
+
// Stamp `consumer` onto every carried file in the informational `reason` map.
|
|
289
|
+
withReason(consumer) {
|
|
290
|
+
const { version, config, entries, modules, formats, imports, executable, repo, package: pkg } = this
|
|
291
|
+
const reason = mergeReason(this.reason, { [consumer]: [...this.sources.keys()] })
|
|
292
|
+
return new Bundle({ version, config, entries, modules, formats, imports, executable, reason, repo, package: pkg })
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Strict union of two Bundles (returns a NEW one): any genuine conflict throws -- a bundle is an attestation.
|
|
296
|
+
merge(other) {
|
|
297
|
+
assert(this.config.scope === other.config.scope,
|
|
298
|
+
`bundle merge: scope mismatch ('${this.config.scope}' vs '${other.config.scope}')`)
|
|
299
|
+
return new Bundle({
|
|
300
|
+
config: { scope: this.config.scope },
|
|
301
|
+
entries: new Set([...this.entries, ...other.entries]),
|
|
302
|
+
modules: mergeModuleMaps(this.modules, other.modules, 'bundle merge'),
|
|
303
|
+
formats: mergeFormatMaps(this.formats, other.formats, 'bundle merge'),
|
|
304
|
+
imports: mergeImportMaps(this.imports, other.imports, 'bundle merge'),
|
|
305
|
+
// `other` (the incoming, newer build) wins for the files it carries -- see mergeExecutableSets.
|
|
306
|
+
executable: mergeExecutableSets(this.executable, other.executable, other.modules, this.config.scope),
|
|
307
|
+
reason: mergeReason(this.reason, other.reason),
|
|
308
|
+
repo: mergeRepo(this.repo, other.repo),
|
|
309
|
+
package: mergePackage(this.package, other.package),
|
|
310
|
+
})
|
|
311
|
+
}
|
|
256
312
|
}
|