@contember/engine-http 1.4.0-alpha.1 → 1.4.0-alpha.3

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.
Files changed (94) hide show
  1. package/dist/src/MasterContainer.d.ts +14 -3
  2. package/dist/src/MasterContainer.d.ts.map +1 -1
  3. package/dist/src/MasterContainer.js +14 -4
  4. package/dist/src/MasterContainer.js.map +1 -1
  5. package/dist/src/application/application.d.ts +26 -10
  6. package/dist/src/application/application.d.ts.map +1 -1
  7. package/dist/src/application/application.js +55 -8
  8. package/dist/src/application/application.js.map +1 -1
  9. package/dist/src/config/configSchema.d.ts +1 -0
  10. package/dist/src/config/configSchema.d.ts.map +1 -1
  11. package/dist/src/config/configSchema.js +15 -0
  12. package/dist/src/config/configSchema.js.map +1 -1
  13. package/dist/src/config/configTemplate.d.ts.map +1 -1
  14. package/dist/src/config/configTemplate.js +2 -0
  15. package/dist/src/config/configTemplate.js.map +1 -1
  16. package/dist/src/config/projectConfigResolver.d.ts.map +1 -1
  17. package/dist/src/config/projectConfigResolver.js +4 -4
  18. package/dist/src/config/projectConfigResolver.js.map +1 -1
  19. package/dist/src/content/ContentApiControllerFactory.d.ts.map +1 -1
  20. package/dist/src/content/ContentApiControllerFactory.js +3 -1
  21. package/dist/src/content/ContentApiControllerFactory.js.map +1 -1
  22. package/dist/src/content/ContentGraphQLContextFactory.d.ts +2 -5
  23. package/dist/src/content/ContentGraphQLContextFactory.d.ts.map +1 -1
  24. package/dist/src/content/ContentGraphQLContextFactory.js +1 -2
  25. package/dist/src/content/ContentGraphQLContextFactory.js.map +1 -1
  26. package/dist/src/content/ContentQueryHandlerFactory.d.ts.map +1 -1
  27. package/dist/src/content/ContentQueryHandlerFactory.js +1 -3
  28. package/dist/src/content/ContentQueryHandlerFactory.js.map +1 -1
  29. package/dist/src/graphql/execution.d.ts +1 -0
  30. package/dist/src/graphql/execution.d.ts.map +1 -1
  31. package/dist/src/graphql/execution.js +9 -11
  32. package/dist/src/graphql/execution.js.map +1 -1
  33. package/dist/src/graphql/state.d.ts +0 -5
  34. package/dist/src/graphql/state.d.ts.map +1 -1
  35. package/dist/src/graphql/state.js +0 -9
  36. package/dist/src/graphql/state.js.map +1 -1
  37. package/dist/src/project/ProjectContainer.d.ts +8 -24
  38. package/dist/src/project/ProjectContainer.d.ts.map +1 -1
  39. package/dist/src/project/ProjectContainer.js +18 -20
  40. package/dist/src/project/ProjectContainer.js.map +1 -1
  41. package/dist/src/system/ContentQueryExecutor.d.ts +9 -0
  42. package/dist/src/system/ContentQueryExecutor.d.ts.map +1 -0
  43. package/dist/src/system/ContentQueryExecutor.js +69 -0
  44. package/dist/src/system/ContentQueryExecutor.js.map +1 -0
  45. package/dist/src/system/SystemApiMiddlewareFactory.d.ts.map +1 -1
  46. package/dist/src/system/SystemApiMiddlewareFactory.js +6 -2
  47. package/dist/src/system/SystemApiMiddlewareFactory.js.map +1 -1
  48. package/dist/src/system/SystemGraphQLContextFactory.d.ts +1 -4
  49. package/dist/src/system/SystemGraphQLContextFactory.d.ts.map +1 -1
  50. package/dist/src/system/SystemGraphQLContextFactory.js +2 -3
  51. package/dist/src/system/SystemGraphQLContextFactory.js.map +1 -1
  52. package/dist/src/system/SystemGraphQLHandlerFactory.d.ts +1 -5
  53. package/dist/src/system/SystemGraphQLHandlerFactory.d.ts.map +1 -1
  54. package/dist/src/system/SystemGraphQLHandlerFactory.js +1 -3
  55. package/dist/src/system/SystemGraphQLHandlerFactory.js.map +1 -1
  56. package/dist/src/tenant/TenantApiMiddlewareFactory.d.ts.map +1 -1
  57. package/dist/src/tenant/TenantApiMiddlewareFactory.js +6 -2
  58. package/dist/src/tenant/TenantApiMiddlewareFactory.js.map +1 -1
  59. package/dist/src/tenant/TenantGraphQLContextFactory.d.ts +1 -4
  60. package/dist/src/tenant/TenantGraphQLContextFactory.d.ts.map +1 -1
  61. package/dist/src/tenant/TenantGraphQLContextFactory.js +1 -2
  62. package/dist/src/tenant/TenantGraphQLContextFactory.js.map +1 -1
  63. package/dist/src/tenant/TenantGraphQLHandlerFactory.d.ts +1 -3
  64. package/dist/src/tenant/TenantGraphQLHandlerFactory.d.ts.map +1 -1
  65. package/dist/src/tenant/TenantGraphQLHandlerFactory.js +1 -3
  66. package/dist/src/tenant/TenantGraphQLHandlerFactory.js.map +1 -1
  67. package/dist/src/transfer/ImportExecutor.d.ts +3 -1
  68. package/dist/src/transfer/ImportExecutor.d.ts.map +1 -1
  69. package/dist/src/transfer/ImportExecutor.js +4 -3
  70. package/dist/src/transfer/ImportExecutor.js.map +1 -1
  71. package/dist/src/tsconfig.tsbuildinfo +1 -1
  72. package/dist/src/utils/serverStartup.d.ts +1 -0
  73. package/dist/src/utils/serverStartup.d.ts.map +1 -1
  74. package/dist/tests/tsconfig.tsbuildinfo +1 -1
  75. package/package.json +34 -34
  76. package/src/MasterContainer.ts +24 -9
  77. package/src/application/application.ts +96 -24
  78. package/src/config/configSchema.ts +15 -0
  79. package/src/config/configTemplate.ts +2 -0
  80. package/src/config/projectConfigResolver.ts +4 -3
  81. package/src/content/ContentApiControllerFactory.ts +5 -1
  82. package/src/content/ContentGraphQLContextFactory.ts +2 -7
  83. package/src/content/ContentQueryHandlerFactory.ts +2 -10
  84. package/src/graphql/execution.ts +3 -3
  85. package/src/graphql/state.ts +0 -13
  86. package/src/project/ProjectContainer.ts +25 -35
  87. package/src/system/ContentQueryExecutor.ts +79 -0
  88. package/src/system/SystemApiMiddlewareFactory.ts +8 -2
  89. package/src/system/SystemGraphQLContextFactory.ts +2 -6
  90. package/src/system/SystemGraphQLHandlerFactory.ts +2 -16
  91. package/src/tenant/TenantApiMiddlewareFactory.ts +8 -3
  92. package/src/tenant/TenantGraphQLContextFactory.ts +1 -5
  93. package/src/tenant/TenantGraphQLHandlerFactory.ts +3 -11
  94. package/src/transfer/ImportExecutor.ts +4 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contember/engine-http",
