1/**
2 * The `HttpPlatform`, `Etag`, `Path`, and no-op `FileSystem` services `HttpApiBuilder.layer`
3 * wants, for runtimes without a filesystem: Workers, Lambda. File responses
4 * die, compression never engages.
5 */
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as Path from "effect/Path";
10import * as Etag from "effect/http/Etag";
11import * as HttpPlatform from "effect/http/HttpPlatform";
12
13const HttpPlatformStub: Layer.Layer<HttpPlatform.HttpPlatform> = Layer.succeed(
14 HttpPlatform.HttpPlatform,
15 {
16 platform: "web",
17 // Advertises no algorithms, so the compression middleware never engages
18 // and compressResponse is never called.
19 compression: {
20 algorithms: new Set<HttpPlatform.CompressionAlgorithm>(),
21 compressResponse: (response) => Effect.succeed(response),
22 },
23 fileResponse: () => Effect.die("HttpPlatform.fileResponse not supported"),
24 fileWebResponse: () =>
25 Effect.die("HttpPlatform.fileWebResponse not supported"),
26 },
27);
28
29/**
30 * Provide it to `HttpApiBuilder.layer(api)` in a Worker or a Lambda:
31 *
32 * ```typescript
33 * HttpApiBuilder.layer(Api).pipe(
34 * Layer.provide(handlers),
35 * Layer.provide(Http.Platform),
36 * HttpRouter.toHttpEffect,
37 * )
38 * ```
39 */
40export const Platform: Layer.Layer<
41 Etag.Generator | HttpPlatform.HttpPlatform | Path.Path | FileSystem.FileSystem
42> = Layer.mergeAll(
43 Etag.layer,
44 HttpPlatformStub,
45 Path.layer,
46 FileSystem.layerNoop({}),
47);