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

http-packet

v0.2.16

Published

HTTP crossplatform packet parser/generator

Downloads

1

Readme

http-packet

Build Status
Coverage Status

HTTP crossplatform request generator. It allows generating and parsing HTTP 1.1 requests. May be used with net.Socket or any other transport. Working on browser and node.

How to use

Creating instance

First, we need create HttpPacket instance.

// Use import HttpPacket from '@shpingalet007/http-packet';
// or const HttpPacket = require('@shpingalet007/http-packet');

const httpPacket = new HttpPacket(/* params object */);

HttpPacket supports more options to be passed in constructor, but they would be described further. The only mandatory param is url.

What we need to know is that there are default params set:

version: '1.1' | HttpVersions.v1_1              // Currently stable on HTTP 1.1
method: 'get' | RequestMethods.Get              // HTTP Method GET
encoding: 'text/plain' | Encodings.TextPlain    // Text plain

Generating HTTP request

Example 1: Simple GET request

The library can produce HTTP requests as string or as Uint8Array binary.

const httpPacket = new HttpPacket({ url: 'http://example.net/' });
const httpRequest = httpPacket.generate('string');

This will produce GET request to http://example.net/ :

GET / HTTP/1.1\r\n
Host: example.net\r\n
\r\n

Example 2: GET request with query params

We can specify query params with constructor setting queryParams, or they might be decoded from url setting if there are any.

const httpPacket = new HttpPacket({
  url: 'http://example.net/',
  queryParams: { testParam: 'This is just a test' }
});
const httpRequest = httpPacket.generate('string');

All queryParams would be urlencoded automatically. And as a result, we get next request:

GET /?testParam=This%20is%20just%20a%20test HTTP/1.1\r\n
Host: example.net\r\n
\r\n

Example 3: POST request with data in body

In this example we would change the HTTP method and set some data in body

const httpPacket = new HttpPacket({
  method: 'POST',
  url: 'http://example.net/',
  body: {  
    encoding: /* 'application/json' || Encodings.Json, */
    content: { testParam: 'This is just a test' }
  }
});
const httpRequest = httpPacket.generate('string');

And we get next request:

POST / HTTP/1.1\r\n
Host: example.net\r\n
Content-Type: application/json\r\n
Content-Length: 35\r\n
\r\n
{"testParam":"This is just a test"}

Example 4: Complex request

Alright, here we would make a conclusion creating complex request with all settings available in HttpPacket library.

const httpPacket = new HttpPacket({  
  version: HttpVersions.v2_0,  
  authentication: {  
    type: AuthTypes.Basic,  
    credentials: { username: 'admin', password: '0000' },  
  },  
  method: RequestMethods.Post,  
  url: 'http://example.net/',  
  queryParams: {  
    q1: 'test',  
  },  
  headers: {  
    // 'X-Application': 'Browser',  
    xApplication: 'Browser', // This will result in 'X-Application' header
    _xApplication: 'Browser', // This will result in 'xApplication' header
  },  
  body: {  
    encoding: Encodings.FormUrlencoded,  
    content: {  
      a: '100',  
      b: '200',  
    },  
  },  
});

Resulting in next packet:

POST /?q1=test HTTP/2.0\r\n
Host: example.net\r\n
Content-Type: application/x-www-form-urlencoded\r\n
X-Application: Browser\r\n
xApplication: Test header\r\n
Authorization: Basic YWRtaW46MDAwMA==\r\n
Content-Length: 11\r\n
\r\n
a=100&b=200

HttpPacket settings

Settings are passed on instance construction. Full list is next:

version: typeof HttpVersions (default: HttpVersions.v1_1)
authentication: AuthBasic | AuthBearer (default: null)
method: typeof RequestMethods (default: RequestMethods.Get)
url: string, (mandatory param)
queryParams: object, (default: null)
headers: object, (default: null)
body: ApplicationJsonBody | FormDataBody | UrlencodedBody | TextPlainBody