Skip to content

Commit

Permalink
Fix Firefox bug when displaying progress events while reading file fr…
Browse files Browse the repository at this point in the history
…om browser cache (#374)

* Fix firefox bug

Do not display progress events when reading from browser cache in Firefox.

* Typo
  • Loading branch information
xenova authored Nov 8, 2023
1 parent e457c4b commit f7321ad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
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

0 comments on commit f7321ad

Please sign in to comment.