From 23c8e8f729644ceff4decb1b940cb0e034776f07 Mon Sep 17 00:00:00 2001 From: Dan Jones <dan.jones@noc.ac.uk> Date: Mon, 30 Jan 2023 15:47:26 +0000 Subject: [PATCH] docs: add doc blocks for new adapter methods --- dist/adapter.esm.js | 53 ++++++++++++++++++++++++++++++++++++++---- dist/adapter.js | 55 +++++++++++++++++++++++++++++++++++++++----- src/adapter/index.js | 52 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 144 insertions(+), 16 deletions(-) diff --git a/dist/adapter.esm.js b/dist/adapter.esm.js index 54cd5df..9398f89 100644 --- a/dist/adapter.esm.js +++ b/dist/adapter.esm.js @@ -93,6 +93,10 @@ class Adapter { }); } + /** + * Setup a timer to call poll every interval seconds + * @param {int} interval + */ pollEveryXSeconds(interval) { if (this.pollTimer) clearTimeout(this.pollTimer); this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000); @@ -138,6 +142,11 @@ class Adapter { }); } + /** + * Process a message received either from poll + * or from socket.onMessage + * @param {object} message + */ receive(message) { try { const parsed = JSON.parse(message.message); @@ -153,21 +162,30 @@ class Adapter { } } + /** + * Decide whether to use http or socket for comms + * with the backbone + * @returns string + */ getMode() { return ('mode' in this.config) ? this.config.mode : 'http'; } + /** + * Create a new socket connection and send an authentication + * message. Websocket doesn't support headers so as soon + * as the socket is open a message is sent containing + * the authorisation header to authenticate the connection + */ openSocket() { this.getAuthorizationHeader() .then((headers) => { const protocols = []; - const options = { headers }; - + const socket = new WebSocket( this.config.socket, protocols, - options, ); socket.addEventListener('open', () => { @@ -193,6 +211,11 @@ class Adapter { }); } + /** + * If the socket connection is lost ensure that a new socket + * is opened rather than continuing to send messages on this + * socket + */ closeSocket() { this.socket = null; } @@ -203,7 +226,6 @@ class Adapter { * Messages should be passed through encode before sending * @param {string} topic * @param {string} body - * @param {boolean} is_retry * @returns */ publish(topic, body) { @@ -216,6 +238,13 @@ class Adapter { return response; } + /** + * Send via a POST to /send + * @param {string} topic + * @param {string} body + * @param {boolean} is_retry + * @returns Promise + */ http_publish(topic, body, is_retry = false) { let adapterConfig = this.config; return this.getAuthorizationHeader() @@ -252,6 +281,11 @@ class Adapter { }); } + /** + * Publish via websocket socket.send + * @param {string} topic + * @param {string} body + */ socket_publish(topic, body) { if (this.socket) { this.socket.send(JSON.stringify({ topic, message: body })); @@ -271,7 +305,6 @@ class Adapter { * * Messages should be passed through encode before sending * @param {string} body - * @param {boolean} is_retry * @returns */ broadcast(body) { @@ -284,6 +317,12 @@ class Adapter { return response; } + /** + * Broadcast via POST to /notify + * @param {string} body + * @param {boolean} is_retry + * @returns Promise + */ http_broadcast(body, is_retry = false) { let adapterConfig = this.config; return this.getAuthorizationHeader() @@ -319,6 +358,10 @@ class Adapter { }); } + /** + * Broadcast via websocket socket.send + * @param {string} body + */ socket_broadcast(body) { if (this.socket) { this.socket.send(JSON.stringify({ message: body })); diff --git a/dist/adapter.js b/dist/adapter.js index 58c2f58..a262c18 100644 --- a/dist/adapter.js +++ b/dist/adapter.js @@ -95,6 +95,10 @@ class Adapter { }); } + /** + * Setup a timer to call poll every interval seconds + * @param {int} interval + */ pollEveryXSeconds(interval) { if (this.pollTimer) clearTimeout(this.pollTimer); this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000); @@ -140,6 +144,11 @@ class Adapter { }); } + /** + * Process a message received either from poll + * or from socket.onMessage + * @param {object} message + */ receive(message) { try { const parsed = JSON.parse(message.message); @@ -155,21 +164,30 @@ class Adapter { } } + /** + * Decide whether to use http or socket for comms + * with the backbone + * @returns string + */ getMode() { return ('mode' in this.config) ? this.config.mode : 'http'; } + /** + * Create a new socket connection and send an authentication + * message. Websocket doesn't support headers so as soon + * as the socket is open a message is sent containing + * the authorisation header to authenticate the connection + */ openSocket() { this.getAuthorizationHeader() .then((headers) => { const protocols = []; - const options = { headers }; - + const socket = new WebSocket( this.config.socket, protocols, - options, ); socket.addEventListener('open', () => { @@ -195,6 +213,11 @@ class Adapter { }); } + /** + * If the socket connection is lost ensure that a new socket + * is opened rather than continuing to send messages on this + * socket + */ closeSocket() { this.socket = null; } @@ -205,7 +228,6 @@ class Adapter { * Messages should be passed through encode before sending * @param {string} topic * @param {string} body - * @param {boolean} is_retry * @returns */ publish(topic, body) { @@ -218,7 +240,14 @@ class Adapter { return response; } - http_publish(topic, body, body, is_retry = false) { + /** + * Send via a POST to /send + * @param {string} topic + * @param {string} body + * @param {boolean} is_retry + * @returns Promise + */ + http_publish(topic, body, is_retry = false) { let adapterConfig = this.config; return this.getAuthorizationHeader() .then((headers) => { @@ -254,6 +283,11 @@ class Adapter { }); } + /** + * Publish via websocket socket.send + * @param {string} topic + * @param {string} body + */ socket_publish(topic, body) { if (this.socket) { this.socket.send(JSON.stringify({ topic, message: body })); @@ -273,7 +307,6 @@ class Adapter { * * Messages should be passed through encode before sending * @param {string} body - * @param {boolean} is_retry * @returns */ broadcast(body) { @@ -286,6 +319,12 @@ class Adapter { return response; } + /** + * Broadcast via POST to /notify + * @param {string} body + * @param {boolean} is_retry + * @returns Promise + */ http_broadcast(body, is_retry = false) { let adapterConfig = this.config; return this.getAuthorizationHeader() @@ -321,6 +360,10 @@ class Adapter { }); } + /** + * Broadcast via websocket socket.send + * @param {string} body + */ socket_broadcast(body) { if (this.socket) { this.socket.send(JSON.stringify({ message: body })); diff --git a/src/adapter/index.js b/src/adapter/index.js index e27d9d4..fe7faa1 100644 --- a/src/adapter/index.js +++ b/src/adapter/index.js @@ -93,6 +93,10 @@ export class Adapter { }); } + /** + * Setup a timer to call poll every interval seconds + * @param {int} interval + */ pollEveryXSeconds(interval) { if (this.pollTimer) clearTimeout(this.pollTimer); this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000); @@ -138,6 +142,11 @@ export class Adapter { }); } + /** + * Process a message received either from poll + * or from socket.onMessage + * @param {object} message + */ receive(message) { try { const parsed = JSON.parse(message.message); @@ -153,21 +162,30 @@ export class Adapter { } } + /** + * Decide whether to use http or socket for comms + * with the backbone + * @returns string + */ getMode() { return ('mode' in this.config) ? this.config.mode : 'http'; } + /** + * Create a new socket connection and send an authentication + * message. Websocket doesn't support headers so as soon + * as the socket is open a message is sent containing + * the authorisation header to authenticate the connection + */ openSocket() { this.getAuthorizationHeader() .then((headers) => { const protocols = []; - const options = { headers }; - + const socket = new WebSocket( this.config.socket, protocols, - options, ); socket.addEventListener('open', () => { @@ -193,6 +211,11 @@ export class Adapter { }); } + /** + * If the socket connection is lost ensure that a new socket + * is opened rather than continuing to send messages on this + * socket + */ closeSocket() { this.socket = null; } @@ -203,7 +226,6 @@ export class Adapter { * Messages should be passed through encode before sending * @param {string} topic * @param {string} body - * @param {boolean} is_retry * @returns */ publish(topic, body) { @@ -216,6 +238,12 @@ export class Adapter { return response; } + /** + * Send via a POST to /send + * @param {string} topic + * @param {string} body + * @returns Promise + */ http_publish(topic, body, is_retry = false) { let adapterConfig = this.config; return this.getAuthorizationHeader() @@ -252,6 +280,11 @@ export class Adapter { }); } + /** + * Publish via websocket socket.send + * @param {string} topic + * @param {string} body + */ socket_publish(topic, body) { if (this.socket) { this.socket.send(JSON.stringify({ topic, message: body })); @@ -271,7 +304,6 @@ export class Adapter { * * Messages should be passed through encode before sending * @param {string} body - * @param {boolean} is_retry * @returns */ broadcast(body) { @@ -284,6 +316,12 @@ export class Adapter { return response; } + /** + * Broadcast via POST to /notify + * @param {string} body + * @param {boolean} is_retry + * @returns Promise + */ http_broadcast(body, is_retry = false) { let adapterConfig = this.config; return this.getAuthorizationHeader() @@ -319,6 +357,10 @@ export class Adapter { }); } + /** + * Broadcast via websocket socket.send + * @param {string} body + */ socket_broadcast(body) { if (this.socket) { this.socket.send(JSON.stringify({ message: body })); -- GitLab