@windrun-huaiin/backend-core 13.0.0 → 14.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.
Files changed (104) hide show
  1. package/dist/_virtual/index.js +7 -3
  2. package/dist/_virtual/index.mjs +5 -3
  3. package/dist/_virtual/index2.js +2 -6
  4. package/dist/_virtual/index2.mjs +2 -6
  5. package/dist/index.js +2 -0
  6. package/dist/index.mjs +1 -1
  7. package/dist/lib/index.js +2 -0
  8. package/dist/lib/index.mjs +1 -1
  9. package/dist/lib/upstash/qstash.d.ts.map +1 -1
  10. package/dist/lib/upstash/qstash.js +66 -62
  11. package/dist/lib/upstash/qstash.mjs +67 -63
  12. package/dist/lib/upstash/redis-counter.d.ts.map +1 -1
  13. package/dist/lib/upstash/redis-counter.js +9 -24
  14. package/dist/lib/upstash/redis-counter.mjs +10 -25
  15. package/dist/lib/upstash/redis-favorite.d.ts.map +1 -1
  16. package/dist/lib/upstash/redis-favorite.js +22 -36
  17. package/dist/lib/upstash/redis-favorite.mjs +23 -37
  18. package/dist/lib/upstash/redis-like.d.ts.map +1 -1
  19. package/dist/lib/upstash/redis-like.js +22 -36
  20. package/dist/lib/upstash/redis-like.mjs +23 -37
  21. package/dist/lib/upstash/redis-lock.d.ts.map +1 -1
  22. package/dist/lib/upstash/redis-lock.js +22 -38
  23. package/dist/lib/upstash/redis-lock.mjs +23 -39
  24. package/dist/lib/upstash/redis-structures.d.ts.map +1 -1
  25. package/dist/lib/upstash/redis-structures.js +77 -113
  26. package/dist/lib/upstash/redis-structures.mjs +78 -114
  27. package/dist/lib/upstash-config.d.ts +9 -1
  28. package/dist/lib/upstash-config.d.ts.map +1 -1
  29. package/dist/lib/upstash-config.js +221 -27
  30. package/dist/lib/upstash-config.mjs +220 -28
  31. 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
  32. 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
  33. 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
  34. 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
  35. package/dist/node_modules/.pnpm/{@upstash_redis@1.36.1 → @upstash_redis@1.37.0}/node_modules/@upstash/redis/nodejs.js +6 -5
  36. package/dist/node_modules/.pnpm/{@upstash_redis@1.36.1 → @upstash_redis@1.37.0}/node_modules/@upstash/redis/nodejs.mjs +2 -2
  37. package/dist/node_modules/.pnpm/crypto-js@4.2.0/node_modules/crypto-js/index.js +1 -1
  38. package/dist/node_modules/.pnpm/crypto-js@4.2.0/node_modules/crypto-js/index.mjs +1 -1
  39. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/jws/flattened/verify.js +6 -6
  40. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/jwt/verify.js +1 -1
  41. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/key/import.js +2 -2
  42. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/epoch.js +3 -1
  43. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/is_disjoint.js +3 -1
  44. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/is_jwk.js +1 -1
  45. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/is_object.js +3 -1
  46. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/jwt_claims_set.js +7 -5
  47. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/secs.js +3 -1
  48. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/validate_algorithms.js +3 -1
  49. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/lib/validate_crit.js +3 -1
  50. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/check_key_length.js +3 -1
  51. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/dsa_digest.js +3 -1
  52. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/get_named_curve.js +4 -2
  53. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/get_sign_verify_key.js +3 -1
  54. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/hmac_digest.js +3 -1
  55. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/is_key_like.js +1 -1
  56. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/is_key_object.js +3 -1
  57. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/jwk_to_key.js +3 -1
  58. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/node_key.js +6 -4
  59. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/sign.js +6 -4
  60. package/dist/node_modules/.pnpm/jose@5.10.0/node_modules/jose/dist/node/esm/runtime/verify.js +7 -5
  61. package/dist/prisma/prisma.d.ts +1 -1
  62. package/dist/prisma/prisma.d.ts.map +1 -1
  63. package/package.json +5 -5
  64. package/src/lib/upstash/qstash.ts +64 -62
  65. package/src/lib/upstash/redis-counter.ts +10 -26
  66. package/src/lib/upstash/redis-favorite.ts +23 -42
  67. package/src/lib/upstash/redis-like.ts +23 -42
  68. package/src/lib/upstash/redis-lock.ts +23 -49
  69. package/src/lib/upstash/redis-structures.ts +82 -131
  70. package/src/lib/upstash-config.ts +231 -24
  71. package/dist/_virtual/index3.js +0 -5
  72. package/dist/_virtual/index3.mjs +0 -3
  73. package/dist/node_modules/.pnpm/@upstash_lock@0.2.1_typescript@5.9.3/node_modules/@upstash/lock/dist/index.js +0 -191
  74. package/dist/node_modules/.pnpm/@upstash_lock@0.2.1_typescript@5.9.3/node_modules/@upstash/lock/dist/index.mjs +0 -189
  75. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/errors.js +0 -54
  76. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/errors.mjs +0 -51
  77. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/iso.js +0 -44
  78. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/iso.mjs +0 -35
  79. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/parse.js +0 -31
  80. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/parse.mjs +0 -18
  81. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/schemas.js +0 -587
  82. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/schemas.mjs +0 -527
  83. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/api.js +0 -447
  84. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/api.mjs +0 -399
  85. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/checks.js +0 -245
  86. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/checks.mjs +0 -232
  87. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/core.js +0 -68
  88. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/core.mjs +0 -62
  89. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/doc.js +0 -39
  90. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/doc.mjs +0 -37
  91. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/errors.js +0 -80
  92. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/errors.mjs +0 -75
  93. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/parse.js +0 -101
  94. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/parse.mjs +0 -86
  95. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/regexes.js +0 -102
  96. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/regexes.mjs +0 -76
  97. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/registries.js +0 -56
  98. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/registries.mjs +0 -52
  99. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/schemas.js +0 -1205
  100. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/schemas.mjs +0 -1157
  101. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/util.js +0 -407
  102. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/util.mjs +0 -374
  103. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/versions.js +0 -9
  104. package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/versions.mjs +0 -7
