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

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'],
@@ -149,15 +155,10 @@ const CODE_EXT_FORMATS = new Map([
149
155
  ['cmake', 'cmake'],
150
156
  ['podspec', 'podspec'],
151
157
  ['template', 'template'],
152
- ['xml', 'xml'],
153
- ['plist', 'xml'],
154
- ['xcprivacy', 'xml'],
155
- ['xcscheme', 'xml'],
156
- ['storyboard', 'xml'],
157
- ['entitlements', 'xml'],
158
+ ['xml', 'xml'], ['plist', 'xml'], ['xcprivacy', 'xml'], ['xcscheme', 'xml'],
159
+ ['storyboard', 'xml'], ['entitlements', 'xml'], ['xcworkspacedata', 'xml'],
158
160
  ['env', 'env'],
159
161
  ['pbxproj', 'pbxproj'],
160
- ['xcworkspacedata', 'xml'],
161
162
  ])
162
163
 
163
164
  // Code formats by exact (lowercased) basename, for names whose extension is too generic to key
@@ -193,11 +194,9 @@ const NATIVE_EXCLUDE_NAMES = new Set([
193
194
  'gradle-wrapper.properties', // the APP's wrapper drives the build, not a module's own
194
195
  ])
195
196
  export function isExcludedNativeFile(name, { win32 = process.platform === 'win32' } = {}) {
196
- const base = basename(name).toLowerCase()
197
- if (NATIVE_EXCLUDE_NAMES.has(base)) return true
197
+ if (NATIVE_EXCLUDE_NAMES.has(basename(name).toLowerCase())) return true
198
198
  const ext = pathExt(name)
199
- if (NATIVE_EXCLUDE_EXTS.has(ext)) return true
200
- return !win32 && ext === 'bat'
199
+ return NATIVE_EXCLUDE_EXTS.has(ext) || (!win32 && ext === 'bat')
201
200
  }
202
201
 
203
202
  export function isExcludedNativeDir(name, { win32 = process.platform === 'win32' } = {}) {
@@ -287,9 +286,8 @@ export function classifyFormat(name, { content } = {}) {
287
286
  // The Metro native capture's policy view. Returns { action, format }: 'code' (a native build input),
288
287
  // 'skip' (excluded noise, or a JS-family file matched BY EXTENSION -- Metro owns those), else 'resource'.
289
288
  export function classifyNativeCapture(name, { win32 = process.platform === 'win32', content } = {}) {
290
- if (isExcludedNativeFile(name, { win32 })) return { action: 'skip' }
291
289
  // `.env` files carry secrets: an automated capture must never sweep them in.
292
- if (isDotEnvFile(name)) return { action: 'skip' }
290
+ if (isExcludedNativeFile(name, { win32 }) || isDotEnvFile(name)) return { action: 'skip' }
293
291
  const base = basename(name).toLowerCase()
294
292
  // Name-matched code is always a native build input, even when its tag is a Node format the JS
295
293
  // graph would otherwise own.
@@ -298,8 +296,7 @@ export function classifyNativeCapture(name, { win32 = process.platform === 'win3
298
296
  if (byName !== undefined) return { action: 'code', format: byName }
299
297
  if (CODE_EXTENSIONS.has(pathExt(name))) return { action: 'skip' }
300
298
  const format = classifyFormat(name, { content })
301
- if (format !== undefined) return { action: 'code', format }
302
- return { action: 'resource' }
299
+ return format === undefined ? { action: 'resource' } : { action: 'code', format }
303
300
  }
304
301
 
305
302
  // The byte-level half of the native classification, refining what classifyNativeCapture derived from the
@@ -320,11 +317,7 @@ export function isNativeManifest(name) {
320
317
  // podspecs invoke them at pod-install). Project-relative, so react-native's `exports` can't hide them.
321
318
  export const RN_CORE_INCLUDE_FILES = ['sdks/hermes-engine/utils/replace_hermes_version.js']
322
319
 
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
- }
320
+ export const extSetsEqual = (a, b) => a.size === b.size && [...a].every((ext) => b.has(ext))
328
321
 
329
322
  export const EXECUTE_BITS = 0o111
330
323
 
@@ -332,30 +325,23 @@ export const isExecutableMode = (stats) => stats.isFile() && (stats.mode & EXECU
332
325
 
333
326
  // Tri-state: `undefined` means the mode could NOT be observed (gone mid-run, EACCES, ELOOP, a synthetic
334
327
  // 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)
328
+ export function observeExecutable(abs, host = diskHost) {
329
+ const stats = host.stat(abs)
330
+ return stats === null ? undefined : isExecutableMode(stats)
344
331
  }
345
332
 
346
333
  // Boolean view for callers with nothing to refute (recording a fresh set from scratch).
347
- export const isExecutableFile = (abs) => observeExecutable(abs) === true
334
+ export const isExecutableFile = (abs, host) => observeExecutable(abs, host) === true
348
335
 
349
336
  // Windows reports no POSIX execute bits, so a capture there records none and must NOT read "no bit" as
350
337
  // "the bit was removed" and strip what a POSIX capture attested.
351
338
  export const canObserveExecuteBits = ({ win32 = process.platform === 'win32' } = {}) => !win32
352
339
 
353
340
  // 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)) {
341
+ // external file into an attestable bundle. realpath surfaces ENOENT, which loaders treat as "missing".
342
+ export function assertRealPathWithinBase(realBase, baseDir, relPath, host = diskHost) {
343
+ const real = host.realpath(join(baseDir, relPath))
344
+ if (!isPathWithin(realBase, real)) {
359
345
  throw new Error(`Refusing to follow symlink escaping bundle root: ${relPath} -> ${real}`)
360
346
  }
361
347
  }