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

tcp-http-client

v1.0.9

Published

Basic Http client using built in net and tls modules

Downloads

19

Readme

TCP-Http-Client

This is a basic http client written in node.js. It uses the built in tls and net modules to send http requests. The client has built in socket pooling, a built in cookie container that can be turned on or off, and also supports using a proxy.

Install

npm install tcp-http-client

Example Usage

This example will send an HTTP POST request to www.example.com. It creates the TCPHttpClient, and then creates a request to be sent by the client. It adds headers and content to the request, then sends the request / receives and then logs the response.

const {TCPHttpClient, TCPHttpRequest, TCPHttpMethod} = require('tcp-http-client');
const { URL } = require('url');

const client = new TCPHttpClient();

const request = new TCPHttpRequest(new URL("https://www.example.com"), TCPHttpMethod.POST);
request.addHeader("name", "value");
request.addContent("Example Content", "text/plain");

const response = await client.sendRequest(request); //returns a promise
console.log(response.content.toString()); //response.content will be a byte[] by default. It does not automatically decode encoded data.
client.dispose(); //Make sure to dispose of the client when finished using.

API

Class: TCPHttpClient

This class is the main class responsible for sending and receiving requests over sockets using net and tls modules. It does the parsing of http responses, and also connects to the proxy.

new TCPHttpClient([options])

  • options <Object> Available options are:
    • proxy <Object> Needs two fields <host, port> where host is a string, and port is an integer. If specified will create a proxy connection through the client.
    • CookieContainer <Boolean> If specified, turns the Cookie Container on for true or off for false. Default setting is true.

TCPHttpClient.sendRequest(TCPHttpRequest)

  • Sends an HTTP request to the server given in the TCPHttpRequest object.
  • Returns a promise that is resolves to a TCPHttpResponseMessage when the HTTP response is received from the server.

TCPHttpClient.Connect(URL)

  • Creates a connection to the specified URL and adds that connection to the socket pool without sending an HTTP message.

TCPHttpClient.dispose()

  • Closes all active listeners and connections within the socket pool.

Class: TCPHttpRequest

This class is used to create a request to be used with the TCPHttpClient.

new TCPHttpRequest(URL, TCPHttpMethod)

  • returns an empty TCPHttpRequest with no headers or content.
    • URL <URL> The URL of a host you want to send a request to.
    • TCPHttpMethod <TCPHttpMethod> The type of request ex: TCPHttpMethod.POST or TCPHttpMethod.GET

TCPHttpRequest.addHeader(Name, Value)

  • adds a header to the TCPHttpRequest with header name: Name and value: Value.
    • Name <String>
    • Value <String>

TCPHttpRequest.addContent(Content, ContentType)

  • adds content to the TCPHttpRequest along with the content type.
    • Content <String>
    • ContentType <String> ex: "application/json", "text/plain"

TCPHttpRequest.addCookie(CookieName, CookieValue)

  • adds a cookie to the TCPHttpRequest.
    • CookieName <String>
    • CookieValue <String>

TCPHttpRequest.toString()

  • returns a string representation of the raw HTTP request that is going to be sent.

Class: TCPHttpResponseMessage

This class represents the raw HTTP message that is sent back from the server when using TCPHttpClient.sendRequest()

TCPHttpResponseMessage.Content

  • Returns Buffer[] // this will not be decoded by default if the response is gzip / deflate / br etc.

TCPHttpResponseMessage.StatusPhrase

  • Returns a string of the HTTP status phrase ex: OK, Forbidden, Found

TCPHttpResponseMessage.StatusCode

  • Returns an Integer of the HTTP status code ex: 200, 403, 302

TCPHttpResponseMessage.StatusVersion

  • Returns a string of the HTTP version. Should always be HTTP/1.1

TCPHttpResponseMessage.toString()

  • Returns a string representation of the raw HTTP response.