npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@butility/network

v1.0.3

Published

This library provides a set of utilities and helper functions for managing network-related tasks, such as IP address validation and manipulation, making HTTP requests, handling service workers, and working with URLs. It is organized into five main namespa

Downloads

2

Readme

Butility Network Utils

This library provides a set of utilities and helper functions for managing network-related tasks, such as IP address validation and manipulation, making HTTP requests, handling service workers, and working with URLs. It is organized into five main namespaces: Network, IP, Request, SW, and URL.

Overview

Network

The Network namespace imports and centralizes various utility methods, such as IP validation, HTTP requests, service worker management, and URL handling.

Key Methods:

  • IP: isPrivateIP, isValidIPv4, convertIPv4ToIPv6, etc.
  • Request: get, post, ajax, etc.
  • SW - Service Workers: handleBackgroundSync, registerServiceWorker, etc.
  • URL: constructURL, mergeURL, etc.

Installation

To install the package, you can use npm or CDN:

npm install @butility/network
<!-- To use all the functions and methods -->
<script src="https://unpkg.com/@butility/network@latest/network.js" type="module"></script>
<script src="https://cdn.jsdelivr.net/npm/@butility/network@latest/network.js"></script>
<!-- To use IP utils -->
<script src="https://unpkg.com/@butility/network@latest/ip.js" type="module"></script>
<script src="https://cdn.jsdelivr.net/npm/@butility/network@latest/ip.js"></script>
<!-- To use Ajax utils -->
<script src="https://unpkg.com/@butility/network@latest/request.js" type="module"></script>
<script src="https://cdn.jsdelivr.net/npm/@butility/network@latest/request.js"></script>
<!-- To use Service Worker -->
<script src="https://unpkg.com/@butility/network@latest/sw.js" type="module"></script>
<script src="https://cdn.jsdelivr.net/npm/@butility/network@latest/sw.js"></script>
<!-- To use URL utils -->
<script src="https://unpkg.com/@butility/network@latest/url.js" type="module"></script>
<script src="https://cdn.jsdelivr.net/npm/@butility/network@latest/url.js"></script>

Example

Working with IP

import { getUserIPAddress, getLocationByIP } from "@butility/network/ip"; 
// First, get the user's IP address
getUserIPAddress((ip) => {
    if (ip) {
        console.log('User IP Address:', ip);

        // Now use the IP to get the geolocation
        getLocationByIP(ip, (location) => {
            if (location) {
                console.log(`User is located at Latitude: ${location.latitude}, Longitude: ${location.longitude}`);
            } else {
                console.error('Failed to retrieve geolocation.');
            }
        });
    } else {
        console.error('Failed to retrieve IP address.');
    }
});

Working with HTTP request

import { get } from "@butility/network/request";
// Define the URL and data
const apiUrl = 'https://api.example.com/data';
const requestData = {
    query: 'example',
    limit: 10,
};

// Define options for the AJAX request (All optional)
const ajaxOptions = {
    method: 'GET',               // The HTTP method (already default for 'get')
    headers: {                   // Custom headers
        'Authorization': 'Bearer some-token',
        'Content-Type': 'application/json',
    },
    responseType: 'json',         // Expect a JSON response
    timeout: 5000,                // Set a timeout of 5 seconds
    retries: 3,                   // Retry the request 3 times in case of failure
    retryDelay: 1000,             // Wait 1 second before each retry
    useFetch: true,               // Use the Fetch API instead of XMLHttpRequest
    onProgress: (loaded, total) => {  // Progress callback (if supported)
        console.log(`Loaded ${loaded} of ${total} bytes`);
    },
    abortSignal: new AbortController().signal,  // Ability to abort request in Fetch API
    success: (response) => {  // Success callback
        console.log('Request successful:', response);
    },
    error: (error) => {  // Error callback
        console.error('Request failed:', error);
    },
};

// Make the GET request using the defined URL, data, and options
get(apiUrl, requestData, ajaxOptions);

Documentation

For @butility/network documentation refer to the docs folder.

Changelog

You can find changelog here

License

This project is licensed under the MIT License.