Skip to content

Commit

Permalink
allow Request.signal to be missing in .toWebHandler apis (#3816)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Oct 22, 2024
1 parent afb4e43 commit c86b1d7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-terms-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform": patch
---

allow Request.signal to be missing in .toWebHandler apis
33 changes: 18 additions & 15 deletions packages/platform/src/HttpApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,24 @@ export const withPreResponseHandler = internal.withPreResponseHandler
*/
export const toWebHandlerRuntime = <R>(runtime: Runtime.Runtime<R>) => {
const run = Runtime.runFork(runtime)
return <E>(self: Default<E, R | Scope.Scope>, middleware?: HttpMiddleware | undefined) =>
(request: Request): Promise<Response> =>
new Promise((resolve) => {
const fiber = run(Effect.provideService(
toHandled(self, (request, response) => {
resolve(ServerResponse.toWeb(response, { withoutBody: request.method === "HEAD", runtime }))
return Effect.void
}, middleware),
ServerRequest.HttpServerRequest,
ServerRequest.fromWeb(request)
))
request.signal.addEventListener("abort", () => {
fiber.unsafeInterruptAsFork(ServerError.clientAbortFiberId)
}, { once: true })
})
return <E>(self: Default<E, R | Scope.Scope>, middleware?: HttpMiddleware | undefined) => {
const resolveSymbol = Symbol.for("@effect/platform/HttpApp/resolve")
const httpApp = toHandled(self, (request, response) => {
;(request as any)[resolveSymbol](
ServerResponse.toWeb(response, { withoutBody: request.method === "HEAD", runtime })
)
return Effect.void
}, middleware)
return (request: Request): Promise<Response> =>
new Promise((resolve) => {
const httpServerRequest = ServerRequest.fromWeb(request)
;(httpServerRequest as any)[resolveSymbol] = resolve
const fiber = run(Effect.provideService(httpApp, ServerRequest.HttpServerRequest, httpServerRequest))
request.signal?.addEventListener("abort", () => {
fiber.unsafeInterruptAsFork(ServerError.clientAbortFiberId)
}, { once: true })
})
}
}

/**
Expand Down

0 comments on commit c86b1d7

Please sign in to comment.