@exodus/stasis-core 1.0.0-beta.5 → 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.5",
3
+ "version": "1.0.0-beta.7",
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,69 @@ 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`, the one its own manifest names, with the bundle's own fields; first-party
99
+ // code carries none.
100
+ const normalizeModuleRepo = (dir, { ecosystem, repo }, what) => {
101
+ if (repo === undefined) return undefined
102
+ if (dependencyEcosystem(dir, ecosystem) === undefined) assert(false, `${what}: '${dir}' is no dependency's bucket, and carries no repo`)
103
+ return normalizeRepo(repo, `${what} module '${dir}' repo`)
104
+ }
105
+
106
+ // A module bucket record in canonical key order; `ecosystem` and `repo` are omitted (not undefined) when absent.
107
+ export const moduleInfo = ({ name, version, ecosystem, repo, files }) =>
108
+ ({ name, version, ...(ecosystem === undefined ? {} : { ecosystem }), ...(repo === undefined ? {} : { repo }), files })
109
+
110
+ // A parsed bucket `dir` of a `what` artifact: an absent version has one spelling (a literal null folds
111
+ // into undefined so identity comparisons and JSON round-trips can't split on it), `repo` is validated,
112
+ // and `files` is null-prototype.
113
+ export function normalizeModule({ name, version, ecosystem, repo, files }, dir, what) {
60
114
  assert(ecosystem === undefined || typeof ecosystem === 'string')
61
- return moduleInfo({ name, version: version ?? undefined, ecosystem, files: fromEntries(Object.entries(files)) })
115
+ return moduleInfo({ name, version: version ?? undefined, ecosystem, repo: normalizeModuleRepo(dir, { ecosystem, repo }, what), files: fromEntries(Object.entries(files)) })
62
116
  }
63
117
 
64
118
  // The keys a module map records -- the set an artifact's `executable` must be a subset of. `scope` MUST
@@ -298,16 +352,17 @@ export function serializeImports(imports) {
298
352
  return fileMapToObject(imports)
299
353
  }
300
354
 
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 } = {}) {
355
+ // A module map as the serialized `modules` (node_modules buckets) and `sources` (the rest) objects
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 } = {}) {
304
359
  const grouped = { modules: [], sources: [] }
305
360
  for (const [dir, info] of modules) {
306
361
  if (skipEmpty && Object.keys(info.files).length === 0) continue
307
362
  const inNodeModules = hasNodeModulesSegment(dir)
308
363
  if (inNodeModules) assert(info.name && info.version && info.files)
309
364
  const files = fromEntries(Object.entries(info.files).toSorted(byPath))
310
- grouped[inNodeModules ? 'modules' : 'sources'].push([dir, moduleInfo({ ...info, files })])
365
+ grouped[inNodeModules ? 'modules' : 'sources'].push([dir, moduleInfo({ ...info, repo: repo ? normalizeModuleRepo(dir, info, what) : undefined, files })])
311
366
  }
312
367
  return { modules: fromEntries(grouped.modules.toSorted(byPath)), sources: fromEntries(grouped.sources.toSorted(byPath)) }
313
368
  }
@@ -436,6 +491,8 @@ export function mergeModuleMaps(a, b, label) {
436
491
  `package without one; regenerate it (bundle=replace / lock=replace)`))
437
492
  assert(existing.ecosystem === info.ecosystem,
438
493
  `${label}: module '${dir}' ecosystem mismatch ('${existing.ecosystem ?? '(none)'}' vs '${info.ecosystem ?? '(none)'}')`)
494
+ // `repo` is metadata, held to nothing: either side's, where only one records it, else `a`'s.
495
+ if (existing.repo === undefined && info.repo !== undefined) out.set(dir, moduleInfo({ ...existing, repo: info.repo }))
439
496
  for (const [rel, value] of Object.entries(info.files)) {
440
497
  if (Object.hasOwn(existing.files, rel)) {
441
498
  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,23 @@ 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
+ // 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
+
208
+ // A package.json's declared `base` (its `repository.directory`) joined with `rel` as a `repoLocation`;
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.
213
+ const declaredLocation = (base, rel) => {
214
+ if (typeof base !== 'string') return {}
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)
202
219
  }
203
220
 
204
221
  // Git and common dirs, following a worktree/submodule `.git` file.
@@ -223,27 +240,45 @@ function gitHeadCommit({ gitDir, commonDir }, host) {
223
240
  return isValidRepoField('commit', commit) ? commit : undefined
224
241
  }
225
242
 
226
- // Bundle `repo` for `dir`: git origin/HEAD at the work tree root, else nearest package.json `repository`.
243
+ // A parsed package.json's `repository` url (the string shorthand, or its `url`), or undefined.
244
+ function repositoryUrl(json) {
245
+ const repository = json?.repository
246
+ const url = typeof repository === 'string' ? repository : repository?.url
247
+ return typeof url === 'string' ? url : undefined
248
+ }
249
+
250
+ // The `repo` a parsed package.json's `repository` names for `rel`, a directory below it ('' for its
251
+ // own): `github` from its GitHub URL or shorthand, and `directory` from `repository.directory` joined
252
+ // with `rel` (declaredLocation); undefined where it names no GitHub repository.
253
+ export function packageRepo(json, rel = '') {
254
+ const github = parseGithubRepository(repositoryUrl(json))
255
+ if (!github) return undefined
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
+ }
263
+ return { github, ...declaredLocation(base, rel) }
264
+ }
265
+
266
+ // Bundle `repo` for `dir`: git origin/HEAD at the work tree root, with `dir`'s place in it (`''` at
267
+ // the root), else nearest package.json `repository` (packageRepo).
227
268
  export function detectRepo(dir, host = diskHost) {
228
269
  const start = resolve(dir)
229
270
  let pkg = null // null: no package.json `repository` seen yet; undefined: one seen, not GitHub
230
271
  for (let cursor = start; ; cursor = dirname(cursor)) {
231
272
  const rel = toPosix(relative(cursor, start))
232
273
  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
- }
274
+ const json = readJson(join(cursor, 'package.json'), host)
275
+ if (repositoryUrl(json) !== undefined) pkg = packageRepo(json, rel)
241
276
  }
