unping 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -10
- package/dist/drivers/hybrid.cjs +22 -31
- package/dist/drivers/hybrid.d.ts +2 -2
- package/dist/drivers/hybrid.mjs +14 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -107,20 +107,31 @@ console.log(`DNS Resolution: ${results[0].alive ? "Success" : "Failed"}`);
|
|
|
107
107
|
|
|
108
108
|
### Hybrid Driver
|
|
109
109
|
|
|
110
|
-
Smart detection with automatic fallback between multiple
|
|
110
|
+
Smart detection with automatic fallback between multiple drivers.
|
|
111
111
|
|
|
112
112
|
```typescript
|
|
113
113
|
import hybridDriver from "unping/drivers/hybrid";
|
|
114
|
+
import tcpDriver from "unping/drivers/tcp";
|
|
115
|
+
import httpDriver from "unping/drivers/http";
|
|
116
|
+
import dnsDriver from "unping/drivers/dns";
|
|
114
117
|
|
|
118
|
+
// Use default driver configuration (TCP → HTTP → DNS)
|
|
115
119
|
const hybrid = createPingManager({
|
|
120
|
+
driver: hybridDriver(),
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// Or specify custom drivers with specific configurations
|
|
124
|
+
const customHybrid = createPingManager({
|
|
116
125
|
driver: hybridDriver({
|
|
117
|
-
|
|
126
|
+
drivers: [
|
|
127
|
+
tcpDriver({ port: 443 }), // Try HTTPS first
|
|
128
|
+
httpDriver({ method: "GET" }), // Then HTTP GET
|
|
129
|
+
dnsDriver({ type: "AAAA" }), // Then IPv6 DNS
|
|
130
|
+
],
|
|
118
131
|
}),
|
|
119
132
|
});
|
|
120
133
|
|
|
121
|
-
// Automatically tries TCP → HTTP → DNS until one succeeds
|
|
122
134
|
const results = await hybrid.ping("example.com");
|
|
123
|
-
console.log(`Detection Method: ${results[0].source || "auto"}`);
|
|
124
135
|
console.log(`Alive: ${results[0].alive}`);
|
|
125
136
|
console.log(`Time: ${results[0].time}ms`);
|
|
126
137
|
```
|
|
@@ -217,10 +228,16 @@ const dns = createPingManager({
|
|
|
217
228
|
|
|
218
229
|
```typescript
|
|
219
230
|
import hybridDriver from "unping/drivers/hybrid";
|
|
231
|
+
import tcpDriver from "unping/drivers/tcp";
|
|
232
|
+
import httpDriver from "unping/drivers/http";
|
|
220
233
|
|
|
221
234
|
const hybrid = createPingManager({
|
|
222
235
|
driver: hybridDriver({
|
|
223
|
-
|
|
236
|
+
drivers: [
|
|
237
|
+
tcpDriver({ port: 8080 }), // Custom TCP driver
|
|
238
|
+
httpDriver({ method: "GET" }), // Custom HTTP driver
|
|
239
|
+
// Add more drivers as needed
|
|
240
|
+
],
|
|
224
241
|
}),
|
|
225
242
|
});
|
|
226
243
|
```
|
|
@@ -324,7 +341,7 @@ interface DNSDriverOptions {
|
|
|
324
341
|
|
|
325
342
|
```typescript
|
|
326
343
|
interface HybridDriverOptions {
|
|
327
|
-
|
|
344
|
+
drivers?: Driver[]; // Array of drivers to try in order (default: [tcp, http, dns])
|
|
328
345
|
}
|
|
329
346
|
```
|
|
330
347
|
|
|
@@ -342,17 +359,33 @@ The Hybrid Driver provides intelligent network connectivity detection by trying
|
|
|
342
359
|
- **HTTP Second**: Validates that the service is actually responding at application level (~100-300ms)
|
|
343
360
|
- **DNS Last**: Only checks if domain can be resolved, doesn't guarantee host reachability (~10-20ms)
|
|
344
361
|
|
|
345
|
-
**Custom
|
|
362
|
+
**Custom Driver Configuration Example:**
|
|
346
363
|
|
|
347
364
|
```typescript
|
|
348
365
|
// For web service monitoring - prioritize HTTP
|
|
366
|
+
import httpDriver from "unping/drivers/http";
|
|
367
|
+
import tcpDriver from "unping/drivers/tcp";
|
|
368
|
+
import dnsDriver from "unping/drivers/dns";
|
|
369
|
+
|
|
349
370
|
const webMonitor = createPingManager({
|
|
350
|
-
driver: hybridDriver({
|
|
371
|
+
driver: hybridDriver({
|
|
372
|
+
drivers: [
|
|
373
|
+
httpDriver({ method: "HEAD", path: "/health" }),
|
|
374
|
+
tcpDriver({ port: 80 }),
|
|
375
|
+
dnsDriver(),
|
|
376
|
+
],
|
|
377
|
+
}),
|
|
351
378
|
});
|
|
352
379
|
|
|
353
380
|
// For quick connectivity checks - prioritize TCP
|
|
354
381
|
const quickCheck = createPingManager({
|
|
355
|
-
driver: hybridDriver({
|
|
382
|
+
driver: hybridDriver({
|
|
383
|
+
drivers: [
|
|
384
|
+
tcpDriver({ port: 443 }),
|
|
385
|
+
dnsDriver(),
|
|
386
|
+
httpDriver({ method: "GET" }),
|
|
387
|
+
],
|
|
388
|
+
}),
|
|
356
389
|
});
|
|
357
390
|
```
|
|
358
391
|
|
|
@@ -392,8 +425,19 @@ for (const port of ports) {
|
|
|
392
425
|
### Network Diagnostics
|
|
393
426
|
|
|
394
427
|
```typescript
|
|
428
|
+
import hybridDriver from "unping/drivers/hybrid";
|
|
429
|
+
import tcpDriver from "unping/drivers/tcp";
|
|
430
|
+
import httpDriver from "unping/drivers/http";
|
|
431
|
+
import dnsDriver from "unping/drivers/dns";
|
|
432
|
+
|
|
395
433
|
const diagnostic = createPingManager({
|
|
396
|
-
driver: hybridDriver({
|
|
434
|
+
driver: hybridDriver({
|
|
435
|
+
drivers: [
|
|
436
|
+
tcpDriver({ port: 80 }),
|
|
437
|
+
httpDriver({ method: "HEAD" }),
|
|
438
|
+
dnsDriver(),
|
|
439
|
+
],
|
|
440
|
+
}),
|
|
397
441
|
});
|
|
398
442
|
|
|
399
443
|
const hosts = ["google.com", "github.com", "cloudflare.com"];
|
package/dist/drivers/hybrid.cjs
CHANGED
|
@@ -6,41 +6,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
module.exports = hybridDriver;
|
|
7
7
|
function hybridDriver(options = {}) {
|
|
8
8
|
const {
|
|
9
|
-
|
|
9
|
+
drivers: customDrivers
|
|
10
10
|
} = options;
|
|
11
|
+
const defaultDrivers = getDefaultDrivers();
|
|
12
|
+
const drivers = customDrivers || defaultDrivers;
|
|
11
13
|
const ping = async (host, opts) => {
|
|
12
14
|
const lastError = new Error("All drivers failed");
|
|
13
|
-
for (const
|
|
15
|
+
for (const driver of drivers) {
|
|
14
16
|
try {
|
|
15
|
-
let driver;
|
|
16
|
-
switch (driverType) {
|
|
17
|
-
case "http":
|
|
18
|
-
{
|
|
19
|
-
const {
|
|
20
|
-
default: httpDriver
|
|
21
|
-
} = await Promise.resolve().then(() => require("./http.cjs"));
|
|
22
|
-
driver = httpDriver(options);
|
|
23
|
-
break;
|
|
24
|
-
}
|
|
25
|
-
case "tcp":
|
|
26
|
-
{
|
|
27
|
-
const {
|
|
28
|
-
default: tcpDriver
|
|
29
|
-
} = await Promise.resolve().then(() => require("./tcp.cjs"));
|
|
30
|
-
driver = tcpDriver(options);
|
|
31
|
-
break;
|
|
32
|
-
}
|
|
33
|
-
case "dns":
|
|
34
|
-
{
|
|
35
|
-
const {
|
|
36
|
-
default: dnsDriver
|
|
37
|
-
} = await Promise.resolve().then(() => require("./dns.cjs"));
|
|
38
|
-
driver = dnsDriver(options);
|
|
39
|
-
break;
|
|
40
|
-
}
|
|
41
|
-
default:
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
17
|
const result = await driver.ping(host, opts);
|
|
45
18
|
return result;
|
|
46
19
|
} catch (error) {
|
|
@@ -55,4 +28,22 @@ function hybridDriver(options = {}) {
|
|
|
55
28
|
options,
|
|
56
29
|
ping
|
|
57
30
|
};
|
|
31
|
+
}
|
|
32
|
+
function getDefaultDrivers() {
|
|
33
|
+
const {
|
|
34
|
+
default: tcpDriver
|
|
35
|
+
} = require("./tcp.cjs");
|
|
36
|
+
const {
|
|
37
|
+
default: httpDriver
|
|
38
|
+
} = require("./http.cjs");
|
|
39
|
+
const {
|
|
40
|
+
default: dnsDriver
|
|
41
|
+
} = require("./dns.cjs");
|
|
42
|
+
return [tcpDriver({
|
|
43
|
+
port: 80
|
|
44
|
+
}), httpDriver({
|
|
45
|
+
method: "HEAD"
|
|
46
|
+
}), dnsDriver({
|
|
47
|
+
type: "A"
|
|
48
|
+
})];
|
|
58
49
|
}
|
package/dist/drivers/hybrid.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Driver, DriverOptions } from "../types";
|
|
2
2
|
export interface HybridDriverOptions extends DriverOptions {
|
|
3
|
-
/**
|
|
4
|
-
|
|
3
|
+
/** Array of drivers to try in order (default: [tcp, http, dns]) */
|
|
4
|
+
drivers?: Driver[];
|
|
5
5
|
}
|
|
6
6
|
export default function hybridDriver(options?: HybridDriverOptions): Driver;
|
package/dist/drivers/hybrid.mjs
CHANGED
|
@@ -1,29 +1,11 @@
|
|
|
1
1
|
export default function hybridDriver(options = {}) {
|
|
2
|
-
const {
|
|
2
|
+
const { drivers: customDrivers } = options;
|
|
3
|
+
const defaultDrivers = getDefaultDrivers();
|
|
4
|
+
const drivers = customDrivers || defaultDrivers;
|
|
3
5
|
const ping = async (host, opts) => {
|
|
4
6
|
const lastError = new Error("All drivers failed");
|
|
5
|
-
for (const
|
|
7
|
+
for (const driver of drivers) {
|
|
6
8
|
try {
|
|
7
|
-
let driver;
|
|
8
|
-
switch (driverType) {
|
|
9
|
-
case "http": {
|
|
10
|
-
const { default: httpDriver } = await import("./http.mjs");
|
|
11
|
-
driver = httpDriver(options);
|
|
12
|
-
break;
|
|
13
|
-
}
|
|
14
|
-
case "tcp": {
|
|
15
|
-
const { default: tcpDriver } = await import("./tcp.mjs");
|
|
16
|
-
driver = tcpDriver(options);
|
|
17
|
-
break;
|
|
18
|
-
}
|
|
19
|
-
case "dns": {
|
|
20
|
-
const { default: dnsDriver } = await import("./dns.mjs");
|
|
21
|
-
driver = dnsDriver(options);
|
|
22
|
-
break;
|
|
23
|
-
}
|
|
24
|
-
default:
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
9
|
const result = await driver.ping(host, opts);
|
|
28
10
|
return result;
|
|
29
11
|
} catch (error) {
|
|
@@ -39,3 +21,13 @@ export default function hybridDriver(options = {}) {
|
|
|
39
21
|
ping
|
|
40
22
|
};
|
|
41
23
|
}
|
|
24
|
+
function getDefaultDrivers() {
|
|
25
|
+
const { default: tcpDriver } = require("./tcp");
|
|
26
|
+
const { default: httpDriver } = require("./http");
|
|
27
|
+
const { default: dnsDriver } = require("./dns");
|
|
28
|
+
return [
|
|
29
|
+
tcpDriver({ port: 80 }),
|
|
30
|
+
httpDriver({ method: "HEAD" }),
|
|
31
|
+
dnsDriver({ type: "A" })
|
|
32
|
+
];
|
|
33
|
+
}
|