pinata 2.4.3 → 2.4.5

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
@@ -2567,33 +2567,50 @@ var uploadFile = async (config, file, network, options) => {
2567
2567
  let uploadReq;
2568
2568
  for (let i = 0; i < totalChunks; i++) {
2569
2569
  const chunk = file.slice(offset, offset + chunkSize);
2570
- uploadReq = await fetch(url, {
2571
- method: "PATCH",
2572
- headers: {
2573
- "Content-Type": "application/offset+octet-stream",
2574
- "Upload-Offset": offset.toString(),
2575
- ...headers2
2576
- },
2577
- body: chunk
2578
- });
2579
- if (!uploadReq.ok) {
2580
- const errorData = await uploadReq.text();
2581
- throw new chunkP556VRQU_js.NetworkError(
2582
- `HTTP error during chunk upload: ${errorData}`,
2583
- uploadReq.status,
2584
- {
2585
- error: errorData,
2586
- code: "HTTP_ERROR",
2587
- metadata: {
2588
- requestUrl: uploadReq.url
2589
- }
2570
+ let retryCount = 0;
2571
+ const maxRetries = 5;
2572
+ while (retryCount <= maxRetries) {
2573
+ try {
2574
+ uploadReq = await fetch(url, {
2575
+ method: "PATCH",
2576
+ headers: {
2577
+ "Content-Type": "application/offset+octet-stream",
2578
+ "Upload-Offset": offset.toString(),
2579
+ ...headers2
2580
+ },
2581
+ body: chunk
2582
+ });
2583
+ if (uploadReq.ok) {
2584
+ break;
2585
+ } else {
2586
+ const errorData = await uploadReq.text();
2587
+ throw new Error(`HTTP ${uploadReq.status}: ${errorData}`);
2590
2588
  }
2591
- );
2589
+ } catch (error) {
2590
+ retryCount++;
2591
+ if (retryCount > maxRetries) {
2592
+ const errorData = uploadReq ? await uploadReq.text().catch(() => "Unknown error") : error instanceof Error ? error.message : String(error);
2593
+ throw new chunkP556VRQU_js.NetworkError(
2594
+ `HTTP error during chunk upload after ${maxRetries} retries: ${errorData}`,
2595
+ uploadReq?.status || 0,
2596
+ {
2597
+ error: errorData,
2598
+ code: "HTTP_ERROR",
2599
+ metadata: {
2600
+ requestUrl: uploadReq?.url || url,
2601
+ retriesAttempted: maxRetries
2602
+ }
2603
+ }
2604
+ );
2605
+ }
2606
+ const delay = Math.min(1e3 * Math.pow(2, retryCount - 1), 1e4);
2607
+ await new Promise((resolve) => setTimeout(resolve, delay));
2608
+ }
2592
2609
  }
2593
2610
  offset += chunk.size;
2594
2611
  }
2595
2612
  if (uploadReq.status === 204) {
2596
- const fileId = chunkP556VRQU_js.getFileIdFromUrl(url);
2613
+ const cid = uploadReq.headers.get("upload-cid");
2597
2614
  let dataEndpoint;
2598
2615
  if (config.endpointUrl) {
2599
2616
  dataEndpoint = config.endpointUrl;
@@ -2601,7 +2618,7 @@ var uploadFile = async (config, file, network, options) => {
2601
2618
  dataEndpoint = "https://api.pinata.cloud/v3";
2602
2619
  }
2603
2620
  const fileInfoReq = await fetch(
2604
- `${dataEndpoint}/files/${network}/${fileId}`,
2621
+ `${dataEndpoint}/files/${network}?cid=${cid}`,
2605
2622
  {
2606
2623
  method: "GET",
2607
2624
  headers: {
@@ -2610,7 +2627,7 @@ var uploadFile = async (config, file, network, options) => {
2610
2627
  }
2611
2628
  );
2612
2629
  const fileInfo = await fileInfoReq.json();
2613
- const data2 = fileInfo.data;
2630
+ const data2 = fileInfo.data.files[0];
2614
2631
  if (options?.vectorize) {
2615
2632
  const vectorReq = await fetch(
2616
2633
  `${endpoint}/vectorize/files/${data2.id}`,
@@ -2705,10 +2722,16 @@ var uploadFile = async (config, file, network, options) => {
2705
2722
  throw error;
2706
2723
  }
2707
2724
  if (error instanceof Error) {
2708
- throw new chunkP556VRQU_js.PinataError(`Error processing base64: ${error.message}`);
2725
+ throw new chunkP556VRQU_js.PinataError(
2726
+ `Error uploading file: ${error.message}`,
2727
+ void 0,
2728
+ {
2729
+ error: error.toString()
2730
+ }
2731
+ );
2709
2732
  }
2710
2733
  throw new chunkP556VRQU_js.PinataError(
2711
- "An unknown error occurred while trying to upload base64"
2734
+ "An unknown error occurred while trying to upload file"
2712
2735
  );
2713
2736
  }
2714
2737
  }
@@ -2777,9 +2800,17 @@ var uploadFile = async (config, file, network, options) => {
2777
2800
  throw error;
2778
2801
  }
2779
2802
  if (error instanceof Error) {
2780
- throw new chunkP556VRQU_js.PinataError(`Error uploading file: ${error.message}`);
2803
+ throw new chunkP556VRQU_js.PinataError(
2804
+ `Error uploading file: ${error.message}`,
2805
+ void 0,
2806
+ {
2807
+ error: error.toString()
2808
+ }
2809
+ );
2781
2810
  }
2782
- throw new chunkP556VRQU_js.PinataError("An unknown error occurred while uploading the file");
2811
+ throw new chunkP556VRQU_js.PinataError(
2812
+ "An unknown error occurred while trying to upload file"
2813
+ );
2783
2814
  }
2784
2815
  };
2785
2816