Skip to content

Commit 9cb5f03

Browse files
v0.5.2 (#568)
## Description Resource Fetcher changes: - Fix an issue which prevented models from loading when device was not connected to the internet. - Fix an issue which crashed IOS apps in release mode using bundled model sources. ### Introduces a breaking change? - [ ] Yes - [x] No ### Type of change - [x] Bug fix (change which fixes an issue) - [ ] New feature (change which adds functionality) - [ ] Documentation update (improves or adds clarity to existing documentation) - [ ] Other (chores, tests, code style improvements etc.) ### Tested on - [x] iOS - [x] Android ### Testing instructions <!-- Provide step-by-step instructions on how to test your changes. Include setup details if necessary. --> ### Screenshots <!-- Add screenshots here, if applicable --> ### Related issues <!-- Link related issues here using #issue-number --> ### Checklist - [x] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have updated the documentation accordingly - [x] My changes generate no new warnings ### Additional notes <!-- Include any additional information, assumptions, or context that reviewers might need to understand this PR. -->
1 parent 66edaee commit 9cb5f03

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

packages/react-native-executorch/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-executorch",
3-
"version": "0.5.1",
3+
"version": "0.5.2",
44
"description": "An easy way to run AI models in React Native with ExecuTorch",
55
"source": "./src/index.ts",
66
"main": "./lib/module/index.js",

packages/react-native-executorch/src/utils/ResourceFetcher.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030
import {
3131
cacheDirectory,
32+
copyAsync,
3233
createDownloadResumable,
3334
moveAsync,
3435
FileSystemSessionType,
@@ -38,6 +39,7 @@ import {
3839
readDirectoryAsync,
3940
} from 'expo-file-system';
4041
import { Asset } from 'expo-asset';
42+
import { Platform } from 'react-native';
4143
import { RNEDirectory } from '../constants/directories';
4244
import { ResourceSource } from '../types/common';
4345
import {
@@ -61,7 +63,6 @@ export class ResourceFetcher {
6163
}
6264
const { results: info, totalLength } =
6365
await ResourceFetcherUtils.getFilesSizes(sources);
64-
6566
const head: ResourceSourceExtended = {
6667
source: info[0]!.source,
6768
sourceType: info[0]!.type,
@@ -316,16 +317,17 @@ export class ResourceFetcher {
316317
const uri = asset.uri;
317318
const filename = ResourceFetcherUtils.getFilenameFromUri(uri);
318319
const fileUri = `${RNEDirectory}${filename}`;
319-
const fileUriWithType = `${fileUri}.${asset.type}`;
320+
// On Android, file uri does not contain file extension, so we add it manually
321+
const fileUriWithType =
322+
Platform.OS === 'android' ? `${fileUri}.${asset.type}` : fileUri;
320323
if (await ResourceFetcherUtils.checkFileExists(fileUri)) {
321324
return ResourceFetcherUtils.removeFilePrefix(fileUri);
322325
}
323326
await ResourceFetcherUtils.createDirectoryIfNoExists();
324-
await asset.downloadAsync();
325-
if (!asset.localUri) {
326-
throw new Error(`Asset local URI is not available for ${source}`);
327-
}
328-
await moveAsync({ from: asset.localUri, to: fileUriWithType });
327+
await copyAsync({
328+
from: asset.uri,
329+
to: fileUriWithType,
330+
});
329331
return ResourceFetcherUtils.removeFilePrefix(fileUriWithType);
330332
}
331333

packages/react-native-executorch/src/utils/ResourceFetcherUtils.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,16 @@ export namespace ResourceFetcherUtils {
5353
return SourceType.OBJECT;
5454
} else if (typeof source === 'number') {
5555
const uri = Asset.fromModule(source).uri;
56-
if (!uri.includes('://')) {
57-
return SourceType.RELEASE_MODE_FILE;
56+
if (uri.startsWith('http')) {
57+
return SourceType.DEV_MODE_FILE;
5858
}
59-
return SourceType.DEV_MODE_FILE;
60-
} else {
61-
// typeof source == 'string'
62-
if (source.startsWith('file://')) {
63-
return SourceType.LOCAL_FILE;
64-
}
65-
return SourceType.REMOTE_FILE;
59+
return SourceType.RELEASE_MODE_FILE;
60+
}
61+
// typeof source == 'string'
62+
if (source.startsWith('file://')) {
63+
return SourceType.LOCAL_FILE;
6664
}
65+
return SourceType.REMOTE_FILE;
6766
}
6867

6968
export async function getFilesSizes(sources: ResourceSource[]) {
@@ -78,9 +77,8 @@ export namespace ResourceFetcherUtils {
7877
for (const source of sources) {
7978
const type = await ResourceFetcherUtils.getType(source);
8079
let length = 0;
81-
82-
if (type === SourceType.REMOTE_FILE && typeof source === 'string') {
83-
try {
80+
try {
81+
if (type === SourceType.REMOTE_FILE && typeof source === 'string') {
8482
const response = await fetch(source, { method: 'HEAD' });
8583
if (!response.ok) {
8684
Logger.warn(
@@ -97,14 +95,14 @@ export namespace ResourceFetcherUtils {
9795
length = contentLength ? parseInt(contentLength, 10) : 0;
9896
previousFilesTotalLength = totalLength;
9997
totalLength += length;
100-
} catch (error) {
101-
Logger.warn(`Error fetching HEAD for ${source}:`, error);
102-
continue;
10398
}
99+
} catch (error) {
100+
Logger.warn(`Error fetching HEAD for ${source}:`, error);
101+
continue;
102+
} finally {
103+
results.push({ source, type, length, previousFilesTotalLength });
104104
}
105-
results.push({ source, type, length, previousFilesTotalLength });
106105
}
107-
108106
return { results, totalLength };
109107
}
110108

0 commit comments

Comments
 (0)