Skip to content

Commit c065147

Browse files
committed
refactor(getport): use "node:net" over "node:http"
use lowest level server creation possible (should be lower overhead)
1 parent 074a5e7 commit c065147

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

packages/mongodb-memory-server-core/src/util/getport/__tests__/getport.test.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as getPort from '../index';
2-
import * as http from 'node:http';
2+
import * as net from 'node:net';
33

44
// the following tests may fail on systems with actual ports being used in those ranges (20000 to 40000)
55

@@ -31,7 +31,7 @@ describe('getport', () => {
3131

3232
it('should return "false" on used port', async () => {
3333
const testPort = 30000;
34-
const blockingServer = http.createServer();
34+
const blockingServer = net.createServer();
3535
blockingServer.unref();
3636
blockingServer.listen(testPort);
3737
await expect(getPort.tryPort(testPort)).resolves.toStrictEqual(false);
@@ -63,10 +63,8 @@ describe('getport', () => {
6363
const testPort = 23232;
6464
await expect(getPort.getFreePort(testPort)).resolves.toStrictEqual(testPort);
6565

66-
const server = await new Promise<
67-
http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>
68-
>((res) => {
69-
const server = http.createServer();
66+
const server = await new Promise<net.Server>((res) => {
67+
const server = net.createServer();
7068
server.unref();
7169
server.listen(testPort, () => res(server));
7270
});

packages/mongodb-memory-server-core/src/util/getport/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as http from 'node:http';
1+
import * as net from 'node:net';
22

33
/** Linux min port that does not require root permissions */
44
export const MIN_PORT = 1024;
@@ -101,7 +101,7 @@ export function validPort(port: number): number {
101101
*/
102102
export function tryPort(port: number): Promise<boolean> {
103103
return new Promise((res, rej) => {
104-
const server = http.createServer();
104+
const server = net.createServer();
105105

106106
// some engines dont support ".unref"(net / tcp.unref), like "deno" in the past and now "bun"
107107
if (typeof server.unref === 'function') {

0 commit comments

Comments
 (0)