@efebia/sqs-queue-v2 0.0.2 → 0.0.3

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/lib/index.d.ts CHANGED
@@ -1,33 +1,22 @@
1
- /// <reference types="node" />
1
+ import { Queue as BaseQueue, Message, QueueOptions } from "@efebia/queue";
2
2
  import AWS from "aws-sdk";
3
- import EventEmitter from "events";
4
- export type QueueOptions = {
5
- sleepTimeout: number;
6
- errorCallback: (error: any) => void | Promise<void>;
7
- };
8
3
  export type QueueConstructorOptions = {
9
4
  client: AWS.SQS;
10
5
  receiveOptions: AWS.SQS.ReceiveMessageRequest;
6
+ canRead?: () => boolean | Promise<boolean>;
11
7
  } & Partial<QueueOptions>;
12
8
  export type QueueCallback<TMessage extends object> = (name: string, params: TMessage, state: {
13
9
  del: boolean;
14
10
  }) => void | Promise<void>;
15
- export declare class Queue extends EventEmitter {
11
+ export declare class Queue<TMessage extends object> extends BaseQueue<AWS.SQS.Message, TMessage> {
16
12
  receiveOptions: AWS.SQS.ReceiveMessageRequest;
17
13
  client: AWS.SQS;
18
- forever: boolean;
19
- options: Omit<QueueOptions, "client" | "receiveOptions">;
20
- constructor({ receiveOptions, client, ...otherOptions }: QueueConstructorOptions);
21
- start<TMessage extends object>(callback: QueueCallback<TMessage>, canThrow?: boolean): Promise<void>;
14
+ canRead: () => boolean | Promise<boolean>;
15
+ constructor({ receiveOptions, client, canRead, ...otherOptions }: QueueConstructorOptions);
16
+ readMessages(): Promise<AWS.SQS.MessageList | undefined>;
22
17
  processMessage<TMessage extends object>(message: AWS.SQS.Message, callback?: QueueCallback<TMessage>): Promise<void>;
23
18
  delete(ReceiptHandle: string): Promise<{
24
19
  $response: AWS.Response<{}, AWS.AWSError>;
25
20
  }>;
26
- stop(): void;
27
- resume(...args: Parameters<typeof this.start>): Promise<void>;
28
- isRunning(): boolean;
29
- send(body: {
30
- name: string;
31
- params: object;
32
- }): Promise<import("aws-sdk/lib/request").PromiseResult<AWS.SQS.SendMessageResult, AWS.AWSError>>;
21
+ sendMessage(body: Message<TMessage>): Promise<void>;
33
22
  }
package/lib/index.js CHANGED
@@ -10,49 +10,30 @@ var __rest = (this && this.__rest) || function (s, e) {
10
10
  }
11
11
  return t;
12
12
  };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
13
  Object.defineProperty(exports, "__esModule", { value: true });
17
14
  exports.Queue = void 0;
18
- const events_1 = __importDefault(require("events"));
19
- const packageName = '@efebia/sqs-queue-v2';
20
- const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
21
- class Queue extends events_1.default {
15
+ const queue_1 = require("@efebia/queue");
16
+ const packageName = "@efebia/sqs-queue-v2";
17
+ class Queue extends queue_1.Queue {
22
18
  constructor(_a) {
23
- var { receiveOptions, client } = _a, otherOptions = __rest(_a, ["receiveOptions", "client"]);
24
- super();
19
+ var { receiveOptions, client, canRead } = _a, otherOptions = __rest(_a, ["receiveOptions", "client", "canRead"]);
20
+ super(otherOptions);
25
21
  const options = Object.assign({
26
22
  WaitTimeSeconds: 20,
27
23
  MaxNumberOfMessages: 1,
28
24
  }, receiveOptions);
29
- this.forever = true;
30
25
  this.receiveOptions = options;
31
26
  this.client = client;
32
- this.options = Object.assign({
33
- sleepTimeout: 5000,
34
- errorCallback: (e) => console.error(e)
35
- }, otherOptions);
27
+ this.canRead = canRead !== null && canRead !== void 0 ? canRead : (() => Promise.resolve(true));
36
28
  }
37
- async start(callback, canThrow = false) {
38
- while (this.forever) {
39
- try {
40
- const { Messages } = await this.client
41
- .receiveMessage(this.receiveOptions)
42
- .promise();
43
- if (Messages == undefined)
44
- continue;
45
- for (const message of Messages) {
46
- await this.processMessage(message, callback);
47
- }
48
- }
49
- catch (error) {
50
- if (canThrow)
51
- throw error;
52
- await this.options.errorCallback(error);
53
- await sleep(this.options.sleepTimeout);
54
- }
55
- }
29
+ async readMessages() {
30
+ const readable = await this.canRead();
31
+ if (!readable)
32
+ return undefined;
33
+ const { Messages } = await this.client
34
+ .receiveMessage(this.receiveOptions)
35
+ .promise();
36
+ return Messages;
56
37
  }
57
38
  async processMessage(message, callback) {
58
39
  const { ReceiptHandle, Body: stringifiedBody } = message;
@@ -70,14 +51,11 @@ class Queue extends events_1.default {
70
51
  }
71
52
  const { name, params } = parsedBody;
72
53
  const state = { del: true };
73
- if (callback) {
74
- await callback(name, params, state);
75
- if (state.del) {
76
- await this.delete(ReceiptHandle);
77
- }
78
- }
79
- else {
80
- this.emit(name, params, ReceiptHandle);
54
+ if (!callback)
55
+ return;
56
+ await callback(name, params, state);
57
+ if (state.del) {
58
+ await this.delete(ReceiptHandle);
81
59
  }
82
60
  }
83
61
  delete(ReceiptHandle) {
@@ -85,20 +63,13 @@ class Queue extends events_1.default {
85
63
  .deleteMessage({ QueueUrl: this.receiveOptions.QueueUrl, ReceiptHandle })
86
64
  .promise();
87
65
  }
88
- stop() {
89
- this.forever = false;
90
- }
91
- resume(...args) {
92
- this.forever = true;
93
- return this.start(...args);
94
- }
95
- isRunning() {
96
- return this.forever;
97
- }
98
- send(body) {
99
- const updatedBody = Object.assign(Object.assign({}, body), { params: Object.assign(Object.assign({}, body.params), { createdAt: new Date() }) });
100
- const MessageBody = JSON.stringify(updatedBody);
101
- return this.client.sendMessage({ QueueUrl: this.receiveOptions.QueueUrl, MessageBody }).promise();
66
+ async sendMessage(body) {
67
+ await this.client
68
+ .sendMessage({
69
+ QueueUrl: this.receiveOptions.QueueUrl,
70
+ MessageBody: JSON.stringify(body),
71
+ })
72
+ .promise();
102
73
  }
103
74
  }
104
75
  exports.Queue = Queue;
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@efebia/sqs-queue-v2",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "license": "MIT",
5
5
  "devDependencies": {
6
6
  "@types/node": "^18.11.18",
7
7
  "typescript": "^4.9.4"
8
8
  },
9
9
  "dependencies": {
10
+ "@efebia/queue": "^0.0.1",
10
11
  "aws-sdk": "^2.1292.0"
11
12
  },
12
13
  "files": [