@kronos-integration/service-swarm 1.9.86 → 1.9.87
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/LICENSE +12 -23
- package/README.md +23 -26
- package/package.json +19 -19
- package/send.mjs +24 -10
- package/src/peers-endpoint.mjs +3 -9
- package/src/service-swarm.mjs +70 -87
- package/src/topic-endpoint.mjs +7 -12
- package/src/topic.mjs +21 -20
- package/update.sh +0 -16
package/LICENSE
CHANGED
|
@@ -1,23 +1,12 @@
|
|
|
1
|
-
Copyright (
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
15
|
-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
16
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
17
|
-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
18
|
-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
19
|
-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
20
|
-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
21
|
-
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
22
|
-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
23
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
1
|
+
Copyright (C) 2020-2025 by arlac77
|
|
2
|
+
|
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
4
|
+
purpose with or without fee is hereby granted.
|
|
5
|
+
|
|
6
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
7
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
8
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
9
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
10
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
11
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
12
|
+
PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
[](https://opensource.org/licenses/BSD-3-Clause)
|
|
3
|
-
[](https://bundlephobia.com/result?p=@kronos-integration/service-swarm)
|
|
4
|
-
[](https://npmjs.org/package/@kronos-integration/service-swarm)
|
|
1
|
+
[](https://spdx.org/licenses/0BSD.html)
|
|
5
2
|
[](https://github.com/Kronos-Integration/service-swarm/issues)
|
|
6
3
|
[](https://actions-badge.atrox.dev/Kronos-Integration/service-swarm/goto)
|
|
7
4
|
[](https://github.com/prettier/prettier)
|
|
@@ -19,7 +16,7 @@ Manage a set of remote services
|
|
|
19
16
|
|
|
20
17
|
### Table of Contents
|
|
21
18
|
|
|
22
|
-
* [
|
|
19
|
+
* [PEERS\_NAME\_PREFIX](#peers_name_prefix)
|
|
23
20
|
* [PeersEndpoint](#peersendpoint)
|
|
24
21
|
* [Parameters](#parameters)
|
|
25
22
|
* [Properties](#properties)
|
|
@@ -27,7 +24,7 @@ Manage a set of remote services
|
|
|
27
24
|
* [endpointFactoryFromConfig](#endpointfactoryfromconfig)
|
|
28
25
|
* [Parameters](#parameters-1)
|
|
29
26
|
* [name](#name)
|
|
30
|
-
* [
|
|
27
|
+
* [TOPIC\_NAME\_PREFIX](#topic_name_prefix)
|
|
31
28
|
* [TopicEndpoint](#topicendpoint)
|
|
32
29
|
* [Parameters](#parameters-2)
|
|
33
30
|
* [Properties](#properties-1)
|
|
@@ -35,7 +32,7 @@ Manage a set of remote services
|
|
|
35
32
|
* [Parameters](#parameters-3)
|
|
36
33
|
* [Properties](#properties-2)
|
|
37
34
|
|
|
38
|
-
##
|
|
35
|
+
## PEERS\_NAME\_PREFIX
|
|
39
36
|
|
|
40
37
|
Endpoint name prefix for peers endpoints.
|
|
41
38
|
|
|
@@ -51,13 +48,13 @@ Endpoint to link against a swarm topic.
|
|
|
51
48
|
|
|
52
49
|
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** endpoint name
|
|
53
50
|
* `owner` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** owner of the endpoint
|
|
54
|
-
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
51
|
+
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
|
|
55
52
|
|
|
56
|
-
* `options.topic` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** defaults to endpoint name (without @see
|
|
53
|
+
* `options.topic` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** defaults to endpoint name (without @see PEERS\_NAME\_PREFIX)
|
|
57
54
|
|
|
58
55
|
### Properties
|
|
59
56
|
|
|
60
|
-
* `topic` **[Topic](#topic)
|
|
57
|
+
* `topic` **[Topic](#topic)** 
|
|
61
58
|
|
|
62
59
|
## ServiceSwarm
|
|
63
60
|
|
|
@@ -71,9 +68,9 @@ On demand create topic endpoints.
|
|
|
71
68
|
|
|
72
69
|
#### Parameters
|
|
73
70
|
|
|
74
|
-
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)
|
|
75
|
-
* `definition` **([Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))
|
|
76
|
-
* `ic`
|
|
71
|
+
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
|
|
72
|
+
* `definition` **([Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))** 
|
|
73
|
+
* `ic`  
|
|
77
74
|
|
|
78
75
|
Returns **Class** TopicEndpoint if name starts with 'topic.'
|
|
79
76
|
|
|
@@ -81,7 +78,7 @@ Returns **Class** TopicEndpoint if name starts with 'topic.'
|
|
|
81
78
|
|
|
82
79
|
Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 'swarm'
|
|
83
80
|
|
|
84
|
-
##
|
|
81
|
+
## TOPIC\_NAME\_PREFIX
|
|
85
82
|
|
|
86
83
|
Endpoint name prefix for topic endpoints.
|
|
87
84
|
|
|
@@ -97,30 +94,30 @@ Endpoint to link against a swarm topic.
|
|
|
97
94
|
|
|
98
95
|
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** endpoint name
|
|
99
96
|
* `owner` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** owner of the endpoint
|
|
100
|
-
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
97
|
+
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
|
|
101
98
|
|
|
102
|
-
* `options.topic` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** defaults to endpoint name (without @see
|
|
99
|
+
* `options.topic` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** defaults to endpoint name (without @see TOPIC\_NAME\_PREFIX)
|
|
103
100
|
|
|
104
101
|
### Properties
|
|
105
102
|
|
|
106
|
-
* `topic` **[Topic](#topic)
|
|
103
|
+
* `topic` **[Topic](#topic)** 
|
|
107
104
|
|
|
108
105
|
## Topic
|
|
109
106
|
|
|
110
107
|
### Parameters
|
|
111
108
|
|
|
112
|
-
* `service` **[ServiceSwarm](#serviceswarm)
|
|
113
|
-
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)
|
|
114
|
-
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
109
|
+
* `service` **[ServiceSwarm](#serviceswarm)** 
|
|
110
|
+
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
|
|
111
|
+
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
|
|
115
112
|
|
|
116
113
|
### Properties
|
|
117
114
|
|
|
118
|
-
* `service` **[ServiceSwarm](#serviceswarm)
|
|
119
|
-
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)
|
|
120
|
-
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
121
|
-
* `key` **[Buffer](https://nodejs.org/api/buffer.html)
|
|
122
|
-
* `topicEndpoints` **[Set](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)\<TopicEndppoint
|
|
123
|
-
* `peerEndpoints` **[Set](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)\<PeerEndpoint
|
|
115
|
+
* `service` **[ServiceSwarm](#serviceswarm)** 
|
|
116
|
+
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
|
|
117
|
+
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
|
|
118
|
+
* `key` **[Buffer](https://nodejs.org/api/buffer.html)** 
|
|
119
|
+
* `topicEndpoints` **[Set](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)\<TopicEndppoint>** 
|
|
120
|
+
* `peerEndpoints` **[Set](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)\<PeerEndpoint>** 
|
|
124
121
|
|
|
125
122
|
# install
|
|
126
123
|
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kronos-integration/service-swarm",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.87",
|
|
4
4
|
"publishConfig": {
|
|
5
|
-
"access": "public"
|
|
5
|
+
"access": "public",
|
|
6
|
+
"provenance": true
|
|
6
7
|
},
|
|
7
|
-
"sideEffects": false,
|
|
8
8
|
"exports": {
|
|
9
9
|
".": "./src/service-swarm.mjs"
|
|
10
10
|
},
|
|
@@ -22,34 +22,34 @@
|
|
|
22
22
|
],
|
|
23
23
|
"license": "BSD-2-Clause",
|
|
24
24
|
"scripts": {
|
|
25
|
-
"test": "
|
|
26
|
-
"test:ava": "ava --timeout
|
|
27
|
-
"cover": "c8 -x 'tests/**/*' --temp-directory build/tmp ava --timeout
|
|
25
|
+
"test": "node --run test:ava",
|
|
26
|
+
"test:ava": "ava --timeout 4m tests/*-ava.mjs tests/*-ava-node.mjs",
|
|
27
|
+
"cover": "c8 -x 'tests/**/*' --temp-directory build/tmp ava --timeout 4m tests/*-ava.mjs tests/*-ava-node.mjs && c8 report -r lcov -o build/coverage --temp-directory build/tmp",
|
|
28
28
|
"docs": "documentation readme --section=API ./src/**/*.mjs",
|
|
29
|
-
"lint": "
|
|
29
|
+
"lint": "node --run lint:docs",
|
|
30
30
|
"lint:docs": "documentation lint ./src/**/*.mjs"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@kronos-integration/endpoint": "^9.
|
|
34
|
-
"@kronos-integration/service": "^
|
|
35
|
-
"hyperswarm": "^
|
|
36
|
-
"length-prefix-framed-stream": "^2.0.
|
|
37
|
-
"model-attributes": "^4.
|
|
33
|
+
"@kronos-integration/endpoint": "^9.5.7",
|
|
34
|
+
"@kronos-integration/service": "^11.2.8",
|
|
35
|
+
"hyperswarm": "^4.11.7",
|
|
36
|
+
"length-prefix-framed-stream": "^2.0.12",
|
|
37
|
+
"model-attributes": "^4.2.4"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@hyperswarm/dht": "^
|
|
41
|
-
"ava": "^3.
|
|
42
|
-
"c8": "^
|
|
43
|
-
"documentation": "^
|
|
40
|
+
"@hyperswarm/dht": "^6.5.1",
|
|
41
|
+
"ava": "^6.3.0",
|
|
42
|
+
"c8": "^10.1.3",
|
|
43
|
+
"documentation": "^14.0.3",
|
|
44
44
|
"nonsynchronous": "^1.2.0",
|
|
45
|
-
"semantic-release": "^
|
|
45
|
+
"semantic-release": "^24.2.4"
|
|
46
46
|
},
|
|
47
47
|
"engines": {
|
|
48
|
-
"node": ">=
|
|
48
|
+
"node": ">=22.16.0"
|
|
49
49
|
},
|
|
50
50
|
"repository": {
|
|
51
51
|
"type": "git",
|
|
52
|
-
"url": "https://github.com/Kronos-Integration/service-swarm.git"
|
|
52
|
+
"url": "git+https://github.com/Kronos-Integration/service-swarm.git"
|
|
53
53
|
},
|
|
54
54
|
"bugs": {
|
|
55
55
|
"url": "https://github.com/Kronos-Integration/service-swarm/issues"
|
package/send.mjs
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
import { createHash } from "crypto";
|
|
1
|
+
#!/usr/bin/env node
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
import Hyperswarm from "hyperswarm";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
5
|
|
|
6
|
-
const
|
|
6
|
+
const isServer = process.argv.at(-1) === "server";
|
|
7
7
|
|
|
8
|
-
swarm
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
const swarm = new Hyperswarm();
|
|
9
|
+
|
|
10
|
+
const topic = createHash("sha256")
|
|
11
|
+
.update("sdlfjksdfjdflk56kj5jk5jk54lk6sdcfffmgdfklf")
|
|
12
|
+
.digest();
|
|
13
|
+
|
|
14
|
+
const discovery = swarm.join(
|
|
15
|
+
topic,
|
|
16
|
+
isServer
|
|
17
|
+
? {
|
|
18
|
+
server: true,
|
|
19
|
+
client: false
|
|
20
|
+
}
|
|
21
|
+
: { server: false, client: true }
|
|
22
|
+
);
|
|
14
23
|
|
|
15
24
|
swarm.on("connection", (socket, info) => {
|
|
16
25
|
console.log("new connection!", info);
|
|
@@ -18,3 +27,8 @@ swarm.on("connection", (socket, info) => {
|
|
|
18
27
|
// you can now use the socket as a stream, eg:
|
|
19
28
|
process.stdin.pipe(socket).pipe(process.stdout);
|
|
20
29
|
});
|
|
30
|
+
|
|
31
|
+
await discovery.flushed();
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
console.log("flushed",isServer);
|
package/src/peers-endpoint.mjs
CHANGED
|
@@ -19,18 +19,12 @@ export class PeersEndpoint extends MultiSendEndpoint {
|
|
|
19
19
|
return name.startsWith(PEERS_NAME_PREFIX);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
constructor(name, owner, options
|
|
22
|
+
constructor(name, owner, options) {
|
|
23
23
|
super(name, owner, options);
|
|
24
24
|
|
|
25
|
-
const topicName = options.
|
|
26
|
-
? options.topic
|
|
27
|
-
: name.replace(PEERS_NAME_PREFIX, "");
|
|
25
|
+
const topicName = options?.topic || name.replace(PEERS_NAME_PREFIX, "");
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
topic: {
|
|
31
|
-
value: owner.createTopic(topicName, options)
|
|
32
|
-
}
|
|
33
|
-
});
|
|
27
|
+
this.topic = owner.createTopic(topicName, options);
|
|
34
28
|
|
|
35
29
|
this.topic.addPeersEndpoint(this);
|
|
36
30
|
}
|
package/src/service-swarm.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { pipeline } from "stream";
|
|
2
|
-
import
|
|
1
|
+
import { pipeline } from "node:stream";
|
|
2
|
+
import Hyperswarm from "hyperswarm";
|
|
3
3
|
import { Decode, Encode } from "length-prefix-framed-stream";
|
|
4
4
|
import { mergeAttributes, createAttributes } from "model-attributes";
|
|
5
5
|
import { Service } from "@kronos-integration/service";
|
|
@@ -11,7 +11,8 @@ import { PeersEndpoint } from "./peers-endpoint.mjs";
|
|
|
11
11
|
* Swarm detecting sync service.
|
|
12
12
|
*/
|
|
13
13
|
export class ServiceSwarm extends Service {
|
|
14
|
-
//
|
|
14
|
+
_topics; // = new Map();
|
|
15
|
+
_topicsByName; // = new Map();
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* @return {string} 'swarm'
|
|
@@ -23,42 +24,27 @@ export class ServiceSwarm extends Service {
|
|
|
23
24
|
static get configurationAttributes() {
|
|
24
25
|
return mergeAttributes(
|
|
25
26
|
createAttributes({
|
|
26
|
-
|
|
27
|
-
description: "well known peer addresses",
|
|
27
|
+
server: {
|
|
28
28
|
needsRestart: true,
|
|
29
|
-
|
|
29
|
+
default: false,
|
|
30
|
+
type: "boolean"
|
|
30
31
|
},
|
|
31
|
-
|
|
32
|
-
description: "total amount of peers that this peer will connect to",
|
|
33
|
-
default: 10,
|
|
32
|
+
client: {
|
|
34
33
|
needsRestart: true,
|
|
35
|
-
|
|
34
|
+
default: false,
|
|
35
|
+
type: "boolean"
|
|
36
36
|
},
|
|
37
|
-
|
|
37
|
+
dht: {
|
|
38
|
+
description: "well known dht addresses",
|
|
38
39
|
needsRestart: true,
|
|
39
|
-
type: "
|
|
40
|
+
type: "object"
|
|
40
41
|
},
|
|
41
|
-
|
|
42
|
+
maxPeers: {
|
|
43
|
+
description: "total amount of peers that this peer will connect to",
|
|
44
|
+
default: 10,
|
|
42
45
|
needsRestart: true,
|
|
43
46
|
type: "integer"
|
|
44
47
|
},
|
|
45
|
-
|
|
46
|
-
/*
|
|
47
|
-
"node-id": {
|
|
48
|
-
description: "id of our node",
|
|
49
|
-
needsRestart: true,
|
|
50
|
-
type: "string"
|
|
51
|
-
},
|
|
52
|
-
*/
|
|
53
|
-
ephemeral: {
|
|
54
|
-
description: `Set to false if this is a long running instance on a server
|
|
55
|
-
When running in ephemeral mode you don't join the DHT but just
|
|
56
|
-
query it instead. If unset, or set to a non-boolean (default undefined)
|
|
57
|
-
then the node will start in short-lived (ephemeral) mode and switch
|
|
58
|
-
to long-lived (non-ephemeral) mode after a certain period of uptime`,
|
|
59
|
-
needsRestart: true,
|
|
60
|
-
type: "boolean"
|
|
61
|
-
},
|
|
62
48
|
key: {
|
|
63
49
|
description: "topic initial key",
|
|
64
50
|
needsRestart: true,
|
|
@@ -70,17 +56,28 @@ to long-lived (non-ephemeral) mode after a certain period of uptime`,
|
|
|
70
56
|
);
|
|
71
57
|
}
|
|
72
58
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
59
|
+
get topics()
|
|
60
|
+
{
|
|
61
|
+
if(!this._topics) {
|
|
62
|
+
this._topics = new Map();
|
|
63
|
+
}
|
|
64
|
+
return this._topics;
|
|
65
|
+
}
|
|
78
66
|
|
|
67
|
+
get topicsByName()
|
|
68
|
+
{
|
|
69
|
+
if(!this._topicsByName) {
|
|
70
|
+
this._topicsByName = new Map();
|
|
71
|
+
}
|
|
72
|
+
return this._topicsByName;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
createTopic(name, options) {
|
|
79
76
|
let topic = this.topicsByName.get(name);
|
|
80
77
|
if (!topic) {
|
|
81
78
|
topic = new Topic(this, name, options);
|
|
82
79
|
this.topicsByName.set(name, topic);
|
|
83
|
-
this.topics.set(topic.key, topic
|
|
80
|
+
this.topics.set(topic.key, topic);
|
|
84
81
|
}
|
|
85
82
|
|
|
86
83
|
return topic;
|
|
@@ -105,55 +102,40 @@ to long-lived (non-ephemeral) mode after a certain period of uptime`,
|
|
|
105
102
|
}
|
|
106
103
|
|
|
107
104
|
async _start() {
|
|
108
|
-
const swarm =
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
this.swarm = swarm;
|
|
122
|
-
|
|
123
|
-
await Promise.all(
|
|
124
|
-
[...this.topics.values()].map(topic => {
|
|
125
|
-
this.trace(`join topic ${topic.name} ${JSON.stringify(topic.options)}`);
|
|
126
|
-
return new Promise(resolve => {
|
|
127
|
-
this.swarm.join(topic.key, topic.options, () => {
|
|
128
|
-
this.trace(`joined topic ${topic.name}`);
|
|
129
|
-
resolve();
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
})
|
|
105
|
+
const swarm = this.swarm = new Hyperswarm({
|
|
106
|
+
dht: this.dht,
|
|
107
|
+
maxPeers: this.maxPeers});
|
|
108
|
+
|
|
109
|
+
/*
|
|
110
|
+
this.discovery = swarm.join(
|
|
111
|
+
topic,
|
|
112
|
+
? {
|
|
113
|
+
server: this.server,
|
|
114
|
+
client: this.client
|
|
115
|
+
}
|
|
133
116
|
);
|
|
117
|
+
*/
|
|
118
|
+
|
|
119
|
+
swarm.on("update", () => {
|
|
120
|
+
console.log("Hyperswarm update", swarm);
|
|
121
|
+
});
|
|
134
122
|
|
|
135
123
|
swarm.on("peer", peer => {
|
|
136
124
|
const topic = this.topics.get(peer.topic);
|
|
137
125
|
topic.addPeer(peer);
|
|
138
126
|
});
|
|
139
127
|
|
|
140
|
-
swarm.on("
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
/*swarm.on("updated", key => {
|
|
145
|
-
this.info(`updated: ${JSON.stringify(key)}`);
|
|
146
|
-
});*/
|
|
147
|
-
|
|
148
|
-
swarm.on("connection", async (socket, info) => {
|
|
128
|
+
swarm.on("connection", async (socket, peerInfo) => {
|
|
129
|
+
console.log(socket, peerInfo);
|
|
130
|
+
/*
|
|
149
131
|
this.trace(
|
|
150
132
|
`connection: peer=${info.peer ? "true" : "false"} client=${
|
|
151
133
|
info.client ? "true" : "false"
|
|
152
134
|
} ${JSON.stringify(socket.address())} ${socket.remoteAddress}`
|
|
153
|
-
)
|
|
135
|
+
);*/
|
|
154
136
|
|
|
155
|
-
if (
|
|
156
|
-
const topic = this.topics.get(
|
|
137
|
+
if (peerInfo) {
|
|
138
|
+
const topic = this.topics.get(peerInfo.peer.topic);
|
|
157
139
|
|
|
158
140
|
this.trace(`Connection for topic ${topic.name}`);
|
|
159
141
|
|
|
@@ -164,29 +146,18 @@ to long-lived (non-ephemeral) mode after a certain period of uptime`,
|
|
|
164
146
|
|
|
165
147
|
const encode = new Encode();
|
|
166
148
|
|
|
167
|
-
|
|
168
|
-
setInterval(() => {
|
|
169
|
-
encode.write(`hello from ${hostname()}`);
|
|
170
|
-
}, 5 * 60 * 1000);*/
|
|
171
|
-
|
|
172
|
-
pipeline(encode, socket, e => {
|
|
173
|
-
this.trace(`Encoding pipeline end ${e}`);
|
|
174
|
-
});
|
|
149
|
+
pipeline(encode, socket, e => this.trace(`Encoding pipeline end ${e}`));
|
|
175
150
|
|
|
176
151
|
this.trace(`Encoding pipeline established ${topic.name}`);
|
|
177
152
|
}
|
|
178
153
|
|
|
179
154
|
const decode = new Decode({ objectMode: true, encoding: "utf8" });
|
|
180
155
|
|
|
181
|
-
pipeline(socket, decode, e => {
|
|
182
|
-
this.trace(`Decoding pipeline end ${e}`);
|
|
183
|
-
});
|
|
156
|
+
pipeline(socket, decode, e => this.trace(`Decoding pipeline end ${e}`));
|
|
184
157
|
|
|
185
158
|
this.trace(`Decoding pipeline established`);
|
|
186
159
|
|
|
187
|
-
decode.on("data", data => {
|
|
188
|
-
this.trace(`got ${data}`);
|
|
189
|
-
});
|
|
160
|
+
decode.on("data", data => this.trace(`got ${data}`));
|
|
190
161
|
});
|
|
191
162
|
|
|
192
163
|
swarm.on("disconnection", (socket, info) => {
|
|
@@ -200,6 +171,18 @@ to long-lived (non-ephemeral) mode after a certain period of uptime`,
|
|
|
200
171
|
}
|
|
201
172
|
}
|
|
202
173
|
});
|
|
174
|
+
|
|
175
|
+
await Promise.all(
|
|
176
|
+
[...this.topics.values()].map(topic => {
|
|
177
|
+
this.trace(`join topic ${topic.name} ${JSON.stringify(topic.options)}`);
|
|
178
|
+
return new Promise(resolve => {
|
|
179
|
+
this.swarm.join(topic.key, topic.options, () => {
|
|
180
|
+
this.trace(`joined topic ${topic.name}`);
|
|
181
|
+
resolve();
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
})
|
|
185
|
+
);
|
|
203
186
|
}
|
|
204
187
|
|
|
205
188
|
async _stop() {
|
package/src/topic-endpoint.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { pipeline } from "stream";
|
|
1
|
+
import { pipeline } from "node:stream";
|
|
2
2
|
import { MultiSendEndpoint } from "@kronos-integration/endpoint";
|
|
3
3
|
import { Encode } from "length-prefix-framed-stream";
|
|
4
4
|
|
|
@@ -20,20 +20,15 @@ export class TopicEndpoint extends MultiSendEndpoint {
|
|
|
20
20
|
return name.startsWith(TOPIC_NAME_PREFIX);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
sockets = new Set();
|
|
24
|
+
encode = new Encode();
|
|
25
|
+
|
|
26
|
+
constructor(name, owner, options) {
|
|
24
27
|
super(name, owner, options);
|
|
25
28
|
|
|
26
|
-
const topicName = options.
|
|
27
|
-
? options.topic
|
|
28
|
-
: name.replace(TOPIC_NAME_PREFIX, "");
|
|
29
|
+
const topicName = options?.topic || name.replace(TOPIC_NAME_PREFIX, "");
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
topic: {
|
|
32
|
-
value: owner.createTopic(topicName, options)
|
|
33
|
-
},
|
|
34
|
-
sockets: { value: new Set() },
|
|
35
|
-
encode: { value: new Encode() }
|
|
36
|
-
});
|
|
31
|
+
this.topic = owner.createTopic(topicName, options);
|
|
37
32
|
|
|
38
33
|
this.topic.addTopicEndpoint(this);
|
|
39
34
|
}
|
package/src/topic.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createHash } from "crypto";
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @param {ServiceSwarm} service
|
|
@@ -13,25 +13,26 @@ import { createHash } from "crypto";
|
|
|
13
13
|
* @property {Set<PeerEndpoint>} peerEndpoints
|
|
14
14
|
*/
|
|
15
15
|
export class Topic {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
16
|
+
peers = new Map();
|
|
17
|
+
sockets = new Set();
|
|
18
|
+
topicEndpoints = new Set();
|
|
19
|
+
peersEndpoints = new Set();
|
|
20
|
+
|
|
21
|
+
constructor(service, name, options) {
|
|
22
|
+
this.service = service;
|
|
23
|
+
this.name = name;
|
|
24
|
+
this.options = { server: false, client: true, ...options };
|
|
25
|
+
this.key = createHash("sha256")
|
|
26
|
+
.update(service.key + name)
|
|
27
|
+
.digest();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
join() {
|
|
31
|
+
return this.service.swarm.join(this.key, this.options);
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
addSocket(socket) {
|
|
34
|
-
if(this.sockets.has(socket)) {
|
|
35
|
+
if (this.sockets.has(socket)) {
|
|
35
36
|
this.service.error(`socket already present`);
|
|
36
37
|
return;
|
|
37
38
|
}
|
|
@@ -68,7 +69,7 @@ export class Topic {
|
|
|
68
69
|
}
|
|
69
70
|
|
|
70
71
|
addPeer(peer) {
|
|
71
|
-
/*
|
|
72
|
+
/*
|
|
72
73
|
port: 45505
|
|
73
74
|
host: '10.0.6.2'
|
|
74
75
|
local: true
|
|
@@ -111,8 +112,8 @@ referrer: {
|
|
|
111
112
|
return { host: p.host, port: p.port, local: p.local };
|
|
112
113
|
}),
|
|
113
114
|
sockets: this.sockets.size,
|
|
114
|
-
|
|
115
|
-
|
|
115
|
+
server: this.options.server,
|
|
116
|
+
client: this.options.client
|
|
116
117
|
};
|
|
117
118
|
}
|
|
118
119
|
}
|
package/update.sh
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
SERVERS="odroid0 odroid1 odroid2 odroid3 odroid4 odroid5 pine1 rpi1 xhyve2.private"
|
|
5
|
-
SERVERS="odroid0 odroid1 odroid2 odroid3 odroid4 odroid5 pine1 rpi1"
|
|
6
|
-
SERVERS="odroid odroid1 odroid2 pine1"
|
|
7
|
-
SERVERS="odroid1 pine1"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
for server in $SERVERS
|
|
11
|
-
do
|
|
12
|
-
echo "*** " $server
|
|
13
|
-
scp src/* root@$server:/services/system-dashboard/node_modules/@kronos-integration/service-swarm/src
|
|
14
|
-
ssh -l root -A $server "systemctl restart system-dashboard"
|
|
15
|
-
ssh -l root -A $server "systemctl status system-dashboard"
|
|
16
|
-
done
|