@exodus/stasis-core 1.0.0-beta.3 → 1.0.0-beta.5

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.3",
3
+ "version": "1.0.0-beta.5",
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",
@@ -21,6 +22,7 @@
21
22
  "files": [
22
23
  "bin/stasis-core.js",
23
24
  "src/add.js",
25
+ "src/artifact-util.js",
24
26
  "src/brotli.js",
25
27
  "src/bundle.js",
26
28
  "src/bundle-util.js",
@@ -28,6 +30,7 @@
28
30
  "src/extract.js",
29
31
  "src/fs.js",
30
32
  "src/hooks.js",
33
+ "src/host.js",
31
34
  "src/loader.js",
32
35
  "src/lockfile.js",
33
36
  "src/package.cjs",
@@ -45,13 +48,13 @@
45
48
  },
46
49
  "repository": {
47
50
  "type": "git",
48
- "url": "git+https://github.com/ExodusOSS/stasis.git",
51
+ "url": "git+https://github.com/PreventiveMeasures/stasis.git",
49
52
  "directory": "stasis-core"
50
53
  },
51
54
  "author": "Exodus Movement, Inc.",
52
55
  "license": "MIT",
53
56
  "bugs": {
54
- "url": "https://github.com/ExodusOSS/stasis/issues"
57
+ "url": "https://github.com/PreventiveMeasures/stasis/issues"
55
58
  },
56
- "homepage": "https://github.com/ExodusOSS/stasis#readme"
59
+ "homepage": "https://github.com/PreventiveMeasures/stasis#readme"
57
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,15 @@ 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
+ const bucketFiles = (dir, name, version, ecosystem) => {
65
+ if (!modules.has(dir)) modules.set(dir, moduleInfo({ name, version, ecosystem, files: Object.create(null) }))
66
+ return modules.get(dir).files
67
67
  }
68
- // Memoize per directory: findPackageMetadata's result depends only on the file's directory.
68
+ // findPackageMetadata's result depends only on the file's directory.
69
69
  const metaByDir = new Map()
70
70
  const metaFor = (rel) => {
71
71
  const dir = dirname(rel)
@@ -75,31 +75,21 @@ function assembleBundle(baseDir, files, workspaceName, workspaceVersion) {
75
75
 
76
76
  for (const [rel, { content, format, executable: isExec }] of files) {
77
77
  const meta = metaFor(rel)
78
- const inNodeModules = splitNodeModulesPath(rel) !== null
78
+ // A node_modules file whose nearest package.json is the workspace root (or none at all) is a misconfigured dep -- refuse.
79
+ if (splitNodeModulesPath(rel) !== null && !(meta && hasNodeModulesSegment(meta.pkgDir))) {
80
+ throw new Error(`add: no package.json with name+version found for ${rel}`)
81
+ }
79
82
  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
83
  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
84
+ bucketFiles(meta.pkgDir, meta.name, meta.version, hasNodeModulesSegment(meta.pkgDir) ? 'npm' : undefined)[relInBucket] = content
87
85
  } else {
88
- if (inNodeModules) throw new Error(`add: no package.json with name+version found for ${rel}`)
89
- ensureBucket('.', workspaceName, workspaceVersion).files[rel] = content
86
+ bucketFiles('.', workspaceName, workspaceVersion)[rel] = content
90
87
  }
91
- formats.set(rel, format)
88
+ if (format != null) formats.set(rel, format)
92
89
  if (isExec) executable.add(rel)
93
90
  }
94
91
 
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')
92
+ return new Bundle({ config: { scope: 'full' }, entries: new Set(), modules, formats, imports: new Map(), executable, repo }).withReason('add')
103
93
  }
104
94
 
105
95
  // Merge add-if-missing into any existing bundle (divergent bytes for an attested path throw -- see
@@ -135,7 +125,7 @@ function bundleToLockfile(bundle, integrities) {
135
125
  for (const [dir, m] of bundle.modules) {
136
126
  const files = Object.create(null)
137
127
  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 })
128
+ modules.set(dir, moduleInfo({ ...m, files }))
139
129
  }
140
130
  return new Lockfile({ config: bundle.config, entries: bundle.entries, modules, imports: bundle.imports, formats: bundle.formats, executable: bundle.executable })
141
131
  }
