-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow explicit sw de-registering + timebomb (#100)
* feat: allow explicit sw de-registering * feat: create a generic indexDb wrapper * feat: service-workers implode after 24hrs * fix: close DB after calling loadConfigFromLocalStorage * chore: remove registration.update() calls on sw * chore: dont throw error on db.close if already closed * tmp: do not prefetch content * fix: sw handling of request checks
- Loading branch information
Showing
7 changed files
with
219 additions
and
82 deletions.
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
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,16 @@ | ||
import { trace } from './logger.ts' | ||
|
||
// things are wonky with hash routes for deregistering. | ||
export function isDeregisterRequest (url: string): boolean { | ||
const urlObj = new URL(url) | ||
const result = urlObj.search.includes('ipfs-sw-deregister') | ||
trace('isDeregisterRequest: ', url, result) | ||
return result | ||
} | ||
|
||
export function getRedirectUrl (url: string): URL { | ||
const redirectUrl = new URL(url) | ||
redirectUrl.search = '' | ||
redirectUrl.hash = '#/ipfs-sw-config' | ||
return redirectUrl | ||
} |
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,68 @@ | ||
export type BaseDbConfig = Record<string, any> | ||
|
||
type DbKeys<T extends BaseDbConfig> = (keyof T) | ||
type validDbKey<T extends BaseDbConfig, K> = K extends IDBValidKey ? keyof T : never | ||
|
||
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style | ||
interface TypedIDBDatabase<T extends BaseDbConfig> extends IDBDatabase { | ||
get<K extends keyof T>(key: validDbKey<T, K>): Promise<T[K]> | ||
put<K extends DbKeys<T>>(value: T[K], key: validDbKey<T, K>): Promise<void> | ||
store(name: string): IDBObjectStore | ||
|
||
} | ||
|
||
export class GenericIDB<T extends BaseDbConfig> { | ||
private db: TypedIDBDatabase<T> | null = null | ||
|
||
constructor (private readonly dbName: string, private readonly storeName: string) { | ||
} | ||
|
||
#openDatabase = async (): Promise<TypedIDBDatabase<T>> => { | ||
return new Promise((resolve, reject) => { | ||
const request = indexedDB.open(this.dbName, 1) | ||
request.onerror = () => { reject(request.error) } | ||
request.onsuccess = () => { resolve(request.result as TypedIDBDatabase<T>) } | ||
request.onupgradeneeded = (event) => { | ||
const db = request.result | ||
db.createObjectStore(this.storeName) | ||
} | ||
}) | ||
} | ||
|
||
async open (): Promise<void> { | ||
this.db = await this.#openDatabase() | ||
} | ||
|
||
async put <K extends keyof T>(key: any extends K ? never : K & IDBValidKey, value: any extends (K extends keyof T ? T[K] : never) ? never : T[K]): Promise<void> { | ||
if (this.db == null) { | ||
throw new Error('Database not opened') | ||
} | ||
const transaction = this.db.transaction(this.storeName, 'readwrite') | ||
const store = transaction.objectStore(this.storeName) | ||
const request = store.put(value, key) | ||
return new Promise((resolve, reject) => { | ||
request.onerror = () => { reject(request.error) } | ||
request.onsuccess = () => { resolve() } | ||
}) | ||
} | ||
|
||
async get<K extends keyof T> (key: any extends T[K] ? never : K & IDBValidKey): Promise<K extends keyof T ? T[K] : never> { | ||
if (this.db == null) { | ||
throw new Error('Database not opened') | ||
} | ||
const transaction = this.db.transaction(this.storeName, 'readonly') | ||
const store = transaction.objectStore(this.storeName) | ||
const request = store.get(key) as IDBRequest<T[K]> | ||
return new Promise((resolve, reject) => { | ||
request.onerror = () => { reject(request.error) } | ||
request.onsuccess = () => { resolve(request.result) } | ||
}) | ||
} | ||
|
||
close (): void { | ||
if (this.db != null) { | ||
this.db.close() | ||
this.db = null | ||
} | ||
} | ||
} |
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
Oops, something went wrong.