-
Notifications
You must be signed in to change notification settings - Fork 17
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
Closed
Changes from all commits
Commits
Show all changes
168 commits
Select commit
Hold shift + click to select a range
2dda3e7
Init module tldraw
860e585
Init commit, add tldraw websocket module
e8a29ec
Merge branch 'main' into BC-4201-planning-impolementation
b3c1b53
Update package-lock.json
f39abca
add yml file to main yml
29d597f
delete probes in tldraw yml
a97da82
Change socket.io to WebSocket
ec32f16
Adjusting code to websockets
047e870
Update const variables and unit file
a6410de
Merge branch 'main' into BC-4201-planning-impolementation
48fc70e
Fix tests.
4b71c4a
Merge branch 'main' into BC-4201-planning-impolementation
a78fd9a
update of packages after merging with main
93ddafd
fix exports, add env vars imports, delete unnecessary code
9a5bdb2
Change response callback to axios
bb75a6a
Remove doubled functionality
1d6ae7a
Fix utils lint and code style
aae1b3b
Fix utils lint and code style
ab201ae
Fix lint and code style
35b4941
Get rid of unnecessary var
3c78b47
Improve import and global var
4786e15
Fix config url setting, tidy modules import
306d8b6
Fix config url setting, tidy modules import
fd24b34
Add checking for docName in url
ed433be
Add allowed licence, fix tests, fix suggested changes
ecd8a5f
Move connection string to config file, delete unnecessary export.
73940db
Clear constructor and connection interval code.
25a559f
Change licence allow list
9695c5a
Remove lint errors
c6c075a
Adjust websocket port for testing.
957c83e
Delete unusable code
2a37276
Improve tests
b88925b
Improve tests
bb3de3d
Merge branch 'main' into BC-4201-planning-impolementation
f564943
Improve tests
bf4a5fb
Improve tests
779a1bc
Get rid of code smell, check if last test is bad (commenting it)
18681a7
Change app listen to init in tests,create svc for tldraw in server side
c9bf0d8
Fix typing of tldraw config
f643ed0
Fix typing in gateway of mongo persistence
b519dce
Fix test by using fake timer
a69995e
Deleting setInterval test
9a80ae4
Change yml main to BC-4256-Integration-tldraw.
47bb9e2
Add tests for updateHandler and closing connection in send method.
427fd76
Delete unreachable Error
4670a6e
Merge remote-tracking branch 'origin/main' into BC-4256-Integration-t…
94c1096
Fix packages
147bac2
Delete listen tldraw server port.
75594cb
Change tldraw env vars into group. Change beforeEach in test to befor…
23cd0ca
Delete annotations of port and scrape for tldraw
02785cf
Change container port in tldraw-deployment.yml.j2
91c5431
Hard code port as suggested
e735058
Change env var default definition
56c9aea
Change test test app life cycle
8e3d661
Add Error handling
80f0b73
fix tldraw test environment
davwas 19b9796
fix tldraw.app.ts file
davwas 62ff413
fix tldraw.app.ts file #2
davwas 8d740f2
fix tldraw.app.ts file #3
davwas 9b09f8a
Merge branch 'main' into BC-4256-Integration-tldraw
davwas 10b40c2
Merge branch 'main' into BC-4256-Integration-tldraw
davwas f0214f1
fix package lock json
davwas fc77f7d
fix packages
davwas 7aab343
fix dependency license problem
davwas 7c1ca24
Revert "fix dependency license problem"
davwas 0318faa
add tests
davwas f5faed5
remove unused imports
davwas b83e095
fix tldraw app start
davwas aca59e6
fix dependency review file
davwas c59e0c9
Squashed minor test changes, one main merge, and sonar settings changes
e0658e7
Check git remote logs.
728fd6b
Check git remote logs. (Revert changes)
blazejpass 4e941ce
Merge branch 'main' into BC-4256-Integration-tldraw
blazejpass 9c3a5ee
Merge main and resolve conflicts
blazejpass a8503eb
add unit tests
5aac216
fix unit tests
b9fc8bd
fix unit tests
a0bccbb
add unit test
b1ad383
add unit tests
faf6015
fix tests
dfbf5fb
fix tests
bf77f9f
extend tests timeout
9a7f895
add some unit tests
b3fddd4
add unit tests
045b9c2
fix test
ad55a8f
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 80bc773
tests refactor
42aeac3
resolve comments v1
cbf415f
resolve comments v2
761f224
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek eee4fdc
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek a717448
restore ydoc on update
6e89051
fix test
bc862a3
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek a66cba0
code refactor
f8643c4
add missing lne
b5db76b
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek f4841bf
add unit test
4ff48ef
Merge branch 'main' into BC-4256-Integration-tldraw
3521378
update packages versions
3115c4e
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 564f929
Adjust packages after merge
blazejpass f471d00
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 465bbbe
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 9d02852
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek eab54dc
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 1ca757e
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 07276e4
fix license
d8ed180
update package version
b0be792
update package-lock.json
e8d6d0d
fix tests
ff4b97f
code refactor
69da678
fix lint
e22b43a
add unit test
acc7f1f
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 310cb47
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 24b61bf
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 2a02a5b
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 0af7104
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 3bc3cdc
fix type
fbd698b
code refactor:
b966167
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 8326300
code refactor
a4784bf
refactor tests
97f5b6c
Change naming tldraw controller
blazejpass 23129c0
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek f4c522b
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 823ba51
code refactor
439f5e7
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 1cc735a
Change module imports, db address config data, improwe sending data v…
blazejpass de7b6f9
Delete uni tests for gateway/ws file
blazejpass b454c4c
Change db tldraw url configuration
blazejpass 9126b04
Add Service to testing module
blazejpass 5f8127c
Add Service to testing module
blazejpass 8b8d7e5
Add Service to testing module
blazejpass 635281f
Add repo tests
blazejpass b844426
Add Shared Doc tests
blazejpass b262bf3
Add tldraw service tests
blazejpass dd329c8
Merge branch 'main' into BC-4256-Integration-tldraw
blazejpass 206fe2d
Change push yml uses branch
blazejpass 4cdc81c
Merge remote-tracking branch 'origin/BC-4256-Integration-tldraw' into…
blazejpass 418e569
Merge branch 'main' into BC-4256-Integration-tldraw
blazejpass 705d093
Change push yml uses branch
blazejpass a7d9beb
Change push yml uses branch
blazejpass d073aef
Delete TLDRAW_DB_TEST_URL
blazejpass 6a90db1
Code improvements according to PR comments in review.
blazejpass 3dd1f08
Move yml deployment and service files for tldraw to separate role folder
blazejpass 752b403
Add descriptions to ws-shared-doc.do.ts methods
blazejpass 3ef0637
Delete delay for test
blazejpass 33a9726
Merge branch 'main' into BC-4256-Integration-tldraw
blazejpass 015c300
Update package-lock after merging
blazejpass f11d39f
Improve tests after merge.
blazejpass b2fd397
Use tldraw-test-module, improve code
blazejpass 7eecf87
Fix tests according to suggested comments.
blazejpass a9037cf
Change deployment yml to main
blazejpass 336de77
Fix code according to comments
blazejpass 7475bf5
Fix tests code according to comments. Move test helper
blazejpass 9afa7b5
Check doff_app_deploy branch
blazejpass e1ddad5
Merge branch 'main' into BC-4256-Integration-tldraw
blazejpass d1b24cc
Change config to public in tldraw-board.repo.ts
blazejpass 67e40a3
Merge branch 'main' into BC-4256-Integration-tldraw
blazejpass eef6243
fix mock doc object for tests
0420836
increase timeout
42d1221
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek def15ff
update mongodb provider lib version
188c151
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek c92e243
Merge branch 'main' into BC-4256-Integration-tldraw
wiaderwek 5f6fc6d
Merge branch 'main' into BC-4256-Integration-tldraw
Metauriel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
50 changes: 50 additions & 0 deletions
50
ansible/roles/schulcloud-server-tldraw/templates/api-tldraw-deployment.yml.j2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }} |
16 changes: 16 additions & 0 deletions
16
ansible/roles/schulcloud-server-tldraw/templates/api-tldraw-svc.yml.j2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
129 changes: 129 additions & 0 deletions
129
apps/server/src/modules/tldraw/controller/api-test/tldraw.ws.api.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './tldraw.ws'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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