-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
70 lines (57 loc) · 1.61 KB
/
index.mjs
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
import fastify from 'fastify';
import fetch from 'node-fetch';
import { defaultProvider } from '@aws-sdk/credential-provider-node';
import aws4 from 'aws4';
const host = 'search-whisperserverse-ly7cuzks4cti-25v5p7hn7dn2j5zhjbj4jeawbe.us-west-2.es.amazonaws.com';
const region = 'us-west-2';
const service = 'es';
const app = fastify({ logger: true });
app.removeAllContentTypeParsers();
app.addContentTypeParser('*', (req, payload, done) => {
let data = '';
payload.on('data', chunk => { data += chunk });
payload.on('end', () => {
done(null, data);
});
});
const provider = defaultProvider();
app.all('/*', async (request, reply) => {
const credentials = await provider();
const path = request.url;
delete request.headers.host;
delete request.headers.referer;
const opts = {
method: request.method,
body: request.method !== 'GET' ? request.body : undefined,
headers: {
...request.headers,
},
host,
path,
region,
service,
};
aws4.sign(opts, credentials);
const res = await fetch(`https://${host}${path}`, {
method: request.method,
headers: {
...request.headers,
...opts.headers,
},
body: request.method !== 'GET' ? request.body : undefined,
});
let body = '';
if (res.headers.get('content-type') === 'application/json') {
body = await res.json();
} else {
body = Buffer.from(await res.arrayBuffer());
}
for (const [key, value] of res.headers.entries()) {
if (key === 'content-encoding') {
continue;
}
reply.header(key, value);
}
reply.send(body);
});
app.listen({ port: 9200, host: '0.0.0.0' });