@ooneex/pub-sub 0.0.13 → 0.0.15
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.js +2 -153
- package/dist/index.js.map +2 -2
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,155 +1,4 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
5
|
-
r = Reflect.decorate(decorators, target, key, desc);
|
|
6
|
-
else
|
|
7
|
-
for (var i = decorators.length - 1;i >= 0; i--)
|
|
8
|
-
if (d = decorators[i])
|
|
9
|
-
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
10
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
11
|
-
};
|
|
12
|
-
var __legacyMetadataTS = (k, v) => {
|
|
13
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
|
|
14
|
-
return Reflect.metadata(k, v);
|
|
15
|
-
};
|
|
2
|
+
var b=function(e,t,i,n){var o=arguments.length,s=o<3?t:n===null?n=Object.getOwnPropertyDescriptor(t,i):n,a;if(typeof Reflect==="object"&&typeof Reflect.decorate==="function")s=Reflect.decorate(e,t,i,n);else for(var u=e.length-1;u>=0;u--)if(a=e[u])s=(o<3?a(s):o>3?a(t,i,s):a(t,i))||s;return o>3&&s&&Object.defineProperty(t,i,s),s};var l=(e,t)=>{if(typeof Reflect==="object"&&typeof Reflect.metadata==="function")return Reflect.metadata(e,t)};import{container as p,EContainerScope as d}from"@ooneex/container";var m={pubSub:(e=d.Singleton)=>{return(t)=>{p.add(t,e),p.get(t).subscribe()}}};class h{client;constructor(e){this.client=e}async publish(e,t){await this.client.publish({channel:await this.getChannel(),data:e,...t!==void 0&&{key:t}})}async subscribe(){await this.client.subscribe(await this.getChannel(),this.handler.bind(this))}async unsubscribe(){await this.client.unsubscribe(await this.getChannel())}async unsubscribeAll(){await this.client.unsubscribeAll()}}import{Exception as S}from"@ooneex/exception";import{HttpStatus as y}from"@ooneex/http-status";class r extends S{constructor(e,t={}){super(e,{status:y.Code.InternalServerError,data:t});this.name="PubSubException"}}import{injectable as P}from"@ooneex/container";class c{client;subscriber=null;constructor(e={}){let t=e.connectionString||Bun.env.PUBSUB_REDIS_URL;if(!t)throw new r("Redis connection string is required. Please provide a connection string either through the constructor options or set the PUBSUB_REDIS_URL environment variable.");let{connectionString:i,...n}=e,s={...{connectionTimeout:1e4,idleTimeout:30000,autoReconnect:!0,maxRetries:3,enableOfflineQueue:!0,enableAutoPipelining:!0},...n};this.client=new Bun.RedisClient(t,s)}async connect(){if(!this.client.connected)await this.client.connect()}async connectSubscriber(){if(!this.subscriber)this.subscriber=await this.client.duplicate();if(!this.subscriber.connected)await this.subscriber.connect()}async publish(e){try{await this.connect();let t=JSON.stringify(e.data);await this.client.publish(e.channel,t)}catch(t){throw new r(`Failed to publish message to channel "${e.channel}": ${t}`)}finally{this.client.close()}}async subscribe(e,t){try{await this.connectSubscriber(),await this.subscriber?.subscribe(e,(i,n)=>{let o=JSON.parse(i);t({data:o,channel:n})})}catch(i){throw new r(`Failed to subscribe to channel "${e}": ${i}`)}}async unsubscribe(e){try{if(!this.subscriber)return;await this.subscriber.unsubscribe(e)}catch(t){throw new r(`Failed to unsubscribe from channel "${e}": ${t}`)}}async unsubscribeAll(){try{if(!this.subscriber)return;await this.subscriber.unsubscribe()}catch(e){throw new r(`Failed to unsubscribe from all channels: ${e}`)}}}c=b([P(),l("design:paramtypes",[typeof RedisPubSubOptionsType==="undefined"?Object:RedisPubSubOptionsType])],c);export{m as decorator,c as RedisPubSub,r as PubSubException,h as PubSub};
|
|
16
3
|
|
|
17
|
-
|
|
18
|
-
import { container, EContainerScope } from "@ooneex/container";
|
|
19
|
-
var decorator = {
|
|
20
|
-
pubSub: (scope = EContainerScope.Singleton) => {
|
|
21
|
-
return (target) => {
|
|
22
|
-
container.add(target, scope);
|
|
23
|
-
const pubsub = container.get(target);
|
|
24
|
-
pubsub.subscribe();
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
// src/PubSub.ts
|
|
29
|
-
class PubSub {
|
|
30
|
-
client;
|
|
31
|
-
constructor(client) {
|
|
32
|
-
this.client = client;
|
|
33
|
-
}
|
|
34
|
-
async publish(data, key) {
|
|
35
|
-
await this.client.publish({
|
|
36
|
-
channel: await this.getChannel(),
|
|
37
|
-
data,
|
|
38
|
-
...key !== undefined && { key }
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
async subscribe() {
|
|
42
|
-
await this.client.subscribe(await this.getChannel(), this.handler.bind(this));
|
|
43
|
-
}
|
|
44
|
-
async unsubscribe() {
|
|
45
|
-
await this.client.unsubscribe(await this.getChannel());
|
|
46
|
-
}
|
|
47
|
-
async unsubscribeAll() {
|
|
48
|
-
await this.client.unsubscribeAll();
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
// src/PubSubException.ts
|
|
52
|
-
import { Exception } from "@ooneex/exception";
|
|
53
|
-
import { HttpStatus } from "@ooneex/http-status";
|
|
54
|
-
|
|
55
|
-
class PubSubException extends Exception {
|
|
56
|
-
constructor(message, data = {}) {
|
|
57
|
-
super(message, {
|
|
58
|
-
status: HttpStatus.Code.InternalServerError,
|
|
59
|
-
data
|
|
60
|
-
});
|
|
61
|
-
this.name = "PubSubException";
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
// src/RedisPubSubClient.ts
|
|
65
|
-
import { injectable } from "@ooneex/container";
|
|
66
|
-
class RedisPubSubClient {
|
|
67
|
-
client;
|
|
68
|
-
subscriber = null;
|
|
69
|
-
constructor(options = {}) {
|
|
70
|
-
const connectionString = options.connectionString || Bun.env.PUBSUB_REDIS_URL;
|
|
71
|
-
if (!connectionString) {
|
|
72
|
-
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.");
|
|
73
|
-
}
|
|
74
|
-
const { connectionString: _, ...userOptions } = options;
|
|
75
|
-
const defaultOptions = {
|
|
76
|
-
connectionTimeout: 1e4,
|
|
77
|
-
idleTimeout: 30000,
|
|
78
|
-
autoReconnect: true,
|
|
79
|
-
maxRetries: 3,
|
|
80
|
-
enableOfflineQueue: true,
|
|
81
|
-
enableAutoPipelining: true
|
|
82
|
-
};
|
|
83
|
-
const clientOptions = { ...defaultOptions, ...userOptions };
|
|
84
|
-
this.client = new Bun.RedisClient(connectionString, clientOptions);
|
|
85
|
-
}
|
|
86
|
-
async connect() {
|
|
87
|
-
if (!this.client.connected) {
|
|
88
|
-
await this.client.connect();
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
async connectSubscriber() {
|
|
92
|
-
if (!this.subscriber) {
|
|
93
|
-
this.subscriber = await this.client.duplicate();
|
|
94
|
-
}
|
|
95
|
-
if (!this.subscriber.connected) {
|
|
96
|
-
await this.subscriber.connect();
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
async publish(config) {
|
|
100
|
-
try {
|
|
101
|
-
await this.connect();
|
|
102
|
-
const message = JSON.stringify(config.data);
|
|
103
|
-
await this.client.publish(config.channel, message);
|
|
104
|
-
} catch (error) {
|
|
105
|
-
throw new PubSubException(`Failed to publish message to channel "${config.channel}": ${error}`);
|
|
106
|
-
} finally {
|
|
107
|
-
this.client.close();
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
async subscribe(channel, handler) {
|
|
111
|
-
try {
|
|
112
|
-
await this.connectSubscriber();
|
|
113
|
-
await this.subscriber?.subscribe(channel, (message, ch) => {
|
|
114
|
-
const data = JSON.parse(message);
|
|
115
|
-
handler({ data, channel: ch });
|
|
116
|
-
});
|
|
117
|
-
} catch (error) {
|
|
118
|
-
throw new PubSubException(`Failed to subscribe to channel "${channel}": ${error}`);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
async unsubscribe(channel) {
|
|
122
|
-
try {
|
|
123
|
-
if (!this.subscriber) {
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
await this.subscriber.unsubscribe(channel);
|
|
127
|
-
} catch (error) {
|
|
128
|
-
throw new PubSubException(`Failed to unsubscribe from channel "${channel}": ${error}`);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
async unsubscribeAll() {
|
|
132
|
-
try {
|
|
133
|
-
if (!this.subscriber) {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
await this.subscriber.unsubscribe();
|
|
137
|
-
} catch (error) {
|
|
138
|
-
throw new PubSubException(`Failed to unsubscribe from all channels: ${error}`);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
RedisPubSubClient = __legacyDecorateClassTS([
|
|
143
|
-
injectable(),
|
|
144
|
-
__legacyMetadataTS("design:paramtypes", [
|
|
145
|
-
typeof RedisPubSubOptionsType === "undefined" ? Object : RedisPubSubOptionsType
|
|
146
|
-
])
|
|
147
|
-
], RedisPubSubClient);
|
|
148
|
-
export {
|
|
149
|
-
decorator,
|
|
150
|
-
RedisPubSubClient as RedisPubSub,
|
|
151
|
-
PubSubException,
|
|
152
|
-
PubSub
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
//# debugId=A4EA7FF3C910E0DE64756E2164756E21
|
|
4
|
+
//# debugId=71EA4C698ED7AF9C64756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
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
8
|
"import { 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(options: RedisPubSubOptionsType = {}) {\n const connectionString = options.connectionString || Bun.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; key?: string }): 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}`);\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}`);\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}`);\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}`);\n }\n }\n}\n"
|
|
9
9
|
],
|
|
10
|
-
"mappings": "
|
|
11
|
-
"debugId": "
|
|
10
|
+
"mappings": ";ybAAA,oBAAS,qBAAW,0BAGb,IAAM,EAAY,CACvB,OAAQ,CAAC,EAAyB,EAAgB,YAAc,CAC9D,MAAO,CAAC,IAAkC,CACxC,EAAU,IAAI,EAAQ,CAAK,EACZ,EAAU,IAAa,CAAM,EACrC,UAAU,GAGvB,ECRO,MAAe,CAEtB,CACiC,OAA/B,WAAW,CAAoB,EAA6B,CAA7B,mBAKlB,QAAO,CAAC,EAAY,EAA6B,CAC5D,MAAM,KAAK,OAAO,QAAQ,CACxB,QAAS,MAAM,KAAK,WAAW,EAC/B,UACI,IAAQ,QAAa,CAAE,KAAI,CACjC,CAAC,OAGU,UAAS,EAAkB,CACtC,MAAM,KAAK,OAAO,UAAU,MAAM,KAAK,WAAW,EAAG,KAAK,QAAQ,KAAK,IAAI,CAAC,OAGjE,YAAW,EAAkB,CACxC,MAAM,KAAK,OAAO,YAAY,MAAM,KAAK,WAAW,CAAC,OAG1C,eAAc,EAAkB,CAC3C,MAAM,KAAK,OAAO,eAAe,EAErC,CC9BA,oBAAS,0BACT,qBAAS,4BAEF,MAAM,UAAwB,CAAU,CAC7C,WAAW,CAAC,EAAiB,EAAgC,CAAC,EAAG,CAC/D,MAAM,EAAS,CACb,OAAQ,EAAW,KAAK,oBACxB,MACF,CAAC,EACD,KAAK,KAAO,kBAEhB,CCXA,qBAAS,0BAMF,MAAM,CAA0F,CAC7F,OACA,WAAqC,KAE7C,WAAW,CAAC,EAAkC,CAAC,EAAG,CAChD,IAAM,EAAmB,EAAQ,kBAAoB,IAAI,IAAI,i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WACR,OAGF,MAAM,KAAK,WAAW,YAAY,EAClC,MAAO,EAAO,CACd,MAAM,IAAI,EAAgB,4CAA4C,GAAO,GAGnF,CA5Fa,EAAN,GADN,EAAW,EACL,oGAAM",
|
|
11
|
+
"debugId": "71EA4C698ED7AF9C64756E2164756E21",
|
|
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 pattern implementation for event-driven communication between application components",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.15",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -28,12 +28,12 @@
|
|
|
28
28
|
"test": "bun test tests"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@ooneex/container": "0.0.
|
|
32
|
-
"@ooneex/exception": "0.0.
|
|
33
|
-
"@ooneex/http-status": "0.0.
|
|
31
|
+
"@ooneex/container": "0.0.14",
|
|
32
|
+
"@ooneex/exception": "0.0.13",
|
|
33
|
+
"@ooneex/http-status": "0.0.13"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@ooneex/types": "0.0.
|
|
36
|
+
"@ooneex/types": "0.0.13"
|
|
37
37
|
},
|
|
38
38
|
"keywords": [
|
|
39
39
|
"bun",
|