@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.
@@ -20,7 +20,12 @@ assert(basename(jsname) === 'stasis-core' || pathsEqual(jsname, fileURLToPath(im
20
20
 
21
21
  function usage(prefix = '') {
22
22
  console.error(`${prefix}\nUsage:
23
- stasis-core run --lock=(add|replace|frozen|ignore) [--bundle=(add|replace|load|frozen|ignore)] [--bundle-file=path/to/bundle.br] [--resources-bundle-file=path/to/resources.br] [--dependencies] [--child-process] [--fs=(sync|async)] [--resources=ext,ext] [--brotli-quality=0..11] path/to/file.js ...
23
+ stasis-core run --lock=(add|replace|frozen|ignore) [--bundle=(add|replace|load|frozen|ignore)] [--bundle-file=path/to/bundle.br] [--resources-bundle-file=path/to/resources.br] [--dependencies] [--child-process] [--import=module ...] [--fs=(sync|async)] [--resources=ext,ext] [--brotli-quality=0..11] path/to/file.js ...
24
+ (--import forwards extra preload modules (repeatable) to the node process running the entry,
25
+ e.g. --import=./instrument.mjs for a setup/instrumentation preload; a preload's own module
26
+ graph is runner infrastructure like stasis's loader itself, so it stays out of the
27
+ lockfile/bundle -- except modules the app graph also reaches, which are attested like any
28
+ other app code)
24
29
  stasis-core add path/to/(file|dir) ...
25
30
  (adds the listed files to the project's bundle(s) with no dependency resolution;
26
31
  a directory expands to its files. Requires a stasis.config.json (all fields optional).)
@@ -51,12 +56,13 @@ if (command === '-v' || command === '--version') {
51
56
  debug: { type: 'boolean' },
52
57
  dependencies: { type: 'boolean' },
53
58
  'child-process': { type: 'boolean' },
59
+ import: { type: 'string', multiple: true },
54
60
  fs: { type: 'string' },
55
61
  resources: { type: 'string' },
56
62
  'brotli-quality': { type: 'string' },
57
63
  }
58
64
  const values = parseLeadingOptions(argv, options, {
59
- valueFlags: ['--bundle', '--bundle-file', '--resources-bundle-file', '--lock', '--resources', '--brotli-quality'],
65
+ valueFlags: ['--bundle', '--bundle-file', '--resources-bundle-file', '--lock', '--import', '--resources', '--brotli-quality'],
60
66
  onError: usage,
61
67
  })
62
68
  if (argv.length === 0) usage('Nothing to run: no path to file given')
@@ -85,7 +91,11 @@ if (command === '-v' || command === '--version') {
85
91
  }
86
92
  }
87
93
  const childProcess = values['child-process'] ? '1' : ''
88
- console.warn('[stasis-core] Running stasis with config:', { lock, scope, bundle, ...(bundleFile && { bundleFile }), ...(resourcesBundleFile && { resourcesBundleFile }), ...(childProcess && { childProcess: true }), ...(values.fs && { fs: values.fs }), ...(resources && { resources }), ...(brotliQuality !== undefined && { brotliQuality }) })
94
+ // --import: extra preload module(s) passed through to the spawned node. Node resolves each
95
+ // against the project cwd; they ride AFTER stasis's own loader import.
96
+ const imports = values.import ?? []
97
+ if (imports.some((s) => s === '')) usage('Error: --import requires a module specifier (e.g. --import=./instrument.mjs)')
98
+ console.warn('[stasis-core] Running stasis with config:', { lock, scope, bundle, ...(bundleFile && { bundleFile }), ...(resourcesBundleFile && { resourcesBundleFile }), ...(childProcess && { childProcess: true }), ...(imports.length > 0 && { import: imports }), ...(values.fs && { fs: values.fs }), ...(resources && { resources }), ...(brotliQuality !== undefined && { brotliQuality }) })
89
99
  if (debug) console.warn(`[stasis-core] Warning: stasis debug mode active`)
90
100
  setEnv('EXODUS_STASIS_LOCK', lock)
91
101
  setEnv('EXODUS_STASIS_SCOPE', scope)
@@ -99,6 +109,10 @@ if (command === '-v' || command === '--version') {
99
109
  // Only set when given: an unconditional setEnv('') would reject an ambient EXODUS_STASIS_BROTLI_QUALITY as a conflict.
100
110
  if (brotliQuality !== undefined) setEnv('EXODUS_STASIS_BROTLI_QUALITY', String(brotliQuality))
101
111
  const nodeArgs = ['--import', import.meta.resolve('../src/loader.js')]
112
+ // Passthrough preloads ride after the loader, so they evaluate under its hooks; their module
113
+ // graphs pass through uncaptured unless the app graph reaches them -- then they're promoted
114
+ // into the capture (see src/hooks.js).
115
+ for (const specifier of imports) nodeArgs.push('--import', specifier)
102
116
  const child = spawn(process.execPath, [...nodeArgs, ...argv], { stdio: 'inherit' })
103
117
  const [code, signal] = await once(child, 'close')
104
118
  // code is null when the child died from a signal; report 128+signo (shell convention) instead of the implicit 0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/stasis-core",
3
- "version": "1.0.0-beta.4",
3
+ "version": "1.0.0-beta.6",
4
4
  "description": "Zero-dependency stasis runtime",
5
5
  "type": "module",
6
6
  "exports": {
@@ -12,6 +12,7 @@
12
12
  "./add": "./src/add.js",
13
13
  "./bundle-util": "./src/bundle-util.js",
14
14
  "./extract": "./src/extract.js",
15
+ "./host": "./src/host.js",
15
16
  "./lockfile": "./src/lockfile.js",
16
17
  "./util": "./src/util.js",
17
18
  "./brotli": "./src/brotli.js",
@@ -29,6 +30,7 @@
29
30
  "src/extract.js",
30
31
  "src/fs.js",
31
32
  "src/hooks.js",
33
+ "src/host.js",
32
34
  "src/loader.js",
33
35
  "src/lockfile.js",
34
36
  "src/package.cjs",
@@ -46,13 +48,13 @@
46
48
  },
47
49
  "repository": {
48
50
  "type": "git",
49
- "url": "git+https://github.com/ExodusOSS/stasis.git",
51
+ "url": "git+https://github.com/PreventiveMeasures/stasis.git",
50
52
  "directory": "stasis-core"
51
53
  },
52
54
  "author": "Exodus Movement, Inc.",
53
55
  "license": "MIT",
54
56
  "bugs": {
55
- "url": "https://github.com/ExodusOSS/stasis/issues"
57
+ "url": "https://github.com/PreventiveMeasures/stasis/issues"
56
58
  },
57
- "homepage": "https://github.com/ExodusOSS/stasis#readme"
59
+ "homepage": "https://github.com/PreventiveMeasures/stasis#readme"
58
60
  }
package/src/add.js CHANGED
@@ -6,24 +6,28 @@ import { brotliCompressSync, brotliDecompressSync } from 'node:zlib'
6
6
  import { Bundle } from './bundle.js'
7
7
  import { Lockfile } from './lockfile.js'
8
8
  import { brotliOptions } from './brotli.js'
9
- import { findPackageMetadata, normalizeEntries, packageType, readJson } from './bundle-util.js'
9
+ import { detectRepo, findPackageMetadata, normalizeEntries, packageType, readJson } from './bundle-util.js'
10
10
  import { canonicalizePath, sha512integrity } from './state-util.js'
11
- import { assertRealPathWithinBase, classifyFormat, hasNodeModulesSegment, isAutoExcludedDir, isAutoExcludedFile, isBinaryPlist, isBrotliQuality, isExecutableMode, moduleFileKey, parseResourcesOption, pathExt, sortPaths, splitNodeModulesPath, toPosix } from './util.js'
11
+ import { assertRealPathWithinBase, classifyFormat, hasNodeModulesSegment, isAutoExcludedDir, isAutoExcludedFile, isBinaryPlist, isBrotliQuality, isExecutableMode, moduleFileKey, moduleInfo, parseResourcesOption, pathExt, sortPaths, splitNodeModulesPath, toPosix } from './util.js'
12
12
 
13
13
  const CONFIG_FILE = 'stasis.config.json'
14
14
  const LOCK_FILE = 'stasis.lock.json'
15
15
 
16
- // LIMITATION: a `.js`/`.ts` with no package `type` falls back to commonjs -- use `.mjs`/`.cjs` or set `type` when the module system matters.
16
+ // A `.js`/`.ts` follows its package `type` (LIMITATION: none falls back to commonjs -- use `.mjs`/`.cjs`
17
+ // or set `type` when the module system matters). Any other name is classifyFormat's call: a format,
18
+ // null for a JS-family file (`.jsx`/`.tsx`: source attested with no loader format, as the runtime
19
+ // records one), undefined when unrecognized.
17
20
  function sourceFormat(absFile, content) {
18
21
  const ext = extname(absFile).toLowerCase()
19
22
  if (ext === '.js' || ext === '.ts') return `${packageType(absFile) ?? 'commonjs'}${ext === '.ts' ? '-typescript' : ''}`
20
- return classifyFormat(absFile, { content }) ?? null
23
+ return classifyFormat(absFile, { content })
21
24
  }
22
25
 
23
- const tally = (bundle) => {
26
+ // File and non-empty package counts of a bundle or lockfile.
27
+ const tally = (artifact) => {
24
28
  let files = 0
25
29
  let packages = 0
26
- for (const m of bundle.modules.values()) {
30
+ for (const m of artifact.modules.values()) {
27
31
  const n = Object.keys(m.files).length
28
32
  if (n > 0) {
29
33
  files += n
@@ -53,19 +57,16 @@ function readAddConfig(baseDir) {
53
57
  }
54
58
 
55
59
  // Assemble a Bundle bucketed per package.json. `add` records NO entries -- its files are attested, not entry points -- so nothing in an add bundle is runnable via `--bundle=load`.
56
- function assembleBundle(baseDir, files, workspaceName, workspaceVersion) {
60
+ function assembleBundle(baseDir, files, workspaceName, workspaceVersion, repo) {
57
61
  const modules = new Map()
58
62
  const formats = new Map()
59
63
  const executable = new Set()
60
- const ensureBucket = (dir, name, version, ecosystem) => {
61
- if (!modules.has(dir)) {
62
- modules.set(dir, ecosystem === undefined
63
- ? { name, version, files: Object.create(null) }
64
- : { name, version, ecosystem, files: Object.create(null) })
65
- }
66
- return modules.get(dir)
64
+ // `identity`'s name, version, ecosystem and repo (moduleInfo takes those alone).
65
+ const bucketFiles = (dir, identity) => {
66
+ if (!modules.has(dir)) modules.set(dir, moduleInfo({ ...identity, files: Object.create(null) }))
67
+ return modules.get(dir).files
67
68
  }
68
- // Memoize per directory: findPackageMetadata's result depends only on the file's directory.
69
+ // findPackageMetadata's result depends only on the file's directory.
69
70
  const metaByDir = new Map()
70
71
  const metaFor = (rel) => {
71
72
  const dir = dirname(rel)
@@ -75,31 +76,21 @@ function assembleBundle(baseDir, files, workspaceName, workspaceVersion) {
75
76
 
76
77
  for (const [rel, { content, format, executable: isExec }] of files) {
77
78
  const meta = metaFor(rel)
78
- const inNodeModules = splitNodeModulesPath(rel) !== null
79
+ // A node_modules file whose nearest package.json is the workspace root (or none at all) is a misconfigured dep -- refuse.
80
+ if (splitNodeModulesPath(rel) !== null && !(meta && hasNodeModulesSegment(meta.pkgDir))) {
81
+ throw new Error(`add: no package.json with name+version found for ${rel}`)
82
+ }
79
83
  if (meta) {
80
- // A node_modules file whose nearest package.json is the workspace root is a misconfigured dep -- refuse.
81
- if (inNodeModules && !hasNodeModulesSegment(meta.pkgDir)) {
82
- throw new Error(`add: no package.json with name+version found for ${rel}`)
83
- }
84
84
  const relInBucket = meta.pkgDir === '.' ? rel : rel.slice(meta.pkgDir.length + 1)
85
- const ecosystem = hasNodeModulesSegment(meta.pkgDir) ? 'npm' : undefined
86
- ensureBucket(meta.pkgDir, meta.name, meta.version, ecosystem).files[relInBucket] = content
85
+ bucketFiles(meta.pkgDir, meta)[relInBucket] = content
87
86
  } else {
88
- if (inNodeModules) throw new Error(`add: no package.json with name+version found for ${rel}`)
89
- ensureBucket('.', workspaceName, workspaceVersion).files[rel] = content
87
+ bucketFiles('.', { name: workspaceName, version: workspaceVersion })[rel] = content
90
88
  }
91
- formats.set(rel, format)
89
+ if (format != null) formats.set(rel, format)
92
90
  if (isExec) executable.add(rel)
93
91
  }
94
92
 
95
- return new Bundle({
96
- config: { scope: 'full' },
97
- entries: new Set(),
98
- modules,
99
- formats,
100
- imports: new Map(),
101
- executable,
102
- }).withReason('add')
93
+ return new Bundle({ config: { scope: 'full' }, entries: new Set(), modules, formats, imports: new Map(), executable, repo }).withReason('add')
103
94
  }
104
95
 
105
96
  // Merge add-if-missing into any existing bundle (divergent bytes for an attested path throw -- see
@@ -135,7 +126,7 @@ function bundleToLockfile(bundle, integrities) {
135
126
  for (const [dir, m] of bundle.modules) {
136
127
  const files = Object.create(null)
137
128
  for (const rel of Object.keys(m.files)) files[rel] = integrities.get(moduleFileKey(dir, rel))
138
- modules.set(dir, { name: m.name, version: m.version, ...(m.ecosystem === undefined ? {} : { ecosystem: m.ecosystem }), files })
129
+ modules.set(dir, moduleInfo({ ...m, files }))
139
130
  }
140
131
  return new Lockfile({ config: bundle.config, entries: bundle.entries, modules, imports: bundle.imports, formats: bundle.formats, executable: bundle.executable })
141
132
  }
@@ -168,12 +159,12 @@ function expandDirectories(baseDir, rels, outputs) {
168
159
  const files = new Map() // rel -> stats
169
160
  const excluded = new Set()
170
161
  for (const rel of rels) {
171
- const stats = statSync(join(baseDir, rel), { throwIfNoEntry: false })
162
+ const dirAbs = join(baseDir, rel)
163
+ const stats = statSync(dirAbs, { throwIfNoEntry: false })
172
164
  if (stats === undefined || !stats.isDirectory()) {
173
165
  files.set(rel, stats)
174
166
  continue
175
167
  }
176
- const dirAbs = join(baseDir, rel)
177
168
  const found = []
178
169
  for (const match of globSync('**/*', { cwd: dirAbs })) {
179
170
  const fileAbs = join(dirAbs, match)
@@ -181,8 +172,8 @@ function expandDirectories(baseDir, rels, outputs) {
181
172
  const fileStats = statSync(fileAbs, { throwIfNoEntry: false })
182
173
  if (fileStats?.isFile()) found.push([toPosix(relative(baseDir, fileAbs)), match, fileStats])
183
174
  }
184
- // Sorted because serialize() emits `reason` in insertion order (everything else it re-sorts):
185
- // without this, the attested bundle's BYTES would depend on readdir order.
175
+ // Sorted so the sweep is deterministic regardless of readdir order: artifact bytes are
176
+ // canonicalized by serialize() either way, but multi-file diagnostics keep a stable order.
186
177
  for (const [file, match, fileStats] of found.toSorted(([a], [b]) => sortPaths(a, b))) {
187
178
  if (toPosix(match).split('/').slice(0, -1).some(isAutoExcludedDir)
188
179
  || outputs.has(file) || isAutoExcludedFile(file)) excluded.add(file)
@@ -199,9 +190,9 @@ const listPaths = (rels) => [...rels].toSorted(sortPaths).join(', ')
199
190
  // One error for every file validation rejected, so a directory sweep names all of them at once
200
191
  // instead of one per re-run. A lone offender keeps its exact standalone message.
201
192
  function validationError({ missing, undeclared, nonUtf8 }) {
202
- // `one`/`many` per category; empty categories contribute nothing.
203
193
  const phrase = (items, one, many) => (items.length === 1 ? [one(items[0])] : items.length > 1 ? [many(items)] : [])
204
- const named = ({ rel, format }) => `${rel} (format '${format}')`
194
+ const withFormat = (format) => (format == null ? '' : ` (format '${format}')`)
195
+ const named = ({ rel, format }) => `${rel}${withFormat(format)}`
205
196
  const parts = [
206
197
  ...phrase(missing,
207
198
  (rel) => `file not found: ${rel}`,
@@ -210,7 +201,7 @@ function validationError({ missing, undeclared, nonUtf8 }) {
210
201
  (rel) => `${rel} is neither a recognized source file nor a declared resource; add its extension to "resources" in ${CONFIG_FILE}`,
211
202
  (rels) => `${rels.length} files are neither recognized source files nor declared resources; add their extensions to "resources" in ${CONFIG_FILE}: ${listPaths(rels)}`),
212
203
  ...phrase(nonUtf8,
213
- ({ rel, format }) => `${rel} is not valid UTF-8 (format '${format}')`,
204
+ ({ rel, format }) => `${rel} is not valid UTF-8${withFormat(format)}`,
214
205
  (bad) => `${bad.length} files are not valid UTF-8: ${bad.map(named).join(', ')}`),
215
206
  ]
216
207
  return new Error(`add: ${parts.join('; ')}`)
@@ -234,14 +225,13 @@ function validateFiles({ baseDir, realBase, files, resources, withIntegrity }) {
234
225
  continue
235
226
  }
236
227
  const executable = isExecutableMode(stats)
237
- // Refuse a symlink whose realpath escapes the project root -- a bundle carries only in-tree bytes.
238
228
  assertRealPathWithinBase(realBase, baseDir, rel)
239
229
  const abs = join(baseDir, rel)
240
230
  const buf = readFileSync(abs)
241
231
  if (withIntegrity) integrities.set(rel, sha512integrity(buf))
242
232
  const format = sourceFormat(abs, buf)
243
233
  // A binary plist can't be stored as the UTF-8 string its 'xml' format implies, so it is NOT source: it falls through to the resource branch (opaque base64).
244
- if (format !== null && !isBinaryPlist(rel, buf)) {
234
+ if (format !== undefined && !isBinaryPlist(rel, buf)) {
245
235
  // Source is stored as a UTF-8 string, so non-UTF-8 bytes would lossily diverge from the file on disk.
246
236
  if (isUtf8(buf)) codeFiles.set(rel, { content: buf.toString('utf8'), format, executable })
247
237
  else nonUtf8.push({ rel, format })
@@ -295,9 +285,10 @@ export function addCommand({ cwd = process.cwd(), entries, logLabel = 'stasis-co
295
285
  // ADD step: every target's merge is computed first, then the writes run -- see prepareBundleFile.
296
286
  const summary = []
297
287
  const writes = []
288
+ const repo = detectRepo(baseDir)
298
289
  const planTarget = (target, entriesForTarget, kind) => {
299
290
  if (entriesForTarget.size === 0) return
300
- const { write, counts } = prepareBundleFile(baseDir, target, assembleBundle(baseDir, entriesForTarget, workspaceName, workspaceVersion), brotliQuality)
291
+ const { write, counts } = prepareBundleFile(baseDir, target, assembleBundle(baseDir, entriesForTarget, workspaceName, workspaceVersion, repo), brotliQuality)
301
292
  writes.push(write)
302
293
  summary.push(`+${counts.added} ${kind} (${counts.total} total) -> ${counts.path}`)
303
294
  }
@@ -305,9 +296,8 @@ export function addCommand({ cwd = process.cwd(), entries, logLabel = 'stasis-co
305
296
  planTarget(bundleFile, codeFiles, 'source')
306
297
  planTarget(resourcesBundleFile, resourceFiles, 'resource')
307
298
  } else {
308
- const all = new Map([...codeFiles, ...resourceFiles])
309
299
  const kind = codeFiles.size > 0 && resourceFiles.size > 0 ? 'file' : resourceFiles.size > 0 ? 'resource' : 'source'
310
- planTarget(bundleFile, all, kind)
300
+ planTarget(bundleFile, new Map([...codeFiles, ...resourceFiles]), kind)
311
301
  }
312
302
 
313
303
  // One lockfile attests both split targets, so merge in every packed file's integrity (code + resources together).