diff --git a/src/lib/transport/Connection.mjs b/src/lib/transport/Connection.mjs index 3a73c40..c9a45b7 100644 --- a/src/lib/transport/Connection.mjs +++ b/src/lib/transport/Connection.mjs @@ -17,6 +17,12 @@ import { encrypt_bytes, decrypt_bytes } from '../crypto/secretbox.mjs'; * Represents a connection to a single endpoint. * @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. + * + * @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 { /** @@ -74,6 +80,11 @@ class Connection extends EventEmitter { await this.rekey(); // 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`); } @@ -83,6 +94,11 @@ class Connection extends EventEmitter { this.session_key = await rekey(this, this.session_key); this.rekey_interval = this.rekey_interval_base + crypto.randomInt(0, 15 * 60 * 1000); this.rekey_last = new Date(); + /** + * The session key has been re-exchanged. + * @event Connection#rekey + * @type {void} + */ this.emit("rekey"); } catch(error) { @@ -102,6 +118,11 @@ class Connection extends EventEmitter { await this.socket.end(); await this.socket.destroy(); } + /** + * The connection has been closed + * @event Connection#destroy + * @type {void} + */ this.emit("destroy"); } @@ -130,7 +151,17 @@ class Connection extends EventEmitter { // Set and forget here 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); + /** + * 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); }