@avtechno/sfr 1.0.3 → 1.0.5

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/mq.mjs CHANGED
@@ -38,13 +38,38 @@ export class MQLib {
38
38
  return this.channel;
39
39
  }
40
40
  }
41
+ export class BaseMQ {
42
+ channel;
43
+ type;
44
+ constructor(channel, type) {
45
+ this.channel = channel;
46
+ this.type = type;
47
+ }
48
+ /**
49
+ * Sends a reply to the message sender in a Request-Reply pattern.
50
+ *
51
+ * @param msg - The original message to reply to.
52
+ * @param payload - The reply message to send back to the requester.
53
+ * @example
54
+ * // Send a response back to the client in a Request-Reply pattern
55
+ * const mq = new TargetedMQ(channel, "Request-Reply");
56
+ * mq.reply(msg, { result: "Processed successfully" });
57
+ */
58
+ async reply(msg, payload) {
59
+ if (this.type !== "Request-Reply")
60
+ return;
61
+ this.channel.sendToQueue(msg.properties.replyTo, encode_payload(payload), { correlationId: msg.properties.correlationId });
62
+ this.channel.ack(msg);
63
+ }
64
+ }
41
65
  /**
42
66
  * BroadcastMQ class to handle different types of broadcast communication patterns: Publish-Subscribe, Routing, and Topic.
43
67
  */
