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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/util.js CHANGED
@@ -1,20 +1,15 @@
1
1
  import { isUtf8 } from 'node:buffer'
2
- import * as fs from 'node:fs'
3
- import { basename, isAbsolute, join, relative } from 'node:path'
2
+ import { basename, isAbsolute, join, relative, sep } from 'node:path'
4
3
  import { parseArgs } from 'node:util'
5
4
 
6
5
  import { NODE_FORMATS } from './artifact-util.js'
6
+ import { diskHost } from './host.js'
7
7
 
8
8
  // The Node-side half of the util split: byte/name classification for the capture walks, fs/execute-bit
9
- // observation and CLI parsing. The pure artifact data model (formats universe, keys, merges,
10
- // executable-set rules, converters) lives in artifact-util.js; re-exported here so
11
- // `@exodus/stasis-core/util` keeps serving the full set.
9
+ // observation and CLI parsing. The pure artifact data model lives in artifact-util.js, re-exported
10
+ // here so `@exodus/stasis-core/util` keeps serving the full set.
12
11
  export * from './artifact-util.js'
13
12
 
14
- // Snapshot before `stasis run --fs` patches fs: the patched realpathSync doesn't throw ENOENT (which
15
- // assertRealPathWithinBase relies on) and patched statSync answers with synthetic Stats modes.
16
- const { realpathSync, statSync } = fs
17
-
18
13
  // JS_UNRESOLVED_EXTS (.js/.ts type/syntax-dependent, .jsx/.tsx transformed) classify as null, not a format.
