@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
1 lines • 5.74 kB
Source Map (JSON)
{"version":3,"sources":["../../src/platform/utils.ts"],"sourcesContent":["/**\n * Platform detection and adapter utilities\n *\n * This module provides utilities for detecting the current runtime environment\n * and creating appropriate platform adapters automatically.\n */\n\nimport type { VanaPlatformAdapter, PlatformType } from \"./interface\";\n\n/**\n * Detects the current runtime environment\n *\n * @returns The detected platform type\n */\nexport function detectPlatform(): PlatformType {\n // Check for Node.js environment\n if (\n typeof process !== \"undefined\" &&\n process.versions &&\n process.versions.node\n ) {\n return \"node\";\n }\n\n // Check for browser environment\n if (typeof window !== \"undefined\" && typeof document !== \"undefined\") {\n return \"browser\";\n }\n\n // Default to Node.js if we can't determine (e.g., SSR environments)\n return \"node\";\n}\n\n/**\n * Creates the appropriate platform adapter based on the current environment\n *\n * @returns A platform adapter instance for the current environment\n * @throws {Error} If platform adapters cannot be imported or created\n */\nexport async function createPlatformAdapter(): Promise<VanaPlatformAdapter> {\n const platform = detectPlatform();\n\n try {\n if (platform === \"node\") {\n // Check if we're in a browser environment\n if (typeof window !== \"undefined\") {\n throw new Error(\n \"NodePlatformAdapter is not available in browser environments. Use BrowserPlatformAdapter instead.\",\n );\n }\n\n // Use string concatenation to avoid static analysis\n const moduleName = \"./node\";\n const { NodePlatformAdapter } = await import(moduleName);\n return new NodePlatformAdapter();\n } else {\n const { BrowserPlatformAdapter } = await import(\"./browser\");\n return new BrowserPlatformAdapter();\n }\n } catch (error) {\n throw new Error(\n `Failed to create platform adapter for ${platform}: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n );\n }\n}\n\n/**\n * Creates a platform adapter for a specific platform type\n *\n * @param platformType - The platform type to create an adapter for\n * @returns A platform adapter instance for the specified platform\n * @throws {Error} If platform adapters cannot be imported or created\n */\nexport async function createPlatformAdapterFor(\n platformType: PlatformType,\n): Promise<VanaPlatformAdapter> {\n try {\n if (platformType === \"node\") {\n // Check if we're in a browser environment\n if (typeof window !== \"undefined\") {\n throw new Error(\n \"NodePlatformAdapter is not available in browser environments. Use BrowserPlatformAdapter instead.\",\n );\n }\n\n // Use string concatenation to avoid static analysis\n const moduleName = \"./node\";\n const { NodePlatformAdapter } = await import(moduleName);\n return new NodePlatformAdapter();\n } else {\n const { BrowserPlatformAdapter } = await import(\"./browser\");\n return new BrowserPlatformAdapter();\n }\n } catch (error) {\n throw new Error(\n `Failed to create platform adapter for ${platformType}: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n );\n }\n}\n\n/**\n * Checks if the current environment supports the given platform adapter\n *\n * @param platformType - The platform type to check\n * @returns True if the platform is supported, false otherwise\n */\nexport function isPlatformSupported(platformType: PlatformType): boolean {\n const currentPlatform = detectPlatform();\n return currentPlatform === platformType;\n}\n\n/**\n * Gets platform-specific capabilities\n *\n * @returns Object describing available platform capabilities\n */\nexport function getPlatformCapabilities() {\n const platform = detectPlatform();\n\n return {\n platform,\n crypto: {\n webCrypto: typeof crypto !== \"undefined\" && crypto.subtle,\n nodeCrypto:\n typeof process !== \"undefined\" &&\n process.versions &&\n process.versions.node,\n },\n fetch:\n typeof fetch !== \"undefined\" || typeof globalThis.fetch !== \"undefined\",\n streams: typeof ReadableStream !== \"undefined\",\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAcO,SAAS,iBAA+B;AAE7C,MACE,OAAO,YAAY,eACnB,QAAQ,YACR,QAAQ,SAAS,MACjB;AACA,WAAO;AAAA,EACT;AAGA,MAAI,OAAO,WAAW,eAAe,OAAO,aAAa,aAAa;AACpE,WAAO;AAAA,EACT;AAGA,SAAO;AACT;AAQA,eAAsB,wBAAsD;AAC1E,QAAM,WAAW,eAAe;AAEhC,MAAI;AACF,QAAI,aAAa,QAAQ;AAEvB,UAAI,OAAO,WAAW,aAAa;AACjC,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;A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