@peerbit/server 4.0.3 → 4.0.5

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/src/client.ts CHANGED
@@ -10,7 +10,7 @@ import {
10
10
  PROGRAM_PATH,
11
11
  RESTART_PATH,
12
12
  TRUST_PATH,
13
- REMOTE_API_PORT,
13
+ REMOTE_API_PORT
14
14
  } from "./routes.js";
15
15
  import { Address } from "@peerbit/program";
16
16
  import { multiaddr } from "@multiformats/multiaddr";
@@ -20,7 +20,7 @@ import {
20
20
  Ed25519PublicKey,
21
21
  Identity,
22
22
  PublicSignKey,
23
- getPublicKeyFromPeerId,
23
+ getPublicKeyFromPeerId
24
24
  } from "@peerbit/crypto";
25
25
  import { PeerId } from "@libp2p/interface/peer-id";
26
26
  import { waitForResolved } from "@peerbit/time";
@@ -29,7 +29,7 @@ import { RemoteOrigin } from "./remotes.js";
29
29
  export const createClient = async (
30
30
  keypair: Identity<Ed25519PublicKey>,
31
31
  remote: { address: string; origin?: RemoteOrigin } = {
32
- address: "http://localhost:" + LOCAL_API_PORT,
32
+ address: "http://localhost:" + LOCAL_API_PORT
33
33
  }
34
34
  ) => {
35
35
  // Add missing protocol
@@ -96,26 +96,26 @@ export const createClient = async (
96
96
  throwIfNot200(
97
97
  await axiosInstance.get(endpoint + PEER_ID_PATH, {
98
98
  validateStatus,
99
- timeout: 5000,
99
+ timeout: 5000
100
100
  })
101
101
  ).data;
102
102
 
103
103
  return {
104
104
  peer: {
105
105
  id: {
106
- get: getId,
106
+ get: getId
107
107
  },
108
108
  addresses: {
109
109
  get: async () => {
110
110
  return (
111
111
  throwIfNot200(
112
112
  await axiosInstance.get(endpoint + ADDRESS_PATH, {
113
- validateStatus,
113
+ validateStatus
114
114
  })
115
115
  ).data as string[]
116
116
  ).map((x) => multiaddr(x));
117
- },
118
- },
117
+ }
118
+ }
119
119
  },
120
120
 
121
121
  access: {
@@ -155,7 +155,7 @@ export const createClient = async (
155
155
  throw new Error(result.data);
156
156
  }
157
157
  return result.status === 200 ? true : false;
158
- },
158
+ }
159
159
  },
160
160
  program: {
161
161
  has: async (address: Address | string): Promise<boolean> => {
@@ -178,7 +178,7 @@ export const createClient = async (
178
178
  endpoint + PROGRAM_PATH,
179
179
  JSON.stringify(program),
180
180
  {
181
- validateStatus,
181
+ validateStatus
182
182
  }
183
183
  )
184
184
  );
@@ -193,7 +193,7 @@ export const createClient = async (
193
193
  "/" +
194
194
  encodeURIComponent(address.toString()),
195
195
  {
196
- validateStatus,
196
+ validateStatus
197
197
  }
198
198
  )
199
199
  );
@@ -208,7 +208,7 @@ export const createClient = async (
208
208
  encodeURIComponent(address.toString()) +
209
209
  "?delete=true",
210
210
  {
211
- validateStatus,
211
+ validateStatus
212
212
  }
213
213
  )
214
214
  );
@@ -217,11 +217,11 @@ export const createClient = async (
217
217
  list: async (): Promise<string[]> => {
218
218
  const resp = throwIfNot200(
219
219
  await axiosInstance.get(endpoint + PROGRAMS_PATH, {
220
- validateStatus,
220
+ validateStatus
221
221
  })
222
222
  );
223
223
  return resp.data as string[];
224
- },
224
+ }
225
225
  },
226
226
  dependency: {
227
227
  install: async (instruction: InstallDependency): Promise<string[]> => {
@@ -229,7 +229,7 @@ export const createClient = async (
229
229
  endpoint + INSTALL_PATH,
230
230
  JSON.stringify(instruction),
231
231
  {
232
- validateStatus,
232
+ validateStatus
233
233
  }
234
234
  );
235
235
  if (resp.status !== 200) {
@@ -238,29 +238,29 @@ export const createClient = async (
238
238
  );
239
239
  }
240
240
  return resp.data;
241
- },
241
+ }
242
242
  },
243
243
  network: {
244
244
  bootstrap: async (): Promise<void> => {
245
245
  throwIfNot200(
246
246
  await axiosInstance.post(endpoint + BOOTSTRAP_PATH, undefined, {
247
- validateStatus,
247
+ validateStatus
248
248
  })
249
249
  );
250
- },
250
+ }
251
251
  },
252
252
 