@@ -1,53 +1,247 @@
1
1
  'use strict';
2
2
 
3
- var nodejs = require('../node_modules/.pnpm/@upstash_redis@1.36.1/node_modules/@upstash/redis/nodejs.js');
4
- var chunkRQPZUJXG = require('../node_modules/.pnpm/@upstash_qstash@2.8.4/node_modules/@upstash/qstash/chunk-RQPZUJXG.js');
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 nodejs = require('../node_modules/.pnpm/@upstash_redis@1.37.0/node_modules/@upstash/redis/nodejs.js');
5
+ var chunk35B33QW3 = require('../node_modules/.pnpm/@upstash_qstash@2.10.1/node_modules/@upstash/qstash/chunk-35B33QW3.js');
5
6
 
6
7
  let cachedRedis = null;
7
8
  let cachedQstash = null;
8
- let redisInitAttempted = false;
9
- let qstashInitAttempted = false;
9
+ let redisInitPromise = null;
10
+ let qstashInitPromise = null;
11
+ let redisWarnedMissingEnv = false;
12
+ let redisWarnedInvalidEnv = false;
13
+ let redisWarnedInitError = false;
14
+ let redisWarnedHealthCheck = false;
15
+ let redisWarnedHealthSchedule = false;
16
+ let qstashWarnedMissingEnv = false;
17
+ let qstashWarnedInitError = false;
18
+ let qstashWarnedHealthCheck = false;
19
+ let qstashWarnedHealthSchedule = false;
20
+ let redisHealthTimer = null;
21
+ let qstashHealthTimer = null;
22
+ const isNonEmpty = (value) => typeof value === 'string' && value.trim().length > 0;
23
+ const isValidUrl = (value) => {
24
+ try {
25
+ const parsed = new URL(value);
26
+ return parsed.protocol === 'http:' || parsed.protocol === 'https:';
27
+ }
28
+ catch (_a) {
29
+ return false;
30
+ }
31
+ };
32
+ const parseMinutes = (value, fallback) => {
33
+ if (!isNonEmpty(value)) {
34
+ return fallback;
35
+ }
36
+ const minutes = Number(value);
37
+ if (!Number.isFinite(minutes) || minutes <= 0) {
38
+ return fallback;
39
+ }
40
+ return minutes;
41
+ };
42
+ const getRedisHealthIntervalMinutes = () => parseMinutes(process.env.UPSTASH_REDIS_HEALTHCHECK_INTERVAL_MINUTES, 10);
43
+ const getQstashHealthIntervalMinutes = () => parseMinutes(process.env.UPSTASH_QSTASH_HEALTHCHECK_INTERVAL_MINUTES, 10);
44
+ const getQstashHealthcheckUrl = () => { var _a; return (_a = process.env.UPSTASH_QSTASH_HEALTHCHECK_URL) !== null && _a !== void 0 ? _a : 'https://qstash.upstash.io/v2/topics'; };
45
+ const scheduleRedisHealthCheck = () => {
46
+ if (redisHealthTimer || !cachedRedis) {
47
+ return;
48
+ }
49
+ const delayMs = getRedisHealthIntervalMinutes() * 60000;
50
+ redisHealthTimer = setTimeout(() => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
51
+ redisHealthTimer = null;
52
+ if (!cachedRedis) {
53
+ return;
54
+ }
55
+ try {
56
+ yield cachedRedis.ping();
57
+ }
58
+ catch (error) {
59
+ cachedRedis = null;
60
+ if (!redisWarnedHealthCheck) {
61
+ redisWarnedHealthCheck = true;
62
+ const message = error instanceof Error ? error.message : String(error);
63
+ console.warn(`[Upstash Config] Redis health check failed: ${message}`);
64
+ }
65
+ }
66
+ finally {
67
+ try {
68
+ scheduleRedisHealthCheck();
69
+ }
70
+ catch (error) {
71
+ if (!redisWarnedHealthSchedule) {
72
+ redisWarnedHealthSchedule = true;
73
+ const message = error instanceof Error ? error.message : String(error);
74
+ console.warn(`[Upstash Config] Redis health check schedule failed: ${message}`);
75
+ }
76
+ }
77
+ }
78
+ }), delayMs);
79
+ };
80
+ const checkQstashHealth = (token) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
81
+ const response = yield fetch(getQstashHealthcheckUrl(), {
82
+ method: 'GET',
83
+ headers: {
84
+ Authorization: `Bearer ${token}`,
85
+ },
86
+ cache: 'no-store',
87
+ });
88
+ if (!response.ok) {
89
+ throw new Error(`HTTP ${response.status}`);
90
+ }
91
+ });
92
+ const scheduleQstashHealthCheck = (token) => {
93
+ if (qstashHealthTimer || !cachedQstash) {
94
+ return;
95
+ }
96
+ const delayMs = getQstashHealthIntervalMinutes() * 60000;
97
+ qstashHealthTimer = setTimeout(() => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
98
+ qstashHealthTimer = null;
99
+ if (!cachedQstash) {
100
+ return;
101
+ }
102
+ try {
103
+ yield checkQstashHealth(token);
104
+ }
105
+ catch (error) {
106
+ cachedQstash = null;
107
+ if (!qstashWarnedHealthCheck) {
108
+ qstashWarnedHealthCheck = true;
109
+ const message = error instanceof Error ? error.message : String(error);
110
+ console.warn(`[Upstash Config] QStash health check failed: ${message}`);
111
+ }
112
+ }
113
+ finally {
114
+ try {
115
+ scheduleQstashHealthCheck(token);
116
+ }
117
+ catch (error) {
118
+ if (!qstashWarnedHealthSchedule) {
119
+ qstashWarnedHealthSchedule = true;
120
+ const message = error instanceof Error ? error.message : String(error);
121
+ console.warn(`[Upstash Config] QStash health check schedule failed: ${message}`);
122
+ }
123
+ }
124
+ }
125
+ }), delayMs);
126
+ };
10
127
  /**
11
- * Get the Upstash Redis client. Returns null when required env vars are missing.
128
+ * Get the Upstash Redis client. Returns null when required env vars are missing/invalid.
129
+ *
130
+ * Singleton semantics:
131
+ * - read-through cached instance only
12
132
  */
