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, 06:00 UTC
| 1 | import * as Data from "effect/Data"; |
| 2 | import * as Duration from "effect/Duration"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as HttpClient from "effect/http/HttpClient"; |
| 5 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 6 | import type * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { artifactFileStream, type ArtifactFile } from "./ArtifactFile.ts"; |
| 10 | import { PrismaUploadClient } from "./HttpClient.ts"; |
| 11 | |
| 12 | export class ArtifactUploadOperationFailed extends Data.TaggedError( |
| 13 | "ArtifactUploadOperationFailed", |
| 14 | )<{ |
| 15 | readonly message: string; |
| 16 | readonly cause?: unknown; |
| 17 | }> {} |
| 18 | |
| 19 | const ARTIFACT_UPLOAD_TIMEOUT = Duration.minutes(15); |
| 20 | const UPLOAD_ERROR_BODY_BYTES = 64 * 1024; |
| 21 | |
| 22 | export const executeArtifactUpload = ( |
| 23 | uploadUrl: string, |
| 24 | artifact: Uint8Array | ArtifactFile, |
| 25 | contentType: string, |
| 26 | ) => |
| 27 | Effect.gen(function* () { |
| 28 | // Prefer the Prisma-scoped upload client (node transport, provided by |
| 29 | // live wiring — presigned upload URLs need explicit Content-Length on |
| 30 | // file-backed bodies); fall back to the ambient client so tests can |
| 31 | // stub uploads through `HttpClient.HttpClient`. |
| 32 | const uploadClient = yield* Effect.serviceOption(PrismaUploadClient); |
| 33 | const http = Option.isSome(uploadClient) ? uploadClient.value : yield* HttpClient.HttpClient; |
| 34 | const request = HttpClientRequest.put(uploadUrl).pipe( |
| 35 | artifact instanceof Uint8Array |
| 36 | ? HttpClientRequest.bodyUint8Array(artifact, contentType) |
| 37 | : HttpClientRequest.bodyStream(artifactFileStream(artifact), { |
| 38 | contentType, |
| 39 | contentLength: artifact.size, |
| 40 | }), |
| 41 | ); |
| 42 | const responseOption = yield* http.execute(request).pipe( |
| 43 | Effect.mapError( |
| 44 | () => |
| 45 | new ArtifactUploadOperationFailed({ |
| 46 | message: "Prisma artifact upload transport failed before a response was received.", |
| 47 | }), |
| 48 | ), |
| 49 | Effect.timeoutOption(ARTIFACT_UPLOAD_TIMEOUT), |
| 50 | ); |
| 51 | if (Option.isNone(responseOption)) { |
| 52 | return yield* new ArtifactUploadOperationFailed({ |
| 53 | message: "Prisma artifact upload timed out after 15 minutes.", |
| 54 | }); |
| 55 | } |
| 56 | const response = responseOption.value; |
| 57 | if (response.status < 200 || response.status >= 300) { |
| 58 | const bodyBytesOption = yield* readResponsePrefixByteCount( |
| 59 | response, |
| 60 | UPLOAD_ERROR_BODY_BYTES, |
| 61 | ).pipe( |
| 62 | Effect.timeoutOption(Duration.seconds(2)), |
| 63 | Effect.orElseSucceed(() => Option.none<number>()), |
| 64 | ); |
| 65 | const diagnostic = Option.match(bodyBytesOption, { |
| 66 | onNone: () => "diagnostic body unavailable", |
| 67 | onSome: (bytes) => `diagnostic body prefix: ${bytes} bytes`, |
| 68 | }); |
| 69 | return yield* new ArtifactUploadOperationFailed({ |
| 70 | message: `Prisma artifact upload failed (HTTP ${response.status}; ${diagnostic}).`, |
| 71 | }); |
| 72 | } |
| 73 | }); |
| 74 | |
| 75 | const readResponsePrefixByteCount = ( |
| 76 | response: HttpClientResponse.HttpClientResponse, |
| 77 | limit: number, |
| 78 | ) => |
| 79 | Effect.gen(function* () { |
| 80 | let bytes = 0; |
| 81 | yield* Stream.runForEachWhile(response.stream, (chunk) => |
| 82 | Effect.sync(() => { |
| 83 | const remaining = limit - bytes; |
| 84 | if (remaining <= 0) return false; |
| 85 | bytes += Math.min(chunk.byteLength, remaining); |
| 86 | return bytes < limit; |
| 87 | }), |
| 88 | ); |
| 89 | return bytes; |
| 90 | }); |