@contember/engine-http 2.2.0-alpha.19 → 2.2.0-alpha.20

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 (188) hide show
  1. package/CLAUDE.md +2 -0
  2. package/dist/development/index.cjs +8 -0
  3. package/dist/development/index.cjs.map +1 -1
  4. package/dist/development/index.js +9 -1
  5. package/dist/development/index.js.map +1 -1
  6. package/dist/development/src/MasterContainer.cjs +28 -14
  7. package/dist/development/src/MasterContainer.cjs.map +1 -1
  8. package/dist/development/src/MasterContainer.js +28 -14
  9. package/dist/development/src/MasterContainer.js.map +1 -1
  10. package/dist/development/src/config/configSchema.cjs +50 -0
  11. package/dist/development/src/config/configSchema.cjs.map +1 -1
  12. package/dist/development/src/config/configSchema.js +50 -0
  13. package/dist/development/src/config/configSchema.js.map +1 -1
  14. package/dist/development/src/config/configTemplate.cjs +8 -0
  15. package/dist/development/src/config/configTemplate.cjs.map +1 -1
  16. package/dist/development/src/config/configTemplate.js +8 -0
  17. package/dist/development/src/config/configTemplate.js.map +1 -1
  18. package/dist/development/src/content/ContentApiControllerFactory.cjs +12 -1
  19. package/dist/development/src/content/ContentApiControllerFactory.cjs.map +1 -1
  20. package/dist/development/src/content/ContentApiControllerFactory.js +12 -1
  21. package/dist/development/src/content/ContentApiControllerFactory.js.map +1 -1
  22. package/dist/development/src/content/ContentApiSpecificCache.cjs +20 -5
  23. package/dist/development/src/content/ContentApiSpecificCache.cjs.map +1 -1
  24. package/dist/development/src/content/ContentApiSpecificCache.js +20 -5
  25. package/dist/development/src/content/ContentApiSpecificCache.js.map +1 -1
  26. package/dist/development/src/content/ContentGraphQLEngine.cjs +17 -0
  27. package/dist/development/src/content/ContentGraphQLEngine.cjs.map +1 -0
  28. package/dist/development/src/content/ContentGraphQLEngine.js +17 -0
  29. package/dist/development/src/content/ContentGraphQLEngine.js.map +1 -0
  30. package/dist/development/src/content/ContentQueryHandlerFactory.cjs +4 -1
  31. package/dist/development/src/content/ContentQueryHandlerFactory.cjs.map +1 -1
  32. package/dist/development/src/content/ContentQueryHandlerFactory.js +5 -2
  33. package/dist/development/src/content/ContentQueryHandlerFactory.js.map +1 -1
  34. package/dist/development/src/content/ContentRequestLog.cjs +97 -0
  35. package/dist/development/src/content/ContentRequestLog.cjs.map +1 -0
  36. package/dist/development/src/content/ContentRequestLog.js +97 -0
  37. package/dist/development/src/content/ContentRequestLog.js.map +1 -0
  38. package/dist/development/src/content/GraphQlSchemaFactory.cjs +121 -45
  39. package/dist/development/src/content/GraphQlSchemaFactory.cjs.map +1 -1
  40. package/dist/development/src/content/GraphQlSchemaFactory.js +122 -46
  41. package/dist/development/src/content/GraphQlSchemaFactory.js.map +1 -1
  42. package/dist/development/src/graphql/execution.cjs +18 -5
  43. package/dist/development/src/graphql/execution.cjs.map +1 -1
  44. package/dist/development/src/graphql/execution.js +19 -6
  45. package/dist/development/src/graphql/execution.js.map +1 -1
  46. package/dist/development/src/project/ProjectContainer.cjs +2 -0
  47. package/dist/development/src/project/ProjectContainer.cjs.map +1 -1
  48. package/dist/development/src/project/ProjectContainer.js +2 -0
  49. package/dist/development/src/project/ProjectContainer.js.map +1 -1
  50. package/dist/development/src/projectGroup/ProjectGroupContainer.cjs +2 -0
  51. package/dist/development/src/projectGroup/ProjectGroupContainer.cjs.map +1 -1
  52. package/dist/development/src/projectGroup/ProjectGroupContainer.js +2 -0
  53. package/dist/development/src/projectGroup/ProjectGroupContainer.js.map +1 -1
  54. package/dist/development/src/system/ContentQueryExecutor.cjs +23 -6
  55. package/dist/development/src/system/ContentQueryExecutor.cjs.map +1 -1
  56. package/dist/development/src/system/ContentQueryExecutor.js +25 -8
  57. package/dist/development/src/system/ContentQueryExecutor.js.map +1 -1
  58. package/dist/development/src/system/SystemGraphQLHandlerFactory.cjs +1 -1
  59. package/dist/development/src/system/SystemGraphQLHandlerFactory.cjs.map +1 -1
  60. package/dist/development/src/system/SystemGraphQLHandlerFactory.js +2 -2
  61. package/dist/development/src/system/SystemGraphQLHandlerFactory.js.map +1 -1
  62. package/dist/development/src/tenant/TenantGraphQLHandlerFactory.cjs +1 -1
  63. package/dist/development/src/tenant/TenantGraphQLHandlerFactory.cjs.map +1 -1
  64. package/dist/development/src/tenant/TenantGraphQLHandlerFactory.js +2 -2
  65. package/dist/development/src/tenant/TenantGraphQLHandlerFactory.js.map +1 -1
  66. package/dist/production/index.cjs +8 -0
  67. package/dist/production/index.cjs.map +1 -1
  68. package/dist/production/index.js +9 -1
  69. package/dist/production/index.js.map +1 -1
  70. package/dist/production/src/MasterContainer.cjs +28 -14
  71. package/dist/production/src/MasterContainer.cjs.map +1 -1
  72. package/dist/production/src/MasterContainer.js +28 -14
  73. package/dist/production/src/MasterContainer.js.map +1 -1
  74. package/dist/production/src/config/configSchema.cjs +50 -0
  75. package/dist/production/src/config/configSchema.cjs.map +1 -1
  76. package/dist/production/src/config/configSchema.js +50 -0
  77. package/dist/production/src/config/configSchema.js.map +1 -1
  78. package/dist/production/src/config/configTemplate.cjs +8 -0
  79. package/dist/production/src/config/configTemplate.cjs.map +1 -1
  80. package/dist/production/src/config/configTemplate.js +8 -0
  81. package/dist/production/src/config/configTemplate.js.map +1 -1
  82. package/dist/production/src/content/ContentApiControllerFactory.cjs +12 -1
  83. package/dist/production/src/content/ContentApiControllerFactory.cjs.map +1 -1
  84. package/dist/production/src/content/ContentApiControllerFactory.js +12 -1
  85. package/dist/production/src/content/ContentApiControllerFactory.js.map +1 -1
  86. package/dist/production/src/content/ContentApiSpecificCache.cjs +20 -5
  87. package/dist/production/src/content/ContentApiSpecificCache.cjs.map +1 -1
  88. package/dist/production/src/content/ContentApiSpecificCache.js +20 -5
  89. package/dist/production/src/content/ContentApiSpecificCache.js.map +1 -1
  90. package/dist/production/src/content/ContentGraphQLEngine.cjs +17 -0
  91. package/dist/production/src/content/ContentGraphQLEngine.cjs.map +1 -0
  92. package/dist/production/src/content/ContentGraphQLEngine.js +17 -0
  93. package/dist/production/src/content/ContentGraphQLEngine.js.map +1 -0
  94. package/dist/production/src/content/ContentQueryHandlerFactory.cjs +4 -1
  95. package/dist/production/src/content/ContentQueryHandlerFactory.cjs.map +1 -1
  96. package/dist/production/src/content/ContentQueryHandlerFactory.js +5 -2
  97. package/dist/production/src/content/ContentQueryHandlerFactory.js.map +1 -1
  98. package/dist/production/src/content/ContentRequestLog.cjs +97 -0
  99. package/dist/production/src/content/ContentRequestLog.cjs.map +1 -0
  100. package/dist/production/src/content/ContentRequestLog.js +97 -0
  101. package/dist/production/src/content/ContentRequestLog.js.map +1 -0
  102. package/dist/production/src/content/GraphQlSchemaFactory.cjs +121 -45
  103. package/dist/production/src/content/GraphQlSchemaFactory.cjs.map +1 -1
  104. package/dist/production/src/content/GraphQlSchemaFactory.js +122 -46
  105. package/dist/production/src/content/GraphQlSchemaFactory.js.map +1 -1
  106. package/dist/production/src/graphql/execution.cjs +18 -5
  107. package/dist/production/src/graphql/execution.cjs.map +1 -1
  108. package/dist/production/src/graphql/execution.js +19 -6
  109. package/dist/production/src/graphql/execution.js.map +1 -1
  110. package/dist/production/src/project/ProjectContainer.cjs +2 -0
  111. package/dist/production/src/project/ProjectContainer.cjs.map +1 -1
  112. package/dist/production/src/project/ProjectContainer.js +2 -0
  113. package/dist/production/src/project/ProjectContainer.js.map +1 -1
  114. package/dist/production/src/projectGroup/ProjectGroupContainer.cjs +2 -0
  115. package/dist/production/src/projectGroup/ProjectGroupContainer.cjs.map +1 -1
  116. package/dist/production/src/projectGroup/ProjectGroupContainer.js +2 -0
  117. package/dist/production/src/projectGroup/ProjectGroupContainer.js.map +1 -1
  118. package/dist/production/src/system/ContentQueryExecutor.cjs +23 -6
  119. package/dist/production/src/system/ContentQueryExecutor.cjs.map +1 -1
  120. package/dist/production/src/system/ContentQueryExecutor.js +25 -8
  121. package/dist/production/src/system/ContentQueryExecutor.js.map +1 -1
  122. package/dist/production/src/system/SystemGraphQLHandlerFactory.cjs +1 -1
  123. package/dist/production/src/system/SystemGraphQLHandlerFactory.cjs.map +1 -1
  124. package/dist/production/src/system/SystemGraphQLHandlerFactory.js +2 -2
  125. package/dist/production/src/system/SystemGraphQLHandlerFactory.js.map +1 -1
  126. package/dist/production/src/tenant/TenantGraphQLHandlerFactory.cjs +1 -1
  127. package/dist/production/src/tenant/TenantGraphQLHandlerFactory.cjs.map +1 -1
  128. package/dist/production/src/tenant/TenantGraphQLHandlerFactory.js +2 -2
  129. package/dist/production/src/tenant/TenantGraphQLHandlerFactory.js.map +1 -1
  130. package/dist/tests/cases/unit/contentGraphqlCompat.test.d.ts +2 -0
  131. package/dist/tests/cases/unit/contentGraphqlCompat.test.d.ts.map +1 -0
  132. package/dist/tests/cases/unit/contentRequestLog.test.d.ts +2 -0
  133. package/dist/tests/cases/unit/contentRequestLog.test.d.ts.map +1 -0
  134. package/dist/tests/contentControllerHarness.d.ts +29 -0
  135. package/dist/tests/contentControllerHarness.d.ts.map +1 -0
  136. package/dist/tests/tsconfig.tsbuildinfo +1 -1
  137. package/dist/types/MasterContainer.d.ts +10 -1
  138. package/dist/types/MasterContainer.d.ts.map +1 -1
  139. package/dist/types/config/configSchema.d.ts +10 -0
  140. package/dist/types/config/configSchema.d.ts.map +1 -1
  141. package/dist/types/config/configTemplate.d.ts.map +1 -1
  142. package/dist/types/content/ContentApiControllerFactory.d.ts +3 -1
  143. package/dist/types/content/ContentApiControllerFactory.d.ts.map +1 -1
  144. package/dist/types/content/ContentApiSpecificCache.d.ts +2 -0
  145. package/dist/types/content/ContentApiSpecificCache.d.ts.map +1 -1
  146. package/dist/types/content/ContentGraphQLEngine.d.ts +18 -0
  147. package/dist/types/content/ContentGraphQLEngine.d.ts.map +1 -0
  148. package/dist/types/content/ContentQueryHandlerFactory.d.ts +3 -2
  149. package/dist/types/content/ContentQueryHandlerFactory.d.ts.map +1 -1
  150. package/dist/types/content/ContentRequestLog.d.ts +49 -0
  151. package/dist/types/content/ContentRequestLog.d.ts.map +1 -0
  152. package/dist/types/content/GraphQlSchemaFactory.d.ts +14 -4
  153. package/dist/types/content/GraphQlSchemaFactory.d.ts.map +1 -1
  154. package/dist/types/content/index.d.ts +2 -0
  155. package/dist/types/content/index.d.ts.map +1 -1
  156. package/dist/types/graphql/execution.d.ts +21 -3
  157. package/dist/types/graphql/execution.d.ts.map +1 -1
  158. package/dist/types/system/ContentQueryExecutor.d.ts +4 -1
  159. package/dist/types/system/ContentQueryExecutor.d.ts.map +1 -1
  160. package/dist/types/system/SystemGraphQLHandlerFactory.d.ts.map +1 -1
  161. package/dist/types/tenant/TenantGraphQLHandlerFactory.d.ts.map +1 -1
  162. package/dist/types/tsconfig.tsbuildinfo +1 -1
  163. package/package.json +21 -18
  164. package/src/MasterContainer.ts +31 -14
  165. package/src/config/configSchema.ts +63 -0
  166. package/src/config/configTemplate.ts +8 -0
  167. package/src/content/ContentApiControllerFactory.ts +15 -2
  168. package/src/content/ContentApiSpecificCache.ts +22 -7
  169. package/src/content/ContentGraphQLEngine.ts +24 -0
  170. package/src/content/ContentQueryHandlerFactory.ts +16 -4
  171. package/src/content/ContentRequestLog.ts +138 -0
  172. package/src/content/GraphQlSchemaFactory.ts +173 -54
  173. package/src/content/index.ts +2 -0
  174. package/src/graphql/execution.ts +35 -7
  175. package/src/project/ProjectContainer.ts +1 -1
  176. package/src/system/ContentQueryExecutor.ts +43 -10
  177. package/src/system/SystemGraphQLHandlerFactory.ts +8 -2
  178. package/src/tenant/TenantGraphQLHandlerFactory.ts +2 -2
  179. package/src/tsconfig.json +1 -0
  180. package/tests/cases/unit/__snapshots__/contentGraphqlCompat.test.ts.snap +14295 -0
  181. package/tests/cases/unit/contentGraphqlCompat.test.ts +372 -0
  182. package/tests/cases/unit/contentRequestLog.test.ts +141 -0
  183. package/tests/cases/unit/forcePrimary.test.ts +93 -284
  184. package/tests/cases/unit/graphQlSchemaFactory.test.ts +227 -35
  185. package/tests/cases/unit/requestMemoryBudget.test.ts +96 -78
  186. package/tests/cases/unit/serverConfig.test.ts +133 -0
  187. package/tests/contentControllerHarness.ts +222 -0
  188. package/tests/tsconfig.json +3 -1
