Skip to content

Commit

Permalink
Require Node.js 18
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jul 26, 2024
1 parent b0d113a commit 632e989
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
- 16
- 14
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
7 changes: 4 additions & 3 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env browser */
import publicIp from 'public-ip';
import {publicIpv4, publicIpv6} from 'public-ip';

export default async function isOnline(options) {
options = {
Expand All @@ -8,14 +8,15 @@ export default async function isOnline(options) {
...options,
};

// eslint-disable-next-line n/no-unsupported-features/node-builtins
if (!navigator?.onLine) {
return false;
}

const publicIpFunctionName = options.ipVersion === 4 ? 'v4' : 'v6';
const publicIpFunction = options.ipVersion === 4 ? publicIpv4 : publicIpv6;

try {
await publicIp[publicIpFunctionName](options);
await publicIpFunction(options);
return true;
} catch {
return false;
Expand Down
13 changes: 6 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os from 'node:os';
import got, {CancelError} from 'got';
import publicIp from 'public-ip';
import {publicIpv4, publicIpv6} from 'public-ip';
import pAny from 'p-any';
import pTimeout from 'p-timeout';

Expand All @@ -18,7 +18,7 @@ const appleCheck = options => {
const promise = (async () => {
try {
const {body} = await gotPromise;
if (!body || !body.includes('Success')) {
if (!body?.includes('Success')) {
throw new Error('Apple check failed');
}
} catch (error) {
Expand Down Expand Up @@ -49,19 +49,18 @@ export default function isOnline(options) {
throw new TypeError('`ipVersion` must be 4 or 6');
}

const publicIpFunctionName = options.ipVersion === 4 ? 'v4' : 'v6';

const publicIpFunction = options.ipVersion === 4 ? publicIpv4 : publicIpv6;
const queries = [];

const promise = pAny([
(async () => {
const query = publicIp[publicIpFunctionName](options);
const query = publicIpFunction(options);
queries.push(query);
await query;
return true;
})(),
(async () => {
const query = publicIp[publicIpFunctionName]({...options, onlyHttps: true});
const query = publicIpFunction({...options, onlyHttps: true});
queries.push(query);
await query;
return true;
Expand All @@ -74,7 +73,7 @@ export default function isOnline(options) {
})(),
]);

return pTimeout(promise, options.timeout).catch(() => {
return pTimeout(promise, {milliseconds: options.timeout}).catch(() => { // eslint-disable-line promise/prefer-await-to-then
for (const query of queries) {
query.cancel();
}
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
"node": "./index.js",
"default": "./browser.js"
},
"sideEffects": false,
"engines": {
"node": ">=14.16"
"node": ">=18"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -53,15 +54,15 @@
"disconnected"
],
"dependencies": {
"got": "^12.1.0",
"got": "^13.0.0",
"p-any": "^4.0.0",
"p-timeout": "^5.1.0",
"public-ip": "^5.0.0"
"p-timeout": "^6.1.2",
"public-ip": "^7.0.1"
},
"devDependencies": {
"ava": "^4.3.0",
"tsd": "^0.20.0",
"xo": "^0.49.0"
"ava": "^6.1.3",
"tsd": "^0.31.1",
"xo": "^0.59.2"
},
"ava": {
"files": [
Expand Down
2 changes: 1 addition & 1 deletion test-browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Need to test manually in DevTools
// $ browserify test-browser.js | pbcopy
// $ npx browserify test-browser.js | pbcopy
import isOnline from './browser.js';

console.log('is online:', await isOnline());

0 comments on commit 632e989

Please sign in to comment.