Skip to content

Commit 0c5bd24

Browse files
committed
Rearrange some code
1 parent 0204e18 commit 0c5bd24

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

src/rfc-8252-http-server.spec.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RFC8252HTTPServer } from './rfc-8252-http-server';
1+
import { getAllInterfaces, RFC8252HTTPServer } from './rfc-8252-http-server';
22
import { expect } from 'chai';
33
import type { Server as HTTPServer } from 'http';
44
import { createServer as createHTTPServer } from 'http';
@@ -487,27 +487,21 @@ describe('RFC8252HTTPServer', function () {
487487
});
488488
});
489489

490-
context('with dns duplicates', function () {
490+
context('getAllInterfaces', function () {
491491
let dnsLookupStub: sinon.SinonStub;
492-
const _getAllInterfaces = RFC8252HTTPServer['_getAllInterfaces'];
493-
494492
this.beforeEach(function () {
495-
dnsLookupStub = sinon.stub(dns, 'lookup');
496-
});
497-
498-
this.afterEach(function () {
499-
sinon.restore();
493+
dnsLookupStub = sinon.stub();
500494
});
501495

502-
it('only filters exact duplicates', async function () {
496+
it('filters out exact duplicates', async function () {
503497
dnsLookupStub.resolves([
504498
{ address: '127.0.0.1', family: 4 },
505499
{ address: '127.0.0.1', family: 4 },
506500
{ address: '[::1]', family: 6 },
507501
{ address: '[::1]', family: 6 },
508502
]);
509503

510-
const interfaces = await _getAllInterfaces('localhost');
504+
const interfaces = await getAllInterfaces('localhost', dnsLookupStub);
511505

512506
expect(interfaces).to.have.lengthOf(2);
513507
expect(interfaces[0].address).to.equal('127.0.0.1');
@@ -522,7 +516,7 @@ describe('RFC8252HTTPServer', function () {
522516
{ address: '127.0.0.1', family: 6 },
523517
]);
524518

525-
const interfaces = await _getAllInterfaces('localhost');
519+
const interfaces = await getAllInterfaces('localhost', dnsLookupStub);
526520

527521
expect(interfaces).to.have.lengthOf(2);
528522
expect(interfaces[0].address).to.equal('127.0.0.1');
@@ -537,7 +531,7 @@ describe('RFC8252HTTPServer', function () {
537531
{ address: '192.168.1.15', family: 4 },
538532
]);
539533

540-
const interfaces = await _getAllInterfaces('localhost');
534+
const interfaces = await getAllInterfaces('localhost', dnsLookupStub);
541535

542536
expect(interfaces).to.have.lengthOf(2);
543537
expect(interfaces[0].address).to.equal('127.0.0.1');

src/rfc-8252-http-server.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ export interface RFC8252HTTPServerOptions {
2929
redirectServerRequestHandler?: RedirectServerRequestHandler;
3030
}
3131

32+
export async function getAllInterfaces(
33+
hostname: string,
34+
lookup: typeof dns.lookup = dns.lookup
35+
): Promise<{ address: string; family: number }[]> {
36+
const dnsResults = await lookup(hostname, {
37+
all: true,
38+
hints: ADDRCONFIG,
39+
});
40+
41+
return dnsResults
42+
.filter(
43+
(dns, index, arr) =>
44+
arr.findIndex(
45+
(otherDns) =>
46+
dns.address === otherDns.address && dns.family === otherDns.family
47+
) === index
48+
)
49+
.map(({ address, family }) => ({ address, family }));
50+
}
51+
3252
/** @internal */
3353
export class RFC8252HTTPServer {
3454
private readonly redirectUrl: URL;
@@ -272,25 +292,6 @@ export class RFC8252HTTPServer {
272292
});
273293
};
274294

275-
private static async _getAllInterfaces(
276-
hostname: string
277-
): Promise<{ address: string; family: number }[]> {
278-
const dnsResults = await dns.lookup(hostname, {
279-
all: true,
280-
hints: ADDRCONFIG,
281-
});
282-
283-
return dnsResults
284-
.filter(
285-
(dns, index, arr) =>
286-
arr.findIndex(
287-
(otherDns) =>
288-
dns.address === otherDns.address && dns.family === otherDns.family
289-
) === index
290-
)
291-
.map(({ address, family }) => ({ address, family }));
292-
}
293-
294295
/**
295296
* Add a redirect from a local URL served on the server to an external URL.
296297
*/
@@ -391,7 +392,7 @@ export class RFC8252HTTPServer {
391392
hostname = hostname.slice(1, -1);
392393
}
393394

394-
const dnsResults = await RFC8252HTTPServer._getAllInterfaces(hostname);
395+
const dnsResults = await getAllInterfaces(hostname);
395396

396397
this.logger.emit('mongodb-oidc-plugin:local-listen-resolved-hostname', {
397398
url: this.redirectUrl.toString(),

0 commit comments

Comments
 (0)