-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
PlatformAws.ts
80 lines (66 loc) · 2.19 KB
/
PlatformAws.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
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
import {PlatformBuilder} from "@tsed/common";
import {Type} from "@tsed/core";
import {createServer, proxy} from "aws-serverless-express";
declare global {
namespace TsED {
interface Configuration {
aws: {
binaryMimeTypes: string[];
};
}
}
}
// NOTE: If you get ERR_CONTENT_DECODING_FAILED in your browser, this is likely
// due to a compressed response (e.g. gzip) which has not been handled correctly
// by aws-serverless-express and/or API Gateway. Add the necessary MIME types to
// binaryMimeTypes below, then redeploy (`npm run package-deploy`)
export const BYNARY_MIME_TYPES = [
"application/javascript",
"application/json",
"application/octet-stream",
"application/xml",
"font/eot",
"font/opentype",
"font/otf",
"image/jpeg",
"image/png",
"image/svg+xml",
"text/comma-separated-values",
"text/css",
"text/html",
"text/javascript",
"text/plain",
"text/text",
"text/xml"
];
export class PlatformAws {
public static promise: Promise<any>;
public static platform: PlatformBuilder;
public static awsServer: any;
static bootstrap(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {
// istanbul ignore next
PlatformBuilder.currentPlatform = settings.platform || PlatformBuilder.currentPlatform;
const platform = PlatformBuilder.currentPlatform.create(module, settings);
this.promise = platform.bootstrap().then(PlatformAws.onInit);
return PlatformAws;
}
static callback() {
return async (event: any, context: any) => {
await PlatformAws.promise;
return proxy(PlatformAws.awsServer, event, context, "PROMISE").promise;
};
}
protected static async onInit(platform: PlatformBuilder) {
PlatformAws.platform = platform;
const binaryMimeTypes = platform.settings.get("aws.binaryMimeTypes", BYNARY_MIME_TYPES);
await platform.callHook("$beforeListen");
// create Aws server
PlatformAws.awsServer = createServer(platform.callback(), PlatformAws.onListen, binaryMimeTypes);
}
protected static async onListen() {
const {platform} = PlatformAws;
await platform.callHook("$afterListen");
await platform.injector.emit("$afterListen");
await platform.ready();
}
}