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

tixfactory.http

v1.0.9027

Published

A promised based http module.

Downloads

80

Readme

TixFactory.Http

Table of Contents

Introduction

This is just an http module I made for personal use that I decided to make public, don't expect professional grade http requests. This http module is mostly a dummy. It does not yet respect any headers, will not follow redirects, and will not allow setting the follow headers:

  • Content-Type
  • ^ Will include when requestBody is present - is set to requestBody.length
  • Host
  • ^ Will always be included as the host But if it helps anyone that's great! Expect bad documentation.

Typical installation, and module usage. To install:

$ npm install tixfactory.http

To use:

var http = require("tixfactory.http");
var httpClient = new http.client(); // Everything happens from an individual client

Will auto-handle cookies, and accept-encoding.

  • ^ Documentation required

Methods

var http = require("tixfactory.http");

var httpClient = new http.client();
httpClient.request(object requestData); // Returns promise: .then(function(response){ ... }).catch(function(errors){ ... });
httpClient.get(string url[, object queryParameters]); // Returns http.request preset with url, and queryParameters with method: GET
httpClient.post(string url, buffer requestBody); // Returns http.request with preset url, and requestBody with method: POST
httpClient.socketConfiguration([object configurationOverride]);
httpClient.configuration([object configurationOverride]);

var httpServer = new http.server(port);

Client request object

{
	"url": "https://www.google.com", // Required
	"method": "GET", // Required
	"queryParameters": {"a": "b"} // Optional
	"port": 80, // Optional, will also pull from url or default based on protocol
	"isSecure": true, // Optional, will default based on protocol (whether or not to use tls socket)
	"requestHeaders": [{name: "Hello", value: "world"}, ...], // Optional, also accepts object: {"Hello": "world"}
	"requestBody": Buffer // Optional
	"cookieJar": cookieJarObject // Optional - See: https://www.npmjs.com/package/cookiejar
}

Client response object

{
	"statusCode": 400,
	"statusText": "Bad Request",
	"headers": [{name: "Set-Cookie", value: "wat=who"}, ...],
	"body": Buffer,
	"contentType": "application/json" // Semi-formatted Content-Type response header (when provided by server)
	"responseJson": {"x": "y"}, // Will be available if the contentType is Json, and it can be parsed from JSON.
	"responseText": "{\"x\":\"y\"}" // Will be available for some known UTF-8 types.
}

Making a request

var http = require("tixfactory.http");
var httpClient = new http.client();
httpClient.request({
	"method": "GET"
	"url": "https://www.roblox.com/profile?userId=48103520"
}).then(function(response){
	console.log(response.statusCode, response.statusText);
	console.log(response.responseJson);
}).catch(function(errors){
	console.error(errors);
});

The same request can also be made with:

var http = require("tixfactory.http");
var httpClient = new http.client();
httpClient.get("https://www.roblox.com/profile", { userId: 48103520 }).then(function(response){
	console.log(response.statusCode, response.statusText);
	console.log(response.responseJson);
}).catch(function(errors){
	console.error(errors);
});

Client http configuration

var http = require("tixfactory.http");
var httpClient = new http.client();
var currentConfiguration = httpClient.configuration();
console.log(currentConfiguration);

Configuration object:

{
	"cookieJar": cookieJarNpmObject,
	"userAgent": "TixFactory.Http (node.js)" // This will be added automatically to any requests that do not have a User-Agent request header. Can be overriden by just setting it.
}

Mostly works like socketConfiguration, may or may not right documentation for this method at some point.

Client socket configuration

This module allows for some configuration around sockets including throttling and socket limits. To get current configuration settings:

var http = require("tixfactory.http");
var httpClient = new http.client();
var currentConfiguration = httpClient.socketConfiguration();
console.log(currentConfiguration);

Current configuration will look along the lines of:

{
	"expiration": 30000, // How long a socket will remain open before closing from inactivity.
	"timeoutBetweenQueueProcessing": 50, // If a request is attempting to be made, but all the sockets are in use, and no more can be created: how long to wait before re-checking.
	"getWritesPerSecond": function(host, port), // This function should return how many writes per second the module is allowed to make per the arguments. Default returns 1000. MUST return number always.
	"getMaxSockets": function(host, port, isSecure) // This is how many sockets are allowed to be open per host (domain), port, and whether or not the socket type is tls. Defaults to 6. MUST return number always.
}

To set configuration:

var http = require("tixfactory.http");
var httpClient = new http.client();
httpClient.socketConfiguration({
	getWritesPerSecond: function(host, port){
		// example if host is Google, or port is 443 only one request per second is allowed.
		if (host === "www.google.com" || port === 443) {
			return 1;
		}
		// otherwise we can make 2 requests per second.
		return 2;
	}
})

You only have to specify the fields you would like to override. For best results: setting configuration should only be done when the process starts.

Server request object

TODO: Document better

{
	"ip": "127.0.0.1", // The remote IP address
	"method": "GET", // The request method
	"headers": [{name: "Hello", value: "world"}, ...], // The request headers
	"headerMap": {"hello": "world"}, // The request headers in object format (names are all lowercase)
	url: {"pathname": "/"}, // [node URL object](https://nodejs.org/api/url.html)
	"queryParameters": [{name: "A", value: "b"}, ...], // The query parameters
	"queryParameterMap": {"a": "b"}, // The query parameters in object format(names are all lowercase)
	"respond": function (response) { ... }, // Returns promise
	"body": Buffer, // Will not be included if there is no response body
}

Server response object

TODO: Document better

{
	"statusCode": 200, // The status response code (defaults to 200)
	"statusText": "OK", // The status description/text (defaults to [http.STATUS_CODES](https://nodejs.org/api/http.html#http_http_status_codes))
	"body": Buffer, // The response buffer
	"headers": [{name: "Hello", value: "world"}, ...] // The response headers (can be array or object)
}

Server example

TODO: documentation wya?

var http = require("tixfactory.http");
var httpServer = new http.server(80);
httpServer.on("request", function(request){
	request.respond({
		statusCode: 200,
		statusText: "OK",
		body: Body.from("Hello, world!"),
		headers: [{name: "Hello", value: "world"}]
	}).then(function(){
		console.log("Sent response!");
	}).catch(function(errors){
		console.error(errors);
	});
});