@decentnetwork/peer 0.1.25 → 0.1.26
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/peer.js +44 -5
- package/package.json +1 -1
package/dist/peer.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EventEmitter } from "node:events";
|
|
2
|
-
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { mkdir, readFile, writeFile, rename } from "node:fs/promises";
|
|
3
3
|
import { networkInterfaces } from "node:os";
|
|
4
4
|
import { dirname } from "node:path";
|
|
5
5
|
import nacl from "tweetnacl";
|
|
@@ -168,6 +168,7 @@ export class Peer {
|
|
|
168
168
|
#pendingFriendRequests = new Map();
|
|
169
169
|
#friends = new Map();
|
|
170
170
|
#friendStoreFile;
|
|
171
|
+
#persistSeq = 0; // makes atomic friend-store temp filenames unique per write
|
|
171
172
|
#cookieSymmetricKey;
|
|
172
173
|
#friendSessions = new Map();
|
|
173
174
|
#express;
|
|
@@ -3892,17 +3893,55 @@ export class Peer {
|
|
|
3892
3893
|
}
|
|
3893
3894
|
this.#debugLog(`loaded ${this.#friends.size} persisted friends`);
|
|
3894
3895
|
}
|
|
3895
|
-
catch {
|
|
3896
|
-
//
|
|
3896
|
+
catch (error) {
|
|
3897
|
+
// A missing file is normal (fresh node). A PARSE error is not — it
|
|
3898
|
+
// means the store is corrupt (historically: concatenated JSON from a
|
|
3899
|
+
// write race, now prevented by atomic persistFriends). Swallowing it
|
|
3900
|
+
// silently left the node with zero friends and unable to register, with
|
|
3901
|
+
// no clue why — so surface it loudly. Try to recover the first valid
|
|
3902
|
+
// JSON array so a corrupt store self-heals instead of stranding the node.
|
|
3903
|
+
const isParse = error instanceof SyntaxError;
|
|
3904
|
+
if (isParse) {
|
|
3905
|
+
try {
|
|
3906
|
+
const raw = await readFile(this.#friendStoreFile, "utf8");
|
|
3907
|
+
const end = raw.indexOf("]");
|
|
3908
|
+
if (end !== -1) {
|
|
3909
|
+
const recovered = JSON.parse(raw.slice(0, end + 1));
|
|
3910
|
+
if (Array.isArray(recovered)) {
|
|
3911
|
+
for (const record of recovered) {
|
|
3912
|
+
if (record && typeof record.pubkey === "string" && record.pubkey) {
|
|
3913
|
+
this.#friends.set(record.pubkey, record);
|
|
3914
|
+
}
|
|
3915
|
+
}
|
|
3916
|
+
// Rewrite cleanly (atomic) so the corruption is gone for good.
|
|
3917
|
+
this.#persistFriends();
|
|
3918
|
+
}
|
|
3919
|
+
}
|
|
3920
|
+
}
|
|
3921
|
+
catch { /* unrecoverable — fall through with whatever we have */ }
|
|
3922
|
+
// eslint-disable-next-line no-console
|
|
3923
|
+
console.error(`[peer] friend store ${this.#friendStoreFile} was corrupt (${error.message}); ` +
|
|
3924
|
+
`recovered ${this.#friends.size} friend(s).`);
|
|
3925
|
+
}
|
|
3897
3926
|
}
|
|
3898
3927
|
}
|
|
3899
3928
|
#persistFriends() {
|
|
3900
3929
|
if (!this.#friendStoreFile) {
|
|
3901
3930
|
return;
|
|
3902
3931
|
}
|
|
3932
|
+
const file = this.#friendStoreFile;
|
|
3903
3933
|
const payload = JSON.stringify([...this.#friends.values()], null, 2);
|
|
3904
|
-
|
|
3905
|
-
|
|
3934
|
+
// Write to a unique temp file then atomically rename over the target.
|
|
3935
|
+
// A plain writeFile can interleave when two writers race — observed: the
|
|
3936
|
+
// `agentnet init` friend-request flow (a short-lived standalone peer) and
|
|
3937
|
+
// the daemon both persisting produced a file with two CONCATENATED JSON
|
|
3938
|
+
// arrays, so JSON.parse threw, the loader's catch swallowed it, the node
|
|
3939
|
+
// loaded ZERO friends, and could never sendText/register again. rename()
|
|
3940
|
+
// is atomic on POSIX, so a reader always sees a complete old-or-new file.
|
|
3941
|
+
const tmp = `${file}.tmp.${process.pid}.${this.#persistSeq++}`;
|
|
3942
|
+
void mkdir(dirname(file), { recursive: true })
|
|
3943
|
+
.then(() => writeFile(tmp, payload, "utf8"))
|
|
3944
|
+
.then(() => rename(tmp, file))
|
|
3906
3945
|
.catch((error) => {
|
|
3907
3946
|
this.#debugLog(`persist friends failed: ${error.message}`);
|
|
3908
3947
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decentnetwork/peer",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.26",
|
|
4
4
|
"description": "Pure TypeScript port of Elastos Carrier (toxcore-derived) P2P messaging. DHT, onion routing, TCP relay, FlatBuffers app payloads, Express offline relay. Wire-compatible with iOS Beagle and the Carrier C SDK.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|