Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bc 4256 integration tldraw #4361

Closed
wants to merge 168 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
168 commits
Select commit Hold shift + click to select a range
2dda3e7
Init module tldraw
Jun 20, 2023
860e585
Init commit, add tldraw websocket module
Jun 29, 2023
e8a29ec
Merge branch 'main' into BC-4201-planning-impolementation
Jun 30, 2023
b3c1b53
Update package-lock.json
Jun 30, 2023
f39abca
add yml file to main yml
Jun 30, 2023
29d597f
delete probes in tldraw yml
Jun 30, 2023
a97da82
Change socket.io to WebSocket
Jul 3, 2023
ec32f16
Adjusting code to websockets
Jul 4, 2023
047e870
Update const variables and unit file
Jul 6, 2023
a6410de
Merge branch 'main' into BC-4201-planning-impolementation
Jul 6, 2023
48fc70e
Fix tests.
Jul 6, 2023
4b71c4a
Merge branch 'main' into BC-4201-planning-impolementation
Jul 11, 2023
a78fd9a
update of packages after merging with main
Jul 11, 2023
93ddafd
fix exports, add env vars imports, delete unnecessary code
Jul 12, 2023
9a5bdb2
Change response callback to axios
Jul 13, 2023
bb75a6a
Remove doubled functionality
Jul 14, 2023
1d6ae7a
Fix utils lint and code style
Jul 17, 2023
aae1b3b
Fix utils lint and code style
Jul 17, 2023
ab201ae
Fix lint and code style
Jul 18, 2023
35b4941
Get rid of unnecessary var
Jul 18, 2023
3c78b47
Improve import and global var
Jul 18, 2023
4786e15
Fix config url setting, tidy modules import
Jul 19, 2023
306d8b6
Fix config url setting, tidy modules import
Jul 19, 2023
fd24b34
Add checking for docName in url
Jul 20, 2023
ed433be
Add allowed licence, fix tests, fix suggested changes
Jul 20, 2023
ecd8a5f
Move connection string to config file, delete unnecessary export.
Jul 20, 2023
73940db
Clear constructor and connection interval code.
Jul 21, 2023
25a559f
Change licence allow list
Jul 21, 2023
9695c5a
Remove lint errors
Jul 24, 2023
c6c075a
Adjust websocket port for testing.
Jul 24, 2023
957c83e
Delete unusable code
Jul 25, 2023
2a37276
Improve tests
Jul 25, 2023
b88925b
Improve tests
Jul 25, 2023
bb3de3d
Merge branch 'main' into BC-4201-planning-impolementation
Jul 26, 2023
f564943
Improve tests
Jul 27, 2023
bf4a5fb
Improve tests
Jul 28, 2023
779a1bc
Get rid of code smell, check if last test is bad (commenting it)
Jul 28, 2023
18681a7
Change app listen to init in tests,create svc for tldraw in server side
Jul 28, 2023
c9bf0d8
Fix typing of tldraw config
Jul 31, 2023
f643ed0
Fix typing in gateway of mongo persistence
Jul 31, 2023
b519dce
Fix test by using fake timer
Aug 1, 2023
a69995e
Deleting setInterval test
Aug 2, 2023
9a80ae4
Change yml main to BC-4256-Integration-tldraw.
Aug 2, 2023
47bb9e2
Add tests for updateHandler and closing connection in send method.
Aug 3, 2023
427fd76
Delete unreachable Error
Aug 4, 2023
4670a6e
Merge remote-tracking branch 'origin/main' into BC-4256-Integration-t…
Aug 4, 2023
94c1096
Fix packages
Aug 4, 2023
147bac2
Delete listen tldraw server port.
Aug 4, 2023
75594cb
Change tldraw env vars into group. Change beforeEach in test to befor…
Aug 4, 2023
23cd0ca
Delete annotations of port and scrape for tldraw
Aug 4, 2023
02785cf
Change container port in tldraw-deployment.yml.j2
Aug 4, 2023
91c5431
Hard code port as suggested
Aug 4, 2023
e735058
Change env var default definition
Aug 4, 2023
56c9aea
Change test test app life cycle
Aug 4, 2023
8e3d661
Add Error handling
Aug 4, 2023
80f0b73
fix tldraw test environment
davwas Aug 10, 2023
19b9796
fix tldraw.app.ts file
davwas Aug 10, 2023
62ff413
fix tldraw.app.ts file #2
davwas Aug 10, 2023
8d740f2
fix tldraw.app.ts file #3
davwas Aug 10, 2023
9b09f8a
Merge branch 'main' into BC-4256-Integration-tldraw
davwas Aug 10, 2023
10b40c2
Merge branch 'main' into BC-4256-Integration-tldraw
davwas Aug 10, 2023
f0214f1
fix package lock json
davwas Aug 10, 2023
fc77f7d
fix packages
davwas Aug 10, 2023
7aab343
fix dependency license problem
davwas Aug 10, 2023
7c1ca24
Revert "fix dependency license problem"
davwas Aug 10, 2023
0318faa
add tests
davwas Aug 10, 2023
f5faed5
remove unused imports
davwas Aug 10, 2023
b83e095
fix tldraw app start
davwas Aug 10, 2023
aca59e6
fix dependency review file
davwas Aug 10, 2023
c59e0c9
Squashed minor test changes, one main merge, and sonar settings changes
Aug 28, 2023
e0658e7
Check git remote logs.
Aug 28, 2023
728fd6b
Check git remote logs. (Revert changes)
blazejpass Aug 28, 2023
4e941ce
Merge branch 'main' into BC-4256-Integration-tldraw
blazejpass Aug 29, 2023
9c3a5ee
Merge main and resolve conflicts
blazejpass Aug 29, 2023
a8503eb
add unit tests
Aug 29, 2023
5aac216
fix unit tests
Aug 29, 2023
b9fc8bd
fix unit tests
Aug 29, 2023
a0bccbb
add unit test
Aug 30, 2023
b1ad383
add unit tests
Aug 30, 2023
faf6015
fix tests
Aug 30, 2023
dfbf5fb
fix tests
Aug 31, 2023
bf77f9f
extend tests timeout
Aug 31, 2023
9a7f895
add some unit tests
Aug 31, 2023
b3fddd4
add unit tests
Aug 31, 2023
045b9c2
fix test
Aug 31, 2023
ad55a8f
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 1, 2023
80bc773
tests refactor
Sep 1, 2023
42aeac3
resolve comments v1
Sep 4, 2023
cbf415f
resolve comments v2
Sep 4, 2023
761f224
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 4, 2023
eee4fdc
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 6, 2023
a717448
restore ydoc on update
Sep 6, 2023
6e89051
fix test
Sep 6, 2023
bc862a3
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 6, 2023
a66cba0
code refactor
Sep 6, 2023
f8643c4
add missing lne
Sep 7, 2023
b5db76b
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 7, 2023
f4841bf
add unit test
Sep 18, 2023
4ff48ef
Merge branch 'main' into BC-4256-Integration-tldraw
Sep 19, 2023
3521378
update packages versions
Sep 19, 2023
3115c4e
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 19, 2023
564f929
Adjust packages after merge
blazejpass Sep 19, 2023
f471d00
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 20, 2023
465bbbe
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 20, 2023
9d02852
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 20, 2023
eab54dc
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 20, 2023
1ca757e
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 20, 2023
07276e4
fix license
Sep 20, 2023
d8ed180
update package version
Sep 20, 2023
b0be792
update package-lock.json
Sep 20, 2023
e8d6d0d
fix tests
Sep 20, 2023
ff4b97f
code refactor
Sep 20, 2023
69da678
fix lint
Sep 20, 2023
e22b43a
add unit test
Sep 20, 2023
acc7f1f
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 21, 2023
310cb47
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 21, 2023
24b61bf
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 21, 2023
2a02a5b
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 21, 2023
0af7104
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 22, 2023
3bc3cdc
fix type
Sep 22, 2023
fbd698b
code refactor:
Sep 25, 2023
b966167
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 25, 2023
8326300
code refactor
Sep 25, 2023
a4784bf
refactor tests
Sep 25, 2023
97f5b6c
Change naming tldraw controller
blazejpass Sep 25, 2023
23129c0
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 26, 2023
f4c522b
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 27, 2023
823ba51
code refactor
Sep 28, 2023
439f5e7
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Sep 28, 2023
1cc735a
Change module imports, db address config data, improwe sending data v…
blazejpass Oct 3, 2023
de7b6f9
Delete uni tests for gateway/ws file
blazejpass Oct 3, 2023
b454c4c
Change db tldraw url configuration
blazejpass Oct 3, 2023
9126b04
Add Service to testing module
blazejpass Oct 3, 2023
5f8127c
Add Service to testing module
blazejpass Oct 3, 2023
8b8d7e5
Add Service to testing module
blazejpass Oct 3, 2023
635281f
Add repo tests
blazejpass Oct 5, 2023
b844426
Add Shared Doc tests
blazejpass Oct 6, 2023
b262bf3
Add tldraw service tests
blazejpass Oct 6, 2023
dd329c8
Merge branch 'main' into BC-4256-Integration-tldraw
blazejpass Oct 9, 2023
206fe2d
Change push yml uses branch
blazejpass Oct 9, 2023
4cdc81c
Merge remote-tracking branch 'origin/BC-4256-Integration-tldraw' into…
blazejpass Oct 9, 2023
418e569
Merge branch 'main' into BC-4256-Integration-tldraw
blazejpass Oct 10, 2023
705d093
Change push yml uses branch
blazejpass Oct 10, 2023
a7d9beb
Change push yml uses branch
blazejpass Oct 11, 2023
d073aef
Delete TLDRAW_DB_TEST_URL
blazejpass Oct 12, 2023
6a90db1
Code improvements according to PR comments in review.
blazejpass Oct 16, 2023
3dd1f08
Move yml deployment and service files for tldraw to separate role folder
blazejpass Oct 16, 2023
752b403
Add descriptions to ws-shared-doc.do.ts methods
blazejpass Oct 17, 2023
3ef0637
Delete delay for test
blazejpass Oct 18, 2023
33a9726
Merge branch 'main' into BC-4256-Integration-tldraw
blazejpass Oct 18, 2023
015c300
Update package-lock after merging
blazejpass Oct 18, 2023
f11d39f
Improve tests after merge.
blazejpass Oct 18, 2023
b2fd397
Use tldraw-test-module, improve code
blazejpass Oct 20, 2023
7eecf87
Fix tests according to suggested comments.
blazejpass Oct 23, 2023
a9037cf
Change deployment yml to main
blazejpass Oct 25, 2023
336de77
Fix code according to comments
blazejpass Oct 25, 2023
7475bf5
Fix tests code according to comments. Move test helper
blazejpass Oct 25, 2023
9afa7b5
Check doff_app_deploy branch
blazejpass Nov 2, 2023
e1ddad5
Merge branch 'main' into BC-4256-Integration-tldraw
blazejpass Nov 2, 2023
d1b24cc
Change config to public in tldraw-board.repo.ts
blazejpass Nov 2, 2023
67e40a3
Merge branch 'main' into BC-4256-Integration-tldraw
blazejpass Nov 2, 2023
eef6243
fix mock doc object for tests
Nov 2, 2023
0420836
increase timeout
Nov 2, 2023
42d1221
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Nov 2, 2023
def15ff
update mongodb provider lib version
Nov 2, 2023
188c151
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Nov 3, 2023
c92e243
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek Nov 6, 2023
5f6fc6d
Merge branch 'main' into BC-4256-Integration-tldraw
Metauriel Nov 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ jobs:
- name: 'Dependency Review'
uses: actions/dependency-review-action@v3
with:
allow-licenses: AGPL-3.0-only, LGPL-3.0, MIT, Apache-2.0, BSD-2-Clause, BSD-3-Clause, ISC, X11, 0BSD, GPL-3.0, Unlicense
allow-licenses: AGPL-3.0-only, LGPL-3.0, MIT, Apache-2.0, BSD-2-Clause, BSD-3-Clause, ISC, X11, 0BSD, GPL-3.0, BSD-3-Clause AND BSD-3-Clause-Clear, Unlicense
allow-dependencies-licenses: 'pkg:npm/parse-mongo-url'
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ jobs:
needs:
- build_and_push
- branch_meta
uses: hpi-schul-cloud/dof_app_deploy/.github/workflows/deploy.yml@main
uses: hpi-schul-cloud/dof_app_deploy/.github/workflows/deploy.yml@BC-4256-Integration-tldraw
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be switch back to main, before merge

