Skip to content

Commit 3204ed8

Browse files
committed
add support for external hostname options to be an array
1 parent 7d47892 commit 3204ed8

File tree

4 files changed

+167
-12
lines changed

4 files changed

+167
-12
lines changed

compose.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,27 @@ 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 proto = head(ary)
107+
var trans = tail(ary)
108+
var v = proto.stringify(scope)
109+
if(!v) return
110+
else {
111+
if (v.split(';').length > 1) {
112+
var addresses = v.split(';')
113+
addresses.forEach(a => {
114+
_ary.push(a)
115+
})
116+
}
117+
else _ary.push(v)
118+
}
119+
return _ary.map(e => {
120+
var singleAddr = [e].concat(trans.map(t => {
121+
return t.stringify(scope)
122+
}))
123+
124+
return SE.stringify(singleAddr)
125+
}).join(';')
113126
}
114127
}
115128
}

plugins/net.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,13 @@ module.exports = ({ scope = 'device', host, port, external, allowHalfOpen, pause
121121
}
122122

123123
// Remove IPv6 scopeid suffix, if any, e.g. `%wlan0`
124-
resultHost = resultHost.replace(/(\%\w+)$/, '')
125-
126-
return toAddress(resultHost, port)
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)
127131
}
128132
}
129133
}

plugins/ws.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,16 @@ module.exports = function (opts = {}) {
137137
return null
138138
}
139139

140-
return URL.format({
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({
141150
protocol: secure ? 'wss' : 'ws',
142151
slashes: true,
143152
hostname: resultHost,

test/plugs.js

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

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

0 commit comments

Comments
 (0)