2026-06-15 01:44:20 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* SDK 自动初始化模块。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 对齐 Android SDK 的 ContentProvider 模式。
|
|
|
|
|
|
*
|
|
|
|
|
|
* React Native 没有 ContentProvider,所以用 Metro moduleNameMapper 桥接:
|
|
|
|
|
|
* 1. 宿主把加密配置放到 src/assets/xuqm/config.xuqmconfig.ts
|
|
|
|
|
|
* 2. 宿主 babel.config.js 添加 alias: '@xuqm/autoinit-config' → 该文件
|
|
|
|
|
|
* 3. 本模块 tryRequire('@xuqm/autoinit-config') 自动读取并初始化
|
|
|
|
|
|
* 4. 宿主代码零初始化调用
|
|
|
|
|
|
*
|
|
|
|
|
|
* 如果 alias 未配置(require 失败),静默跳过,不崩溃。
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import {isInitialized} from './config'
|
|
|
|
|
|
import {XuqmSDK} from './sdk'
|
|
|
|
|
|
|
|
|
|
|
|
let _autoInitAttempted = false
|
|
|
|
|
|
|
|
|
|
|
|
function tryRequireConfig(): string | null {
|
|
|
|
|
|
try {
|
|
|
|
|
|
// Metro moduleNameMapper 将此路径映射到宿主的配置文件
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
|
|
|
|
const mod = require('@xuqm/autoinit-config') as {ENCRYPTED_CONFIG?: string; default?: string}
|
|
|
|
|
|
return mod?.ENCRYPTED_CONFIG ?? mod?.default ?? null
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
return null
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 尝试自动初始化。幂等,只执行一次。
|
|
|
|
|
|
* 失败时静默跳过(对齐 Android SDK 的 runCatching + Log.w 行为)。
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function tryAutoInit(): void {
|
|
|
|
|
|
if (_autoInitAttempted) return
|
|
|
|
|
|
_autoInitAttempted = true
|
|
|
|
|
|
if (isInitialized()) return
|
|
|
|
|
|
|
|
|
|
|
|
const encrypted = tryRequireConfig()
|
|
|
|
|
|
if (!encrypted) return
|
|
|
|
|
|
|
2026-06-16 12:01:44 +08:00
|
|
|
|
XuqmSDK.initWithConfigFile(encrypted, {debug: __DEV__}).catch((e: unknown) => {
|
|
|
|
|
|
console.error('[XuqmSDK] Auto-init failed:', e)
|
|
|
|
|
|
if (__DEV__) throw e
|
2026-06-15 01:44:20 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|