The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | /** @effect-diagnostics preferSchemaOverJson:skip-file */ |
| 2 | import { NodeHttpClient, NodeHttpServer, NodeHttpServerRequest, NodeWS } from "@effect/platform-node" |
| 3 | import { assert, describe, expect, it } from "@effect/vitest" |
| 4 | import { ByteSize, Effect, Exit, Formatter, Logger, Option, References } from "effect" |
| 5 | import * as Deferred from "effect/Deferred" |
| 6 | import * as Duration from "effect/Duration" |
| 7 | import * as Fiber from "effect/Fiber" |
| 8 | import { constVoid } from "effect/Function" |
| 9 | import { |
| 10 | Cookies, |
| 11 | FetchHttpClient, |
| 12 | Headers, |
| 13 | HttpBody, |
| 14 | HttpClient, |
| 15 | HttpClientRequest, |
| 16 | HttpClientResponse, |
| 17 | HttpIncomingMessage, |
| 18 | HttpMiddleware, |
| 19 | HttpPlatform, |
| 20 | HttpRouter, |
| 21 | HttpServer, |
| 22 | HttpServerRequest, |
| 23 | HttpServerRespondable, |
| 24 | HttpServerResponse, |
| 25 | Multipart, |
| 26 | UrlParams |
| 27 | } from "effect/http" |
| 28 | import * as HttpApiError from "effect/http-api/HttpApiError" |
| 29 | import * as Latch from "effect/Latch" |
| 30 | import * as Layer from "effect/Layer" |
| 31 | import * as ManagedRuntime from "effect/ManagedRuntime" |
| 32 | import * as IpNetwork from "effect/net/IpNetwork" |
| 33 | import * as NetAddress from "effect/net/NetAddress" |
| 34 | import * as Schema from "effect/Schema" |
| 35 | import * as Scope from "effect/Scope" |
| 36 | import { Socket } from "effect/socket" |
| 37 | import * as Stream from "effect/Stream" |
| 38 | import * as Tracer from "effect/Tracer" |
| 39 | import * as Buffer from "node:buffer" |
| 40 | import { randomBytes } from "node:crypto" |
| 41 | import { EventEmitter } from "node:events" |
| 42 | import { createReadStream, readFileSync } from "node:fs" |
| 43 | import * as Http from "node:http" |
| 44 | import * as Http2 from "node:http2" |
| 45 | import * as Https from "node:https" |
| 46 | import * as Net from "node:net" |
| 47 | import { Readable } from "node:stream" |
| 48 | import * as Tls from "node:tls" |
| 49 | import * as Zlib from "node:zlib" |
| 50 | import { |
| 51 | describeHttpServerProtocol, |
| 52 | http2Get, |
| 53 | layerHttp2Client, |
| 54 | layerTestClient |
| 55 | } from "../../node-shared/test/HttpServerHttp2.test-utils.ts" |
| 56 | |
| 57 | const Todo = Schema.Struct({ |
| 58 | id: Schema.Number, |
| 59 | title: Schema.String |
| 60 | }) |
| 61 | const IdParams = Schema.Struct({ |
| 62 | id: Schema.FiniteFromString |
| 63 | }) |
| 64 | const todoResponse = HttpServerResponse.schemaJson(Todo) |
| 65 | |
| 66 | describe("HttpServer", () => { |
| 67 | it.effect.each([8 * 1024, 512 * 1024])( |
| 68 | "delivers a partial-read 413 and drains or closes for %i leftover bytes", |
| 69 | (leftoverBytes) => |
| 70 | Effect.gen(function*() { |
| 71 | const server = Http.createServer() |
| 72 | const body = "rejected".repeat(16 * 1024) |
| 73 | yield* HttpServer.serve( |
| 74 | HttpServerRequest.HttpServerRequest.use((request) => |
| 75 | request.url === "/next" |
| 76 | ? Effect.succeed(HttpServerResponse.text("next")) |
| 77 | : request.stream.pipe( |
| 78 | Stream.take(1), |
| 79 | Stream.runDrain, |
| 80 | Effect.as(HttpServerResponse.text(body, { status: 413 })) |
| 81 | ) |
| 82 | ) |
| 83 | ).pipe( |
| 84 | Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })), |
| 85 | Layer.build |
| 86 | ) |
| 87 | if (leftoverBytes > 256 * 1024) { |
| 88 | // A raw client observes the server FIN; node:http closes its own socket on Connection: close. |
| 89 | yield* Effect.callback<void, Error>((resume) => { |
| 90 | const socket = Net.connect({ host: "127.0.0.1", port: tcpPort(server), allowHalfOpen: true }) |
| 91 | let received = "" |
| 92 | let sentLeftover = false |
| 93 | let fin = false |
| 94 | socket.setEncoding("utf8") |
| 95 | socket.on("error", (error) => resume(Effect.fail(error))) |
| 96 | socket.on("connect", () => { |
| 97 | socket.write( |
| 98 | `POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: ${ |
| 99 | leftoverBytes + 1 |
| 100 | }\r\nConnection: keep-alive\r\n\r\nx` |
| 101 | ) |
| 102 | }) |
| 103 | socket.on("data", (chunk: string) => { |
| 104 | received += chunk |
| 105 | if (!sentLeftover) { |
| 106 | sentLeftover = true |
| 107 | // Wait for the response so the handler consumes only the first byte. |
| 108 | socket.write(Buffer.Buffer.alloc(leftoverBytes)) |
| 109 | } |
| 110 | }) |
| 111 | socket.on("end", () => { |
| 112 | fin = true |
| 113 | resume(Effect.sync(() => { |
| 114 | const separator = received.indexOf("\r\n\r\n") |
| 115 | assert.isAtLeast(separator, 0) |
| 116 | const headers = received.slice(0, separator).toLowerCase() |
| 117 | assert.match(headers, /^http\/1\.1 413 /) |
| 118 | assert.include(headers, "\r\nconnection: close") |
| 119 | assert.include(headers, `\r\ncontent-length: ${Buffer.Buffer.byteLength(body)}`) |
| 120 | assert.strictEqual(received.slice(separator + 4), body) |
| 121 | })) |
| 122 | }) |
| 123 | socket.on("close", () => { |
| 124 | if (!fin) resume(Effect.fail(new Error("Socket closed before the server FIN"))) |
| 125 | }) |
| 126 | return Effect.sync(() => socket.destroy()) |
| 127 | }).pipe(Effect.timeout("3 seconds")) |
| 128 | return |
| 129 | } |
| 130 | const agent = yield* Effect.acquireRelease( |
| 131 | Effect.sync(() => new Http.Agent({ keepAlive: true, maxSockets: 1 })), |
| 132 | (agent) => Effect.sync(() => agent.destroy()) |
| 133 | ) |
| 134 | yield* Effect.callback<void, Error>((resume) => { |
| 135 | const fail = (error: Error) => resume(Effect.fail(error)) |
| 136 | const request = Http.request({ |
| 137 | host: "127.0.0.1", |
| 138 | port: tcpPort(server), |
| 139 | method: "POST", |
| 140 | headers: { "content-length": leftoverBytes + 1 }, |
| 141 | agent |
| 142 | }) |
| 143 | let firstSocket: Net.Socket |
| 144 | request.on("error", fail) |
| 145 | request.on("socket", (socket) => { |
| 146 | firstSocket = socket |
| 147 | socket.on("error", fail) |
| 148 | }) |
| 149 | request.on("response", (response) => { |
| 150 | let received = "" |
| 151 | response.setEncoding("utf8") |
| 152 | response.on("data", (chunk: string) => received += chunk) |
| 153 | response.on("error", fail) |
| 154 | response.on("end", () => { |
| 155 | const status = response.statusCode |
| 156 | const next = Http.get({ host: "127.0.0.1", port: tcpPort(server), path: "/next", agent }, (response) => { |
| 157 | response.on("error", fail) |
| 158 | response.on("end", () => { |
| 159 | resume(Effect.sync(() => { |
| 160 | assert.strictEqual(status, 413) |
| 161 | assert.strictEqual(received, body) |
| 162 | assert.strictEqual(response.statusCode, 200) |
| 163 | // Queued requests can reuse the socket without setting ClientRequest.reusedSocket. |
| 164 | assert.strictEqual(next.socket, firstSocket) |
| 165 | })) |
| 166 | }) |
| 167 | response.resume() |
| 168 | }) |
| 169 | next.on("error", fail) |
| 170 | }) |
| 171 | // The first chunk must be consumed on its own, not coalesced with the leftover body. |
| 172 | request.end(Buffer.Buffer.alloc(leftoverBytes)) |
| 173 | }) |
| 174 | request.write("x") |
| 175 | return Effect.sync(() => request.destroy()) |
| 176 | }).pipe(Effect.timeout("3 seconds")) |
| 177 | }) |
| 178 | ) |
| 179 | |
| 180 | it.effect("returns 413 before a chunked upload finishes", () => |
| 181 | Effect.gen(function*() { |
| 182 | yield* HttpRouter.add( |
| 183 | "POST", |
| 184 | "/body", |
| 185 | HttpServerRequest.HttpServerRequest.use((request) => Effect.as(request.text, HttpServerResponse.empty())).pipe( |
| 186 | Effect.provideService(HttpIncomingMessage.MaxBodySize, ByteSize.bytes(4)) |
| 187 | ) |
| 188 | ).pipe(HttpRouter.serve, Layer.build) |
| 189 | const server = yield* HttpServer.HttpServer |
| 190 | const port = (server.address as NetAddress.InetAddress).port |
| 191 | |
| 192 | const response = yield* Effect.callback<{ status: number | undefined; uploadComplete: boolean }, Error>( |
| 193 | (resume) => { |
| 194 | const request = Http.request({ hostname: "127.0.0.1", port, path: "/body", method: "POST", agent: false }) |
| 195 | request.on("error", (error) => resume(Effect.fail(error))) |
| 196 | request.on("response", (response) => { |
| 197 | const result = { status: response.statusCode, uploadComplete: request.writableEnded } |
| 198 | // Withhold the final chunk until the server has sent its response. |
| 199 | request.end("remaining upload") |
| 200 | response.on("error", (error) => resume(Effect.fail(error))) |
| 201 | response.on("end", () => { |
| 202 | request.destroy() |
| 203 | resume(Effect.succeed(result)) |
| 204 | }) |
| 205 | response.resume() |
| 206 | }) |
| 207 | request.write("12345") |
| 208 | return Effect.sync(() => request.destroy()) |
| 209 | } |
| 210 | ) |
| 211 | assert.strictEqual(response.status, 413) |
| 212 | assert.isFalse(response.uploadComplete) |
| 213 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 214 | |
| 215 | it.effect("returns 400 for malformed JSON within MaxBodySize", () => |
| 216 | Effect.gen(function*() { |
| 217 | yield* HttpRouter.add( |
| 218 | "POST", |
| 219 | "/body", |
| 220 | HttpServerRequest.HttpServerRequest.use((request) => Effect.as(request.json, HttpServerResponse.empty())).pipe( |
| 221 | Effect.provideService(HttpIncomingMessage.MaxBodySize, ByteSize.bytes(4)) |
| 222 | ) |
| 223 | ).pipe(HttpRouter.serve, Layer.build) |
| 224 | const client = yield* HttpClient.HttpClient |
| 225 | const response = yield* client.post("/body", { body: HttpBody.text("{") }) |
| 226 | assert.strictEqual(response.status, 400) |
| 227 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 228 | |
| 229 | it.effect("does not duplicate Node's automatic 100 Continue with makeHandler", () => |
| 230 | Effect.gen(function*() { |
| 231 | const scope = yield* Effect.scope |
| 232 | const handler = yield* NodeHttpServer.makeHandler( |
| 233 | HttpServerRequest.HttpServerRequest.use((request) => request.text.pipe(Effect.map(HttpServerResponse.text))), |
| 234 | { scope } |
| 235 | ) |
| 236 | // No checkContinue listener: Node acknowledges before invoking the handler. |
| 237 | const server = yield* Effect.acquireRelease( |
| 238 | Effect.sync(() => Http.createServer(handler)), |
| 239 | (server) => |
| 240 | Effect.callback<void>((resume) => { |
| 241 | server.close(() => resume(Effect.void)) |
| 242 | }) |
| 243 | ) |
| 244 | yield* Effect.callback<void, Error>((resume) => { |
| 245 | server.once("error", (error) => resume(Effect.fail(error))) |
| 246 | server.listen(0, "127.0.0.1", () => resume(Effect.void)) |
| 247 | }) |
| 248 | |
| 249 | const response = yield* expectContinueRequest(tcpPort(server), "hello") |
| 250 | assert.strictEqual(response.status, 200) |
| 251 | assert.strictEqual(response.body, "hello") |
| 252 | assert.isTrue(response.uploaded) |
| 253 | assert.strictEqual(response.continues, 1) |
| 254 | })) |
| 255 | |
| 256 | it.effect("rejects Expect: 100-continue without requesting the body", () => |
| 257 | Effect.gen(function*() { |
| 258 | const server = Http.createServer() |
| 259 | yield* HttpServer.serve( |
| 260 | HttpServerRequest.HttpServerRequest.use((request) => { |
| 261 | // Obtaining a body accessor must not start reading the body. |
| 262 | void request.stream |
| 263 | void request.text |
| 264 | void request.multipart |
| 265 | void request.multipartStream |
| 266 | return Effect.succeed(HttpServerResponse.empty({ status: 413 })) |
| 267 | }) |
| 268 | ).pipe( |
| 269 | Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })), |
| 270 | Layer.build |
| 271 | ) |
| 272 | |
| 273 | const response = yield* expectContinueRequest(tcpPort(server), "hello") |
| 274 | assert.strictEqual(response.status, 413) |
| 275 | assert.strictEqual(response.continues, 0) |
| 276 | assert.isFalse(response.uploaded) |
| 277 | })) |
| 278 | |
| 279 | it.effect.each(["stream", "text", "json", "urlParamsBody", "arrayBuffer"] as const)( |
| 280 | "sends one 100 Continue when consuming %s", |
| 281 | (accessor) => |
| 282 | Effect.gen(function*() { |
| 283 | const server = Http.createServer() |
| 284 | yield* HttpServer.serve( |
| 285 | HttpServerRequest.HttpServerRequest.use((original) => { |
| 286 | const request = original.modify({ url: "/body" }) |
| 287 | const body = accessor === "stream" |
| 288 | ? request.stream.pipe(Stream.decodeText(), Stream.mkString) |
| 289 | : accessor === "arrayBuffer" |
| 290 | ? request.arrayBuffer.pipe(Effect.map((buffer) => new TextDecoder().decode(buffer))) |
| 291 | : accessor === "json" |
| 292 | ? request.json.pipe(Effect.map((value) => String(value))) |
| 293 | : accessor === "urlParamsBody" |
| 294 | ? request.urlParamsBody.pipe(Effect.map(UrlParams.toString)) |
| 295 | : request.text |
| 296 | return body.pipe(Effect.map(HttpServerResponse.text)) |
| 297 | }) |
| 298 | ).pipe( |
| 299 | Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })), |
| 300 | Layer.build |
| 301 | ) |
| 302 | |
| 303 | const payload = accessor === "json" ? "\"hello\"" : accessor === "urlParamsBody" ? "hello=world" : "hello" |
| 304 | const response = yield* expectContinueRequest(tcpPort(server), payload) |
| 305 | assert.strictEqual(response.status, 200) |
| 306 | assert.strictEqual(response.body, accessor === "urlParamsBody" ? "hello=world" : "hello") |
| 307 | assert.strictEqual(response.continues, 1) |
| 308 | assert.isTrue(response.uploaded) |
| 309 | }) |
| 310 | ) |
| 311 | |
| 312 | it.effect.each(["multipart", "multipartStream"] as const)( |
| 313 | "sends one 100 Continue when consuming %s", |
| 314 | (accessor) => |
| 315 | Effect.gen(function*() { |
| 316 | const server = Http.createServer() |
| 317 | yield* HttpServer.serve( |
| 318 | HttpServerRequest.HttpServerRequest.use((original) => { |
| 319 | const request = original.modify({ url: "/upload" }) |
| 320 | const field = accessor === "multipart" |
| 321 | ? request.multipart.pipe(Effect.map((parts) => parts.message)) |
| 322 | : request.multipartStream.pipe( |
| 323 | Stream.runCollect, |
| 324 | Effect.map((parts) => parts.map((part) => Multipart.isField(part) ? part.value : "file").join("")) |
| 325 | ) |
| 326 | return field.pipe(Effect.map((value) => HttpServerResponse.text(String(value)))) |
| 327 | }) |
| 328 | ).pipe( |
| 329 | Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })), |
| 330 | Layer.build |
| 331 | ) |
| 332 | |
| 333 | const payload = |
| 334 | "--boundary\r\nContent-Disposition: form-data; name=\"message\"\r\n\r\nhello\r\n--boundary--\r\n" |
| 335 | const response = yield* expectContinueRequest(tcpPort(server), payload, { |
| 336 | "content-type": "multipart/form-data; boundary=boundary" |
| 337 | }) |
| 338 | assert.strictEqual(response.status, 200) |
| 339 | assert.strictEqual(response.body, "hello") |
| 340 | assert.strictEqual(response.continues, 1) |
| 341 | assert.isTrue(response.uploaded) |
| 342 | }) |
| 343 | ) |
| 344 | |
| 345 | it.effect("sends one 100 Continue across repeated original and modified body reads", () => |
| 346 | Effect.gen(function*() { |
| 347 | const server = Http.createServer() |
| 348 | yield* HttpServer.serve( |
| 349 | HttpServerRequest.HttpServerRequest.use((original) => |
| 350 | Effect.gen(function*() { |
| 351 | const modified = original.modify({ url: "/body" }) |
| 352 | // Start both readers before the client uploads, then read their cached bodies again. |
| 353 | const bodies = yield* Effect.all([original.text, modified.text], { concurrency: "unbounded" }) |
| 354 | const repeated = yield* Effect.all([original.text, modified.text]) |
| 355 | return HttpServerResponse.text([...bodies, ...repeated].join(",")) |
| 356 | }) |
| 357 | ) |
| 358 | ).pipe( |
| 359 | Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })), |
| 360 | Layer.build |
| 361 | ) |
| 362 | |
| 363 | const response = yield* expectContinueRequest(tcpPort(server), "hello") |
| 364 | assert.strictEqual(response.status, 200) |
| 365 | assert.strictEqual(response.body, "hello,hello,hello,hello") |
| 366 | assert.strictEqual(response.continues, 1) |
| 367 | assert.isTrue(response.uploaded) |
| 368 | })) |
| 369 | |
| 370 | it.effect("keeps routes isolated between independent servers", () => |
| 371 | Effect.gen(function*() { |
| 372 | const publicServer = Http.createServer() |
| 373 | const internalServer = Http.createServer() |
| 374 | |
| 375 | const Health = Layer.effectDiscard( |
| 376 | Effect.flatMap(HttpRouter.HttpRouter, (router) => |
| 377 | router.add("GET", "/health", HttpServerResponse.text("healthy"))) |
| 378 | ) |
| 379 | const publicApp = Layer.mergeAll( |
| 380 | HttpRouter.add("GET", "/public", HttpServerResponse.text("public")), |
| 381 | Health |
| 382 | ) |
| 383 | const internalApp = Layer.mergeAll( |
| 384 | HttpRouter.add("GET", "/internal", HttpServerResponse.text("internal")), |
| 385 | Health |
| 386 | ) |
| 387 | |
| 388 | yield* Layer.mergeAll( |
| 389 | HttpRouter.serve(publicApp, { disableListenLog: true, disableLogger: true }).pipe( |
| 390 | Layer.provide(NodeHttpServer.layer(() => |
| 391 | publicServer, { port: 0 })) |
| 392 | ), |
| 393 | HttpRouter.serve(internalApp, { disableListenLog: true, disableLogger: true }).pipe( |
| 394 | Layer.provide(NodeHttpServer.layer(() => internalServer, { port: 0 })) |
| 395 | ) |
| 396 | ).pipe(Layer.build) |
| 397 | |
| 398 | const status = (port: number, path: string) => |
| 399 | Effect.promise(() => fetch("http://localhost:" + port + path).then((response) => response.status)) |
| 400 | |
| 401 | assert.strictEqual(yield* status(tcpPort(publicServer), "/public"), 200) |
| 402 | assert.strictEqual(yield* status(tcpPort(internalServer), "/internal"), 200) |
| 403 | assert.strictEqual(yield* status(tcpPort(publicServer), "/health"), 200) |
| 404 | assert.strictEqual(yield* status(tcpPort(internalServer), "/health"), 200) |
| 405 | assert.strictEqual(yield* status(tcpPort(publicServer), "/internal"), 404) |
| 406 | assert.strictEqual(yield* status(tcpPort(internalServer), "/public"), 404) |
| 407 | })) |
| 408 | |
| 409 | it.effect("takes the host header from the HTTP/2 authority", () => |
| 410 | Effect.gen(function*() { |
| 411 | const server = Http2.createServer() |
| 412 | yield* HttpServer.serve( |
| 413 | HttpServerRequest.HttpServerRequest.use((request) => |
| 414 | Effect.succeed(HttpServerResponse.text( |
| 415 | `${request.headers.host} ${Option.getOrThrow(HttpServerRequest.toURL(request)).href}` |
| 416 | )) |
| 417 | ) |
| 418 | ).pipe( |
| 419 | Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })), |
| 420 | Layer.build |
| 421 | ) |
| 422 | const port = tcpPort(server) |
| 423 | |
| 424 | for (const host of [undefined, "client.test"]) { |
| 425 | assert.strictEqual( |
| 426 | yield* http2Get(port, { ":path": "/info", ":authority": "example.test:4573", host }), |
| 427 | "example.test:4573 http://example.test:4573/info" |
| 428 | ) |
| 429 | } |
| 430 | })) |
| 431 | |
| 432 | it.live("acknowledges Expect: 100-continue over HTTP/2", () => |
| 433 | Effect.gen(function*() { |
| 434 | const server = Http2.createServer() |
| 435 | yield* HttpServer.serve( |
| 436 | HttpServerRequest.HttpServerRequest.use((request) => Effect.map(request.text, HttpServerResponse.text)) |
| 437 | ).pipe( |
| 438 | Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })), |
| 439 | Layer.build |
| 440 | ) |
| 441 | const session = yield* Effect.acquireRelease( |
| 442 | Effect.sync(() => Http2.connect(`http://127.0.0.1:${tcpPort(server)}`)), |
| 443 | (session) => Effect.sync(() => session.destroy()) |
| 444 | ) |
| 445 | const body = yield* Effect.callback<string, Error>((resume) => { |
| 446 | const stream = session.request({ ":method": "POST", ":path": "/", expect: "100-continue" }) |
| 447 | let body = "" |
| 448 | stream.setEncoding("utf8") |
| 449 | stream.on("continue", () => stream.end("hello")) |
| 450 | stream.on("data", (chunk: string) => { |
| 451 | body += chunk |
| 452 | }) |
| 453 | stream.on("end", () => resume(Effect.succeed(body))) |
| 454 | stream.on("error", (error) => resume(Effect.fail(error))) |
| 455 | }).pipe(Effect.timeout("2 seconds")) |
| 456 | assert.strictEqual(body, "hello") |
| 457 | })) |
| 458 | |
| 459 | it.effect("schema", () => |
| 460 | Effect.gen(function*() { |
| 461 | yield* HttpRouter.add( |
| 462 | "GET", |
| 463 | "/todos/:id", |
| 464 | Effect.flatMap( |
| 465 | HttpRouter.schemaParams(IdParams), |
| 466 | ({ id }) => todoResponse({ id, title: "test" }) |
| 467 | ) |
| 468 | ).pipe( |
| 469 | HttpRouter.serve, |
| 470 | Layer.build |
| 471 | ) |
| 472 | const todo = yield* HttpClient.get("/todos/1").pipe( |
| 473 | Effect.flatMap(HttpClientResponse.schemaBodyJson(Todo)) |
| 474 | ) |
| 475 | expect(todo).toEqual({ id: 1, title: "test" }) |
| 476 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 477 | |
| 478 | it.effect("toServerResponse works behind the decompression middleware", () => |
| 479 | Effect.gen(function*() { |
| 480 | yield* HttpServer.serveEffect( |
| 481 | HttpServerRequest.HttpServerRequest.use((request) => { |
| 482 | const responses = [request, request.modify({ url: "/modified" })].map(NodeHttpServerRequest.toServerResponse) |
| 483 | return Effect.succeed( |
| 484 | HttpServerResponse.text(responses.map((response) => response instanceof Http.ServerResponse).join(",")) |
| 485 | ) |
| 486 | }), |
| 487 | HttpMiddleware.decompression() |
| 488 | ) |
| 489 | const response = yield* HttpClient.post("/", { |
| 490 | body: HttpBody.uint8Array(Zlib.gzipSync("hello")), |
| 491 | headers: { "content-encoding": "gzip" } |
| 492 | }) |
| 493 | assert.strictEqual(yield* response.text, "true,true") |
| 494 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 495 | |
| 496 | it.effect("redacts query params when inspecting requests", () => |
| 497 | Effect.gen(function*() { |
| 498 | yield* HttpRouter.add( |
| 499 | "GET", |
| 500 | "/inspect", |
| 501 | Effect.gen(function*() { |
| 502 | const request = yield* HttpServerRequest.HttpServerRequest |
| 503 | return yield* HttpServerResponse.json({ |
| 504 | url: request.url, |
| 505 | json: JSON.stringify(request), |
| 506 | formatted: Formatter.format(request) |
| 507 | }) |
| 508 | }).pipe(Effect.provideService(UrlParams.CurrentRedactedNames, ["token"])) |
| 509 | ).pipe(HttpRouter.serve, Layer.build) |
| 510 | |
| 511 | const result = (yield* HttpClient.get("/inspect?token=token-secret&sig=sig-visible").pipe( |
| 512 | Effect.flatMap((response) => response.json) |
| 513 | )) as { readonly url: string; readonly json: string; readonly formatted: string } |
| 514 | |
| 515 | assert.strictEqual(result.url, "/inspect?token=token-secret&sig=sig-visible") |
| 516 | assert.match(result.json, /"url":"http:\/\/[^/"]+\/inspect\?token=REDACTED&sig=sig-visible"/) |
| 517 | assert.include(result.formatted, "/inspect?token=REDACTED&sig=sig-visible") |
| 518 | assert.notInclude(result.formatted, "token-secret") |
| 519 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 520 | |
| 521 | it.effect("redacts configured headers when inspecting requests", () => |
| 522 | Effect.gen(function*() { |
| 523 | yield* HttpRouter.add( |
| 524 | "GET", |
| 525 | "/inspect", |
| 526 | Effect.gen(function*() { |
| 527 | const request = yield* HttpServerRequest.HttpServerRequest |
| 528 | return yield* HttpServerResponse.json({ |
| 529 | json: JSON.stringify(request.headers), |
| 530 | formatted: Formatter.format(request.headers) |
| 531 | }) |
| 532 | }).pipe(Effect.provideService(Headers.CurrentRedactedNames, ["x-secret"])) |
| 533 | ).pipe(HttpRouter.serve, Layer.build) |
| 534 | |
| 535 | const result = (yield* HttpClient.get("/inspect", { headers: { "x-secret": "header-secret" } }).pipe( |
| 536 | Effect.flatMap((response) => response.json) |
| 537 | )) as { readonly json: string; readonly formatted: string } |
| 538 | |
| 539 | assert.include(result.json, `"x-secret":"<redacted>"`) |
| 540 | assert.notInclude(result.json, "header-secret") |
| 541 | assert.notInclude(result.formatted, "header-secret") |
| 542 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 543 | |
| 544 | it.effect("schemaJson applies a JSON reviver", () => |
| 545 | Effect.gen(function*() { |
| 546 | yield* HttpRouter.add( |
| 547 | "POST", |
| 548 | "/json", |
| 549 | Effect.gen(function*() { |
| 550 | const decoded = yield* HttpRouter.schemaJson( |
| 551 | Schema.Struct({ body: Schema.Struct({ value: Schema.String }) }), |
| 552 | { reviver: (key, value) => key === "value" ? "revived" : value } |
| 553 | ) |
| 554 | assert.deepStrictEqual(decoded, { body: { value: "revived" } }) |
| 555 | return HttpServerResponse.empty() |
| 556 | }) |
| 557 | ).pipe(HttpRouter.serve, Layer.build) |
| 558 | |
| 559 | const response = yield* HttpClient.post("/json", { |
| 560 | body: HttpBody.jsonUnsafe({ value: "original" }) |
| 561 | }) |
| 562 | assert.strictEqual(response.status, 204) |
| 563 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 564 | |
| 565 | it.effect.each(["Uploaded", ""] as const)("forwards status text %j", (statusText) => |
| 566 | Effect.gen(function*() { |
| 567 | yield* HttpRouter.add( |
| 568 | "GET", |
| 569 | "/", |
| 570 | HttpServerResponse.empty({ status: 201, statusText }) |
| 571 | ).pipe( |
| 572 | HttpRouter.serve, |
| 573 | Layer.build |
| 574 | ) |
| 575 | const server = yield* HttpServer.HttpServer |
| 576 | assert.isTrue(NetAddress.isInetAddress(server.address)) |
| 577 | if (!NetAddress.isInetAddress(server.address)) return |
| 578 | const port = server.address.port |
| 579 | |
| 580 | assert.strictEqual(yield* getStatusText(port), statusText) |
| 581 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 582 | |
| 583 | it.effect("formData", () => |
| 584 | Effect.gen(function*() { |
| 585 | yield* HttpRouter.add( |
| 586 | "POST", |
| 587 | "/upload", |
| 588 | Effect.gen(function*() { |
| 589 | const request = yield* HttpServerRequest.HttpServerRequest |
| 590 | const formData = yield* request.multipart |
| 591 | const part = formData.file |
| 592 | assert(typeof part !== "string") |
| 593 | const file = part[0] |
| 594 | assert(typeof file !== "string") |
| 595 | expect(file.path.endsWith("/test.txt")).toEqual(true) |
| 596 | expect(file.contentType).toEqual("text/plain") |
| 597 | return yield* HttpServerResponse.json({ ok: "file" in formData }) |
| 598 | }) |
| 599 | ).pipe( |
| 600 | HttpRouter.serve, |
| 601 | Layer.build |
| 602 | ) |
| 603 | const client = yield* HttpClient.HttpClient |
| 604 | const formData = new FormData() |
| 605 | formData.append("file", new Blob(["test"], { type: "text/plain" }), "test.txt") |
| 606 | const result = yield* client.post("/upload", { body: HttpBody.formData(formData) }).pipe( |
| 607 | Effect.flatMap((r) => r.json) |
| 608 | ) |
| 609 | expect(result).toEqual({ ok: true }) |
| 610 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 611 | |
| 612 | it.effect("schemaBodyForm", () => |
| 613 | Effect.gen(function*() { |
| 614 | yield* HttpRouter.add( |
| 615 | "POST", |
| 616 | "/upload", |
| 617 | Effect.gen(function*() { |
| 618 | const files = yield* HttpServerRequest.schemaBodyForm(Schema.Struct({ |
| 619 | file: Multipart.FilesSchema, |
| 620 | test: Schema.String |
| 621 | })) |
| 622 | expect(files).toHaveProperty("file") |
| 623 | expect(files).toHaveProperty("test") |
| 624 | return HttpServerResponse.empty() |
| 625 | }) |
| 626 | ).pipe( |
| 627 | HttpRouter.serve, |
| 628 | Layer.build |
| 629 | ) |
| 630 | const client = yield* HttpClient.HttpClient |
| 631 | const formData = new FormData() |
| 632 | formData.append("file", new Blob(["test"], { type: "text/plain" }), "test.txt") |
| 633 | formData.append("test", "test") |
| 634 | const response = yield* client.post("/upload", { body: HttpBody.formData(formData) }) |
| 635 | expect(response.status).toEqual(204) |
| 636 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 637 | |
| 638 | it.effect("formData withMaxFileSize", () => |
| 639 | Effect.gen(function*() { |
| 640 | yield* HttpRouter.add( |
| 641 | "POST", |
| 642 | "/upload", |
| 643 | Effect.gen(function*() { |
| 644 | const request = yield* HttpServerRequest.HttpServerRequest |
| 645 | yield* request.multipart |
| 646 | return HttpServerResponse.empty() |
| 647 | }).pipe( |
| 648 | Effect.catchTag("MultipartError", (error) => |
| 649 | error.reason._tag === "FileTooLarge" ? |
| 650 | Effect.succeed(HttpServerResponse.empty({ status: 413 })) : |
| 651 | Effect.fail(error)) |
| 652 | ) |
| 653 | ).pipe( |
| 654 | HttpRouter.serve, |
| 655 | Layer.build, |
| 656 | Effect.provideService(Multipart.MaxFileSize, ByteSize.bytes(100)) |
| 657 | ) |
| 658 | const client = yield* HttpClient.HttpClient |
| 659 | const formData = new FormData() |
| 660 | const data = new Uint8Array(1000) |
| 661 | formData.append("file", new Blob([data], { type: "text/plain" }), "test.txt") |
| 662 | const response = yield* client.post("/upload", { body: HttpBody.formData(formData) }) |
| 663 | expect(response.status).toEqual(413) |
| 664 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 665 | |
| 666 | it.effect("formData withMaxFieldSize", () => |
| 667 | Effect.gen(function*() { |
| 668 | yield* HttpRouter.add( |
| 669 | "POST", |
| 670 | "/upload", |
| 671 | Effect.gen(function*() { |
| 672 | const request = yield* HttpServerRequest.HttpServerRequest |
| 673 | yield* request.multipart |
| 674 | return HttpServerResponse.empty() |
| 675 | }).pipe( |
| 676 | Effect.catchTag("MultipartError", (error) => |
| 677 | error.reason._tag === "FieldTooLarge" ? |
| 678 | Effect.succeed(HttpServerResponse.empty({ status: 413 })) : |
| 679 | Effect.fail(error)) |
| 680 | ) |
| 681 | ).pipe( |
| 682 | HttpRouter.serve, |
| 683 | Layer.build, |
| 684 | Effect.provideService(Multipart.MaxFieldSize, ByteSize.bytes(100)) |
| 685 | ) |
| 686 | const client = yield* HttpClient.HttpClient |
| 687 | const formData = new FormData() |
| 688 | const data = new Uint8Array(1000).fill(1) |
| 689 | formData.append("file", new TextDecoder().decode(data)) |
| 690 | const response = yield* client.post("/upload", { body: HttpBody.formData(formData) }) |
| 691 | expect(response.status).toEqual(413) |
| 692 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 693 | |
| 694 | it.effect("mountApp", () => |
| 695 | Effect.gen(function*() { |
| 696 | const child = Effect.map(HttpServerRequest.HttpServerRequest, (_) => HttpServerResponse.text(_.url)) |
| 697 | yield* HttpRouter.use((router) => router.prefixed("/child").add("*", "*", child)).pipe( |
| 698 | HttpRouter.serve, |
| 699 | Layer.build |
| 700 | ) |
| 701 | const client = yield* HttpClient.HttpClient |
| 702 | const todo = yield* client.get("/child/1").pipe(Effect.flatMap((_) => _.text)) |
| 703 | expect(todo).toEqual("/1") |
| 704 | const root = yield* client.get("/child").pipe(Effect.flatMap((_) => _.text)) |
| 705 | expect(root).toEqual("/") |
| 706 | const rootSearch = yield* client.get("/child?foo=bar").pipe(Effect.flatMap((_) => _.text)) |
| 707 | expect(rootSearch).toEqual("/?foo=bar") |
| 708 | const rootSlash = yield* client.get("/child/").pipe(Effect.flatMap((_) => _.text)) |
| 709 | expect(rootSlash).toEqual("/") |
| 710 | const invalid = yield* client.get("/child1/", { |
| 711 | urlParams: { foo: "bar" } |
| 712 | }).pipe(Effect.map((_) => _.status)) |
| 713 | expect(invalid).toEqual(404) |
| 714 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 715 | |
| 716 | it.effect("file", () => |
| 717 | Effect.gen(function*() { |
| 718 | yield* (yield* HttpServerResponse.file(`${__dirname}/fixtures/text.txt`).pipe( |
| 719 | Effect.updateService( |
| 720 | HttpPlatform.HttpPlatform, |
| 721 | (_) => ({ |
| 722 | ..._, |
| 723 | fileResponse: (path, options) => |
| 724 | Effect.map( |
| 725 | _.fileResponse(path, options), |
| 726 | (res) => { |
| 727 | ;(res as any).headers.etag = "\"etag\"" |
| 728 | return res |
| 729 | } |
| 730 | ) |
| 731 | }) |
| 732 | ) |
| 733 | )).pipe( |
| 734 | Effect.succeed, |
| 735 | HttpServer.serveEffect() |
| 736 | ) |
| 737 | const client = yield* HttpClient.HttpClient |
| 738 | const res = yield* client.get("/") |
| 739 | expect(res.status).toEqual(200) |
| 740 | expect(res.headers["content-type"]).toEqual("text/plain") |
| 741 | expect(res.headers["content-length"]).toEqual("27") |
| 742 | expect(res.headers.etag).toEqual("\"etag\"") |
| 743 | const text = yield* res.text |
| 744 | expect(text.trim()).toEqual("lorem ipsum dolar sit amet") |
| 745 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 746 | |
| 747 | it.effect("fileWeb", () => |
| 748 | Effect.gen(function*() { |
| 749 | const now = new Date() |
| 750 | const file = new Buffer.File([new TextEncoder().encode("test")], "test.txt", { |
| 751 | type: "text/plain", |
| 752 | lastModified: now.getTime() |
| 753 | }) |
| 754 | yield* HttpServerResponse.fileWeb(file).pipe( |
| 755 | Effect.updateService( |
| 756 | HttpPlatform.HttpPlatform, |
| 757 | (_) => ({ |
| 758 | ..._, |
| 759 | fileWebResponse: (path, options) => |
| 760 | Effect.map( |
| 761 | _.fileWebResponse(path, options), |
| 762 | (res) => ({ ...res, headers: { ...res.headers, etag: "W/\"etag\"" } }) |
| 763 | ) |
| 764 | }) |
| 765 | ), |
| 766 | HttpServer.serveEffect() |
| 767 | ) |
| 768 | const client = yield* HttpClient.HttpClient |
| 769 | const res = yield* client.get("/") |
| 770 | expect(res.status).toEqual(200) |
| 771 | expect(res.headers["content-type"]).toEqual("text/plain") |
| 772 | expect(res.headers["content-length"]).toEqual("4") |
| 773 | expect(res.headers["last-modified"]).toEqual(now.toUTCString()) |
| 774 | expect(res.headers.etag).toEqual("W/\"etag\"") |
| 775 | const text = yield* res.text |
| 776 | expect(text.trim()).toEqual("test") |
| 777 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 778 | |
| 779 | it.effect("schemaBodyUrlParams", () => |
| 780 | Effect.gen(function*() { |
| 781 | yield* HttpRouter.add( |
| 782 | "POST", |
| 783 | "/todos", |
| 784 | Effect.flatMap( |
| 785 | HttpServerRequest.schemaBodyUrlParams(Schema.Struct({ |
| 786 | id: Schema.FiniteFromString, |
| 787 | title: Schema.String |
| 788 | })), |
| 789 | ({ id, title }) => todoResponse({ id, title }) |
| 790 | ) |
| 791 | ).pipe( |
| 792 | HttpRouter.serve, |
| 793 | Layer.build |
| 794 | ) |
| 795 | const todo = yield* HttpClientRequest.post("/todos").pipe( |
| 796 | HttpClientRequest.bodyUrlParams({ id: "1", title: "test" }), |
| 797 | HttpClient.execute, |
| 798 | Effect.flatMap(HttpClientResponse.schemaBodyJson(Todo)) |
| 799 | ) |
| 800 | expect(todo).toEqual({ id: 1, title: "test" }) |
| 801 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 802 | |
| 803 | it.effect("schemaBodyUrlParams error", () => |
| 804 | Effect.gen(function*() { |
| 805 | yield* HttpRouter.add( |
| 806 | "GET", |
| 807 | "/todos", |
| 808 | Effect.flatMap( |
| 809 | HttpServerRequest.schemaBodyUrlParams(Schema.Struct({ |
| 810 | id: Schema.FiniteFromString, |
| 811 | title: Schema.String |
| 812 | })), |
| 813 | ({ id, title }) => todoResponse({ id, title }) |
| 814 | ).pipe( |
| 815 | Effect.catchTag("SchemaError", (error) => |
| 816 | Effect.succeed(HttpServerResponse.jsonUnsafe({ error }, { status: 400 }))) |
| 817 | ) |
| 818 | ).pipe( |
| 819 | HttpRouter.serve, |
| 820 | Layer.build |
| 821 | ) |
| 822 | const client = yield* HttpClient.HttpClient |
| 823 | const response = yield* client.get("/todos") |
| 824 | expect(response.status).toEqual(400) |
| 825 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 826 | |
| 827 | it.effect("schemaBodyFormJson", () => |
| 828 | Effect.gen(function*() { |
| 829 | yield* HttpRouter.add( |
| 830 | "POST", |
| 831 | "/upload", |
| 832 | Effect.gen(function*() { |
| 833 | const result = yield* HttpServerRequest.schemaBodyFormJson( |
| 834 | Schema.Struct({ |
| 835 | test: Schema.String |
| 836 | }), |
| 837 | { reviver: (key, value) => key === "test" ? "revived" : value } |
| 838 | )("json") |
| 839 | expect(result.test).toEqual("revived") |
| 840 | return HttpServerResponse.empty() |
| 841 | }) |
| 842 | ).pipe( |
| 843 | HttpRouter.serve, |
| 844 | Layer.build |
| 845 | ) |
| 846 | const client = yield* HttpClient.HttpClient |
| 847 | const formData = new FormData() |
| 848 | formData.append("json", JSON.stringify({ test: "content" })) |
| 849 | const response = yield* client.post("/upload", { body: HttpBody.formData(formData) }) |
| 850 | expect(response.status).toEqual(204) |
| 851 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 852 | |
| 853 | it.effect("schemaBodyFormJson file", () => |
| 854 | Effect.gen(function*() { |
| 855 | yield* HttpRouter.add( |
| 856 | "POST", |
| 857 | "/upload", |
| 858 | Effect.gen(function*() { |
| 859 | const result = yield* HttpServerRequest.schemaBodyFormJson(Schema.Struct({ |
| 860 | test: Schema.String |
| 861 | }))("json") |
| 862 | |
| 863 | expect(result.test).toEqual("content") |
| 864 | return HttpServerResponse.empty() |
| 865 | }) |
| 866 | ).pipe( |
| 867 | HttpRouter.serve, |
| 868 | Layer.build |
| 869 | ) |
| 870 | const client = yield* HttpClient.HttpClient |
| 871 | const formData = new FormData() |
| 872 | formData.append( |
| 873 | "json", |
| 874 | new Blob([JSON.stringify({ test: "content" })], { type: "application/json" }), |
| 875 | "test.json" |
| 876 | ) |
| 877 | const response = yield* client.post("/upload", { body: HttpBody.formData(formData) }) |
| 878 | expect(response.status).toEqual(204) |
| 879 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 880 | |
| 881 | it.effect("schemaBodyFormJson url encoded", () => |
| 882 | Effect.gen(function*() { |
| 883 | yield* HttpRouter.add( |
| 884 | "POST", |
| 885 | "/upload", |
| 886 | Effect.gen(function*() { |
| 887 | const result = yield* HttpServerRequest.schemaBodyFormJson(Schema.Struct({ |
| 888 | test: Schema.String |
| 889 | }))("json") |
| 890 | expect(result.test).toEqual("content") |
| 891 | return HttpServerResponse.empty() |
| 892 | }) |
| 893 | ).pipe( |
| 894 | HttpRouter.serve, |
| 895 | Layer.build |
| 896 | ) |
| 897 | const client = yield* HttpClient.HttpClient |
| 898 | const response = yield* client.post("/upload", { |
| 899 | body: HttpBody.urlParams(UrlParams.fromInput({ |
| 900 | json: JSON.stringify({ test: "content" }) |
| 901 | })) |
| 902 | }) |
| 903 | expect(response.status).toEqual(204) |
| 904 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 905 | |
| 906 | it.effect("tracing", () => |
| 907 | Effect.gen(function*() { |
| 908 | yield* HttpRouter.add( |
| 909 | "GET", |
| 910 | "/", |
| 911 | Effect.flatMap( |
| 912 | Effect.currentSpan, |
| 913 | (_) => HttpServerResponse.json({ spanId: _.spanId, parent: _.parent }) |
| 914 | ) |
| 915 | ).pipe( |
| 916 | HttpRouter.serve, |
| 917 | Layer.build |
| 918 | ) |
| 919 | const client = yield* HttpClient.HttpClient |
| 920 | const requestSpan = yield* Effect.makeSpan("client request") |
| 921 | const body = yield* client.get("/").pipe( |
| 922 | Effect.flatMap((r) => r.json), |
| 923 | Effect.provideService( |
| 924 | Tracer.Tracer, |
| 925 | Tracer.make({ |
| 926 | span(options) { |
| 927 | assert.strictEqual(options.name, "GET") |
| 928 | assert.strictEqual(options.kind, "client") |
| 929 | assert(options.parent._tag === "Some") |
| 930 | if (options.parent.value._tag !== "Span") { |
| 931 | throw new Error("Expected span parent") |
| 932 | } |
| 933 | assert.strictEqual(options.parent.value.name, "request parent") |
| 934 | return requestSpan |
| 935 | } |
| 936 | }) |
| 937 | ), |
| 938 | Effect.withSpan("request parent"), |
| 939 | Effect.repeat({ times: 2 }) |
| 940 | ) |
| 941 | expect((body as any).parent._tag).toEqual("Some") |
| 942 | expect((body as any).parent.value.spanId).toEqual(requestSpan.spanId) |
| 943 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 944 | |
| 945 | it.effect("html", () => |
| 946 | Effect.gen(function*() { |
| 947 | yield* HttpRouter.addAll([ |
| 948 | HttpRouter.route("GET", "/home", HttpServerResponse.html("<html />")), |
| 949 | HttpRouter.route( |
| 950 | "GET", |
| 951 | "/about", |
| 952 | HttpServerResponse.html`<html>${Effect.succeed("<body />")}</html>` |
| 953 | ), |
| 954 | HttpRouter.route( |
| 955 | "GET", |
| 956 | "/stream", |
| 957 | HttpServerResponse.htmlStream`<html>${Stream.make("<body />", 123, "hello")}</html>` |
| 958 | ) |
| 959 | ]).pipe( |
| 960 | HttpRouter.serve, |
| 961 | Layer.build |
| 962 | ) |
| 963 | const client = yield* HttpClient.HttpClient |
| 964 | const home = yield* client.get("/home").pipe(Effect.flatMap((r) => r.text)) |
| 965 | expect(home).toEqual("<html />") |
| 966 | const about = yield* client.get("/about").pipe(Effect.flatMap((r) => r.text)) |
| 967 | expect(about).toEqual("<html><body /></html>") |
| 968 | const stream = yield* client.get("/stream").pipe(Effect.flatMap((r) => r.text)) |
| 969 | expect(stream).toEqual("<html><body />123hello</html>") |
| 970 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 971 | |
| 972 | it.effect("setCookie", () => |
| 973 | Effect.gen(function*() { |
| 974 | yield* HttpRouter.add( |
| 975 | "GET", |
| 976 | "/home", |
| 977 | HttpServerResponse.empty().pipe( |
| 978 | HttpServerResponse.setCookieUnsafe("test", "value"), |
| 979 | HttpServerResponse.setCookieUnsafe("test2", "value2", { |
| 980 | httpOnly: true, |
| 981 | secure: true, |
| 982 | sameSite: "lax", |
| 983 | partitioned: true, |
| 984 | path: "/", |
| 985 | domain: "example.com", |
| 986 | expires: new Date(2022, 1, 1, 0, 0, 0, 0), |
| 987 | maxAge: "5 minutes" |
| 988 | }) |
| 989 | ) |
| 990 | ).pipe( |
| 991 | HttpRouter.serve, |
| 992 | Layer.build |
| 993 | ) |
| 994 | const client = yield* HttpClient.HttpClient |
| 995 | const res = yield* client.get("/home") |
| 996 | assert.deepStrictEqual( |
| 997 | res.cookies.toJSON(), |
| 998 | Cookies.fromReadonlyRecord({ |
| 999 | test: Cookies.makeCookieUnsafe("test", "value"), |
| 1000 | test2: Cookies.makeCookieUnsafe("test2", "value2", { |
| 1001 | httpOnly: true, |
| 1002 | secure: true, |
| 1003 | sameSite: "lax", |
| 1004 | partitioned: true, |
| 1005 | path: "/", |
| 1006 | domain: "example.com", |
| 1007 | expires: new Date(2022, 1, 1, 0, 0, 0, 0), |
| 1008 | maxAge: Duration.minutes(5) |
| 1009 | }) |
| 1010 | }).toJSON() |
| 1011 | ) |
| 1012 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 1013 | |
| 1014 | it.live("uninterruptible routes", () => |
| 1015 | Effect.gen(function*() { |
| 1016 | yield* HttpRouter.add( |
| 1017 | "GET", |
| 1018 | "/home", |
| 1019 | Effect.gen(function*() { |
| 1020 | const fiber = Fiber.getCurrent()! |
| 1021 | setTimeout(() => fiber.interruptUnsafe(fiber.id), 10) |
| 1022 | yield* Effect.sleep(50) |
| 1023 | return HttpServerResponse.empty() |
| 1024 | }), |
| 1025 | { uninterruptible: true } |
| 1026 | ).pipe( |
| 1027 | HttpRouter.serve, |
| 1028 | Layer.build |
| 1029 | ) |
| 1030 | const client = yield* HttpClient.HttpClient |
| 1031 | const res = yield* client.get("/home") |
| 1032 | assert.strictEqual(res.status, 204) |
| 1033 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 1034 | |
| 1035 | it.effect("completes a HEAD response once when close precedes the end callback", () => |
| 1036 | Effect.gen(function*() { |
| 1037 | const scope = yield* Effect.scope |
| 1038 | const handler = yield* NodeHttpServer.makeHandler( |
| 1039 | Effect.succeed(HttpServerResponse.empty()), |
| 1040 | { scope } |
| 1041 | ) |
| 1042 | const completed = Latch.makeUnsafe() |
| 1043 | let writableEnded = false |
| 1044 | const nodeResponse = Object.defineProperty(new EventEmitter(), "writableEnded", { |
| 1045 | get: () => writableEnded |
| 1046 | }) as Http.ServerResponse |
| 1047 | let closeListenerRemovals = 0 |
| 1048 | nodeResponse.writeHead = () => nodeResponse |
| 1049 | nodeResponse.off = ((event: string | symbol, listener: (...args: Array<unknown>) => void) => { |
| 1050 | if (event === "close") { |
| 1051 | closeListenerRemovals++ |
| 1052 | } |
| 1053 | return EventEmitter.prototype.off.call(nodeResponse, event, listener) as Http.ServerResponse |
| 1054 | }) as Http.ServerResponse["off"] |
| 1055 | nodeResponse.end = ((callback: () => void) => { |
| 1056 | writableEnded = true |
| 1057 | nodeResponse.emit("close") |
| 1058 | callback() |
| 1059 | completed.openUnsafe() |
| 1060 | return nodeResponse |
| 1061 | }) as Http.ServerResponse["end"] |
| 1062 | |
| 1063 | handler( |
| 1064 | { method: "HEAD", url: "/", headers: {}, socket: {} } as Http.IncomingMessage, |
| 1065 | nodeResponse |
| 1066 | ) |
| 1067 | yield* completed.await |
| 1068 | |
| 1069 | assert.strictEqual(closeListenerRemovals, 1) |
| 1070 | })) |
| 1071 | |
| 1072 | it.live.each([ |
| 1073 | { body: "Uint8Array", event: "close", response: () => HttpServerResponse.uint8Array(new Uint8Array(1024)) }, |
| 1074 | { body: "Raw", event: "aborted", response: () => HttpServerResponse.raw(new Uint8Array(1024)) } |
| 1075 | ])("completes a $body response sent on $event after a disconnect", ({ event, response }) => |
| 1076 | Effect.gen(function*() { |
| 1077 | const server = Http.createServer() |
| 1078 | const scope = yield* Scope.make() |
| 1079 | // A stuck uninterruptible request fiber would block the server's shutdown |
| 1080 | yield* Effect.addFinalizer(() => Effect.forkDetach(Scope.close(scope, Exit.void))) |
| 1081 | const received = yield* Latch.make() |
| 1082 | const sent = yield* Deferred.make<HttpServerResponse.HttpServerResponse, unknown>() |
| 1083 | yield* HttpRouter.add( |
| 1084 | "GET", |
| 1085 | "/", |
| 1086 | Effect.gen(function*() { |
| 1087 | const incoming = NodeHttpServerRequest.toIncomingMessage(yield* HttpServerRequest.HttpServerRequest) |
| 1088 | yield* received.open |
| 1089 | // Node emits the request's `aborted` before it marks the response destroyed |
| 1090 | yield* Effect.callback<void>((resume) => { |
| 1091 | if (incoming.socket.destroyed) return resume(Effect.void) |
| 1092 | ;(event === "aborted" ? incoming : incoming.socket).once(event, () => resume(Effect.void)) |
| 1093 | }) |
| 1094 | return response() |
| 1095 | }), |
| 1096 | { uninterruptible: true } |
| 1097 | ).pipe( |
| 1098 | (app) => |
| 1099 | HttpRouter.serve(app, { middleware: (app) => Effect.onExit(app, (exit) => Deferred.done(sent, exit)) }), |
| 1100 | Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })), |
| 1101 | Layer.buildWithScope(scope) |
| 1102 | ) |
| 1103 | const socket = Net.connect({ port: tcpPort(server), host: "127.0.0.1" }, () => { |
| 1104 | socket.write("GET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n") |
| 1105 | }) |
| 1106 | socket.on("error", constVoid) |
| 1107 | yield* received.await |
| 1108 | socket.destroy() |
| 1109 | const res = yield* Deferred.await(sent).pipe(Effect.timeout("2 seconds")) |
| 1110 | assert.strictEqual(res.status, 200) |
| 1111 | })) |
| 1112 | |
| 1113 | it.effect("returns none when a late remoteAddress read finds no socket", () => |
| 1114 | Effect.gen(function*() { |
| 1115 | const scope = yield* Effect.scope |
| 1116 | let request: HttpServerRequest.HttpServerRequest | undefined |
| 1117 | const handler = yield* NodeHttpServer.makeHandler( |
| 1118 | Effect.gen(function*() { |
| 1119 | request = yield* HttpServerRequest.HttpServerRequest |
| 1120 | return HttpServerResponse.empty() |
| 1121 | }), |
| 1122 | { scope } |
| 1123 | ) |
| 1124 | const completed = Latch.makeUnsafe() |
| 1125 | let writableEnded = false |
| 1126 | const nodeResponse = Object.defineProperty(new EventEmitter(), "writableEnded", { |
| 1127 | get: () => writableEnded |
| 1128 | }) as Http.ServerResponse |
| 1129 | nodeResponse.writeHead = () => nodeResponse |
| 1130 | nodeResponse.end = (() => { |
| 1131 | writableEnded = true |
| 1132 | completed.openUnsafe() |
| 1133 | return nodeResponse |
| 1134 | }) as Http.ServerResponse["end"] |
| 1135 | const nodeRequest = { |
| 1136 | method: "GET", |
| 1137 | url: "/", |
| 1138 | headers: {}, |
| 1139 | socket: { remoteAddress: "127.0.0.1" } |
| 1140 | } as unknown as Http.IncomingMessage |
| 1141 | |
| 1142 | handler(nodeRequest, nodeResponse) |
| 1143 | yield* completed.await |
| 1144 | assert(request !== undefined) |
| 1145 | ;(nodeRequest as unknown as { socket: null }).socket = null |
| 1146 | |
| 1147 | assert.deepStrictEqual(request.remoteAddress, Option.none()) |
| 1148 | })) |
| 1149 | |
| 1150 | it.effect("coalesces streaming chunks from the same pull", () => |
| 1151 | Effect.gen(function*() { |
| 1152 | const scope = yield* Effect.scope |
| 1153 | const handler = yield* NodeHttpServer.makeHandler( |
| 1154 | Effect.succeed(HttpServerResponse.stream(Stream.make( |
| 1155 | Buffer.Buffer.from("a"), |
| 1156 | Buffer.Buffer.from("b") |
| 1157 | ))), |
| 1158 | { scope } |
| 1159 | ) |
| 1160 | const completed = Latch.makeUnsafe() |
| 1161 | const writes: Array<Uint8Array> = [] |
| 1162 | let writableEnded = false |
| 1163 | const nodeResponse = Object.defineProperty(new EventEmitter(), "writableEnded", { |
| 1164 | get: () => writableEnded |
| 1165 | }) as Http.ServerResponse |
| 1166 | nodeResponse.writeHead = () => nodeResponse |
| 1167 | nodeResponse.write = ((chunk: Uint8Array) => { |
| 1168 | writes.push(chunk) |
| 1169 | return true |
| 1170 | }) as Http.ServerResponse["write"] |
| 1171 | nodeResponse.end = (() => { |
| 1172 | writableEnded = true |
| 1173 | completed.openUnsafe() |
| 1174 | return nodeResponse |
| 1175 | }) as Http.ServerResponse["end"] |
| 1176 | |
| 1177 | handler( |
| 1178 | { method: "GET", url: "/", headers: {}, socket: {} } as Http.IncomingMessage, |
| 1179 | nodeResponse |
| 1180 | ) |
| 1181 | yield* completed.await |
| 1182 | |
| 1183 | assert.deepStrictEqual(writes.map((chunk) => Buffer.Buffer.from(chunk).toString()), ["ab"]) |
| 1184 | })) |
| 1185 | |
| 1186 | it.effect("waits for drain after a streaming write applies backpressure", () => |
| 1187 | Effect.gen(function*() { |
| 1188 | const scope = yield* Effect.scope |
| 1189 | const handler = yield* NodeHttpServer.makeHandler( |
| 1190 | Effect.succeed(HttpServerResponse.stream(Stream.make( |
| 1191 | Buffer.Buffer.from("a"), |
| 1192 | Buffer.Buffer.from("b") |
| 1193 | ))), |
| 1194 | { scope } |
| 1195 | ) |
| 1196 | const writeObserved = Latch.makeUnsafe() |
| 1197 | const completed = Latch.makeUnsafe() |
| 1198 | let writableEnded = false |
| 1199 | const nodeResponse = Object.defineProperty(new EventEmitter(), "writableEnded", { |
| 1200 | get: () => writableEnded |
| 1201 | }) as Http.ServerResponse |
| 1202 | let writeCount = 0 |
| 1203 | nodeResponse.writeHead = () => nodeResponse |
| 1204 | nodeResponse.write = (() => { |
| 1205 | writeCount++ |
| 1206 | queueMicrotask(() => writeObserved.openUnsafe()) |
| 1207 | return writeCount > 1 |
| 1208 | }) as Http.ServerResponse["write"] |
| 1209 | nodeResponse.end = (() => { |
| 1210 | writableEnded = true |
| 1211 | completed.openUnsafe() |
| 1212 | return nodeResponse |
| 1213 | }) as Http.ServerResponse["end"] |
| 1214 | |
| 1215 | handler( |
| 1216 | { method: "GET", url: "/", headers: {}, socket: {} } as Http.IncomingMessage, |
| 1217 | nodeResponse |
| 1218 | ) |
| 1219 | yield* writeObserved.await |
| 1220 | assert.strictEqual(nodeResponse.writableEnded, false) |
| 1221 | |
| 1222 | nodeResponse.emit("drain") |
| 1223 | yield* completed.await |
| 1224 | assert.strictEqual(writeCount, 1) |
| 1225 | })) |
| 1226 | |
| 1227 | it.live.each([ |
| 1228 | { method: "GET", status: 204 }, |
| 1229 | { method: "GET", status: 205 }, |
| 1230 | { method: "GET", status: 304 }, |
| 1231 | { method: "HEAD", status: 204 }, |
| 1232 | { method: "HEAD", status: 205 } |
| 1233 | ])("sends no body for $method with status $status", ({ method, status }) => |
| 1234 | Effect.gen(function*() { |
| 1235 | yield* HttpServer.serveEffect(Effect.succeed(HttpServerResponse.text("x", { status }))) |
| 1236 | const server = yield* HttpServer.HttpServer |
| 1237 | const received = yield* rawGet((server.address as NetAddress.InetAddress).port, ["/"], method) |
| 1238 | assert.match(received, new RegExp(`^HTTP/1.1 ${status} `)) |
| 1239 | assert.isTrue(received.endsWith("\r\n\r\n")) |
| 1240 | // 304 describes the representation, 204 and 205 have no content. |
| 1241 | if (status === 304) { |
| 1242 | assert.match(received, /\r\ncontent-length: 1\r\n/i) |
| 1243 | } else { |
| 1244 | assert.notMatch(received, /\r\ncontent-length:/i) |
| 1245 | } |
| 1246 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 1247 | |
| 1248 | it.live.each([ |
| 1249 | { |
| 1250 | body: "Uint8Array", |
| 1251 | contentLength: 3, |
| 1252 | response: () => HttpServerResponse.text("hello").pipe(HttpServerResponse.setHeader("content-length", "3")) |
| 1253 | }, |
| 1254 | { |
| 1255 | body: "Raw file Readable", |
| 1256 | contentLength: 3, |
| 1257 | response: () => |
| 1258 | HttpServerResponse.raw(createReadStream(new URL("./fixtures/text.txt", import.meta.url)), { contentLength: 3 }) |
| 1259 | }, |
| 1260 | { |
| 1261 | body: "FormData", |
| 1262 | contentLength: 3, |
| 1263 | response: () => { |
| 1264 | const formData = new FormData() |
| 1265 | formData.append("text", "hello") |
| 1266 | return HttpServerResponse.formData(formData, { headers: { "content-length": "3" } }) |
| 1267 | } |
| 1268 | }, |
| 1269 | { |
| 1270 | body: "Stream", |
| 1271 | contentLength: 10, |
| 1272 | response: () => HttpServerResponse.stream(Stream.make(Buffer.Buffer.from("hello")), { contentLength: 10 }) |
| 1273 | } |
| 1274 | ])( |
| 1275 | "aborts the connection when a $body body doesn't match content-length", |
| 1276 | ({ contentLength, response }) => |
| 1277 | Effect.gen(function*() { |
| 1278 | yield* Layer.mergeAll( |
| 1279 | HttpRouter.add("GET", "/mismatch", Effect.sync(response)), |
| 1280 | HttpRouter.add("GET", "/next", HttpServerResponse.text("next")) |
| 1281 | ).pipe( |
| 1282 | HttpRouter.serve, |
| 1283 | Layer.build |
| 1284 | ) |
| 1285 | const server = yield* HttpServer.HttpServer |
| 1286 | const received = yield* rawGet((server.address as NetAddress.InetAddress).port, ["/mismatch", "/next"]) |
| 1287 | // The client must neither wait for the declared body nor read the next response as part of it. |
| 1288 | const headEnd = received.indexOf("\r\n\r\n") |
| 1289 | const body = headEnd === -1 ? "" : received.slice(headEnd + 4) |
| 1290 | assert.isBelow(body.length, contentLength) |
| 1291 | }).pipe(Effect.provide(NodeHttpServer.layerTest)) |
| 1292 | ) |
| 1293 | |
| 1294 | it.effect("aborts the response when a streamed body fails after it started", () => |
| 1295 | Effect.gen(function*() { |
| 1296 | const received = yield* Latch.make() |
| 1297 | yield* HttpRouter.add( |
| 1298 | "GET", |
| 1299 | "/stream", |
| 1300 | HttpServerResponse.stream( |
| 1301 | Stream.make(Buffer.Buffer.from("partial")).pipe( |
| 1302 | Stream.concat(Stream.fromEffect(received.await.pipe(Effect.andThen(Effect.fail("boom"))))) |
| 1303 | ) |
| 1304 | ) |
| 1305 | ).pipe( |
| 1306 | HttpRouter.serve, |
| 1307 | Layer.build |
| 1308 | ) |
| 1309 | const response = yield* HttpClient.get("/stream") |
| 1310 | assert.strictEqual(response.status, 200) |
| 1311 | let body = "" |
| 1312 | const exit = yield* response.stream.pipe( |
| 1313 | Stream.runForEach((chunk) => { |
| 1314 | body += Buffer.Buffer.from(chunk).toString() |
| 1315 | return received.open |
| 1316 | }), |
| 1317 | Effect.exit |
| 1318 | ) |
| 1319 | assert.strictEqual(body, "partial") |
| 1320 | assert.isTrue(Exit.isFailure(exit)) |
| 1321 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 1322 | |
| 1323 | describe("when writeHead throws", () => { |
| 1324 | it.live("answers rejected HTTP/1.1 headers with a 500 and reports the failure", () => |
| 1325 | Effect.gen(function*() { |
| 1326 | const observed = yield* Deferred.make<Exit.Exit<unknown, unknown>>() |
| 1327 | yield* HttpServer.serve( |
| 1328 | Effect.succeed(HttpServerResponse.formData(new FormData(), { headers: { "x-invalid": "a\nb" } })), |
| 1329 | (app) => Effect.onExit(app, (exit) => Deferred.succeed(observed, exit)) |
| 1330 | ).pipe(Layer.build) |
| 1331 | |
| 1332 | const response = yield* HttpClient.get("/") |
| 1333 | assert.strictEqual(response.status, 500) |
| 1334 | assert.strictEqual(yield* response.text, "") |
| 1335 | assert.isTrue(Exit.isFailure(yield* Deferred.await(observed))) |
| 1336 | }).pipe(Effect.timeout("2 seconds"), Effect.provide(NodeHttpServer.layerTest))) |
| 1337 | |
| 1338 | it.live.each( |
| 1339 | [ |
| 1340 | { method: "GET", status: 103 }, |
| 1341 | { method: "GET", status: 600 }, |
| 1342 | { method: "HEAD", status: 600 } |
| 1343 | ] as const |
| 1344 | )( |
| 1345 | "answers a $method response with status $status with a 500 and destroys the raw body", |
| 1346 | ({ method, status }) => |
| 1347 | Effect.gen(function*() { |
| 1348 | const body = Readable.from(["ok"]) |
| 1349 | const observed = yield* Deferred.make<Exit.Exit<unknown, unknown>>() |
| 1350 | yield* HttpServer.serve( |
| 1351 | Effect.succeed(HttpServerResponse.raw(body, { status })), |
| 1352 | (app) => Effect.onExit(app, (exit) => Deferred.succeed(observed, exit)) |
| 1353 | ).pipe(Layer.build) |
| 1354 | |
| 1355 | const response = yield* HttpClient.execute(HttpClientRequest.make(method)("/")) |
| 1356 | assert.strictEqual(response.status, 500) |
| 1357 | assert.isTrue(Exit.isFailure(yield* Deferred.await(observed))) |
| 1358 | assert.isTrue(body.destroyed) |
| 1359 | }).pipe(Effect.timeout("2 seconds"), Effect.provide(NodeHttpServer.layerTest)) |
| 1360 | ) |
| 1361 | }) |
| 1362 | |
| 1363 | it.live("answers keep-alive requests while graceful shutdown drains an existing connection", () => { |
| 1364 | const requestStarted = Latch.makeUnsafe() |
| 1365 | const releaseResponse = Latch.makeUnsafe() |
| 1366 | const firstResponse = Latch.makeUnsafe() |
| 1367 | const secondResponse = Latch.makeUnsafe() |
| 1368 | const server = Http.createServer() |
| 1369 | server.keepAliveTimeout = 5000 |
| 1370 | const runtime = ManagedRuntime.make( |
| 1371 | HttpServer.serve(Effect.gen(function*() { |
| 1372 | yield* requestStarted.open |
| 1373 | yield* releaseResponse.await |
| 1374 | return HttpServerResponse.text("ok") |
| 1375 | })).pipe( |
| 1376 | Layer.provide(NodeHttpServer.layer(() => server, { |
| 1377 | host: "127.0.0.1", |
| 1378 | port: 0, |
| 1379 | gracefulShutdownTimeout: "10 seconds" |
| 1380 | })) |
| 1381 | ) |
| 1382 | ) |
| 1383 | let socket: Net.Socket | undefined |
| 1384 | let responseData = "" |
| 1385 | |
| 1386 | return Effect.gen(function*() { |
| 1387 | yield* Effect.promise(() => runtime.context()) |
| 1388 | const connection = Net.connect(tcpPort(server), "127.0.0.1") |
| 1389 | socket = connection |
| 1390 | connection.on("data", (chunk) => { |
| 1391 | responseData += chunk.toString() |
| 1392 | if (responseData.includes("\r\n\r\nok")) firstResponse.openUnsafe() |
| 1393 | if ((responseData.match(/HTTP\/1\.1 \d{3}/g) ?? []).length >= 2) secondResponse.openUnsafe() |
| 1394 | }) |
| 1395 | yield* Effect.callback<void, Error>((resume) => { |
| 1396 | connection.once("connect", () => resume(Effect.void)) |
| 1397 | connection.on("error", (error) => resume(Effect.fail(error))) |
| 1398 | }).pipe(Effect.timeout("2 seconds")) |
| 1399 | |
| 1400 | connection.write("GET /first HTTP/1.1\r\nHost: localhost\r\n\r\n") |
| 1401 | yield* requestStarted.await.pipe(Effect.timeout("2 seconds")) |
| 1402 | const disposed = runtime.dispose() |
| 1403 | // Wait for server.close() to stop listening, without letting the first request finish. |
| 1404 | yield* Effect.callback<void>((resume) => { |
| 1405 | const timer = setInterval(() => { |
| 1406 | if (!server.listening) resume(Effect.void) |
| 1407 | }, 1) |
| 1408 | return Effect.sync(() => clearInterval(timer)) |
| 1409 | }).pipe(Effect.timeout("2 seconds")) |
| 1410 | |
| 1411 | releaseResponse.openUnsafe() |
| 1412 | yield* firstResponse.await.pipe(Effect.timeout("2 seconds")) |
| 1413 | assert.include(responseData, "HTTP/1.1 200") |
| 1414 | connection.write("GET /second HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n") |
| 1415 | const answered = yield* secondResponse.await.pipe(Effect.timeoutOption("1 second")) |
| 1416 | assert.strictEqual( |
| 1417 | answered._tag, |
| 1418 | "Some", |
| 1419 | "the second keep-alive request must receive a response before the shutdown timeout" |
| 1420 | ) |
| 1421 | yield* Effect.promise(() => disposed).pipe(Effect.timeout("2 seconds")) |
| 1422 | }).pipe( |
| 1423 | Effect.ensuring( |
| 1424 | Effect.sync(() => { |
| 1425 | releaseResponse.openUnsafe() |
| 1426 | socket?.destroy() |
| 1427 | server.closeAllConnections() |
| 1428 | }).pipe(Effect.andThen(Effect.promise(() => runtime.dispose()))) |
| 1429 | ) |
| 1430 | ) |
| 1431 | }) |
| 1432 | |
| 1433 | it.live("closes idle HTTP/2 sessions on shutdown", () => |
| 1434 | Effect.gen(function*() { |
| 1435 | const server = Http2.createServer() |
| 1436 | const scope = yield* Scope.make() |
| 1437 | yield* HttpServer.serve(Effect.succeed(HttpServerResponse.text("ok"))).pipe( |
| 1438 | Layer.provide(NodeHttpServer.layer(() => server, { port: 0, gracefulShutdownTimeout: "10 seconds" })), |
| 1439 | Layer.buildWithScope(scope) |
| 1440 | ) |
| 1441 | const session = yield* Effect.acquireRelease( |
| 1442 | Effect.sync(() => Http2.connect(`http://127.0.0.1:${tcpPort(server)}`)), |
| 1443 | (session) => Effect.sync(() => session.destroy()) |
| 1444 | ) |
| 1445 | session.on("error", constVoid) |
| 1446 | yield* Effect.callback<void>((resume) => { |
| 1447 | session.once("connect", () => resume(Effect.void)) |
| 1448 | }).pipe(Effect.timeout("2 seconds")) |
| 1449 | |
| 1450 | // Finalizers are uninterruptible, so the timeout applies to the join |
| 1451 | const closed = yield* Effect.forkDetach(Scope.close(scope, Exit.void)) |
| 1452 | yield* Fiber.join(closed).pipe(Effect.timeout("2 seconds")) |
| 1453 | })) |
| 1454 | |
| 1455 | it.live("closes HTTP/2 sessions whose TLS handshake finishes during shutdown", () => |
| 1456 | Effect.gen(function*() { |
| 1457 | const server = Http2.createSecureServer({ cert: tlsCert, key: tlsKey }) |
| 1458 | const scope = yield* Scope.make() |
| 1459 | yield* HttpServer.serve(Effect.succeed(HttpServerResponse.text("ok"))).pipe( |
| 1460 | Layer.provide(NodeHttpServer.layer(() => server, { port: 0, gracefulShutdownTimeout: "10 seconds" })), |
| 1461 | Layer.buildWithScope(scope) |
| 1462 | ) |
| 1463 | const port = tcpPort(server) |
| 1464 | const socket = yield* Effect.acquireRelease( |
| 1465 | Effect.sync(() => Net.connect(port, "127.0.0.1")), |
| 1466 | (socket) => Effect.sync(() => socket.destroy()) |
| 1467 | ) |
| 1468 | yield* Effect.callback<void>((resume) => { |
| 1469 | server.once("connection", () => resume(Effect.void)) |
| 1470 | }).pipe(Effect.timeout("2 seconds")) |
| 1471 | |
| 1472 | // Finalizers are uninterruptible, so the timeout applies to the join |
| 1473 | const closed = yield* Effect.forkDetach(Scope.close(scope, Exit.void)) |
| 1474 | yield* Effect.callback<void>((resume) => { |
| 1475 | const timer = setInterval(() => { |
| 1476 | if (!server.listening) resume(Effect.void) |
| 1477 | }, 1) |
| 1478 | return Effect.sync(() => clearInterval(timer)) |
| 1479 | }).pipe(Effect.timeout("2 seconds")) |
| 1480 | const session = Http2.connect(`https://127.0.0.1:${port}`, { |
| 1481 | createConnection: () => Tls.connect({ socket, ca: tlsCert, ALPNProtocols: ["h2"] }) |
| 1482 | }) |
| 1483 | yield* Effect.addFinalizer(() => Effect.sync(() => session.destroy())) |
| 1484 | session.on("error", constVoid) |
| 1485 | // The server sends its settings once the session exists |
| 1486 | yield* Effect.callback<void>((resume) => { |
| 1487 | session.once("remoteSettings", () => resume(Effect.void)) |
| 1488 | }).pipe(Effect.timeout("2 seconds")) |
| 1489 | |
| 1490 | yield* Fiber.join(closed).pipe(Effect.timeout("2 seconds")) |
| 1491 | })) |
| 1492 | |
| 1493 | it.live("disposes after a client aborts a handler awaiting an upstream request", () => { |
| 1494 | const upstreamStarted = Latch.makeUnsafe() |
| 1495 | const upstream = Http.createServer(() => { |
| 1496 | upstreamStarted.openUnsafe() |
| 1497 | }) |
| 1498 | const server = Http.createServer() |
| 1499 | const router = HttpRouter.use((router) => |
| 1500 | router.add( |
| 1501 | "GET", |
| 1502 | "/", |
| 1503 | Effect.gen(function*() { |
| 1504 | const request = yield* HttpServerRequest.HttpServerRequest |
| 1505 | if (request.method !== "HEAD") { |
| 1506 | return HttpServerResponse.text("ok") |
| 1507 | } |
| 1508 | yield* HttpClient.head(`http://localhost:${tcpPort(upstream)}`) |
| 1509 | return HttpServerResponse.empty() |
| 1510 | }) |
| 1511 | ) |
| 1512 | ) |
| 1513 | const runtime = ManagedRuntime.make( |
| 1514 | Layer.effectDiscard( |
| 1515 | HttpRouter.serve(router).pipe( |
| 1516 | Layer.provide(NodeHttpServer.layer(() => server, { |
| 1517 | port: 0, |
| 1518 | gracefulShutdownTimeout: "100 millis" |
| 1519 | })), |
| 1520 | Layer.provide(FetchHttpClient.layer), |
| 1521 | Layer.launch, |
| 1522 | Effect.forkScoped, |
| 1523 | Effect.asVoid |
| 1524 | ) |
| 1525 | ) |
| 1526 | ) |
| 1527 | |
| 1528 | return Effect.gen(function*() { |
| 1529 | yield* Effect.callback<void>((resume) => { |
| 1530 | upstream.listen(0, () => resume(Effect.void)) |
| 1531 | }) |
| 1532 | yield* Effect.promise(() => runtime.context()) |
| 1533 | yield* Effect.callback<void>((resume) => { |
| 1534 | if (server.listening) { |
| 1535 | resume(Effect.void) |
| 1536 | } else { |
| 1537 | server.once("listening", () => resume(Effect.void)) |
| 1538 | } |
| 1539 | }) |
| 1540 | |
| 1541 | const downstreamClosed = Latch.makeUnsafe() |
| 1542 | const downstream = Http.request({ method: "HEAD", port: tcpPort(server), path: "/" }) |
| 1543 | downstream.on("error", () => {}) |
| 1544 | downstream.on("close", () => downstreamClosed.openUnsafe()) |
| 1545 | downstream.end() |
| 1546 | yield* upstreamStarted.await |
| 1547 | downstream.destroy() |
| 1548 | yield* downstreamClosed.await |
| 1549 | |
| 1550 | const disposed = yield* Effect.promise(() => runtime.dispose()).pipe( |
| 1551 | Effect.timeoutOption("2 seconds") |
| 1552 | ) |
| 1553 | assert.strictEqual(disposed._tag, "Some") |
| 1554 | }).pipe( |
| 1555 | Effect.ensuring( |
| 1556 | Effect.sync(() => { |
| 1557 | upstream.closeAllConnections() |
| 1558 | upstream.close() |
| 1559 | }).pipe( |
| 1560 | Effect.andThen(Effect.promise(() => runtime.dispose())), |
| 1561 | Effect.timeoutOrElse({ duration: "2 seconds", orElse: () => Effect.void }) |
| 1562 | ) |
| 1563 | ) |
| 1564 | ) |
| 1565 | }) |
| 1566 | |
| 1567 | describe("HttpServerRespondable", () => { |
| 1568 | it.effect("error/schema", () => |
| 1569 | Effect.gen(function*() { |
| 1570 | class CustomError extends Schema.Error<CustomError>("CustomError")({ |
| 1571 | _tag: Schema.tag("CustomError"), |
| 1572 | name: Schema.String |
| 1573 | }) { |
| 1574 | [HttpServerRespondable.symbol]() { |
| 1575 | return HttpServerResponse.schemaJson(CustomError)(this, { status: 599 }) |
| 1576 | } |
| 1577 | } |
| 1578 | yield* HttpRouter.add( |
| 1579 | "GET", |
| 1580 | "/home", |
| 1581 | new CustomError({ name: "test" }) |
| 1582 | ).pipe( |
| 1583 | HttpRouter.serve, |
| 1584 | Layer.build |
| 1585 | ) |
| 1586 | const client = yield* HttpClient.HttpClient |
| 1587 | const res = yield* client.get("/home") |
| 1588 | assert.strictEqual(res.status, 599) |
| 1589 | const err = yield* HttpClientResponse.schemaBodyJson(CustomError)(res) |
| 1590 | assert.deepStrictEqual(err, new CustomError({ name: "test" })) |
| 1591 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 1592 | |
| 1593 | it.effect("httpapi error", () => |
| 1594 | Effect.gen(function*() { |
| 1595 | yield* HttpRouter.add( |
| 1596 | "GET", |
| 1597 | "/home", |
| 1598 | new HttpApiError.BadRequest({}) |
| 1599 | ).pipe( |
| 1600 | HttpRouter.serve, |
| 1601 | Layer.build |
| 1602 | ) |
| 1603 | const client = yield* HttpClient.HttpClient |
| 1604 | const res = yield* client.get("/home") |
| 1605 | assert.strictEqual(res.status, 400) |
| 1606 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 1607 | }) |
| 1608 | |
| 1609 | it.effect("RouterConfig", () => |
| 1610 | Effect.gen(function*() { |
| 1611 | yield* HttpRouter.add( |
| 1612 | "GET", |
| 1613 | "/:param", |
| 1614 | Effect.succeed(HttpServerResponse.empty()) |
| 1615 | ).pipe( |
| 1616 | HttpRouter.serve, |
| 1617 | Layer.build |
| 1618 | ) |
| 1619 | let res = yield* HttpClient.get("/123456") |
| 1620 | assert.strictEqual(res.status, 404) |
| 1621 | res = yield* HttpClient.get("/12345") |
| 1622 | assert.strictEqual(res.status, 204) |
| 1623 | }).pipe( |
| 1624 | Effect.provide([ |
| 1625 | NodeHttpServer.layerTest, |
| 1626 | Layer.succeed(HttpRouter.RouterConfig)({ maxParamLength: 5 }) |
| 1627 | ]) |
| 1628 | )) |
| 1629 | |
| 1630 | it.effect("HttpRouter prefixed", () => |
| 1631 | Effect.gen(function*() { |
| 1632 | const handler = HttpRouter.serve(HttpRouter.use(Effect.fnUntraced(function*(router_) { |
| 1633 | const router = router_.prefixed("/todos") |
| 1634 | yield* router.add( |
| 1635 | "GET", |
| 1636 | "/:id", |
| 1637 | Effect.flatMap( |
| 1638 | HttpRouter.schemaParams(IdParams), |
| 1639 | ({ id }) => todoResponse({ id, title: "test" }) |
| 1640 | ) |
| 1641 | ) |
| 1642 | yield* router.addAll([ |
| 1643 | HttpRouter.route("GET", "/", Effect.succeed(HttpServerResponse.text("root"))) |
| 1644 | ]) |
| 1645 | }))) |
| 1646 | |
| 1647 | yield* Layer.build(handler) |
| 1648 | |
| 1649 | const todo = yield* HttpClient.get("/todos/1").pipe( |
| 1650 | Effect.flatMap(HttpClientResponse.schemaBodyJson(Todo)) |
| 1651 | ) |
| 1652 | expect(todo).toEqual({ id: 1, title: "test" }) |
| 1653 | const root = yield* HttpClient.get("/todos").pipe( |
| 1654 | Effect.flatMap((r) => r.text) |
| 1655 | ) |
| 1656 | expect(root).toEqual("root") |
| 1657 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 1658 | |
| 1659 | it.effect("logs and traces status 101 after an upgraded WebSocket closes", () => |
| 1660 | Effect.gen(function*() { |
| 1661 | const logged = yield* Deferred.make<unknown>() |
| 1662 | const logger = Logger.make((options) => { |
| 1663 | const annotations = options.fiber.getRef(References.CurrentLogAnnotations) |
| 1664 | if (annotations["http.url"] === "/ws") { |
| 1665 | Deferred.doneUnsafe(logged, Effect.succeed(annotations["http.status"])) |
| 1666 | } |
| 1667 | }) |
| 1668 | const traced = yield* Deferred.make<unknown>() |
| 1669 | const tracer = Tracer.make({ |
| 1670 | span(options) { |
| 1671 | const span = new Tracer.NativeSpan(options) |
| 1672 | if (options.kind === "server") { |
| 1673 | span.end = (endTime, exit) => { |
| 1674 | Tracer.NativeSpan.prototype.end.call(span, endTime, exit) |
| 1675 | Deferred.doneUnsafe(traced, Effect.succeed(span.attributes.get("http.response.status_code"))) |
| 1676 | } |
| 1677 | } |
| 1678 | return span |
| 1679 | } |
| 1680 | }) |
| 1681 | yield* HttpRouter.add( |
| 1682 | "GET", |
| 1683 | "/ws", |
| 1684 | Effect.gen(function*() { |
| 1685 | const request = yield* HttpServerRequest.HttpServerRequest |
| 1686 | const socket = yield* request.upgrade |
| 1687 | yield* Stream.runDrain(Socket.toStream(socket)).pipe(Effect.ignore) |
| 1688 | return HttpServerResponse.empty() |
| 1689 | }) |
| 1690 | ).pipe( |
| 1691 | (layer) => HttpRouter.serve(layer, { disableListenLog: true }), |
| 1692 | Layer.provide(Logger.layer([logger])), |
| 1693 | Layer.provide(Layer.succeed(Tracer.Tracer)(tracer)), |
| 1694 | Layer.build |
| 1695 | ) |
| 1696 | const server = yield* HttpServer.HttpServer |
| 1697 | const port = (server.address as NetAddress.InetAddress).port |
| 1698 | const handshakeStatus = yield* Effect.callback<number | undefined, Error>((resume) => { |
| 1699 | const ws = new NodeWS.WebSocket(`ws://127.0.0.1:${port}/ws`) |
| 1700 | let status: number | undefined |
| 1701 | ws.on("upgrade", (response) => { |
| 1702 | status = response.statusCode |
| 1703 | }) |
| 1704 | ws.on("open", () => ws.close(1000)) |
| 1705 | ws.on("close", () => resume(Effect.succeed(status))) |
| 1706 | ws.on("error", (error) => resume(Effect.fail(error))) |
| 1707 | return Effect.sync(() => ws.close()) |
| 1708 | }) |
| 1709 | assert.strictEqual(handshakeStatus, 101) |
| 1710 | assert.strictEqual(yield* Deferred.await(logged), 101) |
| 1711 | assert.strictEqual(yield* Deferred.await(traced), 101) |
| 1712 | }).pipe(Effect.timeout("5 seconds"), Effect.provide(layerTestWebsocket)), 10000) |
| 1713 | |
| 1714 | for (const failHandler of [false, true]) { |
| 1715 | it.effect( |
| 1716 | `logs the fallback status when an upgrade fails and the handler ${failHandler ? "fails" : "recovers"}`, |
| 1717 | () => |
| 1718 | Effect.gen(function*() { |
| 1719 | const logged = yield* Deferred.make<unknown>() |
| 1720 | const logger = Logger.make((options) => { |
| 1721 | const annotations = options.fiber.getRef(References.CurrentLogAnnotations) |
| 1722 | if (annotations["http.url"] === "/ws") { |
| 1723 | Deferred.doneUnsafe(logged, Effect.succeed(annotations["http.status"])) |
| 1724 | } |
| 1725 | }) |
| 1726 | yield* HttpRouter.add( |
| 1727 | "GET", |
| 1728 | "/ws", |
| 1729 | Effect.gen(function*() { |
| 1730 | const request = yield* HttpServerRequest.HttpServerRequest |
| 1731 | const result = yield* Effect.exit(request.upgrade) |
| 1732 | assert.isTrue(Exit.isFailure(result)) |
| 1733 | if (failHandler) return yield* Effect.fail(new Error("upgrade failed")) |
| 1734 | return HttpServerResponse.empty({ status: 426 }) |
| 1735 | }) |
| 1736 | ).pipe( |
| 1737 | (layer) => HttpRouter.serve(layer, { disableListenLog: true }), |
| 1738 | Layer.provide(Logger.layer([logger])), |
| 1739 | Layer.build |
| 1740 | ) |
| 1741 | // An ordinary HTTP request has no upgrade transport, so acquisition fails. |
| 1742 | const response = yield* HttpClient.get("/ws") |
| 1743 | assert.strictEqual(response.status, failHandler ? 500 : 426) |
| 1744 | assert.strictEqual(yield* Deferred.await(logged), response.status) |
| 1745 | }).pipe(Effect.timeout("5 seconds"), Effect.provide(layerTestWebsocket)), |
| 1746 | 10000 |
| 1747 | ) |
| 1748 | } |
| 1749 | |
| 1750 | it.effect("websocket options are forwarded to the WebSocketServer", () => |
| 1751 | Effect.gen(function*() { |
| 1752 | yield* HttpRouter.add( |
| 1753 | "GET", |
| 1754 | "/ws", |
| 1755 | Effect.gen(function*() { |
| 1756 | const request = yield* HttpServerRequest.HttpServerRequest |
| 1757 | const socket = yield* Effect.orDie(request.upgrade) |
| 1758 | yield* Effect.gen(function*() { |
| 1759 | const { pull } = yield* socket.reader |
| 1760 | while (true) { |
| 1761 | yield* pull |
| 1762 | } |
| 1763 | }).pipe( |
| 1764 | Effect.scoped, |
| 1765 | Effect.catchTag("SocketError", () => Effect.void), |
| 1766 | Effect.orDie |
| 1767 | ) |
| 1768 | return HttpServerResponse.empty() |
| 1769 | }) |
| 1770 | ).pipe( |
| 1771 | HttpRouter.serve, |
| 1772 | Layer.build |
| 1773 | ) |
| 1774 | const server = yield* HttpServer.HttpServer |
| 1775 | const port = (server.address as NetAddress.InetAddress).port |
| 1776 | |
| 1777 | const connect = (perMessageDeflate: boolean) => |
| 1778 | Effect.acquireRelease( |
| 1779 | Effect.callback<NodeWS.WebSocket, Error>((resume) => { |
| 1780 | const ws = new NodeWS.WebSocket(`ws://127.0.0.1:${port}/ws`, { perMessageDeflate }) |
| 1781 | ws.on("open", () => resume(Effect.succeed(ws))) |
| 1782 | ws.on("error", (error) => resume(Effect.fail(error))) |
| 1783 | }), |
| 1784 | (ws) => Effect.sync(() => ws.close()) |
| 1785 | ) |
| 1786 | |
| 1787 | // layerTest configures websocket: { perMessageDeflate: true }, so the |
| 1788 | // server accepts the extension when the client offers it... |
| 1789 | const compressed = yield* connect(true) |
| 1790 | expect(compressed.extensions).toContain("permessage-deflate") |
| 1791 | |
| 1792 | // ...and clients that do not offer it still connect uncompressed. |
| 1793 | const plain = yield* connect(false) |
| 1794 | expect(plain.extensions).not.toContain("permessage-deflate") |
| 1795 | }).pipe(Effect.scoped, Effect.provide(layerTestWebsocket))) |
| 1796 | |
| 1797 | for ( |
| 1798 | const [name, exit, code] of [ |
| 1799 | ["success", "success", 1000], |
| 1800 | ["interrupt", "interrupt", 1001], |
| 1801 | ["failure", "failure", 1011], |
| 1802 | ["defect", "defect", 1011], |
| 1803 | ["explicit close before failure", "explicit", 4400] |
| 1804 | ] as const |
| 1805 | ) { |
| 1806 | it.effect(`closes a WebSocket with the handler's ${name} code`, () => |
| 1807 | Effect.gen(function*() { |
| 1808 | const opened = yield* Deferred.make<void>() |
| 1809 | yield* HttpRouter.add( |
| 1810 | "GET", |
| 1811 | "/ws", |
| 1812 | Effect.gen(function*() { |
| 1813 | const request = yield* HttpServerRequest.HttpServerRequest |
| 1814 | const socket = yield* request.upgrade |
| 1815 | yield* socket.reader |
| 1816 | yield* Deferred.await(opened) |
| 1817 | if (exit === "explicit") { |
| 1818 | const writer = yield* socket.writer |
| 1819 | yield* writer.write(new Socket.CloseEvent(4400, "handler closed")) |
| 1820 | } |
| 1821 | if (exit === "interrupt") return yield* Effect.interrupt |
| 1822 | if (exit === "failure" || exit === "explicit") return yield* Effect.fail(new Error("handler failed")) |
| 1823 | if (exit === "defect") return yield* Effect.die(new Error("handler defect")) |
| 1824 | return HttpServerResponse.empty() |
| 1825 | }) |
| 1826 | ).pipe((layer) => HttpRouter.serve(layer), Layer.build) |
| 1827 | const server = yield* HttpServer.HttpServer |
| 1828 | const port = (server.address as NetAddress.InetAddress).port |
| 1829 | const actual = yield* Effect.callback<number, Error>((resume) => { |
| 1830 | const ws = new NodeWS.WebSocket(`ws://127.0.0.1:${port}/ws`) |
| 1831 | ws.on("open", () => { |
| 1832 | Effect.runSync(Deferred.succeed(opened, undefined)) |
| 1833 | }) |
| 1834 | ws.on("close", (code) => resume(Effect.succeed(code))) |
| 1835 | ws.on("error", (error) => resume(Effect.fail(error))) |
| 1836 | return Effect.sync(() => ws.close()) |
| 1837 | }) |
| 1838 | assert.strictEqual(actual, code) |
| 1839 | }).pipe(Effect.timeout("5 seconds"), Effect.provide(layerTestWebsocket)), 10000) |
| 1840 | } |
| 1841 | |
| 1842 | it.effect("an upgrade connection reset by the peer does not crash the process", () => |
| 1843 | Effect.gen(function*() { |
| 1844 | yield* HttpRouter.add("GET", "/", HttpServerResponse.text("ok")).pipe( |
| 1845 | HttpRouter.serve, |
| 1846 | Layer.build |
| 1847 | ) |
| 1848 | const server = yield* HttpServer.HttpServer |
| 1849 | const port = (server.address as NetAddress.InetAddress).port |
| 1850 | |
| 1851 | const uncaught: Array<unknown> = [] |
| 1852 | const onUncaught = (error: unknown) => uncaught.push(error) |
| 1853 | process.on("uncaughtException", onUncaught) |
| 1854 | yield* Effect.addFinalizer(() => Effect.sync(() => process.off("uncaughtException", onUncaught))) |
| 1855 | |
| 1856 | yield* Effect.callback<void>((resume) => { |
| 1857 | const socket = Net.connect({ port, host: "127.0.0.1" }, () => { |
| 1858 | socket.write( |
| 1859 | "GET /ws HTTP/1.1\r\n" + |
| 1860 | "Host: 127.0.0.1\r\n" + |
| 1861 | "Upgrade: websocket\r\n" + |
| 1862 | "Connection: Upgrade\r\n" + |
| 1863 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + |
| 1864 | "Sec-WebSocket-Version: 13\r\n\r\n" |
| 1865 | ) |
| 1866 | setTimeout(() => { |
| 1867 | socket.resetAndDestroy() |
| 1868 | setTimeout(() => resume(Effect.void), 50) |
| 1869 | }, 50) |
| 1870 | }) |
| 1871 | socket.on("error", () => {}) |
| 1872 | }) |
| 1873 | |
| 1874 | expect(uncaught).toEqual([]) |
| 1875 | const response = yield* HttpClient.get("/") |
| 1876 | expect(response.status).toEqual(200) |
| 1877 | }).pipe(Effect.provide(layerTestWebsocket))) |
| 1878 | |
| 1879 | it.effect("fails refused websocket handshakes without hanging shutdown", () => |
| 1880 | Effect.gen(function*() { |
| 1881 | const outcome = yield* Deferred.make<Socket.SocketError | undefined>() |
| 1882 | yield* HttpRouter.add( |
| 1883 | "GET", |
| 1884 | "/ws", |
| 1885 | Effect.gen(function*() { |
| 1886 | const request = yield* HttpServerRequest.HttpServerRequest |
| 1887 | const socket = yield* request.upgrade |
| 1888 | const error = yield* Effect.scoped(socket.reader).pipe( |
| 1889 | Effect.match({ onFailure: (error) => error, onSuccess: () => undefined }) |
| 1890 | ) |
| 1891 | yield* Deferred.succeed(outcome, error) |
| 1892 | return HttpServerResponse.empty() |
| 1893 | }) |
| 1894 | ).pipe( |
| 1895 | HttpRouter.serve, |
| 1896 | Layer.build |
| 1897 | ) |
| 1898 | const server = yield* HttpServer.HttpServer |
| 1899 | const port = (server.address as NetAddress.InetAddress).port |
| 1900 | |
| 1901 | const status = yield* Effect.callback<number | undefined, Error>((resume) => { |
| 1902 | const req = Http.request({ |
| 1903 | hostname: "127.0.0.1", |
| 1904 | port, |
| 1905 | agent: false, |
| 1906 | path: "/ws", |
| 1907 | method: "GET", |
| 1908 | headers: { |
| 1909 | Connection: "Upgrade", |
| 1910 | Upgrade: "websocket", |
| 1911 | "Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==", |
| 1912 | "Sec-WebSocket-Version": "12" // Unsupported version forces handshake rejection. |
| 1913 | } |
| 1914 | }) |
| 1915 | req.on("response", (res) => { |
| 1916 | res.resume() |
| 1917 | res.on("end", () => resume(Effect.succeed(res.statusCode))) |
| 1918 | }) |
| 1919 | req.on("error", (error) => resume(Effect.fail(error))) |
| 1920 | req.end() |
| 1921 | return Effect.sync(() => req.destroy()) |
| 1922 | }) |
| 1923 | assert.strictEqual(status, 400) |
| 1924 | assert.isTrue(Socket.SocketError.is(yield* Deferred.await(outcome))) |
| 1925 | }).pipe(Effect.provide(layerTestWebsocket), Effect.scoped)) |
| 1926 | |
| 1927 | it.effect("does not write the HTTP response to an upgraded connection", () => |
| 1928 | Effect.gen(function*() { |
| 1929 | yield* HttpRouter.add( |
| 1930 | "GET", |
| 1931 | "/ws", |
| 1932 | Effect.gen(function*() { |
| 1933 | const request = yield* HttpServerRequest.HttpServerRequest |
| 1934 | const socket = yield* Effect.orDie(request.upgrade) |
| 1935 | yield* Effect.asVoid(socket.reader) |
| 1936 | const writer = yield* socket.writer |
| 1937 | yield* writer.write("refused") |
| 1938 | yield* writer.write(new Socket.CloseEvent(4400, "refused")) |
| 1939 | return HttpServerResponse.empty() |
| 1940 | }).pipe(Effect.scoped) |
| 1941 | ).pipe( |
| 1942 | HttpRouter.serve, |
| 1943 | Layer.build |
| 1944 | ) |
| 1945 | const server = yield* HttpServer.HttpServer |
| 1946 | const port = (server.address as NetAddress.InetAddress).port |
| 1947 | const { frames, trailing } = yield* Effect.promise(() => rawWebSocket(port, "/ws")) |
| 1948 | assert.strictEqual(frames.length, 2) |
| 1949 | assert.strictEqual(frames[0].opcode, 1) |
| 1950 | assert.strictEqual(frames[0].payload.toString(), "refused") |
| 1951 | assert.strictEqual(frames[1].opcode, 8) |
| 1952 | assert.strictEqual(frames[1].payload.readUInt16BE(0), 4400) |
| 1953 | assert.strictEqual(trailing.toString(), "") |
| 1954 | }).pipe(Effect.provide(layerTestWebsocket))) |
| 1955 | |
| 1956 | it.effect("writes the HTTP response when the upgrade is not consumed", () => |
| 1957 | Effect.gen(function*() { |
| 1958 | yield* HttpRouter.add( |
| 1959 | "GET", |
| 1960 | "/no-ws", |
| 1961 | HttpServerResponse.text("upgrade refused", { status: 426 }) |
| 1962 | ).pipe( |
| 1963 | HttpRouter.serve, |
| 1964 | Layer.build |
| 1965 | ) |
| 1966 | const server = yield* HttpServer.HttpServer |
| 1967 | const port = (server.address as NetAddress.InetAddress).port |
| 1968 | const response = yield* Effect.promise(() => rawUpgradeRequest(port, "/no-ws")) |
| 1969 | assert.match(response, /^HTTP\/1\.1 426/) |
| 1970 | assert.match(response, /upgrade refused/) |
| 1971 | }).pipe(Effect.provide(layerTestWebsocket))) |
| 1972 | |
| 1973 | it.effect("switches other protocols to the raw connection", () => |
| 1974 | Effect.gen(function*() { |
| 1975 | yield* HttpRouter.add( |
| 1976 | "POST", |
| 1977 | "/attach", |
| 1978 | Effect.gen(function*() { |
| 1979 | const request = yield* HttpServerRequest.HttpServerRequest |
| 1980 | const socket = yield* Effect.orDie(request.upgrade) |
| 1981 | const reader = yield* socket.reader |
| 1982 | const writer = yield* socket.writer |
| 1983 | const decoder = new TextDecoder() |
| 1984 | let received = "" |
| 1985 | while (true) { |
| 1986 | const chunks = yield* reader.pull.pipe( |
| 1987 | Effect.catchReason("SocketError", "SocketCloseError", () => Effect.succeed(undefined)) |
| 1988 | ) |
| 1989 | if (chunks === undefined) break |
| 1990 | for (const chunk of chunks) received += typeof chunk === "string" ? chunk : decoder.decode(chunk) |
| 1991 | } |
| 1992 | // the client half-closed, but the connection stays writable |
| 1993 | yield* writer.write(`echo:${received}`) |
| 1994 | return HttpServerResponse.empty() |
| 1995 | }).pipe(Effect.scoped) |
| 1996 | ).pipe( |
| 1997 | HttpRouter.serve, |
| 1998 | Layer.build |
| 1999 | ) |
| 2000 | const server = yield* HttpServer.HttpServer |
| 2001 | const port = (server.address as NetAddress.InetAddress).port |
| 2002 | // The bytes after the request head arrive with it, before the 101. |
| 2003 | const response = yield* Effect.promise(() => |
| 2004 | rawExchange( |
| 2005 | port, |
| 2006 | "POST /attach HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\nearly" |
| 2007 | ) |
| 2008 | ) |
| 2009 | assert.strictEqual( |
| 2010 | response, |
| 2011 | "HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\necho:early" |
| 2012 | ) |
| 2013 | }).pipe(Effect.provide(layerTestWebsocket))) |
| 2014 | |
| 2015 | it.effect.each([ |
| 2016 | { |
| 2017 | name: "a Content-Length body split from the head", |
| 2018 | input: [ |
| 2019 | "POST /attach HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: Upgrade\r\nUpgrade: tcp\r\nContent-Length: 5\r\n\r\nhe", |
| 2020 | "lloearly" |
| 2021 | ], |
| 2022 | read: true, |
| 2023 | upgrades: true |
| 2024 | }, |
| 2025 | { |
| 2026 | name: "an unread Content-Length body", |
| 2027 | input: [ |
| 2028 | "POST /attach HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: Upgrade\r\nUpgrade: tcp\r\nContent-Length: 5\r\n\r\nhe", |
| 2029 | "lloearly" |
| 2030 | ], |
| 2031 | read: false, |
| 2032 | upgrades: true |
| 2033 | }, |
| 2034 | { |
| 2035 | name: "a chunked body", |
| 2036 | input: [ |
| 2037 | "POST /attach HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: Upgrade\r\nUpgrade: tcp\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhe", |
| 2038 | "llo\r\n0\r\n\r\nearly" |
| 2039 | ], |
| 2040 | read: true, |
| 2041 | // Node 25 and older do not parse upgrade request bodies, and only Content-Length is framed by hand. |
| 2042 | upgrades: Number(process.versions.node.split(".")[0]) >= 26 |
| 2043 | } |
| 2044 | ])("reads $name before switching protocols", ({ input, read, upgrades }) => |
| 2045 | Effect.gen(function*() { |
| 2046 | yield* HttpRouter.add( |
| 2047 | "POST", |
| 2048 | "/attach", |
| 2049 | Effect.gen(function*() { |
| 2050 | const request = yield* HttpServerRequest.HttpServerRequest |
| 2051 | const body = read ? yield* request.text : "" |
| 2052 | const socket = yield* Effect.orDie(request.upgradeWith({ headers: { "content-type": "application/raw" } })) |
| 2053 | const reader = yield* socket.reader |
| 2054 | const writer = yield* socket.writer |
| 2055 | const decoder = new TextDecoder() |
| 2056 | let received = "" |
| 2057 | while (true) { |
| 2058 | const chunks = yield* reader.pull.pipe( |
| 2059 | Effect.catchReason("SocketError", "SocketCloseError", () => Effect.succeed(undefined)) |
| 2060 | ) |
| 2061 | if (chunks === undefined) break |
| 2062 | for (const chunk of chunks) received += typeof chunk === "string" ? chunk : decoder.decode(chunk) |
| 2063 | } |
| 2064 | yield* writer.write(`${body}|${received}`) |
| 2065 | return HttpServerResponse.empty() |
| 2066 | }).pipe(Effect.scoped) |
| 2067 | ).pipe( |
| 2068 | HttpRouter.serve, |
| 2069 | Layer.build |
| 2070 | ) |
| 2071 | const server = yield* HttpServer.HttpServer |
| 2072 | const port = (server.address as NetAddress.InetAddress).port |
| 2073 | const response = yield* Effect.promise(() => rawExchange(port, input)) |
| 2074 | if (upgrades) { |
| 2075 | assert.strictEqual( |
| 2076 | response, |
| 2077 | `HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: tcp\r\ncontent-type: application/raw\r\n\r\n${ |
| 2078 | read ? "hello" : "" |
| 2079 | }|early` |
| 2080 | ) |
| 2081 | } else { |
| 2082 | assert.match(response, /^HTTP\/1\.1 411 /) |
| 2083 | } |
| 2084 | }).pipe(Effect.provide(layerTestWebsocket))) |
| 2085 | |
| 2086 | it.effect.each([ |
| 2087 | { |
| 2088 | name: "when the unread body exceeds MaxBodySize", |
| 2089 | method: "POST" as const, |
| 2090 | input: [ |
| 2091 | "POST /attach HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: Upgrade\r\nUpgrade: tcp\r\nContent-Length: 5\r\n\r\nhe", |
| 2092 | "llo" |
| 2093 | ], |
| 2094 | headers: {}, |
| 2095 | maxBodySize: ByteSize.bytes(4) |
| 2096 | }, |
| 2097 | ...["connection", "upgrade", "content-length", "transfer-encoding"].map((name) => ({ |
| 2098 | name: `with a ${name} header`, |
| 2099 | method: "POST" as const, |
| 2100 | input: "POST /attach HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n", |
| 2101 | headers: { [name]: "1" }, |
| 2102 | maxBodySize: undefined |
| 2103 | })), |
| 2104 | { |
| 2105 | name: "with an invalid header name", |
| 2106 | method: "POST" as const, |
| 2107 | input: "POST /attach HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n", |
| 2108 | headers: { "x name": "1" }, |
| 2109 | maxBodySize: undefined |
| 2110 | }, |
| 2111 | { |
| 2112 | name: "with an invalid header value", |
| 2113 | method: "POST" as const, |
| 2114 | input: "POST /attach HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n", |
| 2115 | headers: { "x-name": "a\r\nx-injected: 1" }, |
| 2116 | maxBodySize: undefined |
| 2117 | }, |
| 2118 | { |
| 2119 | name: "with headers on a WebSocket upgrade", |
| 2120 | method: "GET" as const, |
| 2121 | input: upgradeRequest("/attach"), |
| 2122 | headers: { "x-name": "1" }, |
| 2123 | maxBodySize: undefined |
| 2124 | } |
| 2125 | ])("does not switch protocols $name", ({ headers, input, maxBodySize, method }) => |
| 2126 | Effect.gen(function*() { |
| 2127 | yield* HttpRouter.add( |
| 2128 | method, |
| 2129 | "/attach", |
| 2130 | Effect.gen(function*() { |
| 2131 | const request = yield* HttpServerRequest.HttpServerRequest |
| 2132 | const socket = yield* request.upgradeWith({ headers }) |
| 2133 | yield* socket.reader |
| 2134 | return HttpServerResponse.empty() |
| 2135 | }).pipe(Effect.scoped, Effect.provideService(HttpIncomingMessage.MaxBodySize, maxBodySize)) |
| 2136 | ).pipe( |
| 2137 | HttpRouter.serve, |
| 2138 | Layer.build |
| 2139 | ) |
| 2140 | const server = yield* HttpServer.HttpServer |
| 2141 | const port = (server.address as NetAddress.InetAddress).port |
| 2142 | const response = yield* Effect.promise(() => rawExchange(port, input)) |
| 2143 | assert.match(response, /^HTTP\/1\.1 [45]\d\d /) |
| 2144 | }).pipe(Effect.provide(layerTestWebsocket))) |
| 2145 | |
| 2146 | it.effect("serves other requests after a truncated multipart upgrade body", () => |
| 2147 | Effect.gen(function*() { |
| 2148 | yield* HttpRouter.addAll([ |
| 2149 | HttpRouter.route( |
| 2150 | "POST", |
| 2151 | "/attach", |
| 2152 | Effect.gen(function*() { |
| 2153 | const request = yield* HttpServerRequest.HttpServerRequest |
| 2154 | yield* request.multipart |
| 2155 | const socket = yield* Effect.orDie(request.upgrade) |
| 2156 | yield* socket.reader |
| 2157 | return HttpServerResponse.empty() |
| 2158 | }).pipe(Effect.scoped) |
| 2159 | ), |
| 2160 | HttpRouter.route("GET", "/ping", HttpServerResponse.text("pong")) |
| 2161 | ]).pipe( |
| 2162 | HttpRouter.serve, |
| 2163 | Layer.build |
| 2164 | ) |
| 2165 | const server = yield* HttpServer.HttpServer |
| 2166 | const port = (server.address as NetAddress.InetAddress).port |
| 2167 | // The client stops after the first boundary of a 200 byte body. |
| 2168 | const response = yield* Effect.promise(() => |
| 2169 | rawExchange( |
| 2170 | port, |
| 2171 | "POST /attach HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n" + |
| 2172 | "Content-Type: multipart/form-data; boundary=abc\r\nContent-Length: 200\r\n\r\n--abc\r\n" |
| 2173 | ) |
| 2174 | ) |
| 2175 | assert.notMatch(response, /^HTTP\/1\.1 101 /) |
| 2176 | const ping = yield* HttpClient.get("/ping") |
| 2177 | assert.strictEqual(yield* ping.text, "pong") |
| 2178 | }).pipe(Effect.provide(layerTestWebsocket))) |
| 2179 | |
| 2180 | it.effect("writes upgrade response header values as Latin-1", () => |
| 2181 | Effect.gen(function*() { |
| 2182 | yield* HttpRouter.add( |
| 2183 | "POST", |
| 2184 | "/attach", |
| 2185 | Effect.gen(function*() { |
| 2186 | const request = yield* HttpServerRequest.HttpServerRequest |
| 2187 | const socket = yield* Effect.orDie(request.upgradeWith({ headers: { "x-name": "café" } })) |
| 2188 | yield* Effect.orDie(socket.reader) |
| 2189 | return HttpServerResponse.empty() |
| 2190 | }).pipe(Effect.scoped) |
| 2191 | ).pipe( |
| 2192 | HttpRouter.serve, |
| 2193 | Layer.build |
| 2194 | ) |
| 2195 | const server = yield* HttpServer.HttpServer |
| 2196 | const port = (server.address as NetAddress.InetAddress).port |
| 2197 | const headers = yield* Effect.callback<Http.IncomingHttpHeaders, Error>((resume) => { |
| 2198 | const request = Http.request({ |
| 2199 | hostname: "127.0.0.1", |
| 2200 | port, |
| 2201 | path: "/attach", |
| 2202 | method: "POST", |
| 2203 | agent: false, |
| 2204 | headers: { connection: "Upgrade", upgrade: "tcp" } |
| 2205 | }) |
| 2206 | request.on("error", (error) => resume(Effect.fail(error))) |
| 2207 | request.on("upgrade", (response, socket) => { |
| 2208 | socket.destroy() |
| 2209 | resume(Effect.succeed(response.headers)) |
| 2210 | }) |
| 2211 | request.end() |
| 2212 | return Effect.sync(() => request.destroy()) |
| 2213 | }) |
| 2214 | // Node decodes header bytes as Latin-1, like other HTTP peers. |
| 2215 | assert.strictEqual(headers["x-name"], "café") |
| 2216 | }).pipe(Effect.provide(layerTestWebsocket))) |
| 2217 | |
| 2218 | describe("request URL", () => { |
| 2219 | it.effect("toURL reports https over TLS", () => |
| 2220 | Effect.gen(function*() { |
| 2221 | const port = yield* serveUrlEcho(Https.createServer({ cert: tlsCert, key: tlsKey })) |
| 2222 | assert.strictEqual( |
| 2223 | yield* rawRequest(port, `GET /x?y=1 HTTP/1.1\r\nHost: 127.0.0.1:${port}`, { tls: true }), |
| 2224 | `127.0.0.1:${port} /x?y=1 https://127.0.0.1:${port}/x?y=1` |
| 2225 | ) |
| 2226 | })) |
| 2227 | |
| 2228 | it.effect("xForwardedHeaders trusts an IPv4-mapped peer and keeps provenance after modify", () => |
| 2229 | Effect.gen(function*() { |
| 2230 | const server = Http.createServer() |
| 2231 | yield* HttpServer.serve( |
| 2232 | HttpServerRequest.HttpServerRequest.use((request) => { |
| 2233 | const moved = request.modify({ url: "/moved" }) |
| 2234 | return Effect.succeed(HttpServerResponse.text( |
| 2235 | `${Option.getOrUndefined(moved.remoteAddress)} ${Option.getOrUndefined(moved.peerAddress)} ${ |
| 2236 | Option.getOrThrow(HttpServerRequest.toURL(moved)).href |
| 2237 | }` |
| 2238 | )) |
| 2239 | }).pipe(HttpMiddleware.xForwardedHeaders({ trustedProxies: [IpNetwork.fromStringUnsafe("127.0.0.0/8")] })) |
| 2240 | ).pipe( |
| 2241 | Layer.provide(NodeHttpServer.layer(() => server, { port: 0, host: "::" })), |
| 2242 | Layer.build |
| 2243 | ) |
| 2244 | assert.strictEqual( |
| 2245 | yield* rawRequest( |
| 2246 | tcpPort(server), |
| 2247 | "GET /x HTTP/1.1\r\nHost: app.test\r\nX-Forwarded-For: 198.51.100.7\r\nX-Forwarded-Proto: https" |
| 2248 | ), |
| 2249 | "198.51.100.7 ::ffff:127.0.0.1 https://app.test/moved" |
| 2250 | ) |
| 2251 | })) |
| 2252 | |
| 2253 | it.effect("absolute-form request-target replaces the host (RFC 9112 §3.2.2)", () => |
| 2254 | Effect.gen(function*() { |
| 2255 | const port = yield* serveUrlEcho(Http.createServer()) |
| 2256 | assert.strictEqual( |
| 2257 | yield* rawRequest(port, "GET http://abs.test/x?y=1 HTTP/1.1\r\nHost: other.test"), |
| 2258 | "abs.test /x?y=1 http://abs.test/x?y=1" |
| 2259 | ) |
| 2260 | assert.strictEqual( |
| 2261 | yield* rawRequest(port, "GET http://abs.test//other.test/x?y=1 HTTP/1.1\r\nHost: other.test"), |
| 2262 | "abs.test //other.test/x?y=1 http://abs.test//other.test/x?y=1" |
| 2263 | ) |
| 2264 | })) |
| 2265 | |
| 2266 | it.effect("searchParamsParser parses the query", () => |
| 2267 | Effect.gen(function*() { |
| 2268 | yield* HttpRouter.add( |
| 2269 | "GET", |
| 2270 | "/search", |
| 2271 | Effect.map(HttpServerRequest.ParsedSearchParams, (params) => HttpServerResponse.jsonUnsafe(params)).pipe( |
| 2272 | HttpMiddleware.searchParamsParser |
| 2273 | ) |
| 2274 | ).pipe(HttpRouter.serve, Layer.build) |
| 2275 | const body = yield* HttpClient.get("/search?a=1&a=2&b=3").pipe(Effect.flatMap((response) => response.json)) |
| 2276 | assert.deepStrictEqual(body, { a: ["1", "2"], b: "3" }) |
| 2277 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 2278 | }) |
| 2279 | }) |
| 2280 | |
| 2281 | const tlsCert = readFileSync(new URL("./fixtures/tls/cert.pem", import.meta.url)) |
| 2282 | const tlsKey = readFileSync(new URL("./fixtures/tls/key.pem", import.meta.url)) |
| 2283 | |
| 2284 | const layerHttp2Secure = ( |
| 2285 | client: Layer.Layer<HttpClient.HttpClient>, |
| 2286 | options?: Http2.SecureServerOptions |
| 2287 | ) => |
| 2288 | layerTestClient("https").pipe( |
| 2289 | Layer.provide(client), |
| 2290 | Layer.provideMerge( |
| 2291 | NodeHttpServer.layerServer(() => Http2.createSecureServer({ cert: tlsCert, key: tlsKey, ...options }), { |
| 2292 | port: 0 |
| 2293 | }) |
| 2294 | ) |
| 2295 | ) |
| 2296 | |
| 2297 | describeHttpServerProtocol("HTTP/1.1", NodeHttpServer.layerTest) |
| 2298 | describeHttpServerProtocol( |
| 2299 | "h2c", |
| 2300 | layerTestClient("http").pipe( |
| 2301 | Layer.provide(layerHttp2Client()), |
| 2302 | Layer.provideMerge(NodeHttpServer.layerServer(() => Http2.createServer(), { port: 0 })) |
| 2303 | ) |
| 2304 | ) |
| 2305 | describeHttpServerProtocol("HTTP/2 over TLS", layerHttp2Secure(layerHttp2Client({ ca: tlsCert }))) |
| 2306 | describeHttpServerProtocol( |
| 2307 | "HTTP/2 on an allowHTTP1 server", |
| 2308 | layerHttp2Secure(layerHttp2Client({ ca: tlsCert }), { allowHTTP1: true }) |
| 2309 | ) |
| 2310 | describeHttpServerProtocol( |
| 2311 | "HTTP/1.1 on an allowHTTP1 server", |
| 2312 | layerHttp2Secure(NodeHttpClient.layerUndiciOptions({ allowH2: false, connect: { ca: tlsCert } }), { |
| 2313 | allowHTTP1: true |
| 2314 | }) |
| 2315 | ) |
| 2316 | |
| 2317 | const serveUrlEcho = Effect.fnUntraced(function*(server: Http.Server) { |
| 2318 | yield* HttpRouter.add( |
| 2319 | "GET", |
| 2320 | "*", |
| 2321 | HttpServerRequest.HttpServerRequest.use((request) => |
| 2322 | Effect.succeed(HttpServerResponse.text( |
| 2323 | `${request.headers.host} ${request.url} ${Option.getOrThrow(HttpServerRequest.toURL(request)).href}` |
| 2324 | )) |
| 2325 | ) |
| 2326 | ).pipe( |
| 2327 | HttpRouter.serve, |
| 2328 | Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })), |
| 2329 | Layer.build |
| 2330 | ) |
| 2331 | return tcpPort(server) |
| 2332 | }) |
| 2333 | |
| 2334 | const rawRequest = (port: number, head: string, options?: { readonly tls?: boolean }) => |
| 2335 | Effect.callback<string, Error>((resume) => { |
| 2336 | const socket = options?.tls |
| 2337 | ? Tls.connect({ host: "127.0.0.1", port, rejectUnauthorized: false }) |
| 2338 | : Net.createConnection({ host: "127.0.0.1", port }) |
| 2339 | let response = "" |
| 2340 | socket.setEncoding("utf8") |
| 2341 | socket.write(`${head}\r\nConnection: close\r\n\r\n`) |
| 2342 | socket.on("data", (chunk: string) => { |
| 2343 | response += chunk |
| 2344 | }) |
| 2345 | socket.on("error", (error) => resume(Effect.fail(error))) |
| 2346 | socket.on("close", () => resume(Effect.succeed(response.slice(response.indexOf("\r\n\r\n") + 4)))) |
| 2347 | return Effect.sync(() => socket.destroy()) |
| 2348 | }) |
| 2349 | |
| 2350 | const layerTestWebsocket = HttpServer.layerTestClient.pipe( |
| 2351 | Layer.provide( |
| 2352 | Layer.fresh(FetchHttpClient.layer).pipe( |
| 2353 | Layer.provide(Layer.succeed(FetchHttpClient.RequestInit)({ keepalive: false })) |
| 2354 | ) |
| 2355 | ), |
| 2356 | Layer.provideMerge(NodeHttpServer.layer(Http.createServer, { |
| 2357 | port: 0, |
| 2358 | websocket: { perMessageDeflate: true } |
| 2359 | })) |
| 2360 | ) |
| 2361 | |
| 2362 | const getStatusText = (port: number) => |
| 2363 | Effect.callback<string | undefined, Error>((resume) => { |
| 2364 | const request = Http.get({ hostname: "127.0.0.1", port, agent: false }, (response) => { |
| 2365 | response.resume() |
| 2366 | response.on("end", () => resume(Effect.succeed(response.statusMessage))) |
| 2367 | }) |
| 2368 | request.on("error", (error) => resume(Effect.fail(error))) |
| 2369 | return Effect.sync(() => request.destroy()) |
| 2370 | }) |
| 2371 | |
| 2372 | const tcpPort = (server: Net.Server): number => { |
| 2373 | const address = server.address() |
| 2374 | assert(address !== null && typeof address !== "string") |
| 2375 | return address.port |
| 2376 | } |
| 2377 | |
| 2378 | // Sends the requests pipelined on one connection and returns everything received until the connection closes. |
| 2379 | const rawGet = (port: number, paths: ReadonlyArray<string>, method = "GET") => |
| 2380 | Effect.callback<string>((resume) => { |
| 2381 | const socket = Net.connect({ port, host: "127.0.0.1" }, () => { |
| 2382 | socket.write( |
| 2383 | paths.map((path, index) => |
| 2384 | `${method} ${path} HTTP/1.1\r\nHost: 127.0.0.1\r\n${ |
| 2385 | index === paths.length - 1 ? "Connection: close\r\n" : "" |
| 2386 | }\r\n` |
| 2387 | ).join("") |
| 2388 | ) |
| 2389 | }) |
| 2390 | let received = "" |
| 2391 | socket.setEncoding("latin1") |
| 2392 | socket.on("data", (chunk) => received += chunk) |
| 2393 | socket.on("error", constVoid) |
| 2394 | socket.on("close", () => resume(Effect.succeed(received))) |
| 2395 | return Effect.sync(() => socket.destroy()) |
| 2396 | }).pipe(Effect.timeout("2 seconds")) |
| 2397 | |
| 2398 | interface WebSocketFrame { |
| 2399 | readonly opcode: number |
| 2400 | readonly payload: Buffer.Buffer |
| 2401 | } |
| 2402 | |
| 2403 | const parseWebSocketFrames = ( |
| 2404 | stream: Buffer.Buffer |
| 2405 | ): { readonly frames: ReadonlyArray<WebSocketFrame>; readonly trailing: Buffer.Buffer } => { |
| 2406 | const frames: Array<WebSocketFrame> = [] |
| 2407 | let offset = 0 |
| 2408 | while (stream.length - offset >= 2) { |
| 2409 | const first = stream[offset] |
| 2410 | const second = stream[offset + 1] |
| 2411 | const opcode = first & 0x0f |
| 2412 | let length = second & 0x7f |
| 2413 | let headerSize = 2 |
| 2414 | if (length === 126) { |
| 2415 | if (stream.length - offset < 4) break |
| 2416 | length = stream.readUInt16BE(offset + 2) |
| 2417 | headerSize = 4 |
| 2418 | } else if (length === 127) { |
| 2419 | break |
| 2420 | } |
| 2421 | const controlFrame = (opcode & 0x8) !== 0 |
| 2422 | if ( |
| 2423 | (first & 0x70) !== 0 || (second & 0x80) !== 0 || |
| 2424 | ![0x0, 0x1, 0x2, 0x8, 0x9, 0xa].includes(opcode) || |
| 2425 | (controlFrame && length > 125) |
| 2426 | ) { |
| 2427 | break |
| 2428 | } |
| 2429 | if (stream.length - offset < headerSize + length) break |
| 2430 | frames.push({ opcode, payload: stream.subarray(offset + headerSize, offset + headerSize + length) }) |
| 2431 | offset += headerSize + length |
| 2432 | } |
| 2433 | return { frames, trailing: stream.subarray(offset) } |
| 2434 | } |
| 2435 | |
| 2436 | const upgradeRequest = (path: string): string => |
| 2437 | [ |
| 2438 | `GET ${path} HTTP/1.1`, |
| 2439 | "Host: 127.0.0.1", |
| 2440 | "Upgrade: websocket", |
| 2441 | "Connection: Upgrade", |
| 2442 | `Sec-WebSocket-Key: ${randomBytes(16).toString("base64")}`, |
| 2443 | "Sec-WebSocket-Version: 13", |
| 2444 | "", |
| 2445 | "" |
| 2446 | ].join("\r\n") |
| 2447 | |
| 2448 | const closeFrame = (code: number): Buffer.Buffer => { |
| 2449 | const payload = Buffer.Buffer.alloc(2) |
| 2450 | payload.writeUInt16BE(code, 0) |
| 2451 | const mask = randomBytes(4) |
| 2452 | const masked = Buffer.Buffer.from(payload) |
| 2453 | for (let index = 0; index < masked.length; index++) { |
| 2454 | masked[index] = masked[index] ^ mask[index % 4] |
| 2455 | } |
| 2456 | return Buffer.Buffer.concat([Buffer.Buffer.from([0x88, 0x80 | payload.length]), mask, masked]) |
| 2457 | } |
| 2458 | |
| 2459 | const rawWebSocket = ( |
| 2460 | port: number, |
| 2461 | path: string |
| 2462 | ): Promise<{ readonly frames: ReadonlyArray<WebSocketFrame>; readonly trailing: Buffer.Buffer }> => |
| 2463 | new Promise((resolve, reject) => { |
| 2464 | const socket = Net.createConnection({ host: "127.0.0.1", port }) |
| 2465 | const timer = setTimeout(() => { |
| 2466 | socket.destroy() |
| 2467 | reject(new Error("the websocket conversation did not finish in time")) |
| 2468 | }, 5000) |
| 2469 | let upgraded = false |
| 2470 | let stream = Buffer.Buffer.alloc(0) |
| 2471 | let closeEchoed = false |
| 2472 | socket.on("connect", () => socket.write(upgradeRequest(path))) |
| 2473 | socket.on("data", (data) => { |
| 2474 | stream = Buffer.Buffer.concat([stream, Buffer.Buffer.from(data)]) |
| 2475 | if (!upgraded) { |
| 2476 | const headerEnd = stream.indexOf("\r\n\r\n") |
| 2477 | if (headerEnd === -1) return |
| 2478 | if (!stream.subarray(0, headerEnd).toString().includes("101")) { |
| 2479 | clearTimeout(timer) |
| 2480 | socket.destroy() |
| 2481 | reject(new Error("the upgrade was refused")) |
| 2482 | return |
| 2483 | } |
| 2484 | upgraded = true |
| 2485 | stream = Buffer.Buffer.from(stream.subarray(headerEnd + 4)) |
| 2486 | } |
| 2487 | if (!closeEchoed) { |
| 2488 | const close = parseWebSocketFrames(stream).frames.find((frame) => frame.opcode === 8) |
| 2489 | if (close !== undefined) { |
| 2490 | closeEchoed = true |
| 2491 | socket.write(closeFrame(close.payload.length >= 2 ? close.payload.readUInt16BE(0) : 1000)) |
| 2492 | } |
| 2493 | } |
| 2494 | }) |
| 2495 | socket.on("error", constVoid) |
| 2496 | socket.on("close", () => { |
| 2497 | clearTimeout(timer) |
| 2498 | resolve(parseWebSocketFrames(stream)) |
| 2499 | }) |
| 2500 | }) |
| 2501 | |
| 2502 | // Writes `input`, half-closes, and resolves with everything the server sent. |
| 2503 | const rawExchange = (port: number, input: string | ReadonlyArray<string>): Promise<string> => |
| 2504 | new Promise((resolve, reject) => { |
| 2505 | const socket = Net.createConnection({ host: "127.0.0.1", port }) |
| 2506 | const timer = setTimeout(() => { |
| 2507 | socket.destroy() |
| 2508 | reject(new Error("the exchange did not finish in time")) |
| 2509 | }, 5000) |
| 2510 | let stream = Buffer.Buffer.alloc(0) |
| 2511 | // Pause between parts so the server reads them separately. |
| 2512 | const parts = typeof input === "string" ? [input] : [...input] |
| 2513 | const writeNext = () => { |
| 2514 | const part = parts.shift()! |
| 2515 | if (parts.length === 0) return socket.end(part) |
| 2516 | socket.write(part) |
| 2517 | setTimeout(writeNext, 50) |
| 2518 | } |
| 2519 | socket.on("connect", writeNext) |
| 2520 | socket.on("data", (data) => { |
| 2521 | stream = Buffer.Buffer.concat([stream, Buffer.Buffer.from(data)]) |
| 2522 | }) |
| 2523 | socket.on("error", constVoid) |
| 2524 | socket.on("close", () => { |
| 2525 | clearTimeout(timer) |
| 2526 | resolve(stream.toString()) |
| 2527 | }) |
| 2528 | }) |
| 2529 | |
| 2530 | const rawUpgradeRequest = (port: number, path: string): Promise<string> => |
| 2531 | new Promise((resolve, reject) => { |
| 2532 | const socket = Net.createConnection({ host: "127.0.0.1", port }) |
| 2533 | const timer = setTimeout(() => { |
| 2534 | socket.destroy() |
| 2535 | reject(new Error("the upgrade request was not answered in time")) |
| 2536 | }, 5000) |
| 2537 | let stream = Buffer.Buffer.alloc(0) |
| 2538 | socket.on("connect", () => socket.write(upgradeRequest(path))) |
| 2539 | socket.on("data", (data) => { |
| 2540 | stream = Buffer.Buffer.concat([stream, Buffer.Buffer.from(data)]) |
| 2541 | }) |
| 2542 | socket.on("error", constVoid) |
| 2543 | socket.on("close", () => { |
| 2544 | clearTimeout(timer) |
| 2545 | resolve(stream.toString()) |
| 2546 | }) |
| 2547 | }) |
| 2548 | |
| 2549 | // Flush only headers. Like curl, upload only after the server asks for the body. |
| 2550 | const expectContinueRequest = (port: number, payload: string, headers: Http.OutgoingHttpHeaders = {}) => |
| 2551 | Effect.callback<{ status: number; body: string; continues: number; uploaded: boolean }, Error>((resume) => { |
| 2552 | let continues = 0 |
| 2553 | let uploaded = false |
| 2554 | const request = Http.request({ |
| 2555 | host: "127.0.0.1", |
| 2556 | port, |
| 2557 | method: "POST", |
| 2558 | headers: { |
| 2559 | ...headers, |
| 2560 | expect: "100-continue", |
| 2561 | "content-length": Buffer.Buffer.byteLength(payload), |
| 2562 | connection: "close" |
| 2563 | }, |
| 2564 | agent: false |
| 2565 | }, (response) => { |
| 2566 | let body = "" |
| 2567 | response.setEncoding("utf8") |
| 2568 | response.on("data", (chunk: string) => { |
| 2569 | body += chunk |
| 2570 | }) |
| 2571 | response.on("error", (error) => resume(Effect.fail(error))) |
| 2572 | response.on("end", () => resume(Effect.succeed({ status: response.statusCode!, body, continues, uploaded }))) |
| 2573 | }) |
| 2574 | request.on("continue", () => { |
| 2575 | continues++ |
| 2576 | if (!uploaded) { |
| 2577 | uploaded = true |
| 2578 | request.end(payload) |
| 2579 | } |
| 2580 | }) |
| 2581 | request.on("error", (error) => resume(Effect.fail(error))) |
| 2582 | request.setTimeout(3000, () => request.destroy(new Error("Expect: 100-continue request timed out"))) |
| 2583 | request.flushHeaders() |
| 2584 | return Effect.sync(() => request.destroy()) |
| 2585 | }) |