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

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.6",
3
+ "version": "1.0.0-beta.7",
4
4
  "description": "Zero-dependency stasis runtime",
5
5
  "type": "module",
6
6
  "exports": {
@@ -95,15 +95,12 @@ export const normalizeRepo = (repo, what = 'bundle repo') => normalizeBlock(repo
95
95
  // node_modules (an artifact from before the tag); undefined for first-party code.
96
96
  export const dependencyEcosystem = (dir, ecosystem) => ecosystem ?? (hasNodeModulesSegment(dir) ? 'npm' : undefined)
97
97
 
98
- // A dependency's `repo` holds a build's own fields, but never `directory: ''`: only a layout a build
99
- // reads, its own, places code at a repository's root, never a manifest (packageRepo).
100
- const DEPENDENCY_REPO_FIELDS = { ...REPO_FIELDS, directory: (v) => v !== '' && REPO_FIELDS.directory(v) }
101
-
102
- // A dependency's `repo`, the one its own manifest names; first-party code carries none.
98
+ // A dependency's `repo`, the one its own manifest names, with the bundle's own fields; first-party
99
+ // code carries none.
103
100
  const normalizeModuleRepo = (dir, { ecosystem, repo }, what) => {
104
101
  if (repo === undefined) return undefined
105
102
  if (dependencyEcosystem(dir, ecosystem) === undefined) assert(false, `${what}: '${dir}' is no dependency's bucket, and carries no repo`)
106
- return normalizeBlock(repo, DEPENDENCY_REPO_FIELDS, `${what} module '${dir}' repo`)
103
+ return normalizeRepo(repo, `${what} module '${dir}' repo`)
107
104
  }
108
105
 
109
106
  // A module bucket record in canonical key order; `ecosystem` and `repo` are omitted (not undefined) when absent.
@@ -356,15 +353,16 @@ export function serializeImports(imports) {
356
353
  }
357
354
 
358
355
  // A module map as the serialized `modules` (node_modules buckets) and `sources` (the rest) objects
359
- // of a `what` artifact, buckets and files path-sorted so the bytes are canonical.
360
- export function groupModules(modules, { skipEmpty = false, what } = {}) {
356
+ // of a `what` artifact, buckets and files path-sorted so the bytes are canonical. `repo: false`
357
+ // leaves each record's `repo` out: metadata, which a lockfile never carries.
358
+ export function groupModules(modules, { skipEmpty = false, repo = true, what } = {}) {
361
359
  const grouped = { modules: [], sources: [] }
362
360
  for (const [dir, info] of modules) {
363
361
  if (skipEmpty && Object.keys(info.files).length === 0) continue
364
362
  const inNodeModules = hasNodeModulesSegment(dir)
365
363
  if (inNodeModules) assert(info.name && info.version && info.files)
366
364
  const files = fromEntries(Object.entries(info.files).toSorted(byPath))
367
- grouped[inNodeModules ? 'modules' : 'sources'].push([dir, moduleInfo({ ...info, repo: normalizeModuleRepo(dir, info, what), files })])
365
+ grouped[inNodeModules ? 'modules' : 'sources'].push([dir, moduleInfo({ ...info, repo: repo ? normalizeModuleRepo(dir, info, what) : undefined, files })])
368
366
  }
369
367
  return { modules: fromEntries(grouped.modules.toSorted(byPath)), sources: fromEntries(grouped.sources.toSorted(byPath)) }
370
368
  }
@@ -493,8 +491,7 @@ export function mergeModuleMaps(a, b, label) {
493
491
  `package without one; regenerate it (bundle=replace / lock=replace)`))
494
492
  assert(existing.ecosystem === info.ecosystem,
495
493
  `${label}: module '${dir}' ecosystem mismatch ('${existing.ecosystem ?? '(none)'}' vs '${info.ecosystem ?? '(none)'}')`)
496
- assert(reposAgree(existing.repo, info.repo),
497
- `${label}: module '${dir}' repo mismatch (${JSON.stringify(existing.repo)} vs ${JSON.stringify(info.repo)})`)
494
+ // `repo` is metadata, held to nothing: either side's, where only one records it, else `a`'s.
498
495
  if (existing.repo === undefined && info.repo !== undefined) out.set(dir, moduleInfo({ ...existing, repo: info.repo }))
499
496
  for (const [rel, value] of Object.entries(info.files)) {
500
497
  if (Object.hasOwn(existing.files, rel)) {
@@ -202,14 +202,20 @@ export function githubHomepageDirectory(homepage, github) {
202
202
  // `{ directory }` where it is a valid one, else `{}`.
203
203
  const repoLocation = (directory) => (isValidRepoField('directory', directory) ? { directory } : {})
204
204
 
205
+ // Whether a declared path is the repository's root: nothing but empty and `.` parts (`./`, `/.`).
206
+ const isRootPath = (path) => path.replaceAll('\\', '/').split('/').every((part) => part === '' || part === '.')
207
+
205
208
  // A package.json's declared `base` (its `repository.directory`) joined with `rel` as a `repoLocation`;
206
- // `{}` where none is declared (unknown, not the root), or where it comes to the root: `directory: ''`
207
- // is recorded from a layout read (detectRepo's git step), never a manifest's claim. Windows users
208
- // write `base` with `\`: metadata, not a name on disk, so its `\` is taken for a separator.
209
+ // `{}` where none is declared (unknown, not the root), or where `base` has a `..` part, whatever it
210
+ // would come to (`a/..` is no claim on the root). Its empty and `.` parts are dropped, and one that
211
+ // comes to the root (`''`, `.`, `./`, `/`, `/.`) is the root, `''`. Windows users write `base` with
212
+ // `\`: metadata, not a name on disk, so its `\` is taken for a separator.
209
213
  const declaredLocation = (base, rel) => {
210
214
  if (typeof base !== 'string') return {}
211
- const directory = posix.join(base.replaceAll('\\', '/'), rel).replace(/^\/+|\/+$/gu, '')
212
- return directory === '' || directory === '.' ? {} : repoLocation(directory) // `/`, `./`, `a/../`
215
+ const path = base.replaceAll('\\', '/')
216
+ if (path.split('/').includes('..')) return {}
217
+ const directory = posix.join(path, rel).replace(/^\/+|\/+$/gu, '')
218
+ return repoLocation(directory === '.' ? '' : directory)
213
219
  }
214
220
 
215
221
  // Git and common dirs, following a worktree/submodule `.git` file.
@@ -247,8 +253,13 @@ function repositoryUrl(json) {
247
253
  export function packageRepo(json, rel = '') {
248
254
  const github = parseGithubRepository(repositoryUrl(json))
249
255
  if (!github) return undefined
250
- // Often unset; fall back to a GitHub tree `homepage`.
251
- const base = typeof json.repository.directory === 'string' ? json.repository.directory : githubHomepageDirectory(json.homepage, github)
256
+ let base = json.repository.directory
257
+ // Often unset; fall back to a GitHub tree `homepage`, which places a package below the root, never
258
+ // at it: only `repository.directory` declares the root (`/tree/main/.` declares nothing).
259
+ if (typeof base !== 'string') {
260
+ base = githubHomepageDirectory(json.homepage, github)
261
+ if (base !== undefined && isRootPath(base)) base = undefined
262
+ }
252
263
  return { github, ...declaredLocation(base, rel) }
253
264
  }
254
265
 
package/src/lockfile.js CHANGED
@@ -4,6 +4,9 @@ const VERSION = 0
4
4
 
5
5
  const duplicateKey = duplicateKeyError('lockfile', 'lockfile (lock=replace)')
6
6
 
7
+ // A record as a lockfile holds it: without `repo`, metadata a bundle carries and nothing attests.
8
+ const lockRecord = ({ repo: _metadata, ...info }) => info
9
+
7
10
  export class Lockfile {
8
11
  static VERSION = VERSION
9
12
 
@@ -43,7 +46,7 @@ export class Lockfile {
43
46
  for (const [dir, info] of Object.entries(json.modules)) {
44
47
  assert(hasNodeModulesSegment(dir))
45
48
  assert(info?.name && info.version && info.files)
46
- modules.set(dir, normalizeModule(info, dir, 'lockfile'))
49
+ modules.set(dir, normalizeModule(lockRecord(info), dir, 'lockfile'))
47
50
  }
48
51
 
49
52
  let entries = new Set()
@@ -53,7 +56,7 @@ export class Lockfile {
53
56
  assert(!hasNodeModulesSegment(dir))
54
57
  // A workspace bucket may omit version (a private/unpublished package.json can lack one).
55
58
  assert(info?.name && info.files)
56
- modules.set(dir, normalizeModule(info, dir, 'lockfile'))
59
+ modules.set(dir, normalizeModule(lockRecord(info), dir, 'lockfile'))
57
60
  }
58
61
  }
59
62
 
@@ -78,7 +81,7 @@ export class Lockfile {
78
81
  serialize() {
79
82
  // Never write an artifact that parse would reject.
80
83
  flatFileKeys(this.modules, 'lockfile', duplicateKey)
81
- const { modules, sources } = groupModules(this.modules, { what: 'lockfile' })
84
+ const { modules, sources } = groupModules(this.modules, { repo: false, what: 'lockfile' })
82
85
  const store = { version: this.version, config: this.config }
83
86
  if (this.config.scope === 'full') Object.assign(store, { entries: serializeEntries(this.entries, 'lockfile'), sources })
84
87
  store.modules = modules
package/src/state.js CHANGED
@@ -11,7 +11,7 @@ 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, canObserveExecuteBits, classifyFormat, erasedTypeScriptFormat, fileMapToObject, hasNodeModulesSegment, isBinaryPlist, isNativeArtifact, isPathWithin, isStatFormat, moduleFileKey, moduleInfo, narrowExecutable, nestedMap, objectToMaps, observeExecutable, pathExt, reconcileFormat, relativeEscapes, reposAgree, sameRepo, sortPaths, splitNodeModulesPath } from './util.js'
14
+ import { CODE_EXTENSIONS, canObserveExecuteBits, classifyFormat, erasedTypeScriptFormat, fileMapToObject, hasNodeModulesSegment, isBinaryPlist, isNativeArtifact, isPathWithin, isStatFormat, moduleFileKey, moduleInfo, narrowExecutable, nestedMap, objectToMaps, observeExecutable, pathExt, reconcileFormat, relativeEscapes, sortPaths, splitNodeModulesPath } from './util.js'
15
15
  import { detectRepo, packageJSONStat, packageJSONText, packageRepo, readModuleManifest } from './bundle-util.js'
16
16
  import { diskHost } from './host.js'
17
17
  import corePackage from './package.cjs'
@@ -449,9 +449,7 @@ export class State {
449
449
  // a real workspace identity (or a stripped field), never v0 partial metadata -- a
450
450
  // version-less bundle bucket must not dodge the lockfile consistency check.
451
451
  assert.equal(info.version, lockModule.version, `bundle module ${dir} version mismatch with lockfile`)
452
- assert.ok(reposAgree(info.repo, lockModule.repo), `bundle module ${dir} repo mismatch with lockfile`)
453
- // A lockfile from before the field records none: the bundle's then holds the package.json
454
- // read and the other half of a split bundle to it.
452
+ // `repo` is metadata, which a lockfile never records: the bundle's is taken, to be written again.
455
453
  if (lockModule.repo === undefined && info.repo !== undefined) this.modules.set(dir, moduleInfo({ ...lockModule, repo: info.repo }))
456
454
  for (const rel of Object.keys(info.files)) {
457
455
  assert.ok(Object.hasOwn(lockModule.files, rel), `bundle file ${dir}/${rel} missing in lockfile`)
@@ -500,7 +498,7 @@ export class State {
500
498
  // A dir may be added twice (code + resource entries), and both must agree.
501
499
  assert.equal(info.name, existing.name, `bundle ${dir} name mismatch`)
502
500
  assert.equal(info.version, existing.version, `bundle ${dir} version mismatch`)
503
- assert.ok(reposAgree(info.repo, existing.repo), `bundle ${dir} repo mismatch`)
501
+ // `repo` is metadata, held to nothing: the first half's that records one.
504
502
  if (existing.repo === undefined && info.repo !== undefined) this.modules.set(dir, moduleInfo({ ...existing, repo: info.repo }))
505
503
  }
506
504
  }
@@ -788,14 +786,8 @@ export class State {
788
786
  assert.fail(`module identity mismatch for '${dir}': artifact records ` +
789
787
  `'${module.name}@${module.version ?? '(none)'}', package.json has '${name}@${version ?? '(none)'}'${hint}`)
790
788
  }
791
- // A dependency's repo is its package.json's, held to the artifact's record as its identity is; an
792
- // artifact from an older stasis records none, and gets it.
793
- if (module.repo === undefined) {
794
- if (repo !== undefined) this.modules.set(dir, moduleInfo({ ...module, repo }))
795
- } else if (repo === undefined || !sameRepo(module.repo, repo)) {
796
- assert.fail(`module repo mismatch for '${dir}': artifact records ${JSON.stringify(module.repo)}, ` +
797
- `package.json names ${repo === undefined ? 'no GitHub repository' : JSON.stringify(repo)}`)
798
- }
789
+ // A dependency's repo is metadata, its package.json's, held to nothing: a record with none gets it.
790
+ if (module.repo === undefined && repo !== undefined) this.modules.set(dir, moduleInfo({ ...module, repo }))
799
791
 
800
792
  return { absolute, file, dir, module, closestType }
801
793
  }