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

Function onSslResponse

packages/sql/pg/src/PgConnection.ts:2614–2665  ·  view source on GitHub ↗
(chunk: Uint8Array)

Source from the content-addressed store, hash-verified

2612 }
2613
2614 const onSslResponse = (chunk: Uint8Array): void => {
2615 if (done) return
2616 if (sslErrorParser !== undefined || chunk[0] === 0x45) {
2617 sslErrorParser ??= PgProtocol.makeParser({ maxMessageSize: config.maxMessageSize })
2618 let messages: ReadonlyArray<PgProtocol.BackendMessage>
2619 try {
2620 messages = sslErrorParser.push(chunk)
2621 } catch (cause) {
2622 return failConnect(cause, "PgConnection: Failed to parse SSLRequest error response")
2623 }
2624 if (messages.length === 0) return
2625 socket.off("data", onSslResponse)
2626 const message = messages[0]
2627 if (messages.length !== 1 || message._tag !== "ErrorResponse") {
2628 return failConnect(
2629 new Error("Expected one ErrorResponse after SSLRequest"),
2630 "PgConnection: Invalid SSLRequest response"
2631 )
2632 }
2633 return failErrorResponse(message.fields, "PgConnection: Failed to negotiate TLS")
2634 }
2635 socket.off("data", onSslResponse)
2636 if (chunk.length !== 1) {
2637 return failConnect(
2638 new Error(`Received ${chunk.length} bytes in response to SSLRequest`),
2639 "PgConnection: Invalid SSLRequest response"
2640 )
2641 }
2642 const response = PgProtocol.decodeSslResponse(chunk[0])
2643 if (EffectResult.isFailure(response)) {
2644 return failConnect(response.failure, "PgConnection: Invalid SSLRequest response")
2645 }
2646 if (response.success === "N") {
2647 if (config.sslOptional) return startup()
2648 return failConnect(new Error("The server does not support TLS"), "PgConnection: Server refused TLS", "refused")
2649 }
2650 const raw = socket
2651 raw.off("error", onError)
2652 raw.off("close", onClose)
2653 socket = Tls.connect({
2654 host: config.host,
2655 servername: Net.isIP(config.host) === 0 ? config.host : undefined,
2656 ...(typeof config.ssl === "object" ? config.ssl : {}),
2657 socket: raw as Net.Socket
2658 })
2659 socket.on("error", onError)
2660 socket.on("close", onClose)
2661 socket.once("secureConnect", () => {
2662 encrypted = true
2663 startup()
2664 })
2665 }
2666
2667 const begin = (): void => {
2668 if (config.ssl === false) return startup()

Callers

nothing calls this directly

Calls 7

failConnectFunction · 0.85
failErrorResponseFunction · 0.85
startupFunction · 0.85
onceMethod · 0.80
pushMethod · 0.65
connectMethod · 0.65
onMethod · 0.65

Tested by

no test coverage detected