edge-throttle 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+
3
+ // src/adapters/redis.ts
4
+ var RedisAdapter = class {
5
+ client;
6
+ isNodeRedis;
7
+ constructor(options) {
8
+ if ("client" in options) {
9
+ this.client = options.client;
10
+ this.isNodeRedis = options.clientType !== void 0 ? options.clientType === "node-redis" : detectNodeRedis(options.client);
11
+ } else {
12
+ this.client = options;
13
+ this.isNodeRedis = detectNodeRedis(options);
14
+ }
15
+ }
16
+ async get(key) {
17
+ const raw = await this.client.get(key);
18
+ if (raw === null || raw === void 0) {
19
+ return null;
20
+ }
21
+ try {
22
+ return JSON.parse(raw);
23
+ } catch {
24
+ return raw;
25
+ }
26
+ }
27
+ async set(key, value, ttlMs) {
28
+ const ttl = Math.max(1, Math.ceil(ttlMs));
29
+ const serialized = JSON.stringify(value);
30
+ if (this.isNodeRedis) {
31
+ await this.client.set(key, serialized, { PX: ttl });
32
+ } else {
33
+ await this.client.set(key, serialized, "PX", ttl);
34
+ }
35
+ }
36
+ async increment(key, amount, ttlMs) {
37
+ const ttl = Math.max(1, Math.ceil(ttlMs));
38
+ if (this.isNodeRedis && this.client.incrByFloat) {
39
+ const next2 = Number(await this.client.incrByFloat(key, amount));
40
+ if (this.client.pExpire) {
41
+ await this.client.pExpire(key, ttl);
42
+ }
43
+ return next2;
44
+ }
45
+ if (this.client.incrbyfloat) {
46
+ const next2 = Number(await this.client.incrbyfloat(key, amount));
47
+ if (this.client.pexpire) {
48
+ await this.client.pexpire(key, ttl);
49
+ }
50
+ return next2;
51
+ }
52
+ const current = await this.get(key) ?? 0;
53
+ const next = current + amount;
54
+ await this.set(key, next, ttl);
55
+ return next;
56
+ }
57
+ async eval(script, keys, args) {
58
+ if (this.isNodeRedis) {
59
+ return this.client.eval(script, {
60
+ keys,
61
+ arguments: args.map(String)
62
+ });
63
+ }
64
+ return this.client.eval(script, keys.length, ...keys, ...args);
65
+ }
66
+ };
67
+ function detectNodeRedis(client) {
68
+ if (typeof client.defineCommand === "function") {
69
+ return false;
70
+ }
71
+ return "isOpen" in client || typeof client.incrByFloat === "function";
72
+ }
73
+
74
+ exports.RedisAdapter = RedisAdapter;
75
+ //# sourceMappingURL=redis.cjs.map
76
+ //# sourceMappingURL=redis.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/adapters/redis.ts"],"names":["next"],"mappings":";;;AAmCO,IAAM,eAAN,MAA6C;AAAA,EACjC,MAAA;AAAA,EACA,WAAA;AAAA,EAEjB,YAAY,OAAA,EAAgD;AAC1D,IAAA,IAAI,YAAY,OAAA,EAAS;AACvB,MAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AACtB,MAAA,IAAA,CAAK,WAAA,GACH,QAAQ,UAAA,KAAe,MAAA,GACnB,QAAQ,UAAA,KAAe,YAAA,GACvB,eAAA,CAAgB,OAAA,CAAQ,MAAM,CAAA;AAAA,IACtC,CAAA,MAAO;AACL,MAAA,IAAA,CAAK,MAAA,GAAS,OAAA;AACd,MAAA,IAAA,CAAK,WAAA,GAAc,gBAAgB,OAAO,CAAA;AAAA,IAC5C;AAAA,EACF;AAAA,EAEA,MAAM,IAAO,GAAA,EAAgC;AAC3C,IAAA,MAAM,GAAA,GAAM,MAAM,IAAA,CAAK,MAAA,CAAO,IAAI,GAAG,CAAA;AACrC,IAAA,IAAI,GAAA,KAAQ,IAAA,IAAQ,GAAA,KAAQ,MAAA,EAAW;AACrC,MAAA,OAAO,IAAA;AAAA,IACT;AAEA,IAAA,IAAI;AACF,MAAA,OAAO,IAAA,CAAK,MAAM,GAAG,CAAA;AAAA,IACvB,CAAA,CAAA,MAAQ;AACN,MAAA,OAAO,GAAA;AAAA,IACT;AAAA,EACF;AAAA,EAEA,MAAM,GAAA,CAAO,GAAA,EAAa,KAAA,EAAU,KAAA,EAA8B;AAChE,IAAA,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AA,MACpC;AACA,MAAA,OAAOA,KAAAA;AAAA,IACT;AAEA,IAAA,MAAM,OAAA,GAAW,MAAM,IAAA,CAAK,GAAA,CAAY,GAAG,CAAA,IAAM,CAAA;AACjD,IAAA,MAAM,OAAO,OAAA,GAAU,MAAA;AACvB,IAAA,MAAM,IAAA,CAAK,GAAA,CAAI,GAAA,EAAK,IAAA,EAAM,GAAG,CAAA;AAC7B,IAAA,OAAO,IAAA;AAAA,EACT;AAAA,EAEA,MAAM,IAAA,CAAK,MAAA,EAAgB,IAAA,EAAgB,IAAA,EAA6C;AACtF,IAAA,IAAI,KAAK,WAAA,EAAa;AACpB,MAAA,OAAO,IAAA,CAAK,MAAA,CAAO,IAAA,CAAK,MAAA,EAAQ;AAAA,QAC9B,IAAA;AAAA,QACA,SAAA,EAAW,IAAA,CAAK,GAAA,CAAI,MAAM;AAAA,OAC3B,CAAA;AAAA,IACH;AAEA,IAAA,OAAO,IAAA,CAAK,OAAO,IAAA,CAAK,MAAA,EAAQ,KAAK,MAAA,EAAQ,GAAG,IAAA,EAAM,GAAG,IAAI,CAAA;AAAA,EAC/D;AACF;AAEA,SAAS,gBAAgB,MAAA,EAAkC;AACzD,EAAA,IAAI,OAAO,MAAA,CAAO,aAAA,KAAkB,UAAA,EAAY;AAC9C,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,OAAO,QAAA,IAAY,MAAA,IAAU,OAAO,MAAA,CAAO,WAAA,KAAgB,UAAA;AAC7D","file":"redis.cjs","sourcesContent":["import type { StorageAdapter } from '../types.js';\n\n/**\n * Structural interface compatible with `ioredis` and `node-redis` v4+ clients.\n */\nexport interface RedisClientLike {\n get(key: string): Promise<string | null>;\n set(key: string, value: string, ...args: unknown[]): Promise<unknown>;\n eval(script: string, ...args: unknown[]): Promise<unknown>;\n incrbyfloat?(key: string, increment: number | string): Promise<string | number>;\n incrByFloat?(key: string, increment: number): Promise<string>;\n pexpire?(key: string, milliseconds: number): Promise<number | boolean>;\n pExpire?(key: string, milliseconds: number): Promise<number | boolean>;\n defineCommand?: unknown;\n isOpen?: boolean;\n}\n\n/**\n * Configuration options for `RedisAdapter`.\n */\nexport interface RedisAdapterOptions {\n /** Connected `ioredis` or `node-redis` client instance. */\n client: RedisClientLike;\n /**\n * Explicitly specify the client dialect (`\"ioredis\"` or `\"node-redis\"`).\n * Auto-detected if omitted.\n */\n clientType?: 'ioredis' | 'node-redis';\n}\n\n/**\n * Standard Redis `StorageAdapter` supporting `ioredis` and `node-redis` clients.\n *\n * Uses atomic Lua scripts via `eval` to prevent race conditions under high concurrency.\n */\nexport class RedisAdapter implements StorageAdapter {\n private readonly client: RedisClientLike;\n private readonly isNodeRedis: boolean;\n\n constructor(options: RedisAdapterOptions | RedisClientLike) {\n if ('client' in options) {\n this.client = options.client;\n this.isNodeRedis =\n options.clientType !== undefined\n ? options.clientType === 'node-redis'\n : detectNodeRedis(options.client);\n } else {\n this.client = options;\n this.isNodeRedis = detectNodeRedis(options);\n }\n }\n\n async get<T>(key: string): Promise<T | null> {\n const raw = await this.client.get(key);\n if (raw === null || raw === undefined) {\n return null;\n }\n\n try {\n return JSON.parse(raw) as T;\n } catch {\n return raw as unknown as T;\n }\n }\n\n async set<T>(key: string, value: T, ttlMs: number): Promise<void> {\n const ttl = Math.max(1, Math.ceil(ttlMs));\n const serialized = JSON.stringify(value);\n\n if (this.isNodeRedis) {\n await this.client.set(key, serialized, { PX: ttl });\n } else {\n await this.client.set(key, serialized, 'PX', ttl);\n }\n }\n\n async increment(key: string, amount: number, ttlMs: number): Promise<number> {\n const ttl = Math.max(1, Math.ceil(ttlMs));\n\n if (this.isNodeRedis && this.client.incrByFloat) {\n const next = Number(await this.client.incrByFloat(key, amount));\n if (this.client.pExpire) {\n await this.client.pExpire(key, ttl);\n }\n return next;\n }\n\n if (this.client.incrbyfloat) {\n const next = Number(await this.client.incrbyfloat(key, amount));\n if (this.client.pexpire) {\n await this.client.pexpire(key, ttl);\n }\n return next;\n }\n\n const current = (await this.get<number>(key)) ?? 0;\n const next = current + amount;\n await this.set(key, next, ttl);\n return next;\n }\n\n async eval(script: string, keys: string[], args: (string | number)[]): Promise<unknown> {\n if (this.isNodeRedis) {\n return this.client.eval(script, {\n keys,\n arguments: args.map(String),\n });\n }\n\n return this.client.eval(script, keys.length, ...keys, ...args);\n }\n}\n\nfunction detectNodeRedis(client: RedisClientLike): boolean {\n if (typeof client.defineCommand === 'function') {\n return false;\n }\n return 'isOpen' in client || typeof client.incrByFloat === 'function';\n}\n"]}
@@ -0,0 +1,44 @@
1
+ import { S as StorageAdapter } from '../types-Ca_Pa4mZ.cjs';
2
+
3
+ /**
4
+ * Structural interface compatible with `ioredis` and `node-redis` v4+ clients.
5
+ */
6
+ interface RedisClientLike {
7
+ get(key: string): Promise<string | null>;
8
+ set(key: string, value: string, ...args: unknown[]): Promise<unknown>;
9
+ eval(script: string, ...args: unknown[]): Promise<unknown>;
10
+ incrbyfloat?(key: string, increment: number | string): Promise<string | number>;
11
+ incrByFloat?(key: string, increment: number): Promise<string>;
12
+ pexpire?(key: string, milliseconds: number): Promise<number | boolean>;
13
+ pExpire?(key: string, milliseconds: number): Promise<number | boolean>;
14
+ defineCommand?: unknown;
15
+ isOpen?: boolean;
16
+ }
17
+ /**
18
+ * Configuration options for `RedisAdapter`.
19
+ */
20
+ interface RedisAdapterOptions {
21
+ /** Connected `ioredis` or `node-redis` client instance. */
22
+ client: RedisClientLike;
23
+ /**
24
+ * Explicitly specify the client dialect (`"ioredis"` or `"node-redis"`).
25
+ * Auto-detected if omitted.
26
+ */
27
+ clientType?: 'ioredis' | 'node-redis';
28
+ }
29
+ /**
30
+ * Standard Redis `StorageAdapter` supporting `ioredis` and `node-redis` clients.
31
+ *
32
+ * Uses atomic Lua scripts via `eval` to prevent race conditions under high concurrency.
33
+ */
34
+ declare class RedisAdapter implements StorageAdapter {
35
+ private readonly client;
36
+ private readonly isNodeRedis;
37
+ constructor(options: RedisAdapterOptions | RedisClientLike);
38
+ get<T>(key: string): Promise<T | null>;
39
+ set<T>(key: string, value: T, ttlMs: number): Promise<void>;
40
+ increment(key: string, amount: number, ttlMs: number): Promise<number>;
41
+ eval(script: string, keys: string[], args: (string | number)[]): Promise<unknown>;
42
+ }
43
+
44
+ export { RedisAdapter, type RedisAdapterOptions, type RedisClientLike };
@@ -0,0 +1,44 @@
1
+ import { S as StorageAdapter } from '../types-Ca_Pa4mZ.js';
2
+
3
+ /**
4
+ * Structural interface compatible with `ioredis` and `node-redis` v4+ clients.
5
+ */
6
+ interface RedisClientLike {
7
+ get(key: string): Promise<string | null>;
8
+ set(key: string, value: string, ...args: unknown[]): Promise<unknown>;
9
+ eval(script: string, ...args: unknown[]): Promise<unknown>;
10
+ incrbyfloat?(key: string, increment: number | string): Promise<string | number>;
11
+ incrByFloat?(key: string, increment: number): Promise<string>;
12
+ pexpire?(key: string, milliseconds: number): Promise<number | boolean>;
13
+ pExpire?(key: string, milliseconds: number): Promise<number | boolean>;
14
+ defineCommand?: unknown;
15
+ isOpen?: boolean;
16
+ }
17
+ /**
18
+ * Configuration options for `RedisAdapter`.
19
+ */
20
+ interface RedisAdapterOptions {
21
+ /** Connected `ioredis` or `node-redis` client instance. */
22
+ client: RedisClientLike;
23
+ /**
24
+ * Explicitly specify the client dialect (`"ioredis"` or `"node-redis"`).
25
+ * Auto-detected if omitted.
26
+ */
27
+ clientType?: 'ioredis' | 'node-redis';
28
+ }
29
+ /**
30
+ * Standard Redis `StorageAdapter` supporting `ioredis` and `node-redis` clients.
31
+ *
32
+ * Uses atomic Lua scripts via `eval` to prevent race conditions under high concurrency.
33
+ */
34
+ declare class RedisAdapter implements StorageAdapter {
35
+ private readonly client;
36
+ private readonly isNodeRedis;
37
+ constructor(options: RedisAdapterOptions | RedisClientLike);
38
+ get<T>(key: string): Promise<T | null>;
39
+ set<T>(key: string, value: T, ttlMs: number): Promise<void>;
40
+ increment(key: string, amount: number, ttlMs: number): Promise<number>;
41
+ eval(script: string, keys: string[], args: (string | number)[]): Promise<unknown>;
42
+ }
43
+
44
+ export { RedisAdapter, type RedisAdapterOptions, type RedisClientLike };
@@ -0,0 +1,74 @@
1
+ // src/adapters/redis.ts
2
+ var RedisAdapter = class {
3
+ client;
4
+ isNodeRedis;
5
+ constructor(options) {
6
+ if ("client" in options) {
7
+ this.client = options.client;
8
+ this.isNodeRedis = options.clientType !== void 0 ? options.clientType === "node-redis" : detectNodeRedis(options.client);
9
+ } else {
10
+ this.client = options;
11
+ this.isNodeRedis = detectNodeRedis(options);
12
+ }
13
+ }
14
+ async get(key) {
15
+ const raw = await this.client.get(key);
16
+ if (raw === null || raw === void 0) {
17
+ return null;
18
+ }
19
+ try {
20
+ return JSON.parse(raw);
21
+ } catch {
22
+ return raw;
23
+ }
24
+ }
25
+ async set(key, value, ttlMs) {
26
+ const ttl = Math.max(1, Math.ceil(ttlMs));
27
+ const serialized = JSON.stringify(value);
28
+ if (this.isNodeRedis) {
29
+ await this.client.set(key, serialized, { PX: ttl });
30
+ } else {
31
+ await this.client.set(key, serialized, "PX", ttl);
32
+ }
33
+ }
34
+ async increment(key, amount, ttlMs) {
35
+ const ttl = Math.max(1, Math.ceil(ttlMs));
36
+ if (this.isNodeRedis && this.client.incrByFloat) {
37
+ const next2 = Number(await this.client.incrByFloat(key, amount));
38
+ if (this.client.pExpire) {
39
+ await this.client.pExpire(key, ttl);
40
+ }
41
+ return next2;
42
+ }
43
+ if (this.client.incrbyfloat) {
44
+ const next2 = Number(await this.client.incrbyfloat(key, amount));
45
+ if (this.client.pexpire) {
46
+ await this.client.pexpire(key, ttl);
47
+ }
48
+ return next2;
49
+ }
50
+ const current = await this.get(key) ?? 0;
51
+ const next = current + amount;
52
+ await this.set(key, next, ttl);
53
+ return next;
54
+ }
55
+ async eval(script, keys, args) {
56
+ if (this.isNodeRedis) {
57
+ return this.client.eval(script, {
58
+ keys,
59
+ arguments: args.map(String)
60
+ });
61
+ }
62
+ return this.client.eval(script, keys.length, ...keys, ...args);
63
+ }
64
+ };
65
+ function detectNodeRedis(client) {
66
+ if (typeof client.defineCommand === "function") {
67
+ return false;
68
+ }
69
+ return "isOpen" in client || typeof client.incrByFloat === "function";
70
+ }
71
+
72
+ export { RedisAdapter };
73
+ //# sourceMappingURL=redis.js.map
74
+ //# sourceMappingURL=redis.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/adapters/redis.ts"],"names":["next"],"mappings":";AAmCO,IAAM,eAAN,MAA6C;AAAA,EACjC,MAAA;AAAA,EACA,WAAA;AAAA,EAEjB,YAAY,OAAA,EAAgD;AAC1D,IAAA,IAAI,YAAY,OAAA,EAAS;AACvB,MAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AACtB,MAAA,IAAA,CAAK,WAAA,GACH,QAAQ,UAAA,KAAe,MAAA,GACnB,QAAQ,UAAA,KAAe,YAAA,GACvB,eAAA,CAAgB,OAAA,CAAQ,MAAM,CAAA;AAAA,IACtC,CAAA,MAAO;AACL,MAAA,IAAA,CAAK,MAAA,GAAS,OAAA;AACd,MAAA,IAAA,CAAK,WAAA,GAAc,gBAAgB,OAAO,CAAA;AAAA,IAC5C;AAAA,EACF;AAAA,EAEA,MAAM,IAAO,GAAA,EAAgC;AAC3C,IAAA,MAAM,GAAA,GAAM,MAAM,IAAA,CAAK,MAAA,CAAO,IAAI,GAAG,CAAA;AACrC,IAAA,IAAI,GAAA,KAAQ,IAAA,IAAQ,GAAA,KAAQ,MAAA,EAAW;AACrC,MAAA,OAAO,IAAA;AAAA,IACT;AAEA,IAAA,IAAI;AACF,MAAA,OAAO,IAAA,CAAK,MAAM,GAAG,CAAA;AAAA,IACvB,CAAA,CAAA,MAAQ;AACN,MAAA,OAAO,GAAA;AAAA,IACT;AAAA,EACF;AAAA,EAEA,MAAM,GAAA,CAAO,GAAA,EAAa,KAAA,EAAU,KAAA,EAA8B;AAChE,IAAA,MAAM,MAAM,IAAA,CAAK,GAAA,CAAI,GAAG,IAAA,CAAK,IAAA,CAAK,KAAK,CAAC,CAAA;AACx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CAAK,GAAA,CAAY,GAAG,CAAA,IAAM,CAAA;AACjD,IAAA,MAAM,OAAO,OAAA,GAAU,MAAA;AACvB,IAAA,MAAM,IAAA,CAAK,GAAA,CAAI,GAAA,EAAK,IAAA,EAAM,GAAG,CAAA;AAC7B,IAAA,OAAO,IAAA;AAAA,EACT;AAAA,EAEA,MAAM,IAAA,CAAK,MAAA,EAAgB,IAAA,EAAgB,IAAA,EAA6C;AACtF,IAAA,IAAI,KAAK,WAAA,EAAa;AACpB,MAAA,OAAO,IAAA,CAAK,MAAA,CAAO,IAAA,CAAK,MAAA,EAAQ;AAAA,QAC9B,IAAA;AAAA,QACA,SAAA,EAAW,IAAA,CAAK,GAAA,CAAI,MAAM;AAAA,OAC3B,CAAA;AAAA,IACH;AAEA,IAAA,OAAO,IAAA,CAAK,OAAO,IAAA,CAAK,MAAA,EAAQ,KAAK,MAAA,EAAQ,GAAG,IAAA,EAAM,GAAG,IAAI,CAAA;AAAA,EAC/D;AACF;AAEA,SAAS,gBAAgB,MAAA,EAAkC;AACzD,EAAA,IAAI,OAAO,MAAA,CAAO,aAAA,KAAkB,UAAA,EAAY;AAC9C,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,OAAO,QAAA,IAAY,MAAA,IAAU,OAAO,MAAA,CAAO,WAAA,KAAgB,UAAA;AAC7D","file":"redis.js","sourcesContent":["import type { StorageAdapter } from '../types.js';\n\n/**\n * Structural interface compatible with `ioredis` and `node-redis` v4+ clients.\n */\nexport interface RedisClientLike {\n get(key: string): Promise<string | null>;\n set(key: string, value: string, ...args: unknown[]): Promise<unknown>;\n eval(script: string, ...args: unknown[]): Promise<unknown>;\n incrbyfloat?(key: string, increment: number | string): Promise<string | number>;\n incrByFloat?(key: string, increment: number): Promise<string>;\n pexpire?(key: string, milliseconds: number): Promise<number | boolean>;\n pExpire?(key: string, milliseconds: number): Promise<number | boolean>;\n defineCommand?: unknown;\n isOpen?: boolean;\n}\n\n/**\n * Configuration options for `RedisAdapter`.\n */\nexport interface RedisAdapterOptions {\n /** Connected `ioredis` or `node-redis` client instance. */\n client: RedisClientLike;\n /**\n * Explicitly specify the client dialect (`\"ioredis\"` or `\"node-redis\"`).\n * Auto-detected if omitted.\n */\n clientType?: 'ioredis' | 'node-redis';\n}\n\n/**\n * Standard Redis `StorageAdapter` supporting `ioredis` and `node-redis` clients.\n *\n * Uses atomic Lua scripts via `eval` to prevent race conditions under high concurrency.\n */\nexport class RedisAdapter implements StorageAdapter {\n private readonly client: RedisClientLike;\n private readonly isNodeRedis: boolean;\n\n constructor(options: RedisAdapterOptions | RedisClientLike) {\n if ('client' in options) {\n this.client = options.client;\n this.isNodeRedis =\n options.clientType !== undefined\n ? options.clientType === 'node-redis'\n : detectNodeRedis(options.client);\n } else {\n this.client = options;\n this.isNodeRedis = detectNodeRedis(options);\n }\n }\n\n async get<T>(key: string): Promise<T | null> {\n const raw = await this.client.get(key);\n if (raw === null || raw === undefined) {\n return null;\n }\n\n try {\n return JSON.parse(raw) as T;\n } catch {\n return raw as unknown as T;\n }\n }\n\n async set<T>(key: string, value: T, ttlMs: number): Promise<void> {\n const ttl = Math.max(1, Math.ceil(ttlMs));\n const serialized = JSON.stringify(value);\n\n if (this.isNodeRedis) {\n await this.client.set(key, serialized, { PX: ttl });\n } else {\n await this.client.set(key, serialized, 'PX', ttl);\n }\n }\n\n async increment(key: string, amount: number, ttlMs: number): Promise<number> {\n const ttl = Math.max(1, Math.ceil(ttlMs));\n\n if (this.isNodeRedis && this.client.incrByFloat) {\n const next = Number(await this.client.incrByFloat(key, amount));\n if (this.client.pExpire) {\n await this.client.pExpire(key, ttl);\n }\n return next;\n }\n\n if (this.client.incrbyfloat) {\n const next = Number(await this.client.incrbyfloat(key, amount));\n if (this.client.pexpire) {\n await this.client.pexpire(key, ttl);\n }\n return next;\n }\n\n const current = (await this.get<number>(key)) ?? 0;\n const next = current + amount;\n await this.set(key, next, ttl);\n return next;\n }\n\n async eval(script: string, keys: string[], args: (string | number)[]): Promise<unknown> {\n if (this.isNodeRedis) {\n return this.client.eval(script, {\n keys,\n arguments: args.map(String),\n });\n }\n\n return this.client.eval(script, keys.length, ...keys, ...args);\n }\n}\n\nfunction detectNodeRedis(client: RedisClientLike): boolean {\n if (typeof client.defineCommand === 'function') {\n return false;\n }\n return 'isOpen' in client || typeof client.incrByFloat === 'function';\n}\n"]}
@@ -0,0 +1,87 @@
1
+ 'use strict';
2
+
3
+ // src/adapters/upstash.ts
4
+ var UpstashAdapter = class {
5
+ url;
6
+ token;
7
+ fetchImpl;
8
+ constructor(options) {
9
+ if (!options.url) {
10
+ throw new TypeError('UpstashAdapter requires a non-empty "url".');
11
+ }
12
+ if (!options.token) {
13
+ throw new TypeError('UpstashAdapter requires a non-empty "token".');
14
+ }
15
+ this.url = options.url.replace(/\/+$/, "");
16
+ this.token = options.token;
17
+ this.fetchImpl = options.fetch ?? globalThis.fetch;
18
+ }
19
+ async command(args) {
20
+ const response = await this.fetchImpl(this.url, {
21
+ method: "POST",
22
+ headers: {
23
+ Authorization: `Bearer ${this.token}`,
24
+ "Content-Type": "application/json"
25
+ },
26
+ body: JSON.stringify(args)
27
+ });
28
+ if (!response.ok) {
29
+ throw new Error(`Upstash REST request failed with HTTP ${response.status} ${response.statusText}`);
30
+ }
31
+ const data = await response.json();
32
+ if (data.error) {
33
+ throw new Error(`Upstash Redis error: ${data.error}`);
34
+ }
35
+ return data.result;
36
+ }
37
+ async pipeline(commands) {
38
+ const response = await this.fetchImpl(`${this.url}/pipeline`, {
39
+ method: "POST",
40
+ headers: {
41
+ Authorization: `Bearer ${this.token}`,
42
+ "Content-Type": "application/json"
43
+ },
44
+ body: JSON.stringify(commands)
45
+ });
46
+ if (!response.ok) {
47
+ throw new Error(`Upstash pipeline request failed with HTTP ${response.status} ${response.statusText}`);
48
+ }
49
+ const results = await response.json();
50
+ return results.map((entry) => {
51
+ if (entry.error) {
52
+ throw new Error(`Upstash Redis pipeline error: ${entry.error}`);
53
+ }
54
+ return entry.result;
55
+ });
56
+ }
57
+ async get(key) {
58
+ const raw = await this.command(["GET", key]);
59
+ if (raw === null || raw === void 0) {
60
+ return null;
61
+ }
62
+ try {
63
+ return JSON.parse(raw);
64
+ } catch {
65
+ return raw;
66
+ }
67
+ }
68
+ async set(key, value, ttlMs) {
69
+ const ttl = Math.max(1, Math.ceil(ttlMs));
70
+ await this.command(["SET", key, JSON.stringify(value), "PX", ttl]);
71
+ }
72
+ async increment(key, amount, ttlMs) {
73
+ const ttl = Math.max(1, Math.ceil(ttlMs));
74
+ const [next] = await this.pipeline([
75
+ ["INCRBYFLOAT", key, amount],
76
+ ["PEXPIRE", key, ttl, "NX"]
77
+ ]);
78
+ return Number(next);
79
+ }
80
+ async eval(script, keys, args) {
81
+ return this.command(["EVAL", script, keys.length, ...keys, ...args]);
82
+ }
83
+ };
84
+
85
+ exports.UpstashAdapter = UpstashAdapter;
86
+ //# sourceMappingURL=upstash.cjs.map
87
+ //# sourceMappingURL=upstash.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/adapters/upstash.ts"],"names":[],"mappings":";;;AA2BO,IAAM,iBAAN,MAA+C;AAAA,EACnC,GAAA;AAAA,EACA,KAAA;AAAA,EACA,SAAA;AAAA,EAEjB,YAAY,OAAA,EAAgC;AAC1C,IAAA,IAAI,CAAC,QAAQ,GAAA,EAAK;AAChB,MAAA,MAAM,IAAI,UAAU,4CAA4C,CAAA;AAAA,IAClE;AACA,IAAA,IAAI,CAAC,QAAQ,KAAA,EAAO;AAClB,MAAA,MAAM,IAAI,UAAU,8CAA8C,CAAA;AAAA,IACpE;AAEA,IAAA,IAAA,CAAK,GAAA,GAAM,OAAA,CAAQ,GAAA,CAAI,OAAA,CAAQ,QAAQ,EAAE,CAAA;AACzC,IAAA,IAAA,CAAK,QAAQ,OAAA,CAAQ,KAAA;AACrB,IAAA,IAAA,CAAK,SAAA,GAAY,OAAA,CAAQ,KAAA,IAAS,UAAA,CAAW,KAAA;AAAA,EAC/C;AAAA,EA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AG,IAAA,CAAK,IAAA,CAAK,KAAK,CAAC,CAAA;AACxC,IAAA,MAAM,CAAC,IAAI,CAAA,GAAI,MAAM,KAAK,QAAA,CAAS;AAAA,MACjC,CAAC,aAAA,EAAe,GAAA,EAAK,MAAM,CAAA;AAAA,MAC3B,CAAC,SAAA,EAAW,GAAA,EAAK,GAAA,EAAK,IAAI;AAAA,KAC3B,CAAA;AACD,IAAA,OAAO,OAAO,IAAI,CAAA;AAAA,EACpB;AAAA,EAEA,MAAM,IAAA,CAAK,MAAA,EAAgB,IAAA,EAAgB,IAAA,EAA6C;AACtF,IAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,CAAC,MAAA,EAAQ,MAAA,EAAQ,IAAA,CAAK,MAAA,EAAQ,GAAG,IAAA,EAAM,GAAG,IAAI,CAAC,CAAA;AAAA,EACrE;AACF","file":"upstash.cjs","sourcesContent":["import type { StorageAdapter } from '../types.js';\n\n/**\n * Configuration options for `UpstashAdapter`.\n */\nexport interface UpstashAdapterOptions {\n /** Upstash Redis REST URL (e.g., `\"https://us1-example.upstash.io\"`). */\n url: string;\n /** Upstash Redis REST bearer token. */\n token: string;\n /**\n * Optional custom `fetch` implementation. Defaults to `globalThis.fetch`.\n */\n fetch?: typeof fetch;\n}\n\ninterface UpstashResponse<T = unknown> {\n result?: T;\n error?: string;\n}\n\n/**\n * Zero-dependency Upstash Redis REST `StorageAdapter` using standard `fetch`.\n *\n * Executes atomic Lua scripts via Upstash's REST API, making it safe for\n * Cloudflare Workers, Vercel Edge Runtime, Deno, Bun, and Node.js.\n */\nexport class UpstashAdapter implements StorageAdapter {\n private readonly url: string;\n private readonly token: string;\n private readonly fetchImpl: typeof fetch;\n\n constructor(options: UpstashAdapterOptions) {\n if (!options.url) {\n throw new TypeError('UpstashAdapter requires a non-empty \"url\".');\n }\n if (!options.token) {\n throw new TypeError('UpstashAdapter requires a non-empty \"token\".');\n }\n\n this.url = options.url.replace(/\\/+$/, '');\n this.token = options.token;\n this.fetchImpl = options.fetch ?? globalThis.fetch;\n }\n\n private async command<T = unknown>(args: (string | number)[]): Promise<T> {\n const response = await this.fetchImpl(this.url, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${this.token}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(args),\n });\n\n if (!response.ok) {\n throw new Error(`Upstash REST request failed with HTTP ${response.status} ${response.statusText}`);\n }\n\n const data = (await response.json()) as UpstashResponse<T>;\n if (data.error) {\n throw new Error(`Upstash Redis error: ${data.error}`);\n }\n\n return data.result as T;\n }\n\n private async pipeline(commands: (string | number)[][]): Promise<unknown[]> {\n const response = await this.fetchImpl(`${this.url}/pipeline`, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${this.token}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(commands),\n });\n\n if (!response.ok) {\n throw new Error(`Upstash pipeline request failed with HTTP ${response.status} ${response.statusText}`);\n }\n\n const results = (await response.json()) as UpstashResponse[];\n return results.map((entry) => {\n if (entry.error) {\n throw new Error(`Upstash Redis pipeline error: ${entry.error}`);\n }\n return entry.result;\n });\n }\n\n async get<T>(key: string): Promise<T | null> {\n const raw = await this.command<string | null>(['GET', key]);\n if (raw === null || raw === undefined) {\n return null;\n }\n\n try {\n return JSON.parse(raw) as T;\n } catch {\n return raw as unknown as T;\n }\n }\n\n async set<T>(key: string, value: T, ttlMs: number): Promise<void> {\n const ttl = Math.max(1, Math.ceil(ttlMs));\n await this.command(['SET', key, JSON.stringify(value), 'PX', ttl]);\n }\n\n async increment(key: string, amount: number, ttlMs: number): Promise<number> {\n const ttl = Math.max(1, Math.ceil(ttlMs));\n const [next] = await this.pipeline([\n ['INCRBYFLOAT', key, amount],\n ['PEXPIRE', key, ttl, 'NX'],\n ]);\n return Number(next);\n }\n\n async eval(script: string, keys: string[], args: (string | number)[]): Promise<unknown> {\n return this.command(['EVAL', script, keys.length, ...keys, ...args]);\n }\n}\n"]}
@@ -0,0 +1,35 @@
1
+ import { S as StorageAdapter } from '../types-Ca_Pa4mZ.cjs';
2
+
3
+ /**
4
+ * Configuration options for `UpstashAdapter`.
5
+ */
6
+ interface UpstashAdapterOptions {
7
+ /** Upstash Redis REST URL (e.g., `"https://us1-example.upstash.io"`). */
8
+ url: string;
9
+ /** Upstash Redis REST bearer token. */
10
+ token: string;
11
+ /**
12
+ * Optional custom `fetch` implementation. Defaults to `globalThis.fetch`.
13
+ */
14
+ fetch?: typeof fetch;
15
+ }
16
+ /**
17
+ * Zero-dependency Upstash Redis REST `StorageAdapter` using standard `fetch`.
18
+ *
19
+ * Executes atomic Lua scripts via Upstash's REST API, making it safe for
20
+ * Cloudflare Workers, Vercel Edge Runtime, Deno, Bun, and Node.js.
21
+ */
22
+ declare class UpstashAdapter implements StorageAdapter {
23
+ private readonly url;
24
+ private readonly token;
25
+ private readonly fetchImpl;
26
+ constructor(options: UpstashAdapterOptions);
27
+ private command;
28
+ private pipeline;
29
+ get<T>(key: string): Promise<T | null>;
30
+ set<T>(key: string, value: T, ttlMs: number): Promise<void>;
31
+ increment(key: string, amount: number, ttlMs: number): Promise<number>;
32
+ eval(script: string, keys: string[], args: (string | number)[]): Promise<unknown>;
33
+ }
34
+
35
+ export { UpstashAdapter, type UpstashAdapterOptions };
@@ -0,0 +1,35 @@
1
+ import { S as StorageAdapter } from '../types-Ca_Pa4mZ.js';
2
+
3
+ /**
4
+ * Configuration options for `UpstashAdapter`.
5
+ */
6
+ interface UpstashAdapterOptions {
7
+ /** Upstash Redis REST URL (e.g., `"https://us1-example.upstash.io"`). */
8
+ url: string;
9
+ /** Upstash Redis REST bearer token. */
10
+ token: string;
11
+ /**
12
+ * Optional custom `fetch` implementation. Defaults to `globalThis.fetch`.
13
+ */
14
+ fetch?: typeof fetch;
15
+ }
16
+ /**
17
+ * Zero-dependency Upstash Redis REST `StorageAdapter` using standard `fetch`.
18
+ *
19
+ * Executes atomic Lua scripts via Upstash's REST API, making it safe for
20
+ * Cloudflare Workers, Vercel Edge Runtime, Deno, Bun, and Node.js.
21
+ */
22
+ declare class UpstashAdapter implements StorageAdapter {
23
+ private readonly url;
24
+ private readonly token;
25
+ private readonly fetchImpl;
26
+ constructor(options: UpstashAdapterOptions);
27
+ private command;
28
+ private pipeline;
29
+ get<T>(key: string): Promise<T | null>;
30
+ set<T>(key: string, value: T, ttlMs: number): Promise<void>;
31
+ increment(key: string, amount: number, ttlMs: number): Promise<number>;
32
+ eval(script: string, keys: string[], args: (string | number)[]): Promise<unknown>;
33
+ }
34
+
35
+ export { UpstashAdapter, type UpstashAdapterOptions };
@@ -0,0 +1,85 @@
1
+ // src/adapters/upstash.ts
2
+ var UpstashAdapter = class {
3
+ url;
4
+ token;
5
+ fetchImpl;
6
+ constructor(options) {
7
+ if (!options.url) {
8
+ throw new TypeError('UpstashAdapter requires a non-empty "url".');
9
+ }
10
+ if (!options.token) {
11
+ throw new TypeError('UpstashAdapter requires a non-empty "token".');
12
+ }
13
+ this.url = options.url.replace(/\/+$/, "");
14
+ this.token = options.token;
15
+ this.fetchImpl = options.fetch ?? globalThis.fetch;
16
+ }
17
+ async command(args) {
18
+ const response = await this.fetchImpl(this.url, {
19
+ method: "POST",
20
+ headers: {
21
+ Authorization: `Bearer ${this.token}`,
22
+ "Content-Type": "application/json"
23
+ },
24
+ body: JSON.stringify(args)
25
+ });
26
+ if (!response.ok) {
27
+ throw new Error(`Upstash REST request failed with HTTP ${response.status} ${response.statusText}`);
28
+ }
29
+ const data = await response.json();
30
+ if (data.error) {
31
+ throw new Error(`Upstash Redis error: ${data.error}`);
32
+ }
33
+ return data.result;
34
+ }
35
+ async pipeline(commands) {
36
+ const response = await this.fetchImpl(`${this.url}/pipeline`, {
37
+ method: "POST",
38
+ headers: {
39
+ Authorization: `Bearer ${this.token}`,
40
+ "Content-Type": "application/json"
41
+ },
42
+ body: JSON.stringify(commands)
43
+ });
44
+ if (!response.ok) {
45
+ throw new Error(`Upstash pipeline request failed with HTTP ${response.status} ${response.statusText}`);
46
+ }
47
+ const results = await response.json();
48
+ return results.map((entry) => {
49
+ if (entry.error) {
50
+ throw new Error(`Upstash Redis pipeline error: ${entry.error}`);
51
+ }
52
+ return entry.result;
53
+ });
54
+ }
55
+ async get(key) {
56
+ const raw = await this.command(["GET", key]);
57
+ if (raw === null || raw === void 0) {
58
+ return null;
59
+ }
60
+ try {
61
+ return JSON.parse(raw);
62
+ } catch {
63
+ return raw;
64
+ }
65
+ }
66
+ async set(key, value, ttlMs) {
67
+ const ttl = Math.max(1, Math.ceil(ttlMs));
68
+ await this.command(["SET", key, JSON.stringify(value), "PX", ttl]);
69
+ }
70
+ async increment(key, amount, ttlMs) {
71
+ const ttl = Math.max(1, Math.ceil(ttlMs));
72
+ const [next] = await this.pipeline([
73
+ ["INCRBYFLOAT", key, amount],
74
+ ["PEXPIRE", key, ttl, "NX"]
75
+ ]);
76
+ return Number(next);
77
+ }
78
+ async eval(script, keys, args) {
79
+ return this.command(["EVAL", script, keys.length, ...keys, ...args]);
80
+ }
81
+ };
82
+
83
+ export { UpstashAdapter };
84
+ //# sourceMappingURL=upstash.js.map
85
+ //# sourceMappingURL=upstash.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/adapters/upstash.ts"],"names":[],"mappings":";AA2BO,IAAM,iBAAN,MAA+C;AAAA,EACnC,GAAA;AAAA,EACA,KAAA;AAAA,EACA,SAAA;AAAA,EAEjB,YAAY,OAAA,EAAgC;AAC1C,IAAA,IAAI,CAAC,QAAQ,GAAA,EAAK;AAChB,MAAA,MAAM,IAAI,UAAU,4CAA4C,CAAA;AAAA,IAClE;AACA,IAAA,IAAI,CAAC,QAAQ,KAAA,EAAO;AAClB,MAAA,MAAM,IAAI,UAAU,8CAA8C,CAAA;AAAA,IACpE;AAEA,IAAA,IAAA,CAAK,GAAA,GAAM,OAAA,CAAQ,GAAA,CAAI,OAAA,CAAQ,QAAQ,EAAE,CAAA;AACzC,IAAA,IAAA,CAAK,QAAQ,OAAA,CAAQ,KAAA;AACrB,IAAA,IAAA,CAAK,SAA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,EAAgB,KAAA,EAAgC;AAC3E,IAAA,MAAM,MAAM,IAAA,CAAK,GAAA,CAAI,GAAG,IAAA,CAAK,IAAA,CAAK,KAAK,CAAC,CAAA;AACxC,IAAA,MAAM,CAAC,IAAI,CAAA,GAAI,MAAM,KAAK,QAAA,CAAS;AAAA,MACjC,CAAC,aAAA,EAAe,GAAA,EAAK,MAAM,CAAA;AAAA,MAC3B,CAAC,SAAA,EAAW,GAAA,EAAK,GAAA,EAAK,IAAI;AAAA,KAC3B,CAAA;AACD,IAAA,OAAO,OAAO,IAAI,CAAA;AAAA,EACpB;AAAA,EAEA,MAAM,IAAA,CAAK,MAAA,EAAgB,IAAA,EAAgB,IAAA,EAA6C;AACtF,IAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,CAAC,MAAA,EAAQ,MAAA,EAAQ,IAAA,CAAK,MAAA,EAAQ,GAAG,IAAA,EAAM,GAAG,IAAI,CAAC,CAAA;AAAA,EACrE;AACF","file":"upstash.js","sourcesContent":["import type { StorageAdapter } from '../types.js';\n\n/**\n * Configuration options for `UpstashAdapter`.\n */\nexport interface UpstashAdapterOptions {\n /** Upstash Redis REST URL (e.g., `\"https://us1-example.upstash.io\"`). */\n url: string;\n /** Upstash Redis REST bearer token. */\n token: string;\n /**\n * Optional custom `fetch` implementation. Defaults to `globalThis.fetch`.\n */\n fetch?: typeof fetch;\n}\n\ninterface UpstashResponse<T = unknown> {\n result?: T;\n error?: string;\n}\n\n/**\n * Zero-dependency Upstash Redis REST `StorageAdapter` using standard `fetch`.\n *\n * Executes atomic Lua scripts via Upstash's REST API, making it safe for\n * Cloudflare Workers, Vercel Edge Runtime, Deno, Bun, and Node.js.\n */\nexport class UpstashAdapter implements StorageAdapter {\n private readonly url: string;\n private readonly token: string;\n private readonly fetchImpl: typeof fetch;\n\n constructor(options: UpstashAdapterOptions) {\n if (!options.url) {\n throw new TypeError('UpstashAdapter requires a non-empty \"url\".');\n }\n if (!options.token) {\n throw new TypeError('UpstashAdapter requires a non-empty \"token\".');\n }\n\n this.url = options.url.replace(/\\/+$/, '');\n this.token = options.token;\n this.fetchImpl = options.fetch ?? globalThis.fetch;\n }\n\n private async command<T = unknown>(args: (string | number)[]): Promise<T> {\n const response = await this.fetchImpl(this.url, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${this.token}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(args),\n });\n\n if (!response.ok) {\n throw new Error(`Upstash REST request failed with HTTP ${response.status} ${response.statusText}`);\n }\n\n const data = (await response.json()) as UpstashResponse<T>;\n if (data.error) {\n throw new Error(`Upstash Redis error: ${data.error}`);\n }\n\n return data.result as T;\n }\n\n private async pipeline(commands: (string | number)[][]): Promise<unknown[]> {\n const response = await this.fetchImpl(`${this.url}/pipeline`, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${this.token}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(commands),\n });\n\n if (!response.ok) {\n throw new Error(`Upstash pipeline request failed with HTTP ${response.status} ${response.statusText}`);\n }\n\n const results = (await response.json()) as UpstashResponse[];\n return results.map((entry) => {\n if (entry.error) {\n throw new Error(`Upstash Redis pipeline error: ${entry.error}`);\n }\n return entry.result;\n });\n }\n\n async get<T>(key: string): Promise<T | null> {\n const raw = await this.command<string | null>(['GET', key]);\n if (raw === null || raw === undefined) {\n return null;\n }\n\n try {\n return JSON.parse(raw) as T;\n } catch {\n return raw as unknown as T;\n }\n }\n\n async set<T>(key: string, value: T, ttlMs: number): Promise<void> {\n const ttl = Math.max(1, Math.ceil(ttlMs));\n await this.command(['SET', key, JSON.stringify(value), 'PX', ttl]);\n }\n\n async increment(key: string, amount: number, ttlMs: number): Promise<number> {\n const ttl = Math.max(1, Math.ceil(ttlMs));\n const [next] = await this.pipeline([\n ['INCRBYFLOAT', key, amount],\n ['PEXPIRE', key, ttl, 'NX'],\n ]);\n return Number(next);\n }\n\n async eval(script: string, keys: string[], args: (string | number)[]): Promise<unknown> {\n return this.command(['EVAL', script, keys.length, ...keys, ...args]);\n }\n}\n"]}