Skip to content

Commit 084b362

Browse files
committed
Add better test for localStorage
Followup from #126 I added a mock `localStorage` for environments where localStorage isn't available, and to remove the 'store' dependency. I've been testing more with [Bun](https://bun.com/) and I found that there, this code doesn't reliably create the mock. I guess in Node trying to access `window.localStorage` would throw an exception, but that's not necessarily the case in other JS runtimes. This commit changes it to use the `if (!('localStorage' in globalThis))` syntax, which should work more reliably everywhere.
1 parent afa6afd commit 084b362

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/osm-auth.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ export function osmAuth(o) {
2727
// Note that accessing localStorage may throw a `SecurityError`, so wrap in a try/catch.
2828
var _store = null;
2929
try {
30-
_store = window.localStorage;
30+
if (!('localStorage' in globalThis)) {
31+
throw new Error('No localStorage');
32+
}
33+
_store = globalThis.localStorage;
34+
3135
} catch (e) {
3236
var _mock = new Map();
3337
_store = {

0 commit comments

Comments
 (0)