@contember/engine-http 1.3.5 → 1.3.7
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/dist/src/MasterContainer.d.ts.map +1 -1
- package/dist/src/MasterContainer.js +4 -0
- package/dist/src/MasterContainer.js.map +1 -1
- package/dist/src/application/application.d.ts +25 -10
- package/dist/src/application/application.d.ts.map +1 -1
- package/dist/src/application/application.js +45 -3
- package/dist/src/application/application.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/package.json +15 -15
- package/src/MasterContainer.ts +5 -0
- package/src/application/application.ts +83 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/engine-http",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.7",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"typings": "dist/src/index.d.ts",
|
|
@@ -13,20 +13,20 @@
|
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"@apollographql/graphql-playground-html": "^1.6.24",
|
|
16
|
-
"@contember/config-loader": "1.3.
|
|
17
|
-
"@contember/database": "1.3.
|
|
18
|
-
"@contember/database-migrations": "1.3.
|
|
19
|
-
"@contember/dic": "1.3.
|
|
20
|
-
"@contember/engine-common": "1.3.
|
|
21
|
-
"@contember/engine-content-api": "1.3.
|
|
22
|
-
"@contember/engine-system-api": "1.3.
|
|
23
|
-
"@contember/engine-tenant-api": "1.3.
|
|
24
|
-
"@contember/graphql-utils": "1.3.
|
|
25
|
-
"@contember/logger": "1.3.
|
|
26
|
-
"@contember/schema": "1.3.
|
|
27
|
-
"@contember/schema-migrations": "1.3.
|
|
28
|
-
"@contember/schema-utils": "1.3.
|
|
29
|
-
"@contember/typesafe": "1.3.
|
|
16
|
+
"@contember/config-loader": "1.3.7",
|
|
17
|
+
"@contember/database": "1.3.7",
|
|
18
|
+
"@contember/database-migrations": "1.3.7",
|
|
19
|
+
"@contember/dic": "1.3.7",
|
|
20
|
+
"@contember/engine-common": "1.3.7",
|
|
21
|
+
"@contember/engine-content-api": "1.3.7",
|
|
22
|
+
"@contember/engine-system-api": "1.3.7",
|
|
23
|
+
"@contember/engine-tenant-api": "1.3.7",
|
|
24
|
+
"@contember/graphql-utils": "1.3.7",
|
|
25
|
+
"@contember/logger": "1.3.7",
|
|
26
|
+
"@contember/schema": "1.3.7",
|
|
27
|
+
"@contember/schema-migrations": "1.3.7",
|
|
28
|
+
"@contember/schema-utils": "1.3.7",
|
|
29
|
+
"@contember/typesafe": "1.3.7",
|
|
30
30
|
"@graphql-tools/merge": "^8.3.5",
|
|
31
31
|
"@graphql-tools/schema": "^8.3.5",
|
|
32
32
|
"@koa/cors": "^3.1.0",
|
package/src/MasterContainer.ts
CHANGED
|
@@ -34,6 +34,7 @@ import { homepageController, playgroundController } from './misc'
|
|
|
34
34
|
import { Plugin } from './plugin/Plugin'
|
|
35
35
|
import { Application } from './application/application'
|
|
36
36
|
import { ApplicationWorkerManager } from './workers/ApplicationWorkerManager'
|
|
37
|
+
import { HttpResponse } from './common'
|
|
37
38
|
|
|
38
39
|
export interface MasterContainer {
|
|
39
40
|
initializer: Initializer
|
|
@@ -160,6 +161,10 @@ export class MasterContainerFactory {
|
|
|
160
161
|
it.addRoute('transfer', '/export', exportApiMiddlewareFactory.create())
|
|
161
162
|
it.addRoute('misc', '/playground', playgroundController)
|
|
162
163
|
it.addRoute('misc', '/', homepageController)
|
|
164
|
+
|
|
165
|
+
it.addInternalRoute('internal', '/health', () => {
|
|
166
|
+
return new HttpResponse(200, 'OK')
|
|
167
|
+
})
|
|
163
168
|
})
|
|
164
169
|
.addService('initializer', ({ projectGroupContainer }) =>
|
|
165
170
|
new Initializer(projectGroupContainer))
|
|
@@ -20,6 +20,7 @@ export class Application {
|
|
|
20
20
|
private middlewares: KoaMiddleware<any>[] = []
|
|
21
21
|
|
|
22
22
|
private routes: Route<HttpController>[] = []
|
|
23
|
+
private internalRoutes: Route<InternalHttpController>[] = []
|
|
23
24
|
private websocketRoutes: Route<WebSocketController>[] = []
|
|
24
25
|
|
|
25
26
|
constructor(
|
|
@@ -43,6 +44,14 @@ export class Application {
|
|
|
43
44
|
})
|
|
44
45
|
}
|
|
45
46
|
|
|
47
|
+
public addInternalRoute(module: string, mask: string, controller: InternalHttpController): void {
|
|
48
|
+
this.internalRoutes.push({
|
|
49
|
+
module,
|
|
50
|
+
controller,
|
|
51
|
+
match: createRequestMatcher(mask),
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
46
55
|
public addWebsocketRoute(module: string, mask: string, controller: WebSocketController): void {
|
|
47
56
|
this.websocketRoutes.push({
|
|
48
57
|
module,
|
|
@@ -133,7 +142,6 @@ export class Application {
|
|
|
133
142
|
timer,
|
|
134
143
|
url,
|
|
135
144
|
request: req,
|
|
136
|
-
requestDebugMode: false,
|
|
137
145
|
authResult,
|
|
138
146
|
params: matchedRequest.params,
|
|
139
147
|
projectGroup: groupContainer,
|
|
@@ -170,15 +178,23 @@ export class Application {
|
|
|
170
178
|
}
|
|
171
179
|
}
|
|
172
180
|
|
|
173
|
-
private async handleHttpRequest(ctx: KoaContext<{module?: string}>) {
|
|
181
|
+
private async handleHttpRequest(ctx: KoaContext<{ module?: string; projectGroup?: string; project?: string }>) {
|
|
182
|
+
const matchedRequest = this.matchRequest(this.routes, ctx.request.URL)
|
|
183
|
+
if (!matchedRequest) {
|
|
184
|
+
const internalMatchedRequest = this.matchRequest(this.internalRoutes, ctx.request.URL)
|
|
185
|
+
if (internalMatchedRequest) {
|
|
186
|
+
return await this.handleInternalRequest(internalMatchedRequest, ctx)
|
|
187
|
+
}
|
|
188
|
+
}
|
|
174
189
|
let httpContext: HttpContext | null = null
|
|
175
190
|
let requestLogger = this.logger
|
|
176
191
|
const { timer, send: sendTimer } = this.createTimer()
|
|
192
|
+
|
|
177
193
|
try {
|
|
178
|
-
const matchedRequest = this.matchRequest(this.routes, ctx.request.URL)
|
|
179
194
|
if (!matchedRequest) {
|
|
180
|
-
|
|
195
|
+
return this.sendHttpResponse(ctx, new HttpErrorResponse(404, 'Route not found'))
|
|
181
196
|
}
|
|
197
|
+
|
|
182
198
|
requestLogger = this.createRequestLogger(ctx.req, ctx.request.body, matchedRequest.module)
|
|
183
199
|
requestLogger.debug('Request processing started', {
|
|
184
200
|
body: ctx.request.body,
|
|
@@ -192,6 +208,8 @@ export class Application {
|
|
|
192
208
|
requestLogger = requestLogger.child({
|
|
193
209
|
projectGroup: groupContainer.slug,
|
|
194
210
|
})
|
|
211
|
+
ctx.state.projectGroup = groupContainer.slug
|
|
212
|
+
ctx.state.project = matchedRequest.params.projectSlug
|
|
195
213
|
|
|
196
214
|
const authResult = await groupContainer.authenticator.authenticate({ request: ctx.req, timer })
|
|
197
215
|
requestLogger.debug('User authenticated', { authResult })
|
|
@@ -237,6 +255,34 @@ export class Application {
|
|
|
237
255
|
}
|
|
238
256
|
}
|
|
239
257
|
|
|
258
|
+
|
|
259
|
+
private async handleInternalRequest(matchedRequest: MatchedRequest<InternalHttpController>, ctx: KoaContext<{ module?: string }>) {
|
|
260
|
+
try {
|
|
261
|
+
const response = await this.logger.scope(async logger => {
|
|
262
|
+
return await matchedRequest.controller({
|
|
263
|
+
koa: ctx,
|
|
264
|
+
body: ctx.request.body,
|
|
265
|
+
url: ctx.request.URL,
|
|
266
|
+
logger,
|
|
267
|
+
request: ctx.req,
|
|
268
|
+
response: ctx.res,
|
|
269
|
+
params: matchedRequest.params,
|
|
270
|
+
})
|
|
271
|
+
})
|
|
272
|
+
if (response) {
|
|
273
|
+
this.sendHttpResponse(ctx, response)
|
|
274
|
+
}
|
|
275
|
+
} catch (e) {
|
|
276
|
+
if (e instanceof HttpResponse) {
|
|
277
|
+
this.sendHttpResponse(ctx, e)
|
|
278
|
+
} else {
|
|
279
|
+
this.sendHttpResponse(ctx, new HttpErrorResponse(500, 'Internal server error'))
|
|
280
|
+
this.logger.error(e)
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
|
|
240
286
|
private sendHttpResponse(ctx: KoaContext<{}>, response: HttpResponse) {
|
|
241
287
|
if (response.contentType) {
|
|
242
288
|
ctx.set('Content-type', response.contentType)
|
|
@@ -336,7 +382,7 @@ export class Application {
|
|
|
336
382
|
return { timer, send }
|
|
337
383
|
}
|
|
338
384
|
|
|
339
|
-
private matchRequest<C>(routes: Route<C>[], url: URL):
|
|
385
|
+
private matchRequest<C>(routes: Route<C>[], url: URL): MatchedRequest<C> | null {
|
|
340
386
|
for (const route of routes) {
|
|
341
387
|
const params = route.match({ url })
|
|
342
388
|
if (params !== null) {
|
|
@@ -352,31 +398,50 @@ type EventTime = { label: string; start: number; duration?: number }
|
|
|
352
398
|
|
|
353
399
|
export type Timer = <T>(event: string, cb: () => T) => T
|
|
354
400
|
|
|
355
|
-
export
|
|
401
|
+
export type MatchedRequest<C> = { params: Params; controller: C; module: string }
|
|
402
|
+
|
|
403
|
+
export type BaseRequestContext = {
|
|
356
404
|
logger: Logger
|
|
357
|
-
timer: Timer
|
|
358
405
|
request: IncomingMessage
|
|
359
406
|
url: URL
|
|
360
|
-
|
|
361
|
-
projectGroup: ProjectGroupContainer
|
|
362
|
-
authResult: AuthResult | null
|
|
363
407
|
params: Params
|
|
364
|
-
|
|
365
|
-
requestDebugMode: boolean
|
|
366
408
|
}
|
|
367
409
|
|
|
368
|
-
export
|
|
410
|
+
export type ApplicationContext =
|
|
411
|
+
& BaseRequestContext
|
|
412
|
+
& {
|
|
413
|
+
projectGroup: ProjectGroupContainer
|
|
414
|
+
authResult: AuthResult | null
|
|
415
|
+
timer: Timer
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
export type BaseHttpRequestContext = {
|
|
369
419
|
koa: KoaContext<{}>
|
|
370
420
|
body: unknown
|
|
371
421
|
response: ServerResponse
|
|
372
422
|
}
|
|
373
423
|
|
|
374
|
-
export
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
424
|
+
export type HttpContext =
|
|
425
|
+
& ApplicationContext
|
|
426
|
+
& BaseHttpRequestContext
|
|
427
|
+
& {
|
|
428
|
+
requestDebugMode: boolean
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export type InternalHttpContext =
|
|
432
|
+
& BaseRequestContext
|
|
433
|
+
& BaseHttpRequestContext
|
|
434
|
+
|
|
435
|
+
export type WebSocketContext =
|
|
436
|
+
& ApplicationContext
|
|
437
|
+
& {
|
|
438
|
+
ws: WebSocket
|
|
439
|
+
abortSignal: AbortSignal
|
|
440
|
+
}
|
|
378
441
|
|
|
379
|
-
export type
|
|
442
|
+
export type HttpControllerResponse = Promise<HttpErrorResponse | undefined | void> | HttpErrorResponse | undefined | void
|
|
443
|
+
export type HttpController = (context: HttpContext) => HttpControllerResponse
|
|
444
|
+
export type InternalHttpController = (context: InternalHttpContext) => HttpControllerResponse
|
|
380
445
|
|
|
381
446
|
export type WebSocketController = (context: WebSocketContext) => void
|
|
382
447
|
|