@@ -17,6 +17,8 @@ import * as syncProtocol from 'y-protocols/sync';
1717// @ts -expect-error
1818import * as Y from 'yjs' ;
1919
20+ import { FastifyBaseLogger } from 'fastify' ;
21+
2022import { db } from '../../../../drizzle/db' ;
2123import { WSDoc } from './WSDoc' ;
2224import { MESSAGE_AWARENESS_CODE , MESSAGE_SYNC_CODE , PING_TIMEOUT } from './constants' ;
@@ -40,8 +42,8 @@ class WSSharedDoc extends WSDoc {
4042 return name + '_shared' ;
4143 }
4244
43- constructor ( pageItemService : PageItemService , name : string ) {
44- super ( pageItemService , name , true ) ;
45+ constructor ( pageItemService : PageItemService , name : string , logger : FastifyBaseLogger ) {
46+ super ( pageItemService , name , true , logger ) ;
4547
4648 const awarenessChangeHandler = (
4749 {
@@ -113,9 +115,9 @@ class WSSharedDoc extends WSDoc {
113115 this . pageItemService . storeUpdate ( db , pageId , update ) ;
114116 } ) ;
115117 } catch ( e ) {
116- console . error ( ' An error occured while binding the state:' , e ) ;
118+ this . logger . error ( `Page ${ pageId } : An error occured while binding the state: ${ e } ` ) ;
117119 // send error to sentry
118- captureException ( e ) ;
120+ captureException ( e , { tags : { feature : 'page' , pageId : this . name } } ) ;
119121
120122 this . conns . forEach ( ( v , conn ) => {
121123 // close connections for unexpected error
@@ -132,8 +134,8 @@ class WSSharedDoc extends WSDoc {
132134class WSReadDoc extends WSDoc {
133135 private SYNC_ORIGIN = 'sync' ;
134136
135- constructor ( pageItemService : PageItemService , name : string ) {
136- super ( pageItemService , name , false ) ;
137+ constructor ( pageItemService : PageItemService , name : string , logger : FastifyBaseLogger ) {
138+ super ( pageItemService , name , false , logger ) ;
137139 this . bindState ( name ) ;
138140
139141 // send yjs doc updates to all connections
@@ -150,9 +152,9 @@ class WSReadDoc extends WSDoc {
150152 const persistedYdoc = await this . pageItemService . getById ( db , pageId ) ;
151153 Y . applyUpdate ( this , Y . encodeStateAsUpdate ( persistedYdoc ) , this . SYNC_ORIGIN ) ;
152154 } catch ( e ) {
153- console . error ( ' An error occured while binding the state:' , e ) ;
155+ this . logger . error ( `Page ${ pageId } : An error occured while binding the state: ${ e } ` ) ;
154156 // send error to sentry
155- captureException ( e ) ;
157+ captureException ( e , { tags : { feature : 'page' , pageId : this . name } } ) ;
156158 this . conns . forEach ( ( v , conn ) => {
157159 // close connections for unexpected error
158160 this . closeConn ( conn , 1011 ) ;
@@ -173,7 +175,7 @@ class WSReadDoc extends WSDoc {
173175 * @param conn websocket connection
174176 * @param doc yjs document
175177 */
176- function setupPingPong ( conn : WebSocket , doc : WSDoc ) {
178+ function setupPingPong ( conn : WebSocket , doc : WSDoc , pageId : string , logger : FastifyBaseLogger ) {
177179 // Check if connection is still alive
178180 let pongReceived = true ;
179181 const pingInterval = setInterval ( ( ) => {
@@ -187,7 +189,7 @@ function setupPingPong(conn: WebSocket, doc: WSDoc) {
187189 try {
188190 conn . ping ( ) ;
189191 } catch ( e ) {
190- console . error ( e ) ;
192+ logger . error ( `Page ${ pageId } : ${ e } ` ) ;
191193 doc . closeConn ( conn ) ;
192194 clearInterval ( pingInterval ) ;
193195 }
@@ -213,30 +215,34 @@ export const setupWSConnectionForWriters = (
213215 conn : WebSocket ,
214216 pageId : string ,
215217 pageItemService : PageItemService ,
218+ logger : FastifyBaseLogger ,
216219) => {
217220 conn . binaryType = 'arraybuffer' ;
218221 // get doc, initialize if it does not exist yet
219222 const doc = map . setIfUndefined ( docs , pageId , ( ) => {
220- const doc = new WSSharedDoc ( pageItemService , pageId ) ;
223+ logger . info ( `Page ${ pageId } : Initializing new doc` ) ;
224+ const doc = new WSSharedDoc ( pageItemService , pageId , logger ) ;
221225
222226 docs . set ( pageId , doc ) ;
223227
224228 return doc ;
225229 } ) ;
226230 doc . addConnection ( conn ) ;
227231
228- setupPingPong ( conn , doc ) ;
232+ setupPingPong ( conn , doc , pageId , logger ) ;
229233
230234 // put the following in a variables in a block so the interval handlers don't keep in in
231235 // scope
232236 {
233237 // send sync step 1
238+ logger . info ( `Page ${ pageId } : send sync step 1 ` ) ;
234239 const encoder = encoding . createEncoder ( ) ;
235240 encoding . writeVarUint ( encoder , MESSAGE_SYNC_CODE ) ;
236241 syncProtocol . writeSyncStep1 ( encoder , doc ) ;
237242 doc . send ( conn , encoding . toUint8Array ( encoder ) ) ;
238243
239244 // send init awareness
245+ logger . info ( `Page ${ pageId } : send init awareness` ) ;
240246 const awarenessStates = doc . awareness . getStates ( ) ;
241247 if ( awarenessStates . size > 0 ) {
242248 const encoder = encoding . createEncoder ( ) ;
@@ -259,23 +265,26 @@ export const setupWSConnectionForRead = (
259265 conn : WebSocket ,
260266 pageId : string ,
261267 pageItemService : PageItemService ,
268+ logger : FastifyBaseLogger ,
262269) => {
263270 conn . binaryType = 'arraybuffer' ;
264271 // get doc, initialize if it does not exist yet
265272 const doc = map . setIfUndefined ( readDocs , pageId , ( ) => {
266- const doc = new WSReadDoc ( pageItemService , pageId ) ;
273+ logger . info ( `Page ${ pageId } : Initializing new doc` ) ;
274+ const doc = new WSReadDoc ( pageItemService , pageId , logger ) ;
267275 readDocs . set ( pageId , doc ) ;
268276
269277 return doc ;
270278 } ) ;
271279 doc . addConnection ( conn ) ;
272280
273- setupPingPong ( conn , doc ) ;
281+ setupPingPong ( conn , doc , pageId , logger ) ;
274282
275283 // put the following in a variables in a block so the interval handlers don't keep in in
276284 // scope
277285 {
278286 // send sync step 1
287+ logger . info ( `Page ${ pageId } : send sync step 1` ) ;
279288 const encoder = encoding . createEncoder ( ) ;
280289 encoding . writeVarUint ( encoder , MESSAGE_SYNC_CODE ) ;
281290 syncProtocol . writeSyncStep1 ( encoder , doc ) ;
0 commit comments