@exodus/stasis-core 1.0.0-beta.5 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/stasis-core",
3
- "version": "1.0.0-beta.5",
3
+ "version": "1.0.0-beta.6",
4
4
  "description": "Zero-dependency stasis runtime",
5
5
  "type": "module",
6
6
  "exports": {
package/src/add.js CHANGED
@@ -61,8 +61,9 @@ function assembleBundle(baseDir, files, workspaceName, workspaceVersion, repo) {
61
61
  const modules = new Map()
62
62
  const formats = new Map()
63
63
  const executable = new Set()
64
- const bucketFiles = (dir, name, version, ecosystem) => {
65
- if (!modules.has(dir)) modules.set(dir, moduleInfo({ name, version, ecosystem, files: Object.create(null) }))
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) }))
66
67
  return modules.get(dir).files
67
68
  }
68
69
  // findPackageMetadata's result depends only on the file's directory.
@@ -81,9 +82,9 @@ function assembleBundle(baseDir, files, workspaceName, workspaceVersion, repo) {
81
82
  }
82
83
  if (meta) {
83
84
  const relInBucket = meta.pkgDir === '.' ? rel : rel.slice(meta.pkgDir.length + 1)
84
- bucketFiles(meta.pkgDir, meta.name, meta.version, hasNodeModulesSegment(meta.pkgDir) ? 'npm' : undefined)[relInBucket] = content
85
+ bucketFiles(meta.pkgDir, meta)[relInBucket] = content
85
86
  } else {
86
- bucketFiles('.', workspaceName, workspaceVersion)[rel] = content
87
+ bucketFiles('.', { name: workspaceName, version: workspaceVersion })[rel] = content
87
88
  }
88
89
  if (format != null) formats.set(rel, format)
89
90
  if (isExec) executable.add(rel)
