entangld 2.1.6 → 2.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "entangld",
3
- "version": "2.1.6",
3
+ "version": "2.3.0",
4
4
  "description": "Synchronized key-value stores with RPCs and events",
5
5
  "main": "index.js",
6
6
  "jshintConfig": {
@@ -12,9 +12,9 @@
12
12
  "test": "mocha",
13
13
  "lint:js": "jshint --reporter=node_modules/jshint-stylish index.js test/",
14
14
  "build": "npm run lint:js -s && npm run test -s && npm run docs -s",
15
- "watch:lint:js": "nodemon -w index.js -w test/ -x npm run lint:js -s",
16
- "dev": "nodemon -w index.js -w test/ -x npm run build -s",
17
- "docs": "node generate_docs.js"
15
+ "docs": "node generate_docs.js",
16
+ "lint": "npx eslint lib test index.js || true",
17
+ "lint:fix": "npx eslint --fix lib test index.js || true"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",
@@ -22,16 +22,15 @@
22
22
  },
23
23
  "author": "c0d3",
24
24
  "contributors": [
25
- "Wilkins White <wilkins.white@daxbot.com>",
26
- "Jonathan Van Schenck <jon.vanschenck@daxbot.com>"
25
+ "Wilkins White <ww@novadynamics.com>",
26
+ "Jonathan Van Schenck <jvschenck@novadynamics.com>"
27
27
  ],
28
28
  "license": "MIT",
29
29
  "homepage": "https://daxbot.com/open-source",