3
- "version": "1.4.0-alpha.1",
3
+ "version": "1.4.0-alpha.3",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/src/index.js",
6
6
  "typings": "dist/src/index.d.ts",
@@ -12,47 +12,47 @@
12
12
  "default": "./dist/src/index.js"
13
13
  },
14
14
  "dependencies": {
15
- "@apollographql/graphql-playground-html": "^1.6.24",
16
- "@contember/config-loader": "1.4.0-alpha.1",
17
- "@contember/database": "1.4.0-alpha.1",
18
- "@contember/database-migrations": "1.4.0-alpha.1",
19
- "@contember/dic": "1.4.0-alpha.1",
20
- "@contember/engine-common": "1.4.0-alpha.1",
21
- "@contember/engine-content-api": "1.4.0-alpha.1",
22
- "@contember/engine-system-api": "1.4.0-alpha.1",
23
- "@contember/engine-tenant-api": "1.4.0-alpha.1",
24
- "@contember/graphql-utils": "1.4.0-alpha.1",
25
- "@contember/logger": "1.4.0-alpha.1",
26
- "@contember/schema": "1.4.0-alpha.1",
27
- "@contember/schema-migrations": "1.4.0-alpha.1",
28
- "@contember/schema-utils": "1.4.0-alpha.1",
29
- "@contember/typesafe": "1.4.0-alpha.1",
15
+ "@apollographql/graphql-playground-html": "^1.6.29",
16
+ "@contember/config-loader": "1.4.0-alpha.3",
17
+ "@contember/database": "1.4.0-alpha.3",
18
+ "@contember/database-migrations": "1.4.0-alpha.3",
19
+ "@contember/dic": "1.4.0-alpha.3",
20
+ "@contember/engine-common": "1.4.0-alpha.3",
21
+ "@contember/engine-content-api": "1.4.0-alpha.3",
22
+ "@contember/engine-system-api": "1.4.0-alpha.3",
23
+ "@contember/engine-tenant-api": "1.4.0-alpha.3",
24
+ "@contember/graphql-utils": "1.4.0-alpha.3",
25
+ "@contember/logger": "1.4.0-alpha.3",
26
+ "@contember/schema": "1.4.0-alpha.3",
27
+ "@contember/schema-migrations": "1.4.0-alpha.3",
28
+ "@contember/schema-utils": "1.4.0-alpha.3",
29
+ "@contember/typesafe": "1.4.0-alpha.3",
30
30
  "@graphql-tools/merge": "^9.0.0",
31
31
  "@graphql-tools/schema": "^10.0.0",
32
- "@koa/cors": "^3.1.0",
33
- "@sentry/node": "^7.1.1",
32
+ "@koa/cors": "^4.0.0",
33
+ "@sentry/node": "^7.80.1",
34
34
  "bcryptjs": "^2.4.3",
35
- "koa": "^2.11.0",
36
- "koa-bodyparser": "^4.3.0",
37
- "koa-compress": "^5.1.0",
38
- "lru-cache": "^7.10.1",
39
- "path-to-regexp": "^6.2.0",
40
- "uuid": "^8.3.1",
41
- "ws": "^8.10.0"
35
+ "koa": "^2.14.2",
36
+ "koa-bodyparser": "^4.4.1",
37
+ "koa-compress": "^5.1.1",
38
+ "lru-cache": "^10.0.2",
39
+ "path-to-regexp": "^6.2.1",
40
+ "uuid": "^9.0.1",
41
+ "ws": "^8.14.2"
42
42
  },
