Skip to content

Commit cc02131

Browse files
committed
feat(net): choose metrics port in test mode, upgrade pino
1 parent 87cc68e commit cc02131

File tree

6 files changed

+41
-37
lines changed

6 files changed

+41
-37
lines changed

config/test.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"level": "warn"
55
},
66
"server": {
7-
"port": 0
7+
"port": 0,
8+
"internalPort": 0
89
}
910
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"glob": "^8.1.0",
8686
"lodash": "^4.17.21",
8787
"minimist": "^1.2.8",
88-
"pino": "^8.21.0",
88+
"pino": "^9.0.0",
8989
"read-pkg-up": "^7.0.1",
9090
"request-ip": "^3.3.0"
9191
},

src/config/index.ts

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import fs from 'fs';
2-
import net from 'net';
32
import path from 'path';
43

54
import {
@@ -10,8 +9,7 @@ import {
109
confit,
1110
} from '@sesamecare-oss/confit';
1211

13-
import { findPort } from '../development/port-finder';
14-
import { isTest } from '../env';
12+
import { getAvailablePort } from '../development/port-finder';
1513

1614
import type { ConfigurationSchema } from './schema';
1715

@@ -57,28 +55,6 @@ async function addDefaultConfiguration<Config extends ConfigurationSchema = Conf
5755
}
5856
}
5957

60-
async function getEphemeralPort(): Promise<number> {
61-
return new Promise((resolve, reject) => {
62-
const server = net.createServer();
63-
64-
server.listen(0, () => {
65-
const address = server.address();
66-
if (typeof address === 'string' || !address) {
67-
reject(new Error('Invalid address'));
68-
return;
69-
}
70-
const port = address.port; // Retrieve the ephemeral port
71-
server.close((err) => {
72-
if (err) {
73-
reject(err);
74-
} else {
75-
resolve(port);
76-
}
77-
});
78-
});
79-
});
80-
}
81-
8258
export interface ServiceConfigurationSpec {
8359
// The LAST configuration is the most "specific" - if a configuration value
8460
// exists in all directories, the last one wins
@@ -117,8 +93,7 @@ export async function loadConfiguration<Config extends ConfigurationSchema>({
11793
// configured to auto-select
11894
const serverConfig = loaded.get().server;
11995
if (serverConfig.port === 0) {
120-
const portPromise: Promise<number> = (isTest() || process.env.TEST_RUNNER) ? getEphemeralPort() : findPort(8001);
121-
const port = await portPromise;
96+
const port = await getAvailablePort(8001);
12297
const store = loaded.get();
12398
store.server = store.server || {};
12499
store.server.port = port;

src/development/port-finder.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import net from 'net';
22

3+
import { isTest } from '../env';
4+
35
// Inspired by https://github.com/kessler/find-port/blob/master/lib/findPort.js
46
async function isAvailable(port: number) {
57
return new Promise((accept, reject) => {
@@ -29,7 +31,7 @@ async function isAvailable(port: number) {
2931
});
3032
}
3133

32-
export async function findPort(start: number) {
34+
async function findPort(start: number) {
3335
for (let p = start; p < start + 1000; p += 1) {
3436
// eslint-disable-next-line no-await-in-loop
3537
if (await isAvailable(p)) {
@@ -38,3 +40,29 @@ export async function findPort(start: number) {
3840
}
3941
return 0;
4042
}
43+
44+
async function getEphemeralPort(): Promise<number> {
45+
return new Promise((resolve, reject) => {
46+
const server = net.createServer();
47+
48+
server.listen(0, () => {
49+
const address = server.address();
50+
if (typeof address === 'string' || !address) {
51+
reject(new Error('Invalid address'));
52+
return;
53+
}
54+
const port = address.port; // Retrieve the ephemeral port
55+
server.close((err) => {
56+
if (err) {
57+
reject(err);
58+
} else {
59+
resolve(port);
60+
}
61+
});
62+
});
63+
});
64+
}
65+
66+
export async function getAvailablePort(basePort: number): Promise<number> {
67+
return (isTest() || process.env.TEST_RUNNER) ? getEphemeralPort() : findPort(basePort);
68+
}

src/express-app/internal-server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express from 'express';
22
import type { Application } from 'express-serve-static-core';
33

44
import { AnyServiceLocals, InternalLocals, ServiceExpress, ServiceLocals } from '../types';
5-
import { findPort } from '../development/port-finder';
5+
import { getAvailablePort } from '../development/port-finder';
66
import { ConfigurationSchema } from '../config/schema';
77

88
export async function startInternalApp<
@@ -11,7 +11,7 @@ export async function startInternalApp<
1111
const app = express() as unknown as Application<InternalLocals<SLocals>>;
1212
app.locals.mainApp = mainApp;
1313

14-
const finalPort = port === 0 ? await findPort(3001) : port;
14+
const finalPort = port === 0 ? await getAvailablePort(3001) : port;
1515

1616
app.get('/health', async (req, res) => {
1717
if (mainApp.locals.service?.healthy) {

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ __metadata:
796796
glob: ^8.1.0
797797
lodash: ^4.17.21
798798
minimist: ^1.2.8
799-
pino: ^8.21.0
799+
pino: ^9.0.0
800800
pino-pretty: ^11.0.0
801801
pinst: ^3.0.0
802802
read-pkg-up: ^7.0.1
@@ -6290,9 +6290,9 @@ __metadata:
62906290
languageName: node
62916291
linkType: hard
62926292

6293-
"pino@npm:^8.21.0":
6294-
version: 8.21.0
6295-
resolution: "pino@npm:8.21.0"
6293+
"pino@npm:^9.0.0":
6294+
version: 9.0.0
6295+
resolution: "pino@npm:9.0.0"
62966296
dependencies:
62976297
atomic-sleep: ^1.0.0
62986298
fast-redact: ^3.1.1
@@ -6307,7 +6307,7 @@ __metadata:
63076307
thread-stream: ^2.6.0
63086308
bin:
63096309
pino: bin.js
6310-
checksum: d895c37cfcb7ade33ad7ac4ca54c0497ab719ec726e42b7c7b9697e07572a09a7c7de18d751440769c3ea5ecbac2075fdac720cf182720a4764defe3de8a1411
6310+
checksum: d4d22aa5ab7d1c116c40b694bcc14a38cc9335ff195186933bb9f622784f9dd95d755c454cde420bf971bcda07d3a6013b3a2a4dcb9be43745f9c1a7f86c352c
63116311
languageName: node
63126312
linkType: hard
63136313

0 commit comments

Comments
 (0)