|
| 1 | +/** |
| 2 | + * safe-imports.ts - Safe imports for optional React Native dependencies |
| 3 | + * |
| 4 | + * This module provides safe imports for dependencies that are optional peer dependencies. |
| 5 | + * When these dependencies are not installed (e.g., using the package in a web/Next.js context), |
| 6 | + * the imports will still work. However, in React Native contexts where these dependencies are |
| 7 | + * required but not installed, clear error messages will be thrown. |
| 8 | + */ |
| 9 | + |
| 10 | +import { SQLiteCloudError } from './types' |
| 11 | + |
| 12 | +/** |
| 13 | + * Detects if we're running in React Native environment |
| 14 | + */ |
| 15 | +export function isReactNative(): boolean { |
| 16 | + return typeof navigator !== 'undefined' && navigator.product === 'ReactNative' |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Safely imports the URL class from whatwg-url or react-native-url-polyfill |
| 21 | + * In React Native: Uses react-native-url-polyfill (via react-native field mapping) |
| 22 | + * In Web/Node: Uses whatwg-url |
| 23 | + */ |
| 24 | +export function getSafeURL(): typeof import('whatwg-url').URL { |
| 25 | + try { |
| 26 | + // In React Native, Metro bundler will resolve this to react-native-url-polyfill |
| 27 | + // In Web/Node, this will resolve to whatwg-url |
| 28 | + const { URL } = require('whatwg-url') |
| 29 | + return URL |
| 30 | + } catch (error) { |
| 31 | + if (isReactNative()) { |
| 32 | + throw new SQLiteCloudError( |
| 33 | + 'Missing required React Native dependency: react-native-url-polyfill. ' + |
| 34 | + 'Please install it using: npm install react-native-url-polyfill', |
| 35 | + { errorCode: 'ERR_MISSING_DEPENDENCY', cause: error as Error } |
| 36 | + ) |
| 37 | + } |
| 38 | + throw new SQLiteCloudError( |
| 39 | + 'Failed to load URL parser. Please ensure whatwg-url is installed.', |
| 40 | + { errorCode: 'ERR_MISSING_DEPENDENCY', cause: error as Error } |
| 41 | + ) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Safely imports the Buffer class from buffer or @craftzdog/react-native-buffer |
| 47 | + * In React Native: Uses @craftzdog/react-native-buffer (via react-native field mapping) |
| 48 | + * In Web/Node: Uses buffer package |
| 49 | + */ |
| 50 | +export function getSafeBuffer(): typeof import('buffer').Buffer { |
| 51 | + try { |
| 52 | + // In React Native, Metro bundler will resolve this to @craftzdog/react-native-buffer |
| 53 | + // In Web/Node, this will resolve to buffer package |
| 54 | + const { Buffer } = require('buffer') |
| 55 | + return Buffer |
| 56 | + } catch (error) { |
| 57 | + if (isReactNative()) { |
| 58 | + throw new SQLiteCloudError( |
| 59 | + 'Missing required React Native dependency: @craftzdog/react-native-buffer. ' + |
| 60 | + 'Please install it using: npm install @craftzdog/react-native-buffer', |
| 61 | + { errorCode: 'ERR_MISSING_DEPENDENCY', cause: error as Error } |
| 62 | + ) |
| 63 | + } |
| 64 | + throw new SQLiteCloudError( |
| 65 | + 'Failed to load Buffer library. Please ensure buffer package is installed.', |
| 66 | + { errorCode: 'ERR_MISSING_DEPENDENCY', cause: error as Error } |
| 67 | + ) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Safely imports the tls module or react-native-tcp-socket |
| 73 | + * In React Native: Uses react-native-tcp-socket (via react-native field mapping) |
| 74 | + * In Node: Uses native tls module |
| 75 | + * In Browser: Will return null (browser field sets tls to false) |
| 76 | + */ |
| 77 | +export function getSafeTLS(): typeof import('tls') | null { |
| 78 | + try { |
| 79 | + // In React Native, Metro bundler will resolve this to react-native-tcp-socket |
| 80 | + // In Node, this will resolve to native tls module |
| 81 | + // In Browser, the browser field in package.json sets tls to false |
| 82 | + const tls = require('tls') |
| 83 | + if (tls === false || !tls) { |
| 84 | + return null |
| 85 | + } |
| 86 | + return tls |
| 87 | + } catch (error) { |
| 88 | + if (isReactNative()) { |
| 89 | + throw new SQLiteCloudError( |
| 90 | + 'Missing required React Native dependency: react-native-tcp-socket. ' + |
| 91 | + 'Please install it using: npm install react-native-tcp-socket', |
| 92 | + { errorCode: 'ERR_MISSING_DEPENDENCY', cause: error as Error } |
| 93 | + ) |
| 94 | + } |
| 95 | + // In browser context, tls is not available (WebSocket should be used instead) |
| 96 | + return null |
| 97 | + } |
| 98 | +} |
0 commit comments