reverse proxy info
This commit is contained in:
@@ -1,6 +1,26 @@
|
||||
import { logDebug, logError, logWarn } from "./logger.js";
|
||||
|
||||
function formatBytes(bytes) {
|
||||
if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(bytes % (1024 * 1024) ? 1 : 0)} MB`;
|
||||
if (bytes >= 1024) return `${Math.ceil(bytes / 1024)} KB`;
|
||||
return `${bytes} B`;
|
||||
}
|
||||
|
||||
function validateUploadSize(body) {
|
||||
if (!(body instanceof FormData)) return;
|
||||
const maxBytes = Number(window.__RUSTPAD_CONFIG__?.uploadMaxSizeBytes || 0);
|
||||
if (!Number.isFinite(maxBytes) || maxBytes <= 0) return;
|
||||
for (const value of body.values()) {
|
||||
if (value instanceof File && value.size > maxBytes) {
|
||||
const error = new Error(`The selected file is ${formatBytes(value.size)}. The upload limit is ${formatBytes(maxBytes)}.`);
|
||||
error.status = 413;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function api(path, options = {}) {
|
||||
validateUploadSize(options.body);
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 12000);
|
||||
try {
|
||||
@@ -24,6 +44,9 @@ export async function api(path, options = {}) {
|
||||
} catch (error) {
|
||||
if (error.name === "AbortError") { logWarn("api.timeout", { method: options.method || "GET", path }); throw new Error("Timed out"); }
|
||||
logError("api.network_error", error, { method: options.method || "GET", path });
|
||||
if (options.body instanceof FormData && error instanceof TypeError) {
|
||||
throw new Error("Upload failed before the server returned a response. The file may exceed the server or proxy upload limit.");
|
||||
}
|
||||
throw error;
|
||||
} finally { clearTimeout(timeout); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user