@@ -168,12 +158,12 @@ function expandDirectories(baseDir, rels, outputs) {
168
158
  const files = new Map() // rel -> stats
169
159
  const excluded = new Set()
170
160
  for (const rel of rels) {
171
- const stats = statSync(join(baseDir, rel), { throwIfNoEntry: false })
161
+ const dirAbs = join(baseDir, rel)
162
+ const stats = statSync(dirAbs, { throwIfNoEntry: false })
172
163
  if (stats === undefined || !stats.isDirectory()) {
173
164
  files.set(rel, stats)
174
165
  continue
175
166
  }
176
- const dirAbs = join(baseDir, rel)
177
167
  const found = []
178
168
  for (const match of globSync('**/*', { cwd: dirAbs })) {
179
169
  const fileAbs = join(dirAbs, match)
@@ -181,8 +171,8 @@ function expandDirectories(baseDir, rels, outputs) {
181
171
  const fileStats = statSync(fileAbs, { throwIfNoEntry: false })
182
172
  if (fileStats?.isFile()) found.push([toPosix(relative(baseDir, fileAbs)), match, fileStats])
183
173
  }
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.
174
+ // Sorted so the sweep is deterministic regardless of readdir order: artifact bytes are
175
+ // canonicalized by serialize() either way, but multi-file diagnostics keep a stable order.
186
176
  for (const [file, match, fileStats] of found.toSorted(([a], [b]) => sortPaths(a, b))) {
187
177
  if (toPosix(match).split('/').slice(0, -1).some(isAutoExcludedDir)
188
178
  || outputs.has(file) || isAutoExcludedFile(file)) excluded.add(file)
@@ -199,9 +189,9 @@ const listPaths = (rels) => [...rels].toSorted(sortPaths).join(', ')
199
189
  // One error for every file validation rejected, so a directory sweep names all of them at once
200
190
  // instead of one per re-run. A lone offender keeps its exact standalone message.
201
191
  function validationError({ missing, undeclared, nonUtf8 }) {
202
- // `one`/`many` per category; empty categories contribute nothing.
203
192
  const phrase = (items, one, many) => (items.length === 1 ? [one(items[0])] : items.length > 1 ? [many(items)] : [])
204
- const named = ({ rel, format }) => `${rel} (format '${format}')`
193
+ const withFormat = (format) => (format == null ? '' : ` (format '${format}')`)
194
+ const named = ({ rel, format }) => `${rel}${withFormat(format)}`
205
195
  const parts = [
206
196
  ...phrase(missing,
207
197
  (rel) => `file not found: ${rel}`,
@@ -210,7 +200,7 @@ function validationError({ missing, undeclared, nonUtf8 }) {
210
200
  (rel) => `${rel} is neither a recognized source file nor a declared resource; add its extension to "resources" in ${CONFIG_FILE}`,
211
201
  (rels) => `${rels.length} files are neither recognized source files nor declared resources; add their extensions to "resources" in ${CONFIG_FILE}: ${listPaths(rels)}`),
212
202
  ...phrase(nonUtf8,
213
- ({ rel, format }) => `${rel} is not valid UTF-8 (format '${format}')`,
203
+ ({ rel, format }) => `${rel} is not valid UTF-8${withFormat(format)}`,
214
204
  (bad) => `${bad.length} files are not valid UTF-8: ${bad.map(named).join(', ')}`),
215
205
  ]
216
206
  return new Error(`add: ${parts.join('; ')}`)
@@ -234,14 +224,13 @@ function validateFiles({ baseDir, realBase, files, resources, withIntegrity }) {
234
224
  continue
235
225
  }
236
226
  const executable = isExecutableMode(stats)
237
- // Refuse a symlink whose realpath escapes the project root -- a bundle carries only in-tree bytes.
238
227
  assertRealPathWithinBase(realBase, baseDir, rel)
239
228
  const abs = join(baseDir, rel)
240
229
  const buf = readFileSync(abs)
241
230
  if (withIntegrity) integrities.set(rel, sha512integrity(buf))
242
231
  const format = sourceFormat(abs, buf)
243
232
  // 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)) {
233
+ if (format !== undefined && !isBinaryPlist(rel, buf)) {
245
234
  // Source is stored as a UTF-8 string, so non-UTF-8 bytes would lossily diverge from the file on disk.
246
235
  if (isUtf8(buf)) codeFiles.set(rel, { content: buf.toString('utf8'), format, executable })
247
236
  else nonUtf8.push({ rel, format })
@@ -295,9 +284,10 @@ export function addCommand({ cwd = process.cwd(), entries, logLabel = 'stasis-co
295
284
  // ADD step: every target's merge is computed first, then the writes run -- see prepareBundleFile.
296
285
  const summary = []
297
286
  const writes = []
287
+ const repo = detectRepo(baseDir)
298
288
  const planTarget = (target, entriesForTarget, kind) => {
299
289
  if (entriesForTarget.size === 0) return
300
- const { write, counts } = prepareBundleFile(baseDir, target, assembleBundle(baseDir, entriesForTarget, workspaceName, workspaceVersion), brotliQuality)
290
+ const { write, counts } = prepareBundleFile(baseDir, target, assembleBundle(baseDir, entriesForTarget, workspaceName, workspaceVersion, repo), brotliQuality)
301
291
  writes.push(write)
302
292
  summary.push(`+${counts.added} ${kind} (${counts.total} total) -> ${counts.path}`)
303
293
  }
@@ -305,9 +295,8 @@ export function addCommand({ cwd = process.cwd(), entries, logLabel = 'stasis-co
305
295
  planTarget(bundleFile, codeFiles, 'source')
306
296
  planTarget(resourcesBundleFile, resourceFiles, 'resource')
307
297
  } else {
308
- const all = new Map([...codeFiles, ...resourceFiles])
309
298
  const kind = codeFiles.size > 0 && resourceFiles.size > 0 ? 'file' : resourceFiles.size > 0 ? 'resource' : 'source'
310
- planTarget(bundleFile, all, kind)
299
+ planTarget(bundleFile, new Map([...codeFiles, ...resourceFiles]), kind)
311
300
  }
312
301
 
313
302
  // One lockfile attests both split targets, so merge in every packed file's integrity (code + resources together).