-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjunk.txt
75 lines (69 loc) · 2.29 KB
/
junk.txt
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
// code to deal with the configuration secrets which are stored on S3
import {S3, AWSError} from 'aws-sdk';
export type Configuration = { [key: string]: string };
export type ConfigurationConsumer = (o: Configuration) => void;
export type ConfigurationProducer = (consumer: ConfigurationConsumer) => void;
export const getConfig: ConfigurationProducer = (callback: ConfigurationConsumer) => {
const s3 = new S3({ apiVersion: '2006-03-01' });
const params = {
Bucket: "extended-stats-aws",
Key: "config.txt"
};
console.log(params);
s3.getObject(params, (err: AWSError, data: S3.Types.GetObjectOutput) => {
console.log(err);
console.log(data);
if (err) {
console.log(err);
console.log("Unable to read config file.");
callback({});
} else {
const s = data.Body ? data.Body.toString() : "";
const re = /[\r\n]+/;
const lines = s.split(re);
const result = {};
lines.forEach(line => {
const fields = line.trim().split("=");
if (fields.length === 2) {
result[fields[0].trim()] = fields[1].trim();
}
});
console.log(result);
callback(result);
}
});
};
function assemble(callback: Callback): GetProcessor {
return (response: IncomingMessage) => {
const data: Buffer[] = [];
response.on('error', (err: Error) => {
console.log("got error in assemble");
return callback(err);
});
response.on('data', (chunk: Buffer) => {
data.push(chunk);
});
response.on('end', () => {
callback(null, Buffer.concat(data));
});
}
}
function assembleJson(callback: Callback): GetProcessor {
return assemble((error?: Error | null, result?: object) => {
if (error) {
callback(error)
} else {
callback(null, JSON.parse(result.toString()));
}
});
}
export function makeAPIGetRequest(path: string): object {
const apiServer = process.env["apiServer"];
const apiKey = process.env["apiKey"];
return {
url: 'https://' + apiServer + path,
headers: {
"x-api-key": apiKey
}
};
}