Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Firefox bug when displaying progress events while reading file from browser cache #374

Merged
merged 2 commits into from
Nov 8, 2023
Merged
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
4 changes: 2 additions & 2 deletions src/utils/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
/**
* Helper function to dispatch progress callbacks.
*
* @param {function} progress_callback The progress callback function to dispatch.
* @param {Function} progress_callback The progress callback function to dispatch.
* @param {any} data The data to pass to the progress callback function.
* @returns {void}
* @private
*/
export function dispatchCallback(progress_callback, data) {
if (progress_callback !== null) progress_callback(data);
if (progress_callback) progress_callback(data);
}

/**
Expand Down
45 changes: 38 additions & 7 deletions src/utils/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
response = await tryCache(cache, localPath, proposedCacheKey);
}

const cacheHit = response !== undefined;

if (response === undefined) {
// Caching not available, or file is not cached, so we perform the request

Expand Down Expand Up @@ -490,14 +492,44 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
file: filename
})

const buffer = await readResponse(response, data => {
const progressInfo = {
status: 'progress',
name: path_or_repo_id,
file: filename
}

/** @type {Uint8Array} */
let buffer;

if (!options.progress_callback) {
// If no progress callback is specified, we can use the `.arrayBuffer()`
// method to read the response.
buffer = new Uint8Array(await response.arrayBuffer());

} else if (
cacheHit // The item is being read from the cache
&&
typeof navigator !== 'undefined' && /firefox/i.test(navigator.userAgent) // We are in Firefox
) {
// Due to bug in Firefox, we cannot display progress when loading from cache.
// Fortunately, since this should be instantaneous, this should not impact users too much.
buffer = new Uint8Array(await response.arrayBuffer());

// For completeness, we still fire the final progress callback
dispatchCallback(options.progress_callback, {
status: 'progress',
...data,
name: path_or_repo_id,
file: filename
...progressInfo,
progress: 100,
loaded: buffer.length,
total: buffer.length,
})
})
} else {
buffer = await readResponse(response, data => {
dispatchCallback(options.progress_callback, {
...progressInfo,
...data,
})
})
}

if (
// Only cache web responses
Expand Down Expand Up @@ -559,7 +591,6 @@ export async function getModelJSON(modelPath, fileName, fatal = true, options =
* @returns {Promise<Uint8Array>} A Promise that resolves with the Uint8Array buffer
*/
async function readResponse(response, progress_callback) {
// Read and track progress when reading a Response object

const contentLength = response.headers.get('Content-Length');
if (contentLength === null) {
Expand Down
Loading