@wxn0brp/db-lock 0.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 wxn0brP
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,55 @@
1
+ # @wxn0brp/db-lock
2
+
3
+ Designed to safely handle multiple processes accessing the same ValtheraDB instance simultaneously
4
+
5
+ ## Overview
6
+
7
+ This package provides a locking mechanism that ensures thread-safe database operations by using file-based locks. It's designed to work with the ValtheraDB ecosystem and prevents concurrent write operations that could lead to data corruption or inconsistencies.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ yarn add github:wxn0brP/ValtheraDB-lock#dist
13
+ ```
14
+
15
+ ## Features
16
+
17
+ - **File-based locks**: Uses filesystem operations to coordinate locks
18
+ - **Stale lock detection**: Automatically removes lock files that have exceeded the stale threshold
19
+ - **Retry mechanism**: Configurable retry count and timing for lock acquisition
20
+ - **Proxy-based**: Transparently wraps database operations without changing your code
21
+ - **Lightweight**: Minimal overhead with efficient lock management
22
+
23
+ ## Usage
24
+
25
+ ```typescript
26
+ import { createLock } from "@wxn0brp/db-lock";
27
+ import { ValtheraClass } from "@wxn0brp/db-core";
28
+
29
+ const db = new ValtheraClass();
30
+ const lockedDb = createLock(db);
31
+
32
+ // Now use lockedDb as you would use your original database instance
33
+ // Locks will be automatically acquired for write operations and released afterwards
34
+ ```
35
+
36
+ ## Configuration Options
37
+
38
+ | Option | Type | Default | Description |
39
+ |-------------|---------|------------|-------------|
40
+ | `file` | string | "valthera.lock" | Path to the lock file |
41
+ | `stale` | number | 5000 | Time in milliseconds after which a lock is considered stale and can be removed |
42
+ | `retryTime` | number | 50 | Time in milliseconds to wait between lock acquisition attempts |
43
+ | `retryCount`| number | 50 | Number of retry attempts before throwing an error |
44
+
45
+ ## How It Works
46
+
47
+ The locking mechanism works by creating a lock file when a database operation begins and removing it when the operation completes. Operations that try to access the database while a lock exists will wait until the lock is released.
48
+
49
+ The library automatically detects and removes stale locks that have been held longer than the configured stale threshold, preventing indefinite blocking (e.g., if a lock process is crashed).
50
+
51
+ The proxy only intercepts operations that contain "add", "find", "remove", or "update" in their method names, allowing other database methods to pass through unaffected.
52
+
53
+ ## License
54
+
55
+ MIT
@@ -0,0 +1,8 @@
1
+ import { ValtheraCompatible } from "@wxn0brp/db-core";
2
+ export interface LockOpts {
3
+ file?: string;
4
+ stale?: number;
5
+ retryTime?: number;
6
+ retryCount?: number;
7
+ }
8
+ export declare function createLock<T extends ValtheraCompatible>(db: T, opts?: LockOpts): T;
package/dist/index.js ADDED
@@ -0,0 +1,57 @@
1
+ import { unlink, open, stat } from "fs/promises";
2
+ const lockOp = ["add", "find", "remove", "update", "toggle"];
3
+ const hasOp = (op) => lockOp.some(v => op.includes(v));
4
+ async function unlock(lockFile) {
5
+ try {
6
+ await unlink(lockFile);
7
+ }
8
+ catch { }
9
+ }
10
+ async function waitLock(opts) {
11
+ let i = 0;
12
+ while (true) {
13
+ if (opts.retryCount && i++ > opts.retryCount) {
14
+ throw new Error("Failed to acquire lock");
15
+ }
16
+ try {
17
+ const fd = await open(opts.file, "wx");
18
+ await fd.close();
19
+ break;
20
+ }
21
+ catch {
22
+ try {
23
+ const s = await stat(opts.file);
24
+ if (Date.now() - s.mtimeMs > opts.stale) {
25
+ await unlink(opts.file);
26
+ }
27
+ }
28
+ catch { }
29
+ await new Promise(r => setTimeout(r, opts.retryTime || 50));
30
+ }
31
+ }
32
+ }
33
+ export function createLock(db, opts = {}) {
34
+ opts = {
35
+ file: "valthera.lock",
36
+ stale: 5000,
37
+ retryTime: 50,
38
+ retryCount: 50,
39
+ ...opts
40
+ };
41
+ return new Proxy(db, {
42
+ get: (target, prop) => {
43
+ if (!hasOp(prop.toString()))
44
+ return target[prop];
45
+ const origMethod = target[prop];
46
+ return async (...args) => {
47
+ await waitLock(opts);
48
+ try {
49
+ return await origMethod.bind(target)(...args);
50
+ }
51
+ finally {
52
+ await unlock(opts.file);
53
+ }
54
+ };
55
+ }
56
+ });
57
+ }
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@wxn0brp/db-lock",
3
+ "version": "0.0.1",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "description": "Provides file-based locking for thread-safe ValtheraDB operations.",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/wxn0brP/ValtheraDB-lock.git"
10
+ },
11
+ "homepage": "https://github.com/wxn0brP/ValtheraDB-lock",
12
+ "author": "wxn0brP",
13
+ "license": "MIT",
14
+ "type": "module",
15
+ "scripts": {
16
+ "build": "tsc && tsc-alias"
17
+ },
18
+ "devDependencies": {
19
+ "@types/bun": "*",
20
+ "@wxn0brp/db-core": "^0.3.0",
21
+ "@wxn0brp/db-storage-dir": "^0.1.4",
22
+ "tsc-alias": "*",
23
+ "typescript": "*"
24
+ },
25
+ "peerDependencies": {
26
+ "@wxn0brp/db-core": ">=0.3.0",
27
+ "@wxn0brp/db-storage-dir": ">=0.1.4"
28
+ },
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "exports": {
33
+ ".": {
34
+ "types": "./dist/index.d.ts",
35
+ "import": "./dist/index.js",
36
+ "default": "./dist/index.js"
37
+ },
38
+ "./*": {
39
+ "types": "./dist/*.d.ts",
40
+ "import": "./dist/*.js",
41
+ "default": "./dist/*.js"
42
+ }
43
+ }
44
+ }