deepspace 0.11.0 → 0.12.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/dist/index.js CHANGED
@@ -3519,15 +3519,59 @@ function toConnectionStatus(queryStatus) {
3519
3519
 
3520
3520
  // src/client/storage/useR2Files.ts
3521
3521
  import { useCallback as useCallback15, useState as useState13, useMemo as useMemo11 } from "react";
3522
+
3523
+ // src/shared/app-files.ts
3524
+ var MAX_APP_FILE_BYTES = 25 * 1024 * 1024;
3525
+ function formatBytes(bytes) {
3526
+ if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MiB`;
3527
+ if (bytes >= 1024) return `${Math.round(bytes / 1024)} KiB`;
3528
+ return `${bytes} B`;
3529
+ }
3530
+ function encodeKeyPath(key) {
3531
+ return key.split("/").map(encodeURIComponent).join("/");
3532
+ }
3533
+ function oversizeMessage(bytes) {
3534
+ return `That file is ${formatBytes(bytes)}; the limit is ${formatBytes(MAX_APP_FILE_BYTES)} per file.`;
3535
+ }
3536
+ function describeFilesFailure(status, body) {
3537
+ const trimmed = body.trim();
3538
+ if (trimmed.startsWith("{")) {
3539
+ try {
3540
+ const parsed = JSON.parse(trimmed);
3541
+ if (parsed.error) return parsed.error;
3542
+ } catch {
3543
+ }
3544
+ }
3545
+ if (status === 413) {
3546
+ return `The file is too large to upload. The limit is ${formatBytes(MAX_APP_FILE_BYTES)} per file; larger uploads are rejected by the network before the app receives them.`;
3547
+ }
3548
+ if (status === 401 || status === 403) return "Not authorized to write these files.";
3549
+ if (status === 415) return "That file type is not accepted.";
3550
+ if (status >= 500) return `The files service failed (HTTP ${status}). Retry shortly.`;
3551
+ return `Upload failed (HTTP ${status}).`;
3552
+ }
3553
+
3554
+ // src/client/storage/useR2Files.ts
3555
+ function oversize(bytes) {
3556
+ return bytes > MAX_APP_FILE_BYTES ? { success: false, error: oversizeMessage(bytes) } : null;
3557
+ }
3558
+ async function readUploadResult(response) {
3559
+ const text = await response.text();
3560
+ if (response.ok) {
3561
+ try {
3562
+ return JSON.parse(text);
3563
+ } catch {
3564
+ return { success: false, error: "The files service returned a malformed response." };
3565
+ }
3566
+ }
3567
+ return { success: false, error: describeFilesFailure(response.status, text) };
3568
+ }
3522
3569
  function resolveFile(fileOrKey) {
3523
3570
  if (typeof fileOrKey === "string") {
3524
3571
  return { key: fileOrKey, name: void 0 };
3525
3572
  }
3526
3573
  return { key: fileOrKey.key, name: fileOrKey.originalName };
3527
3574
  }
3528
- function encodeKeyPath(key) {
3529
- return key.split("/").map(encodeURIComponent).join("/");
3530
- }
3531
3575
  function useR2Files(options) {
3532
3576
  const [isUploading, setIsUploading] = useState13(false);
3533
3577
  const scope = options?.scope ?? "self";
@@ -3547,6 +3591,8 @@ function useR2Files(options) {
3547
3591
  async (file, name) => {
3548
3592
  setIsUploading(true);
3549
3593
  try {
3594
+ const tooBig = oversize(file.size);
3595
+ if (tooBig) return tooBig;
3550
3596
  const headers = await authHeaders();
3551
3597
  const formData = new FormData();
3552
3598
  formData.append("file", file);
@@ -3556,7 +3602,7 @@ function useR2Files(options) {
3556
3602
  headers,
3557
3603
  body: formData
3558
3604
  });
3559
- return await response.json();
3605
+ return await readUploadResult(response);
3560
3606
  } catch (err) {
3561
3607
  return { success: false, error: err instanceof Error ? err.message : "Upload failed" };
3562
3608
  } finally {
@@ -3569,13 +3615,15 @@ function useR2Files(options) {
3569
3615
  async (base64Data, name, mimeType) => {
3570
3616
  setIsUploading(true);
3571
3617
  try {
3618
+ const tooBig = oversize(Math.floor(base64Data.length * 3 / 4));
3619
+ if (tooBig) return tooBig;
3572
3620
  const headers = await authHeaders();
3573
3621
  const response = await fetch(`/api/files/upload?${scopeParams}`, {
3574
3622
  method: "POST",
3575
3623
  headers: { ...headers, "Content-Type": "application/json" },
3576
3624
  body: JSON.stringify({ data: base64Data, name, mimeType })
3577
3625
  });
3578
- return await response.json();
3626
+ return await readUploadResult(response);
3579
3627
  } catch (err) {
3580
3628
  return { success: false, error: err instanceof Error ? err.message : "Upload failed" };
3581
3629
  } finally {