@gethashd/bytecave-browser 1.0.20 → 1.0.21
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/{chunk-6ITKYT45.js → chunk-HKE7DLEO.js} +7 -52
- package/dist/index.cjs +7 -52
- package/dist/index.js +1 -1
- package/dist/p2p-protocols.d.ts +1 -7
- package/dist/react/index.cjs +4 -41
- package/dist/react/index.js +1 -1
- package/package.json +1 -1
- package/src/client.ts +4 -14
- package/src/p2p-protocols.ts +6 -51
|
@@ -5705,15 +5705,9 @@ var P2PProtocolClient = class {
|
|
|
5705
5705
|
const timeoutMs = 3e4 + fileSizeMB * 1e4;
|
|
5706
5706
|
console.log(`[ByteCave P2P] Store timeout: ${Math.round(timeoutMs / 1e3)}s for ${fileSizeMB.toFixed(2)}MB`);
|
|
5707
5707
|
const storePromise = (async () => {
|
|
5708
|
-
console.log("[ByteCave P2P] Step 1:
|
|
5709
|
-
const
|
|
5710
|
-
|
|
5711
|
-
throw new Error("No connection to peer");
|
|
5712
|
-
}
|
|
5713
|
-
const connection = connections[0];
|
|
5714
|
-
console.log("[ByteCave P2P] Step 2: Opening stream on existing connection...");
|
|
5715
|
-
const stream = await connection.newStream("/bytecave/store/1.0.0");
|
|
5716
|
-
console.log("[ByteCave P2P] Step 3: Stream established");
|
|
5708
|
+
console.log("[ByteCave P2P] Step 1: Dialing store protocol...");
|
|
5709
|
+
const stream = await this.node.dialProtocol(peerIdObj, "/bytecave/store/1.0.0");
|
|
5710
|
+
console.log("[ByteCave P2P] Step 2: Stream established");
|
|
5717
5711
|
const dataCopy = new Uint8Array(ciphertext);
|
|
5718
5712
|
const hashBuffer = await crypto.subtle.digest("SHA-256", dataCopy);
|
|
5719
5713
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
@@ -5783,47 +5777,16 @@ var P2PProtocolClient = class {
|
|
|
5783
5777
|
return null;
|
|
5784
5778
|
}
|
|
5785
5779
|
}
|
|
5786
|
-
/**
|
|
5787
|
-
* Wait for a direct (non-relay) connection to a peer
|
|
5788
|
-
* Returns true if direct connection is available, false if timeout
|
|
5789
|
-
*/
|
|
5790
|
-
async waitForDirectConnection(peerId, timeoutMs = 3e3) {
|
|
5791
|
-
if (!this.node) return false;
|
|
5792
|
-
const peerIdObj = peerIdFromString(peerId);
|
|
5793
|
-
const startTime = Date.now();
|
|
5794
|
-
while (Date.now() - startTime < timeoutMs) {
|
|
5795
|
-
const connections = this.node.getConnections(peerIdObj);
|
|
5796
|
-
const hasDirectConnection = connections.some((conn) => {
|
|
5797
|
-
const isRelay = conn.remoteAddr.toString().includes("/p2p-circuit");
|
|
5798
|
-
return !isRelay;
|
|
5799
|
-
});
|
|
5800
|
-
if (hasDirectConnection) {
|
|
5801
|
-
console.log("[ByteCave P2P] Direct connection established to", peerId.slice(0, 12));
|
|
5802
|
-
return true;
|
|
5803
|
-
}
|
|
5804
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
5805
|
-
}
|
|
5806
|
-
console.warn("[ByteCave P2P] Timeout waiting for direct connection to", peerId.slice(0, 12));
|
|
5807
|
-
return false;
|
|
5808
|
-
}
|
|
5809
5780
|
/**
|
|
5810
5781
|
* Get health info from a peer via P2P stream
|
|
5811
|
-
* Waits for direct connection upgrade before querying
|
|
5812
5782
|
*/
|
|
5813
|
-
async getHealthFromPeer(peerId
|
|
5783
|
+
async getHealthFromPeer(peerId) {
|
|
5814
5784
|
if (!this.node) {
|
|
5815
5785
|
console.warn("[ByteCave P2P] No node available for health request");
|
|
5816
5786
|
return null;
|
|
5817
5787
|
}
|
|
5818
5788
|
try {
|
|
5819
5789
|
const peerIdObj = peerIdFromString(peerId);
|
|
5820
|
-
if (waitForDirect) {
|
|
5821
|
-
const hasDirect = await this.waitForDirectConnection(peerId, 3e3);
|
|
5822
|
-
if (!hasDirect) {
|
|
5823
|
-
console.warn("[ByteCave P2P] No direct connection available for health query to", peerId.slice(0, 12));
|
|
5824
|
-
return null;
|
|
5825
|
-
}
|
|
5826
|
-
}
|
|
5827
5790
|
const stream = await this.node.dialProtocol(peerIdObj, PROTOCOL_HEALTH);
|
|
5828
5791
|
await this.writeMessage(stream, {});
|
|
5829
5792
|
const response = await this.readMessage(stream);
|
|
@@ -6327,16 +6290,13 @@ var ByteCaveClient = class {
|
|
|
6327
6290
|
return { success: false, error: "P2P node not initialized" };
|
|
6328
6291
|
}
|
|
6329
6292
|
const allPeers = this.node.getPeers();
|
|
6330
|
-
const connectedPeerIds = allPeers.map((p) => p.toString())
|
|
6331
|
-
const peerInfo = this.knownPeers.get(peerId);
|
|
6332
|
-
return !peerInfo?.isRelay;
|
|
6333
|
-
});
|
|
6293
|
+
const connectedPeerIds = allPeers.map((p) => p.toString());
|
|
6334
6294
|
console.log("[ByteCave] Store - connected storage peers:", connectedPeerIds.length);
|
|
6335
6295
|
console.log("[ByteCave] Store - knownPeers with registration info:", this.knownPeers.size);
|
|
6336
6296
|
if (connectedPeerIds.length === 0) {
|
|
6337
6297
|
return { success: false, error: "No storage peers available" };
|
|
6338
6298
|
}
|
|
6339
|
-
const registeredPeerIds = Array.from(this.knownPeers.values()).filter((p) => p.isRegistered &&
|
|
6299
|
+
const registeredPeerIds = Array.from(this.knownPeers.values()).filter((p) => p.isRegistered && connectedPeerIds.includes(p.peerId)).map((p) => p.peerId);
|
|
6340
6300
|
const storagePeerIds = registeredPeerIds.length > 0 ? [...registeredPeerIds, ...connectedPeerIds.filter((id) => !registeredPeerIds.includes(id))] : connectedPeerIds;
|
|
6341
6301
|
console.log(
|
|
6342
6302
|
"[ByteCave] Store - peer order (registered first):",
|
|
@@ -6686,12 +6646,7 @@ Nonce: ${nonce}`;
|
|
|
6686
6646
|
blobCount: announcement.blobCount,
|
|
6687
6647
|
timestamp: announcement.timestamp,
|
|
6688
6648
|
multiaddrs: announcement.multiaddrs,
|
|
6689
|
-
relayAddrs: announcement.relayAddrs || existing?.relayAddrs
|
|
6690
|
-
// Store on-chain registration status
|
|
6691
|
-
registeredOnChain: announcement.registeredOnChain || false,
|
|
6692
|
-
onChainNodeId: announcement.onChainNodeId || existing?.onChainNodeId,
|
|
6693
|
-
// Store relay flag so we can filter out relays from storage attempts
|
|
6694
|
-
isRelay: announcement.isRelay || false
|
|
6649
|
+
relayAddrs: announcement.relayAddrs || existing?.relayAddrs
|
|
6695
6650
|
};
|
|
6696
6651
|
this.knownPeers.set(announcement.peerId, peerInfo);
|
|
6697
6652
|
this.emit("peerAnnounce", peerInfo);
|
package/dist/index.cjs
CHANGED
|
@@ -5773,15 +5773,9 @@ var P2PProtocolClient = class {
|
|
|
5773
5773
|
const timeoutMs = 3e4 + fileSizeMB * 1e4;
|
|
5774
5774
|
console.log(`[ByteCave P2P] Store timeout: ${Math.round(timeoutMs / 1e3)}s for ${fileSizeMB.toFixed(2)}MB`);
|
|
5775
5775
|
const storePromise = (async () => {
|
|
5776
|
-
console.log("[ByteCave P2P] Step 1:
|
|
5777
|
-
const
|
|
5778
|
-
|
|
5779
|
-
throw new Error("No connection to peer");
|
|
5780
|
-
}
|
|
5781
|
-
const connection = connections[0];
|
|
5782
|
-
console.log("[ByteCave P2P] Step 2: Opening stream on existing connection...");
|
|
5783
|
-
const stream = await connection.newStream("/bytecave/store/1.0.0");
|
|
5784
|
-
console.log("[ByteCave P2P] Step 3: Stream established");
|
|
5776
|
+
console.log("[ByteCave P2P] Step 1: Dialing store protocol...");
|
|
5777
|
+
const stream = await this.node.dialProtocol(peerIdObj, "/bytecave/store/1.0.0");
|
|
5778
|
+
console.log("[ByteCave P2P] Step 2: Stream established");
|
|
5785
5779
|
const dataCopy = new Uint8Array(ciphertext);
|
|
5786
5780
|
const hashBuffer = await crypto.subtle.digest("SHA-256", dataCopy);
|
|
5787
5781
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
@@ -5851,47 +5845,16 @@ var P2PProtocolClient = class {
|
|
|
5851
5845
|
return null;
|
|
5852
5846
|
}
|
|
5853
5847
|
}
|
|
5854
|
-
/**
|
|
5855
|
-
* Wait for a direct (non-relay) connection to a peer
|
|
5856
|
-
* Returns true if direct connection is available, false if timeout
|
|
5857
|
-
*/
|
|
5858
|
-
async waitForDirectConnection(peerId, timeoutMs = 3e3) {
|
|
5859
|
-
if (!this.node) return false;
|
|
5860
|
-
const peerIdObj = peerIdFromString(peerId);
|
|
5861
|
-
const startTime = Date.now();
|
|
5862
|
-
while (Date.now() - startTime < timeoutMs) {
|
|
5863
|
-
const connections = this.node.getConnections(peerIdObj);
|
|
5864
|
-
const hasDirectConnection = connections.some((conn) => {
|
|
5865
|
-
const isRelay = conn.remoteAddr.toString().includes("/p2p-circuit");
|
|
5866
|
-
return !isRelay;
|
|
5867
|
-
});
|
|
5868
|
-
if (hasDirectConnection) {
|
|
5869
|
-
console.log("[ByteCave P2P] Direct connection established to", peerId.slice(0, 12));
|
|
5870
|
-
return true;
|
|
5871
|
-
}
|
|
5872
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
5873
|
-
}
|
|
5874
|
-
console.warn("[ByteCave P2P] Timeout waiting for direct connection to", peerId.slice(0, 12));
|
|
5875
|
-
return false;
|
|
5876
|
-
}
|
|
5877
5848
|
/**
|
|
5878
5849
|
* Get health info from a peer via P2P stream
|
|
5879
|
-
* Waits for direct connection upgrade before querying
|
|
5880
5850
|
*/
|
|
5881
|
-
async getHealthFromPeer(peerId
|
|
5851
|
+
async getHealthFromPeer(peerId) {
|
|
5882
5852
|
if (!this.node) {
|
|
5883
5853
|
console.warn("[ByteCave P2P] No node available for health request");
|
|
5884
5854
|
return null;
|
|
5885
5855
|
}
|
|
5886
5856
|
try {
|
|
5887
5857
|
const peerIdObj = peerIdFromString(peerId);
|
|
5888
|
-
if (waitForDirect) {
|
|
5889
|
-
const hasDirect = await this.waitForDirectConnection(peerId, 3e3);
|
|
5890
|
-
if (!hasDirect) {
|
|
5891
|
-
console.warn("[ByteCave P2P] No direct connection available for health query to", peerId.slice(0, 12));
|
|
5892
|
-
return null;
|
|
5893
|
-
}
|
|
5894
|
-
}
|
|
5895
5858
|
const stream = await this.node.dialProtocol(peerIdObj, PROTOCOL_HEALTH);
|
|
5896
5859
|
await this.writeMessage(stream, {});
|
|
5897
5860
|
const response = await this.readMessage(stream);
|
|
@@ -6380,16 +6343,13 @@ var ByteCaveClient = class {
|
|
|
6380
6343
|
return { success: false, error: "P2P node not initialized" };
|
|
6381
6344
|
}
|
|
6382
6345
|
const allPeers = this.node.getPeers();
|
|
6383
|
-
const connectedPeerIds = allPeers.map((p) => p.toString())
|
|
6384
|
-
const peerInfo = this.knownPeers.get(peerId);
|
|
6385
|
-
return !peerInfo?.isRelay;
|
|
6386
|
-
});
|
|
6346
|
+
const connectedPeerIds = allPeers.map((p) => p.toString());
|
|
6387
6347
|
console.log("[ByteCave] Store - connected storage peers:", connectedPeerIds.length);
|
|
6388
6348
|
console.log("[ByteCave] Store - knownPeers with registration info:", this.knownPeers.size);
|
|
6389
6349
|
if (connectedPeerIds.length === 0) {
|
|
6390
6350
|
return { success: false, error: "No storage peers available" };
|
|
6391
6351
|
}
|
|
6392
|
-
const registeredPeerIds = Array.from(this.knownPeers.values()).filter((p) => p.isRegistered &&
|
|
6352
|
+
const registeredPeerIds = Array.from(this.knownPeers.values()).filter((p) => p.isRegistered && connectedPeerIds.includes(p.peerId)).map((p) => p.peerId);
|
|
6393
6353
|
const storagePeerIds = registeredPeerIds.length > 0 ? [...registeredPeerIds, ...connectedPeerIds.filter((id) => !registeredPeerIds.includes(id))] : connectedPeerIds;
|
|
6394
6354
|
console.log(
|
|
6395
6355
|
"[ByteCave] Store - peer order (registered first):",
|
|
@@ -6739,12 +6699,7 @@ Nonce: ${nonce}`;
|
|
|
6739
6699
|
blobCount: announcement.blobCount,
|
|
6740
6700
|
timestamp: announcement.timestamp,
|
|
6741
6701
|
multiaddrs: announcement.multiaddrs,
|
|
6742
|
-
relayAddrs: announcement.relayAddrs || existing?.relayAddrs
|
|
6743
|
-
// Store on-chain registration status
|
|
6744
|
-
registeredOnChain: announcement.registeredOnChain || false,
|
|
6745
|
-
onChainNodeId: announcement.onChainNodeId || existing?.onChainNodeId,
|
|
6746
|
-
// Store relay flag so we can filter out relays from storage attempts
|
|
6747
|
-
isRelay: announcement.isRelay || false
|
|
6702
|
+
relayAddrs: announcement.relayAddrs || existing?.relayAddrs
|
|
6748
6703
|
};
|
|
6749
6704
|
this.knownPeers.set(announcement.peerId, peerInfo);
|
|
6750
6705
|
this.emit("peerAnnounce", peerInfo);
|
package/dist/index.js
CHANGED
package/dist/p2p-protocols.d.ts
CHANGED
|
@@ -90,16 +90,10 @@ export declare class P2PProtocolClient {
|
|
|
90
90
|
data: Uint8Array;
|
|
91
91
|
mimeType: string;
|
|
92
92
|
} | null>;
|
|
93
|
-
/**
|
|
94
|
-
* Wait for a direct (non-relay) connection to a peer
|
|
95
|
-
* Returns true if direct connection is available, false if timeout
|
|
96
|
-
*/
|
|
97
|
-
private waitForDirectConnection;
|
|
98
93
|
/**
|
|
99
94
|
* Get health info from a peer via P2P stream
|
|
100
|
-
* Waits for direct connection upgrade before querying
|
|
101
95
|
*/
|
|
102
|
-
getHealthFromPeer(peerId: string
|
|
96
|
+
getHealthFromPeer(peerId: string): Promise<P2PHealthResponse | null>;
|
|
103
97
|
/**
|
|
104
98
|
* Query relay for peer directory
|
|
105
99
|
*/
|
package/dist/react/index.cjs
CHANGED
|
@@ -6028,15 +6028,9 @@ var P2PProtocolClient = class {
|
|
|
6028
6028
|
const timeoutMs = 3e4 + fileSizeMB * 1e4;
|
|
6029
6029
|
console.log(`[ByteCave P2P] Store timeout: ${Math.round(timeoutMs / 1e3)}s for ${fileSizeMB.toFixed(2)}MB`);
|
|
6030
6030
|
const storePromise = (async () => {
|
|
6031
|
-
console.log("[ByteCave P2P] Step 1:
|
|
6032
|
-
const
|
|
6033
|
-
|
|
6034
|
-
throw new Error("No connection to peer");
|
|
6035
|
-
}
|
|
6036
|
-
const connection = connections[0];
|
|
6037
|
-
console.log("[ByteCave P2P] Step 2: Opening stream on existing connection...");
|
|
6038
|
-
const stream = await connection.newStream("/bytecave/store/1.0.0");
|
|
6039
|
-
console.log("[ByteCave P2P] Step 3: Stream established");
|
|
6031
|
+
console.log("[ByteCave P2P] Step 1: Dialing store protocol...");
|
|
6032
|
+
const stream = await this.node.dialProtocol(peerIdObj, "/bytecave/store/1.0.0");
|
|
6033
|
+
console.log("[ByteCave P2P] Step 2: Stream established");
|
|
6040
6034
|
const dataCopy = new Uint8Array(ciphertext);
|
|
6041
6035
|
const hashBuffer = await crypto.subtle.digest("SHA-256", dataCopy);
|
|
6042
6036
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
@@ -6106,47 +6100,16 @@ var P2PProtocolClient = class {
|
|
|
6106
6100
|
return null;
|
|
6107
6101
|
}
|
|
6108
6102
|
}
|
|
6109
|
-
/**
|
|
6110
|
-
* Wait for a direct (non-relay) connection to a peer
|
|
6111
|
-
* Returns true if direct connection is available, false if timeout
|
|
6112
|
-
*/
|
|
6113
|
-
async waitForDirectConnection(peerId, timeoutMs = 3e3) {
|
|
6114
|
-
if (!this.node) return false;
|
|
6115
|
-
const peerIdObj = peerIdFromString(peerId);
|
|
6116
|
-
const startTime = Date.now();
|
|
6117
|
-
while (Date.now() - startTime < timeoutMs) {
|
|
6118
|
-
const connections = this.node.getConnections(peerIdObj);
|
|
6119
|
-
const hasDirectConnection = connections.some((conn) => {
|
|
6120
|
-
const isRelay = conn.remoteAddr.toString().includes("/p2p-circuit");
|
|
6121
|
-
return !isRelay;
|
|
6122
|
-
});
|
|
6123
|
-
if (hasDirectConnection) {
|
|
6124
|
-
console.log("[ByteCave P2P] Direct connection established to", peerId.slice(0, 12));
|
|
6125
|
-
return true;
|
|
6126
|
-
}
|
|
6127
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
6128
|
-
}
|
|
6129
|
-
console.warn("[ByteCave P2P] Timeout waiting for direct connection to", peerId.slice(0, 12));
|
|
6130
|
-
return false;
|
|
6131
|
-
}
|
|
6132
6103
|
/**
|
|
6133
6104
|
* Get health info from a peer via P2P stream
|
|
6134
|
-
* Waits for direct connection upgrade before querying
|
|
6135
6105
|
*/
|
|
6136
|
-
async getHealthFromPeer(peerId
|
|
6106
|
+
async getHealthFromPeer(peerId) {
|
|
6137
6107
|
if (!this.node) {
|
|
6138
6108
|
console.warn("[ByteCave P2P] No node available for health request");
|
|
6139
6109
|
return null;
|
|
6140
6110
|
}
|
|
6141
6111
|
try {
|
|
6142
6112
|
const peerIdObj = peerIdFromString(peerId);
|
|
6143
|
-
if (waitForDirect) {
|
|
6144
|
-
const hasDirect = await this.waitForDirectConnection(peerId, 3e3);
|
|
6145
|
-
if (!hasDirect) {
|
|
6146
|
-
console.warn("[ByteCave P2P] No direct connection available for health query to", peerId.slice(0, 12));
|
|
6147
|
-
return null;
|
|
6148
|
-
}
|
|
6149
|
-
}
|
|
6150
6113
|
const stream = await this.node.dialProtocol(peerIdObj, PROTOCOL_HEALTH);
|
|
6151
6114
|
await this.writeMessage(stream, {});
|
|
6152
6115
|
const response = await this.readMessage(stream);
|
package/dist/react/index.js
CHANGED
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -427,14 +427,9 @@ export class ByteCaveClient {
|
|
|
427
427
|
return { success: false, error: 'P2P node not initialized' };
|
|
428
428
|
}
|
|
429
429
|
|
|
430
|
-
// Get all connected peers
|
|
430
|
+
// Get all connected peers
|
|
431
431
|
const allPeers = this.node.getPeers();
|
|
432
|
-
const connectedPeerIds = allPeers.map(p => p.toString())
|
|
433
|
-
.filter(peerId => {
|
|
434
|
-
const peerInfo = this.knownPeers.get(peerId) as any;
|
|
435
|
-
// Filter out relay peers - they announce with isRelay: true
|
|
436
|
-
return !peerInfo?.isRelay;
|
|
437
|
-
});
|
|
432
|
+
const connectedPeerIds = allPeers.map(p => p.toString());
|
|
438
433
|
|
|
439
434
|
console.log('[ByteCave] Store - connected storage peers:', connectedPeerIds.length);
|
|
440
435
|
console.log('[ByteCave] Store - knownPeers with registration info:', this.knownPeers.size);
|
|
@@ -446,7 +441,7 @@ export class ByteCaveClient {
|
|
|
446
441
|
// Prioritize registered peers from knownPeers (populated via floodsub announcements)
|
|
447
442
|
// If no registered peers known yet, use all connected peers
|
|
448
443
|
const registeredPeerIds = Array.from(this.knownPeers.values())
|
|
449
|
-
.filter(p => p.isRegistered &&
|
|
444
|
+
.filter(p => p.isRegistered && connectedPeerIds.includes(p.peerId))
|
|
450
445
|
.map(p => p.peerId);
|
|
451
446
|
|
|
452
447
|
const storagePeerIds = registeredPeerIds.length > 0
|
|
@@ -886,12 +881,7 @@ Nonce: ${nonce}`;
|
|
|
886
881
|
blobCount: announcement.blobCount,
|
|
887
882
|
timestamp: announcement.timestamp,
|
|
888
883
|
multiaddrs: announcement.multiaddrs,
|
|
889
|
-
relayAddrs: announcement.relayAddrs || (existing as any)?.relayAddrs
|
|
890
|
-
// Store on-chain registration status
|
|
891
|
-
registeredOnChain: announcement.registeredOnChain || false,
|
|
892
|
-
onChainNodeId: announcement.onChainNodeId || (existing as any)?.onChainNodeId,
|
|
893
|
-
// Store relay flag so we can filter out relays from storage attempts
|
|
894
|
-
isRelay: announcement.isRelay || false
|
|
884
|
+
relayAddrs: announcement.relayAddrs || (existing as any)?.relayAddrs
|
|
895
885
|
};
|
|
896
886
|
|
|
897
887
|
this.knownPeers.set(announcement.peerId, peerInfo);
|
package/src/p2p-protocols.ts
CHANGED
|
@@ -124,16 +124,10 @@ export class P2PProtocolClient {
|
|
|
124
124
|
|
|
125
125
|
// Wrap the entire operation in a timeout
|
|
126
126
|
const storePromise = (async () => {
|
|
127
|
-
console.log('[ByteCave P2P] Step 1:
|
|
128
|
-
//
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
throw new Error('No connection to peer');
|
|
132
|
-
}
|
|
133
|
-
const connection = connections[0];
|
|
134
|
-
console.log('[ByteCave P2P] Step 2: Opening stream on existing connection...');
|
|
135
|
-
const stream = await connection.newStream('/bytecave/store/1.0.0');
|
|
136
|
-
console.log('[ByteCave P2P] Step 3: Stream established');
|
|
127
|
+
console.log('[ByteCave P2P] Step 1: Dialing store protocol...');
|
|
128
|
+
// dialProtocol will upgrade limited relay connections to full connections automatically
|
|
129
|
+
const stream = await this.node!.dialProtocol(peerIdObj, '/bytecave/store/1.0.0');
|
|
130
|
+
console.log('[ByteCave P2P] Step 2: Stream established');
|
|
137
131
|
|
|
138
132
|
// Generate CID using SHA-256 (matches bytecave-core format: 64-char hex)
|
|
139
133
|
const dataCopy = new Uint8Array(ciphertext);
|
|
@@ -225,59 +219,20 @@ export class P2PProtocolClient {
|
|
|
225
219
|
}
|
|
226
220
|
}
|
|
227
221
|
|
|
228
|
-
/**
|
|
229
|
-
* Wait for a direct (non-relay) connection to a peer
|
|
230
|
-
* Returns true if direct connection is available, false if timeout
|
|
231
|
-
*/
|
|
232
|
-
private async waitForDirectConnection(peerId: string, timeoutMs: number = 3000): Promise<boolean> {
|
|
233
|
-
if (!this.node) return false;
|
|
234
|
-
|
|
235
|
-
const peerIdObj = peerIdFromString(peerId);
|
|
236
|
-
const startTime = Date.now();
|
|
237
|
-
|
|
238
|
-
while (Date.now() - startTime < timeoutMs) {
|
|
239
|
-
const connections = this.node.getConnections(peerIdObj);
|
|
240
|
-
const hasDirectConnection = connections.some(conn => {
|
|
241
|
-
// Check if connection is NOT a relay circuit (direct WebRTC or WebSocket)
|
|
242
|
-
const isRelay = conn.remoteAddr.toString().includes('/p2p-circuit');
|
|
243
|
-
return !isRelay;
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
if (hasDirectConnection) {
|
|
247
|
-
console.log('[ByteCave P2P] Direct connection established to', peerId.slice(0, 12));
|
|
248
|
-
return true;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// Wait 100ms before checking again
|
|
252
|
-
await new Promise(resolve => setTimeout(resolve, 100));
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
console.warn('[ByteCave P2P] Timeout waiting for direct connection to', peerId.slice(0, 12));
|
|
256
|
-
return false;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
222
|
/**
|
|
260
223
|
* Get health info from a peer via P2P stream
|
|
261
|
-
* Waits for direct connection upgrade before querying
|
|
262
224
|
*/
|
|
263
|
-
async getHealthFromPeer(peerId: string
|
|
225
|
+
async getHealthFromPeer(peerId: string): Promise<P2PHealthResponse | null> {
|
|
264
226
|
if (!this.node) {
|
|
265
227
|
console.warn('[ByteCave P2P] No node available for health request');
|
|
266
228
|
return null;
|
|
267
229
|
}
|
|
268
230
|
|
|
269
231
|
try {
|
|
232
|
+
|
|
270
233
|
// Convert string peerId to PeerId object
|
|
271
234
|
const peerIdObj = peerIdFromString(peerId);
|
|
272
235
|
|
|
273
|
-
// Wait for DCUtR to upgrade connection to direct WebRTC
|
|
274
|
-
if (waitForDirect) {
|
|
275
|
-
const hasDirect = await this.waitForDirectConnection(peerId, 3000);
|
|
276
|
-
if (!hasDirect) {
|
|
277
|
-
console.warn('[ByteCave P2P] No direct connection available for health query to', peerId.slice(0, 12));
|
|
278
|
-
return null;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
236
|
|
|
282
237
|
const stream = await this.node.dialProtocol(peerIdObj, PROTOCOL_HEALTH);
|
|
283
238
|
|