mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-02 18:09:58 +08:00
refactor(server): config system (#11081)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { IoAdapter } from '@nestjs/platform-socket.io';
|
||||
import { createAdapter } from '@socket.io/redis-adapter';
|
||||
import { Server } from 'socket.io';
|
||||
import { Server, Socket } from 'socket.io';
|
||||
|
||||
import { Config } from '../config';
|
||||
import { AuthenticationRequired } from '../error';
|
||||
@@ -14,7 +14,9 @@ export class SocketIoAdapter extends IoAdapter {
|
||||
}
|
||||
|
||||
override createIOServer(port: number, options?: any): Server {
|
||||
const config = this.app.get(WEBSOCKET_OPTIONS) as Config['websocket'];
|
||||
const config = this.app.get(WEBSOCKET_OPTIONS) as Config['websocket'] & {
|
||||
canActivate: (socket: Socket) => Promise<boolean>;
|
||||
};
|
||||
const server: Server = super.createIOServer(port, {
|
||||
...config,
|
||||
...options,
|
||||
@@ -22,7 +24,6 @@ export class SocketIoAdapter extends IoAdapter {
|
||||
|
||||
if (config.canActivate) {
|
||||
server.use((socket, next) => {
|
||||
// @ts-expect-error checked
|
||||
config
|
||||
.canActivate(socket)
|
||||
.then(pass => {
|
||||
|
||||
@@ -1,20 +1,34 @@
|
||||
import { GatewayMetadata } from '@nestjs/websockets';
|
||||
import { Socket } from 'socket.io';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { defineStartupConfig, ModuleConfig } from '../config';
|
||||
import { defineModuleConfig } from '../config';
|
||||
|
||||
declare module '../config' {
|
||||
interface AppConfig {
|
||||
websocket: ModuleConfig<
|
||||
GatewayMetadata & {
|
||||
canActivate?: (socket: Socket) => Promise<boolean>;
|
||||
}
|
||||
>;
|
||||
declare global {
|
||||
interface AppConfigSchema {
|
||||
websocket: {
|
||||
transports: ConfigItem<GatewayMetadata['transports']>;
|
||||
maxHttpBufferSize: number;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
defineStartupConfig('websocket', {
|
||||
transports: ['websocket', 'polling'],
|
||||
// see: https://socket.io/docs/v4/server-options/#maxhttpbuffersize
|
||||
maxHttpBufferSize: 1e8, // 100 MB
|
||||
defineModuleConfig('websocket', {
|
||||
transports: {
|
||||
desc: 'The enabled transports for accepting websocket traffics.',
|
||||
default: ['websocket', 'polling'],
|
||||
shape: z.array(z.enum(['websocket', 'polling'])),
|
||||
schema: {
|
||||
type: 'array',
|
||||
items: {
|
||||
type: 'string',
|
||||
enum: ['websocket', 'polling'],
|
||||
},
|
||||
},
|
||||
link: 'https://docs.nestjs.com/websockets/gateways#transports',
|
||||
},
|
||||
maxHttpBufferSize: {
|
||||
desc: 'How many bytes or characters a message can be, before closing the session (to avoid DoS).',
|
||||
default: 1e8, // 100 MB
|
||||
shape: z.number().int().positive(),
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user