-
Notifications
You must be signed in to change notification settings - Fork 34
Bug 1679375 - Implement a Storage that uses the web ext storage API #8
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
86e847a
Implement a Storage that users the web ext storage API
de3dcb6
Update size check script with new webext command
ba8bae6
Attend to review comments
223ee43
Fix issues this PR causes with CI
f5ff1ed
Fix bugs and remove null from StorageValue
3af0d39
Attend to review comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import { Store, StorageIndex, StorageValue, StorageObject, isStorageValue } from "storage"; | ||
| import { updateNestedObject, getValueFromNestedObject, deleteKeyFromNestedObject } from "storage/utils"; | ||
| import { isString, isUndefined } from "utils"; | ||
|
|
||
| type WebExtStoreQuery = { [x: string]: { [x: string]: null; } | null; }; | ||
brizental marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Persistent storage implementation based on the Promise-based | ||
| * [storage API](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage) | ||
| * for web extensions. | ||
| * | ||
| * To make sure this implementation works on Chromium based browsers, the user must install the peer dependency | ||
| * [`mozilla/webextension-polyfill`](https://github.com/mozilla/webextension-polyfill). | ||
| */ | ||
| class WebExtStore implements Store { | ||
| private store; | ||
|
|
||
| // The main key under which all other entries will be recorded for this store instance. | ||
| private rootKey: string; | ||
|
|
||
| constructor(rootKey: string) { | ||
| if (typeof browser === "undefined") { | ||
| throw Error( | ||
| `The web extensions store should only be user in a browser extension context. | ||
| If running is a browser different from Firefox, make sure you have installed | ||
| the webextension-polyfill peer dependency. To do so, run \`npm i webextension-polyfill\`.` | ||
| ); | ||
| } | ||
| this.store = browser.storage.local; | ||
| this.rootKey = rootKey; | ||
| } | ||
|
|
||
| async _testOnly_getWholeStore(): Promise<StorageObject> { | ||
| const result = await this.store.get({ [this.rootKey]: {} }); | ||
| return result[this.rootKey]; | ||
| } | ||
|
|
||
| /** | ||
| * Build a query object to retrieve / update a given entry from the storage. | ||
| * | ||
| * @param index The index to the given entry on the storage. | ||
| * | ||
| * @return The query object. | ||
| */ | ||
| private _buildQuery(index: StorageIndex): WebExtStoreQuery { | ||
| let partialQuery = null; | ||
| for (const key of index) { | ||
| partialQuery = { [key]: partialQuery }; | ||
| } | ||
|
|
||
| return { [this.rootKey]: partialQuery }; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the full store and builds a query object on top of it. | ||
| * | ||
| * @param transformFn The transformation function to apply to the store. | ||
| * | ||
| * @returns The query object with the modified store. | ||
| */ | ||
| private async _buildQueryFromStore(transformFn: (s: StorageObject) => StorageObject): Promise<StorageObject> { | ||
| const store = await this._testOnly_getWholeStore(); | ||
brizental marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return { [this.rootKey]: transformFn(store) }; | ||
| } | ||
|
|
||
| async get(index: StorageIndex): Promise<StorageValue> { | ||
| const query = this._buildQuery(index); | ||
| const response: browser.storage.StorageObject = await this.store.get(query); | ||
| if (!response) { | ||
| return; | ||
| } | ||
|
|
||
| if (isStorageValue(response)) { | ||
| if (!isUndefined(response) && !isString(response)) { | ||
brizental marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return getValueFromNestedObject(response, [ this.rootKey, ...index ]); | ||
| } else { | ||
| return response; | ||
| } | ||
| } | ||
|
|
||
| throw new Error( | ||
| `Unexpected value found in storage for index ${index}. Ignoring. | ||
| ${JSON.stringify(response, null, 2)}` | ||
| ); | ||
| } | ||
|
|
||
| async update( | ||
| index: StorageIndex, | ||
| transformFn: (v: StorageValue) => Exclude<StorageValue, undefined> | ||
| ): Promise<void> { | ||
| if (index.length === 0) { | ||
| throw Error("The index must contain at least one property to update."); | ||
| } | ||
|
|
||
| // We need to get the full store object here, change it as requested and then re-save. | ||
| // This is necessary, because if we try to set a key to an inside object on the storage, | ||
| // it will erase any sibling keys that are not mentioned. | ||
| const query = await this._buildQueryFromStore( | ||
| store => updateNestedObject(store, index, transformFn) | ||
| ); | ||
| return this.store.set(query); | ||
| } | ||
|
|
||
| async delete(index: StorageIndex): Promise<void> { | ||
| // The `remove API`[https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/StorageArea/remove] | ||
| // doesn't expose a way for us to delete nested keys. | ||
| // This means we need to get the whole store, | ||
| // make the necessary changes to it locally and then reset it. | ||
| try { | ||
| const query = await this._buildQueryFromStore( | ||
| store => deleteKeyFromNestedObject(store, index) | ||
| ); | ||
| return this.store.set(query); | ||
| } catch(e) { | ||
| console.warn(e.message, "Ignoring"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default WebExtStore; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.