Coder Social home page Coder Social logo

shgysk8zer0 / node-http Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 89 KB

A JavaScript library that provides various utilities for working with HTTP

Home Page: https://npmjs.com/package/@shgysk8zer0/http

License: MIT License

JavaScript 100.00%
form-data http-error http-status node

node-http's Introduction

@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 });
    }
  }  
}

node-http's People

Contributors

dependabot[bot] avatar shgysk8zer0 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

node-http's Issues

Fix code scanning alert - Polynomial regular expression used on uncontrolled data

Tracking issue for:

Some regular expressions take a long time to match certain input strings to the point where the time it takes to match a string of length n is proportional to nk or even 2n. Such regular expressions can negatively affect performance, or even allow a malicious user to perform a Denial of Service ("DoS") attack by crafting an expensive input string for the regular expression to match.

The regular expression engines provided by many popular JavaScript platforms use backtracking non-deterministic finite automata to implement regular expression matching. While this approach is space-efficient and allows supporting advanced features like capture groups, it is not time-efficient in general. The worst-case time complexity of such an automaton can be polynomial or even exponential, meaning that for strings of a certain shape, increasing the input length by ten characters may make the automaton about 1000 times slower.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.