@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 CHANGED
@@ -191,15 +191,14 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
191
191
  <td align="center"><a href="https://kellychan.im/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&amp;v=4" width="139" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
192
192
  <td align="center"><a href="https://guyariely.com/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Guy Ariely"><img src="https://avatars.githubusercontent.com/u/42813496?u=edb6b7f563bf28e160a290832e7da57c0506f8ca&amp;v=4" width="139" alt="Guy Ariely"/><br />Guy Ariely</a></td>
193
193
  <td align="center"><a href="https://paulsenon.com/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="PaulSenon"><img src="https://avatars.githubusercontent.com/u/19531087?u=6385741eb91d200b8c513ec6045482e301767ca6&amp;v=4" width="139" alt="PaulSenon"/><br />PaulSenon</a></td>
194
- <td align="center"><a href="https://nakasyou.how/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Shotaro Nakamura"><img src="https://avatars.githubusercontent.com/u/79000684?u=f644df3f29f0e8677a90967115774564f1d9d6ab&amp;v=4" width="139" alt="Shotaro Nakamura"/><br />Shotaro Nakamura</a></td>
194
+ <td align="center"><a href="https://piscis.dev/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&amp;v=4" width="139" alt="Alex"/><br />Alex</a></td>
195
195
  </tr>
196
196
  <tr>
197
- <td align="center"><a href="https://piscis.dev/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&amp;v=4" width="139" alt="Alex"/><br />Alex</a></td>
198
197
  <td align="center"><a href="https://opensource.gubanov.eu/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=c5f2daf7ebece498e85c83367bb37b4e10e2649d&amp;v=4" width="139" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
199
198
  </tr>
200
199
  </table>
201
200
 
