@zintrust/cache-redis 0.4.27 → 0.4.36

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.
@@ -1,115 +1,21 @@
1
- import { Env, ErrorFactory, Logger, SignedRequest } 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
- const resolveBaseUrl = () => {
34
- const explicit = Env.get('REDIS_PROXY_URL', '').trim();
35
- if (explicit !== '')
36
- return explicit;
37
- const host = Env.get('REDIS_PROXY_HOST', '127.0.0.1');
38
- const port = Env.getInt('REDIS_PROXY_PORT', 8791);
39
- return `http://${host}:${port}`;
40
- };
41
- const buildProxySettings = () => {
42
- const baseUrl = resolveBaseUrl();
43
- const rawKeyId = Env.get('REDIS_PROXY_KEY_ID', '').trim();
44
- const fallbackKeyId = (Env.APP_NAME ?? 'zintrust').trim().toLowerCase().replaceAll(/\s+/g, '_');
45
- const keyId = (rawKeyId === '' ? fallbackKeyId : rawKeyId) || undefined;
46
- const secret = Env.get('REDIS_PROXY_SECRET', '') || Env.APP_KEY || undefined;
47
- const timeoutMs = Env.getInt('REDIS_PROXY_TIMEOUT_MS', Env.ZT_PROXY_TIMEOUT_MS);
48
- return { baseUrl, keyId, secret, timeoutMs };
49
- };
50
- const buildHeaders = async (settings, requestUrl, body) => {
51
- const headers = {
52
- 'Content-Type': 'application/json',
53
- };
54
- if (settings.keyId && settings.secret) {
55
- const signingUrl = buildSigningUrl(requestUrl, settings.baseUrl);
56
- const signed = await SignedRequest.createHeaders({
57
- method: 'POST',
58
- url: signingUrl,
59
- body,
60
- keyId: settings.keyId,
61
- secret: settings.secret,
62
- });
63
- Object.assign(headers, signed);
64
- }
65
- else {
66
- Logger.warn('[redis-proxy] Proxy signing disabled; sending unsigned request.');
67
- }
68
- return headers;
69
- };
70
- const requestProxy = async (settings, path, payload) => {
71
- if (settings.baseUrl.trim() === '') {
1
+ import { Env, ErrorFactory, createRedisConnection } from '@zintrust/core';
2
+ const createProxyClient = () => {
3
+ if (Env.REDIS_PROXY_URL.trim() === '' && Env.USE_REDIS_PROXY !== true) {
72
4
  throw ErrorFactory.createConfigError('Redis proxy URL is missing (REDIS_PROXY_URL)');
73
5
  }
74
- const body = JSON.stringify(payload);
75
- const url = buildRequestUrl(settings.baseUrl, path);
76
- const headers = await buildHeaders(settings, url, body);
77
- const timeoutSignal = typeof AbortSignal !== 'undefined' && 'timeout' in AbortSignal;
78
- const signal = timeoutSignal ? AbortSignal.timeout(settings.timeoutMs) : undefined;
79
- const response = await fetch(url.toString(), {
80
- method: 'POST',
81
- headers,
82
- body,
83
- signal,
84
- });
85
- if (!response.ok) {
86
- const text = await response.text();
87
- throw ErrorFactory.createTryCatchError(`Redis proxy request failed (${response.status})`, text);
88
- }
89
- return (await response.json());
90
- };
91
- const toNumber = (value) => {
92
- if (typeof value === 'number')
93
- return value;
94
- if (typeof value === 'string') {
95
- const parsed = Number(value);
96
- return Number.isFinite(parsed) ? parsed : 0;
97
- }
98
- return 0;
6
+ return createRedisConnection({
7
+ host: Env.get('REDIS_HOST', 'localhost'),
8
+ port: Env.getInt('REDIS_PORT', 6379),
9
+ password: Env.get('REDIS_PASSWORD'),
10
+ db: Env.getInt('REDIS_DB', 0),
11
+ }, 3, { subsystem: 'cache' });
99
12
  };
100
13
  export const RedisProxyAdapter = Object.freeze({
101
14
  create() {
102
- const settings = buildProxySettings();
103
- const sendCommand = async (command, args) => {
104
- const response = await requestProxy(settings, '/zin/redis/command', {
105
- command,
106
- args,
107
- });
108
- return response.result;
109
- };
15
+ const client = createProxyClient();
110
16
  return {
111
17
  async get(key) {
112
- const result = await sendCommand('GET', [key]);
18
+ const result = await client.get(key);
113
19
  if (result === null)
114
20
  return null;
115
21
  try {
@@ -122,21 +28,21 @@ export const RedisProxyAdapter = Object.freeze({
122
28
  async set(key, value, ttl) {
123
29
  const json = JSON.stringify(value);
124
30
  if (Number.isFinite(ttl) && (ttl ?? 0) > 0) {
125
- await sendCommand('SET', [key, json, 'EX', ttl]);
31
+ await client.set(key, json, 'EX', ttl);
126
32
  }
127
33
  else {
128
- await sendCommand('SET', [key, json]);
34
+ await client.set(key, json);
129
35
  }
130
36
  },
131
37
  async delete(key) {
132
- await sendCommand('DEL', [key]);
38
+ await client.del(key);
133
39
  },
134
40
  async clear() {
135
- await sendCommand('FLUSHDB', []);
41
+ await client.flushdb();
136
42
  },
137
43
  async has(key) {
138
- const result = await sendCommand('EXISTS', [key]);
139
- return toNumber(result) > 0;
44
+ const result = await client.exists(key);
45
+ return result > 0;
140
46
  },
141
47
  };
142
48
  },
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@zintrust/cache-redis",
3
- "version": "0.1.27",
4
- "buildDate": "2026-03-26T14:36:58.621Z",
3
+ "version": "0.4.36",
4
+ "buildDate": "2026-03-30T17:19:32.841Z",
5
5
  "buildEnvironment": {
6
6
  "node": "v22.22.1",
7
7
  "platform": "darwin",
8
8
  "arch": "arm64"
9
9
  },
10
10
  "git": {
11
- "commit": "94caf69e",
12
- "branch": "dev"
11
+ "commit": "a45b4628",
12
+ "branch": "release"
13
13
  },
14
14
  "package": {
15
15
  "engines": {
@@ -28,8 +28,8 @@
28
28
  "sha256": "91df2fbb1916b073c3bafa1dc253ab5ee8998cc924c3cd52308c09798c1d829f"
29
29
  },
30
30
  "RedisProxyAdapter.js": {
31
- "size": 5296,
32
- "sha256": "a58538d3284829c8d0f3b2a7ed48e7db65098c8047f9f84c1015a22944df0100"
31
+ "size": 1693,
32
+ "sha256": "b1cd4417d827598b41226597355559eef423eac869448df5fd94d338d16a09f2"
33
33
  },
34
34
  "RedisWorkersDurableObjectAdapter.d.ts": {
35
35
  "size": 193,
@@ -40,16 +40,16 @@
40
40
  "sha256": "5b0ae2883872ce974ecaf01f07d35b72e6da468439912a57734ccd5222705bfd"
41
41
  },
42
42
  "build-manifest.json": {
43
- "size": 1698,
44
- "sha256": "612dcaf3b1a76efa9ee3060d3abcea12fffb9cd281fd612fac0ef0bf3cb1df00"
43
+ "size": 1697,
44
+ "sha256": "b8d946fa772ad27352ff1fbcfb74a9d0aef873f99ab3a90458fb8362ff3922ed"
45
45
  },
46
46
  "index.d.ts": {
47
47
  "size": 842,
48
48
  "sha256": "b47614dfdb736524165785958f4a685818c055315961c2e2b48d99ef6ca491e2"
49
49
  },
50
50
  "index.js": {
51
- "size": 5428,
52
- "sha256": "985f446856ed668c7b18287acd14b24f1b73e39fd3560e9aa7a913d7aba8686f"
51
+ "size": 5371,
52
+ "sha256": "c3119fa9c36f3aa2c8d9e205ea12b69c16571fc3d9e1000057b4a6f292d3c621"
53
53
  },
54
54
  "register.d.ts": {
55
55
  "size": 184,
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Cloudflare, Env, ErrorFactory, Logger, createRedisConnection } from '@zintrust/core';
2
- import { RedisProxyAdapter } from './RedisProxyAdapter.js';
2
+ const createSharedRedisConnection = createRedisConnection;
3
3
  const safeJsonParse = (value) => {
4
4
  try {
5
5
  return JSON.parse(value);
@@ -51,14 +51,12 @@ const createWorkersCacheDriver = (config) => {
51
51
  let client;
52
52
  let connected = false;
53
53
  const ensureClient = async () => {
54
- if (client === undefined) {
55
- client = createRedisConnection({
56
- host: config.host,
57
- port: config.port,
58
- password: config.password,
59
- db: config.database ?? 0,
60
- });
61
- }
54
+ client ??= createSharedRedisConnection({
55
+ host: config.host,
56
+ port: config.port,
57
+ password: config.password,
58
+ db: config.database ?? 0,
59
+ }, 3, { subsystem: 'cache' });
62
60
  if (!connected && typeof client.connect === 'function') {
63
61
  await client.connect();
64
62
  connected = true;
@@ -127,15 +125,13 @@ const createNodeCacheDriver = (config) => {
127
125
  }, config.ttl ?? 300);
128
126
  };
129
127
  const shouldUseProxy = () => {
130
- if (Env.REDIS_PROXY_URL.trim() !== '')
131
- return true;
132
- return Env.USE_REDIS_PROXY === true;
128
+ return Env.REDIS_PROXY_URL.trim() !== '' || Env.USE_REDIS_PROXY === true;
133
129
  };
134
130
  export const RedisCacheDriver = Object.freeze({
135
131
  create(config) {
136
132
  const isWorkers = Cloudflare.getWorkersEnv() !== null;
137
133
  if (shouldUseProxy()) {
138
- return RedisProxyAdapter.create();
134
+ return createWorkersCacheDriver(config);
139
135
  }
140
136
  if (isWorkers && Cloudflare.isCloudflareSocketsEnabled() === false) {
141
137
  throw ErrorFactory.createConfigError('Redis cache driver requires ENABLE_CLOUDFLARE_SOCKETS=true in Cloudflare Workers.');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zintrust/cache-redis",
3
- "version": "0.4.27",
3
+ "version": "0.4.36",
4
4
  "description": "Redis cache driver for ZinTrust.",
5
5
  "private": false,
6
6
  "type": "module",
@@ -23,7 +23,7 @@
23
23
  "node": ">=20.0.0"
24
24
  },
25
25
  "peerDependencies": {
26
- "@zintrust/core": "^0.4.27"
26
+ "@zintrust/core": "^0.4.36"
27
27
  },
28
28
  "publishConfig": {
29
29
  "access": "public"