mirror of
https://github.com/docmost/docmost.git
synced 2026-05-07 06:23:06 +08:00
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { Hocuspocus, Server as HocuspocusServer } from '@hocuspocus/server';
|
|
import { IncomingMessage } from 'http';
|
|
import WebSocket from 'ws';
|
|
import { AuthenticationExtension } from './extensions/authentication.extension';
|
|
import { PersistenceExtension } from './extensions/persistence.extension';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { Redis } from '@hocuspocus/extension-redis';
|
|
import { EnvironmentService } from '../integrations/environment/environment.service';
|
|
import { createRetryStrategy, parseRedisUrl, RedisConfig } from '../helpers';
|
|
|
|
@Injectable()
|
|
export class CollaborationGateway {
|
|
private hocuspocus: Hocuspocus;
|
|
private redisConfig: RedisConfig;
|
|
|
|
constructor(
|
|
private authenticationExtension: AuthenticationExtension,
|
|
private persistenceExtension: PersistenceExtension,
|
|
private environmentService: EnvironmentService,
|
|
) {
|
|
this.redisConfig = parseRedisUrl(this.environmentService.getRedisUrl());
|
|
|
|
this.hocuspocus = HocuspocusServer.configure({
|
|
debounce: 5000,
|
|
maxDebounce: 10000,
|
|
unloadImmediately: false,
|
|
extensions: [
|
|
this.authenticationExtension,
|
|
this.persistenceExtension,
|
|
new Redis({
|
|
host: this.redisConfig.host,
|
|
port: this.redisConfig.port,
|
|
options: {
|
|
password: this.redisConfig.password,
|
|
retryStrategy: createRetryStrategy(),
|
|
},
|
|
}),
|
|
],
|
|
});
|
|
}
|
|
|
|
handleConnection(client: WebSocket, request: IncomingMessage): any {
|
|
this.hocuspocus.handleConnection(client, request);
|
|
}
|
|
|
|
async destroy(): Promise<void> {
|
|
await this.hocuspocus.destroy();
|
|
}
|
|
}
|