fix: fix ping action throw error in windows

This commit is contained in:
moonrailgun 2024-04-24 00:32:04 +08:00
parent 47cd26cd86
commit 89fed4666d
3 changed files with 22 additions and 1 deletions

View File

@ -377,6 +377,9 @@ importers:
bcryptjs:
specifier: ^2.4.3
version: 2.4.3
chardet:
specifier: ^2.0.0
version: 2.0.0
compose-middleware:
specifier: ^5.0.1
version: 5.0.1
@ -410,6 +413,9 @@ importers:
fs-extra:
specifier: ^11.2.0
version: 11.2.0
iconv-lite:
specifier: ^0.6.3
version: 0.6.3
is-localhost-ip:
specifier: ^2.0.0
version: 2.0.0
@ -12042,6 +12048,10 @@ packages:
resolution: {integrity: sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==}
dev: true
/chardet@2.0.0:
resolution: {integrity: sha512-xVgPpulCooDjY6zH4m9YW3jbkaBe3FKIAvF5sj5t7aBNsVl2ljIE+xwJ4iNgiDZHFQvNIpjdKdVOQvvk5ZfxbQ==}
dev: false
/charenc@0.0.2:
resolution: {integrity: sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==}

View File

@ -1,6 +1,8 @@
import { MonitorProvider } from './type';
import pingUtils from 'ping';
import os from 'os';
import chardet from 'chardet';
import iconv from 'iconv-lite';
export const ping: MonitorProvider<{
hostname: string;
@ -39,7 +41,7 @@ function pingAction(hostname: string, packetSize = 56) {
resolve(res.time);
} else {
if (isWindows) {
reject(new Error(exports.convertToUTF8(res.output)));
reject(new Error(convertToUTF8(res.output)));
} else {
reject(new Error(res.output));
}
@ -50,3 +52,10 @@ function pingAction(hostname: string, packetSize = 56) {
});
});
}
function convertToUTF8(body: string) {
const buffer = Buffer.from(body);
const guessEncoding = chardet.detect(buffer) ?? 'UTF-8';
const str = iconv.decode(buffer, guessEncoding);
return str.toString();
}

View File

@ -28,6 +28,7 @@
"axios": "^1.5.0",
"badge-maker": "^3.3.1",
"bcryptjs": "^2.4.3",
"chardet": "^2.0.0",
"compose-middleware": "^5.0.1",
"compression": "^1.7.4",
"cors": "^2.8.5",
@ -39,6 +40,7 @@
"express-async-errors": "^3.1.1",
"express-validator": "^7.0.1",
"fs-extra": "^11.2.0",
"iconv-lite": "^0.6.3",
"is-localhost-ip": "^2.0.0",
"isolated-vm": "^4.7.2",
"jsonwebtoken": "^9.0.2",