UNPKG

@sqlitecloud/drivers

Version:

SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients

88 lines (87 loc) 3.88 kB
"use strict"; /** * safe-imports.ts - Safe imports for optional React Native dependencies * * This module provides safe imports for dependencies that are optional peer dependencies. * When these dependencies are not installed (e.g., using the package in a web/Next.js context), * the imports will still work. However, in React Native contexts where these dependencies are * required but not installed, clear error messages will be thrown. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.isReactNative = isReactNative; exports.getSafeURL = getSafeURL; exports.getSafeBuffer = getSafeBuffer; exports.getSafeTLS = getSafeTLS; const types_1 = require("./types"); /** * Detects if we're running in React Native environment */ function isReactNative() { return typeof navigator !== 'undefined' && navigator.product === 'ReactNative'; } /** * Safely imports the URL class from whatwg-url or react-native-url-polyfill * In React Native: Uses react-native-url-polyfill (via react-native field mapping) * In Web/Node: Uses whatwg-url */ function getSafeURL() { try { // In React Native, Metro bundler will resolve this to react-native-url-polyfill // In Web/Node, this will resolve to whatwg-url const { URL } = require('whatwg-url'); return URL; } catch (error) { if (isReactNative()) { throw new types_1.SQLiteCloudError('Missing required React Native dependency: react-native-url-polyfill. ' + 'Please install it using: npm install react-native-url-polyfill', { errorCode: 'ERR_MISSING_DEPENDENCY', cause: error }); } throw new types_1.SQLiteCloudError('Failed to load URL parser. Please ensure whatwg-url is installed.', { errorCode: 'ERR_MISSING_DEPENDENCY', cause: error }); } } /** * Safely imports the Buffer class from buffer or @craftzdog/react-native-buffer * In React Native: Uses @craftzdog/react-native-buffer (via react-native field mapping) * In Web/Node: Uses buffer package */ function getSafeBuffer() { try { // In React Native, Metro bundler will resolve this to @craftzdog/react-native-buffer // In Web/Node, this will resolve to buffer package const { Buffer } = require('buffer'); return Buffer; } catch (error) { if (isReactNative()) { throw new types_1.SQLiteCloudError('Missing required React Native dependency: @craftzdog/react-native-buffer. ' + 'Please install it using: npm install @craftzdog/react-native-buffer', { errorCode: 'ERR_MISSING_DEPENDENCY', cause: error }); } throw new types_1.SQLiteCloudError('Failed to load Buffer library. Please ensure buffer package is installed.', { errorCode: 'ERR_MISSING_DEPENDENCY', cause: error }); } } /** * Safely imports the tls module or react-native-tcp-socket * In React Native: Uses react-native-tcp-socket (via react-native field mapping) * In Node: Uses native tls module * In Browser: Will return null (browser field sets tls to false) */ function getSafeTLS() { try { // In React Native, Metro bundler will resolve this to react-native-tcp-socket // In Node, this will resolve to native tls module // In Browser, the browser field in package.json sets tls to false const tls = require('tls'); if (tls === false || !tls) { return null; } return tls; } catch (error) { if (isReactNative()) { throw new types_1.SQLiteCloudError('Missing required React Native dependency: react-native-tcp-socket. ' + 'Please install it using: npm install react-native-tcp-socket', { errorCode: 'ERR_MISSING_DEPENDENCY', cause: error }); } // In browser context, tls is not available (WebSocket should be used instead) return null; } }