@zintrust/cache-redis 0.4.0 → 0.4.27

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,5 @@
1
+ import type { CacheDriver } from './index.js';
2
+ export declare const RedisWorkersDurableObjectAdapter: Readonly<{
3
+ create(): CacheDriver;
4
+ }>;
5
+ export default RedisWorkersDurableObjectAdapter;
@@ -0,0 +1,129 @@
1
+ import { ErrorFactory, Logger } from '@zintrust/core';
2
+ const createSendCommandFunction = (getStub, connect) => {
3
+ return async (command, args) => {
4
+ await connect();
5
+ const stub = getStub();
6
+ const executePath = 'http://do/execute'; //NOSONAR
7
+ const payload = JSON.stringify({
8
+ command,
9
+ params: args,
10
+ });
11
+ const response = await stub.fetch(executePath, {
12
+ method: 'POST',
13
+ headers: { 'Content-Type': 'application/json' },
14
+ body: payload,
15
+ });
16
+ if (!response.ok) {
17
+ const text = await response.text();
18
+ let errDetail;
19
+ try {
20
+ errDetail = JSON.parse(text);
21
+ }
22
+ catch {
23
+ errDetail = { error: text };
24
+ }
25
+ const msg = errDetail.error ||
26
+ errDetail.message ||
27
+ response.statusText;
28
+ throw ErrorFactory.createGeneralError(`DO Command Failed: ${msg}`);
29
+ }
30
+ const json = (await response.json());
31
+ if (json !== null && typeof json === 'object' && 'result' in json) {
32
+ return json.result;
33
+ }
34
+ return json;
35
+ };
36
+ };
37
+ const createConnectionManager = (getNamespace) => {
38
+ let connected = false;
39
+ const getStub = () => {
40
+ const namespace = getNamespace();
41
+ if (!namespace) {
42
+ throw ErrorFactory.createConfigError('REDIS_POOL binding not found. Cannot connect to Durable Object pool.');
43
+ }
44
+ const id = namespace.idFromName('default');
45
+ return namespace.get(id);
46
+ };
47
+ const connect = async () => {
48
+ if (connected)
49
+ return;
50
+ try {
51
+ const stub = getStub();
52
+ const health = 'http://do/health'; //NOSONAR
53
+ const res = await stub.fetch(health, {
54
+ method: 'POST',
55
+ });
56
+ if (!res.ok) {
57
+ throw ErrorFactory.createGeneralError(`DO health check failed: ${res.status}`);
58
+ }
59
+ const body = (await res.json());
60
+ if (!body.connected) {
61
+ Logger.info('[RedisWorkersDurableObjectAdapter] DO not connected yet, will init on first command');
62
+ }
63
+ connected = true;
64
+ }
65
+ catch (err) {
66
+ Logger.error('[RedisWorkersDurableObjectAdapter] Connection failed', err);
67
+ throw ErrorFactory.createGeneralError('Failed to connect to Redis DO', err);
68
+ }
69
+ };
70
+ const sendCommand = createSendCommandFunction(getStub, connect);
71
+ return {
72
+ connect,
73
+ sendCommand,
74
+ disconnect: () => {
75
+ connected = false;
76
+ },
77
+ isConnected: () => connected,
78
+ };
79
+ };
80
+ const toNumber = (value) => {
81
+ if (typeof value === 'number')
82
+ return value;
83
+ if (typeof value === 'string') {
84
+ const parsed = Number(value);
85
+ return Number.isFinite(parsed) ? parsed : 0;
86
+ }
87
+ return 0;
88
+ };
89
+ export const RedisWorkersDurableObjectAdapter = Object.freeze({
90
+ create() {
91
+ const connectionManager = createConnectionManager(() => {
92
+ const globalEnv = globalThis.env;
93
+ return globalEnv?.['REDIS_POOL'];
94
+ });
95
+ return {
96
+ async get(key) {
97
+ const raw = await connectionManager.sendCommand('GET', [key]);
98
+ if (raw === null || raw === undefined)
99
+ return null;
100
+ try {
101
+ return JSON.parse(String(raw));
102
+ }
103
+ catch {
104
+ return null;
105
+ }
106
+ },
107
+ async set(key, value, ttl) {
108
+ const json = JSON.stringify(value);
109
+ if (Number.isFinite(ttl) && (ttl ?? 0) > 0) {
110
+ await connectionManager.sendCommand('SET', [key, json, 'EX', ttl]);
111
+ }
112
+ else {
113
+ await connectionManager.sendCommand('SET', [key, json]);
114
+ }
115
+ },
116
+ async delete(key) {
117
+ await connectionManager.sendCommand('DEL', [key]);
118
+ },
119
+ async clear() {
120
+ await connectionManager.sendCommand('FLUSHDB', []);
121
+ },
122
+ async has(key) {
123
+ const count = await connectionManager.sendCommand('EXISTS', [key]);
124
+ return toNumber(count) > 0;
125
+ },
126
+ };
127
+ },
128
+ });
129
+ export default RedisWorkersDurableObjectAdapter;
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@zintrust/cache-redis",
3
+ "version": "0.1.27",
4
+ "buildDate": "2026-03-26T14:36:58.621Z",
5
+ "buildEnvironment": {
6
+ "node": "v22.22.1",
7
+ "platform": "darwin",
8
+ "arch": "arm64"
9
+ },
10
+ "git": {
11
+ "commit": "94caf69e",
12
+ "branch": "dev"
13
+ },
14
+ "package": {
15
+ "engines": {
16
+ "node": ">=20.0.0"
17
+ },
18
+ "dependencies": [
19
+ "redis"
20
+ ],
21
+ "peerDependencies": [
22
+ "@zintrust/core"
23
+ ]
24
+ },
25
+ "files": {
26
+ "RedisProxyAdapter.d.ts": {
27
+ "size": 163,
28
+ "sha256": "91df2fbb1916b073c3bafa1dc253ab5ee8998cc924c3cd52308c09798c1d829f"
29
+ },
30
+ "RedisProxyAdapter.js": {
31
+ "size": 5296,
32
+ "sha256": "a58538d3284829c8d0f3b2a7ed48e7db65098c8047f9f84c1015a22944df0100"
33
+ },
34
+ "RedisWorkersDurableObjectAdapter.d.ts": {
35
+ "size": 193,
36
+ "sha256": "859d3177d302c65f93add57216eae91d8c45874801bb75bdea3d6f6b00fb00c3"
37
+ },
38
+ "RedisWorkersDurableObjectAdapter.js": {
39
+ "size": 4482,
40
+ "sha256": "5b0ae2883872ce974ecaf01f07d35b72e6da468439912a57734ccd5222705bfd"
41
+ },
42
+ "build-manifest.json": {
43
+ "size": 1698,
44
+ "sha256": "612dcaf3b1a76efa9ee3060d3abcea12fffb9cd281fd612fac0ef0bf3cb1df00"
45
+ },
46
+ "index.d.ts": {
47
+ "size": 842,
48
+ "sha256": "b47614dfdb736524165785958f4a685818c055315961c2e2b48d99ef6ca491e2"
49
+ },
50
+ "index.js": {
51
+ "size": 5428,
52
+ "sha256": "985f446856ed668c7b18287acd14b24f1b73e39fd3560e9aa7a913d7aba8686f"
53
+ },
54
+ "register.d.ts": {
55
+ "size": 184,
56
+ "sha256": "005e0163b4f5833a0096cfcba7d5851b6f21c0fea7d8faf01a913dc2808730ad"
57
+ },
58
+ "register.js": {
59
+ "size": 330,
60
+ "sha256": "9fcab6dfb78323d351970dc89dc713910339fc7b1b392225213a93fbeee7c511"
61
+ }
62
+ }
63
+ }
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@zintrust/cache-redis",
3
- "version": "0.4.0",
3
+ "version": "0.4.27",
4
+ "description": "Redis cache driver for ZinTrust.",
4
5
  "private": false,
5
6
  "type": "module",
6
7
  "main": "./dist/index.js",
@@ -22,11 +23,18 @@
22
23
  "node": ">=20.0.0"
23
24
  },
24
25
  "peerDependencies": {
25
- "@zintrust/core": "^0.4.0"
26
+ "@zintrust/core": "^0.4.27"
26
27
  },
27
28
  "publishConfig": {
28
29
  "access": "public"
29
30
  },
31
+ "keywords": [
32
+ "zintrust",
33
+ "cache",
34
+ "redis",
35
+ "adapter",
36
+ "typescript"
37
+ ],
30
38
  "dependencies": {
31
39
  "redis": "^5.11.0"
32
40
  },