43
43
  "devDependencies": {
44
- "@types/bcryptjs": "^2.4.2",
45
- "@types/koa": "^2.11.3",
46
- "@types/koa-bodyparser": "^4.2.2",
47
- "@types/koa__cors": "^3.0.2",
44
+ "@types/bcryptjs": "^2.4.6",
45
+ "@types/koa": "^2.13.11",
46
+ "@types/koa-bodyparser": "^4.3.12",
47
+ "@types/koa__cors": "^4.0.3",
48
48
  "@types/lru-cache": "^7.10.10",
49
- "@types/node": "^18",
50
- "@types/uuid": "^8.3.0",
51
- "@types/ws": "^8.5.3",
52
- "graphql": "^16.5.0"
49
+ "@types/node": "^20.9.0",
50
+ "@types/uuid": "^9.0.7",
51
+ "@types/ws": "^8.5.9",
52
+ "graphql": "^16.8.1"
53
53
  },
54
54
  "peerDependencies": {
55
- "graphql": "^14.6.0",
55
+ "graphql": ">= 14.6.0",
56
56
  "pg": "^8.9.0"
57
57
  }
58
58
  }
@@ -11,7 +11,7 @@ import { TenantConfigResolver } from './config/tenantConfigResolver'
11
11
  import { ProjectGroupContainerFactory } from './projectGroup/ProjectGroupContainer'
12
12
  import { ProjectGroupResolver, SingleProjectGroupResolver } from './projectGroup/ProjectGroupResolver'
13
13
  import { Logger } from '@contember/logger'
14
- import { ExecutionContainerFactory } from '@contember/engine-content-api'
14
+ import { ExecutionContainerFactory, GraphQlSchemaBuilderFactory, PermissionFactory } from '@contember/engine-content-api'
15
15
  import { createProviders, Providers } from './providers'
16
16
  import { TenantApiMiddlewareFactory, TenantGraphQLContextFactory, TenantGraphQLHandlerFactory } from './tenant'
17
17
  import { SystemApiMiddlewareFactory, SystemGraphQLContextFactory, SystemGraphQLHandlerFactory } from './system'
@@ -34,6 +34,9 @@ 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'
38
+ import { ContentQueryExecutorImpl } from './system/ContentQueryExecutor'
39
+ import { DatabaseMetadataResolver } from '@contember/database'
37
40
 