242
277
  const dotGit = join(cursor, '.git')
243
278
  if (host.stat(dotGit) !== null) {
244
279
  const dirs = gitDirs(dotGit, host)
245
280
  const github = parseGithubRepository(gitOriginUrl(readText(host, join(dirs.commonDir, 'config'))))
246
- if (github) return stripUndefined({ github, ...repoLocation('', rel), commit: gitHeadCommit(dirs, host) })
281
+ if (github) return stripUndefined({ github, ...repoLocation(rel), commit: gitHeadCommit(dirs, host) })
247
282
  return pkg ?? undefined
248
283
  }
249
284
  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
@@ -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))
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))
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)
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
@@ -12,7 +12,7 @@ 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
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'
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,8 @@ 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
+ // `repo` is metadata, which a lockfile never records: the bundle's is taken, to be written again.
453
+ if (lockModule.repo === undefined && info.repo !== undefined) this.modules.set(dir, moduleInfo({ ...lockModule, repo: info.repo }))
442
454
  for (const rel of Object.keys(info.files)) {
443
455
  assert.ok(Object.hasOwn(lockModule.files, rel), `bundle file ${dir}/${rel} missing in lockfile`)
444
456
  }
@@ -486,6 +498,8 @@ export class State {
486
498
  // A dir may be added twice (code + resource entries), and both must agree.
487
499
  assert.equal(info.name, existing.name, `bundle ${dir} name mismatch`)
488
500
  assert.equal(info.version, existing.version, `bundle ${dir} version mismatch`)
501
+ // `repo` is metadata, held to nothing: the first half's that records one.
502
+ if (existing.repo === undefined && info.repo !== undefined) this.modules.set(dir, moduleInfo({ ...existing, repo: info.repo }))
489
503
  }
490
504
  }
491
505
  }
@@ -723,14 +737,15 @@ export class State {
723
737
 
724
738
  // findPackageJSON may land on a `{"type":"module"}` sub-bucket marker lacking name/version.
725
739
  const nmRoot = splitNodeModulesPath(file)?.dir
726
- let pkgAbsolute, name, version
740
+ let pkgAbsolute, name, version, repo
727
741
  if (nmRoot) {
728
742
  pkgAbsolute = resolve(this.root, nmRoot, 'package.json')
729
743
  const rootPkg = pkgAbsolute === closestPkgAbsolute ? closestPkg : readPackageJSON(this.#host, pkgAbsolute)
730
744
  ;({ name, version } = rootPkg)
745
+ repo = packageRepo(rootPkg)
731
746
  assert.ok(name, `Missing name in ${this.relative(pkgAbsolute)}`)
732
747
  assert.ok(version, `Missing version in ${this.relative(pkgAbsolute)}`)
733
- if (closestPkgAbsolute !== pkgAbsolute) {
748
+ if (closestPkgAbsolute !== pkgAbsolute && !isInconsistentPackageJsonException(this.relative(closestPkgAbsolute))) {
734
749
  const message = `Inconsistent data between ${this.relative(closestPkgAbsolute)} and ${this.relative(pkgAbsolute)}`
735
750
  // Allow fake module-name subpaths: the real module owns the prefix (npm wouldn't publish this).
736
751
  if (closestPkg.name !== undefined && closestPkg.name !== name) assert.ok(closestPkg.name.startsWith(`${name}/`), message)
@@ -760,7 +775,7 @@ export class State {
760
775
  if (nmRoot) assert.equal(dir, nmRoot)
761
776
  if (!this.modules.has(dir)) {
762
777
  // 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) }))
778
+ this.modules.set(dir, moduleInfo({ name, version, ecosystem: nmRoot ? 'npm' : undefined, repo, files: Object.create(null) }))
764
779
  }
765
780
  const module = this.modules.get(dir)
766
781
  if (module.name !== name || module.version !== version) {
@@ -771,6 +786,8 @@ export class State {
771
786
  assert.fail(`module identity mismatch for '${dir}': artifact records ` +
772
787
  `'${module.name}@${module.version ?? '(none)'}', package.json has '${name}@${version ?? '(none)'}'${hint}`)
773
788
  }
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 }))
774
791
 
775
792
  return { absolute, file, dir, module, closestType }
776
793
  }
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'],