Connection: Comment events

...JSDoc's syntax is *bad* and outdated for events :-/
This commit is contained in:
Starbeamrainbowlabs 2021-10-09 16:31:58 +01:00
parent 7d3161433a
commit 0b82e34273
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -17,6 +17,12 @@ import { encrypt_bytes, decrypt_bytes } from '../crypto/secretbox.mjs';
* Represents a connection to a single endpoint. * Represents a connection to a single endpoint.
* @param {string} secret_join The shared join secret, encoded as base64 * @param {string} secret_join The shared join secret, encoded as base64
* @param {net.Socket?} socket Optional. A pre-existing socket to take over and manage. * @param {net.Socket?} socket Optional. A pre-existing socket to take over and manage.
*
* @event Connection#connect The initial connection setup is complete.
* @event Connection#rekey The session key has been re-exchanged.
* @event Connection#destroy The connection has been closed
* @event Connection#message A message has been received.
* @event Connection#message-EVENTNAME A message with a given event name has been received
*/ */
class Connection extends EventEmitter { class Connection extends EventEmitter {
/** /**
@ -74,6 +80,11 @@ class Connection extends EventEmitter {
await this.rekey(); await this.rekey();
// We can await .init() or .connect() - this is just another optiom // We can await .init() or .connect() - this is just another optiom
/**
* The initial connection setup is complete.
* @event Connection#connect
* @type {void}
*/
this.emit(`connect`); this.emit(`connect`);
} }
@ -83,6 +94,11 @@ class Connection extends EventEmitter {
this.session_key = await rekey(this, this.session_key); this.session_key = await rekey(this, this.session_key);
this.rekey_interval = this.rekey_interval_base + crypto.randomInt(0, 15 * 60 * 1000); this.rekey_interval = this.rekey_interval_base + crypto.randomInt(0, 15 * 60 * 1000);
this.rekey_last = new Date(); this.rekey_last = new Date();
/**
* The session key has been re-exchanged.
* @event Connection#rekey
* @type {void}
*/
this.emit("rekey"); this.emit("rekey");
} }
catch(error) { catch(error) {
@ -102,6 +118,11 @@ class Connection extends EventEmitter {
await this.socket.end(); await this.socket.end();
await this.socket.destroy(); await this.socket.destroy();
} }
/**
* The connection has been closed
* @event Connection#destroy
* @type {void}
*/
this.emit("destroy"); this.emit("destroy");
} }
@ -130,7 +151,17 @@ class Connection extends EventEmitter {
// Set and forget here // Set and forget here
this.rekey(); this.rekey();
} }
/**
* A message has been received.
* @event Connection#message
* @type {string,object} The name of the event, followed by the message content.
*/
this.emit("message", msg.event, msg.message); this.emit("message", msg.event, msg.message);
/**
* A message with a specific event name has been received.
* @event Connection#message-EVENTNAME
* @type {object} The message content.
*/
this.emit(`message-${msg.event}`, msg.message); this.emit(`message-${msg.event}`, msg.message);
} }