(runtime: Runtime.Runtime<R>)
| 217 | * @category conversions |
| 218 | */ |
| 219 | export const toWebHandlerRuntime = <R>(runtime: Runtime.Runtime<R>) => { |
| 220 | const httpRuntime: Types.Mutable<Runtime.Runtime<R>> = Runtime.make(runtime) |
| 221 | const run = Runtime.runFork(httpRuntime) |
| 222 | return <E>(self: Default<E, R | Scope.Scope>, middleware?: HttpMiddleware | undefined) => { |
| 223 | const resolveSymbol = Symbol.for("@effect/platform/HttpApp/resolve") |
| 224 | const httpApp = toHandled(self, (request, response) => { |
| 225 | response = unsafeEjectStreamScope(response) |
| 226 | ;(request as any)[resolveSymbol]( |
| 227 | ServerResponse.toWeb(response, { withoutBody: request.method === "HEAD", runtime }) |
| 228 | ) |
| 229 | return Effect.void |
| 230 | }, middleware) |
| 231 | return (request: Request, context?: Context.Context<never> | undefined): Promise<Response> => |
| 232 | new Promise((resolve) => { |
| 233 | const contextMap = new Map<string, any>(runtime.context.unsafeMap) |
| 234 | if (Context.isContext(context)) { |
| 235 | for (const [key, value] of context.unsafeMap) { |
| 236 | contextMap.set(key, value) |
| 237 | } |
| 238 | } |
| 239 | const httpServerRequest = ServerRequest.fromWeb(request) |
| 240 | contextMap.set(ServerRequest.HttpServerRequest.key, httpServerRequest) |
| 241 | ;(httpServerRequest as any)[resolveSymbol] = resolve |
| 242 | httpRuntime.context = Context.unsafeMake(contextMap) |
| 243 | const fiber = run(httpApp as any) |
| 244 | request.signal?.addEventListener("abort", () => { |
| 245 | fiber.unsafeInterruptAsFork(ServerError.clientAbortFiberId) |
| 246 | }, { once: true }) |
| 247 | }) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * @since 1.0.0 |
no test coverage detected