-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (36 loc) · 1.13 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
const H = require ( 'highland' );
const AWS = require ( 'aws-sdk' );
module.exports = ( { awsConfig, count, queueUrl, visibilityTimeout, waitTimeSeconds, maxRetry } = { } ) => {
const SQS = new AWS.SQS ( {
apiVersion: '2012-11-05',
...awsConfig
} );
const generator = ( attempt ) => {
return H ( ( push, next ) => {
return H.wrapCallback ( SQS.receiveMessage.bind ( SQS ) ) ( {
MaxNumberOfMessages: count || 5,
QueueUrl: queueUrl,
VisibilityTimeout: visibilityTimeout || 0,
WaitTimeSeconds: waitTimeSeconds || 0
} )
.each ( response => {
const items = response.Messages || [];
if ( items.length ) {
items.forEach ( item => {
push ( null, item );
} );
return setTimeout ( () => {
next ( generator ( 0 ) );
}, 0 );
}
if ( attempt < ( maxRetry || 3 ) ) {
return setTimeout ( () => {
next ( generator ( attempt + 1 ) );
}, 0 );
}
return push ( null, H.nil );
} )
} );
}
return generator ( 0 );
};