13
133
  const getRedis = () => {
134
+ return cachedRedis;
135
+ };
136
+ const ensureRedis = () => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
14
137
  if (cachedRedis) {
15
138
  return cachedRedis;
16
139
  }
17
- if (redisInitAttempted) {
18
- return null;
19
- }
20
- redisInitAttempted = true;
21
- const { UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN } = process.env;
22
- if (!UPSTASH_REDIS_REST_URL || !UPSTASH_REDIS_REST_TOKEN) {
23
- console.warn('[Upstash Config] Redis Missed configration UPSTASH_REDIS_REST_URL or UPSTASH_REDIS_REST_TOKEN, then disabled');
24
- return null;
140
+ if (redisInitPromise) {
141
+ return redisInitPromise;
25
142
  }
26
- cachedRedis = new nodejs.Redis({
27
- url: UPSTASH_REDIS_REST_URL,
28
- token: UPSTASH_REDIS_REST_TOKEN,
29
- });
30
- return cachedRedis;
31
- };
143
+ redisInitPromise = (() => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
144
+ const { UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN } = process.env;
145
+ if (!isNonEmpty(UPSTASH_REDIS_REST_URL) || !isNonEmpty(UPSTASH_REDIS_REST_TOKEN)) {
146
+ if (!redisWarnedMissingEnv) {
147
+ redisWarnedMissingEnv = true;
148
+ console.warn('[Upstash Config] Redis disabled: missing UPSTASH_REDIS_REST_URL or UPSTASH_REDIS_REST_TOKEN');
149
+ }
150
+ return null;
151
+ }
152
+ if (!isValidUrl(UPSTASH_REDIS_REST_URL)) {
153
+ if (!redisWarnedInvalidEnv) {
154
+ redisWarnedInvalidEnv = true;
155
+ console.warn('[Upstash Config] Redis disabled: UPSTASH_REDIS_REST_URL is not a valid URL');
156
+ }
157
+ return null;
158
+ }
159
+ try {
160
+ const client = new nodejs.Redis({
161
+ url: UPSTASH_REDIS_REST_URL,
162
+ token: UPSTASH_REDIS_REST_TOKEN,
163
+ });
164
+ yield client.ping();
165
+ cachedRedis = client;
166
+ scheduleRedisHealthCheck();
167
+ return cachedRedis;
168
+ }
169
+ catch (error) {
170
+ if (!redisWarnedInitError) {
171
+ redisWarnedInitError = true;
172
+ const message = error instanceof Error ? error.message : String(error);
173
+ console.warn(`[Upstash Config] Redis init failed: ${message}`);
174
+ }
175
+ return null;
176
+ }
177
+ finally {
178
+ redisInitPromise = null;
179
+ }
180
+ }))();
181
+ return redisInitPromise;
182
+ });
32
183
  /**
33
184
  * Get the Upstash QStash client. Returns null when required env vars are missing.
185
+ *
186
+ * Singleton semantics:
187
+ * - read-through cached instance only
34
188
  */