@@ -1 +1 @@
1
- {"version":3,"file":"ContentApiControllerFactory.cjs","sources":["../../../../../packages/engine-http/src/content/ContentApiControllerFactory.ts"],"sourcesContent":["import { createAclVariables, ExecutionContainerFactory } from '@contember/engine-content-api'\nimport { StageBySlugQuery } from '@contember/engine-system-api'\nimport { Client, RequestMemoryBudget, RequestMemoryBudgetOptions } from '@contember/database'\nimport { GraphQLSchema } from 'graphql'\nimport { HttpController } from '../application/index.js'\nimport { HttpErrorResponse, HttpResponse } from '../common/index.js'\nimport { GraphQLKoaState } from '../graphql/index.js'\nimport { ProjectContextResolver } from '../project-common/index.js'\nimport { ContentQueryHandler, ContentQueryHandlerFactory } from './ContentQueryHandlerFactory.js'\nimport { GraphQlSchemaFactory } from './GraphQlSchemaFactory.js'\nimport { NotModifiedChecker } from './NotModifiedChecker.js'\nimport { TestTransactionService } from '../testing/index.js'\nimport { isTruthyHeader } from '../utils/truthyHeader.js'\n\nconst debugHeader = 'x-contember-debug'\nconst testSessionHeader = 'x-contember-test-session'\nconst forcePrimaryHeader = 'x-contember-force-primary'\n\nexport class ContentApiControllerFactory {\n\tconstructor(\n\t\tprivate readonly notModifiedChecker: NotModifiedChecker,\n\t\tprivate readonly executionContainerFactory: ExecutionContainerFactory,\n\t\tprivate readonly handlerFactory: ContentQueryHandlerFactory,\n\t\tprivate readonly projectContextResolver: ProjectContextResolver,\n\t\tprivate readonly graphQlSchemaFactory: GraphQlSchemaFactory,\n\t\tprivate readonly testTransactionService: TestTransactionService,\n\t\tprivate readonly forcePrimaryHeaderEnabled: boolean,\n\t\tprivate readonly memoryBudgetOptions?: RequestMemoryBudgetOptions,\n\t) {\n\t}\n\n\tcreate(): HttpController {\n\t\tconst handlerCache = new WeakMap<GraphQLSchema, ContentQueryHandler>()\n\t\treturn async context => {\n\t\t\tconst { params, timer, projectGroup, authResult, request, koa, clientIp } = context\n\t\t\tif (!authResult) {\n\t\t\t\treturn new HttpErrorResponse(401, 'Authentication required')\n\t\t\t}\n\t\t\tconst { projectContainer, project } = await this.projectContextResolver.resolve(context)\n\n\t\t\tconst logger = context.logger.child({\n\t\t\t\tproject: project.slug,\n\t\t\t})\n\n\t\t\tconst forcePrimary = this.forcePrimaryHeaderEnabled && isTruthyHeader(request, forcePrimaryHeader)\n\t\t\t// Without a replica, forced reads change nothing, so clients need not open a window.\n\t\t\tconst markMutations = this.forcePrimaryHeaderEnabled && projectContainer.readConnection !== projectContainer.connection\n\t\t\tconst systemDatabase = forcePrimary ? projectContainer.systemDatabaseContext : projectContainer.systemReadDatabaseContext\n\t\t\tconst stage = await systemDatabase.queryHandler.fetch(new StageBySlugQuery(params.stageSlug))\n\t\t\tif (!stage) {\n\t\t\t\treturn new HttpErrorResponse(404, `Stage ${params.stageSlug} NOT found`)\n\t\t\t}\n\t\t\tconst notModifiedRes = await this.notModifiedChecker.checkNotModified({\n\t\t\t\trequest: context.request,\n\t\t\t\tbody: context.body,\n\t\t\t\ttimer: context.timer,\n\t\t\t\tsystemDatabase,\n\t\t\t\tstageId: stage.id,\n\t\t\t})\n\t\t\tif (notModifiedRes?.isModified === false) {\n\t\t\t\treturn new HttpResponse(304)\n\t\t\t}\n\n\t\t\tconst schemaWithMeta = await projectContainer.contentSchemaResolver.getSchema({ db: systemDatabase, stage: stage.slug, normalize: true })\n\t\t\tconst schema = schemaWithMeta.schema\n\t\t\tconst { effective: memberships, fetched: fetchedMemberships } = await timer(\n\t\t\t\t'MembershipFetch',\n\t\t\t\t() =>\n\t\t\t\t\tprojectGroup.projectMembershipResolver.resolveMemberships({\n\t\t\t\t\t\trequest: koa,\n\t\t\t\t\t\tacl: schema.acl,\n\t\t\t\t\t\tprojectSlug: project.slug,\n\t\t\t\t\t\tidentity: {\n\t\t\t\t\t\t\tidentityId: authResult.identityId,\n\t\t\t\t\t\t\tpersonId: authResult.personId ?? undefined,\n\t\t\t\t\t\t\troles: authResult.roles,\n\t\t\t\t\t\t},\n\t\t\t\t\t}),\n\t\t\t)\n\n\t\t\tlogger.debug('Memberships fetched', { memberships })\n\n\t\t\tconst debugHeaderValue = request.headers[debugHeader]\n\t\t\tif (Array.isArray(debugHeaderValue)) {\n\t\t\t\treturn new HttpErrorResponse(400, `Invalid format of ${debugHeader}`)\n\t\t\t}\n\t\t\tconst requestDebug = debugHeaderValue === '1' && fetchedMemberships.some(it => schema.acl.roles[it.role]?.debug)\n\t\t\tif (requestDebug) {\n\t\t\t\tcontext.requestDebugMode = true\n\t\t\t}\n\n\t\t\tconst projectRoles = memberships.map(it => it.role)\n\n\t\t\tconst { schema: graphQlSchema, permissions, allPermissions } = await timer(\n\t\t\t\t'GraphQLSchemaCreate',\n\t\t\t\t() =>\n\t\t\t\t\tthis.graphQlSchemaFactory.create(schema, {\n\t\t\t\t\t\tprojectRoles: projectRoles,\n\t\t\t\t\t}, project),\n\t\t\t)\n\n\t\t\tconst schemaDatabaseMetadata = await projectContainer.projectDatabaseMetadataResolver.resolveDatabaseMetadata(systemDatabase, schema, stage.schema)\n\n\t\t\t// Test-transaction mode: if the request carries a session header, bind its content DB\n\t\t\t// client to the session's pinned (rolled-back-later) transaction. Decided here, from the\n\t\t\t// request itself — no async-context routing.\n\t\t\tlet testContentDatabase: Client | undefined\n\t\t\tif (this.testTransactionService.isEnabled()) {\n\t\t\t\tconst headerValue = request.headers[testSessionHeader]\n\t\t\t\tif (headerValue !== undefined) {\n\t\t\t\t\tconst token = Array.isArray(headerValue) ? headerValue[0] : headerValue\n\t\t\t\t\ttestContentDatabase = await this.testTransactionService.resolveContentClient(\n\t\t\t\t\t\ttoken,\n\t\t\t\t\t\tproject.slug,\n\t\t\t\t\t\tprojectContainer.connection,\n\t\t\t\t\t\tstage.schema,\n\t\t\t\t\t\t{ module: 'content' },\n\t\t\t\t\t)\n\t\t\t\t\tif (!testContentDatabase) {\n\t\t\t\t\t\treturn new HttpErrorResponse(400, 'Unknown or expired test transaction session')\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst handler = await (async () => {\n\t\t\t\tconst existingHandler = handlerCache.get(graphQlSchema)\n\t\t\t\tif (existingHandler) {\n\t\t\t\t\treturn existingHandler\n\t\t\t\t}\n\t\t\t\tconst newHandler = await this.handlerFactory.create(graphQlSchema)\n\t\t\t\thandlerCache.set(graphQlSchema, newHandler)\n\t\t\t\treturn newHandler\n\t\t\t})()\n\n\t\t\tawait logger.scope(async logger => {\n\t\t\t\tlogger.debug('Content query processing started')\n\n\t\t\t\tawait timer('GraphQL', () =>\n\t\t\t\t\thandler({\n\t\t\t\t\t\trequest: koa.request,\n\t\t\t\t\t\tresponse: koa.response,\n\t\t\t\t\t\tcreateContext: ({ operation, queryHash }) => {\n\t\t\t\t\t\t\t// Clients open a primary read window only on this marker, so it must not be sent while the header is ignored.\n\t\t\t\t\t\t\tif (operation === 'mutation' && markMutations) {\n\t\t\t\t\t\t\t\tkoa.response.set('X-Contember-Mutation', '1')\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t;(koa.state as GraphQLKoaState).graphql = {\n\t\t\t\t\t\t\t\toperationName: operation,\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst baseConnection = operation === 'query' && !forcePrimary ? projectContainer.readConnection : projectContainer.connection\n\t\t\t\t\t\t\tconst maxConnectionsPerRequest = 'maxConnectionsPerRequest' in project.db\n\t\t\t\t\t\t\t\t? project.db.maxConnectionsPerRequest\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t\t// Optionally cap how many pool connections this single request may hold concurrently,\n\t\t\t\t\t\t\t// so one request cannot starve the shared pool under high concurrency. Defaults to unlimited.\n\t\t\t\t\t\t\tconst connection = maxConnectionsPerRequest !== undefined\n\t\t\t\t\t\t\t\t? baseConnection.withMaxConnections(maxConnectionsPerRequest)\n\t\t\t\t\t\t\t\t: baseConnection\n\t\t\t\t\t\t\tconst memoryBudget = this.memoryBudgetOptions\n\t\t\t\t\t\t\t\t? new RequestMemoryBudget(this.memoryBudgetOptions)\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t\tconst contentDatabase = testContentDatabase ?? connection.createClient(stage.schema, { module: 'content' })\n\t\t\t\t\t\t\tif (memoryBudget) {\n\t\t\t\t\t\t\t\tconst logMemory = () => {\n\t\t\t\t\t\t\t\t\tkoa.res.off('finish', logMemory)\n\t\t\t\t\t\t\t\t\tkoa.res.off('close', logMemory)\n\t\t\t\t\t\t\t\t\tconst memory = memoryBudget.snapshot()\n\t\t\t\t\t\t\t\t\tif (memory.warningThresholdExceeded) {\n\t\t\t\t\t\t\t\t\t\tlogger.warn('Content request memory usage', { operation, queryHash, ...memory })\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tkoa.res.once('finish', logMemory)\n\t\t\t\t\t\t\t\tkoa.res.once('close', logMemory)\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst identityVariables = createAclVariables(schema.acl, memberships)\n\t\t\t\t\t\t\tlet identityId = authResult.identityId\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tauthResult.assumedIdentityId\n\t\t\t\t\t\t\t\t&& memberships.some(it => schema.acl.roles[it.role].system?.assumeIdentity)\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tidentityId = authResult.assumedIdentityId\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst executionContainer = this.executionContainerFactory.create({\n\t\t\t\t\t\t\t\tdb: contentDatabase,\n\t\t\t\t\t\t\t\tmemoryBudget,\n\t\t\t\t\t\t\t\tidentityVariables,\n\t\t\t\t\t\t\t\tidentityId,\n\t\t\t\t\t\t\t\tschema,\n\t\t\t\t\t\t\t\tschemaMeta: { id: schemaWithMeta.meta.id },\n\t\t\t\t\t\t\t\tschemaDatabaseMetadata,\n\t\t\t\t\t\t\t\tpermissions,\n\t\t\t\t\t\t\t\tallPermissions,\n\t\t\t\t\t\t\t\tsystemSchema: projectContainer.systemDatabaseContextFactory.schemaName,\n\t\t\t\t\t\t\t\tstage,\n\t\t\t\t\t\t\t\tproject,\n\t\t\t\t\t\t\t\tuserInfo: {\n\t\t\t\t\t\t\t\t\tipAddress: clientIp,\n\t\t\t\t\t\t\t\t\tuserAgent: authResult.clientUserAgent ?? null,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t})\n\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tdb: contentDatabase,\n\t\t\t\t\t\t\t\tmemoryBudget,\n\t\t\t\t\t\t\t\tidentityVariables,\n\t\t\t\t\t\t\t\tidentityId,\n\t\t\t\t\t\t\t\texecutionContainer,\n\t\t\t\t\t\t\t\ttimer,\n\t\t\t\t\t\t\t\trequestDebug,\n\t\t\t\t\t\t\t\tproject,\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t},\n\t\t\t\t\t}))\n\t\t\t\tlogger.debug('Content query finished')\n\t\t\t})\n\n\t\t\tnotModifiedRes?.setResponseHeader(context.response)\n\t\t}\n\t}\n}\n"],"names":["HttpErrorResponse","isTruthyHeader","StageBySlugQuery","HttpResponse","logger","RequestMemoryBudget","createAclVariables"],"mappings":";;;;;;;;;AAcA,MAAM,cAAc;AACpB,MAAM,oBAAoB;AAC1B,MAAM,qBAAqB;AAEpB,MAAM,4BAA4B;AAAA,EACxC,YACkB,oBACA,2BACA,gBACA,wBACA,sBACA,wBACA,2BACA,qBAChB;AARgB,SAAA,qBAAA;AACA,SAAA,4BAAA;AACA,SAAA,iBAAA;AACA,SAAA,yBAAA;AACA,SAAA,uBAAA;AACA,SAAA,yBAAA;AACA,SAAA,4BAAA;AACA,SAAA,sBAAA;AAAA,EAElB;AAAA,EAEA,SAAyB;AACxB,UAAM,mCAAmB,QAAA;AACzB,WAAO,OAAM,YAAW;AACvB,YAAM,EAAE,QAAQ,OAAO,cAAc,YAAY,SAAS,KAAK,aAAa;AAC5E,UAAI,CAAC,YAAY;AAChB,eAAO,IAAIA,aAAAA,kBAAkB,KAAK,yBAA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OAAO,IAAI,MAAM,GAAG,IAAI,EAAE,QAAQ,cAAc,GACzE;AACD,2BAAa,WAAW;AAAA,YACzB;AAEA,kBAAM,qBAAqB,KAAK,0BAA0B,OAAO;AAAA,cAChE,IAAI;AAAA,cACJ;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA,YAAY,EAAE,IAAI,eAAe,KAAK,GAAA;AAAA,cACtC;AAAA,cACA;AAAA,cACA;AAAA,cACA,cAAc,iBAAiB,6BAA6B;AAAA,cAC5D;AAAA,cACA;AAAA,cACA,UAAU;AAAA,gBACT,WAAW;AAAA,gBACX,WAAW,WAAW,mBAAmB;AAAA,cAAA;AAAA,YAC1C,CACA;AAED,mBAAO;AAAA,cACN,IAAI;AAAA,cACJ;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,YAAA;AAAA,UAEF;AAAA,QAAA,CACA,CAAC;AACHF,gBAAO,MAAM,wBAAwB;AAAA,MACtC,CAAC;AAED,sBAAgB,kBAAkB,QAAQ,QAAQ;AAAA,IACnD;AAAA,EACD;AACD;;"}
1
+ {"version":3,"file":"ContentApiControllerFactory.cjs","sources":["../../../../../packages/engine-http/src/content/ContentApiControllerFactory.ts"],"sourcesContent":["import { createAclVariables, ExecutionContainerFactory } from '@contember/engine-content-api'\nimport { StageBySlugQuery } from '@contember/engine-system-api'\nimport { Client, RequestMemoryBudget, RequestMemoryBudgetOptions } from '@contember/database'\nimport { HttpController } from '../application/index.js'\nimport { HttpErrorResponse, HttpResponse } from '../common/index.js'\nimport { GraphQLKoaState } from '../graphql/index.js'\nimport { ProjectContextResolver } from '../project-common/index.js'\nimport { ContentQueryHandler, ContentQueryHandlerFactory } from './ContentQueryHandlerFactory.js'\nimport { GraphQlSchemaFactory } from './GraphQlSchemaFactory.js'\nimport { ContentGraphQLSchema } from './ContentGraphQLEngine.js'\nimport { NotModifiedChecker } from './NotModifiedChecker.js'\nimport { ContentRequestLog } from './ContentRequestLog.js'\nimport { TestTransactionService } from '../testing/index.js'\nimport { isTruthyHeader } from '../utils/truthyHeader.js'\n\nconst debugHeader = 'x-contember-debug'\nconst testSessionHeader = 'x-contember-test-session'\nconst forcePrimaryHeader = 'x-contember-force-primary'\n\nexport class ContentApiControllerFactory {\n\tconstructor(\n\t\tprivate readonly notModifiedChecker: NotModifiedChecker,\n\t\tprivate readonly executionContainerFactory: ExecutionContainerFactory,\n\t\tprivate readonly handlerFactory: ContentQueryHandlerFactory,\n\t\tprivate readonly projectContextResolver: ProjectContextResolver,\n\t\tprivate readonly graphQlSchemaFactory: GraphQlSchemaFactory,\n\t\tprivate readonly testTransactionService: TestTransactionService,\n\t\tprivate readonly forcePrimaryHeaderEnabled: boolean,\n\t\tprivate readonly memoryBudgetOptions?: RequestMemoryBudgetOptions,\n\t\tprivate readonly requestLog?: ContentRequestLog,\n\t) {\n\t}\n\n\tcreate(): HttpController {\n\t\tconst handlerCache = new WeakMap<ContentGraphQLSchema, ContentQueryHandler>()\n\t\treturn async context => {\n\t\t\tconst { params, timer, projectGroup, authResult, request, koa, clientIp } = context\n\t\t\tif (!authResult) {\n\t\t\t\treturn new HttpErrorResponse(401, 'Authentication required')\n\t\t\t}\n\t\t\tconst { projectContainer, project } = await this.projectContextResolver.resolve(context)\n\n\t\t\tconst logger = context.logger.child({\n\t\t\t\tproject: project.slug,\n\t\t\t})\n\n\t\t\tconst forcePrimary = this.forcePrimaryHeaderEnabled && isTruthyHeader(request, forcePrimaryHeader)\n\t\t\t// Without a replica, forced reads change nothing, so clients need not open a window.\n\t\t\tconst markMutations = this.forcePrimaryHeaderEnabled && projectContainer.readConnection !== projectContainer.connection\n\t\t\tconst systemDatabase = forcePrimary ? projectContainer.systemDatabaseContext : projectContainer.systemReadDatabaseContext\n\t\t\tconst stage = await systemDatabase.queryHandler.fetch(new StageBySlugQuery(params.stageSlug))\n\t\t\tif (!stage) {\n\t\t\t\treturn new HttpErrorResponse(404, `Stage ${params.stageSlug} NOT found`)\n\t\t\t}\n\t\t\tconst notModifiedRes = await this.notModifiedChecker.checkNotModified({\n\t\t\t\trequest: context.request,\n\t\t\t\tbody: context.body,\n\t\t\t\ttimer: context.timer,\n\t\t\t\tsystemDatabase,\n\t\t\t\tstageId: stage.id,\n\t\t\t})\n\t\t\tif (notModifiedRes?.isModified === false) {\n\t\t\t\treturn new HttpResponse(304)\n\t\t\t}\n\n\t\t\tconst schemaWithMeta = await projectContainer.contentSchemaResolver.getSchema({ db: systemDatabase, stage: stage.slug, normalize: true })\n\t\t\tconst schema = schemaWithMeta.schema\n\t\t\tconst { effective: memberships, fetched: fetchedMemberships } = await timer(\n\t\t\t\t'MembershipFetch',\n\t\t\t\t() =>\n\t\t\t\t\tprojectGroup.projectMembershipResolver.resolveMemberships({\n\t\t\t\t\t\trequest: koa,\n\t\t\t\t\t\tacl: schema.acl,\n\t\t\t\t\t\tprojectSlug: project.slug,\n\t\t\t\t\t\tidentity: {\n\t\t\t\t\t\t\tidentityId: authResult.identityId,\n\t\t\t\t\t\t\tpersonId: authResult.personId ?? undefined,\n\t\t\t\t\t\t\troles: authResult.roles,\n\t\t\t\t\t\t},\n\t\t\t\t\t}),\n\t\t\t)\n\n\t\t\tlogger.debug('Memberships fetched', { memberships })\n\n\t\t\tconst debugHeaderValue = request.headers[debugHeader]\n\t\t\tif (Array.isArray(debugHeaderValue)) {\n\t\t\t\treturn new HttpErrorResponse(400, `Invalid format of ${debugHeader}`)\n\t\t\t}\n\t\t\tconst requestDebug = debugHeaderValue === '1' && fetchedMemberships.some(it => schema.acl.roles[it.role]?.debug)\n\t\t\tif (requestDebug) {\n\t\t\t\tcontext.requestDebugMode = true\n\t\t\t}\n\n\t\t\tconst projectRoles = memberships.map(it => it.role)\n\n\t\t\tconst { schema: graphQlSchema, permissions, allPermissions } = await timer(\n\t\t\t\t'GraphQLSchemaCreate',\n\t\t\t\t() =>\n\t\t\t\t\tthis.graphQlSchemaFactory.create(schema, {\n\t\t\t\t\t\tprojectRoles: projectRoles,\n\t\t\t\t\t}, project),\n\t\t\t)\n\n\t\t\tconst schemaDatabaseMetadata = await projectContainer.projectDatabaseMetadataResolver.resolveDatabaseMetadata(systemDatabase, schema, stage.schema)\n\n\t\t\t// Test-transaction mode: if the request carries a session header, bind its content DB\n\t\t\t// client to the session's pinned (rolled-back-later) transaction. Decided here, from the\n\t\t\t// request itself — no async-context routing.\n\t\t\tlet testContentDatabase: Client | undefined\n\t\t\tif (this.testTransactionService.isEnabled()) {\n\t\t\t\tconst headerValue = request.headers[testSessionHeader]\n\t\t\t\tif (headerValue !== undefined) {\n\t\t\t\t\tconst token = Array.isArray(headerValue) ? headerValue[0] : headerValue\n\t\t\t\t\ttestContentDatabase = await this.testTransactionService.resolveContentClient(\n\t\t\t\t\t\ttoken,\n\t\t\t\t\t\tproject.slug,\n\t\t\t\t\t\tprojectContainer.connection,\n\t\t\t\t\t\tstage.schema,\n\t\t\t\t\t\t{ module: 'content' },\n\t\t\t\t\t)\n\t\t\t\t\tif (!testContentDatabase) {\n\t\t\t\t\t\treturn new HttpErrorResponse(400, 'Unknown or expired test transaction session')\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst handler = await (async () => {\n\t\t\t\tconst existingHandler = handlerCache.get(graphQlSchema)\n\t\t\t\tif (existingHandler) {\n\t\t\t\t\treturn existingHandler\n\t\t\t\t}\n\t\t\t\tconst newHandler = await this.handlerFactory.create(graphQlSchema)\n\t\t\t\thandlerCache.set(graphQlSchema, newHandler)\n\t\t\t\treturn newHandler\n\t\t\t})()\n\n\t\t\tawait logger.scope(async logger => {\n\t\t\t\tlogger.debug('Content query processing started')\n\n\t\t\t\tawait timer('GraphQL', () =>\n\t\t\t\t\thandler({\n\t\t\t\t\t\trequest: koa.request,\n\t\t\t\t\t\tresponse: koa.response,\n\t\t\t\t\t\tcreateContext: ({ operation, queryHash }) => {\n\t\t\t\t\t\t\t// Clients open a primary read window only on this marker, so it must not be sent while the header is ignored.\n\t\t\t\t\t\t\tif (operation === 'mutation' && markMutations) {\n\t\t\t\t\t\t\t\tkoa.response.set('X-Contember-Mutation', '1')\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t;(koa.state as GraphQLKoaState).graphql = {\n\t\t\t\t\t\t\t\toperationName: operation,\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst baseConnection = operation === 'query' && !forcePrimary ? projectContainer.readConnection : projectContainer.connection\n\t\t\t\t\t\t\tconst maxConnectionsPerRequest = 'maxConnectionsPerRequest' in project.db\n\t\t\t\t\t\t\t\t? project.db.maxConnectionsPerRequest\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t\t// Optionally cap how many pool connections this single request may hold concurrently,\n\t\t\t\t\t\t\t// so one request cannot starve the shared pool under high concurrency. Defaults to unlimited.\n\t\t\t\t\t\t\tconst connection = maxConnectionsPerRequest !== undefined\n\t\t\t\t\t\t\t\t? baseConnection.withMaxConnections(maxConnectionsPerRequest)\n\t\t\t\t\t\t\t\t: baseConnection\n\t\t\t\t\t\t\tconst memoryBudget = this.memoryBudgetOptions\n\t\t\t\t\t\t\t\t? new RequestMemoryBudget(this.memoryBudgetOptions)\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t\tconst contentDatabase = testContentDatabase ?? connection.createClient(stage.schema, { module: 'content' })\n\t\t\t\t\t\t\tif (memoryBudget) {\n\t\t\t\t\t\t\t\tconst logMemory = () => {\n\t\t\t\t\t\t\t\t\tkoa.res.off('finish', logMemory)\n\t\t\t\t\t\t\t\t\tkoa.res.off('close', logMemory)\n\t\t\t\t\t\t\t\t\tconst memory = memoryBudget.snapshot()\n\t\t\t\t\t\t\t\t\tif (memory.warningThresholdExceeded) {\n\t\t\t\t\t\t\t\t\t\tlogger.warn('Content request memory usage', { operation, queryHash, ...memory })\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tkoa.res.once('finish', logMemory)\n\t\t\t\t\t\t\t\tkoa.res.once('close', logMemory)\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst identityVariables = createAclVariables(schema.acl, memberships)\n\t\t\t\t\t\t\tlet identityId = authResult.identityId\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tauthResult.assumedIdentityId\n\t\t\t\t\t\t\t\t&& memberships.some(it => schema.acl.roles[it.role].system?.assumeIdentity)\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tidentityId = authResult.assumedIdentityId\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst executionContainer = this.executionContainerFactory.create({\n\t\t\t\t\t\t\t\tdb: contentDatabase,\n\t\t\t\t\t\t\t\tmemoryBudget,\n\t\t\t\t\t\t\t\tidentityVariables,\n\t\t\t\t\t\t\t\tidentityId,\n\t\t\t\t\t\t\t\tschema,\n\t\t\t\t\t\t\t\tschemaMeta: { id: schemaWithMeta.meta.id },\n\t\t\t\t\t\t\t\tschemaDatabaseMetadata,\n\t\t\t\t\t\t\t\tpermissions,\n\t\t\t\t\t\t\t\tallPermissions,\n\t\t\t\t\t\t\t\tsystemSchema: projectContainer.systemDatabaseContextFactory.schemaName,\n\t\t\t\t\t\t\t\tstage,\n\t\t\t\t\t\t\t\tproject,\n\t\t\t\t\t\t\t\tuserInfo: {\n\t\t\t\t\t\t\t\t\tipAddress: clientIp,\n\t\t\t\t\t\t\t\t\tuserAgent: authResult.clientUserAgent ?? null,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t})\n\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tdb: contentDatabase,\n\t\t\t\t\t\t\t\tmemoryBudget,\n\t\t\t\t\t\t\t\tidentityVariables,\n\t\t\t\t\t\t\t\tidentityId,\n\t\t\t\t\t\t\t\texecutionContainer,\n\t\t\t\t\t\t\t\ttimer,\n\t\t\t\t\t\t\t\trequestDebug,\n\t\t\t\t\t\t\t\tproject,\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t},\n\t\t\t\t\t}))\n\t\t\t\tlogger.debug('Content query finished')\n\t\t\t})\n\n\t\t\tthis.requestLog?.record({\n\t\t\t\tprojectGroup: projectGroup.slug,\n\t\t\t\tproject: project.slug,\n\t\t\t\tstage: stage.slug,\n\t\t\t\troles: projectRoles,\n\t\t\t\tmethod: koa.request.method,\n\t\t\t\t// what the GraphQL handler executed: the POST body, or the query string of a GET\n\t\t\t\trequest: koa.request.method === 'POST' ? koa.request.body : koa.request.query,\n\t\t\t\tstatus: koa.response.status,\n\t\t\t})\n\n\t\t\tnotModifiedRes?.setResponseHeader(context.response)\n\t\t}\n\t}\n}\n"],"names":["HttpErrorResponse","isTruthyHeader","StageBySlugQuery","HttpResponse","logger","RequestMemoryBudget","createAclVariables"],"mappings":";;;;;;;;;AAeA,MAAM,cAAc;AACpB,MAAM,oBAAoB;AAC1B,MAAM,qBAAqB;AAEpB,MAAM,4BAA4B;AAAA,EACxC,YACkB,oBACA,2BACA,gBACA,wBACA,sBACA,wBACA,2BACA,qBACA,YAChB;AATgB,SAAA,qBAAA;AACA,SAAA,4BAAA;AACA,SAAA,iBAAA;AACA,SAAA,yBAAA;AACA,SAAA,uBAAA;AACA,SAAA,yBAAA;AACA,SAAA,4BAAA;AACA,SAAA,sBAAA;AACA,SAAA,aAAA;AAAA,EAElB;AAAA,EAEA,SAAyB;AACxB,UAAM,mCAAmB,QAAA;AACzB,WAAO,OAAM,YAAW;AACvB,YAAM,EAAE,QAAQ,OAAO,cAAc,YAAY,SAAS,KAAK,aAAa;AAC5E,UAAI,CAAC,YAAY;AAChB,eAAO,IAAIA,aAAAA,kBAAkB,KAAK,yBAAyB;AAAA,MAC5D;AACA,YAAM,EAAE,kBAAkB,QAAA,IAAY,MAAM,KAAK,uBAAuB,QAAQ,OAAO;AAEvF,YAAM,SAAS,QAAQ,OAAO,MAAM;AAAA,QACnC,SAAS,QAAQ;AAAA,MAAA,CACjB;AAED,YAAM,eAAe,KAAK,6BAA6BC,aAAAA,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AE,IAAI,eAAe,KAAK,GAAA;AAAA,cACtC;AAAA,cACA;AAAA,cACA;AAAA,cACA,cAAc,iBAAiB,6BAA6B;AAAA,cAC5D;AAAA,cACA;AAAA,cACA,UAAU;AAAA,gBACT,WAAW;AAAA,gBACX,WAAW,WAAW,mBAAmB;AAAA,cAAA;AAAA,YAC1C,CACA;AAED,mBAAO;AAAA,cACN,IAAI;AAAA,cACJ;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,YAAA;AAAA,UAEF;AAAA,QAAA,CACA,CAAC;AACHF,gBAAO,MAAM,wBAAwB;AAAA,MACtC,CAAC;AAED,WAAK,YAAY,OAAO;AAAA,QACvB,cAAc,aAAa;AAAA,QAC3B,SAAS,QAAQ;AAAA,QACjB,OAAO,MAAM;AAAA,QACb,OAAO;AAAA,QACP,QAAQ,IAAI,QAAQ;AAAA;AAAA,QAEpB,SAAS,IAAI,QAAQ,WAAW,SAAS,IAAI,QAAQ,OAAO,IAAI,QAAQ;AAAA,QACxE,QAAQ,IAAI,SAAS;AAAA,MAAA,CACrB;AAED,sBAAgB,kBAAkB,QAAQ,QAAQ;AAAA,IACnD;AAAA,EACD;AACD;;"}
@@ -9,7 +9,7 @@ const debugHeader = "x-contember-debug";
9
9
  const testSessionHeader = "x-contember-test-session";
