import { readFile } from 'node:fs/promises'; import { join } from 'node:path'; export const MEOWA_RELEASE_CREDENTIAL_FILE_NAME = 'meowa-game-assets-credential.json'; const EMBEDDED_MEOWA_API_KEY = 'ma_live_mY19lufmkTJNULToKGUH2yKJX0YmKHhg'; export function readEmbeddedMeowaApiKey(): string | null { const apiKey = EMBEDDED_MEOWA_API_KEY.trim(); return apiKey && apiKey.length <= 512 ? apiKey : null; } type JsonRecord = Record; function asRecord(value: unknown): JsonRecord | null { return value && typeof value === 'object' && !Array.isArray(value) ? value as JsonRecord : null; } export function getMeowaReleaseCredentialPath(resourcesPath: string): string { return join(resourcesPath, 'resources', MEOWA_RELEASE_CREDENTIAL_FILE_NAME); } /** * Read the release-only credential staged by electron-builder's afterPack hook. * This file is intentionally not part of the source tree; it exists only in a * packaged artifact built with MEOWART_API_KEY present in the release env. */ export async function readBundledMeowaApiKey(resourcesPath = process.resourcesPath): Promise { if (typeof resourcesPath !== 'string' || !resourcesPath.trim()) return null; try { const raw = await readFile(getMeowaReleaseCredentialPath(resourcesPath), 'utf8'); const record = asRecord(JSON.parse(raw) as unknown); if (!record) return null; if (record.schemaVersion !== 1) return null; const apiKey = typeof record.apiKey === 'string' ? record.apiKey.trim() : ''; return apiKey && apiKey.length <= 512 ? apiKey : null; } catch { return null; } }