30
30
  "devDependencies": {
31
- "fs-then-native": "^2.0.0",
31
+ "eslint": "^8.35.0",
32
+ "eslint-plugin-node": "^11.1.0",
32
33
  "jsdoc-to-markdown": "^7.1.1",
33
- "jshint": "^2.13.1",
34
- "jshint-stylish": "^2.2.1",
35
34
  "mocha": "^10.0.0",
36
35
  "nodemon": "^2.0.16",
37
36
  "sockhop": "^1.4.3"
package/test/circular.js CHANGED
@@ -1,9 +1,5 @@
1
1
  var Entangld=require("../index.js");
2
2
 
3
- function wait (ms) {
4
- return new Promise(res=>setTimeout(res,ms));
5
- }
6
-
7
3
  describe("Circular References", function() {
8
4
  let s,a,b;
9
5
 
@@ -23,9 +19,9 @@ describe("Circular References", function() {
23
19
  });
24
20
 
25
21
  afterEach(function() {
26
- delete s;
27
- delete a;
28
- delete b;
22
+ delete s; // eslint-disable-line no-delete-var
23
+ delete a; // eslint-disable-line no-delete-var
24
+ delete b; // eslint-disable-line no-delete-var
29
25
  });
30
26
 
31
27
  it("Circular subscriptions don't break", function(done) {
package/test/events.js CHANGED
@@ -18,6 +18,29 @@ describe("Events",()=>{
18
18
  a.transmit((msg, store)=>store.receive(msg, a));
19
19
  b.transmit((msg, store)=>store.receive(msg, b));
20
20
 
21
+
22
+ // Original tests were written with globals and are thus not independent. I'm adding (JLS 2023-02-21) some "clean" scaffolding.
23
+ // TODO - we should eventually rewrite tests to use these new objects.
24
+ let A,B,S;
25
+ beforeEach(()=>{
26
+
27
+ S=new Entangld();
28
+ A=new Entangld();
29
+ B=new Entangld();
30
+
31
+ S.attach("a",A);
32
+ S.attach("b",B);
33
+
34
+ A.attach("parent", S);
35
+ B.attach("parent", S);
36
+
37
+ S.transmit((msg, store)=>store.receive(msg, S));
38
+ A.transmit((msg, store)=>store.receive(msg, A));
39
+ B.transmit((msg, store)=>store.receive(msg, B));
40
+
41
+ });
42
+
43
+
21
44
  it("Subscribe to local event (exact)", (done)=>{
22
45
 
23
46
  s.subscribe("my.own.event",(path, val)=>{
@@ -53,6 +76,22 @@ describe("Events",()=>{
53
76
  a.set("system.voltage",21);
54
77
  });
55
78
 
79
+ it("Subscribe to remote event (wildcard)", (done)=>{
80
+
81
+ S.subscribe("a.*.voltage",(path, val)=>{
82
+
83
+ assert.strictEqual(path,"a.system.voltage");
84
+ assert.strictEqual(val, 21);
85
+
86
+ // Check for proper internals
87
+ assert.strictEqual(s._subscriptions.length,1);
88
+ assert.strictEqual(a._subscriptions.length,1);
89
+ done();
90
+ });
91
+
92
+ A.set("system.voltage",21);
93
+ });
94
+
56
95
  it("Subscribe to remote event (descendant of subscribed path also triggers event)", (done)=>{
57
96
 
58
97
  s.subscribe("a.flowers.roses",(path, val)=>{
package/test/sockets.js CHANGED
@@ -1,16 +1,16 @@
1
- const Entangld=require("../index.js");
2
- const sockhop=require("sockhop");
3
- const assert=require("assert");
1
+ const Entangld=require("../lib/Datastore.js");
2
+ const sockhop=require("sockhop"); // eslint-disable-line node/no-unpublished-require
4
3
 
5
4
  function wait (ms) {
6
5
  return new Promise(res=>setTimeout(res,ms));
7
6
  }
8
7
 
9
8
  // FIXME : comment out for debugging
10
- console.log = () => {};
9
+ // const _log = console.log;
10
+ const _log = () => {};
11
11
 
12
12
  describe("Sockets", function() {
13
- let server,client,sock_ref,s,a,b;
13
+ let server,client,sock_ref,s,a;
14
14
 
15
15
  beforeEach(function() {
16
16
  s = new Entangld();
@@ -19,21 +19,21 @@ describe("Sockets", function() {
19
19
  server = new sockhop.server({
20
20
  address : "localhost",
21
21
  port : 9876
22
- })
23
- server.on("receive", (o, meta) => { console.log("s.r",o); s.receive(o, meta.socket) } );
22
+ });
23
+ server.on("receive", (o, meta) => { _log("s.r",o); s.receive(o, meta.socket); } );
24
24
  client = new sockhop.client({
25
25
  address : "localhost",
26
26
  port : 9876
27
- })
28
- client.on("receive", (o, meta) => { console.log("c.r",o); a.receive(o, client) });
27
+ });
28
+ client.on("receive", (o) => { _log("c.r",o); a.receive(o, client); });
29
29
 
30
30
 
31
31
  s.transmit((msg, socket) => { // server's transmit to net-sockets
32
- console.log("s.t",msg);
32
+ _log("s.t",msg);
33
33
  if ( socket.readyState == 'open' ) server.send(socket, msg);
34
34
  });
35
35
  a.transmit((msg, client) => { // client's transmit to sockhop client
36
- console.log("a.t",msg);
36
+ _log("a.t",msg);
37
37
  if ( client.connected ) client.send(msg);
38
38
  });
39
39
 
@@ -46,11 +46,11 @@ describe("Sockets", function() {
46
46
  });
47
47
 
48
48
  afterEach(function() {
49
- delete s;
50
- delete a;
49
+ delete s; // eslint-disable-line no-delete-var
50
+ delete a; // eslint-disable-line no-delete-var
51
51
  server.close();
52
- delete server;
53
- delete client;
52
+ delete server; // eslint-disable-line no-delete-var
53
+ delete client; // eslint-disable-line no-delete-var
54
54
  });
55
55
 
56
56
  it("Can subscribe", function(done) {
@@ -86,7 +86,7 @@ describe("Sockets", function() {
86
86
  while ( sock_ref.readyState !== "open" ) await wait(1);
87
87
  s.subscribe("path1.a.data");
88
88
  while ( a._subscriptions.length < 1 ) await wait(1);
89
- s.detach("path1.a")
89
+ s.detach("path1.a");
90
90
  s.attach("path1.a",new Entangld());
91
91
  await wait(5); // set messages clear
92
92
  if ( a._subscriptions.length !== 0 ) done(new Error("Subscription never removed"));
@@ -102,7 +102,7 @@ describe("Sockets", function() {
102
102
  s.subscribe("path1.a.data");
103
103
  while ( a._subscriptions.length < 1 ) await wait(1);
104
104
  client.disconnect(); // kill the socket
105
- s.detach("path1.a")
105
+ s.detach("path1.a");
106
106
  s.attach("path1.a",new Entangld());
107
107
  await wait(5); // set messages clear
108
108
  if ( a._subscriptions.length !== 1 ) done(new Error("Subscription was removed!?"));
@@ -128,8 +128,8 @@ describe("Sockets", function() {
128
128
  client = new sockhop.client({
129
129
  address : "localhost",
130
130
  port : 9876
131
- })
132
- client.on("receive", (o, meta) => { console.log("c.r",o); a.receive(o, client) });
131
+ });
132
+ client.on("receive", (o) => { _log("c.r",o); a.receive(o, client); });
133
133
  client.connect();
134
134
  while ( !sock_ref ) await wait(1);
135
135
  while ( sock_ref.readyState !== "open" ) await wait(1);
@@ -19,9 +19,9 @@ describe("Subscription", function() {
19
19
  });
20
20
 
21
21
  afterEach(function() {
22
- delete s;
23
- delete a;
24
- delete b;
22
+ delete s; // eslint-disable-line no-delete-var
23
+ delete a; // eslint-disable-line no-delete-var
24
+ delete b; // eslint-disable-line no-delete-var
25
25
  });
26
26
 
27
27
  it("Should only trigger subscriptions on an exact match", function(done) {
@@ -35,14 +35,14 @@ describe("Subscription", function() {
35
35
  });
36
36
 
37
37
  it("Should emit on recieved subscriptions (locally)", function(done) {
38
- s.on("subscription", ( path, uuid ) => {
38
+ s.on("subscription", ( path ) => {
39
39
  if ( path === "test" ) { done(); return; }
40
40
  assert.fail("Subcription yield path " + path);
41
41
  });
42
42
  s.subscribe('test', () => {});
43
43
  });
44
44
  it("Should emit on recieved unsubscriptions (locally)", function(done) {
45
- s.on("unsubscription", ( path, uuid ) => {
45
+ s.on("unsubscription", ( path ) => {
46
46
  if ( path === "test" ) { done(); return; }
47
47
  assert.fail("Subcription yield path " + path);
48
48
  });
@@ -51,14 +51,14 @@ describe("Subscription", function() {
51
51
  });
52
52
 
53
53
  it("Should emit on recieved subscriptions (remote)", function(done) {
54
- a.on("subscription", ( path, uuid ) => {
54
+ a.on("subscription", ( path ) => {
55
55
  if ( path === "some_data" ) { done(); return; }
56
56
  assert.fail("Subcription yield path " + path);
57
57
  });
58
58
  s.subscribe('path1.A.some_data', () => {});
59
59
  });
60
60
  it("Should emit on recieved unsubscriptions (remote)", function(done) {
61
- a.on("unsubscription", ( path, uuid ) => {
61
+ a.on("unsubscription", ( path ) => {
62
62
  if ( path === "some_data" ) { done(); return; }
63
63
  assert.fail("Subcription yield path " + path);
64
64
  });
@@ -67,7 +67,7 @@ describe("Subscription", function() {
67
67
  });
68
68
 
69
69
  it("Shouldn't emit on sent subscriptions (remote)", function(done) {
70
- s.on("subscription", ( path, uuid ) => {
70
+ s.on("subscription", () => {
71
71
  assert.fail("this shouldn't run");
72
72
  });
73
73
  s.subscribe('path1.A.path3.B.something', () => {});
@@ -75,7 +75,7 @@ describe("Subscription", function() {
75
75
  done();
76
76
  });
77
77
  it("Shouldn't emit on sent unsubscriptions (remote)", function(done) {
78
- s.on("unsubscription", ( path, uuid ) => {
78
+ s.on("unsubscription", () => {
79
79
  assert.fail("this shouldn't run");
80
80
  });
81
81
  s.subscribe('path1.A.path3.B.something', () => {});
@@ -84,7 +84,7 @@ describe("Subscription", function() {
84
84
  done();
85
85
  });
86
86
  it("Shouldn't emit on passthrough subscriptions (remote)", function(done) {
87
- a.on("subscription", ( path, uuid ) => {
87
+ a.on("subscription", () => {
88
88
  assert.fail("this shouldn't run");
89
89
  });
90
90
  s.subscribe('path1.A.path3.B.something', () => {});
@@ -92,7 +92,7 @@ describe("Subscription", function() {
92
92
  done();
93
93
  });
94
94
  it("Shouldn't emit on passthrough unsubscriptions (remote)", function(done) {
95
- a.on("unsubscription", ( path, uuid ) => {
95
+ a.on("unsubscription", () => {
96
96
  assert.fail("this shouldn't run");
97
97
  });
98
98
  s.subscribe('path1.A.path3.B.something', () => {});
@@ -103,7 +103,7 @@ describe("Subscription", function() {
103
103
 
104
104
  it("Emit on each subscription", function(done) {
105
105
  let count = 0;
106
- b.on("subscription", ( path, uuid ) => {
106
+ b.on("subscription", ( path ) => {
107
107
  count += 1;
108
108
  assert.strictEqual(path, "something");
109
109
  });
@@ -118,8 +118,8 @@ describe("Subscription", function() {
118
118
 
119
119
  var count = 0;
120
120
  // Create two subscriptions
121
- s.subscribe("some_data", () => {count += 1});
122
- s.subscribe("some_data", () => {count += 1});
121
+ s.subscribe("some_data", () => {count += 1;});
122
+ s.subscribe("some_data", () => {count += 1;});
123
123
 
124
124
  s.set('some_data', 0.0);
125
125
 
@@ -130,7 +130,7 @@ describe("Subscription", function() {
130
130
 
131
131
  it("Can (not) throttle subscription messages (locally)", (done) => {
132
132
  var count = 0;
133
- s.subscribe("some_data", (path, value) => { count += 1 }, 1)
133
+ s.subscribe("some_data", () => { count += 1; }, 1);
134
134
  for ( let i = 0; i < 10; i ++ ) {
135
135
  s.set("some_data", 1);
136
136
  }
@@ -140,7 +140,7 @@ describe("Subscription", function() {
140
140
 
141
141
  it("Can throttle subscription messages (locally)", (done) => {
142
142
  var count = 0;
143
- s.subscribe("some_data", (path, value) => { count += 1 }, 2)
143
+ s.subscribe("some_data", () => { count += 1; }, 2);
144
144
  for ( let i = 0; i < 10; i ++ ) {
145
145
  s.set("some_data", 1);
146
146
  }
@@ -150,7 +150,7 @@ describe("Subscription", function() {
150
150
 
151
151
  it("Can throttle subscription messages (remote)", (done) => {
152
152
  var count = 0;
153
- s.subscribe("path1.A.path3.B.some_data", (path, value) => { count += 1 }, 2)
153
+ s.subscribe("path1.A.path3.B.some_data", () => { count += 1; }, 2);
154
154
  for ( let i = 0; i < 10; i ++ ) {
155
155
  b.set("some_data", 1);
156
156
  }
@@ -162,8 +162,8 @@ describe("Subscription", function() {
162
162
 
163
163
  var count = 0;
164
164
  // Create two subscriptions
165
- s.subscribe("some_data", () => {count += 1});
166
- s.subscribe("some_data", () => {count += 1});
165
+ s.subscribe("some_data", () => {count += 1;});
166
+ s.subscribe("some_data", () => {count += 1;});
167
167
 
168
168
  s.set('some_data', 0.0);
169
169
 
@@ -176,8 +176,8 @@ describe("Subscription", function() {
176
176
 
177
177
  var count = 0;
178
178
  // Create two subscriptions
179
- s.subscribe("path1.A.some_data", () => {count += 1});
180
- s.subscribe("path1.A.some_data", () => {count += 1});
179
+ s.subscribe("path1.A.some_data", () => {count += 1;});
180
+ s.subscribe("path1.A.some_data", () => {count += 1;});
181
181
 
182
182
  a.set('some_data', 0.0);
183
183
 
@@ -190,26 +190,26 @@ describe("Subscription", function() {
190
190
  s.subscribe("num1", () => {});
191
191
  s.subscribe("path1.A.num1", () => {});
192
192
  s.subscribe("path1.A.path3.B.num1", () => {});
193
- assert.strictEqual(s.subscriptions.length, 3)
194
- assert.strictEqual(a.subscriptions.length, 2)
195
- assert.strictEqual(b.subscriptions.length, 1)
193
+ assert.strictEqual(s.subscriptions.length, 3);
194
+ assert.strictEqual(a.subscriptions.length, 2);
195
+ assert.strictEqual(b.subscriptions.length, 1);
196
196
  done();
197
- });
197
+ });
198
198
 
199
199
  it("Unsubscribing paths removes all paths", (done)=>{
200
200
 
201
201
  var count = 0;
202
202
  // Create two subscriptions
203
- s.subscribe("some_data", () => {count += 1});
204
- s.subscribe("some_data", () => {count += 1});
203
+ s.subscribe("some_data", () => {count += 1;});
204
+ s.subscribe("some_data", () => {count += 1;});
205
205
 
206
- s.unsubscribe("some_data")
206
+ s.unsubscribe("some_data");
207
207
 
208
208
  s.set('some_data', 0.0);
209
209
 
210
210
  assert.strictEqual(count, 0);
211
211
 
212
- done()
212
+ done();
213
213
  });
214
214
 
215
215
 
@@ -217,10 +217,10 @@ describe("Subscription", function() {
217
217
 
218
218
  var count = 0;
219
219
  // Create two subscriptions
220
- let uuid1 = s.subscribe("some_data", () => {count += 1});
221
- let uuid2 = s.subscribe("some_data", () => {count += 1});
220
+ let uuid1 = s.subscribe("some_data", () => {count += 1;});
221
+ let uuid2 = s.subscribe("some_data", () => {count += 1;});
222
222
 
223
- s.unsubscribe(uuid1)
223
+ s.unsubscribe(uuid1);
224
224
 
225
225
  s.set('some_data', 0.0);
226
226
 
@@ -228,28 +228,28 @@ describe("Subscription", function() {
228
228
  assert.strictEqual(s._subscriptions.length, 1);
229
229
  assert.strictEqual(s._subscriptions[0].uuid, uuid2);
230
230
 
231
- done()
231
+ done();
232
232
  });
233
233
 
234
234
  it("Pairs of remote stores can subscribe to the same data", (done)=>{
235
235
 
236
236
  var count = 0;
237
- s.subscribe("path2.B.some_data", () => {count += 1});
238
- a.subscribe("path3.B.some_data", () => {count += 1})
237
+ s.subscribe("path2.B.some_data", () => {count += 1;});
238
+ a.subscribe("path3.B.some_data", () => {count += 1;});
239
239
 
240
240
  b.set("some_data",0.0);
241
241
 
242
242
  assert.strictEqual(count, 2);
243
243
 
244
- done()
244
+ done();
245
245
  });
246
246
 
247
247
 
248
248
  it("Pairs of remote stores don't unsubscribe each other", (done)=>{
249
249
 
250
250
  var count = 0;
251
- let uuid1 = s.subscribe("path2.B.some_data", () => {count += 1});
252
- let uuid2 = a.subscribe("path3.B.some_data", () => {count += 1})
251
+ s.subscribe("path2.B.some_data", () => {count += 1;});
252
+ let uuid2 = a.subscribe("path3.B.some_data", () => {count += 1;});
253
253
 
254
254
  s.unsubscribe("path2.B.some_data");
255
255
 
@@ -259,7 +259,7 @@ describe("Subscription", function() {
259
259
  assert.strictEqual(b._subscriptions.length, 1);
260
260
  assert.strictEqual(b._subscriptions[0].uuid, uuid2);
261
261
 
262
- done()
262
+ done();
263
263
  });
264
264
 
265
265
  it("Orphaned remote subscriptions are successfully cleaned up", (done) => {
@@ -270,11 +270,11 @@ describe("Subscription", function() {
270
270
 
271
271
  s.transmit((msg, store)=> {
272
272
  if (msg.type==="unsubscribe") number_of_cancels_sent += 1;
273
- store.receive(msg, s)
273
+ store.receive(msg, s);
274
274
  });
275
275
  a.transmit((msg, store)=> {
276
276
  if (msg.type==="event") number_of_events += 1;
277
- store.receive(msg, a)
277
+ store.receive(msg, a);
278
278
  });
279
279
 
280
280
 
@@ -283,32 +283,32 @@ describe("Subscription", function() {
283
283
  // orphan the pass through subscription object in A
284
284
  s._subscriptions = [];
285
285
 
286
- a.set("some_data",0.0)
286
+ a.set("some_data",0.0);
287
287
 
288
288
  // The cancel message was sent from S to A
289
- assert.strictEqual(number_of_cancels_sent,1)
290
- assert.strictEqual(number_of_events,1)
289
+ assert.strictEqual(number_of_cancels_sent,1);
290
+ assert.strictEqual(number_of_events,1);
291
291
 
292
- a.set("some_data",0.0)
292
+ a.set("some_data",0.0);
293
293
 
294
294
  // A didn't sent the next next event
295
- assert.strictEqual(number_of_events,1)
295
+ assert.strictEqual(number_of_events,1);
296
296
 
297
297
  done();
298
- })
298
+ });
299
299
 
300
300
  it("Local sets only triggers a single subscribed callback when multiple subscriptions co-exist", (done)=>{
301
301
 
302
302
  // Subscribe once to a local endpoint
303
303
  var sub_1_triggers = 0;
304
- s.subscribe("some_data",(path, val)=>{
304
+ s.subscribe("some_data",()=>{
305
305
  sub_1_triggers += 1;
306
306
  assert.strictEqual(sub_1_triggers, 1);
307
307
  });
308
308
 
309
309
  // Subscribe again to the same local endpoint
310
310
  var sub_2_triggers = 0;
311
- s.subscribe("some_data",(path, val)=>{
311
+ s.subscribe("some_data",()=>{
312
312
  sub_2_triggers += 1;
313
313
  assert.strictEqual(sub_2_triggers, 1);
314
314
  });
@@ -322,14 +322,14 @@ describe("Subscription", function() {
322
322
 
323
323
  // Subscribe once to a remote endpoint
324
324
  var sub_1_triggers = 0;
325
- s.subscribe("path1.A.some_data",(path, val)=>{
325
+ s.subscribe("path1.A.some_data",()=>{
326
326
  sub_1_triggers += 1;
327
327
  assert.strictEqual(sub_1_triggers, 1);
328
328
  });
329
329
 
330
330
  // Subscribe again to the same remote endpoint
331
331
  var sub_2_triggers = 0;
332
- s.subscribe("path1.A.some_data",(path, val)=>{
332
+ s.subscribe("path1.A.some_data",()=>{
333
333
  sub_2_triggers += 1;
334
334
  assert.strictEqual(sub_2_triggers, 1);
335
335
  });