Skip to content

Commit a65ca1e

Browse files
authored
Merge pull request #64 from austinfrey/external-should-be-array
add support for external hostname options to be an array
2 parents 168e54a + 3826c39 commit a65ca1e

File tree

4 files changed

+171
-19
lines changed

4 files changed

+171
-19
lines changed

compose.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,26 @@ module.exports = function (ary, wrap) {
102102
},
103103
parse: parse,
104104
stringify: function (scope) {
105-
var none
106-
var _ary = ary.map(function (e) {
107-
var v = e.stringify(scope)
108-
if(!v) none = true
109-
else return v
110-
})
111-
if(none) return
112-
return SE.stringify(_ary)
105+
var _ary = []
106+
var v = proto.stringify(scope)
107+
if(!v) return
108+
else {
109+
// if true, more than one hostname needs to be updated
110+
if (v.split(';').length > 1) {
111+
var addresses = v.split(';')
112+
addresses.forEach(a => {
113+
_ary.push(a)
114+
})
115+
}
116+
else _ary.push(v)
117+
}
118+
return _ary.map(e => {
119+
var singleAddr = [e].concat(trans.map(t => {
120+
return t.stringify(scope)
121+
}))
122+
123+
return SE.stringify(singleAddr)
124+
}).join(';')
113125
}
114126
}
115127
}

plugins/net.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,15 @@ 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-
resultHost = resultHost.replace(/(\%\w+)$/, '')
125-
126-
return toAddress(resultHost, port)
123+
// convert to an array for easier formatting
124+
if (isString(resultHost)) {
125+
resultHost = [resultHost]
126+
}
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(';')
127132
}
128133
}
129134
}

plugins/ws.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +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 URL.format({
141-
protocol: secure ? 'wss' : 'ws',
142-
slashes: true,
143-
hostname: resultHost,
144-
port: (secure ? port == 443 : port == 80) ? undefined : port
145-
})
140+
if (typeof resultHost === 'string') {
141+
resultHost = [resultHost]
142+
}
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(';')
146152
},
147153
parse: function (str) {
148154
var addr = URL.parse(str)

test/plugs.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,132 @@ tape('multiple scopes different hosts', function(t) {
465465

466466
t.end()
467467
})
468+
469+
tape('net: external is a string', function (t) {
470+
var net = Net({
471+
external: 'domain.de',
472+
scope: 'public',
473+
port: '9966',
474+
server: {
475+
key: null,
476+
address: function () { return {port: 9966}}
477+
}})
478+
t.equal(net.stringify('public'), 'net:domain.de:9966')
479+
t.equal(net.stringify('local'), null)
480+
t.equal(net.stringify('device'), null)
481+
t.end()
482+
})
483+
484+
tape('net: external is an array', function (t) {
485+
var net = Net({
486+
external: ['domain.de', 'funtime.net'],
487+
scope: 'public',
488+
port: '9966',
489+
server: {
490+
key: null,
491+
address: function () { return {port: 9966}}
492+
}})
493+
t.equal(net.stringify('public'), 'net:domain.de:9966;net:funtime.net:9966')
494+
t.equal(net.stringify('local'), null)
495+
t.equal(net.stringify('device'), null)
496+
t.end()
497+
})
498+
499+
tape('net: external is an array w/ a single entry & shs transform', function (t) {
500+
var net = Net({
501+
external: ['domain.de'],
502+
scope: 'public',
503+
port: '9966',
504+
server: {
505+
key: null,
506+
address: function () { return {port: 9966}}
507+
}})
508+
var combined = Compose([net, shs])
509+
t.equal(
510+
combined.stringify('public'),
511+
'net:domain.de:9966~shs:+y42DK+BGzqvU00EWMKiyj4fITskSm+Drxq1Dt2s3Yw='
512+
)
513+
t.end()
514+
})
515+
516+
tape('net: external is an array w/ multiple entries & shs transform', function (t) {
517+
var net = Net({
518+
external: ['domain.de', 'funtime.net'],
519+
scope: 'public',
520+
port: '9966',
521+
server: {
522+
key: null,
523+
address: function () { return {port: 9966}}
524+
}})
525+
var combined = Compose([net, shs])
526+
t.equal(
527+
combined.stringify('public'),
528+
'net:domain.de:9966~shs:+y42DK+BGzqvU00EWMKiyj4fITskSm+Drxq1Dt2s3Yw=;net:funtime.net:9966~shs:+y42DK+BGzqvU00EWMKiyj4fITskSm+Drxq1Dt2s3Yw='
529+
)
530+
t.end()
531+
})
532+
533+
tape('ws: external is a string', function (t) {
534+
var ws = Ws({
535+
external: 'domain.de',
536+
scope: 'public',
537+
port: '9966',
538+
server: {
539+
key: null,
540+
address: function () { return {port: 9966}}
541+
}})
542+
t.equal(ws.stringify('public'), 'ws://domain.de:9966')
543+
t.equal(ws.stringify('local'), null)
544+
t.equal(ws.stringify('device'), null)
545+
t.end()
546+
})
547+
548+
549+
tape('ws: external is an array', function (t) {
550+
var ws = Ws({
551+
external: ['domain.de', 'funtime.net'],
552+
scope: 'public',
553+
port: '9966',
554+
server: {
555+
key: null,
556+
address: function () { return {port: 9966}}
557+
}})
558+
t.equal(ws.stringify('public'), 'ws://domain.de:9966;ws://funtime.net:9966')
559+
t.equal(ws.stringify('local'), null)
560+
t.equal(ws.stringify('device'), null)
561+
t.end()
562+
})
563+
564+
tape('ws: external is an array w/ a single entry & shs transform', function (t) {
565+
var ws = Ws({
566+
external: ['domain.de'],
567+
scope: 'public',
568+
port: '9966',
569+
server: {
570+
key: null,
571+
address: function () { return {port: 9966}}
572+
}})
573+
var combined = Compose([ws, shs])
574+
t.equal(
575+
combined.stringify('public'),
576+
'ws://domain.de:9966~shs:+y42DK+BGzqvU00EWMKiyj4fITskSm+Drxq1Dt2s3Yw='
577+
)
578+
t.end()
579+
})
580+
581+
tape('ws: external is an array w/ multiple entries & shs transform', function (t) {
582+
var ws = Ws({
583+
external: ['domain.de', 'funtime.net'],
584+
scope: 'public',
585+
port: '9966',
586+
server: {
587+
key: null,
588+
address: function () { return {port: 9966}}
589+
}})
590+
var combined = Compose([ws, shs])
591+
t.equal(
592+
combined.stringify('public'),
593+
'ws://domain.de:9966~shs:+y42DK+BGzqvU00EWMKiyj4fITskSm+Drxq1Dt2s3Yw=;ws://funtime.net:9966~shs:+y42DK+BGzqvU00EWMKiyj4fITskSm+Drxq1Dt2s3Yw='
594+
)
595+
t.end()
596+
})

0 commit comments

Comments
 (0)