35
189
  const getQstash = () => {
190
+ return cachedQstash;
191
+ };
192
+ const ensureQstash = () => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
36
193
  if (cachedQstash) {
37
194
  return cachedQstash;
38
195
  }
39
- if (qstashInitAttempted) {
196
+ if (qstashInitPromise) {
197
+ return qstashInitPromise;
198
+ }
199
+ qstashInitPromise = (() => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
200
+ const { QSTASH_TOKEN } = process.env;
201
+ if (!isNonEmpty(QSTASH_TOKEN)) {
202
+ if (!qstashWarnedMissingEnv) {
203
+ qstashWarnedMissingEnv = true;
204
+ console.warn('[Upstash Config] QStash disabled: missing QSTASH_TOKEN');
205
+ }
206
+ return null;
207
+ }
208
+ try {
209
+ const client = new chunk35B33QW3.Client({ token: QSTASH_TOKEN });
210
+ yield checkQstashHealth(QSTASH_TOKEN);
211
+ cachedQstash = client;
212
+ scheduleQstashHealthCheck(QSTASH_TOKEN);
213
+ return cachedQstash;
214
+ }
215
+ catch (error) {
216
+ if (!qstashWarnedInitError) {
217
+ qstashWarnedInitError = true;
218
+ const message = error instanceof Error ? error.message : String(error);
219
+ console.warn(`[Upstash Config] QStash init failed: ${message}`);
220
+ }
221
+ return null;
222
+ }
223
+ finally {
224
+ qstashInitPromise = null;
225
+ }
226
+ }))();
227
+ return qstashInitPromise;
228
+ });
229
+ const withRedis = (fn) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
230
+ const redis = yield ensureRedis();
231
+ if (!redis) {
40
232
  return null;
41
233
  }
