This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 829
Extract functions for service worker usage, and add initial MSC3916 playwright test (when supported) #12414
Merged
Merged
Extract functions for service worker usage, and add initial MSC3916 playwright test (when supported) #12414
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
d420547
Send user credentials to service worker for MSC3916 authentication
turt2live 31c40c4
appease linter
turt2live c5b18f3
Add initial test
turt2live bbb64a4
Remove unsafe access token code
turt2live 853c520
Merge branch 'develop' into travis/msc3916
turt2live 24598e5
Split out base IDB operations to avoid importing `document` in servic…
turt2live b71745c
Use safe crypto access for service workers
turt2live eb782ed
Fix tests/unsafe access
turt2live 2cbb92b
Remove backwards compatibility layer & appease linter
turt2live d83cf9c
Add docs
turt2live fc893fe
Fix tests
turt2live 0c2695d
Appease the linter
turt2live 1392d90
Iterate tests
turt2live e74752c
Merge remote-tracking branch 'origin/develop' into travis/msc3916
turt2live 2ecd138
Factor out pickle key handling for service workers
turt2live d80e603
Enable everything we can about service workers
turt2live 43a435b
Appease the linter
turt2live 8fe3bae
Add docs
turt2live 2da073c
Rename win32 image to linux in hopes of it just working
turt2live 8f48fb2
Use actual image
turt2live 8f63a21
Merge remote-tracking branch 'origin/develop' into travis/msc3916
turt2live 8f36a43
Apply suggestions from code review
turt2live 797d305
Improve documentation
turt2live 42914a5
Merge branch 'travis/msc3916' of https://github.com/matrix-org/matrix…
turt2live 461d326
Document `??` not working
turt2live 4cfa064
Merge branch 'develop' into travis/msc3916
turt2live c8a8cd1
Merge branch 'develop' into travis/msc3916
turt2live 4e46911
Merge branch 'develop' into travis/msc3916
turt2live b2867e8
Merge branch 'develop' into travis/msc3916
turt2live 3f3be42
Try to appease the tests
turt2live b26c34f
Add some notes
turt2live 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 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 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
Binary file added
BIN
+36.9 KB
.../snapshots/timeline/timeline.spec.ts/image-in-timeline-default-layout-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 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 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,132 @@ | ||
/* | ||
Copyright 2019-2021, 2024 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Retrieves the IndexedDB factory object. | ||
* | ||
* @returns {IDBFactory | undefined} The IndexedDB factory object if available, or undefined if it is not supported. | ||
*/ | ||
export function getIDBFactory(): IDBFactory | undefined { | ||
// IndexedDB loading is lazy for easier testing. | ||
|
||
// just *accessing* _indexedDB throws an exception in firefox with | ||
// indexeddb disabled. | ||
try { | ||
// `self` is preferred for service workers, which access this file's functions. | ||
// We check `self` first because `window` returns something which doesn't work for service workers. | ||
// Note: `self?.indexedDB ?? window.indexedDB` breaks in service workers for unknown reasons. | ||
return self?.indexedDB ? self.indexedDB : window.indexedDB; | ||
} catch (e) {} | ||
} | ||
|
||
let idb: IDBDatabase | null = null; | ||
|
||
async function idbInit(): Promise<void> { | ||
if (!getIDBFactory()) { | ||
throw new Error("IndexedDB not available"); | ||
} | ||
idb = await new Promise((resolve, reject) => { | ||
const request = getIDBFactory()!.open("matrix-react-sdk", 1); | ||
request.onerror = reject; | ||
request.onsuccess = (): void => { | ||
resolve(request.result); | ||
}; | ||
request.onupgradeneeded = (): void => { | ||
const db = request.result; | ||
db.createObjectStore("pickleKey"); | ||
db.createObjectStore("account"); | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* Loads an item from an IndexedDB table within the underlying `matrix-react-sdk` database. | ||
* | ||
* If IndexedDB access is not supported in the environment, an error is thrown. | ||
* | ||
* @param {string} table The name of the object store in IndexedDB. | ||
* @param {string | string[]} key The key where the data is stored. | ||
* @returns {Promise<any>} A promise that resolves with the retrieved item from the table. | ||
*/ | ||
export async function idbLoad(table: string, key: string | string[]): Promise<any> { | ||
if (!idb) { | ||
await idbInit(); | ||
} | ||
return new Promise((resolve, reject) => { | ||
const txn = idb!.transaction([table], "readonly"); | ||
txn.onerror = reject; | ||
|
||
const objectStore = txn.objectStore(table); | ||
const request = objectStore.get(key); | ||
request.onerror = reject; | ||
request.onsuccess = (event): void => { | ||
resolve(request.result); | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* Saves data to an IndexedDB table within the underlying `matrix-react-sdk` database. | ||
* | ||
* If IndexedDB access is not supported in the environment, an error is thrown. | ||
* | ||
* @param {string} table The name of the object store in the IndexedDB. | ||
* @param {string|string[]} key The key to use for storing the data. | ||
* @param {*} data The data to be saved. | ||
* @returns {Promise<void>} A promise that resolves when the data is saved successfully. | ||
*/ | ||
export async function idbSave(table: string, key: string | string[], data: any): Promise<void> { | ||
if (!idb) { | ||
await idbInit(); | ||
} | ||
return new Promise((resolve, reject) => { | ||
const txn = idb!.transaction([table], "readwrite"); | ||
txn.onerror = reject; | ||
|
||
const objectStore = txn.objectStore(table); | ||
const request = objectStore.put(data, key); | ||
request.onerror = reject; | ||
request.onsuccess = (event): void => { | ||
resolve(); | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* Deletes a record from an IndexedDB table within the underlying `matrix-react-sdk` database. | ||
* | ||
* If IndexedDB access is not supported in the environment, an error is thrown. | ||
* | ||
* @param {string} table The name of the object store where the record is stored. | ||
* @param {string|string[]} key The key of the record to be deleted. | ||
* @returns {Promise<void>} A Promise that resolves when the record(s) have been successfully deleted. | ||
*/ | ||
export async function idbDelete(table: string, key: string | string[]): Promise<void> { | ||
if (!idb) { | ||
await idbInit(); | ||
} | ||
return new Promise((resolve, reject) => { | ||
const txn = idb!.transaction([table], "readwrite"); | ||
txn.onerror = reject; | ||
|
||
const objectStore = txn.objectStore(table); | ||
const request = objectStore.delete(key); | ||
request.onerror = reject; | ||
request.onsuccess = (): void => { | ||
resolve(); | ||
}; | ||
}); | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reasons I have not yet been able to comprehend, this does not work. It needs to be a ternary at best.
I'm not sure why. I agree it shouldn't matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[is self.indexedDB falsy but not-nullish, maybe? In which case maybe
self?.indexedDB || window.indexedDB
would do the right thing? Not that that is necessarily better than the ternary.]There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly, yea. I personally have mild fear that JS will convert every single
||
into a boolean rather than a useful object, but it is pretty reliable in practice.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
||
does not convert anything into a boolean in JS. But I agree that's not completely obvious to the casual reader, hence "Not that that is necessarily better than the ternary." I don't have a strong preference for||
vs?:
; I'm just musing about the problem.