+21
-6
| const mqtt = require('mqtt') | ||
| class Hub { | ||
| constructor(accountID, apiKey, clientID) { | ||
| constructor(accountID, apiKey, options) { | ||
| this.accountID = accountID | ||
| this.apiKey = apiKey | ||
| this.handlers = {} | ||
| this.options = options || {} | ||
| } | ||
| _normalizeTopic(topic) { | ||
| return `${this.accountID}/${topic}` | ||
| } | ||
| _denormalizeTopic(topic) { | ||
| return topic.replace(this.accountID + '/', ''); | ||
| } | ||
| connect(handler) { | ||
@@ -21,3 +30,10 @@ this.client = mqtt.connect('mqtt://hub.labstack.com', { | ||
| this.client.on('message', (topic, message) => { | ||
| this.handlers[topic](message) | ||
| topic = this._denormalizeTopic(topic) | ||
| handler = this.handlers[topic] | ||
| if (this.options.messageHandler) { | ||
| this.options.messageHandler(topic, message) | ||
| } | ||
| if (handler) { | ||
| handler(topic, message) | ||
| } | ||
| }) | ||
@@ -29,8 +45,7 @@ // this.client.on('end', () => { | ||
| publish(topic, message) { | ||
| this.client.publish(`${this.accountID}/${topic}`, message) | ||
| this.client.publish(this._normalizeTopic(topic), message) | ||
| } | ||
| subscribe(topic, handler) { | ||
| topic = `${this.accountID}/${topic}` | ||
| this.client.subscribe(topic) | ||
| this.client.subscribe(this._normalizeTopic(topic)) | ||
| this.handlers[topic] = handler | ||
@@ -40,3 +55,3 @@ } | ||
| unsubscribe(self, topic) { | ||
| this.client.unsubscribe(`${this.accountID}/${topic}`) | ||
| this.client.unsubscribe(this._normalizeTopic(topic)) | ||
| } | ||
@@ -43,0 +58,0 @@ |
+1
-1
| { | ||
| "name": "labstack", | ||
| "version": "0.31.8", | ||
| "version": "0.32.0", | ||
| "description": "Official Node.js client library for the LabStack platform", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+9
-5
@@ -5,11 +5,15 @@ const express = require('express') | ||
| const hub = new Hub('ie8t5fgcb6s2vaxgg02y', 'VouXFKK2A1TkuMUVz3wV2zvmapIdRuFM') | ||
| const hub = new Hub('ie8t5fgcb6s2vaxgg02y', 'VouXFKK2A1TkuMUVz3wV2zvmapIdRuFM', { | ||
| messageHandler: function(topic, message) { | ||
| console.log(topic, message) | ||
| } | ||
| }) | ||
| hub.connect(() => { | ||
| console.log('connect') | ||
| }) | ||
| hub.publish('hello', '111') | ||
| // hub.publish('hello', '111') | ||
| // hub.subscribe('hello', (message) => { | ||
| // console.log(message) | ||
| // }) | ||
| hub.subscribe('hello', (topic, message) => { | ||
| console.log(topic, message) | ||
| }) | ||
@@ -16,0 +20,0 @@ // const app = express() |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
55535
0.85%450
3.93%