-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathapp.js
More file actions
127 lines (106 loc) · 2.97 KB
/
Copy pathapp.js
File metadata and controls
127 lines (106 loc) · 2.97 KB
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
/*
* (c) Copyright IBM Corp. 2023
*/
'use strict';
// NOTE: c8 bug https://github.com/bcoe/c8/issues/166
process.on('SIGTERM', () => {
process.disconnect();
process.exit(0);
});
require('../../../../../..')();
const express = require('express');
const app = express();
const agentPort = process.env.INSTANA_AGENT_PORT;
const port = require('../../../../../test_util/app-port')();
const sns = require('@aws-sdk/client-sns');
const { StandardRetryStrategy } = require('@aws-sdk/middleware-retry');
const logPrefix = `AWS SDK v3 SNS (${process.pid}):\t`;
const log = require('@instana/core/test/test_util/log').getLogger(logPrefix);
const maxAttempts = 6;
const customRetryStrategy = new StandardRetryStrategy(async () => maxAttempts, {
retryDecider: err => {
// eslint-disable-next-line no-console
console.log('Not connected to LocalStack, retrying...', err.code);
return true;
},
delayDecider: () => 5000
});
const clientOpts = {
credentials: {
accessKeyId: 'test',
secretAccessKey: 'test'
},
endpoint: process.env.LOCALSTACK_AWS,
region: 'us-east-2',
retryStrategy: customRetryStrategy
};
const client = new sns.SNSClient(clientOpts);
const clientV2 = new sns.SNS(clientOpts);
const addMsgAttributes = (options, msgattrs) => {
options.MessageAttributes = {};
for (let i = 0; i < msgattrs; i++) {
options.MessageAttributes[`dummy-attribute-${i}`] = {
DataType: 'String',
StringValue: `dummy value ${i}`
};
}
};
const defaultStyle = async (Command, options) => {
return client.send(new Command(options));
};
const callbackStyle = (Command, options, cb) => {
client.send(new Command(options), cb);
};
const v2Style = async (command, options) => {
let fn = command.match(/(.*)Command$/)[1];
fn = fn[0].toLowerCase() + fn.slice(1);
return clientV2[fn](options);
};
async function executeCommand(options) {
const Command = sns[options.command];
const style = options.style;
const command = options.command;
const msgattrs = options.msgattrs || 0;
delete options.command;
delete options.style;
delete options.msgattrs;
addMsgAttributes(options, msgattrs);
if (style === 'callback') {
return new Promise((resolve, reject) => {
callbackStyle(Command, options, (err, data) => {
if (err) return reject(err);
resolve(data);
});
});
} else if (style === 'v2') {
return v2Style(command, options);
}
return defaultStyle(Command, options);
}
function httpError(res, err) {
res.status(500).send({
status: 'failed',
error: err
});
}
function httpSuccess(res, data) {
res.send({
status: 'ok',
data: data
});
}
app.get('/execute', async (req, res) => {
try {
const data = await executeCommand(req.query);
await fetch(`http://127.0.0.1:${agentPort}`);
httpSuccess(res, data);
} catch (err) {
httpError(res, err);
}
});
app.get('/', (_req, res) => {
res.send('Ok');
});
app.listen(port, () => {
log(`AWS SNS v3 test app started at port ${port}`);
});