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

Function makeUpgradeHandler

packages/platform/node/src/NodeHttpServer.ts:424–564  ·  view source on GitHub ↗
(
  lazyWss: Effect.Effect<NodeWS.WebSocketServer>,
  httpEffect: Effect.Effect<HttpServerResponse, E, R>,
  options: {
    readonly scope: Scope.Scope
    readonly middleware?: Middleware.HttpMiddleware.Applied<App, E, R> | undefined
    /**
     * Inbound WebSocket bytes that may wait for a reader pull before the
     * connection is paused.
     *
     * @default 65536
     */
    readonly highWaterMark?: number | undefined
  }
)

Source from the content-addressed store, hash-verified

422 * @since 4.0.0
423 */
424export const makeUpgradeHandler = <
425 R,
426 E,
427 App extends Effect.Effect<HttpServerResponse, any, any> = Effect.Effect<HttpServerResponse, E, R>
428>(
429 lazyWss: Effect.Effect<NodeWS.WebSocketServer>,
430 httpEffect: Effect.Effect<HttpServerResponse, E, R>,
431 options: {
432 readonly scope: Scope.Scope
433 readonly middleware?: Middleware.HttpMiddleware.Applied<App, E, R> | undefined
434 /**
435 * Inbound WebSocket bytes that may wait for a reader pull before the
436 * connection is paused.
437 *
438 * @default 65536
439 */
440 readonly highWaterMark?: number | undefined
441 }
442): Effect.Effect<
443 (nodeRequest: Http.IncomingMessage, socket: Duplex, head: Buffer) => void,
444 never,
445 Exclude<Effect.Services<App>, HttpServerRequest | Scope.Scope>
446> => {
447 const handledApp = HttpEffect.toHandled(httpEffect, handleResponse, options.middleware as any)
448 return Effect.withFiber((parent) => {
449 const services = parent.context
450 return Effect.succeed(function handler(
451 nodeRequest: Http.IncomingMessage,
452 socket: Duplex,
453 head: Buffer
454 ) {
455 nodeRequest.on("error", constVoid)
456 const protocol = nodeRequest.headers.upgrade
457 const isWebSocket = protocol === undefined || protocol.toLowerCase() === "websocket"
458 let body: Readable | undefined
459 if (!isWebSocket && legacyUpgradeBody) {
460 if (nodeRequest.headers["transfer-encoding"] !== undefined) {
461 socket.on("error", () => {})
462 // Nothing reads the socket after an upgrade, so close it once the 411 is sent.
463 socket.end(
464 "HTTP/1.1 411 Length Required\r\nConnection: close\r\nContent-Length: 0\r\n\r\n",
465 () => socket.destroy()
466 )
467 return
468 }
469 const framed = Upgrade.frameBody(socket, head, Number(nodeRequest.headers["content-length"] ?? 0))
470 body = framed.body
471 head = framed.rest
472 }
473 let nodeResponse_: Http.ServerResponse | undefined = undefined
474 const nodeResponse = () => {
475 if (nodeResponse_ === undefined) {
476 nodeResponse_ = new http.ServerResponse(nodeRequest)
477 if (state.response !== undefined || socket.destroyed) {
478 // End without assigning the socket so handleResponse skips HTTP writes.
479 nodeResponse_.end()
480 } else {
481 nodeResponse_.assignSocket(socket as any)

Callers 1

NodeHttpServer.tsFile · 0.85

Calls 14

onceMethod · 0.80
onCloseFunction · 0.70
closeCodeFunction · 0.70
onMethod · 0.65
endMethod · 0.65
setMethod · 0.65
closeMethod · 0.65
addMethod · 0.65
NumberFunction · 0.50
resumeFunction · 0.50
succeedMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected