()
| 132 | } |
| 133 | |
| 134 | private createSSEServer() { |
| 135 | if (this.#server) { |
| 136 | return this.#server |
| 137 | } |
| 138 | const server = http.createServer((req, res) => { |
| 139 | if (req.url === '/__devtools/sse') { |
| 140 | res.writeHead(200, { |
| 141 | 'Content-Type': 'text/event-stream', |
| 142 | 'Cache-Control': 'no-cache', |
| 143 | Connection: 'keep-alive', |
| 144 | 'Access-Control-Allow-Origin': '*', |
| 145 | }) |
| 146 | res.write('\n') |
| 147 | this.debugLog('New SSE client connected') |
| 148 | this.#sseClients.add(res) |
| 149 | req.on('close', () => this.#sseClients.delete(res)) |
| 150 | return |
| 151 | } |
| 152 | |
| 153 | if (req.url === '/__devtools/send' && req.method === 'POST') { |
| 154 | let body = '' |
| 155 | req.on('data', (chunk) => (body += chunk)) |
| 156 | req.on('end', () => { |
| 157 | try { |
| 158 | const msg = parseWithBigInt(body) |
| 159 | this.debugLog('Received event from client', msg) |
| 160 | this.emitToServer(msg) |
| 161 | } catch {} |
| 162 | }) |
| 163 | res.writeHead(200).end() |
| 164 | return |
| 165 | } |
| 166 | |
| 167 | res.statusCode = 404 |
| 168 | res.end() |
| 169 | }) |
| 170 | globalThis.__TANSTACK_DEVTOOLS_SERVER__ = server |
| 171 | this.#server = server |
| 172 | return server |
| 173 | } |
| 174 | |
| 175 | private createWebSocketServer() { |
| 176 | if (this.#wssServer) { |
no test coverage detected