From 097d2492a537fb62f58532596ee5c7d69f3d0b9b Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 19 Sep 2021 16:28:57 +0700 Subject: [PATCH] Require Node.js 12.20 and move to ESM Fixes #7 Fixes #4 --- .github/funding.yml | 2 -- .github/workflows/main.yml | 7 ++----- index.d.ts | 25 ++++++++++++------------- index.js | 13 ++++++++----- index.test-d.ts | 4 ++-- license | 2 +- package.json | 13 ++++++++----- readme.md | 33 ++++++++++++++++----------------- test.js | 12 +++++++++--- 9 files changed, 58 insertions(+), 53 deletions(-) delete mode 100644 .github/funding.yml diff --git a/.github/funding.yml b/.github/funding.yml deleted file mode 100644 index 7ede066..0000000 --- a/.github/funding.yml +++ /dev/null @@ -1,2 +0,0 @@ -github: sindresorhus -custom: https://sindresorhus.com/donate diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18531b3..441975c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,13 +10,10 @@ jobs: fail-fast: false matrix: node-version: - - 14 - - 12 - - 10 - - 8 + - 16 steps: - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.d.ts b/index.d.ts index 820bd82..3adb0d9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,17 +1,19 @@ -interface Options { +export interface Options { /** - Milliseconds to wait before giving up. + The host to check. - @default 1000 + Can be a domain (optionally, with a sub-domain) or an IP address. + + @example 'localhost' */ - timeout?: number; + readonly host: string; /** - Can be a domain or an IP. + The time to wait in milliseconds before giving up. - @default 'localhost' + @default 1000 */ - host?: string; + readonly timeout?: number; } /** @@ -21,11 +23,8 @@ Check if a local or remote port is reachable. ``` import isPortReachable from 'is-port-reachable'; -if(await isPortReachable(3000)) { - // start server -} +console.log(await isPortReachable(80, {host: 'google.com'})); +//=> true ``` */ -declare function isPortReachable(port: number, options?: Options): Promise; - -export = isPortReachable; +export default function isPortReachable(port: number, options: Options): Promise; diff --git a/index.js b/index.js index 8f09a37..ed9557a 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,10 @@ -'use strict'; -const net = require('net'); +import net from 'node:net'; + +export default async function isPortReachable(port, {host, timeout = 1000} = {}) { + if (typeof host !== 'string') { + throw new TypeError('Specify a `host`'); + } -module.exports = async (port, {timeout = 1000, host} = {}) => { const promise = new Promise(((resolve, reject) => { const socket = new net.Socket(); @@ -23,7 +26,7 @@ module.exports = async (port, {timeout = 1000, host} = {}) => { try { await promise; return true; - } catch (_) { + } catch { return false; } -}; +} diff --git a/index.test-d.ts b/index.test-d.ts index 0a1ebe8..c406748 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,4 +1,4 @@ import {expectType} from 'tsd'; -import isPortReachable = require('.'); +import isPortReachable from './index.js'; -expectType(await isPortReachable(3000)); +expectType>(isPortReachable(3000, {host: 'localhost'})); diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index a7701ea..cb8dd4e 100644 --- a/package.json +++ b/package.json @@ -4,16 +4,19 @@ "description": "Check if a local or remote port is reachable", "license": "MIT", "repository": "sindresorhus/is-port-reachable", + "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, "contributors": [ "silverwind (github.com/silverwind)" ], + "type": "module", + "exports": "./index.js", "engines": { - "node": ">=8" + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "test": "xo && ava && tsd" @@ -40,8 +43,8 @@ "check" ], "devDependencies": { - "ava": "^2.4.0", - "xo": "^0.25.3", - "tsd": "^0.17.0" + "ava": "^3.15.0", + "tsd": "^0.17.0", + "xo": "^0.44.0" } } diff --git a/readme.md b/readme.md index efde84a..644a902 100644 --- a/readme.md +++ b/readme.md @@ -2,54 +2,53 @@ > Check if a local or remote port is reachable - ## Install -``` -$ npm install is-port-reachable +```sh +npm install is-port-reachable ``` - ## Usage ```js -const isPortReachable = require('is-port-reachable'); +import isPortReachable from 'is-port-reachable'; -(async () => { - console.log(await isPortReachable(80, {host: 'google.com'})); - //=> true -})(); +console.log(await isPortReachable(80, {host: 'google.com'})); +//=> true ``` - ## API -### isPortReachable(port, options?) +### isPortReachable(options) -Returns `Promise`. +Returns `Promise` for whether the port is reachable. -#### port +##### port Type: `number` +The port to check. + #### options Type: `object` ##### host +**Required**\ Type: `string`\ -Default: `'localhost'` +Example: `'localhost'` + +The host to check. -Can be a domain or an IP. +Can be a domain (optionally, with a sub-domain) or an IP address. ##### timeout Type: `number`\ Default: `1000` -Milliseconds to wait before giving up. - +The time to wait in milliseconds before giving up. ## Related diff --git a/test.js b/test.js index 1880d95..bdb3a03 100644 --- a/test.js +++ b/test.js @@ -1,9 +1,10 @@ +import process from 'node:process'; import test from 'ava'; -import isPortReachable from '.'; +import isPortReachable from './index.js'; if (!('CI' in process.env)) { test('ip', async t => { - t.true(await isPortReachable(80, {host: '216.58.217.142'})); + t.true(await isPortReachable(80, {host: '142.250.199.46'})); // 142.250.199.46 === google.com }); } @@ -15,6 +16,11 @@ test('domain - alternative port', async t => { t.false(await isPortReachable(8000, {host: 'google.com'})); }); +test('domain - with subdomain', async t => { + t.true(await isPortReachable(80, {host: 'blog.sindresorhus.com'})); +}); + test('fail', async t => { - t.false(await isPortReachable(0)); + t.false(await isPortReachable(0, {host: 'localhost'})); + await t.throwsAsync(isPortReachable(80), {message: 'Specify a `host`'}); });