MCPcopy Create free account
hub / github.com/Effect-TS/effect / connect

Function connect

packages/platform-bun/test/HttpServer.test.ts:16–47  ·  view source on GitHub ↗
(port: number)

Source from the content-addressed store, hash-verified

14import * as Net from "node:net"
15
16const connect = (port: number) =>
17 Effect.async<{ readonly headers: string; readonly firstByte: number }, Error>((resume) => {
18 const socket = Net.createConnection({ host: "127.0.0.1", port })
19 let received = Buffer.alloc(0)
20 socket.on("connect", () => {
21 socket.write([
22 "GET / HTTP/1.1",
23 `Host: 127.0.0.1:${port}`,
24 "Connection: Upgrade",
25 "Upgrade: websocket",
26 "Sec-WebSocket-Version: 13",
27 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==",
28 "Sec-WebSocket-Extensions: permessage-deflate",
29 "",
30 ""
31 ].join("\r\n"))
32 })
33 socket.on("error", (error) => resume(Effect.fail(error)))
34 socket.on("data", (chunk) => {
35 received = Buffer.concat([received, typeof chunk === "string" ? Buffer.from(chunk) : chunk])
36 const headerEnd = received.indexOf("\r\n\r\n")
37 if (headerEnd === -1) return
38 const offset = headerEnd + 4
39 if (received.length <= offset) return
40 socket.destroy()
41 resume(Effect.succeed({
42 headers: received.subarray(0, headerEnd).toString(),
43 firstByte: received[offset]
44 }))
45 })
46 return Effect.sync(() => socket.destroy())
47 })
48
49describe.skipIf(typeof Bun === "undefined")("HttpServer", () => {
50 it.scoped("negotiates permessage-deflate and compresses outgoing WebSocket text", () =>

Callers 1

HttpServer.test.tsFile · 0.85

Calls 7

destroyMethod · 0.80
syncMethod · 0.80
writeMethod · 0.65
joinMethod · 0.65
failMethod · 0.65
toStringMethod · 0.65
resumeFunction · 0.50

Tested by

no test coverage detected