feat: add net.online / net.isOnline() (#21004)

This commit is contained in:
Milan Burda 2020-10-21 04:55:06 +02:00 committed by GitHub
parent 1ef803d2ea
commit df1432a315
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 1 deletions

View file

@ -8,7 +8,7 @@ The `net` module is a client-side API for issuing HTTP(S) requests. It is
similar to the [HTTP](https://nodejs.org/api/http.html) and
[HTTPS](https://nodejs.org/api/https.html) modules of Node.js but uses
Chromium's native networking library instead of the Node.js implementation,
offering better support for web proxies.
offering better support for web proxies. It also supports checking network status.
The following is a non-exhaustive list of why you may consider using the `net`
module instead of the native Node.js modules:
@ -62,3 +62,25 @@ Creates a [`ClientRequest`](./client-request.md) instance using the provided
`options` which are directly forwarded to the `ClientRequest` constructor.
The `net.request` method would be used to issue both secure and insecure HTTP
requests according to the specified protocol scheme in the `options` object.
### `net.isOnline()`
Returns `Boolean` - Whether there is currently internet connection.
A return value of `false` is a pretty strong indicator that the user
won't be able to connect to remote sites. However, a return value of
`true` is inconclusive; even if some link is up, it is uncertain
whether a particular connection attempt to a particular remote site
will be successful.
## Properties
### `net.online` _Readonly_
A `Boolean` property. Whether there is currently internet connection.
A return value of `false` is a pretty strong indicator that the user
won't be able to connect to remote sites. However, a return value of
`true` is inconclusive; even if some link is up, it is uncertain
whether a particular connection attempt to a particular remote site
will be successful.

View file

@ -4,6 +4,7 @@ import { app } from 'electron/main';
import type { ClientRequestConstructorOptions, UploadProgress } from 'electron/main';
const {
isOnline,
isValidHeaderName,
isValidHeaderValue,
createURLLoader
@ -516,3 +517,9 @@ export class ClientRequest extends Writable implements Electron.ClientRequest {
export function request (options: ClientRequestConstructorOptions | string, callback?: (message: IncomingMessage) => void) {
return new ClientRequest(options, callback);
}
exports.isOnline = isOnline;
Object.defineProperty(exports, 'online', {
get: () => isOnline()
});

View file

@ -5,6 +5,7 @@
#include <string>
#include "gin/handle.h"
#include "net/base/network_change_notifier.h"
#include "services/network/public/cpp/features.h"
#include "shell/browser/api/electron_api_url_loader.h"
#include "shell/common/gin_helper/dictionary.h"
@ -14,6 +15,10 @@
namespace {
bool IsOnline() {
return !net::NetworkChangeNotifier::IsOffline();
}
bool IsValidHeaderName(std::string header_name) {
return net::HttpUtil::IsValidHeaderName(header_name);
}
@ -31,6 +36,7 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Isolate* isolate = context->GetIsolate();
gin_helper::Dictionary dict(isolate, exports);
dict.SetMethod("isOnline", &IsOnline);
dict.SetMethod("isValidHeaderName", &IsValidHeaderName);
dict.SetMethod("isValidHeaderValue", &IsValidHeaderValue);
dict.SetMethod("createURLLoader", &SimpleURLLoaderWrapper::Create);

View file

@ -1500,6 +1500,16 @@ describe('net module', () => {
});
});
describe('net.isOnline', () => {
it('getter returns boolean', () => {
expect(net.isOnline()).to.be.a('boolean');
});
it('property returns boolean', () => {
expect(net.online).to.be.a('boolean');
});
});
describe('Stability and performance', () => {
it('should free unreferenced, never-started request objects without crash', (done) => {
net.request('https://test');

View file

@ -194,6 +194,7 @@ declare namespace NodeJS {
createPair(): { port1: Electron.MessagePortMain, port2: Electron.MessagePortMain };
};
_linkedBinding(name: 'electron_browser_net'): {
isOnline(): boolean;
isValidHeaderName: (headerName: string) => boolean;
isValidHeaderValue: (headerValue: string) => boolean;
Net: any;