mirror of
https://github.com/luckyyzh/pi-agent-integrated.git
synced 2026-10-03 02:59:35 +00:00
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import type { AuthContext } from "./types.ts";
|
|
|
|
interface NodeFsModule {
|
|
access(path: string): Promise<void>;
|
|
}
|
|
|
|
interface NodeOsModule {
|
|
homedir(): string;
|
|
}
|
|
|
|
// Variable specifier so browser bundlers do not try to resolve node builtins.
|
|
const importNodeModule = (specifier: string): Promise<unknown> => import(specifier);
|
|
|
|
function getProcessEnv(): Record<string, string | undefined> | undefined {
|
|
const proc = (globalThis as { process?: { env?: Record<string, string | undefined> } }).process;
|
|
return proc?.env;
|
|
}
|
|
|
|
/**
|
|
* Default auth context: env vars from `process.env` (undefined in browsers),
|
|
* file existence via node:fs (always false in browsers).
|
|
*/
|
|
export function defaultProviderAuthContext(): AuthContext {
|
|
return {
|
|
async env(name: string): Promise<string | undefined> {
|
|
const value = getProcessEnv()?.[name];
|
|
return typeof value === "string" && value.trim().length > 0 ? value : undefined;
|
|
},
|
|
|
|
async fileExists(path: string): Promise<boolean> {
|
|
try {
|
|
const fs = (await importNodeModule("node:fs/promises")) as NodeFsModule;
|
|
let resolved = path;
|
|
if (resolved.startsWith("~")) {
|
|
const os = (await importNodeModule("node:os")) as NodeOsModule;
|
|
resolved = os.homedir() + resolved.slice(1);
|
|
}
|
|
await fs.access(resolved);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
},
|
|
};
|
|
}
|