1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-10-28 20:13:10 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

fix filejsbundleprovider

This commit is contained in:
sunnylqm
2025-10-24 22:09:05 +08:00
parent 6e4f432e26
commit 268f39f43b
3 changed files with 99 additions and 82 deletions

View File

@@ -1,49 +1,41 @@
import { HotReloadConfig, JSBundleProvider, JSBundleProviderError, JSPackagerClientConfig } from '@rnoh/react-native-openharmony'; import {
import fileIo from '@ohos.file.fs'; FileJSBundle,
HotReloadConfig,
JSBundleProvider,
JSBundleProviderError
} from '@rnoh/react-native-openharmony';
import common from '@ohos.app.ability.common'; import common from '@ohos.app.ability.common';
import fs from '@ohos.file.fs';
import { UpdateContext } from './UpdateContext'; import { UpdateContext } from './UpdateContext';
export class PushyFileJSBundleProvider extends JSBundleProvider { export class PushyFileJSBundleProvider extends JSBundleProvider {
private updateContext: UpdateContext; private updateContext: UpdateContext;
private filePath: string = '' private path: string = ''
constructor(context: common.UIAbilityContext) { constructor(context: common.UIAbilityContext) {
super(); super();
this.updateContext = new UpdateContext(context); this.updateContext = new UpdateContext(context);
this.path = this.updateContext.getBundleUrl();
} }
getURL(): string { getURL(): string {
return this.updateContext.getBundleUrl()?.substring(1); return this.path;
} }
async getBundle(): Promise<ArrayBuffer> { async getBundle(): Promise<FileJSBundle> {
try { try {
this.filePath = this.updateContext.getBundleUrl(); const status = await fs.access(this.path, fs.OpenMode.READ_ONLY);
const res = fileIo.accessSync(this.filePath); if (status) {
if (res) { return {
const file = fileIo.openSync(this.filePath, fileIo.OpenMode.READ_ONLY); filePath: this.path
try {
const stat = await fileIo.stat(this.filePath);
const fileSize = stat.size;
const buffer = new ArrayBuffer(fileSize);
const bytesRead = fileIo.readSync(file.fd, buffer, {
offset: 0,
length: fileSize
});
if (bytesRead !== fileSize) {
throw new Error(`Failed to read entire file: read ${bytesRead} of ${fileSize} bytes`);
}
return buffer;
} finally {
fileIo.closeSync(file.fd);
} }
} }
throw new Error('Update bundle not found'); throw new Error('Update bundle not found');
} catch (error) { } catch (error) {
throw new JSBundleProviderError({ throw new JSBundleProviderError({
whatHappened: `Couldn't load JSBundle from ${this.filePath}`, whatHappened: `Couldn't load JSBundle from ${this.path}`,
extraData: error, extraData: error,
howCanItBeFixed: [`Check if a bundle exists at "${this.filePath}" on your device.`] howCanItBeFixed: [`Check if a bundle exists at "${this.path}" on your device.`]
}) })
} }
} }

View File

@@ -1,9 +1,10 @@
import { TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts'; import {
TurboModule,
TurboModuleContext,
} from '@rnoh/react-native-openharmony/ts';
import common from '@ohos.app.ability.common'; import common from '@ohos.app.ability.common';
import dataPreferences from '@ohos.data.preferences'; import dataPreferences from '@ohos.data.preferences';
import { bundleManager } from '@kit.AbilityKit'; import { bundleManager } from '@kit.AbilityKit';
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
import { BusinessError } from '@ohos.base';
import logger from './Logger'; import logger from './Logger';
import { UpdateModuleImpl } from './UpdateModuleImpl'; import { UpdateModuleImpl } from './UpdateModuleImpl';
import { UpdateContext } from './UpdateContext'; import { UpdateContext } from './UpdateContext';
@@ -23,50 +24,60 @@ export class PushyTurboModule extends TurboModule {
EventHub.getInstance().setRNInstance(ctx.rnInstance); EventHub.getInstance().setRNInstance(ctx.rnInstance);
} }
getConstants(): Object {
logger.debug(TAG, ',call getConstants');
const context = this.mUiCtx;
const preferencesManager = dataPreferences.getPreferencesSync(context, {
name: 'update',
});
const isFirstTime = preferencesManager.getSync(
'isFirstTime',
false,
) as boolean;
const rolledBackVersion = preferencesManager.getSync(
'rolledBackVersion',
'',
) as string;
const uuid = preferencesManager.getSync('uuid', '') as string;
const currentVersion = preferencesManager.getSync(
'currentVersion',
'',
) as string;
const currentVersionInfo = this.context.getKv(`hash_${currentVersion}`);
const buildTime = preferencesManager.getSync('buildTime', '') as string;
const isUsingBundleUrl = this.context.getIsUsingBundleUrl();
let bundleFlags =
bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
let packageVersion = '';
try {
const bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleFlags);
packageVersion = bundleInfo?.versionName || 'Unknown';
} catch (error) {
console.error('Failed to get bundle info:', error);
}
getConstants(): Object { if (isFirstTime) {
logger.debug(TAG, ',call getConstants'); preferencesManager.deleteSync('isFirstTime');
const context = this.mUiCtx; }
const preferencesManager = dataPreferences.getPreferencesSync(context,{ name: 'update' });
const isFirstTime = preferencesManager.getSync('isFirstTime', false) as boolean; if (rolledBackVersion) {
const rolledBackVersion = preferencesManager.getSync('rolledBackVersion', '') as string; preferencesManager.deleteSync('rolledBackVersion');
const uuid = preferencesManager.getSync('uuid', '') as string; }
const currentVersion = preferencesManager.getSync('currentVersion', '') as string;
const currentVersionInfo = this.context.getKv(`hash_${currentVersion}`); return {
const buildTime = preferencesManager.getSync('buildTime', '') as string; downloadRootDir: `${context.filesDir}/_update`,
const isUsingBundleUrl = this.context.getIsUsingBundleUrl(); currentVersionInfo,
let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION; packageVersion,
let packageVersion = ''; currentVersion,
try { buildTime,
const bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleFlags); isUsingBundleUrl,
packageVersion = bundleInfo?.versionName || 'Unknown'; isFirstTime,
} catch (error) { rolledBackVersion,
console.error('Failed to get bundle info:', error); uuid,
};
} }
if (isFirstTime) { setLocalHashInfo(hash: string, info: string): boolean {
preferencesManager.deleteSync('isFirstTime');
}
if (rolledBackVersion) {
preferencesManager.deleteSync('rolledBackVersion');
}
return {
downloadRootDir: `${context.filesDir}/_update`,
currentVersionInfo,
packageVersion,
currentVersion,
buildTime,
isUsingBundleUrl,
isFirstTime,
rolledBackVersion,
uuid,
};
}
setLocalHashInfo(hash: string, info: string): boolean {
logger.debug(TAG, ',call setLocalHashInfo'); logger.debug(TAG, ',call setLocalHashInfo');
return UpdateModuleImpl.setLocalHashInfo(this.context, hash, info); return UpdateModuleImpl.setLocalHashInfo(this.context, hash, info);
} }
@@ -75,9 +86,9 @@ getConstants(): Object {
return UpdateModuleImpl.getLocalHashInfo(this.context, hash); return UpdateModuleImpl.getLocalHashInfo(this.context, hash);
} }
async setUuid(uuid: string): Promise<boolean> { async setUuid(uuid: string): Promise<boolean> {
logger.debug(TAG, ',call setUuid'); logger.debug(TAG, ',call setUuid');
return UpdateModuleImpl.setUuid(this.context,uuid); return UpdateModuleImpl.setUuid(this.context, uuid);
} }
async reloadUpdate(options: { hash: string }): Promise<void> { async reloadUpdate(options: { hash: string }): Promise<void> {
@@ -95,31 +106,45 @@ getConstants(): Object {
return UpdateModuleImpl.markSuccess(this.context); return UpdateModuleImpl.markSuccess(this.context);
} }
async downloadPatchFromPpk(options: { updateUrl: string; hash: string; originHash: string }): Promise<void> { async downloadPatchFromPpk(options: {
updateUrl: string;
hash: string;
originHash: string;
}): Promise<void> {
logger.debug(TAG, ',call downloadPatchFromPpk'); logger.debug(TAG, ',call downloadPatchFromPpk');
return UpdateModuleImpl.downloadPatchFromPpk(this.context, options); return UpdateModuleImpl.downloadPatchFromPpk(this.context, options);
} }
async downloadPatchFromPackage(options: { updateUrl: string; hash: string }): Promise<void> { async downloadPatchFromPackage(options: {
updateUrl: string;
hash: string;
}): Promise<void> {
logger.debug(TAG, ',call downloadPatchFromPackage'); logger.debug(TAG, ',call downloadPatchFromPackage');
return UpdateModuleImpl.downloadPatchFromPackage(this.context, options); return UpdateModuleImpl.downloadPatchFromPackage(this.context, options);
} }
async downloadFullUpdate(options: { updateUrl: string; hash: string }): Promise<void> { async downloadFullUpdate(options: {
updateUrl: string;
hash: string;
}): Promise<void> {
logger.debug(TAG, ',call downloadFullUpdate'); logger.debug(TAG, ',call downloadFullUpdate');
return UpdateModuleImpl.downloadFullUpdate(this.context, options); return UpdateModuleImpl.downloadFullUpdate(this.context, options);
} }
async downloadAndInstallApk(options: { url: string; target: string; hash: string }): Promise<void> { async downloadAndInstallApk(options: {
url: string;
target: string;
hash: string;
}): Promise<void> {
logger.debug(TAG, ',call downloadAndInstallApk'); logger.debug(TAG, ',call downloadAndInstallApk');
return UpdateModuleImpl.downloadAndInstallApk(this.mUiCtx, options); return UpdateModuleImpl.downloadAndInstallApk(this.mUiCtx, options);
} }
addListener(eventName: string): void { addListener(_eventName: string): void {
logger.debug(TAG, ',call addListener'); logger.debug(TAG, ',call addListener');
} }
removeListeners(count: number): void { removeListeners(_count: number): void {
logger.debug(TAG, ',call removeListeners'); logger.debug(TAG, ',call removeListeners');
} }
} }

View File

@@ -192,11 +192,11 @@ export class UpdateContext {
public static getBundleUrl( public static getBundleUrl(
context: common.UIAbilityContext, context: common.UIAbilityContext,
defaultAssetsUrl?: string, defaultAssetsUrl?: string,
): string { ) {
return new UpdateContext(context).getBundleUrl(defaultAssetsUrl); return new UpdateContext(context).getBundleUrl(defaultAssetsUrl);
} }
public getBundleUrl(defaultAssetsUrl?: string): string { public getBundleUrl(defaultAssetsUrl?: string) {
UpdateContext.isUsingBundleUrl = true; UpdateContext.isUsingBundleUrl = true;
const currentVersion = this.getCurrentVersion(); const currentVersion = this.getCurrentVersion();
if (!currentVersion) { if (!currentVersion) {