@orpc/experimental-lock 2.0.0-beta.47 → 2.0.0-beta.48
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 +2 -3
- package/dist/adapters/base-redis.d.mts +58 -62
- package/dist/adapters/base-redis.mjs +35 -37
- package/dist/adapters/memory.d.mts +31 -35
- package/dist/adapters/memory.mjs +72 -68
- package/dist/adapters/redis.d.mts +22 -26
- package/dist/adapters/redis.mjs +39 -36
- package/dist/adapters/upstash.d.mts +21 -25
- package/dist/adapters/upstash.mjs +26 -26
- package/dist/error.d.mts +12 -0
- package/dist/error.mjs +10 -0
- package/dist/index.d.mts +3 -81
- package/dist/index.mjs +3 -49
- package/dist/middleware.d.mts +69 -0
- package/dist/middleware.mjs +46 -0
- package/dist/types.d.mts +43 -0
- package/dist/types.mjs +1 -0
- package/package.json +5 -5
- package/dist/adapters/base-redis.d.ts +0 -66
- package/dist/adapters/memory.d.ts +0 -38
- package/dist/adapters/redis.d.ts +0 -28
- package/dist/adapters/upstash.d.ts +0 -27
- package/dist/index.d.ts +0 -81
- package/dist/shared/experimental-lock.CoYbYjCJ.mjs +0 -13
- package/dist/shared/experimental-lock.D9u4cJfq.d.mts +0 -46
- package/dist/shared/experimental-lock.D9u4cJfq.d.ts +0 -46
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { BaseRedisLocker } from
|
|
2
|
-
import '@orpc/shared';
|
|
3
|
-
import '../shared/experimental-lock.CoYbYjCJ.mjs';
|
|
1
|
+
import { BaseRedisLocker } from "./base-redis.mjs";
|
|
4
2
|
|
|
5
|
-
class UpstashLocker extends BaseRedisLocker {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
3
|
+
export class UpstashLocker extends BaseRedisLocker {
|
|
4
|
+
redis;
|
|
5
|
+
options;
|
|
6
|
+
constructor(redis, options) {
|
|
7
|
+
super(options);
|
|
8
|
+
this.redis = redis;
|
|
9
|
+
this.options = options;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
prefix(prefix) {
|
|
13
|
+
return new UpstashLocker(this.redis, {
|
|
14
|
+
...this.options,
|
|
15
|
+
prefix: `${this.keyPrefix}${prefix}`
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
async acquire(key, token, ttl) {
|
|
19
|
+
const result = await this.redis.set(key, token, {
|
|
20
|
+
nx: true,
|
|
21
|
+
px: ttl
|
|
22
|
+
});
|
|
23
|
+
return result === "OK";
|
|
24
|
+
}
|
|
25
|
+
async evalScript(script, keys, args) {
|
|
26
|
+
return await this.redis.eval(script, keys, args);
|
|
27
|
+
}
|
|
26
28
|
}
|
|
27
|
-
|
|
28
|
-
export { UpstashLocker };
|
package/dist/error.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when a lock cannot be acquired before the timeout elapses.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#options | Lock Helpers - Options}
|
|
5
|
+
*/
|
|
6
|
+
export declare class LockTimeoutError extends Error {
|
|
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.mjs
ADDED
package/dist/index.d.mts
CHANGED
|
@@ -1,81 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export { a as LockCallbackOptions, b as LockOptions } from './shared/experimental-lock.D9u4cJfq.mjs';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Thrown when a lock cannot be acquired before the timeout elapses.
|
|
8
|
-
*
|
|
9
|
-
* @see {@link https://orpc.dev/docs/helpers/lock#options | Lock Helpers - Options}
|
|
10
|
-
*/
|
|
11
|
-
declare class LockTimeoutError extends Error {
|
|
12
|
-
/**
|
|
13
|
-
* The key of the lock that could not be acquired.
|
|
14
|
-
*/
|
|
15
|
-
readonly key: string;
|
|
16
|
-
constructor(key: string);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
declare const LOCK_MIDDLEWARE_CONTEXT_SYMBOL: unique symbol;
|
|
20
|
-
interface LockMiddlewareContext {
|
|
21
|
-
[LOCK_MIDDLEWARE_CONTEXT_SYMBOL]?: {
|
|
22
|
-
/**
|
|
23
|
-
* The locks held in this request, mainly for deduplication purposes
|
|
24
|
-
*/
|
|
25
|
-
held: {
|
|
26
|
-
locker: Locker;
|
|
27
|
-
key: string;
|
|
28
|
-
waited: boolean;
|
|
29
|
-
}[];
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
interface LockMiddlewareOutContext {
|
|
33
|
-
/**
|
|
34
|
-
* Whether the lock was acquired only after waiting for another holder to release it.
|
|
35
|
-
* `false` means the lock was acquired immediately.
|
|
36
|
-
*/
|
|
37
|
-
['lock/waited']: boolean;
|
|
38
|
-
}
|
|
39
|
-
interface LockMiddlewareOptions<TInContext extends Context, TInput> {
|
|
40
|
-
/**
|
|
41
|
-
* The locker to acquire the lock from
|
|
42
|
-
*/
|
|
43
|
-
locker: Value<Promisable<Locker>, [options: MiddlewareOptions<TInContext, unknown, Record<never, never>>, input: TInput]>;
|
|
44
|
-
/**
|
|
45
|
-
* The key to identify the work that must not run concurrently
|
|
46
|
-
*/
|
|
47
|
-
key: Value<Promisable<string>, [options: MiddlewareOptions<TInContext, unknown, Record<never, never>>, input: TInput]>;
|
|
48
|
-
/**
|
|
49
|
-
* How long the lock is held before it expires automatically, in milliseconds.
|
|
50
|
-
*
|
|
51
|
-
* @default the adapter default
|
|
52
|
-
*/
|
|
53
|
-
ttl?: Value<Promisable<number | undefined>, [options: MiddlewareOptions<TInContext, unknown, Record<never, never>>, input: TInput]> | undefined;
|
|
54
|
-
/**
|
|
55
|
-
* How long to wait for the lock to become available, in milliseconds.
|
|
56
|
-
*
|
|
57
|
-
* @default the adapter default
|
|
58
|
-
*/
|
|
59
|
-
timeout?: Value<Promisable<number | undefined>, [options: MiddlewareOptions<TInContext, unknown, Record<never, never>>, input: TInput]> | undefined;
|
|
60
|
-
/**
|
|
61
|
-
* If your lock middleware is used multiple times
|
|
62
|
-
* or you invoke a procedure inside another procedure (shared the same context) that also has
|
|
63
|
-
* lock middleware **with the same locker and key**, this option
|
|
64
|
-
* will ensure that the lock is only acquired once per request,
|
|
65
|
-
* instead of waiting for itself until the timeout elapses.
|
|
66
|
-
*
|
|
67
|
-
* @default true
|
|
68
|
-
*/
|
|
69
|
-
dedupe?: boolean | undefined;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Creates a middleware that runs oRPC procedures under a lock, so that calls
|
|
73
|
-
* sharing the same key never run concurrently. Rejects with a `CONFLICT` error
|
|
74
|
-
* when the lock cannot be acquired before the timeout elapses.
|
|
75
|
-
*
|
|
76
|
-
* @see {@link https://orpc.dev/docs/helpers/lock#lock-middleware | Lock Helpers - Lock Middleware}
|
|
77
|
-
*/
|
|
78
|
-
declare function lock<TInContext extends Context, TInput>({ dedupe, ...options }: LockMiddlewareOptions<TInContext, TInput>): Middleware<TInContext, LockMiddlewareOutContext, TInput, any, object>;
|
|
79
|
-
|
|
80
|
-
export { LOCK_MIDDLEWARE_CONTEXT_SYMBOL, LockTimeoutError, Locker, lock };
|
|
81
|
-
export type { LockMiddlewareContext, LockMiddlewareOptions, LockMiddlewareOutContext };
|
|
1
|
+
export * from "./error.mjs";
|
|
2
|
+
export * from "./middleware.mjs";
|
|
3
|
+
export * from "./types.mjs";
|
package/dist/index.mjs
CHANGED
|
@@ -1,49 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const LOCK_MIDDLEWARE_CONTEXT_SYMBOL = Symbol.for("ORPC_LOCK_MIDDLEWARE_CONTEXT");
|
|
6
|
-
function lock({ dedupe = true, ...options }) {
|
|
7
|
-
return async function lock2(middlewareOptions, input) {
|
|
8
|
-
const [locker, key, ttl, timeout] = await Promise.all([
|
|
9
|
-
value(options.locker, middlewareOptions, input),
|
|
10
|
-
value(options.key, middlewareOptions, input),
|
|
11
|
-
value(options.ttl, middlewareOptions, input),
|
|
12
|
-
value(options.timeout, middlewareOptions, input)
|
|
13
|
-
]);
|
|
14
|
-
const middlewareContext = middlewareOptions.context[LOCK_MIDDLEWARE_CONTEXT_SYMBOL];
|
|
15
|
-
const held = middlewareContext?.held.find((l) => l.key === key && l.locker === locker);
|
|
16
|
-
if (dedupe && held) {
|
|
17
|
-
return middlewareOptions.next({
|
|
18
|
-
context: {
|
|
19
|
-
"lock/waited": held.waited
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
let acquired = false;
|
|
24
|
-
try {
|
|
25
|
-
return await locker.lock(key, ({ waited }) => {
|
|
26
|
-
acquired = true;
|
|
27
|
-
return middlewareOptions.next({
|
|
28
|
-
context: {
|
|
29
|
-
"lock/waited": waited,
|
|
30
|
-
[LOCK_MIDDLEWARE_CONTEXT_SYMBOL]: {
|
|
31
|
-
...middlewareContext,
|
|
32
|
-
held: [
|
|
33
|
-
...toArray(middlewareContext?.held),
|
|
34
|
-
{ locker, key, waited }
|
|
35
|
-
]
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
}, { ttl, timeout, signal: middlewareOptions.signal });
|
|
40
|
-
} catch (error) {
|
|
41
|
-
if (!acquired && error instanceof LockTimeoutError) {
|
|
42
|
-
throw new ORPCError("CONFLICT", { cause: error });
|
|
43
|
-
}
|
|
44
|
-
throw error;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export { LOCK_MIDDLEWARE_CONTEXT_SYMBOL, LockTimeoutError, lock };
|
|
1
|
+
export * from "./error.mjs";
|
|
2
|
+
export * from "./middleware.mjs";
|
|
3
|
+
export * from "./types.mjs";
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Context, Middleware, MiddlewareOptions } from "@orpc/server";
|
|
2
|
+
import type { Promisable, Value } from "@orpc/shared";
|
|
3
|
+
import type { Locker } from "./types.mjs";
|
|
4
|
+
export declare const LOCK_MIDDLEWARE_CONTEXT_SYMBOL: unique symbol;
|
|
5
|
+
export interface LockMiddlewareContext {
|
|
6
|
+
[LOCK_MIDDLEWARE_CONTEXT_SYMBOL]?: {
|
|
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 {
|
|
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
|
+
}
|
|
24
|
+
export interface LockMiddlewareOptions<
|
|
25
|
+
TInContext extends Context,
|
|
26
|
+
TInput
|
|
27
|
+
> {
|
|
28
|
+
/**
|
|
29
|
+
* The locker to acquire the lock from
|
|
30
|
+
*/
|
|
31
|
+
locker: Value<Promisable<Locker>, [options: MiddlewareOptions<TInContext, unknown, Record<never, never>>, input: TInput]>;
|
|
32
|
+
/**
|
|
33
|
+
* The key to identify the work that must not run concurrently
|
|
34
|
+
*/
|
|
35
|
+
key: Value<Promisable<string>, [options: MiddlewareOptions<TInContext, unknown, Record<never, never>>, input: TInput]>;
|
|
36
|
+
/**
|
|
37
|
+
* How long the lock is held before it expires automatically, in milliseconds.
|
|
38
|
+
*
|
|
39
|
+
* @default the adapter default
|
|
40
|
+
*/
|
|
41
|
+
ttl?: Value<Promisable<number | undefined>, [options: MiddlewareOptions<TInContext, unknown, Record<never, never>>, input: TInput]> | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* How long to wait for the lock to become available, in milliseconds.
|
|
44
|
+
*
|
|
45
|
+
* @default the adapter default
|
|
46
|
+
*/
|
|
47
|
+
timeout?: Value<Promisable<number | undefined>, [options: MiddlewareOptions<TInContext, unknown, Record<never, never>>, input: TInput]> | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* If your lock middleware is used multiple times
|
|
50
|
+
* or you invoke a procedure inside another procedure (shared the same context) that also has
|
|
51
|
+
* lock middleware **with the same locker and key**, this option
|
|
52
|
+
* will ensure that the lock is only acquired once per request,
|
|
53
|
+
* instead of waiting for itself until the timeout elapses.
|
|
54
|
+
*
|
|
55
|
+
* @default true
|
|
56
|
+
*/
|
|
57
|
+
dedupe?: boolean | undefined;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Creates a middleware that runs oRPC procedures under a lock, so that calls
|
|
61
|
+
* sharing the same key never run concurrently. Rejects with a `CONFLICT` error
|
|
62
|
+
* when the lock cannot be acquired before the timeout elapses.
|
|
63
|
+
*
|
|
64
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#lock-middleware | Lock Helpers - Lock Middleware}
|
|
65
|
+
*/
|
|
66
|
+
export declare function lock<
|
|
67
|
+
TInContext extends Context,
|
|
68
|
+
TInput
|
|
69
|
+
>({ dedupe, ...options }: LockMiddlewareOptions<TInContext, TInput>): Middleware<TInContext, LockMiddlewareOutContext, TInput, any, object>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ORPCError } from "@orpc/server";
|
|
2
|
+
import { toArray, value } from "@orpc/shared";
|
|
3
|
+
import { LockTimeoutError } from "./error.mjs";
|
|
4
|
+
export const LOCK_MIDDLEWARE_CONTEXT_SYMBOL = /* @__PURE__ */ Symbol.for("ORPC_LOCK_MIDDLEWARE_CONTEXT");
|
|
5
|
+
|
|
6
|
+
export function lock({ dedupe = true, ...options }) {
|
|
7
|
+
return async function lock(middlewareOptions, input) {
|
|
8
|
+
const [locker, key, ttl, timeout] = await Promise.all([
|
|
9
|
+
value(options.locker, middlewareOptions, input),
|
|
10
|
+
value(options.key, middlewareOptions, input),
|
|
11
|
+
value(options.ttl, middlewareOptions, input),
|
|
12
|
+
value(options.timeout, middlewareOptions, input)
|
|
13
|
+
]);
|
|
14
|
+
const middlewareContext = middlewareOptions.context[LOCK_MIDDLEWARE_CONTEXT_SYMBOL];
|
|
15
|
+
const held = middlewareContext?.held.find((l) => l.key === key && l.locker === locker);
|
|
16
|
+
if (dedupe && held) {
|
|
17
|
+
return middlewareOptions.next({ context: { "lock/waited": held.waited } });
|
|
18
|
+
}
|
|
19
|
+
let acquired = false;
|
|
20
|
+
try {
|
|
21
|
+
return await locker.lock(key, ({ waited }) => {
|
|
22
|
+
acquired = true;
|
|
23
|
+
return middlewareOptions.next({ context: {
|
|
24
|
+
"lock/waited": waited,
|
|
25
|
+
[LOCK_MIDDLEWARE_CONTEXT_SYMBOL]: {
|
|
26
|
+
...middlewareContext,
|
|
27
|
+
held: [...toArray(middlewareContext?.held), {
|
|
28
|
+
locker,
|
|
29
|
+
key,
|
|
30
|
+
waited
|
|
31
|
+
}]
|
|
32
|
+
}
|
|
33
|
+
} });
|
|
34
|
+
}, {
|
|
35
|
+
ttl,
|
|
36
|
+
timeout,
|
|
37
|
+
signal: middlewareOptions.signal
|
|
38
|
+
});
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (!acquired && error instanceof LockTimeoutError) {
|
|
41
|
+
throw new ORPCError("CONFLICT", { cause: error });
|
|
42
|
+
}
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Promisable } from "@orpc/shared";
|
|
2
|
+
export interface LockOptions {
|
|
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
|
+
}
|
|
21
|
+
export interface LockCallbackOptions {
|
|
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
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Standard interface for running work under a lock, so that work sharing
|
|
30
|
+
* the same key never runs concurrently.
|
|
31
|
+
* Implement it for custom strategies, or use one of the provided adapters.
|
|
32
|
+
*
|
|
33
|
+
* @see {@link https://orpc.dev/docs/helpers/lock | Lock Helpers}
|
|
34
|
+
*/
|
|
35
|
+
export interface Locker {
|
|
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/dist/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/experimental-lock",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.48",
|
|
5
5
|
"description": "Locking for oRPC procedures, with memory, Redis, and Upstash adapters",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"funding": [
|
|
@@ -70,12 +70,12 @@
|
|
|
70
70
|
}
|
|
71
71
|
},
|
|
72
72
|
"dependencies": {
|
|
73
|
-
"@orpc/server": "2.0.0-beta.
|
|
74
|
-
"@orpc/shared": "2.0.0-beta.
|
|
73
|
+
"@orpc/server": "2.0.0-beta.48",
|
|
74
|
+
"@orpc/shared": "2.0.0-beta.48"
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
77
|
-
"@upstash/redis": "^1.
|
|
78
|
-
"redis": "^6.3.
|
|
77
|
+
"@upstash/redis": "^1.40.0",
|
|
78
|
+
"redis": "^6.3.1"
|
|
79
79
|
},
|
|
80
80
|
"scripts": {
|
|
81
81
|
"type:check": "tsc -b"
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { Promisable } from '@orpc/shared';
|
|
2
|
-
import { Locker, LockCallbackOptions, LockOptions } from '../index.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Options shared by every Redis-backed locker adapter.
|
|
6
|
-
*
|
|
7
|
-
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
8
|
-
*/
|
|
9
|
-
interface BaseRedisLockerOptions {
|
|
10
|
-
/**
|
|
11
|
-
* The prefix to use for Redis keys.
|
|
12
|
-
*
|
|
13
|
-
* @default ''
|
|
14
|
-
*/
|
|
15
|
-
prefix?: string | undefined;
|
|
16
|
-
/**
|
|
17
|
-
* How long a lock is held before it expires automatically, in milliseconds.
|
|
18
|
-
* Guards against holders that never release the lock, such as a crashed process.
|
|
19
|
-
* Can be overridden per call.
|
|
20
|
-
*/
|
|
21
|
-
ttl: number;
|
|
22
|
-
/**
|
|
23
|
-
* How long to wait for a lock to become available, in milliseconds.
|
|
24
|
-
* Can be overridden per call.
|
|
25
|
-
*
|
|
26
|
-
* @default 10000
|
|
27
|
-
*/
|
|
28
|
-
timeout?: number | undefined;
|
|
29
|
-
/**
|
|
30
|
-
* How long to wait between acquisition attempts while the lock
|
|
31
|
-
* is held by someone else, in milliseconds.
|
|
32
|
-
*
|
|
33
|
-
* @default 100
|
|
34
|
-
*/
|
|
35
|
-
retryInterval?: number | undefined;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Base class for Redis-backed locker adapters. It owns the key naming, the lock
|
|
39
|
-
* token, the acquisition loop, and the atomic Lua release, so every adapter built
|
|
40
|
-
* on it can share locks with the others regardless of the Redis client in use.
|
|
41
|
-
*
|
|
42
|
-
* Extend it and implement the abstract methods to support another Redis client.
|
|
43
|
-
*
|
|
44
|
-
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
45
|
-
*/
|
|
46
|
-
declare abstract class BaseRedisLocker implements Locker {
|
|
47
|
-
protected readonly keyPrefix: string;
|
|
48
|
-
protected readonly ttl: number;
|
|
49
|
-
protected readonly timeout: number;
|
|
50
|
-
protected readonly retryInterval: number;
|
|
51
|
-
constructor(options: BaseRedisLockerOptions);
|
|
52
|
-
/**
|
|
53
|
-
* Sets `key` to `token` only when it does not exist yet, with an expiry
|
|
54
|
-
* of `ttl` milliseconds (`SET key token NX PX ttl`), and resolves with
|
|
55
|
-
* whether the key was set.
|
|
56
|
-
*/
|
|
57
|
-
protected abstract acquire(key: string, token: string, ttl: number): Promise<boolean>;
|
|
58
|
-
/**
|
|
59
|
-
* Runs a Lua script (`EVAL script numkeys key [key ...] arg [arg ...]`).
|
|
60
|
-
*/
|
|
61
|
-
protected abstract evalScript(script: string, keys: string[], args: string[]): Promise<unknown>;
|
|
62
|
-
lock<T>(key: string, fn: (options: LockCallbackOptions) => Promisable<T>, options?: LockOptions): Promise<T>;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export { BaseRedisLocker };
|
|
66
|
-
export type { BaseRedisLockerOptions };
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { Promisable } from '@orpc/shared';
|
|
2
|
-
import { Locker, LockCallbackOptions, LockOptions } from '../index.js';
|
|
3
|
-
|
|
4
|
-
interface MemoryLockerOptions {
|
|
5
|
-
/**
|
|
6
|
-
* How long a lock is held before it expires automatically, in milliseconds.
|
|
7
|
-
* Guards against holders that never release the lock.
|
|
8
|
-
* Can be overridden per call.
|
|
9
|
-
*
|
|
10
|
-
* @default undefined (held until released)
|
|
11
|
-
*/
|
|
12
|
-
ttl?: number | undefined;
|
|
13
|
-
/**
|
|
14
|
-
* How long to wait for a lock to become available, in milliseconds.
|
|
15
|
-
* Can be overridden per call.
|
|
16
|
-
*
|
|
17
|
-
* @default 10000
|
|
18
|
-
*/
|
|
19
|
-
timeout?: number | undefined;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Locker adapter backed by in-memory storage, so locks are only shared within
|
|
23
|
-
* the current process. Waiters acquire the lock in order, without polling.
|
|
24
|
-
*
|
|
25
|
-
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
26
|
-
*/
|
|
27
|
-
declare class MemoryLocker implements Locker {
|
|
28
|
-
private readonly ttl;
|
|
29
|
-
private readonly timeout;
|
|
30
|
-
private readonly entries;
|
|
31
|
-
constructor(options?: MemoryLockerOptions);
|
|
32
|
-
lock<T>(key: string, fn: (options: LockCallbackOptions) => Promisable<T>, options?: LockOptions): Promise<T>;
|
|
33
|
-
private wait;
|
|
34
|
-
private release;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export { MemoryLocker };
|
|
38
|
-
export type { MemoryLockerOptions };
|
package/dist/adapters/redis.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { RedisClientType } from 'redis';
|
|
2
|
-
import { BaseRedisLocker, BaseRedisLockerOptions } from './base-redis.js';
|
|
3
|
-
import '@orpc/shared';
|
|
4
|
-
import '../shared/experimental-lock.D9u4cJfq.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Locker adapter for Redis. Acquires locks with `SET NX PX` and releases them
|
|
8
|
-
* atomically with a Lua script, so every instance using the same server shares
|
|
9
|
-
* the same locks.
|
|
10
|
-
*
|
|
11
|
-
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
12
|
-
*/
|
|
13
|
-
declare class RedisLocker extends BaseRedisLocker {
|
|
14
|
-
private readonly redis;
|
|
15
|
-
private readonly options;
|
|
16
|
-
constructor(redis: RedisClientType<any, any, any, any, any>, options: BaseRedisLockerOptions);
|
|
17
|
-
/**
|
|
18
|
-
* Creates a locker with `prefix` appended to this locker's prefix, sharing its Redis client.
|
|
19
|
-
*
|
|
20
|
-
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
21
|
-
*/
|
|
22
|
-
prefix(prefix: string): RedisLocker;
|
|
23
|
-
protected acquire(key: string, token: string, ttl: number): Promise<boolean>;
|
|
24
|
-
protected evalScript(script: string, keys: string[], args: string[]): Promise<unknown>;
|
|
25
|
-
private connectIfNeeded;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export { RedisLocker };
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { Redis } from '@upstash/redis';
|
|
2
|
-
import { BaseRedisLocker, BaseRedisLockerOptions } from './base-redis.js';
|
|
3
|
-
import '@orpc/shared';
|
|
4
|
-
import '../shared/experimental-lock.D9u4cJfq.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Locker adapter for Upstash Redis. Acquires locks with `SET NX PX` and releases
|
|
8
|
-
* them atomically with a Lua script, so it shares locks with `RedisLocker`.
|
|
9
|
-
* A good fit for serverless and edge runtimes.
|
|
10
|
-
*
|
|
11
|
-
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
12
|
-
*/
|
|
13
|
-
declare class UpstashLocker extends BaseRedisLocker {
|
|
14
|
-
private readonly redis;
|
|
15
|
-
private readonly options;
|
|
16
|
-
constructor(redis: Redis, options: BaseRedisLockerOptions);
|
|
17
|
-
/**
|
|
18
|
-
* Creates a locker with `prefix` appended to this locker's prefix, sharing its Redis client.
|
|
19
|
-
*
|
|
20
|
-
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
21
|
-
*/
|
|
22
|
-
prefix(prefix: string): UpstashLocker;
|
|
23
|
-
protected acquire(key: string, token: string, ttl: number): Promise<boolean>;
|
|
24
|
-
protected evalScript(script: string, keys: string[], args: string[]): Promise<unknown>;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export { UpstashLocker };
|
package/dist/index.d.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { Context, MiddlewareOptions, Middleware } from '@orpc/server';
|
|
2
|
-
import { Value, Promisable } from '@orpc/shared';
|
|
3
|
-
import { L as Locker } from './shared/experimental-lock.D9u4cJfq.js';
|
|
4
|
-
export { a as LockCallbackOptions, b as LockOptions } from './shared/experimental-lock.D9u4cJfq.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Thrown when a lock cannot be acquired before the timeout elapses.
|
|
8
|
-
*
|
|
9
|
-
* @see {@link https://orpc.dev/docs/helpers/lock#options | Lock Helpers - Options}
|
|
10
|
-
*/
|
|
11
|
-
declare class LockTimeoutError extends Error {
|
|
12
|
-
/**
|
|
13
|
-
* The key of the lock that could not be acquired.
|
|
14
|
-
*/
|
|
15
|
-
readonly key: string;
|
|
16
|
-
constructor(key: string);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
declare const LOCK_MIDDLEWARE_CONTEXT_SYMBOL: unique symbol;
|
|
20
|
-
interface LockMiddlewareContext {
|
|
21
|
-
[LOCK_MIDDLEWARE_CONTEXT_SYMBOL]?: {
|
|
22
|
-
/**
|
|
23
|
-
* The locks held in this request, mainly for deduplication purposes
|
|
24
|
-
*/
|
|
25
|
-
held: {
|
|
26
|
-
locker: Locker;
|
|
27
|
-
key: string;
|
|
28
|
-
waited: boolean;
|
|
29
|
-
}[];
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
interface LockMiddlewareOutContext {
|
|
33
|
-
/**
|
|
34
|
-
* Whether the lock was acquired only after waiting for another holder to release it.
|
|
35
|
-
* `false` means the lock was acquired immediately.
|
|
36
|
-
*/
|
|
37
|
-
['lock/waited']: boolean;
|
|
38
|
-
}
|
|
39
|
-
interface LockMiddlewareOptions<TInContext extends Context, TInput> {
|
|
40
|
-
/**
|
|
41
|
-
* The locker to acquire the lock from
|
|
42
|
-
*/
|
|
43
|
-
locker: Value<Promisable<Locker>, [options: MiddlewareOptions<TInContext, unknown, Record<never, never>>, input: TInput]>;
|
|
44
|
-
/**
|
|
45
|
-
* The key to identify the work that must not run concurrently
|
|
46
|
-
*/
|
|
47
|
-
key: Value<Promisable<string>, [options: MiddlewareOptions<TInContext, unknown, Record<never, never>>, input: TInput]>;
|
|
48
|
-
/**
|
|
49
|
-
* How long the lock is held before it expires automatically, in milliseconds.
|
|
50
|
-
*
|
|
51
|
-
* @default the adapter default
|
|
52
|
-
*/
|
|
53
|
-
ttl?: Value<Promisable<number | undefined>, [options: MiddlewareOptions<TInContext, unknown, Record<never, never>>, input: TInput]> | undefined;
|
|
54
|
-
/**
|
|
55
|
-
* How long to wait for the lock to become available, in milliseconds.
|
|
56
|
-
*
|
|
57
|
-
* @default the adapter default
|
|
58
|
-
*/
|
|
59
|
-
timeout?: Value<Promisable<number | undefined>, [options: MiddlewareOptions<TInContext, unknown, Record<never, never>>, input: TInput]> | undefined;
|
|
60
|
-
/**
|
|
61
|
-
* If your lock middleware is used multiple times
|
|
62
|
-
* or you invoke a procedure inside another procedure (shared the same context) that also has
|
|
63
|
-
* lock middleware **with the same locker and key**, this option
|
|
64
|
-
* will ensure that the lock is only acquired once per request,
|
|
65
|
-
* instead of waiting for itself until the timeout elapses.
|
|
66
|
-
*
|
|
67
|
-
* @default true
|
|
68
|
-
*/
|
|
69
|
-
dedupe?: boolean | undefined;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Creates a middleware that runs oRPC procedures under a lock, so that calls
|
|
73
|
-
* sharing the same key never run concurrently. Rejects with a `CONFLICT` error
|
|
74
|
-
* when the lock cannot be acquired before the timeout elapses.
|
|
75
|
-
*
|
|
76
|
-
* @see {@link https://orpc.dev/docs/helpers/lock#lock-middleware | Lock Helpers - Lock Middleware}
|
|
77
|
-
*/
|
|
78
|
-
declare function lock<TInContext extends Context, TInput>({ dedupe, ...options }: LockMiddlewareOptions<TInContext, TInput>): Middleware<TInContext, LockMiddlewareOutContext, TInput, any, object>;
|
|
79
|
-
|
|
80
|
-
export { LOCK_MIDDLEWARE_CONTEXT_SYMBOL, LockTimeoutError, Locker, lock };
|
|
81
|
-
export type { LockMiddlewareContext, LockMiddlewareOptions, LockMiddlewareOutContext };
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
class LockTimeoutError extends Error {
|
|
2
|
-
/**
|
|
3
|
-
* The key of the lock that could not be acquired.
|
|
4
|
-
*/
|
|
5
|
-
key;
|
|
6
|
-
constructor(key) {
|
|
7
|
-
super(`Timed out waiting for the lock of key "${key}"`);
|
|
8
|
-
this.name = "LockTimeoutError";
|
|
9
|
-
this.key = key;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export { LockTimeoutError as L };
|