feat(server): use doc service (#9967)

close CLOUD-94
This commit is contained in:
fengmk2
2025-02-08 05:27:56 +00:00
parent ee0df52531
commit e5d1cd9ea2
17 changed files with 423 additions and 127 deletions

View File

@@ -1,5 +1,3 @@
import { randomUUID } from 'node:crypto';
import {
applyDecorators,
Injectable,
@@ -21,6 +19,7 @@ import { CLS_ID, ClsService } from 'nestjs-cls';
import type { Server, Socket } from 'socket.io';
import { CallMetric } from '../metrics';
import { genRequestId } from '../utils';
import type { EventName } from './def';
const EventHandlerWrapper = (event: EventName): MethodDecorator => {
@@ -94,7 +93,7 @@ export class EventBus implements OnGatewayConnection, OnApplicationBootstrap {
// to internal event system
this.server?.on(event, (payload, requestId?: string) => {
this.cls.run(() => {
requestId = requestId ?? `server_event-${randomUUID()}`;
requestId = requestId ?? genRequestId('se');
this.cls.set(CLS_ID, requestId);
this.logger.log(`Server Event: ${event} (Received)`);
this.emit(event, payload);

View File

@@ -1,3 +1,4 @@
import { randomUUID } from 'node:crypto';
import { IncomingMessage } from 'node:http';
import type { ArgumentsHost, ExecutionContext } from '@nestjs/common';
@@ -77,3 +78,18 @@ export function parseCookies(
{} as Record<string, string>
);
}
/**
* Request type
*
* @description
* - `req`: http request
* - `ws`: websocket request
* - `se`: server event
* - `job`: cron job
*/
export type RequestType = 'req' | 'ws' | 'se' | 'job';
export function genRequestId(type: RequestType) {
return `${AFFiNE.flavor.type}:${type}-${randomUUID()}`;
}