@ooneex/pub-sub 1.1.4 → 1.1.6
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/index.d.ts +1 -1
- package/dist/index.js +8 -7
- package/dist/index.js.map +5 -5
- package/package.json +6 -6
package/dist/index.d.ts
CHANGED
|
@@ -60,7 +60,7 @@ declare abstract class PubSub<Data extends Record<string, ScalarType2> = Record<
|
|
|
60
60
|
}
|
|
61
61
|
import { Exception } from "@ooneex/exception";
|
|
62
62
|
declare class PubSubException extends Exception {
|
|
63
|
-
constructor(message: string, data?: Record<string, unknown>);
|
|
63
|
+
constructor(message: string, key: string, data?: Record<string, unknown>);
|
|
64
64
|
}
|
|
65
65
|
import { AppEnv } from "@ooneex/app-env";
|
|
66
66
|
import { ScalarType as ScalarType3 } from "@ooneex/types";
|
package/dist/index.js
CHANGED
|
@@ -56,8 +56,9 @@ import { Exception } from "@ooneex/exception";
|
|
|
56
56
|
import { HttpStatus } from "@ooneex/http-status";
|
|
57
57
|
|
|
58
58
|
class PubSubException extends Exception {
|
|
59
|
-
constructor(message, data = {}) {
|
|
59
|
+
constructor(message, key, data = {}) {
|
|
60
60
|
super(message, {
|
|
61
|
+
key,
|
|
61
62
|
status: HttpStatus.Code.InternalServerError,
|
|
62
63
|
data
|
|
63
64
|
});
|
|
@@ -75,7 +76,7 @@ class RedisPubSubClient {
|
|
|
75
76
|
this.env = env;
|
|
76
77
|
const connectionString = options.connectionString || this.env.PUBSUB_REDIS_URL;
|
|
77
78
|
if (!connectionString) {
|
|
78
|
-
throw new PubSubException("Redis connection string is required. Please provide a connection string either through the constructor options or set the PUBSUB_REDIS_URL environment variable.");
|
|
79
|
+
throw new PubSubException("Redis connection string is required. Please provide a connection string either through the constructor options or set the PUBSUB_REDIS_URL environment variable.", "CONNECTION_FAILED");
|
|
79
80
|
}
|
|
80
81
|
const { connectionString: _, ...userOptions } = options;
|
|
81
82
|
const defaultOptions = {
|
|
@@ -108,7 +109,7 @@ class RedisPubSubClient {
|
|
|
108
109
|
const message = JSON.stringify(config.data);
|
|
109
110
|
await this.client.publish(config.channel, message);
|
|
110
111
|
} catch (error) {
|
|
111
|
-
throw new PubSubException(`Failed to publish message to channel "${config.channel}": ${error}
|
|
112
|
+
throw new PubSubException(`Failed to publish message to channel "${config.channel}": ${error}`, "PUBLISH_FAILED");
|
|
112
113
|
} finally {
|
|
113
114
|
this.client.close();
|
|
114
115
|
}
|
|
@@ -121,7 +122,7 @@ class RedisPubSubClient {
|
|
|
121
122
|
handler({ data, channel: ch });
|
|
122
123
|
});
|
|
123
124
|
} catch (error) {
|
|
124
|
-
throw new PubSubException(`Failed to subscribe to channel "${channel}": ${error}
|
|
125
|
+
throw new PubSubException(`Failed to subscribe to channel "${channel}": ${error}`, "SUBSCRIBE_FAILED");
|
|
125
126
|
}
|
|
126
127
|
}
|
|
127
128
|
async unsubscribe(channel) {
|
|
@@ -131,7 +132,7 @@ class RedisPubSubClient {
|
|
|
131
132
|
}
|
|
132
133
|
await this.subscriber.unsubscribe(channel);
|
|
133
134
|
} catch (error) {
|
|
134
|
-
throw new PubSubException(`Failed to unsubscribe from channel "${channel}": ${error}
|
|
135
|
+
throw new PubSubException(`Failed to unsubscribe from channel "${channel}": ${error}`, "UNSUBSCRIBE_FAILED");
|
|
135
136
|
}
|
|
136
137
|
}
|
|
137
138
|
async unsubscribeAll() {
|
|
@@ -141,7 +142,7 @@ class RedisPubSubClient {
|
|
|
141
142
|
}
|
|
142
143
|
await this.subscriber.unsubscribe();
|
|
143
144
|
} catch (error) {
|
|
144
|
-
throw new PubSubException(`Failed to unsubscribe from all channels: ${error}
|
|
145
|
+
throw new PubSubException(`Failed to unsubscribe from all channels: ${error}`, "UNSUBSCRIBE_ALL_FAILED");
|
|
145
146
|
}
|
|
146
147
|
}
|
|
147
148
|
}
|
|
@@ -160,4 +161,4 @@ export {
|
|
|
160
161
|
PubSub
|
|
161
162
|
};
|
|
162
163
|
|
|
163
|
-
//# debugId=
|
|
164
|
+
//# debugId=8FFF57400EB6EB8664756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["src/decorators.ts", "src/PubSub.ts", "src/PubSubException.ts", "src/RedisPubSubClient.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import { container, EContainerScope } from \"@ooneex/container\";\nimport type { PubSubClassType } from \"./types\";\n\nexport const decorator = {\n pubSub: (scope: EContainerScope = EContainerScope.Singleton) => {\n return (target: PubSubClassType): void => {\n container.add(target, scope);\n
|
|
5
|
+
"import { container, EContainerScope } from \"@ooneex/container\";\nimport type { PubSubClassType } from \"./types\";\n\nexport const decorator = {\n pubSub: (scope: EContainerScope = EContainerScope.Singleton) => {\n return (target: PubSubClassType): void => {\n container.add(target, scope);\n };\n },\n};\n",
|
|
6
6
|
"import type { ScalarType } from \"@ooneex/types\";\nimport type { ServerWebSocket } from \"bun\";\nimport type { IPubSub, IPubSubClient } from \"./types\";\n\nexport abstract class PubSub<Data extends Record<string, ScalarType> = Record<string, ScalarType>>\n implements IPubSub<Data>\n{\n protected ws: ServerWebSocket | undefined;\n\n constructor(protected readonly client: IPubSubClient<Data>) {}\n\n public abstract getChannel(): string | Promise<string>;\n public abstract handler(context: { data: Data; channel: string }): Promise<void> | void;\n\n public async publish(data: Data, options?: { ws?: ServerWebSocket }): Promise<void> {\n this.ws = options?.ws;\n await this.client.publish({\n channel: await this.getChannel(),\n data,\n });\n }\n\n public async subscribe(ws?: ServerWebSocket): Promise<void> {\n this.ws = ws;\n await this.client.subscribe(await this.getChannel(), this.handler.bind(this));\n }\n\n public async unsubscribe(ws?: ServerWebSocket): Promise<void> {\n this.ws = ws;\n await this.client.unsubscribe(await this.getChannel());\n }\n\n public async unsubscribeAll(ws?: ServerWebSocket): Promise<void> {\n this.ws = ws;\n await this.client.unsubscribeAll();\n }\n}\n",
|
|
7
|
-
"import { Exception } from \"@ooneex/exception\";\nimport { HttpStatus } from \"@ooneex/http-status\";\n\nexport class PubSubException extends Exception {\n constructor(message: string, data: Record<string, unknown> = {}) {\n super(message, {\n status: HttpStatus.Code.InternalServerError,\n data,\n });\n this.name = \"PubSubException\";\n }\n}\n",
|
|
8
|
-
"import { AppEnv } from \"@ooneex/app-env\";\nimport { inject, injectable } from \"@ooneex/container\";\nimport type { ScalarType } from \"@ooneex/types\";\nimport { PubSubException } from \"./PubSubException\";\nimport type { IPubSubClient, PubSubMessageHandlerType, RedisPubSubOptionsType } from \"./types\";\n\n@injectable()\nexport class RedisPubSubClient<Data extends Record<string, ScalarType>> implements IPubSubClient<Data> {\n private client: Bun.RedisClient;\n private subscriber: Bun.RedisClient | null = null;\n\n constructor(\n @inject(AppEnv) private readonly env: AppEnv,\n options: RedisPubSubOptionsType = {},\n ) {\n const connectionString = options.connectionString || this.env.PUBSUB_REDIS_URL;\n\n if (!connectionString) {\n throw new PubSubException(\n \"Redis connection string is required. Please provide a connection string either through the constructor options or set the PUBSUB_REDIS_URL environment variable.\",\n );\n }\n\n const { connectionString: _, ...userOptions } = options;\n\n const defaultOptions = {\n connectionTimeout: 10_000,\n idleTimeout: 30_000,\n autoReconnect: true,\n maxRetries: 3,\n enableOfflineQueue: true,\n enableAutoPipelining: true,\n };\n\n const clientOptions = { ...defaultOptions, ...userOptions };\n\n this.client = new Bun.RedisClient(connectionString, clientOptions);\n }\n\n private async connect(): Promise<void> {\n if (!this.client.connected) {\n await this.client.connect();\n }\n }\n\n private async connectSubscriber(): Promise<void> {\n if (!this.subscriber) {\n this.subscriber = await this.client.duplicate();\n }\n\n if (!this.subscriber.connected) {\n await this.subscriber.connect();\n }\n }\n\n public async publish(config: { channel: string; data: Data }): Promise<void> {\n try {\n await this.connect();\n const message = JSON.stringify(config.data);\n await this.client.publish(config.channel, message);\n } catch (error) {\n throw new PubSubException(`Failed to publish message to channel \"${config.channel}\": ${error}
|
|
7
|
+
"import { Exception } from \"@ooneex/exception\";\nimport { HttpStatus } from \"@ooneex/http-status\";\n\nexport class PubSubException extends Exception {\n constructor(message: string, key: string, data: Record<string, unknown> = {}) {\n super(message, {\n key,\n status: HttpStatus.Code.InternalServerError,\n data,\n });\n this.name = \"PubSubException\";\n }\n}\n",
|
|
8
|
+
"import { AppEnv } from \"@ooneex/app-env\";\nimport { inject, injectable } from \"@ooneex/container\";\nimport type { ScalarType } from \"@ooneex/types\";\nimport { PubSubException } from \"./PubSubException\";\nimport type { IPubSubClient, PubSubMessageHandlerType, RedisPubSubOptionsType } from \"./types\";\n\n@injectable()\nexport class RedisPubSubClient<Data extends Record<string, ScalarType>> implements IPubSubClient<Data> {\n private client: Bun.RedisClient;\n private subscriber: Bun.RedisClient | null = null;\n\n constructor(\n @inject(AppEnv) private readonly env: AppEnv,\n options: RedisPubSubOptionsType = {},\n ) {\n const connectionString = options.connectionString || this.env.PUBSUB_REDIS_URL;\n\n if (!connectionString) {\n throw new PubSubException(\n \"Redis connection string is required. Please provide a connection string either through the constructor options or set the PUBSUB_REDIS_URL environment variable.\",\n \"CONNECTION_FAILED\",\n );\n }\n\n const { connectionString: _, ...userOptions } = options;\n\n const defaultOptions = {\n connectionTimeout: 10_000,\n idleTimeout: 30_000,\n autoReconnect: true,\n maxRetries: 3,\n enableOfflineQueue: true,\n enableAutoPipelining: true,\n };\n\n const clientOptions = { ...defaultOptions, ...userOptions };\n\n this.client = new Bun.RedisClient(connectionString, clientOptions);\n }\n\n private async connect(): Promise<void> {\n if (!this.client.connected) {\n await this.client.connect();\n }\n }\n\n private async connectSubscriber(): Promise<void> {\n if (!this.subscriber) {\n this.subscriber = await this.client.duplicate();\n }\n\n if (!this.subscriber.connected) {\n await this.subscriber.connect();\n }\n }\n\n public async publish(config: { channel: string; data: Data }): Promise<void> {\n try {\n await this.connect();\n const message = JSON.stringify(config.data);\n await this.client.publish(config.channel, message);\n } catch (error) {\n throw new PubSubException(`Failed to publish message to channel \"${config.channel}\": ${error}`, \"PUBLISH_FAILED\");\n } finally {\n this.client.close();\n }\n }\n\n public async subscribe(channel: string, handler: PubSubMessageHandlerType<Data>): Promise<void> {\n try {\n await this.connectSubscriber();\n await this.subscriber?.subscribe(channel, (message: string, ch: string) => {\n const data = JSON.parse(message) as Data;\n handler({ data, channel: ch });\n });\n } catch (error) {\n throw new PubSubException(`Failed to subscribe to channel \"${channel}\": ${error}`, \"SUBSCRIBE_FAILED\");\n }\n }\n\n public async unsubscribe(channel: string): Promise<void> {\n try {\n if (!this.subscriber) {\n return;\n }\n\n await this.subscriber.unsubscribe(channel);\n } catch (error) {\n throw new PubSubException(`Failed to unsubscribe from channel \"${channel}\": ${error}`, \"UNSUBSCRIBE_FAILED\");\n }\n }\n\n public async unsubscribeAll(): Promise<void> {\n try {\n if (!this.subscriber) {\n return;\n }\n\n await this.subscriber.unsubscribe();\n } catch (error) {\n throw new PubSubException(`Failed to unsubscribe from all channels: ${error}`, \"UNSUBSCRIBE_ALL_FAILED\");\n }\n }\n}\n"
|
|
9
9
|
],
|
|
10
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAGO,IAAM,YAAY;AAAA,EACvB,QAAQ,CAAC,QAAyB,gBAAgB,cAAc;AAAA,IAC9D,OAAO,CAAC,WAAkC;AAAA,MACxC,UAAU,IAAI,QAAQ,KAAK;AAAA;AAAA;
|
|
11
|
-
"debugId": "
|
|
10
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAGO,IAAM,YAAY;AAAA,EACvB,QAAQ,CAAC,QAAyB,gBAAgB,cAAc;AAAA,IAC9D,OAAO,CAAC,WAAkC;AAAA,MACxC,UAAU,IAAI,QAAQ,KAAK;AAAA;AAAA;AAGjC;;ACLO,MAAe,OAEtB;AAAA,EAGiC;AAAA,EAFrB;AAAA,EAEV,WAAW,CAAoB,QAA6B;AAAA,IAA7B;AAAA;AAAA,OAKlB,QAAO,CAAC,MAAY,SAAmD;AAAA,IAClF,KAAK,KAAK,SAAS;AAAA,IACnB,MAAM,KAAK,OAAO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O;AAAA,MACzC,OAAO,OAAO;AAAA,MACd,MAAM,IAAI,gBAAgB,uCAAuC,aAAa,SAAS,oBAAoB;AAAA;AAAA;AAAA,OAIlG,eAAc,GAAkB;AAAA,IAC3C,IAAI;AAAA,MACF,IAAI,CAAC,KAAK,YAAY;AAAA,QACpB;AAAA,MACF;AAAA,MAEA,MAAM,KAAK,WAAW,YAAY;AAAA,MAClC,OAAO,OAAO;AAAA,MACd,MAAM,IAAI,gBAAgB,4CAA4C,SAAS,wBAAwB;AAAA;AAAA;AAG7G;AAhGa,oBAAN;AAAA,EADN,WAAW;AAAA,EAMP,kCAAO,MAAM;AAAA,EALX;AAAA;AAAA;AAAA;AAAA,GAAM;",
|
|
11
|
+
"debugId": "8FFF57400EB6EB8664756E2164756E21",
|
|
12
12
|
"names": []
|
|
13
13
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ooneex/pub-sub",
|
|
3
3
|
"description": "Publish-subscribe messaging system for decoupled, event-driven communication between application components with typed event channels",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.6",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -28,13 +28,13 @@
|
|
|
28
28
|
"test": "bun test tests"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@ooneex/app-env": "1.
|
|
32
|
-
"@ooneex/container": "1.
|
|
33
|
-
"@ooneex/exception": "1.
|
|
34
|
-
"@ooneex/http-status": "1.1.
|
|
31
|
+
"@ooneex/app-env": "1.3.0",
|
|
32
|
+
"@ooneex/container": "1.2.2",
|
|
33
|
+
"@ooneex/exception": "1.2.0",
|
|
34
|
+
"@ooneex/http-status": "1.1.3"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@ooneex/types": "1.1.
|
|
37
|
+
"@ooneex/types": "1.1.3"
|
|
38
38
|
},
|
|
39
39
|
"keywords": [
|
|
40
40
|
"bun",
|