| 186 | ); |
| 187 | }, |
| 188 | async socket( |
| 189 | pathOrInit?: string | RequestInit, |
| 190 | maybeInit?: RequestInit |
| 191 | ) { |
| 192 | let res: Response; |
| 193 | // This method is better because it doesn't go via the internet |
| 194 | // and doesn't get 522/404/ get caught in CF firewall |
| 195 | let path: RequestInfo | undefined; |
| 196 | let init: RequestInit | undefined; |
| 197 | if (pathOrInit) { |
| 198 | if (typeof pathOrInit === "string") { |
| 199 | path = pathOrInit; |
| 200 | init = maybeInit; |
| 201 | if (path[0] !== "/") { |
| 202 | throw new Error("Path must start with /"); |
| 203 | } |
| 204 | res = await stub.fetch( |
| 205 | `http://${options.host}/parties/${key}/${name}${path}`, |
| 206 | { |
| 207 | ...init, |
| 208 | headers: { |
| 209 | upgrade: "websocket", |
| 210 | ...init?.headers |
| 211 | } |
| 212 | } |
| 213 | ); |
| 214 | } else { |
| 215 | init = pathOrInit; |
| 216 | res = await stub.fetch( |
| 217 | `http://${options.host}/parties/${key}/${name}`, |
| 218 | { |
| 219 | ...init, |
| 220 | headers: { |
| 221 | upgrade: "websocket", |
| 222 | ...init?.headers |
| 223 | } |
| 224 | } |
| 225 | ); |
| 226 | } |
| 227 | } else { |
| 228 | res = await stub.fetch( |
| 229 | `http://${options.host}/parties/${key}/${name}`, |
| 230 | { |
| 231 | headers: { |
| 232 | upgrade: "websocket" |
| 233 | } |
| 234 | } |
| 235 | ); |
| 236 | } |
| 237 | const ws = res.webSocket; |
| 238 | if (!ws) { |
| 239 | throw new Error("Expected a websocket response"); |
| 240 | } |
| 241 | ws.accept(); |
| 242 | return ws as WebSocket; |
| 243 | } |
| 244 | }; |
| 245 | } |