11import { RNEDirectory } from './constants/directories' ;
22import {
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 */
2522export 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 ) ;
0 commit comments