@windrun-huaiin/backend-core 13.0.0 → 14.1.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.
- package/dist/_virtual/index.js +7 -3
- package/dist/_virtual/index.mjs +5 -3
- package/dist/_virtual/index2.js +2 -6
- package/dist/_virtual/index2.mjs +2 -6
- package/dist/index.js +3 -1
- package/dist/index.mjs +2 -2
- package/dist/lib/index.js +3 -1
- package/dist/lib/index.mjs +2 -2
- package/dist/lib/stripe-config.d.ts +1 -1
- package/dist/lib/stripe-config.d.ts.map +1 -1
- package/dist/lib/stripe-config.js +25 -16
- package/dist/lib/stripe-config.mjs +25 -16
- package/dist/lib/upstash/qstash.d.ts.map +1 -1
- package/dist/lib/upstash/qstash.js +66 -62
- package/dist/lib/upstash/qstash.mjs +67 -63
- package/dist/lib/upstash/redis-counter.d.ts.map +1 -1
- package/dist/lib/upstash/redis-counter.js +9 -24
- package/dist/lib/upstash/redis-counter.mjs +10 -25
- package/dist/lib/upstash/redis-favorite.d.ts.map +1 -1
- package/dist/lib/upstash/redis-favorite.js +22 -36
- package/dist/lib/upstash/redis-favorite.mjs +23 -37
- package/dist/lib/upstash/redis-like.d.ts.map +1 -1
- package/dist/lib/upstash/redis-like.js +22 -36
- package/dist/lib/upstash/redis-like.mjs +23 -37
- package/dist/lib/upstash/redis-lock.d.ts.map +1 -1
- package/dist/lib/upstash/redis-lock.js +22 -38
- package/dist/lib/upstash/redis-lock.mjs +23 -39
- package/dist/lib/upstash/redis-structures.d.ts.map +1 -1
- package/dist/lib/upstash/redis-structures.js +77 -113
- package/dist/lib/upstash/redis-structures.mjs +78 -114
- package/dist/lib/upstash-config.d.ts +9 -1
- package/dist/lib/upstash-config.d.ts.map +1 -1
- package/dist/lib/upstash-config.js +221 -27
- package/dist/lib/upstash-config.mjs +220 -28
- package/dist/node_modules/.pnpm/{@upstash_qstash@2.8.4/node_modules/@upstash/qstash/chunk-RQPZUJXG.js → @upstash_qstash@2.10.1/node_modules/@upstash/qstash/chunk-35B33QW3.js} +897 -468
- package/dist/node_modules/.pnpm/{@upstash_qstash@2.8.4/node_modules/@upstash/qstash/chunk-RQPZUJXG.mjs → @upstash_qstash@2.10.1/node_modules/@upstash/qstash/chunk-35B33QW3.mjs} +895 -468
- package/dist/node_modules/.pnpm/{@upstash_redis@1.36.1/node_modules/@upstash/redis/chunk-LLI2WIYN.js → @upstash_redis@1.37.0/node_modules/@upstash/redis/chunk-IH7W44G6.js} +657 -40
- package/dist/node_modules/.pnpm/{@upstash_redis@1.36.1/node_modules/@upstash/redis/chunk-LLI2WIYN.mjs → @upstash_redis@1.37.0/node_modules/@upstash/redis/chunk-IH7W44G6.mjs} +657 -41
- package/dist/node_modules/.pnpm/{@upstash_redis@1.36.1 → @upstash_redis@1.37.0}/node_modules/@upstash/redis/nodejs.js +6 -5
- package/dist/node_modules/.pnpm/{@upstash_redis@1.36.1 → @upstash_redis@1.37.0}/node_modules/@upstash/redis/nodejs.mjs +2 -2
- package/dist/node_modules/.pnpm/crypto-js@4.2.0/node_modules/crypto-js/index.js +1 -1
- package/dist/node_modules/.pnpm/crypto-js@4.2.0/node_modules/crypto-js/index.mjs +1 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/jws/flattened/verify.js +6 -6
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/jwt/verify.js +1 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/key/import.js +2 -2
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/epoch.js +3 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/is_disjoint.js +3 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/is_jwk.js +1 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/is_object.js +3 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/jwt_claims_set.js +7 -5
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/secs.js +3 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/validate_algorithms.js +3 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/validate_crit.js +3 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/check_key_length.js +3 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/dsa_digest.js +3 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/get_named_curve.js +4 -2
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/get_sign_verify_key.js +3 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/hmac_digest.js +3 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/is_key_like.js +1 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/is_key_object.js +3 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/jwk_to_key.js +3 -1
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/node_key.js +6 -4
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/sign.js +6 -4
- package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/verify.js +7 -5
- package/dist/services/stripe/webhook-handler.js +4 -2
- package/dist/services/stripe/webhook-handler.mjs +3 -1
- package/package.json +5 -5
- package/src/lib/stripe-config.ts +27 -15
- package/src/lib/upstash/qstash.ts +64 -62
- package/src/lib/upstash/redis-counter.ts +10 -26
- package/src/lib/upstash/redis-favorite.ts +23 -42
- package/src/lib/upstash/redis-like.ts +23 -42
- package/src/lib/upstash/redis-lock.ts +23 -49
- package/src/lib/upstash/redis-structures.ts +82 -131
- package/src/lib/upstash-config.ts +231 -24
- package/src/services/stripe/webhook-handler.ts +3 -1
- package/dist/_virtual/index3.js +0 -5
- package/dist/_virtual/index3.mjs +0 -3
- package/dist/node_modules/.pnpm/@upstash_lock@0.2.1_typescript@5.9.3/node_modules/@upstash/lock/dist/index.js +0 -191
- package/dist/node_modules/.pnpm/@upstash_lock@0.2.1_typescript@5.9.3/node_modules/@upstash/lock/dist/index.mjs +0 -189
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/errors.js +0 -54
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/errors.mjs +0 -51
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/iso.js +0 -44
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/iso.mjs +0 -35
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/parse.js +0 -31
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/parse.mjs +0 -18
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/schemas.js +0 -587
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/schemas.mjs +0 -527
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/api.js +0 -447
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/api.mjs +0 -399
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/checks.js +0 -245
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/checks.mjs +0 -232
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/core.js +0 -68
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/core.mjs +0 -62
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/doc.js +0 -39
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/doc.mjs +0 -37
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/errors.js +0 -80
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/errors.mjs +0 -75
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/parse.js +0 -101
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/parse.mjs +0 -86
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/regexes.js +0 -102
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/regexes.mjs +0 -76
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/registries.js +0 -56
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/registries.mjs +0 -52
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/schemas.js +0 -1205
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/schemas.mjs +0 -1157
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/util.js +0 -407
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/util.mjs +0 -374
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/versions.js +0 -9
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/versions.mjs +0 -7
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { __awaiter } from '../../node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.3/node_modules/tslib/tslib.es6.mjs';
|
|
2
|
-
import {
|
|
2
|
+
import { withRedis } from '../upstash-config.mjs';
|
|
3
3
|
|
|
4
4
|
const likeTargetKey = (targetId) => `like:target:${targetId}`;
|
|
5
5
|
const likeUserKey = (userId) => `like:user:${userId}`;
|
|
@@ -7,62 +7,48 @@ const likeUserKey = (userId) => `like:user:${userId}`;
|
|
|
7
7
|
* Like a target. Returns true if the like was added, false if it already existed, null if Redis is unavailable.
|
|
8
8
|
*/
|
|
9
9
|
const likeTarget = (targetId, userId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
return false;
|
|
10
|
+
return withRedis((redis) => __awaiter(void 0, void 0, void 0, function* () {
|
|
11
|
+
const added = yield redis.sadd(likeTargetKey(targetId), userId);
|
|
12
|
+
if (added === 1) {
|
|
13
|
+
yield redis.sadd(likeUserKey(userId), targetId);
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
}));
|
|
20
18
|
});
|
|
21
19
|
/**
|
|
22
20
|
* Unlike a target. Returns true if removed, false if it didn't exist, null if Redis is unavailable.
|
|
23
21
|
*/
|
|
24
22
|
const unlikeTarget = (targetId, userId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
return false;
|
|
23
|
+
return withRedis((redis) => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
+
const removed = yield redis.srem(likeTargetKey(targetId), userId);
|
|
25
|
+
if (removed === 1) {
|
|
26
|
+
yield redis.srem(likeUserKey(userId), targetId);
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
}));
|
|
35
31
|
});
|
|
36
32
|
/**
|
|
37
33
|
* Check whether a user liked a target. Returns null if Redis is unavailable.
|
|
38
34
|
*/
|
|
39
35
|
const isTargetLiked = (targetId, userId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return
|
|
43
|
-
}
|
|
44
|
-
const result = yield redis.sismember(likeTargetKey(targetId), userId);
|
|
45
|
-
return result === 1;
|
|
36
|
+
return withRedis((redis) => __awaiter(void 0, void 0, void 0, function* () {
|
|
37
|
+
const result = yield redis.sismember(likeTargetKey(targetId), userId);
|
|
38
|
+
return result === 1;
|
|
39
|
+
}));
|
|
46
40
|
});
|
|
47
41
|
/**
|
|
48
42
|
* Get like count for a target (unique by user). Returns null if Redis is unavailable.
|
|
49
43
|
*/
|
|
50
44
|
const getTargetLikeCount = (targetId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
51
|
-
|
|
52
|
-
if (!redis) {
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
return redis.scard(likeTargetKey(targetId));
|
|
45
|
+
return withRedis((redis) => redis.scard(likeTargetKey(targetId)));
|
|
56
46
|
});
|
|
57
47
|
/**
|
|
58
48
|
* Get target ids liked by a user. Returns null if Redis is unavailable.
|
|
59
49
|
*/
|
|
60
50
|
const getUserLikedTargets = (userId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
|
-
|
|
62
|
-
if (!redis) {
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
return redis.smembers(likeUserKey(userId));
|
|
51
|
+
return withRedis((redis) => redis.smembers(likeUserKey(userId)));
|
|
66
52
|
});
|
|
67
53
|
|
|
68
54
|
export { getTargetLikeCount, getUserLikedTargets, isTargetLiked, likeTarget, unlikeTarget };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis-lock.d.ts","sourceRoot":"","sources":["../../../src/lib/upstash/redis-lock.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"redis-lock.d.ts","sourceRoot":"","sources":["../../../src/lib/upstash/redis-lock.ts"],"names":[],"mappings":"AAkBA;;GAEG;AACH,eAAO,MAAM,WAAW,GAAU,KAAK,MAAM,EAAE,OAAO,MAAM,KAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAMnF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAU,KAAK,MAAM,EAAE,OAAO,MAAM,KAAG,OAAO,CAAC,OAAO,CAM7E,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,QAAQ,GAAU,CAAC,EAC9B,KAAK,MAAM,EACX,OAAO,MAAM,EACb,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,KACvB,OAAO,CAAC,CAAC,GAAG,IAAI,CAWlB,CAAC"}
|
|
@@ -1,64 +1,48 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var tslib_es6 = require('../../node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.3/node_modules/tslib/tslib.es6.js');
|
|
4
|
-
var index = require('../../_virtual/index.js');
|
|
5
4
|
var upstashConfig = require('../upstash-config.js');
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
const unlockScript = `
|
|
7
|
+
if redis.call("get", KEYS[1]) == ARGV[1] then
|
|
8
|
+
return redis.call("del", KEYS[1])
|
|
9
|
+
else
|
|
10
|
+
return 0
|
|
11
|
+
end
|
|
12
|
+
`;
|
|
13
|
+
const generateToken = () => {
|
|
11
14
|
try {
|
|
12
|
-
return
|
|
15
|
+
return crypto.randomUUID();
|
|
13
16
|
}
|
|
14
17
|
catch (_a) {
|
|
15
|
-
return
|
|
18
|
+
return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
16
19
|
}
|
|
17
20
|
};
|
|
18
|
-
const getLock = () => {
|
|
19
|
-
if (cachedLock) {
|
|
20
|
-
return cachedLock;
|
|
21
|
-
}
|
|
22
|
-
if (lockInitAttempted) {
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
lockInitAttempted = true;
|
|
26
|
-
const redis = upstashConfig.getRedis();
|
|
27
|
-
if (!redis) {
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
cachedLock = createLock(redis);
|
|
31
|
-
return cachedLock;
|
|
32
|
-
};
|
|
33
21
|
/**
|
|
34
22
|
* Acquire a distributed lock. Returns the lock token or null when unavailable.
|
|
35
23
|
*/
|
|
36
24
|
const acquireLock = (key, ttlMs) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
25
|
+
return upstashConfig.withRedis((redis) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
26
|
+
const token = generateToken();
|
|
27
|
+
const result = yield redis.set(key, token, { nx: true, px: ttlMs });
|
|
28
|
+
return result === 'OK' ? token : null;
|
|
29
|
+
}));
|
|
42
30
|
});
|
|
43
31
|
/**
|
|
44
32
|
* Release a distributed lock. Returns false when the lock client is unavailable.
|
|
45
33
|
*/
|
|
46
34
|
const releaseLock = (key, token) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
return
|
|
50
|
-
}
|
|
51
|
-
return
|
|
35
|
+
const result = yield upstashConfig.withRedis((redis) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
36
|
+
const released = yield redis.eval(unlockScript, [key], [token]);
|
|
37
|
+
return Number(released) === 1;
|
|
38
|
+
}));
|
|
39
|
+
return result !== null && result !== void 0 ? result : false;
|
|
52
40
|
});
|
|
53
41
|
/**
|
|
54
42
|
* Run a function under a distributed lock. Returns null when the lock is unavailable.
|
|
55
43
|
*/
|
|
56
44
|
const withLock = (key, ttlMs, fn) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
57
|
-
const
|
|
58
|
-
if (!lock) {
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
const token = yield lock.acquire(key, ttlMs);
|
|
45
|
+
const token = yield acquireLock(key, ttlMs);
|
|
62
46
|
if (!token) {
|
|
63
47
|
return null;
|
|
64
48
|
}
|
|
@@ -66,7 +50,7 @@ const withLock = (key, ttlMs, fn) => tslib_es6.__awaiter(void 0, void 0, void 0,
|
|
|
66
50
|
return yield fn();
|
|
67
51
|
}
|
|
68
52
|
finally {
|
|
69
|
-
yield
|
|
53
|
+
yield releaseLock(key, token);
|
|
70
54
|
}
|
|
71
55
|
});
|
|
72
56
|
|
|
@@ -1,62 +1,46 @@
|
|
|
1
1
|
import { __awaiter } from '../../node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.3/node_modules/tslib/tslib.es6.mjs';
|
|
2
|
-
import {
|
|
3
|
-
import { getRedis } from '../upstash-config.mjs';
|
|
2
|
+
import { withRedis } from '../upstash-config.mjs';
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
const unlockScript = `
|
|
5
|
+
if redis.call("get", KEYS[1]) == ARGV[1] then
|
|
6
|
+
return redis.call("del", KEYS[1])
|
|
7
|
+
else
|
|
8
|
+
return 0
|
|
9
|
+
end
|
|
10
|
+
`;
|
|
11
|
+
const generateToken = () => {
|
|
9
12
|
try {
|
|
10
|
-
return
|
|
13
|
+
return crypto.randomUUID();
|
|
11
14
|
}
|
|
12
15
|
catch (_a) {
|
|
13
|
-
return
|
|
16
|
+
return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
14
17
|
}
|
|
15
18
|
};
|
|
16
|
-
const getLock = () => {
|
|
17
|
-
if (cachedLock) {
|
|
18
|
-
return cachedLock;
|
|
19
|
-
}
|
|
20
|
-
if (lockInitAttempted) {
|
|
21
|
-
return null;
|
|
22
|
-
}
|
|
23
|
-
lockInitAttempted = true;
|
|
24
|
-
const redis = getRedis();
|
|
25
|
-
if (!redis) {
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
cachedLock = createLock(redis);
|
|
29
|
-
return cachedLock;
|
|
30
|
-
};
|
|
31
19
|
/**
|
|
32
20
|
* Acquire a distributed lock. Returns the lock token or null when unavailable.
|
|
33
21
|
*/
|
|
34
22
|
const acquireLock = (key, ttlMs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
23
|
+
return withRedis((redis) => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
+
const token = generateToken();
|
|
25
|
+
const result = yield redis.set(key, token, { nx: true, px: ttlMs });
|
|
26
|
+
return result === 'OK' ? token : null;
|
|
27
|
+
}));
|
|
40
28
|
});
|
|
41
29
|
/**
|
|
42
30
|
* Release a distributed lock. Returns false when the lock client is unavailable.
|
|
43
31
|
*/
|
|
44
32
|
const releaseLock = (key, token) => __awaiter(void 0, void 0, void 0, function* () {
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
return
|
|
33
|
+
const result = yield withRedis((redis) => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
const released = yield redis.eval(unlockScript, [key], [token]);
|
|
35
|
+
return Number(released) === 1;
|
|
36
|
+
}));
|
|
37
|
+
return result !== null && result !== void 0 ? result : false;
|
|
50
38
|
});
|
|
51
39
|
/**
|
|
52
40
|
* Run a function under a distributed lock. Returns null when the lock is unavailable.
|
|
53
41
|
*/
|
|
54
42
|
const withLock = (key, ttlMs, fn) => __awaiter(void 0, void 0, void 0, function* () {
|
|
55
|
-
const
|
|
56
|
-
if (!lock) {
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
const token = yield lock.acquire(key, ttlMs);
|
|
43
|
+
const token = yield acquireLock(key, ttlMs);
|
|
60
44
|
if (!token) {
|
|
61
45
|
return null;
|
|
62
46
|
}
|
|
@@ -64,7 +48,7 @@ const withLock = (key, ttlMs, fn) => __awaiter(void 0, void 0, void 0, function*
|
|
|
64
48
|
return yield fn();
|
|
65
49
|
}
|
|
66
50
|
finally {
|
|
67
|
-
yield
|
|
51
|
+
yield releaseLock(key, token);
|
|
68
52
|
}
|
|
69
53
|
});
|
|
70
54
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis-structures.d.ts","sourceRoot":"","sources":["../../../src/lib/upstash/redis-structures.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,eAAO,MAAM,SAAS,GACpB,KAAK,MAAM,EACX,OAAO,MAAM,EACb,SAAS,MAAM,KACd,OAAO,CAAC,OAAO,
|
|
1
|
+
{"version":3,"file":"redis-structures.d.ts","sourceRoot":"","sources":["../../../src/lib/upstash/redis-structures.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,eAAO,MAAM,SAAS,GACpB,KAAK,MAAM,EACX,OAAO,MAAM,EACb,SAAS,MAAM,KACd,OAAO,CAAC,OAAO,CAUjB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,SAAS,GAAU,KAAK,MAAM,KAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAElE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,OAAO,GAAU,CAAC,EAC7B,KAAK,MAAM,EACX,OAAO,CAAC,EACR,SAAS,MAAM,KACd,OAAO,CAAC,OAAO,CAWjB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,OAAO,GAAU,CAAC,EAAE,KAAK,MAAM,KAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAa9D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,SAAS,GAAU,KAAK,MAAM,KAAG,OAAO,CAAC,OAAO,CAM5D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,YAAY,GAAU,KAAK,MAAM,EAAE,OAAO,MAAM,EAAE,OAAO,MAAM,KAAG,OAAO,CAAC,OAAO,CAM7F,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,YAAY,GAAU,KAAK,MAAM,EAAE,OAAO,MAAM,KAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAEpF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAU,CAAC,EACjC,KAAK,MAAM,EACX,OAAO,MAAM,EACb,OAAO,CAAC,KACP,OAAO,CAAC,OAAO,CAOjB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAU,CAAC,EAAE,KAAK,MAAM,EAAE,OAAO,MAAM,KAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAajF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,UAAU,GAAU,KAAK,MAAM,KAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,CAKnF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,eAAe,GAAU,KAAK,MAAM,EAAE,OAAO,MAAM,KAAG,OAAO,CAAC,OAAO,CAMjF,CAAC;AAEF,KAAK,aAAa,GAAG,MAAM,GAAG,OAAO,CAAC;AAEtC;;GAEG;AACH,eAAO,MAAM,QAAQ,GACnB,KAAK,MAAM,EACX,QAAQ,MAAM,EAAE,EAChB,YAAW,aAAuB,KACjC,OAAO,CAAC,MAAM,GAAG,IAAI,CAUvB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,OAAO,GAClB,KAAK,MAAM,EACX,YAAW,aAAuB,KACjC,OAAO,CAAC,MAAM,GAAG,IAAI,CAIvB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,SAAS,GACpB,KAAK,MAAM,EACX,cAAS,EACT,aAAS,KACR,OAAO,CAAC,MAAM,EAAE,GAAG,IAAI,CAEzB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,UAAU,GAAU,KAAK,MAAM,KAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAEnE,CAAC"}
|
|
@@ -7,191 +7,155 @@ var upstashConfig = require('../upstash-config.js');
|
|
|
7
7
|
* Set a plain string value with optional TTL (seconds).
|
|
8
8
|
*/
|
|
9
9
|
const setString = (key, value, ttlSec) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
yield redis.set(key, value
|
|
10
|
+
return upstashConfig.withRedis((redis) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
11
|
+
if (ttlSec && ttlSec > 0) {
|
|
12
|
+
yield redis.set(key, value, { ex: ttlSec });
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
yield redis.set(key, value);
|
|
16
16
|
return true;
|
|
17
|
-
}
|
|
18
|
-
yield redis.set(key, value);
|
|
19
|
-
return true;
|
|
17
|
+
})).then((result) => result !== null && result !== void 0 ? result : false);
|
|
20
18
|
});
|
|
21
19
|
/**
|
|
22
20
|
* Get a plain string value. Returns null if Redis is unavailable or key missing.
|
|
23
21
|
*/
|
|
24
22
|
const getString = (key) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
-
|
|
26
|
-
if (!redis) {
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
return redis.get(key);
|
|
23
|
+
return upstashConfig.withRedis((redis) => redis.get(key));
|
|
30
24
|
});
|
|
31
25
|
/**
|
|
32
26
|
* Store an object as JSON string with optional TTL (seconds).
|
|
33
27
|
*/
|
|
34
28
|
const setJson = (key, value, ttlSec) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
yield redis.set(key, payload
|
|
29
|
+
return upstashConfig.withRedis((redis) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
30
|
+
const payload = JSON.stringify(value);
|
|
31
|
+
if (ttlSec && ttlSec > 0) {
|
|
32
|
+
yield redis.set(key, payload, { ex: ttlSec });
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
yield redis.set(key, payload);
|
|
42
36
|
return true;
|
|
43
|
-
}
|
|
44
|
-
yield redis.set(key, payload);
|
|
45
|
-
return true;
|
|
37
|
+
})).then((result) => result !== null && result !== void 0 ? result : false);
|
|
46
38
|
});
|
|
47
39
|
/**
|
|
48
40
|
* Get an object stored as JSON string. Returns null if missing or invalid JSON.
|
|
49
41
|
*/
|
|
50
42
|
const getJson = (key) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
43
|
+
return upstashConfig.withRedis((redis) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
44
|
+
const payload = yield redis.get(key);
|
|
45
|
+
if (!payload) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
return JSON.parse(payload);
|
|
50
|
+
}
|
|
51
|
+
catch (_a) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}));
|
|
65
55
|
});
|
|
66
56
|
/**
|
|
67
57
|
* Delete a key. Returns false if Redis is unavailable.
|
|
68
58
|
*/
|
|
69
59
|
const deleteKey = (key) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
return
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return deleted > 0;
|
|
60
|
+
const result = yield upstashConfig.withRedis((redis) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
61
|
+
const deleted = yield redis.del(key);
|
|
62
|
+
return deleted > 0;
|
|
63
|
+
}));
|
|
64
|
+
return result !== null && result !== void 0 ? result : false;
|
|
76
65
|
});
|
|
77
66
|
/**
|
|
78
67
|
* Set a hash field value.
|
|
79
68
|
*/
|
|
80
69
|
const setHashField = (key, field, value) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
return
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return result > 0;
|
|
70
|
+
const result = yield upstashConfig.withRedis((redis) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
71
|
+
const changed = yield redis.hset(key, { [field]: value });
|
|
72
|
+
return changed > 0;
|
|
73
|
+
}));
|
|
74
|
+
return result !== null && result !== void 0 ? result : false;
|
|
87
75
|
});
|
|
88
76
|
/**
|
|
89
77
|
* Get a hash field value.
|
|
90
78
|
*/
|
|
91
79
|
const getHashField = (key, field) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
92
|
-
|
|
93
|
-
if (!redis) {
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
return redis.hget(key, field);
|
|
80
|
+
return upstashConfig.withRedis((redis) => redis.hget(key, field));
|
|
97
81
|
});
|
|
98
82
|
/**
|
|
99
83
|
* Store a hash field as JSON string.
|
|
100
84
|
*/
|
|
101
85
|
const setHashJson = (key, field, value) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
return result > 0;
|
|
86
|
+
const result = yield upstashConfig.withRedis((redis) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
87
|
+
const payload = JSON.stringify(value);
|
|
88
|
+
const changed = yield redis.hset(key, { [field]: payload });
|
|
89
|
+
return changed > 0;
|
|
90
|
+
}));
|
|
91
|
+
return result !== null && result !== void 0 ? result : false;
|
|
109
92
|
});
|
|
110
93
|
/**
|
|
111
94
|
* Get a hash field stored as JSON string.
|
|
112
95
|
*/
|
|
113
96
|
const getHashJson = (key, field) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return null;
|
|
127
|
-
}
|
|
97
|
+
return upstashConfig.withRedis((redis) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
98
|
+
const payload = yield redis.hget(key, field);
|
|
99
|
+
if (!payload) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
return JSON.parse(payload);
|
|
104
|
+
}
|
|
105
|
+
catch (_a) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
}));
|
|
128
109
|
});
|
|
129
110
|
/**
|
|
130
111
|
* Get all hash fields.
|
|
131
112
|
*/
|
|
132
113
|
const getHashAll = (key) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
return null;
|
|
136
|
-
}
|
|
137
|
-
const result = yield redis.hgetall(key);
|
|
138
|
-
return result !== null && result !== void 0 ? result : {};
|
|
114
|
+
return upstashConfig.withRedis((redis) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
115
|
+
const result = yield redis.hgetall(key);
|
|
116
|
+
return result !== null && result !== void 0 ? result : {};
|
|
117
|
+
}));
|
|
139
118
|
});
|
|
140
119
|
/**
|
|
141
120
|
* Remove a hash field.
|
|
142
121
|
*/
|
|
143
122
|
const deleteHashField = (key, field) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
return
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return removed > 0;
|
|
123
|
+
const result = yield upstashConfig.withRedis((redis) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
124
|
+
const removed = yield redis.hdel(key, field);
|
|
125
|
+
return removed > 0;
|
|
126
|
+
}));
|
|
127
|
+
return result !== null && result !== void 0 ? result : false;
|
|
150
128
|
});
|
|
151
129
|
/**
|
|
152
130
|
* Push values to a list. Returns list length or null if Redis is unavailable.
|
|
153
131
|
*/
|
|
154
132
|
const pushList = (key_1, values_1, ...args_1) => tslib_es6.__awaiter(void 0, [key_1, values_1, ...args_1], void 0, function* (key, values, direction = 'right') {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
? redis.lpush(key, ...values)
|
|
164
|
-
: redis.rpush(key, ...values);
|
|
133
|
+
return upstashConfig.withRedis((redis) => {
|
|
134
|
+
if (values.length === 0) {
|
|
135
|
+
return redis.llen(key);
|
|
136
|
+
}
|
|
137
|
+
return direction === 'left'
|
|
138
|
+
? redis.lpush(key, ...values)
|
|
139
|
+
: redis.rpush(key, ...values);
|
|
140
|
+
});
|
|
165
141
|
});
|
|
166
142
|
/**
|
|
167
143
|
* Pop a value from a list.
|
|
168
144
|
*/
|
|
169
145
|
const popList = (key_1, ...args_1) => tslib_es6.__awaiter(void 0, [key_1, ...args_1], void 0, function* (key, direction = 'right') {
|
|
170
|
-
|
|
171
|
-
if (!redis) {
|
|
172
|
-
return null;
|
|
173
|
-
}
|
|
174
|
-
return direction === 'left' ? redis.lpop(key) : redis.rpop(key);
|
|
146
|
+
return upstashConfig.withRedis((redis) => direction === 'left' ? redis.lpop(key) : redis.rpop(key));
|
|
175
147
|
});
|
|
176
148
|
/**
|
|
177
149
|
* Get a range from a list.
|
|
178
150
|
*/
|
|
179
151
|
const rangeList = (key_1, ...args_1) => tslib_es6.__awaiter(void 0, [key_1, ...args_1], void 0, function* (key, start = 0, stop = -1) {
|
|
180
|
-
|
|
181
|
-
if (!redis) {
|
|
182
|
-
return null;
|
|
183
|
-
}
|
|
184
|
-
return redis.lrange(key, start, stop);
|
|
152
|
+
return upstashConfig.withRedis((redis) => redis.lrange(key, start, stop));
|
|
185
153
|
});
|
|
186
154
|
/**
|
|
187
155
|
* Get list length.
|
|
188
156
|
*/
|
|
189
157
|
const listLength = (key) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
|
|
190
|
-
|
|
191
|
-
if (!redis) {
|
|
192
|
-
return null;
|
|
193
|
-
}
|
|
194
|
-
return redis.llen(key);
|
|
158
|
+
return upstashConfig.withRedis((redis) => redis.llen(key));
|
|
195
159
|
});
|
|
196
160
|
|
|
197
161
|
exports.deleteHashField = deleteHashField;
|