upgrade to esm from @std/esm

master
David Kebler 2018-05-16 07:20:33 -07:00
parent 5dda6b1e9e
commit 8ac552e9a8
7 changed files with 34 additions and 47 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/node_modules/
/coverage/
*.lock

19
example/example.js Normal file
View File

@ -0,0 +1,19 @@
import Client from '../src/client'
// let mqtt = new Client({id:'example-mqtt-client', url:'tcp://trantor:1883', topics:'test1'})
let mqtt = new Client({id:'example-mqtt-client', connect:{host:'localhost', port:1883}, topics:'topic1'})
;
(async () => {
await mqtt.connect()
await mqtt.send({cmd:'test', status:'sent to topic1 implicitly'})
await mqtt.subscribe('topic2')
await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 explicitly'})
await mqtt.unsubscribe('topic2')
await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 with subscription'})
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})

View File

@ -1,19 +0,0 @@
import Client from '../src/client'
// let mqtt = new Client({id:'example-mqtt-client', url:'tcp://trantor:1883', topics:'test1'})
let mqtt = new Client({id:'example-mqtt-client', connect:{host:'localhost', port:1883}, topics:'test1'})
;
(async () => {
await mqtt.connect()
await mqtt.send({cmd:'test', status:'I\'m good'})
await mqtt.subscribe('test2',{pub:true})
await mqtt.send({cmd:'test', status:'I\'m good'})
await mqtt.unsubscribe('test2')
await mqtt.send({cmd:'test', status:'I\'m good'})
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})

View File

@ -1,14 +1,14 @@
{
"name": "@uci/mqtt",
"version": "0.0.1",
"version": "0.0.5",
"description": "mqtt client with json payloads and mqtt custom broker",
"main": "src",
"scripts": {
"testw": "mocha -r @std/esm test/*.test.mjs --watch --recurse --watch-extensions mjs",
"test": "mocha -r @std/esm test/*.test.mjs",
"testw": "mocha -r esm test/*.test.mjs --watch --recurse --watch-extensions mjs",
"test": "mocha -r esm test/*.test.mjs",
"testci": "istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- -R spec --recursive && codecov || true",
"example": "node -r @std/esm example/example",
"dev": "./node_modules/.bin/nodemon -r @std/esm -e mjs example/example"
"example": "node -r esm example/example",
"dev": "./node_modules/.bin/nodemon -r esm-e mjs example/example"
},
"author": "David Kebler",
"license": "MIT",
@ -26,7 +26,7 @@
},
"homepage": "https://github.com/uCOMmandIt/uci-changeme#readme",
"dependencies": {
"@uci/logger": "0.0.1",
"@uci/logger": "^0.0.3",
"async-mqtt": "^1.0.1",
"better-try-catch": "^0.6.2",
"lodash.isarray": "^4.0.0",
@ -34,14 +34,13 @@
"lodash.union": "^4.6.0",
"lodash.xor": "^4.5.0"
},
"@std/esm": "cjs",
"devDependencies": {
"@std/esm": "^0.18.0",
"nodemon": "^1.14.3",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"codecov": "^3.0.0",
"esm": "^3.0.21",
"istanbul": "^0.4.5",
"mocha": "^4.0.1"
"mocha": "^4.0.1",
"nodemon": "^1.14.3"
}
}

View File

View File

@ -7,14 +7,12 @@ import logger from '@uci/logger'
let log = {}
export default class Client {
constructor (opts={}) {
this.id = opts.id || opts.name || 'mqtt:'+ new Date().getTime()
this.url = opts.url
// subscription topics can be string of commna delimited or array of strings see object see mqtt.js docs
this.topics = Array.isArray(opts.topics) ? opts.topics : (opts.topics ? opts.topics.split(',') : [this.id])
console.log(this.topics)
this.opts = opts.connect || {} // see options for new mqtt.Client
// self bindings
this.connect = this.connect.bind(this)
@ -70,11 +68,12 @@ export default class Client {
async send(topics,payload,options) {
if (typeof topics !=='string'|| !Array.isArray(topics)) {
if (typeof topics !=='string' && !Array.isArray(topics)) {
payload = topics
topics = this.topics
}
if (typeof topics ==='string') topics = topics.split(',')
// console.log('topics using', topics)
let serial = this._serialize(payload)
if (serial) {
let pubs = []
@ -106,7 +105,7 @@ export default class Client {
this.on('message',messageProcess.bind(this))
async function messageProcess (topic,payload) {
console.log('incoming messeage on topic', topic)
log.info('incoming messeage on topic', topic)
let packet = this._handlePayload(payload)
if (packet) await this._packetProcess (packet,topic)
@ -117,7 +116,7 @@ export default class Client {
let [err,packet] = btc(JSON.parse)(payload.toString())
if (err) {
log.fatal({payload:payload},'Could not parse JSON of payload')
console.log('Could not parse JSON of payload', payload.toString())
// console.log('Could not parse JSON of payload', payload.toString())
return null
}
return packet
@ -136,19 +135,7 @@ export default class Client {
async _log() {
const LOG_OPTS = {
repo:'uci-mqtt',
npm:'@uci/mqtt',
file:'src/client.mjs',
class:'Client',
id:this.id,
instance_created:new Date().getTime(),
mqtt: this._client.options // await mqtt.unsubscribe('test2')
// await mqtt.send({cmd:'test', status:'I\'m good'})
// console.log('+++++++++++++++')
}
log = logger.child(LOG_OPTS)
log = logger({file:'src/client.js',class:'Client',name:'mqtt',id:this.id})
this.on('close', () => {
log.info('connection to broker was closed')