(
options: {
background?: boolean;
failTask?: boolean;
failRequest?: boolean;
blockRequest?: boolean;
} = {},
)
| 9 | ) {} |
| 10 | |
| 11 | const fixture = ( |
| 12 | options: { |
| 13 | background?: boolean; |
| 14 | failTask?: boolean; |
| 15 | failRequest?: boolean; |
| 16 | blockRequest?: boolean; |
| 17 | } = {}, |
| 18 | ) => { |
| 19 | const resources: Resource["Service"][] = []; |
| 20 | const gates: ReturnType<typeof Promise.withResolvers<void>>[] = []; |
| 21 | const keptAlive: Promise<void>[] = []; |
| 22 | const writes: number[] = []; |
| 23 | const entered = Promise.withResolvers<void>(); |
| 24 | const resource = Layer.effect(Resource)( |
| 25 | Effect.acquireRelease( |
| 26 | Effect.sync(() => { |
| 27 | const acquired = { id: resources.length, closed: false }; |
| 28 | resources.push(acquired); |
| 29 | gates.push(Promise.withResolvers<void>()); |
| 30 | return acquired; |
| 31 | }), |
| 32 | (acquired) => |
| 33 | Effect.sync(() => { |
| 34 | acquired.closed = true; |
| 35 | }), |
| 36 | ), |
| 37 | ); |
| 38 | const routes = HttpRouter.add( |
| 39 | "GET", |
| 40 | "/", |
| 41 | Effect.gen(function* () { |
| 42 | const acquired = yield* Resource; |
| 43 | if (options.background !== false) { |
| 44 | const tasks = yield* RequestBackgroundTasks; |
| 45 | const gate = gates[acquired.id]; |
| 46 | if (!gate) return yield* Effect.die("Missing request gate"); |
| 47 | const task = Effect.runPromise( |
| 48 | Effect.gen(function* () { |
| 49 | yield* Effect.promise(() => gate.promise); |
| 50 | if (options.failTask) return yield* Effect.fail("Background failure"); |
| 51 | if (acquired.closed) |
| 52 | return yield* Effect.fail("Resource closed before background write"); |
| 53 | writes.push(acquired.id); |
| 54 | }), |
| 55 | ); |
| 56 | keptAlive.push(tasks.retain(task)); |
| 57 | } |
| 58 | entered.resolve(); |
| 59 | if (options.blockRequest) return yield* Effect.never; |
| 60 | if (options.failRequest) return yield* Effect.die("Request failure"); |
| 61 | return HttpServerResponse.empty(); |
| 62 | }), |
| 63 | ); |
| 64 | const app = HttpRouter.toWebHandler( |
| 65 | routes.pipe( |
| 66 | Layer.provide(requestScopedMiddleware(resource).layer), |
| 67 | Layer.provideMerge(HttpServer.layerServices), |
| 68 | ), |
no test coverage detected