@kronos-integration/service-swarm 4.0.13 → 4.0.14
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/package.json +1 -1
- package/src/peers-endpoint.mjs +10 -5
- package/src/service-swarm.mjs +26 -32
- package/src/topic-endpoint.mjs +7 -5
package/package.json
CHANGED
package/src/peers-endpoint.mjs
CHANGED
|
@@ -7,11 +7,6 @@ const PEERS_NAME_PREFIX = "peers.";
|
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Endpoint to link against a swarm topic.
|
|
10
|
-
* @param {string} name endpoint name
|
|
11
|
-
* @param {Object} owner owner of the endpoint
|
|
12
|
-
* @param {Object} options
|
|
13
|
-
* @param {string} options.topic defaults to endpoint name (without @see PEERS_NAME_PREFIX)
|
|
14
|
-
*
|
|
15
10
|
* @property {Topic} topic
|
|
16
11
|
*/
|
|
17
12
|
export class PeersEndpoint extends MultiSendEndpoint {
|
|
@@ -19,6 +14,16 @@ export class PeersEndpoint extends MultiSendEndpoint {
|
|
|
19
14
|
return name.startsWith(PEERS_NAME_PREFIX);
|
|
20
15
|
}
|
|
21
16
|
|
|
17
|
+
topic;
|
|
18
|
+
socket;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {string} name endpoint name
|
|
22
|
+
* @param {Object} owner owner of the endpoint
|
|
23
|
+
* @param {Object} options
|
|
24
|
+
* @param {string} options.topic defaults to endpoint name (without @see PEERS_NAME_PREFIX)
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
22
27
|
constructor(name, owner, options) {
|
|
23
28
|
super(name, owner, options);
|
|
24
29
|
|
package/src/service-swarm.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { pipeline } from "node:stream";
|
|
|
2
2
|
import Hyperswarm from "hyperswarm";
|
|
3
3
|
import { Decode, Encode } from "length-prefix-framed-stream";
|
|
4
4
|
import {
|
|
5
|
+
getAttributesJSON,
|
|
5
6
|
prepareAttributesDefinitions,
|
|
6
7
|
private_key_attribute,
|
|
7
8
|
boolean_attribute_false,
|
|
@@ -17,9 +18,6 @@ import { PeersEndpoint } from "./peers-endpoint.mjs";
|
|
|
17
18
|
* Swarm detecting sync service.
|
|
18
19
|
*/
|
|
19
20
|
export class ServiceSwarm extends Service {
|
|
20
|
-
_topics; // = new Map();
|
|
21
|
-
_topicsByName; // = new Map();
|
|
22
|
-
|
|
23
21
|
/**
|
|
24
22
|
* @return {string} 'swarm'
|
|
25
23
|
*/
|
|
@@ -31,7 +29,7 @@ export class ServiceSwarm extends Service {
|
|
|
31
29
|
{
|
|
32
30
|
server: {
|
|
33
31
|
...boolean_attribute_false,
|
|
34
|
-
needsRestart: true
|
|
32
|
+
needsRestart: true
|
|
35
33
|
},
|
|
36
34
|
client: {
|
|
37
35
|
...boolean_attribute_false,
|
|
@@ -41,16 +39,18 @@ export class ServiceSwarm extends Service {
|
|
|
41
39
|
...object_attribute,
|
|
42
40
|
description: "well known dht addresses",
|
|
43
41
|
needsRestart: true,
|
|
42
|
+
connectionOption: true
|
|
44
43
|
},
|
|
45
44
|
maxPeers: {
|
|
46
45
|
...integer_attribute,
|
|
47
46
|
description: "total amount of peers that this peer will connect to",
|
|
48
47
|
default: 10,
|
|
49
|
-
needsRestart: true
|
|
48
|
+
needsRestart: true,
|
|
49
|
+
connectionOption: true
|
|
50
50
|
},
|
|
51
|
-
|
|
51
|
+
seed: {
|
|
52
52
|
...private_key_attribute,
|
|
53
|
-
description: "
|
|
53
|
+
description: "key pair seed",
|
|
54
54
|
needsRestart: true
|
|
55
55
|
}
|
|
56
56
|
},
|
|
@@ -86,7 +86,8 @@ export class ServiceSwarm extends Service {
|
|
|
86
86
|
* On demand create topic endpoints.
|
|
87
87
|
* @param {string} name
|
|
88
88
|
* @param {Object|string} definition
|
|
89
|
-
* @
|
|
89
|
+
* @param {InitializationContext} ic
|
|
90
|
+
* @return {Object} TopicEndpoint if name starts with 'topic.'
|
|
90
91
|
*/
|
|
91
92
|
endpointFactoryFromConfig(name, definition, ic) {
|
|
92
93
|
if (TopicEndpoint.isTopicName(name)) {
|
|
@@ -102,20 +103,14 @@ export class ServiceSwarm extends Service {
|
|
|
102
103
|
|
|
103
104
|
async _start() {
|
|
104
105
|
const swarm = (this.swarm = new Hyperswarm({
|
|
105
|
-
|
|
106
|
-
|
|
106
|
+
...getAttributesJSON(
|
|
107
|
+
this,
|
|
108
|
+
this.attributes,
|
|
109
|
+
(name, attribute) => attribute.connectionOption
|
|
110
|
+
),
|
|
111
|
+
...(await this.getCredentials())
|
|
107
112
|
}));
|
|
108
113
|
|
|
109
|
-
/*
|
|
110
|
-
this.discovery = swarm.join(
|
|
111
|
-
topic,
|
|
112
|
-
? {
|
|
113
|
-
server: this.server,
|
|
114
|
-
client: this.client
|
|
115
|
-
}
|
|
116
|
-
);
|
|
117
|
-
*/
|
|
118
|
-
|
|
119
114
|
swarm.on("update", () => {
|
|
120
115
|
console.log("Hyperswarm update", swarm);
|
|
121
116
|
});
|
|
@@ -175,28 +170,27 @@ export class ServiceSwarm extends Service {
|
|
|
175
170
|
await Promise.all(
|
|
176
171
|
[...this.topics.values()].map(topic => {
|
|
177
172
|
this.trace(`join topic ${topic.name} ${JSON.stringify(topic.options)}`);
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
this.trace(`joined topic ${topic.name}`);
|
|
181
|
-
resolve();
|
|
182
|
-
});
|
|
183
|
-
});
|
|
173
|
+
const discovery = this.swarm.join(topic.key, topic.options);
|
|
174
|
+
return discovery.flushed();
|
|
184
175
|
})
|
|
185
176
|
);
|
|
186
177
|
}
|
|
187
178
|
|
|
188
179
|
async _stop() {
|
|
180
|
+
const swarm = this.swarm;
|
|
181
|
+
delete this.swarm;
|
|
182
|
+
this.swam = undefined;
|
|
183
|
+
|
|
189
184
|
return Promise.all(
|
|
190
185
|
[...this.topics.values()].map(topic => {
|
|
191
186
|
this.trace(`leave topic ${topic.name}`);
|
|
192
|
-
return
|
|
193
|
-
this.swarm.join(topic.key, () => {
|
|
194
|
-
this.trace(`leaved topic ${topic.name}`);
|
|
195
|
-
resolve();
|
|
196
|
-
});
|
|
197
|
-
});
|
|
187
|
+
return swarm.leave(topic.key);
|
|
198
188
|
})
|
|
199
189
|
);
|
|
190
|
+
|
|
191
|
+
await swarm.suspend({
|
|
192
|
+
log: (...args) => this.info(...args)
|
|
193
|
+
});
|
|
200
194
|
}
|
|
201
195
|
}
|
|
202
196
|
|
package/src/topic-endpoint.mjs
CHANGED
|
@@ -9,10 +9,6 @@ const TOPIC_NAME_PREFIX = "topic.";
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Endpoint to link against a swarm topic.
|
|
12
|
-
* @param {string} name endpoint name
|
|
13
|
-
* @param {Object} owner owner of the endpoint
|
|
14
|
-
* @param {Object} options
|
|
15
|
-
* @param {string} options.topic defaults to endpoint name (without @see TOPIC_NAME_PREFIX)
|
|
16
12
|
* @property {Topic} topic
|
|
17
13
|
*/
|
|
18
14
|
export class TopicEndpoint extends MultiSendEndpoint {
|
|
@@ -23,6 +19,12 @@ export class TopicEndpoint extends MultiSendEndpoint {
|
|
|
23
19
|
sockets = new Set();
|
|
24
20
|
encode = new Encode();
|
|
25
21
|
|
|
22
|
+
/**
|
|
23
|
+
* @param {string} name endpoint name
|
|
24
|
+
* @param {Object} owner owner of the endpoint
|
|
25
|
+
* @param {Object} options
|
|
26
|
+
* @param {string} options.topic defaults to endpoint name (without @see TOPIC_NAME_PREFIX)
|
|
27
|
+
*/
|
|
26
28
|
constructor(name, owner, options) {
|
|
27
29
|
super(name, owner, options);
|
|
28
30
|
|
|
@@ -34,7 +36,7 @@ export class TopicEndpoint extends MultiSendEndpoint {
|
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
async addSocket(socket) {
|
|
37
|
-
if(this.sockets.has(socket)) {
|
|
39
|
+
if (this.sockets.has(socket)) {
|
|
38
40
|
this.owner.error(`socket already present`);
|
|
39
41
|
return;
|
|
40
42
|
}
|