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

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/src/config.js CHANGED
@@ -1,13 +1,9 @@
1
1
  import assert from 'node:assert/strict'
2
2
 
3
+ import { diskHost } from './host.js'
3
4
  import { canonicalizePath } from './state-util.js'
4
5
  import { extSetsEqual, isBrotliQuality, parseBrotliQuality, parseResourcesOption } from './util.js'
5
6
 
6
- const VALID_SCOPE = new Set(['node_modules', 'full'])
7
- const VALID_LOCK = new Set(['none', 'ignore', 'add', 'replace', 'frozen'])
8
- const VALID_BUNDLE = new Set(['none', 'ignore', 'add', 'replace', 'load', 'frozen'])
9
- const VALID_FS = new Set(['sync', 'async'])
10
-
11
7
  export const DEFAULT_LOCK = 'add'
12
8
  export const DEFAULT_BUNDLE = 'none'
13
9
  export const DEFAULT_SCOPE = 'full'
@@ -22,38 +18,62 @@ const envBool = (name, value) => {
22
18
  // EXODUS_STASIS_RESOURCES is a comma-separated extension list; empty/unset means "no allowlist".
23
19
  const envList = (value) => value.split(',').map((s) => s.trim()).filter(Boolean)
24
20
 
25
- const OPTION_KEYS = ['scope', 'lock', 'bundle', 'bundleFile', 'resourcesBundleFile', 'debug', 'resources', 'childProcess', 'packageJSON']
21
+ // Every option: its env var, how the env string parses (strings pass through) and, for a non-string
22
+ // option, how a `new Config()` option does. `lockFile` is flag/env-only (never from stasis.config.json).
23
+ const OPTIONS = {
24
+ scope: { env: 'EXODUS_STASIS_SCOPE', valid: new Set(['node_modules', 'full']) },
25
+ lock: { env: 'EXODUS_STASIS_LOCK', valid: new Set(['none', 'ignore', 'add', 'replace', 'frozen']) },
26
+ lockFile: { env: 'EXODUS_STASIS_LOCK_FILE', type: 'string' },
27
+ bundle: { env: 'EXODUS_STASIS_BUNDLE', valid: new Set(['none', 'ignore', 'add', 'replace', 'load', 'frozen']) },
28
+ bundleFile: { env: 'EXODUS_STASIS_BUNDLE_FILE', type: 'string' },
29
+ resourcesBundleFile: { env: 'EXODUS_STASIS_RESOURCES_BUNDLE_FILE', type: 'string' },
30
+ debug: { env: 'EXODUS_STASIS_DEBUG', parse: envBool, type: 'boolean' },
31
+ childProcess: { env: 'EXODUS_STASIS_CHILD_PROCESS', parse: envBool, type: 'boolean' },
32
+ packageJSON: { env: 'EXODUS_STASIS_PACKAGE_JSON', parse: envBool, type: 'boolean' },
33
+ fs: { env: 'EXODUS_STASIS_FS', valid: new Set(['sync', 'async']) },
34
+ brotliQuality: { env: 'EXODUS_STASIS_BROTLI_QUALITY', parse: parseBrotliQuality },
35
+ // Compared as parsed sets, not raw strings: ['png','svg'] and 'svg,png' are the same allowlist.
36
+ resources: { env: 'EXODUS_STASIS_RESOURCES', parse: (name, value) => parseResourcesOption('env', envList(value)), option: (value) => parseResourcesOption('options', value) },
37
+ }
38
+ const OPTION_KEYS = Object.keys(OPTIONS)
39
+ const CONFIG_FILE_KEYS = OPTION_KEYS.filter((key) => key !== 'lockFile')
40
+ const PLUGIN_OPTION_KEYS = ['scope', 'lock', 'bundle', 'bundleFile', 'resourcesBundleFile', 'debug', 'childProcess', 'packageJSON', 'resources']
41
+
42
+ const assertKnownKeys = (keys, known, what) => {
43
+ const unknown = keys.filter((key) => !known.includes(key))
44
+ assert.equal(unknown.length, 0, `Unknown ${what} options: ${unknown.join(', ')}`)
45
+ }
46
+
47
+ // One option's membership/type rule; the cross-option rules live in #checkInvariants.
48
+ function assertOption(key, value) {
49
+ const { valid, type } = OPTIONS[key]
50
+ if (valid) assert.ok(valid.has(value), `Invalid ${key}: ${value}`)
51
+ else if (type) assert.equal(typeof value, type, `${key} must be a ${type}`)
52
+ }
53
+
54
+ // Equal for the option's type: `resources` is a Set.
55
+ const assertSame = (key, actual, expected, message) => {
56
+ if (key === 'resources') assert.ok(extSetsEqual(actual, expected), message)
57
+ else assert.equal(actual, expected)
58
+ }
26
59
 
27
60
  // Plugins validate the same options without constructing a Config (which has side effects).
28
61
  export function validatePluginOptions(label, options) {
29
- const rest = { ...options }
30
- for (const key of OPTION_KEYS) delete rest[key]
31
- assert.equal(Object.keys(rest).length, 0, `Unknown ${label} options: ${Object.keys(rest).join(', ')}`)
32
- const { scope, lock, bundle, bundleFile, resourcesBundleFile, debug, resources, childProcess, packageJSON } = options
33
- if (scope !== undefined) assert.ok(VALID_SCOPE.has(scope), `Invalid scope: ${scope}`)
34
- if (lock !== undefined) assert.ok(VALID_LOCK.has(lock), `Invalid lock: ${lock}`)
35
- if (bundle !== undefined) assert.ok(VALID_BUNDLE.has(bundle), `Invalid bundle: ${bundle}`)
36
- if (bundleFile !== undefined) assert.equal(typeof bundleFile, 'string', 'bundleFile must be a string')
37
- if (resourcesBundleFile !== undefined) assert.equal(typeof resourcesBundleFile, 'string', 'resourcesBundleFile must be a string')
38
- if (debug !== undefined) assert.equal(typeof debug, 'boolean', 'debug must be a boolean')
39
- if (childProcess !== undefined) assert.equal(typeof childProcess, 'boolean', 'childProcess must be a boolean')
40
- if (packageJSON !== undefined) assert.equal(typeof packageJSON, 'boolean', 'packageJSON must be a boolean')
41
- if (resources !== undefined) parseResourcesOption(label, resources)
62
+ assertKnownKeys(Object.keys(options), PLUGIN_OPTION_KEYS, label)
63
+ for (const key of PLUGIN_OPTION_KEYS) {
64
+ if (options[key] === undefined) continue
65
+ if (key === 'resources') parseResourcesOption(label, options.resources)
66
+ else assertOption(key, options[key])
67
+ }
42
68
  }
43
69
 
44
70
  // The active Config is authoritative; options a plugin was given must agree with it.
45
71
  export function assertOptionsMatchConfig(config, options) {
46
- const { scope, lock, bundle, bundleFile, resourcesBundleFile, debug, childProcess, packageJSON } = options
47
- // `resources` is deliberately NOT checked here: it's a Set, coordinated in resolvePluginState.
48
72
  try {
49
- if (scope !== undefined) assert.equal(config.scope, scope)
50
- if (lock !== undefined) assert.equal(config.lock, lock)
51
- if (bundle !== undefined) assert.equal(config.bundleMode, bundle)
52
- if (bundleFile !== undefined) assert.equal(config.bundleFile, bundleFile)
53
- if (resourcesBundleFile !== undefined) assert.equal(config.resourcesBundleFile, resourcesBundleFile)
54
- if (debug !== undefined) assert.equal(config.debug, debug)
55
- if (childProcess !== undefined) assert.equal(config.childProcess, childProcess)
56
- if (packageJSON !== undefined) assert.equal(config.packageJSON, packageJSON)
73
+ // `resources` is deliberately NOT checked here: it's a Set, coordinated in resolvePluginState.
74
+ for (const key of PLUGIN_OPTION_KEYS) {
75
+ if (key !== 'resources' && options[key] !== undefined) assert.equal(config[key === 'bundle' ? 'bundleMode' : key], options[key])
76
+ }
57
77
  } catch (cause) {
58
78
  throw new Error(`Plugin options conflict with active stasis state: ${cause.message}`, { cause })
59
79
  }
@@ -61,145 +81,102 @@ export function assertOptionsMatchConfig(config, options) {
61
81
 
62
82
  export class Config {
63
83
  #env
84
+ #host
64
85
  #explicit
65
- #scope
66
- #lock
67
- #lockFile
68
- #bundle
69
- #bundleFile
70
- #resourcesBundleFile
71
- #resources
72
- #debug
73
- #childProcess
74
- #packageJSON
86
+ #opts
75
87
  #shardSignalFlush
76
- #fs
77
- #brotliQuality
78
88
 
79
89
  // Env and options must agree when both are set; #explicit is kept so a later loadConfig can't override them.
80
90
  constructor(options = {}) {
81
- const { scope, lock, lockFile, bundle, bundleFile, resourcesBundleFile, debug, resources, childProcess, packageJSON, fs, brotliQuality, ...rest } = options
82
- assert.equal(Object.keys(rest).length, 0, `Unknown Config options: ${Object.keys(rest).join(', ')}`)
83
- this.#explicit = { scope, lock, lockFile, bundle, bundleFile, resourcesBundleFile, debug, resources, childProcess, packageJSON, fs, brotliQuality }
84
-
85
- this.#env = {
86
- scope: process.env.EXODUS_STASIS_SCOPE || undefined,
87
- lock: process.env.EXODUS_STASIS_LOCK || undefined,
88
- lockFile: process.env.EXODUS_STASIS_LOCK_FILE || undefined,
89
- bundle: process.env.EXODUS_STASIS_BUNDLE || undefined,
90
- bundleFile: process.env.EXODUS_STASIS_BUNDLE_FILE || undefined,
91
- resourcesBundleFile: process.env.EXODUS_STASIS_RESOURCES_BUNDLE_FILE || undefined,
92
- debug: process.env.EXODUS_STASIS_DEBUG || undefined,
93
- resources: process.env.EXODUS_STASIS_RESOURCES || undefined,
94
- childProcess: process.env.EXODUS_STASIS_CHILD_PROCESS || undefined,
95
- packageJSON: process.env.EXODUS_STASIS_PACKAGE_JSON || undefined,
96
- shardSignalFlush: process.env.EXODUS_STASIS_SHARD_SIGNAL_FLUSH || undefined,
97
- fs: process.env.EXODUS_STASIS_FS || undefined,
98
- brotliQuality: process.env.EXODUS_STASIS_BROTLI_QUALITY || undefined,
99
- }
91
+ const { env = process.env, host = diskHost, ...rest } = options
92
+ assertKnownKeys(Object.keys(rest), OPTION_KEYS, 'Config')
93
+ // An empty-string path/mode option is unset, as its resolution below treats it; #explicit must
94
+ // agree, or loadConfig would refuse a stasis.config.json for not matching a phantom override.
95
+ const explicit = Object.fromEntries(Object.entries(rest).map(([key, value]) =>
96
+ [key, value === '' && (OPTIONS[key].valid || OPTIONS[key].type === 'string') ? undefined : value]))
97
+ this.#host = host
98
+ this.#explicit = explicit
99
+ // An env var set to '' counts as unset.
100
+ this.#env = Object.fromEntries(OPTION_KEYS.map((key) => [key, env[OPTIONS[key].env] || undefined]))
100
101
 
101
102
  try {
102
- if (this.#env.scope !== undefined && scope !== undefined) assert.equal(this.#env.scope, scope)
103
- if (this.#env.lock !== undefined && lock !== undefined) assert.equal(this.#env.lock, lock)
104
- if (this.#env.lockFile !== undefined && lockFile !== undefined) {
105
- assert.equal(this.#env.lockFile, lockFile)
106
- }
107
- if (this.#env.bundle !== undefined && bundle !== undefined) assert.equal(this.#env.bundle, bundle)
108
- if (this.#env.bundleFile !== undefined && bundleFile !== undefined) {
109
- assert.equal(this.#env.bundleFile, bundleFile)
110
- }
111
- if (this.#env.resourcesBundleFile !== undefined && resourcesBundleFile !== undefined) {
112
- assert.equal(this.#env.resourcesBundleFile, resourcesBundleFile)
113
- }
114
- if (this.#env.debug !== undefined && debug !== undefined) {
115
- assert.equal(envBool('EXODUS_STASIS_DEBUG', this.#env.debug), debug)
116
- }
117
- if (this.#env.childProcess !== undefined && childProcess !== undefined) {
118
- assert.equal(envBool('EXODUS_STASIS_CHILD_PROCESS', this.#env.childProcess), childProcess)
119
- }
120
- if (this.#env.packageJSON !== undefined && packageJSON !== undefined) {
121
- assert.equal(envBool('EXODUS_STASIS_PACKAGE_JSON', this.#env.packageJSON), packageJSON)
122
- }
123
- if (this.#env.fs !== undefined && fs !== undefined) assert.equal(this.#env.fs, fs)
124
- if (this.#env.brotliQuality !== undefined && brotliQuality !== undefined) {
125
- assert.equal(parseBrotliQuality('EXODUS_STASIS_BROTLI_QUALITY', this.#env.brotliQuality), brotliQuality)
126
- }
127
- // Compare parsed sets, not raw strings: ['png','svg'] and 'svg,png' are the same allowlist.
128
- if (this.#env.resources !== undefined && resources !== undefined) {
129
- assert.ok(
130
- extSetsEqual(parseResourcesOption('env', envList(this.#env.resources)), parseResourcesOption('options', resources)),
131
- 'resources option does not match EXODUS_STASIS_RESOURCES'
132
- )
103
+ for (const key of OPTION_KEYS) {
104
+ if (this.#env[key] !== undefined && explicit[key] !== undefined) {
105
+ assertSame(key, this.#envValue(key), this.#explicitValue(key), 'resources option does not match EXODUS_STASIS_RESOURCES')
106
+ }
133
107
  }
134
108
  } catch (cause) {
135
109
  throw new Error('Config options can not override stasis env', { cause })
136
110
  }
137
111
 
138
- this.#scope = this.#env.scope || scope || DEFAULT_SCOPE
139
- this.#lock = this.#env.lock || lock || DEFAULT_LOCK
140
- this.#lockFile = this.#env.lockFile || lockFile || undefined
141
- this.#bundle = this.#env.bundle || bundle || DEFAULT_BUNDLE
142
- this.#bundleFile = this.#env.bundleFile || bundleFile || undefined
143
- this.#resourcesBundleFile = this.#env.resourcesBundleFile || resourcesBundleFile || undefined
144
- this.#debug = this.#env.debug !== undefined ? envBool('EXODUS_STASIS_DEBUG', this.#env.debug) : (debug ?? false)
145
- this.#childProcess = this.#env.childProcess !== undefined ? envBool('EXODUS_STASIS_CHILD_PROCESS', this.#env.childProcess) : (childProcess ?? false)
146
- this.#packageJSON = this.#env.packageJSON !== undefined ? envBool('EXODUS_STASIS_PACKAGE_JSON', this.#env.packageJSON) : (packageJSON ?? false)
147
- this.#shardSignalFlush = this.#env.shardSignalFlush !== undefined
148
- ? envBool('EXODUS_STASIS_SHARD_SIGNAL_FLUSH', this.#env.shardSignalFlush)
149
- : false
150
- this.#fs = this.#env.fs || fs || undefined
151
- // No `||` chaining here -- 0 is a valid quality and falsy.
152
- this.#brotliQuality = this.#env.brotliQuality !== undefined
153
- ? parseBrotliQuality('EXODUS_STASIS_BROTLI_QUALITY', this.#env.brotliQuality)
154
- : brotliQuality
155
- this.#resources = parseResourcesOption(
156
- 'resources',
157
- this.#env.resources !== undefined ? envList(this.#env.resources) : resources
158
- )
112
+ // Strings: env, else the option, else the default ('' falls through); booleans keep a false.
113
+ const str = (key, fallback) => this.#envValue(key) || explicit[key] || fallback
114
+ const val = (key, fallback) => this.#envValue(key) ?? explicit[key] ?? fallback
115
+ this.#opts = {
116
+ scope: str('scope', DEFAULT_SCOPE),
117
+ lock: str('lock', DEFAULT_LOCK),
118
+ lockFile: str('lockFile', undefined),
119
+ bundle: str('bundle', DEFAULT_BUNDLE),
120
+ bundleFile: str('bundleFile', undefined),
121
+ resourcesBundleFile: str('resourcesBundleFile', undefined),
122
+ debug: val('debug', false),
123
+ childProcess: val('childProcess', false),
124
+ packageJSON: val('packageJSON', false),
125
+ fs: str('fs', undefined),
126
+ brotliQuality: this.#envValue('brotliQuality') ?? explicit.brotliQuality,
127
+ resources: parseResourcesOption('resources', this.#env.resources !== undefined ? envList(this.#env.resources) : explicit.resources),
128
+ }
129
+ const flush = env.EXODUS_STASIS_SHARD_SIGNAL_FLUSH
130
+ this.#shardSignalFlush = Boolean(flush) && envBool('EXODUS_STASIS_SHARD_SIGNAL_FLUSH', flush)
159
131
 
160
132
  this.#checkInvariants()
161
133
  }
162
134
 
135
+ // An option's env value, parsed; undefined when unset.
136
+ #envValue(key) {
137
+ const raw = this.#env[key]
138
+ const { env, parse } = OPTIONS[key]
139
+ return raw === undefined || !parse ? raw : parse(env, raw)
140
+ }
141
+
142
+ #explicitValue(key) {
143
+ const value = this.#explicit[key]
144
+ return value !== undefined && OPTIONS[key].option ? OPTIONS[key].option(value) : value
145
+ }
146
+
163
147
  #checkInvariants() {
164
- assert.ok(VALID_SCOPE.has(this.#scope), `Invalid scope: ${this.#scope}`)
165
- assert.ok(VALID_LOCK.has(this.#lock), `Invalid lock: ${this.#lock}`)
166
- assert.ok(VALID_BUNDLE.has(this.#bundle), `Invalid bundle: ${this.#bundle}`)
167
- assert.equal(typeof this.#debug, 'boolean', 'debug must be a boolean')
168
- assert.equal(typeof this.#childProcess, 'boolean', 'childProcess must be a boolean')
169
- assert.equal(typeof this.#packageJSON, 'boolean', 'packageJSON must be a boolean')
170
- if (this.#brotliQuality !== undefined && !isBrotliQuality(this.#brotliQuality)) {
171
- throw new RangeError(`brotliQuality must be an integer 0..11 (got ${JSON.stringify(this.#brotliQuality)})`)
148
+ const { lock, lockFile, bundle, bundleFile, resourcesBundleFile, fs, brotliQuality } = this.#opts
149
+ for (const key of ['scope', 'lock', 'bundle', 'debug', 'childProcess', 'packageJSON']) assertOption(key, this.#opts[key])
150
+ if (brotliQuality !== undefined && !isBrotliQuality(brotliQuality)) {
151
+ throw new RangeError(`brotliQuality must be an integer 0..11 (got ${JSON.stringify(brotliQuality)})`)
172
152
  }
173
- if (this.#fs !== undefined) {
174
- assert.ok(VALID_FS.has(this.#fs), `Invalid fs: ${this.#fs}`)
153
+ if (fs !== undefined) {
154
+ assertOption('fs', fs)
175
155
  // --fs captures into the bundle (add/replace) or serves from it (load); other modes have nothing to do.
176
- if (this.#bundle !== 'add' && this.#bundle !== 'replace' && this.#bundle !== 'load') {
177
- throw new RangeError(`fs requires bundle=(add|replace|load) (got bundle='${this.#bundle}')`)
156
+ if (!['add', 'replace', 'load'].includes(bundle)) {
157
+ throw new RangeError(`fs requires bundle=(add|replace|load) (got bundle='${bundle}')`)
178
158
  }
179
159
  }
180
- if (this.#bundleFile !== undefined) assert.equal(typeof this.#bundleFile, 'string', 'bundleFile must be a string')
181
- if (this.#lockFile !== undefined) {
182
- assert.equal(typeof this.#lockFile, 'string', 'lockFile must be a string')
183
- if (this.#lock === 'none' || this.#lock === 'ignore') {
184
- throw new RangeError(`lockFile requires an active lock mode (got lock='${this.#lock}')`)
160
+ if (bundleFile !== undefined) assertOption('bundleFile', bundleFile)
161
+ if (lockFile !== undefined) {
162
+ assertOption('lockFile', lockFile)
163
+ if (lock === 'none' || lock === 'ignore') {
164
+ throw new RangeError(`lockFile requires an active lock mode (got lock='${lock}')`)
185
165
  }
186
166
  }
187
- if (this.#resourcesBundleFile !== undefined) {
188
- assert.equal(typeof this.#resourcesBundleFile, 'string', 'resourcesBundleFile must be a string')
189
- if (this.#bundle === 'none' || this.#bundle === 'ignore') {
190
- throw new RangeError(`resourcesBundleFile requires an active bundle mode (got bundle='${this.#bundle}')`)
167
+ if (resourcesBundleFile !== undefined) {
168
+ assertOption('resourcesBundleFile', resourcesBundleFile)
169
+ if (bundle === 'none' || bundle === 'ignore') {
170
+ throw new RangeError(`resourcesBundleFile requires an active bundle mode (got bundle='${bundle}')`)
191
171
  }
192
172
  }
193
173
 
194
174
  // Write targets must be distinct: canonicalize so './x.br' vs 'x.br' and symlinks to one inode compare equal.
195
175
  const claimed = new Map()
196
- for (const [label, value] of [
197
- ['lockFile', this.#lockFile],
198
- ['bundleFile', this.#bundleFile],
199
- ['resourcesBundleFile', this.#resourcesBundleFile],
200
- ]) {
176
+ for (const label of ['lockFile', 'bundleFile', 'resourcesBundleFile']) {
177
+ const value = this.#opts[label]
201
178
  if (value === undefined) continue
202
- const canonical = canonicalizePath(value)
179
+ const canonical = canonicalizePath(value, this.#host)
203
180
  if (claimed.has(canonical)) {
204
181
  throw new RangeError(`${label} '${value}' targets the same file as ${claimed.get(canonical)}; each must be a distinct path`)
205
182
  }
@@ -207,77 +184,39 @@ export class Config {
207
184
  }
208
185
 
209
186
  // bundle=load needs a trust root: frozen pins each file's sha512, else the bundle itself is it (none/ignore).
210
- if (this.#bundle === 'load' && this.#lock !== 'frozen' && this.#lock !== 'none' && this.#lock !== 'ignore') {
187
+ if (bundle === 'load' && !['frozen', 'none', 'ignore'].includes(lock)) {
211
188
  throw new RangeError('bundle=load is incompatible with lock=(add|replace)')
212
189
  }
213
190
 
214
191
  // bundle=frozen deliberately constrains no lock mode: it verifies disk against the bundle, its own trust root.
215
- if (this.#lock === 'none' && this.#bundle === 'none') {
192
+ if (lock === 'none' && bundle === 'none') {
216
193
  throw new RangeError('stasis needs a lockfile or a bundle: set lock or bundle')
217
194
  }
218
195
  }
219
196
 
220
197
  loadConfig(json) {
221
- const {
222
- scope = this.#scope,
223
- lock = this.#lock,
224
- bundle = this.#bundle,
225
- bundleFile = this.#bundleFile,
226
- resourcesBundleFile = this.#resourcesBundleFile,
227
- debug = this.#debug,
228
- childProcess = this.#childProcess,
229
- packageJSON = this.#packageJSON,
230
- fs = this.#fs,
231
- brotliQuality = this.#brotliQuality,
232
- resources,
233
- ...rest
234
- } = JSON.parse(json)
235
- assert.equal(Object.keys(rest).length, 0)
236
- this.#scope = scope
237
- this.#lock = lock
238
- this.#bundle = bundle
239
- // '' -> undefined like the constructor: otherwise canonicalizePath('') claims cwd as a write target.
240
- this.#bundleFile = bundleFile || undefined
241
- this.#resourcesBundleFile = resourcesBundleFile || undefined
242
- this.#debug = debug
243
- this.#childProcess = childProcess
244
- this.#packageJSON = packageJSON
245
- this.#fs = fs
246
- this.#brotliQuality = brotliQuality
247
- if (resources !== undefined) this.#resources = parseResourcesOption('stasis.config.json', resources)
198
+ const config = JSON.parse(json)
199
+ const keys = Object.keys(config)
200
+ assert.equal(keys.filter((key) => !CONFIG_FILE_KEYS.includes(key)).length, 0)
201
+ for (const key of keys) {
202
+ if (key === 'resources') this.#opts.resources = parseResourcesOption('stasis.config.json', config.resources)
203
+ // '' -> undefined like the constructor: otherwise canonicalizePath('') claims cwd as a write target.
204
+ else if (key === 'bundleFile' || key === 'resourcesBundleFile') this.#opts[key] = config[key] || undefined
205
+ else this.#opts[key] = config[key]
206
+ }
248
207
  this.#checkInvariants()
249
208
 
250
209
  try {
251
- if (this.#env.scope !== undefined) assert.equal(this.#scope, this.#env.scope)
252
- if (this.#env.lock !== undefined) assert.equal(this.#lock, this.#env.lock)
253
- if (this.#env.bundle !== undefined) assert.equal(this.#bundle, this.#env.bundle)
254
- if (this.#env.bundleFile !== undefined) assert.equal(this.#bundleFile, this.#env.bundleFile)
255
- if (this.#env.resourcesBundleFile !== undefined) assert.equal(this.#resourcesBundleFile, this.#env.resourcesBundleFile)
256
- if (this.#env.debug !== undefined) assert.equal(this.#debug, envBool('EXODUS_STASIS_DEBUG', this.#env.debug))
257
- if (this.#env.childProcess !== undefined) assert.equal(this.#childProcess, envBool('EXODUS_STASIS_CHILD_PROCESS', this.#env.childProcess))
258
- if (this.#env.packageJSON !== undefined) assert.equal(this.#packageJSON, envBool('EXODUS_STASIS_PACKAGE_JSON', this.#env.packageJSON))
259
- if (this.#env.fs !== undefined) assert.equal(this.#fs, this.#env.fs)
260
- if (this.#env.brotliQuality !== undefined) {
261
- assert.equal(this.#brotliQuality, parseBrotliQuality('EXODUS_STASIS_BROTLI_QUALITY', this.#env.brotliQuality))
262
- }
263
- if (this.#env.resources !== undefined) {
264
- assert.ok(extSetsEqual(this.#resources, parseResourcesOption('env', envList(this.#env.resources))),
265
- 'resources in stasis.config.json must match EXODUS_STASIS_RESOURCES')
210
+ for (const key of CONFIG_FILE_KEYS) {
211
+ if (this.#env[key] !== undefined) {
212
+ assertSame(key, this.#opts[key], this.#envValue(key), 'resources in stasis.config.json must match EXODUS_STASIS_RESOURCES')
213
+ }
266
214
  }
267
215
  // Explicit constructor options are equally authoritative: an on-disk config can't override --scope=full.
268
- if (this.#explicit.scope !== undefined) assert.equal(this.#scope, this.#explicit.scope)
269
- if (this.#explicit.lock !== undefined) assert.equal(this.#lock, this.#explicit.lock)
270
- if (this.#explicit.bundle !== undefined) assert.equal(this.#bundle, this.#explicit.bundle)
271
- if (this.#explicit.bundleFile !== undefined) assert.equal(this.#bundleFile, this.#explicit.bundleFile)
272
- if (this.#explicit.resourcesBundleFile !== undefined) assert.equal(this.#resourcesBundleFile, this.#explicit.resourcesBundleFile)
273
- if (this.#explicit.debug !== undefined) assert.equal(this.#debug, this.#explicit.debug)
274
- if (this.#explicit.childProcess !== undefined) assert.equal(this.#childProcess, this.#explicit.childProcess)
275
- if (this.#explicit.packageJSON !== undefined) assert.equal(this.#packageJSON, this.#explicit.packageJSON)
276
- if (this.#explicit.fs !== undefined) assert.equal(this.#fs, this.#explicit.fs)
277
- if (this.#explicit.brotliQuality !== undefined) assert.equal(this.#brotliQuality, this.#explicit.brotliQuality)
278
- if (this.#explicit.resources !== undefined) {
279
- assert.ok(extSetsEqual(this.#resources, parseResourcesOption('options', this.#explicit.resources)),
280
- 'resources in stasis.config.json must match the resources option')
216
+ for (const key of CONFIG_FILE_KEYS) {
217
+ if (this.#explicit[key] !== undefined) {
218
+ assertSame(key, this.#opts[key], this.#explicitValue(key), 'resources in stasis.config.json must match the resources option')
219
+ }
281
220
  }
282
221
  } catch (cause) {
283
222
  throw new Error('Flags/env can not override stasis.config.json', { cause })
@@ -285,36 +224,36 @@ export class Config {
285
224
  }
286
225
 
287
226
  get bundleFile() {
288
- return this.#bundleFile
227
+ return this.#opts.bundleFile
289
228
  }
290
229
 
291
230
  // Exact lockfile path, bypassing `stasis.lock.json` discovery; bundler plugins can't set it.
292
231
  get lockFile() {
293
- return this.#lockFile
232
+ return this.#opts.lockFile
294
233
  }
295
234
 
296
235
  // When set, resources go to this file and code-only to bundleFile; bundle=load reads both.
297
236
  get resourcesBundleFile() {
298
- return this.#resourcesBundleFile
237
+ return this.#opts.resourcesBundleFile
299
238
  }
300
239
 
301
240
  // Set of lowercase extensions/filenames treated as asset payloads.
302
241
  get resources() {
303
- return this.#resources
242
+ return this.#opts.resources
304
243
  }
305
244
 
306
245
  get debug() {
307
- return this.#debug
246
+ return this.#opts.debug
308
247
  }
309
248
 
310
249
  // Opt-in cross-process capture forwarding: a forked child writes a shard the root merges. Not attested.
311
250
  get childProcess() {
312
- return this.#childProcess
251
+ return this.#opts.childProcess
313
252
  }
314
253
 
315
254
  // Opt-in: bundle every bundled module's package.json even if the run never reached it. Not attested.
316
255
  get packageJSON() {
317
- return this.#packageJSON
256
+ return this.#opts.packageJSON
318
257
  }
319
258
 
320
259
  // Env-only: a capturing child flushes its shard on SIGTERM (jest-worker force-kills Metro workers),
@@ -325,83 +264,84 @@ export class Config {
325
264
 
326
265
  // --fs hook mode: 'sync' patches the sync readers, 'async' both; undefined leaves fs untouched. Not serialized.
327
266
  get fs() {
328
- return this.#fs
267
+ return this.#opts.fs
329
268
  }
330
269
 
331
270
  // Brotli quality (0..11); undefined -> DEFAULT_BROTLI_QUALITY. Not serialized -- output decompresses identically.
332
271
  get brotliQuality() {
333
- return this.#brotliQuality
272
+ return this.#opts.brotliQuality
334
273
  }
335
274
 
336
275
  get values() {
337
- return { scope: this.#scope }
276
+ return { scope: this.#opts.scope }
338
277
  }
339
278
 
340
279
  get bundle() {
341
- return this.#bundle !== 'none' && this.#bundle !== 'ignore'
280
+ return this.#opts.bundle !== 'none' && this.#opts.bundle !== 'ignore'
342
281
  }
343
282
 
344
283
  get bundleMode() {
345
- return this.#bundle
284
+ return this.#opts.bundle
346
285
  }
347
286
 
348
287
  get ignoreBundle() {
349
- return this.#bundle === 'ignore'
288
+ return this.#opts.bundle === 'ignore'
350
289
  }
351
290
 
352
291
  get writeBundle() {
353
- return this.#bundle === 'add' || this.#bundle === 'replace'
292
+ return this.#opts.bundle === 'add' || this.#opts.bundle === 'replace'
354
293
  }
355
294
 
356
295
  get replaceBundle() {
357
- return this.#bundle === 'replace'
296
+ return this.#opts.bundle === 'replace'
358
297
  }
359
298
 
360
299
  get loadBundle() {
361
- return this.#bundle === 'load'
300
+ return this.#opts.bundle === 'load'
362
301
  }
363
302
 
364
303
  // bundle=frozen: loaded, never rewritten, and disk is checked against it (load serves bytes instead).
365
304
  get frozenBundle() {
366
- return this.#bundle === 'frozen'
305
+ return this.#opts.bundle === 'frozen'
367
306
  }
368
307
 
369
308
  get full() {
370
- return this.#scope === 'full'
309
+ return this.#opts.scope === 'full'
371
310
  }
372
311
 
373
312
  get frozen() {
374
- return this.#lock === 'frozen'
313
+ return this.#opts.lock === 'frozen'
375
314
  }
376
315
 
377
316
  get useLockfile() {
378
- return this.#lock !== 'none' && this.#lock !== 'ignore'
317
+ return this.#opts.lock !== 'none' && this.#opts.lock !== 'ignore'
379
318
  }
380
319
 
381
320
  get ignoreLockfile() {
382
- return this.#lock === 'ignore'
321
+ return this.#opts.lock === 'ignore'
383
322
  }
384
323
 
385
324
  get writeLockfile() {
386
- return this.#lock === 'add' || this.#lock === 'replace'
325
+ return this.#opts.lock === 'add' || this.#opts.lock === 'replace'
387
326
  }
388
327
 
389
328
  get replaceLockfile() {
390
- return this.#lock === 'replace'
329
+ return this.#opts.lock === 'replace'
391
330
  }
392
331
 
393
332
  get scope() {
394
- return this.#scope
333
+ return this.#opts.scope
395
334
  }
396
335
 
397
336
  get lock() {
398
- return this.#lock
337
+ return this.#opts.lock
399
338
  }
400
339
 
401
340
  get json() {
402
- const data = { scope: this.#scope, lock: this.#lock, bundle: this.#bundle }
341
+ const { scope, lock, bundle, resources } = this.#opts
342
+ const data = { scope, lock, bundle }
403
343
  // Only serialize `resources` when non-empty, so configs without an allowlist stay byte-identical.
404
- if (this.#resources.size > 0) data.resources = [...this.#resources].toSorted()
344
+ if (resources.size > 0) data.resources = [...resources].toSorted()
405
345
  return JSON.stringify(data, undefined, 2) + '\n'
406
346
  }
407
347
  }
package/src/extract.js CHANGED
@@ -5,7 +5,7 @@ import { brotliDecompressSync } from 'node:zlib'
5
5
  import { Bundle } from './bundle.js'
6
6
  import { Lockfile } from './lockfile.js'
7
7
  import { sha512integrity } from './state-util.js'
8
- import { EXECUTE_BITS, canObserveExecuteBits, moduleFileKey } from './util.js'
8
+ import { EXECUTE_BITS, canObserveExecuteBits, moduleFileKey, moduleInfo } from './util.js'
9
9
 
10
10
  const FILE_LOCK = 'stasis.lock.json'
11
11
 
@@ -20,19 +20,14 @@ function assertCanonicalBase64(content, file) {
20
20
 
21
21
  export function lockfileFromBundle(bundle) {
22
22
  const modules = new Map()
23
- for (const [dir, { name, version, ecosystem, files }] of bundle.modules) {
23
+ for (const [dir, { files, ...info }] of bundle.modules) {
24
24
  const hashed = Object.create(null)
25
25
  for (const [rel, content] of Object.entries(files)) {
26
26
  // Hash the raw on-disk bytes, so decode 'resource:base64' back first.
27
27
  const file = moduleFileKey(dir, rel)
28
- const bytes = bundle.formats.get(file) === 'resource:base64'
29
- ? assertCanonicalBase64(content, file)
30
- : content
31
- hashed[rel] = sha512integrity(bytes)
28
+ hashed[rel] = sha512integrity(bundle.formats.get(file) === 'resource:base64' ? assertCanonicalBase64(content, file) : content)
32
29
  }
33
- modules.set(dir, ecosystem === undefined
34
- ? { name, version, files: hashed }
35
- : { name, version, ecosystem, files: hashed })
30
+ modules.set(dir, moduleInfo({ ...info, files: hashed }))
36
31
  }
37
32
  // Carry imports+formats across, else the derived lockfile is bytes-only and a later frozen run skips those cross-checks.
38
33
  return new Lockfile({
@@ -73,11 +68,11 @@ export function extractCommand({ cwd = process.cwd(), bundleFile, output, logLab
73
68
  let executables = 0
74
69
  for (const [dir, { files }] of bundle.modules) {
75
70
  for (const [rel, content] of Object.entries(files)) {
76
- // `key` is the per-file map key (moduleFileKey handles rel === ''); `file` below is the WRITE path and keeps
77
- // the plain join, so a non-`directory` rel === '' fails the canonical-path check instead of writing at the bucket root.
71
+ // A `directory` capture is a listing at the dir's own path, not a file to write (its children recreate the dir).
78
72
  const key = moduleFileKey(dir, rel)
79
- // Skip a `directory` capture: it's a listing at the dir's own path, not a file to write (its children recreate the dir).
80
73
  if (bundle.formats.get(key) === 'directory') continue
74
+ // The WRITE path keeps the plain join, so a non-`directory` rel === '' fails the canonical-path
75
+ // check instead of writing at the bucket root.
81
76
  const file = dir === '.' ? rel : `${dir}/${rel}`
82
77
  if (typeof content !== 'string') throw new Error(`extract: bundle file content is not a string: ${file}`)
83
78
  const abs = resolve(outDir, file)
@@ -96,10 +91,7 @@ export function extractCommand({ cwd = process.cwd(), bundleFile, output, logLab
96
91
  // Executability rides the plan tuple, so the chmod can only ever reach a file we actually wrote.
97
92
  const exec = bundle.executable.has(key)
98
93
  if (exec) executables += 1
99
- const data = bundle.formats.get(key) === 'resource:base64'
100
- ? assertCanonicalBase64(content, file)
101
- : content
102
- writes.push([abs, data, exec])
94
+ writes.push([abs, bundle.formats.get(key) === 'resource:base64' ? assertCanonicalBase64(content, file) : content, exec])
103
95
  }
104
96
  }
105
97
  // A planned file must not also be a parent directory of another, else the writes throw halfway through.