19
14
  const NODE_EXT_FORMATS = new Map([
20
15
  ['mjs', 'module'], ['cjs', 'commonjs'], ['json', 'json'],
@@ -28,8 +23,20 @@ export function pathExt(filePath) {
28
23
  return m ? m[1].toLowerCase() : ''
29
24
  }
30
25
 
31
- // A native path as the '/'-joined form every artifact key uses (a no-op off Windows).
32
- export const toPosix = (path) => path.split(/[\\/]/u).join('/')
26
+ // A native path as the '/'-joined form every artifact key uses. Off Windows `\` is no separator but
27
+ // part of a name, and a name holding one is refused rather than taken for another path.
28
+ export function toPosix(path) {
29
+ if (sep === '\\') return path.replaceAll('\\', '/')
30
+ if (path.includes('\\')) throw new Error(`stasis: a path holding '\\' is not supported: ${path}`)
31
+ return path
32
+ }
33
+
34
+ // Whether a path.relative() result climbs out of its base: absolute, or beginning with a `..`
35
+ // SEGMENT -- a bare startsWith('..') would also reject a legitimate `..cache` name.
36
+ export const relativeEscapes = (rel) => rel === '..' || rel.startsWith('../') || rel.startsWith(`..${sep}`) || isAbsolute(rel)
37
+
38
+ // True when `path` is `base` or lies beneath it. Lexical: resolve real paths first where symlinks matter.
39
+ export const isPathWithin = (base, path) => !relativeEscapes(relative(base, path))
33
40
 
34
41
  // Both rules are needed: pathExt('.env.local') is 'local', and the extension rule alone misses `.env.*`.
35
42
  export function isDotEnvFile(name) {
@@ -135,8 +142,7 @@ const CODE_EXT_FORMATS = new Map([
135
142
  ['rs', 'rust'],
136
143
  ['patch', 'patch'],
137
144
  ['java', 'java'],
138
- ['kt', 'kotlin'],
139
- ['kts', 'kotlin'],
145
+ ['kt', 'kotlin'], ['kts', 'kotlin'],
140
146
  ['gradle', 'gradle'],
141
147
  ['m', 'objc'],
142
148
  ['mm', 'objcpp'],
@@ -146,18 +152,14 @@ const CODE_EXT_FORMATS = new Map([
146
152
  ['h', 'c-header'],
147
153
  ['hh', 'cpp-header'], ['hxx', 'cpp-header'], ['hpp', 'cpp-header'], ['h++', 'cpp-header'],
148
154
  ['rb', 'ruby'],
155
+ ['py', 'python'], ['pyi', 'python'], ['pyw', 'python'],
149
156
  ['cmake', 'cmake'],
150
157
  ['podspec', 'podspec'],
151
158
  ['template', 'template'],
152
- ['xml', 'xml'],
153
- ['plist', 'xml'],
154
- ['xcprivacy', 'xml'],
155
- ['xcscheme', 'xml'],
156
- ['storyboard', 'xml'],
157
- ['entitlements', 'xml'],
159
+ ['xml', 'xml'], ['plist', 'xml'], ['xcprivacy', 'xml'], ['xcscheme', 'xml'],
160
+ ['storyboard', 'xml'], ['entitlements', 'xml'], ['xcworkspacedata', 'xml'],
158
161
  ['env', 'env'],
159
162
  ['pbxproj', 'pbxproj'],
160
- ['xcworkspacedata', 'xml'],
161
163
  ])
162
164
 
163
165
  // Code formats by exact (lowercased) basename, for names whose extension is too generic to key
@@ -193,11 +195,9 @@ const NATIVE_EXCLUDE_NAMES = new Set([
193
195
  'gradle-wrapper.properties', // the APP's wrapper drives the build, not a module's own
194
196
  ])
195
197
  export function isExcludedNativeFile(name, { win32 = process.platform === 'win32' } = {}) {
196
- const base = basename(name).toLowerCase()
197
- if (NATIVE_EXCLUDE_NAMES.has(base)) return true
198
+ if (NATIVE_EXCLUDE_NAMES.has(basename(name).toLowerCase())) return true
198
199
  const ext = pathExt(name)
199
- if (NATIVE_EXCLUDE_EXTS.has(ext)) return true
200
- return !win32 && ext === 'bat'
200
+ return NATIVE_EXCLUDE_EXTS.has(ext) || (!win32 && ext === 'bat')
201
201
  }
202
202
 
203
203
  export function isExcludedNativeDir(name, { win32 = process.platform === 'win32' } = {}) {
@@ -287,9 +287,8 @@ export function classifyFormat(name, { content } = {}) {
287
287
  // The Metro native capture's policy view. Returns { action, format }: 'code' (a native build input),
288
288
  // 'skip' (excluded noise, or a JS-family file matched BY EXTENSION -- Metro owns those), else 'resource'.
289
289
  export function classifyNativeCapture(name, { win32 = process.platform === 'win32', content } = {}) {
290
- if (isExcludedNativeFile(name, { win32 })) return { action: 'skip' }
291
290
  // `.env` files carry secrets: an automated capture must never sweep them in.
292
- if (isDotEnvFile(name)) return { action: 'skip' }
291
+ if (isExcludedNativeFile(name, { win32 }) || isDotEnvFile(name)) return { action: 'skip' }
293
292
  const base = basename(name).toLowerCase()
294
293
  // Name-matched code is always a native build input, even when its tag is a Node format the JS
295
294
  // graph would otherwise own.
@@ -298,8 +297,7 @@ export function classifyNativeCapture(name, { win32 = process.platform === 'win3
298
297
  if (byName !== undefined) return { action: 'code', format: byName }
299
298
  if (CODE_EXTENSIONS.has(pathExt(name))) return { action: 'skip' }
300
299
  const format = classifyFormat(name, { content })
301
- if (format !== undefined) return { action: 'code', format }
302
- return { action: 'resource' }
300
+ return format === undefined ? { action: 'resource' } : { action: 'code', format }
303
301
  }
304
302
 
305
303
  // The byte-level half of the native classification, refining what classifyNativeCapture derived from the
@@ -320,11 +318,7 @@ export function isNativeManifest(name) {
320
318
  // podspecs invoke them at pod-install). Project-relative, so react-native's `exports` can't hide them.
321
319
  export const RN_CORE_INCLUDE_FILES = ['sdks/hermes-engine/utils/replace_hermes_version.js']
322
320
 
323
- export function extSetsEqual(a, b) {
324
- if (a.size !== b.size) return false
325
- for (const ext of a) if (!b.has(ext)) return false
326
- return true
327
- }
321
+ export const extSetsEqual = (a, b) => a.size === b.size && [...a].every((ext) => b.has(ext))
328
322
 
329
323
  export const EXECUTE_BITS = 0o111
330
324
 
@@ -332,30 +326,23 @@ export const isExecutableMode = (stats) => stats.isFile() && (stats.mode & EXECU
332
326
 
333
327
  // Tri-state: `undefined` means the mode could NOT be observed (gone mid-run, EACCES, ELOOP, a synthetic
334
328
  // bundle entry). Callers must not read that as "not executable" -- failing to look is not evidence.
335
- export function observeExecutable(abs) {
336
- let stats
337
- try {
338
- stats = statSync(abs, { throwIfNoEntry: false })
339
- } catch {
340
- return undefined
341
- }
342
- if (stats === undefined) return undefined
343
- return isExecutableMode(stats)
329
+ export function observeExecutable(abs, host = diskHost) {
330
+ const stats = host.stat(abs)
331
+ return stats === null ? undefined : isExecutableMode(stats)
344
332
  }
345
333
 
346
334
  // Boolean view for callers with nothing to refute (recording a fresh set from scratch).
347
- export const isExecutableFile = (abs) => observeExecutable(abs) === true
335
+ export const isExecutableFile = (abs, host) => observeExecutable(abs, host) === true
348
336
 
349
337
  // Windows reports no POSIX execute bits, so a capture there records none and must NOT read "no bit" as
350
338
  // "the bit was removed" and strip what a POSIX capture attested.
351
339
  export const canObserveExecuteBits = ({ win32 = process.platform === 'win32' } = {}) => !win32
352
340
 
353
341
  // Throws on a symlink escaping the bundle root: a crafted `link.sh -> /etc/passwd` must not pull an
354
- // external file into an attestable bundle. realpathSync surfaces ENOENT, which loaders treat as "missing".
355
- export function assertRealPathWithinBase(realBase, baseDir, relPath) {
356
- const real = realpathSync(join(baseDir, relPath))
357
- const rel = toPosix(relative(realBase, real))
358
- if (rel.startsWith('..') || isAbsolute(rel)) {
342
+ // external file into an attestable bundle. realpath surfaces ENOENT, which loaders treat as "missing".
343
+ export function assertRealPathWithinBase(realBase, baseDir, relPath, host = diskHost) {
344
+ const real = host.realpath(join(baseDir, relPath))
345
+ if (!isPathWithin(realBase, real)) {
359
346
  throw new Error(`Refusing to follow symlink escaping bundle root: ${relPath} -> ${real}`)
360
347
  }
361
348
  }