@gethashd/bytecave-browser 1.0.61 → 1.0.63

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.
@@ -6181,10 +6181,15 @@ var StorageWebSocketClient = class {
6181
6181
  if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
6182
6182
  await this.connect();
6183
6183
  }
6184
+ const connectionTimeout = 5e3;
6185
+ const startTime = Date.now();
6186
+ while ((!this.ws || this.ws.readyState !== WebSocket.OPEN) && Date.now() - startTime < connectionTimeout) {
6187
+ await new Promise((resolve) => setTimeout(resolve, 50));
6188
+ }
6184
6189
  if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
6185
6190
  return {
6186
6191
  success: false,
6187
- error: "WebSocket not connected after connect() call"
6192
+ error: "WebSocket connection timeout"
6188
6193
  };
6189
6194
  }
6190
6195
  const requestId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
@@ -7366,25 +7371,43 @@ function useHashdUrl(hashdUrl) {
7366
7371
  }
7367
7372
  const cid = hashdUrl.replace("hashd://", "").split("?")[0];
7368
7373
  let mounted = true;
7374
+ let retryCount = 0;
7375
+ const maxRetries = 3;
7376
+ const retryDelay = 1e3;
7369
7377
  setLoading(true);
7370
7378
  setError(null);
7371
- retrieve(cid).then((result) => {
7372
- if (!mounted) return;
7373
- if (result.success && result.data) {
7374
- const dataCopy = new Uint8Array(result.data);
7375
- const blob = new Blob([dataCopy]);
7376
- const url = URL.createObjectURL(blob);
7377
- setBlobUrl(url);
7378
- setLoading(false);
7379
- } else {
7380
- setError(result.error || "Failed to retrieve content");
7381
- setLoading(false);
7382
- }
7383
- }).catch((err) => {
7384
- if (!mounted) return;
7385
- setError(err.message || "Failed to retrieve content");
7386
- setLoading(false);
7387
- });
7379
+ const attemptRetrieve = () => {
7380
+ retrieve(cid).then((result) => {
7381
+ if (!mounted) return;
7382
+ if (result.success && result.data) {
7383
+ const dataCopy = new Uint8Array(result.data);
7384
+ const blob = new Blob([dataCopy]);
7385
+ const url = URL.createObjectURL(blob);
7386
+ setBlobUrl(url);
7387
+ setLoading(false);
7388
+ } else {
7389
+ if (retryCount < maxRetries && result.error?.includes("not connected")) {
7390
+ retryCount++;
7391
+ console.log(`[useHashdUrl] Retry ${retryCount}/${maxRetries} for CID:`, cid.slice(0, 12));
7392
+ setTimeout(attemptRetrieve, retryDelay);
7393
+ } else {
7394
+ setError(result.error || "Failed to retrieve content");
7395
+ setLoading(false);
7396
+ }
7397
+ }
7398
+ }).catch((err) => {
7399
+ if (!mounted) return;
7400
+ if (retryCount < maxRetries && err.message?.includes("not connected")) {
7401
+ retryCount++;
7402
+ console.log(`[useHashdUrl] Retry ${retryCount}/${maxRetries} for CID:`, cid.slice(0, 12));
7403
+ setTimeout(attemptRetrieve, retryDelay);
7404
+ } else {
7405
+ setError(err.message || "Failed to retrieve content");
7406
+ setLoading(false);
7407
+ }
7408
+ });
7409
+ };
7410
+ attemptRetrieve();
7388
7411
  return () => {
7389
7412
  mounted = false;
7390
7413
  if (blobUrl) {
package/dist/index.cjs CHANGED
@@ -6234,10 +6234,15 @@ var StorageWebSocketClient = class {
6234
6234
  if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
6235
6235
  await this.connect();
6236
6236
  }
6237
+ const connectionTimeout = 5e3;
6238
+ const startTime = Date.now();
6239
+ while ((!this.ws || this.ws.readyState !== WebSocket.OPEN) && Date.now() - startTime < connectionTimeout) {
6240
+ await new Promise((resolve) => setTimeout(resolve, 50));
6241
+ }
6237
6242
  if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
6238
6243
  return {
6239
6244
  success: false,
6240
- error: "WebSocket not connected after connect() call"
6245
+ error: "WebSocket connection timeout"
6241
6246
  };
6242
6247
  }
6243
6248
  const requestId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
@@ -7561,25 +7566,43 @@ function useHashdUrl(hashdUrl) {
7561
7566
  }
7562
7567
  const cid = hashdUrl.replace("hashd://", "").split("?")[0];
7563
7568
  let mounted = true;
7569
+ let retryCount = 0;
7570
+ const maxRetries = 3;
7571
+ const retryDelay = 1e3;
7564
7572
  setLoading(true);
7565
7573
  setError(null);
7566
- retrieve(cid).then((result) => {
7567
- if (!mounted) return;
7568
- if (result.success && result.data) {
7569
- const dataCopy = new Uint8Array(result.data);
7570
- const blob = new Blob([dataCopy]);
7571
- const url = URL.createObjectURL(blob);
7572
- setBlobUrl(url);
7573
- setLoading(false);
7574
- } else {
7575
- setError(result.error || "Failed to retrieve content");
7576
- setLoading(false);
7577
- }
7578
- }).catch((err) => {
7579
- if (!mounted) return;
7580
- setError(err.message || "Failed to retrieve content");
7581
- setLoading(false);
7582
- });
7574
+ const attemptRetrieve = () => {
7575
+ retrieve(cid).then((result) => {
7576
+ if (!mounted) return;
7577
+ if (result.success && result.data) {
7578
+ const dataCopy = new Uint8Array(result.data);
7579
+ const blob = new Blob([dataCopy]);
7580
+ const url = URL.createObjectURL(blob);
7581
+ setBlobUrl(url);
7582
+ setLoading(false);
7583
+ } else {
7584
+ if (retryCount < maxRetries && result.error?.includes("not connected")) {
7585
+ retryCount++;
7586
+ console.log(`[useHashdUrl] Retry ${retryCount}/${maxRetries} for CID:`, cid.slice(0, 12));
7587
+ setTimeout(attemptRetrieve, retryDelay);
7588
+ } else {
7589
+ setError(result.error || "Failed to retrieve content");
7590
+ setLoading(false);
7591
+ }
7592
+ }
7593
+ }).catch((err) => {
7594
+ if (!mounted) return;
7595
+ if (retryCount < maxRetries && err.message?.includes("not connected")) {
7596
+ retryCount++;
7597
+ console.log(`[useHashdUrl] Retry ${retryCount}/${maxRetries} for CID:`, cid.slice(0, 12));
7598
+ setTimeout(attemptRetrieve, retryDelay);
7599
+ } else {
7600
+ setError(err.message || "Failed to retrieve content");
7601
+ setLoading(false);
7602
+ }
7603
+ });
7604
+ };
7605
+ attemptRetrieve();
7583
7606
  return () => {
7584
7607
  mounted = false;
7585
7608
  if (blobUrl) {
package/dist/index.js CHANGED
@@ -13,7 +13,7 @@ import {
13
13
  useHashdImage,
14
14
  useHashdMedia,
15
15
  useHashdUrl
16
- } from "./chunk-IIXRZSDW.js";
16
+ } from "./chunk-RA6YSZXC.js";
17
17
  import {
18
18
  clearHashdCache,
19
19
  createHashdUrl,
@@ -6383,25 +6383,43 @@ function useHashdUrl(hashdUrl) {
6383
6383
  }
6384
6384
  const cid = hashdUrl.replace("hashd://", "").split("?")[0];
6385
6385
  let mounted = true;
6386
+ let retryCount = 0;
6387
+ const maxRetries = 3;
6388
+ const retryDelay = 1e3;
6386
6389
  setLoading(true);
6387
6390
  setError(null);
6388
- retrieve(cid).then((result) => {
6389
- if (!mounted) return;
6390
- if (result.success && result.data) {
6391
- const dataCopy = new Uint8Array(result.data);
6392
- const blob = new Blob([dataCopy]);
6393
- const url = URL.createObjectURL(blob);
6394
- setBlobUrl(url);
6395
- setLoading(false);
6396
- } else {
6397
- setError(result.error || "Failed to retrieve content");
6398
- setLoading(false);
6399
- }
6400
- }).catch((err) => {
6401
- if (!mounted) return;
6402
- setError(err.message || "Failed to retrieve content");
6403
- setLoading(false);
6404
- });
6391
+ const attemptRetrieve = () => {
6392
+ retrieve(cid).then((result) => {
6393
+ if (!mounted) return;
6394
+ if (result.success && result.data) {
6395
+ const dataCopy = new Uint8Array(result.data);
6396
+ const blob = new Blob([dataCopy]);
6397
+ const url = URL.createObjectURL(blob);
6398
+ setBlobUrl(url);
6399
+ setLoading(false);
6400
+ } else {
6401
+ if (retryCount < maxRetries && result.error?.includes("not connected")) {
6402
+ retryCount++;
6403
+ console.log(`[useHashdUrl] Retry ${retryCount}/${maxRetries} for CID:`, cid.slice(0, 12));
6404
+ setTimeout(attemptRetrieve, retryDelay);
6405
+ } else {
6406
+ setError(result.error || "Failed to retrieve content");
6407
+ setLoading(false);
6408
+ }
6409
+ }
6410
+ }).catch((err) => {
6411
+ if (!mounted) return;
6412
+ if (retryCount < maxRetries && err.message?.includes("not connected")) {
6413
+ retryCount++;
6414
+ console.log(`[useHashdUrl] Retry ${retryCount}/${maxRetries} for CID:`, cid.slice(0, 12));
6415
+ setTimeout(attemptRetrieve, retryDelay);
6416
+ } else {
6417
+ setError(err.message || "Failed to retrieve content");
6418
+ setLoading(false);
6419
+ }
6420
+ });
6421
+ };
6422
+ attemptRetrieve();
6405
6423
  return () => {
6406
6424
  mounted = false;
6407
6425
  if (blobUrl) {
@@ -8,7 +8,7 @@ import {
8
8
  useHashdImage,
9
9
  useHashdMedia,
10
10
  useHashdUrl
11
- } from "../chunk-IIXRZSDW.js";
11
+ } from "../chunk-RA6YSZXC.js";
12
12
  import "../chunk-EEZWRIUI.js";
13
13
  export {
14
14
  HashdAudio,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gethashd/bytecave-browser",
3
- "version": "1.0.61",
3
+ "version": "1.0.63",
4
4
  "description": "ByteCave browser client for WebRTC P2P connections to storage nodes",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",
@@ -32,29 +32,52 @@ export function useHashdUrl(hashdUrl: string | null | undefined): UseHashdUrlRes
32
32
  const cid = hashdUrl.replace('hashd://', '').split('?')[0];
33
33
 
34
34
  let mounted = true;
35
+ let retryCount = 0;
36
+ const maxRetries = 3;
37
+ const retryDelay = 1000; // 1 second between retries
38
+
35
39
  setLoading(true);
36
40
  setError(null);
37
41
 
38
- retrieve(cid)
39
- .then(result => {
40
- if (!mounted) return;
42
+ const attemptRetrieve = () => {
43
+ retrieve(cid)
44
+ .then(result => {
45
+ if (!mounted) return;
46
+
47
+ if (result.success && result.data) {
48
+ const dataCopy = new Uint8Array(result.data);
49
+ const blob = new Blob([dataCopy]);
50
+ const url = URL.createObjectURL(blob);
51
+ setBlobUrl(url);
52
+ setLoading(false);
53
+ } else {
54
+ // Retry on failure if we haven't exceeded max retries
55
+ if (retryCount < maxRetries && result.error?.includes('not connected')) {
56
+ retryCount++;
57
+ console.log(`[useHashdUrl] Retry ${retryCount}/${maxRetries} for CID:`, cid.slice(0, 12));
58
+ setTimeout(attemptRetrieve, retryDelay);
59
+ } else {
60
+ setError(result.error || 'Failed to retrieve content');
61
+ setLoading(false);
62
+ }
63
+ }
64
+ })
65
+ .catch(err => {
66
+ if (!mounted) return;
67
+
68
+ // Retry on connection errors
69
+ if (retryCount < maxRetries && err.message?.includes('not connected')) {
70
+ retryCount++;
71
+ console.log(`[useHashdUrl] Retry ${retryCount}/${maxRetries} for CID:`, cid.slice(0, 12));
72
+ setTimeout(attemptRetrieve, retryDelay);
73
+ } else {
74
+ setError(err.message || 'Failed to retrieve content');
75
+ setLoading(false);
76
+ }
77
+ });
78
+ };
41
79
 
42
- if (result.success && result.data) {
43
- const dataCopy = new Uint8Array(result.data);
44
- const blob = new Blob([dataCopy]);
45
- const url = URL.createObjectURL(blob);
46
- setBlobUrl(url);
47
- setLoading(false);
48
- } else {
49
- setError(result.error || 'Failed to retrieve content');
50
- setLoading(false);
51
- }
52
- })
53
- .catch(err => {
54
- if (!mounted) return;
55
- setError(err.message || 'Failed to retrieve content');
56
- setLoading(false);
57
- });
80
+ attemptRetrieve();
58
81
 
59
82
  return () => {
60
83
  mounted = false;
@@ -215,15 +215,22 @@ export class StorageWebSocketClient {
215
215
  }
216
216
 
217
217
  async retrieve(cid: string, timeout: number = 30000): Promise<{ success: boolean; data?: Uint8Array; mimeType?: string; error?: string }> {
218
+ // Ensure connection is established
218
219
  if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
219
220
  await this.connect();
220
221
  }
221
-
222
- // Double-check connection state after connect() completes
222
+
223
+ // Wait for WebSocket to be in OPEN state (with timeout)
224
+ const connectionTimeout = 5000; // 5 seconds max wait
225
+ const startTime = Date.now();
226
+ while ((!this.ws || this.ws.readyState !== WebSocket.OPEN) && (Date.now() - startTime < connectionTimeout)) {
227
+ await new Promise(resolve => setTimeout(resolve, 50));
228
+ }
229
+
223
230
  if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
224
231
  return {
225
232
  success: false,
226
- error: 'WebSocket not connected after connect() call'
233
+ error: 'WebSocket connection timeout'
227
234
  };
228
235
  }
229
236