# @szmarczak/http-timer

> Timings for HTTP requests

Latest version **5.0.1** (published 2021-08-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install @szmarczak/http-timer
pnpm add @szmarczak/http-timer
yarn add @szmarczak/http-timer
bun add @szmarczak/http-timer
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.1 |
| Published | 2021-08-20 |
| First published | 2018-08-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=14.16 |
| Dependencies | 1 |
| Unpacked size | 10 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 191 |
| Author | Szymon Marczak |
| Maintainers | szmarczak |
| Keywords | http, https, http2, timer, timings, performance, measure |

## Links

- npm: https://www.npmjs.com/package/@szmarczak/http-timer
- Repository: https://github.com/szmarczak/http-timer
- Homepage: https://github.com/szmarczak/http-timer#readme
- Issues: https://github.com/szmarczak/http-timer/issues
- npm.io page: https://npm.io/package/@szmarczak/http-timer

## Dependencies (1)

- [defer-to-connect](https://npm.io/package/defer-to-connect.md) ^2.0.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 5.0.1 (latest) — 2021-08-20
- 5.0.0 — 2021-08-20
- 4.0.6 — 2021-07-12
- 4.0.5 — 2020-02-03
- 4.0.4 — 2020-02-03
- 4.0.3 — 2020-02-03
- 4.0.2 — 2020-02-03
- 4.0.1 — 2020-02-03
- 4.0.0 — 2019-12-27
- 3.1.1 — 2019-12-04
- 3.1.0 — 2019-11-15
- 3.0.0 — 2019-11-02
- 2.1.0 — 2019-11-01
- 2.0.0 — 2019-04-20
- 1.1.2 — 2019-01-04
- … 5 more at https://npm.io/package/@szmarczak/http-timer/versions

## README

# http-timer
> Timings for HTTP requests

[![Build Status](https://travis-ci.org/szmarczak/http-timer.svg?branch=master)](https://travis-ci.org/szmarczak/http-timer)
[![Coverage Status](https://coveralls.io/repos/github/szmarczak/http-timer/badge.svg?branch=master)](https://coveralls.io/github/szmarczak/http-timer?branch=master)
[![install size](https://packagephobia.now.sh/badge?p=@szmarczak/http-timer)](https://packagephobia.now.sh/result?p=@szmarczak/http-timer)

Inspired by the [`request` package](https://github.com/request/request).

## Installation

NPM:

> `npm install @szmarczak/http-timer`

Yarn:

> `yarn add @szmarczak/http-timer`

## Usage
**Note:**
> - The measured events resemble Node.js events, not the kernel ones.
> - Sending a chunk greater than [`highWaterMark`](https://nodejs.org/api/stream.html#stream_new_stream_writable_options) will result in invalid `upload` and `response` timings. You can avoid this by splitting the payload into smaller chunks.

```js
import https from 'https';
import timer from '@szmarczak/http-timer';

const request = https.get('https://httpbin.org/anything');
timer(request);

request.once('response', response => {
	response.resume();
	response.once('end', () => {
		console.log(response.timings); // You can use `request.timings` as well
	});
});

// {
//   start: 1572712180361,
//   socket: 1572712180362,
//   lookup: 1572712180415,
//   connect: 1572712180571,
//   upload: 1572712180884,
//   response: 1572712181037,
//   end: 1572712181039,
//   error: undefined,
//   abort: undefined,
//   phases: {
//     wait: 1,
//     dns: 53,
//     tcp: 156,
//     request: 313,
//     firstByte: 153,
//     download: 2,
//     total: 678
//   }
// }
```

## API

### timer(request)

Returns: `Object`

**Note**: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.

- `start` - Time when the request started.
- `socket` - Time when a socket was assigned to the request.
- `lookup` - Time when the DNS lookup finished.
- `connect` - Time when the socket successfully connected.
- `secureConnect` - Time when the socket securely connected.
- `upload` - Time when the request finished uploading.
- `response` - Time when the request fired `response` event.
- `end` - Time when the response fired `end` event.
- `error` - Time when the request fired `error` event.
- `abort` - Time when the request fired `abort` event.
- `phases`
	- `wait` - `timings.socket - timings.start`
	- `dns` - `timings.lookup - timings.socket`
	- `tcp` - `timings.connect - timings.lookup`
	- `tls` - `timings.secureConnect - timings.connect`
	- `request` - `timings.upload - (timings.secureConnect || timings.connect)`
	- `firstByte` - `timings.response - timings.upload`
	- `download` - `timings.end - timings.response`
	- `total` - `(timings.end || timings.error || timings.abort) - timings.start`

If something has not been measured yet, it will be `undefined`.

## License

MIT

---
_Source: https://npm.io/package/@szmarczak/http-timer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
