The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | /** |
| 2 | * Deno implementation of the Effect HTTP platform service. |
| 3 | * |
| 4 | * File responses use `Deno.FsFile.readable` directly so full-file responses |
| 5 | * retain Deno's resource-backed fast path. Deno closes the file after the body |
| 6 | * is sent, but server adapters must cancel a raw body when they do not send it, |
| 7 | * including when handling `HEAD` requests. |
| 8 | * |
| 9 | * The provided layer uses strong ETags, unlike the portable `HttpPlatform` |
| 10 | * layer, which uses weak ETags. |
| 11 | * |
| 12 | * @stability unstable |
| 13 | * @since 4.0.0 |
| 14 | */ |
| 15 | import * as NodeHttpCompression from "@effect/platform-node-shared/NodeHttpCompression" |
| 16 | import { contentType } from "@std/media-types" |
| 17 | import { extname } from "@std/path" |
| 18 | import { ByteSliceStream } from "@std/streams" |
| 19 | import * as Etag from "effect/http/Etag" |
| 20 | import * as Platform from "effect/http/HttpPlatform" |
| 21 | import * as Response from "effect/http/HttpServerResponse" |
| 22 | import * as Layer from "effect/Layer" |
| 23 | import * as DenoFileSystem from "./DenoFileSystem.ts" |
| 24 | |
| 25 | // gzip and deflate use the native CompressionStream, which does not expose a |
| 26 | // per-chunk flush control. br and zstd go through node:zlib compatibility |
| 27 | // streams and flush each input chunk. |
| 28 | const compression = NodeHttpCompression.make(Platform.makeCompressionWeb({ |
| 29 | algorithms: NodeHttpCompression.algorithms, |
| 30 | transform: (algorithm, options) => |
| 31 | algorithm === "gzip" || algorithm === "deflate" |
| 32 | ? Platform.compressionTransformWeb(algorithm) |
| 33 | : NodeHttpCompression.compressTransformWeb(algorithm, options) |
| 34 | })) |
| 35 | |
| 36 | /** |
| 37 | * Creates the Deno `HttpPlatform`, serving file responses from resource-backed |
| 38 | * readable streams and adding content type and content length headers. |
| 39 | * |
| 40 | * @stability unstable |
| 41 | * @category constructors |
| 42 | * @since 4.0.0 |
| 43 | */ |
| 44 | export const make = Platform.make({ |
| 45 | platform: "deno", |
| 46 | compression, |
| 47 | fileResponse(path, status, statusText, headers, start, end, contentLength) { |
| 48 | let body: ReadableStream<Uint8Array> |
| 49 | if (contentLength === BigInt(0)) { |
| 50 | body = new ReadableStream<Uint8Array>({ |
| 51 | start(controller) { |
| 52 | controller.close() |
| 53 | } |
| 54 | }) |
| 55 | } else { |
| 56 | const file = Deno.openSync(path) |
| 57 | file.seekSync(start, Deno.SeekMode.Start) |
| 58 | body = end === undefined |
| 59 | ? file.readable |
| 60 | : file.readable.pipeThrough(new ByteSliceStream(0, end - start - 1)) |
| 61 | } |
| 62 | return Response.raw(body, { |
| 63 | headers: { |
| 64 | ...headers, |
| 65 | "content-type": headers["content-type"] ?? contentType(extname(path)) ?? "application/octet-stream", |
| 66 | "content-length": contentLength.toString() |
| 67 | }, |
| 68 | status, |
| 69 | statusText |
| 70 | }) |
| 71 | }, |
| 72 | fileWebResponse(file, status, statusText, headers, options) { |
| 73 | const offset = options?.offset ?? 0 |
| 74 | const available = Math.max(0, file.size - offset) |
| 75 | const contentLength = options?.bytesToRead === undefined |
| 76 | ? available |
| 77 | : Math.min(available, Math.max(0, options.bytesToRead)) |
| 78 | let body: typeof file | ReadableStream<Uint8Array> = file |
| 79 | if (contentLength === 0) { |
| 80 | body = new ReadableStream<Uint8Array>({ |
| 81 | start(controller) { |
| 82 | controller.close() |
| 83 | } |
| 84 | }) |
| 85 | } else if (offset > 0 || options?.bytesToRead !== undefined) { |
| 86 | body = (file.stream() as ReadableStream<Uint8Array>).pipeThrough( |
| 87 | new ByteSliceStream(offset, offset + contentLength - 1) |
| 88 | ) |
| 89 | } |
| 90 | return Response.raw(body, { |
| 91 | headers: { |
| 92 | ...headers, |
| 93 | "content-type": headers["content-type"] ?? |
| 94 | (file.type === "" ? contentType(extname(file.name)) ?? "application/octet-stream" : file.type), |
| 95 | "content-length": contentLength.toString() |
| 96 | }, |
| 97 | status, |
| 98 | statusText |
| 99 | }) |
| 100 | } |
| 101 | }) |
| 102 | |
| 103 | /** |
| 104 | * Provides the Deno `HttpPlatform` together with its filesystem and strong ETag |
| 105 | * services. |
| 106 | * |
| 107 | * @stability unstable |
| 108 | * @category layers |
| 109 | * @since 4.0.0 |
| 110 | */ |
| 111 | export const layer: Layer.Layer<Platform.HttpPlatform> = Layer.effect(Platform.HttpPlatform)(make).pipe( |
| 112 | Layer.provide(DenoFileSystem.layer), |
| 113 | Layer.provide(Etag.layer) |
| 114 | ) |