Skip to content

Commit

Permalink
feat: add a localStorage shim to the service worker so the AtProto OA…
Browse files Browse the repository at this point in the history
…uth client doesn't error because it's not defined.
  • Loading branch information
zicklag committed Dec 29, 2024
1 parent a4d4564 commit 471bb2e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
41 changes: 41 additions & 0 deletions src/sw.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,51 @@
/// <reference lib="WebWorker" />

import { kvsIndexedDB } from '@kvs/indexeddb';
import { MatrixShim } from './matrix-shim';

export type {};
declare const self: ServiceWorkerGlobalScope;

// TODO: This might be a horrible local storage shim. I don't know how it handles multiple tabs
// open.
// Works for now... 🤞 We just need it so that the atproto/oauth-client-browser doesn't panic because
// localStorage isn't defined.
const localStorageShimStore = kvsIndexedDB<{ data: string }>({
name: 'localStorage-shim',
version: 1,
});
globalThis.localStorage = {
data: {} as { [key: string]: string },
persist() {
localStorageShimStore.then((s) => {
s.set('data', JSON.stringify(this.data));
});
},
clear() {
this.data = {};
},
getItem(s: string): string | null {
return this.data[s] || null;
},
key(idx: number): string | null {
return (Object.values(this.data)[idx] as string | undefined) || null;
},
get length(): number {
return Object.values(this.data).length;
},
removeItem(key: string) {
this.data[key] = undefined;
this.persist();
},
setItem(key: string, value: string) {
this.data[key] = value;
this.persist()
},
};
localStorageShimStore.then(async (s) => {
globalThis.localStorage.data = JSON.parse((await s.get('data')) || '{}');
});

// async function askForAccessToken(client: Client): Promise<string | undefined> {
// return new Promise((resolve) => {
// const responseKey = Math.random().toString(36);
Expand Down

0 comments on commit 471bb2e

Please sign in to comment.