38
41
  export interface MasterContainer {
39
42
  initializer: Initializer
@@ -93,10 +96,20 @@ export class MasterContainerFactory {
93
96
  new TenantContainerFactory(providers))
94
97
  .addService('modificationHandlerFactory', () =>
95
98
  new ModificationHandlerFactory(ModificationHandlerFactory.defaultFactoryMap))
96
- .addService('systemContainerFactory', ({ providers, modificationHandlerFactory }) =>
97
- new SystemContainerFactory(providers, modificationHandlerFactory))
98
- .addService('projectContainerFactoryFactory', ({ debugMode, plugins, providers, serverConfig }) =>
99
- new ProjectContainerFactoryFactory(debugMode, plugins, providers, serverConfig))
99
+ .addService('executionContainerFactory', ({ providers }) =>
100
+ new ExecutionContainerFactory(providers))
101
+ .addService('graphQlSchemaBuilderFactory', () =>
102
+ new GraphQlSchemaBuilderFactory())
103
+ .addService('contentQueryExecutor', ({ executionContainerFactory, graphQlSchemaBuilderFactory }) =>
104
+ new ContentQueryExecutorImpl(executionContainerFactory, graphQlSchemaBuilderFactory))
105
+ .addService('systemContainerFactory', ({ providers, modificationHandlerFactory, contentQueryExecutor }) =>
106
+ new SystemContainerFactory(providers, modificationHandlerFactory, contentQueryExecutor))
107
+ .addService('contentPermissionFactory', ({}) =>
108
+ new PermissionFactory())
109
+ .addService('databaseMetadataResolver', () =>
110
+ new DatabaseMetadataResolver())
111
+ .addService('projectContainerFactoryFactory', ({ plugins, providers, serverConfig, graphQlSchemaBuilderFactory, contentPermissionFactory, databaseMetadataResolver }) =>
112
+ new ProjectContainerFactoryFactory(plugins, providers, serverConfig, graphQlSchemaBuilderFactory, contentPermissionFactory, databaseMetadataResolver))
100
113
  .addService('tenantGraphQLHandlerFactory', () =>
101
114
  new TenantGraphQLHandlerFactory())
102
115
  .addService('systemGraphQLHandlerFactory', ({ debugMode }) =>
@@ -111,8 +124,6 @@ export class MasterContainerFactory {
111
124
  new SingleProjectGroupResolver(projectGroupContainer))
112
125
  .addService('notModifiedChecker', () =>
113
126
  new NotModifiedChecker())
114
- .addService('executionContainerFactory', ({ providers }) =>
115
- new ExecutionContainerFactory(providers))
116
127
  .addService('contentGraphqlContextFactory', ({ providers, executionContainerFactory }) =>
117
128
  new ContentGraphQLContextFactory(providers, executionContainerFactory))
118
129
  .addService('contentQueryHandlerFactory', ({ debugMode }) =>
@@ -133,8 +144,8 @@ export class MasterContainerFactory {
133
144
  new ContentSchemaTransferMappingFactory())
134
145
  .addService('systemSchemaTransferMappingFactory', () =>
135
146
  new SystemSchemaTransferMappingFactory())
136
- .addService('importExecutor', ({ contentSchemaTransferMappingFactory, systemSchemaTransferMappingFactory }) =>
137
- new ImportExecutor(contentSchemaTransferMappingFactory, systemSchemaTransferMappingFactory))
147
+ .addService('importExecutor', ({ contentSchemaTransferMappingFactory, systemSchemaTransferMappingFactory, databaseMetadataResolver }) =>
148
+ new ImportExecutor(contentSchemaTransferMappingFactory, systemSchemaTransferMappingFactory, databaseMetadataResolver))
138
149
  .addService('exportExecutor', ({ contentSchemaTransferMappingFactory, systemSchemaTransferMappingFactory }) =>
139
150
  new ExportExecutor(contentSchemaTransferMappingFactory, systemSchemaTransferMappingFactory))
140
151
  .addService('importApiMiddlewareFactory', ({ projectGroupResolver, importExecutor }) =>
@@ -160,6 +171,10 @@ export class MasterContainerFactory {
160
171
  it.addRoute('transfer', '/export', exportApiMiddlewareFactory.create())
161
172
  it.addRoute('misc', '/playground', playgroundController)
162
173
  it.addRoute('misc', '/', homepageController)
174
+
175
+ it.addInternalRoute('internal', '/health', () => {
176
+ return new HttpResponse(200, 'OK')
177
+ })
163
178
  })
164
179
  .addService('initializer', ({ projectGroupContainer }) =>
165
180
  new Initializer(projectGroupContainer))
@@ -20,8 +20,11 @@ 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
 
26
+ private suppressAccessLog: boolean | RegExp
27
+
25
28
  constructor(
26
29
  private readonly projectGroupResolver: ProjectGroupResolver,
27
30
  private readonly serverConfig: ServerConfig,
@@ -29,6 +32,8 @@ export class Application {
29
32
  private readonly version: string | undefined,
30
33
  private readonly logger: Logger,
31
34
  ) {
35
+ const suppressAccessLogRaw = serverConfig.http.suppressAccessLog
36
+ this.suppressAccessLog = suppressAccessLogRaw === true ? true : suppressAccessLogRaw ? new RegExp(suppressAccessLogRaw) : false
32
37
  }
33
38
 
34
39
  addMiddleware(middleware: KoaMiddleware<any>) {
@@ -43,6 +48,14 @@ export class Application {
43
48
  })
44
49
  }
45
50
 
51
+ public addInternalRoute(module: string, mask: string, controller: InternalHttpController): void {
52
+ this.internalRoutes.push({
53
+ module,
54
+ controller,
55
+ match: createRequestMatcher(mask),
56
+ })
57
+ }
58
+
46
59
  public addWebsocketRoute(module: string, mask: string, controller: WebSocketController): void {
47
60
  this.websocketRoutes.push({
48
61
  module,
@@ -133,7 +146,6 @@ export class Application {
133
146
  timer,
134
147
  url,
135
148
  request: req,
136
- requestDebugMode: false,
137
149
  authResult,
138
150
  params: matchedRequest.params,
139
151
  projectGroup: groupContainer,
@@ -164,22 +176,29 @@ export class Application {
164
176
  requestLogger.debug('Websocket connection failed')
165
177
  } finally {
166
178
  sendTimer({
179
+ req,
167
180
  requestDebugMode: false,
168
181
  logger: requestLogger,
169
182
  })
170
183
  }
171
184
  }
172
185
 
173
- private async handleHttpRequest(ctx: KoaContext<{module?: string}>) {
186
+ private async handleHttpRequest(ctx: KoaContext<{ module?: string; projectGroup?: string; project?: string }>) {
187
+ const matchedRequest = this.matchRequest(this.routes, ctx.request.URL)
188
+ if (!matchedRequest) {
189
+ const internalMatchedRequest = this.matchRequest(this.internalRoutes, ctx.request.URL)
190
+ if (internalMatchedRequest) {
191
+ return await this.handleInternalRequest(internalMatchedRequest, ctx)
192
+ }
193
+ }
174
194
  let httpContext: HttpContext | null = null
175
- let requestLogger = this.logger
195
+ let requestLogger = this.createRequestLogger(ctx.req, ctx.request.body, matchedRequest?.module)
176
196
  const { timer, send: sendTimer } = this.createTimer()
197
+
177
198
  try {
178
- const matchedRequest = this.matchRequest(this.routes, ctx.request.URL)
179
199
  if (!matchedRequest) {
180
- throw new HttpErrorResponse(404, 'Route not found')
200
+ return this.sendHttpResponse(ctx, new HttpErrorResponse(404, 'Route not found'))
181
201
  }
182
- requestLogger = this.createRequestLogger(ctx.req, ctx.request.body, matchedRequest.module)
183
202
  requestLogger.debug('Request processing started', {
184
203
  body: ctx.request.body,
185
204
  query: ctx.request.query,
@@ -192,6 +211,8 @@ export class Application {
192
211
  requestLogger = requestLogger.child({
193
212
  projectGroup: groupContainer.slug,
194
213
  })
214
+ ctx.state.projectGroup = groupContainer.slug
215
+ ctx.state.project = matchedRequest.params.projectSlug
195
216
 
196
217
  const authResult = await groupContainer.authenticator.authenticate({ request: ctx.req, timer })
197
218
  requestLogger.debug('User authenticated', { authResult })
@@ -228,6 +249,7 @@ export class Application {
228
249
  }
229
250
  } finally {
230
251
  sendTimer({
252
+ req: ctx.req,
231
253
  response: ctx.res,
232
254
  body: ctx.response.body,
233
255
  requestDebugMode: (httpContext as HttpContext | null)?.requestDebugMode ?? false,
@@ -237,6 +259,34 @@ export class Application {
237
259
  }
238
260
  }
239
261
 
262
+
263
+ private async handleInternalRequest(matchedRequest: MatchedRequest<InternalHttpController>, ctx: KoaContext<{ module?: string }>) {
264
+ try {
265
+ const response = await this.logger.scope(async logger => {
266
+ return await matchedRequest.controller({
267
+ koa: ctx,
268
+ body: ctx.request.body,
269
+ url: ctx.request.URL,
270
+ logger,
271
+ request: ctx.req,
272
+ response: ctx.res,
273
+ params: matchedRequest.params,
274
+ })
275
+ })
276
+ if (response) {
277
+ this.sendHttpResponse(ctx, response)
278
+ }
279
+ } catch (e) {
280
+ if (e instanceof HttpResponse) {
281
+ this.sendHttpResponse(ctx, e)
282
+ } else {
283
+ this.sendHttpResponse(ctx, new HttpErrorResponse(500, 'Internal server error'))
284
+ this.logger.error(e)
285
+ }
286
+ }
287
+ }
288
+
289
+
240
290
  private sendHttpResponse(ctx: KoaContext<{}>, response: HttpResponse) {
241
291
  if (response.contentType) {
242
292
  ctx.set('Content-type', response.contentType)
@@ -262,11 +312,11 @@ export class Application {
262
312
  .map(h => `${h}: ${headers[h]}`)
263
313
  .join('\r\n') +
264
314
  '\r\n\r\n' +
265
- response.body ?? '',
315
+ (response.body ?? ''),
266
316
  )
267
317
  }
268
318
 
269
- private createRequestLogger(request: IncomingMessage, body: any, module: string): Logger {
319
+ private createRequestLogger(request: IncomingMessage, body: any, module?: string): Logger {
270
320
  return this.logger.child({
271
321
  method: request.method,
272
322
  uri: request.url,
@@ -300,7 +350,7 @@ export class Application {
300
350
  return res
301
351
  }
302
352
 
303
- const send = (ctx: { response?: ServerResponse; body?: unknown; requestDebugMode: boolean; logger: Logger }) => {
353
+ const send = (ctx: { req: IncomingMessage; response?: ServerResponse; body?: unknown; requestDebugMode: boolean; logger: Logger }) => {
304
354
  if (ctx.response && !ctx.response.headersSent && (ctx.requestDebugMode || this.debugMode) && times.length) {
305
355
  ctx.response.setHeader('server-timing', times.map(it => `${it.label};desc="${it.label} T+${it.start}";dur=${it.duration ?? -1}`).join(', '))
306
356
  }
@@ -308,7 +358,10 @@ export class Application {
308
358
  const emit = () => {
309
359
  const total = new Date().getTime() - globalStart
310
360
  const timeLabel = total > 500 ? 'TIME_SLOW' : 'TIME_OK'
311
- ctx.logger.info(!ctx.response ? 'Connection established' : ctx.response.statusCode < 400 ? `Request successful` : 'Request failed', {
361
+ const shouldSuppress = this.suppressAccessLog === true || !ctx.req.url || (this.suppressAccessLog !== false && this.suppressAccessLog.test(ctx.req.url))
362
+
363
+ const level = shouldSuppress ? 'debug' : 'info'
364
+ ctx.logger.log(level, !ctx.response ? 'Connection established' : ctx.response.statusCode < 400 ? `Request successful` : 'Request failed', {
312
365
  status: ctx.response?.statusCode,
313
366
  timeLabel: timeLabel,
314
367
  totalTimeMs: total,
@@ -336,7 +389,7 @@ export class Application {
336
389
  return { timer, send }
337
390
  }
338
391
 
339
- private matchRequest<C>(routes: Route<C>[], url: URL): { params: Params; controller: C; module: string } | null {
392
+ private matchRequest<C>(routes: Route<C>[], url: URL): MatchedRequest<C> | null {
340
393
  for (const route of routes) {
341
394
  const params = route.match({ url })
342
395
  if (params !== null) {
@@ -352,31 +405,50 @@ type EventTime = { label: string; start: number; duration?: number }
352
405
 
353
406
  export type Timer = <T>(event: string, cb: () => T) => T
354
407
 
355
- export interface ApplicationContext {
408
+ export type MatchedRequest<C> = { params: Params; controller: C; module: string }
409
+
410
+ export type BaseRequestContext = {
356
411
  logger: Logger
357
- timer: Timer
358
412
  request: IncomingMessage
359
413
  url: URL
360
-
361
- projectGroup: ProjectGroupContainer
362
- authResult: AuthResult | null
363
414
  params: Params
364
-
365
- requestDebugMode: boolean
366
415
  }
367
416
 
368
- export interface HttpContext extends ApplicationContext {
417
+ export type ApplicationContext =
418
+ & BaseRequestContext
419
+ & {
420
+ projectGroup: ProjectGroupContainer
421
+ authResult: AuthResult | null
422
+ timer: Timer
423
+ }
424
+
425
+ export type BaseHttpRequestContext = {
369
426
  koa: KoaContext<{}>
370
427
  body: unknown
371
428
  response: ServerResponse
372
429
  }
373
430
 
374
- export interface WebSocketContext extends ApplicationContext {
375
- ws: WebSocket
376
- abortSignal: AbortSignal
377
- }
431
+ export type HttpContext =
432
+ & ApplicationContext
433
+ & BaseHttpRequestContext
434
+ & {
435
+ requestDebugMode: boolean
436
+ }
437
+
438
+ export type InternalHttpContext =
439
+ & BaseRequestContext
440
+ & BaseHttpRequestContext
441
+
442
+ export type WebSocketContext =
443
+ & ApplicationContext
444
+ & {
445
+ ws: WebSocket
446
+ abortSignal: AbortSignal
447
+ }
378
448
 
379
- export type HttpController = (context: HttpContext) => Promise<HttpErrorResponse | undefined | void> | HttpErrorResponse | undefined | void
449
+ export type HttpControllerResponse = Promise<HttpErrorResponse | undefined | void> | HttpErrorResponse | undefined | void
450
+ export type HttpController = (context: HttpContext) => HttpControllerResponse
451
+ export type InternalHttpController = (context: InternalHttpContext) => HttpControllerResponse
380
452
 
381
453
  export type WebSocketController = (context: WebSocketContext) => void
382
454
 
@@ -87,6 +87,21 @@ export const serverConfigSchema = Typesafe.object({
87
87
  port: Typesafe.number,
88
88
  http: Typesafe.partial({
89
89
  requestBodySize: Typesafe.string,
90
+ suppressAccessLog: (val: unknown) => {
91
+ if (!val) {
92
+ return undefined
93
+ }
94
+ if (val === 'true' || val === '1' || val === 'on') {
95
+ return true
96
+ }
97
+ if (val === 'false' || val === '0' || val === 'off') {
98
+ return false
99
+ }
100
+ if (typeof val === 'string') {
101
+ return val
102
+ }
103
+ Typesafe.fail([])
104
+ },
90
105
  }),
91
106
  contentApi: Typesafe.partial({
92
107
  schemaCacheTtlSeconds: Typesafe.integer,
@@ -56,6 +56,7 @@ export const configTemplate: any = {
56
56
  },
57
57
  },
58
58
  projectDefaults: {
59
+ stageSlug: '%?project.env.STAGE_SLUG%',
59
60
  db: {
60
61
  host: `%?project.env.DB_HOST%`,
61
62
  port: `%?project.env.DB_PORT::number%`,
@@ -80,6 +81,7 @@ export const configTemplate: any = {
80
81
  applicationWorker: '%?env.CONTEMBER_APPLICATION_WORKER::string%',
81
82
  http: {
82
83
  requestBodySize: '%?env.CONTEMBER_HTTP_REQUEST_BODY_SIZE::string%',
84
+ suppressAccessLog: '%?env.CONTEMBER_HTTP_SUPPRESS_ACCESS_LOG::string%',
83
85
  },
84
86
  contentApi: {
85
87
  schemaCacheTtlSeconds: '%?env.CONTEMBER_CONTENT_API_SCHEMA_CACHE_TTL_SECONDS::number%',
@@ -21,11 +21,12 @@ export const createProjectConfigResolver = (env: Env, config: any, configProcess
21
21
  (config?.projects as any)?.[slug] as any,
22
22
  additionalConfig,
23
23
  )
24
- if (!mergedConfig.stages) {
25
- mergedConfig.stages = { live: {} }
26
- }
27
24
  const resolvedConfig = resolveParameters(mergedConfig, createProjectParametersResolver(slug, env, secrets))
28
25
 
26
+ if (!resolvedConfig.stages) {
27
+ resolvedConfig.stages = { [resolvedConfig.stageSlug ?? 'live']: {} }
28
+ }
29
+
29
30
  const projectConfigSchemaWithPlugins = configProcessors.reduce(
30
31
  (schema, current) => {
31
32
  if (!current.getProjectConfigSchema) {
@@ -6,6 +6,7 @@ import { NotModifiedChecker } from './NotModifiedChecker'
6
6
  import { ContentGraphQLContextFactory } from './ContentGraphQLContextFactory'
7
7
  import { ContentQueryHandler, ContentQueryHandlerFactory } from './ContentQueryHandlerFactory'
8
8
  import { GraphQLSchema } from 'graphql'
9
+ import { GraphQLKoaState } from '../graphql'
9
10
 
10
11
  const debugHeader = 'x-contember-debug'
11
12
 
@@ -103,6 +104,10 @@ export class ContentApiControllerFactory {
103
104
  request: koa.request,
104
105
  response: koa.response,
105
106
  createContext: ({ operation }) => {
107
+ (koa.state as GraphQLKoaState).graphql = {
108
+ operationName: operation,
109
+ }
110
+
106
111
  const connection = operation === 'query' ? projectContainer.readConnection : projectContainer.connection
107
112
  const contentDatabase = connection.createClient(stage.schema, { module: 'content' })
108
113
 
@@ -114,7 +119,6 @@ export class ContentApiControllerFactory {
114
119
  schemaDatabaseMetadata,
115
120
  schema,
116
121
  timer,
117
- koaContext: koa,
118
122
  requestDebug,
119
123
  systemSchema: projectContainer.systemDatabaseContextFactory.schemaName,
120
124
  stage,
@@ -1,18 +1,15 @@
1
1
  import { Context, createAclVariables, ExecutionContainerFactory } from '@contember/engine-content-api'
2
2
  import { Client, DatabaseMetadata } from '@contember/database'
3
3
  import { Acl, Schema } from '@contember/schema'
4
-
5
4
  import { AuthResult } from '../common'
6
- import { KoaContext, Timer } from '../application'
5
+ import { Timer } from '../application'
7
6
  import { Providers } from '../providers'
8
- import { GraphQLKoaState } from '../graphql'
9
7
  import { ParsedMembership } from '@contember/schema-utils'
10
8
  import { Stage } from '@contember/engine-system-api'
11
9
  import { ProjectConfig } from '../project/config'
12
10
 
13
11
  export type ExtendedGraphqlContext = Context & {
14
12
  identityId: string
15
- koaContext: KoaContext<GraphQLKoaState>
16
13
  requestDebug: boolean
17
14
  }
18
15
 
@@ -23,7 +20,7 @@ export class ContentGraphQLContextFactory {
23
20
  ) {
24
21
  }
25
22
 
26
- create({ db, schema, schemaDatabaseMetadata, authResult, memberships, permissions, timer, koaContext, requestDebug, systemSchema, stage, project }: {
23
+ create({ db, schema, schemaDatabaseMetadata, authResult, memberships, permissions, timer, requestDebug, systemSchema, stage, project }: {
27
24
  db: Client
28
25
  schema: Schema & { id: number }
29
26
  schemaDatabaseMetadata: DatabaseMetadata
@@ -31,7 +28,6 @@ export class ContentGraphQLContextFactory {
31
28
  authResult: AuthResult
32
29
  memberships: readonly ParsedMembership[]
33
30
  timer: Timer
34
- koaContext: KoaContext<GraphQLKoaState>
35
31
  requestDebug: boolean
36
32
  systemSchema: string
37
33
  stage: Stage
@@ -64,7 +60,6 @@ export class ContentGraphQLContextFactory {
64
60
  identityId,
65
61
  executionContainer,
66
62
  timer,
67
- koaContext,
68
63
  requestDebug,
69
64
  }
70
65
  }
@@ -1,11 +1,5 @@
1
1
  import { GraphQLSchema } from 'graphql'
2
- import {
3
- createDbQueriesListener,
4
- createGraphQLQueryHandler,
5
- createGraphqlRequestInfoProviderListener,
6
- GraphQLListener,
7
- GraphQLQueryHandler,
8
- } from '../graphql'
2
+ import { createDbQueriesListener, createGraphQLQueryHandler, GraphQLListener, GraphQLQueryHandler } from '../graphql'
9
3
  import { ExtendedGraphqlContext } from './ContentGraphQLContextFactory'
10
4
 
11
5
  export type ContentQueryHandler = GraphQLQueryHandler<ExtendedGraphqlContext>
@@ -14,9 +8,7 @@ export class ContentQueryHandlerFactory {
14
8
  constructor(private readonly debug: boolean) {}
15
9
 
16
10
  public create(graphQlSchema: GraphQLSchema): ContentQueryHandler {
17
- const listeners: GraphQLListener<ExtendedGraphqlContext>[] = [
18
- createGraphqlRequestInfoProviderListener(),
19
- ]
11
+ const listeners: GraphQLListener<ExtendedGraphqlContext>[] = []
20
12
  listeners.push(createDbQueriesListener(context => context.db, this.debug))
21
13
 
22
14
  return createGraphQLQueryHandler<ExtendedGraphqlContext>({
@@ -10,7 +10,7 @@ import {
10
10
  validate,
11
11
  validateSchema,
12
12
  } from 'graphql'
13
- import LRUCache from 'lru-cache'
13
+ import { LRUCache } from 'lru-cache'
14
14
  import { createHash } from 'node:crypto'
15
15
  import { Request, Response } from 'koa'
16
16
  import { logger } from '@contember/logger'
@@ -65,7 +65,7 @@ export const createGraphQLQueryHandler = <Context>({
65
65
 
66
66
  const now = Date.now()
67
67
  if ((now - lastPrune) > pruneIntervalSeconds * 1000) {
68
- documentCache.prune()
68
+ documentCache.purgeStale()
69
69
  lastPrune = now
70
70
  }
71
71
  const listenersQueue = [...listeners]
@@ -147,7 +147,7 @@ export const createGraphQLQueryHandler = <Context>({
147
147
  }
148
148
 
149
149
 
150
- const extractOriginalError = (e: Error): Error => {
150
+ export const extractOriginalError = (e: Error): Error => {
151
151
  if (e instanceof GraphQLError && e.originalError) {
152
152
  return extractOriginalError(e.originalError)
153
153
  }
@@ -1,16 +1,3 @@
1
- import { GraphQLListener } from './execution'
2
- import { KoaContext } from '../application'
3
-
4
- export const createGraphqlRequestInfoProviderListener = (): GraphQLListener<{
5
- koaContext: KoaContext<GraphQLKoaState>
6
- }> => ({
7
- onExecute: ({ context, operation }) => {
8
- context.koaContext.state.graphql = {
9
- operationName: operation,
10
- }
11
- },
12
- })
13
-
14
1
  export type GraphQLKoaState = {
15
2
  graphql?: {
16
3
  operationName: string | undefined