Skip to content

Commit 0a1fa1e

Browse files
committed
prepare for ut
1 parent bab3262 commit 0a1fa1e

2 files changed

Lines changed: 59 additions & 58 deletions

File tree

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

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,45 @@ import NativePatchCore, {
1212
CopyGroupResult,
1313
} from './NativePatchCore';
1414

15-
interface PatchManifestArrays {
15+
export interface PatchManifestArrays {
1616
copyFroms: string[];
1717
copyTos: string[];
1818
deletes: string[];
1919
}
2020

21+
export function parseManifestToArrays(
22+
manifest: Record<string, any>,
23+
normalizeResourceCopies: boolean,
24+
): PatchManifestArrays {
25+
const copyFroms: string[] = [];
26+
const copyTos: string[] = [];
27+
const deletesValue = manifest.deletes;
28+
const deletes = Array.isArray(deletesValue)
29+
? deletesValue.map(item => String(item))
30+
: deletesValue && typeof deletesValue === 'object'
31+
? Object.keys(deletesValue)
32+
: [];
33+
34+
const copies = (manifest.copies || {}) as Record<string, string>;
35+
for (const [to, rawFrom] of Object.entries(copies)) {
36+
let from = String(rawFrom || '');
37+
if (normalizeResourceCopies) {
38+
from = from.replace('resources/rawfile/', '');
39+
if (!from) {
40+
from = to;
41+
}
42+
}
43+
copyFroms.push(from);
44+
copyTos.push(to);
45+
}
46+
47+
return {
48+
copyFroms,
49+
copyTos,
50+
deletes,
51+
};
52+
}
53+
2154
const DIFF_MANIFEST_ENTRY = '__diff.json';
2255
const HARMONY_BUNDLE_PATCH_ENTRY = 'bundle.harmony.js.patch';
2356
const TEMP_ORIGIN_BUNDLE_ENTRY = '.origin.bundle.harmony.js';
@@ -166,44 +199,12 @@ export class DownloadTask {
166199
};
167200
}
168201

169-
return this.manifestToArrays(
202+
return parseManifestToArrays(
170203
this.parseJsonEntry(await this.readFileContent(manifestPath)),
171204
normalizeResourceCopies,
172205
);
173206
}
174207

175-
private manifestToArrays(
176-
manifest: Record<string, any>,
177-
normalizeResourceCopies: boolean,
178-
): PatchManifestArrays {
179-
const copyFroms: string[] = [];
180-
const copyTos: string[] = [];
181-
const deletesValue = manifest.deletes;
182-
const deletes = Array.isArray(deletesValue)
183-
? deletesValue.map(item => String(item))
184-
: deletesValue && typeof deletesValue === 'object'
185-
? Object.keys(deletesValue)
186-
: [];
187-
188-
const copies = (manifest.copies || {}) as Record<string, string>;
189-
for (const [to, rawFrom] of Object.entries(copies)) {
190-
let from = String(rawFrom || '');
191-
if (normalizeResourceCopies) {
192-
from = from.replace('resources/rawfile/', '');
193-
if (!from) {
194-
from = to;
195-
}
196-
}
197-
copyFroms.push(from);
198-
copyTos.push(to);
199-
}
200-
201-
return {
202-
copyFroms,
203-
copyTos,
204-
deletes,
205-
};
206-
}
207208

208209
private async applyBundlePatchFromFileSource(
209210
originContent: ArrayBuffer,

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ import { util } from '@kit.ArkTS';
1111

1212
const TAG = 'PushyTurboModule';
1313

14+
export function getErrorMessage(error: any): string {
15+
if (error && typeof error === 'object' && 'message' in error) {
16+
return String(error.message);
17+
}
18+
return String(error);
19+
}
20+
21+
export function validateHashInfo(info: string): void {
22+
try {
23+
const parsed = JSON.parse(info);
24+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
25+
throw Error('invalid json string');
26+
}
27+
} catch {
28+
throw Error('invalid json string');
29+
}
30+
}
31+
1432
export class PushyTurboModule extends UITurboModule {
1533
public static readonly NAME = 'Pushy';
1634

@@ -29,13 +47,6 @@ export class PushyTurboModule extends UITurboModule {
2947
return bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
3048
}
3149

32-
private getErrorMessage(error: any): string {
33-
if (error && typeof error === 'object' && 'message' in error) {
34-
return String(error.message);
35-
}
36-
return String(error);
37-
}
38-
3950
private getPackageVersion(): string {
4051
try {
4152
const bundleInfo = bundleManager.getBundleInfoForSelfSync(
@@ -70,17 +81,6 @@ export class PushyTurboModule extends UITurboModule {
7081
return hash;
7182
}
7283

73-
private validateHashInfo(info: string): void {
74-
try {
75-
const parsed = JSON.parse(info);
76-
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
77-
throw Error('invalid json string');
78-
}
79-
} catch {
80-
throw Error('invalid json string');
81-
}
82-
}
83-
8484
private async restartAbility(): Promise<void> {
8585
const bundleInfo = await bundleManager.getBundleInfoForSelf(
8686
this.getBundleFlags(),
@@ -127,14 +127,14 @@ export class PushyTurboModule extends UITurboModule {
127127

128128
setLocalHashInfo(hash: string, info: string): boolean {
129129
logger.debug(TAG, ',call setLocalHashInfo');
130-
this.validateHashInfo(info);
130+
validateHashInfo(info);
131131
this.context.setKv(`hash_${hash}`, info);
132132
return true;
133133
}
134134

135135
getLocalHashInfo(hash: string): string {
136136
const value = this.context.getKv(`hash_${hash}`);
137-
this.validateHashInfo(value);
137+
validateHashInfo(value);
138138
return value;
139139
}
140140

@@ -151,8 +151,8 @@ export class PushyTurboModule extends UITurboModule {
151151
this.context.switchVersion(hash);
152152
await this.restartAbility();
153153
} catch (error) {
154-
logger.error(TAG, `reloadUpdate failed: ${this.getErrorMessage(error)}`);
155-
throw Error(`switchVersion failed ${this.getErrorMessage(error)}`);
154+
logger.error(TAG, `reloadUpdate failed: ${getErrorMessage(error)}`);
155+
throw Error(`switchVersion failed ${getErrorMessage(error)}`);
156156
}
157157
}
158158

@@ -161,8 +161,8 @@ export class PushyTurboModule extends UITurboModule {
161161
try {
162162
await this.restartAbility();
163163
} catch (error) {
164-
logger.error(TAG, `restartApp failed: ${this.getErrorMessage(error)}`);
165-
throw Error(`restartApp failed ${this.getErrorMessage(error)}`);
164+
logger.error(TAG, `restartApp failed: ${getErrorMessage(error)}`);
165+
throw Error(`restartApp failed ${getErrorMessage(error)}`);
166166
}
167167
}
168168

0 commit comments

Comments
 (0)