Skip to content

Commit

Permalink
feat: add net.online / net.isOnline() (electron#21004)
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak authored Oct 21, 2020
1 parent 1ef803d commit df1432a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
24 changes: 23 additions & 1 deletion docs/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
7 changes: 7 additions & 0 deletions lib/browser/api/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { app } from 'electron/main';
import type { ClientRequestConstructorOptions, UploadProgress } from 'electron/main';

const {
isOnline,
isValidHeaderName,
isValidHeaderValue,
createURLLoader
Expand Down Expand Up @@ -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()
});
6 changes: 6 additions & 0 deletions shell/browser/api/electron_api_net.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -14,6 +15,10 @@

namespace {

bool IsOnline() {
return !net::NetworkChangeNotifier::IsOffline();
}

bool IsValidHeaderName(std::string header_name) {
return net::HttpUtil::IsValidHeaderName(header_name);
}
Expand All @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions spec-main/api-net-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions typings/internal-ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit df1432a

Please sign in to comment.