@peerbit/server 5.10.13 → 5.10.14-3f16953

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.
@@ -23,7 +23,7 @@
23
23
  Learn how to configure a non-root public URL by running `npm run build`.
24
24
  -->
25
25
  <title>Peerbit</title>
26
- <script type="module" crossorigin src="/assets/index-0tCU_HqE.js"></script>
26
+ <script type="module" crossorigin src="/assets/index-Uwc-1pv0.js"></script>
27
27
  <link rel="stylesheet" crossorigin href="/assets/index-CIfVvUo9.css">
28
28
  </head>
29
29
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peerbit/server",
3
- "version": "5.10.13",
3
+ "version": "5.10.14-3f16953",
4
4
  "author": "dao.xyz",
5
5
  "repository": {
6
6
  "type": "git",
@@ -58,6 +58,8 @@
58
58
  ]
59
59
  },
60
60
  "devDependencies": {
61
+ "@peerbit/test-lib": "0.0.1-3f16953",
62
+ "@peerbit/test-utils": "2.3.19-3f16953",
61
63
  "@types/yargs": "17.0.24",
62
64
  "aws-sdk": "^2.1259.0",
63
65
  "dotenv": "^16.1.4",
@@ -65,13 +67,12 @@
65
67
  "@types/tar-stream": "^3.1.3",
66
68
  "shx": "^0.3.4",
67
69
  "@types/libsodium-wrappers": "^0.7.14",
68
- "uuid": "^10.0.0",
69
- "@peerbit/test-lib": "0.0.1",
70
- "@peerbit/test-utils": "2.3.18"
70
+ "uuid": "^10.0.0"
71
71
  },
72
72
  "dependencies": {
73
73
  "axios": "^1.4.0",
74
74
  "chalk": "^5.3.0",
75
+ "peerbit": "4.4.19-3f16953",
75
76
  "yargs": "^17.7.2",
76
77
  "tar-stream": "^3.1.7",
77
78
  "tmp": "^0.2.1",
@@ -82,19 +83,18 @@
82
83
  "@libp2p/tcp": "^11.0.2",
83
84
  "@libp2p/websockets": "^10.1.0",
84
85
  "@multiformats/multiaddr": "^13.0.1",
86
+ "@peerbit/blocks": "3.1.8-3f16953",
87
+ "@peerbit/crypto": "2.4.1-3f16953",
88
+ "@peerbit/program": "5.6.3-3f16953",
89
+ "@peerbit/pubsub": "4.1.4-3f16953",
90
+ "@peerbit/time": "2.3.0-3f16953",
85
91
  "@dao-xyz/borsh": "^6.0.0",
86
92
  "libsodium-wrappers": "0.7.15",
87
93
  "uint8arrays": "^5.1.0",
88
94
  "level": "^10.0.0",
89
95
  "memory-level": "^3.1.0",
90
96
  "multiformats": "^13.4.1",
91
- "abstract-level": "^3.1.0",
92
- "peerbit": "4.4.18",
93
- "@peerbit/blocks": "3.1.8",
94
- "@peerbit/crypto": "2.4.1",
95
- "@peerbit/program": "5.6.2",
96
- "@peerbit/pubsub": "4.1.4",
97
- "@peerbit/time": "2.3.0"
97
+ "abstract-level": "^3.1.0"
98
98
  },
