refactor(server): standalone runtime module (#9120)

This commit is contained in:
forehalo
2024-12-13 06:27:14 +00:00
parent 4c23991047
commit 81c68032e1
18 changed files with 85 additions and 75 deletions

View File

@@ -20,6 +20,7 @@ import {
InternalServerError,
InvalidEmail,
InvalidEmailToken,
Runtime,
SignUpForbidden,
Throttle,
URLHelper,
@@ -57,7 +58,8 @@ export class AuthController {
private readonly auth: AuthService,
private readonly user: UserService,
private readonly token: TokenService,
private readonly config: Config
private readonly config: Config,
private readonly runtime: Runtime
) {
if (config.node.dev) {
// set DNS servers in dev mode
@@ -159,12 +161,12 @@ export class AuthController {
// send email magic link
const user = await this.user.findUserByEmail(email);
if (!user) {
const allowSignup = await this.config.runtime.fetch('auth/allowSignup');
const allowSignup = await this.runtime.fetch('auth/allowSignup');
if (!allowSignup) {
throw new SignUpForbidden();
}
const requireEmailDomainVerification = await this.config.runtime.fetch(
const requireEmailDomainVerification = await this.runtime.fetch(
'auth/requireEmailDomainVerification'
);
if (requireEmailDomainVerification) {

View File

@@ -12,7 +12,7 @@ import {
import { RuntimeConfig, RuntimeConfigType } from '@prisma/client';
import { GraphQLJSON, GraphQLJSONObject } from 'graphql-scalars';
import { Config, URLHelper } from '../../base';
import { Config, Runtime, URLHelper } from '../../base';
import { Public } from '../auth';
import { Admin } from '../common';
import { FeatureType } from '../features';
@@ -76,6 +76,7 @@ export class ServerFlagsType implements ServerFlags {
export class ServerConfigResolver {
constructor(
private readonly config: Config,
private readonly runtime: Runtime,
private readonly url: URLHelper,
private readonly server: ServerService
) {}
@@ -103,7 +104,7 @@ export class ServerConfigResolver {
description: 'credentials requirement',
})
async credentialsRequirement() {
const config = await this.config.runtime.fetchAll({
const config = await this.runtime.fetchAll({
'auth/password.max': true,
'auth/password.min': true,
});
@@ -120,7 +121,7 @@ export class ServerConfigResolver {
description: 'server flags',
})
async flags(): Promise<ServerFlagsType> {
const records = await this.config.runtime.list('flags');
const records = await this.runtime.list('flags');
return records.reduce((flags, record) => {
flags[record.key as keyof ServerFlagsType] = record.value as any;
@@ -184,13 +185,13 @@ interface ServerDatabaseConfig {
@Admin()
@Resolver(() => ServerRuntimeConfigType)
export class ServerRuntimeConfigResolver {
constructor(private readonly config: Config) {}
constructor(private readonly runtime: Runtime) {}
@Query(() => [ServerRuntimeConfigType], {
description: 'get all server runtime configurable settings',
})
serverRuntimeConfig(): Promise<ServerRuntimeConfigType[]> {
return this.config.runtime.list();
return this.runtime.list();
}
@Mutation(() => ServerRuntimeConfigType, {
@@ -200,7 +201,7 @@ export class ServerRuntimeConfigResolver {
@Args('id') id: string,
@Args({ type: () => GraphQLJSON, name: 'value' }) value: any
): Promise<ServerRuntimeConfigType> {
return await this.config.runtime.set(id as any, value);
return await this.runtime.set(id as any, value);
}
@Mutation(() => [ServerRuntimeConfigType], {
@@ -211,7 +212,7 @@ export class ServerRuntimeConfigResolver {
): Promise<ServerRuntimeConfigType[]> {
const keys = Object.keys(updates);
const results = await Promise.all(
keys.map(key => this.config.runtime.set(key as any, updates[key]))
keys.map(key => this.runtime.set(key as any, updates[key]))
);
return results;

View File

@@ -7,6 +7,7 @@ import {
Config,
mergeUpdatesInApplyWay as yotcoMergeUpdates,
metrics,
Runtime,
} from '../../base';
import { PermissionService } from '../permission';
import { QuotaService } from '../quota';
@@ -35,6 +36,7 @@ export class DocStorageOptions implements IDocStorageOptions {
constructor(
private readonly config: Config,
private readonly runtime: Runtime,
private readonly permission: PermissionService,
private readonly quota: QuotaService
) {}
@@ -43,9 +45,7 @@ export class DocStorageOptions implements IDocStorageOptions {
const doc = await this.recoverDoc(updates);
const yjsResult = Buffer.from(Y.encodeStateAsUpdate(doc));
const useYocto = await this.config.runtime.fetch(
'doc/experimentalMergeWithYOcto'
);
const useYocto = await this.runtime.fetch('doc/experimentalMergeWithYOcto');
if (useYocto) {
metrics.jwst.counter('codec_merge_counter').add(1);

View File

@@ -1,6 +1,6 @@
import { Injectable, Logger } from '@nestjs/common';
import { Config, type EventPayload, OnEvent } from '../../base';
import { type EventPayload, OnEvent, Runtime } from '../../base';
import { UserService } from '../user/service';
import { FeatureService } from './service';
import { FeatureType } from './types';
@@ -19,7 +19,7 @@ export class FeatureManagementService {
constructor(
private readonly feature: FeatureService,
private readonly user: UserService,
private readonly config: Config
private readonly runtime: Runtime
) {}
// ======== Admin ========
@@ -95,7 +95,7 @@ export class FeatureManagementService {
email: string,
type: EarlyAccessType = EarlyAccessType.App
) {
const earlyAccessControlEnabled = await this.config.runtime.fetch(
const earlyAccessControlEnabled = await this.runtime.fetch(
'flags/earlyAccessControl'
);

View File

@@ -12,11 +12,11 @@ import { Socket } from 'socket.io';
import {
AlreadyInSpace,
CallMetric,
Config,
DocNotFound,
GatewayErrorWrapper,
metrics,
NotInSpace,
Runtime,
SpaceAccessDenied,
VersionRejected,
} from '../../base';
@@ -139,7 +139,7 @@ export class SpaceSyncGateway
private connectionCount = 0;
constructor(
private readonly config: Config,
private readonly runtime: Runtime,
private readonly permissions: PermissionService,
private readonly workspace: PgWorkspaceDocStorageAdapter,
private readonly userspace: PgUserspaceDocStorageAdapter
@@ -175,7 +175,7 @@ export class SpaceSyncGateway
}
async assertVersion(client: Socket, version?: string) {
const shouldCheckClientVersion = await this.config.runtime.fetch(
const shouldCheckClientVersion = await this.runtime.fetch(
'flags/syncClientVersionCheck'
);
if (

View File

@@ -8,6 +8,7 @@ import {
EventEmitter,
type EventPayload,
OnEvent,
Runtime,
WrongSignInCredentials,
WrongSignInMethod,
} from '../../base';
@@ -33,6 +34,7 @@ export class UserService {
constructor(
private readonly config: Config,
private readonly runtime: Runtime,
private readonly crypto: CryptoHelper,
private readonly prisma: PrismaClient,
private readonly emitter: EventEmitter,
@@ -60,7 +62,7 @@ export class UserService {
validators.assertValidEmail(data.email);
if (data.password) {
const config = await this.config.runtime.fetchAll({
const config = await this.runtime.fetchAll({
'auth/password.max': true,
'auth/password.min': true,
});
@@ -242,7 +244,7 @@ export class UserService {
select: Prisma.UserSelect = this.defaultUserSelect
) {
if (data.password) {
const config = await this.config.runtime.fetchAll({
const config = await this.runtime.fetchAll({
'auth/password.max': true,
'auth/password.min': true,
});