42
- qstashInitAttempted = true;
43
- const { QSTASH_TOKEN } = process.env;
44
- if (!QSTASH_TOKEN) {
45
- console.warn('[Upstash Config] QStash Missed configration QSTASH_TOKEN, then disabled');
234
+ return fn(redis);
235
+ });
236
+ const withQstash = (fn) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
237
+ const qstash = yield ensureQstash();
238
+ if (!qstash) {
46
239
  return null;
47
240
  }
48
- cachedQstash = new chunkRQPZUJXG.Client({ token: QSTASH_TOKEN });
49
- return cachedQstash;
50
- };
241
+ return fn(qstash);
242
+ });
51
243
 
52
244
  exports.getQstash = getQstash;
53
245
  exports.getRedis = getRedis;
246
+ exports.withQstash = withQstash;
247
+ exports.withRedis = withRedis;
@@ -1,50 +1,242 @@
1
- import { Redis as Redis2 } from '../node_modules/.pnpm/@upstash_redis@1.36.1/node_modules/@upstash/redis/nodejs.mjs';
2
- import { Client } from '../node_modules/.pnpm/@upstash_qstash@2.8.4/node_modules/@upstash/qstash/chunk-RQPZUJXG.mjs';
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 { Redis as Redis2 } from '../node_modules/.pnpm/@upstash_redis@1.37.0/node_modules/@upstash/redis/nodejs.mjs';
3
+ import { Client } from '../node_modules/.pnpm/@upstash_qstash@2.10.1/node_modules/@upstash/qstash/chunk-35B33QW3.mjs';
3
4
 
4
5
  let cachedRedis = null;
5
6
  let cachedQstash = null;
