-
Notifications
You must be signed in to change notification settings - Fork 1
/
flag-getter.ts
30 lines (25 loc) · 865 Bytes
/
flag-getter.ts
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
import { S3 } from "aws-sdk";
import { S3Loader } from "./s3-loader";
const dotenv = require("dotenv");
export class FlagGetter extends S3Loader {
private expire: number;
private expiration: number = Date.now();
private data: any;
constructor(options?: S3.Types.ClientConfiguration & { s3Options: { expire?: number, Bucket: string, Key: string } }) {
super(options);
this.expire = (options.s3Options.expire || 60 * 60) * 1000;
}
public async getFlag() {
if (this.expiration < Date.now()) {
try {
this.data = await this.transformFlagFileToObject();
this.expiration = Date.now() + this.expire;
} catch (e) {
}
}
return this.data;
}
private transformFlagFileToObject(): Promise<object> {
return this.getImageFromS3().then((data: S3.Types.GetObjectOutput) => dotenv.parse(data.Body));
}
}