hsync 0.30.2 → 0.32.0

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.
@@ -5,7 +5,17 @@
5
5
  * @license MIT
6
6
  */
7
7
 
8
- /*! For license information please see mqtt.browser.js.LICENSE.txt */
8
+ /*! Bundled license information:
9
+
10
+ @jspm/core/nodelibs/browser/chunk-DtuTasat.js:
11
+ (*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> *)
12
+
13
+ safe-buffer/index.js:
14
+ (*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> *)
15
+
16
+ @babel/runtime/helpers/regenerator.js:
17
+ (*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE *)
18
+ */
9
19
 
10
20
  /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
11
21
 
@@ -0,0 +1,26 @@
1
+ import js from '@eslint/js';
2
+ import prettierConfig from 'eslint-config-prettier';
3
+ import globals from 'globals';
4
+
5
+ export default [
6
+ { ignores: ['node_modules/**', 'dist/**'] },
7
+ js.configs.recommended,
8
+ {
9
+ files: ['**/*.js'],
10
+ languageOptions: {
11
+ ecmaVersion: 'latest',
12
+ sourceType: 'module',
13
+ globals: { ...globals.node, ...globals.browser },
14
+ },
15
+ rules: {
16
+ 'no-unused-vars': [
17
+ 'warn',
18
+ { argsIgnorePattern: '^_', varsIgnorePattern: '^_', caughtErrorsIgnorePattern: '^_' },
19
+ ],
20
+ 'no-console': 'off',
21
+ 'prefer-const': 'error',
22
+ 'no-var': 'error',
23
+ },
24
+ },
25
+ prettierConfig,
26
+ ];
package/hsync-web.js CHANGED
@@ -1,21 +1,23 @@
1
- const mqtt = require('precompiled-mqtt');
2
- const buffer = require('buffer');
3
- const net = require('net-web');
4
- const { createHsync, setNet, setMqtt } = require('./connection');
5
- const { setRTC } = require('./lib/peers');
6
- const rtc = require('./lib/rtc-web');
7
- const config = require('./config');
8
-
1
+ import mqtt from 'mqtt';
2
+ import { Buffer } from 'buffer';
3
+ import net from 'net-web';
4
+ import { createHsync, setNet, setMqtt } from './connection.js';
5
+ import { setRTC } from './lib/peers.js';
6
+ import rtc from './lib/rtc-web.js';
7
+ import config from './config.js';
9
8
 
10
9
  // TODO need to make this work with web/service workers
11
- window.Buffer = buffer.Buffer;
10
+ if (typeof window !== 'undefined') {
11
+ window.Buffer = Buffer;
12
+ }
13
+ globalThis.Buffer = Buffer;
12
14
 
13
15
  setRTC(rtc);
14
16
  setNet(net);
15
17
  setMqtt(mqtt);
16
18
 
