@zintrust/queue-redis 0.1.23 → 0.1.41

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,251 @@
1
+ import { Env, ErrorFactory, SignedRequest, ZintrustLang } from '@zintrust/core';
2
+ const resolveSigningPrefix = (baseUrl) => {
3
+ try {
4
+ const parsed = new URL(baseUrl);
5
+ const path = parsed.pathname.endsWith('/') ? parsed.pathname.slice(0, -1) : parsed.pathname;
6
+ if (path === '' || path === '/')
7
+ return undefined;
8
+ return path;
9
+ }
10
+ catch {
11
+ return undefined;
12
+ }
13
+ };
14
+ const buildRequestUrl = (baseUrl, path) => {
15
+ const url = new URL(baseUrl);
16
+ const basePath = url.pathname.endsWith('/') ? url.pathname.slice(0, -1) : url.pathname;
17
+ const requestPath = path.startsWith('/') ? path : `/${path}`;
18
+ url.pathname = `${basePath}${requestPath}`;
19
+ return url;
20
+ };
21
+ const buildSigningUrl = (requestUrl, baseUrl) => {
22
+ const prefix = resolveSigningPrefix(baseUrl);
23
+ if (!prefix)
24
+ return requestUrl;
25
+ if (requestUrl.pathname === prefix || requestUrl.pathname.startsWith(`${prefix}/`)) {
26
+ const signingUrl = new URL(requestUrl.toString());
27
+ const stripped = requestUrl.pathname.slice(prefix.length);
28
+ signingUrl.pathname = stripped.startsWith('/') ? stripped : `/${stripped}`;
29
+ return signingUrl;
30
+ }
31
+ return requestUrl;
32
+ };
33
+ let publishClientInstance = null;
34
+ let publishClientConnected = false;
35
+ const resolveProxyBaseUrl = () => {
36
+ const explicit = Env.REDIS_PROXY_URL.trim();
37
+ if (explicit !== '')
38
+ return explicit;
39
+ if (Env.USE_REDIS_PROXY === false)
40
+ return '';
41
+ const host = Env.REDIS_PROXY_HOST || '127.0.0.1';
42
+ const port = Env.REDIS_PROXY_PORT;
43
+ return `http://${host}:${port}`;
44
+ };
45
+ const buildProxySettings = () => {
46
+ const baseUrl = resolveProxyBaseUrl();
47
+ const keyId = Env.REDIS_PROXY_KEY_ID || undefined;
48
+ const secret = Env.REDIS_PROXY_SECRET || Env.APP_KEY || undefined;
49
+ const timeoutMs = Env.REDIS_PROXY_TIMEOUT_MS;
50
+ return { baseUrl, keyId, secret, timeoutMs };
51
+ };
52
+ const buildHeaders = async (settings, requestUrl, body) => {
53
+ const headers = {
54
+ 'Content-Type': 'application/json',
55
+ };
56
+ if (settings.keyId && settings.secret) {
57
+ const signingUrl = buildSigningUrl(requestUrl, settings.baseUrl);
58
+ const signed = await SignedRequest.createHeaders({
59
+ method: 'POST',
60
+ url: signingUrl,
61
+ body,
62
+ keyId: settings.keyId,
63
+ secret: settings.secret,
64
+ });
65
+ Object.assign(headers, signed);
66
+ }
67
+ return headers;
68
+ };
69
+ const requestProxy = async (settings, path, payload) => {
70
+ if (settings.baseUrl.trim() === '') {
71
+ throw ErrorFactory.createConfigError('Redis proxy URL is missing (REDIS_PROXY_URL)');
72
+ }
73
+ const body = JSON.stringify(payload);
74
+ const url = buildRequestUrl(settings.baseUrl, path);
75
+ const headers = await buildHeaders(settings, url, body);
76
+ const timeoutSignal = typeof AbortSignal !== 'undefined' && 'timeout' in AbortSignal;
77
+ const signal = timeoutSignal ? AbortSignal.timeout(settings.timeoutMs) : undefined;
78
+ const response = await fetch(url.toString(), {
79
+ method: 'POST',
80
+ headers,
81
+ body,
82
+ signal,
83
+ });
84
+ if (!response.ok) {
85
+ const text = await response.text();
86
+ throw ErrorFactory.createTryCatchError(`Redis proxy request failed (${response.status})`, text);
87
+ }
88
+ return (await response.json());
89
+ };
90
+ const toNumber = (value) => {
91
+ if (typeof value === 'number')
92
+ return value;
93
+ if (typeof value === 'string') {
94
+ const parsed = Number(value);
95
+ return Number.isFinite(parsed) ? parsed : 0;
96
+ }
97
+ return 0;
98
+ };
99
+ const tryCreateProxyPublishClient = async () => {
100
+ const settings = buildProxySettings();
101
+ if (settings.baseUrl.trim() === '')
102
+ return null;
103
+ return {
104
+ publish: async (channel, message) => {
105
+ const response = await requestProxy(settings, '/zin/redis/command', {
106
+ command: 'PUBLISH',
107
+ args: [channel, message],
108
+ });
109
+ return toNumber(response.result);
110
+ },
111
+ };
112
+ };
113
+ /**
114
+ * Build Redis URL from environment variables
115
+ */
116
+ const buildRedisUrl = () => {
117
+ // Get REDIS_URL from environment
118
+ const redisUrl = getRedisUrlFromEnv();
119
+ if (redisUrl)
120
+ return redisUrl;
121
+ // Build URL from individual components
122
+ return buildRedisUrlFromComponents();
123
+ };
124
+ /**
125
+ * Get REDIS_URL from environment variables
126
+ */
127
+ const getRedisUrlFromEnv = () => {
128
+ const anyEnv = process.env;
129
+ const fromEnv = typeof anyEnv.get === 'function' ? anyEnv.get('REDIS_URL', '') : '';
130
+ const hasProcess = typeof process === 'object' && process !== null;
131
+ const fallback = hasProcess ? (process.env?.['REDIS_URL'] ?? '') : '';
132
+ const trimmed = fromEnv.trim();
133
+ const url = (trimmed.length > 0 ? fromEnv : String(fallback)).trim();
134
+ return url.length > 0 ? url : null;
135
+ };
136
+ /**
137
+ * Build Redis URL from individual environment components
138
+ */
139
+ const buildRedisUrlFromComponents = () => {
140
+ const host = process.env?.['REDIS_HOST'] ?? 'localhost';
141
+ const port = Number(process.env?.['REDIS_PORT'] ?? ZintrustLang.REDIS_DEFAULT_PORT);
142
+ const password = process.env?.['REDIS_PASSWORD'];
143
+ const database = Number(process.env?.['REDIS_QUEUE_DB'] ?? ZintrustLang.REDIS_DEFAULT_DB);
144
+ let redisUrl = `redis://`;
145
+ if (password)
146
+ redisUrl += `:${password}@`;
147
+ redisUrl += `${host}:${port}`;
148
+ if (database > 0)
149
+ redisUrl += `/${database}`;
150
+ return redisUrl;
151
+ };
152
+ /**
153
+ * Singleton Redis publish client factory
154
+ * Creates and caches a Redis publish client for broadcasting
155
+ */
156
+ export const createRedisPublishClient = async () => {
157
+ // Return cached instance if available
158
+ if (publishClientConnected && publishClientInstance !== null) {
159
+ return publishClientInstance;
160
+ }
161
+ const proxyClient = await tryCreateProxyPublishClient();
162
+ if (proxyClient) {
163
+ return cacheAndReturnClient(proxyClient);
164
+ }
165
+ const url = buildRedisUrl();
166
+ if (url === null)
167
+ throw ErrorFactory.createConfigError('Redis publish client requires REDIS_URL');
168
+ // Try different Redis clients in order of preference
169
+ const redisClient = (await tryCreateRedisClient(url)) || (await tryCreateIoRedisClient(url)) || getFallbackClient();
170
+ return redisClient;
171
+ };
172
+ /**
173
+ * Try to create client using 'redis' package
174
+ */
175
+ const tryCreateRedisClient = async (url) => {
176
+ try {
177
+ const mod = (await import('redis'));
178
+ const client = mod.createClient({ url });
179
+ if (typeof client.connect === 'function') {
180
+ await connectClient(client, 'Redis publish client failed to connect');
181
+ }
182
+ return cacheAndReturnClient(client);
183
+ }
184
+ catch {
185
+ return null;
186
+ }
187
+ };
188
+ /**
189
+ * Try to create client using 'ioredis' package
190
+ */
191
+ const tryCreateIoRedisClient = async (url) => {
192
+ try {
193
+ const mod = (await import('ioredis'));
194
+ const redis = mod.default(url);
195
+ const client = {
196
+ publish: (channel, message) => redis.publish(channel, message),
197
+ connect: redis.connect
198
+ ? async () => {
199
+ const connectFn = redis.connect;
200
+ await connectFn();
201
+ }
202
+ : undefined,
203
+ };
204
+ if (typeof client.connect === 'function') {
205
+ await connectClient(client, 'Redis publish client (ioredis) failed to connect');
206
+ }
207
+ return cacheAndReturnClient(client);
208
+ }
209
+ catch {
210
+ return null;
211
+ }
212
+ };
213
+ /**
214
+ * Get fallback client from global or throw error
215
+ */
216
+ const getFallbackClient = () => {
217
+ const globalFake = globalThis
218
+ .__fakeRedisClient;
219
+ if (globalFake === undefined) {
220
+ throw ErrorFactory.createConfigError("Redis publish client requires the 'redis' or 'ioredis' package (run `zin add broadcast:redis' / `zin plugin install broadcast:redis`, or `npm install redis` / `npm install ioredis`) or a test fake client set in globalThis.__fakeRedisClient");
221
+ }
222
+ return cacheAndReturnClient(globalFake);
223
+ };
224
+ /**
225
+ * Connect to Redis client with error handling
226
+ */
227
+ const connectClient = async (client, errorMessage) => {
228
+ try {
229
+ if (client.connect) {
230
+ await client.connect();
231
+ }
232
+ }
233
+ catch (err) {
234
+ throw ErrorFactory.createTryCatchError(errorMessage, err);
235
+ }
236
+ };
237
+ /**
238
+ * Cache and return the client instance
239
+ */
240
+ const cacheAndReturnClient = (client) => {
241
+ publishClientInstance = client;
242
+ publishClientConnected = true;
243
+ return client;
244
+ };
245
+ /**
246
+ * Reset the singleton publish client (useful for testing)
247
+ */
248
+ export const resetPublishClient = () => {
249
+ publishClientInstance = null;
250
+ publishClientConnected = false;
251
+ };
@@ -1,41 +1,76 @@
1
1
  {
2
2
  "name": "@zintrust/queue-redis",
3
- "version": "0.1.15",
4
- "buildDate": "2026-01-21T12:27:09.660Z",
3
+ "version": "0.1.27",
4
+ "buildDate": "2026-02-15T07:17:27.370Z",
5
5
  "buildEnvironment": {
6
- "node": "v20.19.6",
6
+ "node": "v20.20.0",
7
7
  "platform": "linux",
8
8
  "arch": "x64"
9
9
  },
10
10
  "git": {
11
- "commit": "ae2c0a6",
11
+ "commit": "33b681d",
12
12
  "branch": "master"
13
13
  },
14
14
  "package": {
15
15
  "engines": {
16
16
  "node": ">=20.0.0"
17
17
  },
18
- "dependencies": [],
18
+ "dependencies": [
19
+ "ioredis",
20
+ "redis"
21
+ ],
19
22
  "peerDependencies": [
20
23
  "@zintrust/core"
21
24
  ]
22
25
  },
23
26
  "files": {
27
+ "BullMQRedisQueue.d.ts": {
28
+ "size": 1062,
29
+ "sha256": "52fb0f688cd17cc7d8e8128e60df6f5eea4c803282382ac75550e6aacee7cbb0"
30
+ },
31
+ "BullMQRedisQueue.js": {
32
+ "size": 19557,
33
+ "sha256": "28719e3ec86045a4c837a2d0f698b5b7825e74344e8b65caa4b4d9f4359214ff"
34
+ },
35
+ "HttpQueueDriver.d.ts": {
36
+ "size": 835,
37
+ "sha256": "31735e10a83cf905ca48f3bf1c79f40029b9ac7c0ce6c26751ad646fa77bc764"
38
+ },
39
+ "HttpQueueDriver.js": {
40
+ "size": 7984,
41
+ "sha256": "daec8d2ba999d1f189e446432122a5bcc347bc65c6de9cf5408f35de233c3933"
42
+ },
43
+ "QueueHttpGateway.d.ts": {
44
+ "size": 432,
45
+ "sha256": "16c32e2179c3286569714233a9f84f32a64056593fe502a381dc9bc45e972e3a"
46
+ },
47
+ "QueueHttpGateway.js": {
48
+ "size": 8066,
49
+ "sha256": "ad4b78c5a40221aa6cf1f5959729f7ad9f6b0321c443723fff481c7dbfb91239"
50
+ },
51
+ "RedisPublishClient.d.ts": {
52
+ "size": 451,
53
+ "sha256": "341a68a3b8603b453146dc721f9d2eaf0d65bb6a1acb67908a9c6f9542b9fd2d"
54
+ },
55
+ "RedisPublishClient.js": {
56
+ "size": 8688,
57
+ "sha256": "4252875ce0326c74fb96bd6901e328bff4a4f6cd894681a3378f990a528880ff"
58
+ },
24
59
  "index.d.ts": {
25
- "size": 722,
26
- "sha256": "96a1b122c2fe70231fbc9536e918cdfc43b1972b65645ee54cece16e9239f0ff"
60
+ "size": 565,
61
+ "sha256": "7bc063419989a39530b3a4213627e0aea33fbaacac8c307d9e7e654c99bd5b8d"
27
62
  },
28
63
  "index.js": {
29
- "size": 531,
30
- "sha256": "d719fbd693fa1ab7bad4e1b78f913ff4c4228999b4890e40768270a65cb49984"
64
+ "size": 671,
65
+ "sha256": "43528a869c085982e10a862c71af41a1bbaa22081d2cd88c5781c0306f1b054c"
31
66
  },
32
67
  "register.d.ts": {
33
68
  "size": 169,
34
69
  "sha256": "1ba6de37e0d3276ed9b9e6de02541aab03d3d1b7d3246013af27b66179c38948"
35
70
  },
36
71
  "register.js": {
37
- "size": 556,
38
- "sha256": "6e6e200499a529afcaed74489667d2970717870158e62cc1ae78aea0015b4337"
72
+ "size": 513,
73
+ "sha256": "3c5de15f10641521cac21e0a26112bb7bde615c60652327210e77b5c90307409"
39
74
  }
40
75
  }
41
76
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
- declare const RedisQueue: any;
2
- export { RedisQueue };
1
+ export { BullMQRedisQueue } from './BullMQRedisQueue';
2
+ export { HttpQueueDriver } from './HttpQueueDriver';
3
+ export { QueueHttpGateway } from './QueueHttpGateway';
4
+ export { createRedisPublishClient, resetPublishClient, type RedisPublishClient, } from './RedisPublishClient';
3
5
  export type { QueueMessage } from '@zintrust/core';
4
6
  /**
5
7
  * Package version and build metadata
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
- import { RedisQueue as CoreRedisQueue } from '@zintrust/core';
2
- const RedisQueue = CoreRedisQueue;
3
- export { RedisQueue };
1
+ export { BullMQRedisQueue } from './BullMQRedisQueue';
2
+ export { HttpQueueDriver } from './HttpQueueDriver';
3
+ export { QueueHttpGateway } from './QueueHttpGateway';
4
+ export { createRedisPublishClient, resetPublishClient, } from './RedisPublishClient';
4
5
  /**
5
6
  * Package version and build metadata
6
7
  * Available at runtime for debugging and health checks
package/dist/register.js CHANGED
@@ -1,8 +1,6 @@
1
1
  export async function registerRedisQueueDriver(queue) {
2
- const core = (await importCore());
3
- if (core.RedisQueue === undefined)
4
- return;
5
- queue.register('redis', core.RedisQueue);
2
+ const mod = await import('./BullMQRedisQueue');
3
+ queue.register('redis', mod.default);
6
4
  }
7
5
  const importCore = async () => {
8
6
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zintrust/queue-redis",
3
- "version": "0.1.23",
3
+ "version": "0.1.41",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -22,7 +22,7 @@
22
22
  "node": ">=20.0.0"
23
23
  },
24
24
  "peerDependencies": {
25
- "@zintrust/core": "^0.1.23"
25
+ "@zintrust/core": "^0.1.41"
26
26
  },
27
27
  "publishConfig": {
28
28
  "access": "public"
@@ -30,5 +30,9 @@
30
30
  "scripts": {
31
31
  "build": "tsc -p tsconfig.json",
32
32
  "prepublishOnly": "npm run build"
33
+ },
34
+ "dependencies": {
35
+ "ioredis": "^5.9.2",
36
+ "redis": "^4.7.1"
33
37
  }
34
38
  }