aes70 2.0.3 → 2.0.4

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/dist/AES70.es5.js CHANGED
@@ -61,6 +61,10 @@
61
61
  * @param {Function} cb - Callback function.
62
62
  */
63
63
  on(name, cb) {
64
+ if (typeof name !== 'string')
65
+ throw new TypeError('Event name must be a string.');
66
+ if (typeof cb !== 'function')
67
+ throw new TypeError('Event handler must be a function.');
64
68
  let handlers = this.event_handlers.get(name);
65
69
 
66
70
  if (!handlers) {
@@ -90,12 +94,36 @@
90
94
  handlers.delete(cb);
91
95
  }
92
96
 
97
+ /**
98
+ * Removes an event handler.
99
+ *
100
+ * @param {strign} name
101
+ * @param {Function} cb
102
+ */
103
+ off(name, cb) {
104
+ this.removeEventListener(name, cb);
105
+ }
106
+
93
107
  /**
94
108
  * Removes all event listeners.
95
109
  */
96
110
  removeAllEventListeners() {
97
111
  this.event_handlers.clear();
98
112
  }
113
+
114
+ /**
115
+ *
116
+ * @param {string} name
117
+ * @param {Function} cb
118
+ */
119
+ subscribe(name, cb) {
120
+ this.on(name, cb);
121
+ return () => {
122
+ if (name === undefined) return;
123
+ this.off(name, cb);
124
+ name = undefined;
125
+ };
126
+ }
99
127
  }
100
128
 
101
129
  class PDU {
@@ -1185,6 +1213,12 @@
1185
1213
  * Keepalive interval in seconds.
1186
1214
  */
1187
1215
  set_keepalive_interval(seconds) {
1216
+ if (!(seconds <= 10)) {
1217
+ console.warn(
1218
+ 'Unusually large keepalive interval %o seconds. Confusion of ms vs. seconds?'
1219
+ );
1220
+ }
1221
+
1188
1222
  const t = seconds * 1000;
1189
1223
 
1190
1224
  if (this._keepalive_interval_id !== null) {
@@ -1782,11 +1816,45 @@
1782
1816
  if (!(o.time > 0)) {
1783
1817
  throw new Error('Bad keepalive timeout.');
1784
1818
  }
1819
+ this.emit('keepalive', o);
1785
1820
  } else {
1786
1821
  throw new Error('Unexpected PDU');
1787
1822
  }
1788
1823
  }
1789
1824
  }
1825
+
1826
+ /**
1827
+ * Activates keepalive handling (using set_keepalive_interval) and waits for
1828
+ * at least one keepalive packet to arrive. If no keepalive message is received,
1829
+ * the connection will be closed and the returned promise will reject.
1830
+ * @param {number} interval
1831
+ * Keepalive interval in seconds.
1832
+ */
1833
+ wait_for_keepalive(interval) {
1834
+ return new Promise((resolve, reject) => {
1835
+ const subscriptions = [];
1836
+
1837
+ const cleanup = () => {
1838
+ subscriptions.forEach((cb) => cb());
1839
+ subscriptions.length = 0;
1840
+ };
1841
+ subscriptions.push(
1842
+ this.subscribe('error', (error) => {
1843
+ reject(error);
1844
+ cleanup();
1845
+ }),
1846
+ this.subscribe('close', () => {
1847
+ reject(new CloseError());
1848
+ cleanup();
1849
+ }),
1850
+ this.subscribe('keepalive', () => {
1851
+ resolve();
1852
+ cleanup();
1853
+ })
1854
+ );
1855
+ this.set_keepalive_interval(interval);
1856
+ });
1857
+ }
1790
1858
  }
1791
1859
 
1792
1860
  function getLengthEncoder(byteLength) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aes70",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "A controller library for the AES70 protocol.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/connection.js CHANGED
