@depup/uploadthing 7.7.4-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +33 -0
- package/changes.json +18 -0
- package/client/index.cjs +331 -0
- package/client/index.d.cts +36 -0
- package/client/index.d.cts.map +1 -0
- package/client/index.d.ts +36 -0
- package/client/index.d.ts.map +1 -0
- package/client/index.js +286 -0
- package/client/index.js.map +1 -0
- package/client-future/index.cjs +426 -0
- package/client-future/index.d.cts +373 -0
- package/client-future/index.d.cts.map +1 -0
- package/client-future/index.d.ts +373 -0
- package/client-future/index.d.ts.map +1 -0
- package/client-future/index.js +383 -0
- package/client-future/index.js.map +1 -0
- package/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/deprecations-DPGpmqha.cjs +13 -0
- package/dist/deprecations-pLmw6Ytd.js +8 -0
- package/dist/deprecations-pLmw6Ytd.js.map +1 -0
- package/dist/package-BQ_k22T9.cjs +11 -0
- package/dist/package-DpScpvTA.js +6 -0
- package/dist/package-DpScpvTA.js.map +1 -0
- package/dist/shared-schemas-BmG5ARoX.js +82 -0
- package/dist/shared-schemas-BmG5ARoX.js.map +1 -0
- package/dist/shared-schemas-CG9VaBtT.cjs +129 -0
- package/dist/to-web-request-BQtxSXgE.cjs +98 -0
- package/dist/to-web-request-DhP0wXG-.js +87 -0
- package/dist/to-web-request-DhP0wXG-.js.map +1 -0
- package/dist/types-Bs3w2d_3.d.ts +627 -0
- package/dist/types-Bs3w2d_3.d.ts.map +1 -0
- package/dist/types-DiVC1t2V.d.cts +625 -0
- package/dist/types-DiVC1t2V.d.cts.map +1 -0
- package/dist/upload-builder-BUa7tovh.d.cts +32 -0
- package/dist/upload-builder-BUa7tovh.d.cts.map +1 -0
- package/dist/upload-builder-BcFawEj0.d.ts +32 -0
- package/dist/upload-builder-BcFawEj0.d.ts.map +1 -0
- package/dist/upload-builder-BlFOAnsv.js +699 -0
- package/dist/upload-builder-BlFOAnsv.js.map +1 -0
- package/dist/upload-builder-D6Ken9H0.cjs +794 -0
- package/dist/ut-reporter-BHoyNnzW.cjs +120 -0
- package/dist/ut-reporter-Dlppchbx.js +103 -0
- package/dist/ut-reporter-Dlppchbx.js.map +1 -0
- package/effect-platform/index.cjs +22 -0
- package/effect-platform/index.d.cts +54 -0
- package/effect-platform/index.d.cts.map +1 -0
- package/effect-platform/index.d.ts +54 -0
- package/effect-platform/index.d.ts.map +1 -0
- package/effect-platform/index.js +19 -0
- package/effect-platform/index.js.map +1 -0
- package/express/index.cjs +30 -0
- package/express/index.d.cts +28 -0
- package/express/index.d.cts.map +1 -0
- package/express/index.d.ts +28 -0
- package/express/index.d.ts.map +1 -0
- package/express/index.js +27 -0
- package/express/index.js.map +1 -0
- package/fastify/index.cjs +27 -0
- package/fastify/index.d.cts +28 -0
- package/fastify/index.d.cts.map +1 -0
- package/fastify/index.d.ts +28 -0
- package/fastify/index.d.ts.map +1 -0
- package/fastify/index.js +24 -0
- package/fastify/index.js.map +1 -0
- package/h3/index.cjs +20 -0
- package/h3/index.d.cts +28 -0
- package/h3/index.d.cts.map +1 -0
- package/h3/index.d.ts +28 -0
- package/h3/index.d.ts.map +1 -0
- package/h3/index.js +17 -0
- package/h3/index.js.map +1 -0
- package/next/index.cjs +22 -0
- package/next/index.d.cts +30 -0
- package/next/index.d.cts.map +1 -0
- package/next/index.d.ts +30 -0
- package/next/index.d.ts.map +1 -0
- package/next/index.js +19 -0
- package/next/index.js.map +1 -0
- package/next-legacy/index.cjs +28 -0
- package/next-legacy/index.d.cts +28 -0
- package/next-legacy/index.d.cts.map +1 -0
- package/next-legacy/index.d.ts +28 -0
- package/next-legacy/index.d.ts.map +1 -0
- package/next-legacy/index.js +25 -0
- package/next-legacy/index.js.map +1 -0
- package/package.json +210 -0
- package/remix/index.cjs +22 -0
- package/remix/index.d.cts +30 -0
- package/remix/index.d.cts.map +1 -0
- package/remix/index.d.ts +30 -0
- package/remix/index.d.ts.map +1 -0
- package/remix/index.js +19 -0
- package/remix/index.js.map +1 -0
- package/server/index.cjs +414 -0
- package/server/index.d.cts +211 -0
- package/server/index.d.cts.map +1 -0
- package/server/index.d.ts +213 -0
- package/server/index.d.ts.map +1 -0
- package/server/index.js +405 -0
- package/server/index.js.map +1 -0
- package/tw/index.cjs +70 -0
- package/tw/index.d.cts +29 -0
- package/tw/index.d.cts.map +1 -0
- package/tw/index.d.ts +29 -0
- package/tw/index.d.ts.map +1 -0
- package/tw/index.js +74 -0
- package/tw/index.js.map +1 -0
- package/tw/v4.css +11 -0
- package/types/index.cjs +3 -0
- package/types/index.d.cts +2 -0
- package/types/index.d.ts +2 -0
- package/types/index.js +3 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const __uploadthing_shared = require_chunk.__toESM(require("@uploadthing/shared"));
|
|
3
|
+
const effect_Schema = require_chunk.__toESM(require("effect/Schema"));
|
|
4
|
+
|
|
5
|
+
//#region src/_internal/shared-schemas.ts
|
|
6
|
+
const ContentDispositionSchema = effect_Schema.Literal(...__uploadthing_shared.ValidContentDispositions);
|
|
7
|
+
const ACLSchema = effect_Schema.Literal(...__uploadthing_shared.ValidACLs);
|
|
8
|
+
/**
|
|
9
|
+
* Valid options for the `?actionType` query param
|
|
10
|
+
*/
|
|
11
|
+
const ActionType = effect_Schema.Literal("upload");
|
|
12
|
+
/**
|
|
13
|
+
* Valid options for the `uploadthing-hook` header
|
|
14
|
+
* for requests coming from UT server
|
|
15
|
+
*/
|
|
16
|
+
const UploadThingHook = effect_Schema.Literal("callback", "error");
|
|
17
|
+
/**
|
|
18
|
+
* =============================================================================
|
|
19
|
+
* =========================== Configuration ===================================
|
|
20
|
+
* =============================================================================
|
|
21
|
+
*/
|
|
22
|
+
const DecodeString = effect_Schema.transform(effect_Schema.Uint8ArrayFromSelf, effect_Schema.String, {
|
|
23
|
+
decode: (data) => new TextDecoder().decode(data),
|
|
24
|
+
encode: (data) => new TextEncoder().encode(data)
|
|
25
|
+
});
|
|
26
|
+
const ParsedToken = effect_Schema.Struct({
|
|
27
|
+
apiKey: effect_Schema.Redacted(effect_Schema.String.pipe(effect_Schema.startsWith("sk_"))),
|
|
28
|
+
appId: effect_Schema.String,
|
|
29
|
+
regions: effect_Schema.NonEmptyArray(effect_Schema.String),
|
|
30
|
+
ingestHost: effect_Schema.String.pipe(effect_Schema.optionalWith({ default: () => "ingest.uploadthing.com" }))
|
|
31
|
+
});
|
|
32
|
+
const UploadThingToken = effect_Schema.Uint8ArrayFromBase64.pipe(effect_Schema.compose(DecodeString), effect_Schema.compose(effect_Schema.parseJson(ParsedToken)));
|
|
33
|
+
/**
|
|
34
|
+
* =============================================================================
|
|
35
|
+
* ======================== File Type Hierarchy ===============================
|
|
36
|
+
* =============================================================================
|
|
37
|
+
*/
|
|
38
|
+
/**
|
|
39
|
+
* Properties from the web File object, this is what the client sends when initiating an upload
|
|
40
|
+
*/
|
|
41
|
+
var FileUploadData = class extends effect_Schema.Class("FileUploadData")({
|
|
42
|
+
name: effect_Schema.String,
|
|
43
|
+
size: effect_Schema.Number,
|
|
44
|
+
type: effect_Schema.String,
|
|
45
|
+
lastModified: effect_Schema.Number.pipe(effect_Schema.optional)
|
|
46
|
+
}) {};
|
|
47
|
+
/**
|
|
48
|
+
* `.middleware()` can add a customId to the incoming file data
|
|
49
|
+
*/
|
|
50
|
+
var FileUploadDataWithCustomId = class extends FileUploadData.extend("FileUploadDataWithCustomId")({ customId: effect_Schema.NullOr(effect_Schema.String) }) {};
|
|
51
|
+
/**
|
|
52
|
+
* When files are uploaded, we get back
|
|
53
|
+
* - a key
|
|
54
|
+
* - URLs for the file
|
|
55
|
+
* - the hash (md5-hex) of the uploaded file's contents
|
|
56
|
+
*/
|
|
57
|
+
var UploadedFileData = class extends FileUploadDataWithCustomId.extend("UploadedFileData")({
|
|
58
|
+
key: effect_Schema.String,
|
|
59
|
+
url: effect_Schema.String,
|
|
60
|
+
appUrl: effect_Schema.String,
|
|
61
|
+
ufsUrl: effect_Schema.String,
|
|
62
|
+
fileHash: effect_Schema.String
|
|
63
|
+
}) {};
|
|
64
|
+
var MetadataFetchStreamPart = class extends effect_Schema.Class("MetadataFetchStreamPart")({
|
|
65
|
+
payload: effect_Schema.String,
|
|
66
|
+
signature: effect_Schema.String,
|
|
67
|
+
hook: UploadThingHook
|
|
68
|
+
}) {};
|
|
69
|
+
var MetadataFetchResponse = class extends effect_Schema.Class("MetadataFetchResponse")({ ok: effect_Schema.Boolean }) {};
|
|
70
|
+
var CallbackResultResponse = class extends effect_Schema.Class("CallbackResultResponse")({ ok: effect_Schema.Boolean }) {};
|
|
71
|
+
/**
|
|
72
|
+
* =============================================================================
|
|
73
|
+
* ======================== Client Action Payloads ============================
|
|
74
|
+
* =============================================================================
|
|
75
|
+
*/
|
|
76
|
+
var UploadActionPayload = class extends effect_Schema.Class("UploadActionPayload")({
|
|
77
|
+
files: effect_Schema.Array(FileUploadData),
|
|
78
|
+
input: effect_Schema.Unknown
|
|
79
|
+
}) {};
|
|
80
|
+
|
|
81
|
+
//#endregion
|
|
82
|
+
Object.defineProperty(exports, 'ActionType', {
|
|
83
|
+
enumerable: true,
|
|
84
|
+
get: function () {
|
|
85
|
+
return ActionType;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
Object.defineProperty(exports, 'CallbackResultResponse', {
|
|
89
|
+
enumerable: true,
|
|
90
|
+
get: function () {
|
|
91
|
+
return CallbackResultResponse;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
Object.defineProperty(exports, 'MetadataFetchResponse', {
|
|
95
|
+
enumerable: true,
|
|
96
|
+
get: function () {
|
|
97
|
+
return MetadataFetchResponse;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
Object.defineProperty(exports, 'MetadataFetchStreamPart', {
|
|
101
|
+
enumerable: true,
|
|
102
|
+
get: function () {
|
|
103
|
+
return MetadataFetchStreamPart;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
Object.defineProperty(exports, 'UploadActionPayload', {
|
|
107
|
+
enumerable: true,
|
|
108
|
+
get: function () {
|
|
109
|
+
return UploadActionPayload;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
Object.defineProperty(exports, 'UploadThingHook', {
|
|
113
|
+
enumerable: true,
|
|
114
|
+
get: function () {
|
|
115
|
+
return UploadThingHook;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
Object.defineProperty(exports, 'UploadThingToken', {
|
|
119
|
+
enumerable: true,
|
|
120
|
+
get: function () {
|
|
121
|
+
return UploadThingToken;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
Object.defineProperty(exports, 'UploadedFileData', {
|
|
125
|
+
enumerable: true,
|
|
126
|
+
get: function () {
|
|
127
|
+
return UploadedFileData;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const __uploadthing_shared = require_chunk.__toESM(require("@uploadthing/shared"));
|
|
3
|
+
const effect_Effect = require_chunk.__toESM(require("effect/Effect"));
|
|
4
|
+
const effect_Config = require_chunk.__toESM(require("effect/Config"));
|
|
5
|
+
const effect_Data = require_chunk.__toESM(require("effect/Data"));
|
|
6
|
+
|
|
7
|
+
//#region src/_internal/to-web-request.ts
|
|
8
|
+
var InvalidURL = class extends effect_Data.Error {
|
|
9
|
+
_tag = "InvalidURL";
|
|
10
|
+
name = "InvalidURLError";
|
|
11
|
+
constructor(attemptedUrl, base) {
|
|
12
|
+
effect_Effect.runSync(effect_Effect.logError(`Failed to parse URL from request. '${attemptedUrl}' is not a valid URL with base '${base}'.`));
|
|
13
|
+
super({ reason: `Failed to parse URL from request. '${attemptedUrl}' is not a valid URL with base '${base}'.` });
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const parseURL = (req) => {
|
|
17
|
+
const headers = req.headers;
|
|
18
|
+
let relativeUrl = req.url ?? "/";
|
|
19
|
+
if ("baseUrl" in req && typeof req.baseUrl === "string") relativeUrl = req.baseUrl + relativeUrl;
|
|
20
|
+
const proto = headers?.["x-forwarded-proto"] ?? "http";
|
|
21
|
+
const host = headers?.["x-forwarded-host"] ?? headers?.host;
|
|
22
|
+
const baseUrl = effect_Config.string("url").pipe(effect_Config.withDefault(`${proto.toString()}://${host?.toString()}`));
|
|
23
|
+
return effect_Effect.flatMap(baseUrl, (baseUrl$1) => effect_Effect.try({
|
|
24
|
+
try: () => new URL(relativeUrl, baseUrl$1),
|
|
25
|
+
catch: () => new InvalidURL(relativeUrl, baseUrl$1)
|
|
26
|
+
})).pipe(effect_Effect.catchTag("ConfigError", () => effect_Effect.fail(new InvalidURL(relativeUrl))));
|
|
27
|
+
};
|
|
28
|
+
const isBodyAllowed = (method) => [
|
|
29
|
+
"POST",
|
|
30
|
+
"PUT",
|
|
31
|
+
"PATCH"
|
|
32
|
+
].includes(method);
|
|
33
|
+
const getPostBody = (opts) => effect_Effect.async((resume) => {
|
|
34
|
+
const { req } = opts;
|
|
35
|
+
if (!req.method || !isBodyAllowed(req.method)) return resume(effect_Effect.succeed(void 0));
|
|
36
|
+
const contentType = req.headers?.["content-type"];
|
|
37
|
+
if ("body" in req) {
|
|
38
|
+
if (contentType !== "application/json") {
|
|
39
|
+
effect_Effect.runSync(effect_Effect.logError("Expected JSON content type, got:", contentType));
|
|
40
|
+
return resume(new __uploadthing_shared.UploadThingError({
|
|
41
|
+
code: "BAD_REQUEST",
|
|
42
|
+
message: "INVALID_CONTENT_TYPE"
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
if (typeof req.body !== "object") {
|
|
46
|
+
effect_Effect.runSync(effect_Effect.logError("Expected body to be of type 'object', got:", typeof req.body));
|
|
47
|
+
return resume(new __uploadthing_shared.UploadThingError({
|
|
48
|
+
code: "BAD_REQUEST",
|
|
49
|
+
message: "INVALID_BODY"
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
effect_Effect.runSync(effect_Effect.logDebug("Body parsed successfully.", req.body));
|
|
53
|
+
return resume(effect_Effect.succeed(req.body));
|
|
54
|
+
}
|
|
55
|
+
let body = "";
|
|
56
|
+
req.on("data", (data) => body += data);
|
|
57
|
+
req.on("end", () => {
|
|
58
|
+
const parsedBody = effect_Effect.try({
|
|
59
|
+
try: () => JSON.parse(body),
|
|
60
|
+
catch: (err) => new __uploadthing_shared.UploadThingError({
|
|
61
|
+
code: "BAD_REQUEST",
|
|
62
|
+
message: "INVALID_JSON",
|
|
63
|
+
cause: err
|
|
64
|
+
})
|
|
65
|
+
});
|
|
66
|
+
return resume(parsedBody);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
const toWebRequest = (req, body) => {
|
|
70
|
+
body ??= req.body;
|
|
71
|
+
const bodyStr = typeof body === "string" ? body : JSON.stringify(body);
|
|
72
|
+
const method = req.method ?? "GET";
|
|
73
|
+
const allowsBody = isBodyAllowed(method);
|
|
74
|
+
const headers = new Headers();
|
|
75
|
+
for (const [key, value] of Object.entries(req.headers ?? [])) {
|
|
76
|
+
if (typeof value === "string") headers.set(key, value);
|
|
77
|
+
if (Array.isArray(value)) headers.set(key, value.join(","));
|
|
78
|
+
}
|
|
79
|
+
return parseURL(req).pipe(effect_Effect.catchTag("InvalidURL", (e) => effect_Effect.die(e)), effect_Effect.andThen((url) => new Request(url, {
|
|
80
|
+
method,
|
|
81
|
+
headers,
|
|
82
|
+
...allowsBody ? { body: bodyStr } : {}
|
|
83
|
+
})));
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
//#endregion
|
|
87
|
+
Object.defineProperty(exports, 'getPostBody', {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
get: function () {
|
|
90
|
+
return getPostBody;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
Object.defineProperty(exports, 'toWebRequest', {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
get: function () {
|
|
96
|
+
return toWebRequest;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { UploadThingError } from "@uploadthing/shared";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import * as Config from "effect/Config";
|
|
4
|
+
import * as Data from "effect/Data";
|
|
5
|
+
|
|
6
|
+
//#region src/_internal/to-web-request.ts
|
|
7
|
+
var InvalidURL = class extends Data.Error {
|
|
8
|
+
_tag = "InvalidURL";
|
|
9
|
+
name = "InvalidURLError";
|
|
10
|
+
constructor(attemptedUrl, base) {
|
|
11
|
+
Effect.runSync(Effect.logError(`Failed to parse URL from request. '${attemptedUrl}' is not a valid URL with base '${base}'.`));
|
|
12
|
+
super({ reason: `Failed to parse URL from request. '${attemptedUrl}' is not a valid URL with base '${base}'.` });
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const parseURL = (req) => {
|
|
16
|
+
const headers = req.headers;
|
|
17
|
+
let relativeUrl = req.url ?? "/";
|
|
18
|
+
if ("baseUrl" in req && typeof req.baseUrl === "string") relativeUrl = req.baseUrl + relativeUrl;
|
|
19
|
+
const proto = headers?.["x-forwarded-proto"] ?? "http";
|
|
20
|
+
const host = headers?.["x-forwarded-host"] ?? headers?.host;
|
|
21
|
+
const baseUrl = Config.string("url").pipe(Config.withDefault(`${proto.toString()}://${host?.toString()}`));
|
|
22
|
+
return Effect.flatMap(baseUrl, (baseUrl$1) => Effect.try({
|
|
23
|
+
try: () => new URL(relativeUrl, baseUrl$1),
|
|
24
|
+
catch: () => new InvalidURL(relativeUrl, baseUrl$1)
|
|
25
|
+
})).pipe(Effect.catchTag("ConfigError", () => Effect.fail(new InvalidURL(relativeUrl))));
|
|
26
|
+
};
|
|
27
|
+
const isBodyAllowed = (method) => [
|
|
28
|
+
"POST",
|
|
29
|
+
"PUT",
|
|
30
|
+
"PATCH"
|
|
31
|
+
].includes(method);
|
|
32
|
+
const getPostBody = (opts) => Effect.async((resume) => {
|
|
33
|
+
const { req } = opts;
|
|
34
|
+
if (!req.method || !isBodyAllowed(req.method)) return resume(Effect.succeed(void 0));
|
|
35
|
+
const contentType = req.headers?.["content-type"];
|
|
36
|
+
if ("body" in req) {
|
|
37
|
+
if (contentType !== "application/json") {
|
|
38
|
+
Effect.runSync(Effect.logError("Expected JSON content type, got:", contentType));
|
|
39
|
+
return resume(new UploadThingError({
|
|
40
|
+
code: "BAD_REQUEST",
|
|
41
|
+
message: "INVALID_CONTENT_TYPE"
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
if (typeof req.body !== "object") {
|
|
45
|
+
Effect.runSync(Effect.logError("Expected body to be of type 'object', got:", typeof req.body));
|
|
46
|
+
return resume(new UploadThingError({
|
|
47
|
+
code: "BAD_REQUEST",
|
|
48
|
+
message: "INVALID_BODY"
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
Effect.runSync(Effect.logDebug("Body parsed successfully.", req.body));
|
|
52
|
+
return resume(Effect.succeed(req.body));
|
|
53
|
+
}
|
|
54
|
+
let body = "";
|
|
55
|
+
req.on("data", (data) => body += data);
|
|
56
|
+
req.on("end", () => {
|
|
57
|
+
const parsedBody = Effect.try({
|
|
58
|
+
try: () => JSON.parse(body),
|
|
59
|
+
catch: (err) => new UploadThingError({
|
|
60
|
+
code: "BAD_REQUEST",
|
|
61
|
+
message: "INVALID_JSON",
|
|
62
|
+
cause: err
|
|
63
|
+
})
|
|
64
|
+
});
|
|
65
|
+
return resume(parsedBody);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
const toWebRequest = (req, body) => {
|
|
69
|
+
body ??= req.body;
|
|
70
|
+
const bodyStr = typeof body === "string" ? body : JSON.stringify(body);
|
|
71
|
+
const method = req.method ?? "GET";
|
|
72
|
+
const allowsBody = isBodyAllowed(method);
|
|
73
|
+
const headers = new Headers();
|
|
74
|
+
for (const [key, value] of Object.entries(req.headers ?? [])) {
|
|
75
|
+
if (typeof value === "string") headers.set(key, value);
|
|
76
|
+
if (Array.isArray(value)) headers.set(key, value.join(","));
|
|
77
|
+
}
|
|
78
|
+
return parseURL(req).pipe(Effect.catchTag("InvalidURL", (e) => Effect.die(e)), Effect.andThen((url) => new Request(url, {
|
|
79
|
+
method,
|
|
80
|
+
headers,
|
|
81
|
+
...allowsBody ? { body: bodyStr } : {}
|
|
82
|
+
})));
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
export { getPostBody, toWebRequest };
|
|
87
|
+
//# sourceMappingURL=to-web-request-DhP0wXG-.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to-web-request-DhP0wXG-.js","names":["attemptedUrl: string","base?: string","req: IncomingMessageLike","baseUrl","method: string","opts: {\n req: IncomingMessageLike & {\n on: (event: string, listener: (data: any) => void) => void;\n };\n}","body?: any"],"sources":["../src/_internal/to-web-request.ts"],"sourcesContent":["import * as Config from \"effect/Config\";\nimport * as Data from \"effect/Data\";\nimport * as Effect from \"effect/Effect\";\n\nimport { UploadThingError } from \"@uploadthing/shared\";\n\ntype IncomingMessageLike = {\n method?: string | undefined;\n url?: string | undefined;\n headers?: Record<string, string | string[] | undefined>;\n body?: any;\n};\n\nclass InvalidURL extends Data.Error<{\n reason: string;\n}> {\n readonly _tag = \"InvalidURL\";\n readonly name = \"InvalidURLError\";\n constructor(attemptedUrl: string, base?: string) {\n Effect.runSync(\n Effect.logError(\n `Failed to parse URL from request. '${attemptedUrl}' is not a valid URL with base '${base}'.`,\n ),\n );\n super({\n reason: `Failed to parse URL from request. '${attemptedUrl}' is not a valid URL with base '${base}'.`,\n });\n }\n}\n\nconst parseURL = (req: IncomingMessageLike): Effect.Effect<URL, InvalidURL> => {\n const headers = req.headers;\n let relativeUrl = req.url ?? \"/\";\n if (\"baseUrl\" in req && typeof req.baseUrl === \"string\") {\n relativeUrl = req.baseUrl + relativeUrl;\n }\n\n const proto = headers?.[\"x-forwarded-proto\"] ?? \"http\";\n const host = headers?.[\"x-forwarded-host\"] ?? headers?.host;\n\n const baseUrl = Config.string(\"url\").pipe(\n Config.withDefault(`${proto.toString()}://${host?.toString()}`),\n );\n\n return Effect.flatMap(baseUrl, (baseUrl) =>\n Effect.try({\n try: () => new URL(relativeUrl, baseUrl),\n catch: () => new InvalidURL(relativeUrl, baseUrl),\n }),\n ).pipe(\n Effect.catchTag(\"ConfigError\", () =>\n Effect.fail(new InvalidURL(relativeUrl)),\n ),\n );\n};\n\nconst isBodyAllowed = (method: string) =>\n [\"POST\", \"PUT\", \"PATCH\"].includes(method);\n\nexport const getPostBody = <TBody = unknown>(opts: {\n req: IncomingMessageLike & {\n on: (event: string, listener: (data: any) => void) => void;\n };\n}) =>\n Effect.async<TBody | undefined, UploadThingError>((resume) => {\n const { req } = opts;\n if (!req.method || !isBodyAllowed(req.method)) {\n return resume(Effect.succeed(undefined));\n }\n const contentType = req.headers?.[\"content-type\"];\n\n if (\"body\" in req) {\n if (contentType !== \"application/json\") {\n Effect.runSync(\n Effect.logError(\"Expected JSON content type, got:\", contentType),\n );\n return resume(\n new UploadThingError({\n code: \"BAD_REQUEST\",\n message: \"INVALID_CONTENT_TYPE\",\n }),\n );\n }\n\n if (typeof req.body !== \"object\") {\n Effect.runSync(\n Effect.logError(\n \"Expected body to be of type 'object', got:\",\n typeof req.body,\n ),\n );\n return resume(\n new UploadThingError({\n code: \"BAD_REQUEST\",\n message: \"INVALID_BODY\",\n }),\n );\n }\n\n Effect.runSync(Effect.logDebug(\"Body parsed successfully.\", req.body));\n return resume(Effect.succeed(req.body as TBody));\n }\n\n let body = \"\";\n req.on(\"data\", (data) => (body += data));\n req.on(\"end\", () => {\n const parsedBody = Effect.try({\n try: () => JSON.parse(body) as TBody,\n catch: (err) =>\n new UploadThingError({\n code: \"BAD_REQUEST\",\n message: \"INVALID_JSON\",\n cause: err,\n }),\n });\n return resume(parsedBody);\n });\n });\n\nexport const toWebRequest = (\n req: IncomingMessageLike,\n body?: any,\n): Effect.Effect<Request, never> => {\n body ??= req.body;\n const bodyStr = typeof body === \"string\" ? body : JSON.stringify(body);\n const method = req.method ?? \"GET\";\n const allowsBody = isBodyAllowed(method);\n\n const headers = new Headers();\n for (const [key, value] of Object.entries(req.headers ?? [])) {\n if (typeof value === \"string\") headers.set(key, value);\n if (Array.isArray(value)) headers.set(key, value.join(\",\"));\n }\n\n return parseURL(req).pipe(\n Effect.catchTag(\"InvalidURL\", (e) => Effect.die(e)),\n Effect.andThen(\n (url) =>\n new Request(url, {\n method,\n headers,\n ...(allowsBody ? { body: bodyStr } : {}),\n }),\n ),\n );\n};\n"],"mappings":"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"}
|