@@ -12,6 +12,7 @@ import { Logger } from '../../../infra/logger/index.js';
1212import { MetricsService } from '../../../infra/metrics/index.js' ;
1313import { RedisAdapter } from '../../../infra/redis/index.js' ;
1414import { YRedisClient , YRedisDoc , YRedisService , YRedisUser , YRedisUserFactory } from '../../../infra/y-redis/index.js' ;
15+ import { WebSocketCloseCode } from '../../../shared/type/websocket-close-code.js' ;
1516import { REDIS_FOR_SUBSCRIBE_OF_DELETION , UWS } from '../server.const.js' ;
1617import { TldrawServerConfig } from '../tldraw-server.config.js' ;
1718
@@ -21,14 +22,6 @@ interface RequestHeaderInfos {
2122 headerWsProtocol : string ;
2223}
2324
24- // https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
25- enum WebSocketErrorCodes {
26- InternalError = 1011 ,
27- PolicyViolation = 1008 ,
28- TldrawPolicyViolation = 4401 ,
29- TldrawInternalError = 4500 ,
30- }
31-
3225@Injectable ( )
3326export class WebsocketGateway implements OnModuleInit , OnModuleDestroy {
3427 public constructor (
@@ -120,7 +113,7 @@ export class WebsocketGateway implements OnModuleInit, OnModuleDestroy {
120113 }
121114
122115 if ( user . room === null || user . userid === null ) {
123- ws . end ( WebSocketErrorCodes . PolicyViolation , 'Missing room or userid' ) ;
116+ ws . end ( WebSocketCloseCode . InternalError , 'Missing room or userid' ) ;
124117
125118 return ;
126119 }
@@ -149,7 +142,7 @@ export class WebsocketGateway implements OnModuleInit, OnModuleDestroy {
149142 this . yRedisService . ensureLatestContentSubscription ( yRedisDoc , user ) ;
150143 } catch ( error ) {
151144 this . logger . warning ( error ) ;
152- ws . end ( WebSocketErrorCodes . InternalError , 'Internal Server Error' ) ;
145+ ws . end ( WebSocketCloseCode . InternalError , 'Internal Server Error' ) ;
153146 }
154147 }
155148
@@ -176,7 +169,7 @@ export class WebsocketGateway implements OnModuleInit, OnModuleDestroy {
176169 const user = ws . getUserData ( ) ;
177170
178171 if ( ! user . hasWriteAccess || ! user . room ) {
179- ws . end ( WebSocketErrorCodes . TldrawPolicyViolation , 'User has no write access or room is missing' ) ;
172+ ws . end ( WebSocketCloseCode . Unauthorized , 'User has no write access or room is missing' ) ;
180173
181174 return ;
182175 }
@@ -188,7 +181,7 @@ export class WebsocketGateway implements OnModuleInit, OnModuleDestroy {
188181 }
189182 } catch ( error ) {
190183 this . logger . warning ( error ) ;
191- ws . end ( WebSocketErrorCodes . InternalError ) ;
184+ ws . end ( WebSocketCloseCode . InternalError ) ;
192185 }
193186 }
194187
@@ -207,7 +200,7 @@ export class WebsocketGateway implements OnModuleInit, OnModuleDestroy {
207200 MetricsService . openConnectionsGauge . dec ( ) ;
208201 } catch ( error ) {
209202 this . logger . warning ( error ) ;
210- ws . end ( WebSocketErrorCodes . InternalError ) ;
203+ ws . end ( WebSocketCloseCode . InternalError ) ;
211204 }
212205 }
213206
0 commit comments