with:
branch: ${{ needs.branch_meta.outputs.branch }}
secrets:
Expand Down
9 changes: 9 additions & 0 deletions ansible/roles/schulcloud-server-tldraw/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
galaxy_info:
role_name: schulcloud-server-tldraw
author: Schul-Cloud Verbund
description: tldraw role for the schulcloud-server
company: Schul-Cloud Verbund
license: license (AGPLv3)
min_ansible_version: 2.8
galaxy_tags: []
dependencies: []
11 changes: 11 additions & 0 deletions ansible/roles/schulcloud-server-tldraw/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- name: TlDrawServerDeployment
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: api-tldraw-deployment.yml.j2

- name: TlDrawWsService
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: api-tldraw-svc.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-tldraw-deployment
namespace: {{ NAMESPACE }}
labels:
app: tldraw
spec:
replicas: {{ TLDRAW_EDITOR_REPLICAS|default("1", true) }}
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
#maxUnavailable: 1
revisionHistoryLimit: 4
paused: false
selector:
matchLabels:
app: tldraw
template:
metadata:
labels:
app: tldraw
spec:
securityContext:
runAsUser: 1000
runAsGroup: 1000
fsGroup: 1000
runAsNonRoot: true
containers:
- name: tldraw
image: {{ SCHULCLOUD_SERVER_IMAGE }}:{{ SCHULCLOUD_SERVER_IMAGE_TAG }}
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3345
name: tldraw
protocol: TCP
envFrom:
- configMapRef:
name: api-configmap
- secretRef:
name: api-secret
command: ['npm', 'run', 'nest:start:tldraw:prod']
resources:
limits:
cpu: {{ TLDRAW_EDITOR_CPU_LIMITS|default("2000m", true) }}
memory: {{ TLDRAW_EDITOR_MEMORY_LIMITS|default("500Mi", true) }}
requests:
cpu: {{ TLDRAW_EDITOR_CPU_REQUESTS|default("100m", true) }}
memory: {{ TLDRAW_EDITOR_MEMORY_REQUESTS|default("50Mi", true) }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: Service
metadata:
name: api-tldraw-svc
namespace: {{ NAMESPACE }}
labels:
app: tldraw
spec:
type: ClusterIP
ports:
- port: 3345
targetPort: 3345
protocol: TCP
name: tldraw
selector:
app: tldraw
31 changes: 31 additions & 0 deletions apps/server/src/apps/tldraw.app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* istanbul ignore file */
/* eslint-disable no-console */
import { NestFactory } from '@nestjs/core';
import { install as sourceMapInstall } from 'source-map-support';
import { TldrawModule } from '@src/modules/tldraw';
import { Logger } from '@src/core/logger';
import * as WebSocket from 'ws';
wiaderwek marked this conversation as resolved.
Show resolved Hide resolved
import { WsAdapter } from '@nestjs/platform-ws';
import { enableOpenApiDocs } from '@shared/controller/swagger';
import { AppStartLoggable } from '@src/apps/helpers/app-start-loggable';

async function bootstrap() {
sourceMapInstall();

const nestApp = await NestFactory.create(TldrawModule);
const wss = new WebSocket.Server({ noServer: true });
nestApp.useWebSocketAdapter(new WsAdapter(wss));
nestApp.enableCors();
enableOpenApiDocs(nestApp, 'docs');

const logger = await nestApp.resolve(Logger);
await nestApp.init();

logger.info(
new AppStartLoggable({
appName: 'Tldraw server app',
})
);
}

void bootstrap();
30 changes: 30 additions & 0 deletions apps/server/src/modules/tldraw/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Configuration } from '@hpi-schul-cloud/commons';

