@contember/engine-http 2.2.0-alpha.12 → 2.2.0-alpha.14
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/development/index.cjs +1 -0
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +2 -1
- package/dist/development/src/MasterContainer.cjs +2 -1
- package/dist/development/src/MasterContainer.cjs.map +1 -1
- package/dist/development/src/MasterContainer.js +2 -1
- package/dist/development/src/MasterContainer.js.map +1 -1
- package/dist/development/src/config/configSchema.cjs +23 -0
- package/dist/development/src/config/configSchema.cjs.map +1 -1
- package/dist/development/src/config/configSchema.js +23 -0
- package/dist/development/src/config/configSchema.js.map +1 -1
- package/dist/development/src/config/configTemplate.cjs +5 -0
- package/dist/development/src/config/configTemplate.cjs.map +1 -1
- package/dist/development/src/config/configTemplate.js +5 -0
- package/dist/development/src/config/configTemplate.js.map +1 -1
- package/dist/development/src/content/GraphQlSchemaFactory.cjs +3 -2
- package/dist/development/src/content/GraphQlSchemaFactory.cjs.map +1 -1
- package/dist/development/src/content/GraphQlSchemaFactory.js +3 -2
- package/dist/development/src/content/GraphQlSchemaFactory.js.map +1 -1
- package/dist/development/src/content/ProjectMembershipResolver.cjs +3 -6
- package/dist/development/src/content/ProjectMembershipResolver.cjs.map +1 -1
- package/dist/development/src/content/ProjectMembershipResolver.js +3 -6
- package/dist/development/src/content/ProjectMembershipResolver.js.map +1 -1
- package/dist/development/src/project/ProjectContainer.cjs +12 -8
- package/dist/development/src/project/ProjectContainer.cjs.map +1 -1
- package/dist/development/src/project/ProjectContainer.js +12 -8
- package/dist/development/src/project/ProjectContainer.js.map +1 -1
- package/dist/development/src/project/ProjectContainerResolver.cjs +23 -8
- package/dist/development/src/project/ProjectContainerResolver.cjs.map +1 -1
- package/dist/development/src/project/ProjectContainerResolver.js +23 -8
- package/dist/development/src/project/ProjectContainerResolver.js.map +1 -1
- package/dist/development/src/project/ProjectContainerStore.cjs +3 -0
- package/dist/development/src/project/ProjectContainerStore.cjs.map +1 -1
- package/dist/development/src/project/ProjectContainerStore.js +3 -0
- package/dist/development/src/project/ProjectContainerStore.js.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainer.cjs +12 -8
- package/dist/development/src/projectGroup/ProjectGroupContainer.cjs.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainer.js +12 -8
- package/dist/development/src/projectGroup/ProjectGroupContainer.js.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainerResolver.cjs +1 -3
- package/dist/development/src/projectGroup/ProjectGroupContainerResolver.cjs.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainerResolver.js +1 -3
- package/dist/development/src/projectGroup/ProjectGroupContainerResolver.js.map +1 -1
- package/dist/development/src/prometheus/dbMetrics.cjs +1 -1
- package/dist/development/src/prometheus/dbMetrics.cjs.map +1 -1
- package/dist/development/src/prometheus/dbMetrics.js +1 -1
- package/dist/development/src/prometheus/dbMetrics.js.map +1 -1
- package/dist/development/src/system/migrationLockOptions.cjs +14 -0
- package/dist/development/src/system/migrationLockOptions.cjs.map +1 -0
- package/dist/development/src/system/migrationLockOptions.js +14 -0
- package/dist/development/src/system/migrationLockOptions.js.map +1 -0
- package/dist/development/src/transfer/ImportApiMiddlewareFactory.cjs +7 -1
- package/dist/development/src/transfer/ImportApiMiddlewareFactory.cjs.map +1 -1
- package/dist/development/src/transfer/ImportApiMiddlewareFactory.js +8 -2
- package/dist/development/src/transfer/ImportApiMiddlewareFactory.js.map +1 -1
- package/dist/production/index.cjs +1 -0
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +2 -1
- package/dist/production/src/MasterContainer.cjs +2 -1
- package/dist/production/src/MasterContainer.cjs.map +1 -1
- package/dist/production/src/MasterContainer.js +2 -1
- package/dist/production/src/MasterContainer.js.map +1 -1
- package/dist/production/src/config/configSchema.cjs +23 -0
- package/dist/production/src/config/configSchema.cjs.map +1 -1
- package/dist/production/src/config/configSchema.js +23 -0
- package/dist/production/src/config/configSchema.js.map +1 -1
- package/dist/production/src/config/configTemplate.cjs +5 -0
- package/dist/production/src/config/configTemplate.cjs.map +1 -1
- package/dist/production/src/config/configTemplate.js +5 -0
- package/dist/production/src/config/configTemplate.js.map +1 -1
- package/dist/production/src/content/GraphQlSchemaFactory.cjs +3 -2
- package/dist/production/src/content/GraphQlSchemaFactory.cjs.map +1 -1
- package/dist/production/src/content/GraphQlSchemaFactory.js +3 -2
- package/dist/production/src/content/GraphQlSchemaFactory.js.map +1 -1
- package/dist/production/src/content/ProjectMembershipResolver.cjs +3 -6
- package/dist/production/src/content/ProjectMembershipResolver.cjs.map +1 -1
- package/dist/production/src/content/ProjectMembershipResolver.js +3 -6
- package/dist/production/src/content/ProjectMembershipResolver.js.map +1 -1
- package/dist/production/src/project/ProjectContainer.cjs +12 -8
- package/dist/production/src/project/ProjectContainer.cjs.map +1 -1
- package/dist/production/src/project/ProjectContainer.js +12 -8
- package/dist/production/src/project/ProjectContainer.js.map +1 -1
- package/dist/production/src/project/ProjectContainerResolver.cjs +23 -8
- package/dist/production/src/project/ProjectContainerResolver.cjs.map +1 -1
- package/dist/production/src/project/ProjectContainerResolver.js +23 -8
- package/dist/production/src/project/ProjectContainerResolver.js.map +1 -1
- package/dist/production/src/project/ProjectContainerStore.cjs +3 -0
- package/dist/production/src/project/ProjectContainerStore.cjs.map +1 -1
- package/dist/production/src/project/ProjectContainerStore.js +3 -0
- package/dist/production/src/project/ProjectContainerStore.js.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainer.cjs +12 -8
- package/dist/production/src/projectGroup/ProjectGroupContainer.cjs.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainer.js +12 -8
- package/dist/production/src/projectGroup/ProjectGroupContainer.js.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainerResolver.cjs +1 -3
- package/dist/production/src/projectGroup/ProjectGroupContainerResolver.cjs.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainerResolver.js +1 -3
- package/dist/production/src/projectGroup/ProjectGroupContainerResolver.js.map +1 -1
- package/dist/production/src/prometheus/dbMetrics.cjs +1 -1
- package/dist/production/src/prometheus/dbMetrics.cjs.map +1 -1
- package/dist/production/src/prometheus/dbMetrics.js +1 -1
- package/dist/production/src/prometheus/dbMetrics.js.map +1 -1
- package/dist/production/src/system/migrationLockOptions.cjs +14 -0
- package/dist/production/src/system/migrationLockOptions.cjs.map +1 -0
- package/dist/production/src/system/migrationLockOptions.js +14 -0
- package/dist/production/src/system/migrationLockOptions.js.map +1 -0
- package/dist/production/src/transfer/ImportApiMiddlewareFactory.cjs +7 -1
- package/dist/production/src/transfer/ImportApiMiddlewareFactory.cjs.map +1 -1
- package/dist/production/src/transfer/ImportApiMiddlewareFactory.js +8 -2
- package/dist/production/src/transfer/ImportApiMiddlewareFactory.js.map +1 -1
- package/dist/tests/cases/unit/dbMetrics.test.d.ts +2 -0
- package/dist/tests/cases/unit/dbMetrics.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/importClientAbort.fixture.d.ts +2 -0
- package/dist/tests/cases/unit/importClientAbort.fixture.d.ts.map +1 -0
- package/dist/tests/cases/unit/importClientAbort.test.d.ts +2 -0
- package/dist/tests/cases/unit/importClientAbort.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/projectContainerResolver.test.d.ts +2 -0
- package/dist/tests/cases/unit/projectContainerResolver.test.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/MasterContainer.d.ts +5 -0
- 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/GraphQlSchemaFactory.d.ts.map +1 -1
- package/dist/types/content/ProjectMembershipResolver.d.ts.map +1 -1
- package/dist/types/project/ProjectContainer.d.ts.map +1 -1
- package/dist/types/project/ProjectContainerResolver.d.ts +7 -1
- package/dist/types/project/ProjectContainerResolver.d.ts.map +1 -1
- package/dist/types/project/ProjectContainerStore.d.ts +1 -0
- package/dist/types/project/ProjectContainerStore.d.ts.map +1 -1
- package/dist/types/projectGroup/ProjectGroupContainer.d.ts.map +1 -1
- package/dist/types/projectGroup/ProjectGroupContainerResolver.d.ts.map +1 -1
- package/dist/types/system/migrationLockOptions.d.ts +4 -0
- package/dist/types/system/migrationLockOptions.d.ts.map +1 -0
- package/dist/types/transfer/ImportApiMiddlewareFactory.d.ts +3 -0
- package/dist/types/transfer/ImportApiMiddlewareFactory.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -16
- package/src/MasterContainer.ts +3 -2
- package/src/config/configSchema.ts +24 -0
- package/src/config/configTemplate.ts +5 -0
- package/src/content/GraphQlSchemaFactory.ts +5 -2
- package/src/content/ProjectMembershipResolver.ts +5 -6
- package/src/project/ProjectContainer.ts +11 -7
- package/src/project/ProjectContainerResolver.ts +27 -9
- package/src/project/ProjectContainerStore.ts +4 -0
- package/src/projectGroup/ProjectGroupContainer.ts +11 -7
- package/src/projectGroup/ProjectGroupContainerResolver.ts +1 -4
- package/src/prometheus/dbMetrics.ts +1 -1
- package/src/system/migrationLockOptions.ts +13 -0
- package/src/transfer/ImportApiMiddlewareFactory.ts +9 -1
- package/tests/cases/unit/dbMetrics.test.ts +40 -0
- package/tests/cases/unit/graphQlSchemaFactory.test.ts +43 -0
- package/tests/cases/unit/importClientAbort.fixture.ts +43 -0
- package/tests/cases/unit/importClientAbort.test.ts +24 -0
- package/tests/cases/unit/projectContainerResolver.test.ts +150 -0
- package/tests/cases/unit/projectMembershipResolver.test.ts +44 -0
- package/tests/cases/unit/serverConfig.test.ts +37 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/engine-http",
|
|
3
|
-
"version": "2.2.0-alpha.
|
|
3
|
+
"version": "2.2.0-alpha.14",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "./dist/production/index.js",
|
|
6
6
|
"typings": "./dist/types/index.d.ts",
|
|
@@ -26,21 +26,21 @@
|
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@contember/config-loader": "2.2.0-alpha.
|
|
30
|
-
"@contember/database": "2.2.0-alpha.
|
|
31
|
-
"@contember/database-migrations": "2.2.0-alpha.
|
|
32
|
-
"@contember/dic": "2.2.0-alpha.
|
|
33
|
-
"@contember/engine-common": "2.2.0-alpha.
|
|
34
|
-
"@contember/engine-content-api": "2.2.0-alpha.
|
|
35
|
-
"@contember/engine-system-api": "2.2.0-alpha.
|
|
36
|
-
"@contember/engine-tenant-api": "2.2.0-alpha.
|
|
37
|
-
"@contember/graphql-utils": "2.2.0-alpha.
|
|
38
|
-
"@contember/logger": "2.2.0-alpha.
|
|
39
|
-
"@contember/schema": "2.2.0-alpha.
|
|
40
|
-
"@contember/schema-migrations": "2.2.0-alpha.
|
|
41
|
-
"@contember/schema-utils": "2.2.0-alpha.
|
|
42
|
-
"@contember/telemetry": "2.2.0-alpha.
|
|
43
|
-
"@contember/typesafe": "2.2.0-alpha.
|
|
29
|
+
"@contember/config-loader": "2.2.0-alpha.14",
|
|
30
|
+
"@contember/database": "2.2.0-alpha.14",
|
|
31
|
+
"@contember/database-migrations": "2.2.0-alpha.14",
|
|
32
|
+
"@contember/dic": "2.2.0-alpha.14",
|
|
33
|
+
"@contember/engine-common": "2.2.0-alpha.14",
|
|
34
|
+
"@contember/engine-content-api": "2.2.0-alpha.14",
|
|
35
|
+
"@contember/engine-system-api": "2.2.0-alpha.14",
|
|
36
|
+
"@contember/engine-tenant-api": "2.2.0-alpha.14",
|
|
37
|
+
"@contember/graphql-utils": "2.2.0-alpha.14",
|
|
38
|
+
"@contember/logger": "2.2.0-alpha.14",
|
|
39
|
+
"@contember/schema": "2.2.0-alpha.14",
|
|
40
|
+
"@contember/schema-migrations": "2.2.0-alpha.14",
|
|
41
|
+
"@contember/schema-utils": "2.2.0-alpha.14",
|
|
42
|
+
"@contember/telemetry": "2.2.0-alpha.14",
|
|
43
|
+
"@contember/typesafe": "2.2.0-alpha.14",
|
|
44
44
|
"@graphql-tools/merge": "^9.1.7",
|
|
45
45
|
"@graphql-tools/schema": "^10.0.31",
|
|
46
46
|
"@koa/cors": "^5.0.0",
|
package/src/MasterContainer.ts
CHANGED
|
@@ -38,6 +38,7 @@ import { PrometheusRegistryFactory } from './prometheus/PrometheusRegistryFactor
|
|
|
38
38
|
import { ProjectGroupContainerTelemetryHook } from './telemetry/ProjectGroupContainerTelemetryHook.js'
|
|
39
39
|
import { createProviders, Providers } from './providers.js'
|
|
40
40
|
import { SystemApiMiddlewareFactory, SystemGraphQLContextFactory, SystemGraphQLHandlerFactory } from './system/index.js'
|
|
41
|
+
import { createMigrationLockOptions } from './system/migrationLockOptions.js'
|
|
41
42
|
import { ContentQueryExecutorImpl } from './system/ContentQueryExecutor.js'
|
|
42
43
|
import { OidcBackchannelLogoutMiddlewareFactory, TenantApiMiddlewareFactory, TenantGraphQLHandlerFactory } from './tenant/index.js'
|
|
43
44
|
import {
|
|
@@ -128,8 +129,8 @@ export class MasterContainerFactory {
|
|
|
128
129
|
)
|
|
129
130
|
.addService(
|
|
130
131
|
'systemContainerFactory',
|
|
131
|
-
({ providers, modificationHandlerFactory, contentQueryExecutor }) =>
|
|
132
|
-
new SystemContainerFactory(providers, modificationHandlerFactory, contentQueryExecutor),
|
|
132
|
+
({ providers, modificationHandlerFactory, contentQueryExecutor, serverConfig }) =>
|
|
133
|
+
new SystemContainerFactory(providers, modificationHandlerFactory, contentQueryExecutor, createMigrationLockOptions(serverConfig.systemApi)),
|
|
133
134
|
)
|
|
134
135
|
.addService('contentPermissionFactory', ({}) => new PermissionFactory())
|
|
135
136
|
.addService('databaseMetadataResolver', () => new DatabaseMetadataResolver())
|
|
@@ -111,6 +111,11 @@ const positiveInteger: Typesafe.Type<number> = (input, path = []) => {
|
|
|
111
111
|
return value > 0 ? value : Typesafe.fail(path, 'must be a positive integer')
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
+
const nonNegativeInteger: Typesafe.Type<number> = (input, path = []) => {
|
|
115
|
+
const value = Typesafe.integer(input, path)
|
|
116
|
+
return value >= 0 ? value : Typesafe.fail(path, 'must be a non-negative integer')
|
|
117
|
+
}
|
|
118
|
+
|
|
114
119
|
export const telemetryConfigSchema = Typesafe.partial({
|
|
115
120
|
resource: Typesafe.partial({
|
|
116
121
|
serviceName: Typesafe.string,
|
|
@@ -233,6 +238,25 @@ export const serverConfigSchema = Typesafe.partial({
|
|
|
233
238
|
return Typesafe.fail([])
|
|
234
239
|
},
|
|
235
240
|
}),
|
|
241
|
+
systemApi: Typesafe.transform(
|
|
242
|
+
Typesafe.partial({
|
|
243
|
+
// Opt-in: unset, a migration waits for its locks as long as PostgreSQL lets it and is never retried.
|
|
244
|
+
// Zero is rejected rather than passed on: PostgreSQL reads `lock_timeout = 0` as no limit.
|
|
245
|
+
migrationLockTimeoutMs: positiveInteger,
|
|
246
|
+
migrationMaxAttempts: positiveInteger,
|
|
247
|
+
migrationRetryDelayMs: nonNegativeInteger,
|
|
248
|
+
}),
|
|
249
|
+
(value, input, path) => {
|
|
250
|
+
const hasRetrySettings = value.migrationMaxAttempts !== undefined || value.migrationRetryDelayMs !== undefined
|
|
251
|
+
if (hasRetrySettings && value.migrationLockTimeoutMs === undefined) {
|
|
252
|
+
return Typesafe.fail(
|
|
253
|
+
path,
|
|
254
|
+
'migrationMaxAttempts and migrationRetryDelayMs require migrationLockTimeoutMs (CONTEMBER_SYSTEM_API_MIGRATION_LOCK_TIMEOUT_MS), without it migrations are not retried',
|
|
255
|
+
)
|
|
256
|
+
}
|
|
257
|
+
return value
|
|
258
|
+
},
|
|
259
|
+
),
|
|
236
260
|
contentApi: Typesafe.partial({
|
|
237
261
|
schemaCacheTtlSeconds: Typesafe.integer,
|
|
238
262
|
schemaCacheMaxEntries: (input: unknown, path: PropertyKey[] = []): number | undefined => {
|
|
@@ -103,6 +103,11 @@ export const configTemplate: any = {
|
|
|
103
103
|
enabled: '%?env.CONTEMBER_PANEL_ENABLED::bool%',
|
|
104
104
|
path: '%?env.CONTEMBER_PANEL_PATH%',
|
|
105
105
|
},
|
|
106
|
+
systemApi: {
|
|
107
|
+
migrationLockTimeoutMs: '%?env.CONTEMBER_SYSTEM_API_MIGRATION_LOCK_TIMEOUT_MS::number%',
|
|
108
|
+
migrationMaxAttempts: '%?env.CONTEMBER_SYSTEM_API_MIGRATION_MAX_ATTEMPTS::number%',
|
|
109
|
+
migrationRetryDelayMs: '%?env.CONTEMBER_SYSTEM_API_MIGRATION_RETRY_DELAY_MS::number%',
|
|
110
|
+
},
|
|
106
111
|
contentApi: {
|
|
107
112
|
schemaCacheTtlSeconds: '%?env.CONTEMBER_CONTENT_API_SCHEMA_CACHE_TTL_SECONDS::number%',
|
|
108
113
|
schemaCacheMaxEntries: '%?env.CONTEMBER_CONTENT_API_SCHEMA_CACHE_MAX_ENTRIES::number%',
|
|
@@ -41,7 +41,7 @@ export class GraphQlSchemaFactory {
|
|
|
41
41
|
const { root: permissions, all: allPermissions } = computedPermissions ?? this.permissionFactory.createContextual(schema, identity.projectRoles)
|
|
42
42
|
|
|
43
43
|
const customPrimary = schema.acl.customPrimary ?? false
|
|
44
|
-
const refreshMaterializedView =
|
|
44
|
+
const refreshMaterializedView = this.permissionFactory.isRefreshMaterializedViewAllowed(schema, identity.projectRoles)
|
|
45
45
|
// Types are shaped by the nested set, so a field granted only through a relation still exists
|
|
46
46
|
// on the shared type; the root query and mutation fields are gated on the root set alone.
|
|
47
47
|
const authorizator = new Authorizator(allPermissions, customPrimary, refreshMaterializedView)
|
|
@@ -110,7 +110,10 @@ export class GraphQlSchemaFactory {
|
|
|
110
110
|
return { permissionsKey: cachedKey }
|
|
111
111
|
}
|
|
112
112
|
const computedPermissions = this.permissionFactory.createContextual(schema, roles)
|
|
113
|
-
const
|
|
113
|
+
const refreshMaterializedView = this.permissionFactory.isRefreshMaterializedViewAllowed(schema, roles)
|
|
114
|
+
const permissionsKey = createHash('sha256')
|
|
115
|
+
.update(JSON.stringify([computedPermissions.root, computedPermissions.all, refreshMaterializedView]))
|
|
116
|
+
.digest('hex')
|
|
114
117
|
keysByRoles.set(rolesKey, permissionsKey)
|
|
115
118
|
return { permissionsKey, computedPermissions }
|
|
116
119
|
}
|
|
@@ -65,14 +65,13 @@ export class ProjectMembershipResolver {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
const explicitProjectRoles = explicitMemberships.map(it => it.role)
|
|
68
|
-
const
|
|
68
|
+
const implicitMemberships = implicitRoles
|
|
69
|
+
.filter(it => !explicitProjectRoles.includes(it))
|
|
70
|
+
.map((role): Acl.Membership => ({ role, variables: [] }))
|
|
69
71
|
|
|
70
72
|
return {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
...membershipResolver.resolve(acl, explicitMemberships, identity, false).memberships,
|
|
74
|
-
...implicitRolesToAssign.map(it => ({ role: it, variables: [] })),
|
|
75
|
-
],
|
|
73
|
+
// intentionally ignoring validation errors of stored memberships
|
|
74
|
+
effective: membershipResolver.resolve(acl, [...explicitMemberships, ...implicitMemberships], identity, false).memberships,
|
|
76
75
|
fetched: explicitMemberships,
|
|
77
76
|
}
|
|
78
77
|
}
|
|
@@ -88,14 +88,18 @@ export class ProjectContainerFactory {
|
|
|
88
88
|
if (!project.db.read) {
|
|
89
89
|
return connection
|
|
90
90
|
}
|
|
91
|
-
return Connection.create(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
91
|
+
return Connection.create(
|
|
92
|
+
{
|
|
93
|
+
...project.db,
|
|
94
|
+
...project.db.read,
|
|
95
|
+
pool: {
|
|
96
|
+
...project.db.pool,
|
|
97
|
+
...project.db.read.pool,
|
|
98
|
+
},
|
|
97
99
|
},
|
|
98
|
-
|
|
100
|
+
err => logger.error(err),
|
|
101
|
+
{ retryStaleConnection: true },
|
|
102
|
+
)
|
|
99
103
|
})
|
|
100
104
|
.addService('contentSchemaResolver', () => new ContentSchemaResolver(this.schemaProvider))
|
|
101
105
|
.addService('systemSchemaName', ({ project }) => project.db.systemSchema ?? 'system')
|
|
@@ -14,13 +14,14 @@ export type ProjectContainerResolverEvents = {
|
|
|
14
14
|
export class ProjectContainerResolver implements EventEmitter<ProjectContainerResolverEvents> {
|
|
15
15
|
private projectContainers = new ProjectContainerStore()
|
|
16
16
|
private eventManager = new EventManager<ProjectContainerResolverEvents>()
|
|
17
|
+
private disposed = false
|
|
17
18
|
|
|
18
19
|
public readonly on = this.eventManager.on.bind(this.eventManager)
|
|
19
20
|
|
|
20
21
|
constructor(
|
|
21
22
|
private readonly projectContainerFactory: ProjectContainerFactory,
|
|
22
23
|
private readonly projectConfigResolver: ProjectConfigResolver,
|
|
23
|
-
private readonly projectManager: ProjectManager,
|
|
24
|
+
private readonly projectManager: Pick<ProjectManager, 'getProjectState' | 'getProjectWithSecretsBySlug'>,
|
|
24
25
|
private readonly tenantDatabase: DatabaseContext,
|
|
25
26
|
private readonly tenantConfig: TenantConfig,
|
|
26
27
|
) {}
|
|
@@ -30,7 +31,7 @@ export class ProjectContainerResolver implements EventEmitter<ProjectContainerRe
|
|
|
30
31
|
if (realSlug) {
|
|
31
32
|
slug = realSlug
|
|
32
33
|
}
|
|
33
|
-
const existing = this.projectContainers.getContainer(slug)
|
|
34
|
+
const existing = this.disposed ? undefined : this.projectContainers.getContainer(slug)
|
|
34
35
|
if (existing) {
|
|
35
36
|
const existingAwaited = await existing
|
|
36
37
|
const state = await this.projectManager.getProjectState(this.tenantDatabase, slug, existingAwaited.timestamp)
|
|
@@ -47,19 +48,19 @@ export class ProjectContainerResolver implements EventEmitter<ProjectContainerRe
|
|
|
47
48
|
return undefined
|
|
48
49
|
}
|
|
49
50
|
const container = await this.createProjectContainer(project)
|
|
50
|
-
if (slug !== project.slug) {
|
|
51
|
+
if (slug !== project.slug && !this.disposed) {
|
|
51
52
|
this.projectContainers.setAlias(project.slug, slug)
|
|
52
53
|
}
|
|
53
54
|
return container
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
public async createProjectContainer(project: ProjectWithSecrets, logger?: Logger): Promise<ProjectContainer> {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
await
|
|
58
|
+
if (this.disposed) {
|
|
59
|
+
// A request that still holds the disposed group gets a container nobody tracks; its pool closes on idle timeout.
|
|
60
|
+
return await this.initializeContainer(project, logger)
|
|
61
|
+
}
|
|
62
|
+
return (await this.projectContainers.fetchContainer(project.slug, async () => {
|
|
63
|
+
const projectContainer = await this.initializeContainer(project, logger)
|
|
63
64
|
|
|
64
65
|
const cleanups = this.eventManager.fire('create', { container: projectContainer }).map(it => it ?? (() => null))
|
|
65
66
|
return {
|
|
@@ -70,6 +71,23 @@ export class ProjectContainerResolver implements EventEmitter<ProjectContainerRe
|
|
|
70
71
|
})).container
|
|
71
72
|
}
|
|
72
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Destroys all cached containers. Containers requested afterwards are neither cached nor announced to 'create' listeners.
|
|
76
|
+
*/
|
|
77
|
+
public async dispose(): Promise<void> {
|
|
78
|
+
this.disposed = true
|
|
79
|
+
await Promise.allSettled(this.projectContainers.getSlugs().map(slug => this.destroyContainer(slug)))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private async initializeContainer(project: ProjectWithSecrets, logger?: Logger): Promise<ProjectContainer> {
|
|
83
|
+
const projectConfig = this.projectConfigResolver(project.slug, project.config, project.secrets, this.tenantConfig)
|
|
84
|
+
const projectContainer = this.projectContainerFactory.createContainer({
|
|
85
|
+
project: projectConfig,
|
|
86
|
+
})
|
|
87
|
+
await projectContainer.projectInitializer.initialize(logger ?? projectContainer.logger)
|
|
88
|
+
return projectContainer
|
|
89
|
+
}
|
|
90
|
+
|
|
73
91
|
public async destroyContainer(slug: string): Promise<void> {
|
|
74
92
|
const existing = this.projectContainers.getContainer(slug)
|
|
75
93
|
if (existing) {
|
|
@@ -27,6 +27,10 @@ export class ProjectContainerStore {
|
|
|
27
27
|
return await this.containers.fetch(slug, factory)
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
public getSlugs(): string[] {
|
|
31
|
+
return Array.from(this.containers.keys())
|
|
32
|
+
}
|
|
33
|
+
|
|
30
34
|
public removeContainer(slug: string): void {
|
|
31
35
|
this.containers.delete(slug)
|
|
32
36
|
}
|
|
@@ -93,14 +93,18 @@ export class ProjectGroupContainerFactory {
|
|
|
93
93
|
if (!tenantDbCredentials.read) {
|
|
94
94
|
return tenantConnection
|
|
95
95
|
}
|
|
96
|
-
return Connection.create(
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
96
|
+
return Connection.create(
|
|
97
|
+
{
|
|
98
|
+
...tenantDbCredentials,
|
|
99
|
+
...tenantDbCredentials.read,
|
|
100
|
+
pool: {
|
|
101
|
+
...tenantDbCredentials.pool,
|
|
102
|
+
...tenantDbCredentials.read.pool,
|
|
103
|
+
},
|
|
102
104
|
},
|
|
103
|
-
|
|
105
|
+
err => logger.error(err),
|
|
106
|
+
{ retryStaleConnection: true },
|
|
107
|
+
)
|
|
104
108
|
})
|
|
105
109
|
.addService('projectSchemaResolver', () => new ProjectSchemaResolverProxy())
|
|
106
110
|
.addService('projectInitializer', () => new ProjectInitializerProxy())
|
|
@@ -39,10 +39,7 @@ export class ProjectGroupContainerResolver implements EventEmitter<ProjectGroupC
|
|
|
39
39
|
slug,
|
|
40
40
|
})
|
|
41
41
|
const cleanups = this.eventManager.fire('create', { container, slug }).map(it => it ?? (() => null))
|
|
42
|
-
|
|
43
|
-
container.projectContainerResolver.on('create', ({ container: projectContainer }) => {
|
|
44
|
-
cleanups.push(() => container.projectContainerResolver.destroyContainer(projectContainer.project.slug))
|
|
45
|
-
})
|
|
42
|
+
cleanups.push(() => container.projectContainerResolver.dispose())
|
|
46
43
|
|
|
47
44
|
await container.tenantContainer.migrationsRunner.run(container.logger.child())
|
|
48
45
|
return { container, inputConfig: config, cleanups }
|
|
@@ -92,7 +92,7 @@ const createDbPoolMetricsCollector = (registry: prom.Registry) => {
|
|
|
92
92
|
const labels = entry.labels
|
|
93
93
|
const poolInfo = db.getPoolStatus()
|
|
94
94
|
if (!poolInfo) {
|
|
95
|
-
|
|
95
|
+
continue
|
|
96
96
|
}
|
|
97
97
|
totalCount.set(labels, poolInfo.active + poolInfo.idle + poolInfo.connecting)
|
|
98
98
|
idleCount.set(labels, poolInfo.idle)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MigrationLockOptions } from '@contember/engine-system-api'
|
|
2
|
+
import { ServerConfig } from '../config/config.js'
|
|
3
|
+
|
|
4
|
+
export const createMigrationLockOptions = (config: ServerConfig['systemApi']): MigrationLockOptions | undefined => {
|
|
5
|
+
if (config?.migrationLockTimeoutMs === undefined) {
|
|
6
|
+
return undefined
|
|
7
|
+
}
|
|
8
|
+
return {
|
|
9
|
+
lockTimeoutMs: config.migrationLockTimeoutMs,
|
|
10
|
+
maxAttempts: config.migrationMaxAttempts ?? 5,
|
|
11
|
+
retryDelayMs: config.migrationRetryDelayMs ?? 1000,
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -3,7 +3,15 @@ import { HttpErrorResponse } from '../common/index.js'
|
|
|
3
3
|
import { fromBuffer } from './CommandStream.js'
|
|
4
4
|
import { ImportError, ImportExecutor } from './ImportExecutor.js'
|
|
5
5
|
import { createGunzip } from 'node:zlib'
|
|
6
|
+
import { pipeline, Readable } from 'node:stream'
|
|
6
7
|
import { ProjectGroupResolver } from '../projectGroup/ProjectGroupResolver.js'
|
|
8
|
+
import { Command } from './Command.js'
|
|
9
|
+
|
|
10
|
+
export const readImportCommands = (body: Readable, isGzip: boolean): AsyncIterable<Command> => {
|
|
11
|
+
// pipe() would not pass on an aborted request, and the import would wait for the rest of the body forever;
|
|
12
|
+
// pipeline() destroys the gunzip with the error, which rejects the read. The callback only satisfies the API.
|
|
13
|
+
return fromBuffer(isGzip ? pipeline(body, createGunzip(), () => {}) : body)
|
|
14
|
+
}
|
|
7
15
|
|
|
8
16
|
export class ImportApiMiddlewareFactory {
|
|
9
17
|
constructor(
|
|
@@ -32,7 +40,7 @@ export class ImportApiMiddlewareFactory {
|
|
|
32
40
|
|
|
33
41
|
try {
|
|
34
42
|
const isGzip = request.headers['content-encoding'] === 'gzip'
|
|
35
|
-
const commands =
|
|
43
|
+
const commands = readImportCommands(request.req, isGzip)
|
|
36
44
|
await this.importExecutor.import(projectGroup, authResult, commands)
|
|
37
45
|
response.status = 200
|
|
38
46
|
response.body = { ok: true }
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test'
|
|
2
|
+
import { Connection, EventManager } from '@contember/database'
|
|
3
|
+
import { Registry } from 'prom-client'
|
|
4
|
+
import { createMock } from '../../utils.js'
|
|
5
|
+
import { createDbMetricsRegistrar } from '../../../src/prometheus/dbMetrics.js'
|
|
6
|
+
|
|
7
|
+
const labels = (project: string) => ({
|
|
8
|
+
contember_module: 'content',
|
|
9
|
+
contember_project: project,
|
|
10
|
+
contember_project_group: 'group',
|
|
11
|
+
database_instance: 'single',
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
test('a pool without status does not hide metrics of the other pools', async () => {
|
|
15
|
+
const registry = new Registry()
|
|
16
|
+
const register = createDbMetricsRegistrar(registry)
|
|
17
|
+
register({
|
|
18
|
+
connection: createMock<Connection.PoolStatusProvider & Connection.Queryable>({
|
|
19
|
+
getPoolStatus: () => undefined,
|
|
20
|
+
eventManager: new EventManager(null),
|
|
21
|
+
query: () => {
|
|
22
|
+
throw new Error('Unused test dependency')
|
|
23
|
+
},
|
|
24
|
+
}),
|
|
25
|
+
labels: labels('without-status'),
|
|
26
|
+
})
|
|
27
|
+
register({
|
|
28
|
+
connection: Connection.create(
|
|
29
|
+
{ host: 'localhost', port: 5432, user: 'test', password: 'test', database: 'test', pool: { maxConnections: 7 } },
|
|
30
|
+
() => {},
|
|
31
|
+
),
|
|
32
|
+
labels: labels('with-status'),
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
const maxCount = (await registry.metrics()).split('\n').filter(it => it.startsWith('contember_db_pool_max_count{'))
|
|
36
|
+
|
|
37
|
+
expect(maxCount).toEqual([
|
|
38
|
+
'contember_db_pool_max_count{contember_module="content",contember_project="with-status",contember_project_group="group",database_instance="single"} 7',
|
|
39
|
+
])
|
|
40
|
+
})
|
|
@@ -85,3 +85,46 @@ test('a contributor cache key separates role combinations with identical permiss
|
|
|
85
85
|
|
|
86
86
|
expect(create(factory, ['editor'])).not.toBe(create(factory, ['admin']))
|
|
87
87
|
})
|
|
88
|
+
|
|
89
|
+
const stats: Model.Entity = {
|
|
90
|
+
name: 'Stats',
|
|
91
|
+
primary: 'id',
|
|
92
|
+
primaryColumn: 'id',
|
|
93
|
+
tableName: 'stats',
|
|
94
|
+
unique: [],
|
|
95
|
+
indexes: [],
|
|
96
|
+
eventLog: { enabled: false },
|
|
97
|
+
view: { sql: 'SELECT 1', materialized: true },
|
|
98
|
+
fields: {
|
|
99
|
+
id: { name: 'id', columnName: 'id', type: Model.ColumnType.Uuid, columnType: 'uuid', nullable: false },
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const readStats: Acl.Permissions = { Stats: { predicates: {}, operations: { read: { id: true } } } }
|
|
104
|
+
|
|
105
|
+
const materializedViewSchema: Schema = {
|
|
106
|
+
...emptySchema,
|
|
107
|
+
model: { entities: { Stats: stats }, enums: {} },
|
|
108
|
+
acl: {
|
|
109
|
+
roles: {
|
|
110
|
+
reader: role(readStats),
|
|
111
|
+
refresher: { ...role(readStats), content: { refreshMaterializedView: true } },
|
|
112
|
+
inheritedRefresher: { ...role(readStats), inherits: ['refresher'] },
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const hasRefreshMutation = (factory: GraphQlSchemaFactory, projectRoles: string[]) =>
|
|
118
|
+
factory.create(materializedViewSchema, { projectRoles }, project).schema.getMutationType()?.getFields().refreshMaterializedView !== undefined
|
|
119
|
+
|
|
120
|
+
test('a role without the refresh permission cannot refresh a materialized view another role may refresh', () => {
|
|
121
|
+
const factory = createFactory()
|
|
122
|
+
|
|
123
|
+
expect(hasRefreshMutation(factory, ['reader'])).toBe(false)
|
|
124
|
+
expect(hasRefreshMutation(factory, ['refresher'])).toBe(true)
|
|
125
|
+
expect(hasRefreshMutation(factory, ['reader'])).toBe(false)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('the refresh permission is inherited', () => {
|
|
129
|
+
expect(hasRefreshMutation(createFactory(), ['inheritedRefresher'])).toBe(true)
|
|
130
|
+
})
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createServer, request } from 'node:http'
|
|
2
|
+
import { gzipSync } from 'node:zlib'
|
|
3
|
+
import { readImportCommands } from '../../../src/transfer/ImportApiMiddlewareFactory.js'
|
|
4
|
+
|
|
5
|
+
// Driven by importClientAbort.test.ts: uploads an import, whole or half of it and then drops the connection,
|
|
6
|
+
// and prints how reading the commands ended — COMPLETED, REJECTED, or HANGING when it neither ends nor fails.
|
|
7
|
+
const isGzip = process.argv[2] === 'gzip'
|
|
8
|
+
const dropsConnection = process.argv[3] === 'abort'
|
|
9
|
+
const ndjson = Buffer.from(
|
|
10
|
+
Array.from({ length: 2000 }, (_, i) => JSON.stringify(['insertRow', [i, 'x'.repeat(100)]]) + '\n').join(''),
|
|
11
|
+
)
|
|
12
|
+
const body = isGzip ? gzipSync(ndjson, { level: 0 }) : ndjson
|
|
13
|
+
|
|
14
|
+
const finish = (outcome: string) => {
|
|
15
|
+
console.log(outcome)
|
|
16
|
+
process.exit(0)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const server = createServer(async req => {
|
|
20
|
+
try {
|
|
21
|
+
for await (const _ of readImportCommands(req, isGzip)) {
|
|
22
|
+
}
|
|
23
|
+
finish('COMPLETED')
|
|
24
|
+
} catch {
|
|
25
|
+
finish('REJECTED')
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
server.listen(0, '127.0.0.1', () => {
|
|
30
|
+
const address = server.address()
|
|
31
|
+
if (address === null || typeof address === 'string') {
|
|
32
|
+
throw new Error('The server does not listen on a TCP port')
|
|
33
|
+
}
|
|
34
|
+
const client = request({ host: '127.0.0.1', port: address.port, method: 'POST', headers: { 'Content-Length': body.length } })
|
|
35
|
+
client.on('error', () => {})
|
|
36
|
+
if (dropsConnection) {
|
|
37
|
+
client.write(body.subarray(0, body.length / 2))
|
|
38
|
+
setTimeout(() => client.destroy(), 100)
|
|
39
|
+
} else {
|
|
40
|
+
client.end(body)
|
|
41
|
+
}
|
|
42
|
+
setTimeout(() => finish('HANGING'), 2000)
|
|
43
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
|
|
4
|
+
// A subprocess, because the happy-dom globals of the test preload keep an in-process server from seeing the abort.
|
|
5
|
+
const fixture = join(import.meta.dir, 'importClientAbort.fixture.ts')
|
|
6
|
+
|
|
7
|
+
const readUpload = async (encoding: 'gzip' | 'plain', upload: 'abort' | 'complete'): Promise<string> => {
|
|
8
|
+
const proc = Bun.spawn(['bun', '--conditions=typescript', fixture, encoding, upload], { stdout: 'pipe', stderr: 'inherit' })
|
|
9
|
+
const output = await new Response(proc.stdout).text()
|
|
10
|
+
await proc.exited
|
|
11
|
+
return output.trim()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
test('stops reading a gzip import when the client drops the connection mid-upload', async () => {
|
|
15
|
+
expect(await readUpload('gzip', 'abort')).toBe('REJECTED')
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
test('stops reading a plain import when the client drops the connection mid-upload', async () => {
|
|
19
|
+
expect(await readUpload('plain', 'abort')).toBe('REJECTED')
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('reads a complete gzip import to the end', async () => {
|
|
23
|
+
expect(await readUpload('gzip', 'complete')).toBe('COMPLETED')
|
|
24
|
+
})
|