@exodus/stasis-core 1.0.0-beta.1 → 1.0.0-beta.3
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/README.md +1 -1
- package/bin/stasis-core.js +38 -28
- package/package.json +8 -1
- package/src/add.js +325 -0
- package/src/brotli.js +15 -16
- package/src/bundle-util.js +66 -0
- package/src/bundle.js +99 -88
- package/src/config.js +78 -104
- package/src/extract.js +159 -0
- package/src/fs.js +123 -313
- package/src/hooks.js +129 -449
- package/src/loader.js +1 -3
- package/src/lockfile.js +82 -62
- package/src/prune.js +37 -152
- package/src/shard.js +60 -0
- package/src/state-util.js +4 -26
- package/src/state.js +579 -1128
- package/src/util.js +545 -97
package/src/util.js
CHANGED
|
@@ -1,70 +1,74 @@
|
|
|
1
|
+
import { isUtf8 } from 'node:buffer'
|
|
1
2
|
import * as fs from 'node:fs'
|
|
2
3
|
import { basename, isAbsolute, join, posix, relative } from 'node:path'
|
|
4
|
+
import { parseArgs } from 'node:util'
|
|
3
5
|
|
|
4
|
-
// Snapshot
|
|
5
|
-
//
|
|
6
|
-
|
|
7
|
-
// recorded-but-absent path -- which would silently defeat assertRealPathWithinBase's reliance
|
|
8
|
-
// on that throw (see its comment). Stasis's own reads must stay on the genuine builtin, so we
|
|
9
|
-
// snapshot here exactly like state.js / state-util.js / fs.js do for the other patched readers.
|
|
10
|
-
const { realpathSync } = fs
|
|
6
|
+
// Snapshot before `stasis run --fs` patches fs: the patched realpathSync doesn't throw ENOENT (which
|
|
7
|
+
// assertRealPathWithinBase relies on) and patched statSync answers with synthetic Stats modes.
|
|
8
|
+
const { realpathSync, statSync } = fs
|
|
11
9
|
|
|
12
10
|
const sep = '/'
|
|
13
11
|
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
// Adding a new format is a deliberate schema change: list it here AND extend
|
|
24
|
-
// hooks.js's executable allowlist if Node should serve it from a bundle.
|
|
12
|
+
// KNOWN_FORMATS is the closed universe of `format` strings; parsers reject anything outside it.
|
|
13
|
+
export const NODE_FORMATS = new Set(['module', 'commonjs', 'json', 'module-typescript', 'commonjs-typescript'])
|
|
14
|
+
export const SOURCE_LANGUAGE_FORMATS = new Set(['solidity', 'php', 'shell', 'rust'])
|
|
15
|
+
export const NATIVE_BUILD_FORMATS = new Set([
|
|
16
|
+
'java', 'kotlin', 'gradle', 'objc', 'objcpp', 'swift', 'c', 'cpp', 'c-header', 'cpp-header',
|
|
17
|
+
'ruby', 'cmake', 'podspec', 'podfile', 'podfile-lock', 'template', 'xml', 'env', 'fastlane', 'pbxproj',
|
|
18
|
+
])
|
|
19
|
+
export const RESOURCE_FORMATS = new Set(['resource', 'resource:base64'])
|
|
20
|
+
export const STAT_FORMATS = new Set(['stat:file', 'stat:directory'])
|
|
25
21
|
export const KNOWN_FORMATS = new Set([
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
'
|
|
31
|
-
'solidity',
|
|
32
|
-
'php',
|
|
33
|
-
'bash',
|
|
34
|
-
'rust',
|
|
35
|
-
'resource',
|
|
36
|
-
'resource:base64',
|
|
22
|
+
...NODE_FORMATS,
|
|
23
|
+
...SOURCE_LANGUAGE_FORMATS,
|
|
24
|
+
...NATIVE_BUILD_FORMATS,
|
|
25
|
+
...RESOURCE_FORMATS,
|
|
26
|
+
'patch', // a `.patch` unified diff (pnpm patchedDependencies, patch-package): UTF-8 text applied by a patch step
|
|
37
27
|
'directory',
|
|
28
|
+
...STAT_FORMATS,
|
|
29
|
+
])
|
|
30
|
+
|
|
31
|
+
// JS_UNRESOLVED_EXTS (.js/.ts type/syntax-dependent, .jsx/.tsx transformed) classify as null, not a format.
|
|
32
|
+
const NODE_EXT_FORMATS = new Map([
|
|
33
|
+
['mjs', 'module'], ['cjs', 'commonjs'], ['json', 'json'],
|
|
34
|
+
['mts', 'module-typescript'], ['cts', 'commonjs-typescript'],
|
|
38
35
|
])
|
|
36
|
+
const JS_UNRESOLVED_EXTS = new Set(['js', 'ts', 'jsx', 'tsx'])
|
|
37
|
+
export const CODE_EXTENSIONS = new Set([...NODE_EXT_FORMATS.keys(), ...JS_UNRESOLVED_EXTS])
|
|
38
|
+
|
|
39
|
+
// Payload-free stat records: attest a path's KIND, no content, and yield to a real format.
|
|
40
|
+
export const isStatFormat = (format) => STAT_FORMATS.has(format)
|
|
41
|
+
|
|
42
|
+
// Gates reconcileFormat: a stat record only reconciles with a real format of the SAME kind.
|
|
43
|
+
const formatKind = (format) => (format === 'directory' || format === 'stat:directory' ? 'directory' : 'file')
|
|
44
|
+
|
|
45
|
+
// A weak 'stat:*' yields to a real format of the SAME kind and never displaces one; anything else throws.
|
|
46
|
+
export function reconcileFormat(format, currentFormat, name) {
|
|
47
|
+
if (format === currentFormat) return format
|
|
48
|
+
const stat = isStatFormat(format)
|
|
49
|
+
const currentStat = isStatFormat(currentFormat)
|
|
50
|
+
if (stat !== currentStat && formatKind(format) === formatKind(currentFormat)) {
|
|
51
|
+
return stat ? currentFormat : format
|
|
52
|
+
}
|
|
53
|
+
throw new Error(`format conflict for '${name}' ('${currentFormat}' vs '${format}')`)
|
|
54
|
+
}
|
|
39
55
|
|
|
40
|
-
// File extensions whose bytes are JS-shaped code -- Node's loader and the
|
|
41
|
-
// lockfile/bundle format machinery can attest them (module/commonjs/json/
|
|
42
|
-
// *-typescript) and `bundle=load` can round-trip them as modules. This is the
|
|
43
|
-
// SINGLE source of truth for "is this a code file", shared so the two capture
|
|
44
|
-
// paths can't disagree: bundler plugins classify module-graph imports against it
|
|
45
|
-
// (classifyExtension), parseResourcesOption refuses to admit any of these into a
|
|
46
|
-
// `resources` allowlist (a code file is never an asset payload), and State both
|
|
47
|
-
// classifies `--fs` reads and guards `resource:true` against it. Dot-less +
|
|
48
|
-
// lowercase, matching pathExt. Drift here was the root of a lockfile/bundle
|
|
49
|
-
// desync: `.jsx`/`.tsx` were code to the plugins but missing from the fs-capture's
|
|
50
|
-
// own set, so an fs-read tagged a `.jsx` 'resource' while the import tagged it code.
|
|
51
|
-
export const CODE_EXTENSIONS = new Set(['js', 'mjs', 'cjs', 'ts', 'jsx', 'tsx', 'json', 'mts', 'cts'])
|
|
52
|
-
|
|
53
|
-
// Lowercased, dot-less extension of a path, or '' if none.
|
|
54
56
|
export function pathExt(filePath) {
|
|
55
57
|
const m = /\.([^./\\]+)$/.exec(filePath)
|
|
56
58
|
return m ? m[1].toLowerCase() : ''
|
|
57
59
|
}
|
|
58
60
|
|
|
59
|
-
//
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
61
|
+
// A native path as the '/'-joined form every artifact key uses (a no-op off Windows).
|
|
62
|
+
export const toPosix = (path) => path.split(/[\\/]/u).join('/')
|
|
63
|
+
|
|
64
|
+
// Both rules are needed: pathExt('.env.local') is 'local', and the extension rule alone misses `.env.*`.
|
|
65
|
+
export function isDotEnvFile(name) {
|
|
66
|
+
const base = basename(name).toLowerCase()
|
|
67
|
+
return base === '.env' || base.startsWith('.env.') || pathExt(base) === 'env'
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Entries are a bare extension (`png`) or extensionless filename (`LICENSE`); a dotted entry
|
|
71
|
+
// (`data.bin`) is rejected because a file with an extension is keyed by it and would never match.
|
|
68
72
|
export function parseResourcesOption(label, resources) {
|
|
69
73
|
if (resources === undefined) return new Set()
|
|
70
74
|
if (!Array.isArray(resources)) {
|
|
@@ -87,55 +91,373 @@ export function parseResourcesOption(label, resources) {
|
|
|
87
91
|
return out
|
|
88
92
|
}
|
|
89
93
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
//
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
94
|
+
export const isBrotliQuality = (n) => Number.isInteger(n) && n >= 0 && n <= 11
|
|
95
|
+
|
|
96
|
+
// `${n}` must round-trip to the input, so non-canonical forms ('5.0', '05', ' 5 ') throw.
|
|
97
|
+
export function parseBrotliQuality(name, value) {
|
|
98
|
+
const n = Number(value)
|
|
99
|
+
if (`${n}` !== value || !isBrotliQuality(n)) {
|
|
100
|
+
throw new RangeError(`${name} must be an integer 0..11 (got '${value}')`)
|
|
101
|
+
}
|
|
102
|
+
return n
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Shifts the leading option tokens off `argv`, stopping at the first positional so `run`'s forwarded
|
|
106
|
+
// child argv survives. `valueFlags` lists options whose separate-token value (`-o dir`) isn't the positional.
|
|
107
|
+
export function parseLeadingOptions(argv, options, { valueFlags = [], onError } = {}) {
|
|
108
|
+
const takesValue = new Set(valueFlags)
|
|
109
|
+
const flags = []
|
|
110
|
+
while (argv.length > 0 && (argv[0].startsWith('-') || takesValue.has(flags.at(-1)))) {
|
|
111
|
+
flags.push(argv.shift())
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
return parseArgs({ args: flags, options }).values
|
|
115
|
+
} catch (cause) {
|
|
116
|
+
onError(`Error: ${cause.message}`) // usage(), which exits
|
|
117
|
+
return undefined
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// The JS-graph bundlers' policy view over classifyFormat: only JS-family code (null or a NODE_FORMAT)
|
|
122
|
+
// counts as 'code'; native/source formats don't. Otherwise 'resource' if allowlisted, else 'unknown'.
|
|
99
123
|
export function classifyExtension(filePath, resources) {
|
|
100
|
-
const
|
|
101
|
-
if (
|
|
102
|
-
if (resources.has(
|
|
124
|
+
const format = classifyFormat(filePath)
|
|
125
|
+
if (format === null || NODE_FORMATS.has(format)) return 'code'
|
|
126
|
+
if (resources.has(pathExt(filePath) || basename(filePath).toLowerCase())) return 'resource'
|
|
103
127
|
return 'unknown'
|
|
104
128
|
}
|
|
105
129
|
|
|
106
|
-
//
|
|
107
|
-
//
|
|
108
|
-
|
|
130
|
+
// Compiled/prebuilt artifacts, non-deterministic across installs, so the native capture skips them
|
|
131
|
+
// entirely. Matched on a file OR directory name (an Apple `*.framework`/`*.xcodeproj` bundle dir).
|
|
132
|
+
const NATIVE_ARTIFACT_EXTS = new Set([
|
|
133
|
+
'a', 'so', 'o', 'obj', 'dylib', 'lib', 'dll', 'exe', 'pdb',
|
|
134
|
+
'aar', 'jar', 'class', 'dex',
|
|
135
|
+
'node',
|
|
136
|
+
'framework', 'xcframework', 'dsym',
|
|
137
|
+
'xcodeproj', 'xcworkspace',
|
|
138
|
+
'zip', 'tar', 'gz', 'tgz', 'bz2', 'xz', '7z',
|
|
139
|
+
])
|
|
140
|
+
export function isNativeArtifact(name) {
|
|
141
|
+
return NATIVE_ARTIFACT_EXTS.has(pathExt(name))
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// An extensionless non-UTF-8 file is a compiled tool (Hermes' `hermesc`), never source -- skipped.
|
|
145
|
+
function isExtensionlessBinary(name, content) {
|
|
146
|
+
return pathExt(name) === '' && Buffer.isBuffer(content) && !isUtf8(content)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// A binary plist is a real build input, but its bytes can't ride the text-only 'xml' format
|
|
150
|
+
// classifyFormat gives a `.plist`. Callers treat it as NOT code, so it needs `.plist` in `resources`.
|
|
151
|
+
export function isBinaryPlist(name, content) {
|
|
152
|
+
return pathExt(name) === 'plist' && Buffer.isBuffer(content) && !isUtf8(content)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Discovered by name: RN scatters podspecs in subdirs `react-native config` misses.
|
|
156
|
+
export function isPodspec(name) {
|
|
157
|
+
return name.endsWith('.podspec') || name.endsWith('.podspec.json')
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Non-JS code formats by extension. Every value must be in KNOWN_FORMATS.
|
|
161
|
+
const CODE_EXT_FORMATS = new Map([
|
|
162
|
+
['sol', 'solidity'],
|
|
163
|
+
['php', 'php'],
|
|
164
|
+
['sh', 'shell'], ['bash', 'shell'],
|
|
165
|
+
['rs', 'rust'],
|
|
166
|
+
['patch', 'patch'],
|
|
167
|
+
['java', 'java'],
|
|
168
|
+
['kt', 'kotlin'],
|
|
169
|
+
['kts', 'kotlin'],
|
|
170
|
+
['gradle', 'gradle'],
|
|
171
|
+
['m', 'objc'],
|
|
172
|
+
['mm', 'objcpp'],
|
|
173
|
+
['swift', 'swift'],
|
|
174
|
+
['c', 'c'],
|
|
175
|
+
['cc', 'cpp'], ['cxx', 'cpp'], ['cpp', 'cpp'], ['c++', 'cpp'],
|
|
176
|
+
['h', 'c-header'],
|
|
177
|
+
['hh', 'cpp-header'], ['hxx', 'cpp-header'], ['hpp', 'cpp-header'], ['h++', 'cpp-header'],
|
|
178
|
+
['rb', 'ruby'],
|
|
179
|
+
['cmake', 'cmake'],
|
|
180
|
+
['podspec', 'podspec'],
|
|
181
|
+
['template', 'template'],
|
|
182
|
+
['xml', 'xml'],
|
|
183
|
+
['plist', 'xml'],
|
|
184
|
+
['xcprivacy', 'xml'],
|
|
185
|
+
['xcscheme', 'xml'],
|
|
186
|
+
['storyboard', 'xml'],
|
|
187
|
+
['entitlements', 'xml'],
|
|
188
|
+
['env', 'env'],
|
|
189
|
+
['pbxproj', 'pbxproj'],
|
|
190
|
+
['xcworkspacedata', 'xml'],
|
|
191
|
+
])
|
|
192
|
+
|
|
193
|
+
// Code formats by exact (lowercased) basename, for names whose extension is too generic to key
|
|
194
|
+
// (Podfile.lock's `.lock`, CMakeLists.txt's `.txt`).
|
|
195
|
+
const CODE_NAME_FORMATS = new Map([
|
|
196
|
+
['podfile', 'podfile'],
|
|
197
|
+
['podfile.lock', 'podfile-lock'],
|
|
198
|
+
['cmakelists.txt', 'cmake'],
|
|
199
|
+
['gradlew', 'shell'],
|
|
200
|
+
['appfile', 'fastlane'],
|
|
201
|
+
['fastfile', 'fastlane'],
|
|
202
|
+
['apple-app-site-association', 'json'],
|
|
203
|
+
])
|
|
204
|
+
|
|
205
|
+
const SHELL_SHEBANG = /^#![^\n]*\b(?:bash|sh)\b/u
|
|
206
|
+
function isShellShebang(content) {
|
|
207
|
+
if (!Buffer.isBuffer(content) || !isUtf8(content)) return false
|
|
208
|
+
const head = content.subarray(0, 256).toString('utf8')
|
|
209
|
+
const nl = head.indexOf('\n')
|
|
210
|
+
return SHELL_SHEBANG.test(nl === -1 ? head : head.slice(0, nl))
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Files a native package ships that are NOT build inputs (docs/legal, editor/lint/CI config, logs,
|
|
214
|
+
// sidecars), excluded from the Metro native capture. `flow` covers the `*.js.flow` sidecars.
|
|
215
|
+
const NATIVE_EXCLUDE_EXTS = new Set(['md', 'log', 'map', 'flow', 'swiftdoc'])
|
|
216
|
+
const NATIVE_EXCLUDE_NAMES = new Set([
|
|
217
|
+
'license', 'licence', 'third-party-licenses',
|
|
218
|
+
'.prettierrc', '.prettierignore', '.prettierrc.js', '.gitattributes', '.flowconfig', '.eslintignore',
|
|
219
|
+
'.releaserc', '.clang-format', '.buckconfig', '.watchmanconfig', '.editorconfig', 'circle.yml', '.swiftlint.yml',
|
|
220
|
+
'documentation.yml', // documentation.js config
|
|
221
|
+
'yarn.lock',
|
|
222
|
+
'.project', // Eclipse IDE metadata
|
|
223
|
+
'gradle-wrapper.properties', // the APP's wrapper drives the build, not a module's own
|
|
224
|
+
])
|
|
225
|
+
export function isExcludedNativeFile(name, { win32 = process.platform === 'win32' } = {}) {
|
|
226
|
+
const base = basename(name).toLowerCase()
|
|
227
|
+
if (NATIVE_EXCLUDE_NAMES.has(base)) return true
|
|
228
|
+
const ext = pathExt(name)
|
|
229
|
+
if (NATIVE_EXCLUDE_EXTS.has(ext)) return true
|
|
230
|
+
return !win32 && ext === 'bat'
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function isExcludedNativeDir(name, { win32 = process.platform === 'win32' } = {}) {
|
|
234
|
+
return !win32 && name === 'windows'
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// An Apple prebuilt-slice dir (`ios-arm64`, `tvos-arm64_x86_64-simulator`): compiled output, not source,
|
|
238
|
+
// at ANY depth. The PLATFORM segment is deliberately loose -- the required ARCH segment is the precision.
|
|
239
|
+
const APPLE_ARCH = String.raw`(?:arm64e|arm64_32|arm64|armv7k|armv7s|armv7|x86_64|i386)`
|
|
240
|
+
const APPLE_SLICE_DIR = new RegExp(
|
|
241
|
+
String.raw`^[a-z0-9]+-${APPLE_ARCH}(?:_${APPLE_ARCH})*(?:-(?:simulator|maccatalyst))?$`,
|
|
242
|
+
'u'
|
|
243
|
+
)
|
|
244
|
+
export function isAppleSliceDir(name) {
|
|
245
|
+
return APPLE_SLICE_DIR.test(name)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Types only, never a runtime module: the resolvers must refuse to land on one and fall through to the
|
|
249
|
+
// real `.js`. Keyed by compound suffix -- pathExt only sees the trailing `ts`.
|
|
250
|
+
const TYPE_DECLARATION_SUFFIXES = ['.d.ts', '.d.mts', '.d.cts']
|
|
251
|
+
export function isTypeDeclaration(name) {
|
|
252
|
+
const base = basename(name).toLowerCase()
|
|
253
|
+
return TYPE_DECLARATION_SUFFIXES.some((suffix) => base.endsWith(suffix))
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function stripTypeDeclaration(name) {
|
|
257
|
+
const lower = name.toLowerCase()
|
|
258
|
+
const suffix = TYPE_DECLARATION_SUFFIXES.find((s) => lower.endsWith(s))
|
|
259
|
+
return suffix === undefined ? name : name.slice(0, -suffix.length)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// stasis's own outputs (`stasis.lock.json`, a `*stasis*.br`): absent on a first run but present on a
|
|
263
|
+
// later one, so a listing (`--fs` readdir) or a sweep (`add .`) that records one diverges between runs
|
|
264
|
+
// and the later capture conflicts.
|
|
265
|
+
export function isStasisArtifactName(name) {
|
|
266
|
+
const base = basename(name).toLowerCase()
|
|
267
|
+
return base === 'stasis.lock.json' || (base.includes('stasis') && base.endsWith('.br'))
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Dirs no walk descends into: VCS/CI/IDE metadata, sample apps, test scaffolding -- none of it is the
|
|
271
|
+
// code being shipped -- plus the Apple prebuilt slice dirs. `name` is a BARE dir name (a dirent name
|
|
272
|
+
// or path segment), matched at any depth, so every walk agrees on what "not source" means.
|
|
273
|
+
const AUTO_EXCLUDED_DIRS = new Set([
|
|
274
|
+
'.git', '.github', '.settings', // VCS / CI / IDE metadata
|
|
275
|
+
'example', 'examples', // sample apps -- they import the package, they aren't it
|
|
276
|
+
'__tests__', '__mocks__', 'jest', // test scaffolding
|
|
277
|
+
])
|
|
278
|
+
export function isAutoExcludedDir(name) {
|
|
279
|
+
const base = name.toLowerCase()
|
|
280
|
+
return AUTO_EXCLUDED_DIRS.has(base) || isAppleSliceDir(base)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Dirs the NATIVE walks (Metro plugin + the static --metro bundler) skip, on top of the shared set:
|
|
284
|
+
// nested packages (attested separately) and regenerated build output -- none is a build input.
|
|
285
|
+
const NATIVE_BUILD_OUTPUT_DIRS = new Set(['node_modules', 'build', '.gradle', '.cxx', 'Pods', 'DerivedData'])
|
|
286
|
+
export function isSkippedNativeWalkDir(name) {
|
|
287
|
+
return NATIVE_BUILD_OUTPUT_DIRS.has(name) || isAutoExcludedDir(name) || isNativeArtifact(name)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Files a sweep drops: type declarations (types only, erased at runtime), the `.env` family (secrets),
|
|
291
|
+
// stasis's own outputs (they'd attest themselves), and everything the native capture excludes as
|
|
292
|
+
// non-build-input noise (docs/legal, editor/lint/CI config, `*.map`/`*.js.flow` sidecars, logs).
|
|
293
|
+
// A path can only reach this via a glob -- an EXPLICITLY named one is added as asked (`add .env` still
|
|
294
|
+
// captures), which is also the only way to attest a file this set covers.
|
|
295
|
+
export function isAutoExcludedFile(name) {
|
|
296
|
+
return isTypeDeclaration(name) || isDotEnvFile(name) || isStasisArtifactName(name) || isExcludedNativeFile(name)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// The single name->format classifier every capture path is a policy view of. Returns a concrete format
|
|
300
|
+
// when the name determines one; null for a JS-family file (.js/.ts/.jsx/.tsx); undefined if unrecognized.
|
|
301
|
+
export function classifyFormat(name, { content } = {}) {
|
|
302
|
+
const base = basename(name).toLowerCase()
|
|
303
|
+
// Before the extension rules, so a `*.podspec.json` isn't shadowed.
|
|
304
|
+
if (base === 'package.json' || base.endsWith('.podspec.json')) return 'json'
|
|
305
|
+
// Compound suffix: pathExt only sees the trailing `in`.
|
|
306
|
+
if (base.endsWith('.cmake.in')) return 'cmake'
|
|
307
|
+
const byName = CODE_NAME_FORMATS.get(base)
|
|
308
|
+
if (byName !== undefined) return byName
|
|
309
|
+
const ext = pathExt(name)
|
|
310
|
+
if (JS_UNRESOLVED_EXTS.has(ext)) return null
|
|
311
|
+
const byExt = NODE_EXT_FORMATS.get(ext) ?? CODE_EXT_FORMATS.get(ext)
|
|
312
|
+
if (byExt !== undefined) return byExt
|
|
313
|
+
if (ext === '' && isShellShebang(content)) return 'shell'
|
|
314
|
+
return undefined
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// The Metro native capture's policy view. Returns { action, format }: 'code' (a native build input),
|
|
318
|
+
// 'skip' (excluded noise, or a JS-family file matched BY EXTENSION -- Metro owns those), else 'resource'.
|
|
319
|
+
export function classifyNativeCapture(name, { win32 = process.platform === 'win32', content } = {}) {
|
|
320
|
+
if (isExcludedNativeFile(name, { win32 })) return { action: 'skip' }
|
|
321
|
+
// `.env` files carry secrets: an automated capture must never sweep them in.
|
|
322
|
+
if (isDotEnvFile(name)) return { action: 'skip' }
|
|
323
|
+
const base = basename(name).toLowerCase()
|
|
324
|
+
// Name-matched code is always a native build input, even when its tag is a Node format the JS
|
|
325
|
+
// graph would otherwise own.
|
|
326
|
+
if (base === 'package.json' || base.endsWith('.podspec.json')) return { action: 'code', format: 'json' }
|
|
327
|
+
const byName = CODE_NAME_FORMATS.get(base)
|
|
328
|
+
if (byName !== undefined) return { action: 'code', format: byName }
|
|
329
|
+
if (CODE_EXTENSIONS.has(pathExt(name))) return { action: 'skip' }
|
|
330
|
+
const format = classifyFormat(name, { content })
|
|
331
|
+
if (format !== undefined) return { action: 'code', format }
|
|
332
|
+
return { action: 'resource' }
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// The byte-level half of the native classification, refining what classifyNativeCapture derived from the
|
|
336
|
+
// NAME. Both rules only ever demote. A 'resource' carries no format: storage derives base64 from bytes.
|
|
337
|
+
export function refineNativeCapture(classified, name, content, resources = new Set()) {
|
|
338
|
+
if (isExtensionlessBinary(name, content)) return { action: 'skip' }
|
|
339
|
+
if (isBinaryPlist(name, content)) return { action: resources.has('plist') ? 'resource' : 'skip' }
|
|
340
|
+
return classified
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Files `pod install` reads while loading podspecs (Ruby helpers, package.json) -- NOT the native
|
|
344
|
+
// source those podspecs compile.
|
|
345
|
+
export function isNativeManifest(name) {
|
|
346
|
+
return isPodspec(name) || pathExt(name) === 'rb' || name === 'package.json'
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// RN core build scripts the native walk would defer to Metro as `.js`, but Metro never sees them (RN's
|
|
350
|
+
// podspecs invoke them at pod-install). Project-relative, so react-native's `exports` can't hide them.
|
|
351
|
+
export const RN_CORE_INCLUDE_FILES = ['sdks/hermes-engine/utils/replace_hermes_version.js']
|
|
352
|
+
|
|
109
353
|
export function extSetsEqual(a, b) {
|
|
110
354
|
if (a.size !== b.size) return false
|
|
111
355
|
for (const ext of a) if (!b.has(ext)) return false
|
|
112
356
|
return true
|
|
113
357
|
}
|
|
114
358
|
|
|
115
|
-
// Flat project-relative key
|
|
116
|
-
//
|
|
117
|
-
// `directory` capture whose path IS a module root (e.g. `fs.readdirSync` of a
|
|
118
|
-
// package dir, or of the project root): the listing is keyed at the bucket itself,
|
|
119
|
-
// so the flat key is the dir (or '' for the workspace root) — never `${dir}/`,
|
|
120
|
-
// which a trailing-slash join would wrongly produce and break the round-trip. For
|
|
121
|
-
// every ordinary file `rel` is a real basename, so this matches the historical
|
|
122
|
-
// `dir === '.' ? rel : `${dir}/${rel}`` exactly.
|
|
359
|
+
// Flat project-relative key. `rel === ''` (a `directory` capture whose path IS a module root) keys the
|
|
360
|
+
// bare dir, never `${dir}/` -- a trailing slash breaks the round-trip.
|
|
123
361
|
export function moduleFileKey(dir, rel) {
|
|
124
|
-
if (rel === '') return dir
|
|
362
|
+
if (rel === '') return dir
|
|
125
363
|
return dir === '.' ? rel : `${dir}/${rel}`
|
|
126
364
|
}
|
|
127
365
|
|
|
128
|
-
//
|
|
129
|
-
//
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
366
|
+
// The keys a module map records -- the set an artifact's `executable` must be a subset of. `scope` MUST
|
|
367
|
+
// be the artifact's own: a non-full-scope artifact records only its node_modules buckets.
|
|
368
|
+
export function moduleFileKeys(modules, { scope = 'full' } = {}) {
|
|
369
|
+
const keys = new Set()
|
|
370
|
+
for (const [dir, { files }] of modules) {
|
|
371
|
+
if (scope !== 'full' && !hasNodeModulesSegment(dir)) continue
|
|
372
|
+
for (const rel of Object.keys(files)) keys.add(moduleFileKey(dir, rel))
|
|
373
|
+
}
|
|
374
|
+
return keys
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export const EXECUTE_BITS = 0o111
|
|
378
|
+
|
|
379
|
+
export const isExecutableMode = (stats) => stats.isFile() && (stats.mode & EXECUTE_BITS) !== 0
|
|
380
|
+
|
|
381
|
+
// Tri-state: `undefined` means the mode could NOT be observed (gone mid-run, EACCES, ELOOP, a synthetic
|
|
382
|
+
// bundle entry). Callers must not read that as "not executable" -- failing to look is not evidence.
|
|
383
|
+
export function observeExecutable(abs) {
|
|
384
|
+
let stats
|
|
385
|
+
try {
|
|
386
|
+
stats = statSync(abs, { throwIfNoEntry: false })
|
|
387
|
+
} catch {
|
|
388
|
+
return undefined
|
|
389
|
+
}
|
|
390
|
+
if (stats === undefined) return undefined
|
|
391
|
+
return isExecutableMode(stats)
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Boolean view for callers with nothing to refute (recording a fresh set from scratch).
|
|
395
|
+
export const isExecutableFile = (abs) => observeExecutable(abs) === true
|
|
396
|
+
|
|
397
|
+
// Windows reports no POSIX execute bits, so a capture there records none and must NOT read "no bit" as
|
|
398
|
+
// "the bit was removed" and strip what a POSIX capture attested.
|
|
399
|
+
export const canObserveExecuteBits = ({ win32 = process.platform === 'win32' } = {}) => !win32
|
|
400
|
+
|
|
401
|
+
// THE rule an artifact's `executable` entry must satisfy; returns the problem, or null when legal.
|
|
402
|
+
// parseExecutable (read), assertExecutable (write) and narrowExecutable share it so they cannot drift.
|
|
403
|
+
function executableEntryProblem(file, { what, files, formats, scope }) {
|
|
404
|
+
if (posixPathEscapes(file)) return 'escapes the root'
|
|
405
|
+
if (scope !== 'full' && !hasNodeModulesSegment(file)) {
|
|
406
|
+
return `is outside node_modules, which a '${scope}'-scope ${what} does not record`
|
|
407
|
+
}
|
|
408
|
+
if (!files.has(file)) return `names no file the ${what} records`
|
|
409
|
+
const format = formats.get(file)
|
|
410
|
+
if (format === 'directory') return 'is a directory capture, not a file'
|
|
411
|
+
if (isStatFormat(format)) return `is a payload-free '${format}' record, not a file`
|
|
412
|
+
return null
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// Applied at every write site, so the in-memory artifact is honest before it is ever serialized.
|
|
416
|
+
export function narrowExecutable(executable, { modules, formats, scope }) {
|
|
417
|
+
const out = new Set()
|
|
418
|
+
if (executable.size === 0) return out
|
|
419
|
+
const files = moduleFileKeys(modules, { scope })
|
|
420
|
+
for (const file of executable) {
|
|
421
|
+
if (executableEntryProblem(file, { what: 'artifact', files, formats, scope }) === null) out.add(file)
|
|
422
|
+
}
|
|
423
|
+
return out
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function assertExecutable(executable, { what, files, formats, scope }) {
|
|
427
|
+
for (const file of executable) {
|
|
428
|
+
const problem = executableEntryProblem(file, { what, files, formats, scope })
|
|
429
|
+
assert(problem === null, `${what}: executable entry '${file}' ${problem}`)
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
export function parseExecutable(list, { what, files, formats, scope = 'full' }) {
|
|
434
|
+
if (list === undefined) return new Set()
|
|
435
|
+
const at = `${what}: executable`
|
|
436
|
+
assert(Array.isArray(list), `${at} must be an array of file paths`)
|
|
437
|
+
const out = new Set()
|
|
438
|
+
for (const file of list) {
|
|
439
|
+
assert(typeof file === 'string' && file !== '', `${at} entry must be a non-empty string`)
|
|
440
|
+
// A dupe would silently collapse in out.add() and round-trip to different bytes.
|
|
441
|
+
assert(!out.has(file), `${at} entry '${file}' is listed twice`)
|
|
442
|
+
out.add(file)
|
|
443
|
+
}
|
|
444
|
+
assertExecutable(out, { what, files, formats, scope })
|
|
445
|
+
return out
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// The one choke point every producer goes through, so a write site that forgot to narrow fails HERE, not
|
|
449
|
+
// on the next read. Omitted when empty: keeps every pre-`executable` artifact byte-identical when rewritten.
|
|
450
|
+
export function serializeExecutable(executable, { what, modules, formats, scope }) {
|
|
451
|
+
if (executable.size === 0) return undefined
|
|
452
|
+
assertExecutable(executable, { what, files: moduleFileKeys(modules, { scope }), formats, scope })
|
|
453
|
+
return fileSetToObject(executable)
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// Throws on a symlink escaping the bundle root: a crafted `link.sh -> /etc/passwd` must not pull an
|
|
457
|
+
// external file into an attestable bundle. realpathSync surfaces ENOENT, which loaders treat as "missing".
|
|
136
458
|
export function assertRealPathWithinBase(realBase, baseDir, relPath) {
|
|
137
459
|
const real = realpathSync(join(baseDir, relPath))
|
|
138
|
-
const rel = relative(realBase, real)
|
|
460
|
+
const rel = toPosix(relative(realBase, real))
|
|
139
461
|
if (rel.startsWith('..') || isAbsolute(rel)) {
|
|
140
462
|
throw new Error(`Refusing to follow symlink escaping bundle root: ${relPath} -> ${real}`)
|
|
141
463
|
}
|
|
@@ -157,11 +479,9 @@ export function sortPaths(a, b) {
|
|
|
157
479
|
if (al.length < 2) return bl.length < 2 && al > bl ? 1 : -1
|
|
158
480
|
if (bl.length < 2) return 1
|
|
159
481
|
|
|
160
|
-
// * comes first
|
|
161
482
|
if (al[0] === '*') return -1
|
|
162
483
|
if (bl[0] === '*') return 1
|
|
163
484
|
|
|
164
|
-
// node_modules comes last
|
|
165
485
|
if (al[0] === 'node_modules') return 1
|
|
166
486
|
if (bl[0] === 'node_modules') return -1
|
|
167
487
|
|
|
@@ -178,28 +498,156 @@ export const fromEntries = (entries) => Object.setPrototypeOf(Object.fromEntries
|
|
|
178
498
|
|
|
179
499
|
export const fileSetToObject = (set) => [...set].toSorted((a, b) => sortPaths(a, b))
|
|
180
500
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
)
|
|
501
|
+
// `sorted: false` only for a machine-only payload whose reader does not care about ordering (shard.js).
|
|
502
|
+
export const fileMapToObject = (map, { sorted = true } = {}) => {
|
|
503
|
+
if (!sorted) {
|
|
504
|
+
const out = Object.create(null)
|
|
505
|
+
for (const [k, v] of map) out[k] = v instanceof Map ? fileMapToObject(v, { sorted }) : v
|
|
506
|
+
return out
|
|
507
|
+
}
|
|
508
|
+
return fromEntries(
|
|
509
|
+
[...map]
|
|
510
|
+
.toSorted((a, b) => sortPaths(a[0], b[0]))
|
|
511
|
+
.map(([k, v]) => [k, v instanceof Map ? fileMapToObject(v, { sorted }) : v])
|
|
512
|
+
)
|
|
513
|
+
}
|
|
186
514
|
|
|
187
515
|
export const objectToMaps = (obj) => new Map(
|
|
188
516
|
Object.entries(obj).map(([k, v]) => [k, isPlainObject(v) ? objectToMaps(v) : v])
|
|
189
517
|
)
|
|
190
518
|
|
|
191
|
-
//
|
|
192
|
-
// a leading `..`, an absolute path, or a mid-path `..` that pops above the
|
|
193
|
-
// root (`a/../../x`). Plain `startsWith('..')` only catches the leading case,
|
|
194
|
-
// so it's not enough for paths that later get resolved against the root.
|
|
519
|
+
// Normalizes first: plain `startsWith('..')` would miss a mid-path `..` that pops above the root.
|
|
195
520
|
export function posixPathEscapes(path) {
|
|
521
|
+
// Exact prefilter: normalize() can only yield a '..'-prefixed result from an input containing '..',
|
|
522
|
+
// or an absolute one from an input already absolute.
|
|
523
|
+
if (!path.includes('..') && !posix.isAbsolute(path)) return false
|
|
196
524
|
const normalized = posix.normalize(path)
|
|
197
525
|
return normalized === '..' || normalized.startsWith('../') || posix.isAbsolute(normalized)
|
|
198
526
|
}
|
|
199
527
|
|
|
528
|
+
// A target is a resolved-file string, or a { platform: file } Map under --metro.
|
|
529
|
+
function importTargetsEqual(a, b) {
|
|
530
|
+
const aMap = a instanceof Map
|
|
531
|
+
if (aMap !== (b instanceof Map)) return false
|
|
532
|
+
if (!aMap) return a === b
|
|
533
|
+
if (a.size !== b.size) return false
|
|
534
|
+
for (const [platform, file] of a) if (b.get(platform) !== file) return false
|
|
535
|
+
return true
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
const fmtTarget = (t) => (t instanceof Map ? `{${[...t].map(([p, f]) => `${p}: ${f}`).join(', ')}}` : t)
|
|
539
|
+
|
|
540
|
+
// Merge two import maps (conditions -> parent -> specifier -> target); a redirect conflict throws.
|
|
541
|
+
export function mergeImportMaps(a, b, label) {
|
|
542
|
+
const out = new Map()
|
|
543
|
+
const absorb = (imports) => {
|
|
544
|
+
for (const [conditions, byParent] of imports) {
|
|
545
|
+
let outByParent = out.get(conditions)
|
|
546
|
+
if (outByParent === undefined) out.set(conditions, (outByParent = new Map()))
|
|
547
|
+
for (const [parent, specs] of byParent) {
|
|
548
|
+
let outSpecs = outByParent.get(parent)
|
|
549
|
+
if (outSpecs === undefined) outByParent.set(parent, (outSpecs = new Map()))
|
|
550
|
+
for (const [spec, target] of specs) {
|
|
551
|
+
if (outSpecs.has(spec)) {
|
|
552
|
+
assert(importTargetsEqual(outSpecs.get(spec), target),
|
|
553
|
+
`${label}: import '${spec}' from '${parent}' resolves differently ('${fmtTarget(outSpecs.get(spec))}' vs '${fmtTarget(target)}')`)
|
|
554
|
+
} else {
|
|
555
|
+
outSpecs.set(spec, target instanceof Map ? new Map(target) : target)
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
absorb(a)
|
|
562
|
+
absorb(b)
|
|
563
|
+
return out
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
export function mergeFormatMaps(a, b, label) {
|
|
567
|
+
const out = new Map()
|
|
568
|
+
const absorb = (formats) => {
|
|
569
|
+
for (const [file, format] of formats) {
|
|
570
|
+
const currentFormat = out.get(file)
|
|
571
|
+
if (currentFormat === undefined) {
|
|
572
|
+
out.set(file, format)
|
|
573
|
+
continue
|
|
574
|
+
}
|
|
575
|
+
try {
|
|
576
|
+
out.set(file, reconcileFormat(format, currentFormat, file))
|
|
577
|
+
} catch (cause) {
|
|
578
|
+
throw new Error(`${label}: ${cause.message}`, { cause })
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
absorb(a)
|
|
583
|
+
absorb(b)
|
|
584
|
+
return out
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// A union, EXCEPT that `b` (the INCOMING, newer artifact -- every call site passes it on the right) is
|
|
588
|
+
// authoritative for its own files: a since-lost execute bit is cleared, not resurrected by the union.
|
|
589
|
+
export function mergeExecutableSets(a, b, bModules, scope) {
|
|
590
|
+
if (a.size === 0) return new Set(b)
|
|
591
|
+
const bFiles = moduleFileKeys(bModules, { scope })
|
|
592
|
+
const out = new Set(b)
|
|
593
|
+
for (const file of a) if (!bFiles.has(file)) out.add(file)
|
|
594
|
+
return out
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// Result `files` objects are null-prototype, so a `__proto__` file name is a plain own key.
|
|
598
|
+
export function mergeModuleMaps(a, b, label) {
|
|
599
|
+
const out = new Map()
|
|
600
|
+
const absorb = (modules) => {
|
|
601
|
+
for (const [dir, info] of modules) {
|
|
602
|
+
const existing = out.get(dir)
|
|
603
|
+
if (existing === undefined) {
|
|
604
|
+
out.set(dir, {
|
|
605
|
+
name: info.name,
|
|
606
|
+
version: info.version,
|
|
607
|
+
...(info.ecosystem === undefined ? {} : { ecosystem: info.ecosystem }),
|
|
608
|
+
files: Object.assign(Object.create(null), info.files),
|
|
609
|
+
})
|
|
610
|
+
continue
|
|
611
|
+
}
|
|
612
|
+
assert(existing.name === info.name,
|
|
613
|
+
`${label}: module '${dir}' name mismatch ('${existing.name}' vs '${info.name}')`)
|
|
614
|
+
assert(existing.version === info.version,
|
|
615
|
+
`${label}: module '${dir}' version mismatch ('${existing.version}' vs '${info.version}')`)
|
|
616
|
+
assert(existing.ecosystem === info.ecosystem,
|
|
617
|
+
`${label}: module '${dir}' ecosystem mismatch ('${existing.ecosystem ?? '(none)'}' vs '${info.ecosystem ?? '(none)'}')`)
|
|
618
|
+
for (const [rel, value] of Object.entries(info.files)) {
|
|
619
|
+
if (Object.hasOwn(existing.files, rel)) {
|
|
620
|
+
assert(existing.files[rel] === value, `${label}: content mismatch for '${moduleFileKey(dir, rel)}'`)
|
|
621
|
+
} else {
|
|
622
|
+
existing.files[rel] = value
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
absorb(a)
|
|
628
|
+
absorb(b)
|
|
629
|
+
return out
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// A full path SEGMENT, NOT a bare substring (`foo_node_modules/dep` is a source dir, not a bucket).
|
|
633
|
+
export function hasNodeModulesSegment(path) {
|
|
634
|
+
return path.split('/').includes('node_modules')
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// Deepest segment-aligned `node_modules/` marker, or -1: `lastIndexOf` alone would also match a bare
|
|
638
|
+
// substring (`foo_node_modules/`), so a candidate only counts at the start or after a `/`.
|
|
639
|
+
function lastNodeModulesMarker(path) {
|
|
640
|
+
const marker = 'node_modules/'
|
|
641
|
+
let idx = path.length
|
|
642
|
+
while ((idx = path.lastIndexOf(marker, idx - 1)) !== -1) {
|
|
643
|
+
if (idx === 0 || path[idx - 1] === '/') return idx
|
|
644
|
+
}
|
|
645
|
+
return -1
|
|
646
|
+
}
|
|
647
|
+
|
|
200
648
|
export function splitNodeModulesPath(path) {
|
|
201
649
|
const marker = 'node_modules/'
|
|
202
|
-
const idx = path
|
|
650
|
+
const idx = lastNodeModulesMarker(path)
|
|
203
651
|
if (idx === -1) return null
|
|
204
652
|
const after = idx + marker.length
|
|
205
653
|
const parts = path.slice(after).split('/')
|