This repository was archived by the owner on Dec 3, 2025. It is now read-only.
forked from coapjs/coap-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·97 lines (81 loc) · 2.83 KB
/
Copy pathindex.js
File metadata and controls
executable file
·97 lines (81 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#! /usr/bin/env node
var program = require('commander')
var version = require('./package').version
var coap = require('coap')
var request = coap.request
var URL = require('url')
var through = require('through2')
var method = 'GET' // default
var url
var req
program
.version(version)
.option('-o, --observe', 'Observe the given resource', 'boolean', false)
.option('-n, --no-new-line', 'No new line at the end of the stream', 'boolean', true)
.option('-p, --payload <payload>', 'The payload for POST and PUT requests')
.option('-b, --block2 <option>', 'set the block2 size option', parseInt)
.option('-q, --quiet', 'Do not print status codes of received packets', 'boolean', false)
.option('-c, --non-confirmable', 'non-confirmable', 'boolean', false)
.option('-t, --timeout <seconds>', 'The maximum send time in seconds')
.option('-T, --show-timing', 'Print request time, handy for simple performance tests', 'boolean', false)
.usage('[command] [options] url')
;['GET', 'PUT', 'POST', 'DELETE'].forEach(function (name) {
program
.command(name.toLowerCase())
.description('performs a ' + name + ' request')
.action(function () { method = name })
})
program.parse(process.argv)
if (!program.args[0]) {
program.outputHelp()
process.exit(-1)
}
url = URL.parse(program.args[0])
url.method = method
url.observe = program.observe
url.confirmable = !program.nonConfirmable
if (url.protocol !== 'coap:' || !url.hostname) {
console.log('Wrong URL. Protocol is not coap or no hostname found.')
process.exit(-1)
}
coap.parameters.exchangeLifetime = program.timeout ? program.timeout : 30
if (program.block2 && (program.block2 < 1 || program.block2 > 6)) {
console.log('Invalid block2 size, valid range [1..6]')
console.log('block2 1: 32 bytes payload, block2 2: 64 bytes payload...')
process.exit(-1)
}
var startTime = new Date()
req = request(url)
if (program.block2) {
req.setOption('Block2', new Buffer([program.block2]))
}
req.on('response', function (res) {
var endTime = new Date()
if (program.showTiming) {
console.log('Request took ' + (endTime.getTime() - startTime.getTime()) + ' ms')
}
// print only status code on empty response
if (!res.payload.length && !program.quiet) {
process.stderr.write('\x1b[1m(' + res.code + ')\x1b[0m\n')
}
res.pipe(through(function addNewLine (chunk, enc, callback) {
if (!program.quiet) {
process.stderr.write('\x1b[1m(' + res.code + ')\x1b[0m\t')
}
if (program.newLine && chunk) {
chunk = chunk.toString('utf-8') + '\n'
}
this.push(chunk)
callback()
})).pipe(process.stdout)
// needed because of some weird issue with
// empty responses and streams
if (!res.payload.length) {
process.exit(0)
}
})
if (method === 'GET' || method === 'DELETE' || program.payload) {
req.end(program.payload)
} else {
process.stdin.pipe(req)
}