@peerbit/server 5.7.0 → 5.8.2-c30b40f
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/src/aws.d.ts +1 -1
- package/dist/src/aws.d.ts.map +1 -1
- package/dist/src/aws.js +5 -4
- package/dist/src/aws.js.map +1 -1
- package/dist/src/cli.d.ts.map +1 -1
- package/dist/src/cli.js +10 -10
- package/dist/src/cli.js.map +1 -1
- package/dist/src/client.d.ts +12 -0
- package/dist/src/client.d.ts.map +1 -1
- package/dist/src/client.js +9 -1
- package/dist/src/client.js.map +1 -1
- package/dist/src/domain.d.ts.map +1 -1
- package/dist/src/domain.js +5 -5
- package/dist/src/domain.js.map +1 -1
- package/dist/src/nginx-template.conf +0 -18
- package/dist/src/peerbit.d.ts.map +1 -1
- package/dist/src/peerbit.js +17 -6
- package/dist/src/peerbit.js.map +1 -1
- package/dist/src/routes.d.ts +1 -0
- package/dist/src/routes.d.ts.map +1 -1
- package/dist/src/routes.js +1 -0
- package/dist/src/routes.js.map +1 -1
- package/dist/src/server.d.ts +1 -2
- package/dist/src/server.d.ts.map +1 -1
- package/dist/src/server.js +31 -1
- package/dist/src/server.js.map +1 -1
- package/dist/test/api.spec.js.map +1 -1
- package/dist/test/domain.integration.spec.js +3 -6
- package/dist/test/domain.integration.spec.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/ui/assets/{index-DO8U-NKu.js → index-BQTJHTQs.js} +1 -1
- package/dist/ui/index.html +1 -1
- package/package.json +89 -89
- package/src/aws.ts +6 -5
- package/src/cli.ts +14 -15
- package/src/client.ts +17 -0
- package/src/domain.ts +8 -8
- package/src/nginx-template.conf +0 -18
- package/src/peerbit.ts +19 -8
- package/src/routes.ts +1 -0
- package/src/server.ts +37 -4
package/dist/ui/index.html
CHANGED
|
@@ -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-
|
|
26
|
+
<script type="module" crossorigin src="/assets/index-BQTJHTQs.js"></script>
|
|
27
27
|
<link rel="stylesheet" crossorigin href="/assets/index-CIfVvUo9.css">
|
|
28
28
|
</head>
|
|
29
29
|
|
package/package.json
CHANGED
|
@@ -1,91 +1,91 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
2
|
+
"name": "@peerbit/server",
|
|
3
|
+
"version": "5.8.2-c30b40f",
|
|
4
|
+
"author": "dao.xyz",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/dao-xyz/peerbit"
|
|
8
|
+
},
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"bin": {
|
|
13
|
+
"peerbit": "./dist/src/bin.js"
|
|
14
|
+
},
|
|
15
|
+
"module": "dist/src/index.js",
|
|
16
|
+
"types": "dist/src/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
"import": "./dist/src/index.js"
|
|
19
|
+
},
|
|
20
|
+
"browser": {
|
|
21
|
+
"./dist/src/server.js": "./dist/src/server.browser.js",
|
|
22
|
+
"./server.js": "./dist/src/server.browser.js",
|
|
23
|
+
"./dist/src/config.js": "./dist/src/config.browser.js",
|
|
24
|
+
"./config.js": "./dist/src/config.browser.js",
|
|
25
|
+
"./dist/src/remotes.js": "./dist/src/remotes.browser.js",
|
|
26
|
+
"./remotes.js": "./dist/src/remotes.browser.js",
|
|
27
|
+
"./dist/src/docker.js": "./dist/src/docker.browser.js",
|
|
28
|
+
"./docker.js": "./dist/src/docker.browser.js",
|
|
29
|
+
"./dist/src/aws.js": "./dist/src/aws.browser.js"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"src",
|
|
34
|
+
"src/nginx-template.conf",
|
|
35
|
+
"!src/test",
|
|
36
|
+
"!src/**/__tests__",
|
|
37
|
+
"!lib/**/__tests__",
|
|
38
|
+
"patches",
|
|
39
|
+
"LICENSE"
|
|
40
|
+
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18"
|
|
46
|
+
},
|
|
47
|
+
"eslintConfig": {
|
|
48
|
+
"extends": "peerbit",
|
|
49
|
+
"parserOptions": {
|
|
50
|
+
"project": true,
|
|
51
|
+
"sourceType": "module"
|
|
52
|
+
},
|
|
53
|
+
"ignorePatterns": [
|
|
54
|
+
"!.aegir.js",
|
|
55
|
+
"test/ts-use",
|
|
56
|
+
"*.d.ts"
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"clean": "shx rm -rf lib/*",
|
|
61
|
+
"build": "yarn clean && yarn build-lib && yarn build-ui",
|
|
62
|
+
"build-lib": "tsc -p tsconfig.json",
|
|
63
|
+
"build-ui": "cd ../frontend && yarn build && cd ../node",
|
|
64
|
+
"postbuild": "cp src/nginx-template.conf dist/src/ && cp -r ../frontend/dist/. dist/ui",
|
|
65
|
+
"test": "aegir test --t node",
|
|
66
|
+
"lint": "aegir lint"
|
|
67
|
+
},
|
|
68
|
+
"devDependencies": {
|
|
69
|
+
"@peerbit/test-lib": "0.0.1-c30b40f",
|
|
70
|
+
"@peerbit/test-utils": "2.2.1-c30b40f",
|
|
71
|
+
"@types/yargs": "17.0.24",
|
|
72
|
+
"aws-sdk": "^2.1259.0",
|
|
73
|
+
"dotenv": "^16.1.4",
|
|
74
|
+
"@types/tmp": "^0.2.3",
|
|
75
|
+
"@types/tar-stream": "^3.1.3",
|
|
76
|
+
"shx": "^0.3.4"
|
|
77
|
+
},
|
|
78
|
+
"dependencies": {
|
|
79
|
+
"axios": "^1.4.0",
|
|
80
|
+
"chalk": "^5.3.0",
|
|
81
|
+
"peerbit": "4.3.0-c30b40f",
|
|
82
|
+
"yargs": "^17.7.2",
|
|
83
|
+
"tar-stream": "^3.1.7",
|
|
84
|
+
"tmp": "^0.2.1",
|
|
85
|
+
"tty-table": "^4.2.1"
|
|
86
|
+
},
|
|
87
|
+
"optionalDependencies": {
|
|
88
|
+
"@aws-sdk/client-ec2": "^3.390.0",
|
|
89
|
+
"@aws-sdk/client-route-53": "^3.391.0"
|
|
90
|
+
}
|
|
91
91
|
}
|
package/src/aws.ts
CHANGED
|
@@ -52,17 +52,18 @@ export const createRecord = async (options: {
|
|
|
52
52
|
await client.send(cmd);
|
|
53
53
|
};
|
|
54
54
|
|
|
55
|
-
const setupUserData = (
|
|
55
|
+
const setupUserData = (grantAccess: PeerId[] = [], serverVersion?: string) => {
|
|
56
56
|
const peerIdStrings = grantAccess.map((x) => x.toString());
|
|
57
57
|
|
|
58
58
|
// better-sqlite3 force use to install build-essentials for `make` command, TOOD dont bundle better-sqlite3 by default?
|
|
59
|
+
const versionSpec = serverVersion ? `@${serverVersion}` : "";
|
|
59
60
|
return `#!/bin/bash
|
|
60
61
|
cd /home/ubuntu
|
|
61
62
|
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash - &&\
|
|
62
63
|
sudo apt-get install -y nodejs
|
|
63
64
|
sudo apt-get install -y build-essential
|
|
64
|
-
npm install -g @peerbit/server
|
|
65
|
-
sudo peerbit domain test
|
|
65
|
+
npm install -g @peerbit/server${versionSpec}
|
|
66
|
+
sudo peerbit domain test
|
|
66
67
|
peerbit start ${peerIdStrings.map((key) => `--ga ${key}`)} > log.txt 2>&1 &
|
|
67
68
|
`;
|
|
68
69
|
};
|
|
@@ -105,11 +106,11 @@ export const AWS_LINUX_ARM_AMIs: Record<string, string> = {
|
|
|
105
106
|
};
|
|
106
107
|
export const launchNodes = async (properties: {
|
|
107
108
|
region?: string;
|
|
108
|
-
email: string;
|
|
109
109
|
count?: number;
|
|
110
110
|
size?: "micro" | "small" | "medium" | "large" | "xlarge" | "2xlarge";
|
|
111
111
|
namePrefix?: string;
|
|
112
112
|
grantAccess?: PeerId[];
|
|
113
|
+
serverVersion?: string;
|
|
113
114
|
}): Promise<
|
|
114
115
|
{ instanceId: string; publicIp: string; name: string; region: string }[]
|
|
115
116
|
> => {
|
|
@@ -218,7 +219,7 @@ export const launchNodes = async (properties: {
|
|
|
218
219
|
SecurityGroupIds: [securityGroupOut.GroupId!],
|
|
219
220
|
InstanceType: ("t4g." + (properties.size || "micro")) as any, // TODO types
|
|
220
221
|
UserData: Buffer.from(
|
|
221
|
-
setupUserData(properties.
|
|
222
|
+
setupUserData(properties.grantAccess, properties.serverVersion),
|
|
222
223
|
).toString("base64"),
|
|
223
224
|
MinCount: count,
|
|
224
225
|
MaxCount: count,
|
package/src/cli.ts
CHANGED
|
@@ -219,7 +219,7 @@ export const cli = async (args?: string[]) => {
|
|
|
219
219
|
"Setup a testing domain with SSL (no guarantess on how long the domain will be available)",
|
|
220
220
|
builder: {
|
|
221
221
|
email: {
|
|
222
|
-
describe: "Email for Lets
|
|
222
|
+
describe: "Email for Lets security messages",
|
|
223
223
|
type: "string",
|
|
224
224
|
demandOption: true,
|
|
225
225
|
},
|
|
@@ -273,11 +273,6 @@ export const cli = async (args?: string[]) => {
|
|
|
273
273
|
alias: "sk",
|
|
274
274
|
type: "string",
|
|
275
275
|
},
|
|
276
|
-
email: {
|
|
277
|
-
describe: "Email for Lets encrypt auto-renewal messages",
|
|
278
|
-
type: "string",
|
|
279
|
-
demandOption: true,
|
|
280
|
-
},
|
|
281
276
|
outdir: {
|
|
282
277
|
describe: "Output path for Nginx config",
|
|
283
278
|
type: "string",
|
|
@@ -310,12 +305,7 @@ export const cli = async (args?: string[]) => {
|
|
|
310
305
|
}
|
|
311
306
|
: undefined,
|
|
312
307
|
});
|
|
313
|
-
await startCertbot(
|
|
314
|
-
args.domain,
|
|
315
|
-
args.email,
|
|
316
|
-
args.outdir,
|
|
317
|
-
args.wait,
|
|
318
|
-
);
|
|
308
|
+
await startCertbot(args.domain, args.email, args.wait);
|
|
319
309
|
exit();
|
|
320
310
|
},
|
|
321
311
|
})
|
|
@@ -386,24 +376,33 @@ export const cli = async (args?: string[]) => {
|
|
|
386
376
|
alias: "d",
|
|
387
377
|
default: getHomeConfigDir(),
|
|
388
378
|
});
|
|
379
|
+
awsArgs.option("server-version", {
|
|
380
|
+
describe:
|
|
381
|
+
"@peerbit/server version or tag to install on the instance (e.g. 5.7.0-58d3d09)",
|
|
382
|
+
type: "string",
|
|
383
|
+
alias: ["sv"],
|
|
384
|
+
});
|
|
389
385
|
return awsArgs;
|
|
390
386
|
},
|
|
391
387
|
handler: async (args) => {
|
|
392
388
|
const accessGrant: PeerId[] =
|
|
393
|
-
args
|
|
394
|
-
? args
|
|
389
|
+
args["grant-access"]?.length > 0
|
|
390
|
+
? (args["grant-access"] as string[]).map((x) =>
|
|
391
|
+
peerIdFromString(x),
|
|
392
|
+
)
|
|
395
393
|
: [
|
|
396
394
|
await (
|
|
397
395
|
await getKeypair(args.directory)
|
|
398
396
|
).publicKey.toPeerId(),
|
|
399
397
|
];
|
|
400
398
|
const nodes = await launchNodes({
|
|
401
|
-
email: "marcus@dao.xyz",
|
|
402
399
|
count: args.count,
|
|
403
400
|
namePrefix: args.name,
|
|
404
401
|
region: args.region,
|
|
405
402
|
grantAccess: accessGrant,
|
|
406
403
|
size: args.size,
|
|
404
|
+
serverVersion:
|
|
405
|
+
(args["server-version"] as string) || undefined,
|
|
407
406
|
});
|
|
408
407
|
|
|
409
408
|
console.log(
|
package/src/client.ts
CHANGED
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
REMOTE_API_PORT,
|
|
24
24
|
RESTART_PATH,
|
|
25
25
|
SELF_UPDATE_PATH,
|
|
26
|
+
STATS_PATH,
|
|
26
27
|
STOP_PATH,
|
|
27
28
|
TRUST_PATH,
|
|
28
29
|
VERSIONS_PATH,
|
|
@@ -132,6 +133,22 @@ export const createClient = async (
|
|
|
132
133
|
).map((x) => multiaddr(x));
|
|
133
134
|
},
|
|
134
135
|
},
|
|
136
|
+
stats: {
|
|
137
|
+
get: async (): Promise<{
|
|
138
|
+
connections: { total: number; inbound: number; outbound: number };
|
|
139
|
+
dialQueue: { pending: number };
|
|
140
|
+
}> => {
|
|
141
|
+
const resp = throwIfNot200(
|
|
142
|
+
await axiosInstance.get(endpoint + STATS_PATH, {
|
|
143
|
+
validateStatus,
|
|
144
|
+
}),
|
|
145
|
+
);
|
|
146
|
+
return resp.data as {
|
|
147
|
+
connections: { total: number; inbound: number; outbound: number };
|
|
148
|
+
dialQueue: { pending: number };
|
|
149
|
+
};
|
|
150
|
+
},
|
|
151
|
+
},
|
|
135
152
|
},
|
|
136
153
|
|
|
137
154
|
access: {
|
package/src/domain.ts
CHANGED
|
@@ -3,14 +3,6 @@ import { waitFor } from "@peerbit/time";
|
|
|
3
3
|
|
|
4
4
|
const isNode = typeof process !== "undefined" && process.versions?.node != null;
|
|
5
5
|
|
|
6
|
-
const validateEmail = (email: any) => {
|
|
7
|
-
return String(email)
|
|
8
|
-
.toLowerCase()
|
|
9
|
-
.match(
|
|
10
|
-
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
|
|
11
|
-
);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
6
|
const getConfigFileTemplate = async (): Promise<string> => {
|
|
15
7
|
const url = await import("url");
|
|
16
8
|
const filename = url.fileURLToPath(import.meta.url);
|
|
@@ -129,6 +121,14 @@ export const createTestDomain = async () => {
|
|
|
129
121
|
return domain;
|
|
130
122
|
};
|
|
131
123
|
|
|
124
|
+
const validateEmail = (email: any) => {
|
|
125
|
+
return String(email)
|
|
126
|
+
.toLowerCase()
|
|
127
|
+
.match(
|
|
128
|
+
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
|
|
129
|
+
);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
132
|
export const startCertbot = async (
|
|
133
133
|
domain: string,
|
|
134
134
|
email: string,
|
package/src/nginx-template.conf
CHANGED
|
@@ -1,23 +1,5 @@
|
|
|
1
1
|
server_names_hash_bucket_size 128;
|
|
2
2
|
|
|
3
|
-
# --- stream (TCP/UDP) layer ---
|
|
4
|
-
stream {
|
|
5
|
-
# libp2p TCP forward: /dns4/%DOMAIN%/tcp/4002 -> 127.0.0.1:8001
|
|
6
|
-
server {
|
|
7
|
-
listen 4002;
|
|
8
|
-
proxy_connect_timeout 10s;
|
|
9
|
-
proxy_timeout 1h;
|
|
10
|
-
proxy_pass 127.0.0.1:8001;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
# webrtc-direct UDP forward: /dns4/%DOMAIN%/udp/4004/webrtc-direct -> 127.0.0.1:8004
|
|
14
|
-
server {
|
|
15
|
-
listen 4003 udp;
|
|
16
|
-
proxy_timeout 1h;
|
|
17
|
-
proxy_pass 127.0.0.1:8003;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
3
|
server {
|
|
22
4
|
server_name %DOMAIN%;
|
|
23
5
|
listen [::]:4003 ssl ipv6only=on;
|
package/src/peerbit.ts
CHANGED
|
@@ -33,16 +33,27 @@ export const create = (properties: {
|
|
|
33
33
|
? [
|
|
34
34
|
`/dns4/${properties.domain}/tcp/4002`,
|
|
35
35
|
`/dns4/${properties.domain}/tcp/4003/wss`,
|
|
36
|
-
`/dns4/${properties.domain}/
|
|
36
|
+
`/dns4/${properties.domain}/udp/4004/webrtc-direct`,
|
|
37
37
|
]
|
|
38
38
|
: undefined,
|
|
39
|
-
listen:
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
listen: (() => {
|
|
40
|
+
if (properties.domain) {
|
|
41
|
+
// Direct binds on public ports, keep only local WS for nginx TLS->WS proxy
|
|
42
|
+
return [
|
|
43
|
+
`/ip4/0.0.0.0/tcp/4002`,
|
|
44
|
+
`/ip4/127.0.0.1/tcp/${
|
|
45
|
+
listenPort !== 0 ? listenPort + 1 : listenPort
|
|
46
|
+
}/ws`,
|
|
47
|
+
`/ip4/0.0.0.0/udp/4004/webrtc-direct`,
|
|
48
|
+
];
|
|
49
|
+
}
|
|
50
|
+
// Local-only defaults (no domain)
|
|
51
|
+
return [
|
|
52
|
+
`/ip4/127.0.0.1/tcp/${listenPort}`,
|
|
53
|
+
`/ip4/127.0.0.1/tcp/${listenPort !== 0 ? listenPort + 1 : listenPort}/ws`,
|
|
54
|
+
`/ip4/127.0.0.1/udp/${listenPort !== 0 ? listenPort + 2 : listenPort}/webrtc-direct`,
|
|
55
|
+
];
|
|
56
|
+
})(),
|
|
46
57
|
},
|
|
47
58
|
connectionMonitor: {
|
|
48
59
|
abortConnectionOnPingFailure: false,
|
package/src/routes.ts
CHANGED
|
@@ -14,6 +14,7 @@ export const LOCAL_API_PORT = 8082;
|
|
|
14
14
|
export const TRUST_PATH = "/trust";
|
|
15
15
|
export const PEER_ID_PATH = "/peer/id";
|
|
16
16
|
export const ADDRESS_PATH = "/peer/address";
|
|
17
|
+
export const STATS_PATH = "/peer/stats";
|
|
17
18
|
export const PROGRAM_PATH = "/program";
|
|
18
19
|
export const PROGRAMS_PATH = "/programs";
|
|
19
20
|
export const PROGRAM_VARIANTS_PATH = "/program/variants";
|
package/src/server.ts
CHANGED
|
@@ -4,7 +4,6 @@ import { peerIdFromString } from "@libp2p/peer-id";
|
|
|
4
4
|
import { fromBase64, getPublicKeyFromPeerId } from "@peerbit/crypto";
|
|
5
5
|
import {
|
|
6
6
|
Program,
|
|
7
|
-
type ProgramClient,
|
|
8
7
|
getProgramFromVariant,
|
|
9
8
|
getProgramFromVariants,
|
|
10
9
|
} from "@peerbit/program";
|
|
@@ -36,6 +35,7 @@ import {
|
|
|
36
35
|
PROGRAM_VARIANTS_PATH,
|
|
37
36
|
RESTART_PATH,
|
|
38
37
|
SELF_UPDATE_PATH,
|
|
38
|
+
STATS_PATH,
|
|
39
39
|
STOP_PATH,
|
|
40
40
|
TRUST_PATH,
|
|
41
41
|
VERSIONS_PATH,
|
|
@@ -268,7 +268,7 @@ function findPeerbitProgramFolder(inputDirectory: string): string | null {
|
|
|
268
268
|
}
|
|
269
269
|
|
|
270
270
|
export const startApiServer = async (
|
|
271
|
-
client:
|
|
271
|
+
client: Peerbit,
|
|
272
272
|
properties: {
|
|
273
273
|
trust: Trust;
|
|
274
274
|
session?: Session;
|
|
@@ -332,7 +332,7 @@ export const startApiServer = async (
|
|
|
332
332
|
|
|
333
333
|
const e404 = "404";
|
|
334
334
|
|
|
335
|
-
const endpoints = (client:
|
|
335
|
+
const endpoints = (client: Peerbit): http.RequestListener => {
|
|
336
336
|
return async (req, res) => {
|
|
337
337
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
338
338
|
res.setHeader("Access-Control-Request-Method", "*");
|
|
@@ -887,12 +887,45 @@ export const startApiServer = async (
|
|
|
887
887
|
r404();
|
|
888
888
|
break;
|
|
889
889
|
}
|
|
890
|
-
} */ else if (req.url.startsWith(
|
|
890
|
+
} */ else if (req.url.startsWith(STATS_PATH)) {
|
|
891
|
+
if (req.method === "GET") {
|
|
892
|
+
try {
|
|
893
|
+
const connections = client.libp2p.getConnections();
|
|
894
|
+
const inbound = connections.filter(
|
|
895
|
+
(c) => c.direction === "inbound",
|
|
896
|
+
).length;
|
|
897
|
+
const outbound = connections.filter(
|
|
898
|
+
(c) => c.direction === "outbound",
|
|
899
|
+
).length;
|
|
900
|
+
const dialQueue = client.libp2p.getDialQueue();
|
|
901
|
+
|
|
902
|
+
const payload = {
|
|
903
|
+
connections: {
|
|
904
|
+
total: connections.length,
|
|
905
|
+
inbound,
|
|
906
|
+
outbound,
|
|
907
|
+
},
|
|
908
|
+
dialQueue: {
|
|
909
|
+
pending: dialQueue.length,
|
|
910
|
+
},
|
|
911
|
+
};
|
|
912
|
+
res.setHeader("Content-Type", "application/json");
|
|
913
|
+
res.writeHead(200);
|
|
914
|
+
res.end(JSON.stringify(payload));
|
|
915
|
+
} catch (error: any) {
|
|
916
|
+
res.writeHead(500);
|
|
917
|
+
res.end(error?.message || "Failed to compute stats");
|
|
918
|
+
}
|
|
919
|
+
} else {
|
|
920
|
+
r404();
|
|
921
|
+
}
|
|
922
|
+
} else if (req.url.startsWith(PEER_ID_PATH)) {
|
|
891
923
|
res.writeHead(200);
|
|
892
924
|
res.end(client.peerId.toString());
|
|
893
925
|
} else if (req.url.startsWith(ADDRESS_PATH)) {
|
|
894
926
|
res.setHeader("Content-Type", "application/json");
|
|
895
927
|
res.writeHead(200);
|
|
928
|
+
|
|
896
929
|
const addresses = client.getMultiaddrs().map((x) => x.toString());
|
|
897
930
|
res.end(JSON.stringify(addresses));
|
|
898
931
|
} else {
|