Skip to content

Commit

Permalink
chore: rename to "until-connected"
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Jan 17, 2024
1 parent ad5945c commit b191e1e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Wait For Connection
# Until Connected

Wait for a connection at the given target without making any requests.

Expand Down Expand Up @@ -31,19 +31,19 @@ This module solves this problem by relying on establishing a socket connection (
## Usage

```sh
npm i wait-for-connection
npm i until-connected
```

```js
import { exec } from 'node:child_process'
import { waitForConnection } from 'wait-for-connection'
import { untilConnected } from 'until-connected'

async function runServerThenTest(runCommand, url, testCommand) {
// Start the application server.
exec(runCommand)

// Wait for the application server to be running.
await waitForConnection({ target: url })
await untilConnected({ target: url })

// Run the tests.
exec(testCommand)
Expand All @@ -52,19 +52,19 @@ async function runServerThenTest(runCommand, url, testCommand) {

## API

### `waitForConnection(options)`
### `untilConnected(options)`

Returns a Promise that resolves if the connection was successful and rejects if it wasn't, given additional options.

```js
// Wait for the application on port 3000.
await waitForConnection({ target: 3000 })
await untilConnected({ target: 3000 })
```

The returned Promise rejects if the connection couldn't be established or if it fails for any other reason. The original connection error is exposed under `error.cause` on the Promise:

```js
waitForConnection({ target: 3000 }).catch((error) => {
untilConnected({ target: 3000 }).catch((error) => {
console.log('Original connection error:', error.cause)
})
```
Expand All @@ -79,12 +79,12 @@ A target to await. Supports a standalone port number and a full URL string or a

```js
// Wait for the application on port 3000.
await waitForConnection({
await untilConnected({
target: 3000,
})

// Wait for the application at the address.
await waitForConnection({
await untilConnected({
target: 'http://localhost:56789',
})
```
Expand All @@ -96,7 +96,7 @@ await waitForConnection({
Maximum number of retries before rejecting the connection Promise.

```js
await waitForConnection({
await untilConnected({
port: 3000,
// By using 1, the wait function will reject
// if the connection fails after the first attempt.
Expand All @@ -111,7 +111,7 @@ await waitForConnection({
An interval between connection retries.

```js
await waitForConnection({
await untilConnected({
target: 3000,
// Wait for 500ms after a failed connection
// before attempting another connection.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "wait-for-connection",
"name": "until-connected",
"type": "module",
"version": "1.0.0",
"description": "Wait for a connection at the given target without making any requests.",
Expand Down
2 changes: 1 addition & 1 deletion release.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": [
{
"name": "latest",
"use": "pnpm publish --no-git-checks"
"use": "pnpm publish --no-git-checks --access public"
}
]
}
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ export type Options = {
*
* @example
* // Wait until the server is running at port 3000.
* waitForConnection({ target: 3000 })
* untilConnected({ target: 3000 })
*
* // Wait until the server is running at the address.
* waitForConnection({ target: 'http://127.0.0.1:56789' })
* untilConnected({ target: 'http://127.0.0.1:56789' })
*
* // Control the maximum number of retries before rejecting.
* // When using 1, this will reject if the server isn't already running.
* waitForConnection({ target: 3000, maxRetries: 1 })
* untilConnected({ target: 3000, maxRetries: 1 })
*/
export function waitForConnection(options: Options): Promise<void> {
export function untilConnected(options: Options): Promise<void> {
const { port, host } = resolveConnectionOptions(options)
let maxRetries = options.maxRetries || 5
let retries = 0
Expand Down
14 changes: 7 additions & 7 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { waitForConnection } from '../src'
import { createServer } from './utils'
import { untilConnected } from '../src/index.js'
import { createServer } from './utils.js'

it('resolves immediately for already open connection', async () => {
await createServer(56780)
await expect(waitForConnection({ target: 56780 })).resolves.toBeUndefined()
await expect(untilConnected({ target: 56780 })).resolves.toBeUndefined()
})

it('resolves when the connection gets open', async () => {
const connectionPromise = waitForConnection({ target: 56781 })
const connectionPromise = untilConnected({ target: 56781 })
createServer(56781)
await expect(connectionPromise).resolves.toBeUndefined()
})

it('resolves if the connection opens within the timeout', async () => {
const connectionPromise = waitForConnection({
const connectionPromise = untilConnected({
target: 56782,
maxRetries: 5,
connectionInterval: 100,
Expand All @@ -23,7 +23,7 @@ it('resolves if the connection opens within the timeout', async () => {
})

it('rejects if the connection fails to open within the timeout', async () => {
const errorPromise = waitForConnection({
const errorPromise = untilConnected({
target: 56783,
connectionInterval: 100,
})
Expand All @@ -42,7 +42,7 @@ it('rejects if the connection fails to open within the timeout', async () => {
})

it('rejects if the connection fails to open after max retries', async () => {
const error = await waitForConnection({ target: 3000 })
const error = await untilConnected({ target: 3000 })
.then(() => {
throw new Error('Must never resolve')
})
Expand Down

0 comments on commit b191e1e

Please sign in to comment.