Files
openchamber/packages/ui/src/lib/outsideFileGrants.ts
T

117 lines
3.3 KiB
TypeScript
Raw Normal View History

2026-06-12 18:24:07 +03:00
import { requestExistingFileAccess } from '@/lib/desktop';
import { isFilePathWithinDirectory, normalizeFilePath } from '@/lib/path-utils';
import { getRuntimeKey } from '@/lib/runtime-switch';
2026-06-12 18:24:07 +03:00
type OutsideFileGrantEntry = {
outsideFileGrant: string;
expiresAt: number;
};
const GRANT_RENEWAL_BUFFER_MS = 5_000;
const grantsByCacheKey = new Map<string, OutsideFileGrantEntry>();
const pendingGrantsByCacheKey = new Map<string, Promise<string | undefined>>();
const grantCacheKey = (path: string, runtimeKey = getRuntimeKey()): string => `${runtimeKey}\0${path}`;
2026-06-12 18:24:07 +03:00
export const getOutsideFileGrant = (path: string): string | undefined => {
const normalizedPath = normalizeFilePath(path);
if (!normalizedPath) {
return undefined;
}
const cacheKey = grantCacheKey(normalizedPath);
const entry = grantsByCacheKey.get(cacheKey);
2026-06-12 18:24:07 +03:00
if (!entry) {
return undefined;
}
if (entry.expiresAt <= Date.now()) {
grantsByCacheKey.delete(cacheKey);
2026-06-12 18:24:07 +03:00
return undefined;
}
return entry.outsideFileGrant;
};
const rememberOutsideFileGrant = (
2026-06-12 18:24:07 +03:00
path: string,
outsideFileGrant: string,
expiresAt: number,
runtimeKey: string,
2026-06-12 18:24:07 +03:00
): void => {
const normalizedPath = normalizeFilePath(path);
if (!normalizedPath || !outsideFileGrant) {
return;
}
grantsByCacheKey.set(grantCacheKey(normalizedPath, runtimeKey), {
2026-06-12 18:24:07 +03:00
outsideFileGrant,
expiresAt: expiresAt - GRANT_RENEWAL_BUFFER_MS,
2026-06-12 18:24:07 +03:00
});
};
export const ensureOutsideFileGrantForDesktop = async (
path: string,
workspaceRoot: string,
): Promise<string | undefined> => {
const normalizedPath = normalizeFilePath(path);
if (!normalizedPath || !workspaceRoot || isFilePathWithinDirectory(normalizedPath, workspaceRoot)) {
return undefined;
}
const runtimeKey = getRuntimeKey();
if (runtimeKey !== 'local') {
return undefined;
}
const cacheKey = grantCacheKey(normalizedPath, runtimeKey);
2026-06-12 18:24:07 +03:00
const existing = getOutsideFileGrant(normalizedPath);
if (existing) {
return existing;
}
const pending = pendingGrantsByCacheKey.get(cacheKey);
if (pending) {
return pending;
}
const request = requestExistingFileAccess(normalizedPath).then((result) => {
if (!result.success || getRuntimeKey() !== runtimeKey) {
return undefined;
}
const { path: grantedPath, outsideFileGrant, expiresAt } = result;
if (expiresAt <= Date.now() + GRANT_RENEWAL_BUFFER_MS) {
return undefined;
}
rememberOutsideFileGrant(grantedPath, outsideFileGrant, expiresAt, runtimeKey);
if (normalizeFilePath(grantedPath) !== normalizedPath) {
rememberOutsideFileGrant(normalizedPath, outsideFileGrant, expiresAt, runtimeKey);
}
return outsideFileGrant;
});
pendingGrantsByCacheKey.set(cacheKey, request);
try {
return await request;
} finally {
pendingGrantsByCacheKey.delete(cacheKey);
2026-06-12 18:24:07 +03:00
}
};
2026-06-12 18:24:07 +03:00
export const resolveOutsideFileReadOptions = async (
path: string,
workspaceRoot: string,
enabled: boolean,
): Promise<{ allowOutsideWorkspace: boolean; outsideFileGrant?: string }> => {
const allowOutsideWorkspace = enabled
&& Boolean(workspaceRoot)
&& !isFilePathWithinDirectory(path, workspaceRoot);
if (!allowOutsideWorkspace) {
return { allowOutsideWorkspace: false };
2026-06-12 18:24:07 +03:00
}
return {
allowOutsideWorkspace: true,
outsideFileGrant: await ensureOutsideFileGrantForDesktop(path, workspaceRoot),
};
2026-06-12 18:24:07 +03:00
};