202
- With thanks to [39 past sponsors](https://orpc.dev/sponsors#past-sponsors) who helped get oRPC here.
201
+ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who helped get oRPC here.
203
202
 
204
203
  ## Open Source Programs
205
204
 
@@ -1,66 +1,62 @@
1
- import { Promisable } from '@orpc/shared';
2
- import { Locker, LockCallbackOptions, LockOptions } from '../index.mjs';
3
-
1
+ import type { Promisable } from "@orpc/shared";
2
+ import type { LockCallbackOptions, Locker, LockOptions } from "../types.mjs";
4
3
  /**
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;
4
+ * Options shared by every Redis-backed locker adapter.
5
+ *
6
+ * @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
7
+ */
8
+ export interface BaseRedisLockerOptions {
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;
36
35
  }
37
36
  /**
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>;
37
+ * Base class for Redis-backed locker adapters. It owns the key naming, the lock
38
+ * token, the acquisition loop, and the atomic Lua release, so every adapter built
39
+ * on it can share locks with the others regardless of the Redis client in use.
40
+ *
41
+ * Extend it and implement the abstract methods to support another Redis client.
42
+ *
43
+ * @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
44
+ */
45
+ export declare abstract class BaseRedisLocker implements Locker {
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>;
63
62
  }
64
-
65
- export { BaseRedisLocker };
66
- export type { BaseRedisLockerOptions };
@@ -1,6 +1,5 @@
1
- import { throwIfAborted, sleep } from '@orpc/shared';
2
- import { L as LockTimeoutError } from '../shared/experimental-lock.CoYbYjCJ.mjs';
3
-
1
+ import { sleep, throwIfAborted } from "@orpc/shared";
2
+ import { LockTimeoutError } from "../error.mjs";
4
3
  const RELEASE_LOCK_SCRIPT = `
5
4
  if redis.call('GET', KEYS[1]) == ARGV[1] then
6
5
  return redis.call('DEL', KEYS[1])
@@ -8,38 +7,37 @@ end
8
7
 
9
8
  return 0
10
9
  `;
11
- class BaseRedisLocker {
12
- keyPrefix;
13
- ttl;
14
- timeout;
15
- retryInterval;
16
- constructor(options) {
17
- this.keyPrefix = options.prefix ?? "";
18
- this.ttl = options.ttl;
19
- this.timeout = options.timeout ?? 1e4;
20
- this.retryInterval = options.retryInterval ?? 100;
21
- }
22
- async lock(key, fn, options = {}) {
23
- const prefixedKey = `${this.keyPrefix}${key}`;
24
- const ttl = options.ttl ?? this.ttl;
25
- const deadline = Date.now() + (options.timeout ?? this.timeout);
26
- const token = crypto.randomUUID();
27
- let waited = false;
28
- throwIfAborted(options.signal);
29
- while (!await this.acquire(prefixedKey, token, ttl)) {
30
- const remaining = deadline - Date.now();
31
- if (remaining <= 0) {
32
- throw new LockTimeoutError(key);
33
- }
34
- waited = true;
35
- await sleep(Math.min(this.retryInterval, remaining), { signal: options.signal });
36
- }
37
- try {
38
- return await fn({ waited });
39
- } finally {
40
- await this.evalScript(RELEASE_LOCK_SCRIPT, [prefixedKey], [token]);
41
- }
42
- }
43
- }
44
10
 
45
- export { BaseRedisLocker };
11
+ export class BaseRedisLocker {
12
+ keyPrefix;
13
+ ttl;
14
+ timeout;
15
+ retryInterval;
16
+ constructor(options) {
17
+ this.keyPrefix = options.prefix ?? "";
18
+ this.ttl = options.ttl;
19
+ this.timeout = options.timeout ?? 1e4;
20
+ this.retryInterval = options.retryInterval ?? 100;
21
+ }
22
+ async lock(key, fn, options = {}) {
23
+ const prefixedKey = `${this.keyPrefix}${key}`;
24
+ const ttl = options.ttl ?? this.ttl;
25
+ const deadline = Date.now() + (options.timeout ?? this.timeout);
26
+ const token = crypto.randomUUID();
27
+ let waited = false;
28
+ throwIfAborted(options.signal);
29
+ while (!await this.acquire(prefixedKey, token, ttl)) {
30
+ const remaining = deadline - Date.now();
31
+ if (remaining <= 0) {
32
+ throw new LockTimeoutError(key);
33
+ }
34
+ waited = true;
35
+ await sleep(Math.min(this.retryInterval, remaining), { signal: options.signal });
36
+ }
37
+ try {
38
+ return await fn({ waited });
39
+ } finally {
40
+ await this.evalScript(RELEASE_LOCK_SCRIPT, [prefixedKey], [token]);
41
+ }
42
+ }
43
+ }
@@ -1,38 +1,34 @@
1
- import { Promisable } from '@orpc/shared';
2
- import { Locker, LockCallbackOptions, LockOptions } from '../index.mjs';
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;
1
+ import type { Promisable } from "@orpc/shared";
2
+ import type { LockCallbackOptions, Locker, LockOptions } from "../types.mjs";
3
+ export interface MemoryLockerOptions {
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;
20
19
  }
21
20
  /**
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;
21
+ * Locker adapter backed by in-memory storage, so locks are only shared within
22
+ * the current process. Waiters acquire the lock in order, without polling.
23
+ *
24
+ * @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
25
+ */
26
+ export declare class MemoryLocker implements Locker {
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;
35
34
  }
36
-
37
- export { MemoryLocker };
38
- export type { MemoryLockerOptions };
@@ -1,70 +1,74 @@
1
- import { throwIfAborted, promiseWithResolvers } from '@orpc/shared';
2
- import { L as LockTimeoutError } from '../shared/experimental-lock.CoYbYjCJ.mjs';
1
+ import { promiseWithResolvers, throwIfAborted } from "@orpc/shared";
2
+ import { LockTimeoutError } from "../error.mjs";
3
3
 
4
- class MemoryLocker {
5
- ttl;
6
- timeout;
7
- entries = /* @__PURE__ */ new Map();
8
- constructor(options = {}) {
9
- this.ttl = options.ttl;
10
- this.timeout = options.timeout ?? 1e4;
11
- }
12
- async lock(key, fn, options = {}) {
13
- const ttl = options.ttl ?? this.ttl;
14
- const timeout = options.timeout ?? this.timeout;
15
- const token = {};
16
- throwIfAborted(options.signal);
17
- let entry = this.entries.get(key);
18
- const waited = entry !== void 0;
19
- if (entry) {
20
- await this.wait(key, entry, token, timeout, options.signal);
21
- } else {
22
- entry = { holder: token, waiters: /* @__PURE__ */ new Set() };
23
- this.entries.set(key, entry);
24
- }
25
- if (ttl !== void 0) {
26
- entry.expiry = setTimeout(() => this.release(key, token), ttl);
27
- }
28
- try {
29
- return await fn({ waited });
30
- } finally {
31
- this.release(key, token);
32
- }
33
- }
34
- wait(key, entry, token, timeout, signal) {
35
- if (timeout <= 0) {
36
- return Promise.reject(new LockTimeoutError(key));
37
- }
38
- const { promise, resolve, reject } = promiseWithResolvers();
39
- const waiter = { token, resolve };
40
- entry.waiters.add(waiter);
41
- const fail = (reason) => {
42
- entry.waiters.delete(waiter);
43
- reject(reason);
44
- };
45
- const timer = setTimeout(() => fail(new LockTimeoutError(key)), timeout);
46
- const abortListener = () => fail(signal?.reason);
47
- signal?.addEventListener("abort", abortListener, { once: true });
48
- return promise.finally(() => {
49
- clearTimeout(timer);
50
- signal?.removeEventListener("abort", abortListener);
51
- });
52
- }
53
- release(key, token) {
54
- const entry = this.entries.get(key);
55
- if (entry?.holder !== token) {
56
- return;
57
- }
58
- clearTimeout(entry.expiry);
59
- const [next] = entry.waiters;
60
- if (!next) {
61
- this.entries.delete(key);
62
- return;
63
- }
64
- entry.waiters.delete(next);
65
- entry.holder = next.token;
66
- next.resolve();
67
- }
4
+ export class MemoryLocker {
5
+ ttl;
6
+ timeout;
7
+ entries = new Map();
8
+ constructor(options = {}) {
9
+ this.ttl = options.ttl;
10
+ this.timeout = options.timeout ?? 1e4;
11
+ }
12
+ async lock(key, fn, options = {}) {
13
+ const ttl = options.ttl ?? this.ttl;
14
+ const timeout = options.timeout ?? this.timeout;
15
+ const token = {};
16
+ throwIfAborted(options.signal);
17
+ let entry = this.entries.get(key);
18
+ const waited = entry !== undefined;
19
+ if (entry) {
20
+ await this.wait(key, entry, token, timeout, options.signal);
21
+ } else {
22
+ entry = {
23
+ holder: token,
24
+ waiters: new Set()
25
+ };
26
+ this.entries.set(key, entry);
27
+ }
28
+ if (ttl !== undefined) {
29
+ entry.expiry = setTimeout(() => this.release(key, token), ttl);
30
+ }
31
+ try {
32
+ return await fn({ waited });
33
+ } finally {
34
+ this.release(key, token);
35
+ }
36
+ }
37
+ wait(key, entry, token, timeout, signal) {
38
+ if (timeout <= 0) {
39
+ return Promise.reject(new LockTimeoutError(key));
40
+ }
41
+ const { promise, resolve, reject } = promiseWithResolvers();
42
+ const waiter = {
43
+ token,
44
+ resolve
45
+ };
46
+ entry.waiters.add(waiter);
47
+ const fail = (reason) => {
48
+ entry.waiters.delete(waiter);
49
+ reject(reason);
50
+ };
51
+ const timer = setTimeout(() => fail(new LockTimeoutError(key)), timeout);
52
+ const abortListener = () => fail(signal?.reason);
53
+ signal?.addEventListener("abort", abortListener, { once: true });
54
+ return promise.finally(() => {
55
+ clearTimeout(timer);
56
+ signal?.removeEventListener("abort", abortListener);
57
+ });
58
+ }
59
+ release(key, token) {
60
+ const entry = this.entries.get(key);
61
+ if (entry?.holder !== token) {
62
+ return;
63
+ }
64
+ clearTimeout(entry.expiry);
65
+ const [next] = entry.waiters;
66
+ if (!next) {
67
+ this.entries.delete(key);
68
+ return;
69
+ }
70
+ entry.waiters.delete(next);
71
+ entry.holder = next.token;
72
+ next.resolve();
73
+ }
68
74
  }
69
-
70
- export { MemoryLocker };
@@ -1,28 +1,24 @@
1
- import { RedisClientType } from 'redis';
2
- import { BaseRedisLocker, BaseRedisLockerOptions } from './base-redis.mjs';
3
- import '@orpc/shared';
4
- import '../shared/experimental-lock.D9u4cJfq.mjs';
5
-
1
+ import type { RedisClientType } from "redis";
2
+ import type { BaseRedisLockerOptions } from "./base-redis.mjs";
3
+ import { BaseRedisLocker } from "./base-redis.mjs";
6
4
  /**
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;
5
+ * Locker adapter for Redis. Acquires locks with `SET NX PX` and releases them
6
+ * atomically with a Lua script, so every instance using the same server shares
7
+ * the same locks.
8
+ *
9
+ * @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
10
+ */
11
+ export declare class RedisLocker extends BaseRedisLocker {
12
+ private readonly redis;
13
+ private readonly options;
14
+ constructor(redis: RedisClientType<any, any, any, any, any>, options: BaseRedisLockerOptions);
15
+ /**
16
+ * Creates a locker with `prefix` appended to this locker's prefix, sharing its Redis client.
17
+ *
18
+ * @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
19
+ */
20
+ prefix(prefix: string): RedisLocker;
21
+ protected acquire(key: string, token: string, ttl: number): Promise<boolean>;
22
+ protected evalScript(script: string, keys: string[], args: string[]): Promise<unknown>;
23
+ private connectIfNeeded;
26
24
  }
27
-
28
- export { RedisLocker };
@@ -1,38 +1,41 @@
1
- import { BaseRedisLocker } from './base-redis.mjs';
2
- import '@orpc/shared';
3
- import '../shared/experimental-lock.CoYbYjCJ.mjs';
1
+ import { BaseRedisLocker } from "./base-redis.mjs";
4
2
 
5
- class RedisLocker extends BaseRedisLocker {
6
- constructor(redis, options) {
7
- super(options);
8
- this.redis = redis;
9
- this.options = options;
10
- }
11
- /**
12
- * Creates a locker with `prefix` appended to this locker's prefix, sharing its Redis client.
13
- *
14
- * @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
15
- */
16
- prefix(prefix) {
17
- return new RedisLocker(this.redis, { ...this.options, prefix: `${this.keyPrefix}${prefix}` });
18
- }
19
- async acquire(key, token, ttl) {
20
- await this.connectIfNeeded();
21
- const result = await this.redis.set(key, token, {
22
- condition: "NX",
23
- expiration: { type: "PX", value: ttl }
24
- });
25
- return result === "OK";
26
- }
27
- async evalScript(script, keys, args) {
28
- await this.connectIfNeeded();
29
- return await this.redis.eval(script, { keys, arguments: args });
30
- }
31
- async connectIfNeeded() {
32
- if (!this.redis.isOpen) {
33
- await this.redis.connect();
34
- }
35
- }
3
+ export class RedisLocker 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 RedisLocker(this.redis, {
14
+ ...this.options,
15
+ prefix: `${this.keyPrefix}${prefix}`
16
+ });
17
+ }
18
+ async acquire(key, token, ttl) {
19
+ await this.connectIfNeeded();
20
+ const result = await this.redis.set(key, token, {
21
+ condition: "NX",
22
+ expiration: {
23
+ type: "PX",
24
+ value: ttl
25
+ }
26
+ });
27
+ return result === "OK";
28
+ }
29
+ async evalScript(script, keys, args) {
30
+ await this.connectIfNeeded();
31
+ return await this.redis.eval(script, {
32
+ keys,
33
+ arguments: args
34
+ });
35
+ }
36
+ async connectIfNeeded() {
37
+ if (!this.redis.isOpen) {
38
+ await this.redis.connect();
39
+ }
40
+ }
36
41
  }
37
-
38
- export { RedisLocker };
@@ -1,27 +1,23 @@
1
- import { Redis } from '@upstash/redis';
2
- import { BaseRedisLocker, BaseRedisLockerOptions } from './base-redis.mjs';
3
- import '@orpc/shared';
4
- import '../shared/experimental-lock.D9u4cJfq.mjs';
5
-
1
+ import type { Redis } from "@upstash/redis";
2
+ import type { BaseRedisLockerOptions } from "./base-redis.mjs";
3
+ import { BaseRedisLocker } from "./base-redis.mjs";
6
4
  /**
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>;
5
+ * Locker adapter for Upstash Redis. Acquires locks with `SET NX PX` and releases
6
+ * them atomically with a Lua script, so it shares locks with `RedisLocker`.
7
+ * A good fit for serverless and edge runtimes.
8
+ *
9
+ * @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
10
+ */
11
+ export declare class UpstashLocker extends BaseRedisLocker {
12
+ private readonly redis;
13
+ private readonly options;
14
+ constructor(redis: Redis, options: BaseRedisLockerOptions);
15
+ /**
16
+ * Creates a locker with `prefix` appended to this locker's prefix, sharing its Redis client.
17
+ *
18
+ * @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
19
+ */
20
+ prefix(prefix: string): UpstashLocker;
21
+ protected acquire(key: string, token: string, ttl: number): Promise<boolean>;
22
+ protected evalScript(script: string, keys: string[], args: string[]): Promise<unknown>;
25
23
  }
26
-
27
- export { UpstashLocker };