Skip to content

Commit 04b4511

Browse files
committed
use helper functions and treat all external values as arrays when formatting addresses
1 parent 3204ed8 commit 04b4511

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

compose.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,10 @@ module.exports = function (ary, wrap) {
103103
parse: parse,
104104
stringify: function (scope) {
105105
var _ary = []
106-
var proto = head(ary)
107-
var trans = tail(ary)
108106
var v = proto.stringify(scope)
109107
if(!v) return
110108
else {
109+
// if true, more than one hostname needs to be updated
111110
if (v.split(';').length > 1) {
112111
var addresses = v.split(';')
113112
addresses.forEach(a => {

plugins/net.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,19 @@ module.exports = ({ scope = 'device', host, port, external, allowHalfOpen, pause
120120
return null
121121
}
122122

123-
// Remove IPv6 scopeid suffix, if any, e.g. `%wlan0`
124-
return Array.isArray(resultHost)
125-
// if an array of external hostnames is supplied,
126-
// return all of them in an ';' separated address string
127-
? resultHost.map((h) => {
128-
return toAddress(h.replace(/(\%\w+)$/, ''), port)
129-
}).join(';')
130-
: toAddress(resultHost.replace(/(\%\w+)$/, ''), port)
123+
return formatNetAddress(resultHost)
124+
125+
function formatNetAddress (resultHost) {
126+
// convert to an array for easier formatting
127+
if (isString(resultHost)) {
128+
resultHost = [resultHost]
129+
}
130+
131+
return resultHost.map((h) => {
132+
// Remove IPv6 scopeid suffix, if any, e.g. `%wlan0`
133+
return toAddress(h.replace(/(\%\w+)$/, ''), port)
134+
}).join(';')
135+
}
131136
}
132137
}
133138
}

plugins/ws.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,22 @@ module.exports = function (opts = {}) {
137137
return null
138138
}
139139

140-
return Array.isArray(resultHost)
141-
? resultHost.map((h) => {
142-
return URL.format({
143-
protocol: secure ? 'wss' : 'ws',
144-
slashes: true,
145-
hostname: h,
146-
port: (secure ? port == 443 : port == 80) ? undefined : port
147-
})
148-
}).join(';')
149-
: URL.format({
150-
protocol: secure ? 'wss' : 'ws',
151-
slashes: true,
152-
hostname: resultHost,
153-
port: (secure ? port == 443 : port == 80) ? undefined : port
154-
})
140+
return formatWsAddress(resultHost)
141+
142+
function formatWsAddress(resultHost) {
143+
if (typeof resultHost === 'string') {
144+
resultHost = [resultHost]
145+
}
146+
147+
return resultHost.map((h) => {
148+
return URL.format({
149+
protocol: secure ? 'wss' : 'ws',
150+
slashes: true,
151+
hostname: h,
152+
port: (secure ? port == 443 : port == 80) ? undefined : port
153+
})
154+
}).join(';')
155+
}
155156
},
156157
parse: function (str) {
157158
var addr = URL.parse(str)

0 commit comments

Comments
 (0)