@orpc/experimental-lock 2.0.0-beta.49 → 2.0.0-beta.51
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/README.md +10 -8
- package/dist/adapters/base-redis.d.ts +45 -45
- package/dist/adapters/base-redis.js +45 -49
- package/dist/adapters/memory.d.ts +25 -25
- package/dist/adapters/memory.js +73 -74
- package/dist/adapters/redis.d.ts +15 -16
- package/dist/adapters/redis.js +49 -41
- package/dist/adapters/upstash.d.ts +14 -15
- package/dist/adapters/upstash.js +38 -31
- package/dist/error.d.ts +6 -6
- package/dist/error.js +17 -15
- package/dist/index.d.ts +4 -3
- package/dist/index.js +2 -3
- package/dist/middleware.d.ts +49 -61
- package/dist/middleware.js +48 -49
- package/dist/types.d.ts +31 -31
- package/package.json +3 -3
- package/dist/types.js +0 -1
package/README.md
CHANGED
|
@@ -78,13 +78,14 @@ Read the full documentation at [orpc.dev](https://orpc.dev), starting with the [
|
|
|
78
78
|
|
|
79
79
|
**Built-in features**
|
|
80
80
|
|
|
81
|
-
| Package
|
|
82
|
-
|
|
|
83
|
-
| [@orpc/publisher](https://www.npmjs.com/package/@orpc/publisher)
|
|
84
|
-
| [@orpc/ratelimit](https://www.npmjs.com/package/@orpc/ratelimit)
|
|
85
|
-
| [@orpc/experimental-lock](https://www.npmjs.com/package/@orpc/experimental-lock)
|
|
86
|
-
| [@orpc/
|
|
87
|
-
| [@orpc/
|
|
81
|
+
| Package | Purpose | Docs |
|
|
82
|
+
| ---------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
|
|
83
|
+
| [@orpc/publisher](https://www.npmjs.com/package/@orpc/publisher) | Pub/Sub with memory, Redis, and Upstash adapters. | [Publisher](https://orpc.dev/docs/helpers/publisher) |
|
|
84
|
+
| [@orpc/ratelimit](https://www.npmjs.com/package/@orpc/ratelimit) | Rate limiting with memory, Redis, and Upstash adapters. | [Rate Limit](https://orpc.dev/docs/helpers/ratelimit) |
|
|
85
|
+
| [@orpc/experimental-lock](https://www.npmjs.com/package/@orpc/experimental-lock) | Locking with memory, Redis, and Upstash adapters. | [Lock](https://orpc.dev/docs/helpers/lock) |
|
|
86
|
+
| [@orpc/experimental-openapi-inference](https://www.npmjs.com/package/@orpc/experimental-openapi-inference) | Generate OpenAPI specs from TypeScript types, no output schemas required. | [Type Inference](https://orpc.dev/docs/openapi/specification#type-inference) |
|
|
87
|
+
| [@orpc/hibernation](https://www.npmjs.com/package/@orpc/hibernation) | Leverage Hibernation APIs like [Cloudflare's Hibernation WebSocket](https://developers.cloudflare.com/durable-objects/best-practices/websockets/#durable-objects-hibernation-websocket-api). | [Hibernation](https://orpc.dev/docs/integrations/hibernation) |
|
|
88
|
+
| [@orpc/json-schema](https://www.npmjs.com/package/@orpc/json-schema) | Smart coercion for OpenAPI requests. | [Smart Coercion](https://orpc.dev/docs/plugins/smart-coercion) |
|
|
88
89
|
|
|
89
90
|
**Framework & ecosystem integrations**
|
|
90
91
|
|
|
@@ -192,9 +193,10 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
192
193
|
<td align="center"><a href="https://kellychan.im/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="139" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
|
|
193
194
|
<td align="center"><a href="https://guyariely.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Guy Ariely"><img src="https://avatars.githubusercontent.com/u/42813496?u=edb6b7f563bf28e160a290832e7da57c0506f8ca&v=4" width="139" alt="Guy Ariely"/><br />Guy Ariely</a></td>
|
|
194
195
|
<td align="center"><a href="https://paulsenon.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="PaulSenon"><img src="https://avatars.githubusercontent.com/u/19531087?u=6385741eb91d200b8c513ec6045482e301767ca6&v=4" width="139" alt="PaulSenon"/><br />PaulSenon</a></td>
|
|
195
|
-
<td align="center"><a href="https://
|
|
196
|
+
<td align="center"><a href="https://usagerail.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="UsageRail"><img src="https://avatars.githubusercontent.com/u/347653714?v=4" width="139" alt="UsageRail"/><br />UsageRail</a></td>
|
|
196
197
|
</tr>
|
|
197
198
|
<tr>
|
|
199
|
+
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
198
200
|
<td align="center"><a href="https://opensource.gubanov.eu/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=c5f2daf7ebece498e85c83367bb37b4e10e2649d&v=4" width="139" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
|
|
199
201
|
</tr>
|
|
200
202
|
</table>
|
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { LockCallbackOptions, LockOptions, Locker } from "../types.js";
|
|
2
|
+
import { Promisable } from "@orpc/shared";
|
|
3
3
|
/**
|
|
4
4
|
* Options shared by every Redis-backed locker adapter.
|
|
5
5
|
*
|
|
6
6
|
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
7
7
|
*/
|
|
8
8
|
export interface BaseRedisLockerOptions {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The prefix to use for Redis keys.
|
|
11
|
+
*
|
|
12
|
+
* @default ''
|
|
13
|
+
*/
|
|
14
|
+
prefix?: string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* How long a lock is held before it expires automatically, in milliseconds.
|
|
17
|
+
* Guards against holders that never release the lock, such as a crashed process.
|
|
18
|
+
* Can be overridden per call.
|
|
19
|
+
*/
|
|
20
|
+
ttl: number;
|
|
21
|
+
/**
|
|
22
|
+
* How long to wait for a lock to become available, in milliseconds.
|
|
23
|
+
* Can be overridden per call.
|
|
24
|
+
*
|
|
25
|
+
* @default 10000
|
|
26
|
+
*/
|
|
27
|
+
timeout?: number | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* How long to wait between acquisition attempts while the lock
|
|
30
|
+
* is held by someone else, in milliseconds.
|
|
31
|
+
*
|
|
32
|
+
* @default 100
|
|
33
|
+
*/
|
|
34
|
+
retryInterval?: number | undefined;
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
37
|
* Base class for Redis-backed locker adapters. It owns the key naming, the lock
|
|
@@ -43,20 +43,20 @@ export interface BaseRedisLockerOptions {
|
|
|
43
43
|
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
44
44
|
*/
|
|
45
45
|
export declare abstract class BaseRedisLocker implements Locker {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
46
|
+
protected readonly keyPrefix: string;
|
|
47
|
+
protected readonly ttl: number;
|
|
48
|
+
protected readonly timeout: number;
|
|
49
|
+
protected readonly retryInterval: number;
|
|
50
|
+
constructor(options: BaseRedisLockerOptions);
|
|
51
|
+
/**
|
|
52
|
+
* Sets `key` to `token` only when it does not exist yet, with an expiry
|
|
53
|
+
* of `ttl` milliseconds (`SET key token NX PX ttl`), and resolves with
|
|
54
|
+
* whether the key was set.
|
|
55
|
+
*/
|
|
56
|
+
protected abstract acquire(key: string, token: string, ttl: number): Promise<boolean>;
|
|
57
|
+
/**
|
|
58
|
+
* Runs a Lua script (`EVAL script numkeys key [key ...] arg [arg ...]`).
|
|
59
|
+
*/
|
|
60
|
+
protected abstract evalScript(script: string, keys: string[], args: string[]): Promise<unknown>;
|
|
61
|
+
lock<T>(key: string, fn: (options: LockCallbackOptions) => Promisable<T>, options?: LockOptions): Promise<T>;
|
|
62
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { LockTimeoutError } from "../error.js";
|
|
2
|
+
import { sleep, throwIfAborted } from "@orpc/shared";
|
|
3
|
+
// src/adapters/base-redis.ts
|
|
3
4
|
const RELEASE_LOCK_SCRIPT = `
|
|
4
5
|
if redis.call('GET', KEYS[1]) == ARGV[1] then
|
|
5
6
|
return redis.call('DEL', KEYS[1])
|
|
@@ -8,50 +9,45 @@ end
|
|
|
8
9
|
return 0
|
|
9
10
|
`;
|
|
10
11
|
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
// the lock still expires through its TTL
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
12
|
+
* Base class for Redis-backed locker adapters. It owns the key naming, the lock
|
|
13
|
+
* token, the acquisition loop, and the atomic Lua release, so every adapter built
|
|
14
|
+
* on it can share locks with the others regardless of the Redis client in use.
|
|
15
|
+
*
|
|
16
|
+
* Extend it and implement the abstract methods to support another Redis client.
|
|
17
|
+
*
|
|
18
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
19
|
+
*/
|
|
20
|
+
var BaseRedisLocker = class {
|
|
21
|
+
keyPrefix;
|
|
22
|
+
ttl;
|
|
23
|
+
timeout;
|
|
24
|
+
retryInterval;
|
|
25
|
+
constructor(options) {
|
|
26
|
+
this.keyPrefix = options.prefix ?? "";
|
|
27
|
+
this.ttl = options.ttl;
|
|
28
|
+
this.timeout = options.timeout ?? 1e4;
|
|
29
|
+
this.retryInterval = options.retryInterval ?? 100;
|
|
30
|
+
}
|
|
31
|
+
async lock(key, fn, options = {}) {
|
|
32
|
+
const prefixedKey = `${this.keyPrefix}${key}`;
|
|
33
|
+
const ttl = options.ttl ?? this.ttl;
|
|
34
|
+
const deadline = Date.now() + (options.timeout ?? this.timeout);
|
|
35
|
+
const token = crypto.randomUUID();
|
|
36
|
+
let waited = false;
|
|
37
|
+
throwIfAborted(options.signal);
|
|
38
|
+
while (!await this.acquire(prefixedKey, token, ttl)) {
|
|
39
|
+
const remaining = deadline - Date.now();
|
|
40
|
+
if (remaining <= 0) throw new LockTimeoutError(key);
|
|
41
|
+
waited = true;
|
|
42
|
+
await sleep(Math.min(this.retryInterval, remaining), { signal: options.signal });
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
return await fn({ waited });
|
|
46
|
+
} finally {
|
|
47
|
+
try {
|
|
48
|
+
await this.evalScript(RELEASE_LOCK_SCRIPT, [prefixedKey], [token]);
|
|
49
|
+
} catch {}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
export { BaseRedisLocker };
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { LockCallbackOptions, LockOptions, Locker } from "../types.js";
|
|
2
|
+
import { Promisable } from "@orpc/shared";
|
|
3
3
|
export interface MemoryLockerOptions {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
4
|
+
/**
|
|
5
|
+
* How long a lock is held before it expires automatically, in milliseconds.
|
|
6
|
+
* Guards against holders that never release the lock.
|
|
7
|
+
* Can be overridden per call.
|
|
8
|
+
*
|
|
9
|
+
* @default undefined (held until released)
|
|
10
|
+
*/
|
|
11
|
+
ttl?: number | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* How long to wait for a lock to become available, in milliseconds.
|
|
14
|
+
* Can be overridden per call.
|
|
15
|
+
*
|
|
16
|
+
* @default 10000
|
|
17
|
+
*/
|
|
18
|
+
timeout?: number | undefined;
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
21
|
* Locker adapter backed by in-memory storage, so locks are only shared within
|
|
@@ -24,11 +24,11 @@ export interface MemoryLockerOptions {
|
|
|
24
24
|
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
25
25
|
*/
|
|
26
26
|
export declare class MemoryLocker implements Locker {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
27
|
+
private readonly ttl;
|
|
28
|
+
private readonly timeout;
|
|
29
|
+
private readonly entries;
|
|
30
|
+
constructor(options?: MemoryLockerOptions);
|
|
31
|
+
lock<T>(key: string, fn: (options: LockCallbackOptions) => Promisable<T>, options?: LockOptions): Promise<T>;
|
|
32
|
+
private wait;
|
|
33
|
+
private release;
|
|
34
|
+
}
|
package/dist/adapters/memory.js
CHANGED
|
@@ -1,75 +1,74 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { LockTimeoutError } from "../error.js";
|
|
2
|
+
import { promiseWithResolvers, throwIfAborted } from "@orpc/shared";
|
|
3
|
+
// src/adapters/memory.ts
|
|
3
4
|
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
}
|
|
5
|
+
* Locker adapter backed by in-memory storage, so locks are only shared within
|
|
6
|
+
* the current process. Waiters acquire the lock in order, without polling.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
9
|
+
*/
|
|
10
|
+
var MemoryLocker = class {
|
|
11
|
+
ttl;
|
|
12
|
+
timeout;
|
|
13
|
+
entries = /* @__PURE__ */ new Map();
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
this.ttl = options.ttl;
|
|
16
|
+
this.timeout = options.timeout ?? 1e4;
|
|
17
|
+
}
|
|
18
|
+
async lock(key, fn, options = {}) {
|
|
19
|
+
const ttl = options.ttl ?? this.ttl;
|
|
20
|
+
const timeout = options.timeout ?? this.timeout;
|
|
21
|
+
const token = {};
|
|
22
|
+
throwIfAborted(options.signal);
|
|
23
|
+
let entry = this.entries.get(key);
|
|
24
|
+
const waited = entry !== void 0;
|
|
25
|
+
if (entry) await this.wait(key, entry, token, timeout, options.signal);
|
|
26
|
+
else {
|
|
27
|
+
entry = {
|
|
28
|
+
holder: token,
|
|
29
|
+
waiters: /* @__PURE__ */ new Set()
|
|
30
|
+
};
|
|
31
|
+
this.entries.set(key, entry);
|
|
32
|
+
}
|
|
33
|
+
if (ttl !== void 0) entry.expiry = setTimeout(() => this.release(key, token), ttl);
|
|
34
|
+
try {
|
|
35
|
+
return await fn({ waited });
|
|
36
|
+
} finally {
|
|
37
|
+
this.release(key, token);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
wait(key, entry, token, timeout, signal) {
|
|
41
|
+
if (timeout <= 0) return Promise.reject(new LockTimeoutError(key));
|
|
42
|
+
const { promise, resolve, reject } = promiseWithResolvers();
|
|
43
|
+
const waiter = {
|
|
44
|
+
token,
|
|
45
|
+
resolve
|
|
46
|
+
};
|
|
47
|
+
entry.waiters.add(waiter);
|
|
48
|
+
const fail = (reason) => {
|
|
49
|
+
entry.waiters.delete(waiter);
|
|
50
|
+
reject(reason);
|
|
51
|
+
};
|
|
52
|
+
const timer = setTimeout(() => fail(new LockTimeoutError(key)), timeout);
|
|
53
|
+
const abortListener = () => fail(signal?.reason);
|
|
54
|
+
signal?.addEventListener("abort", abortListener, { once: true });
|
|
55
|
+
return promise.finally(() => {
|
|
56
|
+
clearTimeout(timer);
|
|
57
|
+
signal?.removeEventListener("abort", abortListener);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
release(key, token) {
|
|
61
|
+
const entry = this.entries.get(key);
|
|
62
|
+
if (entry?.holder !== token) return;
|
|
63
|
+
clearTimeout(entry.expiry);
|
|
64
|
+
const [next] = entry.waiters;
|
|
65
|
+
if (!next) {
|
|
66
|
+
this.entries.delete(key);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
entry.waiters.delete(next);
|
|
70
|
+
entry.holder = next.token;
|
|
71
|
+
next.resolve();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
export { MemoryLocker };
|
package/dist/adapters/redis.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { BaseRedisLocker } from './base-redis.ts';
|
|
1
|
+
import { BaseRedisLocker, BaseRedisLockerOptions } from "./base-redis.js";
|
|
2
|
+
import { RedisClientType } from "redis";
|
|
4
3
|
/**
|
|
5
4
|
* Locker adapter for Redis. Acquires locks with `SET NX PX` and releases them
|
|
6
5
|
* atomically with a Lua script, so every instance using the same server shares
|
|
@@ -9,16 +8,16 @@ import { BaseRedisLocker } from './base-redis.ts';
|
|
|
9
8
|
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
10
9
|
*/
|
|
11
10
|
export declare class RedisLocker extends BaseRedisLocker {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
11
|
+
private readonly redis;
|
|
12
|
+
private readonly options;
|
|
13
|
+
constructor(redis: RedisClientType<any, any, any, any, any>, options: BaseRedisLockerOptions);
|
|
14
|
+
/**
|
|
15
|
+
* Creates a locker with `prefix` appended to this locker's prefix, sharing its Redis client.
|
|
16
|
+
*
|
|
17
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
18
|
+
*/
|
|
19
|
+
prefix(prefix: string): RedisLocker;
|
|
20
|
+
protected acquire(key: string, token: string, ttl: number): Promise<boolean>;
|
|
21
|
+
protected evalScript(script: string, keys: string[], args: string[]): Promise<unknown>;
|
|
22
|
+
private connectIfNeeded;
|
|
23
|
+
}
|
package/dist/adapters/redis.js
CHANGED
|
@@ -1,42 +1,50 @@
|
|
|
1
|
-
import { BaseRedisLocker } from
|
|
1
|
+
import { BaseRedisLocker } from "./base-redis.js";
|
|
2
|
+
// src/adapters/redis.ts
|
|
2
3
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
4
|
+
* Locker adapter for Redis. Acquires locks with `SET NX PX` and releases them
|
|
5
|
+
* atomically with a Lua script, so every instance using the same server shares
|
|
6
|
+
* the same locks.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
9
|
+
*/
|
|
10
|
+
var RedisLocker = class RedisLocker extends BaseRedisLocker {
|
|
11
|
+
redis;
|
|
12
|
+
options;
|
|
13
|
+
constructor(redis, options) {
|
|
14
|
+
super(options);
|
|
15
|
+
this.redis = redis;
|
|
16
|
+
this.options = options;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates a locker with `prefix` appended to this locker's prefix, sharing its Redis client.
|
|
20
|
+
*
|
|
21
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
22
|
+
*/
|
|
23
|
+
prefix(prefix) {
|
|
24
|
+
return new RedisLocker(this.redis, {
|
|
25
|
+
...this.options,
|
|
26
|
+
prefix: `${this.keyPrefix}${prefix}`
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
async acquire(key, token, ttl) {
|
|
30
|
+
await this.connectIfNeeded();
|
|
31
|
+
return await this.redis.set(key, token, {
|
|
32
|
+
condition: "NX",
|
|
33
|
+
expiration: {
|
|
34
|
+
type: "PX",
|
|
35
|
+
value: ttl
|
|
36
|
+
}
|
|
37
|
+
}) === "OK";
|
|
38
|
+
}
|
|
39
|
+
async evalScript(script, keys, args) {
|
|
40
|
+
await this.connectIfNeeded();
|
|
41
|
+
return await this.redis.eval(script, {
|
|
42
|
+
keys,
|
|
43
|
+
arguments: args
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async connectIfNeeded() {
|
|
47
|
+
if (!this.redis.isOpen) await this.redis.connect();
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
export { RedisLocker };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { BaseRedisLocker } from './base-redis.ts';
|
|
1
|
+
import { BaseRedisLocker, BaseRedisLockerOptions } from "./base-redis.js";
|
|
2
|
+
import { Redis } from "@upstash/redis";
|
|
4
3
|
/**
|
|
5
4
|
* Locker adapter for Upstash Redis. Acquires locks with `SET NX PX` and releases
|
|
6
5
|
* them atomically with a Lua script, so it shares locks with `RedisLocker`.
|
|
@@ -9,15 +8,15 @@ import { BaseRedisLocker } from './base-redis.ts';
|
|
|
9
8
|
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
10
9
|
*/
|
|
11
10
|
export declare class UpstashLocker extends BaseRedisLocker {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
11
|
+
private readonly redis;
|
|
12
|
+
private readonly options;
|
|
13
|
+
constructor(redis: Redis, options: BaseRedisLockerOptions);
|
|
14
|
+
/**
|
|
15
|
+
* Creates a locker with `prefix` appended to this locker's prefix, sharing its Redis client.
|
|
16
|
+
*
|
|
17
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
18
|
+
*/
|
|
19
|
+
prefix(prefix: string): UpstashLocker;
|
|
20
|
+
protected acquire(key: string, token: string, ttl: number): Promise<boolean>;
|
|
21
|
+
protected evalScript(script: string, keys: string[], args: string[]): Promise<unknown>;
|
|
22
|
+
}
|
package/dist/adapters/upstash.js
CHANGED
|
@@ -1,32 +1,39 @@
|
|
|
1
|
-
import { BaseRedisLocker } from
|
|
1
|
+
import { BaseRedisLocker } from "./base-redis.js";
|
|
2
|
+
// src/adapters/upstash.ts
|
|
2
3
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
4
|
+
* Locker adapter for Upstash Redis. Acquires locks with `SET NX PX` and releases
|
|
5
|
+
* them atomically with a Lua script, so it shares locks with `RedisLocker`.
|
|
6
|
+
* A good fit for serverless and edge runtimes.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
9
|
+
*/
|
|
10
|
+
var UpstashLocker = class UpstashLocker extends BaseRedisLocker {
|
|
11
|
+
redis;
|
|
12
|
+
options;
|
|
13
|
+
constructor(redis, options) {
|
|
14
|
+
super(options);
|
|
15
|
+
this.redis = redis;
|
|
16
|
+
this.options = options;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates a locker with `prefix` appended to this locker's prefix, sharing its Redis client.
|
|
20
|
+
*
|
|
21
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
22
|
+
*/
|
|
23
|
+
prefix(prefix) {
|
|
24
|
+
return new UpstashLocker(this.redis, {
|
|
25
|
+
...this.options,
|
|
26
|
+
prefix: `${this.keyPrefix}${prefix}`
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
async acquire(key, token, ttl) {
|
|
30
|
+
return await this.redis.set(key, token, {
|
|
31
|
+
nx: true,
|
|
32
|
+
px: ttl
|
|
33
|
+
}) === "OK";
|
|
34
|
+
}
|
|
35
|
+
async evalScript(script, keys, args) {
|
|
36
|
+
return await this.redis.eval(script, keys, args);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
export { UpstashLocker };
|
package/dist/error.d.ts
CHANGED
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
* @see {@link https://orpc.dev/docs/helpers/lock#options | Lock Helpers - Options}
|
|
5
5
|
*/
|
|
6
6
|
export declare class LockTimeoutError extends Error {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
7
|
+
/**
|
|
8
|
+
* The key of the lock that could not be acquired.
|
|
9
|
+
*/
|
|
10
|
+
readonly key: string;
|
|
11
|
+
constructor(key: string);
|
|
12
|
+
}
|
package/dist/error.js
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
+
// src/error.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
3
|
+
* Thrown when a lock cannot be acquired before the timeout elapses.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#options | Lock Helpers - Options}
|
|
6
|
+
*/
|
|
7
|
+
var LockTimeoutError = class extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* The key of the lock that could not be acquired.
|
|
10
|
+
*/
|
|
11
|
+
key;
|
|
12
|
+
constructor(key) {
|
|
13
|
+
super(`Timed out waiting for the lock of key "${key}"`);
|
|
14
|
+
this.name = "LockTimeoutError";
|
|
15
|
+
this.key = key;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
export { LockTimeoutError };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { LockTimeoutError } from "./error.js";
|
|
2
|
+
import { LOCK_MIDDLEWARE_CONTEXT_SYMBOL, LockMiddlewareContext, LockMiddlewareOptions, LockMiddlewareOutContext, lock } from "./middleware.js";
|
|
3
|
+
import { LockCallbackOptions, LockOptions, Locker } from "./types.js";
|
|
4
|
+
export { LOCK_MIDDLEWARE_CONTEXT_SYMBOL, LockCallbackOptions, LockMiddlewareContext, LockMiddlewareOptions, LockMiddlewareOutContext, LockOptions, LockTimeoutError, Locker, lock };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export * from './types.js';
|
|
1
|
+
export { LockTimeoutError } from "./error.js";
|
|
2
|
+
export { LOCK_MIDDLEWARE_CONTEXT_SYMBOL, lock } from "./middleware.js";
|
package/dist/middleware.d.ts
CHANGED
|
@@ -1,69 +1,57 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { Locker } from "./types.js";
|
|
2
|
+
import { Context, Middleware, MiddlewareOptions } from "@orpc/server";
|
|
3
|
+
import { Promisable, Value } from "@orpc/shared";
|
|
4
4
|
export declare const LOCK_MIDDLEWARE_CONTEXT_SYMBOL: unique symbol;
|
|
5
5
|
export interface LockMiddlewareContext {
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The locks held in this request, mainly for deduplication purposes
|
|
9
|
-
*/
|
|
10
|
-
held: {
|
|
11
|
-
locker: Locker;
|
|
12
|
-
key: string;
|
|
13
|
-
waited: boolean;
|
|
14
|
-
}[];
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
export interface LockMiddlewareOutContext {
|
|
6
|
+
[LOCK_MIDDLEWARE_CONTEXT_SYMBOL]?: {
|
|
18
7
|
/**
|
|
19
|
-
*
|
|
20
|
-
* `false` means the lock was acquired immediately.
|
|
8
|
+
* The locks held in this request, mainly for deduplication purposes
|
|
21
9
|
*/
|
|
22
|
-
|
|
10
|
+
held: {
|
|
11
|
+
locker: Locker;
|
|
12
|
+
key: string;
|
|
13
|
+
waited: boolean;
|
|
14
|
+
}[];
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export interface LockMiddlewareOutContext {
|
|
18
|
+
/**
|
|
19
|
+
* Whether the lock was acquired only after waiting for another holder to release it.
|
|
20
|
+
* `false` means the lock was acquired immediately.
|
|
21
|
+
*/
|
|
22
|
+
["lock/waited"]: boolean;
|
|
23
23
|
}
|
|
24
24
|
export interface LockMiddlewareOptions<TInContext extends Context, TInput> {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
input: TInput
|
|
56
|
-
]> | undefined;
|
|
57
|
-
/**
|
|
58
|
-
* If your lock middleware is used multiple times
|
|
59
|
-
* or you invoke a procedure inside another procedure (shared the same context) that also has
|
|
60
|
-
* lock middleware **with the same locker and key**, this option
|
|
61
|
-
* will ensure that the lock is only acquired once per request,
|
|
62
|
-
* instead of waiting for itself until the timeout elapses.
|
|
63
|
-
*
|
|
64
|
-
* @default true
|
|
65
|
-
*/
|
|
66
|
-
dedupe?: boolean | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* The locker to acquire the lock from
|
|
27
|
+
*/
|
|
28
|
+
locker: Value<Promisable<Locker>, [options: MiddlewareOptions<TInContext, unknown, {}>, input: TInput]>;
|
|
29
|
+
/**
|
|
30
|
+
* The key to identify the work that must not run concurrently
|
|
31
|
+
*/
|
|
32
|
+
key: Value<Promisable<string>, [options: MiddlewareOptions<TInContext, unknown, {}>, input: TInput]>;
|
|
33
|
+
/**
|
|
34
|
+
* How long the lock is held before it expires automatically, in milliseconds.
|
|
35
|
+
*
|
|
36
|
+
* @default the adapter default
|
|
37
|
+
*/
|
|
38
|
+
ttl?: Value<Promisable<number | undefined>, [options: MiddlewareOptions<TInContext, unknown, {}>, input: TInput]> | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* How long to wait for the lock to become available, in milliseconds.
|
|
41
|
+
*
|
|
42
|
+
* @default the adapter default
|
|
43
|
+
*/
|
|
44
|
+
timeout?: Value<Promisable<number | undefined>, [options: MiddlewareOptions<TInContext, unknown, {}>, input: TInput]> | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* If your lock middleware is used multiple times
|
|
47
|
+
* or you invoke a procedure inside another procedure (shared the same context) that also has
|
|
48
|
+
* lock middleware **with the same locker and key**, this option
|
|
49
|
+
* will ensure that the lock is only acquired once per request,
|
|
50
|
+
* instead of waiting for itself until the timeout elapses.
|
|
51
|
+
*
|
|
52
|
+
* @default true
|
|
53
|
+
*/
|
|
54
|
+
dedupe?: boolean | undefined;
|
|
67
55
|
}
|
|
68
56
|
/**
|
|
69
57
|
* Creates a middleware that runs oRPC procedures under a lock, so that calls
|
|
@@ -72,4 +60,4 @@ export interface LockMiddlewareOptions<TInContext extends Context, TInput> {
|
|
|
72
60
|
*
|
|
73
61
|
* @see {@link https://orpc.dev/docs/helpers/lock#lock-middleware | Lock Helpers - Lock Middleware}
|
|
74
62
|
*/
|
|
75
|
-
export declare function lock<TInContext extends Context, TInput>({ dedupe, ...options }: LockMiddlewareOptions<TInContext, TInput>): Middleware<TInContext, LockMiddlewareOutContext, TInput, any, object>;
|
|
63
|
+
export declare function lock<TInContext extends Context, TInput>({ dedupe, ...options }: LockMiddlewareOptions<TInContext, TInput>): Middleware<TInContext, LockMiddlewareOutContext, TInput, any, object>;
|
package/dist/middleware.js
CHANGED
|
@@ -1,51 +1,50 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { LockTimeoutError } from "./error.js";
|
|
2
|
+
import { ORPCError } from "@orpc/server";
|
|
3
|
+
import { toArray, value } from "@orpc/shared";
|
|
4
|
+
// src/middleware.ts
|
|
5
|
+
const LOCK_MIDDLEWARE_CONTEXT_SYMBOL = /* @__PURE__ */ Symbol.for("ORPC_LOCK_MIDDLEWARE_CONTEXT");
|
|
5
6
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
throw error;
|
|
49
|
-
}
|
|
50
|
-
};
|
|
7
|
+
* Creates a middleware that runs oRPC procedures under a lock, so that calls
|
|
8
|
+
* sharing the same key never run concurrently. Rejects with a `CONFLICT` error
|
|
9
|
+
* when the lock cannot be acquired before the timeout elapses.
|
|
10
|
+
*
|
|
11
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#lock-middleware | Lock Helpers - Lock Middleware}
|
|
12
|
+
*/
|
|
13
|
+
function lock({ dedupe = true, ...options }) {
|
|
14
|
+
return async function lock(middlewareOptions, input) {
|
|
15
|
+
const [locker, key, ttl, timeout] = await Promise.all([
|
|
16
|
+
value(options.locker, middlewareOptions, input),
|
|
17
|
+
value(options.key, middlewareOptions, input),
|
|
18
|
+
value(options.ttl, middlewareOptions, input),
|
|
19
|
+
value(options.timeout, middlewareOptions, input)
|
|
20
|
+
]);
|
|
21
|
+
const middlewareContext = middlewareOptions.context[LOCK_MIDDLEWARE_CONTEXT_SYMBOL];
|
|
22
|
+
const held = middlewareContext?.held.find((l) => l.key === key && l.locker === locker);
|
|
23
|
+
if (dedupe && held) return middlewareOptions.next({ context: { "lock/waited": held.waited } });
|
|
24
|
+
let acquired = false;
|
|
25
|
+
try {
|
|
26
|
+
return await locker.lock(key, ({ waited }) => {
|
|
27
|
+
acquired = true;
|
|
28
|
+
return middlewareOptions.next({ context: {
|
|
29
|
+
"lock/waited": waited,
|
|
30
|
+
[LOCK_MIDDLEWARE_CONTEXT_SYMBOL]: {
|
|
31
|
+
...middlewareContext,
|
|
32
|
+
held: [...toArray(middlewareContext?.held), {
|
|
33
|
+
locker,
|
|
34
|
+
key,
|
|
35
|
+
waited
|
|
36
|
+
}]
|
|
37
|
+
}
|
|
38
|
+
} });
|
|
39
|
+
}, {
|
|
40
|
+
ttl,
|
|
41
|
+
timeout,
|
|
42
|
+
signal: middlewareOptions.signal
|
|
43
|
+
});
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (!acquired && error instanceof LockTimeoutError) throw new ORPCError("CONFLICT", { cause: error });
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
51
49
|
}
|
|
50
|
+
export { LOCK_MIDDLEWARE_CONTEXT_SYMBOL, lock };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Promisable } from "@orpc/shared";
|
|
2
2
|
export interface LockOptions {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
3
|
+
/**
|
|
4
|
+
* How long the lock is held before it expires automatically, in milliseconds.
|
|
5
|
+
* Guards against holders that never release the lock, such as a crashed process.
|
|
6
|
+
*
|
|
7
|
+
* @default the adapter default
|
|
8
|
+
*/
|
|
9
|
+
ttl?: number | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* How long to wait for the lock to become available, in milliseconds.
|
|
12
|
+
*
|
|
13
|
+
* @default the adapter default
|
|
14
|
+
*/
|
|
15
|
+
timeout?: number | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Aborts waiting for the lock. Has no effect once the lock is acquired.
|
|
18
|
+
*/
|
|
19
|
+
signal?: AbortSignal | undefined;
|
|
20
20
|
}
|
|
21
21
|
export interface LockCallbackOptions {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Whether the lock was acquired only after waiting for another holder to release it.
|
|
24
|
+
* `false` means the lock was acquired immediately.
|
|
25
|
+
*/
|
|
26
|
+
waited: boolean;
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
29
|
* Standard interface for running work under a lock, so that work sharing
|
|
@@ -33,11 +33,11 @@ export interface LockCallbackOptions {
|
|
|
33
33
|
* @see {@link https://orpc.dev/docs/helpers/lock | Lock Helpers}
|
|
34
34
|
*/
|
|
35
35
|
export interface Locker {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
36
|
+
/**
|
|
37
|
+
* Runs `fn` while holding the lock for `key`, and releases the lock afterwards,
|
|
38
|
+
* even when `fn` throws.
|
|
39
|
+
*
|
|
40
|
+
* @throws {LockTimeoutError} when the lock cannot be acquired before the timeout elapses
|
|
41
|
+
*/
|
|
42
|
+
lock<T>(key: string, fn: (options: LockCallbackOptions) => Promisable<T>, options?: LockOptions): Promise<T>;
|
|
43
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/experimental-lock",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.51",
|
|
4
4
|
"description": "Locking for oRPC procedures, with memory, Redis, and Upstash adapters",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
@@ -58,8 +58,8 @@
|
|
|
58
58
|
}
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@orpc/server": "2.0.0-beta.
|
|
62
|
-
"@orpc/shared": "2.0.0-beta.
|
|
61
|
+
"@orpc/server": "2.0.0-beta.51",
|
|
62
|
+
"@orpc/shared": "2.0.0-beta.51"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
65
|
"@upstash/redis": "^1.41.0",
|
package/dist/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|