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