mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-10-28 20:13:10 +08:00
fix filejsbundleprovider
This commit is contained in:
@@ -1,49 +1,41 @@
|
||||
import { HotReloadConfig, JSBundleProvider, JSBundleProviderError, JSPackagerClientConfig } from '@rnoh/react-native-openharmony';
|
||||
import fileIo from '@ohos.file.fs';
|
||||
import {
|
||||
FileJSBundle,
|
||||
HotReloadConfig,
|
||||
JSBundleProvider,
|
||||
JSBundleProviderError
|
||||
} from '@rnoh/react-native-openharmony';
|
||||
import common from '@ohos.app.ability.common';
|
||||
import fs from '@ohos.file.fs';
|
||||
import { UpdateContext } from './UpdateContext';
|
||||
|
||||
export class PushyFileJSBundleProvider extends JSBundleProvider {
|
||||
private updateContext: UpdateContext;
|
||||
private filePath: string = ''
|
||||
private path: string = ''
|
||||
|
||||
constructor(context: common.UIAbilityContext) {
|
||||
super();
|
||||
this.updateContext = new UpdateContext(context);
|
||||
}
|
||||
getURL(): string {
|
||||
return this.updateContext.getBundleUrl()?.substring(1);
|
||||
this.path = this.updateContext.getBundleUrl();
|
||||
}
|
||||
|
||||
async getBundle(): Promise<ArrayBuffer> {
|
||||
getURL(): string {
|
||||
return this.path;
|
||||
}
|
||||
|
||||
async getBundle(): Promise<FileJSBundle> {
|
||||
try {
|
||||
this.filePath = this.updateContext.getBundleUrl();
|
||||
const res = fileIo.accessSync(this.filePath);
|
||||
if (res) {
|
||||
const file = fileIo.openSync(this.filePath, fileIo.OpenMode.READ_ONLY);
|
||||
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);
|
||||
const status = await fs.access(this.path, fs.OpenMode.READ_ONLY);
|
||||
if (status) {
|
||||
return {
|
||||
filePath: this.path
|
||||
}
|
||||
}
|
||||
throw new Error('Update bundle not found');
|
||||
} catch (error) {
|
||||
throw new JSBundleProviderError({
|
||||
whatHappened: `Couldn't load JSBundle from ${this.filePath}`,
|
||||
whatHappened: `Couldn't load JSBundle from ${this.path}`,
|
||||
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.`]
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 dataPreferences from '@ohos.data.preferences';
|
||||
import { bundleManager } from '@kit.AbilityKit';
|
||||
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
|
||||
import { BusinessError } from '@ohos.base';
|
||||
import logger from './Logger';
|
||||
import { UpdateModuleImpl } from './UpdateModuleImpl';
|
||||
import { UpdateContext } from './UpdateContext';
|
||||
@@ -23,50 +24,60 @@ export class PushyTurboModule extends TurboModule {
|
||||
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 {
|
||||
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);
|
||||
if (isFirstTime) {
|
||||
preferencesManager.deleteSync('isFirstTime');
|
||||
}
|
||||
|
||||
if (rolledBackVersion) {
|
||||
preferencesManager.deleteSync('rolledBackVersion');
|
||||
}
|
||||
|
||||
return {
|
||||
downloadRootDir: `${context.filesDir}/_update`,
|
||||
currentVersionInfo,
|
||||
packageVersion,
|
||||
currentVersion,
|
||||
buildTime,
|
||||
isUsingBundleUrl,
|
||||
isFirstTime,
|
||||
rolledBackVersion,
|
||||
uuid,
|
||||
};
|
||||
}
|
||||
|
||||
if (isFirstTime) {
|
||||
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 {
|
||||
setLocalHashInfo(hash: string, info: string): boolean {
|
||||
logger.debug(TAG, ',call setLocalHashInfo');
|
||||
return UpdateModuleImpl.setLocalHashInfo(this.context, hash, info);
|
||||
}
|
||||
@@ -75,9 +86,9 @@ getConstants(): Object {
|
||||
return UpdateModuleImpl.getLocalHashInfo(this.context, hash);
|
||||
}
|
||||
|
||||
async setUuid(uuid: string): Promise<boolean> {
|
||||
async setUuid(uuid: string): Promise<boolean> {
|
||||
logger.debug(TAG, ',call setUuid');
|
||||
return UpdateModuleImpl.setUuid(this.context,uuid);
|
||||
return UpdateModuleImpl.setUuid(this.context, uuid);
|
||||
}
|
||||
|
||||
async reloadUpdate(options: { hash: string }): Promise<void> {
|
||||
@@ -95,31 +106,45 @@ getConstants(): Object {
|
||||
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');
|
||||
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');
|
||||
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');
|
||||
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');
|
||||
return UpdateModuleImpl.downloadAndInstallApk(this.mUiCtx, options);
|
||||
}
|
||||
|
||||
addListener(eventName: string): void {
|
||||
addListener(_eventName: string): void {
|
||||
logger.debug(TAG, ',call addListener');
|
||||
}
|
||||
|
||||
removeListeners(count: number): void {
|
||||
removeListeners(_count: number): void {
|
||||
logger.debug(TAG, ',call removeListeners');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,11 +192,11 @@ export class UpdateContext {
|
||||
public static getBundleUrl(
|
||||
context: common.UIAbilityContext,
|
||||
defaultAssetsUrl?: string,
|
||||
): string {
|
||||
) {
|
||||
return new UpdateContext(context).getBundleUrl(defaultAssetsUrl);
|
||||
}
|
||||
|
||||
public getBundleUrl(defaultAssetsUrl?: string): string {
|
||||
public getBundleUrl(defaultAssetsUrl?: string) {
|
||||
UpdateContext.isUsingBundleUrl = true;
|
||||
const currentVersion = this.getCurrentVersion();
|
||||
if (!currentVersion) {
|
||||
|
||||
Reference in New Issue
Block a user