From 60449f8ffcb1c23b535f25f10a9fcfe415ee4ad3 Mon Sep 17 00:00:00 2001 From: Michael Gerdemann Date: Wed, 20 Oct 2021 07:43:15 +0200 Subject: [PATCH] fix: Support IPv6 with interface Resolves: #73 --- index.js | 27 +++++-------- package-lock.json | 98 +++++++++++++++++++++++------------------------ package.json | 3 +- 3 files changed, 59 insertions(+), 69 deletions(-) diff --git a/index.js b/index.js index b479793..1335df8 100755 --- a/index.js +++ b/index.js @@ -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) { @@ -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) { diff --git a/package-lock.json b/package-lock.json index 47dae60..ef39f74 100644 --- a/package-lock.json +++ b/package-lock.json @@ -238,7 +238,7 @@ "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", "dev": true, "requires": { - "balanced-match": "1.0.0", + "balanced-match": "^1.0.0", "concat-map": "0.0.1" } }, @@ -458,10 +458,10 @@ "integrity": "sha512-MWufYdFw53ccGjCA+Ol7XJYpAlW6/prSMzuPOTRnJGcGzuhLn4Scrz7qf6o8bROZ514ltazcIFJZevcfbo0x7A==", "dev": true, "requires": { - "buffer-from": "1.1.1", - "inherits": "2.0.4", - "readable-stream": "3.4.0", - "typedarray": "0.0.6" + "buffer-from": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^3.0.2", + "typedarray": "^0.0.6" }, "dependencies": { "readable-stream": { @@ -470,9 +470,9 @@ "integrity": "sha512-jItXPLmrSR8jmTRmRWJXCnGJsfy85mB3Wd/uINMXA65yrnFo0cPClFIUWzo2najVNSl+mx7/4W8ttlLWJe99pQ==", "dev": true, "requires": { - "inherits": "2.0.4", - "string_decoder": "1.1.1", - "util-deprecate": "1.0.2" + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" } } } @@ -989,13 +989,13 @@ "integrity": "sha512-qACXdu/9VHPBzcyhdOWR5/IahhGMf0roTeZJfzz077GwylcDd90yOHLouhmv7GJ5XzPi6ekaQWd8AvPP2nOvpA==", "dev": true, "requires": { - "duplexer": "0.1.1", - "from": "0.1.7", + "duplexer": "^0.1.1", + "from": "^0.1.7", "map-stream": "0.0.7", - "pause-stream": "0.0.11", - "split": "1.0.1", - "stream-combiner": "0.2.2", - "through": "2.3.8" + "pause-stream": "^0.0.11", + "split": "^1.0.1", + "stream-combiner": "^0.2.2", + "through": "^2.3.8" } }, "fast-deep-equal": { @@ -1265,8 +1265,8 @@ "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", "dev": true, "requires": { - "once": "1.4.0", - "wrappy": "1.0.2" + "once": "^1.3.0", + "wrappy": "1" } }, "inherits": { @@ -1361,7 +1361,7 @@ "integrity": "sha512-5G0tKtBTFImOqDnLB2hG6Bp2qcKEFduo4tZu9MT/H6NQv/ghhy30o55ufafxJ/LdH79LLs2Kfrn85TLKyA7BUg==", "dev": true, "requires": { - "is-extglob": "2.1.1" + "is-extglob": "^2.1.1" } }, "is-negative-zero": { @@ -1580,8 +1580,8 @@ "integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==", "dev": true, "requires": { - "pseudomap": "1.0.2", - "yallist": "2.1.2" + "pseudomap": "^1.0.2", + "yallist": "^2.1.2" } }, "map-stream": { @@ -1596,7 +1596,7 @@ "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", "dev": true, "requires": { - "brace-expansion": "1.1.11" + "brace-expansion": "^1.1.7" } }, "minimist": { @@ -1876,7 +1876,7 @@ "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", "dev": true, "requires": { - "wrappy": "1.0.2" + "wrappy": "1" } }, "optionator": { @@ -1987,7 +1987,7 @@ "integrity": "sha1-/lo0sMvOErWqaitAPuLnO2AvFEU=", "dev": true, "requires": { - "through": "2.3.8" + "through": "~2.3" } }, "picomatch": { @@ -2106,9 +2106,9 @@ "integrity": "sha1-287g7p3nI15X95xW186UZBpp7sY=", "dev": true, "requires": { - "cross-spawn": "5.1.0", - "spawn-sync": "1.0.15", - "which": "1.2.14" + "cross-spawn": "^5.0.1", + "spawn-sync": "^1.0.15", + "which": "1.2.x" }, "dependencies": { "cross-spawn": { @@ -2117,9 +2117,9 @@ "integrity": "sha1-6L0O/uWPz/b4+UUQoKVUu/ojVEk=", "dev": true, "requires": { - "lru-cache": "4.1.5", - "shebang-command": "1.2.0", - "which": "1.2.14" + "lru-cache": "^4.0.1", + "shebang-command": "^1.2.0", + "which": "^1.2.9" } }, "which": { @@ -2128,7 +2128,7 @@ "integrity": "sha1-mofEN48D6CfOyvGs31bHNsAcFOU=", "dev": true, "requires": { - "isexe": "2.0.0" + "isexe": "^2.0.0" } } } @@ -2171,8 +2171,7 @@ "punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", - "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==", - "dev": true + "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" }, "randombytes": { "version": "2.1.0", @@ -2216,13 +2215,13 @@ "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", "dev": true, "requires": { - "core-util-is": "1.0.2", - "inherits": "2.0.4", - "isarray": "1.0.0", - "process-nextick-args": "2.0.1", - "safe-buffer": "5.1.2", - "string_decoder": "1.1.1", - "util-deprecate": "1.0.2" + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" }, "dependencies": { "safe-buffer": { @@ -2342,7 +2341,7 @@ "integrity": "sha1-RKrGW2lbAzmJaMOfNj/uXer98eo=", "dev": true, "requires": { - "shebang-regex": "1.0.0" + "shebang-regex": "^1.0.0" } }, "shebang-regex": { @@ -2405,8 +2404,8 @@ "integrity": "sha1-sAeZVX63+wyDdsKdROih6mfldHY=", "dev": true, "requires": { - "concat-stream": "1.6.2", - "os-shim": "0.1.3" + "concat-stream": "^1.4.7", + "os-shim": "^0.1.2" }, "dependencies": { "concat-stream": { @@ -2415,10 +2414,10 @@ "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", "dev": true, "requires": { - "buffer-from": "1.1.1", - "inherits": "2.0.4", - "readable-stream": "2.3.6", - "typedarray": "0.0.6" + "buffer-from": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^2.2.2", + "typedarray": "^0.0.6" } } } @@ -2461,7 +2460,7 @@ "integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==", "dev": true, "requires": { - "through": "2.3.8" + "through": "2" } }, "sprintf-js": { @@ -2504,8 +2503,8 @@ "integrity": "sha1-rsjLrBd7Vrb0+kec7YwZEs7lKFg=", "dev": true, "requires": { - "duplexer": "0.1.1", - "through": "2.3.8" + "duplexer": "~0.1.1", + "through": "~2.3.4" } }, "string-width": { @@ -2574,7 +2573,7 @@ "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", "requires": { - "safe-buffer": "5.1.2" + "safe-buffer": "~5.1.0" }, "dependencies": { "safe-buffer": { @@ -2752,7 +2751,6 @@ "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==", - "dev": true, "requires": { "punycode": "^2.1.0" } diff --git a/package.json b/package.json index 84f8829..73428d5 100644 --- a/package.json +++ b/package.json @@ -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" } }