export interface TldrawConfig {
NEST_LOG_LEVEL: string;
INCOMING_REQUEST_TIMEOUT: number;
TLDRAW_DB_COLLECTION_NAME: string;
TLDRAW_DB_FLUSH_SIZE: string;
TLDRAW_DB_MULTIPLE_COLLECTIONS: boolean;
CONNECTION_STRING: string;
FEATURE_TLDRAW_ENABLED: boolean;
TLDRAW_PING_TIMEOUT: number;
TLDRAW_GC_ENABLED: number;
}

const tldrawConnectionString: string = Configuration.get('TLDRAW_DB_URL') as string;

const tldrawConfig = {
NEST_LOG_LEVEL: Configuration.get('NEST_LOG_LEVEL') as string,
INCOMING_REQUEST_TIMEOUT: Configuration.get('INCOMING_REQUEST_TIMEOUT_API') as number,
TLDRAW_DB_COLLECTION_NAME: Configuration.get('TLDRAW__DB_COLLECTION_NAME') as string,
TLDRAW_DB_FLUSH_SIZE: Configuration.get('TLDRAW__DB_FLUSH_SIZE') as number,
TLDRAW_DB_MULTIPLE_COLLECTIONS: Configuration.get('TLDRAW__DB_MULTIPLE_COLLECTIONS') as boolean,
FEATURE_TLDRAW_ENABLED: Configuration.get('FEATURE_TLDRAW_ENABLED') as boolean,
CONNECTION_STRING: tldrawConnectionString,
TLDRAW_PING_TIMEOUT: Configuration.get('TLDRAW__PING_TIMEOUT') as number,
TLDRAW_GC_ENABLED: Configuration.get('TLDRAW__GC_ENABLED') as boolean,
};

