@kronos-integration/service-swarm 4.0.12 → 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/README.md CHANGED
@@ -1,10 +1,12 @@
1
+ [![npm](https://img.shields.io/npm/v/@kronos-integration/service-swarm.svg)](https://www.npmjs.com/package/@kronos-integration/service-swarm)
1
2
  [![License](https://img.shields.io/badge/License-0BSD-blue.svg)](https://spdx.org/licenses/0BSD.html)
3
+ [![bundlejs](https://deno.bundlejs.com/?q=@kronos-integration/service-swarm\&badge=detailed)](https://bundlejs.com/?q=@kronos-integration/service-swarm)
4
+ [![downloads](http://img.shields.io/npm/dm/@kronos-integration/service-swarm.svg?style=flat-square)](https://npmjs.org/package/@kronos-integration/service-swarm)
2
5
  [![GitHub Issues](https://img.shields.io/github/issues/Kronos-Integration/service-swarm.svg?style=flat-square)](https://github.com/Kronos-Integration/service-swarm/issues)
3
6
  [![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2FKronos-Integration%2Fservice-swarm%2Fbadge\&style=flat)](https://actions-badge.atrox.dev/Kronos-Integration/service-swarm/goto)
4
7
  [![Styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
5
8
  [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
6
9
  [![Known Vulnerabilities](https://snyk.io/test/github/Kronos-Integration/service-swarm/badge.svg)](https://snyk.io/test/github/Kronos-Integration/service-swarm)
7
- [![Coverage Status](https://coveralls.io/repos/Kronos-Integration/service-swarm/badge.svg)](https://coveralls.io/github/Kronos-Integration/service-swarm)
8
10
 
9
11
  # @kronos-integration/service-swarm
10
12
 
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@kronos-integration/service-swarm",
3
- "version": "4.0.12",
3
+ "version": "4.0.14",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
7
7
  },
8
+ "packageManager": "npm@11.6.4+sha512.1118cab46a05a50aee6bff5b1b4fa1df18afff89d57465620a3518035026955db87c5bdf9d207b07b7487d99f2490d450cb774655ad63ec2cba7bf1d0ad25d45",
8
9
  "exports": {
9
10
  ".": "./src/service-swarm.mjs"
10
11
  },
@@ -20,21 +21,21 @@
20
21
  "email": "markus.felten@gmx.de"
21
22
  }
22
23
  ],
23
- "license": "BSD-2-Clause",
24
+ "license": "0BSD",
24
25
  "scripts": {
25
26
  "test": "node --run test:ava",
26
27
  "test:ava": "ava --timeout 4m tests/*-ava.mjs tests/*-ava-node.mjs",
27
28
  "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
- "docs": "documentation readme --section=API ./src/**/*.mjs",
29
+ "docs": "documentation readme --section=API ./src**/*.mjs",
29
30
  "lint": "node --run lint:docs",
30
- "lint:docs": "documentation lint ./src/**/*.mjs"
31
+ "lint:docs": "documentation lint ./src**/*.mjs"
31
32
  },
32
33
  "dependencies": {
33
34
  "@kronos-integration/endpoint": "^11.0.5",
34
35
  "@kronos-integration/service": "^15.1.1",
35
- "hyperswarm": "^4.15.1",
36
+ "hyperswarm": "^4.16.0",
36
37
  "length-prefix-framed-stream": "^2.0.12",
37
- "pacc": "^5.1.0"
38
+ "pacc": "^5.2.0"
38
39
  },
39
40
  "devDependencies": {
40
41
  "@hyperswarm/dht": "^6.5.1",
@@ -45,7 +46,7 @@
45
46
  "semantic-release": "^25.0.2"
46
47
  },
47
48
  "engines": {
48
- "node": ">=24.11.1"
49
+ "node": ">=24.12.0"
49
50
  },
50
51
  "repository": {
51
52
  "type": "git",
@@ -60,6 +61,5 @@
60
61
  "arlac77/template-arlac77-github",
61
62
  "arlac77/template-kronos-component"
62
63
  ]
63
- },
64
- "packageManager": "npm@11.6.4+sha512.1118cab46a05a50aee6bff5b1b4fa1df18afff89d57465620a3518035026955db87c5bdf9d207b07b7487d99f2490d450cb774655ad63ec2cba7bf1d0ad25d45"
64
+ }
65
65
  }
@@ -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
 
@@ -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
- key: {
51
+ seed: {
52
52
  ...private_key_attribute,
53
- description: "topic initial key",
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
- * @return {Class} TopicEndpoint if name starts with 'topic.'
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
- dht: this.dht,
106
- maxPeers: this.maxPeers
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
- return new Promise(resolve => {
179
- this.swarm.join(topic.key, topic.options, () => {
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 new Promise(resolve => {
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
 
@@ -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
  }