-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathnetwork-connectivity-validator.ts
38 lines (34 loc) · 1.21 KB
/
network-connectivity-validator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import * as dns from "dns";
import { INetworkConnectivityValidator } from "../declarations";
import { injector } from "../common/yok";
import { IErrors } from "../common/declarations";
export class NetworkConnectivityValidator
implements INetworkConnectivityValidator {
private static DNS_LOOKUP_URL = "play.nativescript.org";
private static NO_INTERNET_ERROR_CODE = "ENOTFOUND";
private static NO_INTERNET_ERROR_MESSAGE =
"No internet connection. Check your internet settings and try again.";
constructor(private $errors: IErrors, private $logger: ILogger) {}
public async validate(): Promise<void> {
const isConnected = await this.isConnected();
if (!isConnected) {
this.$errors.fail(NetworkConnectivityValidator.NO_INTERNET_ERROR_MESSAGE);
}
}
private isConnected(): Promise<boolean> {
return new Promise((resolve, reject) => {
dns.lookup(NetworkConnectivityValidator.DNS_LOOKUP_URL, (err) => {
this.$logger.trace(`Error from dns.lookup is ${err}.`);
if (
err &&
err.code === NetworkConnectivityValidator.NO_INTERNET_ERROR_CODE
) {
resolve(false);
} else {
resolve(true);
}
});
});
}
}
injector.register("networkConnectivityValidator", NetworkConnectivityValidator);