Hyperlinkv0.8.0-beta.28

NodeHttpServer

<
  R,
  E,
  App extends Effect.Effect<HttpServerResponse, any, any> = Effect.Effect<
    HttpServerResponse,
    E,
    R
  >
>(
  httpEffect: Effect.Effect<HttpServerResponse, E, R>,
  options: {
    readonly scope: Scope.Scope
    readonly middleware?:
      | Middleware.HttpMiddleware.Applied<App, E, R>
      | undefined
  }
): Effect.Effect<
  (
    nodeRequest: Http.IncomingMessage,
    nodeResponse: Http.ServerResponse
  ) => void,
  never,
  Exclude<Effect.Services<App>, HttpServerRequest | Scope.Scope>
>

Creates a Node request event handler for an Effect HTTP application, injecting a HttpServerRequest and interrupting the request fiber if the client closes the response before it finishes.

handlers
export const makeHandler = <
  R,
  E,
  App extends Effect.Effect<HttpServerResponse, any, any> = Effect.Effect<HttpServerResponse, E, R>
>(
  httpEffect: Effect.Effect<HttpServerResponse, E, R>,
  options: {
    readonly scope: Scope.Scope
    readonly middleware?: Middleware.HttpMiddleware.Applied<App, E, R> | undefined
  }
): Effect.Effect<
  (nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse) => void,
  never,
  Exclude<Effect.Services<App>, HttpServerRequest | Scope.Scope>
> => {
  const handled = HttpEffect.toHandled(httpEffect, handleResponse, options.middleware as any)
  return Effect.withFiber((parent) => {
    const services = parent.context
    return Effect.succeed(function handler(
      nodeRequest: Http.IncomingMessage,
      nodeResponse: Http.ServerResponse
    ) {
      const map = new Map(services.mapUnsafe)
      map.set(HttpServerRequest.key, new ServerRequestImpl(nodeRequest, nodeResponse))
      const fiber = Fiber.runIn(Effect.runForkWith(Context.makeUnsafe<any>(map))(handled), options.scope)
      nodeResponse.on("close", () => {
        if (!nodeResponse.writableEnded) {
          fiber.interruptUnsafe(parent.id, ClientAbort.annotation)
        }
      })
    })
  })
}
Referenced by 1 symbols