(server)
| 75 | return { |
| 76 | name: 'hocuspocus', |
| 77 | async configureServer(server) { |
| 78 | const keepaliveGraceTimers = new Map<string, ReturnType<typeof setTimeout>>(); |
| 79 | const keepaliveGraceInflight = new Set<Promise<void>>(); |
| 80 | let shuttingDown = false; |
| 81 | |
| 82 | configureServerInvocations += 1; |
| 83 | if (configureServerInvocations > 1) { |
| 84 | console.warn( |
| 85 | `[collab] configureServer invoked ${configureServerInvocations}× — Vite restarted; spinning up a fresh ServerInstance. The previous srv will be destroyed by its httpServer close handler.`, |
| 86 | ); |
| 87 | } else { |
| 88 | console.info(`[collab] configureServer invocation=1 pid=${process.pid}`); |
| 89 | } |
| 90 | |
| 91 | const currentSrv = createServer({ |
| 92 | contentDir: CONTENT_DIR, |
| 93 | projectDir: TEST_PROJECT_DIR ?? (isTestIsolated ? CONTENT_DIR : PROJECT_ROOT), |
| 94 | contentRoot: isTestIsolated ? '' : CONTENT_ROOT, |
| 95 | gitEnabled: isEphemeralTest ? false : !isTestIsolated || gitEnabledForTest, |
| 96 | enableTestRoutes: true, |
| 97 | quiet: true, |
| 98 | ...(isEphemeralTest ? { ephemeral: true, singleDocRelPath: SINGLE_DOC_REL_PATH } : {}), |
| 99 | }); |
| 100 | |
| 101 | latestLockDir = currentSrv.lockDir; |
| 102 | if (!exitHandlerRegistered) { |
| 103 | exitHandlerRegistered = true; |
| 104 | process.once('exit', () => { |
| 105 | if (latestLockDir === null) return; |
| 106 | try { |
| 107 | releaseServerLock(latestLockDir); |
| 108 | } catch {} |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | if (configureServerInvocations === 1) { |
| 113 | getLogger('hocuspocus').info({ contentDir: CONTENT_DIR }, 'content dir'); |
| 114 | } |
| 115 | |
| 116 | server.httpServer?.once('listening', () => { |
| 117 | const addr = server.httpServer?.address(); |
| 118 | if (typeof addr === 'object' && addr !== null) { |
| 119 | updateServerLockPort(currentSrv.lockDir, addr.port); |
| 120 | } |
| 121 | }); |
| 122 | |
| 123 | const { hocuspocus, sessionManager, agentFocusBroadcaster, agentPresenceBroadcaster } = |
| 124 | currentSrv; |
| 125 | |
| 126 | const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_COLLAB_MESSAGE_BYTES }); |
| 127 | wss.on('error', (err) => { |
| 128 | console.error('[collab] WebSocketServer error:', err); |
| 129 | }); |
| 130 | |
| 131 | server.httpServer?.prependListener('upgrade', (req, socket, head) => { |
| 132 | if (!req.url?.startsWith('/collab')) return; |
| 133 | |
| 134 | console.info( |
nothing calls this directly
no test coverage detected