6
- let redisInitAttempted = false;
7
- let qstashInitAttempted = false;
7
+ let redisInitPromise = null;
8
+ let qstashInitPromise = null;
9
+ let redisWarnedMissingEnv = false;
10
+ let redisWarnedInvalidEnv = false;
11
+ let redisWarnedInitError = false;
12
+ let redisWarnedHealthCheck = false;
13
+ let redisWarnedHealthSchedule = false;
14
+ let qstashWarnedMissingEnv = false;
15
+ let qstashWarnedInitError = false;
16
+ let qstashWarnedHealthCheck = false;
17
+ let qstashWarnedHealthSchedule = false;
18
+ let redisHealthTimer = null;
19
+ let qstashHealthTimer = null;
20
+ const isNonEmpty = (value) => typeof value === 'string' && value.trim().length > 0;
21
+ const isValidUrl = (value) => {
22
+ try {
23
+ const parsed = new URL(value);
24
+ return parsed.protocol === 'http:' || parsed.protocol === 'https:';
25
+ }
26
+ catch (_a) {
27
+ return false;
28
+ }
29
+ };
30
+ const parseMinutes = (value, fallback) => {
31
+ if (!isNonEmpty(value)) {
32
+ return fallback;
33
+ }
34
+ const minutes = Number(value);
35
+ if (!Number.isFinite(minutes) || minutes <= 0) {
36
+ return fallback;
37
+ }
38
+ return minutes;
39
+ };
40
+ const getRedisHealthIntervalMinutes = () => parseMinutes(process.env.UPSTASH_REDIS_HEALTHCHECK_INTERVAL_MINUTES, 10);
41
+ const getQstashHealthIntervalMinutes = () => parseMinutes(process.env.UPSTASH_QSTASH_HEALTHCHECK_INTERVAL_MINUTES, 10);
42
+ const getQstashHealthcheckUrl = () => { var _a; return (_a = process.env.UPSTASH_QSTASH_HEALTHCHECK_URL) !== null && _a !== void 0 ? _a : 'https://qstash.upstash.io/v2/topics'; };
43
+ const scheduleRedisHealthCheck = () => {
44
+ if (redisHealthTimer || !cachedRedis) {
45
+ return;
46
+ }
47
+ const delayMs = getRedisHealthIntervalMinutes() * 60000;
48
+ redisHealthTimer = setTimeout(() => __awaiter(void 0, void 0, void 0, function* () {
49
+ redisHealthTimer = null;
50
+ if (!cachedRedis) {
51
+ return;
52
+ }
53
+ try {
54
+ yield cachedRedis.ping();
55
+ }
56
+ catch (error) {
57
+ cachedRedis = null;
58
+ if (!redisWarnedHealthCheck) {
59
+ redisWarnedHealthCheck = true;
60
+ const message = error instanceof Error ? error.message : String(error);
61
+ console.warn(`[Upstash Config] Redis health check failed: ${message}`);
62
+ }
63
+ }
64
+ finally {
65
+ try {
66
+ scheduleRedisHealthCheck();
67
+ }
68
+ catch (error) {
69
+ if (!redisWarnedHealthSchedule) {
70
+ redisWarnedHealthSchedule = true;
71
+ const message = error instanceof Error ? error.message : String(error);
72
+ console.warn(`[Upstash Config] Redis health check schedule failed: ${message}`);
73
+ }
74
+ }
75
+ }
76
+ }), delayMs);
77
+ };
78
+ const checkQstashHealth = (token) => __awaiter(void 0, void 0, void 0, function* () {
79
+ const response = yield fetch(getQstashHealthcheckUrl(), {
80
+ method: 'GET',
81
+ headers: {
82
+ Authorization: `Bearer ${token}`,
83
+ },
84
+ cache: 'no-store',
85
+ });
86
+ if (!response.ok) {
87
+ throw new Error(`HTTP ${response.status}`);
88
+ }
89
+ });
90
+ const scheduleQstashHealthCheck = (token) => {
91
+ if (qstashHealthTimer || !cachedQstash) {
92
+ return;
93
+ }
94
+ const delayMs = getQstashHealthIntervalMinutes() * 60000;
95
+ qstashHealthTimer = setTimeout(() => __awaiter(void 0, void 0, void 0, function* () {
96
+ qstashHealthTimer = null;
97
+ if (!cachedQstash) {
98
+ return;
99
+ }
100
+ try {
101
+ yield checkQstashHealth(token);
102
+ }
103
+ catch (error) {
104
+ cachedQstash = null;
105
+ if (!qstashWarnedHealthCheck) {
106
+ qstashWarnedHealthCheck = true;
107
+ const message = error instanceof Error ? error.message : String(error);
108
+ console.warn(`[Upstash Config] QStash health check failed: ${message}`);
109
+ }
110
+ }
111
+ finally {
112
+ try {
113
+ scheduleQstashHealthCheck(token);
114
+ }
115
+ catch (error) {
116
+ if (!qstashWarnedHealthSchedule) {
117
+ qstashWarnedHealthSchedule = true;
118
+ const message = error instanceof Error ? error.message : String(error);
119
+ console.warn(`[Upstash Config] QStash health check schedule failed: ${message}`);
120
+ }
121
+ }
122
+ }
123
+ }), delayMs);
124
+ };
8
125
  /**
9
- * Get the Upstash Redis client. Returns null when required env vars are missing.
126
+ * Get the Upstash Redis client. Returns null when required env vars are missing/invalid.
127
+ *
128
+ * Singleton semantics:
129
+ * - read-through cached instance only
10
130
  */
