@exodus/stasis-core 1.0.0-beta.15 → 1.0.0-beta.17
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 +3 -1
- package/src/add.js +109 -194
- package/src/artifact-util.js +234 -291
- package/src/brotli.js +3 -9
- package/src/bundle-util.js +66 -162
- package/src/bundle.js +57 -130
- package/src/cli.js +15 -45
- package/src/config.js +58 -102
- package/src/extract.js +40 -74
- package/src/fs.js +54 -89
- package/src/hooks.js +118 -235
- package/src/host.js +1 -4
- package/src/loader.js +1 -1
- package/src/lockfile.js +48 -80
- package/src/policy.js +222 -0
- package/src/prune.js +60 -114
- package/src/shard.js +10 -25
- package/src/state-util.js +11 -40
- package/src/state.js +498 -830
- package/src/util.js +97 -149
package/src/brotli.js
CHANGED
|
@@ -7,9 +7,7 @@ import { assert, isBrotliQuality, parseBrotliQuality } from './util.js'
|
|
|
7
7
|
export const DEFAULT_BROTLI_QUALITY = 6
|
|
8
8
|
|
|
9
9
|
export function brotliOptions(quality) {
|
|
10
|
-
|
|
11
|
-
assert(isBrotliQuality(quality), `brotli quality must be an integer 0..11, got ${quality}`)
|
|
12
|
-
}
|
|
10
|
+
assert(quality === undefined || isBrotliQuality(quality), `brotli quality must be an integer 0..11, got ${quality}`)
|
|
13
11
|
const raw = process.env.EXODUS_STASIS_BROTLI_QUALITY
|
|
14
12
|
if (raw) {
|
|
15
13
|
const n = parseBrotliQuality('EXODUS_STASIS_BROTLI_QUALITY', raw)
|
|
@@ -20,21 +18,17 @@ export function brotliOptions(quality) {
|
|
|
20
18
|
return { params: { [zlibConstants.BROTLI_PARAM_QUALITY]: quality ?? DEFAULT_BROTLI_QUALITY } }
|
|
21
19
|
}
|
|
22
20
|
|
|
23
|
-
// zlib's brotli decoder
|
|
24
|
-
// DecompressionStream refuses bytes after a gzip or deflate stream; this is Node's error for those.
|
|
21
|
+
// zlib's brotli decoder ignores bytes after the stream, where DecompressionStream refuses them with this error.
|
|
25
22
|
export const trailingJunkError = () => Object.assign(
|
|
26
23
|
new TypeError('Trailing junk found after the end of the compressed stream'),
|
|
27
24
|
{ code: 'ERR_TRAILING_JUNK_AFTER_STREAM_END' })
|
|
28
25
|
|
|
29
|
-
//
|
|
30
|
-
// bytesWritten counts the input it consumed, which stops where the stream does.
|
|
26
|
+
// The engine's bytesWritten counts the input it consumed, which stops where the stream does.
|
|
31
27
|
export function brotliDecompressStrict(bytes) {
|
|
32
28
|
const { buffer, engine } = brotliDecompressSync(bytes, { info: true })
|
|
33
29
|
if (engine.bytesWritten !== Buffer.byteLength(bytes)) throw trailingJunkError()
|
|
34
30
|
return buffer
|
|
35
31
|
}
|
|
36
32
|
|
|
37
|
-
// A bundle file's bytes, of a serialized bundle's text (brotliOptions' `quality`), and the Bundle of
|
|
38
|
-
// those bytes, which are one brotli stream and nothing after it.
|
|
39
33
|
export const compressBundleText = (text, quality) => brotliCompressSync(text, brotliOptions(quality))
|
|
40
34
|
export const parseBundleBytes = (bytes) => Bundle.parse(brotliDecompressStrict(bytes).toString('utf8'))
|
package/src/bundle-util.js
CHANGED
|
@@ -4,49 +4,36 @@ import { dirname, join, posix, relative, resolve } from 'node:path'
|
|
|
4
4
|
import { isValidRepoField } from './bundle.js'
|
|
5
5
|
import { isPackageString, isSubpackage, posixPathEscapes } from './artifact-util.js'
|
|
6
6
|
import { diskHost } from './host.js'
|
|
7
|
-
import { assertRealPathWithinBase, hasNodeModulesSegment, relativeEscapes, splitNodeModulesPath, toPosix } from './util.js'
|
|
7
|
+
import { assertRealPathWithinBase, hasNodeModulesSegment, memoized, relativeEscapes, splitNodeModulesPath, toPosix } from './util.js'
|
|
8
8
|
|
|
9
|
-
//
|
|
9
|
+
// As Node reads a package.json: UTF-8, past a byte order mark.
|
|
10
10
|
const utf8 = new TextDecoder()
|
|
11
11
|
export const packageJSONText = (bytes) => utf8.decode(bytes)
|
|
12
12
|
|
|
13
|
-
//
|
|
14
|
-
// package.json's path where one is given (a scan's, which reads the same one for each file under it).
|
|
13
|
+
// `kept(path, read)` caches by the package.json's path (a scan reads the same one for each file under it).
|
|
15
14
|
export function packageType(file, host = diskHost, kept = (path, read) => read()) {
|
|
16
15
|
const pkg = host.findPackageJSON(file)
|
|
17
16
|
if (!pkg) return null
|
|
18
17
|
return kept(pkg, () => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return type === 'module' || type === 'commonjs' ? type : null
|
|
22
|
-
} catch {
|
|
23
|
-
return null
|
|
24
|
-
}
|
|
18
|
+
const type = readJson(pkg, host)?.type
|
|
19
|
+
return type === 'module' || type === 'commonjs' ? type : null
|
|
25
20
|
})
|
|
26
21
|
}
|
|
27
22
|
|
|
28
|
-
//
|
|
29
|
-
// by the host package's name (so any install layout or alias matches), package-relative. A package.json
|
|
30
|
-
// below one is a vendored package's: it names that package, not its host, so a file it covers is its
|
|
31
|
-
// host's (State#locateModule holds it to nothing), listed under that package in the host record's
|
|
32
|
-
// `vendored`. Maintained list: add only a confirmed upstream vendoring layout, with its reason.
|
|
23
|
+
// By host package name, so any layout or alias matches. Add only a confirmed upstream layout, with its reason.
|
|
33
24
|
const VENDOR_DIRS = {
|
|
34
25
|
__proto__: null,
|
|
35
26
|
next: ['dist/compiled'], // the dependencies Next.js compiles in, ~140, each with a trimmed package.json
|
|
36
27
|
}
|
|
37
28
|
const VENDOR_DIR_LIST = Object.values(VENDOR_DIRS).flat()
|
|
38
29
|
|
|
39
|
-
// Whether a package-relative path is below any package's vendor dir: no need to read whose it is otherwise.
|
|
40
30
|
const inAnyVendorDir = (rel) => VENDOR_DIR_LIST.some((dir) => rel.startsWith(`${dir}/`))
|
|
41
31
|
|
|
42
|
-
// The vendor dir of the package named `name` that its file `rel` (package-relative, POSIX) is below, or undefined.
|
|
43
32
|
export const vendorDirOf = (name, rel) => VENDOR_DIRS[name]?.find((dir) => rel.startsWith(`${dir}/`))
|
|
44
33
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
//
|
|
48
|
-
// or its name is no package string (a version that is none is left out). `below`: whether any package.json
|
|
49
|
-
// there covers the file at all.
|
|
34
|
+
const manifestIdentity = (pkg) => ({ name: pkg.name, ...(isPackageString(pkg.version) ? { version: pkg.version } : {}) })
|
|
35
|
+
|
|
36
|
+
// `below`: whether any package.json under `vendorDir` covers the file at all.
|
|
50
37
|
export function vendoredPackageOf(rel, vendorDir, read) {
|
|
51
38
|
let below = false
|
|
52
39
|
for (let dir = posix.dirname(rel); dir.startsWith(`${vendorDir}/`); dir = posix.dirname(dir)) {
|
|
@@ -54,15 +41,11 @@ export function vendoredPackageOf(rel, vendorDir, read) {
|
|
|
54
41
|
if (pkg === null) continue
|
|
55
42
|
below = true
|
|
56
43
|
if (pkg?.name === undefined) continue // a `{"type":"module"}` marker inside the vendored package
|
|
57
|
-
|
|
58
|
-
return { below, vendored: { dir, name: pkg.name, ...(isPackageString(pkg.version) ? { version: pkg.version } : {}) } }
|
|
44
|
+
return isPackageString(pkg.name) ? { below, vendored: { dir, ...manifestIdentity(pkg) } } : { below }
|
|
59
45
|
}
|
|
60
46
|
return { below }
|
|
61
47
|
}
|
|
62
48
|
|
|
63
|
-
// The nearest package.json with a `name` above a file `rel` of a package (package-relative, POSIX), below the
|
|
64
|
-
// package's own, walking past `{"type":"module"}` markers, each read by `read(dir)` (null where there is
|
|
65
|
-
// none) -> { dir, pkg }, or undefined where there is none.
|
|
66
49
|
export function namedManifestOf(rel, read) {
|
|
67
50
|
for (let dir = posix.dirname(rel); dir !== '.'; dir = posix.dirname(dir)) {
|
|
68
51
|
const pkg = read(dir)
|
|
@@ -71,11 +54,7 @@ export function namedManifestOf(rel, read) {
|
|
|
71
54
|
return undefined
|
|
72
55
|
}
|
|
73
56
|
|
|
74
|
-
//
|
|
75
|
-
// POSIX), short of the package's own: a `{"type":"module"}` marker such as `esm/package.json`, or a
|
|
76
|
-
// subpackage's or vendored package's, which Node takes the file's `type` from and Metro's resolver
|
|
77
|
-
// (getPackageForModule) the file's fields. `exists(rel)` says whether one is there -> its rel, or
|
|
78
|
-
// undefined where the package's own is the nearest.
|
|
57
|
+
// A nearer package.json than the package's own, which Node takes the file's `type` from and Metro its fields.
|
|
79
58
|
export function scopeManifestOf(rel, pkgDir, exists) {
|
|
80
59
|
if (pkgDir !== '.' && !rel.startsWith(`${pkgDir}/`)) return undefined
|
|
81
60
|
for (let dir = posix.dirname(rel); dir !== pkgDir && dir !== '.'; dir = posix.dirname(dir)) {
|
|
@@ -84,19 +63,14 @@ export function scopeManifestOf(rel, pkgDir, exists) {
|
|
|
84
63
|
return undefined
|
|
85
64
|
}
|
|
86
65
|
|
|
87
|
-
//
|
|
88
|
-
//
|
|
89
|
-
// outside node_modules may omit version (the name alone claims the bucket, matching
|
|
90
|
-
// State#locateModule), and a node_modules one's `ecosystem` (npm) and `repo`, where its `repository`
|
|
91
|
-
// names a GitHub one (packageRepo). Null if none. A malformed one is walked past, or with `strict` throws
|
|
92
|
-
// (its files would otherwise land in the parent package); `check`, `host`: see readPackageJson.
|
|
93
|
-
// A file in a vendored package (vendorDirOf) is its host's, as State#locateModule has it, with the
|
|
94
|
-
// package as the record's `vendored` would list it, `{ [dir]: { name, version? } }` (vendoredPackageOf);
|
|
95
|
-
// so is one in a subpackage (isSubpackage), its package.json walked past, as `subpackages`.
|
|
66
|
+
// In node_modules a bucket's package.json needs name and version; outside, a name alone, as State#locateModule.
|
|
67
|
+
// A malformed one is walked past, or with `strict` throws (its files would otherwise land in the parent's).
|
|
96
68
|
export function findPackageMetadata(baseDir, fileRelPath, { strict = false, check, host = diskHost } = {}) {
|
|
69
|
+
const options = { strict, check, host }
|
|
70
|
+
const read = (rel) => readPackageJson(baseDir, rel, options)
|
|
97
71
|
const nm = splitNodeModulesPath(toPosix(fileRelPath))
|
|
72
|
+
// A file in a vendored package is its host's, as State#locateModule has it.
|
|
98
73
|
if (nm !== null && inAnyVendorDir(nm.rel)) {
|
|
99
|
-
const read = (rel) => readPackageJson(baseDir, rel, { strict, check, host })
|
|
100
74
|
const pkg = read(`${nm.dir}/package.json`)
|
|
101
75
|
const vendorDir = pkg?.name && pkg.version ? vendorDirOf(pkg.name, nm.rel) : undefined
|
|
102
76
|
const { below, vendored } = vendorDir === undefined ? {} : vendoredPackageOf(nm.rel, vendorDir, (sub) => read(`${nm.dir}/${sub}/package.json`))
|
|
@@ -106,56 +80,40 @@ export function findPackageMetadata(baseDir, fileRelPath, { strict = false, chec
|
|
|
106
80
|
return { pkgDir: nm.dir, name: pkg.name, version: pkg.version, ecosystem: 'npm', ...(repo === undefined ? {} : { repo }), ...(vendored === undefined ? {} : { vendored: { [sub]: identity } }) }
|
|
107
81
|
}
|
|
108
82
|
}
|
|
109
|
-
//
|
|
110
|
-
// markers, as namedManifestOf walks) is one's, as the record's `subpackages` would list it.
|
|
83
|
+
// A file in a subpackage is its host's too: the nearest package.json with a name is walked past.
|
|
111
84
|
let subpackages
|
|
112
85
|
let nearest = true
|
|
113
86
|
for (let dir = dirname(fileRelPath); ; dir = dirname(dir)) {
|
|
114
|
-
const pkg =
|
|
115
|
-
const
|
|
116
|
-
const
|
|
87
|
+
const pkg = read(toPosix(join(dir, 'package.json')))
|
|
88
|
+
const posixDir = toPosix(dir)
|
|
89
|
+
const inNodeModules = hasNodeModulesSegment(posixDir)
|
|
90
|
+
const sub = pkg?.name && nm !== null ? hostSubpackageDir(read, nm.dir, posixDir, pkg.name) : undefined
|
|
117
91
|
if (sub !== undefined) {
|
|
118
|
-
if (nearest) subpackages = { [sub]:
|
|
119
|
-
|
|
120
|
-
continue
|
|
121
|
-
}
|
|
122
|
-
if (pkg?.name) nearest = false
|
|
123
|
-
if (pkg?.name && (pkg.version || !inNodeModules)) {
|
|
92
|
+
if (nearest) subpackages = { [sub]: manifestIdentity(pkg) }
|
|
93
|
+
} else if (pkg?.name && (pkg.version || !inNodeModules)) {
|
|
124
94
|
const repo = inNodeModules ? packageRepo(pkg) : undefined
|
|
125
|
-
const own =
|
|
95
|
+
const own = posixDir === nm?.dir ? subpackages : undefined // the subpackage's package, not one above it
|
|
126
96
|
// `?? undefined` folds a literal `"version": null` into the one absent-version spelling.
|
|
127
97
|
return { pkgDir: dir, name: pkg.name, version: pkg.version ?? undefined, ...(inNodeModules ? { ecosystem: 'npm' } : {}), ...(repo === undefined ? {} : { repo }), ...(own === undefined ? {} : { subpackages: own }) }
|
|
128
98
|
}
|
|
129
|
-
if (
|
|
99
|
+
if (pkg?.name) nearest = false
|
|
100
|
+
if (dirname(dir) === dir) return null // '.' and a filesystem root are their own dirname
|
|
130
101
|
}
|
|
131
102
|
}
|
|
132
103
|
|
|
133
|
-
//
|
|
134
|
-
|
|
135
|
-
export function packageLookup(baseDir, options) {
|
|
136
|
-
const byDir = new Map()
|
|
137
|
-
return (path) => {
|
|
138
|
-
const dir = posix.dirname(path)
|
|
139
|
-
if (!byDir.has(dir)) byDir.set(dir, findPackageMetadata(baseDir, path, options))
|
|
140
|
-
return byDir.get(dir)
|
|
141
|
-
}
|
|
142
|
-
}
|
|
104
|
+
// Once per directory, the only thing findPackageMetadata's answer depends on.
|
|
105
|
+
export const packageLookup = (baseDir, options) => memoized((path) => findPackageMetadata(baseDir, path, options), (path) => posix.dirname(path))
|
|
143
106
|
|
|
144
|
-
|
|
145
|
-
// POSIX) where it is a subpackage (isSubpackage) of the package at `pkgDir`; else undefined. The package's
|
|
146
|
-
// own package.json is read only where the name ends in that directory.
|
|
147
|
-
function hostSubpackageDir(baseDir, pkgDir, dir, manifestName, options) {
|
|
107
|
+
function hostSubpackageDir(read, pkgDir, dir, manifestName) {
|
|
148
108
|
if (!dir.startsWith(`${pkgDir}/`)) return undefined
|
|
149
109
|
const sub = dir.slice(pkgDir.length + 1)
|
|
150
110
|
if (typeof manifestName !== 'string' || !manifestName.endsWith(`/${sub}`)) return undefined
|
|
151
|
-
return isSubpackage(manifestName,
|
|
111
|
+
return isSubpackage(manifestName, read(`${pkgDir}/package.json`)?.name, sub) ? sub : undefined
|
|
152
112
|
}
|
|
153
113
|
|
|
154
|
-
// The error codes that mean nothing is at a path.
|
|
155
114
|
export const NO_ENTRY = new Set(['ENOENT', 'ENOTDIR'])
|
|
156
115
|
|
|
157
|
-
//
|
|
158
|
-
// when nothing is).
|
|
116
|
+
// A link is not followed, a broken one counted too (`host.readlink` throws when nothing is there).
|
|
159
117
|
export function lexists(p, host) {
|
|
160
118
|
try {
|
|
161
119
|
host.readlink(p)
|
|
@@ -166,49 +124,30 @@ export function lexists(p, host) {
|
|
|
166
124
|
}
|
|
167
125
|
}
|
|
168
126
|
|
|
169
|
-
//
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
127
|
+
// host.stat answers null for any failure, as Node's module lookup does; this, only when nothing is there. Something
|
|
128
|
+
// there that can't be read (a link loop, an unsearchable directory, a too-long name) throws `fail(cause)`.
|
|
129
|
+
function statOrFail(host, file, fail) {
|
|
130
|
+
const stat = host.stat(file)
|
|
131
|
+
if (stat !== null) return stat
|
|
174
132
|
try {
|
|
175
133
|
host.readFile(file)
|
|
176
134
|
} catch (err) {
|
|
177
|
-
|
|
135
|
+
if (NO_ENTRY.has(err.code)) return null
|
|
136
|
+
throw fail(err)
|
|
178
137
|
}
|
|
179
|
-
|
|
138
|
+
throw fail(new Error(`${file} could be read but not stat'ed`))
|
|
180
139
|
}
|
|
181
140
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
export function statStrict(host, file, label) {
|
|
185
|
-
const stat = host.stat(file)
|
|
186
|
-
if (stat !== null) return stat
|
|
187
|
-
const failure = readFailure(host, file)
|
|
188
|
-
if (failure === null) return null
|
|
189
|
-
throw new Error(`${label}: can't be read (${failure.code ?? failure.message})`, { cause: failure })
|
|
190
|
-
}
|
|
141
|
+
export const statStrict = (host, file, label) =>
|
|
142
|
+
statOrFail(host, file, (cause) => new Error(`${label}: can't be read (${cause.code ?? cause.message})`, { cause }))
|
|
191
143
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
cause === undefined ? new Error(`Invalid package config ${path}.`) : new Error(`Invalid package config ${path}.`, { cause }),
|
|
195
|
-
{ code: 'ERR_INVALID_PACKAGE_CONFIG' })
|
|
144
|
+
export const invalidPackageConfig = (path, cause) =>
|
|
145
|
+
Object.assign(new Error(`Invalid package config ${path}.`, cause === undefined ? {} : { cause }), { code: 'ERR_INVALID_PACKAGE_CONFIG' })
|
|
196
146
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
// as Node refuses it.
|
|
200
|
-
export function packageJSONStat(host, file) {
|
|
201
|
-
const stat = host.stat(file)
|
|
202
|
-
if (stat !== null) return stat
|
|
203
|
-
const cause = readFailure(host, file)
|
|
204
|
-
if (cause === null) return null
|
|
205
|
-
throw Object.assign(new Error(`Cannot read package config ${file}: ${cause.code ?? cause.message}.`, { cause }), { code: 'ERR_INVALID_PACKAGE_CONFIG' })
|
|
206
|
-
}
|
|
147
|
+
export const packageJSONStat = (host, file) => statOrFail(host, file, (cause) =>
|
|
148
|
+
Object.assign(new Error(`Cannot read package config ${file}: ${cause.code ?? cause.message}.`, { cause }), { code: 'ERR_INVALID_PACKAGE_CONFIG' }))
|
|
207
149
|
|
|
208
|
-
//
|
|
209
|
-
// It's read only when it's a regular file: a FIFO, a socket, a device or a link to one
|
|
210
|
-
// (`/dev/stdin`) throws, naming it `label`, rather than stalling or reading the process's input, and
|
|
211
|
-
// so does one there that can't be read (statStrict).
|
|
150
|
+
// A FIFO, a socket, a device or a link to one (`/dev/stdin`) throws rather than stalling or reading the process's input.
|
|
212
151
|
export function readRegularFileOrNull(file, label, host = diskHost) {
|
|
213
152
|
const stat = statStrict(host, file, label)
|
|
214
153
|
if (stat === null || stat.isDirectory()) return null
|
|
@@ -216,24 +155,17 @@ export function readRegularFileOrNull(file, label, host = diskHost) {
|
|
|
216
155
|
return host.readFile(file)
|
|
217
156
|
}
|
|
218
157
|
|
|
219
|
-
//
|
|
220
|
-
//
|
|
221
|
-
// when it doesn't parse, isn't a regular file or can't be read -- unless `strict`, then that throws
|
|
222
|
-
// (only nothing there is no package.json, as Node reads one: statStrict), saying where
|
|
223
|
-
// with the parser's line and column but never its message, which quotes the text (a file that isn't
|
|
224
|
-
// JSON may be anything, a secret included). `check(rel)`, when given, sees the path before it is
|
|
225
|
-
// read, and may throw to refuse it.
|
|
158
|
+
// Parsed past a BOM, as npm and Node; null when there's none or it can't be read, isn't a regular file or doesn't
|
|
159
|
+
// parse -- unless `strict`: then only nothing there is null. `check(rel)` sees the path before it is read.
|
|
226
160
|
export function readPackageJson(baseDir, rel, { strict = false, check, host = diskHost } = {}) {
|
|
227
161
|
const file = join(baseDir, rel)
|
|
228
162
|
const stat = strict ? statStrict(host, file, rel) : host.stat(file)
|
|
229
163
|
if (stat === null || stat.isDirectory()) return null
|
|
230
164
|
check?.(rel)
|
|
231
165
|
try {
|
|
232
|
-
// A FIFO, a socket or a device (or a link to one) is never read: it could stall the bundle.
|
|
233
166
|
if (!stat.isFile()) throw new Error(`${rel}: not a regular file`)
|
|
234
167
|
const bytes = host.readFile(file)
|
|
235
|
-
//
|
|
236
|
-
// have (a stray byte as U+FFFD).
|
|
168
|
+
// Lenient lookups decode it as they always have (a stray byte as U+FFFD).
|
|
237
169
|
if (strict && !isUtf8(bytes)) throw new Error(`${rel}: not valid UTF-8`)
|
|
238
170
|
return parseJson(packageJSONText(bytes), rel)
|
|
239
171
|
} catch (err) {
|
|
@@ -242,8 +174,7 @@ export function readPackageJson(baseDir, rel, { strict = false, check, host = di
|
|
|
242
174
|
}
|
|
243
175
|
}
|
|
244
176
|
|
|
245
|
-
//
|
|
246
|
-
// message, which quotes the text.
|
|
177
|
+
// Never the parser's message, which quotes the text (a file that isn't JSON may be anything, a secret included).
|
|
247
178
|
function parseJson(text, rel) {
|
|
248
179
|
try {
|
|
249
180
|
return JSON.parse(text)
|
|
@@ -263,8 +194,7 @@ export function normalizeEntries(entries, cwd) {
|
|
|
263
194
|
})
|
|
264
195
|
}
|
|
265
196
|
|
|
266
|
-
//
|
|
267
|
-
// read (statStrict) or isn't UTF-8 aborts (never silently skipped).
|
|
197
|
+
// One that can't be read or isn't UTF-8 aborts, never silently skipped.
|
|
268
198
|
export function readModuleManifest({ baseDir, realBase, rel, host = diskHost } = {}) {
|
|
269
199
|
const absolute = join(baseDir, rel)
|
|
270
200
|
if (statStrict(host, absolute, rel) === null) return null
|
|
@@ -283,30 +213,24 @@ export function readJson(file, host = diskHost) {
|
|
|
283
213
|
}
|
|
284
214
|
}
|
|
285
215
|
|
|
286
|
-
//
|
|
287
|
-
//
|
|
288
|
-
// a `?` or `\` ending the authority early (`https://evil.example?@github.com/a/b`) isn't GitHub's. An
|
|
289
|
-
// scp-like `git@github.com:owner/name` is taken behind an `ssh://` too (`git+ssh://git@github.com:o/n`),
|
|
290
|
-
// as package.json files spell it. The host is GitHub's with a `www.` too, which is stripped.
|
|
216
|
+
// Userinfo holds only what RFC 3986 allows: a `?` or `\` ending the authority early isn't GitHub's
|
|
217
|
+
// (`https://evil.example?@github.com/a/b`). scp-like `git@github.com:o/n` is taken behind `ssh://` too.
|
|
291
218
|
export function parseGithubRepository(url) {
|
|
292
219
|
if (typeof url !== 'string') return null
|
|
293
220
|
const match = /^(?:github:|(?:git\+)?(?:(?:https?|ssh|git):\/\/(?:[\w.~%!$&'()*+,;=:-]*@)?(?:www\.)?github\.com(?::\d+)?\/|ssh:\/\/(?:[\w.~%!$&'()*+,;=-]*@)?(?:www\.)?github\.com:|(?:[^@/:]+@)?(?:www\.)?github\.com:))?([\w.-]+)\/([\w.-]+?)(?:\.git)?\/?$/iu.exec(url.trim().replace(/#.*$/su, ''))
|
|
294
|
-
// Must also pass the bundle format's `github` check.
|
|
295
221
|
const github = match && `${match[1]}/${match[2]}`
|
|
296
222
|
return github && isValidRepoField('github', github) ? github : null
|
|
297
223
|
}
|
|
298
224
|
|
|
299
|
-
//
|
|
225
|
+
// A literal match of git's own `.git/config` layout, no parsing.
|
|
300
226
|
const GIT_ORIGIN_URL = '[remote "origin"]\n\turl = '
|
|
301
227
|
export function gitOriginUrl(text) {
|
|
302
228
|
const at = text?.indexOf(GIT_ORIGIN_URL) ?? -1
|
|
303
229
|
if (at === -1) return null
|
|
304
|
-
|
|
305
|
-
const end = text.indexOf('\n', start)
|
|
306
|
-
return text.slice(start, end === -1 ? undefined : end).trim() || null
|
|
230
|
+
return text.slice(at + GIT_ORIGIN_URL.length).split('\n', 1)[0].trim() || null
|
|
307
231
|
}
|
|
308
232
|
|
|
309
|
-
//
|
|
233
|
+
// Read through `host` (never the --fs-patched fs), it is never captured.
|
|
310
234
|
export const readText = (host, file) => {
|
|
311
235
|
try {
|
|
312
236
|
return host.readFile(file).toString('utf8')
|
|
@@ -315,7 +239,7 @@ export const readText = (host, file) => {
|
|
|
315
239
|
}
|
|
316
240
|
}
|
|
317
241
|
|
|
318
|
-
//
|
|
242
|
+
// The branch is taken as one segment: one holding a `/` reads as part of the dir.
|
|
319
243
|
export function githubHomepageDirectory(homepage, github) {
|
|
320
244
|
if (typeof homepage !== 'string') return undefined
|
|
321
245
|
const match = /^https?:\/\/(?:www\.)?github\.com\/([^/]+\/[^/]+)\/tree\/[^/#?]+\/([^#?]+)/iu.exec(homepage.trim())
|
|
@@ -327,17 +251,12 @@ export function githubHomepageDirectory(homepage, github) {
|
|
|
327
251
|
}
|
|
328
252
|
}
|
|
329
253
|
|
|
330
|
-
// `{ directory }` where it is a valid one, else `{}`.
|
|
331
254
|
const repoLocation = (directory) => (isValidRepoField('directory', directory) ? { directory } : {})
|
|
332
255
|
|
|
333
|
-
// Whether a declared path is the repository's root: nothing but empty and `.` parts (`./`, `/.`).
|
|
334
256
|
const isRootPath = (path) => path.replaceAll('\\', '/').split('/').every((part) => part === '' || part === '.')
|
|
335
257
|
|
|
336
|
-
//
|
|
337
|
-
//
|
|
338
|
-
// would come to (`a/..` is no claim on the root). Its empty and `.` parts are dropped, and one that
|
|
339
|
-
// comes to the root (`''`, `.`, `./`, `/`, `/.`) is the root, `''`. Windows users write `base` with
|
|
340
|
-
// `\`: metadata, not a name on disk, so its `\` is taken for a separator.
|
|
258
|
+
// `{}` where none is declared (unknown, not the root) or `base` has a `..` part (`a/..` is no claim on the root).
|
|
259
|
+
// Its `\` is a separator: Windows users write it so, and it is metadata, not a name on disk.
|
|
341
260
|
const declaredLocation = (base, rel) => {
|
|
342
261
|
if (typeof base !== 'string') return {}
|
|
343
262
|
const path = base.replaceAll('\\', '/')
|
|
@@ -346,7 +265,6 @@ const declaredLocation = (base, rel) => {
|
|
|
346
265
|
return repoLocation(directory === '.' ? '' : directory)
|
|
347
266
|
}
|
|
348
267
|
|
|
349
|
-
// Git and common dirs, following a worktree/submodule `.git` file.
|
|
350
268
|
function gitDirs(dotGit, host) {
|
|
351
269
|
const pointer = readText(host, dotGit) // null for a `.git` directory (EISDIR)
|
|
352
270
|
const gitDir = pointer?.startsWith('gitdir: ') ? resolve(dirname(dotGit), pointer.slice('gitdir: '.length).trim()) : dotGit
|
|
@@ -354,7 +272,6 @@ function gitDirs(dotGit, host) {
|
|
|
354
272
|
return { gitDir, commonDir: common ? resolve(gitDir, common) : gitDir }
|
|
355
273
|
}
|
|
356
274
|
|
|
357
|
-
// HEAD's commit: detached sha, loose ref, or packed-refs; undefined unless a valid sha.
|
|
358
275
|
function gitHeadCommit({ gitDir, commonDir }, host) {
|
|
359
276
|
const head = readText(host, join(gitDir, 'HEAD'))?.trim()
|
|
360
277
|
if (!head) return undefined
|
|
@@ -368,30 +285,23 @@ function gitHeadCommit({ gitDir, commonDir }, host) {
|
|
|
368
285
|
return isValidRepoField('commit', commit) ? commit : undefined
|
|
369
286
|
}
|
|
370
287
|
|
|
371
|
-
//
|
|
372
|
-
// file of a submodule or worktree; undefined where it holds none. Read through `host`, under `.git`
|
|
373
|
-
// alone; git never runs.
|
|
288
|
+
// Read through `host`, under `.git` alone; git never runs.
|
|
374
289
|
export function checkoutCommit(dir, host = diskHost) {
|
|
375
290
|
const dotGit = join(dir, '.git')
|
|
376
291
|
return host.stat(dotGit) === null ? undefined : gitHeadCommit(gitDirs(dotGit, host), host)
|
|
377
292
|
}
|
|
378
293
|
|
|
379
|
-
// A parsed package.json's `repository` url (the string shorthand, or its `url`), or undefined.
|
|
380
294
|
function repositoryUrl(json) {
|
|
381
295
|
const repository = json?.repository
|
|
382
296
|
const url = typeof repository === 'string' ? repository : repository?.url
|
|
383
297
|
return typeof url === 'string' ? url : undefined
|
|
384
298
|
}
|
|
385
299
|
|
|
386
|
-
// The `repo` a parsed package.json's `repository` names for `rel`, a directory below it ('' for its
|
|
387
|
-
// own): `github` from its GitHub URL or shorthand, and `directory` from `repository.directory` joined
|
|
388
|
-
// with `rel` (declaredLocation); undefined where it names no GitHub repository.
|
|
389
300
|
function declaredRepo(json, rel) {
|
|
390
301
|
const github = parseGithubRepository(repositoryUrl(json))
|
|
391
302
|
if (!github) return undefined
|
|
392
303
|
let base = json.repository.directory
|
|
393
|
-
// Often unset;
|
|
394
|
-
// at it: only `repository.directory` declares the root (`/tree/main/.` declares nothing).
|
|
304
|
+
// Often unset; a GitHub tree `homepage` places a package below the root, never at it (`/tree/main/.` declares nothing).
|
|
395
305
|
if (typeof base !== 'string') {
|
|
396
306
|
base = githubHomepageDirectory(json.homepage, github)
|
|
397
307
|
if (base !== undefined && isRootPath(base)) base = undefined
|
|
@@ -399,18 +309,13 @@ function declaredRepo(json, rel) {
|
|
|
399
309
|
return { github, ...declaredLocation(base, rel) }
|
|
400
310
|
}
|
|
401
311
|
|
|
402
|
-
//
|
|
403
|
-
// commit its `gitHead` names where that is a full one. npm publish writes `gitHead` into the
|
|
404
|
-
// registry's version document alone, but lerna writes it into the published package.json too, and
|
|
405
|
-
// npm keeps one a package.json has, so the two agree. Undefined where it names no GitHub repository.
|
|
312
|
+
// lerna writes `gitHead` into package.json too (npm publish only into the registry's document), and npm keeps it.
|
|
406
313
|
export function packageRepo(json) {
|
|
407
314
|
const repo = declaredRepo(json, '')
|
|
408
315
|
return repo !== undefined && isValidRepoField('commit', json.gitHead) ? { ...repo, commit: json.gitHead } : repo
|
|
409
316
|
}
|
|
410
317
|
|
|
411
|
-
//
|
|
412
|
-
// the root), else nearest package.json `repository` (declaredRepo). Its commit is git's HEAD alone,
|
|
413
|
-
// never a package.json's `gitHead`: a project's own files are likely changed since the one it names.
|
|
318
|
+
// Its commit is git's HEAD alone, never a package.json's `gitHead`: a project's own files likely changed since.
|
|
414
319
|
export function detectRepo(dir, host = diskHost) {
|
|
415
320
|
const start = resolve(dir)
|
|
416
321
|
let pkg = null // null: no package.json `repository` seen yet; undefined: one seen, not GitHub
|
|
@@ -424,11 +329,10 @@ export function detectRepo(dir, host = diskHost) {
|
|
|
424
329
|
if (host.stat(dotGit) !== null) {
|
|
425
330
|
const dirs = gitDirs(dotGit, host)
|
|
426
331
|
const github = parseGithubRepository(gitOriginUrl(readText(host, join(dirs.commonDir, 'config'))))
|
|
427
|
-
if (github) return
|
|
428
|
-
|
|
332
|
+
if (!github) return pkg ?? undefined
|
|
333
|
+
const commit = gitHeadCommit(dirs, host)
|
|
334
|
+
return { github, ...repoLocation(rel), ...(commit === undefined ? {} : { commit }) }
|
|
429
335
|
}
|
|
430
336
|
if (dirname(cursor) === cursor) return pkg ?? undefined
|
|
431
337
|
}
|
|
432
338
|
}
|
|
433
|
-
|
|
434
|
-
const stripUndefined = (obj) => Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== undefined))
|