Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ exports[`public API surface matches the recorded export list 1`] = `
"defaultWorkletRuntime",
"download",
"getRegisteredBackends",
"getResourceCachePath",
"inspectModel",
"isRnExecuTorchError",
"llm",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Platform } from 'react-native';
import RNBlobUtil from 'react-native-blob-util';
import { getResourceCachePath } from '../../src/utils';
import { download } from '../../src/fetcher/fetcher';
import { setTelemetryEnabled } from '../../src/fetcher/telemetry';
import { RENAMED_HF_REPOS } from '../../src/fetcher/renamedRepos';
import { fakeFs, fakeNet } from '../support/blobUtilMock';

const org = 'https://huggingface.co/software-mansion/';

describe('getResourceCachePath', () => {
it.each(Object.entries(RENAMED_HF_REPOS))('preserves cache identity for %s', (name, alias) => {
const path = '/resolve/v1.0.0/model.pte';
expect(getResourceCachePath(org + name + path)).toBe(getResourceCachePath(org + alias + path));
});

it('ignores query parameters and keeps different resource URLs distinct', () => {
const source = 'https://example.com/models/one/model.pte';
expect(getResourceCachePath(source + '?download=true')).toBe(getResourceCachePath(source));
expect(getResourceCachePath(source)).not.toBe(
getResourceCachePath(source.replace('/one/', '/two/'))
);
});

it('uses the platform persistent directory', () => {
const directory =
Platform.OS === 'android'
? RNBlobUtil.fs.dirs.SDCardDir || RNBlobUtil.fs.dirs.DocumentDir
: RNBlobUtil.fs.dirs.DocumentDir;
expect(getResourceCachePath('https://example.com/model.pte')).toMatch(
directory + '/react-native-executorch/'
);
});

it('passes through local paths without filesystem or network work', () => {
expect(getResourceCachePath('file:///local/model.pte')).toBe('/local/model.pte');
expect(getResourceCachePath('/local/model.pte')).toBe('/local/model.pte');
getResourceCachePath('https://example.com/model.pte');
expect(fakeFs.paths()).toHaveLength(0);
expect(fakeNet.requests()).toHaveLength(0);
});

it('returns the same path as the downloader', async () => {
const source = 'https://example.com/model.pte';
fakeNet.serve(source, { body: 'model-bytes' });
setTelemetryEnabled(false);
try {
const path = await download(source);
expect(path).toBe(getResourceCachePath(source));
expect(fakeFs.readText(path)).toBe('model-bytes');
} finally {
setTelemetryEnabled(true);
}
});
});
16 changes: 13 additions & 3 deletions packages/react-native-executorch/src/fetcher/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ const djb2 = (s: string): number => {

const isRemote = (source: string): boolean => /^https?:\/\//.test(source);

function cachePathFor(url: string): string {
/**
* Returns the persistent resource path used by {@link download} without downloading
* or checking whether it exists. Repository aliases and query parameters use the
* same cache identity as downloads. Local paths lose any `file://` prefix.
* @category Utils / Functions
* @param source The remote resource URL or local file path.
* @returns The persistent cache path or the supplied local path.
*/
export function getResourceCachePath(source: string): string {
if (!isRemote(source)) return source.replace(/^file:\/\//, '');
const url = source;
const urlWithoutQuery = url.split('?')[0]!;
const basename = urlWithoutQuery.split('/').pop() || 'model';
// Hash guards against basename collisions across different repos/versions.
Expand Down Expand Up @@ -224,7 +234,7 @@ const inFlight = new Map<string, InFlightDownload>();
async function downloadUrl(url: string, cb: DownloadUrlCallbacks): Promise<string> {
if (cb.signal?.aborted) throw abortError();

const dest = cachePathFor(url);
const dest = getResourceCachePath(url);

if (cb.forceDownload) {
// Drop the cached copy so the checks below fall through to a real fetch.
Expand Down Expand Up @@ -800,7 +810,7 @@ export async function download<T>(source: T, options: DownloadOptions = {}): Pro
const measured = await Promise.all(
urls.map(async (url) => {
if (!options.forceDownload) {
const cached = await fileSize(cachePathFor(url));
const cached = await fileSize(getResourceCachePath(url));
if (cached > 0) return { size: cached, pending: undefined, cached: true };
}
return { size: 0, pending: remoteSize(url, options.signal), cached: false };
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-executorch/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { loadModel } from './core/model';
import type { ModelSpec, ConcreteDim } from './core/schema';
import RNBlobUtil from 'react-native-blob-util';

export { getResourceCachePath } from './fetcher/fetcher';

/**
* Options accepted by {@link useResourceDownload} and by every `use<Task>` hook
* built on top of it.
Expand Down
Loading