Skip to content

Commit

Permalink
fix(cancel)!: cancel _checkInternetReachability request on timeout & …
Browse files Browse the repository at this point in the history
…cancel via AbortController (#678)

* fix!: cancel _checkInternetReachability request on timeout & cancel

Taken from #439 (comment)

* style(lint): yarn validate:estlint --fix

eslint behaves differently right now between windows/macOS and ubuntu
the CI check is on ubuntu, and some problems are only showing up there

* fix!: node v16 is now the minimum supported version - AbortController

BREAKING CHANGE: netinfo now requires AbortController, node v16 / edge required
it will likely not work on internet explorer from this version onwards
  • Loading branch information
jcarrag authored and mikehardy committed Nov 1, 2023
1 parent bbebadf commit 4bfd3e2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
node-version: '16'
cache: 'yarn'
- run: yarn install --frozen-lockfile
- run: yarn test
Expand All @@ -25,7 +25,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
node-version: '16'
cache: 'yarn'
- run: yarn install --frozen-lockfile
- run: yarn ci:publish
Expand Down
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -80,6 +80,12 @@ but no support will be provided.
The web implementation heavily depends on the [Network Information API](https://developer.mozilla.org/en-US/docs/Web/API/Network_Information_API) which is still an is an experimental technology and thus it's not supported in every browser.
If this API is not available the library will safely fallback to the old [onLine](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine) property and return basic connection information.

AbortController is used to cancel network requests, and may not be available on Internet Explorer, though it is available on Edge https://caniuse.com/abortcontroller

## Node Compatibility

Node v16 is the minimum required node version - `AbortController` is only present in stable versions of node from v16 on

## Migrating from the core `react-native` module
This module was created when the NetInfo was split out from the core of React Native. To migrate to this module you need to follow the installation instructions above and then change you imports from:

Expand Down
16 changes: 11 additions & 5 deletions src/internal/internetReachability.ts
Expand Up @@ -68,20 +68,23 @@ export default class InternetReachability {
};

private _checkInternetReachability = (): InternetReachabilityCheckHandler => {
const controller = new AbortController();

const responsePromise = fetch(this._configuration.reachabilityUrl, {
headers: this._configuration.reachabilityHeaders,
method: this._configuration.reachabilityMethod,
cache: 'no-cache',
signal: controller.signal,
});

// Create promise that will reject after the request timeout has been reached
let timeoutHandle: ReturnType<typeof setTimeout>;
const timeoutPromise = new Promise<Response>(
(_, reject): void => {
timeoutHandle = setTimeout(
(): void => reject('timedout'),
this._configuration.reachabilityRequestTimeout,
);
timeoutHandle = setTimeout((): void => {
controller.abort();
reject('timedout');
}, this._configuration.reachabilityRequestTimeout);
},
);

Expand All @@ -90,7 +93,10 @@ export default class InternetReachability {
let cancel: () => void = (): void => {};
const cancelPromise = new Promise<Response>(
(_, reject): void => {
cancel = (): void => reject('canceled');
cancel = (): void => {
controller.abort();
reject('canceled');
};
},
);

Expand Down

0 comments on commit 4bfd3e2

Please sign in to comment.