@fgiova/sqs-consumer 2.1.4 → 2.2.1

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.
@@ -1,7 +1,7 @@
1
1
  /** biome-ignore-all lint/complexity/noBannedTypes: Function required as generic */
2
2
  import type { Message } from "@fgiova/mini-sqs-client";
3
3
  import type { Logger } from "./logger";
4
- export type HookName = "onPoll" | "onMessage" | "onHandlerSuccess" | "onHandlerTimeout" | "onHandlerError" | "onSuccess" | "onError" | "onSQSError";
4
+ export type HookName = "onStart" | "onStop" | "onPoll" | "onMessage" | "onHandlerSuccess" | "onHandlerTimeout" | "onHandlerError" | "onSuccess" | "onError" | "onSQSError";
5
5
  export declare class Hooks {
6
6
  private readonly hookSymbols;
7
7
  private readonly hooks;
package/dist/cjs/hooks.js CHANGED
@@ -4,6 +4,16 @@ exports.Hooks = void 0;
4
4
  class Hooks {
5
5
  constructor(logger = console) {
6
6
  this.hookSymbols = {
7
+ onStart: {
8
+ S: Symbol("start"),
9
+ throwable: false,
10
+ type: "void",
11
+ },
12
+ onStop: {
13
+ S: Symbol("stop"),
14
+ throwable: false,
15
+ type: "void",
16
+ },
7
17
  onPoll: {
8
18
  S: Symbol("poll"),
9
19
  throwable: true,
@@ -47,6 +57,8 @@ class Hooks {
47
57
  };
48
58
  this.logger = logger;
49
59
  this.hooks = {
60
+ [this.hookSymbols.onStart.S]: [],
61
+ [this.hookSymbols.onStop.S]: [],
50
62
  [this.hookSymbols.onPoll.S]: [],
51
63
  [this.hookSymbols.onMessage.S]: [],
52
64
  [this.hookSymbols.onHandlerSuccess.S]: [],
@@ -38,7 +38,7 @@ export type HooksOptions = {
38
38
  };
39
39
  export type SQSConsumerOptions = {
40
40
  queueARN: string;
41
- handler: (message: Message) => Promise<any>;
41
+ handler: (message: Message) => Promise<unknown>;
42
42
  logger?: Logger;
43
43
  autostart?: boolean;
44
44
  handlerOptions?: HandlerOptions;
@@ -68,6 +68,8 @@ export declare class SQSConsumer {
68
68
  start(): Promise<void>;
69
69
  stop(destroy?: boolean): Promise<void>;
70
70
  get isRunning(): boolean;
71
+ addHook(hookName: "onStart", value: (sqsConsumer: SQSConsumerOptions) => void): this;
72
+ addHook(hookName: "onStop", value: (sqsConsumer: SQSConsumerOptions) => void): this;
71
73
  addHook(hookName: "onPoll", value: (messages: Message[]) => Promise<Message[]>): this;
72
74
  addHook(hookName: "onMessage", value: (message: Message) => Promise<Message>): this;
73
75
  addHook(hookName: "onHandlerSuccess", value: (message: Message) => Promise<Message>): this;
package/dist/cjs/index.js CHANGED
@@ -80,7 +80,7 @@ class SQSConsumer {
80
80
  try {
81
81
  let handlerResult;
82
82
  if (this.handlerOptions.executionTimeout) {
83
- const response = await unpromise_1.Unpromise.race([
83
+ handlerResult = await unpromise_1.Unpromise.race([
84
84
  handler(message),
85
85
  new Promise((_resolve, reject) => {
86
86
  setTimeout(() => {
@@ -88,7 +88,6 @@ class SQSConsumer {
88
88
  }, this.handlerOptions.executionTimeout);
89
89
  }),
90
90
  ]);
91
- handlerResult = response;
92
91
  }
93
92
  else {
94
93
  handlerResult = await handler(message);
@@ -194,25 +193,34 @@ class SQSConsumer {
194
193
  this.polling = false;
195
194
  await this.pollMessages();
196
195
  }
197
- // eslint-disable-next-line @typescript-eslint/require-await
198
196
  async start() {
199
- if (this.running)
200
- throw new Error("Consumer is already running");
201
- if (this.destroyed)
202
- throw new Error("Consumer is destroyed");
203
- this.running = true;
204
- void this.pollMessages();
197
+ try {
198
+ if (this.running)
199
+ throw new Error("Consumer is already running");
200
+ if (this.destroyed)
201
+ throw new Error("Consumer is destroyed");
202
+ this.running = true;
203
+ void this.pollMessages();
204
+ }
205
+ finally {
206
+ await this.hooks.runHook("onStart", this);
207
+ }
205
208
  }
206
209
  async stop(destroy = false) {
207
- if (!this.running)
208
- throw new Error("Consumer is not running");
209
- this.running = false;
210
- while (this.messagesOnFly > 0 || this.polling) {
211
- await (0, promises_1.setTimeout)(500);
210
+ try {
211
+ if (!this.running)
212
+ throw new Error("Consumer is not running");
213
+ this.running = false;
214
+ while (this.messagesOnFly > 0 || this.polling) {
215
+ await (0, promises_1.setTimeout)(500);
216
+ }
217
+ if (destroy) {
218
+ await this.sqsClient.destroy(this.clientOptions.destroySigner);
219
+ this.destroyed = true;
220
+ }
212
221
  }
213
- if (destroy) {
214
- await this.sqsClient.destroy(this.clientOptions.destroySigner);
215
- this.destroyed = true;
222
+ finally {
223
+ await this.hooks.runHook("onStop", this);
216
224
  }
217
225
  }
218
226
  /* c8 ignore next 3 */
@@ -1,7 +1,7 @@
1
1
  /** biome-ignore-all lint/complexity/noBannedTypes: Function required as generic */
2
2
  import type { Message } from "@fgiova/mini-sqs-client";
3
3
  import type { Logger } from "./logger.mjs";
4
- export type HookName = "onPoll" | "onMessage" | "onHandlerSuccess" | "onHandlerTimeout" | "onHandlerError" | "onSuccess" | "onError" | "onSQSError";
4
+ export type HookName = "onStart" | "onStop" | "onPoll" | "onMessage" | "onHandlerSuccess" | "onHandlerTimeout" | "onHandlerError" | "onSuccess" | "onError" | "onSQSError";
5
5
  export declare class Hooks {
6
6
  private readonly hookSymbols;
7
7
  private readonly hooks;
@@ -1,6 +1,16 @@
1
1
  export class Hooks {
2
2
  constructor(logger = console) {
3
3
  this.hookSymbols = {
4
+ onStart: {
5
+ S: Symbol("start"),
6
+ throwable: false,
7
+ type: "void",
8
+ },
9
+ onStop: {
10
+ S: Symbol("stop"),
11
+ throwable: false,
12
+ type: "void",
13
+ },
4
14
  onPoll: {
5
15
  S: Symbol("poll"),
6
16
  throwable: true,
@@ -44,6 +54,8 @@ export class Hooks {
44
54
  };
45
55
  this.logger = logger;
46
56
  this.hooks = {
57
+ [this.hookSymbols.onStart.S]: [],
58
+ [this.hookSymbols.onStop.S]: [],
47
59
  [this.hookSymbols.onPoll.S]: [],
48
60
  [this.hookSymbols.onMessage.S]: [],
49
61
  [this.hookSymbols.onHandlerSuccess.S]: [],
@@ -38,7 +38,7 @@ export type HooksOptions = {
38
38
  };
39
39
  export type SQSConsumerOptions = {
40
40
  queueARN: string;
41
- handler: (message: Message) => Promise<any>;
41
+ handler: (message: Message) => Promise<unknown>;
42
42
  logger?: Logger;
43
43
  autostart?: boolean;
44
44
  handlerOptions?: HandlerOptions;
@@ -68,6 +68,8 @@ export declare class SQSConsumer {
68
68
  start(): Promise<void>;
69
69
  stop(destroy?: boolean): Promise<void>;
70
70
  get isRunning(): boolean;
71
+ addHook(hookName: "onStart", value: (sqsConsumer: SQSConsumerOptions) => void): this;
72
+ addHook(hookName: "onStop", value: (sqsConsumer: SQSConsumerOptions) => void): this;
71
73
  addHook(hookName: "onPoll", value: (messages: Message[]) => Promise<Message[]>): this;
72
74
  addHook(hookName: "onMessage", value: (message: Message) => Promise<Message>): this;
73
75
  addHook(hookName: "onHandlerSuccess", value: (message: Message) => Promise<Message>): this;
@@ -74,7 +74,7 @@ export class SQSConsumer {
74
74
  try {
75
75
  let handlerResult;
76
76
  if (this.handlerOptions.executionTimeout) {
77
- const response = await Unpromise.race([
77
+ handlerResult = await Unpromise.race([
78
78
  handler(message),
79
79
  new Promise((_resolve, reject) => {
80
80
  setTimeout(() => {
@@ -82,7 +82,6 @@ export class SQSConsumer {
82
82
  }, this.handlerOptions.executionTimeout);
83
83
  }),
84
84
  ]);
85
- handlerResult = response;
86
85
  }
87
86
  else {
88
87
  handlerResult = await handler(message);
@@ -188,25 +187,34 @@ export class SQSConsumer {
188
187
  this.polling = false;
189
188
  await this.pollMessages();
190
189
  }
191
- // eslint-disable-next-line @typescript-eslint/require-await
192
190
  async start() {
193
- if (this.running)
194
- throw new Error("Consumer is already running");
195
- if (this.destroyed)
196
- throw new Error("Consumer is destroyed");
197
- this.running = true;
198
- void this.pollMessages();
191
+ try {
192
+ if (this.running)
193
+ throw new Error("Consumer is already running");
194
+ if (this.destroyed)
195
+ throw new Error("Consumer is destroyed");
196
+ this.running = true;
197
+ void this.pollMessages();
198
+ }
199
+ finally {
200
+ await this.hooks.runHook("onStart", this);
201
+ }
199
202
  }
200
203
  async stop(destroy = false) {
201
- if (!this.running)
202
- throw new Error("Consumer is not running");
203
- this.running = false;
204
- while (this.messagesOnFly > 0 || this.polling) {
205
- await setTimeoutAsync(500);
204
+ try {
205
+ if (!this.running)
206
+ throw new Error("Consumer is not running");
207
+ this.running = false;
208
+ while (this.messagesOnFly > 0 || this.polling) {
209
+ await setTimeoutAsync(500);
210
+ }
211
+ if (destroy) {
212
+ await this.sqsClient.destroy(this.clientOptions.destroySigner);
213
+ this.destroyed = true;
214
+ }
206
215
  }
207
- if (destroy) {
208
- await this.sqsClient.destroy(this.clientOptions.destroySigner);
209
- this.destroyed = true;
216
+ finally {
217
+ await this.hooks.runHook("onStop", this);
210
218
  }
211
219
  }
212
220
  /* c8 ignore next 3 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgiova/sqs-consumer",
3
- "version": "2.1.4",
3
+ "version": "2.2.1",
4
4
  "main": "./dist/cjs/index.js",
5
5
  "types": "./dist/cjs/index.d.ts",
6
6
  "exports": {
@@ -56,12 +56,12 @@
56
56
  ]
57
57
  },
58
58
  "dependencies": {
59
- "@fgiova/mini-sqs-client": "^3.2.2",
59
+ "@fgiova/mini-sqs-client": "^3.4.0",
60
60
  "@watchable/unpromise": "^1.0.2",
61
61
  "p-map": "^7.0.3"
62
62
  },
63
63
  "peerDependencies": {
64
- "@fgiova/aws-signature": "^3.1.2"
64
+ "@fgiova/aws-signature": "^3.2.0"
65
65
  },
66
66
  "devDependencies": {
67
67
  "@aws-sdk/client-sqs": "^3.758.0",