tophat / networkjs

A utility that monitors a browser's network and emits connectivity events

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NetworkJS

Deprecation Notice

As part of general Open Source org housekeeping, and due to lack of interest in this project, we have decided to archive NetworkJS.


GitHub license All Contributors Maturity badge - level 1 Slack workspace

codecov

Overview

Network JS is a utility library that emits network connectivity events.

Installation

Min Node Version. 12.16.1

yarn add git@github.com:tophat/networkjs.git

or

npm install git@github.com:tophat/networkjs.git

Usage

import Network from 'network-js'

If you're not using ECMAScript modules:

const Network = require('network-js').default

Then initialize the library with your preferred configuration described below.

const Net = new Network({
    service: { /* service config */ },
    stability: { /* stability config */ }
})

This library monitors for three types of network events:

Network: Browser offline/online events

Events

  • offline: Fired when the browser loses network connection
  • online: Fired when the browser reconnects

Stability: Network unstable/stable events

Events

  • unstable: Fired when the network speed goes under a given threshold
  • stable: Fired when the network speed goes back above the given threshold

Options

  • maxBufferSize: The number of recent performance entries to track at any given time (optional, default 10)
  • speedThreshold: The speed threshold (in KBps) that determines network stability (optional, default 100)

Notes

Avoid using a large value for maxBufferSize (>100) as performance entries are stored in memory. The smaller the number you provide, the less accurate the monitor may be. The larger the number you provide, the longer it will take to propagate changes in network stability. You should play around with this value until you find a good balance.

This monitor uses the window.PerformanceObserver API which is not supported in Internet Explorer. As such, this monitor will be disabled in Internet Explorer.

Service: Service degraded/resolved events

Events

  • degraded: Fired when a service that matches a given prefix becomes degraded
  • resolved: Fired when a service degradation is resolved

Options

  • definitions: Array of service definitions to track (optional, defaults to tracking any failures)
    • name: Name of the service
    • regex: The RegExp to test against
  • statuses: Array of statuses that determine a service degradation (optional, default [502, 503, 504])
  • failureThreshold: Minimum number of consecutive failures that determine if a service is degraded (optional, default 2)
  • decrementTime: Amount of time until a failure is dismissed (optional, default 10000ms)

Notes

This monitor needs to be hooked into your current HTTP library. For each request, you feed it the request URL and the response status code, and it will emit events upon hitting the given failure threshold.

const Net = new Network({ service = { /* service config */ } })

this.axios.interceptors.response.use(
    success => success,
    (error) => {
        Net.serviceError(error.requestUrl, error.status)
        return error
    },
)

Examples

Network

const Net = new Network()

Net.on('online', () => {
    console.log('Network - ONLINE')
})

Net.on('offline', () => {
    console.log('Network - OFFLINE')
})

Stability

const Net = new Network({
    stability: {
        maxBufferSize: 15,
        speedThreshold: 150
    }
})

Net.on('unstable', () => {
    console.log('Network - UNSTABLE')
})

Net.on('stable', () => {
    console.log('Network - STABLE')
})

Service

const Net = new Network({
    services: {
        definitions: [
            {
                name: 'resource 1',
                regex: new RegExp('api/v1/resource1')
            },
            {
                name: 'resource 2',
                regex: new RegExp('api/v2/resource2')
            }
        ],
        statuses: [500, 502, 503, 504]
    }
})

Net.on('degraded', (serviceName) => {
    console.log(`Network - ${serviceName} - DEGRADED`)
})

Net.on('resolved', (serviceName) => {
    console.log(`Network - ${serviceName} - RESOLVED`)
})

All

const Net = new Network({
    stability: {
        maxBufferSize: 15,
        speedThreshold: 150
    },
    services: {
        prefixes: ['api/v1/resource1', 'api/v2/resource2'],
        statuses: [500, 502, 503, 504]
    }
})

Net.all((event) => {
    console.log(`Network - ${event}`)
})

Contributors ✨

See Contributing Guide to get started contributing.

Thanks goes to these wonderful people (emoji key):


Jack Cohen

🤔 💻

Marc Cataford

🚇

StealthGiraffe

💻

Anthony Sottile

🛡️

This project follows the all-contributors specification. Contributions of any kind welcome!

About

A utility that monitors a browser's network and emits connectivity events

License:Apache License 2.0


Languages

Language:JavaScript 99.8%Language:Shell 0.2%