fix(detector): Improve test coverage and adjust detection result handling

This commit is contained in:
2025-05-26 09:48:42 +00:00
parent a39edf4c56
commit 851e12f499
6 changed files with 165 additions and 20 deletions

View File

@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@uptime.link/detector',
version: '2.1.0',
version: '2.1.1',
description: 'a detector for answering network questions locally. It does not rely on any online services.'
}

View File

@@ -29,9 +29,13 @@ export class Detector {
isActive: portAvailable
};
if (portAvailable && options?.detectServiceType) {
const serviceType = await this.detectType(urlArg);
result.serviceType = serviceType;
if (options?.detectServiceType) {
if (portAvailable) {
const serviceType = await this.detectType(urlArg);
result.serviceType = serviceType;
} else {
result.serviceType = ServiceType.UNKNOWN;
}
}
return result;
@@ -46,9 +50,13 @@ export class Detector {
isActive: portAvailable
};
if (portAvailable && options?.detectServiceType) {
const serviceType = await this.detectType(urlArg);
result.serviceType = serviceType;
if (options?.detectServiceType) {
if (portAvailable) {
const serviceType = await this.detectType(urlArg);
result.serviceType = serviceType;
} else {
result.serviceType = ServiceType.UNKNOWN;
}
}
return result;
@@ -57,7 +65,27 @@ export class Detector {
public async detectType(urlArg: string): Promise<ServiceType> {
const parsedUrl = plugins.smarturl.Smarturl.createFromUrl(urlArg);
const port = parseInt(parsedUrl.port, 10);
// Handle different URL schemes and default ports
let port = parseInt(parsedUrl.port, 10);
if (isNaN(port)) {
// Set default ports based on scheme
const defaultPorts: { [key: string]: number } = {
'http': 80,
'https': 443,
'ssh': 22,
'ftp': 21,
'smtp': 25,
'mysql': 3306,
'postgresql': 5432,
'mongodb': 27017,
'redis': 6379
};
const scheme = parsedUrl.protocol.replace(':', '').toLowerCase();
port = defaultPorts[scheme] || 80;
}
const hostname = parsedUrl.hostname;
// Check common ports first