-
Notifications
You must be signed in to change notification settings - Fork 444
Expand file tree
/
Copy pathhandler.ts
More file actions
739 lines (684 loc) · 24.2 KB
/
Copy pathhandler.ts
File metadata and controls
739 lines (684 loc) · 24.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
import {
HttpApp,
HttpBody,
HttpClient,
HttpClientRequest,
HttpClientResponse,
HttpRouter,
HttpServerRequest,
HttpServerResponse,
} from "@effect/platform";
import type { ResponseError } from "@effect/platform/HttpClientError";
import * as Config from "effect/Config";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Match from "effect/Match";
import * as Redacted from "effect/Redacted";
import * as Schema from "effect/Schema";
import {
fillInputRouteConfig,
generateKey,
generateSignedURL,
getStatusCodeFromError,
matchFileType,
objectKeys,
UploadThingError,
verifySignature,
} from "@uploadthing/shared";
import * as pkgJson from "../../package.json";
import type { FileRouter, RouteHandlerOptions } from "../types";
import { IngestUrl, IsDevelopment, UTToken } from "./config";
import { logDeprecationWarning } from "./deprecations";
import { formatError } from "./error-formatter";
import { handleJsonLineStream } from "./jsonl";
import { logHttpClientError, logHttpClientResponse } from "./logger";
import { getParseFn } from "./parser";
import { assertFilesMeetConfig, extractRouterConfig } from "./route-config";
import { makeRuntime } from "./runtime";
import {
ActionType,
CallbackResultResponse,
MetadataFetchResponse,
MetadataFetchStreamPart,
UploadActionPayload,
UploadedFileData,
UploadThingHook,
} from "./shared-schemas";
import { UTFiles, UTRegion } from "./types";
import type { AnyFileRoute, UTEvents } from "./types";
export class AdapterArguments extends Context.Tag(
"uploadthing/AdapterArguments",
)<AdapterArguments, Record<string, unknown>>() {}
/**
* Create a request handler adapter for any framework or server library.
* Refer to the existing adapters for examples on how to use this function.
* @public
*
* @param makeAdapterArgs - Function that takes the args from your framework and returns an Effect that resolves to the adapter args.
* These args are passed to the `.middleware`, `.onUploadComplete`, and `.onUploadError` hooks.
* @param toRequest - Function that takes the args from your framework and returns an Effect that resolves to a web Request object.
* @param opts - The router config and other options that are normally passed to `createRequestHandler` of official adapters
* @param beAdapter - [Optional] The adapter name of the adapter, used for telemetry purposes
* @returns A function that takes the args from your framework and returns a promise that resolves to a Response object.
*/
export const makeAdapterHandler = <
Args extends any[],
AdapterArgs extends Record<string, unknown>,
>(
makeAdapterArgs: (...args: Args) => Effect.Effect<AdapterArgs>,
toRequest: (...args: Args) => Effect.Effect<Request>,
opts: RouteHandlerOptions<FileRouter>,
beAdapter?: string,
): ((...args: Args) => Promise<Response>) => {
const managed = makeRuntime(
opts.config?.fetch as typeof globalThis.fetch,
opts.config,
);
const handle = Effect.promise(() =>
managed.runtime().then(HttpApp.toWebHandlerRuntime),
);
const app = (...args: Args) =>
Effect.map(
Effect.promise(() =>
managed.runPromise(createRequestHandler(opts, beAdapter ?? "custom")),
),
Effect.provideServiceEffect(AdapterArguments, makeAdapterArgs(...args)),
);
return async (...args: Args) => {
const result = await handle.pipe(
Effect.ap(app(...args)),
Effect.ap(toRequest(...args)),
Effect.withLogSpan("requestHandler"),
managed.runPromise,
);
return result;
};
};
export const createRequestHandler = <TRouter extends FileRouter>(
opts: RouteHandlerOptions<TRouter>,
beAdapter: string,
) =>
Effect.gen(function* () {
const isDevelopment = yield* IsDevelopment;
const routerConfig = yield* extractRouterConfig(opts.router);
const handleDaemon = (() => {
if (opts.config?.handleDaemonPromise) {
return opts.config.handleDaemonPromise;
}
return isDevelopment ? "void" : "await";
})();
if (isDevelopment && handleDaemon === "await") {
return yield* new UploadThingError({
code: "INVALID_SERVER_CONFIG",
message: 'handleDaemonPromise: "await" is forbidden in development.',
});
}
const GET = Effect.gen(function* () {
return yield* HttpServerResponse.json(routerConfig);
});
const POST = Effect.gen(function* () {
const {
"uploadthing-hook": uploadthingHook,
"x-uploadthing-package": fePackage,
"x-uploadthing-version": clientVersion,
} = yield* HttpServerRequest.schemaHeaders(
Schema.Struct({
"uploadthing-hook": UploadThingHook.pipe(Schema.optional),
"x-uploadthing-package": Schema.String.pipe(
Schema.optionalWith({ default: () => "unknown" }),
),
"x-uploadthing-version": Schema.String.pipe(
Schema.optionalWith({ default: () => pkgJson.version }),
),
}),
);
if (clientVersion !== pkgJson.version) {
const serverVersion = pkgJson.version;
yield* Effect.logWarning(
"Client version mismatch. Things may not work as expected, please sync your versions to ensure compatibility.",
).pipe(Effect.annotateLogs({ clientVersion, serverVersion }));
}
const { slug, actionType } = yield* HttpRouter.schemaParams(
Schema.Struct({
actionType: ActionType.pipe(Schema.optional),
slug: Schema.String,
}),
);
const uploadable = opts.router[slug];
if (!uploadable) {
const msg = `No file route found for slug ${slug}`;
yield* Effect.logError(msg);
return yield* new UploadThingError({
code: "NOT_FOUND",
message: msg,
});
}
const { body, fiber } = yield* Match.value({
actionType,
uploadthingHook,
}).pipe(
Match.when({ actionType: "upload", uploadthingHook: undefined }, () =>
handleUploadAction({
uploadable,
fePackage,
beAdapter,
slug,
}),
),
Match.when({ actionType: undefined, uploadthingHook: "callback" }, () =>
handleCallbackRequest({ uploadable, fePackage, beAdapter }),
),
Match.when({ actionType: undefined, uploadthingHook: "error" }, () =>
handleErrorRequest({ uploadable }),
),
Match.orElse(() => Effect.succeed({ body: null, fiber: null })),
);
if (fiber) {
yield* Effect.logDebug("Running fiber as daemon").pipe(
Effect.annotateLogs("handleDaemon", handleDaemon),
);
if (handleDaemon === "void") {
// noop
} else if (handleDaemon === "await") {
yield* fiber.await;
} else if (typeof handleDaemon === "function") {
handleDaemon(Effect.runPromise(fiber.await));
}
}
yield* Effect.logDebug("Sending response").pipe(
Effect.annotateLogs("body", body),
);
return yield* HttpServerResponse.json(body);
}).pipe(
Effect.catchTags({
ParseError: (e) =>
HttpServerResponse.json(
formatError(
new UploadThingError({
code: "BAD_REQUEST",
message: "Invalid input",
cause: e.message,
}),
opts.router,
),
{ status: 400 },
),
UploadThingError: (e) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
HttpServerResponse.json(formatError(e, opts.router), {
status: getStatusCodeFromError(e),
}),
}),
);
const appendResponseHeaders = Effect.map(
HttpServerResponse.setHeader("x-uploadthing-version", pkgJson.version),
);
return HttpRouter.empty.pipe(
HttpRouter.get("*", GET),
HttpRouter.post("*", POST),
HttpRouter.use(appendResponseHeaders),
);
}).pipe(Effect.withLogSpan("createRequestHandler"));
const handleErrorRequest = (opts: { uploadable: AnyFileRoute }) =>
Effect.gen(function* () {
const { uploadable } = opts;
const request = yield* HttpServerRequest.HttpServerRequest;
const { apiKey } = yield* UTToken;
const verified = yield* verifySignature(
yield* request.text,
request.headers["x-uploadthing-signature"] ?? null,
apiKey,
);
yield* Effect.logDebug(`Signature verified: ${verified}`);
if (!verified) {
yield* Effect.logError("Invalid signature");
return yield* new UploadThingError({
code: "BAD_REQUEST",
message: "Invalid signature",
});
}
const requestInput = yield* HttpServerRequest.schemaBodyJson(
Schema.Struct({
fileKey: Schema.String,
error: Schema.String,
}),
);
yield* Effect.logDebug("Handling error callback request with input:").pipe(
Effect.annotateLogs("json", requestInput),
);
const adapterArgs = yield* AdapterArguments;
const fiber = yield* Effect.tryPromise({
try: async () =>
uploadable.onUploadError({
...adapterArgs,
error: new UploadThingError({
code: "UPLOAD_FAILED",
message: `Upload failed for ${requestInput.fileKey}: ${requestInput.error}`,
}),
fileKey: requestInput.fileKey,
}),
catch: (error) =>
new UploadThingError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to run onUploadError",
cause: error,
}),
})
.pipe(
Effect.tapError((error) =>
Effect.logError(
"Failed to run onUploadError. You probably shouldn't be throwing errors here.",
).pipe(Effect.annotateLogs("error", error)),
),
)
.pipe(Effect.ignoreLogged, Effect.forkDaemon);
return {
body: null,
fiber,
};
}).pipe(Effect.withLogSpan("handleErrorRequest"));
const handleCallbackRequest = (opts: {
uploadable: AnyFileRoute;
fePackage: string;
beAdapter: string;
}) =>
Effect.gen(function* () {
const { uploadable, fePackage, beAdapter } = opts;
const request = yield* HttpServerRequest.HttpServerRequest;
const { apiKey } = yield* UTToken;
const verified = yield* verifySignature(
yield* request.text,
request.headers["x-uploadthing-signature"] ?? null,
apiKey,
);
yield* Effect.logDebug(`Signature verified: ${verified}`);
if (!verified) {
yield* Effect.logError("Invalid signature");
return yield* new UploadThingError({
code: "BAD_REQUEST",
message: "Invalid signature",
});
}
const requestInput = yield* HttpServerRequest.schemaBodyJson(
Schema.Struct({
status: Schema.String,
file: UploadedFileData,
origin: Schema.String,
metadata: Schema.Record({ key: Schema.String, value: Schema.Unknown }),
}),
);
yield* Effect.logDebug("Handling callback request with input:").pipe(
Effect.annotateLogs("json", requestInput),
);
/**
* Run `.onUploadComplete` as a daemon to prevent the
* request from UT to potentially timeout.
*/
const fiber = yield* Effect.gen(function* () {
const adapterArgs = yield* AdapterArguments;
const serverData = yield* Effect.tryPromise({
try: async () =>
uploadable.onUploadComplete({
...adapterArgs,
file: {
...requestInput.file,
get url() {
logDeprecationWarning(
"`file.url` is deprecated and will be removed in uploadthing v9. Use `file.ufsUrl` instead.",
);
return requestInput.file.url;
},
get appUrl() {
logDeprecationWarning(
"`file.appUrl` is deprecated and will be removed in uploadthing v9. Use `file.ufsUrl` instead.",
);
return requestInput.file.appUrl;
},
},
metadata: requestInput.metadata,
}) as Promise<unknown>,
catch: (error) =>
new UploadThingError({
code: "INTERNAL_SERVER_ERROR",
message:
"Failed to run onUploadComplete. You probably shouldn't be throwing errors here.",
cause: error,
}),
});
const payload = {
fileKey: requestInput.file.key,
callbackData: serverData ?? null,
};
yield* Effect.logDebug(
"'onUploadComplete' callback finished. Sending response to UploadThing:",
).pipe(Effect.annotateLogs("callbackData", payload));
const httpClient = (yield* HttpClient.HttpClient).pipe(
HttpClient.filterStatusOk,
);
yield* HttpClientRequest.post(`/callback-result`).pipe(
HttpClientRequest.prependUrl(requestInput.origin),
HttpClientRequest.setHeaders({
"x-uploadthing-api-key": Redacted.value(apiKey),
"x-uploadthing-version": pkgJson.version,
"x-uploadthing-be-adapter": beAdapter,
"x-uploadthing-fe-package": fePackage,
}),
HttpClientRequest.bodyJson(payload),
Effect.flatMap(httpClient.execute),
Effect.tapError(
logHttpClientError("Failed to register callback result"),
),
Effect.flatMap(
HttpClientResponse.schemaBodyJson(CallbackResultResponse),
),
Effect.tap(Effect.log("Sent callback result to UploadThing")),
Effect.scoped,
);
}).pipe(Effect.ignoreLogged, Effect.forkDaemon);
return { body: null, fiber };
}).pipe(Effect.withLogSpan("handleCallbackRequest"));
const runRouteMiddleware = (opts: {
json: typeof UploadActionPayload.Type;
uploadable: AnyFileRoute;
}) =>
Effect.gen(function* () {
const {
json: { files, input },
uploadable,
} = opts;
yield* Effect.logDebug("Running middleware");
const adapterArgs = yield* AdapterArguments;
const metadata = yield* Effect.tryPromise({
try: async () =>
uploadable.middleware({
...adapterArgs,
input,
files,
}),
catch: (error) =>
error instanceof UploadThingError
? error
: new UploadThingError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to run middleware",
cause: error,
}),
});
if (metadata[UTFiles] && metadata[UTFiles].length !== files.length) {
const msg = `Expected files override to have the same length as original files, got ${metadata[UTFiles].length} but expected ${files.length}`;
yield* Effect.logError(msg);
return yield* new UploadThingError({
code: "BAD_REQUEST",
message: "Files override must have the same length as files",
cause: msg,
});
}
// Attach customIds from middleware to the files
const filesWithCustomIds = yield* Effect.forEach(files, (file, idx) =>
Effect.gen(function* () {
const theirs = metadata[UTFiles]?.[idx];
if (theirs && theirs.size !== file.size) {
yield* Effect.logWarning(
"File size mismatch. Reverting to original size",
);
}
return {
name: theirs?.name ?? file.name,
size: file.size,
type: file.type,
customId: theirs?.customId,
lastModified: theirs?.lastModified ?? Date.now(),
};
}),
);
return {
metadata,
filesWithCustomIds,
preferredRegion: metadata[UTRegion],
};
}).pipe(Effect.withLogSpan("runRouteMiddleware"));
const handleUploadAction = (opts: {
uploadable: AnyFileRoute;
fePackage: string;
beAdapter: string;
slug: string;
}) =>
Effect.gen(function* () {
const httpClient = (yield* HttpClient.HttpClient).pipe(
HttpClient.filterStatusOk,
);
const { uploadable, fePackage, beAdapter, slug } = opts;
const json = yield* HttpServerRequest.schemaBodyJson(UploadActionPayload);
yield* Effect.logDebug("Handling upload request").pipe(
Effect.annotateLogs("json", json),
);
// validate the input
yield* Effect.logDebug("Parsing user input");
const parsedInput = yield* Effect.tryPromise({
try: () => getParseFn(uploadable.inputParser)(json.input),
catch: (error) =>
new UploadThingError({
code: "BAD_REQUEST",
message: "Invalid input",
cause: error,
}),
});
yield* Effect.logDebug("Input parsed successfully").pipe(
Effect.annotateLogs("input", parsedInput),
);
const { metadata, filesWithCustomIds, preferredRegion } =
yield* runRouteMiddleware({
json: { input: parsedInput, files: json.files },
uploadable,
});
yield* Effect.logDebug("Parsing route config").pipe(
Effect.annotateLogs("routerConfig", uploadable.routerConfig),
);
const parsedConfig = yield* fillInputRouteConfig(
uploadable.routerConfig,
).pipe(
Effect.catchTag(
"InvalidRouteConfig",
(err) =>
new UploadThingError({
code: "BAD_REQUEST",
message: "Invalid route config",
cause: err,
}),
),
);
yield* Effect.logDebug("Route config parsed successfully").pipe(
Effect.annotateLogs("routeConfig", parsedConfig),
);
yield* Effect.logDebug(
"Validating files meet the config requirements",
).pipe(Effect.annotateLogs("files", json.files));
yield* assertFilesMeetConfig(json.files, parsedConfig).pipe(
Effect.mapError(
(e) =>
new UploadThingError({
code: "BAD_REQUEST",
message: `Invalid config: ${e._tag}`,
cause: "reason" in e ? e.reason : e.message,
}),
),
);
yield* Effect.logDebug("Files validated.");
const fileUploadRequests = yield* Effect.forEach(
filesWithCustomIds,
(file) =>
Effect.map(matchFileType(file, objectKeys(parsedConfig)), (type) => ({
name: file.name,
size: file.size,
type: file.type || type,
lastModified: file.lastModified,
customId: file.customId,
contentDisposition:
parsedConfig[type]?.contentDisposition ?? "inline",
acl: parsedConfig[type]?.acl,
})),
).pipe(
Effect.catchTags({
/** Shouldn't happen since config is validated above so just dying is fine I think */
InvalidFileType: (e) => Effect.die(e),
UnknownFileType: (e) => Effect.die(e),
}),
);
const routeOptions = uploadable.routeOptions;
const { apiKey, appId } = yield* UTToken;
const ingestUrl = yield* IngestUrl(preferredRegion);
const isDev = yield* IsDevelopment;
yield* Effect.logDebug("Generating presigned URLs").pipe(
Effect.annotateLogs("fileUploadRequests", fileUploadRequests),
Effect.annotateLogs("ingestUrl", ingestUrl),
);
const presignedUrls = yield* Effect.forEach(
fileUploadRequests,
(file) =>
Effect.gen(function* () {
const key = yield* generateKey(
file,
appId,
routeOptions.getFileHashParts,
);
const url = yield* generateSignedURL(`${ingestUrl}/${key}`, apiKey, {
ttlInSeconds: routeOptions.presignedURLTTL,
data: {
"x-ut-identifier": appId,
"x-ut-file-name": file.name,
"x-ut-file-size": file.size,
"x-ut-file-type": file.type,
"x-ut-slug": slug,
"x-ut-custom-id": file.customId,
"x-ut-content-disposition": file.contentDisposition,
"x-ut-acl": file.acl,
},
});
return { url, key };
}),
{ concurrency: "unbounded" },
);
const serverReq = yield* HttpServerRequest.HttpServerRequest;
const requestUrl = yield* HttpServerRequest.toURL(serverReq);
const devHookRequest = yield* Config.string("callbackUrl").pipe(
Config.withDefault(requestUrl.origin + requestUrl.pathname),
Effect.map((url) =>
HttpClientRequest.post(url).pipe(
HttpClientRequest.appendUrlParam("slug", slug),
),
),
);
const metadataRequest = HttpClientRequest.post("/route-metadata").pipe(
HttpClientRequest.prependUrl(ingestUrl),
HttpClientRequest.setHeaders({
"x-uploadthing-api-key": Redacted.value(apiKey),
"x-uploadthing-version": pkgJson.version,
"x-uploadthing-be-adapter": beAdapter,
"x-uploadthing-fe-package": fePackage,
}),
HttpClientRequest.bodyJson({
fileKeys: presignedUrls.map(({ key }) => key),
metadata: metadata,
isDev,
callbackUrl: devHookRequest.url,
callbackSlug: slug,
awaitServerData: routeOptions.awaitServerData ?? true,
}),
Effect.flatMap(httpClient.execute),
);
const handleDevStreamError = Effect.fn("handleDevStreamError")(function* (
err: ResponseError,
chunk: string,
) {
const schema = Schema.parseJson(
Schema.Struct({ file: UploadedFileData }),
);
const parsedChunk = yield* Schema.decodeUnknown(schema)(chunk);
const key = parsedChunk.file.key;
yield* Effect.logError(
"Failed to forward callback request from dev stream",
).pipe(Effect.annotateLogs({ fileKey: key, error: err.message }));
const httpResponse = yield* HttpClientRequest.post(
"/callback-result",
).pipe(
HttpClientRequest.prependUrl(ingestUrl),
HttpClientRequest.setHeaders({
"x-uploadthing-api-key": Redacted.value(apiKey),
"x-uploadthing-version": pkgJson.version,
"x-uploadthing-be-adapter": beAdapter,
"x-uploadthing-fe-package": fePackage,
}),
HttpClientRequest.bodyJson({
fileKey: key,
error: `Failed to forward callback request from dev stream: ${err.message}`,
}),
Effect.flatMap(httpClient.execute),
);
yield* logHttpClientResponse("Reported callback error to UploadThing")(
httpResponse,
);
});
// Send metadata to UT server (non blocking as a daemon)
// In dev, keep the stream open and simulate the callback requests as
// files complete uploading
const fiber = yield* Effect.if(isDev, {
onTrue: () =>
metadataRequest.pipe(
Effect.tapBoth({
onSuccess: logHttpClientResponse("Registered 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, (chunk) =>
devHookRequest.pipe(
HttpClientRequest.setHeaders({
"uploadthing-hook": chunk.hook,
"x-uploadthing-signature": chunk.signature,
}),
HttpClientRequest.setBody(
HttpBody.text(chunk.payload, "application/json"),
),
httpClient.execute,
Effect.tap(
logHttpClientResponse(
"Successfully forwarded callback request from dev stream",
),
),
Effect.catchTag("ResponseError", (err) =>
handleDevStreamError(err, chunk.payload),
),
Effect.annotateLogs(chunk),
Effect.asVoid,
Effect.ignoreLogged,
Effect.scoped,
),
),
),
onFalse: () =>
metadataRequest.pipe(
Effect.tapBoth({
onSuccess: logHttpClientResponse("Registered metadata"),
onFailure: logHttpClientError("Failed to register metadata"),
}),
Effect.flatMap(
HttpClientResponse.schemaBodyJson(MetadataFetchResponse),
),
Effect.scoped,
),
}).pipe(Effect.forkDaemon);
const presigneds = presignedUrls.map((p, i) => ({
url: p.url,
key: p.key,
name: fileUploadRequests[i]!.name,
customId: fileUploadRequests[i]!.customId ?? null,
}));
yield* Effect.logInfo("Sending presigned URLs to client").pipe(
Effect.annotateLogs("presignedUrls", presigneds),
);
return {
body: presigneds satisfies UTEvents["upload"]["out"],
fiber,
};
}).pipe(Effect.withLogSpan("handleUploadAction"));