Skip to content

Commit cb7ef9d

Browse files
committed
chore: dedup shared logic with legacy path
1 parent cc3234b commit cb7ef9d

3 files changed

Lines changed: 101 additions & 140 deletions

File tree

‎packages/expo-resource-fetcher/src/ResourceFetcherUtils.ts‎

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { RNEDirectory } from './constants/directories';
22
import {
3-
ResourceSource,
4-
Logger,
5-
ResourceFetcherUtils as CoreUtils,
63
HTTP_CODE,
74
DownloadStatus,
85
SourceType,
96
RnExecutorchError,
107
RnExecutorchErrorCode,
118
} from 'react-native-executorch';
12-
import { Asset } from 'expo-asset';
9+
import * as SharedUtils from './sharedUtils';
1310

1411
/**
1512
* @internal
@@ -23,73 +20,16 @@ export { HTTP_CODE, DownloadStatus, SourceType };
2320
* @category Utilities - General
2421
*/
2522
export namespace ResourceFetcherUtils {
26-
export const removeFilePrefix = CoreUtils.removeFilePrefix;
27-
export const hashObject = CoreUtils.hashObject;
28-
export const calculateDownloadProgress = CoreUtils.calculateDownloadProgress;
23+
export const removeFilePrefix = SharedUtils.removeFilePrefix;
24+
export const hashObject = SharedUtils.hashObject;
25+
export const calculateDownloadProgress =
26+
SharedUtils.calculateDownloadProgress;
2927
export const triggerHuggingFaceDownloadCounter =
30-
CoreUtils.triggerHuggingFaceDownloadCounter;
31-
export const triggerDownloadEvent = CoreUtils.triggerDownloadEvent;
32-
export const getFilenameFromUri = CoreUtils.getFilenameFromUri;
33-
34-
export function getType(source: ResourceSource): SourceType {
35-
if (typeof source === 'object') {
36-
return SourceType.OBJECT;
37-
} else if (typeof source === 'number') {
38-
const uri = Asset.fromModule(source).uri;
39-
if (uri.startsWith('http')) {
40-
return SourceType.DEV_MODE_FILE;
41-
}
42-
return SourceType.RELEASE_MODE_FILE;
43-
}
44-
// typeof source == 'string'
45-
if (source.startsWith('file://')) {
46-
return SourceType.LOCAL_FILE;
47-
}
48-
return SourceType.REMOTE_FILE;
49-
}
50-
51-
export async function getFilesSizes(sources: ResourceSource[]) {
52-
const results: Array<{
53-
source: ResourceSource;
54-
type: SourceType;
55-
length: number;
56-
previousFilesTotalLength: number;
57-
}> = [];
58-
let totalLength = 0;
59-
60-
for (const source of sources) {
61-
const type = ResourceFetcherUtils.getType(source);
62-
let length = 0;
63-
64-
if (type === SourceType.REMOTE_FILE && typeof source === 'string') {
65-
try {
66-
const response = await fetch(source, { method: 'HEAD' });
67-
if (!response.ok) {
68-
Logger.warn(
69-
`Failed to fetch HEAD for ${source}: ${response.status}`
70-
);
71-
continue;
72-
}
73-
74-
const contentLength = response.headers.get('content-length');
75-
if (!contentLength) {
76-
Logger.warn(`No content-length header for ${source}`);
77-
}
78-
79-
length = contentLength ? parseInt(contentLength, 10) : 0;
80-
} catch (error) {
81-
Logger.warn(`Error fetching HEAD for ${source}:`, error);
82-
continue;
83-
}
84-
}
85-
86-
const previousFilesTotalLength = totalLength;
87-
totalLength += length;
88-
results.push({ source, type, length, previousFilesTotalLength });
89-
}
90-
91-
return { results, totalLength };
92-
}
28+
SharedUtils.triggerHuggingFaceDownloadCounter;
29+
export const triggerDownloadEvent = SharedUtils.triggerDownloadEvent;
30+
export const getFilenameFromUri = SharedUtils.getFilenameFromUri;
31+
export const getType = SharedUtils.getType;
32+
export const getFilesSizes = SharedUtils.getFilesSizes;
9333

9434
export async function createDirectoryIfNoExists() {
9535
const directory = new Directory(RNEDirectory);

‎packages/expo-resource-fetcher/src/legacy/ResourceFetcherUtils.ts‎

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { RNEDirectory } from './constants/directories';
22
import {
3-
ResourceSource,
4-
Logger,
5-
ResourceFetcherUtils as CoreUtils,
63
HTTP_CODE,
74
DownloadStatus,
85
SourceType,
96
RnExecutorchError,
107
RnExecutorchErrorCode,
118
} from 'react-native-executorch';
12-
import { Asset } from 'expo-asset';
9+
import * as SharedUtils from '../sharedUtils';
1310

1411
/**
1512
* @internal
@@ -23,73 +20,16 @@ export { HTTP_CODE, DownloadStatus, SourceType };
2320
* @category Utilities - General
2421
*/
2522
export namespace ResourceFetcherUtils {
26-
export const removeFilePrefix = CoreUtils.removeFilePrefix;
27-
export const hashObject = CoreUtils.hashObject;
28-
export const calculateDownloadProgress = CoreUtils.calculateDownloadProgress;
23+
export const removeFilePrefix = SharedUtils.removeFilePrefix;
24+
export const hashObject = SharedUtils.hashObject;
25+
export const calculateDownloadProgress =
26+
SharedUtils.calculateDownloadProgress;
2927
export const triggerHuggingFaceDownloadCounter =
30-
CoreUtils.triggerHuggingFaceDownloadCounter;
31-
export const triggerDownloadEvent = CoreUtils.triggerDownloadEvent;
32-
export const getFilenameFromUri = CoreUtils.getFilenameFromUri;
33-
34-
export function getType(source: ResourceSource): SourceType {
35-
if (typeof source === 'object') {
36-
return SourceType.OBJECT;
37-
} else if (typeof source === 'number') {
38-
const uri = Asset.fromModule(source).uri;
39-
if (uri.startsWith('http')) {
40-
return SourceType.DEV_MODE_FILE;
41-
}
42-
return SourceType.RELEASE_MODE_FILE;
43-
}
44-
// typeof source == 'string'
45-
if (source.startsWith('file://')) {
46-
return SourceType.LOCAL_FILE;
47-
}
48-
return SourceType.REMOTE_FILE;
49-
}
50-
51-
export async function getFilesSizes(sources: ResourceSource[]) {
52-
const results: Array<{
53-
source: ResourceSource;
54-
type: SourceType;
55-
length: number;
56-
previousFilesTotalLength: number;
57-
}> = [];
58-
let totalLength = 0;
59-
60-
for (const source of sources) {
61-
const type = ResourceFetcherUtils.getType(source);
62-
let length = 0;
63-
64-
if (type === SourceType.REMOTE_FILE && typeof source === 'string') {
65-
try {
66-
const response = await fetch(source, { method: 'HEAD' });
67-
if (!response.ok) {
68-
Logger.warn(
69-
`Failed to fetch HEAD for ${source}: ${response.status}`
70-
);
71-
continue;
72-
}
73-
74-
const contentLength = response.headers.get('content-length');
75-
if (!contentLength) {
76-
Logger.warn(`No content-length header for ${source}`);
77-
}
78-
79-
length = contentLength ? parseInt(contentLength, 10) : 0;
80-
} catch (error) {
81-
Logger.warn(`Error fetching HEAD for ${source}:`, error);
82-
continue;
83-
}
84-
}
85-
86-
const previousFilesTotalLength = totalLength;
87-
totalLength += length;
88-
results.push({ source, type, length, previousFilesTotalLength });
89-
}
90-
91-
return { results, totalLength };
92-
}
28+
SharedUtils.triggerHuggingFaceDownloadCounter;
29+
export const triggerDownloadEvent = SharedUtils.triggerDownloadEvent;
30+
export const getFilenameFromUri = SharedUtils.getFilenameFromUri;
31+
export const getType = SharedUtils.getType;
32+
export const getFilesSizes = SharedUtils.getFilesSizes;
9333

9434
export async function createDirectoryIfNoExists() {
9535
if (!(await checkFileExists(RNEDirectory))) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Utilities shared between the current (`expo-file-system`) and legacy
3+
* (`expo-file-system/legacy`) resource fetcher implementations.
4+
*
5+
* Everything in this module must stay free of `expo-file-system` imports —
6+
* it is pulled in by both entry points, so it can only depend on APIs that
7+
* exist regardless of which file-system API the consumer's SDK ships.
8+
*/
9+
import {
10+
ResourceSource,
11+
Logger,
12+
ResourceFetcherUtils as CoreUtils,
13+
SourceType,
14+
} from 'react-native-executorch';
15+
import { Asset } from 'expo-asset';
16+
17+
export const removeFilePrefix = CoreUtils.removeFilePrefix;
18+
export const hashObject = CoreUtils.hashObject;
19+
export const calculateDownloadProgress = CoreUtils.calculateDownloadProgress;
20+
export const triggerHuggingFaceDownloadCounter =
21+
CoreUtils.triggerHuggingFaceDownloadCounter;
22+
export const triggerDownloadEvent = CoreUtils.triggerDownloadEvent;
23+
export const getFilenameFromUri = CoreUtils.getFilenameFromUri;
24+
25+
export function getType(source: ResourceSource): SourceType {
26+
if (typeof source === 'object') {
27+
return SourceType.OBJECT;
28+
} else if (typeof source === 'number') {
29+
const uri = Asset.fromModule(source).uri;
30+
if (uri.startsWith('http')) {
31+
return SourceType.DEV_MODE_FILE;
32+
}
33+
return SourceType.RELEASE_MODE_FILE;
34+
}
35+
// typeof source == 'string'
36+
if (source.startsWith('file://')) {
37+
return SourceType.LOCAL_FILE;
38+
}
39+
return SourceType.REMOTE_FILE;
40+
}
41+
42+
export async function getFilesSizes(sources: ResourceSource[]) {
43+
const results: Array<{
44+
source: ResourceSource;
45+
type: SourceType;
46+
length: number;
47+
previousFilesTotalLength: number;
48+
}> = [];
49+
let totalLength = 0;
50+
51+
for (const source of sources) {
52+
const type = getType(source);
53+
let length = 0;
54+
55+
if (type === SourceType.REMOTE_FILE && typeof source === 'string') {
56+
try {
57+
const response = await fetch(source, { method: 'HEAD' });
58+
if (!response.ok) {
59+
Logger.warn(`Failed to fetch HEAD for ${source}: ${response.status}`);
60+
continue;
61+
}
62+
63+
const contentLength = response.headers.get('content-length');
64+
if (!contentLength) {
65+
Logger.warn(`No content-length header for ${source}`);
66+
}
67+
68+
length = contentLength ? parseInt(contentLength, 10) : 0;
69+
} catch (error) {
70+
Logger.warn(`Error fetching HEAD for ${source}:`, error);
71+
continue;
72+
}
73+
}
74+
75+
const previousFilesTotalLength = totalLength;
76+
totalLength += length;
77+
results.push({ source, type, length, previousFilesTotalLength });
78+
}
79+
80+
return { results, totalLength };
81+
}

0 commit comments

Comments
 (0)