44
- export class BroadcastMQ {
68
+ export class BroadcastMQ extends BaseMQ {
45
69
  channel;
46
70
  type;
47
71
  constructor(channel, type) {
72
+ super(channel, type);
48
73
  this.channel = channel;
49
74
  this.type = type;
50
75
  }
@@ -98,7 +123,7 @@ export class BroadcastMQ {
98
123
  await this.channel.assertExchange(exchange, "fanout", { durable: false });
99
124
  const { queue } = await this.channel.assertQueue("", { exclusive: true });
100
125
  this.channel.bindQueue(queue, exchange, "");
101
- this.channel.consume(queue, (v) => fn(parse_payload(v)), cfg.options);
126
+ this.channel.consume(queue, (v) => fn(parse_payload(v)), cfg.consume_options);
102
127
  break;
103
128
  }
104
129
  case "Routing": {
@@ -121,10 +146,11 @@ export class BroadcastMQ {
121
146
  /**
122
147
  * TargetedMQ class to handle Point-to-Point and Request-Reply communication patterns.
123
148
  */
124
- export class TargetedMQ {
149
+ export class TargetedMQ extends BaseMQ {
125
150
  channel;
126
151
  type;
127
152
  constructor(channel, type) {
153
+ super(channel, type);
128
154
  this.channel = channel;
129
155
  this.type = type;
130
156
  }
@@ -152,22 +178,6 @@ export class TargetedMQ {
152
178
  }
153
179
  }
154
180
  }
155
- /**
156
- * Sends a reply to the message sender in a Request-Reply pattern.
157
- *
158
- * @param msg - The original message to reply to.
159
- * @param payload - The reply message to send back to the requester.
160
- * @example
161
- * // Send a response back to the client in a Request-Reply pattern
162
- * const mq = new TargetedMQ(channel, "Request-Reply");
163
- * mq.reply(msg, { result: "Processed successfully" });
164
- */
165
- async reply(msg, payload) {
166
- if (this.type !== "Request-Reply")
167
- return;
168
- this.channel.sendToQueue(msg.properties.replyTo, encode_payload(payload), { correlationId: msg.properties.correlationId });
169
- this.channel.ack(msg);
170
- }
171
181
  /**
172
182
  * Consumes messages from the specified queue based on the communication pattern.
173
183
  *
@@ -182,13 +192,13 @@ export class TargetedMQ {
182
192
  let fn = cfg.fn.bind({ channel: this.channel, type: this.type });
183
193
  switch (this.type) {
184
194
  case "Point-to-Point": {
185
- await this.channel.assertQueue(queue);
186
- this.channel.consume(queue, (v) => fn(parse_payload(v)), cfg.options);
195
+ await this.channel.assertQueue(queue, cfg.queue_options);
196
+ this.channel.consume(queue, (v) => fn(parse_payload(v)), cfg.consume_options);
187
197
  break;
188
198
  }
189
199
  case "Request-Reply": {
190
- await this.channel.assertQueue(queue);
191
- this.channel.consume(queue, (v) => fn(parse_payload(v)), cfg.options);
200
+ await this.channel.assertQueue(queue, cfg.queue_options);
201
+ this.channel.consume(queue, (v) => fn(parse_payload(v)), cfg.consume_options);
192
202
  break;
193
203
  }
194
204
  }
@@ -196,7 +196,7 @@ export class SFRPipeline {
196
196
  for (let [name, handler] of valid_handlers) {
197
197
  const dir = `${base_dir}${namespace}/${name}`;
198
198
  const validator = validators[name];
199
- const validator_type = typeof validator !== "function" ? "joi" : "multer"; // TODO: Dynamically update parameter content based on validator type.
199
+ //const validator_type = typeof validator !== "function" ? "joi" : "multer"; // TODO: Dynamically update parameter content based on validator type.
200
200
  let validator_fn = function (msg) {
201
201
  const error = template(validator, msg.content);
202
202
  if (error) {
@@ -219,33 +219,17 @@ export class SFRPipeline {
219
219
  /* bind validators */
220
220
  if (mq instanceof TargetedMQ) {
221
221
  mq.consume(dir, {
222
- options: handler.options,
222
+ consume_options: handler.consume_options,
223
+ queue_options: handler.queue_options,
223
224
  fn: validator_fn
224
225
  });
225
226
  }
226
227
  if (mq instanceof BroadcastMQ) {
227
- switch (pattern) {
228
- case "Publish-Subscribe":
229
- {
230
- mq.subscribe(dir, handler.key, {
231
- options: handler.options,
232
- fn: validator_fn
233
- });
234
- }
235
- break;
236
- case "Routing": {
237
- mq.subscribe(dir, handler.key, {
238
- options: handler.options,
239
- fn: validator_fn
240
- });
241
- }
242
- case "Topic": {
243
- mq.subscribe(dir, handler.key, {
244
- options: handler.options,
245
- fn: validator_fn
246
- });
247
- }
248
- }
228
+ mq.subscribe(dir, handler.key, {
229
+ queue_options: handler.queue_options,
230
+ consume_options: handler.consume_options,
231
+ fn: validator_fn
232
+ });
249
233
  }
250
234
  this.mount_data.mq++;
251
235
  }
@@ -27,10 +27,26 @@ export declare class MQLib {
27
27
  */
28
28
  get_channel(): Channel;
29
29
  }
30
+ export declare class BaseMQ {
31
+ channel: Channel;
32
+ protected type: CommunicationPattern;
33
+ constructor(channel: Channel, type: CommunicationPattern);
34
+ /**
35
+ * Sends a reply to the message sender in a Request-Reply pattern.
36
+ *
37
+ * @param msg - The original message to reply to.
38
+ * @param payload - The reply message to send back to the requester.
39
+ * @example
40
+ * // Send a response back to the client in a Request-Reply pattern
41
+ * const mq = new TargetedMQ(channel, "Request-Reply");
42
+ * mq.reply(msg, { result: "Processed successfully" });
43
+ */
44
+ reply(msg: ConsumeMessage, payload: any): Promise<void>;
45
+ }
30
46
  /**
31
47
  * BroadcastMQ class to handle different types of broadcast communication patterns: Publish-Subscribe, Routing, and Topic.
32
48
  */
33
- export declare class BroadcastMQ {
49
+ export declare class BroadcastMQ extends BaseMQ {
34
50
  channel: Channel;
35
51
  protected type: CommunicationPattern;
36
52
  constructor(channel: Channel, type: CommunicationPattern);
@@ -64,7 +80,7 @@ export declare class BroadcastMQ {
64
80
  /**
65
81
  * TargetedMQ class to handle Point-to-Point and Request-Reply communication patterns.
66
82
  */
67
- export declare class TargetedMQ {
83
+ export declare class TargetedMQ extends BaseMQ {
68
84
  channel: Channel;
69
85
  protected type: CommunicationPattern;
70
86
  constructor(channel: Channel, type: CommunicationPattern);
@@ -80,17 +96,6 @@ export declare class TargetedMQ {
80
96
  * mq.produce("taskQueue", { taskId: 1, action: "process" });
81
97
  */
82
98
  produce(binding: string, payload: any, options?: Options.Publish): Promise<boolean>;
83
- /**
84
- * Sends a reply to the message sender in a Request-Reply pattern.
85
- *
86
- * @param msg - The original message to reply to.
87
- * @param payload - The reply message to send back to the requester.
88
- * @example
89
- * // Send a response back to the client in a Request-Reply pattern
90
- * const mq = new TargetedMQ(channel, "Request-Reply");
91
- * mq.reply(msg, { result: "Processed successfully" });
92
- */
93
- reply(msg: ConsumeMessage, payload: any): Promise<void>;
94
99
  /**
95
100
  * Consumes messages from the specified queue based on the communication pattern.
96
101
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@avtechno/sfr",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "An opinionated way of writing services using ExpressJS.",
5
5
  "type": "module",
6
6
  "files": [
package/src/mq.mts CHANGED
@@ -43,11 +43,33 @@ export class MQLib {
43
43
  }
44
44
  }
45
45
 
46
+ export class BaseMQ{
47
+ constructor(public channel: Channel, protected type: CommunicationPattern) {}
48
+ /**
49
+ * Sends a reply to the message sender in a Request-Reply pattern.
50
+ *
51
+ * @param msg - The original message to reply to.
52
+ * @param payload - The reply message to send back to the requester.
53
+ * @example
54
+ * // Send a response back to the client in a Request-Reply pattern
55
+ * const mq = new TargetedMQ(channel, "Request-Reply");
56
+ * mq.reply(msg, { result: "Processed successfully" });
57
+ */
58
+ async reply(msg: ConsumeMessage, payload: any) {
59
+ if (this.type !== "Request-Reply") return;
60
+
61
+ this.channel.sendToQueue(msg.properties.replyTo, encode_payload(payload), {correlationId : msg.properties.correlationId});
62
+ this.channel.ack(msg);
63
+ }
64
+ }
65
+
46
66
  /**
47
67
  * BroadcastMQ class to handle different types of broadcast communication patterns: Publish-Subscribe, Routing, and Topic.
48
68
  */
49
- export class BroadcastMQ {
50
- constructor(public channel: Channel, protected type: CommunicationPattern) {}
69
+ export class BroadcastMQ extends BaseMQ{
70
+ constructor(public channel: Channel, protected type: CommunicationPattern) {
71
+ super(channel, type);
72
+ }
51
73
 
52
74
  /**
53
75
  * Publishes a message to the specified exchange based on the communication pattern.
@@ -109,7 +131,7 @@ export class BroadcastMQ {
109
131
  const { queue } = await this.channel.assertQueue("", { exclusive: true });
110
132
 
111
133
  this.channel.bindQueue(queue, exchange, "");
112
- this.channel.consume(queue, (v) => fn(parse_payload(v)), cfg.options);
134
+ this.channel.consume(queue, (v) => fn(parse_payload(v)), cfg.consume_options);
113
135
  break;
114
136
  }
115
137
 
@@ -137,9 +159,10 @@ export class BroadcastMQ {
137
159
  /**
138
160
  * TargetedMQ class to handle Point-to-Point and Request-Reply communication patterns.
139
161
  */
140
- export class TargetedMQ {
141
- constructor(public channel: Channel, protected type: CommunicationPattern) {}
142
-
162
+ export class TargetedMQ extends BaseMQ{
163
+ constructor(public channel: Channel, protected type: CommunicationPattern) {
164
+ super(channel, type);
165
+ }
143
166
  /**
144
167
  * Sends a message to the specified queue depending on the communication pattern.
145
168
  *
@@ -166,23 +189,6 @@ export class TargetedMQ {
166
189
  }
167
190
  }
168
191
 
169
- /**
170
- * Sends a reply to the message sender in a Request-Reply pattern.
171
- *
172
- * @param msg - The original message to reply to.
173
- * @param payload - The reply message to send back to the requester.
174
- * @example
175
- * // Send a response back to the client in a Request-Reply pattern
176
- * const mq = new TargetedMQ(channel, "Request-Reply");
177
- * mq.reply(msg, { result: "Processed successfully" });
178
- */
179
- async reply(msg: ConsumeMessage, payload: any) {
180
- if (this.type !== "Request-Reply") return;
181
-
182
- this.channel.sendToQueue(msg.properties.replyTo, encode_payload(payload), {correlationId : msg.properties.correlationId});
183
- this.channel.ack(msg);
184
- }
185
-
186
192
  /**
187
193
  * Consumes messages from the specified queue based on the communication pattern.
188
194
  *
@@ -197,17 +203,17 @@ export class TargetedMQ {
197
203
  let fn = cfg.fn.bind({ channel: this.channel, type: this.type });
198
204
 
199
205
  switch (this.type) {
200
- case "Point-to-Point": {
201
- await this.channel.assertQueue(queue);
202
- this.channel.consume(queue, (v) => fn(parse_payload(v)), cfg.options);
203
- break;
204
- }
205
-
206
- case "Request-Reply": {
207
- await this.channel.assertQueue(queue);
208
- this.channel.consume(queue, (v) => fn(parse_payload(v)), cfg.options);
209
- break;
210
- }
206
+ case "Point-to-Point": {
207
+ await this.channel.assertQueue(queue, cfg.queue_options);
208
+ this.channel.consume(queue, (v) => fn(parse_payload(v)), cfg.consume_options);
209
+ break;
210
+ }
211
+
212
+ case "Request-Reply": {
213
+ await this.channel.assertQueue(queue, cfg.queue_options);
214
+ this.channel.consume(queue, (v) => fn(parse_payload(v)), cfg.consume_options);
215
+ break;
216
+ }
211
217
  }
212
218
  }
213
219
  }
@@ -205,7 +205,7 @@ export class SFRPipeline {
205
205
  for (let [name, handler] of valid_handlers) {
206
206
  const dir = `${base_dir}${namespace}/${name}`;
207
207
  const validator = validators[name];
208
- const validator_type = typeof validator !== "function" ? "joi" : "multer"; // TODO: Dynamically update parameter content based on validator type.
208
+ //const validator_type = typeof validator !== "function" ? "joi" : "multer"; // TODO: Dynamically update parameter content based on validator type.
209
209
 
210
210
  let validator_fn = function (msg: ConsumeMessage) {
211
211
  const error = template(validator, msg.content);
@@ -231,34 +231,18 @@ export class SFRPipeline {
231
231
  /* bind validators */
232
232
  if (mq instanceof TargetedMQ) {
233
233
  mq.consume(dir, {
234
- options: handler.options,
234
+ consume_options: handler.consume_options,
235
+ queue_options : handler.queue_options,
235
236
  fn: validator_fn
236
237
  })
237
238
  }
238
239
 
239
240
  if (mq instanceof BroadcastMQ) {
240
- switch (pattern) {
241
- case "Publish-Subscribe": {
242
- mq.subscribe(dir, handler.key, {
243
- options: handler.options,
244
- fn: validator_fn
245
- })
246
- } break;
247
-
248
- case "Routing": {
249
- mq.subscribe(dir, handler.key, {
250
- options: handler.options,
251
- fn: validator_fn
252
- });
253
- }
254
-
255
- case "Topic": {
256
- mq.subscribe(dir, handler.key, {
257
- options: handler.options,
258
- fn : validator_fn
259
- });
260
- }
261
- }
241
+ mq.subscribe(dir, handler.key, {
242
+ queue_options : handler.queue_options,
243
+ consume_options: handler.consume_options,
244
+ fn : validator_fn
245
+ });
262
246
  }
263
247
 
264
248
  this.mount_data.mq++;
@@ -20,6 +20,7 @@ declare type Socket = import("socket.io").Socket;
20
20
  declare type BroadcastMQ = import("../mq.mts").BroadcastMQ;
21
21
  declare type TargetedMQ = import("../mq.mts").TargetedMQ;
22
22
 
23
+
23
24
  declare enum PROTOCOLS {
24
25
  REST = "REST",
25
26
  WS = "WS",
@@ -279,10 +280,12 @@ declare type ParserCFG = {
279
280
 
280
281
  declare type Channel = import("amqplib").Channel;
281
282
  declare type ConsumeMessage = import("amqplib").ConsumeMessage;
283
+ declare type QueueOptions = import("amqplib").Options.AssertQueue;
282
284
  declare type ConsumeOptions = import("amqplib").Options.Consume;
283
285
 
284
286
  declare type ConsumerConfig = {
285
- options?: ConsumeOptions;
287
+ queue_options : QueueOptions;
288
+ consume_options?: ConsumeOptions;
286
289
  fn: ((msg: ConsumeMessage) => void);
287
290
  } & ThisType<ConsumerContext>;
288
291
  declare type ConsumerContext = {
@@ -338,9 +341,9 @@ declare type TopicHandlerMap = {
338
341
  }
339
342
 
340
343
  declare type MQConsumeOptions = {
341
- options?: ConsumeOptions;
344
+ queue_options? : QueueOptions;
345
+ consume_options?: ConsumeOptions;
342
346
  key? : string;
343
- queue?: string[];
344
347
  fn: MQRequestHandler;
345
348
  }
346
349