Socket
Socket
Sign inDemoInstall

tixfactory.http

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tixfactory.http

A promised based http module.


Version published
Weekly downloads
7
increased by40%
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 30 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc