Skip to content

Commit 3826c39

Browse files
committed
refactor to remove helper functions in ws/net stringify method
1 parent 04b4511 commit 3826c39

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

plugins/net.js

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

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(';')
123+
// convert to an array for easier formatting
124+
if (isString(resultHost)) {
125+
resultHost = [resultHost]
135126
}
127+
128+
return resultHost.map((h) => {
129+
// Remove IPv6 scopeid suffix, if any, e.g. `%wlan0`
130+
return toAddress(h.replace(/(\%\w+)$/, ''), port)
131+
}).join(';')
136132
}
137133
}
138134
}

plugins/ws.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,29 +130,25 @@ module.exports = function (opts = {}) {
130130

131131
const port = opts.server ? opts.server.address().port : opts.port
132132
const externalHost = targetScope === 'public' && opts.external
133-
const resultHost = externalHost || opts.host || scopes.host(targetScope)
133+
let resultHost = externalHost || opts.host || scopes.host(targetScope)
134134

135135
if (resultHost == null) {
136136
// The device has no network interface for a given `targetScope`.
137137
return null
138138
}
139139

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(';')
140+
if (typeof resultHost === 'string') {
141+
resultHost = [resultHost]
155142
}
143+
144+
return resultHost.map((h) => {
145+
return URL.format({
146+
protocol: secure ? 'wss' : 'ws',
147+
slashes: true,
148+
hostname: h,
149+
port: (secure ? port == 443 : port == 80) ? undefined : port
150+
})
151+
}).join(';')
156152
},
157153
parse: function (str) {
158154
var addr = URL.parse(str)

0 commit comments

Comments
 (0)