@exodus/stasis-core 1.0.0-beta.12 → 1.0.0-beta.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/stasis-core",
3
- "version": "1.0.0-beta.12",
3
+ "version": "1.0.0-beta.13",
4
4
  "description": "Zero-dependency stasis runtime",
5
5
  "type": "module",
6
6
  "exports": {
@@ -67,6 +67,19 @@ export function namedManifestOf(rel, read) {
67
67
  return undefined
68
68
  }
69
69
 
70
+ // The package.json nearest above a file `rel` of the package at `pkgDir` (both project-relative,
71
+ // POSIX), short of the package's own: a `{"type":"module"}` marker such as `esm/package.json`, or a
72
+ // subpackage's or vendored package's, which Node takes the file's `type` from and Metro's resolver
73
+ // (getPackageForModule) the file's fields. `exists(rel)` says whether one is there -> its rel, or
74
+ // undefined where the package's own is the nearest.
75
+ export function scopeManifestOf(rel, pkgDir, exists) {
76
+ if (pkgDir !== '.' && !rel.startsWith(`${pkgDir}/`)) return undefined
77
+ for (let dir = posix.dirname(rel); dir !== pkgDir && dir !== '.'; dir = posix.dirname(dir)) {
78
+ if (exists(`${dir}/package.json`)) return `${dir}/package.json`
79
+ }
80
+ return undefined
81
+ }
82
+
70
83
  // Nearest package.json (walking up) that identifies a bucket; pkgDir is relative to baseDir ("."
71
84
  // at the root). Inside node_modules both name and version are required; a workspace package
72
85
  // outside node_modules may omit version (the name alone claims the bucket, matching
@@ -341,7 +354,7 @@ function repositoryUrl(json) {
341
354
  // The `repo` a parsed package.json's `repository` names for `rel`, a directory below it ('' for its
342
355
  // own): `github` from its GitHub URL or shorthand, and `directory` from `repository.directory` joined
343
356
  // with `rel` (declaredLocation); undefined where it names no GitHub repository.
344
- export function packageRepo(json, rel = '') {
357
+ function declaredRepo(json, rel) {
345
358
  const github = parseGithubRepository(repositoryUrl(json))
346
359
  if (!github) return undefined
347
360
  let base = json.repository.directory
@@ -354,8 +367,18 @@ export function packageRepo(json, rel = '') {
354
367
  return { github, ...declaredLocation(base, rel) }
355
368
  }
356
369
 
370
+ // A dependency's `repo`, from its parsed package.json: the repository it names (declaredRepo), at the
371
+ // commit its `gitHead` names where that is a full one. npm publish writes `gitHead` into the
372
+ // registry's version document alone, but lerna writes it into the published package.json too, and
373
+ // npm keeps one a package.json has, so the two agree. Undefined where it names no GitHub repository.
374
+ export function packageRepo(json) {
375
+ const repo = declaredRepo(json, '')
376
+ return repo !== undefined && isValidRepoField('commit', json.gitHead) ? { ...repo, commit: json.gitHead } : repo
377
+ }
378
+
357
379
  // Bundle `repo` for `dir`: git origin/HEAD at the work tree root, with `dir`'s place in it (`''` at
358
- // the root), else nearest package.json `repository` (packageRepo).
380
+ // the root), else nearest package.json `repository` (declaredRepo). Its commit is git's HEAD alone,
381
+ // never a package.json's `gitHead`: a project's own files are likely changed since the one it names.
359
382
  export function detectRepo(dir, host = diskHost) {
360
383
  const start = resolve(dir)
361
384
  let pkg = null // null: no package.json `repository` seen yet; undefined: one seen, not GitHub
@@ -363,7 +386,7 @@ export function detectRepo(dir, host = diskHost) {
363
386
  const rel = toPosix(relative(cursor, start))
364
387
  if (pkg === null) {
365
388
  const json = readJson(join(cursor, 'package.json'), host)
366
- if (repositoryUrl(json) !== undefined) pkg = packageRepo(json, rel)
389
+ if (repositoryUrl(json) !== undefined) pkg = declaredRepo(json, rel)
367
390
  }
368
391
  const dotGit = join(cursor, '.git')
369
392
  if (host.stat(dotGit) !== null) {
package/src/state.js CHANGED
@@ -11,8 +11,8 @@ import { Lockfile } from './lockfile.js'
11
11
  import { parseShard, serializeShard } from './shard.js'
12
12
  import { canonicalizePath, sha512integrity, readFileSyncMaybe, noupsert } from './state-util.js'
13
13
  import { brotliOptions } from './brotli.js'
14
- import { CODE_EXTENSIONS, EMPTY_MODULE_PATH, assertRealPathWithinBase, canObserveExecuteBits, classifyFormat, erasedTypeScriptFormat, fileMapToObject, hasNodeModulesSegment, isBinaryPlist, isNativeArtifact, isPackageString, isPathWithin, isStatFormat, isSubpackage, moduleFileKey, moduleInfo, narrowExecutable, nestedMap, objectToMaps, observeExecutable, pathExt, reconcileFormat, relativeEscapes, sortPaths, splitNodeModulesPath, toPosix, withMetadataOf } from './util.js'
15
- import { detectRepo, namedManifestOf, packageJSONStat, packageJSONText, packageRepo, readModuleManifest, vendorDirOf, vendoredPackageOf } from './bundle-util.js'
14
+ import { CODE_EXTENSIONS, EMPTY_MODULE_PATH, assertRealPathWithinBase, canObserveExecuteBits, classifyFormat, erasedTypeScriptFormat, fileMapToObject, hasNodeModulesSegment, isBinaryPlist, isNativeArtifact, isPackageString, isPathWithin, isPlainObject, isStatFormat, isSubpackage, moduleFileKey, moduleInfo, narrowExecutable, nestedMap, objectToMaps, observeExecutable, pathExt, reconcileFormat, relativeEscapes, sortPaths, splitNodeModulesPath, toPosix, withMetadataOf } from './util.js'
15
+ import { detectRepo, namedManifestOf, packageJSONStat, packageJSONText, packageRepo, readModuleManifest, scopeManifestOf, vendorDirOf, vendoredPackageOf } from './bundle-util.js'
16
16
  import { expandExtra, validationError } from './add.js'
17
17
  import { diskHost } from './host.js'
18
18
  import corePackage from './package.cjs'
@@ -54,6 +54,9 @@ function upsertFormat(map, file, format) {
54
54
  map.set(file, currentFormat === undefined ? format : reconcileFormat(format, currentFormat, file))
55
55
  }
56
56
 
57
+ // A bundled file's integrity as addFile/addFsDir hash it: a 'resource:base64' payload's is its decoded bytes'.
58
+ const bundledIntegrity = (content, format) => sha512integrity(format === 'resource:base64' ? Buffer.from(content, 'base64') : content)
59
+
57
60
  // Loader formats the extension alone decides; `.js`/`.ts` follow the nearest package `type`.
58
61
  const EXT_FORMATS = { __proto__: null, '.json': 'json', '.mjs': 'module', '.cjs': 'commonjs', '.mts': 'module-typescript', '.cts': 'commonjs-typescript' }
59
62
  // With no `type`, Node decides .js/.ts by syntax detection: either variant is possible, the first is the default.
@@ -525,6 +528,18 @@ export class State {
525
528
  const withMetadata = withMetadataOf(existing, info)
526
529
  if (withMetadata !== existing) this.modules.set(dir, withMetadata)
527
530
  }
531
+ // A bundle=add run writes these buckets back, and #bundleModules carries only the files a
532
+ // bucket lists: seed them as #absorbLockfile does, else a bundled file this run doesn't
533
+ // load again drops out of the rewritten bundle.
534
+ if (!this.config.writeBundle) continue
535
+ const { files } = this.modules.get(dir)
536
+ for (const [rel, content] of Object.entries(info.files)) {
537
+ const file = moduleFileKey(dir, rel)
538
+ const integrity = bundledIntegrity(content, bundle.formats.get(file))
539
+ noupsert(this.hashes, file, integrity)
540
+ if (!Object.hasOwn(files, rel)) files[rel] = integrity
541
+ assert.equal(files[rel], integrity, `bundle file ${file} conflicts with its recorded hash`)
542
+ }
528
543
  }
529
544
  }
530
545
  }
@@ -1697,32 +1712,55 @@ export class State {
1697
1712
  }
1698
1713
 
1699
1714
  // Add every bundled module's `<dir>/package.json` (config.packageJSON) even if the run never
1700
- // reached it, so a load/prune can read each dependency's manifest. The skip keys on bundle
1701
- // membership, NOT this.hashes (which also holds lockfile-absorbed entries and is shared by
1702
- // reference across sidecars).
1715
+ // reached it, so a load/prune can read each dependency's manifest, and the nearer one a bundled
1716
+ // file of it has (scopeManifestOf: a `{"type":"module"}` marker such as `esm/package.json`), which
1717
+ // Node and Metro's resolver read for the file. The skip keys on bundle membership, NOT this.hashes
1718
+ // (which also holds lockfile-absorbed entries and is shared by reference across sidecars).
1703
1719
  includePackageJson() {
1704
1720
  // Gather first: addFile mutates this.modules, so don't addFile while iterating it.
1705
1721
  const realRoot = this.#host.realpath(this.root)
1706
1722
  const claims = this.#reasonClaims()
1723
+ const bundled = (rel) => this.sources.has(rel) || this.resources.has(rel)
1724
+ const found = new Map()
1725
+ const exists = (rel) => found.get(rel) ?? found.set(rel, packageJSONStat(this.#host, resolve(this.root, rel))?.isFile() ?? false).get(rel)
1726
+ // A manifest's bytes and parsed fields, or null where it's gone or malformed (skipped).
1727
+ const read = (rel) => {
1728
+ const buf = readModuleManifest({ baseDir: this.root, realBase: realRoot, rel, host: this.#host })
1729
+ if (!buf) return null
1730
+ try {
1731
+ const pkg = JSON.parse(packageJSONText(buf))
1732
+ return isPlainObject(pkg) ? { buf, pkg } : null
1733
+ } catch {
1734
+ return null
1735
+ }
1736
+ }
1707
1737
  const toAdd = []
1708
1738
  for (const [dir, module] of this.modules) {
1709
- const rel = moduleFileKey(dir, 'package.json')
1710
- if (this.sources.has(rel) || this.resources.has(rel)) continue // already in this bundle
1711
- const buf = readModuleManifest({ baseDir: this.root, realBase: realRoot, rel, host: this.#host })
1712
- if (!buf) continue
1713
- // Only carry a manifest whose on-disk identity still matches the bundled bucket: under
1739
+ const own = moduleFileKey(dir, 'package.json')
1740
+ // Only carry the manifests of a bucket whose on-disk identity still matches: under
1714
1741
  // bundle=add a dep may have drifted, and addFile -> #locateModule would crash on it.
1715
- let pkg
1716
- try { pkg = JSON.parse(buf.toString()) } catch { continue } // malformed manifest for an untouched bucket: skip
1717
- if (pkg?.name !== module.name || pkg?.version !== module.version) continue
1718
- // The manifest rides along BECAUSE some consumer bundled the bucket, so it inherits every
1719
- // consumer that claims a file of the bucket (#reasonClaims: not run for a file it merely
1720
- // fs-read for a plugin) -- nothing observed the manifest itself, but an unattributed file
1721
- // can't be sorted by provenance downstream. Derived BEFORE the adds below, so one fold's
1722
- // manifest can't count as another bucket's consumer evidence.
1723
- const files = Object.keys(module.files)
1724
- const consumers = [...claims].filter(([, claimed]) => files.some((f) => claimed.has(moduleFileKey(dir, f)))).map(([who]) => who)
1725
- toAdd.push({ rel, buf, consumers })
1742
+ const manifest = read(own)
1743
+ if (manifest === null || manifest.pkg.name !== module.name || manifest.pkg.version !== module.version) continue
1744
+ // Each manifest -> the files it is the package.json of: the bucket's own, of every file; a
1745
+ // nearer one, of the bundled files below it.
1746
+ const files = Object.keys(module.files).map((f) => moduleFileKey(dir, f))
1747
+ const scopes = new Map([[own, files]])
1748
+ for (const file of files) {
1749
+ const nearest = bundled(file) ? scopeManifestOf(file, dir, exists) : undefined
1750
+ if (nearest !== undefined) (scopes.get(nearest) ?? scopes.set(nearest, []).get(nearest)).push(file)
1751
+ }
1752
+ for (const [rel, of] of scopes) {
1753
+ if (bundled(rel)) continue // already in this bundle
1754
+ const buf = rel === own ? manifest.buf : read(rel)?.buf
1755
+ if (buf === undefined) continue
1756
+ // The manifest rides along BECAUSE some consumer bundled its files, so it inherits every
1757
+ // consumer that claims one (#reasonClaims: not run for a file it merely fs-read for a
1758
+ // plugin) -- nothing observed the manifest itself, but an unattributed file can't be
1759
+ // sorted by provenance downstream. Derived BEFORE the adds below, so one fold's manifest
1760
+ // can't count as another bucket's consumer evidence.
1761
+ const consumers = [...claims].filter(([, claimed]) => of.some((file) => claimed.has(file))).map(([who]) => who)
1762
+ toAdd.push({ rel, buf, consumers })
1763
+ }
1726
1764
  }
1727
1765
  for (const { rel, buf, consumers } of toAdd) {
1728
1766
  this.addFile(pathToFileURL(resolve(this.root, rel)).toString(), { source: buf, reason: null })