@@ -186,6 +186,12 @@ export class Connection extends Events {
186
186
  * Keepalive interval in seconds.
187
187
  */
188
188
  set_keepalive_interval(seconds) {
189
+ if (!(seconds <= 10)) {
190
+ console.warn(
191
+ 'Unusually large keepalive interval %o seconds. Confusion of ms vs. seconds?'
192
+ );
193
+ }
194
+
189
195
  const t = seconds * 1000;
190
196
 
191
197
  if (this._keepalive_interval_id !== null) {
@@ -33,4 +33,13 @@ export interface PendingCommand {
33
33
  */
34
34
  export declare class ClientConnection extends Connection {
35
35
  constructor(options: IClientConnectionOptions);
36
+
37
+ /**
38
+ * Activates keepalive handling (using set_keepalive_interval) and waits for
39
+ * at least one keepalive packet to arrive. If no keepalive message is received,
40
+ * the connection will be closed and the returned promise will reject.
41
+ * @param {number} interval
42
+ * Keepalive interval in seconds.
43
+ */
44
+ wait_for_keepalive(interval: number): Promise<void>;
36
45
  }
@@ -241,9 +241,43 @@ export class ClientConnection extends Connection {
241
241
  if (!(o.time > 0)) {
242
242
  throw new Error('Bad keepalive timeout.');
243
243
  }
244
+ this.emit('keepalive', o);
244
245
  } else {
245
246
  throw new Error('Unexpected PDU');
246
247
  }
247
248
  }
248
249
  }
250
+
251
+ /**
252
+ * Activates keepalive handling (using set_keepalive_interval) and waits for
253
+ * at least one keepalive packet to arrive. If no keepalive message is received,
254
+ * the connection will be closed and the returned promise will reject.
255
+ * @param {number} interval
256
+ * Keepalive interval in seconds.
257
+ */
258
+ wait_for_keepalive(interval) {
259
+ return new Promise((resolve, reject) => {
260
+ const subscriptions = [];
261
+
262
+ const cleanup = () => {
263
+ subscriptions.forEach((cb) => cb());
264
+ subscriptions.length = 0;
265
+ };
266
+ subscriptions.push(
267
+ this.subscribe('error', (error) => {
268
+ reject(error);
269
+ cleanup();
270
+ }),
271
+ this.subscribe('close', () => {
272
+ reject(new CloseError());
273
+ cleanup();
274
+ }),
275
+ this.subscribe('keepalive', () => {
276
+ resolve();
277
+ cleanup();
278
+ })
279
+ );
280
+ this.set_keepalive_interval(interval);
281
+ });
282
+ }
249
283
  }
package/src/events.d.ts CHANGED
@@ -24,8 +24,19 @@ export class Events {
24
24
  */
25
25
  removeEventListener(name: string, cb: (...args) => void): void;
26
26
 
27
+ /**
28
+ * Unsubscribe from an event.
29
+ */
30
+ off(name: string, cb: (...args) => void): void;
31
+
27
32
  /**
28
33
  * Removes all event listeners.
29
34
  */
30
35
  removeAllEventListeners(): void;
36
+
37
+ /**
38
+ * Subscribe to an event. Returns a cleanup function which can be called to
39
+ * unsubscribe.
40
+ */
41
+ subscribe(name: string, cb: (...args) => void): () => void;
31
42
  }
package/src/events.js CHANGED
@@ -34,6 +34,10 @@ export class Events {
34
34
  * @param {Function} cb - Callback function.
35
35
  */
36
36
  on(name, cb) {
37
+ if (typeof name !== 'string')
38
+ throw new TypeError('Event name must be a string.');
39
+ if (typeof cb !== 'function')
40
+ throw new TypeError('Event handler must be a function.');
37
41
  let handlers = this.event_handlers.get(name);
38
42
 
39
43
  if (!handlers) {
@@ -63,10 +67,34 @@ export class Events {
63
67
  handlers.delete(cb);
64
68
  }
65
69
 
70
+ /**
71
+ * Removes an event handler.
72
+ *
73
+ * @param {strign} name
74
+ * @param {Function} cb
75
+ */
76
+ off(name, cb) {
77
+ this.removeEventListener(name, cb);
78
+ }
79
+
66
80
  /**
67
81
  * Removes all event listeners.
68
82
  */
69
83
  removeAllEventListeners() {
70
84
  this.event_handlers.clear();
71
85
  }
86
+
87
+ /**
88
+ *
89
+ * @param {string} name
90
+ * @param {Function} cb
91
+ */
92
+ subscribe(name, cb) {
93
+ this.on(name, cb);
94
+ return () => {
95
+ if (name === undefined) return;
96
+ this.off(name, cb);
97
+ name = undefined;
98
+ };
99
+ }
72
100
  }