94 行
2.4 KiB
TypeScript
94 行
2.4 KiB
TypeScript
|
|
/**
|
|||
|
|
* XuqmBundleModule JS 封装。
|
|||
|
|
*
|
|||
|
|
* 提供 bundle 文件的原生管理能力:
|
|||
|
|
* - 读取/写入/删除 bundle 文件
|
|||
|
|
* - manifest 管理
|
|||
|
|
* - bundle 路径查询
|
|||
|
|
*
|
|||
|
|
* 注意:bundle 注入到运行中的 RN host(loadBundle)
|
|||
|
|
* 由宿主的 BundleRuntime 负责(RN 0.84 不暴露公开 API)。
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
import {NativeModules, Platform} from 'react-native'
|
|||
|
|
|
|||
|
|
interface XuqmBundleModuleInterface {
|
|||
|
|
bundleExists(moduleId: string): Promise<boolean>
|
|||
|
|
readBundle(moduleId: string): Promise<string>
|
|||
|
|
writeBundle(moduleId: string, source: string, md5: string): Promise<boolean>
|
|||
|
|
deleteBundle(moduleId: string): Promise<boolean>
|
|||
|
|
getBundlePath(moduleId: string): Promise<string>
|
|||
|
|
getManifest(): Promise<string>
|
|||
|
|
writeManifest(manifestJson: string): Promise<boolean>
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function getModule(): XuqmBundleModuleInterface {
|
|||
|
|
const mod = NativeModules.XuqmBundleModule
|
|||
|
|
if (!mod) {
|
|||
|
|
throw new Error('[XuqmBundleModule] Native module not available')
|
|||
|
|
}
|
|||
|
|
return mod as XuqmBundleModuleInterface
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export const NativeBundle = {
|
|||
|
|
/**
|
|||
|
|
* 检查指定 bundle 文件是否存在。
|
|||
|
|
*/
|
|||
|
|
async exists(moduleId: string): Promise<boolean> {
|
|||
|
|
return getModule().bundleExists(moduleId)
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 读取 bundle 文件内容(JS 源码文本)。
|
|||
|
|
*/
|
|||
|
|
async read(moduleId: string): Promise<string> {
|
|||
|
|
return getModule().readBundle(moduleId)
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 将 bundle 源码写入本地文件。
|
|||
|
|
*
|
|||
|
|
* @param moduleId 插件 ID
|
|||
|
|
* @param source JS 源码文本
|
|||
|
|
* @param md5 文件 MD5 校验值
|
|||
|
|
*/
|
|||
|
|
async write(moduleId: string, source: string, md5: string): Promise<void> {
|
|||
|
|
await getModule().writeBundle(moduleId, source, md5)
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 删除指定 bundle 文件。
|
|||
|
|
*/
|
|||
|
|
async remove(moduleId: string): Promise<void> {
|
|||
|
|
await getModule().deleteBundle(moduleId)
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取 bundle 文件的绝对路径。
|
|||
|
|
*/
|
|||
|
|
async getPath(moduleId: string): Promise<string> {
|
|||
|
|
return getModule().getBundlePath(moduleId)
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 读取 manifest.json 内容。
|
|||
|
|
*/
|
|||
|
|
async getManifest(): Promise<string> {
|
|||
|
|
return getModule().getManifest()
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 写入 manifest.json。
|
|||
|
|
*/
|
|||
|
|
async writeManifest(manifestJson: string): Promise<void> {
|
|||
|
|
await getModule().writeManifest(manifestJson)
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取 bundle 文件名(平台相关)。
|
|||
|
|
*/
|
|||
|
|
getFileName(moduleId: string): string {
|
|||
|
|
return `${moduleId}.${Platform.OS}.bundle`
|
|||
|
|
},
|
|||
|
|
}
|