From dac6f4b07388675c23658f9e338270351c74fb3e Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Sat, 23 Sep 2023 03:34:10 +0200 Subject: [PATCH] Improve example browser extension template (#325) Removes unnecessary complexity --- examples/extension/src/background.js | 6 --- examples/extension/src/cache.js | 80 ---------------------------- 2 files changed, 86 deletions(-) delete mode 100644 examples/extension/src/cache.js diff --git a/examples/extension/src/background.js b/examples/extension/src/background.js index cc7dc8692..db3ffee35 100644 --- a/examples/extension/src/background.js +++ b/examples/extension/src/background.js @@ -1,12 +1,6 @@ // background.js - Handles requests from the UI, runs the model, then sends back a response import { pipeline, env } from '@xenova/transformers'; -import { CustomCache } from "./cache.js"; - -// Define caching parameters -env.useBrowserCache = false; -env.useCustomCache = true; -env.customCache = new CustomCache('transformers-cache'); // Skip initial check for local models, since we are not loading any local models. env.allowLocalModels = false; diff --git a/examples/extension/src/cache.js b/examples/extension/src/cache.js deleted file mode 100644 index 833475e0d..000000000 --- a/examples/extension/src/cache.js +++ /dev/null @@ -1,80 +0,0 @@ -// Design a caching API to be used by the extension which implements the same interface as -// the browser's native Cache API (https://developer.mozilla.org/en-US/docs/Web/API/Cache) -// but uses the browser's local storage API (https://developer.chrome.com/docs/extensions/reference/storage/). -// -// Since the local storage API requires all data to be stored as JSON (which doesn't allow some ASCII chars), -// one of the better approaches is to store the response body as a base64-encoded string. This is not ideal, -// as it increases the size of the response body by ~33%, but it's the best we can do with the local storage API. -// See https://stackoverflow.com/a/1443240/13989043 for more information about this. -// -// For serialization (arraybuffer -> string) and unserialization (string -> arraybuffer), -// use the `FileReader` and `Blob` APIs. Although other options are also possible, this approach -// is considered to be better for larger files (like models). -// -// Other references: -// - https://developer.chrome.com/docs/extensions/reference/storage/#property-local -// - https://stackoverflow.com/questions/6965107/converting-between-strings-and-arraybuffers - -export class CustomCache { - /** - * Instantiate a `CustomCache` object. - * @param {string} path - */ - constructor(cacheName) { - this.cacheName = cacheName; - } - - /** - * Checks whether the given request is in the cache. - * @param {Request|string} request - * @returns {Promise} - */ - async match(request) { - const url = request instanceof Request ? request.url : request; - const cached = await chrome.storage.local.get([url]); - - if (cached[url]) { - return await fetch(cached[url]._body); - } else { - return undefined; - } - } - - /** - * Adds the given response to the cache. - * @param {Request|string} request - * @param {Response} response - * @returns {Promise} - */ - async put(request, response) { - const url = request instanceof Request ? request.url : request; - const buffer = await response.arrayBuffer(); - - const body = await new Promise((resolve, reject) => { - const reader = new FileReader(); - reader.onload = e => resolve(e.target.result); - reader.onerror = e => reject(e.target.error); - reader.readAsDataURL(new Blob([buffer], { type: 'application/octet-stream' })); - }); - - try { - await chrome.storage.local.set({ - [url]: { - _body: body, - - // Save original response in case - status: response.status, - statusText: response.statusText, - headers: Object.fromEntries(response.headers.entries()), - url: response.url, - redirected: response.redirected, - type: response.type, - ok: response.ok, - } - }); - - } catch (err) { - console.warn('An error occurred while writing the file to cache:', err) - } - } -}