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

Function makeHandler

packages/platform/node/src/NodeHttpServer.ts:187–218  ·  view source on GitHub ↗
(
  httpEffect: Effect.Effect<HttpServerResponse, E, R>,
  options: {
    readonly scope: Scope.Scope
    readonly middleware?: Middleware.HttpMiddleware.Applied<App, E, R> | undefined
  }
)

Source from the content-addressed store, hash-verified

185 * @since 4.0.0
186 */
187export 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,

Callers 1

NodeHttpServer.tsFile · 0.70

Calls 4

interruptUnsafeMethod · 0.80
addMethod · 0.65
onMethod · 0.65
succeedMethod · 0.45

Tested by

no test coverage detected