@har-tools/stream
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

@har-tools/stream

Create a stream of HAR entries from the Fetch API Request/Response instances.

Installation

npm install @har-tools/stream

Usage

import { HarStream, NetworkEntry } from '@har-tools/stream'

// Create an HAR stream.
const stream = new HarStream()

// Create a new network entry.
const entry = new NetworkEntry(new Request('https://example.com'))

// Write the network entry early.
// As soon as you ".end()" the entry with a Response,
// the stream will emit the complete HAR entry.
stream.write(entry)

// Call the ".end()" method once you receive the response.
entry.end(new Response('Hello world'))

Listening to HAR entries:

stream.on('data', (entry) => {
  // Observe generated HAR entries as they come in.
  console.log(entry.request, entry.response)
})

Stop the stream:

stream.destroy()

Timings

You can control the network entry timings using the entry.timings API. It provides you with the methods you have to call sequentially to indicate various staged of the request.

The order of the timing metrics:

  1. socketOpened, socket opened but not connected yet.
  2. dnsLookupEnd, socket has resolved the DNS name.
  3. connected, socket has connected.
  4. connectedSecure (Optional)
  5. requestEnd, request is finished (request headers and body sent).
  6. responseStart, first byte of the response received.
  7. responseEnd, last byte of the response received.

Entry timings are dependent, so if the previous timing is missing, the next one will not be calculated. For example, if entry.timings.dnsLookupEnd() was never called, the network entry will have both dns and connected timings as -1.

Package Sidebar

Install

npm i @har-tools/stream

Weekly Downloads

16

Version

0.1.2

License

MIT

Unpacked Size

48.6 kB

Total Files

53

Last publish

Collaborators

  • kettanaito