11
131
  const getRedis = () => {
132
+ return cachedRedis;
133
+ };
134
+ const ensureRedis = () => __awaiter(void 0, void 0, void 0, function* () {
12
135
  if (cachedRedis) {
13
136
  return cachedRedis;
14
137
  }
15
- if (redisInitAttempted) {
16
- return null;
17
- }
18
- redisInitAttempted = true;
19
- const { UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN } = process.env;
20
- if (!UPSTASH_REDIS_REST_URL || !UPSTASH_REDIS_REST_TOKEN) {
21
- console.warn('[Upstash Config] Redis Missed configration UPSTASH_REDIS_REST_URL or UPSTASH_REDIS_REST_TOKEN, then disabled');
22
- return null;
138
+ if (redisInitPromise) {
139
+ return redisInitPromise;
23
140
  }
24
- cachedRedis = new Redis2({
25
- url: UPSTASH_REDIS_REST_URL,
26
- token: UPSTASH_REDIS_REST_TOKEN,
27
- });
28
- return cachedRedis;
29
- };
141
+ redisInitPromise = (() => __awaiter(void 0, void 0, void 0, function* () {
142
+ const { UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN } = process.env;
143
+ if (!isNonEmpty(UPSTASH_REDIS_REST_URL) || !isNonEmpty(UPSTASH_REDIS_REST_TOKEN)) {
144
+ if (!redisWarnedMissingEnv) {
145
+ redisWarnedMissingEnv = true;
146
+ console.warn('[Upstash Config] Redis disabled: missing UPSTASH_REDIS_REST_URL or UPSTASH_REDIS_REST_TOKEN');
147
+ }
148
+ return null;
149
+ }
150
+ if (!isValidUrl(UPSTASH_REDIS_REST_URL)) {
151
+ if (!redisWarnedInvalidEnv) {
152
+ redisWarnedInvalidEnv = true;
153
+ console.warn('[Upstash Config] Redis disabled: UPSTASH_REDIS_REST_URL is not a valid URL');
154
+ }
155
+ return null;
156
+ }
157
+ try {
158
+ const client = new Redis2({
159
+ url: UPSTASH_REDIS_REST_URL,
160
+ token: UPSTASH_REDIS_REST_TOKEN,
161
+ });
162
+ yield client.ping();
163
+ cachedRedis = client;
164
+ scheduleRedisHealthCheck();
165
+ return cachedRedis;
166
+ }
167
+ catch (error) {
168
+ if (!redisWarnedInitError) {
169
+ redisWarnedInitError = true;
170
+ const message = error instanceof Error ? error.message : String(error);
171
+ console.warn(`[Upstash Config] Redis init failed: ${message}`);
172
+ }
173
+ return null;
174
+ }
175
+ finally {
176
+ redisInitPromise = null;
177
+ }
178
+ }))();
179
+ return redisInitPromise;
180
+ });
30
181
  /**
31
182
  * Get the Upstash QStash client. Returns null when required env vars are missing.
183
+ *
184
+ * Singleton semantics:
185
+ * - read-through cached instance only
32
186
  */
33
187
  const getQstash = () => {
188
+ return cachedQstash;
189
+ };
190
+ const ensureQstash = () => __awaiter(void 0, void 0, void 0, function* () {
34
191
  if (cachedQstash) {
35
192
  return cachedQstash;
36
193
  }
37
- if (qstashInitAttempted) {
194
+ if (qstashInitPromise) {
195
+ return qstashInitPromise;
196
+ }
197
+ qstashInitPromise = (() => __awaiter(void 0, void 0, void 0, function* () {
198
+ const { QSTASH_TOKEN } = process.env;
199
+ if (!isNonEmpty(QSTASH_TOKEN)) {
200
+ if (!qstashWarnedMissingEnv) {
201
+ qstashWarnedMissingEnv = true;
202
+ console.warn('[Upstash Config] QStash disabled: missing QSTASH_TOKEN');
203
+ }
204
+ return null;
205
+ }
206
+ try {
207
+ const client = new Client({ token: QSTASH_TOKEN });
208
+ yield checkQstashHealth(QSTASH_TOKEN);
209
+ cachedQstash = client;
210
+ scheduleQstashHealthCheck(QSTASH_TOKEN);
211
+ return cachedQstash;
212
+ }
213
+ catch (error) {
214
+ if (!qstashWarnedInitError) {
215
+ qstashWarnedInitError = true;
216
+ const message = error instanceof Error ? error.message : String(error);
217
+ console.warn(`[Upstash Config] QStash init failed: ${message}`);
218
+ }
219
+ return null;
220
+ }
221
+ finally {
222
+ qstashInitPromise = null;
223
+ }
224
+ }))();
225
+ return qstashInitPromise;
226
+ });
227
+ const withRedis = (fn) => __awaiter(void 0, void 0, void 0, function* () {
228
+ const redis = yield ensureRedis();
229
+ if (!redis) {
38
230
  return null;
39
231
  }
40
- qstashInitAttempted = true;
41
- const { QSTASH_TOKEN } = process.env;
42
- if (!QSTASH_TOKEN) {
43
- console.warn('[Upstash Config] QStash Missed configration QSTASH_TOKEN, then disabled');
232
+ return fn(redis);
233
+ });
234
+ const withQstash = (fn) => __awaiter(void 0, void 0, void 0, function* () {
235
+ const qstash = yield ensureQstash();
236
+ if (!qstash) {
44
237
  return null;
45
238
  }
46
- cachedQstash = new Client({ token: QSTASH_TOKEN });
47
- return cachedQstash;
48
- };
239
+ return fn(qstash);
240
+ });
49
241
 
50
- export { getQstash, getRedis };
242
+ export { getQstash, getRedis, withQstash, withRedis };