@contember/engine-http 0.12.0-alpha.9 → 1.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/ProjectConfig.d.ts +0 -1
- package/dist/src/ProjectConfig.d.ts.map +1 -1
- package/dist/src/ProjectContainer.d.ts +5 -2
- package/dist/src/ProjectContainer.d.ts.map +1 -1
- package/dist/src/common/ApiMiddleware.d.ts +12 -0
- package/dist/src/common/ApiMiddleware.d.ts.map +1 -0
- package/dist/src/common/ApiMiddleware.js +25 -0
- package/dist/src/common/ApiMiddleware.js.map +1 -0
- package/dist/src/common/AuthMiddleware.d.ts +11 -6
- package/dist/src/common/AuthMiddleware.d.ts.map +1 -1
- package/dist/src/common/AuthMiddleware.js +33 -25
- package/dist/src/common/AuthMiddleware.js.map +1 -1
- package/dist/src/common/ErrorFactory.d.ts +8 -0
- package/dist/src/common/ErrorFactory.d.ts.map +1 -0
- package/dist/src/common/ErrorFactory.js +21 -0
- package/dist/src/common/ErrorFactory.js.map +1 -0
- package/dist/src/common/ModuleInfoMiddleware.d.ts +1 -1
- package/dist/src/common/ModuleInfoMiddleware.d.ts.map +1 -1
- package/dist/src/common/ModuleInfoMiddleware.js.map +1 -1
- package/dist/src/common/TimerMiddleware.d.ts +1 -1
- package/dist/src/common/TimerMiddleware.d.ts.map +1 -1
- package/dist/src/common/TimerMiddleware.js.map +1 -1
- package/dist/src/common/index.d.ts +2 -2
- package/dist/src/common/index.d.ts.map +1 -1
- package/dist/src/common/index.js +2 -2
- package/dist/src/common/index.js.map +1 -1
- package/dist/src/content/ContentQueryHandlerFactory.d.ts +1 -2
- package/dist/src/content/ContentQueryHandlerFactory.d.ts.map +1 -1
- package/dist/src/content/ContentQueryHandlerFactory.js +7 -7
- package/dist/src/content/ContentQueryHandlerFactory.js.map +1 -1
- package/dist/src/content/ContentQueryHandlerProvider.d.ts.map +1 -1
- package/dist/src/content/ContentQueryHandlerProvider.js +1 -7
- package/dist/src/content/ContentQueryHandlerProvider.js.map +1 -1
- package/dist/src/content/ContentSchemaResolver.js +1 -1
- package/dist/src/content/ContentSchemaResolver.js.map +1 -1
- package/dist/src/content/ContentServerMiddleware.d.ts +5 -3
- package/dist/src/content/ContentServerMiddleware.d.ts.map +1 -1
- package/dist/src/content/ContentServerMiddleware.js +17 -15
- package/dist/src/content/ContentServerMiddleware.js.map +1 -1
- package/dist/src/content/GraphQlSchemaFactory.d.ts +1 -2
- package/dist/src/content/GraphQlSchemaFactory.d.ts.map +1 -1
- package/dist/src/content/GraphQlSchemaFactory.js +12 -16
- package/dist/src/content/GraphQlSchemaFactory.js.map +1 -1
- package/dist/src/content/NotModifiedMiddlewareFactory.d.ts +5 -2
- package/dist/src/content/NotModifiedMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/content/NotModifiedMiddlewareFactory.js +33 -31
- package/dist/src/content/NotModifiedMiddlewareFactory.js.map +1 -1
- package/dist/src/content/StageResolveMiddlewareFactory.d.ts +8 -2
- package/dist/src/content/StageResolveMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/content/StageResolveMiddlewareFactory.js +19 -14
- package/dist/src/content/StageResolveMiddlewareFactory.js.map +1 -1
- package/dist/src/content/index.d.ts +0 -1
- package/dist/src/content/index.d.ts.map +1 -1
- package/dist/src/content/index.js +0 -1
- package/dist/src/content/index.js.map +1 -1
- package/dist/src/graphql/errors.js +3 -3
- package/dist/src/graphql/errors.js.map +1 -1
- package/dist/src/graphql/execution.d.ts.map +1 -1
- package/dist/src/graphql/execution.js +7 -5
- package/dist/src/graphql/execution.js.map +1 -1
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +0 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/koa/router.js +1 -1
- package/dist/src/koa/router.js.map +1 -1
- package/dist/src/misc/HomepageMiddlewareFactory.js +1 -1
- package/dist/src/misc/HomepageMiddlewareFactory.js.map +1 -1
- package/dist/src/misc/PlaygroundMiddlewareFactory.d.ts +1 -1
- package/dist/src/misc/PlaygroundMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/misc/PlaygroundMiddlewareFactory.js +4 -44
- package/dist/src/misc/PlaygroundMiddlewareFactory.js.map +1 -1
- package/dist/src/project-common/ProjectGroupMiddleware.d.ts +17 -0
- package/dist/src/project-common/ProjectGroupMiddleware.d.ts.map +1 -0
- package/dist/src/project-common/ProjectGroupMiddleware.js +33 -0
- package/dist/src/project-common/ProjectGroupMiddleware.js.map +1 -0
- package/dist/src/project-common/ProjectMemberMiddlewareFactory.d.ts +12 -4
- package/dist/src/project-common/ProjectMemberMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/project-common/ProjectMemberMiddlewareFactory.js +26 -17
- package/dist/src/project-common/ProjectMemberMiddlewareFactory.js.map +1 -1
- package/dist/src/project-common/ProjectResolveMiddlewareFactory.d.ts +11 -5
- package/dist/src/project-common/ProjectResolveMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/project-common/ProjectResolveMiddlewareFactory.js +21 -15
- package/dist/src/project-common/ProjectResolveMiddlewareFactory.js.map +1 -1
- package/dist/src/project-common/index.d.ts +1 -0
- package/dist/src/project-common/index.d.ts.map +1 -1
- package/dist/src/project-common/index.js +1 -0
- package/dist/src/project-common/index.js.map +1 -1
- package/dist/src/providers.d.ts +2 -2
- package/dist/src/providers.d.ts.map +1 -1
- package/dist/src/providers.js +6 -5
- package/dist/src/providers.js.map +1 -1
- package/dist/src/system/SystemGraphQLMiddlewareFactory.d.ts +3 -4
- package/dist/src/system/SystemGraphQLMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/system/SystemGraphQLMiddlewareFactory.js +9 -12
- package/dist/src/system/SystemGraphQLMiddlewareFactory.js.map +1 -1
- package/dist/src/system/index.d.ts +0 -1
- package/dist/src/system/index.d.ts.map +1 -1
- package/dist/src/system/index.js +0 -1
- package/dist/src/system/index.js.map +1 -1
- package/dist/src/tenant/TenantGraphQLMiddlewareFactory.d.ts +6 -5
- package/dist/src/tenant/TenantGraphQLMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/tenant/TenantGraphQLMiddlewareFactory.js +8 -8
- package/dist/src/tenant/TenantGraphQLMiddlewareFactory.js.map +1 -1
- package/dist/src/tenant/index.d.ts +0 -1
- package/dist/src/tenant/index.d.ts.map +1 -1
- package/dist/src/tenant/index.js +0 -1
- package/dist/src/tenant/index.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/package.json +10 -10
- package/src/ProjectConfig.ts +0 -1
- package/src/ProjectContainer.ts +6 -5
- package/src/common/ApiMiddleware.ts +24 -0
- package/src/common/AuthMiddleware.ts +46 -31
- package/src/common/ErrorFactory.ts +21 -0
- package/src/common/ModuleInfoMiddleware.ts +4 -4
- package/src/common/TimerMiddleware.ts +5 -4
- package/src/common/index.ts +2 -2
- package/src/content/ContentQueryHandlerFactory.ts +1 -3
- package/src/content/ContentQueryHandlerProvider.ts +1 -4
- package/src/content/ContentServerMiddleware.ts +23 -20
- package/src/content/GraphQlSchemaFactory.ts +18 -27
- package/src/content/NotModifiedMiddlewareFactory.ts +34 -27
- package/src/content/StageResolveMiddlewareFactory.ts +29 -14
- package/src/content/index.ts +0 -1
- package/src/graphql/execution.ts +3 -1
- package/src/index.ts +0 -1
- package/src/misc/PlaygroundMiddlewareFactory.ts +1 -43
- package/src/project-common/ProjectGroupMiddleware.ts +50 -0
- package/src/project-common/ProjectMemberMiddlewareFactory.ts +42 -27
- package/src/project-common/ProjectResolveMiddlewareFactory.ts +33 -22
- package/src/project-common/index.ts +1 -0
- package/src/providers.ts +8 -5
- package/src/system/SystemGraphQLMiddlewareFactory.ts +17 -6
- package/src/system/index.ts +0 -1
- package/src/tenant/TenantGraphQLMiddlewareFactory.ts +11 -8
- package/src/tenant/index.ts +0 -1
- package/dist/src/common/DebugInfoMiddleware.d.ts +0 -6
- package/dist/src/common/DebugInfoMiddleware.d.ts.map +0 -1
- package/dist/src/common/DebugInfoMiddleware.js +0 -12
- package/dist/src/common/DebugInfoMiddleware.js.map +0 -1
- package/dist/src/common/ErrorResponseMiddleware.d.ts +0 -15
- package/dist/src/common/ErrorResponseMiddleware.d.ts.map +0 -1
- package/dist/src/common/ErrorResponseMiddleware.js +0 -30
- package/dist/src/common/ErrorResponseMiddleware.js.map +0 -1
- package/dist/src/content/ContentMiddleware.d.ts +0 -3
- package/dist/src/content/ContentMiddleware.d.ts.map +0 -1
- package/dist/src/content/ContentMiddleware.js +0 -27
- package/dist/src/content/ContentMiddleware.js.map +0 -1
- package/dist/src/services/ApiKeyManagerState.d.ts +0 -5
- package/dist/src/services/ApiKeyManagerState.d.ts.map +0 -1
- package/dist/src/services/ApiKeyManagerState.js +0 -3
- package/dist/src/services/ApiKeyManagerState.js.map +0 -1
- package/dist/src/services/ProjectContainerResolverState.d.ts +0 -5
- package/dist/src/services/ProjectContainerResolverState.d.ts.map +0 -1
- package/dist/src/services/ProjectContainerResolverState.js +0 -3
- package/dist/src/services/ProjectContainerResolverState.js.map +0 -1
- package/dist/src/services/ProjectMemberManagerState.d.ts +0 -5
- package/dist/src/services/ProjectMemberManagerState.d.ts.map +0 -1
- package/dist/src/services/ProjectMemberManagerState.js +0 -3
- package/dist/src/services/ProjectMemberManagerState.js.map +0 -1
- package/dist/src/services/ProvidersState.d.ts +0 -5
- package/dist/src/services/ProvidersState.d.ts.map +0 -1
- package/dist/src/services/ProvidersState.js +0 -3
- package/dist/src/services/ProvidersState.js.map +0 -1
- package/dist/src/services/ServicesProviderMiddleware.d.ts +0 -8
- package/dist/src/services/ServicesProviderMiddleware.d.ts.map +0 -1
- package/dist/src/services/ServicesProviderMiddleware.js +0 -17
- package/dist/src/services/ServicesProviderMiddleware.js.map +0 -1
- package/dist/src/services/index.d.ts +0 -6
- package/dist/src/services/index.d.ts.map +0 -1
- package/dist/src/services/index.js +0 -18
- package/dist/src/services/index.js.map +0 -1
- package/dist/src/system/SystemMiddleware.d.ts +0 -3
- package/dist/src/system/SystemMiddleware.d.ts.map +0 -1
- package/dist/src/system/SystemMiddleware.js +0 -22
- package/dist/src/system/SystemMiddleware.js.map +0 -1
- package/dist/src/tenant/TenantMiddleware.d.ts +0 -3
- package/dist/src/tenant/TenantMiddleware.d.ts.map +0 -1
- package/dist/src/tenant/TenantMiddleware.js +0 -19
- package/dist/src/tenant/TenantMiddleware.js.map +0 -1
- package/src/common/DebugInfoMiddleware.ts +0 -13
- package/src/common/ErrorResponseMiddleware.ts +0 -41
- package/src/content/ContentMiddleware.ts +0 -23
- package/src/services/ApiKeyManagerState.ts +0 -5
- package/src/services/ProjectContainerResolverState.ts +0 -5
- package/src/services/ProjectMemberManagerState.ts +0 -5
- package/src/services/ProvidersState.ts +0 -3
- package/src/services/ServicesProviderMiddleware.ts +0 -24
- package/src/services/index.ts +0 -5
- package/src/system/SystemMiddleware.ts +0 -20
- package/src/tenant/TenantMiddleware.ts +0 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/engine-http",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-alpha.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"typings": "dist/src/index.d.ts",
|
|
@@ -9,14 +9,14 @@
|
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@apollographql/graphql-playground-html": "^1.6.24",
|
|
12
|
-
"@contember/database": "^0.
|
|
13
|
-
"@contember/engine-content-api": "^0.
|
|
14
|
-
"@contember/engine-plugins": "^0.
|
|
15
|
-
"@contember/engine-system-api": "^0.
|
|
16
|
-
"@contember/engine-tenant-api": "^0.
|
|
17
|
-
"@contember/graphql-utils": "^0.
|
|
18
|
-
"@contember/schema": "^0.
|
|
19
|
-
"@contember/schema-utils": "^0.
|
|
12
|
+
"@contember/database": "^1.0.0-alpha.0",
|
|
13
|
+
"@contember/engine-content-api": "^1.0.0-alpha.0",
|
|
14
|
+
"@contember/engine-plugins": "^1.0.0-alpha.0",
|
|
15
|
+
"@contember/engine-system-api": "^1.0.0-alpha.0",
|
|
16
|
+
"@contember/engine-tenant-api": "^1.0.0-alpha.0",
|
|
17
|
+
"@contember/graphql-utils": "^1.0.0-alpha.0",
|
|
18
|
+
"@contember/schema": "^1.0.0-alpha.0",
|
|
19
|
+
"@contember/schema-utils": "^1.0.0-alpha.0",
|
|
20
20
|
"@graphql-tools/merge": "^6.2.14",
|
|
21
21
|
"@graphql-tools/schema": "^7.1.5",
|
|
22
22
|
"@koa/cors": "^3.1.0",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"@types/koa-bodyparser": "^4.2.2",
|
|
35
35
|
"@types/koa__cors": "^3.0.2",
|
|
36
36
|
"@types/lru-cache": "^5.1.0",
|
|
37
|
-
"@types/node": "^
|
|
37
|
+
"@types/node": "^17.0.5",
|
|
38
38
|
"@types/uuid": "^8.3.0",
|
|
39
39
|
"graphql": "^15.4.0"
|
|
40
40
|
},
|
package/src/ProjectConfig.ts
CHANGED
package/src/ProjectContainer.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { DatabaseContextFactory } from '@contember/engine-system-api'
|
|
2
2
|
import { Connection } from '@contember/database'
|
|
3
|
-
import {
|
|
3
|
+
import { ContentQueryHandlerProvider, ContentSchemaResolver } from './content'
|
|
4
4
|
import { ProjectConfig } from './ProjectConfig'
|
|
5
|
+
import { ProjectGroup } from '@contember/engine-tenant-api'
|
|
5
6
|
|
|
6
7
|
export interface ProjectContainer {
|
|
7
8
|
systemDatabaseContextFactory: DatabaseContextFactory
|
|
@@ -11,7 +12,7 @@ export interface ProjectContainer {
|
|
|
11
12
|
contentSchemaResolver: ContentSchemaResolver
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
export
|
|
15
|
-
slug: string,
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
export interface ProjectContainerResolver {
|
|
16
|
+
getProjectContainer(projectGroup: ProjectGroup, slug: string, aliasFallback?: boolean): Promise<ProjectContainer | undefined>
|
|
17
|
+
}
|
|
18
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { compose, KoaMiddleware } from '../koa'
|
|
2
|
+
import { createModuleInfoMiddleware } from './ModuleInfoMiddleware'
|
|
3
|
+
import corsMiddleware from '@koa/cors'
|
|
4
|
+
import { AuthMiddlewareFactory } from './AuthMiddleware'
|
|
5
|
+
import { ProjectGroupMiddlewareFactory } from '../project-common'
|
|
6
|
+
|
|
7
|
+
type KoaState = unknown
|
|
8
|
+
|
|
9
|
+
export class ApiMiddlewareFactory {
|
|
10
|
+
constructor(
|
|
11
|
+
private readonly projectGroupMiddlewareFactory: ProjectGroupMiddlewareFactory,
|
|
12
|
+
private readonly authMiddlewareFactory: AuthMiddlewareFactory,
|
|
13
|
+
) {
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public create(module: string): KoaMiddleware<KoaState> {
|
|
17
|
+
return compose([
|
|
18
|
+
this.projectGroupMiddlewareFactory.create(),
|
|
19
|
+
createModuleInfoMiddleware(module),
|
|
20
|
+
corsMiddleware(),
|
|
21
|
+
this.authMiddlewareFactory.create(),
|
|
22
|
+
])
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -1,41 +1,56 @@
|
|
|
1
|
-
import { VerifyResult } from '@contember/engine-tenant-api'
|
|
2
|
-
import { KoaMiddleware } from '../koa'
|
|
3
|
-
import { ErrorResponseMiddlewareState } from './ErrorResponseMiddleware'
|
|
4
|
-
import { ApiKeyManagerState } from '../services'
|
|
1
|
+
import { ApiKeyManager, VerifyResult } from '@contember/engine-tenant-api'
|
|
2
|
+
import { KoaContext, KoaMiddleware } from '../koa'
|
|
5
3
|
import { TimerMiddlewareState } from './TimerMiddleware'
|
|
4
|
+
import { ErrorFactory } from './ErrorFactory'
|
|
5
|
+
import { ProjectGroupState } from '../project-common'
|
|
6
6
|
|
|
7
|
-
type InputState =
|
|
7
|
+
type InputState =
|
|
8
|
+
& TimerMiddlewareState
|
|
8
9
|
|
|
9
|
-
type KoaState =
|
|
10
|
+
type KoaState =
|
|
11
|
+
& InputState
|
|
12
|
+
& AuthMiddlewareState
|
|
13
|
+
& ProjectGroupState
|
|
14
|
+
|
|
15
|
+
export interface AuthMiddlewareState {
|
|
16
|
+
authResult: VerifyResult & { assumedIdentityId?: string }
|
|
17
|
+
}
|
|
10
18
|
|
|
11
19
|
const assumeIdentityHeader = 'x-contember-assume-identity'
|
|
12
|
-
export const createAuthMiddleware = (): KoaMiddleware<KoaState> => {
|
|
13
|
-
const auth: KoaMiddleware<KoaState> = async (ctx, next) => {
|
|
14
|
-
const authHeader = ctx.request.get('Authorization')
|
|
15
|
-
if (typeof authHeader !== 'string' || authHeader === '') {
|
|
16
|
-
return ctx.state.fail.authorizationFailure(`Authorization header is missing`)
|
|
17
|
-
}
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
export class AuthMiddlewareFactory {
|
|
22
|
+
constructor(
|
|
23
|
+
private readonly apiKeyManager: ApiKeyManager,
|
|
24
|
+
private readonly errorFactory: ErrorFactory,
|
|
25
|
+
) {
|
|
26
|
+
}
|
|
20
27
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
public create(): KoaMiddleware<KoaState> {
|
|
29
|
+
const authError = (ctx: KoaContext<KoaState>, message: string) => this.errorFactory.createError(ctx, `Authorization failure: ${message}`, 401)
|
|
30
|
+
const auth: KoaMiddleware<KoaState> = async (ctx, next) => {
|
|
31
|
+
const authHeader = ctx.request.get('Authorization')
|
|
32
|
+
if (typeof authHeader !== 'string' || authHeader === '') {
|
|
33
|
+
return authError(ctx, `Authorization header is missing`)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const authHeaderPattern = /^Bearer\s+(\w+)$/i
|
|
37
|
+
|
|
38
|
+
const match = authHeader.match(authHeaderPattern)
|
|
39
|
+
if (match === null) {
|
|
40
|
+
return authError(ctx, `invalid Authorization header format`)
|
|
41
|
+
}
|
|
42
|
+
const [, token] = match
|
|
43
|
+
const tenantDatabase = ctx.state.projectGroup.database
|
|
44
|
+
const authResult = await ctx.state.timer('Auth', () => this.apiKeyManager.verifyAndProlong(tenantDatabase, token))
|
|
45
|
+
if (!authResult.ok) {
|
|
46
|
+
return authError(ctx, authResult.errorMessage)
|
|
47
|
+
}
|
|
48
|
+
ctx.state.authResult = {
|
|
49
|
+
...authResult.result,
|
|
50
|
+
assumedIdentityId: ctx.request.get(assumeIdentityHeader) || undefined,
|
|
51
|
+
}
|
|
52
|
+
await next()
|
|
33
53
|
}
|
|
34
|
-
|
|
54
|
+
return auth
|
|
35
55
|
}
|
|
36
|
-
return auth
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface AuthMiddlewareState {
|
|
40
|
-
authResult: VerifyResult & { assumedIdentityId?: string }
|
|
41
56
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { KoaContext } from '../koa'
|
|
2
|
+
import { AuthMiddlewareState } from './AuthMiddleware'
|
|
3
|
+
|
|
4
|
+
export class ErrorFactory {
|
|
5
|
+
constructor(
|
|
6
|
+
private readonly debug: boolean,
|
|
7
|
+
) {
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
createError(ctx: KoaContext<Partial<AuthMiddlewareState>>, message: string, code: number) {
|
|
11
|
+
ctx.set('Content-type', 'application/json')
|
|
12
|
+
ctx.status = code
|
|
13
|
+
const body: any = { errors: [{ message, code }] }
|
|
14
|
+
if (this.debug && ctx.state.authResult) {
|
|
15
|
+
body.identity = {
|
|
16
|
+
roles: ctx.state.authResult.roles,
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
ctx.body = JSON.stringify(body)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { KoaMiddleware } from '../koa'
|
|
2
2
|
|
|
3
|
+
export interface ModuleInfoMiddlewareState {
|
|
4
|
+
module: string
|
|
5
|
+
}
|
|
6
|
+
|
|
3
7
|
export const createModuleInfoMiddleware = (module: string): KoaMiddleware<ModuleInfoMiddlewareState> => {
|
|
4
8
|
return (ctx, next) => {
|
|
5
9
|
ctx.state.module = module
|
|
6
10
|
return next()
|
|
7
11
|
}
|
|
8
12
|
}
|
|
9
|
-
|
|
10
|
-
export interface ModuleInfoMiddlewareState {
|
|
11
|
-
module: string
|
|
12
|
-
}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { KoaMiddleware } from '../koa'
|
|
2
2
|
|
|
3
3
|
type EventTime = { label: string; start: number; duration?: number }
|
|
4
|
+
|
|
5
|
+
export interface TimerMiddlewareState {
|
|
6
|
+
timer: <T>(event: string, cb?: () => T) => Promise<T>
|
|
7
|
+
}
|
|
8
|
+
|
|
4
9
|
export const createTimerMiddleware = (): KoaMiddleware<TimerMiddlewareState> => {
|
|
5
10
|
const timer: KoaMiddleware<TimerMiddlewareState> = async (ctx, next) => {
|
|
6
11
|
const times: EventTime[] = []
|
|
@@ -32,7 +37,3 @@ export const createTimerMiddleware = (): KoaMiddleware<TimerMiddlewareState> =>
|
|
|
32
37
|
}
|
|
33
38
|
return timer
|
|
34
39
|
}
|
|
35
|
-
|
|
36
|
-
export interface TimerMiddlewareState {
|
|
37
|
-
timer: <T>(event: string, cb?: () => T) => Promise<T>
|
|
38
|
-
}
|
package/src/common/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
export * from './ApiMiddleware'
|
|
1
2
|
export * from './AuthMiddleware'
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './ErrorResponseMiddleware'
|
|
3
|
+
export * from './ErrorFactory'
|
|
4
4
|
export * from './ModuleInfoMiddleware'
|
|
5
5
|
export * from './PoweredByHeaderMiddleware'
|
|
6
6
|
export * from './TimerMiddleware'
|
|
@@ -29,7 +29,7 @@ type InputKoaContext = KoaContext<KoaState>
|
|
|
29
29
|
|
|
30
30
|
type ExtendedGraphqlContext = Context & { koaContext: KoaContext<KoaState> }
|
|
31
31
|
|
|
32
|
-
class ContentQueryHandlerFactory {
|
|
32
|
+
export class ContentQueryHandlerFactory {
|
|
33
33
|
constructor(
|
|
34
34
|
private readonly projectName: string,
|
|
35
35
|
private readonly debug: boolean,
|
|
@@ -91,5 +91,3 @@ class ContentQueryHandlerFactory {
|
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
|
-
|
|
95
|
-
export { ContentQueryHandlerFactory }
|
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import { ContentSchemaResolver } from './ContentSchemaResolver'
|
|
2
2
|
import { GraphQlSchemaFactory } from './GraphQlSchemaFactory'
|
|
3
3
|
import { ContentQueryHandlerFactory, KoaState } from './ContentQueryHandlerFactory'
|
|
4
|
-
import LRUCache from 'lru-cache'
|
|
5
4
|
import { GraphQLSchema } from 'graphql'
|
|
6
5
|
import { DatabaseContext } from '@contember/engine-system-api'
|
|
7
6
|
import { KoaMiddleware } from '../koa'
|
|
8
7
|
import { StageConfig } from '../ProjectConfig'
|
|
9
8
|
|
|
10
9
|
export class ContentQueryHandlerProvider {
|
|
11
|
-
private cache = new
|
|
12
|
-
max: 100,
|
|
13
|
-
})
|
|
10
|
+
private cache = new WeakMap<GraphQLSchema, KoaMiddleware<KoaState>>()
|
|
14
11
|
|
|
15
12
|
constructor(
|
|
16
13
|
private readonly contentSchemaFactory: ContentSchemaResolver,
|
|
@@ -4,34 +4,37 @@ import { AuthMiddlewareState, TimerMiddlewareState } from '../common'
|
|
|
4
4
|
import { Client } from '@contember/database'
|
|
5
5
|
import { formatSchemaName, unnamedIdentity } from '@contember/engine-system-api'
|
|
6
6
|
import { StageResolveMiddlewareState } from './StageResolveMiddlewareFactory'
|
|
7
|
-
import { ProvidersState } from '../services'
|
|
8
7
|
|
|
9
|
-
type
|
|
8
|
+
type InputKoaState =
|
|
10
9
|
& ProjectMemberMiddlewareState
|
|
11
10
|
& TimerMiddlewareState
|
|
12
11
|
& ProjectResolveMiddlewareState
|
|
13
12
|
& StageResolveMiddlewareState
|
|
14
|
-
& ContentServerMiddlewareState
|
|
15
|
-
& ProvidersState
|
|
16
13
|
& AuthMiddlewareState
|
|
17
14
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const stage = ctx.state.stage
|
|
22
|
-
const projectContainer = ctx.state.projectContainer
|
|
23
|
-
const dbContextFactory = ctx.state.projectContainer.systemDatabaseContextFactory
|
|
24
|
-
const dbClient = projectContainer.connection.createClient(formatSchemaName(stage), { module: 'content' })
|
|
25
|
-
ctx.state.db = dbClient
|
|
26
|
-
const handler = await ctx.state.timer('GraphQLServerCreate', () =>
|
|
27
|
-
projectContainer.contentQueryHandlerProvider.get(dbContextFactory.create(unnamedIdentity), stage, projectRoles),
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
await ctx.state.timer('GraphQL', () => handler(ctx, next))
|
|
31
|
-
}
|
|
32
|
-
return contentServer
|
|
33
|
-
}
|
|
15
|
+
type KoaState =
|
|
16
|
+
& InputKoaState
|
|
17
|
+
& ContentServerMiddlewareState
|
|
34
18
|
|
|
35
19
|
export interface ContentServerMiddlewareState {
|
|
36
20
|
db: Client
|
|
37
21
|
}
|
|
22
|
+
|
|
23
|
+
export class ContentServerMiddlewareFactory {
|
|
24
|
+
public create(): KoaMiddleware<KoaState> {
|
|
25
|
+
const contentServer: KoaMiddleware<KoaState> = async (ctx, next) => {
|
|
26
|
+
const projectRoles = ctx.state.projectMemberships.map(it => it.role)
|
|
27
|
+
const stage = ctx.state.stage
|
|
28
|
+
const projectContainer = ctx.state.projectContainer
|
|
29
|
+
const dbContextFactory = ctx.state.projectContainer.systemDatabaseContextFactory
|
|
30
|
+
const dbClient = projectContainer.connection.createClient(formatSchemaName(stage), { module: 'content' })
|
|
31
|
+
ctx.state.db = dbClient
|
|
32
|
+
const handler = await ctx.state.timer('GraphQLServerCreate', () =>
|
|
33
|
+
projectContainer.contentQueryHandlerProvider.get(dbContextFactory.create(unnamedIdentity), stage, projectRoles),
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
await ctx.state.timer('GraphQL', () => handler(ctx, next))
|
|
37
|
+
}
|
|
38
|
+
return contentServer
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -15,16 +15,16 @@ import { GraphQLSchemaContributor } from '@contember/engine-plugins'
|
|
|
15
15
|
import { JSONType } from '@contember/graphql-utils'
|
|
16
16
|
|
|
17
17
|
type Context = { schema: Schema; identity: Identity }
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
|
|
19
|
+
type CacheEntry = {
|
|
20
|
+
graphQlSchema: GraphQLSchema
|
|
21
|
+
permissions: Acl.Permissions
|
|
22
|
+
contributorsCacheKey: string
|
|
23
|
+
verifier: (identity: Identity) => boolean
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class GraphQlSchemaFactory {
|
|
27
|
+
private cache = new WeakMap<Schema, CacheEntry[]>()
|
|
28
28
|
|
|
29
29
|
constructor(
|
|
30
30
|
private readonly graphqlSchemaBuilderFactory: GraphQlSchemaBuilderFactory,
|
|
@@ -37,24 +37,17 @@ class GraphQlSchemaFactory {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
public create(schema: Schema, identity: Identity): [GraphQLSchema, Acl.Permissions] {
|
|
40
|
+
let cacheEntries = this.cache.get(schema)
|
|
40
41
|
const contributorsCacheKey = this.getContributorsCacheKey({ schema, identity })
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (!schemaCacheEntry) {
|
|
45
|
-
schemaCacheEntry = {
|
|
46
|
-
schema,
|
|
47
|
-
cacheKey: contributorsCacheKey,
|
|
48
|
-
entries: [],
|
|
49
|
-
}
|
|
50
|
-
this.cache.push(schemaCacheEntry)
|
|
51
|
-
} else {
|
|
52
|
-
const entry = schemaCacheEntry.entries.find(it => it.verifier(identity))
|
|
53
|
-
if (entry) {
|
|
42
|
+
if (cacheEntries !== undefined) {
|
|
43
|
+
const entry = cacheEntries.find(it => it.contributorsCacheKey === contributorsCacheKey && it.verifier(identity))
|
|
44
|
+
if (entry !== undefined) {
|
|
54
45
|
return [entry.graphQlSchema, entry.permissions]
|
|
55
46
|
}
|
|
47
|
+
} else {
|
|
48
|
+
cacheEntries = []
|
|
49
|
+
this.cache.set(schema, cacheEntries)
|
|
56
50
|
}
|
|
57
|
-
|
|
58
51
|
const { permissions, verifier } = this.permissionFactory.createPermissions(schema, identity)
|
|
59
52
|
|
|
60
53
|
const authorizator = new StaticAuthorizator(permissions)
|
|
@@ -83,10 +76,8 @@ class GraphQlSchemaFactory {
|
|
|
83
76
|
Json: JSONType,
|
|
84
77
|
},
|
|
85
78
|
})
|
|
86
|
-
|
|
79
|
+
cacheEntries.push({ graphQlSchema, verifier, permissions, contributorsCacheKey })
|
|
87
80
|
|
|
88
81
|
return [graphQlSchema, permissions]
|
|
89
82
|
}
|
|
90
83
|
}
|
|
91
|
-
|
|
92
|
-
export { GraphQlSchemaFactory }
|
|
@@ -4,34 +4,41 @@ import { KoaMiddleware } from '../koa'
|
|
|
4
4
|
import { LatestTransactionIdByStageQuery } from '@contember/engine-system-api'
|
|
5
5
|
import { TimerMiddlewareState } from '../common'
|
|
6
6
|
|
|
7
|
-
type
|
|
7
|
+
type InputKoaState =
|
|
8
|
+
& ProjectResolveMiddlewareState
|
|
9
|
+
& StageResolveMiddlewareState
|
|
10
|
+
& TimerMiddlewareState
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
12
|
+
type KoaState = InputKoaState
|
|
13
|
+
|
|
14
|
+
export class NotModifiedMiddlewareFactory {
|
|
15
|
+
public create(): KoaMiddleware<KoaState> {
|
|
16
|
+
const notModified: KoaMiddleware<KoaState> = async (ctx, next) => {
|
|
17
|
+
const NotModifiedHeaderName = 'x-contember-ref'
|
|
18
|
+
if (ctx.request.headers[NotModifiedHeaderName] === undefined) {
|
|
19
|
+
return next()
|
|
20
|
+
}
|
|
21
|
+
const requestRef = ctx.request.get(NotModifiedHeaderName)
|
|
22
|
+
const body = ctx.request.body
|
|
23
|
+
const isMutation = typeof body === 'object' && 'query' in body && String(body.query).includes('mutation')
|
|
24
|
+
if (isMutation) {
|
|
25
|
+
return await next()
|
|
26
|
+
}
|
|
27
|
+
const latestRef = await ctx.state.timer('NotModifiedCheck', () => {
|
|
28
|
+
const db = ctx.state.projectContainer.systemDatabaseContextFactory.create(undefined)
|
|
29
|
+
const queryHandler = db.queryHandler
|
|
30
|
+
const stageSlug = ctx.state.stage.slug
|
|
31
|
+
return queryHandler.fetch(new LatestTransactionIdByStageQuery(stageSlug))
|
|
32
|
+
})
|
|
33
|
+
if (latestRef === requestRef) {
|
|
34
|
+
ctx.status = 304
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
await next()
|
|
38
|
+
if (ctx.status === 200) {
|
|
39
|
+
ctx.response.set(NotModifiedHeaderName, latestRef)
|
|
40
|
+
}
|
|
34
41
|
}
|
|
42
|
+
return notModified
|
|
35
43
|
}
|
|
36
|
-
return notModifiedMiddleware
|
|
37
44
|
}
|
|
@@ -1,24 +1,39 @@
|
|
|
1
1
|
import { KoaMiddleware, KoaRequestState } from '../koa'
|
|
2
2
|
import { ProjectResolveMiddlewareState } from '../project-common'
|
|
3
3
|
import { StageConfig } from '../ProjectConfig'
|
|
4
|
+
import { AuthMiddlewareState, ErrorFactory } from '../common'
|
|
4
5
|
|
|
5
|
-
type
|
|
6
|
+
type InputKoaState =
|
|
7
|
+
& ProjectResolveMiddlewareState
|
|
8
|
+
& KoaRequestState
|
|
9
|
+
& AuthMiddlewareState
|
|
6
10
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
11
|
+
type KoaState =
|
|
12
|
+
& InputKoaState
|
|
13
|
+
& StageResolveMiddlewareState
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
export interface StageResolveMiddlewareState {
|
|
16
|
+
stage: StageConfig
|
|
17
|
+
}
|
|
12
18
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return next()
|
|
19
|
+
export class StageResolveMiddlewareFactory {
|
|
20
|
+
constructor(
|
|
21
|
+
private errorFactory: ErrorFactory,
|
|
22
|
+
) {
|
|
18
23
|
}
|
|
19
|
-
return stageResolve
|
|
20
|
-
}
|
|
21
24
|
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
public create(): KoaMiddleware<KoaState> {
|
|
26
|
+
const stageResolve: KoaMiddleware<KoaState> = (ctx, next) => {
|
|
27
|
+
const project = ctx.state.projectContainer.project
|
|
28
|
+
|
|
29
|
+
const stage = project.stages.find(stage => stage.slug === ctx.state.params.stageSlug)
|
|
30
|
+
|
|
31
|
+
if (stage === undefined) {
|
|
32
|
+
return this.errorFactory.createError(ctx, `Stage ${ctx.state.params.stageSlug} NOT found`, 404)
|
|
33
|
+
}
|
|
34
|
+
ctx.state.stage = stage
|
|
35
|
+
return next()
|
|
36
|
+
}
|
|
37
|
+
return stageResolve
|
|
38
|
+
}
|
|
24
39
|
}
|
package/src/content/index.ts
CHANGED
package/src/graphql/execution.ts
CHANGED
|
@@ -31,7 +31,9 @@ export const createGraphQLQueryHandler = <Context, KoaState>({
|
|
|
31
31
|
listeners,
|
|
32
32
|
}: FactoryArgs<Context, KoaState>): GraphQLQueryHandler<KoaState> => {
|
|
33
33
|
let schemaValidated = false
|
|
34
|
-
const documentCache = new LRUCache<string, DocumentNode>(
|
|
34
|
+
const documentCache = new LRUCache<string, DocumentNode>({
|
|
35
|
+
max: 200,
|
|
36
|
+
})
|
|
35
37
|
return async ctx => {
|
|
36
38
|
const listenersQueue = listeners
|
|
37
39
|
|
package/src/index.ts
CHANGED
|
@@ -5,46 +5,7 @@ import {
|
|
|
5
5
|
import accepts from 'accepts'
|
|
6
6
|
import { get, KoaContext, KoaMiddleware, KoaRequestState } from '../koa'
|
|
7
7
|
|
|
8
|
-
const
|
|
9
|
-
{
|
|
10
|
-
endpoint: '/content/sandbox/live',
|
|
11
|
-
name: 'Create a post',
|
|
12
|
-
variables: JSON.stringify({
|
|
13
|
-
post: {
|
|
14
|
-
title: 'Hello Contember',
|
|
15
|
-
content:
|
|
16
|
-
'Contember is a headless, GraphQL-first platform for building custom web applications. Prototype yours in a day, launch it in a week and run it for years.',
|
|
17
|
-
},
|
|
18
|
-
}),
|
|
19
|
-
query: `mutation($post: PostCreateInput!) {
|
|
20
|
-
createPost(data: $post) {
|
|
21
|
-
ok
|
|
22
|
-
errorMessage
|
|
23
|
-
node {
|
|
24
|
-
id
|
|
25
|
-
title
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}`,
|
|
29
|
-
headers: {
|
|
30
|
-
Authorization: 'Bearer 0000000000000000000000000000000000000000',
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
endpoint: '/content/sandbox/live',
|
|
35
|
-
name: 'List posts',
|
|
36
|
-
query: `query {
|
|
37
|
-
listPost {
|
|
38
|
-
title
|
|
39
|
-
}
|
|
40
|
-
}`,
|
|
41
|
-
headers: {
|
|
42
|
-
Authorization: 'Bearer 0000000000000000000000000000000000000000',
|
|
43
|
-
},
|
|
44
|
-
},
|
|
45
|
-
]
|
|
46
|
-
|
|
47
|
-
export const createPlaygroundMiddleware = (sandbox = false): KoaMiddleware<KoaRequestState> => {
|
|
8
|
+
export const createPlaygroundMiddleware = (): KoaMiddleware<KoaRequestState> => {
|
|
48
9
|
return get('/', (ctx: KoaContext<KoaRequestState>, next) => {
|
|
49
10
|
const accept = accepts(ctx.req)
|
|
50
11
|
const types = accept.types() as string[]
|
|
@@ -55,9 +16,6 @@ export const createPlaygroundMiddleware = (sandbox = false): KoaMiddleware<KoaRe
|
|
|
55
16
|
const playgroundRenderPageOptions: PlaygroundRenderPageOptions = {
|
|
56
17
|
endpoint: ctx.originalUrl,
|
|
57
18
|
}
|
|
58
|
-
if (sandbox) {
|
|
59
|
-
playgroundRenderPageOptions.tabs = sandboxTabs
|
|
60
|
-
}
|
|
61
19
|
|
|
62
20
|
ctx.set('Content-Type', 'text/html')
|
|
63
21
|
ctx.body = renderPlaygroundPage(playgroundRenderPageOptions)
|