10
10
  const forcePrimaryHeader = "x-contember-force-primary";
11
11
  class ContentApiControllerFactory {
12
- constructor(notModifiedChecker, executionContainerFactory, handlerFactory, projectContextResolver, graphQlSchemaFactory, testTransactionService, forcePrimaryHeaderEnabled, memoryBudgetOptions) {
12
+ constructor(notModifiedChecker, executionContainerFactory, handlerFactory, projectContextResolver, graphQlSchemaFactory, testTransactionService, forcePrimaryHeaderEnabled, memoryBudgetOptions, requestLog) {
13
13
  this.notModifiedChecker = notModifiedChecker;
14
14
  this.executionContainerFactory = executionContainerFactory;
15
15
  this.handlerFactory = handlerFactory;
@@ -18,6 +18,7 @@ class ContentApiControllerFactory {
18
18
  this.testTransactionService = testTransactionService;
19
19
  this.forcePrimaryHeaderEnabled = forcePrimaryHeaderEnabled;
20
20
  this.memoryBudgetOptions = memoryBudgetOptions;
21
+ this.requestLog = requestLog;
21
22
  }
22
23
  create() {
23
24
  const handlerCache = /* @__PURE__ */ new WeakMap();
@@ -171,6 +172,16 @@ class ContentApiControllerFactory {
171
172
  }));
172
173
  logger2.debug("Content query finished");
173
174
  });
175
+ this.requestLog?.record({
176
+ projectGroup: projectGroup.slug,
177
+ project: project.slug,
178
+ stage: stage.slug,
179
+ roles: projectRoles,
180
+ method: koa.request.method,
181
+ // what the GraphQL handler executed: the POST body, or the query string of a GET
182
+ request: koa.request.method === "POST" ? koa.request.body : koa.request.query,
183
+ status: koa.response.status
184
+ });
174
185
  notModifiedRes?.setResponseHeader(context.response);
175
186
  };
