@clairejs/server 3.22.10 → 3.22.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/system/LambdaWrapper.d.ts +1 -0
- package/dist/system/LambdaWrapper.js +20 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,6 +12,7 @@ export declare class LambdaWrapper {
|
|
|
12
12
|
protected readonly serverFactory: () => Promise<ClaireServer>;
|
|
13
13
|
protected readonly requestMapper: (event: any) => RequestOptions | undefined;
|
|
14
14
|
protected readonly config?: LambdaWrapperConfig | undefined;
|
|
15
|
+
private logger?;
|
|
15
16
|
private httpRequestHandler?;
|
|
16
17
|
private socketManager?;
|
|
17
18
|
private jobScheduler?;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getServiceProvider, HttpMethod, SocketMethod, Errors } from "@clairejs/core";
|
|
1
|
+
import { getServiceProvider, HttpMethod, SocketMethod, Errors, AbstractLogger } from "@clairejs/core";
|
|
2
2
|
import { AbstractHttpRequestHandler } from "../http/controller/AbstractHttpRequestHandler";
|
|
3
3
|
import { AbstractServerSocketManager } from "../socket/AbstractServerSocketManager";
|
|
4
4
|
import { CRON_REQUEST_METHOD } from "../job/interfaces";
|
|
@@ -66,6 +66,7 @@ export class LambdaWrapper {
|
|
|
66
66
|
serverFactory;
|
|
67
67
|
requestMapper;
|
|
68
68
|
config;
|
|
69
|
+
logger;
|
|
69
70
|
httpRequestHandler;
|
|
70
71
|
socketManager;
|
|
71
72
|
jobScheduler;
|
|
@@ -80,30 +81,34 @@ export class LambdaWrapper {
|
|
|
80
81
|
if (!this._server) {
|
|
81
82
|
this._server = await this.serverFactory();
|
|
82
83
|
const injector = getServiceProvider().getInjector();
|
|
84
|
+
this.logger = injector.resolveOptional(AbstractLogger);
|
|
83
85
|
this.socketManager = injector.resolveOptional(AbstractServerSocketManager);
|
|
84
86
|
this.httpRequestHandler = injector.resolveOptional(AbstractHttpRequestHandler);
|
|
85
87
|
this.jobScheduler = injector.resolveOptional(AbstractJobScheduler);
|
|
86
88
|
await injector.initInstances();
|
|
87
89
|
this.socketManager?.configure();
|
|
88
90
|
await this._server.init();
|
|
91
|
+
this.logger?.info("Server init finish");
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
this.logger?.info("Server already booted, reusing");
|
|
89
95
|
}
|
|
90
96
|
}
|
|
91
97
|
async handler(event) {
|
|
92
|
-
await this.bootServer();
|
|
93
|
-
//-- handle http otherwise
|
|
94
98
|
const requestOptions = this.requestMapper(event);
|
|
95
|
-
|
|
96
|
-
throw Errors.SYSTEM_ERROR("Cannot resolve event");
|
|
97
|
-
}
|
|
98
|
-
const corsHeaders = convertCorsToHeaders(requestOptions.headers?.origin || "", this.httpRequestHandler?.corsConfig);
|
|
99
|
-
if (requestOptions.method === HttpMethod.OPTIONS) {
|
|
100
|
-
return toApiGatewayFormat({ code: 200, headers: corsHeaders, cookies: {} });
|
|
101
|
-
}
|
|
102
|
-
if (requestOptions.method === CRON_REQUEST_METHOD) {
|
|
103
|
-
await this.jobScheduler?.handleCron(requestOptions.body);
|
|
104
|
-
return toApiGatewayFormat({ code: 200, headers: corsHeaders, cookies: {} });
|
|
105
|
-
}
|
|
99
|
+
const corsHeaders = convertCorsToHeaders(requestOptions?.headers?.origin || "", this.httpRequestHandler?.corsConfig);
|
|
106
100
|
try {
|
|
101
|
+
await this.bootServer();
|
|
102
|
+
if (!requestOptions) {
|
|
103
|
+
throw Errors.SYSTEM_ERROR("Cannot resolve event");
|
|
104
|
+
}
|
|
105
|
+
if (requestOptions.method === HttpMethod.OPTIONS) {
|
|
106
|
+
return toApiGatewayFormat({ code: 200, headers: corsHeaders, cookies: {} });
|
|
107
|
+
}
|
|
108
|
+
if (requestOptions.method === CRON_REQUEST_METHOD) {
|
|
109
|
+
await this.jobScheduler?.handleCron(requestOptions.body);
|
|
110
|
+
return toApiGatewayFormat({ code: 200, headers: corsHeaders, cookies: {} });
|
|
111
|
+
}
|
|
107
112
|
if (Object.values(HttpMethod).includes(requestOptions.method)) {
|
|
108
113
|
if (!this.httpRequestHandler) {
|
|
109
114
|
throw Errors.SYSTEM_ERROR("Http request handler not found");
|
|
@@ -136,6 +141,7 @@ export class LambdaWrapper {
|
|
|
136
141
|
return toApiGatewayFormat({ code: 200, headers: corsHeaders, cookies: {} });
|
|
137
142
|
}
|
|
138
143
|
catch (err) {
|
|
144
|
+
this.logger?.error(err);
|
|
139
145
|
return toApiGatewayFormat({
|
|
140
146
|
code: typeof err.code === "number" ? err.code : 500,
|
|
141
147
|
value: { name: err.name, message: err.message, code: err.code || 500 },
|