(
httpEffect: Effect.Effect<HttpServerResponse, E, R>,
options: {
readonly scope: Scope.Scope
readonly middleware?: Middleware.HttpMiddleware.Applied<App, E, R> | undefined
}
)
| 185 | * @since 4.0.0 |
| 186 | */ |
| 187 | export const makeHandler = < |
| 188 | R, |
| 189 | E, |
| 190 | App extends Effect.Effect<HttpServerResponse, any, any> = Effect.Effect<HttpServerResponse, E, R> |
| 191 | >( |
| 192 | httpEffect: Effect.Effect<HttpServerResponse, E, R>, |
| 193 | options: { |
| 194 | readonly scope: Scope.Scope |
| 195 | readonly middleware?: Middleware.HttpMiddleware.Applied<App, E, R> | undefined |
| 196 | } |
| 197 | ): Effect.Effect< |
| 198 | (nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse) => void, |
| 199 | never, |
| 200 | Exclude<Effect.Services<App>, HttpServerRequest | Scope.Scope> |
| 201 | > => { |
| 202 | const handled = HttpEffect.toHandled(httpEffect, handleResponse, options.middleware as any) |
| 203 | return Effect.withFiber((parent) => { |
| 204 | const services = parent.context |
| 205 | return Effect.succeed(function handler( |
| 206 | nodeRequest: Http.IncomingMessage, |
| 207 | nodeResponse: Http.ServerResponse |
| 208 | ) { |
| 209 | const context = Context.add(services, HttpServerRequest, new ServerRequestImpl(nodeRequest, nodeResponse)) |
| 210 | const fiber = Fiber.runIn(Effect.runForkWith(context as Context.Context<any>)(handled), options.scope) |
| 211 | nodeResponse.on("close", () => { |
| 212 | if (!nodeResponse.writableEnded) { |
| 213 | fiber.interruptUnsafe(parent.id, ClientAbort.annotation) |
| 214 | } |
| 215 | }) |
| 216 | }) |
| 217 | }) |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Creates a Node `upgrade` event handler for an Effect HTTP application, |
no test coverage detected