@tereo-io/react 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +167 -2
- package/dist/api.d.ts +11 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +62 -0
- package/dist/api.js.map +1 -0
- package/dist/config.d.ts +69 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +41 -0
- package/dist/config.js.map +1 -0
- package/dist/events.d.ts +48 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +129 -0
- package/dist/events.js.map +1 -0
- package/dist/hooks.d.ts +21 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +46 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +3 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +9 -0
- package/dist/logger.js.map +1 -0
- package/dist/markers.d.ts +7 -0
- package/dist/markers.d.ts.map +1 -0
- package/dist/markers.js +15 -0
- package/dist/markers.js.map +1 -0
- package/dist/permissions.d.ts +4 -0
- package/dist/permissions.d.ts.map +1 -0
- package/dist/permissions.js +64 -0
- package/dist/permissions.js.map +1 -0
- package/dist/provider.d.ts +30 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +213 -0
- package/dist/provider.js.map +1 -0
- package/dist/refresh.d.ts +15 -0
- package/dist/refresh.d.ts.map +1 -0
- package/dist/refresh.js +98 -0
- package/dist/refresh.js.map +1 -0
- package/dist/signIn.d.ts +13 -0
- package/dist/signIn.d.ts.map +1 -0
- package/dist/signIn.js +134 -0
- package/dist/signIn.js.map +1 -0
- package/dist/signOut.d.ts +8 -0
- package/dist/signOut.d.ts.map +1 -0
- package/dist/signOut.js +69 -0
- package/dist/signOut.js.map +1 -0
- package/dist/storage.d.ts +6 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +73 -0
- package/dist/storage.js.map +1 -0
- package/dist/storageLock.d.ts +14 -0
- package/dist/storageLock.d.ts.map +1 -0
- package/dist/storageLock.js +100 -0
- package/dist/storageLock.js.map +1 -0
- package/dist/tokens.d.ts +13 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +27 -0
- package/dist/tokens.js.map +1 -0
- package/package.json +66 -4
- package/src/api.ts +82 -0
- package/src/config.ts +108 -0
- package/src/events.ts +184 -0
- package/src/hooks.ts +62 -0
- package/src/index.ts +34 -0
- package/src/logger.ts +9 -0
- package/src/markers.ts +26 -0
- package/src/permissions.ts +77 -0
- package/src/provider.tsx +291 -0
- package/src/refresh.ts +120 -0
- package/src/signIn.ts +168 -0
- package/src/signOut.ts +85 -0
- package/src/storage.ts +80 -0
- package/src/storageLock.ts +111 -0
- package/src/tokens.ts +38 -0
package/src/storage.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { useCallback, useSyncExternalStore } from "react";
|
|
2
|
+
|
|
3
|
+
const listeners = new Map<string, Set<() => void>>();
|
|
4
|
+
|
|
5
|
+
const notify = (key: string): void => {
|
|
6
|
+
listeners.get(key)?.forEach((fn) => fn());
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const readRaw = (key: string): string | null => {
|
|
10
|
+
try {
|
|
11
|
+
return localStorage.getItem(key);
|
|
12
|
+
} catch {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const readJson = <T>(key: string): T | null => {
|
|
18
|
+
const raw = readRaw(key);
|
|
19
|
+
if (raw === null) return null;
|
|
20
|
+
try {
|
|
21
|
+
return JSON.parse(raw) as T;
|
|
22
|
+
} catch {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const writeJson = (key: string, value: unknown): void => {
|
|
28
|
+
try {
|
|
29
|
+
localStorage.setItem(key, JSON.stringify(value));
|
|
30
|
+
} catch {
|
|
31
|
+
// Storage unavailable: the session lives only until the page closes.
|
|
32
|
+
}
|
|
33
|
+
notify(key);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const removeKey = (key: string): void => {
|
|
37
|
+
try {
|
|
38
|
+
localStorage.removeItem(key);
|
|
39
|
+
} catch {
|
|
40
|
+
// See writeJson.
|
|
41
|
+
}
|
|
42
|
+
notify(key);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// `storage` events fire only in other tabs, so same-tab writes notify directly.
|
|
46
|
+
export const subscribeKey = (key: string, fn: () => void): (() => void) => {
|
|
47
|
+
let set = listeners.get(key);
|
|
48
|
+
if (!set) listeners.set(key, (set = new Set()));
|
|
49
|
+
set.add(fn);
|
|
50
|
+
|
|
51
|
+
const onStorage = (e: StorageEvent) => {
|
|
52
|
+
if (e.key === key || e.key === null) fn();
|
|
53
|
+
};
|
|
54
|
+
window.addEventListener("storage", onStorage);
|
|
55
|
+
|
|
56
|
+
return () => {
|
|
57
|
+
set.delete(fn);
|
|
58
|
+
window.removeEventListener("storage", onStorage);
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// useSyncExternalStore compares snapshots by identity, so the raw string is the
|
|
63
|
+
// snapshot and the parse happens after it.
|
|
64
|
+
export const useStoredValue = <T>(key: string): T | null => {
|
|
65
|
+
const subscribe = useCallback(
|
|
66
|
+
(fn: () => void) => subscribeKey(key, fn),
|
|
67
|
+
[key]
|
|
68
|
+
);
|
|
69
|
+
const raw = useSyncExternalStore(
|
|
70
|
+
subscribe,
|
|
71
|
+
() => readRaw(key),
|
|
72
|
+
() => null
|
|
73
|
+
);
|
|
74
|
+
if (raw === null) return null;
|
|
75
|
+
try {
|
|
76
|
+
return JSON.parse(raw) as T;
|
|
77
|
+
} catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A cross-tab lock over localStorage, for where `navigator.locks` is missing
|
|
3
|
+
* (outside secure contexts: plain-http hosts). The key holds a lease — the
|
|
4
|
+
* owner and when it lapses — renewed while the holder runs, so a tab that
|
|
5
|
+
* closes mid-refresh blocks the others only until its lease lapses. Tabs
|
|
6
|
+
* waiting on it wake on the `storage` event, or poll.
|
|
7
|
+
*
|
|
8
|
+
* localStorage has no compare-and-set: a tab writes its lease, waits a beat,
|
|
9
|
+
* and holds the lock only if its write is still the one there (the last of
|
|
10
|
+
* racing writers wins, and the others see it).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
interface Lease {
|
|
14
|
+
owner: string;
|
|
15
|
+
expiresAt: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const LEASE_MS = 15_000;
|
|
19
|
+
const RENEW_MS = 5_000;
|
|
20
|
+
const POLL_MS = 250;
|
|
21
|
+
const SETTLE_MS = 50;
|
|
22
|
+
|
|
23
|
+
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
24
|
+
|
|
25
|
+
const readLease = (key: string): Lease | null => {
|
|
26
|
+
try {
|
|
27
|
+
const raw = localStorage.getItem(key);
|
|
28
|
+
if (!raw) return null;
|
|
29
|
+
const lease = JSON.parse(raw) as Partial<Lease>;
|
|
30
|
+
return typeof lease.owner === "string" &&
|
|
31
|
+
typeof lease.expiresAt === "number"
|
|
32
|
+
? (lease as Lease)
|
|
33
|
+
: null;
|
|
34
|
+
} catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** False when localStorage is unusable (blocked, full, private mode). */
|
|
40
|
+
const writeLease = (key: string, owner: string): boolean => {
|
|
41
|
+
try {
|
|
42
|
+
localStorage.setItem(
|
|
43
|
+
key,
|
|
44
|
+
JSON.stringify({ owner, expiresAt: Date.now() + LEASE_MS })
|
|
45
|
+
);
|
|
46
|
+
return true;
|
|
47
|
+
} catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const releaseLease = (key: string, owner: string) => {
|
|
53
|
+
try {
|
|
54
|
+
if (readLease(key)?.owner === owner) localStorage.removeItem(key);
|
|
55
|
+
} catch {
|
|
56
|
+
// Nothing to release into; the lease lapses on its own.
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** Resolves when another tab changes the key, or after `ms`. */
|
|
61
|
+
const waitForChange = (key: string, ms: number) =>
|
|
62
|
+
new Promise<void>((resolve) => {
|
|
63
|
+
const done = () => {
|
|
64
|
+
clearTimeout(timer);
|
|
65
|
+
window.removeEventListener("storage", onStorage);
|
|
66
|
+
resolve();
|
|
67
|
+
};
|
|
68
|
+
const onStorage = (e: StorageEvent) => {
|
|
69
|
+
if (e.key === key || e.key === null) done();
|
|
70
|
+
};
|
|
71
|
+
const timer = setTimeout(done, ms);
|
|
72
|
+
window.addEventListener("storage", onStorage);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const newOwnerId = () =>
|
|
76
|
+
`${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Resolves once this tab holds the lease, or at once when localStorage is
|
|
80
|
+
* unusable (then nothing can be shared, and the caller runs unlocked).
|
|
81
|
+
*/
|
|
82
|
+
const acquire = async (key: string, owner: string): Promise<void> => {
|
|
83
|
+
for (;;) {
|
|
84
|
+
const lease = readLease(key);
|
|
85
|
+
if (!lease || lease.expiresAt <= Date.now()) {
|
|
86
|
+
if (!writeLease(key, owner)) return;
|
|
87
|
+
await sleep(SETTLE_MS);
|
|
88
|
+
if (readLease(key)?.owner === owner) return;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
await waitForChange(key, Math.min(POLL_MS, lease.expiresAt - Date.now()));
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/** Runs `fn` while holding the lock named `key`, across this origin's tabs. */
|
|
96
|
+
export const withStorageLock = async <T>(
|
|
97
|
+
key: string,
|
|
98
|
+
fn: () => Promise<T>
|
|
99
|
+
): Promise<T> => {
|
|
100
|
+
const owner = newOwnerId();
|
|
101
|
+
await acquire(key, owner);
|
|
102
|
+
const renew = setInterval(() => {
|
|
103
|
+
if (readLease(key)?.owner === owner) writeLease(key, owner);
|
|
104
|
+
}, RENEW_MS);
|
|
105
|
+
try {
|
|
106
|
+
return await fn();
|
|
107
|
+
} finally {
|
|
108
|
+
clearInterval(renew);
|
|
109
|
+
releaseLease(key, owner);
|
|
110
|
+
}
|
|
111
|
+
};
|
package/src/tokens.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { readJson, removeKey, writeJson } from "./storage.js";
|
|
2
|
+
import { activeConfig } from "./config.js";
|
|
3
|
+
|
|
4
|
+
export interface StoredTokens {
|
|
5
|
+
access_token: string;
|
|
6
|
+
id_token?: string;
|
|
7
|
+
token_type: string;
|
|
8
|
+
expires_in: number;
|
|
9
|
+
scope: string;
|
|
10
|
+
expiresAt: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Read from storage rather than hook state: mutation closures can hold a
|
|
14
|
+
// stale render's value.
|
|
15
|
+
export const readTokens = (): StoredTokens | null =>
|
|
16
|
+
readJson<StoredTokens>(activeConfig().keys.tokens);
|
|
17
|
+
|
|
18
|
+
export const writeTokens = (t: StoredTokens | null): void => {
|
|
19
|
+
const { tokens } = activeConfig().keys;
|
|
20
|
+
if (t === null) removeKey(tokens);
|
|
21
|
+
else writeJson(tokens, t);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** Reads a JWT's claims without verifying it. */
|
|
25
|
+
export const decodeJwt = (token: string): Record<string, unknown> | null => {
|
|
26
|
+
try {
|
|
27
|
+
const parts = token.split(".");
|
|
28
|
+
if (parts.length !== 3) return null;
|
|
29
|
+
const binary = atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"));
|
|
30
|
+
const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
|
|
31
|
+
return JSON.parse(new TextDecoder().decode(bytes)) as Record<
|
|
32
|
+
string,
|
|
33
|
+
unknown
|
|
34
|
+
>;
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
};
|