t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/AWS/Lambda/init-io-probe.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

65 lines
2.1 KiB
TypeScript

import * as AWS from "@/AWS/index.ts";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as HttpClient from "effect/http/HttpClient";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
/**
* A service whose Layer performs real async I/O during the sandbox's init
* (cold start) — the "fetch a value once and cache it for every
* invocation" pattern.
*/
class TraceConfig extends Context.Service<
TraceConfig,
{ trace: string; nonce: string }
>()("InitIO.TraceConfig") {}
const TraceConfigLive = Layer.effect(
TraceConfig,
Effect.gen(function* () {
// Counted per sandbox so every response can assert the layer's I/O ran
// exactly once no matter how many invocations the sandbox has served.
yield* Effect.sync(() => {
(globalThis as any).__initFetches =
((globalThis as any).__initFetches ?? 0) + 1;
});
const client = yield* HttpClient.HttpClient;
const response = yield* client.get(
"https://www.cloudflare.com/cdn-cgi/trace",
);
const trace = yield* response.text;
return { trace, nonce: crypto.randomUUID() };
}).pipe(
// A failed init fetch is a defect: the sandbox is useless without its
// config, and init's typed error channel is reserved for ConfigError.
Effect.orDie,
),
);
/**
* Pins that one-shot async I/O during init / layer build works on Lambda:
* the layer's fetch runs exactly once per sandbox (at cold start, under the
* instance scope) and the cached plain value serves every invocation.
*/
export default class InitIOProbe extends AWS.Lambda.Function<InitIOProbe>()(
"InitIOProbe",
{
main: import.meta.url,
functionUrl: true,
},
Effect.gen(function* () {
const config = yield* TraceConfig;
return {
fetch: Effect.gen(function* () {
return yield* HttpServerResponse.json({
nonce: config.nonce,
traceLength: config.trace.length,
hasUag: config.trace.includes("uag="),
initFetches: (globalThis as any).__initFetches ?? 0,
});
}),
};
}).pipe(Effect.provide(TraceConfigLive)),
) {}