Skip to content

Commit

Permalink
Merge pull request #175 from StarfilesFileSharing/alpha
Browse files Browse the repository at this point in the history
Nicer logs
  • Loading branch information
QuixThe2nd authored Nov 16, 2024
2 parents 9e5e708 + aa8dc84 commit a9f64f0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
16 changes: 8 additions & 8 deletions src/rpc/peers/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class PeerDB {
duration: values.duration ?? 0,
updatedAt: values.updatedAt ?? new Date().toISOString(),
} as PeerAttributes;
console.log(` ${peer.host} Peer INSERTed`, values);
console.log(`Peer: ${peer.host} Peer INSERTed`, values);
if (this.db.type === "SQLITE") {
const query = `INSERT INTO peer (host, hits, rejects, bytes, duration, updatedAt) VALUES (?, ?, ?, ?, ?, ?)`;

Expand All @@ -185,7 +185,7 @@ class PeerDB {
const request = objectStore.add(peer);

// @ts-expect-error:
request.onsuccess = (event): void => console.log(` ${peer.hash} Peer added successfully:`, event.target.result);
request.onsuccess = (event): void => console.log(`Peer: ${peer.hash} Peer added successfully:`, event.target.result);
// @ts-expect-error:
request.onerror = (event): void => console.error("Error adding peer:", event.target.error);
}
Expand Down Expand Up @@ -225,8 +225,8 @@ class PeerDB {
const query = `UPDATE peer SET ${updatedColumn.map((column) => `${column} = ?`).join(", ")} WHERE host = ?`;
this.db.db.prepare(query).values(params);
console.log(
` ${host} Peer UPDATEd - Updated Columns: ${updatedColumn.join(", ")}` + (this._rpcClient._client.config.logLevel === "verbose" ? ` - Params: ${params.join(", ")} - Query: ${query}` : ""),
this._rpcClient._client.config.logLevel === "verbose" ? console.log(` ${host} Updated Values:`, beforeAndAfter) : "",
`Peer: ${host} Peer UPDATEd - Updated Columns: ${updatedColumn.join(", ")}` + (this._rpcClient._client.config.logLevel === "verbose" ? ` - Params: ${params.join(", ")} - Query: ${query}` : ""),
this._rpcClient._client.config.logLevel === "verbose" ? console.log(`Peer: ${host} Updated Values:`, beforeAndAfter) : "",
);
} else {
// @ts-expect-error:
Expand All @@ -239,8 +239,8 @@ class PeerDB {
};
}
console.log(
` ${host} Peer UPDATEd - Updated Columns: ${updatedColumn.join(", ")}` + (this._rpcClient._client.config.logLevel === "verbose" ? ` - Params: ${params.join(", ")}` : ""),
this._rpcClient._client.config.logLevel === "verbose" ? console.log(` ${host} Updated Values:`, beforeAndAfter) : "",
`Peer: ${host} Peer UPDATEd - Updated Columns: ${updatedColumn.join(", ")}` + (this._rpcClient._client.config.logLevel === "verbose" ? ` - Params: ${params.join(", ")}` : ""),
this._rpcClient._client.config.logLevel === "verbose" ? console.log(`Peer: ${host} Updated Values:`, beforeAndAfter) : "",
);
}

Expand All @@ -257,7 +257,7 @@ class PeerDB {
if (objectStore instanceof ErrorWrongDatabaseType) return new ErrorWrongDatabaseType();
objectStore.delete(host).onerror = console.error;
}
console.log(` ${host} Peer DELETEd`);
console.log(`Peer: ${host} Peer DELETEd`);
return true;
}