@@ -8,7 +8,7 @@ export const NODE_FORMATS = new Set(['module', 'commonjs', 'json', 'module-types
8
8
  export const SOURCE_LANGUAGE_FORMATS = new Set(['solidity', 'php', 'shell', 'rust'])
9
9
  export const NATIVE_BUILD_FORMATS = new Set([
10
10
  'java', 'kotlin', 'gradle', 'objc', 'objcpp', 'swift', 'c', 'cpp', 'c-header', 'cpp-header',
11
- 'ruby', 'cmake', 'podspec', 'podfile', 'podfile-lock', 'template', 'xml', 'env', 'fastlane', 'pbxproj',
11
+ 'ruby', 'python', 'cmake', 'podspec', 'podfile', 'podfile-lock', 'template', 'xml', 'env', 'fastlane', 'pbxproj',
12
12
  ])
13
13
  export const RESOURCE_FORMATS = new Set(['resource', 'resource:base64'])
14
14
  export const STAT_FORMATS = new Set(['stat:file', 'stat:directory'])
@@ -50,15 +50,72 @@ export function moduleFileKey(dir, rel) {
50
50
  return dir === '.' ? rel : `${dir}/${rel}`
51
51
  }
52
52
 
53
- // A module bucket record in canonical key order; `ecosystem` is omitted (not undefined) when absent.
54
- export const moduleInfo = ({ name, version, ecosystem, files }) =>
55
- ({ name, version, ...(ecosystem === undefined ? {} : { ecosystem }), files })
56
-
57
- // A parsed bucket: an absent version has one spelling (a literal null folds into undefined so
58
- // identity comparisons and JSON round-trips can't split on it), and `files` is null-prototype.
59
- export function normalizeModule({ name, version, ecosystem, files }) {
53
+ // GitHub `owner/name` (owner 1-39, name 1-100 chars).
54
+ const GITHUB_REPO = /^(?=[A-Za-z0-9-]{1,39}\/)[A-Za-z0-9](?:-?[A-Za-z0-9])*\/(?!\.\.?$)[\w.-]{1,100}$/u
55
+ // Non-empty normalized repo-relative path of URL-safe segments.
56
+ const REPO_DIRECTORY = /^(?!\.\.?(?:\/|$))[\w.~@+-]+(?:\/(?!\.\.?(?:\/|$))[\w.~@+-]+)*$/u
57
+ // Full lowercase SHA-1 or SHA-256.
58
+ const GIT_SHA = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/u
59
+ // The fields of a `repo`, the bundle's own or a dependency's, and their checks.
60
+ export const REPO_FIELDS = {
61
+ github: (v) => typeof v === 'string' && GITHUB_REPO.test(v),
62
+ // `''` is the repository's root.
63
+ directory: (v) => typeof v === 'string' && v.length <= 1024 && (v === '' || REPO_DIRECTORY.test(v)),
64
+ commit: (v) => typeof v === 'string' && GIT_SHA.test(v),
65
+ }
66
+
67
+ // Whether two GitHub `owner/name`s are one: GitHub's names are case-insensitive.
68
+ export const sameGithub = (a, b) => a?.toLowerCase() === b?.toLowerCase()
69
+
70
+ // Whether two `repo`s name the same place: every field alike, `github` in any case.
71
+ export const sameRepo = (a, b) => Object.keys(REPO_FIELDS).every((key) => (key === 'github' ? sameGithub(a.github, b.github) : a[key] === b[key]))
72
+
73
+ // Whether two records of one dependency agree on its `repo`: an artifact from before the field records none.
74
+ export const reposAgree = (a, b) => a === undefined || b === undefined || sameRepo(a, b)
75
+
76
+ // Validate a block against its `fields` (each optional: a check, or a nested block's fields), `what`
77
+ // naming it in errors; canonical, frozen, undefined if empty. Messages are built on failure alone: a
78
+ // dependency's `repo` is checked on every parse and write.
79
+ export const normalizeBlock = (block, fields, what) => {
80
+ if (block === undefined) return undefined
81
+ if (!isPlainObject(block)) assert(false, `${what} must be an object`)
82
+ for (const key of Object.keys(block)) if (!Object.hasOwn(fields, key)) assert(false, `unknown ${what} key '${key}'`)
83
+ const entries = Object.entries(fields).map(([key, check]) => {
84
+ if (typeof check === 'object') return [key, normalizeBlock(block[key], check, `${what}.${key}`)]
85
+ if (block[key] !== undefined && !check(block[key])) assert(false, `invalid ${what}.${key}: ${JSON.stringify(block[key])}`)
86
+ return [key, block[key]]
87
+ }).filter(([, value]) => value !== undefined)
88
+ return entries.length === 0 ? undefined : Object.freeze(fromEntries(entries))
89
+ }
90
+
91
+ // Validate a `repo` (all fields optional); canonical key order, undefined if empty.
92
+ export const normalizeRepo = (repo, what = 'bundle repo') => normalizeBlock(repo, REPO_FIELDS, what)
93
+
94
+ // A bucket's ecosystem as a dependency's: its `ecosystem` tag, or npm for an untagged one under
95
+ // node_modules (an artifact from before the tag); undefined for first-party code.
96
+ export const dependencyEcosystem = (dir, ecosystem) => ecosystem ?? (hasNodeModulesSegment(dir) ? 'npm' : undefined)
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.
103
+ const normalizeModuleRepo = (dir, { ecosystem, repo }, what) => {
104
+ if (repo === undefined) return undefined
105
+ 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`)
107
+ }
108
+
109
+ // A module bucket record in canonical key order; `ecosystem` and `repo` are omitted (not undefined) when absent.
110
+ export const moduleInfo = ({ name, version, ecosystem, repo, files }) =>
111
+ ({ name, version, ...(ecosystem === undefined ? {} : { ecosystem }), ...(repo === undefined ? {} : { repo }), files })
112
+
113
+ // A parsed bucket `dir` of a `what` artifact: an absent version has one spelling (a literal null folds
114
+ // into undefined so identity comparisons and JSON round-trips can't split on it), `repo` is validated,
115
+ // and `files` is null-prototype.
116
+ export function normalizeModule({ name, version, ecosystem, repo, files }, dir, what) {
60
117
  assert(ecosystem === undefined || typeof ecosystem === 'string')
61
- return moduleInfo({ name, version: version ?? undefined, ecosystem, files: fromEntries(Object.entries(files)) })
118
+ return moduleInfo({ name, version: version ?? undefined, ecosystem, repo: normalizeModuleRepo(dir, { ecosystem, repo }, what), files: fromEntries(Object.entries(files)) })
62
119
  }
63
120
 
64
121
  // The keys a module map records -- the set an artifact's `executable` must be a subset of. `scope` MUST
@@ -298,16 +355,16 @@ export function serializeImports(imports) {
298
355
  return fileMapToObject(imports)
299
356
  }
300
357
 
301
- // A module map as the serialized `modules` (node_modules buckets) and `sources` (the rest) objects,
302
- // buckets and files path-sorted so the bytes are canonical.
303
- export function groupModules(modules, { skipEmpty = false } = {}) {
358
+ // 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 } = {}) {
304
361
  const grouped = { modules: [], sources: [] }
305
362
  for (const [dir, info] of modules) {
306
363
  if (skipEmpty && Object.keys(info.files).length === 0) continue
307
364
  const inNodeModules = hasNodeModulesSegment(dir)
308
365
  if (inNodeModules) assert(info.name && info.version && info.files)
309
366
  const files = fromEntries(Object.entries(info.files).toSorted(byPath))
310
- grouped[inNodeModules ? 'modules' : 'sources'].push([dir, moduleInfo({ ...info, files })])
367
+ grouped[inNodeModules ? 'modules' : 'sources'].push([dir, moduleInfo({ ...info, repo: normalizeModuleRepo(dir, info, what), files })])
311
368
  }
312
369
  return { modules: fromEntries(grouped.modules.toSorted(byPath)), sources: fromEntries(grouped.sources.toSorted(byPath)) }
313
370
  }
@@ -436,6 +493,9 @@ export function mergeModuleMaps(a, b, label) {
436
493
  `package without one; regenerate it (bundle=replace / lock=replace)`))
437
494
  assert(existing.ecosystem === info.ecosystem,
438
495
  `${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)})`)
498
+ if (existing.repo === undefined && info.repo !== undefined) out.set(dir, moduleInfo({ ...existing, repo: info.repo }))
439
499
  for (const [rel, value] of Object.entries(info.files)) {
440
500
  if (Object.hasOwn(existing.files, rel)) {
441
501
  assert(existing.files[rel] === value, `${label}: content mismatch for '${moduleFileKey(dir, rel)}'`)
@@ -24,14 +24,17 @@ export function packageType(file, host = diskHost) {
24
24
  // Nearest package.json (walking up) that identifies a bucket; pkgDir is relative to baseDir ("."
25
25
  // at the root). Inside node_modules both name and version are required; a workspace package
26
26
  // outside node_modules may omit version (the name alone claims the bucket, matching
27
- // State#locateModule). Null if none. A malformed one is walked past, or with `strict` throws
27
+ // State#locateModule), and a node_modules one's `ecosystem` (npm) and `repo`, where its `repository`
28
+ // names a GitHub one (packageRepo). Null if none. A malformed one is walked past, or with `strict` throws
28
29
  // (its files would otherwise land in the parent package); `check`, `host`: see readPackageJson.
29
30
  export function findPackageMetadata(baseDir, fileRelPath, { strict = false, check, host = diskHost } = {}) {
30
31
  for (let dir = dirname(fileRelPath); ; dir = dirname(dir)) {
31
32
  const pkg = readPackageJson(baseDir, toPosix(join(dir, 'package.json')), { strict, check, host })
32
- if (pkg?.name && (pkg.version || !hasNodeModulesSegment(toPosix(dir)))) {
33
+ const inNodeModules = hasNodeModulesSegment(toPosix(dir))
34
+ if (pkg?.name && (pkg.version || !inNodeModules)) {
35
+ const repo = inNodeModules ? packageRepo(pkg) : undefined
33
36
  // `?? undefined` folds a literal `"version": null` into the one absent-version spelling.
34
- return { pkgDir: dir, name: pkg.name, version: pkg.version ?? undefined }
37
+ return { pkgDir: dir, name: pkg.name, version: pkg.version ?? undefined, ...(inNodeModules ? { ecosystem: 'npm' } : {}), ...(repo === undefined ? {} : { repo }) }
35
38
  }
36
39
  if (dir === '.' || dir === '/' || dir === '' || dirname(dir) === dir) return null
37
40
  }
@@ -152,10 +155,14 @@ export function readJson(file, host = diskHost) {
152
155
  }
153
156
  }
154
157
 
155
- // `owner/name` from a package.json `repository` (GitHub URL or shorthand), else null.
158
+ // `owner/name` from a package.json `repository` (GitHub URL or shorthand), else null. A `#committish`
159
+ // npm lets it name is no part of the repo; a URL's userinfo holds only what RFC 3986 allows there, so
160
+ // a `?` or `\` ending the authority early (`https://evil.example?@github.com/a/b`) isn't GitHub's. An
161
+ // scp-like `git@github.com:owner/name` is taken behind an `ssh://` too (`git+ssh://git@github.com:o/n`),
162
+ // as package.json files spell it.
156
163
  export function parseGithubRepository(url) {
157
164
  if (typeof url !== 'string') return null
158
- const match = /^(?:github:|(?:git\+)?(?:(?:https?|ssh|git):\/\/(?:[^@/]+@)?github\.com(?::\d+)?\/|(?:[^@/:]+@)?github\.com:))?([\w.-]+)\/([\w.-]+?)(?:\.git)?\/?$/iu.exec(url.trim())
165
+ const match = /^(?:github:|(?:git\+)?(?:(?:https?|ssh|git):\/\/(?:[\w.~%!$&'()*+,;=:-]*@)?github\.com(?::\d+)?\/|ssh:\/\/(?:[\w.~%!$&'()*+,;=-]*@)?github\.com:|(?:[^@/:]+@)?github\.com:))?([\w.-]+)\/([\w.-]+?)(?:\.git)?\/?$/iu.exec(url.trim().replace(/#.*$/su, ''))
159
166
  // Must also pass the bundle format's `github` check.
160
167
  const github = match && `${match[1]}/${match[2]}`
161
168
  return github && isValidRepoField('github', github) ? github : null
@@ -192,13 +199,17 @@ export function githubHomepageDirectory(homepage, github) {
192
199
  }
193
200
  }
194
201
 
195
- // `base` joined with `rel` as `{ root: true }`, `{ directory }`, or `{}` if not a valid `directory`.
196
- // `base` is a package.json's `repository.directory`, which Windows users write with `\`: metadata, not
197
- // a name on disk, so its `\` is taken for a separator.
198
- const repoLocation = (base, rel) => {
199
- const directory = posix.join(typeof base === 'string' ? base.replaceAll('\\', '/') : '', rel).replace(/^\/+|\/+$/gu, '')
200
- if (directory === '' || directory === '.') return { root: true } // `./`, `a/../`, trailing slashes
201
- return isValidRepoField('directory', directory) ? { directory } : {}
202
+ // `{ directory }` where it is a valid one, else `{}`.
203
+ const repoLocation = (directory) => (isValidRepoField('directory', directory) ? { directory } : {})
204
+
205
+ // 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
+ const declaredLocation = (base, rel) => {
210
+ if (typeof base !== 'string') return {}
211
+ const directory = posix.join(base.replaceAll('\\', '/'), rel).replace(/^\/+|\/+$/gu, '')
212
+ return directory === '' || directory === '.' ? {} : repoLocation(directory) // `/`, `./`, `a/../`
202
213
  }
203
214
 
204
215
  // Git and common dirs, following a worktree/submodule `.git` file.
@@ -223,27 +234,40 @@ function gitHeadCommit({ gitDir, commonDir }, host) {
223
234
  return isValidRepoField('commit', commit) ? commit : undefined
224
235
  }
225
236
 
226
- // Bundle `repo` for `dir`: git origin/HEAD at the work tree root, else nearest package.json `repository`.
237
+ // A parsed package.json's `repository` url (the string shorthand, or its `url`), or undefined.
238
+ function repositoryUrl(json) {
239
+ const repository = json?.repository
240
+ const url = typeof repository === 'string' ? repository : repository?.url
241
+ return typeof url === 'string' ? url : undefined
242
+ }
243
+
244
+ // The `repo` a parsed package.json's `repository` names for `rel`, a directory below it ('' for its
245
+ // own): `github` from its GitHub URL or shorthand, and `directory` from `repository.directory` joined
246
+ // with `rel` (declaredLocation); undefined where it names no GitHub repository.
247
+ export function packageRepo(json, rel = '') {
248
+ const github = parseGithubRepository(repositoryUrl(json))
249
+ 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)
252
+ return { github, ...declaredLocation(base, rel) }
253
+ }
254
+
255
+ // Bundle `repo` for `dir`: git origin/HEAD at the work tree root, with `dir`'s place in it (`''` at
256
+ // the root), else nearest package.json `repository` (packageRepo).
227
257
  export function detectRepo(dir, host = diskHost) {
228
258
  const start = resolve(dir)
229
259
  let pkg = null // null: no package.json `repository` seen yet; undefined: one seen, not GitHub
230
260
  for (let cursor = start; ; cursor = dirname(cursor)) {
231
261
  const rel = toPosix(relative(cursor, start))
232
262
  if (pkg === null) {
233
- const { repository, homepage } = readJson(join(cursor, 'package.json'), host) ?? {}
234
- const url = typeof repository === 'string' ? repository : repository?.url
235
- if (typeof url === 'string') {
236
- const github = parseGithubRepository(url)
237
- // Often unset; fall back to a GitHub tree `homepage`.
238
- const base = typeof repository.directory === 'string' ? repository.directory : githubHomepageDirectory(homepage, github ?? '')
239
- pkg = github ? { github, ...repoLocation(base, rel) } : undefined
240
- }
263
+ const json = readJson(join(cursor, 'package.json'), host)
264
+ if (repositoryUrl(json) !== undefined) pkg = packageRepo(json, rel)
241
265
  }
242
266
  const dotGit = join(cursor, '.git')
243
267
  if (host.stat(dotGit) !== null) {
244
268
  const dirs = gitDirs(dotGit, host)
245
269
  const github = parseGithubRepository(gitOriginUrl(readText(host, join(dirs.commonDir, 'config'))))
246
- if (github) return stripUndefined({ github, ...repoLocation('', rel), commit: gitHeadCommit(dirs, host) })
270
+ if (github) return stripUndefined({ github, ...repoLocation(rel), commit: gitHeadCommit(dirs, host) })
247
271
  return pkg ?? undefined
248
272
  }
249
273
  if (dirname(cursor) === cursor) return pkg ?? undefined
package/src/bundle.js CHANGED
@@ -14,7 +14,9 @@ import {
14
14
  mergeExecutableSets,
15
15
  mergeModuleMaps,
16
16
  moduleFileKey,
17
+ normalizeBlock,
17
18
  normalizeModule,
19
+ normalizeRepo,
18
20
  parseEntries,
19
21
  parseExecutable,
20
22
  parseFormats,
@@ -24,6 +26,8 @@ import {
24
26
  serializeFormats,
25
27
  serializeImports,
26
28
  posixPathEscapes,
29
+ REPO_FIELDS,
30
+ sameGithub,
27
31
  splitNodeModulesPath,
28
32
  } from './artifact-util.js'
29
33
 
@@ -69,47 +73,22 @@ const mergeReason = (a, b) => {
69
73
  return fromEntries([...merged.keys()].toSorted().map((c) => [c, fileSetToObject(merged.get(c))]))
70
74
  }
71
75
 
72
- // GitHub `owner/name` (owner 1-39, name 1-100 chars).
73
- const GITHUB_REPO = /^(?=[A-Za-z0-9-]{1,39}\/)[A-Za-z0-9](?:-?[A-Za-z0-9])*\/(?!\.\.?$)[\w.-]{1,100}$/u
74
- // Non-empty normalized repo-relative path of URL-safe segments (the repo root is `root: true`).
75
- const REPO_DIRECTORY = /^(?!\.\.?(?:\/|$))[\w.~@+-]+(?:\/(?!\.\.?(?:\/|$))[\w.~@+-]+)*$/u
76
- // Full lowercase SHA-1 or SHA-256.
77
- const GIT_SHA = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/u
78
- const REPO_FIELDS = {
79
- github: (v) => typeof v === 'string' && GITHUB_REPO.test(v),
80
- directory: (v) => typeof v === 'string' && v.length <= 1024 && REPO_DIRECTORY.test(v),
81
- root: (v) => v === true,
82
- commit: (v) => typeof v === 'string' && GIT_SHA.test(v),
83
- }
84
-
85
76
  // Validate one `repo` field without throwing.
86
77
  export const isValidRepoField = (key, value) => Object.hasOwn(REPO_FIELDS, key) && REPO_FIELDS[key](value)
87
78
 
88
79
  // `repo` of a bundle plus one added to it: only agreeing fields survive, and none if `github` differs.
89
80
  export const mergeRepo = (a, b) => {
90
- if (a?.github === undefined || a.github.toLowerCase() !== b?.github?.toLowerCase()) return undefined // GitHub names are case-insensitive
81
+ if (a?.github === undefined || !sameGithub(a.github, b?.github)) return undefined
91
82
  const kept = Object.keys(REPO_FIELDS).filter((key) => a[key] !== undefined && (key === 'github' || a[key] === b[key]))
92
83
  return fromEntries(kept.map((key) => [key, a[key]]))
93
84
  }
94
85
 
95
- // Validate a block against its `fields` (each optional: a check, or a nested block's fields); canonical, frozen, undefined if empty.
96
- const normalizeBlock = (block, fields, what) => {
97
- if (block === undefined) return undefined
98
- assert(isPlainObject(block), `bundle ${what} must be an object`)
99
- for (const key of Object.keys(block)) assert(Object.hasOwn(fields, key), `unknown bundle ${what} key '${key}'`)
100
- const entries = Object.entries(fields).map(([key, check]) => {
101
- if (typeof check === 'object') return [key, normalizeBlock(block[key], check, `${what}.${key}`)]
102
- assert(block[key] === undefined || check(block[key]), `invalid bundle ${what}.${key}: ${JSON.stringify(block[key])}`)
103
- return [key, block[key]]
104
- }).filter(([, value]) => value !== undefined)
105
- return entries.length === 0 ? undefined : Object.freeze(fromEntries(entries))
106
- }
107
-
108
- // Validate `repo` (all fields optional); canonical key order, undefined if empty.
109
- const normalizeRepo = (repo) => {
110
- const normalized = normalizeBlock(repo, REPO_FIELDS, 'repo')
111
- assert(normalized?.directory === undefined || normalized.root === undefined, 'bundle repo has both directory and root')
112
- return normalized
86
+ // A parsed header `repo`, an older stasis's `root: true` read as unknown: it recorded one from a
87
+ // package.json's claim as well as from a git work tree.
88
+ const parsedRepo = (repo) => {
89
+ if (!isPlainObject(repo) || repo.root !== true || Object.hasOwn(repo, 'directory')) return repo
90
+ const { root: _root, ...rest } = repo
91
+ return rest
113
92
  }
114
93
 
115
94
  // A package name or version: characters some ecosystem uses there (npm's legacy `~'!()*` too), so not space or `"#$%&,:;<=>?[\]^`{|}`.
@@ -152,7 +131,7 @@ export class Bundle {
152
131
  return this.#package
153
132
  }
154
133
  set package(pkg) {
155
- this.#package = normalizeBlock(pkg, PACKAGE_FIELDS, 'package')
134
+ this.#package = normalizeBlock(pkg, PACKAGE_FIELDS, 'bundle package')
156
135
  }
157
136
 
158
137
  constructor({ config = { scope: 'full' }, entries, modules, formats, imports, executable, reason, repo, package: pkg, version = VERSION } = {}) {
@@ -231,7 +210,7 @@ export class Bundle {
231
210
  assert(hasNodeModulesSegment(dir))
232
211
  assertArtifactPath(dir, 'bundle')
233
212
  assert(info?.name && info.version && info.files)
234
- modules.set(dir, normalizeModule(info))
213
+ modules.set(dir, normalizeModule(info, dir, 'bundle'))
235
214
  }
236
215
  }
237
216
  if (full) {
@@ -241,7 +220,7 @@ export class Bundle {
241
220
  assertArtifactPath(dir, 'bundle')
242
221
  // A workspace bucket may omit version (a private/unpublished package.json can lack one).
243
222
  assert(info?.name && info.files)
244
- modules.set(dir, normalizeModule(info))
223
+ modules.set(dir, normalizeModule(info, dir, 'bundle'))
245
224
  }
246
225
  // Empty entries are valid (`stasis add` attests files without making them entry points); state.assertEntry fails closed on an empty set.
247
226
  if (json.entries !== undefined) entries = parseEntries(json.entries, 'bundle')
@@ -280,7 +259,7 @@ export class Bundle {
280
259
  ? parseExecutable(json.executable, { what: 'bundle', files: flatKeys, formats, scope: json.config.scope })
281
260
  : new Set(),
282
261
  reason: isPlainObject(json.reason) ? json.reason : undefined,
283
- repo: json.repo,
262
+ repo: parsedRepo(json.repo),
284
263
  package: json.package,
285
264
  })
286
265
  }
@@ -288,7 +267,7 @@ export class Bundle {
288
267
  serialize() {
289
268
  // Never write an artifact that parse would reject.
290
269
  flatFileKeys(this.modules, 'bundle', duplicateKey)
291
- const { modules, sources } = groupModules(this.modules, { skipEmpty: true })
270
+ const { modules, sources } = groupModules(this.modules, { skipEmpty: true, what: 'bundle' })
292
271
  const full = this.config.scope === 'full'
293
272
  const data = { version: VERSION, config: this.config }
294
273
  if (this.repo !== undefined) data.repo = this.repo
package/src/extract.js CHANGED
@@ -20,14 +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
28
  hashed[rel] = sha512integrity(bundle.formats.get(file) === 'resource:base64' ? assertCanonicalBase64(content, file) : content)
29
29
  }
30
- modules.set(dir, moduleInfo({ name, version, ecosystem, files: hashed }))
30
+ modules.set(dir, moduleInfo({ ...info, files: hashed }))
31
31
  }
32
32
  // Carry imports+formats across, else the derived lockfile is bytes-only and a later frozen run skips those cross-checks.
33
33
  return new Lockfile({
package/src/lockfile.js CHANGED
@@ -43,7 +43,7 @@ export class Lockfile {
43
43
  for (const [dir, info] of Object.entries(json.modules)) {
44
44
  assert(hasNodeModulesSegment(dir))
45
45
  assert(info?.name && info.version && info.files)
46
- modules.set(dir, normalizeModule(info))
46
+ modules.set(dir, normalizeModule(info, dir, 'lockfile'))
47
47
  }
48
48
 
49
49
  let entries = new Set()
@@ -53,7 +53,7 @@ export class Lockfile {
53
53
  assert(!hasNodeModulesSegment(dir))
54
54
  // A workspace bucket may omit version (a private/unpublished package.json can lack one).
55
55
  assert(info?.name && info.files)
56
- modules.set(dir, normalizeModule(info))
56
+ modules.set(dir, normalizeModule(info, dir, 'lockfile'))
57
57
  }
58
58
  }
59
59
 
@@ -78,7 +78,7 @@ export class Lockfile {
78
78
  serialize() {
79
79
  // Never write an artifact that parse would reject.
80
80
  flatFileKeys(this.modules, 'lockfile', duplicateKey)
81
- const { modules, sources } = groupModules(this.modules)
81
+ const { modules, sources } = groupModules(this.modules, { what: 'lockfile' })
82
82
  const store = { version: this.version, config: this.config }
83
83
  if (this.config.scope === 'full') Object.assign(store, { entries: serializeEntries(this.entries, 'lockfile'), sources })
84
84
  store.modules = modules
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, canObserveExecuteBits, classifyFormat, erasedTypeScriptFormat, fileMapToObject, hasNodeModulesSegment, isBinaryPlist, isNativeArtifact, isPathWithin, isStatFormat, moduleFileKey, moduleInfo, narrowExecutable, nestedMap, objectToMaps, observeExecutable, pathExt, reconcileFormat, relativeEscapes, sortPaths, splitNodeModulesPath } from './util.js'
15
- import { detectRepo, packageJSONStat, packageJSONText, readModuleManifest } from './bundle-util.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'
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'
18
18
 
@@ -58,6 +58,16 @@ const EXT_FORMATS = { __proto__: null, '.json': 'json', '.mjs': 'module', '.cjs'
58
58
  // With no `type`, Node decides .js/.ts by syntax detection: either variant is possible, the first is the default.
59
59
  const UNTYPED_VARIANTS = { __proto__: null, '.js': ['commonjs', 'module'], '.ts': ['commonjs-typescript', 'module-typescript'] }
60
60
 
61
+ // Nested package.json files known to disagree with their package root's name/version because of
62
+ // upstream packaging, exempt from #locateModule's consistency check. Matched as a segment-aligned
63
+ // suffix of the project-relative path, so hoisted, nested and .pnpm-store copies all match.
64
+ // Maintained list: add only a confirmed upstream false positive, with its reason.
65
+ const INCONSISTENT_PACKAGE_JSON_EXCEPTIONS = [
66
+ 'node_modules/@redis/client/dist/package.json', // stale build-time copy
67
+ ]
68
+ const isInconsistentPackageJsonException = (file) =>
69
+ INCONSISTENT_PACKAGE_JSON_EXCEPTIONS.some((suffix) => file === suffix || file.endsWith(`/${suffix}`))
70
+
61
71
  // On globalThis via `Symbol.for` so the registry is shared across duplicate stasis-core copies.
62
72
  const STATES_KEY = Symbol.for('@exodus/stasis-core/states')
63
73
  const liveStates = () => (globalThis[STATES_KEY] ??= new Set())
@@ -439,6 +449,10 @@ export class State {
439
449
  // a real workspace identity (or a stripped field), never v0 partial metadata -- a
440
450
  // version-less bundle bucket must not dodge the lockfile consistency check.
441
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.
455
+ if (lockModule.repo === undefined && info.repo !== undefined) this.modules.set(dir, moduleInfo({ ...lockModule, repo: info.repo }))
442
456
  for (const rel of Object.keys(info.files)) {
443
457
  assert.ok(Object.hasOwn(lockModule.files, rel), `bundle file ${dir}/${rel} missing in lockfile`)
444
458
  }
@@ -486,6 +500,8 @@ export class State {
486
500
  // A dir may be added twice (code + resource entries), and both must agree.
487
501
  assert.equal(info.name, existing.name, `bundle ${dir} name mismatch`)
488
502
  assert.equal(info.version, existing.version, `bundle ${dir} version mismatch`)
503
+ assert.ok(reposAgree(info.repo, existing.repo), `bundle ${dir} repo mismatch`)
504
+ if (existing.repo === undefined && info.repo !== undefined) this.modules.set(dir, moduleInfo({ ...existing, repo: info.repo }))
489
505
  }
490
506
  }
491
507
  }
@@ -723,14 +739,15 @@ export class State {
723
739
 
724
740
  // findPackageJSON may land on a `{"type":"module"}` sub-bucket marker lacking name/version.
725
741
  const nmRoot = splitNodeModulesPath(file)?.dir
726
- let pkgAbsolute, name, version
742
+ let pkgAbsolute, name, version, repo
727
743
  if (nmRoot) {
728
744
  pkgAbsolute = resolve(this.root, nmRoot, 'package.json')
729
745
  const rootPkg = pkgAbsolute === closestPkgAbsolute ? closestPkg : readPackageJSON(this.#host, pkgAbsolute)
730
746
  ;({ name, version } = rootPkg)
747
+ repo = packageRepo(rootPkg)
731
748
  assert.ok(name, `Missing name in ${this.relative(pkgAbsolute)}`)
732
749
  assert.ok(version, `Missing version in ${this.relative(pkgAbsolute)}`)
733
- if (closestPkgAbsolute !== pkgAbsolute) {
750
+ if (closestPkgAbsolute !== pkgAbsolute && !isInconsistentPackageJsonException(this.relative(closestPkgAbsolute))) {
734
751
  const message = `Inconsistent data between ${this.relative(closestPkgAbsolute)} and ${this.relative(pkgAbsolute)}`
735
752
  // Allow fake module-name subpaths: the real module owns the prefix (npm wouldn't publish this).
736
753
  if (closestPkg.name !== undefined && closestPkg.name !== name) assert.ok(closestPkg.name.startsWith(`${name}/`), message)
@@ -760,7 +777,7 @@ export class State {
760
777
  if (nmRoot) assert.equal(dir, nmRoot)
761
778
  if (!this.modules.has(dir)) {
762
779
  // Tag node_modules buckets `npm`; workspace/top-level buckets carry no ecosystem.
763
- this.modules.set(dir, moduleInfo({ name, version, ecosystem: nmRoot ? 'npm' : undefined, files: Object.create(null) }))
780
+ this.modules.set(dir, moduleInfo({ name, version, ecosystem: nmRoot ? 'npm' : undefined, repo, files: Object.create(null) }))
764
781
  }
765
782
  const module = this.modules.get(dir)
766
783
  if (module.name !== name || module.version !== version) {
@@ -771,6 +788,14 @@ export class State {
771
788
  assert.fail(`module identity mismatch for '${dir}': artifact records ` +
772
789
  `'${module.name}@${module.version ?? '(none)'}', package.json has '${name}@${version ?? '(none)'}'${hint}`)
773
790
  }
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
+ }
774
799
 
775
800
  return { absolute, file, dir, module, closestType }
776
801
  }
package/src/util.js CHANGED
@@ -152,6 +152,7 @@ const CODE_EXT_FORMATS = new Map([
152
152
  ['h', 'c-header'],
153
153
  ['hh', 'cpp-header'], ['hxx', 'cpp-header'], ['hpp', 'cpp-header'], ['h++', 'cpp-header'],
154
154
  ['rb', 'ruby'],
155
+ ['py', 'python'], ['pyi', 'python'], ['pyw', 'python'],
155
156
  ['cmake', 'cmake'],
156
157
  ['podspec', 'podspec'],
157
158
  ['template', 'template'],