Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
Fixes #7
Fixes #4
  • Loading branch information
sindresorhus committed Sep 19, 2021
1 parent ca9a379 commit 097d249
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 53 deletions.
2 changes: 0 additions & 2 deletions .github/funding.yml

This file was deleted.

7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 12 additions & 13 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}

/**
Expand All @@ -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<boolean>;

export = isPortReachable;
export default function isPortReachable(port: number, options: Options): Promise<boolean>;
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -23,7 +26,7 @@ module.exports = async (port, {timeout = 1000, host} = {}) => {
try {
await promise;
return true;
} catch (_) {
} catch {
return false;
}
};
}
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import isPortReachable = require('.');
import isPortReachable from './index.js';

expectType<boolean>(await isPortReachable(3000));
expectType<Promise<boolean>>(isPortReachable(3000, {host: 'localhost'}));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:

Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <me@silverwind.io> (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"
Expand All @@ -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"
}
}
33 changes: 16 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>`.
Returns `Promise<boolean>` 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

Expand Down
12 changes: 9 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -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
});
}

Expand All @@ -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`'});
});

0 comments on commit 097d249

Please sign in to comment.