Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support IPv6 with interface #128

Merged
merged 1 commit into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ const program = require('commander')
const version = require('./package').version
const coap = require('coap')
const request = coap.request
const URL = require('url')
const URI = require('uri-js')
const through = require('through2')
let method = 'GET' // default
let url
const coapOptionSeperator = ','

function collectOptions (val, memo) {
Expand Down Expand Up @@ -50,30 +49,22 @@ let inputUrl

program.parse(process.argv)

try {
url = new URL.URL(decodeURIComponent(inputUrl))
} catch (err) {
if (err instanceof TypeError) {
console.log('Invalid URL. Protocol is not given or URL is malformed.')
process.exit(-1)
}
}
const url = URI.parse(inputUrl)

let hostname = url.hostname
// Remove brackets from literal IPv6 addresses
if (hostname.startsWith('[') && hostname.endsWith(']')) {
hostname = hostname.substring(1, hostname.length - 1)
if (url.scheme === undefined || url.host === undefined) {
console.log('Invalid URL. Protocol is not given or URL is malformed.')
process.exit(-1)
}

const requestParams = {
method,
observe: program.opts().observe,
confirmable: !program.opts().nonConfirmable,
hostname,
pathname: url.pathname,
protocol: url.protocol,
hostname: url.host,
pathname: url.path,
protocol: url.scheme + ':',
port: url.port,
query: url.search.substring(1)
query: url.query
}

if (requestParams.protocol !== 'coap:' || !requestParams.hostname) {
Expand Down
98 changes: 48 additions & 50 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"dependencies": {
"coap": "~0.25.0",
"commander": "~8.2.0",
"minimist": ">=1.2.2",
"through2": "~4.0.2",
"minimist": ">=1.2.2"
"uri-js": "^4.4.1"
}
}