17
19
  async function dynamicConnect(configObj = { useLocalStorage: true }) {
18
- const fullConfig = {...config, ...configObj};
20
+ const fullConfig = { ...config, ...configObj };
19
21
  fullConfig.dynamicHost = fullConfig.dynamicHost || fullConfig.defaultDynamicHost;
20
22
  if (fullConfig.net) {
21
23
  setNet(fullConfig.net);
@@ -25,16 +27,16 @@ async function dynamicConnect(configObj = { useLocalStorage: true }) {
25
27
  const localConfigStr = localStorage.getItem('hsyncConfig');
26
28
  if (localConfigStr) {
27
29
  const localConfig = JSON.parse(localConfigStr);
28
- if ((Date.now() - localConfig.created) < (localConfig.timeout * 0.66)) {
30
+ if (Date.now() - localConfig.created < localConfig.timeout * 0.66) {
29
31
  fullConfig.hsyncSecret = localConfig.hsyncSecret;
30
32
  fullConfig.hsyncServer = localConfig.hsyncServer;
31
33
  } else {
32
34
  localStorage.removeItem('hsyncConfig');
33
35
  }
34
36
  }
35
-
37
+
36
38
  con = await createHsync(fullConfig);
37
-
39
+
38
40
  if (!fullConfig.hsyncSecret) {
39
41
  const storeConfig = {
40
42
  hsyncSecret: con.hsyncSecret,
@@ -51,15 +53,13 @@ async function dynamicConnect(configObj = { useLocalStorage: true }) {
51
53
  con = await createHsync(fullConfig);
52
54
 
53
55
  return con;
54
-
55
56
  }
56
57
 
57
58
  function createConnection(configObj = {}) {
58
- const fullConfig = {...config, ...configObj};
59
+ const fullConfig = { ...config, ...configObj };
59
60
  return createHsync(fullConfig);
60
61
  }
61
62
 
62
-
63
63
  const hsync = globalThis.hsync || {
64
64
  createConnection,
65
65
  dynamicConnect,
@@ -68,4 +68,5 @@ const hsync = globalThis.hsync || {
68
68
  };
69
69
  globalThis.hsync = hsync;
70
70
 
71
- module.exports = hsync;
71
+ export default hsync;
72
+ export { createConnection, dynamicConnect, config };
package/hsync.js CHANGED
@@ -1,10 +1,12 @@
1
- const net = require('net');
2
- const mqtt = require('mqtt');
3
- const debugError = require('debug')('errors');
4
- const { createHsync, setNet, setMqtt } = require('./connection');
5
- const config = require('./config');
6
- const { setRTC } = require('./lib/peers');
7
- const rtc = require('./lib/rtc-node');
1
+ import net from 'net';
2
+ import mqtt from 'mqtt';
3
+ import createDebug from 'debug';
4
+ import { createHsync, setNet, setMqtt } from './connection.js';
5
+ import config from './config.js';
6
+ import { setRTC } from './lib/peers.js';
7
+ import rtc from './lib/rtc-node.js';
8
+
9
+ const debugError = createDebug('errors');
8
10
 
9
11
  setRTC(rtc);
10
12
  setNet(net);
@@ -13,28 +15,22 @@ setMqtt(mqtt);
13
15
  process.on('unhandledRejection', (reason, p) => {
14
16
  debugError(reason, 'Unhandled Rejection at Promise', p, reason.stack, p.stack);
15
17
  });
16
- process.on('uncaughtException', err => {
18
+ process.on('uncaughtException', (err) => {
17
19
  debugError(err, 'Uncaught Exception thrown', err.stack);
18
20
  });
19
21
 
20
22
  async function dynamicConnect(configObj = {}) {
21
- const fullConfig = {...config, ...configObj};
22
- let con;
23
+ const fullConfig = { ...config, ...configObj };
23
24
 
24
25
  fullConfig.dynamicHost = fullConfig.dynamicHost || fullConfig.defaultDynamicHost;
25
- con = await createHsync(fullConfig);
26
+ const con = await createHsync(fullConfig);
26
27
 
27
28
  return con;
28
29
  }
29
30
 
30
31
  function createConnection(configObj = {}) {
31
- const fullConfig = {...config, ...configObj};
32
+ const fullConfig = { ...config, ...configObj };
32
33
  return createHsync(fullConfig);
33
34
  }
34
35
 
35
- module.exports = {
36
- createConnection,
37
- dynamicConnect,
38
- config,
39
- };
40
-
36
+ export { createConnection, dynamicConnect, config };
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
- const config = require('./config');
2
- const { createConnection } = require('./hsync');
1
+ import config from './config.js';
2
+ import { createConnection } from './hsync.js';
3
3
 
4
4
  const [defaultCon] = config.connections;
5
5
  if (!defaultCon.hsyncServer && !defaultCon.dynamicHost) {
@@ -7,5 +7,5 @@ if (!defaultCon.hsyncServer && !defaultCon.dynamicHost) {
7
7
  }
8
8
 
9
9
  config.connections.forEach((conConfig) => {
10
- const con = createConnection(conConfig);
10
+ createConnection(conConfig);
11
11
  });
package/lib/fetch.js CHANGED
@@ -1,5 +1,3 @@
1
- const fetch = require('isomorphic-fetch');
2
-
3
1
  function getContent(res) {
4
2
  const contentType = res.headers.get('content-type');
5
3
  if (contentType.startsWith('application/json')) {
@@ -30,7 +28,7 @@ function apiFetch(path, options = {}) {
30
28
  }
31
29
  if (options.query) {
32
30
  if (Object.keys(options.query).length) {
33
- qs = `?${(new URLSearchParams(options.query)).toString()}`;
31
+ qs = `?${new URLSearchParams(options.query).toString()}`;
34
32
  }
35
33
  }
36
34
  Object.assign(options, { credentials: 'include' });
@@ -53,4 +51,4 @@ apiFetch.del = (path, body = {}) => {
53
51
  return apiFetch(path, { method: 'DELETE', body });
54
52
  };
55
53
 
56
- module.exports = apiFetch;
54
+ export default apiFetch;
package/lib/peers.js CHANGED
@@ -1,18 +1,19 @@
1
- const rawr = require('rawr');
2
- const b64id = require('b64id');
3
- const debug = require('debug')('hsync:peers');
4
- const EventEmitter = require('events').EventEmitter;
5
- const buffer = require('buffer');
6
- const mqttPacket = require('mqtt-packet-web');
1
+ import rawr from 'rawr';
2
+ import b64id from 'b64id';
3
+ import createDebug from 'debug';
4
+ import { EventEmitter } from 'events';
5
+ import { Buffer } from 'buffer';
6
+ import mqttPacket from 'mqtt-packet';
7
+ import { handleSocketPacket } from './socket-map.js';
8
+ import fetch from './fetch.js';
7
9
 
8
- globalThis.Buffer = buffer.Buffer;
10
+ const debug = createDebug('hsync:peers');
9
11
 
10
- const { handleSocketPacket } = require('./socket-map');
11
- const fetch = require('./fetch');
12
+ globalThis.Buffer = Buffer;
12
13
 
13
14
  function createPacket(topic, payload) {
14
- let payloadStr = payload;
15
- const packet = mqttPacket.generate({
15
+ const payloadStr = payload;
16
+ const packet = mqttPacket.generate({
16
17
  qos: 0,
17
18
  cmd: 'publish',
18
19
  topic,
@@ -30,22 +31,21 @@ function parsePacket(packet) {
30
31
  });
31
32
  }
32
33
 
33
-
34
34
  let rtc;
35
35
 
36
- function setRTC(rtcImpl) {
36
+ export function setRTC(rtcImpl) {
37
37
  rtc = rtcImpl;
38
38
  }
39
39
 
40
- function initPeers(hsyncClient) {
40
+ export function initPeers(hsyncClient) {
41
41
  const cachedPeers = {};
42
42
  const peerLib = new EventEmitter();
43
43
  function getRPCPeer(options = {}) {
44
- const { hostName, temporary, timeout = 10000, hsyncClient } = options;
44
+ const { hostName, temporary, timeout = 10000 } = options;
45
45
  let peer = cachedPeers[hostName];
46
46
  if (!peer) {
47
47
  debug('CREATING peer', hostName);
48
- peer = createRPCPeer({hostName, hsyncClient: this, timeout});
48
+ peer = createRPCPeer({ hostName, hsyncClient: this, timeout });
49
49
  peerLib.emit('peerCreated', peer);
50
50
  peer.myAuth = b64id.generateId();
51
51
  if (temporary) {
@@ -55,7 +55,7 @@ function initPeers(hsyncClient) {
55
55
  }
56
56
  return peer;
57
57
  }
58
-
58
+
59
59
  function createRPCPeer(options = {}) {
60
60
  const { hostName, timeout = 10000, useRTC = true } = options;
61
61
  if (!hostName) {
@@ -66,18 +66,24 @@ function initPeers(hsyncClient) {
66
66
  }
67
67
  const myAuth = b64id.generateId();
68
68
  const transport = new EventEmitter();
69
- const peer = rawr({transport, methods: Object.assign({}, hsyncClient.peerMethods), timeout, idGenerator: b64id.generateId});
69
+ const peer = rawr({
70
+ transport,
71
+ methods: Object.assign({}, hsyncClient.peerMethods),
72
+ timeout,
73
+ idGenerator: b64id.generateId,
74
+ });
70
75
  debug('createRPCPeer rawr', peer);
71
-
76
+
72
77
  peer.hostName = hostName;
73
78
  peer.rtcEvents = new EventEmitter();
74
79
  peer.localMethods = Object.assign({}, hsyncClient.peerMethods);
75
80
  peer.sockets = {};
76
-
81
+
77
82
  peer.localMethods.rtcSignal = async (peerInfo, signal) => {
78
83
  debug('rtcSignal', signal.type);
79
- try{
80
- if (signal.type === 'offer') { // && !peer.rtcCon && !signal.alreadySent) {
84
+ try {
85
+ if (signal.type === 'offer') {
86
+ // && !peer.rtcCon && !signal.alreadySent) {
81
87
  peer.rtcStatus = 'connecting';
82
88
  await rtc.answerPeer(peer, signal);
83
89
  } else if (signal.type === 'answer') {
@@ -87,10 +93,10 @@ function initPeers(hsyncClient) {
87
93
  debug('error handling rtcSignal', e, signal);
88
94
  return e.message;
89
95
  }
90
-
96
+
91
97
  return `rtcSignal ${signal.type} handled ok`;
92
- }
93
-
98
+ };
99
+
94
100
  peer.rtcEvents.on('packet', async (packet) => {
95
101
  debug('↓ on packet', packet);
96
102
  let toParse = packet;
@@ -99,7 +105,7 @@ function initPeers(hsyncClient) {
99
105
  toParse = await packet.arrayBuffer();
100
106
  }
101
107
  const msg = await parsePacket(toParse);
102
- const [p1, p2, p3] = msg.topic.split('/');
108
+ const [p1] = msg.topic.split('/');
103
109
  if (p1 === 'rpc') {
104
110
  const rpcMsg = JSON.parse(msg.payload.toString());
105
111
  debug('↓ peer RTC rpc', rpcMsg);
@@ -125,7 +131,7 @@ function initPeers(hsyncClient) {
125
131
  debug('bad packet', e, packet);
126
132
  }
127
133
  });
128
-
134
+
129
135
  peer.rtcEvents.on('dcOpen', () => {
130
136
  debug(peer.answerer ? 'answerer' : 'offerer', 'dcOpen');
131
137
  peer.packAndSend = (topic, payload) => {
@@ -134,16 +140,16 @@ function initPeers(hsyncClient) {
134
140
  debug('sending test packet', packet);
135
141
  }
136
142
  peer.rtcSend(packet);
137
- }
143
+ };
138
144
  // firefox is weird about the first bit of data, so send a test packet
139
145
  peer.packAndSend('test', 'test');
140
146
  });
141
-
147
+
142
148
  peer.rtcEvents.on('closed', () => {
143
149
  peer.dcOpen = false;
144
150
  delete peer.packAndSend;
145
151
  debug('rtc closed');
146
- for (s in peer.sockets) {
152
+ for (const s in peer.sockets) {
147
153
  try {
148
154
  debug('closing socket', s);
149
155
  peer.sockets[s].destroy();
@@ -153,12 +159,12 @@ function initPeers(hsyncClient) {
153
159
  }
154
160
  }
155
161
  });
156
-
162
+
157
163
  peer.rtcEvents.on('disconnected', () => {
158
164
  peer.dcOpen = false;
159
165
  delete peer.packAndSend;
160
166
  debug('rtc disconnected');
161
- for (s in peer.sockets) {
167
+ for (const s in peer.sockets) {
162
168
  try {
163
169
  debug('closing socket', s);
164
170
  peer.sockets[s].destroy();
@@ -168,26 +174,26 @@ function initPeers(hsyncClient) {
168
174
  }
169
175
  }
170
176
  });
171
-
177
+
172
178
  peer.connectRTC = async () => {
173
179
  debug('connectRTC');
174
180
  peer.rtcStatus = 'connecting';
175
181
  peer.rtcEvents.emit('connecting');
176
- return new Promise(async (resolve, reject) => {
177
- try {
178
- const offer = await rtc.offerPeer(peer);
179
- debug('offer', offer);
182
+ try {
183
+ const offer = await rtc.offerPeer(peer);
184
+ debug('offer', offer);
185
+ return new Promise((resolve) => {
180
186
  peer.rtcEvents.once('dcOpen', () => {
181
187
  peer.rtcStatus = 'connected';
182
188
  debug('offerer dcOpen!');
183
189
  resolve(offer);
184
190
  });
185
- } catch (e) {
186
- debug('error connecting to rtc', e);
187
- peer.rtcStatus = 'error';
188
- reject(e);
189
- }
190
- });
191
+ });
192
+ } catch (e) {
193
+ debug('error connecting to rtc', e);
194
+ peer.rtcStatus = 'error';
195
+ throw e;
196
+ }
191
197
  };
192
198
 
193
199
  peer.sendJSONMsg = (msg) => {
@@ -200,7 +206,7 @@ function initPeers(hsyncClient) {
200
206
  const payload = JSON.stringify(msg);
201
207
  peer.packAndSend('jsonMsg', payload);
202
208
  };
203
-
209
+
204
210
  transport.send = async (msg) => {
205
211
  const fullMsg = {
206
212
  msg,
@@ -208,21 +214,21 @@ function initPeers(hsyncClient) {
208
214
  toHost: hostName,
209
215
  fromHost: hsyncClient.webUrl,
210
216
  };
211
-
217
+
212
218
  debug('↑ peer rpc', peer.dcOpen ? 'RTC' : 'REST', `${hostName}/_hs/rpc`, msg.method);
213
-
219
+
214
220
  let msgObj = msg;
215
221
  if (typeof msg !== 'object') {
216
222
  try {
217
223
  msgObj = JSON.parse(msg);
218
- } catch (e) {
219
- msgObj = {error: 'invalid JSON'};
224
+ } catch (_e) {
225
+ msgObj = { error: 'invalid JSON' };
220
226
  }
221
227
  }
222
228
 
223
229
  // sometimes it takes a while for RTC do detect a disconnect
224
230
  // do not rtcSignal messages over the RTC connection
225
- if (peer.dcOpen && (msgObj.method !== 'rtcSignal')) {
231
+ if (peer.dcOpen && msgObj.method !== 'rtcSignal') {
226
232
  let payload = msg;
227
233
  if (typeof msg === 'object') {
228
234
  payload = JSON.stringify(payload);
@@ -231,32 +237,32 @@ function initPeers(hsyncClient) {
231
237
  peer.rtcSend(packet);
232
238
  return;
233
239
  }
234
-
240
+
235
241
  try {
236
242
  const path = `${hostName}/_hs/rpc`;
237
243
  debug('fetching', path, fullMsg, useRTC);
238
244
  const result = await fetch.post(path, fullMsg);
239
245
  debug('fetch result', result);
240
246
  if (msg.id) {
241
- transport.receiveData({id: msg.id, result, jsonrpc: msg.jsonrpc});
247
+ transport.receiveData({ id: msg.id, result, jsonrpc: msg.jsonrpc });
242
248
  }
243
- } catch(e) {
249
+ } catch (e) {
244
250
  debug('error sending peer RPC request', e);
245
- if (msg.id) { // only send error if it's a request, not a notification
251
+ if (msg.id) {
252
+ // only send error if it's a request, not a notification
246
253
  transport.receiveData({
247
254
  id: msg.id,
248
255
  error: e.message,
249
256
  method: msg.method,
250
- jsonrpc: msg.jsonrpc
257
+ jsonrpc: msg.jsonrpc,
251
258
  });
252
259
  }
253
260
  }
254
-
255
261
  };
256
-
262
+
257
263
  transport.receiveData = (msg) => {
258
264
  debug('↓ transport.receiveData', msg);
259
- if(typeof msg === 'string') {
265
+ if (typeof msg === 'string') {
260
266
  msg = JSON.parse(msg);
261
267
  }
262
268
  debug('↓ peer rpc receivedData', msg);
@@ -267,16 +273,16 @@ function initPeers(hsyncClient) {
267
273
  transport.emit('rpc', msg);
268
274
  // debug('transport emitted', msg);
269
275
  };
270
-
276
+
271
277
  peer.myAuth = myAuth;
272
278
  peer.hostName = hostName;
273
279
  return peer;
274
280
  }
275
-
281
+
276
282
  function createServerPeer(hsyncClient, methods) {
277
283
  const transport = new EventEmitter();
278
284
  transport.send = (msg) => {
279
- if(typeof msg === 'object') {
285
+ if (typeof msg === 'object') {
280
286
  msg = JSON.stringify(msg);
281
287
  }
282
288
  const topic = `srpc/${hsyncClient.myHostName}`;
@@ -284,19 +290,19 @@ function initPeers(hsyncClient) {
284
290
  hsyncClient.mqConn.publish(topic, Buffer.from(msg));
285
291
  };
286
292
  transport.receiveData = (msg) => {
287
- if(msg) {
293
+ if (msg) {
288
294
  msg = JSON.parse(msg);
289
295
  }
290
296
  debug('↓ server rpc inbound', msg);
291
297
  transport.emit('rpc', msg);
292
298
  };
293
- const peer = rawr({transport, methods, timeout: 5000});
299
+ const peer = rawr({ transport, methods, timeout: 5000 });
294
300
  return peer;
295
301
  }
296
302
 
297
303
  hsyncClient.cachedPeers = cachedPeers;
298
304
  hsyncClient.getRPCPeer = getRPCPeer;
299
- hsyncClient.createServerPeer = createServerPeer;
305
+ hsyncClient.createServerPeer = createServerPeer;
300
306
  hsyncClient.createRPCPeer = createRPCPeer;
301
307
 
302
308
  peerLib.cachedPeers = cachedPeers;
@@ -306,8 +312,3 @@ function initPeers(hsyncClient) {
306
312
 
307
313
  return peerLib;
308
314
  }
309
-
310
- module.exports = {
311
- initPeers,
312
- setRTC,
313
- };