99
99
  "optionalDependencies": {
100
100
  "@aws-sdk/client-ec2": "^3.390.0",
package/src/client.ts CHANGED
@@ -77,18 +77,31 @@ export const createClient = async (
77
77
 
78
78
  const throwIfNot200 = (resp: { status: number; data: any }) => {
79
79
  if (resp.status !== 200) {
80
- throw new Error(resp.data);
80
+ throw new Error(`HTTP ${resp.status}: ${resp.data ?? ""}`.trim());
81
81
  }
82
82
  return resp;
83
83
  };
84
84
 
85
+ const endpointUrl = (path: string) => new URL(path, endpoint).toString();
86
+
85
87
  const getId = async () =>
86
- throwIfNot200(
87
- await axiosInstance.get(endpoint + PEER_ID_PATH, {
88
- validateStatus,
89
- timeout: 5000,
90
- }),
91
- ).data;
88
+ (
89
+ await waitForResolved(
90
+ async () => {
91
+ const resp = await axiosInstance.get(endpointUrl(PEER_ID_PATH), {
92
+ validateStatus,
93
+ timeout: 5000,
94
+ });
95
+ return throwIfNot200(resp).data as string;
96
+ },
97
+ {
98
+ // Occasional CI flake: server can return 404 during very early startup
99
+ // or transient network hiccups; retry briefly.
100
+ timeout: 5000,
101
+ delayInterval: 50,
102
+ },
103
+ )
104
+ ).toString();
92
105
 
93
106
  const close = async (address: string) => {
94
107
  return throwIfNot200(
@@ -123,14 +136,17 @@ export const createClient = async (
123
136
  },
124
137
  addresses: {
125
138
  get: async () => {
126
- return (
127
- throwIfNot200(
128
- await axiosInstance.get(endpoint + ADDRESS_PATH, {
139
+ const addresses = await waitForResolved(
140
+ async () => {
141
+ const resp = await axiosInstance.get(endpointUrl(ADDRESS_PATH), {
129
142
  timeout: 5000,
130
143
  validateStatus,
131
- }),
132
- ).data as string[]
133
- ).map((x) => multiaddr(x));
144
+ });
145
+ return throwIfNot200(resp).data as string[];
146
+ },
147
+ { timeout: 5000, delayInterval: 50 },
148
+ );
149
+ return addresses.map((x) => multiaddr(x));
134
150
  },
135
151
  },
136
152
  stats: {
@@ -304,11 +320,17 @@ export const createClient = async (
304
320
  },
305
321
  },
306
322
  network: {
307
- bootstrap: async (): Promise<void> => {
323
+ bootstrap: async (options?: { addresses?: string[] }): Promise<void> => {
308
324
  throwIfNot200(
309
- await axiosInstance.post(endpoint + BOOTSTRAP_PATH, undefined, {
310
- validateStatus,
311
- }),
325
+ await axiosInstance.post(
326
+ endpoint + BOOTSTRAP_PATH,
327
+ options?.addresses
328
+ ? JSON.stringify({ addresses: options.addresses })
329
+ : undefined,
330
+ {
331
+ validateStatus,
332
+ },
333
+ ),
312
334
  );
313
335
  },
314
336
  },
package/src/peerbit.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { keys } from "@libp2p/crypto";
2
2
  import { DirectBlock } from "@peerbit/blocks";
3
3
  import type { Ed25519Keypair } from "@peerbit/crypto";
4
- import { DirectSub } from "@peerbit/pubsub";
4
+ import { FanoutTree, TopicControlPlane, TopicRootControlPlane } from "@peerbit/pubsub";
5
5
  import path from "path";
6
6
  import { Peerbit } from "peerbit";
7
7
  import { concat } from "uint8arrays";
@@ -14,6 +14,19 @@ export const create = (properties: {
14
14
  keypair: Ed25519Keypair;
15
15
  }) => {
16
16
  const listenPort = properties.listenPort ?? LIBP2P_LISTEN_PORT;
17
+ const topicRootControlPlane = new TopicRootControlPlane({
18
+ defaultCandidates: [properties.keypair.publicKey.hashcode()],
19
+ });
20
+ let fanoutInstance: FanoutTree | undefined;
21
+ const getOrCreateFanout = (c: any) => {
22
+ if (!fanoutInstance) {
23
+ fanoutInstance = new FanoutTree(c, {
24
+ connectionManager: false,
25
+ topicRootControlPlane,
26
+ });
27
+ }
28
+ return fanoutInstance;
29
+ };
17
30
  const blocksDirectory =
18
31
  properties.directory != null
19
32
  ? path.join(properties.directory, "/blocks").toString()
@@ -72,7 +85,14 @@ export const create = (properties: {
72
85
  directory: blocksDirectory,
73
86
  canRelayMessage: true,
74
87
  }),
75
- pubsub: (c) => new DirectSub(c, { canRelayMessage: true }),
88
+ pubsub: (c) =>
89
+ new TopicControlPlane(c, {
90
+ canRelayMessage: true,
91
+ topicRootControlPlane,
92
+ fanout: getOrCreateFanout(c),
93
+ hostShards: true,
94
+ }),
95
+ fanout: (c) => getOrCreateFanout(c),
76
96
  },
77
97
  },
78
98
  directory: properties.directory,
package/src/server.ts CHANGED
@@ -851,16 +851,26 @@ export const startApiServer = async (
851
851
  }
852
852
  } else if (req.url.startsWith(BOOTSTRAP_PATH)) {
853
853
  switch (req.method) {
854
- case "POST":
854
+ case "POST": {
855
855
  if (client instanceof Peerbit === false) {
856
856
  res.writeHead(400);
857
857
  res.end("Server node is not running a native client");
858
858
  return;
859
859
  }
860
- await (client as Peerbit).bootstrap();
860
+ let addresses: string[] | undefined;
861
+ try {
862
+ const parsed = body ? JSON.parse(body) : undefined;
863
+ if (Array.isArray(parsed?.addresses)) {
864
+ addresses = parsed.addresses;
865
+ }
866
+ } catch {
867
+ // ignore invalid bootstrap payload; fall back to default bootstrap list
868
+ }
869
+ await (client as Peerbit).bootstrap(addresses);
861
870
  res.writeHead(200);
862
871
  res.end();
863
872
  break;
873
+ }
864
874
 
865
875
  default:
866
876
  r404();