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

Function connect

packages/sql/pg/src/PgConnection.ts:2437–2703  ·  view source on GitHub ↗
(
  config: ResolvedConfig,
  resolvedPassword: string | undefined,
  failure: ConnectFailure
)

Source from the content-addressed store, hash-verified

2435 })
2436
2437const connect = (
2438 config: ResolvedConfig,
2439 resolvedPassword: string | undefined,
2440 failure: ConnectFailure
2441): Effect.Effect<Session, SqlError> =>
2442 Effect.callback<Session, SqlError>((resume) => {
2443 let done = false
2444 let socket: Duplex
2445 let encrypted = false
2446 let parser: PgProtocol.Parser<unknown> | undefined
2447 let sslErrorParser: PgProtocol.Parser | undefined
2448 let scram: PgAuth.ScramState | undefined
2449 let processId = 0
2450 let secretKey = 0
2451
2452 const fail = (reason: SqlErrorReason, type: ConnectFailureType, sqlState?: string): void => {
2453 if (done) return
2454 done = true
2455 failure.type = type
2456 failure.sqlState = sqlState
2457 socket?.destroy()
2458 resume(Effect.fail(new SqlError({ reason })))
2459 }
2460 const failConnect = (cause: unknown, message: string, type: ConnectFailureType = "other"): void =>
2461 fail(new ConnectionError({ cause, message, operation: "connect" }), type)
2462 const failAuth = (cause: unknown, message: string): void =>
2463 fail(new AuthenticationError({ cause, message, operation: "connect" }), "auth")
2464 const failErrorResponse = (fields: PgProtocol.ErrorFields, message: string): void => {
2465 const reason = classifyFields(fields, message, "connect")
2466 const code = fields.code
2467 fail(
2468 reason,
2469 reason._tag === "AuthenticationError" ? "auth" : "refused",
2470 code !== undefined && sqlStatePattern.test(code) ? code : undefined
2471 )
2472 }
2473
2474 const onError = (cause: Error) => failConnect(cause, "PgConnection: Failed to connect", "refused")
2475 const onClose = () =>
2476 failConnect(
2477 new Error("Connection closed unexpectedly"),
2478 "PgConnection: Connection closed during startup",
2479 "refused"
2480 )
2481
2482 const password = (): string | undefined => {
2483 if (resolvedPassword === undefined) {
2484 failAuth(
2485 new Error("The server requested password authentication"),
2486 "PgConnection: No password configured"
2487 )
2488 return undefined
2489 }
2490 return resolvedPassword
2491 }
2492
2493 const handleMessage = (message: PgProtocol.BackendMessage<unknown>): void => {
2494 switch (message._tag) {

Callers 1

makeFunction · 0.70

Calls 8

failConnectFunction · 0.85
onceMethod · 0.80
beginFunction · 0.70
streamMethod · 0.65
connectMethod · 0.65
onMethod · 0.65
syncMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected