@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.
- package/CLAUDE.md +2 -0
- package/dist/development/index.cjs +8 -0
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +9 -1
- package/dist/development/index.js.map +1 -1
- package/dist/development/src/MasterContainer.cjs +28 -14
- package/dist/development/src/MasterContainer.cjs.map +1 -1
- package/dist/development/src/MasterContainer.js +28 -14
- package/dist/development/src/MasterContainer.js.map +1 -1
- package/dist/development/src/config/configSchema.cjs +50 -0
- package/dist/development/src/config/configSchema.cjs.map +1 -1
- package/dist/development/src/config/configSchema.js +50 -0
- package/dist/development/src/config/configSchema.js.map +1 -1
- package/dist/development/src/config/configTemplate.cjs +8 -0
- package/dist/development/src/config/configTemplate.cjs.map +1 -1
- package/dist/development/src/config/configTemplate.js +8 -0
- package/dist/development/src/config/configTemplate.js.map +1 -1
- package/dist/development/src/content/ContentApiControllerFactory.cjs +12 -1
- package/dist/development/src/content/ContentApiControllerFactory.cjs.map +1 -1
- package/dist/development/src/content/ContentApiControllerFactory.js +12 -1
- package/dist/development/src/content/ContentApiControllerFactory.js.map +1 -1
- package/dist/development/src/content/ContentApiSpecificCache.cjs +20 -5
- package/dist/development/src/content/ContentApiSpecificCache.cjs.map +1 -1
- package/dist/development/src/content/ContentApiSpecificCache.js +20 -5
- package/dist/development/src/content/ContentApiSpecificCache.js.map +1 -1
- package/dist/development/src/content/ContentGraphQLEngine.cjs +17 -0
- package/dist/development/src/content/ContentGraphQLEngine.cjs.map +1 -0
- package/dist/development/src/content/ContentGraphQLEngine.js +17 -0
- package/dist/development/src/content/ContentGraphQLEngine.js.map +1 -0
- package/dist/development/src/content/ContentQueryHandlerFactory.cjs +4 -1
- package/dist/development/src/content/ContentQueryHandlerFactory.cjs.map +1 -1
- package/dist/development/src/content/ContentQueryHandlerFactory.js +5 -2
- package/dist/development/src/content/ContentQueryHandlerFactory.js.map +1 -1
- package/dist/development/src/content/ContentRequestLog.cjs +97 -0
- package/dist/development/src/content/ContentRequestLog.cjs.map +1 -0
- package/dist/development/src/content/ContentRequestLog.js +97 -0
- package/dist/development/src/content/ContentRequestLog.js.map +1 -0
- package/dist/development/src/content/GraphQlSchemaFactory.cjs +121 -45
- package/dist/development/src/content/GraphQlSchemaFactory.cjs.map +1 -1
- package/dist/development/src/content/GraphQlSchemaFactory.js +122 -46
- package/dist/development/src/content/GraphQlSchemaFactory.js.map +1 -1
- package/dist/development/src/graphql/execution.cjs +18 -5
- package/dist/development/src/graphql/execution.cjs.map +1 -1
- package/dist/development/src/graphql/execution.js +19 -6
- package/dist/development/src/graphql/execution.js.map +1 -1
- package/dist/development/src/project/ProjectContainer.cjs +2 -0
- package/dist/development/src/project/ProjectContainer.cjs.map +1 -1
- package/dist/development/src/project/ProjectContainer.js +2 -0
- package/dist/development/src/project/ProjectContainer.js.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainer.cjs +2 -0
- package/dist/development/src/projectGroup/ProjectGroupContainer.cjs.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainer.js +2 -0
- package/dist/development/src/projectGroup/ProjectGroupContainer.js.map +1 -1
- package/dist/development/src/system/ContentQueryExecutor.cjs +23 -6
- package/dist/development/src/system/ContentQueryExecutor.cjs.map +1 -1
- package/dist/development/src/system/ContentQueryExecutor.js +25 -8
- package/dist/development/src/system/ContentQueryExecutor.js.map +1 -1
- package/dist/development/src/system/SystemGraphQLHandlerFactory.cjs +1 -1
- package/dist/development/src/system/SystemGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/development/src/system/SystemGraphQLHandlerFactory.js +2 -2
- package/dist/development/src/system/SystemGraphQLHandlerFactory.js.map +1 -1
- package/dist/development/src/tenant/TenantGraphQLHandlerFactory.cjs +1 -1
- package/dist/development/src/tenant/TenantGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/development/src/tenant/TenantGraphQLHandlerFactory.js +2 -2
- package/dist/development/src/tenant/TenantGraphQLHandlerFactory.js.map +1 -1
- package/dist/production/index.cjs +8 -0
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +9 -1
- package/dist/production/index.js.map +1 -1
- package/dist/production/src/MasterContainer.cjs +28 -14
- package/dist/production/src/MasterContainer.cjs.map +1 -1
- package/dist/production/src/MasterContainer.js +28 -14
- package/dist/production/src/MasterContainer.js.map +1 -1
- package/dist/production/src/config/configSchema.cjs +50 -0
- package/dist/production/src/config/configSchema.cjs.map +1 -1
- package/dist/production/src/config/configSchema.js +50 -0
- package/dist/production/src/config/configSchema.js.map +1 -1
- package/dist/production/src/config/configTemplate.cjs +8 -0
- package/dist/production/src/config/configTemplate.cjs.map +1 -1
- package/dist/production/src/config/configTemplate.js +8 -0
- package/dist/production/src/config/configTemplate.js.map +1 -1
- package/dist/production/src/content/ContentApiControllerFactory.cjs +12 -1
- package/dist/production/src/content/ContentApiControllerFactory.cjs.map +1 -1
- package/dist/production/src/content/ContentApiControllerFactory.js +12 -1
- package/dist/production/src/content/ContentApiControllerFactory.js.map +1 -1
- package/dist/production/src/content/ContentApiSpecificCache.cjs +20 -5
- package/dist/production/src/content/ContentApiSpecificCache.cjs.map +1 -1
- package/dist/production/src/content/ContentApiSpecificCache.js +20 -5
- package/dist/production/src/content/ContentApiSpecificCache.js.map +1 -1
- package/dist/production/src/content/ContentGraphQLEngine.cjs +17 -0
- package/dist/production/src/content/ContentGraphQLEngine.cjs.map +1 -0
- package/dist/production/src/content/ContentGraphQLEngine.js +17 -0
- package/dist/production/src/content/ContentGraphQLEngine.js.map +1 -0
- package/dist/production/src/content/ContentQueryHandlerFactory.cjs +4 -1
- package/dist/production/src/content/ContentQueryHandlerFactory.cjs.map +1 -1
- package/dist/production/src/content/ContentQueryHandlerFactory.js +5 -2
- package/dist/production/src/content/ContentQueryHandlerFactory.js.map +1 -1
- package/dist/production/src/content/ContentRequestLog.cjs +97 -0
- package/dist/production/src/content/ContentRequestLog.cjs.map +1 -0
- package/dist/production/src/content/ContentRequestLog.js +97 -0
- package/dist/production/src/content/ContentRequestLog.js.map +1 -0
- package/dist/production/src/content/GraphQlSchemaFactory.cjs +121 -45
- package/dist/production/src/content/GraphQlSchemaFactory.cjs.map +1 -1
- package/dist/production/src/content/GraphQlSchemaFactory.js +122 -46
- package/dist/production/src/content/GraphQlSchemaFactory.js.map +1 -1
- package/dist/production/src/graphql/execution.cjs +18 -5
- package/dist/production/src/graphql/execution.cjs.map +1 -1
- package/dist/production/src/graphql/execution.js +19 -6
- package/dist/production/src/graphql/execution.js.map +1 -1
- package/dist/production/src/project/ProjectContainer.cjs +2 -0
- package/dist/production/src/project/ProjectContainer.cjs.map +1 -1
- package/dist/production/src/project/ProjectContainer.js +2 -0
- package/dist/production/src/project/ProjectContainer.js.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainer.cjs +2 -0
- package/dist/production/src/projectGroup/ProjectGroupContainer.cjs.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainer.js +2 -0
- package/dist/production/src/projectGroup/ProjectGroupContainer.js.map +1 -1
- package/dist/production/src/system/ContentQueryExecutor.cjs +23 -6
- package/dist/production/src/system/ContentQueryExecutor.cjs.map +1 -1
- package/dist/production/src/system/ContentQueryExecutor.js +25 -8
- package/dist/production/src/system/ContentQueryExecutor.js.map +1 -1
- package/dist/production/src/system/SystemGraphQLHandlerFactory.cjs +1 -1
- package/dist/production/src/system/SystemGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/production/src/system/SystemGraphQLHandlerFactory.js +2 -2
- package/dist/production/src/system/SystemGraphQLHandlerFactory.js.map +1 -1
- package/dist/production/src/tenant/TenantGraphQLHandlerFactory.cjs +1 -1
- package/dist/production/src/tenant/TenantGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/production/src/tenant/TenantGraphQLHandlerFactory.js +2 -2
- package/dist/production/src/tenant/TenantGraphQLHandlerFactory.js.map +1 -1
- package/dist/tests/cases/unit/contentGraphqlCompat.test.d.ts +2 -0
- package/dist/tests/cases/unit/contentGraphqlCompat.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/contentRequestLog.test.d.ts +2 -0
- package/dist/tests/cases/unit/contentRequestLog.test.d.ts.map +1 -0
- package/dist/tests/contentControllerHarness.d.ts +29 -0
- package/dist/tests/contentControllerHarness.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/MasterContainer.d.ts +10 -1
- package/dist/types/MasterContainer.d.ts.map +1 -1
- package/dist/types/config/configSchema.d.ts +10 -0
- package/dist/types/config/configSchema.d.ts.map +1 -1
- package/dist/types/config/configTemplate.d.ts.map +1 -1
- package/dist/types/content/ContentApiControllerFactory.d.ts +3 -1
- package/dist/types/content/ContentApiControllerFactory.d.ts.map +1 -1
- package/dist/types/content/ContentApiSpecificCache.d.ts +2 -0
- package/dist/types/content/ContentApiSpecificCache.d.ts.map +1 -1
- package/dist/types/content/ContentGraphQLEngine.d.ts +18 -0
- package/dist/types/content/ContentGraphQLEngine.d.ts.map +1 -0
- package/dist/types/content/ContentQueryHandlerFactory.d.ts +3 -2
- package/dist/types/content/ContentQueryHandlerFactory.d.ts.map +1 -1
- package/dist/types/content/ContentRequestLog.d.ts +49 -0
- package/dist/types/content/ContentRequestLog.d.ts.map +1 -0
- package/dist/types/content/GraphQlSchemaFactory.d.ts +14 -4
- package/dist/types/content/GraphQlSchemaFactory.d.ts.map +1 -1
- package/dist/types/content/index.d.ts +2 -0
- package/dist/types/content/index.d.ts.map +1 -1
- package/dist/types/graphql/execution.d.ts +21 -3
- package/dist/types/graphql/execution.d.ts.map +1 -1
- package/dist/types/system/ContentQueryExecutor.d.ts +4 -1
- package/dist/types/system/ContentQueryExecutor.d.ts.map +1 -1
- package/dist/types/system/SystemGraphQLHandlerFactory.d.ts.map +1 -1
- package/dist/types/tenant/TenantGraphQLHandlerFactory.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +21 -18
- package/src/MasterContainer.ts +31 -14
- package/src/config/configSchema.ts +63 -0
- package/src/config/configTemplate.ts +8 -0
- package/src/content/ContentApiControllerFactory.ts +15 -2
- package/src/content/ContentApiSpecificCache.ts +22 -7
- package/src/content/ContentGraphQLEngine.ts +24 -0
- package/src/content/ContentQueryHandlerFactory.ts +16 -4
- package/src/content/ContentRequestLog.ts +138 -0
- package/src/content/GraphQlSchemaFactory.ts +173 -54
- package/src/content/index.ts +2 -0
- package/src/graphql/execution.ts +35 -7
- package/src/project/ProjectContainer.ts +1 -1
- package/src/system/ContentQueryExecutor.ts +43 -10
- package/src/system/SystemGraphQLHandlerFactory.ts +8 -2
- package/src/tenant/TenantGraphQLHandlerFactory.ts +2 -2
- package/src/tsconfig.json +1 -0
- package/tests/cases/unit/__snapshots__/contentGraphqlCompat.test.ts.snap +14295 -0
- package/tests/cases/unit/contentGraphqlCompat.test.ts +372 -0
- package/tests/cases/unit/contentRequestLog.test.ts +141 -0
- package/tests/cases/unit/forcePrimary.test.ts +93 -284
- package/tests/cases/unit/graphQlSchemaFactory.test.ts +227 -35
- package/tests/cases/unit/requestMemoryBudget.test.ts +96 -78
- package/tests/cases/unit/serverConfig.test.ts +133 -0
- package/tests/contentControllerHarness.ts +222 -0
- package/tests/tsconfig.json +3 -1
|
@@ -1,295 +1,104 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import Koa from 'koa'
|
|
4
|
-
import { IncomingMessage, ServerResponse } from 'node:http'
|
|
5
|
-
import { Socket } from 'node:net'
|
|
6
|
-
import { GraphQLError, GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql'
|
|
7
|
-
import { Connection, emptyDatabaseMetadata, EventManager, Pool } from '@contember/database'
|
|
8
|
-
import { DatabaseContext, DatabaseContextFactory } from '@contember/engine-system-api'
|
|
9
|
-
import { ExecutionContainerFactory } from '@contember/engine-content-api'
|
|
10
|
-
import { emptySchema } from '@contember/schema-utils'
|
|
11
|
-
import { createLogger, TestLoggerHandler } from '@contember/logger'
|
|
12
|
-
import { noopTracer } from '@contember/telemetry'
|
|
13
|
-
import { createMock } from '../../utils.js'
|
|
14
|
-
import { ContentApiControllerFactory } from '../../../src/content/ContentApiControllerFactory.js'
|
|
15
|
-
import { ContentGraphqlContext } from '../../../src/content/ContentGraphqlContext.js'
|
|
16
|
-
import { ContentQueryHandlerFactory } from '../../../src/content/ContentQueryHandlerFactory.js'
|
|
17
|
-
import { ContentSchemaResolver } from '../../../src/content/ContentSchemaResolver.js'
|
|
18
|
-
import { GraphQlSchemaFactory } from '../../../src/content/GraphQlSchemaFactory.js'
|
|
19
|
-
import { NotModifiedChecker } from '../../../src/content/NotModifiedChecker.js'
|
|
20
|
-
import { ProjectMembershipResolver } from '../../../src/content/ProjectMembershipResolver.js'
|
|
21
|
-
import { ProjectContextResolver } from '../../../src/project-common/index.js'
|
|
22
|
-
import { ProjectContainer } from '../../../src/project/ProjectContainer.js'
|
|
23
|
-
import { ProjectDatabaseMetadataResolver } from '../../../src/project/ProjectDatabaseMetadataResolver.js'
|
|
24
|
-
import { ProjectConfig } from '../../../src/project/config.js'
|
|
25
|
-
import { ProjectGroupContainer } from '../../../src/projectGroup/ProjectGroupContainer.js'
|
|
26
|
-
import { TestTransactionService } from '../../../src/testing/index.js'
|
|
27
|
-
import { createProviders } from '../../../src/providers.js'
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { ContentGraphQLEngine } from '../../../src/content/ContentGraphQLEngine.js'
|
|
28
3
|
import { HttpResponse } from '../../../src/common/index.js'
|
|
4
|
+
import { createContentControllerHarness } from '../../contentControllerHarness.js'
|
|
29
5
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
events.on(EventManager.Event.queryStart, ({ sql }) => {
|
|
46
|
-
statements.push({ side, sql })
|
|
47
|
-
if (side === 'replica' && !replicaAvailable) {
|
|
48
|
-
query.mockRejectedValueOnce(new Error('Replica unavailable'))
|
|
49
|
-
return
|
|
50
|
-
}
|
|
51
|
-
const rows = sql.includes('"stage_transaction"')
|
|
52
|
-
? [{ transaction_id: side === 'primary' ? 'new-ref' : 'old-ref' }]
|
|
53
|
-
: sql.includes('"stage"')
|
|
54
|
-
? [stage]
|
|
55
|
-
: [{ marker: side }]
|
|
56
|
-
query.mockResolvedValueOnce({ rows, rowCount: rows.length, command: 'SELECT', oid: 0, fields: [] })
|
|
57
|
-
})
|
|
58
|
-
return new Connection(new Pool(() => client, { maxConnections: 1, logError: () => {} }), events)
|
|
59
|
-
}
|
|
60
|
-
const primary = database('primary')
|
|
61
|
-
const replica = withReplica ? database('replica') : primary
|
|
62
|
-
const providers = createProviders()
|
|
63
|
-
const logger = createLogger(new TestLoggerHandler())
|
|
64
|
-
const systemDatabaseContextFactory = new DatabaseContextFactory('system', providers)
|
|
65
|
-
const project: ProjectConfig = {
|
|
66
|
-
slug: 'test',
|
|
67
|
-
name: 'Test',
|
|
68
|
-
stages: [],
|
|
69
|
-
db: { host: 'primary', port: 5432, user: 'test', password: 'test', database: 'test', read: { host: 'replica' } },
|
|
70
|
-
}
|
|
71
|
-
const projectContainer: ProjectContainer = {
|
|
72
|
-
project,
|
|
73
|
-
logger,
|
|
74
|
-
connection: primary,
|
|
75
|
-
readConnection: replica,
|
|
76
|
-
systemDatabaseContextFactory,
|
|
77
|
-
systemDatabaseContext: systemDatabaseContextFactory.create(primary),
|
|
78
|
-
systemReadDatabaseContext: systemDatabaseContextFactory.create(replica),
|
|
79
|
-
contentSchemaResolver: createMock<ContentSchemaResolver>({
|
|
80
|
-
clearCache: () => {},
|
|
81
|
-
getSchema: async ({ db }) => {
|
|
82
|
-
preparation.push(db)
|
|
83
|
-
return { schema: emptySchema, meta: {} }
|
|
84
|
-
},
|
|
85
|
-
}),
|
|
86
|
-
projectDatabaseMetadataResolver: createMock<ProjectDatabaseMetadataResolver>({
|
|
87
|
-
resolveDatabaseMetadata: async db => {
|
|
88
|
-
preparation.push(db)
|
|
89
|
-
return emptyDatabaseMetadata
|
|
90
|
-
},
|
|
91
|
-
}),
|
|
92
|
-
get projectInitializer() {
|
|
93
|
-
return unavailable()
|
|
94
|
-
},
|
|
95
|
-
}
|
|
96
|
-
const marker = {
|
|
97
|
-
type: GraphQLString,
|
|
98
|
-
resolve: async (_source: unknown, _args: unknown, context: ContentGraphqlContext) => {
|
|
99
|
-
const result = await context.db.query<{ marker: string }>('SELECT marker')
|
|
100
|
-
return result.rows[0].marker
|
|
101
|
-
},
|
|
102
|
-
}
|
|
103
|
-
const schema = new GraphQLSchema({
|
|
104
|
-
query: new GraphQLObjectType({ name: 'Query', fields: { marker } }),
|
|
105
|
-
mutation: new GraphQLObjectType({
|
|
106
|
-
name: 'Mutation',
|
|
107
|
-
fields: {
|
|
108
|
-
marker,
|
|
109
|
-
fail: {
|
|
110
|
-
type: GraphQLString,
|
|
111
|
-
resolve: () => {
|
|
112
|
-
throw new GraphQLError('Mutation failed')
|
|
113
|
-
},
|
|
114
|
-
},
|
|
115
|
-
},
|
|
116
|
-
}),
|
|
6
|
+
describe.each<ContentGraphQLEngine>(['graphql-js', 'graphql-json'])('on %s', engine => {
|
|
7
|
+
test('force-primary routes preparation, cache validation and content to primary even with an unavailable replica', async () => {
|
|
8
|
+
const harness = createContentControllerHarness(engine)
|
|
9
|
+
try {
|
|
10
|
+
harness.setReplicaUnavailable()
|
|
11
|
+
const { koa, result } = await harness.request('{ marker }', { 'x-contember-force-primary': '1', 'x-contember-ref': 'old-ref' })
|
|
12
|
+
expect(result).toBeUndefined()
|
|
13
|
+
expect(koa.body).toBe('{"data":{"marker":"primary"}}')
|
|
14
|
+
expect(koa.response.get('X-Contember-Mutation')).toBe('')
|
|
15
|
+
expect(harness.statements.every(it => it.side === 'primary')).toBe(true)
|
|
16
|
+
expect(harness.statements.some(it => it.sql.includes('"stage_transaction"'))).toBe(true)
|
|
17
|
+
expect(harness.preparation).toEqual([harness.primarySystem, harness.primarySystem])
|
|
18
|
+
} finally {
|
|
19
|
+
await harness.close()
|
|
20
|
+
}
|
|
117
21
|
})
|
|
118
|
-
const controller = new ContentApiControllerFactory(
|
|
119
|
-
new NotModifiedChecker(),
|
|
120
|
-
new ExecutionContainerFactory(providers),
|
|
121
|
-
new ContentQueryHandlerFactory(false),
|
|
122
|
-
createMock<ProjectContextResolver>({ resolve: async () => ({ projectContainer, project }) }),
|
|
123
|
-
createMock<GraphQlSchemaFactory>({ create: () => ({ schema, permissions: {}, allPermissions: {} }) }),
|
|
124
|
-
new TestTransactionService(false),
|
|
125
|
-
forcePrimaryHeader,
|
|
126
|
-
).create()
|
|
127
|
-
const projectGroup: ProjectGroupContainer = {
|
|
128
|
-
slug: undefined,
|
|
129
|
-
logger,
|
|
130
|
-
projectMembershipResolver: createMock<ProjectMembershipResolver>({ resolveMemberships: async () => ({ effective: [], fetched: [] }) }),
|
|
131
|
-
get authenticator() {
|
|
132
|
-
return unavailable()
|
|
133
|
-
},
|
|
134
|
-
get projectContainerResolver() {
|
|
135
|
-
return unavailable()
|
|
136
|
-
},
|
|
137
|
-
get projectSchemaResolver() {
|
|
138
|
-
return unavailable()
|
|
139
|
-
},
|
|
140
|
-
get projectInitializer() {
|
|
141
|
-
return unavailable()
|
|
142
|
-
},
|
|
143
|
-
get tenantContainer() {
|
|
144
|
-
return unavailable()
|
|
145
|
-
},
|
|
146
|
-
get tenantGraphQLHandler() {
|
|
147
|
-
return unavailable()
|
|
148
|
-
},
|
|
149
|
-
get systemContainer() {
|
|
150
|
-
return unavailable()
|
|
151
|
-
},
|
|
152
|
-
get systemGraphQLHandler() {
|
|
153
|
-
return unavailable()
|
|
154
|
-
},
|
|
155
|
-
}
|
|
156
|
-
return {
|
|
157
|
-
statements,
|
|
158
|
-
preparation,
|
|
159
|
-
primarySystem: projectContainer.systemDatabaseContext,
|
|
160
|
-
replicaSystem: projectContainer.systemReadDatabaseContext,
|
|
161
|
-
setReplicaUnavailable: () => {
|
|
162
|
-
replicaAvailable = false
|
|
163
|
-
},
|
|
164
|
-
close: async () => {
|
|
165
|
-
await primary.end()
|
|
166
|
-
if (replica !== primary) {
|
|
167
|
-
await replica.end()
|
|
168
|
-
}
|
|
169
|
-
},
|
|
170
|
-
request: async (query: string, headers: Record<string, string> = {}) => {
|
|
171
|
-
const message = new IncomingMessage(new Socket())
|
|
172
|
-
message.method = 'POST'
|
|
173
|
-
message.url = '/content/test/live'
|
|
174
|
-
message.headers = headers
|
|
175
|
-
const response = new ServerResponse(message)
|
|
176
|
-
const koa = new Koa().createContext(message, response)
|
|
177
|
-
const body = { query }
|
|
178
|
-
Object.assign(koa.request, { body })
|
|
179
|
-
const result = await controller({
|
|
180
|
-
koa,
|
|
181
|
-
body,
|
|
182
|
-
request: message,
|
|
183
|
-
response,
|
|
184
|
-
projectGroup,
|
|
185
|
-
logger,
|
|
186
|
-
url: new URL('http://localhost/content/test/live'),
|
|
187
|
-
clientIp: '127.0.0.1',
|
|
188
|
-
timer: (_event, callback) => callback(),
|
|
189
|
-
tracer: noopTracer,
|
|
190
|
-
requestDebugMode: false,
|
|
191
|
-
authResult: { valid: true, identityId: 'identity', apiKeyId: 'key', roles: [], personId: null, trustForwardedInfo: false },
|
|
192
|
-
params: { projectSlug: 'test', stageSlug: 'live' },
|
|
193
|
-
})
|
|
194
|
-
return { koa, result }
|
|
195
|
-
},
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
test('force-primary routes preparation, cache validation and content to primary even with an unavailable replica', async () => {
|
|
200
|
-
const harness = createHarness()
|
|
201
|
-
try {
|
|
202
|
-
harness.setReplicaUnavailable()
|
|
203
|
-
const { koa, result } = await harness.request('{ marker }', { 'x-contember-force-primary': '1', 'x-contember-ref': 'old-ref' })
|
|
204
|
-
expect(result).toBeUndefined()
|
|
205
|
-
expect(koa.body).toBe('{"data":{"marker":"primary"}}')
|
|
206
|
-
expect(koa.response.get('X-Contember-Mutation')).toBe('')
|
|
207
|
-
expect(harness.statements.every(it => it.side === 'primary')).toBe(true)
|
|
208
|
-
expect(harness.statements.some(it => it.sql.includes('"stage_transaction"'))).toBe(true)
|
|
209
|
-
expect(harness.preparation).toEqual([harness.primarySystem, harness.primarySystem])
|
|
210
|
-
} finally {
|
|
211
|
-
await harness.close()
|
|
212
|
-
}
|
|
213
|
-
})
|
|
214
22
|
|
|
215
|
-
test('a forced primary read can return 304 from the primary reference', async () => {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
})
|
|
226
|
-
|
|
227
|
-
test('without the header, preparation, cache validation and content read the replica', async () => {
|
|
228
|
-
const harness = createHarness()
|
|
229
|
-
try {
|
|
230
|
-
const { koa } = await harness.request('{ marker }', { 'x-contember-ref': 'new-ref' })
|
|
231
|
-
expect(koa.body).toBe('{"data":{"marker":"replica"}}')
|
|
232
|
-
expect(harness.statements.every(it => it.side === 'replica')).toBe(true)
|
|
233
|
-
expect(harness.statements.some(it => it.sql.includes('"stage_transaction"'))).toBe(true)
|
|
234
|
-
expect(harness.preparation).toEqual([harness.replicaSystem, harness.replicaSystem])
|
|
235
|
-
} finally {
|
|
236
|
-
await harness.close()
|
|
237
|
-
}
|
|
238
|
-
})
|
|
239
|
-
|
|
240
|
-
test('the header accepts the same truthy values as X-Contember-Force-Ok; ordinary queries still read the replica', async () => {
|
|
241
|
-
const harness = createHarness()
|
|
242
|
-
try {
|
|
243
|
-
for (const value of ['1', 'true', ' ON ', 'yes']) {
|
|
244
|
-
const { koa } = await harness.request('{ marker }', { 'x-contember-force-primary': value })
|
|
245
|
-
expect(koa.body).toBe('{"data":{"marker":"primary"}}')
|
|
23
|
+
test('a forced primary read can return 304 from the primary reference', async () => {
|
|
24
|
+
const harness = createContentControllerHarness(engine)
|
|
25
|
+
try {
|
|
26
|
+
const { result } = await harness.request('{ marker }', { 'x-contember-force-primary': '1', 'x-contember-ref': 'new-ref' })
|
|
27
|
+
expect(result).toBeInstanceOf(HttpResponse)
|
|
28
|
+
expect(result instanceof HttpResponse && result.code).toBe(304)
|
|
29
|
+
expect(harness.statements.some(it => it.sql === 'SELECT marker')).toBe(false)
|
|
30
|
+
} finally {
|
|
31
|
+
await harness.close()
|
|
246
32
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test('without the header, preparation, cache validation and content read the replica', async () => {
|
|
36
|
+
const harness = createContentControllerHarness(engine)
|
|
37
|
+
try {
|
|
38
|
+
const { koa } = await harness.request('{ marker }', { 'x-contember-ref': 'new-ref' })
|
|
250
39
|
expect(koa.body).toBe('{"data":{"marker":"replica"}}')
|
|
40
|
+
expect(harness.statements.every(it => it.side === 'replica')).toBe(true)
|
|
41
|
+
expect(harness.statements.some(it => it.sql.includes('"stage_transaction"'))).toBe(true)
|
|
42
|
+
expect(harness.preparation).toEqual([harness.replicaSystem, harness.replicaSystem])
|
|
43
|
+
} finally {
|
|
44
|
+
await harness.close()
|
|
251
45
|
}
|
|
252
|
-
}
|
|
253
|
-
await harness.close()
|
|
254
|
-
}
|
|
255
|
-
})
|
|
46
|
+
})
|
|
256
47
|
|
|
257
|
-
test('
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
48
|
+
test('the header accepts the same truthy values as X-Contember-Force-Ok; ordinary queries still read the replica', async () => {
|
|
49
|
+
const harness = createContentControllerHarness(engine)
|
|
50
|
+
try {
|
|
51
|
+
for (const value of ['1', 'true', ' ON ', 'yes']) {
|
|
52
|
+
const { koa } = await harness.request('{ marker }', { 'x-contember-force-primary': value })
|
|
53
|
+
expect(koa.body).toBe('{"data":{"marker":"primary"}}')
|
|
54
|
+
}
|
|
55
|
+
const headerCases: Record<string, string>[] = [{}, { 'x-contember-force-primary': '0' }, { 'x-contember-force-primary': 'false' }]
|
|
56
|
+
for (const headers of headerCases) {
|
|
57
|
+
const { koa } = await harness.request('{ marker }', headers)
|
|
58
|
+
expect(koa.body).toBe('{"data":{"marker":"replica"}}')
|
|
59
|
+
}
|
|
60
|
+
} finally {
|
|
61
|
+
await harness.close()
|
|
62
|
+
}
|
|
63
|
+
})
|
|
271
64
|
|
|
272
|
-
test('
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
}
|
|
65
|
+
test('without the config opt-in the header is ignored and mutations are not marked', async () => {
|
|
66
|
+
const harness = createContentControllerHarness(engine, { forcePrimaryHeader: false })
|
|
67
|
+
try {
|
|
68
|
+
const query = await harness.request('{ marker }', { 'x-contember-force-primary': '1' })
|
|
69
|
+
expect(query.koa.body).toBe('{"data":{"marker":"replica"}}')
|
|
70
|
+
expect(harness.statements.every(it => it.side === 'replica')).toBe(true)
|
|
71
|
+
expect(harness.preparation).toEqual([harness.replicaSystem, harness.replicaSystem])
|
|
72
|
+
const mutation = await harness.request('mutation { marker }')
|
|
73
|
+
expect(mutation.koa.body).toBe('{"data":{"marker":"primary"}}')
|
|
74
|
+
expect(mutation.koa.response.get('X-Contember-Mutation')).toBe('')
|
|
75
|
+
} finally {
|
|
76
|
+
await harness.close()
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('mutations run on primary and are marked even when a later root fails', async () => {
|
|
81
|
+
const harness = createContentControllerHarness(engine)
|
|
82
|
+
try {
|
|
83
|
+
const { koa } = await harness.request('mutation { marker fail }')
|
|
84
|
+
expect(koa.response.get('X-Contember-Mutation')).toBe('1')
|
|
85
|
+
expect(koa.body).toContain('"marker":"primary"')
|
|
86
|
+
expect(koa.body).toContain('Mutation failed')
|
|
87
|
+
const invalid = await harness.request('mutation { unknown }')
|
|
88
|
+
expect(invalid.koa.response.get('X-Contember-Mutation')).toBe('')
|
|
89
|
+
} finally {
|
|
90
|
+
await harness.close()
|
|
91
|
+
}
|
|
92
|
+
})
|
|
285
93
|
|
|
286
|
-
test('a project without a read replica does not mark mutations', async () => {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
94
|
+
test('a project without a read replica does not mark mutations', async () => {
|
|
95
|
+
const harness = createContentControllerHarness(engine, { withReplica: false })
|
|
96
|
+
try {
|
|
97
|
+
const { koa } = await harness.request('mutation { marker }')
|
|
98
|
+
expect(koa.body).toBe('{"data":{"marker":"primary"}}')
|
|
99
|
+
expect(koa.response.get('X-Contember-Mutation')).toBe('')
|
|
100
|
+
} finally {
|
|
101
|
+
await harness.close()
|
|
102
|
+
}
|
|
103
|
+
})
|
|
295
104
|
})
|