176
187
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ContentApiControllerFactory.js","sources":["../../../../../packages/engine-http/src/content/ContentApiControllerFactory.ts"],"sourcesContent":["import { createAclVariables, ExecutionContainerFactory } from '@contember/engine-content-api'\nimport { StageBySlugQuery } from '@contember/engine-system-api'\nimport { Client, RequestMemoryBudget, RequestMemoryBudgetOptions } from '@contember/database'\nimport { GraphQLSchema } from 'graphql'\nimport { HttpController } from '../application/index.js'\nimport { HttpErrorResponse, HttpResponse } from '../common/index.js'\nimport { GraphQLKoaState } from '../graphql/index.js'\nimport { ProjectContextResolver } from '../project-common/index.js'\nimport { ContentQueryHandler, ContentQueryHandlerFactory } from './ContentQueryHandlerFactory.js'\nimport { GraphQlSchemaFactory } from './GraphQlSchemaFactory.js'\nimport { NotModifiedChecker } from './NotModifiedChecker.js'\nimport { TestTransactionService } from '../testing/index.js'\nimport { isTruthyHeader } from '../utils/truthyHeader.js'\n\nconst debugHeader = 'x-contember-debug'\nconst testSessionHeader = 'x-contember-test-session'\nconst forcePrimaryHeader = 'x-contember-force-primary'\n\nexport class ContentApiControllerFactory {\n\tconstructor(\n\t\tprivate readonly notModifiedChecker: NotModifiedChecker,\n\t\tprivate readonly executionContainerFactory: ExecutionContainerFactory,\n\t\tprivate readonly handlerFactory: ContentQueryHandlerFactory,\n\t\tprivate readonly projectContextResolver: ProjectContextResolver,\n\t\tprivate readonly graphQlSchemaFactory: GraphQlSchemaFactory,\n\t\tprivate readonly testTransactionService: TestTransactionService,\n\t\tprivate readonly forcePrimaryHeaderEnabled: boolean,\n\t\tprivate readonly memoryBudgetOptions?: RequestMemoryBudgetOptions,\n\t) {\n\t}\n\n\tcreate(): HttpController {\n\t\tconst handlerCache = new WeakMap<GraphQLSchema, ContentQueryHandler>()\n\t\treturn async context => {\n\t\t\tconst { params, timer, projectGroup, authResult, request, koa, clientIp } = context\n\t\t\tif (!authResult) {\n\t\t\t\treturn new HttpErrorResponse(401, 'Authentication required')\n\t\t\t}\n\t\t\tconst { projectContainer, project } = await this.projectContextResolver.resolve(context)\n\n\t\t\tconst logger = context.logger.child({\n\t\t\t\tproject: project.slug,\n\t\t\t})\n\n\t\t\tconst forcePrimary = this.forcePrimaryHeaderEnabled && isTruthyHeader(request, forcePrimaryHeader)\n\t\t\t// Without a replica, forced reads change nothing, so clients need not open a window.\n\t\t\tconst markMutations = this.forcePrimaryHeaderEnabled && projectContainer.readConnection !== projectContainer.connection\n\t\t\tconst systemDatabase = forcePrimary ? projectContainer.systemDatabaseContext : projectContainer.systemReadDatabaseContext\n\t\t\tconst stage = await systemDatabase.queryHandler.fetch(new StageBySlugQuery(params.stageSlug))\n\t\t\tif (!stage) {\n\t\t\t\treturn new HttpErrorResponse(404, `Stage ${params.stageSlug} NOT found`)\n\t\t\t}\n\t\t\tconst notModifiedRes = await this.notModifiedChecker.checkNotModified({\n\t\t\t\trequest: context.request,\n\t\t\t\tbody: context.body,\n\t\t\t\ttimer: context.timer,\n\t\t\t\tsystemDatabase,\n\t\t\t\tstageId: stage.id,\n\t\t\t})\n\t\t\tif (notModifiedRes?.isModified === false) {\n\t\t\t\treturn new HttpResponse(304)\n\t\t\t}\n\n\t\t\tconst schemaWithMeta = await projectContainer.contentSchemaResolver.getSchema({ db: systemDatabase, stage: stage.slug, normalize: true })\n\t\t\tconst schema = schemaWithMeta.schema\n\t\t\tconst { effective: memberships, fetched: fetchedMemberships } = await timer(\n\t\t\t\t'MembershipFetch',\n\t\t\t\t() =>\n\t\t\t\t\tprojectGroup.projectMembershipResolver.resolveMemberships({\n\t\t\t\t\t\trequest: koa,\n\t\t\t\t\t\tacl: schema.acl,\n\t\t\t\t\t\tprojectSlug: project.slug,\n\t\t\t\t\t\tidentity: {\n\t\t\t\t\t\t\tidentityId: authResult.identityId,\n\t\t\t\t\t\t\tpersonId: authResult.personId ?? undefined,\n\t\t\t\t\t\t\troles: authResult.roles,\n\t\t\t\t\t\t},\n\t\t\t\t\t}),\n\t\t\t)\n\n\t\t\tlogger.debug('Memberships fetched', { memberships })\n\n\t\t\tconst debugHeaderValue = request.headers[debugHeader]\n\t\t\tif (Array.isArray(debugHeaderValue)) {\n\t\t\t\treturn new HttpErrorResponse(400, `Invalid format of ${debugHeader}`)\n\t\t\t}\n\t\t\tconst requestDebug = debugHeaderValue === '1' && fetchedMemberships.some(it => schema.acl.roles[it.role]?.debug)\n\t\t\tif (requestDebug) {\n\t\t\t\tcontext.requestDebugMode = true\n\t\t\t}\n\n\t\t\tconst projectRoles = memberships.map(it => it.role)\n\n\t\t\tconst { schema: graphQlSchema, permissions, allPermissions } = await timer(\n\t\t\t\t'GraphQLSchemaCreate',\n\t\t\t\t() =>\n\t\t\t\t\tthis.graphQlSchemaFactory.create(schema, {\n\t\t\t\t\t\tprojectRoles: projectRoles,\n\t\t\t\t\t}, project),\n\t\t\t)\n\n\t\t\tconst schemaDatabaseMetadata = await projectContainer.projectDatabaseMetadataResolver.resolveDatabaseMetadata(systemDatabase, schema, stage.schema)\n\n\t\t\t// Test-transaction mode: if the request carries a session header, bind its content DB\n\t\t\t// client to the session's pinned (rolled-back-later) transaction. Decided here, from the\n\t\t\t// request itself — no async-context routing.\n\t\t\tlet testContentDatabase: Client | undefined\n\t\t\tif (this.testTransactionService.isEnabled()) {\n\t\t\t\tconst headerValue = request.headers[testSessionHeader]\n\t\t\t\tif (headerValue !== undefined) {\n\t\t\t\t\tconst token = Array.isArray(headerValue) ? headerValue[0] : headerValue\n\t\t\t\t\ttestContentDatabase = await this.testTransactionService.resolveContentClient(\n\t\t\t\t\t\ttoken,\n\t\t\t\t\t\tproject.slug,\n\t\t\t\t\t\tprojectContainer.connection,\n\t\t\t\t\t\tstage.schema,\n\t\t\t\t\t\t{ module: 'content' },\n\t\t\t\t\t)\n\t\t\t\t\tif (!testContentDatabase) {\n\t\t\t\t\t\treturn new HttpErrorResponse(400, 'Unknown or expired test transaction session')\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst handler = await (async () => {\n\t\t\t\tconst existingHandler = handlerCache.get(graphQlSchema)\n\t\t\t\tif (existingHandler) {\n\t\t\t\t\treturn existingHandler\n\t\t\t\t}\n\t\t\t\tconst newHandler = await this.handlerFactory.create(graphQlSchema)\n\t\t\t\thandlerCache.set(graphQlSchema, newHandler)\n\t\t\t\treturn newHandler\n\t\t\t})()\n\n\t\t\tawait logger.scope(async logger => {\n\t\t\t\tlogger.debug('Content query processing started')\n\n\t\t\t\tawait timer('GraphQL', () =>\n\t\t\t\t\thandler({\n\t\t\t\t\t\trequest: koa.request,\n\t\t\t\t\t\tresponse: koa.response,\n\t\t\t\t\t\tcreateContext: ({ operation, queryHash }) => {\n\t\t\t\t\t\t\t// Clients open a primary read window only on this marker, so it must not be sent while the header is ignored.\n\t\t\t\t\t\t\tif (operation === 'mutation' && markMutations) {\n\t\t\t\t\t\t\t\tkoa.response.set('X-Contember-Mutation', '1')\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t;(koa.state as GraphQLKoaState).graphql = {\n\t\t\t\t\t\t\t\toperationName: operation,\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst baseConnection = operation === 'query' && !forcePrimary ? projectContainer.readConnection : projectContainer.connection\n\t\t\t\t\t\t\tconst maxConnectionsPerRequest = 'maxConnectionsPerRequest' in project.db\n\t\t\t\t\t\t\t\t? project.db.maxConnectionsPerRequest\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t\t// Optionally cap how many pool connections this single request may hold concurrently,\n\t\t\t\t\t\t\t// so one request cannot starve the shared pool under high concurrency. Defaults to unlimited.\n\t\t\t\t\t\t\tconst connection = maxConnectionsPerRequest !== undefined\n\t\t\t\t\t\t\t\t? baseConnection.withMaxConnections(maxConnectionsPerRequest)\n\t\t\t\t\t\t\t\t: baseConnection\n\t\t\t\t\t\t\tconst memoryBudget = this.memoryBudgetOptions\n\t\t\t\t\t\t\t\t? new RequestMemoryBudget(this.memoryBudgetOptions)\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t\tconst contentDatabase = testContentDatabase ?? connection.createClient(stage.schema, { module: 'content' })\n\t\t\t\t\t\t\tif (memoryBudget) {\n\t\t\t\t\t\t\t\tconst logMemory = () => {\n\t\t\t\t\t\t\t\t\tkoa.res.off('finish', logMemory)\n\t\t\t\t\t\t\t\t\tkoa.res.off('close', logMemory)\n\t\t\t\t\t\t\t\t\tconst memory = memoryBudget.snapshot()\n\t\t\t\t\t\t\t\t\tif (memory.warningThresholdExceeded) {\n\t\t\t\t\t\t\t\t\t\tlogger.warn('Content request memory usage', { operation, queryHash, ...memory })\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tkoa.res.once('finish', logMemory)\n\t\t\t\t\t\t\t\tkoa.res.once('close', logMemory)\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst identityVariables = createAclVariables(schema.acl, memberships)\n\t\t\t\t\t\t\tlet identityId = authResult.identityId\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tauthResult.assumedIdentityId\n\t\t\t\t\t\t\t\t&& memberships.some(it => schema.acl.roles[it.role].system?.assumeIdentity)\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tidentityId = authResult.assumedIdentityId\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst executionContainer = this.executionContainerFactory.create({\n\t\t\t\t\t\t\t\tdb: contentDatabase,\n\t\t\t\t\t\t\t\tmemoryBudget,\n\t\t\t\t\t\t\t\tidentityVariables,\n\t\t\t\t\t\t\t\tidentityId,\n\t\t\t\t\t\t\t\tschema,\n\t\t\t\t\t\t\t\tschemaMeta: { id: schemaWithMeta.meta.id },\n\t\t\t\t\t\t\t\tschemaDatabaseMetadata,\n\t\t\t\t\t\t\t\tpermissions,\n\t\t\t\t\t\t\t\tallPermissions,\n\t\t\t\t\t\t\t\tsystemSchema: projectContainer.systemDatabaseContextFactory.schemaName,\n\t\t\t\t\t\t\t\tstage,\n\t\t\t\t\t\t\t\tproject,\n\t\t\t\t\t\t\t\tuserInfo: {\n\t\t\t\t\t\t\t\t\tipAddress: clientIp,\n\t\t\t\t\t\t\t\t\tuserAgent: authResult.clientUserAgent ?? null,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t})\n\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tdb: contentDatabase,\n\t\t\t\t\t\t\t\tmemoryBudget,\n\t\t\t\t\t\t\t\tidentityVariables,\n\t\t\t\t\t\t\t\tidentityId,\n\t\t\t\t\t\t\t\texecutionContainer,\n\t\t\t\t\t\t\t\ttimer,\n\t\t\t\t\t\t\t\trequestDebug,\n\t\t\t\t\t\t\t\tproject,\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t},\n\t\t\t\t\t}))\n\t\t\t\tlogger.debug('Content query finished')\n\t\t\t})\n\n\t\t\tnotModifiedRes?.setResponseHeader(context.response)\n\t\t}\n\t}\n}\n"],"names":["logger"],"mappings":";;;;;;;AAcA,MAAM,cAAc;AACpB,MAAM,oBAAoB;AAC1B,MAAM,qBAAqB;AAEpB,MAAM,4BAA4B;AAAA,EACxC,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AS,aAAa,SAAA;AAC5B,oBAAI,OAAO,0BAA0B;AACpCA,0BAAO,KAAK,gCAAgC,EAAE,WAAW,WAAW,GAAG,QAAQ;AAAA,gBAChF;AAAA,cACD;AACA,kBAAI,IAAI,KAAK,UAAU,SAAS;AAChC,kBAAI,IAAI,KAAK,SAAS,SAAS;AAAA,YAChC;AAEA,kBAAM,oBAAoB,mBAAmB,OAAO,KAAK,WAAW;AACpE,gBAAI,aAAa,WAAW;AAC5B,gBACC,WAAW,qBACR,YAAY,KAAK,CAAA,OAAM,OAAO,IAAI,MAAM,GAAG,IAAI,EAAE,QAAQ,cAAc,GACzE;AACD,2BAAa,WAAW;AAAA,YACzB;AAEA,kBAAM,qBAAqB,KAAK,0BAA0B,OAAO;AAAA,cAChE,IAAI;AAAA,cACJ;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA,YAAY,EAAE,IAAI,eAAe,KAAK,GAAA;AAAA,cACtC;AAAA,cACA;AAAA,cACA;AAAA,cACA,cAAc,iBAAiB,6BAA6B;AAAA,cAC5D;AAAA,cACA;AAAA,cACA,UAAU;AAAA,gBACT,WAAW;AAAA,gBACX,WAAW,WAAW,mBAAmB;AAAA,cAAA;AAAA,YAC1C,CACA;AAED,mBAAO;AAAA,cACN,IAAI;AAAA,cACJ;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,YAAA;AAAA,UAEF;AAAA,QAAA,CACA,CAAC;AACHA,gBAAO,MAAM,wBAAwB;AAAA,MACtC,CAAC;AAED,sBAAgB,kBAAkB,QAAQ,QAAQ;AAAA,IACnD;AAAA,EACD;AACD;"}
1
+ {"version":3,"file":"ContentApiControllerFactory.js","sources":["../../../../../packages/engine-http/src/content/ContentApiControllerFactory.ts"],"sourcesContent":["import { createAclVariables, ExecutionContainerFactory } from '@contember/engine-content-api'\nimport { StageBySlugQuery } from '@contember/engine-system-api'\nimport { Client, RequestMemoryBudget, RequestMemoryBudgetOptions } from '@contember/database'\nimport { HttpController } from '../application/index.js'\nimport { HttpErrorResponse, HttpResponse } from '../common/index.js'\nimport { GraphQLKoaState } from '../graphql/index.js'\nimport { ProjectContextResolver } from '../project-common/index.js'\nimport { ContentQueryHandler, ContentQueryHandlerFactory } from './ContentQueryHandlerFactory.js'\nimport { GraphQlSchemaFactory } from './GraphQlSchemaFactory.js'\nimport { ContentGraphQLSchema } from './ContentGraphQLEngine.js'\nimport { NotModifiedChecker } from './NotModifiedChecker.js'\nimport { ContentRequestLog } from './ContentRequestLog.js'\nimport { TestTransactionService } from '../testing/index.js'\nimport { isTruthyHeader } from '../utils/truthyHeader.js'\n\nconst debugHeader = 'x-contember-debug'\nconst testSessionHeader = 'x-contember-test-session'\nconst forcePrimaryHeader = 'x-contember-force-primary'\n\nexport class ContentApiControllerFactory {\n\tconstructor(\n\t\tprivate readonly notModifiedChecker: NotModifiedChecker,\n\t\tprivate readonly executionContainerFactory: ExecutionContainerFactory,\n\t\tprivate readonly handlerFactory: ContentQueryHandlerFactory,\n\t\tprivate readonly projectContextResolver: ProjectContextResolver,\n\t\tprivate readonly graphQlSchemaFactory: GraphQlSchemaFactory,\n\t\tprivate readonly testTransactionService: TestTransactionService,\n\t\tprivate readonly forcePrimaryHeaderEnabled: boolean,\n\t\tprivate readonly memoryBudgetOptions?: RequestMemoryBudgetOptions,\n\t\tprivate readonly requestLog?: ContentRequestLog,\n\t) {\n\t}\n\n\tcreate(): HttpController {\n\t\tconst handlerCache = new WeakMap<ContentGraphQLSchema, ContentQueryHandler>()\n\t\treturn async context => {\n\t\t\tconst { params, timer, projectGroup, authResult, request, koa, clientIp } = context\n\t\t\tif (!authResult) {\n\t\t\t\treturn new HttpErrorResponse(401, 'Authentication required')\n\t\t\t}\n\t\t\tconst { projectContainer, project } = await this.projectContextResolver.resolve(context)\n\n\t\t\tconst logger = context.logger.child({\n\t\t\t\tproject: project.slug,\n\t\t\t})\n\n\t\t\tconst forcePrimary = this.forcePrimaryHeaderEnabled && isTruthyHeader(request, forcePrimaryHeader)\n\t\t\t// Without a replica, forced reads change nothing, so clients need not open a window.\n\t\t\tconst markMutations = this.forcePrimaryHeaderEnabled && projectContainer.readConnection !== projectContainer.connection\n\t\t\tconst systemDatabase = forcePrimary ? projectContainer.systemDatabaseContext : projectContainer.systemReadDatabaseContext\n\t\t\tconst stage = await systemDatabase.queryHandler.fetch(new StageBySlugQuery(params.stageSlug))\n\t\t\tif (!stage) {\n\t\t\t\treturn new HttpErrorResponse(404, `Stage ${params.stageSlug} NOT found`)\n\t\t\t}\n\t\t\tconst notModifiedRes = await this.notModifiedChecker.checkNotModified({\n\t\t\t\trequest: context.request,\n\t\t\t\tbody: context.body,\n\t\t\t\ttimer: context.timer,\n\t\t\t\tsystemDatabase,\n\t\t\t\tstageId: stage.id,\n\t\t\t})\n\t\t\tif (notModifiedRes?.isModified === false) {\n\t\t\t\treturn new HttpResponse(304)\n\t\t\t}\n\n\t\t\tconst schemaWithMeta = await projectContainer.contentSchemaResolver.getSchema({ db: systemDatabase, stage: stage.slug, normalize: true })\n\t\t\tconst schema = schemaWithMeta.schema\n\t\t\tconst { effective: memberships, fetched: fetchedMemberships } = await timer(\n\t\t\t\t'MembershipFetch',\n\t\t\t\t() =>\n\t\t\t\t\tprojectGroup.projectMembershipResolver.resolveMemberships({\n\t\t\t\t\t\trequest: koa,\n\t\t\t\t\t\tacl: schema.acl,\n\t\t\t\t\t\tprojectSlug: project.slug,\n\t\t\t\t\t\tidentity: {\n\t\t\t\t\t\t\tidentityId: authResult.identityId,\n\t\t\t\t\t\t\tpersonId: authResult.personId ?? undefined,\n\t\t\t\t\t\t\troles: authResult.roles,\n\t\t\t\t\t\t},\n\t\t\t\t\t}),\n\t\t\t)\n\n\t\t\tlogger.debug('Memberships fetched', { memberships })\n\n\t\t\tconst debugHeaderValue = request.headers[debugHeader]\n\t\t\tif (Array.isArray(debugHeaderValue)) {\n\t\t\t\treturn new HttpErrorResponse(400, `Invalid format of ${debugHeader}`)\n\t\t\t}\n\t\t\tconst requestDebug = debugHeaderValue === '1' && fetchedMemberships.some(it => schema.acl.roles[it.role]?.debug)\n\t\t\tif (requestDebug) {\n\t\t\t\tcontext.requestDebugMode = true\n\t\t\t}\n\n\t\t\tconst projectRoles = memberships.map(it => it.role)\n\n\t\t\tconst { schema: graphQlSchema, permissions, allPermissions } = await timer(\n\t\t\t\t'GraphQLSchemaCreate',\n\t\t\t\t() =>\n\t\t\t\t\tthis.graphQlSchemaFactory.create(schema, {\n\t\t\t\t\t\tprojectRoles: projectRoles,\n\t\t\t\t\t}, project),\n\t\t\t)\n\n\t\t\tconst schemaDatabaseMetadata = await projectContainer.projectDatabaseMetadataResolver.resolveDatabaseMetadata(systemDatabase, schema, stage.schema)\n\n\t\t\t// Test-transaction mode: if the request carries a session header, bind its content DB\n\t\t\t// client to the session's pinned (rolled-back-later) transaction. Decided here, from the\n\t\t\t// request itself — no async-context routing.\n\t\t\tlet testContentDatabase: Client | undefined\n\t\t\tif (this.testTransactionService.isEnabled()) {\n\t\t\t\tconst headerValue = request.headers[testSessionHeader]\n\t\t\t\tif (headerValue !== undefined) {\n\t\t\t\t\tconst token = Array.isArray(headerValue) ? headerValue[0] : headerValue\n\t\t\t\t\ttestContentDatabase = await this.testTransactionService.resolveContentClient(\n\t\t\t\t\t\ttoken,\n\t\t\t\t\t\tproject.slug,\n\t\t\t\t\t\tprojectContainer.connection,\n\t\t\t\t\t\tstage.schema,\n\t\t\t\t\t\t{ module: 'content' },\n\t\t\t\t\t)\n\t\t\t\t\tif (!testContentDatabase) {\n\t\t\t\t\t\treturn new HttpErrorResponse(400, 'Unknown or expired test transaction session')\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst handler = await (async () => {\n\t\t\t\tconst existingHandler = handlerCache.get(graphQlSchema)\n\t\t\t\tif (existingHandler) {\n\t\t\t\t\treturn existingHandler\n\t\t\t\t}\n\t\t\t\tconst newHandler = await this.handlerFactory.create(graphQlSchema)\n\t\t\t\thandlerCache.set(graphQlSchema, newHandler)\n\t\t\t\treturn newHandler\n\t\t\t})()\n\n\t\t\tawait logger.scope(async logger => {\n\t\t\t\tlogger.debug('Content query processing started')\n\n\t\t\t\tawait timer('GraphQL', () =>\n\t\t\t\t\thandler({\n\t\t\t\t\t\trequest: koa.request,\n\t\t\t\t\t\tresponse: koa.response,\n\t\t\t\t\t\tcreateContext: ({ operation, queryHash }) => {\n\t\t\t\t\t\t\t// Clients open a primary read window only on this marker, so it must not be sent while the header is ignored.\n\t\t\t\t\t\t\tif (operation === 'mutation' && markMutations) {\n\t\t\t\t\t\t\t\tkoa.response.set('X-Contember-Mutation', '1')\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t;(koa.state as GraphQLKoaState).graphql = {\n\t\t\t\t\t\t\t\toperationName: operation,\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst baseConnection = operation === 'query' && !forcePrimary ? projectContainer.readConnection : projectContainer.connection\n\t\t\t\t\t\t\tconst maxConnectionsPerRequest = 'maxConnectionsPerRequest' in project.db\n\t\t\t\t\t\t\t\t? project.db.maxConnectionsPerRequest\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t\t// Optionally cap how many pool connections this single request may hold concurrently,\n\t\t\t\t\t\t\t// so one request cannot starve the shared pool under high concurrency. Defaults to unlimited.\n\t\t\t\t\t\t\tconst connection = maxConnectionsPerRequest !== undefined\n\t\t\t\t\t\t\t\t? baseConnection.withMaxConnections(maxConnectionsPerRequest)\n\t\t\t\t\t\t\t\t: baseConnection\n\t\t\t\t\t\t\tconst memoryBudget = this.memoryBudgetOptions\n\t\t\t\t\t\t\t\t? new RequestMemoryBudget(this.memoryBudgetOptions)\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t\tconst contentDatabase = testContentDatabase ?? connection.createClient(stage.schema, { module: 'content' })\n\t\t\t\t\t\t\tif (memoryBudget) {\n\t\t\t\t\t\t\t\tconst logMemory = () => {\n\t\t\t\t\t\t\t\t\tkoa.res.off('finish', logMemory)\n\t\t\t\t\t\t\t\t\tkoa.res.off('close', logMemory)\n\t\t\t\t\t\t\t\t\tconst memory = memoryBudget.snapshot()\n\t\t\t\t\t\t\t\t\tif (memory.warningThresholdExceeded) {\n\t\t\t\t\t\t\t\t\t\tlogger.warn('Content request memory usage', { operation, queryHash, ...memory })\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tkoa.res.once('finish', logMemory)\n\t\t\t\t\t\t\t\tkoa.res.once('close', logMemory)\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst identityVariables = createAclVariables(schema.acl, memberships)\n\t\t\t\t\t\t\tlet identityId = authResult.identityId\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tauthResult.assumedIdentityId\n\t\t\t\t\t\t\t\t&& memberships.some(it => schema.acl.roles[it.role].system?.assumeIdentity)\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tidentityId = authResult.assumedIdentityId\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst executionContainer = this.executionContainerFactory.create({\n\t\t\t\t\t\t\t\tdb: contentDatabase,\n\t\t\t\t\t\t\t\tmemoryBudget,\n\t\t\t\t\t\t\t\tidentityVariables,\n\t\t\t\t\t\t\t\tidentityId,\n\t\t\t\t\t\t\t\tschema,\n\t\t\t\t\t\t\t\tschemaMeta: { id: schemaWithMeta.meta.id },\n\t\t\t\t\t\t\t\tschemaDatabaseMetadata,\n\t\t\t\t\t\t\t\tpermissions,\n\t\t\t\t\t\t\t\tallPermissions,\n\t\t\t\t\t\t\t\tsystemSchema: projectContainer.systemDatabaseContextFactory.schemaName,\n\t\t\t\t\t\t\t\tstage,\n\t\t\t\t\t\t\t\tproject,\n\t\t\t\t\t\t\t\tuserInfo: {\n\t\t\t\t\t\t\t\t\tipAddress: clientIp,\n\t\t\t\t\t\t\t\t\tuserAgent: authResult.clientUserAgent ?? null,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t})\n\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tdb: contentDatabase,\n\t\t\t\t\t\t\t\tmemoryBudget,\n\t\t\t\t\t\t\t\tidentityVariables,\n\t\t\t\t\t\t\t\tidentityId,\n\t\t\t\t\t\t\t\texecutionContainer,\n\t\t\t\t\t\t\t\ttimer,\n\t\t\t\t\t\t\t\trequestDebug,\n\t\t\t\t\t\t\t\tproject,\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t},\n\t\t\t\t\t}))\n\t\t\t\tlogger.debug('Content query finished')\n\t\t\t})\n\n\t\t\tthis.requestLog?.record({\n\t\t\t\tprojectGroup: projectGroup.slug,\n\t\t\t\tproject: project.slug,\n\t\t\t\tstage: stage.slug,\n\t\t\t\troles: projectRoles,\n\t\t\t\tmethod: koa.request.method,\n\t\t\t\t// what the GraphQL handler executed: the POST body, or the query string of a GET\n\t\t\t\trequest: koa.request.method === 'POST' ? koa.request.body : koa.request.query,\n\t\t\t\tstatus: koa.response.status,\n\t\t\t})\n\n\t\t\tnotModifiedRes?.setResponseHeader(context.response)\n\t\t}\n\t}\n}\n"],"names":["logger"],"mappings":";;;;;;;AAeA,MAAM,cAAc;AACpB,MAAM,oBAAoB;AAC1B,MAAM,qBAAqB;AAEpB,MAAM,4BAA4B;AAAA,EACxC,YACkB,oBACA,2BACA,gBACA,wBACA,sBACA,wBACA,2BACA,qBACA,YAChB;AATgB,SAAA,qBAAA;AACA,SAAA,4BAAA;AACA,SAAA,iBAAA;AACA,SAAA,yBAAA;AACA,SAAA,uBAAA;AACA,SAAA,yBAAA;AACA,SAAA,4BAAA;AACA,SAAA,sBAAA;AACA,SAAA,aAAA;AAAA,EAElB;AAAA,EAEA,SAAyB;AACxB,UAAM,mCAAmB,QAAA;AACzB,WAAO,OAAM,YAAW;AACvB,YAAM,EAAE,QAAQ,OAAO,cAAc,YAAY,SAAS,KAAK,aAAa;AAC5E,UAAI,C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G,IAAI,EAAE,QAAQ,cAAc,GACzE;AACD,2BAAa,WAAW;AAAA,YACzB;AAEA,kBAAM,qBAAqB,KAAK,0BAA0B,OAAO;AAAA,cAChE,IAAI;AAAA,cACJ;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA,YAAY,EAAE,IAAI,eAAe,KAAK,GAAA;AAAA,cACtC;AAAA,cACA;AAAA,cACA;AAAA,cACA,cAAc,iBAAiB,6BAA6B;AAAA,cAC5D;AAAA,cACA;AAAA,cACA,UAAU;AAAA,gBACT,WAAW;AAAA,gBACX,WAAW,WAAW,mBAAmB;AAAA,cAAA;AAAA,YAC1C,CACA;AAED,mBAAO;AAAA,cACN,IAAI;AAAA,cACJ;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,YAAA;AAAA,UAEF;AAAA,QAAA,CACA,CAAC;AACHA,gBAAO,MAAM,wBAAwB;AAAA,MACtC,CAAC;AAED,WAAK,YAAY,OAAO;AAAA,QACvB,cAAc,aAAa;AAAA,QAC3B,SAAS,QAAQ;AAAA,QACjB,OAAO,MAAM;AAAA,QACb,OAAO;AAAA,QACP,QAAQ,IAAI,QAAQ;AAAA;AAAA,QAEpB,SAAS,IAAI,QAAQ,WAAW,SAAS,IAAI,QAAQ,OAAO,IAAI,QAAQ;AAAA,QACxE,QAAQ,IAAI,SAAS;AAAA,MAAA,CACrB;AAED,sBAAgB,kBAAkB,QAAQ,QAAQ;AAAA,IACnD;AAAA,EACD;AACD;"}
@@ -11,18 +11,33 @@ class ContentApiSpecificCache {
11
11
  __publicField(this, "recency", /* @__PURE__ */ new Set());
12
12
  }
13
13
  fetch(objectKey, scalarKey, createValue) {
14
+ const entry = this.cache.get(objectKey)?.get(scalarKey);
15
+ if (entry) {
16
+ this.markUsed(entry);
17
+ return entry.value;
18
+ }
19
+ return this.set(objectKey, scalarKey, createValue());
20
+ }
21
+ get(objectKey, scalarKey) {
22
+ const entry = this.cache.get(objectKey)?.get(scalarKey);
23
+ if (!entry) {
24
+ return void 0;
25
+ }
26
+ this.markUsed(entry);
27
+ return entry.value;
28
+ }
29
+ set(objectKey, scalarKey, value) {
14
30
  let cacheMap = this.cache.get(objectKey);
15
31
  if (!cacheMap) {
16
32
  cacheMap = /* @__PURE__ */ new Map();
17
33
  this.cache.set(objectKey, cacheMap);
18
34
  }
19
- const cacheValue = cacheMap.get(scalarKey);
20
- if (cacheValue) {
21
- this.markUsed(cacheValue);
22
- return cacheValue.value;
35
+ const existing = cacheMap.get(scalarKey);
36
+ if (existing) {
37
+ this.remove(existing.recency);
23
38
  }
24
39
  const entry = {
25
- value: createValue(),
40
+ value,
26
41
  recency: { cacheMapRef: new WeakRef(cacheMap), scalarKey }
27
42
  };
28
43
  cacheMap.set(scalarKey, entry);
@@ -1 +1 @@
1
- {"version":3,"file":"ContentApiSpecificCache.cjs","sources":["../../../../../packages/engine-http/src/content/ContentApiSpecificCache.ts"],"sourcesContent":["import { clearTimeout } from 'node:timers'\n\ntype RecencyToken<V> = {\n\t// weak, so the recency order keeps neither the Map nor its ObjectKey alive\n\tcacheMapRef: WeakRef<Map<string, Entry<V>>>\n\tscalarKey: string\n}\n\ntype Entry<V> = {\n\ttimer?: ReturnType<typeof setTimeout>\n\tvalue: V\n\trecency: RecencyToken<V>\n}\n\nexport class ContentApiSpecificCache<ObjectKey extends object, Value> {\n\tprivate cache = new WeakMap<ObjectKey, Map<string, Entry<Value>>>()\n\n\t// Least recently used first, tracked only with maxEntries: nothing else removes a token whose Map was garbage-collected.\n\t// With maxEntries, such a token stays counted until it is evicted as the oldest.\n\tprivate recency = new Set<RecencyToken<Value>>()\n\n\tconstructor(\n\t\tprivate options: {\n\t\t\tttlSeconds?: number\n\t\t\tmaxEntries?: number\n\t\t},\n\t) {\n\t}\n\n\tpublic fetch(objectKey: ObjectKey, scalarKey: string, createValue: () => Value): Value {\n\t\tlet cacheMap = this.cache.get(objectKey)\n\n\t\tif (!cacheMap) {\n\t\t\tcacheMap = new Map()\n\t\t\tthis.cache.set(objectKey, cacheMap)\n\t\t}\n\n\t\tconst cacheValue = cacheMap.get(scalarKey)\n\t\tif (cacheValue) {\n\t\t\tthis.markUsed(cacheValue)\n\t\t\treturn cacheValue.value\n\t\t}\n\n\t\tconst entry: Entry<Value> = {\n\t\t\tvalue: createValue(),\n\t\t\trecency: { cacheMapRef: new WeakRef(cacheMap), scalarKey },\n\t\t}\n\t\tcacheMap.set(scalarKey, entry)\n\t\tthis.markUsed(entry)\n\t\tthis.evictOverLimit()\n\n\t\treturn entry.value\n\t}\n\n\tprivate markUsed(entry: Entry<Value>): void {\n\t\tclearTimeout(entry.timer)\n\t\tif (this.options.ttlSeconds) {\n\t\t\tconst token = entry.recency\n\t\t\tentry.timer = setTimeout(() => this.remove(token), this.options.ttlSeconds * 1000)\n\t\t}\n\t\tif (this.options.maxEntries) {\n\t\t\tthis.recency.delete(entry.recency)\n\t\t\tthis.recency.add(entry.recency)\n\t\t}\n\t}\n\n\tprivate evictOverLimit(): void {\n\t\tconst maxEntries = this.options.maxEntries\n\t\tif (!maxEntries) {\n\t\t\treturn\n\t\t}\n\t\tfor (const token of this.recency) {\n\t\t\tif (this.recency.size <= maxEntries) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthis.remove(token)\n\t\t}\n\t}\n\n\tprivate remove(token: RecencyToken<Value>): void {\n\t\tthis.recency.delete(token)\n\t\tconst cacheMap = token.cacheMapRef.deref()\n\t\tconst entry = cacheMap?.get(token.scalarKey)\n\t\tif (!cacheMap || entry?.recency !== token) {\n\t\t\treturn\n\t\t}\n\t\tclearTimeout(entry.timer)\n\t\tcacheMap.delete(token.scalarKey)\n\t}\n}\n"],"names":["clearTimeout"],"mappings":";;;;;;AAcO,MAAM,wBAAyD;AAAA,EAOrE,YACS,SAIP;AAJO,SAAA,UAAA;AAPT,kBAAA,MAAQ,6BAAY,QAAA,CAA8C;AAIlE,kBAAA,MAAQ,+BAAc,IAAA,CAAyB;AAAA,EA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AAY,OAAO,YAAY,OAAO;AAC1C;AAAA,IACD;AACAA,gBAAAA,aAAa,MAAM,KAAK;AACxB,aAAS,OAAO,MAAM,SAAS;AAAA,EAChC;AACD;;"}
1
+ {"version":3,"file":"ContentApiSpecificCache.cjs","sources":["../../../../../packages/engine-http/src/content/ContentApiSpecificCache.ts"],"sourcesContent":["import { clearTimeout } from 'node:timers'\n\ntype RecencyToken<V> = {\n\t// weak, so the recency order keeps neither the Map nor its ObjectKey alive\n\tcacheMapRef: WeakRef<Map<string, Entry<V>>>\n\tscalarKey: string\n}\n\ntype Entry<V> = {\n\ttimer?: ReturnType<typeof setTimeout>\n\tvalue: V\n\trecency: RecencyToken<V>\n}\n\nexport class ContentApiSpecificCache<ObjectKey extends object, Value> {\n\tprivate cache = new WeakMap<ObjectKey, Map<string, Entry<Value>>>()\n\n\t// Least recently used first, tracked only with maxEntries: nothing else removes a token whose Map was garbage-collected.\n\t// With maxEntries, such a token stays counted until it is evicted as the oldest.\n\tprivate recency = new Set<RecencyToken<Value>>()\n\n\tconstructor(\n\t\tprivate options: {\n\t\t\tttlSeconds?: number\n\t\t\tmaxEntries?: number\n\t\t},\n\t) {\n\t}\n\n\tpublic fetch(objectKey: ObjectKey, scalarKey: string, createValue: () => Value): Value {\n\t\tconst entry = this.cache.get(objectKey)?.get(scalarKey)\n\t\tif (entry) {\n\t\t\tthis.markUsed(entry)\n\t\t\treturn entry.value\n\t\t}\n\t\treturn this.set(objectKey, scalarKey, createValue())\n\t}\n\n\tpublic get(objectKey: ObjectKey, scalarKey: string): Value | undefined {\n\t\tconst entry = this.cache.get(objectKey)?.get(scalarKey)\n\t\tif (!entry) {\n\t\t\treturn undefined\n\t\t}\n\t\tthis.markUsed(entry)\n\t\treturn entry.value\n\t}\n\n\tpublic set(objectKey: ObjectKey, scalarKey: string, value: Value): Value {\n\t\tlet cacheMap = this.cache.get(objectKey)\n\t\tif (!cacheMap) {\n\t\t\tcacheMap = new Map()\n\t\t\tthis.cache.set(objectKey, cacheMap)\n\t\t}\n\t\tconst existing = cacheMap.get(scalarKey)\n\t\tif (existing) {\n\t\t\tthis.remove(existing.recency)\n\t\t}\n\n\t\tconst entry: Entry<Value> = {\n\t\t\tvalue,\n\t\t\trecency: { cacheMapRef: new WeakRef(cacheMap), scalarKey },\n\t\t}\n\t\tcacheMap.set(scalarKey, entry)\n\t\tthis.markUsed(entry)\n\t\tthis.evictOverLimit()\n\n\t\treturn entry.value\n\t}\n\n\tprivate markUsed(entry: Entry<Value>): void {\n\t\tclearTimeout(entry.timer)\n\t\tif (this.options.ttlSeconds) {\n\t\t\tconst token = entry.recency\n\t\t\tentry.timer = setTimeout(() => this.remove(token), this.options.ttlSeconds * 1000)\n\t\t}\n\t\tif (this.options.maxEntries) {\n\t\t\tthis.recency.delete(entry.recency)\n\t\t\tthis.recency.add(entry.recency)\n\t\t}\n\t}\n\n\tprivate evictOverLimit(): void {\n\t\tconst maxEntries = this.options.maxEntries\n\t\tif (!maxEntries) {\n\t\t\treturn\n\t\t}\n\t\tfor (const token of this.recency) {\n\t\t\tif (this.recency.size <= maxEntries) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthis.remove(token)\n\t\t}\n\t}\n\n\tprivate remove(token: RecencyToken<Value>): void {\n\t\tthis.recency.delete(token)\n\t\tconst cacheMap = token.cacheMapRef.deref()\n\t\tconst entry = cacheMap?.get(token.scalarKey)\n\t\tif (!cacheMap || entry?.recency !== token) {\n\t\t\treturn\n\t\t}\n\t\tclearTimeout(entry.timer)\n\t\tcacheMap.delete(token.scalarKey)\n\t}\n}\n"],"names":["clearTimeout"],"mappings":";;;;;;AAcO,MAAM,wBAAyD;AAAA,EAOrE,YACS,SAIP;AAJO,SAAA,UAAA;AAPT,kBAAA,MAAQ,6BAAY,QAAA,CAA8C;AAIlE,kBAAA,MAAQ,+BAAc,IAAA,CAAyB;AAAA,EAQ/C;AAAA,EAEO,MAAM,WAAsB,WAAmB,aAAiC;AACtF,UAAM,QAAQ,KAAK,MAA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AAC,YAAY;AAChB;AAAA,IACD;AACA,eAAW,SAAS,KAAK,SAAS;AACjC,UAAI,KAAK,QAAQ,QAAQ,YAAY;AACpC;AAAA,MACD;AACA,WAAK,OAAO,KAAK;AAAA,IAClB;AAAA,EACD;AAAA,EAEQ,OAAO,OAAkC;AAChD,SAAK,QAAQ,OAAO,KAAK;AACzB,UAAM,WAAW,MAAM,YAAY,MAAA;AACnC,UAAM,QAAQ,UAAU,IAAI,MAAM,SAAS;AAC3C,QAAI,CAAC,YAAY,OAAO,YAAY,OAAO;AAC1C;AAAA,IACD;AACAA,gBAAAA,aAAa,MAAM,KAAK;AACxB,aAAS,OAAO,MAAM,SAAS;AAAA,EAChC;AACD;;"}
@@ -9,18 +9,33 @@ class ContentApiSpecificCache {
9
9
  __publicField(this, "recency", /* @__PURE__ */ new Set());
10
10
  }
11
11
  fetch(objectKey, scalarKey, createValue) {
12
+ const entry = this.cache.get(objectKey)?.get(scalarKey);
13
+ if (entry) {
14
+ this.markUsed(entry);
15
+ return entry.value;
16
+ }
17
+ return this.set(objectKey, scalarKey, createValue());
18
+ }
19
+ get(objectKey, scalarKey) {
20
+ const entry = this.cache.get(objectKey)?.get(scalarKey);
21
+ if (!entry) {
22
+ return void 0;
23
+ }
24
+ this.markUsed(entry);
25
+ return entry.value;
26
+ }
27
+ set(objectKey, scalarKey, value) {
12
28
  let cacheMap = this.cache.get(objectKey);
13
29
  if (!cacheMap) {
14
30
  cacheMap = /* @__PURE__ */ new Map();
15
31
  this.cache.set(objectKey, cacheMap);
16
32
  }
17
- const cacheValue = cacheMap.get(scalarKey);
18
- if (cacheValue) {
19
- this.markUsed(cacheValue);
20
- return cacheValue.value;
33
+ const existing = cacheMap.get(scalarKey);
34
+ if (existing) {
35
+ this.remove(existing.recency);
21
36
  }
22
37
  const entry = {
23
- value: createValue(),
38
+ value,
24
39
  recency: { cacheMapRef: new WeakRef(cacheMap), scalarKey }
25
40
  };
26
41
  cacheMap.set(scalarKey, entry);
@@ -1 +1 @@
1
- {"version":3,"file":"ContentApiSpecificCache.js","sources":["../../../../../packages/engine-http/src/content/ContentApiSpecificCache.ts"],"sourcesContent":["import { clearTimeout } from 'node:timers'\n\ntype RecencyToken<V> = {\n\t// weak, so the recency order keeps neither the Map nor its ObjectKey alive\n\tcacheMapRef: WeakRef<Map<string, Entry<V>>>\n\tscalarKey: string\n}\n\ntype Entry<V> = {\n\ttimer?: ReturnType<typeof setTimeout>\n\tvalue: V\n\trecency: RecencyToken<V>\n}\n\nexport class ContentApiSpecificCache<ObjectKey extends object, Value> {\n\tprivate cache = new WeakMap<ObjectKey, Map<string, Entry<Value>>>()\n\n\t// Least recently used first, tracked only with maxEntries: nothing else removes a token whose Map was garbage-collected.\n\t// With maxEntries, such a token stays counted until it is evicted as the oldest.\n\tprivate recency = new Set<RecencyToken<Value>>()\n\n\tconstructor(\n\t\tprivate options: {\n\t\t\tttlSeconds?: number\n\t\t\tmaxEntries?: number\n\t\t},\n\t) {\n\t}\n\n\tpublic fetch(objectKey: ObjectKey, scalarKey: string, createValue: () => Value): Value {\n\t\tlet cacheMap = this.cache.get(objectKey)\n\n\t\tif (!cacheMap) {\n\t\t\tcacheMap = new Map()\n\t\t\tthis.cache.set(objectKey, cacheMap)\n\t\t}\n\n\t\tconst cacheValue = cacheMap.get(scalarKey)\n\t\tif (cacheValue) {\n\t\t\tthis.markUsed(cacheValue)\n\t\t\treturn cacheValue.value\n\t\t}\n\n\t\tconst entry: Entry<Value> = {\n\t\t\tvalue: createValue(),\n\t\t\trecency: { cacheMapRef: new WeakRef(cacheMap), scalarKey },\n\t\t}\n\t\tcacheMap.set(scalarKey, entry)\n\t\tthis.markUsed(entry)\n\t\tthis.evictOverLimit()\n\n\t\treturn entry.value\n\t}\n\n\tprivate markUsed(entry: Entry<Value>): void {\n\t\tclearTimeout(entry.timer)\n\t\tif (this.options.ttlSeconds) {\n\t\t\tconst token = entry.recency\n\t\t\tentry.timer = setTimeout(() => this.remove(token), this.options.ttlSeconds * 1000)\n\t\t}\n\t\tif (this.options.maxEntries) {\n\t\t\tthis.recency.delete(entry.recency)\n\t\t\tthis.recency.add(entry.recency)\n\t\t}\n\t}\n\n\tprivate evictOverLimit(): void {\n\t\tconst maxEntries = this.options.maxEntries\n\t\tif (!maxEntries) {\n\t\t\treturn\n\t\t}\n\t\tfor (const token of this.recency) {\n\t\t\tif (this.recency.size <= maxEntries) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthis.remove(token)\n\t\t}\n\t}\n\n\tprivate remove(token: RecencyToken<Value>): void {\n\t\tthis.recency.delete(token)\n\t\tconst cacheMap = token.cacheMapRef.deref()\n\t\tconst entry = cacheMap?.get(token.scalarKey)\n\t\tif (!cacheMap || entry?.recency !== token) {\n\t\t\treturn\n\t\t}\n\t\tclearTimeout(entry.timer)\n\t\tcacheMap.delete(token.scalarKey)\n\t}\n}\n"],"names":[],"mappings":";;;;AAcO,MAAM,wBAAyD;AAAA,EAOrE,YACS,SAIP;AAJO,SAAA,UAAA;AAPT,kBAAA,MAAQ,6BAAY,QAAA,CAA8C;AAIlE,kBAAA,MAAQ,+BAAc,IAAA,CAAyB;AAAA,EAQ/C;AAAA,EAEO,MAAM,WAAsB,WAAmB,aAAiC;AACtF,QAAI,WAAW,KAAK,MAAM,IAAI,SAAS;AAEvC,QAAI,CAAC,UAAU;AACd,qCAAe,IAAA;AACf,WAAK,MAAM,IAAI,WAAW,QAAQ;AAAA,IACnC;AAEA,UAAM,aAAa,SAAS,IAAI,SAAS;AACzC,QAAI,YAAY;AACf,WAAK,SAAS,UAAU;AACxB,aAAO,WAAW;AAAA,IACnB;AAEA,UAAM,QAAsB;AAAA,MAC3B,OAAO,YAAA;AAAA,MACP,SAAS,EAAE,aAAa,IAAI,QAAQ,QAAQ,GAAG,UAAA;AAAA,IAAU;AAE1D,aAAS,IAAI,WAAW,KAAK;AAC7B,SAAK,SAAS,KAAK;AACnB,SAAK,eAAA;AAEL,WAAO,MAAM;AAAA,EACd;AAAA,EAEQ,SAAS,OAA2B;AAC3C,iBAAa,MAAM,KAAK;AACxB,QAAI,KAAK,QAAQ,YAAY;AAC5B,YAAM,QAAQ,MAAM;AACpB,YAAM,QAAQ,WAAW,MAAM,KAAK,OAAO,KAAK,GAAG,KAAK,QAAQ,aAAa,GAAI;AAAA,IAClF;AACA,QAAI,KAAK,QAAQ,YAAY;AAC5B,WAAK,QAAQ,OAAO,MAAM,OAAO;AACjC,WAAK,QAAQ,IAAI,MAAM,OAAO;AAAA,IAC/B;AAAA,EACD;AAAA,EAEQ,iBAAuB;AAC9B,UAAM,aAAa,KAAK,QAAQ;AAChC,QAAI,CAAC,YAAY;AAChB;AAAA,IACD;AACA,eAAW,SAAS,KAAK,SAAS;AACjC,UAAI,KAAK,QAAQ,QAAQ,YAAY;AACpC;AAAA,MACD;AACA,WAAK,OAAO,KAAK;AAAA,IAClB;AAAA,EACD;AAAA,EAEQ,OAAO,OAAkC;AAChD,SAAK,QAAQ,OAAO,KAAK;AACzB,UAAM,WAAW,MAAM,YAAY,MAAA;AACnC,UAAM,QAAQ,UAAU,IAAI,MAAM,SAAS;AAC3C,QAAI,CAAC,YAAY,OAAO,YAAY,OAAO;AAC1C;AAAA,IACD;AACA,iBAAa,MAAM,KAAK;AACxB,aAAS,OAAO,MAAM,SAAS;AAAA,EAChC;AACD;"}
1
+ {"version":3,"file":"ContentApiSpecificCache.js","sources":["../../../../../packages/engine-http/src/content/ContentApiSpecificCache.ts"],"sourcesContent":["import { clearTimeout } from 'node:timers'\n\ntype RecencyToken<V> = {\n\t// weak, so the recency order keeps neither the Map nor its ObjectKey alive\n\tcacheMapRef: WeakRef<Map<string, Entry<V>>>\n\tscalarKey: string\n}\n\ntype Entry<V> = {\n\ttimer?: ReturnType<typeof setTimeout>\n\tvalue: V\n\trecency: RecencyToken<V>\n}\n\nexport class ContentApiSpecificCache<ObjectKey extends object, Value> {\n\tprivate cache = new WeakMap<ObjectKey, Map<string, Entry<Value>>>()\n\n\t// Least recently used first, tracked only with maxEntries: nothing else removes a token whose Map was garbage-collected.\n\t// With maxEntries, such a token stays counted until it is evicted as the oldest.\n\tprivate recency = new Set<RecencyToken<Value>>()\n\n\tconstructor(\n\t\tprivate options: {\n\t\t\tttlSeconds?: number\n\t\t\tmaxEntries?: number\n\t\t},\n\t) {\n\t}\n\n\tpublic fetch(objectKey: ObjectKey, scalarKey: string, createValue: () => Value): Value {\n\t\tconst entry = this.cache.get(objectKey)?.get(scalarKey)\n\t\tif (entry) {\n\t\t\tthis.markUsed(entry)\n\t\t\treturn entry.value\n\t\t}\n\t\treturn this.set(objectKey, scalarKey, createValue())\n\t}\n\n\tpublic get(objectKey: ObjectKey, scalarKey: string): Value | undefined {\n\t\tconst entry = this.cache.get(objectKey)?.get(scalarKey)\n\t\tif (!entry) {\n\t\t\treturn undefined\n\t\t}\n\t\tthis.markUsed(entry)\n\t\treturn entry.value\n\t}\n\n\tpublic set(objectKey: ObjectKey, scalarKey: string, value: Value): Value {\n\t\tlet cacheMap = this.cache.get(objectKey)\n\t\tif (!cacheMap) {\n\t\t\tcacheMap = new Map()\n\t\t\tthis.cache.set(objectKey, cacheMap)\n\t\t}\n\t\tconst existing = cacheMap.get(scalarKey)\n\t\tif (existing) {\n\t\t\tthis.remove(existing.recency)\n\t\t}\n\n\t\tconst entry: Entry<Value> = {\n\t\t\tvalue,\n\t\t\trecency: { cacheMapRef: new WeakRef(cacheMap), scalarKey },\n\t\t}\n\t\tcacheMap.set(scalarKey, entry)\n\t\tthis.markUsed(entry)\n\t\tthis.evictOverLimit()\n\n\t\treturn entry.value\n\t}\n\n\tprivate markUsed(entry: Entry<Value>): void {\n\t\tclearTimeout(entry.timer)\n\t\tif (this.options.ttlSeconds) {\n\t\t\tconst token = entry.recency\n\t\t\tentry.timer = setTimeout(() => this.remove(token), this.options.ttlSeconds * 1000)\n\t\t}\n\t\tif (this.options.maxEntries) {\n\t\t\tthis.recency.delete(entry.recency)\n\t\t\tthis.recency.add(entry.recency)\n\t\t}\n\t}\n\n\tprivate evictOverLimit(): void {\n\t\tconst maxEntries = this.options.maxEntries\n\t\tif (!maxEntries) {\n\t\t\treturn\n\t\t}\n\t\tfor (const token of this.recency) {\n\t\t\tif (this.recency.size <= maxEntries) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthis.remove(token)\n\t\t}\n\t}\n\n\tprivate remove(token: RecencyToken<Value>): void {\n\t\tthis.recency.delete(token)\n\t\tconst cacheMap = token.cacheMapRef.deref()\n\t\tconst entry = cacheMap?.get(token.scalarKey)\n\t\tif (!cacheMap || entry?.recency !== token) {\n\t\t\treturn\n\t\t}\n\t\tclearTimeout(entry.timer)\n\t\tcacheMap.delete(token.scalarKey)\n\t}\n}\n"],"names":[],"mappings":";;;;AAcO,MAAM,wBAAyD;AAAA,EAOrE,YACS,SAIP;AAJO,SAAA,UAAA;AAPT,kBAAA,MAAQ,6BAAY,QAAA,CAA8C;AAIlE,kBAAA,MAAQ,+BAAc,IAAA,CAAyB;AAAA,EAQ/C;AAAA,EAEO,MAAM,WAAsB,WAAmB,aAAiC;AACtF,UAAM,QAAQ,KAAK,MAAM,IAAI,SAAS,GAAG,IAAI,SAAS;AACtD,QAAI,OAAO;AACV,WAAK,SAAS,KAAK;AACnB,aAAO,MAAM;AAAA,IACd;AACA,WAAO,KAAK,IAAI,WAAW,WAAW,aAAa;AAAA,EACpD;AAAA,EAEO,IAAI,WAAsB,WAAsC;AACtE,UAAM,QAAQ,KAAK,MAAM,IAAI,SAAS,GAAG,IAAI,SAAS;AACtD,QAAI,CAAC,OAAO;AACX,aAAO;AAAA,IACR;AACA,SAAK,SAAS,KAAK;AACnB,WAAO,MAAM;AAAA,EACd;AAAA,EAEO,IAAI,WAAsB,WAAmB,OAAqB;AACxE,QAAI,WAAW,KAAK,MAAM,IAAI,SAAS;AACvC,QAAI,CAAC,UAAU;AACd,qCAAe,IAAA;AACf,WAAK,MAAM,IAAI,WAAW,QAAQ;AAAA,IACnC;AACA,UAAM,WAAW,SAAS,IAAI,SAAS;AACvC,QAAI,UAAU;AACb,WAAK,OAAO,SAAS,OAAO;AAAA,IAC7B;AAEA,UAAM,QAAsB;AAAA,MAC3B;AAAA,MACA,SAAS,EAAE,aAAa,IAAI,QAAQ,QAAQ,GAAG,UAAA;AAAA,IAAU;AAE1D,aAAS,IAAI,WAAW,KAAK;AAC7B,SAAK,SAAS,KAAK;AACnB,SAAK,eAAA;AAEL,WAAO,MAAM;AAAA,EACd;AAAA,EAEQ,SAAS,OAA2B;AAC3C,iBAAa,MAAM,KAAK;AACxB,QAAI,KAAK,QAAQ,YAAY;AAC5B,YAAM,QAAQ,MAAM;AACpB,YAAM,QAAQ,WAAW,MAAM,KAAK,OAAO,KAAK,GAAG,KAAK,QAAQ,aAAa,GAAI;AAAA,IAClF;AACA,QAAI,KAAK,QAAQ,YAAY;AAC5B,WAAK,QAAQ,OAAO,MAAM,OAAO;AACjC,WAAK,QAAQ,IAAI,MAAM,OAAO;AAAA,IAC/B;AAAA,EACD;AAAA,EAEQ,iBAAuB;AAC9B,UAAM,aAAa,KAAK,QAAQ;AAChC,QAAI,CAAC,YAAY;AAChB;AAAA,IACD;AACA,eAAW,SAAS,KAAK,SAAS;AACjC,UAAI,KAAK,QAAQ,QAAQ,YAAY;AACpC;AAAA,MACD;AACA,WAAK,OAAO,KAAK;AAAA,IAClB;AAAA,EACD;AAAA,EAEQ,OAAO,OAAkC;AAChD,SAAK,QAAQ,OAAO,KAAK;AACzB,UAAM,WAAW,MAAM,YAAY,MAAA;AACnC,UAAM,QAAQ,UAAU,IAAI,MAAM,SAAS;AAC3C,QAAI,CAAC,YAAY,OAAO,YAAY,OAAO;AAC1C;AAAA,IACD;AACA,iBAAa,MAAM,KAAK;AACxB,aAAS,OAAO,MAAM,SAAS;AAAA,EAChC;AACD;"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ var __defProp = Object.defineProperty;
4
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
+ var __publicField = (obj, key, value) => __defNormalProp(obj, key + "", value);
6
+ class ContentGraphQLEngineResolver {
7
+ constructor(defaultEngine = "graphql-js", graphqlJsonProjects = []) {
8
+ this.defaultEngine = defaultEngine;
9
+ __publicField(this, "graphqlJsonProjects");
10
+ this.graphqlJsonProjects = new Set(graphqlJsonProjects);
11
+ }
12
+ resolve(projectSlug) {
13
+ return this.graphqlJsonProjects.has(projectSlug) ? "graphql-json" : this.defaultEngine;
14
+ }
15
+ }
16
+ exports.ContentGraphQLEngineResolver = ContentGraphQLEngineResolver;
17
+ //# sourceMappingURL=ContentGraphQLEngine.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ContentGraphQLEngine.cjs","sources":["../../../../../packages/engine-http/src/content/ContentGraphQLEngine.ts"],"sourcesContent":["import { JsonContentSchema } from '@contember/engine-content-api'\nimport { GraphQLSchema } from 'graphql'\n\nexport type ContentGraphQLEngine = 'graphql-js' | 'graphql-json'\n\nexport type ContentGraphQLSchema =\n\t| { engine: 'graphql-js'; schema: GraphQLSchema }\n\t| { engine: 'graphql-json'; schema: JsonContentSchema }\n\n/** The engine that serves a project's content API: the listed projects run on graphql-json, the others on the default. */\nexport class ContentGraphQLEngineResolver {\n\tprivate readonly graphqlJsonProjects: ReadonlySet<string>\n\n\tconstructor(\n\t\tprivate readonly defaultEngine: ContentGraphQLEngine = 'graphql-js',\n\t\tgraphqlJsonProjects: readonly string[] = [],\n\t) {\n\t\tthis.graphqlJsonProjects = new Set(graphqlJsonProjects)\n\t}\n\n\tpublic resolve(projectSlug: string): ContentGraphQLEngine {\n\t\treturn this.graphqlJsonProjects.has(projectSlug) ? 'graphql-json' : this.defaultEngine\n\t}\n}\n"],"names":[],"mappings":";;;;;AAUO,MAAM,6BAA6B;AAAA,EAGzC,YACkB,gBAAsC,cACvD,sBAAyC,CAAA,GACxC;AAFgB,SAAA,gBAAA;AAHlB,kBAAA,MAAiB,qBAAA;AAMhB,SAAK,sBAAsB,IAAI,IAAI,mBAAmB;AAAA,EACvD;AAAA,EAEO,QAAQ,aAA2C;AACzD,WAAO,KAAK,oBAAoB,IAAI,WAAW,IAAI,iBAAiB,KAAK;AAAA,EAC1E;AACD;;"}
@@ -0,0 +1,17 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, key + "", value);
4
+ class ContentGraphQLEngineResolver {
5
+ constructor(defaultEngine = "graphql-js", graphqlJsonProjects = []) {
6
+ this.defaultEngine = defaultEngine;
7
+ __publicField(this, "graphqlJsonProjects");
8
+ this.graphqlJsonProjects = new Set(graphqlJsonProjects);
9
+ }
10
+ resolve(projectSlug) {
11
+ return this.graphqlJsonProjects.has(projectSlug) ? "graphql-json" : this.defaultEngine;
12
+ }
13
+ }
14
+ export {
15
+ ContentGraphQLEngineResolver
16
+ };
17
+ //# sourceMappingURL=ContentGraphQLEngine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ContentGraphQLEngine.js","sources":["../../../../../packages/engine-http/src/content/ContentGraphQLEngine.ts"],"sourcesContent":["import { JsonContentSchema } from '@contember/engine-content-api'\nimport { GraphQLSchema } from 'graphql'\n\nexport type ContentGraphQLEngine = 'graphql-js' | 'graphql-json'\n\nexport type ContentGraphQLSchema =\n\t| { engine: 'graphql-js'; schema: GraphQLSchema }\n\t| { engine: 'graphql-json'; schema: JsonContentSchema }\n\n/** The engine that serves a project's content API: the listed projects run on graphql-json, the others on the default. */\nexport class ContentGraphQLEngineResolver {\n\tprivate readonly graphqlJsonProjects: ReadonlySet<string>\n\n\tconstructor(\n\t\tprivate readonly defaultEngine: ContentGraphQLEngine = 'graphql-js',\n\t\tgraphqlJsonProjects: readonly string[] = [],\n\t) {\n\t\tthis.graphqlJsonProjects = new Set(graphqlJsonProjects)\n\t}\n\n\tpublic resolve(projectSlug: string): ContentGraphQLEngine {\n\t\treturn this.graphqlJsonProjects.has(projectSlug) ? 'graphql-json' : this.defaultEngine\n\t}\n}\n"],"names":[],"mappings":";;;AAUO,MAAM,6BAA6B;AAAA,EAGzC,YACkB,gBAAsC,cACvD,sBAAyC,CAAA,GACxC;AAFgB,SAAA,gBAAA;AAHlB,kBAAA,MAAiB,qBAAA;AAMhB,SAAK,sBAAsB,IAAI,IAAI,mBAAmB;AAAA,EACvD;AAAA,EAEO,QAAQ,aAA2C;AACzD,WAAO,KAAK,oBAAoB,IAAI,WAAW,IAAI,iBAAiB,KAAK;AAAA,EAC1E;AACD;"}
@@ -12,11 +12,14 @@ class ContentQueryHandlerFactory {
12
12
  listeners.push(dbQueriesListener.createDbQueriesListener((context) => context.db, this.debug));
13
13
  listeners.push(triggeredActionsListener.createTriggeredActionsListener());
14
14
  return execution.createGraphQLQueryHandler({
15
- schema: graphQlSchema,
15
+ executor: this.createExecutor(graphQlSchema),
16
16
  listeners,
17
17
  getMemoryBudget: (context) => context.memoryBudget
18
18
  });
19
19
  }
20
+ createExecutor(graphQlSchema) {
21
+ return graphQlSchema.engine === "graphql-js" ? execution.createGraphQLJsExecutor(graphQlSchema.schema) : execution.createGraphQLJsonExecutor(graphQlSchema.schema);
22
+ }
20
23
  }
21
24
  exports.ContentQueryHandlerFactory = ContentQueryHandlerFactory;
22
25
  //# sourceMappingURL=ContentQueryHandlerFactory.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"ContentQueryHandlerFactory.cjs","sources":["../../../../../packages/engine-http/src/content/ContentQueryHandlerFactory.ts"],"sourcesContent":["import { GraphQLSchema } from 'graphql'\nimport { createDbQueriesListener, createGraphQLQueryHandler, GraphQLListener, GraphQLQueryHandler } from '../graphql/index.js'\nimport { ContentGraphqlContext } from './ContentGraphqlContext.js'\nimport { createTriggeredActionsListener } from './triggeredActionsListener.js'\n\nexport type ContentQueryHandler = GraphQLQueryHandler<ContentGraphqlContext>\n\nexport class ContentQueryHandlerFactory {\n\tconstructor(private readonly debug: boolean) {}\n\n\tpublic create(graphQlSchema: GraphQLSchema): ContentQueryHandler {\n\t\tconst listeners: GraphQLListener<ContentGraphqlContext>[] = []\n\t\tlisteners.push(createDbQueriesListener(context => context.db, this.debug))\n\t\tlisteners.push(createTriggeredActionsListener())\n\n\t\treturn createGraphQLQueryHandler<ContentGraphqlContext>({\n\t\t\tschema: graphQlSchema,\n\t\t\tlisteners,\n\t\t\tgetMemoryBudget: context => context.memoryBudget,\n\t\t})\n\t}\n}\n"],"names":["createDbQueriesListener","createTriggeredActionsListener","createGraphQLQueryHandler"],"mappings":";;;;;AAOO,MAAM,2BAA2B;AAAA,EACvC,YAA6B,OAAgB;AAAhB,SAAA,QAAA;AAAA,EAAiB;AAAA,EAEvC,OAAO,eAAmD;AAChE,UAAM,YAAsD,CAAA;AAC5D,cAAU,KAAKA,kBAAAA,wBAAwB,CAAA,YAAW,QAAQ,IAAI,KAAK,KAAK,CAAC;AACzE,cAAU,KAAKC,yBAAAA,gCAAgC;AAE/C,WAAOC,oCAAiD;AAAA,MACvD,QAAQ;AAAA,MACR;AAAA,MACA,iBAAiB,aAAW,QAAQ;AAAA,IAAA,CACpC;AAAA,EACF;AACD;;"}
1
+ {"version":3,"file":"ContentQueryHandlerFactory.cjs","sources":["../../../../../packages/engine-http/src/content/ContentQueryHandlerFactory.ts"],"sourcesContent":["import {\n\tcreateDbQueriesListener,\n\tcreateGraphQLJsExecutor,\n\tcreateGraphQLJsonExecutor,\n\tcreateGraphQLQueryHandler,\n\tGraphQLExecutor,\n\tGraphQLListener,\n\tGraphQLQueryHandler,\n} from '../graphql/index.js'\nimport { ContentGraphQLSchema } from './ContentGraphQLEngine.js'\nimport { ContentGraphqlContext } from './ContentGraphqlContext.js'\nimport { createTriggeredActionsListener } from './triggeredActionsListener.js'\n\nexport type ContentQueryHandler = GraphQLQueryHandler<ContentGraphqlContext>\n\nexport class ContentQueryHandlerFactory {\n\tconstructor(private readonly debug: boolean) {}\n\n\tpublic create(graphQlSchema: ContentGraphQLSchema): ContentQueryHandler {\n\t\tconst listeners: GraphQLListener<ContentGraphqlContext>[] = []\n\t\tlisteners.push(createDbQueriesListener(context => context.db, this.debug))\n\t\tlisteners.push(createTriggeredActionsListener())\n\n\t\treturn createGraphQLQueryHandler<ContentGraphqlContext>({\n\t\t\texecutor: this.createExecutor(graphQlSchema),\n\t\t\tlisteners,\n\t\t\tgetMemoryBudget: context => context.memoryBudget,\n\t\t})\n\t}\n\n\tprivate createExecutor(graphQlSchema: ContentGraphQLSchema): GraphQLExecutor<ContentGraphqlContext> {\n\t\treturn graphQlSchema.engine === 'graphql-js' ? createGraphQLJsExecutor(graphQlSchema.schema) : createGraphQLJsonExecutor(graphQlSchema.schema)\n\t}\n}\n"],"names":["createDbQueriesListener","createTriggeredActionsListener","createGraphQLQueryHandler","createGraphQLJsExecutor","createGraphQLJsonExecutor"],"mappings":";;;;;AAeO,MAAM,2BAA2B;AAAA,EACvC,YAA6B,OAAgB;AAAhB,SAAA,QAAA;AAAA,EAAiB;AAAA,EAEvC,OAAO,eAA0D;AACvE,UAAM,YAAsD,CAAA;AAC5D,cAAU,KAAKA,kBAAAA,wBAAwB,CAAA,YAAW,QAAQ,IAAI,KAAK,KAAK,CAAC;AACzE,cAAU,KAAKC,yBAAAA,gCAAgC;AAE/C,WAAOC,oCAAiD;AAAA,MACvD,UAAU,KAAK,eAAe,aAAa;AAAA,MAC3C;AAAA,MACA,iBAAiB,aAAW,QAAQ;AAAA,IAAA,CACpC;AAAA,EACF;AAAA,EAEQ,eAAe,eAA6E;AACnG,WAAO,cAAc,WAAW,eAAeC,UAAAA,wBAAwB,cAAc,MAAM,IAAIC,UAAAA,0BAA0B,cAAc,MAAM;AAAA,EAC9I;AACD;;"}
@@ -1,5 +1,5 @@
1
1
  import { createDbQueriesListener } from "../graphql/dbQueriesListener.js";
2
- import { createGraphQLQueryHandler } from "../graphql/execution.js";
2
+ import { createGraphQLQueryHandler, createGraphQLJsExecutor, createGraphQLJsonExecutor } from "../graphql/execution.js";
3
3
  import { createTriggeredActionsListener } from "./triggeredActionsListener.js";
4
4
  class ContentQueryHandlerFactory {
5
5
  constructor(debug) {
@@ -10,11 +10,14 @@ class ContentQueryHandlerFactory {
10
10
  listeners.push(createDbQueriesListener((context) => context.db, this.debug));
11
11
  listeners.push(createTriggeredActionsListener());
12
12
  return createGraphQLQueryHandler({
13
- schema: graphQlSchema,
13
+ executor: this.createExecutor(graphQlSchema),
14
14
  listeners,
15
15
  getMemoryBudget: (context) => context.memoryBudget
16
16
  });
17
17
  }
18
+ createExecutor(graphQlSchema) {
19
+ return graphQlSchema.engine === "graphql-js" ? createGraphQLJsExecutor(graphQlSchema.schema) : createGraphQLJsonExecutor(graphQlSchema.schema);
20
+ }
18
21
  }
19
22
  export {
20
23
  ContentQueryHandlerFactory
@@ -1 +1 @@
1
- {"version":3,"file":"ContentQueryHandlerFactory.js","sources":["../../../../../packages/engine-http/src/content/ContentQueryHandlerFactory.ts"],"sourcesContent":["import { GraphQLSchema } from 'graphql'\nimport { createDbQueriesListener, createGraphQLQueryHandler, GraphQLListener, GraphQLQueryHandler } from '../graphql/index.js'\nimport { ContentGraphqlContext } from './ContentGraphqlContext.js'\nimport { createTriggeredActionsListener } from './triggeredActionsListener.js'\n\nexport type ContentQueryHandler = GraphQLQueryHandler<ContentGraphqlContext>\n\nexport class ContentQueryHandlerFactory {\n\tconstructor(private readonly debug: boolean) {}\n\n\tpublic create(graphQlSchema: GraphQLSchema): ContentQueryHandler {\n\t\tconst listeners: GraphQLListener<ContentGraphqlContext>[] = []\n\t\tlisteners.push(createDbQueriesListener(context => context.db, this.debug))\n\t\tlisteners.push(createTriggeredActionsListener())\n\n\t\treturn createGraphQLQueryHandler<ContentGraphqlContext>({\n\t\t\tschema: graphQlSchema,\n\t\t\tlisteners,\n\t\t\tgetMemoryBudget: context => context.memoryBudget,\n\t\t})\n\t}\n}\n"],"names":[],"mappings":";;;AAOO,MAAM,2BAA2B;AAAA,EACvC,YAA6B,OAAgB;AAAhB,SAAA,QAAA;AAAA,EAAiB;AAAA,EAEvC,OAAO,eAAmD;AAChE,UAAM,YAAsD,CAAA;AAC5D,cAAU,KAAK,wBAAwB,CAAA,YAAW,QAAQ,IAAI,KAAK,KAAK,CAAC;AACzE,cAAU,KAAK,gCAAgC;AAE/C,WAAO,0BAAiD;AAAA,MACvD,QAAQ;AAAA,MACR;AAAA,MACA,iBAAiB,aAAW,QAAQ;AAAA,IAAA,CACpC;AAAA,EACF;AACD;"}
1
+ {"version":3,"file":"ContentQueryHandlerFactory.js","sources":["../../../../../packages/engine-http/src/content/ContentQueryHandlerFactory.ts"],"sourcesContent":["import {\n\tcreateDbQueriesListener,\n\tcreateGraphQLJsExecutor,\n\tcreateGraphQLJsonExecutor,\n\tcreateGraphQLQueryHandler,\n\tGraphQLExecutor,\n\tGraphQLListener,\n\tGraphQLQueryHandler,\n} from '../graphql/index.js'\nimport { ContentGraphQLSchema } from './ContentGraphQLEngine.js'\nimport { ContentGraphqlContext } from './ContentGraphqlContext.js'\nimport { createTriggeredActionsListener } from './triggeredActionsListener.js'\n\nexport type ContentQueryHandler = GraphQLQueryHandler<ContentGraphqlContext>\n\nexport class ContentQueryHandlerFactory {\n\tconstructor(private readonly debug: boolean) {}\n\n\tpublic create(graphQlSchema: ContentGraphQLSchema): ContentQueryHandler {\n\t\tconst listeners: GraphQLListener<ContentGraphqlContext>[] = []\n\t\tlisteners.push(createDbQueriesListener(context => context.db, this.debug))\n\t\tlisteners.push(createTriggeredActionsListener())\n\n\t\treturn createGraphQLQueryHandler<ContentGraphqlContext>({\n\t\t\texecutor: this.createExecutor(graphQlSchema),\n\t\t\tlisteners,\n\t\t\tgetMemoryBudget: context => context.memoryBudget,\n\t\t})\n\t}\n\n\tprivate createExecutor(graphQlSchema: ContentGraphQLSchema): GraphQLExecutor<ContentGraphqlContext> {\n\t\treturn graphQlSchema.engine === 'graphql-js' ? createGraphQLJsExecutor(graphQlSchema.schema) : createGraphQLJsonExecutor(graphQlSchema.schema)\n\t}\n}\n"],"names":[],"mappings":";;;AAeO,MAAM,2BAA2B;AAAA,EACvC,YAA6B,OAAgB;AAAhB,SAAA,QAAA;AAAA,EAAiB;AAAA,EAEvC,OAAO,eAA0D;AACvE,UAAM,YAAsD,CAAA;AAC5D,cAAU,KAAK,wBAAwB,CAAA,YAAW,QAAQ,IAAI,KAAK,KAAK,CAAC;AACzE,cAAU,KAAK,gCAAgC;AAE/C,WAAO,0BAAiD;AAAA,MACvD,UAAU,KAAK,eAAe,aAAa;AAAA,MAC3C;AAAA,MACA,iBAAiB,aAAW,QAAQ;AAAA,IAAA,CACpC;AAAA,EACF;AAAA,EAEQ,eAAe,eAA6E;AACnG,WAAO,cAAc,WAAW,eAAe,wBAAwB,cAAc,MAAM,IAAI,0BAA0B,cAAc,MAAM;AAAA,EAC9I;AACD;"}
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const node_fs = require("node:fs");
4
+ var __defProp = Object.defineProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ const contentRequestLogMaxRequestsCeiling = 1e5;
8
+ const contentRequestLogMaxUntilDays = 7;
9
+ const closeTimeoutMs = 5e3;
10
+ class ContentRequestLog {
11
+ constructor(options, logger, now = Date.now) {
12
+ this.options = options;
13
+ this.logger = logger;
14
+ this.now = now;
15
+ __publicField(this, "projects");
16
+ __publicField(this, "counts", /* @__PURE__ */ new Map());
17
+ __publicField(this, "until");
18
+ __publicField(this, "stream");
19
+ __publicField(this, "failed", false);
20
+ this.projects = new Set(options.projects);
21
+ this.until = Date.parse(options.until);
22
+ }
23
+ accepts(project) {
24
+ return !this.failed && this.projects.has(project) && (this.counts.get(project) ?? 0) < this.options.maxRequests && this.now() < this.until;
25
+ }
26
+ record(entry) {
27
+ if (!this.accepts(entry.project)) {
28
+ return;
29
+ }
30
+ const count = (this.counts.get(entry.project) ?? 0) + 1;
31
+ this.counts.set(entry.project, count);
32
+ const { request } = entry;
33
+ this.getStream().write(
34
+ JSON.stringify({
35
+ time: new Date(this.now()).toISOString(),
36
+ projectGroup: entry.projectGroup ?? null,
37
+ project: entry.project,
38
+ stage: entry.stage,
39
+ roles: entry.roles,
40
+ method: entry.method,
41
+ operationName: readField(request, "operationName"),
42
+ query: readField(request, "query"),
43
+ variables: readField(request, "variables"),
44
+ status: entry.status
45
+ }) + "\n"
46
+ );
47
+ if (count === this.options.maxRequests) {
48
+ this.logger.warn("Content API request log reached its cap for a project", { project: entry.project, maxRequests: count });
49
+ }
50
+ }
51
+ /** Flushes the recorded requests; called when the engine shuts down. */
52
+ async close() {
53
+ const stream = this.stream;
54
+ this.stream = void 0;
55
+ if (!stream) {
56
+ return;
57
+ }
58
+ if (this.failed) {
59
+ stream.destroy();
60
+ return;
61
+ }
62
+ let timer;
63
+ const flushed = await Promise.race([
64
+ new Promise((resolve) => stream.end(() => resolve(true))),
65
+ new Promise((resolve) => {
66
+ timer = setTimeout(() => resolve(false), closeTimeoutMs);
67
+ })
68
+ ]);
69
+ clearTimeout(timer);
70
+ if (!flushed) {
71
+ stream.destroy();
72
+ this.logger.warn("Content API request log was not flushed in time; the last entries may be missing", { file: this.options.file });
73
+ }
74
+ }
75
+ getStream() {
76
+ if (!this.stream) {
77
+ this.logger.warn("Content API request log started recording request variables", {
78
+ projects: this.options.projects,
79
+ file: this.options.file,
80
+ maxRequests: this.options.maxRequests,
81
+ until: this.options.until
82
+ });
83
+ const stream = node_fs.createWriteStream(this.options.file, { flags: "a", mode: 384 });
84
+ stream.on("error", (error) => {
85
+ this.failed = true;
86
+ this.logger.error("Content API request log failed and is stopped", { error, file: this.options.file });
87
+ });
88
+ this.stream = stream;
89
+ }
90
+ return this.stream;
91
+ }
92
+ }
93
+ const readField = (request, key) => typeof request === "object" && request !== null && Object.hasOwn(request, key) ? Reflect.get(request, key) : void 0;
94
+ exports.ContentRequestLog = ContentRequestLog;
95
+ exports.contentRequestLogMaxRequestsCeiling = contentRequestLogMaxRequestsCeiling;
96
+ exports.contentRequestLogMaxUntilDays = contentRequestLogMaxUntilDays;
97
+ //# sourceMappingURL=ContentRequestLog.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ContentRequestLog.cjs","sources":["../../../../../packages/engine-http/src/content/ContentRequestLog.ts"],"sourcesContent":["import { createWriteStream, WriteStream } from 'node:fs'\nimport { Logger } from '@contember/logger'\n\n// a type, not an interface, so that it fits the config schema's JSON types\nexport type ContentRequestLogOptions = {\n\t/** Only these projects are recorded; there is no wildcard. */\n\treadonly projects: readonly string[]\n\t/** A JSON-lines file; the engine appends to it and creates it readable by its owner only. */\n\treadonly file: string\n\t/** Requests recorded per project slug by one engine process. */\n\treadonly maxRequests: number\n\t/** An ISO timestamp; nothing is recorded after it, in any process and after any restart. */\n\treadonly until: string\n}\n\nexport interface ContentRequestLogEntry {\n\t/** Undefined for an engine that serves a single project group. */\n\treadonly projectGroup: string | undefined\n\treadonly project: string\n\treadonly stage: string\n\treadonly roles: readonly string[]\n\treadonly method: string\n\t/** The GraphQL request as the client sent it (the POST body or the GET query string). */\n\treadonly request: unknown\n\t/** The status the GraphQL handler produced, before `X-Contember-Force-Ok` coerces it. */\n\treadonly status: number\n}\n\n/** Ceiling of `maxRequests`, so that a typo cannot turn the sample into a full copy of the traffic. */\nexport const contentRequestLogMaxRequestsCeiling = 100_000\n\n/** How far ahead `until` may lie when the engine starts, so that a forgotten configuration expires. */\nexport const contentRequestLogMaxUntilDays = 7\n\nconst closeTimeoutMs = 5000\n\n/**\n * Records content API requests of selected projects, to replay them against a copy of the database before the\n * project changes its GraphQL engine. The variables carry client data, so the log is bounded by an absolute time\n * and by a count per process.\n */\nexport class ContentRequestLog {\n\tprivate readonly projects: ReadonlySet<string>\n\tprivate readonly counts = new Map<string, number>()\n\tprivate readonly until: number\n\tprivate stream: WriteStream | undefined\n\tprivate failed = false\n\n\tconstructor(\n\t\tprivate readonly options: ContentRequestLogOptions,\n\t\tprivate readonly logger: Logger,\n\t\tprivate readonly now: () => number = Date.now,\n\t) {\n\t\tthis.projects = new Set(options.projects)\n\t\tthis.until = Date.parse(options.until)\n\t}\n\n\tpublic accepts(project: string): boolean {\n\t\treturn !this.failed\n\t\t\t&& this.projects.has(project)\n\t\t\t&& (this.counts.get(project) ?? 0) < this.options.maxRequests\n\t\t\t&& this.now() < this.until\n\t}\n\n\tpublic record(entry: ContentRequestLogEntry): void {\n\t\tif (!this.accepts(entry.project)) {\n\t\t\treturn\n\t\t}\n\t\tconst count = (this.counts.get(entry.project) ?? 0) + 1\n\t\tthis.counts.set(entry.project, count)\n\t\tconst { request } = entry\n\t\tthis.getStream().write(\n\t\t\tJSON.stringify({\n\t\t\t\ttime: new Date(this.now()).toISOString(),\n\t\t\t\tprojectGroup: entry.projectGroup ?? null,\n\t\t\t\tproject: entry.project,\n\t\t\t\tstage: entry.stage,\n\t\t\t\troles: entry.roles,\n\t\t\t\tmethod: entry.method,\n\t\t\t\toperationName: readField(request, 'operationName'),\n\t\t\t\tquery: readField(request, 'query'),\n\t\t\t\tvariables: readField(request, 'variables'),\n\t\t\t\tstatus: entry.status,\n\t\t\t}) + '\\n',\n\t\t)\n\t\tif (count === this.options.maxRequests) {\n\t\t\tthis.logger.warn('Content API request log reached its cap for a project', { project: entry.project, maxRequests: count })\n\t\t}\n\t}\n\n\t/** Flushes the recorded requests; called when the engine shuts down. */\n\tpublic async close(): Promise<void> {\n\t\tconst stream = this.stream\n\t\tthis.stream = undefined\n\t\tif (!stream) {\n\t\t\treturn\n\t\t}\n\t\tif (this.failed) {\n\t\t\tstream.destroy()\n\t\t\treturn\n\t\t}\n\t\t// a destination that stopped accepting data must not hold up the shutdown\n\t\tlet timer: ReturnType<typeof setTimeout> | undefined\n\t\tconst flushed = await Promise.race([\n\t\t\tnew Promise<boolean>(resolve => stream.end(() => resolve(true))),\n\t\t\tnew Promise<boolean>(resolve => {\n\t\t\t\ttimer = setTimeout(() => resolve(false), closeTimeoutMs)\n\t\t\t}),\n\t\t])\n\t\tclearTimeout(timer)\n\t\tif (!flushed) {\n\t\t\tstream.destroy()\n\t\t\tthis.logger.warn('Content API request log was not flushed in time; the last entries may be missing', { file: this.options.file })\n\t\t}\n\t}\n\n\tprivate getStream(): WriteStream {\n\t\tif (!this.stream) {\n\t\t\tthis.logger.warn('Content API request log started recording request variables', {\n\t\t\t\tprojects: this.options.projects,\n\t\t\t\tfile: this.options.file,\n\t\t\t\tmaxRequests: this.options.maxRequests,\n\t\t\t\tuntil: this.options.until,\n\t\t\t})\n\t\t\tconst stream = createWriteStream(this.options.file, { flags: 'a', mode: 0o600 })\n\t\t\tstream.on('error', error => {\n\t\t\t\t// Recording must never fail a request; a broken destination stops the log instead.\n\t\t\t\tthis.failed = true\n\t\t\t\tthis.logger.error('Content API request log failed and is stopped', { error, file: this.options.file })\n\t\t\t})\n\t\t\tthis.stream = stream\n\t\t}\n\t\treturn this.stream\n\t}\n}\n\nconst readField = (request: unknown, key: string): unknown =>\n\ttypeof request === 'object' && request !== null && Object.hasOwn(request, key) ? Reflect.get(request, key) : undefined\n"],"names":["createWriteStream"],"mappings":";;;;;;AA6BO,MAAM,sCAAsC;AAG5C,MAAM,gCAAgC;AAE7C,MAAM,iBAAiB;AAOhB,MAAM,kBAAkB;AAAA,EAO9B,YACkB,SACA,QACA,MAAoB,KAAK,KACzC;AAHgB,SAAA,UAAA;AACA,SAAA,SAAA;AACA,SAAA,MAAA;AATlB,kBAAA,MAAiB,UAAA;AACjB,kBAAA,MAAiB,8BAAa,IAAA,CAAoB;AAClD,kBAAA,MAAiB,OAAA;AACjB,kBAAA,MAAQ,QAAA;AACR,kBAAA,MAAQ,UAAS,KAAA;AAOhB,SAAK,WAAW,IAAI,IAAI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