Skip to content

Commit 8811ae5

Browse files
authored
Show server locations in verbose mode (#92)
1 parent 00e182d commit 8811ae5

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

source/api.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {isDeepStrictEqual} from 'node:util';
2-
import puppeteer from 'puppeteer';
2+
import {launch, type Page} from 'puppeteer';
33
import {delay} from 'unicorn-magic';
44
import {type SpeedData, type SpeedUnit} from './types.js';
55

66
type Options = {
77
measureUpload?: boolean;
88
};
99

10-
async function * monitorSpeed(page: puppeteer.Page, options?: Options): AsyncGenerator<SpeedData, void, undefined> {
10+
async function * monitorSpeed(page: Page, options?: Options): AsyncGenerator<SpeedData, void, undefined> {
1111
let previousResult: SpeedData | undefined;
1212

1313
while (true) {
@@ -25,6 +25,7 @@ async function * monitorSpeed(page: puppeteer.Page, options?: Options): AsyncGen
2525
latency: Number($('#latency-value')?.textContent?.trim()) || 0,
2626
bufferBloat: Number($('#bufferbloat-value')?.textContent?.trim()) || 0,
2727
userLocation: $('#user-location')?.textContent?.trim() ?? '',
28+
serverLocations: $('#server-locations')?.textContent?.split('|').map(location => location.trim()).filter(Boolean) ?? [],
2829
userIp: $('#user-ip')?.textContent?.trim() ?? '',
2930
isDone: Boolean($('#speed-value.succeeded') && $('#upload-value.succeeded')),
3031
};
@@ -46,7 +47,7 @@ async function * monitorSpeed(page: puppeteer.Page, options?: Options): AsyncGen
4647
}
4748

4849
export default async function * api(options?: Options): AsyncGenerator<SpeedData, void, undefined> {
49-
const browser = await puppeteer.launch({
50+
const browser = await launch({
5051
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
5152
headless: true,
5253
});

source/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type SpeedData = {
1010
readonly latency: number;
1111
readonly bufferBloat: number;
1212
readonly userLocation: string;
13+
readonly serverLocations: string[];
1314
readonly userIp: string;
1415
readonly isDone: boolean;
1516
};

source/ui.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,22 @@ const VerboseInfo: React.FC<VerboseInfoProperties> = ({data, singleLine}) => {
127127
<Text dimColor>Detecting...</Text>
128128
)}
129129
</Box>
130+
<Box>
131+
<Text><FixedSpacer size={4}/></Text>
132+
<Text dimColor>Server: </Text>
133+
{data.serverLocations ? (
134+
data.serverLocations.map((location, index) => (
135+
<Text key={location}>
136+
{index > 0 && <Text dimColor>{' | '}</Text>}
137+
<Text color='white'>{location}</Text>
138+
</Text>
139+
))
140+
) : (
141+
<Text dimColor>Detecting...</Text>
142+
)}
143+
</Box>
130144
</Box>
145+
131146
</>
132147
);
133148
};
@@ -170,6 +185,10 @@ function formatVerboseText(data: PartialSpeedData): string[] {
170185
lines.push(clientLine);
171186
}
172187

188+
if (data.serverLocations?.length) {
189+
lines.push(`Server: ${data.serverLocations.join(' | ')}`);
190+
}
191+
173192
return lines;
174193
}
175194

@@ -184,6 +203,7 @@ function createJsonOutput(data: PartialSpeedData, upload: boolean) {
184203
latency: data.latency,
185204
bufferBloat: data.bufferBloat,
186205
userLocation: data.userLocation,
206+
serverLocations: data.serverLocations,
187207
userIp: data.userIp,
188208
};
189209
}

test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ test('json output', async t => {
2222
t.is(typeof data.downloadSpeed, 'number');
2323
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
2424
t.regex(data.downloadUnit, /^[MGKB]bps$/);
25+
t.true(Array.isArray(data.serverLocations));
2526
});
2627

2728
test('upload flag', async t => {
@@ -64,6 +65,9 @@ test('verbose flag', async t => {
6465

6566
// Should contain client information
6667
t.regex(stdout, /Client:/);
68+
69+
// Should contain server information
70+
t.regex(stdout, /Server:/);
6771
});
6872

6973
test('verbose flag in help', async t => {

0 commit comments

Comments
 (0)