-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
298 lines (224 loc) · 7.73 KB
/
main.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"use strict";
var crypto = require("crypto");
var zlib = require("zlib");
var Promise = require("bluebird");
var AWS = require("aws-sdk");
var createError = require("custom-error-generator");
var _ = require("lodash");
var MessageError = createError('MessageError');
var InvalidArgumentError = createError('InvalidArgumentError');
/**
* Service Bus factory
* @param {String} queueUrl
* @param {Object} options
* @returns {Object}
*/
function serviceBusFactory(queueUrl, options) {
validate(queueUrl, options);
var _delegate;
var _isPolling = false;
var _queueUrl = queueUrl;
var _sqs = new AWS.SQS({
accessKeyId: options.accessKeyId,
secretAccessKey: options.secretAccessKey,
region: options.region
});
var receiveMessagesAsync = Promise.promisify(_sqs.receiveMessage, _sqs);
var deleteMessageAsync = Promise.promisify(_sqs.deleteMessage, _sqs);
var sendMessageAsync = Promise.promisify(_sqs.sendMessage, _sqs);
function acknowledge(message, callback) {
return deleteMessageAsync({
QueueUrl: _queueUrl,
ReceiptHandle: message.receiptHandle
}).nodeify(callback);
}
function publish(data, callback) {
var bufferHashHex;
return toBuffer(data)
.then(function(buffer) {
bufferHashHex = md5(buffer);
return buffer;
})
.then(zipBuffer)
.then(function (zippedBuffer) {
return sendMessageAsync({
QueueUrl: _queueUrl,
MessageBody: bufferHashHex,
DelaySeconds: 0,
MessageAttributes: {
data: {
DataType: "Binary",
BinaryValue: zippedBuffer
}
}
});
})
.then(function (message) {
if (md5(bufferHashHex) !== message.MD5OfMessageBody) {
throw MessageError("Message body MD5 mismatch")
}
return bufferHashHex;
})
.nodeify(callback);
}
function subscribe(delegate, callback) {
return new Promise(function (resolve, reject) {
if (!delegate || typeof delegate !== "function") {
return reject(new Error("No delegate function provided"));
}
_delegate = delegate;
next();
return resolve();
}).nodeify(callback);
}
function poll() {
_isPolling = true;
var params = {
QueueUrl: _queueUrl,
MaxNumberOfMessages: 10,
VisibilityTimeout: 60,
WaitTimeSeconds: 10,
MessageAttributeNames: ["data"]
};
receiveMessagesAsync(params)
.then(function (data) {
_isPolling = false;
if (data && data.Messages) {
return parseMessages(data.Messages)
.then(function(messages) {
_delegate(messages, next);
});
}
else {
next();
}
})
.catch(function (err) {
_isPolling = false;
throw err;
});
}
function next() {
if (_isPolling) return;
poll();
}
function parseMessages(messages, callback) {
return Promise.resolve(messages)
.map(function(message) {
return Promise.resolve(message)
.then(function(message) {
if (!message.MessageAttributes ||
!message.MessageAttributes.data ||
!message.MessageAttributes.data.BinaryValue) {
throw MessageError("Message has invalid payload");
}
if (message.MD5OfBody !== md5(message.Body)) {
throw MessageError("Message body MD5 mismatch")
}
return unzipBuffer(message.MessageAttributes.data.BinaryValue)
.then(function(buffer) {
var bufferHashHex = md5(buffer);
if (bufferHashHex !== message.Body) {
throw MessageError("Message body MD5 mismatch")
}
return buffer;
})
.then(toObject);
})
.then(function(body) {
return {
messageId: message.MessageId,
receiptHandle: message.ReceiptHandle,
body: body
}
});
}).nodeify(callback);
}
function toBuffer(data, callback) {
return new Promise(function (resolve, reject) {
if (!_.isPlainObject(data)) {
return reject(new InvalidArgumentError("Input data has to be plain object"));
}
try {
var dataString = JSON.stringify(data);
var dataBuffer = new Buffer(dataString);
return resolve(dataBuffer);
}
catch (err) {
return reject(err);
}
}).nodeify(callback);
}
function toObject(buffer, callback) {
return new Promise(function (resolve, reject) {
try {
var data = JSON.parse(buffer);
return resolve(data);
}
catch (err) {
return reject(err);
}
}).nodeify(callback);
}
function zipBuffer(buffer, callback) {
return new Promise(function (resolve, reject) {
try {
zlib.gzip(buffer, function (err, res) {
if (err) {
return reject(err);
}
return resolve(res);
});
}
catch (err) {
return reject(err);
}
}).nodeify(callback);
}
function unzipBuffer(zippedBuffer, callback) {
return new Promise(function (resolve, reject) {
zlib.gunzip(zippedBuffer, function (err, res) {
if (err) {
return reject(err);
}
return resolve(res);
});
}).nodeify(callback);
}
function md5(data) {
var hash = crypto.createHash('md5');
hash.update(data);
return hash.digest('hex');
}
function hash(data, callback) {
return toBuffer(data)
.then(md5)
.nodeify(callback);
}
function validate(queueUrl, options) {
if (!_.isString(queueUrl)) {
throw InvalidArgumentError("No AWS SQS queue URL provided");
}
if (!options) {
throw InvalidArgumentError("No options provided");
}
if (!options.accessKeyId) {
throw InvalidArgumentError("No AWS 'accessKeyId' provided");
}
if (!options.secretAccessKey) {
throw InvalidArgumentError("No AWS 'secretAccessKey' provided");
}
if (!options.region) {
throw InvalidArgumentError("No AWS 'region' provided");
}
}
return {
hash: hash,
publish: publish,
subscribe: subscribe,
acknowledge: acknowledge,
InvalidArgumentError: InvalidArgumentError,
MessageError: MessageError
};
}
module.exports = serviceBusFactory;