everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
1 lines • 4.44 kB
Source Map (JSON)
{"version":3,"file":"auth-session.cjs","names":["join","existsSync","readFileSync","homedir"],"sources":["../src/auth-session.ts"],"sourcesContent":["import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\n\nexport interface SessionCredential {\n kind: \"session\";\n apiKey: string;\n apiKeyId: string;\n accountId: string | null;\n label: string;\n siteUrl: string;\n createdAt: string;\n expiresAt: string | null;\n}\n\nexport interface PublishKeyRecord {\n publicKey: string;\n network: \"mainnet\" | \"testnet\";\n contract: string;\n exportedTo: string;\n}\n\nexport interface DelegateKeyRecord {\n publicKey: string;\n privateKey: string;\n accountId: string;\n network: \"mainnet\" | \"testnet\";\n contract: string;\n mintedAt: string;\n}\n\nexport interface BosSessionFile {\n version: 1;\n credential: SessionCredential | null;\n publishKey: PublishKeyRecord | null;\n delegateKey: DelegateKeyRecord | null;\n}\n\nconst SESSION_FILE_NAME = \"session.json\";\n\nexport function getSessionFilePath(configDir: string): string {\n return join(configDir, \".bos\", SESSION_FILE_NAME);\n}\n\nexport function readSessionHandle(configDir: string): BosSessionFile | null {\n const filePath = getSessionFilePath(configDir);\n if (!existsSync(filePath)) return null;\n try {\n const parsed = JSON.parse(readFileSync(filePath, \"utf-8\")) as Partial<BosSessionFile>;\n if (parsed.version !== 1) return null;\n return {\n version: 1,\n credential: parsed.credential ?? null,\n publishKey: parsed.publishKey ?? null,\n delegateKey: parsed.delegateKey ?? null,\n };\n } catch {\n return null;\n }\n}\n\nexport function writeSessionHandle(configDir: string, session: BosSessionFile): void {\n const bosDir = join(configDir, \".bos\");\n mkdirSync(bosDir, { recursive: true });\n writeFileSync(getSessionFilePath(configDir), `${JSON.stringify(session, null, 2)}\\n`, {\n mode: 0o600,\n });\n}\n\nexport function updateSessionHandle(\n configDir: string,\n update: (session: BosSessionFile) => BosSessionFile,\n): BosSessionFile {\n const current = readSessionHandle(configDir) ?? {\n version: 1 as const,\n credential: null,\n publishKey: null,\n delegateKey: null,\n };\n const next = update(current);\n writeSessionHandle(configDir, next);\n return next;\n}\n\nexport function deleteSessionHandle(configDir: string): void {\n const filePath = getSessionFilePath(configDir);\n if (existsSync(filePath)) {\n rmSync(filePath);\n }\n}\n\nexport function nearCredentialsPath(network: \"mainnet\" | \"testnet\", account: string): string {\n return join(homedir(), \".near-credentials\", network, `${account}.json`);\n}\n\nexport function removePublishedKeyFile(\n network: \"mainnet\" | \"testnet\",\n account: string,\n publicKey: string,\n): void {\n const filePath = nearCredentialsPath(network, account);\n if (!existsSync(filePath)) return;\n try {\n const credential = JSON.parse(readFileSync(filePath, \"utf-8\")) as { public_key?: string };\n if (credential.public_key === publicKey) {\n rmSync(filePath);\n }\n } catch {\n // leave malformed files alone\n }\n}\n"],"mappings":";;;;;AAsCA,MAAM,oBAAoB;AAE1B,SAAgB,mBAAmB,WAA2B;CAC5D,WAAOA,gBAAK,WAAW,QAAQ,iBAAiB;AAClD;AAEA,SAAgB,kBAAkB,WAA0C;CAC1E,MAAM,WAAW,mBAAmB,SAAS;CAC7C,IAAI,KAACC,oBAAW,QAAQ,GAAG,OAAO;CAClC,IAAI;EACF,MAAM,SAAS,KAAK,UAAMC,sBAAa,UAAU,OAAO,CAAC;EACzD,IAAI,OAAO,YAAY,GAAG,OAAO;EACjC,OAAO;GACL,SAAS;GACT,YAAY,OAAO,cAAc;GACjC,YAAY,OAAO,cAAc;GACjC,aAAa,OAAO,eAAe;EACrC;CACF,QAAQ;EACN,OAAO;CACT;AACF;AAEA,SAAgB,mBAAmB,WAAmB,SAA+B;CACnF,MAAM,aAASF,gBAAK,WAAW,MAAM;CACrC,uBAAU,QAAQ,EAAE,WAAW,KAAK,CAAC;CACrC,2BAAc,mBAAmB,SAAS,GAAG,GAAG,KAAK,UAAU,SAAS,MAAM,CAAC,EAAE,KAAK,EACpF,MAAM,IACR,CAAC;AACH;AAEA,SAA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