export const SOCKET_PORT = Configuration.get('TLDRAW__SOCKET_PORT') as number;
export const config = () => tldrawConfig;
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { WsAdapter } from '@nestjs/platform-ws';
import { Test } from '@nestjs/testing';
import WebSocket from 'ws';
import { TextEncoder } from 'util';
import { INestApplication } from '@nestjs/common';
import { TldrawTestModule } from '../..';
import { TldrawWs } from '../tldraw.ws';
import { TestConnection } from '../../testing/test-connection';

describe('WebSocketController (WsAdapter)', () => {
blazejpass marked this conversation as resolved.
Show resolved Hide resolved
let app: INestApplication;
let gateway: TldrawWs;
let ws: WebSocket;

const gatewayPort = 3346;
const wsUrl = TestConnection.getWsUrl(gatewayPort);
const clientMessageMock = 'test-message';

const getMessage = () => new TextEncoder().encode(clientMessageMock);

beforeAll(async () => {
const testingModule = await Test.createTestingModule({
imports: [TldrawTestModule],
}).compile();
gateway = testingModule.get<TldrawWs>(TldrawWs);
app = testingModule.createNestApplication();
app.useWebSocketAdapter(new WsAdapter(app));
await app.init();
});

afterAll(async () => {
await app.close();
});

beforeEach(() => {
jest.useFakeTimers({ advanceTimers: true, doNotFake: ['setInterval', 'clearInterval', 'setTimeout'] });
});

afterEach(() => {
jest.clearAllMocks();
});

describe('when tldraw is correctly setup', () => {
const setup = async () => {
const handleConnectionSpy = jest.spyOn(gateway, 'handleConnection');
jest.spyOn(Uint8Array.prototype, 'reduce').mockReturnValueOnce(1);

ws = await TestConnection.setupWs(wsUrl, 'TEST');

const { buffer } = getMessage();

return { handleConnectionSpy, buffer };
};

it(`should handle connection and data transfer`, async () => {
const { handleConnectionSpy, buffer } = await setup();
ws.send(buffer, () => {});

expect(handleConnectionSpy).toHaveBeenCalledTimes(1);
blazejpass marked this conversation as resolved.
Show resolved Hide resolved
ws.close();
});

it(`check if client will receive message`, async () => {
const { buffer } = await setup();
ws.send(buffer, () => {});

gateway.server.on('connection', (client) => {
client.on('message', (payload) => {
expect(payload).toBeInstanceOf(ArrayBuffer);
});
});

ws.close();
});
});

describe('when tldraw doc has multiple clients', () => {
const setup = async () => {
const handleConnectionSpy = jest.spyOn(gateway, 'handleConnection');
ws = await TestConnection.setupWs(wsUrl, 'TEST2');
const ws2 = await TestConnection.setupWs(wsUrl, 'TEST2');

const { buffer } = getMessage();

return {
handleConnectionSpy,
ws2,
buffer,
};
};

it(`should handle 2 connections at same doc and data transfer`, async () => {
const { handleConnectionSpy, ws2, buffer } = await setup();
ws.send(buffer);
ws2.send(buffer);

expect(handleConnectionSpy).toHaveBeenCalled();
expect(handleConnectionSpy).toHaveBeenCalledTimes(2);

ws.close();
ws2.close();
});
});

describe('when tldraw is not correctly setup', () => {
const setup = async () => {
const handleConnectionSpy = jest.spyOn(gateway, 'handleConnection');

ws = await TestConnection.setupWs(wsUrl);

return {
handleConnectionSpy,
};
};

it(`should refuse connection if there is no docName`, async () => {
const { handleConnectionSpy } = await setup();

const { buffer } = getMessage();
ws.send(buffer);

expect(gateway.server).toBeDefined();
expect(handleConnectionSpy).toHaveBeenCalled();
expect(handleConnectionSpy).toHaveBeenCalledTimes(1);

ws.close();
});
});
});
1 change: 1 addition & 0 deletions apps/server/src/modules/tldraw/controller/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './tldraw.ws';
48 changes: 48 additions & 0 deletions apps/server/src/modules/tldraw/controller/tldraw.ws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { WebSocketGateway, WebSocketServer, OnGatewayInit, OnGatewayConnection } from '@nestjs/websockets';
import { Server, WebSocket } from 'ws';
import { ConfigService } from '@nestjs/config';
import { TldrawConfig, SOCKET_PORT } from '../config';
import { WsCloseCodeEnum } from '../types/ws-close-code-enum';
import { TldrawWsService } from '../service';

@WebSocketGateway(SOCKET_PORT)
export class TldrawWs implements OnGatewayInit, OnGatewayConnection {
@WebSocketServer()
server!: Server;

constructor(
private readonly configService: ConfigService<TldrawConfig, true>,
private readonly tldrawWsService: TldrawWsService
) {}

public handleConnection(client: WebSocket, request: Request): void {
const docName = this.getDocNameFromRequest(request);

if (docName.length > 0 && this.configService.get<string>('FEATURE_TLDRAW_ENABLED')) {
this.tldrawWsService.setupWSConnection(client, docName);
} else {
client.close(
WsCloseCodeEnum.WS_CLIENT_BAD_REQUEST_CODE,
'Document name is mandatory in url or Tldraw Tool is turned off.'
);
}
}

public afterInit(): void {
this.tldrawWsService.setPersistence({
bindState: async (docName, ydoc) => {
await this.tldrawWsService.updateDocument(docName, ydoc);
},
writeState: async (docName) => {
// This is called when all connections to the document are closed.
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
await this.tldrawWsService.flushDocument(docName);
},
});
}

private getDocNameFromRequest(request: Request): string {
const urlStripped = request.url.replace('/', '');
return urlStripped;
}
}
Loading
Loading