@gethashd/bytecave-browser 1.0.60 → 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,6 +6181,17 @@ 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
+ }
6189
+ if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
6190
+ return {
6191
+ success: false,
6192
+ error: "WebSocket connection timeout"
6193
+ };
6194
+ }
6184
6195
  const requestId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
6185
6196
  const request = {
6186
6197
  type: "retrieve-request",
@@ -7360,25 +7371,43 @@ function useHashdUrl(hashdUrl) {
7360
7371
  }
7361
7372
  const cid = hashdUrl.replace("hashd://", "").split("?")[0];
7362
7373
  let mounted = true;
7374
+ let retryCount = 0;
7375
+ const maxRetries = 3;
7376
+ const retryDelay = 1e3;
7363
7377
  setLoading(true);
7364
7378
  setError(null);
7365
- retrieve(cid).then((result) => {
7366
- if (!mounted) return;
7367
- if (result.success && result.data) {
7368
- const dataCopy = new Uint8Array(result.data);
7369
- const blob = new Blob([dataCopy]);
7370
- const url = URL.createObjectURL(blob);
7371
- setBlobUrl(url);
7372
- setLoading(false);
7373
- } else {
7374
- setError(result.error || "Failed to retrieve content");
7375
- setLoading(false);
7376
- }
7377
- }).catch((err) => {
7378
- if (!mounted) return;
7379
- setError(err.message || "Failed to retrieve content");
7380
- setLoading(false);
7381
- });
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();
7382
7411
  return () => {
7383
7412
  mounted = false;
7384
7413
  if (blobUrl) {
package/dist/index.cjs CHANGED
@@ -6234,6 +6234,17 @@ 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
+ }
6242
+ if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
6243
+ return {
6244
+ success: false,
6245
+ error: "WebSocket connection timeout"
6246
+ };
6247
+ }
6237
6248
  const requestId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
6238
6249
  const request = {
6239
6250
  type: "retrieve-request",
@@ -7555,25 +7566,43 @@ function useHashdUrl(hashdUrl) {
7555
7566
  }
7556
7567
  const cid = hashdUrl.replace("hashd://", "").split("?")[0];
7557
7568
  let mounted = true;
7569
+ let retryCount = 0;
7570
+ const maxRetries = 3;
7571
+ const retryDelay = 1e3;
7558
7572
  setLoading(true);
7559
7573
  setError(null);
7560
- retrieve(cid).then((result) => {
7561
- if (!mounted) return;
7562
- if (result.success && result.data) {
7563
- const dataCopy = new Uint8Array(result.data);
7564
- const blob = new Blob([dataCopy]);
7565
- const url = URL.createObjectURL(blob);
7566
- setBlobUrl(url);
7567
- setLoading(false);
7568
- } else {
7569
- setError(result.error || "Failed to retrieve content");
7570
- setLoading(false);
7571
- }
7572
- }).catch((err) => {
7573
- if (!mounted) return;
7574
- setError(err.message || "Failed to retrieve content");
7575
- setLoading(false);
7576
- });
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();
7577
7606
  return () => {
7578
7607
  mounted = false;
7579
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-LMK44RJF.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-LMK44RJF.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.60",
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,9 +215,24 @@ 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
  }
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
+
230
+ if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
231
+ return {
232
+ success: false,
233
+ error: 'WebSocket connection timeout'
234
+ };
235
+ }
221
236
 
222
237
  const requestId = Math.random().toString(36).substring(2, 15) +
223
238
  Math.random().toString(36).substring(2, 15);