-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
211 lines (174 loc) · 4.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict';
const assign = require('lodash.assign');
const aws = require('aws-sdk');
const uuid = require('uuid');
const Client = function Client (opts = {}) {
if (!(this instanceof Client)) {
return new Client(opts);
}
const { region = 'us-standard', accessKeyId, secretAccessKey, queue } = opts;
if (!accessKeyId || !secretAccessKey || !queue) {
throw new Error('Missing a required parameter: accessKeyId, secretAccessKey, or queue');
}
this.options = opts;
this.sqs = new aws.SQS({
apiVersion: '2016-07-19',
region,
accessKeyId,
secretAccessKey,
params: {
QueueUrl: queue
}
});
};
/*
* Send a message to the SQS queue
*
* @param payload - An object containing the message data
*/
Client.prototype.sendMessage = function sendMessage (payload, options = {}) {
if (!payload) {
throw new Error('Messages must have a payload.');
}
options.MessageBody = JSON.stringify(payload);
return new Promise((resolve, reject) => {
this.sqs.sendMessage(options, function (err, data) {
if (err) {
return reject(err);
}
return resolve(data);
});
});
};
/*
* Send an array of messages (up to 10) to the SQS queue
*
* @param payloads - An array of objects containing the message data
*/
Client.prototype.sendMessageBatch = function sendMessageBatch (payloads, options = {}) {
if (!payloads || !Array.isArray(payloads)) {
throw new Error('You must pass in an array of payloads.');
}
const Entries = payloads.map(function (payload) {
return {
...options,
Id: uuid.v4(),
MessageBody: JSON.stringify(payload)
};
});
return new Promise((resolve, reject) => {
this.sqs.sendMessageBatch({ Entries }, function (err, data) {
if (err) {
return reject(err);
}
return resolve(data);
});
});
};
/*
* Poll the SQS queue for new messages
*
* @param opts - An object to pass directly to the aws `receiveMessage` method
* @param handler - A function that should return a promise when passed a message
*/
Client.prototype.pollQueue = function pollQueue (opts = {}, handler) {
if (!this.receiveOptions) {
this.receiveOptions = assign({
AttributeNames: ['All'],
MessageAttributeNames: ['All'],
WaitTimeSeconds: 20
}, opts);
}
if (!this.handler) {
this.handler = handler;
}
const self = this;
this.sqs.receiveMessage(this.receiveOptions, (err, data) => {
if (err) {
// Not needed
}
const promises = [];
if (data && data.Messages) {
data.Messages.forEach(function (message) {
promises.push(this.handleMessage(message, this.handler));
}, this);
}
Promise.all(promises).then(function () {
setImmediate(function () {
self.pollQueue();
});
}, function () {
setImmediate(function () {
self.pollQueue();
});
});
});
};
/*
* Interacts with the handler to process a message
*
* @param message - A message returned from SQS
* @param handler - The message handler that will return a promise
*/
Client.prototype.handleMessage = function handleMessage (message, handler) {
const { preventVisibilityTimeoutRemoval, visibilityTimeoutOnError } = this.options;
const messagePromise = Promise.resolve().then(function () {
const body = JSON.parse(message.Body);
return handler(body, message);
});
return messagePromise.then(() => {
return this.deleteMessage(message.ReceiptHandle);
}).catch(() => {
if (preventVisibilityTimeoutRemoval === true) {
return Promise.resolve();
}
if (visibilityTimeoutOnError) {
return this.changeVisibilityTimeout(message, visibilityTimeoutOnError);
}
return this.removeVisibilityTimeout(message);
});
};
/*
* Change the visibility timeout of a message
*
* @param message - A message returned from SQS
* @param newVisibilityTimeout - The new visibility timeout measured in seconds
*/
Client.prototype.changeVisibilityTimeout = function (message, newVisibilityTimeout) {
return new Promise((resolve, reject) => {
this.sqs.changeMessageVisibility({
ReceiptHandle: message.ReceiptHandle,
VisibilityTimeout: newVisibilityTimeout
}, function (err, data) {
if (err) {
return reject(err);
}
return resolve(data);
});
});
};
Client.prototype.removeVisibilityTimeout = function removeVisibilityTimeout (message) {
return new Promise((resolve) => {
this.sqs.changeMessageVisibility({
ReceiptHandle: message.ReceiptHandle,
VisibilityTimeout: 0
}, function () {
resolve();
});
});
};
/*
* Deletes a message from the SQS queue
*
* @param receipt - A message receipt returned from SQS on `receiveMessage`
*/
Client.prototype.deleteMessage = function deleteMessage (receipt) {
return new Promise((resolve) => {
this.sqs.deleteMessage({
ReceiptHandle: receipt
}, function () {
resolve();
});
});
};
module.exports = Client;