@dxos/signal 2.33.9-dev.43c233c0 → 2.33.9-dev.62131029

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.
Files changed (129) hide show
  1. package/.eslintrc.js +6 -0
  2. package/.rush/temp/package-deps_build_test.json +17 -0
  3. package/.rush/temp/shrinkwrap-deps.json +297 -0
  4. package/bin/signal-test-darwin-amd64 +0 -0
  5. package/bin/signal-test-darwin-arm64 +0 -0
  6. package/bin/signal-test-linux-amd64 +0 -0
  7. package/bin/signal-test-linux-arm64 +0 -0
  8. package/bin/signal.js +0 -0
  9. package/dist/src/index.d.ts +1 -3
  10. package/dist/src/index.d.ts.map +1 -1
  11. package/dist/src/index.js +2 -4
  12. package/dist/src/index.js.map +1 -1
  13. package/dist/src/test-broker.d.ts +19 -0
  14. package/dist/src/test-broker.d.ts.map +1 -0
  15. package/dist/src/test-broker.js +104 -0
  16. package/dist/src/test-broker.js.map +1 -0
  17. package/dist/tsconfig.tsbuildinfo +1 -1
  18. package/package.json +11 -57
  19. package/signal.build.log +0 -0
  20. package/src/index.ts +2 -4
  21. package/src/test-broker.ts +94 -0
  22. package/tsconfig.json +20 -0
  23. package/dist/package.json +0 -94
  24. package/dist/src/bin.d.ts +0 -3
  25. package/dist/src/bin.d.ts.map +0 -1
  26. package/dist/src/bin.js +0 -79
  27. package/dist/src/bin.js.map +0 -1
  28. package/dist/src/broker.d.ts +0 -30
  29. package/dist/src/broker.d.ts.map +0 -1
  30. package/dist/src/broker.js +0 -99
  31. package/dist/src/broker.js.map +0 -1
  32. package/dist/src/broker.test.d.ts +0 -2
  33. package/dist/src/broker.test.d.ts.map +0 -1
  34. package/dist/src/broker.test.js +0 -30
  35. package/dist/src/broker.test.js.map +0 -1
  36. package/dist/src/presence.test.d.ts +0 -2
  37. package/dist/src/presence.test.d.ts.map +0 -1
  38. package/dist/src/presence.test.js +0 -54
  39. package/dist/src/presence.test.js.map +0 -1
  40. package/dist/src/serializer.d.ts +0 -7
  41. package/dist/src/serializer.d.ts.map +0 -1
  42. package/dist/src/serializer.js +0 -21
  43. package/dist/src/serializer.js.map +0 -1
  44. package/dist/src/services/discovery.d.ts +0 -21
  45. package/dist/src/services/discovery.d.ts.map +0 -1
  46. package/dist/src/services/discovery.js +0 -93
  47. package/dist/src/services/discovery.js.map +0 -1
  48. package/dist/src/services/index.d.ts +0 -6
  49. package/dist/src/services/index.d.ts.map +0 -1
  50. package/dist/src/services/index.js +0 -25
  51. package/dist/src/services/index.js.map +0 -1
  52. package/dist/src/services/network.d.ts +0 -24
  53. package/dist/src/services/network.d.ts.map +0 -1
  54. package/dist/src/services/network.js +0 -105
  55. package/dist/src/services/network.js.map +0 -1
  56. package/dist/src/services/presence.d.ts +0 -17
  57. package/dist/src/services/presence.d.ts.map +0 -1
  58. package/dist/src/services/presence.js +0 -74
  59. package/dist/src/services/presence.js.map +0 -1
  60. package/dist/src/services/status.d.ts +0 -46
  61. package/dist/src/services/status.d.ts.map +0 -1
  62. package/dist/src/services/status.js +0 -248
  63. package/dist/src/services/status.js.map +0 -1
  64. package/dist/src/services/web.d.ts +0 -8
  65. package/dist/src/services/web.d.ts.map +0 -1
  66. package/dist/src/services/web.js +0 -28
  67. package/dist/src/services/web.js.map +0 -1
  68. package/dist/src/signal/index.d.ts +0 -3
  69. package/dist/src/signal/index.d.ts.map +0 -1
  70. package/dist/src/signal/index.js +0 -22
  71. package/dist/src/signal/index.js.map +0 -1
  72. package/dist/src/signal/peer-map.d.ts +0 -63
  73. package/dist/src/signal/peer-map.d.ts.map +0 -1
  74. package/dist/src/signal/peer-map.js +0 -135
  75. package/dist/src/signal/peer-map.js.map +0 -1
  76. package/dist/src/signal/peer-map.test.d.ts +0 -2
  77. package/dist/src/signal/peer-map.test.d.ts.map +0 -1
  78. package/dist/src/signal/peer-map.test.js +0 -44
  79. package/dist/src/signal/peer-map.test.js.map +0 -1
  80. package/dist/src/signal/signal-server.d.ts +0 -17
  81. package/dist/src/signal/signal-server.d.ts.map +0 -1
  82. package/dist/src/signal/signal-server.js +0 -92
  83. package/dist/src/signal/signal-server.js.map +0 -1
  84. package/dist/src/signal.test.d.ts +0 -2
  85. package/dist/src/signal.test.d.ts.map +0 -1
  86. package/dist/src/signal.test.js +0 -67
  87. package/dist/src/signal.test.js.map +0 -1
  88. package/dist/src/system-information.d.ts +0 -5
  89. package/dist/src/system-information.d.ts.map +0 -1
  90. package/dist/src/system-information.js +0 -96
  91. package/dist/src/system-information.js.map +0 -1
  92. package/dist/src/testing.d.ts +0 -9
  93. package/dist/src/testing.d.ts.map +0 -1
  94. package/dist/src/testing.js +0 -25
  95. package/dist/src/testing.js.map +0 -1
  96. package/dist/src/transporter/bootstrap-node.d.ts +0 -2
  97. package/dist/src/transporter/bootstrap-node.d.ts.map +0 -1
  98. package/dist/src/transporter/bootstrap-node.js +0 -95
  99. package/dist/src/transporter/bootstrap-node.js.map +0 -1
  100. package/dist/src/transporter/index.d.ts +0 -38
  101. package/dist/src/transporter/index.d.ts.map +0 -1
  102. package/dist/src/transporter/index.js +0 -151
  103. package/dist/src/transporter/index.js.map +0 -1
  104. package/dist/src/transporter/messenger.d.ts +0 -2
  105. package/dist/src/transporter/messenger.d.ts.map +0 -1
  106. package/dist/src/transporter/messenger.js +0 -193
  107. package/dist/src/transporter/messenger.js.map +0 -1
  108. package/src/bin.ts +0 -81
  109. package/src/broker.test.js +0 -35
  110. package/src/broker.ts +0 -134
  111. package/src/presence.test.js +0 -67
  112. package/src/serializer.js +0 -17
  113. package/src/services/discovery.js +0 -101
  114. package/src/services/index.ts +0 -9
  115. package/src/services/network.ts +0 -129
  116. package/src/services/presence.js +0 -77
  117. package/src/services/status.js +0 -259
  118. package/src/services/web.ts +0 -24
  119. package/src/shims.d.ts +0 -6
  120. package/src/signal/index.ts +0 -6
  121. package/src/signal/peer-map.test.js +0 -58
  122. package/src/signal/peer-map.ts +0 -174
  123. package/src/signal/signal-server.js +0 -106
  124. package/src/signal.test.js +0 -78
  125. package/src/system-information.ts +0 -110
  126. package/src/testing.ts +0 -27
  127. package/src/transporter/bootstrap-node.js +0 -112
  128. package/src/transporter/index.js +0 -179
  129. package/src/transporter/messenger.js +0 -223
