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

@shgysk8zer0/http

v1.0.5

Published

A JavaScript library that provides various utilities for working with HTTP

Downloads

121

Readme

@shgysk8zer0/http

A JavaScript library that provides various utilities for working with HTTP

CodeQL Node CI Lint Code Base

GitHub license GitHub last commit GitHub release GitHub Sponsors

npm node-current npm bundle size gzipped npm

GitHub followers GitHub forks GitHub stars Twitter Follow

Donate using Liberapay


Key Features

  • Exported constants for common HTTP status codes, such as ok for 200.
  • An extended HTTPError class that inherits from Error.
  • Useful polyfills, including an extended File object (derived from Blob) and URL.canParse() for URL validation.
  • A set of constants for commonly used Content-Types (from @shgysk8zer0/consts).
  • A versatile openLink() function compatible with various JavaScript environments.
  • A Cookie class for working with HTTP cookies, enabling easy cookie creation and management.

[!WARNING] Parsing of form data uses regex which has been reported as vulnerable to ReDoS attacks. parseMultipartFormData() is deprecated and will be removed in an upcoming release. Instead, in node > 18 you can use new Request(body, { headers }).formData().

Installation

NPM Installation

npm i @shgysk8zer0/http

NPM Imports

import { HTTPError } from 'shgysk8zer0/http@shgysk8zer0/http/error.js';
import { NOT_IMPLEMENTED, INTERNAL_SERVER_ERROR } from 'shgysk8zer0/http@shgysk8zer0/http/status.js';
import { JSON } from 'shgysk8zer0/http@shgysk8zer0/http/types.js';
import { Cookie } from 'shgysk8zer0/http@shgysk8zer0/http/cookie.js';

Alternative imports

This package is available on unpkg.com as a collection of modules, making it easily accessible for browser-based projects. It is designed to be versatile and is not limited to a specific Node.js environment, ensuring compatibility across various platforms.

import { HTTPError } from 'https://unpkg.com/@shgysk8zer0/http/error.js';
import { NOT_IMPLEMENTED, INTERNAL_SERVER_ERROR } from 'https://unpkg.com/@shgysk8zer0/http/status.js';
import { JSON } from 'https://unpkg.com/@shgysk8zer0/http/types.js';
import { Cookie } from 'https://unpkg.com/@shgysk8zer0/http/cookie.js';

Example Code

export async function handler() {
  try {
    const error = new HTTPError('Not implemented.', {
      status: NOT_IMPLEMENTED,
      cause: new Error('I have not done this yet...'),
    });
    
    throw err;
  } catch (err) {
    if (err instanceof HTTPError) { // Error has an HTTP status & message for use by client
      return Response.json(error, {
        status: error.status,
        headers: new Headers({
          'Content-Type': JSON,
          'Set-Cookie': new Cookie('uid', crypto.randomUUID(), {
            domain: 'example.com',
            path: '/foo',
            maxAge: 86_400_000,
            sameSite: 'Strict',
            httpOnly: true,
            partitioned: true,
          })
        }),
      });  
    } else { // It is not an HTTPError and may contain sensitive into
      return Response.json({
        error: {
          messsage: 'Something broke :(',
          status: INTERNAL_SERVER_ERROR,
        }
      }, { status: INTERNAL_SERVER_ERROR });
    }
  }  
}