@bridgebase/redis 0.2.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.
package/README.md ADDED
@@ -0,0 +1,129 @@
1
+ # @bridgebase/redis
2
+
3
+ Redis adapter for BridgeBase - Native Redis client with gateway integration.
4
+
5
+ ## Features
6
+
7
+ - **Minimal Abstraction** - Direct pass-through to native Redis client
8
+ - **Gateway Integration** - Automatic proxy tunnel setup via BridgeBase
9
+ - **Full Redis Support** - Access to all Redis commands and options
10
+ - **Connection Pooling** - Leverage Redis client connection management
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install @bridgebase/redis redis
16
+ ```
17
+
18
+ ## Quick Start
19
+
20
+ ```typescript
21
+ import { connectRedis } from "@bridgebase/redis";
22
+
23
+ const session = await connectRedis("your-jwt-token", { db: 0 });
24
+ const redis = session.client; // Access native Redis client
25
+
26
+ // Use native Redis client
27
+ await redis.set("key", "value");
28
+ const value = await redis.get("key");
29
+ console.log(value);
30
+
31
+ await session.disconnect();
32
+ ```
33
+
34
+ ## Factory Functions
35
+
36
+ ### connectRedis()
37
+
38
+ Creates a session and automatically connects:
39
+
40
+ ```typescript
41
+ const session = await connectRedis(jwtToken, {
42
+ db?: number; // Redis database number (default: 0)
43
+ apiBaseUrl?: string; // BridgeBase API endpoint
44
+ });
45
+
46
+ const redis = session.client; // Native Redis client
47
+ ```
48
+
49
+ ### redis()
50
+
51
+ Alias for `connectRedis()`:
52
+
53
+ ```typescript
54
+ const session = await redis(jwtToken, options);
55
+ ```
56
+
57
+ ### createRedisSession()
58
+
59
+ Creates a session without auto-connecting:
60
+
61
+ ```typescript
62
+ const session = createRedisSession(jwtToken, options);
63
+ const redis = await session.connect(); // Manual connect
64
+ ```
65
+
66
+ ## Using Native Redis Client
67
+
68
+ Once connected, you have full access to the native Redis client:
69
+
70
+ ```typescript
71
+ import { connectRedis } from "@bridgebase/redis";
72
+
73
+ const session = await connectRedis(token);
74
+ const redis = session.client;
75
+
76
+ // All native Redis commands available
77
+ await redis.set("key", "value");
78
+ await redis.incr("counter");
79
+ await redis.hincrby("hash", "field", 1);
80
+
81
+ // Transactions
82
+ const result = await redis
83
+ .multi()
84
+ .set("key1", "value1")
85
+ .set("key2", "value2")
86
+ .exec();
87
+
88
+ // Subscriptions
89
+ const subscriber = redis.duplicate();
90
+ await subscriber.subscribe("channel", (message) => {
91
+ console.log(message);
92
+ });
93
+ ```
94
+
95
+ ## Error Handling
96
+
97
+ ```typescript
98
+ import { ConnectionError, GatewayResolutionError } from "@bridgebase/redis";
99
+
100
+ try {
101
+ const session = await connectRedis(token);
102
+ const redis = session.client;
103
+ await redis.set("key", "value");
104
+ } catch (error) {
105
+ if (error instanceof GatewayResolutionError) {
106
+ console.error("Failed to resolve gateway");
107
+ } else if (error instanceof ConnectionError) {
108
+ console.error("Failed to connect to Redis");
109
+ }
110
+ } finally {
111
+ await session?.disconnect();
112
+ }
113
+ ```
114
+
115
+ ## Cleanup
116
+
117
+ ```typescript
118
+ const session = await connectRedis(token);
119
+ try {
120
+ const redis = session.client;
121
+ // Use the client
122
+ } finally {
123
+ await session.disconnect();
124
+ }
125
+ ```
126
+
127
+ ## License
128
+
129
+ MIT
@@ -0,0 +1,64 @@
1
+ import { BaseSession, SessionConfig, DatabaseCredentials } from '@bridgebase/core';
2
+ export { AuthError, BridgeBaseError, ConnectionError, CredentialError, DatabaseCredentials, GatewayError, GatewayResolutionError, ProxyError, SessionConfig } from '@bridgebase/core';
3
+ import { RedisClientType } from 'redis';
4
+ export { RedisClientType } from 'redis';
5
+
6
+ interface RedisSessionConfig extends SessionConfig {
7
+ db?: number;
8
+ }
9
+ /**
10
+ * Session adapter for Redis database
11
+ * Provides full access to node-redis client methods
12
+ */
13
+ declare class RedisSession extends BaseSession<RedisClientType> {
14
+ private db;
15
+ constructor(config: RedisSessionConfig);
16
+ /**
17
+ * Access native Redis client
18
+ * Must call connect() first
19
+ *
20
+ * All redis (node-redis) client methods are available:
21
+ * - Basic commands: get, set, del, exists, etc.
22
+ * - Hashes: hSet, hGet, hGetAll, hDel, etc.
23
+ * - Lists: lPush, rPush, lPop, rPop, lRange, etc.
24
+ * - Sets: sAdd, sRem, sMembers, etc.
25
+ * - Sorted Sets: zAdd, zRem, zRange, zScore, etc.
26
+ * - Pub/Sub: publish, subscribe, unsubscribe, etc.
27
+ * - Transactions: multi, exec, etc.
28
+ * - And many more...
29
+ */
30
+ get client(): RedisClientType;
31
+ protected connectNative(credentials: DatabaseCredentials | null, proxyPort: number): Promise<RedisClientType>;
32
+ protected closeNative(nativeClient: RedisClientType): Promise<void>;
33
+ protected get requiresCredentials(): boolean;
34
+ protected get dbLabel(): string;
35
+ }
36
+
37
+ /**
38
+ * @bridgebase/redis - Redis adapter with BridgeBase gateway integration
39
+ */
40
+
41
+ interface RedisConnection {
42
+ client: RedisClientType;
43
+ disconnect: () => Promise<void>;
44
+ }
45
+ /**
46
+ * Convenience factory function for Redis
47
+ * Creates and connects a session in one call
48
+ */
49
+ declare function connectRedis(jwtToken: string, options?: Partial<RedisSessionConfig>): Promise<RedisConnection>;
50
+ /**
51
+ * Alias for connectRedis
52
+ */
53
+ declare function redis(jwtToken: string, options?: Partial<RedisSessionConfig>): Promise<RedisConnection>;
54
+ /**
55
+ * Factory function to create a session without auto-connecting
56
+ */
57
+ declare function createRedisSession(jwtToken: string, options?: Partial<RedisSessionConfig>): RedisSession;
58
+
59
+ /**
60
+ * SDK version
61
+ */
62
+ declare const version = "0.2.0";
63
+
64
+ export { type RedisConnection, RedisSession, type RedisSessionConfig, connectRedis, createRedisSession, redis, version };
@@ -0,0 +1,64 @@
1
+ import { BaseSession, SessionConfig, DatabaseCredentials } from '@bridgebase/core';
2
+ export { AuthError, BridgeBaseError, ConnectionError, CredentialError, DatabaseCredentials, GatewayError, GatewayResolutionError, ProxyError, SessionConfig } from '@bridgebase/core';
3
+ import { RedisClientType } from 'redis';
4
+ export { RedisClientType } from 'redis';
5
+
6
+ interface RedisSessionConfig extends SessionConfig {
7
+ db?: number;
8
+ }
9
+ /**
10
+ * Session adapter for Redis database
11
+ * Provides full access to node-redis client methods
12
+ */
13
+ declare class RedisSession extends BaseSession<RedisClientType> {
14
+ private db;
15
+ constructor(config: RedisSessionConfig);
16
+ /**
17
+ * Access native Redis client
18
+ * Must call connect() first
19
+ *
20
+ * All redis (node-redis) client methods are available:
21
+ * - Basic commands: get, set, del, exists, etc.
22
+ * - Hashes: hSet, hGet, hGetAll, hDel, etc.
23
+ * - Lists: lPush, rPush, lPop, rPop, lRange, etc.
24
+ * - Sets: sAdd, sRem, sMembers, etc.
25
+ * - Sorted Sets: zAdd, zRem, zRange, zScore, etc.
26
+ * - Pub/Sub: publish, subscribe, unsubscribe, etc.
27
+ * - Transactions: multi, exec, etc.
28
+ * - And many more...
29
+ */
30
+ get client(): RedisClientType;
31
+ protected connectNative(credentials: DatabaseCredentials | null, proxyPort: number): Promise<RedisClientType>;
32
+ protected closeNative(nativeClient: RedisClientType): Promise<void>;
33
+ protected get requiresCredentials(): boolean;
34
+ protected get dbLabel(): string;
35
+ }
36
+
37
+ /**
38
+ * @bridgebase/redis - Redis adapter with BridgeBase gateway integration
39
+ */
40
+
41
+ interface RedisConnection {
42
+ client: RedisClientType;
43
+ disconnect: () => Promise<void>;
44
+ }
45
+ /**
46
+ * Convenience factory function for Redis
47
+ * Creates and connects a session in one call
48
+ */
49
+ declare function connectRedis(jwtToken: string, options?: Partial<RedisSessionConfig>): Promise<RedisConnection>;
50
+ /**
51
+ * Alias for connectRedis
52
+ */
53
+ declare function redis(jwtToken: string, options?: Partial<RedisSessionConfig>): Promise<RedisConnection>;
54
+ /**
55
+ * Factory function to create a session without auto-connecting
56
+ */
57
+ declare function createRedisSession(jwtToken: string, options?: Partial<RedisSessionConfig>): RedisSession;
58
+
59
+ /**
60
+ * SDK version
61
+ */
62
+ declare const version = "0.2.0";
63
+
64
+ export { type RedisConnection, RedisSession, type RedisSessionConfig, connectRedis, createRedisSession, redis, version };
package/dist/index.js ADDED
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ AuthError: () => import_core2.AuthError,
24
+ BridgeBaseError: () => import_core2.BridgeBaseError,
25
+ ConnectionError: () => import_core2.ConnectionError,
26
+ CredentialError: () => import_core2.CredentialError,
27
+ GatewayError: () => import_core2.GatewayError,
28
+ GatewayResolutionError: () => import_core2.GatewayResolutionError,
29
+ ProxyError: () => import_core2.ProxyError,
30
+ RedisSession: () => RedisSession,
31
+ connectRedis: () => connectRedis,
32
+ createRedisSession: () => createRedisSession,
33
+ redis: () => redis,
34
+ version: () => version
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+
38
+ // src/sessions/RedisSession.ts
39
+ var import_core = require("@bridgebase/core");
40
+ var RedisSession = class extends import_core.BaseSession {
41
+ constructor(config) {
42
+ super(config);
43
+ this.db = config.db ?? 0;
44
+ this.dbType = "redis";
45
+ }
46
+ /**
47
+ * Access native Redis client
48
+ * Must call connect() first
49
+ *
50
+ * All redis (node-redis) client methods are available:
51
+ * - Basic commands: get, set, del, exists, etc.
52
+ * - Hashes: hSet, hGet, hGetAll, hDel, etc.
53
+ * - Lists: lPush, rPush, lPop, rPop, lRange, etc.
54
+ * - Sets: sAdd, sRem, sMembers, etc.
55
+ * - Sorted Sets: zAdd, zRem, zRange, zScore, etc.
56
+ * - Pub/Sub: publish, subscribe, unsubscribe, etc.
57
+ * - Transactions: multi, exec, etc.
58
+ * - And many more...
59
+ */
60
+ get client() {
61
+ if (!this.nativeClient) {
62
+ throw new import_core.ConnectionError(
63
+ "Session not connected. Call connect() first.",
64
+ "SESSION_NOT_CONNECTED"
65
+ );
66
+ }
67
+ return this.nativeClient;
68
+ }
69
+ async connectNative(credentials, proxyPort) {
70
+ try {
71
+ const redis2 = require("redis");
72
+ const client = redis2.createClient({
73
+ socket: {
74
+ host: "127.0.0.1",
75
+ port: proxyPort
76
+ },
77
+ database: this.db
78
+ });
79
+ await client.connect();
80
+ return client;
81
+ } catch (error) {
82
+ const message = error instanceof Error ? error.message : String(error);
83
+ throw new import_core.ConnectionError(
84
+ `Failed to connect to Redis: ${message}`,
85
+ "REDIS_CONNECT_FAILED",
86
+ { originalError: error }
87
+ );
88
+ }
89
+ }
90
+ async closeNative(nativeClient) {
91
+ if (nativeClient && typeof nativeClient.quit === "function") {
92
+ await nativeClient.quit();
93
+ } else if (nativeClient && typeof nativeClient.disconnect === "function") {
94
+ await nativeClient.disconnect();
95
+ }
96
+ }
97
+ get requiresCredentials() {
98
+ return false;
99
+ }
100
+ get dbLabel() {
101
+ return "Redis";
102
+ }
103
+ };
104
+
105
+ // src/index.ts
106
+ var import_core2 = require("@bridgebase/core");
107
+ async function connectRedis(jwtToken, options) {
108
+ const session = new RedisSession({
109
+ jwtToken,
110
+ ...options
111
+ });
112
+ await session.connect();
113
+ return {
114
+ client: session.client,
115
+ disconnect: () => session.disconnect()
116
+ };
117
+ }
118
+ async function redis(jwtToken, options) {
119
+ return connectRedis(jwtToken, options);
120
+ }
121
+ function createRedisSession(jwtToken, options) {
122
+ return new RedisSession({
123
+ jwtToken,
124
+ ...options
125
+ });
126
+ }
127
+ var version = "0.2.0";
128
+ // Annotate the CommonJS export names for ESM import in node:
129
+ 0 && (module.exports = {
130
+ AuthError,
131
+ BridgeBaseError,
132
+ ConnectionError,
133
+ CredentialError,
134
+ GatewayError,
135
+ GatewayResolutionError,
136
+ ProxyError,
137
+ RedisSession,
138
+ connectRedis,
139
+ createRedisSession,
140
+ redis,
141
+ version
142
+ });
143
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/sessions/RedisSession.ts"],"sourcesContent":["/**\n * @bridgebase/redis - Redis adapter with BridgeBase gateway integration\n */\n\nimport { RedisSession, RedisSessionConfig } from \"./sessions/RedisSession\";\nimport type { RedisClientType } from \"redis\";\n\nexport interface RedisConnection {\n client: RedisClientType;\n disconnect: () => Promise<void>;\n}\n\n/**\n * Convenience factory function for Redis\n * Creates and connects a session in one call\n */\nexport async function connectRedis(\n jwtToken: string,\n options?: Partial<RedisSessionConfig>\n): Promise<RedisConnection> {\n const session = new RedisSession({\n jwtToken,\n ...options,\n });\n await session.connect();\n return {\n client: session.client,\n disconnect: () => session.disconnect(),\n };\n}\n\n/**\n * Alias for connectRedis\n */\nexport async function redis(\n jwtToken: string,\n options?: Partial<RedisSessionConfig>\n): Promise<RedisConnection> {\n return connectRedis(jwtToken, options);\n}\n\n/**\n * Factory function to create a session without auto-connecting\n */\nexport function createRedisSession(\n jwtToken: string,\n options?: Partial<RedisSessionConfig>\n): RedisSession {\n return new RedisSession({\n jwtToken,\n ...options,\n });\n}\n\n// Session exports\nexport { RedisSession };\nexport type { RedisSessionConfig } from \"./sessions/RedisSession\";\n\n// Re-export commonly used Redis types for convenience\nexport type { RedisClientType } from \"redis\";\n\n// Re-export core types and exceptions\nexport {\n BridgeBaseError,\n AuthError,\n GatewayError,\n GatewayResolutionError,\n ConnectionError,\n CredentialError,\n ProxyError,\n} from \"@bridgebase/core\";\n\nexport type { SessionConfig, DatabaseCredentials } from \"@bridgebase/core\";\n\n/**\n * SDK version\n */\nexport const version = \"0.2.0\";\n","import { BaseSession, DatabaseCredentials, SessionConfig, ConnectionError } from \"@bridgebase/core\";\nimport type { RedisClientType } from \"redis\";\n\nexport interface RedisSessionConfig extends SessionConfig {\n db?: number;\n}\n\n/**\n * Session adapter for Redis database\n * Provides full access to node-redis client methods\n */\nexport class RedisSession extends BaseSession<RedisClientType> {\n private db: number;\n\n constructor(config: RedisSessionConfig) {\n super(config);\n this.db = config.db ?? 0;\n this.dbType = \"redis\";\n }\n\n /**\n * Access native Redis client\n * Must call connect() first\n * \n * All redis (node-redis) client methods are available:\n * - Basic commands: get, set, del, exists, etc.\n * - Hashes: hSet, hGet, hGetAll, hDel, etc.\n * - Lists: lPush, rPush, lPop, rPop, lRange, etc.\n * - Sets: sAdd, sRem, sMembers, etc.\n * - Sorted Sets: zAdd, zRem, zRange, zScore, etc.\n * - Pub/Sub: publish, subscribe, unsubscribe, etc.\n * - Transactions: multi, exec, etc.\n * - And many more...\n */\n get client(): RedisClientType {\n if (!this.nativeClient) {\n throw new ConnectionError(\n \"Session not connected. Call connect() first.\",\n \"SESSION_NOT_CONNECTED\"\n );\n }\n return this.nativeClient;\n }\n\n protected async connectNative(\n credentials: DatabaseCredentials | null,\n proxyPort: number,\n ): Promise<RedisClientType> {\n try {\n // Lazy import redis\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const redis = require(\"redis\");\n\n const client = redis.createClient({\n socket: {\n host: \"127.0.0.1\",\n port: proxyPort,\n },\n database: this.db,\n });\n\n await client.connect();\n return client;\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n throw new ConnectionError(\n `Failed to connect to Redis: ${message}`,\n \"REDIS_CONNECT_FAILED\",\n { originalError: error },\n );\n }\n }\n\n protected async closeNative(nativeClient: RedisClientType): Promise<void> {\n if (nativeClient && typeof nativeClient.quit === \"function\") {\n await nativeClient.quit();\n } else if (nativeClient && typeof nativeClient.disconnect === \"function\") {\n await nativeClient.disconnect();\n }\n }\n\n protected get requiresCredentials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,EAEA,IAAc,sBAA+B;AAC3C,WAAO;AAAA,EACT;AAAA,EAEA,IAAc,UAAkB;AAC9B,WAAO;AAAA,EACT;AACF;;;AD1BA,IAAAC,eAQO;AAtDP,eAAsB,aACpB,UACA,SAC0B;AAC1B,QAAM,UAAU,IAAI,aAAa;AAAA,IAC/B;AAAA,IACA,GAAG;AAAA,EACL,CAAC;AACD,QAAM,QAAQ,QAAQ;AACtB,SAAO;AAAA,IACL,QAAQ,QAAQ;AAAA,IAChB,YAAY,MAAM,QAAQ,WAAW;AAAA,EACvC;AACF;AAKA,eAAsB,MACpB,UACA,SAC0B;AAC1B,SAAO,aAAa,UAAU,OAAO;AACvC;AAKO,SAAS,mBACd,UACA,SACc;AACd,SAAO,IAAI,aAAa;AAAA,IACtB;AAAA,IACA,GAAG;AAAA,EACL,CAAC;AACH;AAyBO,IAAM,UAAU;","names":["redis","import_core"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,120 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ // src/sessions/RedisSession.ts
9
+ import { BaseSession, ConnectionError } from "@bridgebase/core";
10
+ var RedisSession = class extends BaseSession {
11
+ constructor(config) {
12
+ super(config);
13
+ this.db = config.db ?? 0;
14
+ this.dbType = "redis";
15
+ }
16
+ /**
17
+ * Access native Redis client
18
+ * Must call connect() first
19
+ *
20
+ * All redis (node-redis) client methods are available:
21
+ * - Basic commands: get, set, del, exists, etc.
22
+ * - Hashes: hSet, hGet, hGetAll, hDel, etc.
23
+ * - Lists: lPush, rPush, lPop, rPop, lRange, etc.
24
+ * - Sets: sAdd, sRem, sMembers, etc.
25
+ * - Sorted Sets: zAdd, zRem, zRange, zScore, etc.
26
+ * - Pub/Sub: publish, subscribe, unsubscribe, etc.
27
+ * - Transactions: multi, exec, etc.
28
+ * - And many more...
29
+ */
30
+ get client() {
31
+ if (!this.nativeClient) {
32
+ throw new ConnectionError(
33
+ "Session not connected. Call connect() first.",
34
+ "SESSION_NOT_CONNECTED"
35
+ );
36
+ }
37
+ return this.nativeClient;
38
+ }
39
+ async connectNative(credentials, proxyPort) {
40
+ try {
41
+ const redis2 = __require("redis");
42
+ const client = redis2.createClient({
43
+ socket: {
44
+ host: "127.0.0.1",
45
+ port: proxyPort
46
+ },
47
+ database: this.db
48
+ });
49
+ await client.connect();
50
+ return client;
51
+ } catch (error) {
52
+ const message = error instanceof Error ? error.message : String(error);
53
+ throw new ConnectionError(
54
+ `Failed to connect to Redis: ${message}`,
55
+ "REDIS_CONNECT_FAILED",
56
+ { originalError: error }
57
+ );
58
+ }
59
+ }
60
+ async closeNative(nativeClient) {
61
+ if (nativeClient && typeof nativeClient.quit === "function") {
62
+ await nativeClient.quit();
63
+ } else if (nativeClient && typeof nativeClient.disconnect === "function") {
64
+ await nativeClient.disconnect();
65
+ }
66
+ }
67
+ get requiresCredentials() {
68
+ return false;
69
+ }
70
+ get dbLabel() {
71
+ return "Redis";
72
+ }
73
+ };
74
+
75
+ // src/index.ts
76
+ import {
77
+ BridgeBaseError,
78
+ AuthError,
79
+ GatewayError,
80
+ GatewayResolutionError,
81
+ ConnectionError as ConnectionError2,
82
+ CredentialError,
83
+ ProxyError
84
+ } from "@bridgebase/core";
85
+ async function connectRedis(jwtToken, options) {
86
+ const session = new RedisSession({
87
+ jwtToken,
88
+ ...options
89
+ });
90
+ await session.connect();
91
+ return {
92
+ client: session.client,
93
+ disconnect: () => session.disconnect()
94
+ };
95
+ }
96
+ async function redis(jwtToken, options) {
97
+ return connectRedis(jwtToken, options);
98
+ }
99
+ function createRedisSession(jwtToken, options) {
100
+ return new RedisSession({
101
+ jwtToken,
102
+ ...options
103
+ });
104
+ }
105
+ var version = "0.2.0";
106
+ export {
107
+ AuthError,
108
+ BridgeBaseError,
109
+ ConnectionError2 as ConnectionError,
110
+ CredentialError,
111
+ GatewayError,
112
+ GatewayResolutionError,
113
+ ProxyError,
114
+ RedisSession,
115
+ connectRedis,
116
+ createRedisSession,
117
+ redis,
118
+ version
119
+ };
120
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/sessions/RedisSession.ts","../src/index.ts"],"sourcesContent":["import { BaseSession, DatabaseCredentials, SessionConfig, ConnectionError } from \"@bridgebase/core\";\nimport type { RedisClientType } from \"redis\";\n\nexport interface RedisSessionConfig extends SessionConfig {\n db?: number;\n}\n\n/**\n * Session adapter for Redis database\n * Provides full access to node-redis client methods\n */\nexport class RedisSession extends BaseSession<RedisClientType> {\n private db: number;\n\n constructor(config: RedisSessionConfig) {\n super(config);\n this.db = config.db ?? 0;\n this.dbType = \"redis\";\n }\n\n /**\n * Access native Redis client\n * Must call connect() first\n * \n * All redis (node-redis) client methods are available:\n * - Basic commands: get, set, del, exists, etc.\n * - Hashes: hSet, hGet, hGetAll, hDel, etc.\n * - Lists: lPush, rPush, lPop, rPop, lRange, etc.\n * - Sets: sAdd, sRem, sMembers, etc.\n * - Sorted Sets: zAdd, zRem, zRange, zScore, etc.\n * - Pub/Sub: publish, subscribe, unsubscribe, etc.\n * - Transactions: multi, exec, etc.\n * - And many more...\n */\n get client(): RedisClientType {\n if (!this.nativeClient) {\n throw new ConnectionError(\n \"Session not connected. Call connect() first.\",\n \"SESSION_NOT_CONNECTED\"\n );\n }\n return this.nativeClient;\n }\n\n protected async connectNative(\n credentials: DatabaseCredentials | null,\n proxyPort: number,\n ): Promise<RedisClientType> {\n try {\n // Lazy import redis\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const redis = require(\"redis\");\n\n const client = redis.createClient({\n socket: {\n host: \"127.0.0.1\",\n port: proxyPort,\n },\n database: this.db,\n });\n\n await client.connect();\n return client;\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n throw new ConnectionError(\n `Failed to connect to Redis: ${message}`,\n \"REDIS_CONNECT_FAILED\",\n { originalError: error },\n );\n }\n }\n\n protected async closeNative(nativeClient: RedisClientType): Promise<void> {\n if (nativeClient && typeof nativeClient.quit === \"function\") {\n await nativeClient.quit();\n } else if (nativeClient && typeof nativeClient.disconnect === \"function\") {\n await nativeClient.disconnect();\n }\n }\n\n protected get requiresCredentials(): boolean {\n return false;\n }\n\n protected get dbLabel(): string {\n return \"Redis\";\n }\n}\n","/**\n * @bridgebase/redis - Redis adapter with BridgeBase gateway integration\n */\n\nimport { RedisSession, RedisSessionConfig } from \"./sessions/RedisSession\";\nimport type { RedisClientType } from \"redis\";\n\nexport interface RedisConnection {\n client: RedisClientType;\n disconnect: () => Promise<void>;\n}\n\n/**\n * Convenience factory function for Redis\n * Creates and connects a session in one call\n */\nexport async function connectRedis(\n jwtToken: string,\n options?: Partial<RedisSessionConfig>\n): Promise<RedisConnection> {\n const session = new RedisSession({\n jwtToken,\n ...options,\n });\n await session.connect();\n return {\n client: session.client,\n disconnect: () => session.disconnect(),\n };\n}\n\n/**\n * Alias for connectRedis\n */\nexport async function redis(\n jwtToken: string,\n options?: Partial<RedisSessionConfig>\n): Promise<RedisConnection> {\n return connectRedis(jwtToken, options);\n}\n\n/**\n * Factory function to create a session without auto-connecting\n */\nexport function createRedisSession(\n jwtToken: string,\n options?: Partial<RedisSessionConfig>\n): RedisSession {\n return new RedisSession({\n jwtToken,\n ...options,\n });\n}\n\n// Session exports\nexport { RedisSession };\nexport type { RedisSessionConfig } from \"./sessions/RedisSession\";\n\n// Re-export commonly used Redis types for convenience\nexport type { RedisClientType } from \"redis\";\n\n// Re-export core types and exceptions\nexport {\n BridgeBaseError,\n AuthError,\n GatewayError,\n GatewayResolutionError,\n ConnectionError,\n CredentialError,\n ProxyError,\n} from \"@bridgebase/core\";\n\nexport type { SessionConfig, DatabaseCredentials } from \"@bridgebase/core\";\n\n/**\n * SDK version\n */\nexport const version = \"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EACA;AAAA,EACA,mBAAAC;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAtDP,eAAsB,aACpB,UACA,SAC0B;AAC1B,QAAM,UAAU,IAAI,aAAa;AAAA,IAC/B;AAAA,IACA,GAAG;AAAA,EACL,CAAC;AACD,QAAM,QAAQ,QAAQ;AACtB,SAAO;AAAA,IACL,QAAQ,QAAQ;AAAA,IAChB,YAAY,MAAM,QAAQ,WAAW;AAAA,EACvC;AACF;AAKA,eAAsB,MACpB,UACA,SAC0B;AAC1B,SAAO,aAAa,UAAU,OAAO;AACvC;AAKO,SAAS,mBACd,UACA,SACc;AACd,SAAO,IAAI,aAAa;AAAA,IACtB;AAAA,IACA,GAAG;AAAA,EACL,CAAC;AACH;AAyBO,IAAM,UAAU;","names":["redis","ConnectionError"]}
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@bridgebase/redis",
3
+ "version": "0.2.0",
4
+ "description": "BridgeBase Redis Adapter - Native Redis client wrapper with BridgeBase gateway integration",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.js"
12
+ }
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "README.md",
20
+ "LICENSE"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsup",
24
+ "build:watch": "tsup --watch",
25
+ "type-check": "tsc --noEmit",
26
+ "test": "jest --passWithNoTests",
27
+ "test:watch": "jest --watch --passWithNoTests",
28
+ "lint": "eslint src --ext .ts"
29
+ },
30
+ "keywords": [
31
+ "redis",
32
+ "bridgebase",
33
+ "database-adapter"
34
+ ],
35
+ "author": "BridgeBase Team",
36
+ "license": "MIT",
37
+ "homepage": "https://github.com/bridgebase-cloud/bridgebase-ts/tree/main/packages/redis#readme",
38
+ "bugs": {
39
+ "url": "https://github.com/bridgebase-cloud/bridgebase-ts/issues"
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "https://github.com/bridgebase-cloud/bridgebase-ts",
44
+ "directory": "packages/redis"
45
+ },
46
+ "dependencies": {
47
+ "@bridgebase/core": "^0.2.0"
48
+ },
49
+ "peerDependencies": {
50
+ "redis": "^5.0.0"
51
+ },
52
+ "peerDependenciesMeta": {
53
+ "redis": {
54
+ "optional": true
55
+ }
56
+ },
57
+ "devDependencies": {
58
+ "@bridgebase/core": "^0.2.0",
59
+ "@types/node": "^20.0.0",
60
+ "@types/jest": "^29.0.0",
61
+ "jest": "^29.0.0",
62
+ "ts-jest": "^29.0.0",
63
+ "typescript": "^5.0.0",
64
+ "tsup": "^8.0.0",
65
+ "redis": "^5.0.0"
66
+ }
67
+ }