-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathindex.js
60 lines (48 loc) · 1.68 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
process.env.AWS_NODEJS_CONNECTION_REUSE_ENABLED = '1'
const SQS = require('aws-sdk/clients/sqs')
const client = new SQS()
const CorrelationIds = require('@dazn/lambda-powertools-correlation-ids')
function addCorrelationIds (correlationIds, messageAttributes) {
const attributes = {}
const ids = correlationIds.get()
for (const key in ids) {
attributes[key] = {
DataType: 'String',
StringValue: `${ids[key]}`
}
}
// use `attributes` as base so if the user's message attributes would override
// our correlation IDs
return Object.assign(attributes, messageAttributes || {})
}
client._sendMessage = client.sendMessage
client.sendMessage = (...args) => {
return client.sendMessageWithCorrelationIds(CorrelationIds, ...args)
}
client.sendMessageWithCorrelationIds = (correlationIds, params, ...args) => {
const newMessageAttributes = addCorrelationIds(correlationIds, params.MessageAttributes)
const extendedParams = {
...params,
MessageAttributes: newMessageAttributes
}
return client._sendMessage(extendedParams, ...args)
}
client._sendMessageBatch = client.sendMessageBatch
client.sendMessageBatch = (...args) => {
return client.sendMessageBatchWithCorrelationIds(CorrelationIds, ...args)
}
client.sendMessageBatchWithCorrelationIds = (correlationIds, params, ...args) => {
const newEntries = params.Entries.map(entry => {
const newMessageAttributes = addCorrelationIds(correlationIds, entry.MessageAttributes)
return {
...entry,
MessageAttributes: newMessageAttributes
}
})
const extendedParams = {
...params,
Entries: newEntries
}
return client._sendMessageBatch(extendedParams, ...args)
}
module.exports = client