(req: import('node:http').IncomingMessage, res: ServerResponse)
| 114 | let apiConfigNudge: (() => void) | null = null; |
| 115 | |
| 116 | const requestHandler = (req: import('node:http').IncomingMessage, res: ServerResponse) => { |
| 117 | const url = req.url?.split('?')[0]; |
| 118 | |
| 119 | if (req.method === 'GET' && (url === '/' || url === '')) { |
| 120 | const armed = readArmedPaneTarget(lockDir); |
| 121 | if (armed && !/[\r\n]/.test(armed)) { |
| 122 | clearArmedPaneTarget(lockDir); |
| 123 | res.statusCode = 302; |
| 124 | res.setHeader('Location', `/${armed}`); |
| 125 | res.setHeader('Cache-Control', 'no-store'); |
| 126 | res.end(); |
| 127 | return; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (url === '/' || url === '') { |
| 132 | req.url = '/index.html'; |
| 133 | } |
| 134 | |
| 135 | if (url?.startsWith('/api/')) { |
| 136 | if (rejectIfNotLoopbackApi(req, res)) return; |
| 137 | } |
| 138 | |
| 139 | if (url === '/api/config' && req.method === 'DELETE') { |
| 140 | clearArmedPaneTarget(lockDir); |
| 141 | res.setHeader('Cache-Control', 'no-store'); |
| 142 | res.statusCode = 204; |
| 143 | res.end(); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | if (url === '/api/config' && (req.method === 'GET' || req.method === 'HEAD')) { |
| 148 | apiConfigNudge?.(); |
| 149 | const lock = readServerLock(lockDir); |
| 150 | const sameOriginHost = req.headers.host ?? `localhost:${resolvedPort}`; |
| 151 | const collabUrl = lock && lock.port > 0 ? `ws://${sameOriginHost}/collab` : null; |
| 152 | const paneTarget = readArmedPaneTarget(lockDir); |
| 153 | const body = JSON.stringify({ collabUrl, previewUrl: null, port: resolvedPort, paneTarget }); |
| 154 | res.setHeader('Content-Type', 'application/json'); |
| 155 | res.setHeader('Cache-Control', 'no-store'); |
| 156 | res.setHeader('X-Content-Type-Options', 'nosniff'); |
| 157 | res.statusCode = 200; |
| 158 | if (req.method === 'HEAD') { |
| 159 | res.end(); |
| 160 | } else { |
| 161 | res.end(body); |
| 162 | } |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | if (url?.startsWith('/api/')) { |
| 167 | apiConfigNudge?.(); |
| 168 | const lock = readServerLock(lockDir); |
| 169 | if (!lock || lock.port <= 0) { |
| 170 | emitProblem( |
| 171 | res, |
| 172 | 503, |
| 173 | 'urn:ok:error:collab-server-not-running', |
nothing calls this directly
no test coverage detected