From 4bfd3e2544afe51b1882bd59f82b168a47dab47f Mon Sep 17 00:00:00 2001 From: James Carragher Date: Wed, 1 Nov 2023 16:02:58 +0000 Subject: [PATCH] fix(cancel)!: cancel _checkInternetReachability request on timeout & cancel via AbortController (#678) * fix!: cancel _checkInternetReachability request on timeout & cancel Taken from https://github.com/react-native-netinfo/react-native-netinfo/issues/439#issue-787487438 * 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 --- .github/workflows/ci.yml | 4 ++-- README.md | 6 ++++++ src/internal/internetReachability.ts | 16 +++++++++++----- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f8053db2..af924349 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 diff --git a/README.md b/README.md index a9fadd44..0f605e31 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/internal/internetReachability.ts b/src/internal/internetReachability.ts index 55d018c1..2cd4dee4 100644 --- a/src/internal/internetReachability.ts +++ b/src/internal/internetReachability.ts @@ -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; const timeoutPromise = new Promise( (_, reject): void => { - timeoutHandle = setTimeout( - (): void => reject('timedout'), - this._configuration.reachabilityRequestTimeout, - ); + timeoutHandle = setTimeout((): void => { + controller.abort(); + reject('timedout'); + }, this._configuration.reachabilityRequestTimeout); }, ); @@ -90,7 +93,10 @@ export default class InternetReachability { let cancel: () => void = (): void => {}; const cancelPromise = new Promise( (_, reject): void => { - cancel = (): void => reject('canceled'); + cancel = (): void => { + controller.abort(); + reject('canceled'); + }; }, );