Skip to content

Commit 268f39f

Browse files
committed
fix filejsbundleprovider
1 parent 6e4f432 commit 268f39f

File tree

3 files changed

+100
-83
lines changed

3 files changed

+100
-83
lines changed

harmony/pushy/src/main/ets/PushyFileJSBundleProvider.ets

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,41 @@
1-
import { HotReloadConfig, JSBundleProvider, JSBundleProviderError, JSPackagerClientConfig } from '@rnoh/react-native-openharmony';
2-
import fileIo from '@ohos.file.fs';
1+
import {
2+
FileJSBundle,
3+
HotReloadConfig,
4+
JSBundleProvider,
5+
JSBundleProviderError
6+
} from '@rnoh/react-native-openharmony';
37
import common from '@ohos.app.ability.common';
8+
import fs from '@ohos.file.fs';
49
import { UpdateContext } from './UpdateContext';
510

611
export class PushyFileJSBundleProvider extends JSBundleProvider {
712
private updateContext: UpdateContext;
8-
private filePath: string = ''
13+
private path: string = ''
914

1015
constructor(context: common.UIAbilityContext) {
1116
super();
1217
this.updateContext = new UpdateContext(context);
18+
this.path = this.updateContext.getBundleUrl();
1319
}
20+
1421
getURL(): string {
15-
return this.updateContext.getBundleUrl()?.substring(1);
22+
return this.path;
1623
}
1724

18-
async getBundle(): Promise<ArrayBuffer> {
25+
async getBundle(): Promise<FileJSBundle> {
1926
try {
20-
this.filePath = this.updateContext.getBundleUrl();
21-
const res = fileIo.accessSync(this.filePath);
22-
if (res) {
23-
const file = fileIo.openSync(this.filePath, fileIo.OpenMode.READ_ONLY);
24-
try {
25-
const stat = await fileIo.stat(this.filePath);
26-
const fileSize = stat.size;
27-
const buffer = new ArrayBuffer(fileSize);
28-
const bytesRead = fileIo.readSync(file.fd, buffer, {
29-
offset: 0,
30-
length: fileSize
31-
});
32-
33-
if (bytesRead !== fileSize) {
34-
throw new Error(`Failed to read entire file: read ${bytesRead} of ${fileSize} bytes`);
35-
}
36-
return buffer;
37-
} finally {
38-
fileIo.closeSync(file.fd);
27+
const status = await fs.access(this.path, fs.OpenMode.READ_ONLY);
28+
if (status) {
29+
return {
30+
filePath: this.path
3931
}
4032
}
4133
throw new Error('Update bundle not found');
4234
} catch (error) {
4335
throw new JSBundleProviderError({
44-
whatHappened: `Couldn't load JSBundle from ${this.filePath}`,
36+
whatHappened: `Couldn't load JSBundle from ${this.path}`,
4537
extraData: error,
46-
howCanItBeFixed: [`Check if a bundle exists at "${this.filePath}" on your device.`]
38+
howCanItBeFixed: [`Check if a bundle exists at "${this.path}" on your device.`]
4739
})
4840
}
4941
}
Lines changed: 80 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
1+
import {
2+
TurboModule,
3+
TurboModuleContext,
4+
} from '@rnoh/react-native-openharmony/ts';
25
import common from '@ohos.app.ability.common';
36
import dataPreferences from '@ohos.data.preferences';
47
import { bundleManager } from '@kit.AbilityKit';
5-
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
6-
import { BusinessError } from '@ohos.base';
78
import logger from './Logger';
89
import { UpdateModuleImpl } from './UpdateModuleImpl';
910
import { UpdateContext } from './UpdateContext';
@@ -23,50 +24,60 @@ export class PushyTurboModule extends TurboModule {
2324
EventHub.getInstance().setRNInstance(ctx.rnInstance);
2425
}
2526

26-
27-
getConstants(): Object {
28-
logger.debug(TAG, ',call getConstants');
29-
const context = this.mUiCtx;
30-
const preferencesManager = dataPreferences.getPreferencesSync(context,{ name: 'update' });
31-
const isFirstTime = preferencesManager.getSync('isFirstTime', false) as boolean;
32-
const rolledBackVersion = preferencesManager.getSync('rolledBackVersion', '') as string;
33-
const uuid = preferencesManager.getSync('uuid', '') as string;
34-
const currentVersion = preferencesManager.getSync('currentVersion', '') as string;
35-
const currentVersionInfo = this.context.getKv(`hash_${currentVersion}`);
36-
const buildTime = preferencesManager.getSync('buildTime', '') as string;
37-
const isUsingBundleUrl = this.context.getIsUsingBundleUrl();
38-
let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
39-
let packageVersion = '';
40-
try {
41-
const bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleFlags);
42-
packageVersion = bundleInfo?.versionName || 'Unknown';
43-
} catch (error) {
44-
console.error('Failed to get bundle info:', error);
45-
}
46-
47-
if (isFirstTime) {
48-
preferencesManager.deleteSync('isFirstTime');
49-
}
50-
51-
if (rolledBackVersion) {
52-
preferencesManager.deleteSync('rolledBackVersion');
53-
}
54-
55-
return {
56-
downloadRootDir: `${context.filesDir}/_update`,
57-
currentVersionInfo,
58-
packageVersion,
59-
currentVersion,
60-
buildTime,
61-
isUsingBundleUrl,
62-
isFirstTime,
63-
rolledBackVersion,
64-
uuid,
65-
};
66-
}
67-
68-
69-
setLocalHashInfo(hash: string, info: string): boolean {
27+
getConstants(): Object {
28+
logger.debug(TAG, ',call getConstants');
29+
const context = this.mUiCtx;
30+
const preferencesManager = dataPreferences.getPreferencesSync(context, {
31+
name: 'update',
32+
});
33+
const isFirstTime = preferencesManager.getSync(
34+
'isFirstTime',
35+
false,
36+
) as boolean;
37+
const rolledBackVersion = preferencesManager.getSync(
38+
'rolledBackVersion',
39+
'',
40+
) as string;
41+
const uuid = preferencesManager.getSync('uuid', '') as string;
42+
const currentVersion = preferencesManager.getSync(
43+
'currentVersion',
44+
'',
45+
) as string;
46+
const currentVersionInfo = this.context.getKv(`hash_${currentVersion}`);
47+
const buildTime = preferencesManager.getSync('buildTime', '') as string;
48+
const isUsingBundleUrl = this.context.getIsUsingBundleUrl();
49+
let bundleFlags =
50+
bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
51+
let packageVersion = '';
52+
try {
53+
const bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleFlags);
54+
packageVersion = bundleInfo?.versionName || 'Unknown';
55+
} catch (error) {
56+
console.error('Failed to get bundle info:', error);
57+
}
58+
59+
if (isFirstTime) {
60+
preferencesManager.deleteSync('isFirstTime');
61+
}
62+
63+
if (rolledBackVersion) {
64+
preferencesManager.deleteSync('rolledBackVersion');
65+
}
66+
67+
return {
68+
downloadRootDir: `${context.filesDir}/_update`,
69+
currentVersionInfo,
70+
packageVersion,
71+
currentVersion,
72+
buildTime,
73+
isUsingBundleUrl,
74+
isFirstTime,
75+
rolledBackVersion,
76+
uuid,
77+
};
78+
}
79+
80+
setLocalHashInfo(hash: string, info: string): boolean {
7081
logger.debug(TAG, ',call setLocalHashInfo');
7182
return UpdateModuleImpl.setLocalHashInfo(this.context, hash, info);
7283
}
@@ -75,9 +86,9 @@ getConstants(): Object {
7586
return UpdateModuleImpl.getLocalHashInfo(this.context, hash);
7687
}
7788

78-
async setUuid(uuid: string): Promise<boolean> {
89+
async setUuid(uuid: string): Promise<boolean> {
7990
logger.debug(TAG, ',call setUuid');
80-
return UpdateModuleImpl.setUuid(this.context,uuid);
91+
return UpdateModuleImpl.setUuid(this.context, uuid);
8192
}
8293

8394
async reloadUpdate(options: { hash: string }): Promise<void> {
@@ -95,31 +106,45 @@ getConstants(): Object {
95106
return UpdateModuleImpl.markSuccess(this.context);
96107
}
97108

98-
async downloadPatchFromPpk(options: { updateUrl: string; hash: string; originHash: string }): Promise<void> {
109+
async downloadPatchFromPpk(options: {
110+
updateUrl: string;
111+
hash: string;
112+
originHash: string;
113+
}): Promise<void> {
99114
logger.debug(TAG, ',call downloadPatchFromPpk');
100115
return UpdateModuleImpl.downloadPatchFromPpk(this.context, options);
101116
}
102117

103-
async downloadPatchFromPackage(options: { updateUrl: string; hash: string }): Promise<void> {
118+
async downloadPatchFromPackage(options: {
119+
updateUrl: string;
120+
hash: string;
121+
}): Promise<void> {
104122
logger.debug(TAG, ',call downloadPatchFromPackage');
105123
return UpdateModuleImpl.downloadPatchFromPackage(this.context, options);
106124
}
107125

108-
async downloadFullUpdate(options: { updateUrl: string; hash: string }): Promise<void> {
126+
async downloadFullUpdate(options: {
127+
updateUrl: string;
128+
hash: string;
129+
}): Promise<void> {
109130
logger.debug(TAG, ',call downloadFullUpdate');
110131
return UpdateModuleImpl.downloadFullUpdate(this.context, options);
111132
}
112133

113-
async downloadAndInstallApk(options: { url: string; target: string; hash: string }): Promise<void> {
134+
async downloadAndInstallApk(options: {
135+
url: string;
136+
target: string;
137+
hash: string;
138+
}): Promise<void> {
114139
logger.debug(TAG, ',call downloadAndInstallApk');
115140
return UpdateModuleImpl.downloadAndInstallApk(this.mUiCtx, options);
116141
}
117142

118-
addListener(eventName: string): void {
143+
addListener(_eventName: string): void {
119144
logger.debug(TAG, ',call addListener');
120145
}
121146

122-
removeListeners(count: number): void {
147+
removeListeners(_count: number): void {
123148
logger.debug(TAG, ',call removeListeners');
124149
}
125150
}

harmony/pushy/src/main/ets/UpdateContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ export class UpdateContext {
192192
public static getBundleUrl(
193193
context: common.UIAbilityContext,
194194
defaultAssetsUrl?: string,
195-
): string {
195+
) {
196196
return new UpdateContext(context).getBundleUrl(defaultAssetsUrl);
197197
}
198198

199-
public getBundleUrl(defaultAssetsUrl?: string): string {
199+
public getBundleUrl(defaultAssetsUrl?: string) {
200200
UpdateContext.isUsingBundleUrl = true;
201201
const currentVersion = this.getCurrentVersion();
202202
if (!currentVersion) {

0 commit comments

Comments
 (0)