@zintrust/cache-redis 0.4.27 → 0.4.43
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/RedisProxyAdapter.js +17 -111
- package/dist/build-manifest.json +10 -10
- package/dist/index.js +9 -13
- package/package.json +2 -2
|
@@ -1,115 +1,21 @@
|
|
|
1
|
-
import { Env, ErrorFactory,
|
|
2
|
-
const
|
|
3
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
|
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
|
|
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
|
|
31
|
+
await client.set(key, json, 'EX', ttl);
|
|
126
32
|
}
|
|
127
33
|
else {
|
|
128
|
-
await
|
|
34
|
+
await client.set(key, json);
|
|
129
35
|
}
|
|
130
36
|
},
|
|
131
37
|
async delete(key) {
|
|
132
|
-
await
|
|
38
|
+
await client.del(key);
|
|
133
39
|
},
|
|
134
40
|
async clear() {
|
|
135
|
-
await
|
|
41
|
+
await client.flushdb();
|
|
136
42
|
},
|
|
137
43
|
async has(key) {
|
|
138
|
-
const result = await
|
|
139
|
-
return
|
|
44
|
+
const result = await client.exists(key);
|
|
45
|
+
return result > 0;
|
|
140
46
|
},
|
|
141
47
|
};
|
|
142
48
|
},
|
package/dist/build-manifest.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zintrust/cache-redis",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"buildDate": "2026-
|
|
3
|
+
"version": "0.4.43",
|
|
4
|
+
"buildDate": "2026-04-01T18:12:00.772Z",
|
|
5
5
|
"buildEnvironment": {
|
|
6
6
|
"node": "v22.22.1",
|
|
7
7
|
"platform": "darwin",
|
|
8
8
|
"arch": "arm64"
|
|
9
9
|
},
|
|
10
10
|
"git": {
|
|
11
|
-
"commit": "
|
|
12
|
-
"branch": "
|
|
11
|
+
"commit": "57e4d1b5",
|
|
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":
|
|
32
|
-
"sha256": "
|
|
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":
|
|
44
|
-
"sha256": "
|
|
43
|
+
"size": 1701,
|
|
44
|
+
"sha256": "e4b4002108e2ada6e393d6716d6eab75e1b98b82eee0a9c3e5c1402f7f23882e"
|
|
45
45
|
},
|
|
46
46
|
"index.d.ts": {
|
|
47
47
|
"size": 842,
|
|
48
48
|
"sha256": "b47614dfdb736524165785958f4a685818c055315961c2e2b48d99ef6ca491e2"
|
|
49
49
|
},
|
|
50
50
|
"index.js": {
|
|
51
|
-
"size":
|
|
52
|
-
"sha256": "
|
|
51
|
+
"size": 5371,
|
|
52
|
+
"sha256": "91cf637340e283c2c90e4cc11d2be1aa1ee2a009df282356bda4db9c7e57e54a"
|
|
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
|
-
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
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
|
|
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.
|
|
3
|
+
"version": "0.4.43",
|
|
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.
|
|
26
|
+
"@zintrust/core": "^0.4.43"
|
|
27
27
|
},
|
|
28
28
|
"publishConfig": {
|
|
29
29
|
"access": "public"
|