Expand All @@ -275,7 +275,7 @@ class PeerDB {
peer[column] = (peer[column] || 0) + 1;
const objectStore = this.objectStore();
if (objectStore instanceof ErrorWrongDatabaseType) return objectStore;
objectStore.put(peer).onsuccess = () => console.log(` ${host} Incremented ${column}`);
objectStore.put(peer).onsuccess = () => console.log(`Peer: ${host} Incremented ${column}`);
}
};
}
Expand Down
20 changes: 10 additions & 10 deletions src/rpc/peers/rtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class RTCPeers {
};
conn.addEventListener("iceconnectionstatechange", () => {
if (conn.iceConnectionState === "disconnected" || conn.iceConnectionState === "closed" || conn.iceConnectionState === "failed") {
console.warn(`WebRTC: (13/12): ${from} Connection closed. Cleaning up peer connection.`);
console.warn(`WebRTC: (13/12): ${from} Connection closed. Cleaning up peer connection.`);
this.cleanupPeerConnection(conn);
}
});
Expand All @@ -166,7 +166,7 @@ class RTCPeers {

setTimeout(() => {
if (conn.signalingState === "have-local-offer") {
console.warn(`WebRTC: (13/12): ${from} Connection timed out. Cleaning up peer connection.`);
console.warn(`WebRTC: (13/12): ${from} Connection timed out. Cleaning up peer connection.`);
this.cleanupPeerConnection(conn);
}
}, this._rpcClient._client.config.timeout);
Expand All @@ -181,11 +181,11 @@ class RTCPeers {
if (remotePeerId) {
const peerConns = this.peerConnections[remotePeerId];
if (peerConns.offered?.conn === conn) {
console.warn(`WebRTC: (13/12): ${remotePeerId} Offered connecton is ${conn.iceConnectionState}`);
console.warn(`WebRTC: (13/12): ${remotePeerId} Offered connecton is ${conn.iceConnectionState}`);
peerConns.offered.conn.close();
delete peerConns.offered;
} else if (peerConns.answered?.conn === conn) {
console.warn(`WebRTC: (13/12): ${remotePeerId} Answered connecton is ${conn.iceConnectionState}`);
console.warn(`WebRTC: (13/12): ${remotePeerId} Answered connecton is ${conn.iceConnectionState}`);
peerConns.answered.conn.close();
delete peerConns.answered;
}
Expand All @@ -209,7 +209,7 @@ class RTCPeers {
this._rpcClient._client.events.log(this._rpcClient._client.events.rtcEvents.RTCAnnounce);
console.log(`WebRTC: (2/12): ${from} Received announce`);
if (this.peerConnections[from] && this.peerConnections[from].offered) {
console.warn(`WebRTC: (13/12): ${from} Already offered to peer`);
console.warn(`WebRTC: (13/12): ${from} Already offered to peer`);
return;
}
if (!this.peerConnections[from]) this.peerConnections[from] = {};
Expand All @@ -232,12 +232,12 @@ class RTCPeers {

this.peerConnections[from].answered = await this.createPeerConnection(from);
if (this.peerConnections[from].answered.conn.signalingState !== "stable" && this.peerConnections[from].answered.conn.signalingState !== "have-remote-offer") {
console.warn(`WebRTC: (13/12): ${from} Peer connection in unexpected state 1: ${this.peerConnections[from].answered.conn.signalingState}`);
console.warn(`WebRTC: (13/12): ${from} Peer connection in unexpected state 1: ${this.peerConnections[from].answered.conn.signalingState}`);
return;
}
await this.peerConnections[from].answered.conn.setRemoteDescription(offer);
if (this.peerConnections[from].answered.conn.signalingState !== "have-remote-offer") {
console.warn(`WebRTC: (13/12): ${from} Peer connection in unexpected state 2: ${this.peerConnections[from].answered.conn.signalingState}`);
console.warn(`WebRTC: (13/12): ${from} Peer connection in unexpected state 2: ${this.peerConnections[from].answered.conn.signalingState}`);
return;
}
try {
Expand All @@ -256,11 +256,11 @@ class RTCPeers {
this._rpcClient._client.events.log(this._rpcClient._client.events.rtcEvents.RTCAnswer);
if (!this.peerConnections[from] || !this.peerConnections[from].offered) {
console.log(this.peerConnections[from].offered);
console.warn(`WebRTC: (13/12): ${from} Rejecting answer - No open handshake`);
console.warn(`WebRTC: (13/12): ${from} Rejecting answer - No open handshake`);
return;
}
if (this.peerConnections[from].offered.conn.signalingState !== "have-local-offer") {
console.warn(`WebRTC: (13/12): ${from} Rejecting answer - Bad signalling state: ${this.peerConnections[from].offered?.conn.signalingState}`);
console.warn(`WebRTC: (13/12): ${from} Rejecting answer - Bad signalling state: ${this.peerConnections[from].offered?.conn.signalingState}`);
return;
}
console.log(`WebRTC: (7/12): ${from} Received answer`, extractIPAddress(answer.sdp));
Expand All @@ -271,7 +271,7 @@ class RTCPeers {
const iceCandidate = receivedIceCandidate;
this._rpcClient._client.events.log(this._rpcClient._client.events.rtcEvents.RTCIce);
if (!this.peerConnections[from]) {
console.warn(`WebRTC: (13/12): ${from} Rejecting Ice candidates received - No open handshake`);
console.warn(`WebRTC: (13/12): ${from} Rejecting Ice candidates received - No open handshake`);
return;
}
if (this._rpcClient._client.config.logLevel === "verbose") console.log(`WebRTC: (8/12): ${from} Received ICE candidate`);
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ router.set("/download", async (req, headers, client) => {
headers.set("Signal-Strength", String(fileContent.signal));
const address = client.wallet.address();
if (address) headers.set("Ethereum-Address", address);
console.log(` ${hash} Signal Strength:`, fileContent.signal, Utils.estimateHops(fileContent.signal));
console.log(`File: ${hash} Signal Strength:`, fileContent.signal, Utils.estimateHops(fileContent.signal));

headers.set("Content-Length", String(file.size));
if (file.name !== undefined && file.name !== null) {
Expand Down Expand Up @@ -219,7 +219,7 @@ router.set("/infohash", async (req, headers, client): Promise<Response> => {
headers.set("Cache-Control", "public, max-age=31536000");

headers.set("Signal-Strength", String(fileContent.signal));
console.log(` ${file.hash} Signal Strength:`, fileContent.signal, Utils.estimateHops(fileContent.signal));
console.log(`File: ${file.hash} Signal Strength:`, fileContent.signal, Utils.estimateHops(fileContent.signal));

headers.set("Content-Length", String(file.size));
if (file.name !== null) headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(file.name).replace(/%20/g, " ").replace(/(\.\w+)$/, " [HYDRAFILES]$1")}"`);
Expand Down

0 comments on commit a9f64f0

Please sign in to comment.