├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | steps: 15 | - uses: actions/checkout@v4 16 | - uses: actions/setup-node@v4 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export interface Options { 2 | /** 3 | @default 80 4 | */ 5 | port?: number; 6 | 7 | /** 8 | Use a custom path. 9 | 10 | For example, `/health` for a health-check endpoint. 11 | 12 | @default '/' 13 | */ 14 | path?: string; 15 | 16 | /** 17 | Use the `GET` HTTP-method instead of `HEAD` to check if the server is running. 18 | 19 | @default false 20 | */ 21 | useGet?: boolean; 22 | 23 | /** 24 | HTTP status codes to consider as successful responses. 25 | 26 | @default [200] 27 | */ 28 | statusCodes?: readonly number[]; 29 | } 30 | 31 | /** 32 | Wait for localhost to be ready. 33 | 34 | @example 35 | ``` 36 | import waitForLocalhost from 'wait-for-localhost'; 37 | 38 | await waitForLocalhost({port: 8080}); 39 | console.log('Server is ready'); 40 | ``` 41 | */ 42 | export default function waitForLocalhost(options?: Options): Promise<{ipVersion: 4 | 6}>; 43 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import http from 'node:http'; 2 | 3 | export default function waitForLocalhost({port, path, useGet, statusCodes = [200]} = {}) { 4 | return new Promise(resolve => { 5 | const retry = () => { 6 | setTimeout(main, 200); 7 | }; 8 | 9 | const method = useGet ? 'GET' : 'HEAD'; 10 | 11 | const doRequest = (ipVersion, next) => { 12 | const request = http.request({method, port, path, family: ipVersion}, response => { 13 | if (statusCodes.includes(response.statusCode)) { 14 | resolve({ipVersion}); 15 | return; 16 | } 17 | 18 | next(); 19 | }); 20 | 21 | request.on('error', next); 22 | request.end(); 23 | }; 24 | 25 | const main = () => { 26 | doRequest(4, 27 | () => doRequest(6, 28 | () => retry(), 29 | ), 30 | ); 31 | }; 32 | 33 | main(); 34 | }); 35 | } 36 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import waitForLocalhost from './index.js'; 3 | 4 | expectType>(waitForLocalhost()); 5 | expectType>(waitForLocalhost({port: 8080})); 6 | expectType>(waitForLocalhost({path: '/health'})); 7 | expectType>(waitForLocalhost({useGet: true})); 8 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wait-for-localhost", 3 | "version": "4.1.0", 4 | "description": "Wait for localhost to be ready", 5 | "license": "MIT", 6 | "repository": "sindresorhus/wait-for-localhost", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "types": "./index.d.ts", 16 | "sideEffects": false, 17 | "engines": { 18 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 19 | }, 20 | "scripts": { 21 | "test": "xo && ava && tsd" 22 | }, 23 | "files": [ 24 | "index.js", 25 | "index.d.ts" 26 | ], 27 | "keywords": [ 28 | "wait", 29 | "localhost", 30 | "server", 31 | "port", 32 | "delay", 33 | "sleep", 34 | "ready" 35 | ], 36 | "devDependencies": { 37 | "ava": "^3.15.0", 38 | "create-test-server": "^3.0.1", 39 | "delay": "^5.0.0", 40 | "tsd": "^0.17.0", 41 | "xo": "^0.44.0" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # wait-for-localhost 2 | 3 | > Wait for localhost to be ready 4 | 5 | Useful if you need a local server to be ready to accept requests before doing other things. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install wait-for-localhost 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import waitForLocalhost from 'wait-for-localhost'; 17 | 18 | await waitForLocalhost({port: 8080}); 19 | console.log('Server is ready'); 20 | ``` 21 | 22 | ## API 23 | 24 | ### waitForLocalHost(options?) 25 | 26 | Returns a `Promise` that settles when localhost is ready. 27 | 28 | The object contains a `ipVersion` property with a value of either `6` or `4` depending on the IP version that was used. 29 | 30 | #### options 31 | 32 | Type: `object` 33 | 34 | ##### port 35 | 36 | Type: `number`\ 37 | Default: `80` 38 | 39 | ##### path 40 | 41 | Type: `string`\ 42 | Default: `'/'` 43 | 44 | Use a custom path. 45 | 46 | For example, `/health` for a health-check endpoint. 47 | 48 | ##### useGet 49 | 50 | Type: `boolean`\ 51 | Default: `false` 52 | 53 | Use the `GET` HTTP-method instead of `HEAD` to check if the server is running. 54 | 55 | ##### statusCodes 56 | 57 | Type: `number[]`\ 58 | Default: `[200]` 59 | 60 | HTTP status codes to consider as successful responses. 61 | 62 | ## Related 63 | 64 | - [wait-for-localhost-cli](https://github.com/sindresorhus/wait-for-localhost-cli) - CLI for this module 65 | - [delay](https://github.com/sindresorhus/delay) - Delay execution for a given amount of seconds 66 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import delay from 'delay'; 3 | import createTestServer from 'create-test-server'; 4 | import waitForLocalhost from './index.js'; 5 | 6 | test('main', async t => { 7 | t.plan(2); 8 | 9 | const server = await createTestServer(); 10 | server.head('/', async (request, response) => { 11 | await delay(1000); 12 | response.end(); 13 | t.pass(); 14 | }); 15 | 16 | await waitForLocalhost({port: server.port}); 17 | 18 | t.pass(); 19 | 20 | await server.close(); 21 | }); 22 | 23 | test('use get method', async t => { 24 | t.plan(2); 25 | 26 | const server = await createTestServer(); 27 | server.get('/', async (request, response) => { 28 | await delay(1000); 29 | response.end(); 30 | t.pass(); 31 | }); 32 | 33 | await waitForLocalhost({ 34 | port: server.port, 35 | useGet: true, 36 | }); 37 | 38 | t.pass(); 39 | 40 | await server.close(); 41 | }); 42 | 43 | test('use custom path', async t => { 44 | t.plan(2); 45 | 46 | const server = await createTestServer(); 47 | server.get('/health', async (request, response) => { 48 | await delay(1000); 49 | response.end(); 50 | t.pass(); 51 | }); 52 | 53 | await waitForLocalhost({ 54 | port: server.port, 55 | path: '/health', 56 | }); 57 | 58 | t.pass(); 59 | 60 | await server.close(); 61 | }); 62 | 63 | test('use custom statusCodes', async t => { 64 | t.plan(2); 65 | 66 | const server = await createTestServer(); 67 | server.get('/', async (request, response) => { 68 | await delay(1000); 69 | response.status(202).end(); 70 | t.pass(); 71 | }); 72 | 73 | await waitForLocalhost({ 74 | port: server.port, 75 | statusCodes: [201, 202], 76 | }); 77 | 78 | t.pass(); 79 | 80 | await server.close(); 81 | }); 82 | --------------------------------------------------------------------------------