@@ -1,179 +0,0 @@
1
- //
2
- // Copyright 2021 DXOS.org
3
- //
4
-
5
- import { EventEmitter } from 'events';
6
- import { discoveryKey } from 'hypercore-crypto';
7
- import hyperswarm from 'hyperswarm';
8
- import assert from 'node:assert';
9
- import pEvent from 'p-event';
10
- import Protocol from 'simple-hypercore-protocol';
11
-
12
- import { BootstrapNode } from './bootstrap-node';
13
- import { Messenger } from './messenger';
14
-
15
- const { Transporters: { Base: BaseTransporter } } = require('moleculer'); // eslint-disable-line @typescript-eslint/no-var-requires
16
-
17
- export class ProtocolTransporter extends BaseTransporter {
18
- static keyPair () {
19
- return Protocol.keyPair();
20
- }
21
-
22
- constructor (opts) {
23
- const { topic, keyPair, hyperswarm = {}, asBootstrap = false, bootstrapPort } = opts;
24
-
25
- assert(Buffer.isBuffer(topic), 'topic is required and must be a buffer of 32 bytes');
26
- assert(keyPair && Buffer.isBuffer(keyPair.publicKey) && Buffer.isBuffer(keyPair.secretKey), 'keyPair is required and must be an object of { publicKey: Buffer<32>, secretKey: Buffer<32> }');
27
-
28
- super(opts);
29
-
30
- this._topic = topic;
31
- this._keyPair = keyPair;
32
- this._discoveryKey = discoveryKey(topic);
33
- this._hyperswarmOptions = hyperswarm;
34
- this._messenger = new Messenger(this._topic, this._keyPair);
35
-
36
- this._ee = new EventEmitter();
37
- this._nanomessage = null;
38
- this._swarm = null;
39
- this._bootstrapNode = null;
40
-
41
- if (asBootstrap) {
42
- this._bootstrapNode = new BootstrapNode({ port: bootstrapPort });
43
- }
44
-
45
- this.onPeerConnection = this.onPeerConnection.bind(this);
46
- this._messenger.on('message', message => this._ee.emit(message.topic, message.data));
47
- this._messenger.on('peer-added', (peer) => this._ee.emit('peer-added', peer));
48
- this._messenger.on('peer-deleted', (peer) => this._ee.emit('peer-deleted', peer));
49
- }
50
-
51
- get dht () {
52
- return this._swarm && this._swarm.network.discovery && this._swarm.network.discovery.dht;
53
- }
54
-
55
- get onlyLocal () {
56
- return this._messenger.peers.length === 0;
57
- }
58
-
59
- get peers () {
60
- return this._messenger ? this._messenger.peers : [];
61
- }
62
-
63
- on (event, handler) {
64
- this._ee.on(event, handler);
65
- }
66
-
67
- off (event, handler) {
68
- this._ee.off(event, handler);
69
- }
70
-
71
- waitForConnected () {
72
- if (this.connected) {
73
- return;
74
- }
75
- return pEvent(this, 'connected');
76
- }
77
-
78
- async connect () {
79
- const opts = {
80
- ...this._hyperswarmOptions
81
- };
82
-
83
- if (this._bootstrapNode) {
84
- await this._bootstrapNode.start(this.broker);
85
-
86
- if (!opts.bootstrap) {
87
- opts.bootstrap = [];
88
- }
89
-
90
- const address = await this._bootstrapNode.getAddress();
91
- opts.bootstrap.push(`${address.address}:${address.port}`);
92
- }
93
-
94
- if (opts.bootstrap) {
95
- opts.bootstrap = [...new Set(opts.bootstrap)];
96
- }
97
-
98
- this.logger.info('Bootstrap nodes', opts.bootstrap ? opts.bootstrap : 'default');
99
-
100
- this._swarm = hyperswarm(opts);
101
- this._swarm.on('connection', this.onPeerConnection);
102
- this._swarm.once('close', () => {
103
- if (this.connected) {
104
- this.connected = false;
105
- }
106
-
107
- this._swarm.removeListener('connection', this.onPeerConnection);
108
- this.logger.warn('ProtocolTransporter disconnected');
109
- });
110
-
111
- await this._messenger.open();
112
-
113
- return new Promise((resolve, reject) => {
114
- const onError = (err) => {
115
- this.logger.error('ProtocolTransporter error', err.message);
116
- reject(err);
117
- };
118
-
119
- this._swarm.once('error', onError);
120
-
121
- this._swarm.join(this._discoveryKey, {
122
- lookup: true,
123
- announce: true
124
- }, () => {
125
- this.logger.info('ProtocolTransporter connected');
126
- this._swarm.removeListener('error', onError);
127
- this._ee.emit('connected');
128
- void this.onConnected().then(resolve);
129
- });
130
- });
131
- }
132
-
133
- async disconnect () {
134
- if (this._bootstrapNode) {
135
- await this._bootstrapNode.stop();
136
- }
137
-
138
- if (this._swarm) {
139
- await new Promise(resolve => this._swarm.destroy(() => resolve()));
140
- await this._messenger.close();
141
- }
142
- }
143
-
144
- subscribe (cmd, nodeID) {
145
- const t = this.getTopicName(cmd, nodeID);
146
-
147
- this._ee.on(t, msg => this.receive(cmd, msg));
148
-
149
- return Promise.resolve();
150
- }
151
-
152
- /**
153
- * Send data buffer.
154
- *
155
- * @param {String} topic
156
- * @param {Buffer} data
157
- * @param {Object} meta
158
- *
159
- * @returns {Promise}
160
- */
161
- send (topic, data, { packet }) {
162
- if (!this._swarm || this._messenger.closed || this._messenger.closing || this._messenger.peers.length === 0) {
163
- return Promise.resolve();
164
- }
165
-
166
- const sended = this._messenger.send(packet.target, { topic, data });
167
- if (!sended) {
168
- return this._messenger.broadcast({ topic, data });
169
- }
170
- }
171
-
172
- async onPeerConnection (socket, info) {
173
- try {
174
- await this._messenger.addPeer(socket, info);
175
- } catch (err) {
176
- this.logger.error('Peer error', err);
177
- }
178
- }
179
- }
@@ -1,223 +0,0 @@
1
- //
2
- // Copyright 2021 DXOS.org
3
- //
4
-
5
- import eos from 'end-of-stream';
6
- import { EventEmitter } from 'events';
7
- import crypto from 'hypercore-crypto';
8
- import { NanoresourcePromise } from 'nanoresource-promise/emitter';
9
- import Protocol from 'simple-hypercore-protocol';
10
- import varint from 'varint';
11
-
12
- import { Broadcast } from '@dxos/broadcast';
13
-
14
- const BROADCAST_CHANNEL = 0;
15
- const DIRECT_CHANNEL = 1;
16
-
17
- const packetCodec = {
18
- encode: (obj) => {
19
- const length = Buffer.byteLength(obj.topic, 'utf8');
20
- const buf = Buffer.allocUnsafe(varint.encodingLength(length) + length + obj.data.length);
21
- varint.encode(length, buf);
22
- buf.write(obj.topic, varint.encode.bytes, length, 'utf8');
23
- obj.data.copy(buf, varint.encode.bytes + length);
24
- return buf;
25
- },
26
- decode: (buf) => {
27
- const length = varint.decode(buf);
28
- const topic = buf.slice(varint.decode.bytes, varint.decode.bytes + length);
29
- const data = buf.slice(varint.decode.bytes + length);
30
- return { topic: topic.toString(), data };
31
- }
32
- };
33
-
34
- class Peer extends EventEmitter {
35
- constructor ({ initiator, socket, topic, keyPair }) {
36
- super();
37
-
38
- this._initiator = initiator;
39
- this._socket = socket;
40
- this._initializeProtocol(topic, keyPair);
41
- }
42
-
43
- get destroyed () {
44
- return this._socket.destroyed;
45
- }
46
-
47
- get initiator () {
48
- return this._initiator;
49
- }
50
-
51
- get publicKey () {
52
- return this._protocol.publicKey;
53
- }
54
-
55
- get remotePublicKey () {
56
- return this._protocol.remotePublicKey;
57
- }
58
-
59
- get sessionKey () {
60
- return this._protocol.handshakeHash ? crypto.discoveryKey(this._protocol.handshakeHash) : null;
61
- }
62
-
63
- // The broadcast use an `id` prop.
64
- get id () {
65
- return this.remotePublicKey;
66
- }
67
-
68
- broadcast (buf) {
69
- if (this._socket.destroyed) {
70
- return;
71
- }
72
- this._protocol.extension(BROADCAST_CHANNEL, 0, buf);
73
- }
74
-
75
- send (buf) {
76
- if (this._socket.destroyed) {
77
- return;
78
- }
79
- this._protocol.extension(DIRECT_CHANNEL, 0, buf);
80
- }
81
-
82
- destroy () {
83
- if (this._socket.destroyed) {
84
- return;
85
- }
86
- return this._socket.destroy();
87
- }
88
-
89
- _initializeProtocol (topic, keyPair) {
90
- const socket = this._socket;
91
-
92
- this._protocol = new Protocol(this._initiator, {
93
- keyPair,
94
- send (data) {
95
- socket.write(data);
96
- },
97
- onclose () {
98
- if (socket.destroyed) {
99
- return;
100
- }
101
- socket.destroy();
102
- },
103
- onhandshake: () => this.emit('handshake'),
104
- onextension: (ch, id, data) => this.emit('message', ch, data)
105
- });
106
-
107
- socket.on('data', (data) => this._protocol.recv(data));
108
-
109
- eos(socket, () => this._protocol.destroy());
110
- }
111
- }
112
-
113
- class Messenger extends NanoresourcePromise {
114
- constructor (topic, keyPair) {
115
- super();
116
-
117
- this._topic = topic;
118
- this._keyPair = keyPair;
119
- this._maxAge = 30 * 1000;
120
- this._broadcast = new Broadcast(this._middleware(), {
121
- id: this.publicKey,
122
- maxAge: this._maxAge,
123
- maxSize: Number.MAX_SAFE_INTEGER
124
- });
125
- this._peers = new Set();
126
- }
127
-
128
- get publicKey () {
129
- return this._keyPair.publicKey;
130
- }
131
-
132
- get peers () {
133
- return Array.from(this._peers.values());
134
- }
135
-
136
- addPeer (socket, info) {
137
- const peer = new Peer({
138
- initiator: info.client,
139
- socket,
140
- topic: this._topic,
141
- keyPair: this._keyPair
142
- });
143
-
144
- peer.on('handshake', () => {
145
- if (socket.destroyed) {
146
- return;
147
- }
148
- if (info.deduplicate(peer.remotePublicKey, peer.publicKey)) {
149
- return;
150
- }
151
- this._peers.add(peer);
152
- this._broadcast.updatePeers(this.peers);
153
- this.emit('peer-added', { initiator: peer.initiator, sessionKey: peer.sessionKey, peerId: peer.id });
154
- });
155
-
156
- const onBroadcast = (ch, message) => this.emit('peer-message', ch, message);
157
- peer.on('message', onBroadcast);
158
-
159
- eos(socket, () => {
160
- this._peers.delete(peer);
161
- this._broadcast.updatePeers(this.peers);
162
- peer.off('message', onBroadcast);
163
- this.emit('peer-deleted', { initiator: peer.initiator, sessionKey: peer.sessionKey, peerId: peer.id });
164
- });
165
-
166
- return peer;
167
- }
168
-
169
- broadcast (message, options) {
170
- return this._broadcast.publish(packetCodec.encode(message), options);
171
- }
172
-
173
- send (id, message) {
174
- const peer = this.peers.find(p => p.remotePublicKey.toString('hex') === id);
175
-
176
- if (!peer) {
177
- return false;
178
- }
179
-
180
- return peer.send(packetCodec.encode(message));
181
- }
182
-
183
- async _open () {
184
- await this._broadcast.open();
185
-
186
- this._pruneCacheInterval = setInterval(() => {
187
- this._broadcast.pruneCache();
188
- }, this._maxAge);
189
- }
190
-
191
- async _close () {
192
- await this._broadcast.close();
193
- clearInterval(this._pruneCacheInterval);
194
- }
195
-
196
- _middleware () {
197
- return {
198
- // Send must be async.
199
- send: async (packet, node) => node.broadcast(packet),
200
- subscribe: (onData) => {
201
- const onMessage = (ch, message) => {
202
- try {
203
- if (ch === BROADCAST_CHANNEL) {
204
- const response = onData(message);
205
- if (response && response.data) {
206
- this.emit('message', packetCodec.decode(response.data));
207
- }
208
- } else {
209
- this.emit('message', packetCodec.decode(message));
210
- }
211
- } catch (err) {}
212
- };
213
-
214
- this.on('peer-message', onMessage);
215
- return () => {
216
- this.off('peer-message', onMessage);
217
- };
218
- }
219
- };
220
- }
221
- }
222
-
223
- exports.Messenger = Messenger;