253
253
  restart: async (): Promise<void> => {
254
254
  throwIfNot200(
255
255
  await axiosInstance.post(endpoint + RESTART_PATH, undefined, {
256
- validateStatus,
256
+ validateStatus
257
257
  })
258
258
  );
259
259
  },
260
260
  stop: async (): Promise<void> => {
261
261
  throwIfNot200(
262
262
  await axiosInstance.post(endpoint + STOP_PATH, undefined, {
263
- validateStatus,
263
+ validateStatus
264
264
  })
265
265
  );
266
266
  },
@@ -269,10 +269,10 @@ export const createClient = async (
269
269
  if (remote.origin?.type === "aws") {
270
270
  await terminateNode({
271
271
  instanceId: remote.origin.instanceId,
272
- region: remote.origin.region,
272
+ region: remote.origin.region
273
273
  });
274
274
  }
275
- },
275
+ }
276
276
  };
277
277
  };
278
278
 
@@ -281,7 +281,7 @@ export const waitForDomain = async (
281
281
  timeout: number = 5 * 60 * 1000
282
282
  ): Promise<string> => {
283
283
  const c = await createClient(await Ed25519Keypair.create(), {
284
- address: "http://" + ip + ":" + LOCAL_API_PORT,
284
+ address: "http://" + ip + ":" + LOCAL_API_PORT
285
285
  });
286
286
  const result = await waitForResolved(
287
287
  async () => {
@@ -294,7 +294,7 @@ export const waitForDomain = async (
294
294
  },
295
295
  {
296
296
  delayInterval: 5000,
297
- timeout,
297
+ timeout
298
298
  }
299
299
  );
300
300
  if (!result) {
package/src/config.ts CHANGED
@@ -3,7 +3,8 @@ import os from "os";
3
3
  import fs from "fs";
4
4
  import { deserialize, serialize } from "@dao-xyz/borsh";
5
5
  import { Duplex } from "stream"; // Native Node Module
6
- import { Ed25519Keypair, Keypair, fromBase64 } from "@peerbit/crypto";
6
+ import { Ed25519Keypair } from "@peerbit/crypto";
7
+ import { dirname } from "path";
7
8
 
8
9
  const bufferToStream = (myBuffer) => {
9
10
  const tmp = new Duplex();
@@ -42,6 +43,7 @@ export const getKeypair = async (
42
43
  ): Promise<Ed25519Keypair> => {
43
44
  const keypath = getKeysPath(configDir);
44
45
  if (!fs.existsSync(keypath)) {
46
+ fs.mkdirSync(dirname(keypath), { recursive: true });
45
47
  const keypair = await Ed25519Keypair.create();
46
48
  fs.writeFileSync(keypath, serialize(keypair));
47
49
  return keypair;
package/src/docker.ts CHANGED
@@ -33,7 +33,7 @@ export const installDocker = async () => {
33
33
  try {
34
34
  await waitForAsync(() => dockerExist(), {
35
35
  timeout: 30 * 1000,
36
- delayInterval: 1000,
36
+ delayInterval: 1000
37
37
  });
38
38
  } catch (error) {
39
39
  throw new Error("Failed to install docker");
package/src/peerbit.ts CHANGED
@@ -24,29 +24,29 @@ export const create = (properties: {
24
24
  announce: properties.domain
25
25
  ? [
26
26
  `/dns4/${properties.domain}/tcp/4002`,
27
- `/dns4/${properties.domain}/tcp/4003/wss`,
27
+ `/dns4/${properties.domain}/tcp/4003/wss`
28
28
  ]
29
29
  : undefined,
30
30
  listen: [
31
31
  `/ip4/127.0.0.1/tcp/${listenPort}`,
32
32
  `/ip4/127.0.0.1/tcp/${
33
33
  listenPort !== 0 ? listenPort + 1 : listenPort
34
- }/ws`,
35
- ],
34
+ }/ws`
35
+ ]
36
36
  },
37
37
  connectionManager: {
38
38
  maxConnections: Infinity,
39
- minConnections: 0,
39
+ minConnections: 0
40
40
  },
41
41
  services: {
42
42
  blocks: (c) =>
43
43
  new DirectBlock(c, {
44
44
  directory: blocksDirectory,
45
- canRelayMessage: true,
45
+ canRelayMessage: true
46
46
  }),
47
- pubsub: (c) => new DirectSub(c, { canRelayMessage: true }),
48
- },
47
+ pubsub: (c) => new DirectSub(c, { canRelayMessage: true })
48
+ }
49
49
  },
50
- directory: properties.directory,
50
+ directory: properties.directory
51
51
  });
52
52
  };
package/src/remotes.ts CHANGED
@@ -31,7 +31,7 @@ export class Remotes {
31
31
  ) as RemotesObject;
32
32
  } else {
33
33
  this.data = {
34
- remotes: [],
34
+ remotes: []
35
35
  };
36
36
  }
37
37
  }
package/src/server.ts CHANGED
@@ -2,14 +2,14 @@ import http from "http";
2
2
  import {
3
3
  fromBase64,
4
4
  getKeypairFromPeerId,
5
- getPublicKeyFromPeerId,
5
+ getPublicKeyFromPeerId
6
6
  } from "@peerbit/crypto";
7
7
  import { deserialize } from "@dao-xyz/borsh";
8
8
  import {
9
9
  Program,
10
10
  ProgramClient,
11
11
  getProgramFromVariant,
12
- getProgramFromVariants,
12
+ getProgramFromVariants
13
13
  } from "@peerbit/program";
14
14
  import { waitFor } from "@peerbit/time";
15
15
  import { v4 as uuid } from "uuid";
@@ -17,7 +17,7 @@ import {
17
17
  getHomeConfigDir,
18
18
  getNodePath,
19
19
  getKeypair,
20
- getTrustPath,
20
+ getTrustPath
21
21
  } from "./config.js";
22
22
  import { setMaxListeners } from "events";
23
23
  import { create } from "./peerbit.js";
@@ -27,7 +27,7 @@ import {
27
27
  InstallDependency,
28
28
  StartByBase64,
29
29
  StartByVariant,
30
- StartProgram,
30
+ StartProgram
31
31
  } from "./types.js";
32
32
  import {
33
33
  ADDRESS_PATH,
@@ -39,7 +39,7 @@ import {
39
39
  PROGRAM_PATH,
40
40
  RESTART_PATH,
41
41
  TRUST_PATH,
42
- STOP_PATH,
42
+ STOP_PATH
43
43
  } from "./routes.js";
44
44
  import { Session } from "./session.js";
45
45
  import fs from "fs";
@@ -95,7 +95,7 @@ export const startServerWithNode = async (properties: {
95
95
  directory: getNodePath(properties.directory),
96
96
  domain: properties.domain,
97
97
  listenPort: properties.ports?.node,
98
- peerId: await keypair.toPeerId(),
98
+ peerId: await keypair.toPeerId()
99
99
  });
100
100
 
101
101
  if (properties.bootstrap) {
@@ -110,7 +110,7 @@ export const startServerWithNode = async (properties: {
110
110
  sessionDirectory
111
111
  ? new Level<string, Uint8Array>(sessionDirectory, {
112
112
  valueEncoding: "view",
113
- keyEncoding: "utf-8",
113
+ keyEncoding: "utf-8"
114
114
  })
115
115
  : new MemoryLevel({ valueEncoding: "view", keyEncoding: "utf-8" })
116
116
  );
@@ -134,7 +134,7 @@ export const startServerWithNode = async (properties: {
134
134
  const server = await startApiServer(peer, {
135
135
  port: properties.ports?.api,
136
136
  trust,
137
- session,
137
+ session
138
138
  });
139
139
  const printNodeInfo = async () => {
140
140
  console.log("Starting node with address(es): ");
@@ -240,13 +240,13 @@ export const startApiServer = async (
240
240
  process.argv.shift()!,
241
241
  [
242
242
  ...process.execArgv,
243
- ...process.argv.filter((x) => x !== "--reset" && x !== "-r"),
243
+ ...process.argv.filter((x) => x !== "--reset" && x !== "-r")
244
244
  ],
245
245
  {
246
246
  cwd: process.cwd(),
247
247
  detached: true,
248
248
  stdio: "inherit",
249
- gid: process.getgid!(),
249
+ gid: process.getgid!()
250
250
  }
251
251
  );
252
252
  process.exit(0);
@@ -450,7 +450,7 @@ export const startApiServer = async (
450
450
  name:
451
451
  base58btc.encode(Buffer.from(installName)) +
452
452
  uuid() +
453
- ".tgz",
453
+ ".tgz"
454
454
  });
455
455
  fs.writeFileSync(tempFile.fd, binary);
456
456
  clear = () => tempFile.removeCallback();
package/src/session.ts CHANGED
@@ -13,13 +13,13 @@ export class Session {
13
13
  this.imports = new KV(
14
14
  this.level.sublevel<string, Uint8Array>("imports", {
15
15
  keyEncoding: "utf8",
16
- valueEncoding: "view",
16
+ valueEncoding: "view"
17
17
  })
18
18
  );
19
19
  this.programs = new KV(
20
20
  this.level.sublevel<string, Uint8Array>("programs", {
21
21
  keyEncoding: "utf8",
22
- valueEncoding: "view",
22
+ valueEncoding: "view"
23
23
  })
24
24
  );
25
25
  }
@@ -5,7 +5,7 @@ import {
5
5
  fromBase64,
6
6
  toBase64,
7
7
  verify,
8
- Ed25519PublicKey,
8
+ Ed25519PublicKey
9
9
  } from "@peerbit/crypto";
10
10
  import { deserialize, serialize, BinaryWriter } from "@dao-xyz/borsh";
11
11
  import http from "http";