The best way to control Claude Code, Codex and more from any computer or phone

main
ts 71 lines 2.35 KB
Raw
1 /**
2 * Bun implementation of the Effect HTTP platform service.
3 *
4 * This module provides one `layer` for `HttpPlatform`. It implements file
5 * responses with `Bun.file`, supports sliced file responses for byte ranges,
6 * and returns Web `File` values as raw HTTP server responses. The layer also
7 * provides the Bun file-system layer and ETag generator required by
8 * `HttpPlatform`.
9 *
10 * @stability unstable
11 * @since 4.0.0
12 */
13 import * as NodeHttpCompression from "@effect/platform-node-shared/NodeHttpCompression"
14 import type * as Effect from "effect/Effect"
15 import type { FileSystem } from "effect/FileSystem"
16 import * as Etag from "effect/http/Etag"
17 import * as Platform from "effect/http/HttpPlatform"
18 import * as Response from "effect/http/HttpServerResponse"
19 import * as Layer from "effect/Layer"
20 import * as BunFileSystem from "./BunFileSystem.ts"
21
22 // Bun's CompressionStream supports an extended format set covering brotli and
23 // zstd
24 const compression = NodeHttpCompression.make(Platform.makeCompressionWeb({
25 algorithms: ["gzip", "deflate", "br", "zstd"],
26 transform: (algorithm) => Platform.compressionTransformWeb(algorithm === "br" ? "brotli" : algorithm)
27 }))
28
29 /**
30 * @category constructors
31 * @since 4.0.0
32 */
33 const make: Effect.Effect<
34 Platform.HttpPlatform["Service"],
35 never,
36 FileSystem | Etag.Generator
37 > = Platform.make({
38 platform: "bun",
39 compression,
40 fileResponse(path, status, statusText, headers, start, end, contentLength) {
41 let file = Bun.file(path)
42 if (start > 0 || end !== undefined) {
43 file = file.slice(start, end)
44 }
45 return Response.raw(file, {
46 headers: { ...headers, "content-length": contentLength.toString() },
47 status,
48 statusText
49 })
50 },
51 fileWebResponse(file, status, statusText, headers, options) {
52 const start = options?.offset ?? 0
53 const end = options?.bytesToRead !== undefined ? start + options.bytesToRead : undefined
54 const body = start > 0 || end !== undefined
55 ? (file as File).slice(start, end, file.type)
56 : file
57 return Response.raw(body, { headers, status, statusText })
58 }
59 })
60
61 /**
62 * Layer that provides the Bun `HttpPlatform`, including file responses backed by `Bun.file`.
63 *
64 * @stability unstable
65 * @category layers
66 * @since 4.0.0
67 */
68 export const layer = Layer.effect(Platform.HttpPlatform)(make).pipe(
69 Layer.provide(BunFileSystem.layer),
70 Layer.provide(Etag.layer)
71 )