From ad48ddc9745ca1383013902abea9756c287a28e8 Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Mon, 7 Oct 2024 23:42:54 +0200 Subject: [PATCH 1/6] logg --- packages/uploadthing/src/sdk/index.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/uploadthing/src/sdk/index.ts b/packages/uploadthing/src/sdk/index.ts index 180995538d..a5f2b874de 100644 --- a/packages/uploadthing/src/sdk/index.ts +++ b/packages/uploadthing/src/sdk/index.ts @@ -82,10 +82,20 @@ export class UTApi { Effect.logDebug(`UT Response`).pipe( Effect.annotateLogs("res", res), ), + onFailure: (err) => - Effect.logError("UploadThing error").pipe( - Effect.annotateLogs("error", err), - ), + err._tag === "ResponseError" + ? Effect.flatMap(err.response.json, (json) => + Effect.logError( + `Failed to request UploadThing API (${err.response.status})`, + ).pipe( + Effect.annotateLogs("response", err.response), + Effect.annotateLogs("json", json), + ), + ) + : Effect.logError("Failed to request UploadThing API").pipe( + Effect.annotateLogs("error", err), + ), }), HttpClientResponse.schemaBodyJsonScoped(responseSchema), ); From 090c69a5d0c77259dd09a7cbc094a51b275c0596 Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Tue, 8 Oct 2024 00:00:44 +0200 Subject: [PATCH 2/6] rm redundant fields --- packages/uploadthing/src/internal/handler.ts | 18 +++++++----------- packages/uploadthing/src/sdk/index.ts | 7 ++----- packages/uploadthing/test/sdk.test.ts | 5 +++++ 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/packages/uploadthing/src/internal/handler.ts b/packages/uploadthing/src/internal/handler.ts index aac2a98104..b3481c0d0b 100644 --- a/packages/uploadthing/src/internal/handler.ts +++ b/packages/uploadthing/src/internal/handler.ts @@ -379,11 +379,11 @@ const handleCallbackRequest = (opts: { }), HttpClientRequest.jsonBody(payload), Effect.flatMap(HttpClient.filterStatusOk(httpClient)), - Effect.tapErrorTag("ResponseError", ({ response: res }) => - Effect.flatMap(res.json, (json) => + Effect.tapErrorTag("ResponseError", (err) => + Effect.flatMap(err.response.json, () => Effect.logError( - `Failed to register callback result (${res.status})`, - ).pipe(Effect.annotateLogs("error", json)), + `Failed to register callback result (${err.response.status})`, + ).pipe(Effect.annotateLogs("response", err.response)), ), ), HttpClientResponse.schemaBodyJsonScoped(CallbackResultResponse), @@ -619,13 +619,10 @@ const handleUploadAction = (opts: { ), onFailure: (err) => err._tag === "ResponseError" - ? Effect.flatMap(err.response.json, (json) => + ? Effect.flatMap(err.response.json, () => Effect.logError( `Failed to register metadata (${err.response.status})`, - ).pipe( - Effect.annotateLogs("response", err.response), - Effect.annotateLogs("json", json), - ), + ).pipe(Effect.annotateLogs("response", err.response)), ) : Effect.logError("Failed to register metadata").pipe( Effect.annotateLogs("error", err), @@ -667,12 +664,11 @@ const handleUploadAction = (opts: { ), onFailure: (err) => err._tag === "ResponseError" - ? Effect.flatMap(err.response.json, (json) => + ? Effect.flatMap(err.response.json, () => Effect.logError( `Failed to forward callback request from dev stream (${err.response.status})`, ).pipe( Effect.annotateLogs("response", err.response), - Effect.annotateLogs("json", json), Effect.annotateLogs("hook", hook), Effect.annotateLogs("signature", signature), Effect.annotateLogs("payload", payload), diff --git a/packages/uploadthing/src/sdk/index.ts b/packages/uploadthing/src/sdk/index.ts index a5f2b874de..245aa7f6c0 100644 --- a/packages/uploadthing/src/sdk/index.ts +++ b/packages/uploadthing/src/sdk/index.ts @@ -85,13 +85,10 @@ export class UTApi { onFailure: (err) => err._tag === "ResponseError" - ? Effect.flatMap(err.response.json, (json) => + ? Effect.flatMap(err.response.json, () => Effect.logError( `Failed to request UploadThing API (${err.response.status})`, - ).pipe( - Effect.annotateLogs("response", err.response), - Effect.annotateLogs("json", json), - ), + ).pipe(Effect.annotateLogs("response", err.response)), ) : Effect.logError("Failed to request UploadThing API").pipe( Effect.annotateLogs("error", err), diff --git a/packages/uploadthing/test/sdk.test.ts b/packages/uploadthing/test/sdk.test.ts index 61edb8d555..05a88f48ae 100644 --- a/packages/uploadthing/test/sdk.test.ts +++ b/packages/uploadthing/test/sdk.test.ts @@ -24,6 +24,11 @@ import { UTFS_IO_URL, } from "./__test-helpers"; +const fooFile = new File(["foo"], "foo.txt", { type: "text/plain" }); + +const utapi = new UTApi({ token: testToken.encoded }); +const result = await utapi.uploadFiles(fooFile); + describe("uploadFiles", () => { const fooFile = new File(["foo"], "foo.txt", { type: "text/plain" }); From 7f73fedef79dba375f5870df5dcb40fa4c0138ff Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Tue, 8 Oct 2024 00:01:28 +0200 Subject: [PATCH 3/6] rm test junk --- packages/uploadthing/test/sdk.test.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/uploadthing/test/sdk.test.ts b/packages/uploadthing/test/sdk.test.ts index 05a88f48ae..61edb8d555 100644 --- a/packages/uploadthing/test/sdk.test.ts +++ b/packages/uploadthing/test/sdk.test.ts @@ -24,11 +24,6 @@ import { UTFS_IO_URL, } from "./__test-helpers"; -const fooFile = new File(["foo"], "foo.txt", { type: "text/plain" }); - -const utapi = new UTApi({ token: testToken.encoded }); -const result = await utapi.uploadFiles(fooFile); - describe("uploadFiles", () => { const fooFile = new File(["foo"], "foo.txt", { type: "text/plain" }); From 9b9bd944d0d433ad12ab6d488d608b387276a542 Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Tue, 8 Oct 2024 00:17:24 +0200 Subject: [PATCH 4/6] utility --- packages/uploadthing/src/internal/handler.ts | 46 +++++++------------- packages/uploadthing/src/internal/logger.ts | 12 +++++ packages/uploadthing/src/sdk/index.ts | 21 ++++----- 3 files changed, 35 insertions(+), 44 deletions(-) diff --git a/packages/uploadthing/src/internal/handler.ts b/packages/uploadthing/src/internal/handler.ts index b3481c0d0b..9ecc8a0aad 100644 --- a/packages/uploadthing/src/internal/handler.ts +++ b/packages/uploadthing/src/internal/handler.ts @@ -31,7 +31,11 @@ import * as pkgJson from "../../package.json"; import { configProvider, IngestUrl, IsDevelopment, UTToken } from "./config"; import { formatError } from "./error-formatter"; import { handleJsonLineStream } from "./jsonl"; -import { withLogFormat, withMinimalLogLevel } from "./logger"; +import { + logHttpClientError, + withLogFormat, + withMinimalLogLevel, +} from "./logger"; import { getParseFn } from "./parser"; import { assertFilesMeetConfig, extractRouterConfig } from "./route-config"; import { @@ -379,12 +383,8 @@ const handleCallbackRequest = (opts: { }), HttpClientRequest.jsonBody(payload), Effect.flatMap(HttpClient.filterStatusOk(httpClient)), - Effect.tapErrorTag("ResponseError", (err) => - Effect.flatMap(err.response.json, () => - Effect.logError( - `Failed to register callback result (${err.response.status})`, - ).pipe(Effect.annotateLogs("response", err.response)), - ), + Effect.tapError( + logHttpClientError("Failed to register callback result"), ), HttpClientResponse.schemaBodyJsonScoped(CallbackResultResponse), Effect.tap(Effect.log("Sent callback result to UploadThing")), @@ -617,16 +617,7 @@ const handleUploadAction = (opts: { Effect.logDebug("Registerred metadata").pipe( Effect.annotateLogs("response", res), ), - onFailure: (err) => - err._tag === "ResponseError" - ? Effect.flatMap(err.response.json, () => - Effect.logError( - `Failed to register metadata (${err.response.status})`, - ).pipe(Effect.annotateLogs("response", err.response)), - ) - : Effect.logError("Failed to register metadata").pipe( - Effect.annotateLogs("error", err), - ), + onFailure: logHttpClientError("Failed to register metadata"), }), ); @@ -663,20 +654,13 @@ const handleUploadAction = (opts: { Effect.annotateLogs("payload", payload), ), onFailure: (err) => - err._tag === "ResponseError" - ? Effect.flatMap(err.response.json, () => - Effect.logError( - `Failed to forward callback request from dev stream (${err.response.status})`, - ).pipe( - Effect.annotateLogs("response", err.response), - Effect.annotateLogs("hook", hook), - Effect.annotateLogs("signature", signature), - Effect.annotateLogs("payload", payload), - ), - ) - : Effect.logError( - "Failed to forward callback request from dev stream", - ).pipe(Effect.annotateLogs("error", err)), + logHttpClientError( + "Failed to forward callback request from dev stream", + )(err).pipe( + Effect.annotateLogs("hook", hook), + Effect.annotateLogs("signature", signature), + Effect.annotateLogs("payload", payload), + ), }), Effect.ignoreLogged, ), diff --git a/packages/uploadthing/src/internal/logger.ts b/packages/uploadthing/src/internal/logger.ts index 97029f4a84..47e22cba46 100644 --- a/packages/uploadthing/src/internal/logger.ts +++ b/packages/uploadthing/src/internal/logger.ts @@ -1,3 +1,4 @@ +import type { HttpBody, HttpClientError } from "@effect/platform"; import * as Config from "effect/Config"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -51,3 +52,14 @@ export const withLogFormat = Effect.gen(function* () { ), Layer.unwrapEffect, ); + +export const logHttpClientError = + (message: string) => + (err: HttpClientError.HttpClientError | HttpBody.HttpBodyError) => + err._tag === "ResponseError" + ? Effect.flatMap(err.response.json, () => + Effect.logError(`${message} (${err.response.status})`).pipe( + Effect.annotateLogs("response", err.response), + ), + ) + : Effect.logError(message).pipe(Effect.annotateLogs("error", err)); diff --git a/packages/uploadthing/src/sdk/index.ts b/packages/uploadthing/src/sdk/index.ts index 245aa7f6c0..45c3f28cda 100644 --- a/packages/uploadthing/src/sdk/index.ts +++ b/packages/uploadthing/src/sdk/index.ts @@ -27,7 +27,11 @@ import { UPLOADTHING_VERSION, UTToken, } from "../internal/config"; -import { withLogFormat, withMinimalLogLevel } from "../internal/logger"; +import { + logHttpClientError, + withLogFormat, + withMinimalLogLevel, +} from "../internal/logger"; import type { ACLUpdateOptions, DeleteFilesOptions, @@ -79,20 +83,11 @@ export class UTApi { HttpClient.filterStatusOk(httpClient), Effect.tapBoth({ onSuccess: (res) => - Effect.logDebug(`UT Response`).pipe( - Effect.annotateLogs("res", res), + Effect.logDebug(`UploadThing API Response`).pipe( + Effect.annotateLogs("response", res), ), - onFailure: (err) => - err._tag === "ResponseError" - ? Effect.flatMap(err.response.json, () => - Effect.logError( - `Failed to request UploadThing API (${err.response.status})`, - ).pipe(Effect.annotateLogs("response", err.response)), - ) - : Effect.logError("Failed to request UploadThing API").pipe( - Effect.annotateLogs("error", err), - ), + onFailure: logHttpClientError("Failed to request UploadThing API"), }), HttpClientResponse.schemaBodyJsonScoped(responseSchema), ); From a8f1c1da59441fa1f0c7cfa613aa188f7d7a698a Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Tue, 8 Oct 2024 01:25:02 +0200 Subject: [PATCH 5/6] improve more --- packages/uploadthing/src/internal/handler.ts | 75 +++++++++----------- packages/uploadthing/src/internal/logger.ts | 34 +++++++-- packages/uploadthing/src/sdk/index.ts | 7 +- 3 files changed, 63 insertions(+), 53 deletions(-) diff --git a/packages/uploadthing/src/internal/handler.ts b/packages/uploadthing/src/internal/handler.ts index 9ecc8a0aad..9d0f3f088d 100644 --- a/packages/uploadthing/src/internal/handler.ts +++ b/packages/uploadthing/src/internal/handler.ts @@ -33,6 +33,7 @@ import { formatError } from "./error-formatter"; import { handleJsonLineStream } from "./jsonl"; import { logHttpClientError, + logHttpClientResponse, withLogFormat, withMinimalLogLevel, } from "./logger"; @@ -612,13 +613,6 @@ const handleUploadAction = (opts: { awaitServerData: routeOptions.awaitServerData ?? true, }), Effect.flatMap(HttpClient.filterStatusOk(httpClient)), - Effect.tapBoth({ - onSuccess: (res) => - Effect.logDebug("Registerred metadata").pipe( - Effect.annotateLogs("response", res), - ), - onFailure: logHttpClientError("Failed to register metadata"), - }), ); // Send metadata to UT server (non blocking as a daemon) @@ -627,47 +621,44 @@ const handleUploadAction = (opts: { const fiber = yield* Effect.if(isDev, { onTrue: () => metadataRequest.pipe( + Effect.tapBoth({ + onSuccess: logHttpClientResponse("Registerred metadata", { + mixin: "None", // We're reading the stream so can't call a body mixin + }), + onFailure: logHttpClientError("Failed to register metadata"), + }), HttpClientResponse.stream, - handleJsonLineStream( - MetadataFetchStreamPart, - ({ payload, signature, hook }) => - devHookRequest.pipe( - HttpClientRequest.setHeaders({ - "uploadthing-hook": hook, - "x-uploadthing-signature": signature, - }), - HttpClientRequest.setBody( - HttpBody.text(payload, "application/json"), - ), - httpClient, - - HttpClientResponse.arrayBuffer, - Effect.asVoid, - Effect.tapBoth({ - onSuccess: (res) => - Effect.logDebug( - "Successfully forwarded callback request from dev stream", - ).pipe( - Effect.annotateLogs("response", res), - Effect.annotateLogs("hook", hook), - Effect.annotateLogs("signature", signature), - Effect.annotateLogs("payload", payload), - ), - onFailure: (err) => - logHttpClientError( - "Failed to forward callback request from dev stream", - )(err).pipe( - Effect.annotateLogs("hook", hook), - Effect.annotateLogs("signature", signature), - Effect.annotateLogs("payload", payload), - ), - }), - Effect.ignoreLogged, + handleJsonLineStream(MetadataFetchStreamPart, (chunk) => + devHookRequest.pipe( + HttpClientRequest.setHeaders({ + "uploadthing-hook": chunk.hook, + "x-uploadthing-signature": chunk.signature, + }), + HttpClientRequest.setBody( + HttpBody.text(chunk.payload, "application/json"), ), + HttpClient.filterStatusOk(httpClient), + Effect.tapBoth({ + onSuccess: logHttpClientResponse( + "Successfully forwarded callback request from dev stream", + ), + onFailure: logHttpClientError( + "Failed to forward callback request from dev stream", + ), + }), + Effect.annotateLogs(chunk), + HttpClientResponse.json, + Effect.asVoid, + Effect.ignoreLogged, + ), ), ), onFalse: () => metadataRequest.pipe( + Effect.tapBoth({ + onSuccess: logHttpClientResponse("Registerred metadata"), + onFailure: logHttpClientError("Failed to register metadata"), + }), HttpClientResponse.schemaBodyJsonScoped(MetadataFetchResponse), ), }).pipe(Effect.forkDaemon); diff --git a/packages/uploadthing/src/internal/logger.ts b/packages/uploadthing/src/internal/logger.ts index 47e22cba46..0bd2ebc91a 100644 --- a/packages/uploadthing/src/internal/logger.ts +++ b/packages/uploadthing/src/internal/logger.ts @@ -1,4 +1,8 @@ -import type { HttpBody, HttpClientError } from "@effect/platform"; +import type { + HttpBody, + HttpClientError, + HttpClientResponse, +} from "@effect/platform"; import * as Config from "effect/Config"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -53,13 +57,31 @@ export const withLogFormat = Effect.gen(function* () { Layer.unwrapEffect, ); +type HttpClientResponseMixinMethod = "json" | "text" | "arrayBuffer" | "None"; + +export const logHttpClientResponse = ( + message: string, + opts?: { + /** Level to log on, default "Debug" */ + level?: LogLevel.Literal; + /** What body mixin to use to get the response body, default "json" */ + mixin?: HttpClientResponseMixinMethod; + }, +) => { + const mixin = opts?.mixin ?? "json"; + const level = LogLevel.fromLiteral(opts?.level ?? "Debug"); + + return (response: HttpClientResponse.HttpClientResponse) => + Effect.flatMap(mixin !== "None" ? response[mixin] : Effect.void, () => + Effect.logWithLevel(level, `${message} (${response.status})`).pipe( + Effect.annotateLogs("response", response), + ), + ); +}; + export const logHttpClientError = (message: string) => (err: HttpClientError.HttpClientError | HttpBody.HttpBodyError) => err._tag === "ResponseError" - ? Effect.flatMap(err.response.json, () => - Effect.logError(`${message} (${err.response.status})`).pipe( - Effect.annotateLogs("response", err.response), - ), - ) + ? logHttpClientResponse(message, { level: "Error" })(err.response) : Effect.logError(message).pipe(Effect.annotateLogs("error", err)); diff --git a/packages/uploadthing/src/sdk/index.ts b/packages/uploadthing/src/sdk/index.ts index 45c3f28cda..073db5405d 100644 --- a/packages/uploadthing/src/sdk/index.ts +++ b/packages/uploadthing/src/sdk/index.ts @@ -29,6 +29,7 @@ import { } from "../internal/config"; import { logHttpClientError, + logHttpClientResponse, withLogFormat, withMinimalLogLevel, } from "../internal/logger"; @@ -82,11 +83,7 @@ export class UTApi { }), HttpClient.filterStatusOk(httpClient), Effect.tapBoth({ - onSuccess: (res) => - Effect.logDebug(`UploadThing API Response`).pipe( - Effect.annotateLogs("response", res), - ), - + onSuccess: logHttpClientResponse("UploadThing API Response"), onFailure: logHttpClientError("Failed to request UploadThing API"), }), HttpClientResponse.schemaBodyJsonScoped(responseSchema), From 4687ba061e3cb08da9c59fdb664cd46b851b592c Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Tue, 8 Oct 2024 01:33:07 +0200 Subject: [PATCH 6/6] typo --- packages/uploadthing/src/internal/handler.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/uploadthing/src/internal/handler.ts b/packages/uploadthing/src/internal/handler.ts index 9d0f3f088d..43ef02b686 100644 --- a/packages/uploadthing/src/internal/handler.ts +++ b/packages/uploadthing/src/internal/handler.ts @@ -622,7 +622,7 @@ const handleUploadAction = (opts: { onTrue: () => metadataRequest.pipe( Effect.tapBoth({ - onSuccess: logHttpClientResponse("Registerred metadata", { + onSuccess: logHttpClientResponse("Registered metadata", { mixin: "None", // We're reading the stream so can't call a body mixin }), onFailure: logHttpClientError("Failed to register metadata"), @@ -656,7 +656,7 @@ const handleUploadAction = (opts: { onFalse: () => metadataRequest.pipe( Effect.tapBoth({ - onSuccess: logHttpClientResponse("Registerred metadata"), + onSuccess: logHttpClientResponse("Registered metadata"), onFailure: logHttpClientError("Failed to register metadata"), }), HttpClientResponse.schemaBodyJsonScoped(MetadataFetchResponse),