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

Function makeHandler

packages/platform/node/src/NodeHttpServer.ts:347–391  ·  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

345 * @since 4.0.0
346 */
347export const makeHandler = <
348 R,
349 E,
350 App extends Effect.Effect<HttpServerResponse, any, any> = Effect.Effect<HttpServerResponse, E, R>
351>(
352 httpEffect: Effect.Effect<HttpServerResponse, E, R>,
353 options: {
354 readonly scope: Scope.Scope
355 readonly middleware?: Middleware.HttpMiddleware.Applied<App, E, R> | undefined
356 }
357): Effect.Effect<
358 (
359 nodeRequest: Http.IncomingMessage | Http2.Http2ServerRequest,
360 nodeResponse: Http.ServerResponse | Http2.Http2ServerResponse
361 ) => void,
362 never,
363 Exclude<Effect.Services<App>, HttpServerRequest | Scope.Scope>
364> => {
365 const handled = HttpEffect.toHandled(httpEffect, handleResponse, options.middleware as any)
366 return Effect.withFiber((parent) => {
367 const services = parent.context
368 return Effect.succeed(function handler(
369 request: Http.IncomingMessage | Http2.Http2ServerRequest,
370 response: Http.ServerResponse | Http2.Http2ServerResponse
371 ) {
372 // The HTTP/2 compatibility API mirrors the members the adapter uses.
373 const nodeRequest = request as Http.IncomingMessage
374 const nodeResponse = response as Http.ServerResponse
375 nodeRequest.on("error", constVoid)
376 if (nodeRequest.httpVersionMajor === 1) {
377 // Prepended so it runs before Node's own `finish` listener drains or destroys.
378 nodeResponse.prependListener("finish", () => teardownBody(nodeRequest, nodeResponse))
379 }
380 const context = Context.add(services, HttpServerRequest, new ServerRequestImpl(nodeRequest, nodeResponse))
381 const fiber = Fiber.runIn(Effect.runForkWith(context as Context.Context<any>)(handled), options.scope)
382 if (fiber.pollUnsafe() === undefined) {
383 nodeResponse.on("close", () => {
384 if (!nodeResponse.writableEnded) {
385 fiber.interruptUnsafe(parent.id, ClientAbort.annotation)
386 }
387 })
388 }
389 })
390 })
391}
392
393/**
394 * Creates a Node `upgrade` event handler for an Effect HTTP application,

Callers 1

NodeHttpServer.tsFile · 0.70

Calls 6

teardownBodyFunction · 0.85
pollUnsafeMethod · 0.80
onMethod · 0.65
addMethod · 0.65
succeedMethod · 0.45
interruptUnsafeMethod · 0.45

Tested by

no test coverage detected