Something went wrong. Try again.
A reusable desktop shell: your sidecar, your index, safe APIs, installers for every platform
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940// Path checks for the bridge: a page only reaches files under the app's roots// or files the user picked in a dialog this session. Symlinks are resolved on// both sides so a link out of a root does not get through.import { existsSync, realpathSync } from 'node:fs';import { homedir } from 'node:os';import { basename, dirname, join, resolve, sep } from 'node:path';
export function expandHome (p, home = homedir()) { if (p === '~') return home; if (p.startsWith('~/') || p.startsWith('~\\')) return join(home, p.slice(2)); return p;}
// The real, absolute form of `p` even when it does not exist yet: the longest// existing ancestor goes through realpath and the rest is appended.export function realish (p) { let existing = resolve(p); const rest = []; while (!existsSync(existing)) { const parent = dirname(existing); if (parent === existing) break; rest.unshift(basename(existing)); existing = parent; } return join(realpathSync(existing), ...rest);}
export function within (p, root) { return p === root || p.startsWith(root.endsWith(sep) ? root : root + sep);}
// Returns the real path when `p` sits under one of `roots` (configured) or// `grants` (picked in a dialog); throws otherwise.export function allowedPath (p, { roots = [], grants = [] } = {}) { if (typeof p !== 'string' || !p) throw new TypeError('A path is required'); const real = realish(expandHome(p)); for (const r of [...roots, ...grants]) if (within(real, realish(expandHome(r)))) return real; throw new Error(`Not allowed: ${p}`);}