@universal-lock/local-storage 1.0.0
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 +21 -0
- package/README.md +81 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.global.js +81 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +83 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +58 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Lucas Rainett
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# @universal-lock/local-storage
|
|
2
|
+
|
|
3
|
+
LocalStorage backend for [`universal-lock`](https://github.com/lucasrainett/universal-lock). Provides cross-tab locking in browsers using `localStorage`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install universal-lock @universal-lock/local-storage
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### ESM
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { lockFactory } from "universal-lock";
|
|
17
|
+
import { createBackend } from "@universal-lock/local-storage";
|
|
18
|
+
|
|
19
|
+
const lock = lockFactory(createBackend());
|
|
20
|
+
|
|
21
|
+
const release = await lock.acquire("my-resource");
|
|
22
|
+
try {
|
|
23
|
+
// critical section — safe across browser tabs
|
|
24
|
+
} finally {
|
|
25
|
+
await release();
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### CommonJS
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
const { lockFactory } = require("universal-lock");
|
|
33
|
+
const { createBackend } = require("@universal-lock/local-storage");
|
|
34
|
+
|
|
35
|
+
const lock = lockFactory(createBackend());
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Browser (IIFE)
|
|
39
|
+
|
|
40
|
+
```html
|
|
41
|
+
<script src="https://unpkg.com/@universal-lock/local-storage/dist/index.global.js"></script>
|
|
42
|
+
<script src="https://unpkg.com/universal-lock/dist/index.global.js"></script>
|
|
43
|
+
<script>
|
|
44
|
+
const lock = UniversalLock.lockFactory(UniversalLockLocalStorage.createBackend());
|
|
45
|
+
</script>
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## API
|
|
49
|
+
|
|
50
|
+
### `createBackend(prefix?)`
|
|
51
|
+
|
|
52
|
+
Creates a localStorage backend instance.
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { createBackend } from "@universal-lock/local-storage";
|
|
56
|
+
|
|
57
|
+
const backend = createBackend(); // default prefix "universal-lock:"
|
|
58
|
+
const backend = createBackend("my-app:"); // custom prefix
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
| Parameter | Type | Default | Description |
|
|
62
|
+
| --------- | -------- | ------------------- | --------------------------------- |
|
|
63
|
+
| `prefix` | `string` | `"universal-lock:"` | Prefix for localStorage key names |
|
|
64
|
+
|
|
65
|
+
## How It Works
|
|
66
|
+
|
|
67
|
+
Locks are stored as JSON in `localStorage` with a timestamp. A compare-and-swap (CAS) pattern is used: after writing a lock entry, it is immediately read back to verify the write succeeded. This significantly reduces race conditions between tabs.
|
|
68
|
+
|
|
69
|
+
## When to Use
|
|
70
|
+
|
|
71
|
+
- Cross-tab locking in browsers that **don't** support the Web Locks API
|
|
72
|
+
- Fallback for older browser environments
|
|
73
|
+
|
|
74
|
+
## Limitations
|
|
75
|
+
|
|
76
|
+
- `localStorage` is **not** truly atomic. The CAS verification reduces but does not eliminate race conditions. For stronger cross-tab guarantees, use [`@universal-lock/web-locks`](https://www.npmjs.com/package/@universal-lock/web-locks).
|
|
77
|
+
- Throws during `setup` if `localStorage` is unavailable (e.g., private browsing in some browsers, server-side rendering).
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
[MIT](https://github.com/lucasrainett/universal-lock/blob/master/LICENSE)
|
package/dist/index.d.mts
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var UniversalLockLocalStorage = (() => {
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
createBackend: () => createBackend
|
|
25
|
+
});
|
|
26
|
+
var DEFAULT_PREFIX = "universal-lock:";
|
|
27
|
+
var createBackend = (prefix = DEFAULT_PREFIX) => {
|
|
28
|
+
const setup = async () => {
|
|
29
|
+
if (typeof localStorage === "undefined") {
|
|
30
|
+
throw new Error("localStorage is not available");
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const read = (lockName) => {
|
|
34
|
+
const raw = localStorage.getItem(prefix + lockName);
|
|
35
|
+
if (!raw) return null;
|
|
36
|
+
return JSON.parse(raw);
|
|
37
|
+
};
|
|
38
|
+
const write = (lockName, entry) => {
|
|
39
|
+
localStorage.setItem(prefix + lockName, JSON.stringify(entry));
|
|
40
|
+
};
|
|
41
|
+
const remove = (lockName) => {
|
|
42
|
+
localStorage.removeItem(prefix + lockName);
|
|
43
|
+
};
|
|
44
|
+
const acquire = async (lockName, stale, lockId) => {
|
|
45
|
+
const now = Date.now();
|
|
46
|
+
const existing = read(lockName);
|
|
47
|
+
const lockExpired = existing && existing.timestamp + stale <= now;
|
|
48
|
+
if (!existing || lockExpired) {
|
|
49
|
+
write(lockName, { lockId, timestamp: now });
|
|
50
|
+
const verification = read(lockName);
|
|
51
|
+
if (!verification || verification.lockId !== lockId) {
|
|
52
|
+
throw new Error(`${lockName} already locked`);
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
throw new Error(`${lockName} already locked`);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const renew = async (lockName, lockId) => {
|
|
59
|
+
const existing = read(lockName);
|
|
60
|
+
if (!existing) throw new Error(`${lockName} not locked`);
|
|
61
|
+
if (existing.lockId !== lockId)
|
|
62
|
+
throw new Error(`${lockName} not owned by caller`);
|
|
63
|
+
write(lockName, { lockId, timestamp: Date.now() });
|
|
64
|
+
};
|
|
65
|
+
const release = async (lockName, lockId) => {
|
|
66
|
+
const existing = read(lockName);
|
|
67
|
+
if (!existing) throw new Error(`${lockName} not locked`);
|
|
68
|
+
if (existing.lockId !== lockId)
|
|
69
|
+
throw new Error(`${lockName} not owned by caller`);
|
|
70
|
+
remove(lockName);
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
setup,
|
|
74
|
+
acquire,
|
|
75
|
+
renew,
|
|
76
|
+
release
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
return __toCommonJS(index_exports);
|
|
80
|
+
})();
|
|
81
|
+
//# sourceMappingURL=index.global.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type {\n\tBackend,\n\tBackendAcquireFunction,\n\tBackendReleaseFunction,\n\tBackendRenewFunction,\n\tBackendSetupFunction,\n\tTimestampLockEntry,\n} from \"@universal-lock/types\";\n\nconst DEFAULT_PREFIX = \"universal-lock:\";\n\nexport const createBackend = (prefix: string = DEFAULT_PREFIX): Backend => {\n\tconst setup: BackendSetupFunction = async () => {\n\t\tif (typeof localStorage === \"undefined\") {\n\t\t\tthrow new Error(\"localStorage is not available\");\n\t\t}\n\t};\n\n\tconst read = (lockName: string): TimestampLockEntry | null => {\n\t\tconst raw = localStorage.getItem(prefix + lockName);\n\t\tif (!raw) return null;\n\t\treturn JSON.parse(raw) as TimestampLockEntry;\n\t};\n\n\tconst write = (lockName: string, entry: TimestampLockEntry): void => {\n\t\tlocalStorage.setItem(prefix + lockName, JSON.stringify(entry));\n\t};\n\n\tconst remove = (lockName: string): void => {\n\t\tlocalStorage.removeItem(prefix + lockName);\n\t};\n\n\t/**\n\t * Acquires a lock using a compare-and-swap pattern.\n\t * Note: localStorage does not support true atomic operations.\n\t * The CAS verification significantly reduces the race window but\n\t * cannot eliminate it entirely. For stronger cross-tab guarantees,\n\t * use the Web Locks API backend instead.\n\t */\n\tconst acquire: BackendAcquireFunction = async (lockName, stale, lockId) => {\n\t\tconst now = Date.now();\n\t\tconst existing = read(lockName);\n\t\tconst lockExpired = existing && existing.timestamp + stale <= now;\n\t\tif (!existing || lockExpired) {\n\t\t\twrite(lockName, { lockId, timestamp: now });\n\t\t\t// Read-back verification: if another tab wrote between our write and read,\n\t\t\t// we'll see their lockId here and know we lost the race\n\t\t\tconst verification = read(lockName);\n\t\t\tif (!verification || verification.lockId !== lockId) {\n\t\t\t\tthrow new Error(`${lockName} already locked`);\n\t\t\t}\n\t\t} else {\n\t\t\tthrow new Error(`${lockName} already locked`);\n\t\t}\n\t};\n\n\tconst renew: BackendRenewFunction = async (lockName, lockId) => {\n\t\tconst existing = read(lockName);\n\t\tif (!existing) throw new Error(`${lockName} not locked`);\n\t\tif (existing.lockId !== lockId)\n\t\t\tthrow new Error(`${lockName} not owned by caller`);\n\t\twrite(lockName, { lockId, timestamp: Date.now() });\n\t};\n\n\tconst release: BackendReleaseFunction = async (lockName, lockId) => {\n\t\tconst existing = read(lockName);\n\t\tif (!existing) throw new Error(`${lockName} not locked`);\n\t\tif (existing.lockId !== lockId)\n\t\t\tthrow new Error(`${lockName} not owned by caller`);\n\t\tremove(lockName);\n\t};\n\n\treturn {\n\t\tsetup,\n\t\tacquire,\n\t\trenew,\n\t\trelease,\n\t};\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AASA,MAAM,iBAAiB;AAEhB,MAAM,gBAAgB,CAAC,SAAiB,mBAA4B;AAC1E,UAAM,QAA8B,YAAY;AAC/C,UAAI,OAAO,iBAAiB,aAAa;AACxC,cAAM,IAAI,MAAM,+BAA+B;AAAA,MAChD;AAAA,IACD;AAEA,UAAM,OAAO,CAAC,aAAgD;AAC7D,YAAM,MAAM,aAAa,QAAQ,SAAS,QAAQ;AAClD,UAAI,CAAC,IAAK,QAAO;AACjB,aAAO,KAAK,MAAM,GAAG;AAAA,IACtB;AAEA,UAAM,QAAQ,CAAC,UAAkB,UAAoC;AACpE,mBAAa,QAAQ,SAAS,UAAU,KAAK,UAAU,KAAK,CAAC;AAAA,IAC9D;AAEA,UAAM,SAAS,CAAC,aAA2B;AAC1C,mBAAa,WAAW,SAAS,QAAQ;AAAA,IAC1C;AASA,UAAM,UAAkC,OAAO,UAAU,OAAO,WAAW;AAC1E,YAAM,MAAM,KAAK,IAAI;AACrB,YAAM,WAAW,KAAK,QAAQ;AAC9B,YAAM,cAAc,YAAY,SAAS,YAAY,SAAS;AAC9D,UAAI,CAAC,YAAY,aAAa;AAC7B,cAAM,UAAU,EAAE,QAAQ,WAAW,IAAI,CAAC;AAG1C,cAAM,eAAe,KAAK,QAAQ;AAClC,YAAI,CAAC,gBAAgB,aAAa,WAAW,QAAQ;AACpD,gBAAM,IAAI,MAAM,GAAG,QAAQ,iBAAiB;AAAA,QAC7C;AAAA,MACD,OAAO;AACN,cAAM,IAAI,MAAM,GAAG,QAAQ,iBAAiB;AAAA,MAC7C;AAAA,IACD;AAEA,UAAM,QAA8B,OAAO,UAAU,WAAW;AAC/D,YAAM,WAAW,KAAK,QAAQ;AAC9B,UAAI,CAAC,SAAU,OAAM,IAAI,MAAM,GAAG,QAAQ,aAAa;AACvD,UAAI,SAAS,WAAW;AACvB,cAAM,IAAI,MAAM,GAAG,QAAQ,sBAAsB;AAClD,YAAM,UAAU,EAAE,QAAQ,WAAW,KAAK,IAAI,EAAE,CAAC;AAAA,IAClD;AAEA,UAAM,UAAkC,OAAO,UAAU,WAAW;AACnE,YAAM,WAAW,KAAK,QAAQ;AAC9B,UAAI,CAAC,SAAU,OAAM,IAAI,MAAM,GAAG,QAAQ,aAAa;AACvD,UAAI,SAAS,WAAW;AACvB,cAAM,IAAI,MAAM,GAAG,QAAQ,sBAAsB;AAClD,aAAO,QAAQ;AAAA,IAChB;AAEA,WAAO;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;","names":[]}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createBackend: () => createBackend
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var DEFAULT_PREFIX = "universal-lock:";
|
|
27
|
+
var createBackend = (prefix = DEFAULT_PREFIX) => {
|
|
28
|
+
const setup = async () => {
|
|
29
|
+
if (typeof localStorage === "undefined") {
|
|
30
|
+
throw new Error("localStorage is not available");
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const read = (lockName) => {
|
|
34
|
+
const raw = localStorage.getItem(prefix + lockName);
|
|
35
|
+
if (!raw) return null;
|
|
36
|
+
return JSON.parse(raw);
|
|
37
|
+
};
|
|
38
|
+
const write = (lockName, entry) => {
|
|
39
|
+
localStorage.setItem(prefix + lockName, JSON.stringify(entry));
|
|
40
|
+
};
|
|
41
|
+
const remove = (lockName) => {
|
|
42
|
+
localStorage.removeItem(prefix + lockName);
|
|
43
|
+
};
|
|
44
|
+
const acquire = async (lockName, stale, lockId) => {
|
|
45
|
+
const now = Date.now();
|
|
46
|
+
const existing = read(lockName);
|
|
47
|
+
const lockExpired = existing && existing.timestamp + stale <= now;
|
|
48
|
+
if (!existing || lockExpired) {
|
|
49
|
+
write(lockName, { lockId, timestamp: now });
|
|
50
|
+
const verification = read(lockName);
|
|
51
|
+
if (!verification || verification.lockId !== lockId) {
|
|
52
|
+
throw new Error(`${lockName} already locked`);
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
throw new Error(`${lockName} already locked`);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const renew = async (lockName, lockId) => {
|
|
59
|
+
const existing = read(lockName);
|
|
60
|
+
if (!existing) throw new Error(`${lockName} not locked`);
|
|
61
|
+
if (existing.lockId !== lockId)
|
|
62
|
+
throw new Error(`${lockName} not owned by caller`);
|
|
63
|
+
write(lockName, { lockId, timestamp: Date.now() });
|
|
64
|
+
};
|
|
65
|
+
const release = async (lockName, lockId) => {
|
|
66
|
+
const existing = read(lockName);
|
|
67
|
+
if (!existing) throw new Error(`${lockName} not locked`);
|
|
68
|
+
if (existing.lockId !== lockId)
|
|
69
|
+
throw new Error(`${lockName} not owned by caller`);
|
|
70
|
+
remove(lockName);
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
setup,
|
|
74
|
+
acquire,
|
|
75
|
+
renew,
|
|
76
|
+
release
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
80
|
+
0 && (module.exports = {
|
|
81
|
+
createBackend
|
|
82
|
+
});
|
|
83
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type {\n\tBackend,\n\tBackendAcquireFunction,\n\tBackendReleaseFunction,\n\tBackendRenewFunction,\n\tBackendSetupFunction,\n\tTimestampLockEntry,\n} from \"@universal-lock/types\";\n\nconst DEFAULT_PREFIX = \"universal-lock:\";\n\nexport const createBackend = (prefix: string = DEFAULT_PREFIX): Backend => {\n\tconst setup: BackendSetupFunction = async () => {\n\t\tif (typeof localStorage === \"undefined\") {\n\t\t\tthrow new Error(\"localStorage is not available\");\n\t\t}\n\t};\n\n\tconst read = (lockName: string): TimestampLockEntry | null => {\n\t\tconst raw = localStorage.getItem(prefix + lockName);\n\t\tif (!raw) return null;\n\t\treturn JSON.parse(raw) as TimestampLockEntry;\n\t};\n\n\tconst write = (lockName: string, entry: TimestampLockEntry): void => {\n\t\tlocalStorage.setItem(prefix + lockName, JSON.stringify(entry));\n\t};\n\n\tconst remove = (lockName: string): void => {\n\t\tlocalStorage.removeItem(prefix + lockName);\n\t};\n\n\t/**\n\t * Acquires a lock using a compare-and-swap pattern.\n\t * Note: localStorage does not support true atomic operations.\n\t * The CAS verification significantly reduces the race window but\n\t * cannot eliminate it entirely. For stronger cross-tab guarantees,\n\t * use the Web Locks API backend instead.\n\t */\n\tconst acquire: BackendAcquireFunction = async (lockName, stale, lockId) => {\n\t\tconst now = Date.now();\n\t\tconst existing = read(lockName);\n\t\tconst lockExpired = existing && existing.timestamp + stale <= now;\n\t\tif (!existing || lockExpired) {\n\t\t\twrite(lockName, { lockId, timestamp: now });\n\t\t\t// Read-back verification: if another tab wrote between our write and read,\n\t\t\t// we'll see their lockId here and know we lost the race\n\t\t\tconst verification = read(lockName);\n\t\t\tif (!verification || verification.lockId !== lockId) {\n\t\t\t\tthrow new Error(`${lockName} already locked`);\n\t\t\t}\n\t\t} else {\n\t\t\tthrow new Error(`${lockName} already locked`);\n\t\t}\n\t};\n\n\tconst renew: BackendRenewFunction = async (lockName, lockId) => {\n\t\tconst existing = read(lockName);\n\t\tif (!existing) throw new Error(`${lockName} not locked`);\n\t\tif (existing.lockId !== lockId)\n\t\t\tthrow new Error(`${lockName} not owned by caller`);\n\t\twrite(lockName, { lockId, timestamp: Date.now() });\n\t};\n\n\tconst release: BackendReleaseFunction = async (lockName, lockId) => {\n\t\tconst existing = read(lockName);\n\t\tif (!existing) throw new Error(`${lockName} not locked`);\n\t\tif (existing.lockId !== lockId)\n\t\t\tthrow new Error(`${lockName} not owned by caller`);\n\t\tremove(lockName);\n\t};\n\n\treturn {\n\t\tsetup,\n\t\tacquire,\n\t\trenew,\n\t\trelease,\n\t};\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,IAAM,iBAAiB;AAEhB,IAAM,gBAAgB,CAAC,SAAiB,mBAA4B;AAC1E,QAAM,QAA8B,YAAY;AAC/C,QAAI,OAAO,iBAAiB,aAAa;AACxC,YAAM,IAAI,MAAM,+BAA+B;AAAA,IAChD;AAAA,EACD;AAEA,QAAM,OAAO,CAAC,aAAgD;AAC7D,UAAM,MAAM,aAAa,QAAQ,SAAS,QAAQ;AAClD,QAAI,CAAC,IAAK,QAAO;AACjB,WAAO,KAAK,MAAM,GAAG;AAAA,EACtB;AAEA,QAAM,QAAQ,CAAC,UAAkB,UAAoC;AACpE,iBAAa,QAAQ,SAAS,UAAU,KAAK,UAAU,KAAK,CAAC;AAAA,EAC9D;AAEA,QAAM,SAAS,CAAC,aAA2B;AAC1C,iBAAa,WAAW,SAAS,QAAQ;AAAA,EAC1C;AASA,QAAM,UAAkC,OAAO,UAAU,OAAO,WAAW;AAC1E,UAAM,MAAM,KAAK,IAAI;AACrB,UAAM,WAAW,KAAK,QAAQ;AAC9B,UAAM,cAAc,YAAY,SAAS,YAAY,SAAS;AAC9D,QAAI,CAAC,YAAY,aAAa;AAC7B,YAAM,UAAU,EAAE,QAAQ,WAAW,IAAI,CAAC;AAG1C,YAAM,eAAe,KAAK,QAAQ;AAClC,UAAI,CAAC,gBAAgB,aAAa,WAAW,QAAQ;AACpD,cAAM,IAAI,MAAM,GAAG,QAAQ,iBAAiB;AAAA,MAC7C;AAAA,IACD,OAAO;AACN,YAAM,IAAI,MAAM,GAAG,QAAQ,iBAAiB;AAAA,IAC7C;AAAA,EACD;AAEA,QAAM,QAA8B,OAAO,UAAU,WAAW;AAC/D,UAAM,WAAW,KAAK,QAAQ;AAC9B,QAAI,CAAC,SAAU,OAAM,IAAI,MAAM,GAAG,QAAQ,aAAa;AACvD,QAAI,SAAS,WAAW;AACvB,YAAM,IAAI,MAAM,GAAG,QAAQ,sBAAsB;AAClD,UAAM,UAAU,EAAE,QAAQ,WAAW,KAAK,IAAI,EAAE,CAAC;AAAA,EAClD;AAEA,QAAM,UAAkC,OAAO,UAAU,WAAW;AACnE,UAAM,WAAW,KAAK,QAAQ;AAC9B,QAAI,CAAC,SAAU,OAAM,IAAI,MAAM,GAAG,QAAQ,aAAa;AACvD,QAAI,SAAS,WAAW;AACvB,YAAM,IAAI,MAAM,GAAG,QAAQ,sBAAsB;AAClD,WAAO,QAAQ;AAAA,EAChB;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;","names":[]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var DEFAULT_PREFIX = "universal-lock:";
|
|
3
|
+
var createBackend = (prefix = DEFAULT_PREFIX) => {
|
|
4
|
+
const setup = async () => {
|
|
5
|
+
if (typeof localStorage === "undefined") {
|
|
6
|
+
throw new Error("localStorage is not available");
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
const read = (lockName) => {
|
|
10
|
+
const raw = localStorage.getItem(prefix + lockName);
|
|
11
|
+
if (!raw) return null;
|
|
12
|
+
return JSON.parse(raw);
|
|
13
|
+
};
|
|
14
|
+
const write = (lockName, entry) => {
|
|
15
|
+
localStorage.setItem(prefix + lockName, JSON.stringify(entry));
|
|
16
|
+
};
|
|
17
|
+
const remove = (lockName) => {
|
|
18
|
+
localStorage.removeItem(prefix + lockName);
|
|
19
|
+
};
|
|
20
|
+
const acquire = async (lockName, stale, lockId) => {
|
|
21
|
+
const now = Date.now();
|
|
22
|
+
const existing = read(lockName);
|
|
23
|
+
const lockExpired = existing && existing.timestamp + stale <= now;
|
|
24
|
+
if (!existing || lockExpired) {
|
|
25
|
+
write(lockName, { lockId, timestamp: now });
|
|
26
|
+
const verification = read(lockName);
|
|
27
|
+
if (!verification || verification.lockId !== lockId) {
|
|
28
|
+
throw new Error(`${lockName} already locked`);
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
throw new Error(`${lockName} already locked`);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const renew = async (lockName, lockId) => {
|
|
35
|
+
const existing = read(lockName);
|
|
36
|
+
if (!existing) throw new Error(`${lockName} not locked`);
|
|
37
|
+
if (existing.lockId !== lockId)
|
|
38
|
+
throw new Error(`${lockName} not owned by caller`);
|
|
39
|
+
write(lockName, { lockId, timestamp: Date.now() });
|
|
40
|
+
};
|
|
41
|
+
const release = async (lockName, lockId) => {
|
|
42
|
+
const existing = read(lockName);
|
|
43
|
+
if (!existing) throw new Error(`${lockName} not locked`);
|
|
44
|
+
if (existing.lockId !== lockId)
|
|
45
|
+
throw new Error(`${lockName} not owned by caller`);
|
|
46
|
+
remove(lockName);
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
setup,
|
|
50
|
+
acquire,
|
|
51
|
+
renew,
|
|
52
|
+
release
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
export {
|
|
56
|
+
createBackend
|
|
57
|
+
};
|
|
58
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type {\n\tBackend,\n\tBackendAcquireFunction,\n\tBackendReleaseFunction,\n\tBackendRenewFunction,\n\tBackendSetupFunction,\n\tTimestampLockEntry,\n} from \"@universal-lock/types\";\n\nconst DEFAULT_PREFIX = \"universal-lock:\";\n\nexport const createBackend = (prefix: string = DEFAULT_PREFIX): Backend => {\n\tconst setup: BackendSetupFunction = async () => {\n\t\tif (typeof localStorage === \"undefined\") {\n\t\t\tthrow new Error(\"localStorage is not available\");\n\t\t}\n\t};\n\n\tconst read = (lockName: string): TimestampLockEntry | null => {\n\t\tconst raw = localStorage.getItem(prefix + lockName);\n\t\tif (!raw) return null;\n\t\treturn JSON.parse(raw) as TimestampLockEntry;\n\t};\n\n\tconst write = (lockName: string, entry: TimestampLockEntry): void => {\n\t\tlocalStorage.setItem(prefix + lockName, JSON.stringify(entry));\n\t};\n\n\tconst remove = (lockName: string): void => {\n\t\tlocalStorage.removeItem(prefix + lockName);\n\t};\n\n\t/**\n\t * Acquires a lock using a compare-and-swap pattern.\n\t * Note: localStorage does not support true atomic operations.\n\t * The CAS verification significantly reduces the race window but\n\t * cannot eliminate it entirely. For stronger cross-tab guarantees,\n\t * use the Web Locks API backend instead.\n\t */\n\tconst acquire: BackendAcquireFunction = async (lockName, stale, lockId) => {\n\t\tconst now = Date.now();\n\t\tconst existing = read(lockName);\n\t\tconst lockExpired = existing && existing.timestamp + stale <= now;\n\t\tif (!existing || lockExpired) {\n\t\t\twrite(lockName, { lockId, timestamp: now });\n\t\t\t// Read-back verification: if another tab wrote between our write and read,\n\t\t\t// we'll see their lockId here and know we lost the race\n\t\t\tconst verification = read(lockName);\n\t\t\tif (!verification || verification.lockId !== lockId) {\n\t\t\t\tthrow new Error(`${lockName} already locked`);\n\t\t\t}\n\t\t} else {\n\t\t\tthrow new Error(`${lockName} already locked`);\n\t\t}\n\t};\n\n\tconst renew: BackendRenewFunction = async (lockName, lockId) => {\n\t\tconst existing = read(lockName);\n\t\tif (!existing) throw new Error(`${lockName} not locked`);\n\t\tif (existing.lockId !== lockId)\n\t\t\tthrow new Error(`${lockName} not owned by caller`);\n\t\twrite(lockName, { lockId, timestamp: Date.now() });\n\t};\n\n\tconst release: BackendReleaseFunction = async (lockName, lockId) => {\n\t\tconst existing = read(lockName);\n\t\tif (!existing) throw new Error(`${lockName} not locked`);\n\t\tif (existing.lockId !== lockId)\n\t\t\tthrow new Error(`${lockName} not owned by caller`);\n\t\tremove(lockName);\n\t};\n\n\treturn {\n\t\tsetup,\n\t\tacquire,\n\t\trenew,\n\t\trelease,\n\t};\n};\n"],"mappings":";AASA,IAAM,iBAAiB;AAEhB,IAAM,gBAAgB,CAAC,SAAiB,mBAA4B;AAC1E,QAAM,QAA8B,YAAY;AAC/C,QAAI,OAAO,iBAAiB,aAAa;AACxC,YAAM,IAAI,MAAM,+BAA+B;AAAA,IAChD;AAAA,EACD;AAEA,QAAM,OAAO,CAAC,aAAgD;AAC7D,UAAM,MAAM,aAAa,QAAQ,SAAS,QAAQ;AAClD,QAAI,CAAC,IAAK,QAAO;AACjB,WAAO,KAAK,MAAM,GAAG;AAAA,EACtB;AAEA,QAAM,QAAQ,CAAC,UAAkB,UAAoC;AACpE,iBAAa,QAAQ,SAAS,UAAU,KAAK,UAAU,KAAK,CAAC;AAAA,EAC9D;AAEA,QAAM,SAAS,CAAC,aAA2B;AAC1C,iBAAa,WAAW,SAAS,QAAQ;AAAA,EAC1C;AASA,QAAM,UAAkC,OAAO,UAAU,OAAO,WAAW;AAC1E,UAAM,MAAM,KAAK,IAAI;AACrB,UAAM,WAAW,KAAK,QAAQ;AAC9B,UAAM,cAAc,YAAY,SAAS,YAAY,SAAS;AAC9D,QAAI,CAAC,YAAY,aAAa;AAC7B,YAAM,UAAU,EAAE,QAAQ,WAAW,IAAI,CAAC;AAG1C,YAAM,eAAe,KAAK,QAAQ;AAClC,UAAI,CAAC,gBAAgB,aAAa,WAAW,QAAQ;AACpD,cAAM,IAAI,MAAM,GAAG,QAAQ,iBAAiB;AAAA,MAC7C;AAAA,IACD,OAAO;AACN,YAAM,IAAI,MAAM,GAAG,QAAQ,iBAAiB;AAAA,IAC7C;AAAA,EACD;AAEA,QAAM,QAA8B,OAAO,UAAU,WAAW;AAC/D,UAAM,WAAW,KAAK,QAAQ;AAC9B,QAAI,CAAC,SAAU,OAAM,IAAI,MAAM,GAAG,QAAQ,aAAa;AACvD,QAAI,SAAS,WAAW;AACvB,YAAM,IAAI,MAAM,GAAG,QAAQ,sBAAsB;AAClD,UAAM,UAAU,EAAE,QAAQ,WAAW,KAAK,IAAI,EAAE,CAAC;AAAA,EAClD;AAEA,QAAM,UAAkC,OAAO,UAAU,WAAW;AACnE,UAAM,WAAW,KAAK,QAAQ;AAC9B,QAAI,CAAC,SAAU,OAAM,IAAI,MAAM,GAAG,QAAQ,aAAa;AACvD,QAAI,SAAS,WAAW;AACvB,YAAM,IAAI,MAAM,GAAG,QAAQ,sBAAsB;AAClD,WAAO,QAAQ;AAAA,EAChB;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@universal-lock/local-storage",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "LocalStorage backend for universal-lock",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/index.mjs",
|
|
8
|
+
"browser": "dist/index.global.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": {
|
|
13
|
+
"types": "./dist/index.d.mts",
|
|
14
|
+
"default": "./dist/index.mjs"
|
|
15
|
+
},
|
|
16
|
+
"require": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"/dist"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/lucasrainett/universal-lock",
|
|
28
|
+
"directory": "packages/local-storage"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"universal-lock",
|
|
32
|
+
"lock",
|
|
33
|
+
"mutex",
|
|
34
|
+
"localstorage",
|
|
35
|
+
"browser",
|
|
36
|
+
"backend"
|
|
37
|
+
],
|
|
38
|
+
"author": {
|
|
39
|
+
"name": "Lucas Rainett",
|
|
40
|
+
"email": "lucas@rainett.dev",
|
|
41
|
+
"url": "https://github.com/lucasrainett"
|
|
42
|
+
},
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@universal-lock/types": "1.0.0"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsup",
|
|
49
|
+
"clean": "rm -rf dist"
|
|
50
|
+
}
|
|
51
|
+
}
|