UNPKG

3.09 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3'use strict'
4
5var mqtt = require('../')
6var pump = require('pump')
7var path = require('path')
8var fs = require('fs')
9var concat = require('concat-stream')
10var Writable = require('readable-stream').Writable
11var helpMe = require('help-me')({
12 dir: path.join(__dirname, '..', 'doc')
13})
14var minimist = require('minimist')
15var split2 = require('split2')
16
17function send (args) {
18 var client = mqtt.connect(args)
19 client.on('connect', function () {
20 client.publish(args.topic, args.message, args, function (err) {
21 if (err) {
22 console.warn(err)
23 }
24 client.end()
25 })
26 })
27 client.on('error', function (err) {
28 console.warn(err)
29 client.end()
30 })
31}
32
33function multisend (args) {
34 var client = mqtt.connect(args)
35 var sender = new Writable({
36 objectMode: true
37 })
38 sender._write = function (line, enc, cb) {
39 client.publish(args.topic, line.trim(), args, cb)
40 }
41
42 client.on('connect', function () {
43 pump(process.stdin, split2(), sender, function (err) {
44 client.end()
45 if (err) {
46 throw err
47 }
48 })
49 })
50}
51
52function start (args) {
53 args = minimist(args, {
54 string: ['hostname', 'username', 'password', 'key', 'cert', 'ca', 'message', 'clientId', 'i', 'id'],
55 boolean: ['stdin', 'retain', 'help', 'insecure', 'multiline'],
56 alias: {
57 port: 'p',
58 hostname: ['h', 'host'],
59 topic: 't',
60 message: 'm',
61 qos: 'q',
62 clientId: ['i', 'id'],
63 retain: 'r',
64 username: 'u',
65 password: 'P',
66 stdin: 's',
67 multiline: 'M',
68 protocol: ['C', 'l'],
69 help: 'H',
70 ca: 'cafile'
71 },
72 default: {
73 host: 'localhost',
74 qos: 0,
75 retain: false,
76 topic: '',
77 message: ''
78 }
79 })
80
81 if (args.help) {
82 return helpMe.toStdout('publish')
83 }
84
85 if (args.key) {
86 args.key = fs.readFileSync(args.key)
87 }
88
89 if (args.cert) {
90 args.cert = fs.readFileSync(args.cert)
91 }
92
93 if (args.ca) {
94 args.ca = fs.readFileSync(args.ca)
95 }
96
97 if (args.key && args.cert && !args.protocol) {
98 args.protocol = 'mqtts'
99 }
100
101 if (args.port) {
102 if (typeof args.port !== 'number') {
103 console.warn('# Port: number expected, \'%s\' was given.', typeof args.port)
104 return
105 }
106 }
107
108 if (args['will-topic']) {
109 args.will = {}
110 args.will.topic = args['will-topic']
111 args.will.payload = args['will-message']
112 args.will.qos = args['will-qos']
113 args.will.retain = args['will-retain']
114 }
115
116 if (args.insecure) {
117 args.rejectUnauthorized = false
118 }
119
120 args.topic = (args.topic || args._.shift()).toString()
121 args.message = (args.message || args._.shift()).toString()
122
123 if (!args.topic) {
124 console.error('missing topic\n')
125 return helpMe.toStdout('publish')
126 }
127
128 if (args.stdin) {
129 if (args.multiline) {
130 multisend(args)
131 } else {
132 process.stdin.pipe(concat(function (data) {
133 args.message = data.toString().trim()
134 send(args)
135 }))
136 }
137 } else {
138 send(args)
139 }
140}
141
142module.exports = start
143
144if (require.main === module) {
145 start(process.argv.slice(2))
146}