make streaming http requests

Overview

hyperquest

treat http requests as a streaming transport

build status

The hyperquest api is a subset of request.

This module works in the browser with browserify.

rant

animated gif rant

This module disables a lot of infuriating things about core http that WILL cause bugs in your application if you think of http as just another kind of stream:

  • http requests have a default idle timeout of 2 minutes. This is terrible if you just want to pipe together a bunch of persistent backend processes over http.

  • There is a default connection pool of 5 requests. If you have 5 or more extant http requests, any additional requests will HANG for NO GOOD REASON.

hyperquest turns these annoyances off so you can just pretend that core http is just a fancier version of tcp and not the horrible monstrosity that it actually is.

I have it on good authority that these annoyances will be fixed in node 0.12.

example

simple streaming GET

var hyperquest = require('hyperquest');
hyperquest('http://localhost:8000').pipe(process.stdout);
$ node example/req.js
beep boop

pooling is evil

Now to drive the point home about pooling being evil and almost always never what you want ever.

request has its own forever agent thing that works pretty much the same as node core http.request: the wrong, horrible, broken way.

For instance, the following request code takes 12+ seconds to finish:

var http = require('http');
var request = require('request');

var server = http.createServer(function (req, res) {
    res.write(req.url.slice(1) + '\n');
    setTimeout(res.end.bind(res), 3000);
});

server.listen(5000, function () {
    var pending = 20;
    for (var i = 0; i < 20; i++) {
        var r = request('http://localhost:5000/' + i);
        r.pipe(process.stdout, { end: false });
        r.on('end', function () {
            if (--pending === 0) server.close();
        });
    }
});

process.stdout.setMaxListeners(0); // turn off annoying warnings
substack : example $ time node many_request.js 
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

real    0m12.423s
user    0m0.424s
sys 0m0.048s

Surprising? YES. This is pretty much never what you want, particularly if you have a lot of streaming http API endpoints. Your code will just HANG once the connection pool fills up and it won't start working again until some connections die for whatever reason. I have encountered this so many times in production instances and it is SO hard to track down reliably.

Compare to using hyperquest, which is exactly the same code but it takes 3 seconds instead of 12 to finish because it's not completely self-crippled like request and core http.request.

var http = require('http');
var hyperquest = require('hyperquest');

var server = http.createServer(function (req, res) {
    res.write(req.url.slice(1) + '\n');
    setTimeout(res.end.bind(res), 3000);
});

server.listen(5000, function () {
    var pending = 20;
    for (var i = 0; i < 20; i++) {
        var r = hyperquest('http://localhost:5000/' + i);
        r.pipe(process.stdout, { end: false });
        r.on('end', function () {
            if (--pending === 0) server.close();
        });
    }
});

process.stdout.setMaxListeners(0); // turn off annoying warnings
$ time node many_hyperquest.js 
0
1
2
3
4
5
6
8
9
7
10
11
12
13
14
15
16
17
18
19

real    0m3.284s
user    0m0.288s
sys 0m0.060s

So the other thing is, the justification I've heard supporting this horrible limit-of-5 pooling behavior is "performance". The first example which has been tuned for "performance" takes 12 seconds. The second example that removes these "performance" enhancements takes 3. Some performance improvement INDEED!

methods

var hyperquest = require('hyperquest');

var req = hyperquest(uri, opts={}, cb)

Create an outgoing http request to uri or opts.uri. You need not pass any arguments here since there are setter methods documented below.

Return a readable or duplex stream depending on the opts.method.

Default option values:

  • opts.method - "GET"
  • opts.headers - {}
  • opts.auth - undefined, but is set automatically when the uri has an auth string in it such as "http://user:passwd@host". opts.auth is of the form "user:pass", just like http.request().
  • opts.agent - false
  • opts.timeout - Math.pow(2, 32) * 1000 The value is passed as an argument to the underlying req.setTimeout() function.
  • opts.localAddress - local interface to bind for network connections (Node.js only)

In https mode, you can specify options to the underlying tls.connect() call:

  • opts.pfx
  • opts.key
  • opts.cert
  • opts.ca
  • opts.ciphers
  • opts.rejectUnauthorized
  • opts.secureProtocol

The request does not go through until the nextTick so you can set values outside of the opts so long as they are called on the same tick.

Optionally you can pass a cb(err, res) to set up listeners for 'error' and 'response' events in one place.

Note that the optional cb is NOT like request in that hyperquest will not buffer content for you or decode to json or any such magical thing.

req.setHeader(key, value);

Set an outgoing header key to value.

req.setLocation(uri);

Set the location if you didn't specify it in the hyperquest() call.

var req = hyperquest.get(uri, opts, cb)

Return a readable stream from hyperquest(..., { method: 'GET' }).

var req = hyperquest.put(uri, opts, cb)

Return a duplex stream from hyperquest(..., { method: 'PUT' }).

var req = hyperquest.post(uri, opts, cb)

Return a duplex stream from hyperquest(..., { method: 'POST' }).

var req = hyperquest.delete(uri, opts, cb)

Return a readable stream from hyperquest(..., { method: 'DELETE' }).

events

req.on('request', function (req) {})

The 'request' event is fired with the ClientRequest object created as a result of the underlying http.request() call.

req.on('response', function (res) {})

The 'response' event is forwarded from the underlying http.request().

req.on('error', function (res) {})

The 'error' event is forwarded from the underlying http.request().

install

With npm do:

npm install hyperquest

license

MIT

You might also like...

Simplifies node HTTP request making.

Requestify - Simplifies node HTTP request making. Requestify is a super easy to use and extendable HTTP client for nodeJS + it supports cache (-:. Ins

Nov 28, 2022

Run HTTP over UDP with Node.js

nodejs-httpp - Run HTTP over UDP based transport and Bring Web in Peer or P2P styles main js modules: udt.js, httpp.js, udts.js and httpps.js, that's

Aug 2, 2022

Global HTTP/HTTPS proxy agent configurable using environment variables.

global-agent Global HTTP/HTTPS proxy configurable using environment variables. Usage Setup proxy using global-agent/bootstrap Setup proxy using bootst

Dec 20, 2022

An HTTP Web Server for Chrome (chrome.sockets API)

An HTTP Web Server for Chrome (chrome.sockets API)

Dec 31, 2022

HTTP Client for Visual Studio Code to POST JSON, XML, image, ... files to REST APIs

HTTP Client for Visual Studio Code to POST JSON, XML, image, ... files to REST APIs

friflo POST Goal Main goal of this extension is storing all HTTP request & response data automatically as files in a VSCode workspace. This ensures th

Nov 18, 2021

Very very very powerful, extensible http client for both node.js and browser.

ES-Fetch-API ไธญๆ–‡ | English Very very very powerful, extensible http client for both node.js and browser. Why should you use ES-Fetch API? Still using a

Dec 12, 2022

A jQuery plugin for make your ajax request's error and success messages auto handled.

sweetAjax A jQuery plugin for make your ajax request's error and success messages auto handled. Installation sweetAjax plugin has built on jQuery-ui w

May 17, 2022

Webrtc, & web socket based streaming live video streaming and chatting platform. Written in Node, Typescript, and Javascript!

Live Streaming!! Welcome to my implementation of live streaming along with real time chat. I'm going to make a live streaming platform that will supoo

Nov 23, 2022

HLS, DASH, and future HTTP streaming protocols library for video.js

videojs-http-streaming (VHS) Play HLS, DASH, and future HTTP streaming protocols with video.js, even where they're not natively supported. Included in

Jan 5, 2023

SRS Player is a video streaming player, supports HLS/HTTP-FLV/WebRTC etc.

WordPress-Plugin-SrsPlayer SRS Player is a video streaming player, supports HLS/HTTP-FLV/WebRTC etc. Usage First, you should get your own video stream

Jul 27, 2022

SRS Player is a video streaming player, supports HLS/HTTP-FLV/WebRTC etc.

SRS Player is a video streaming player, supports HLS/HTTP-FLV/WebRTC etc.

Oct 15, 2022

Wrap native HTTP requests with RFC compliant cache support

cacheable-request Wrap native HTTP requests with RFC compliant cache support RFC 7234 compliant HTTP caching for native Node.js HTTP/HTTPS requests. C

Dec 20, 2022

This project was developed to practice Front-end and Back-end comunication, data formatting, http requests GET, POST, DELETE, form validation, it also consumes a rest API

This project was developed to practice Front-end and Back-end comunication, data formatting, http requests GET, POST, DELETE, form validation, it also consumes a rest API

React Application ๐Ÿ’ป Demonstration of the application | Features | Technologies used | Application installation ๐Ÿ’ป Demonstration of the application Ap

May 17, 2022

Library agnostic in-process recording of http(s) requests and responses

@gr2m/http-recorder Library agnostic in-process recording of http(s) requests and responses Install npm install @gr2m/http-recorder Usage import http

May 12, 2022

A set of APIs for handling HTTP and HTTPS requests with Deno ๐Ÿฟ๏ธ ๐Ÿฆ•

oak commons A set of APIs that are common to HTTP/HTTPS servers. HTTP Methods (/method.ts) A set of APIs for dealing with HTTP methods. Content Negoti

May 23, 2022

nodejs load balancing app to distribute http requests evenly across multiple servers.

nodejs load balancing app to distribute http requests evenly across multiple servers.

load-balancer-nodejs nodejs load balancing app to distribute http requests evenly across multiple servers. How to use ? Please edit the file 'config.j

Nov 7, 2022

Browser asynchronous http requests

It's AJAX All over again. Includes support for xmlHttpRequest, JSONP, CORS, and CommonJS Promises A. It is also isomorphic allowing you to require('re

Dec 20, 2022

Straightforward interactive HTTP requests from within your Alpine.JS markup

Alpine Fetch Straightforward interactive HTTP requests from within your Alpine.JS markup. View the live demo here What does this do? Alpine.JS is a ru

Dec 21, 2022
Owner
James Halliday
James Halliday
Library agnostic in-process recording of http(s) requests and responses

@gr2m/http-recorder Library agnostic in-process recording of http(s) requests and responses Install npm install @gr2m/http-recorder Usage import http

Gregor Martynus 4 May 12, 2022
Node.js web server framework for Http/1.1 or Http/2

Node.js web server framework for Http/1.1 or Http/2 Description: This is http framework, you can use it to create Http/1.1 or Http/2 serviceใ€‚ Now let'

Jeremy Yu 10 Mar 24, 2022
Promise based HTTP client for the browser and node.js

axios Promise based HTTP client for the browser and node.js New axios docs website: click here Table of Contents Features Browser Support Installing E

axios 98k Dec 31, 2022
๐ŸŠ๐Ÿพ Simplified HTTP request client.

Deprecated! As of Feb 11th 2020, request is fully deprecated. No new changes are expected to land. In fact, none have landed for some time. For more i

request 25.6k Jan 4, 2023
Ajax for Node.js and browsers (JS HTTP client)

superagent Small progressive client-side HTTP request library, and Node.js module with the same API, supporting many high-level HTTP client features T

Sloth 16.2k Jan 1, 2023
A full-featured http proxy for node.js

node-http-proxy node-http-proxy is an HTTP programmable proxying library that supports websockets. It is suitable for implementing components such as

http ... PARTY! 13.1k Jan 3, 2023
HTTP server mocking and expectations library for Node.js

Nock HTTP server mocking and expectations library for Node.js Nock can be used to test modules that perform HTTP requests in isolation. For instance,

Nock 11.9k Jan 3, 2023
๐ŸŒ Human-friendly and powerful HTTP request library for Node.js

Sindre's open source work is supported by the community. Special thanks to: Human-friendly and powerful HTTP request library for Node.js Moving from R

Sindre Sorhus 12.5k Jan 9, 2023
HTTP Client Utilities

@hapi/wreck HTTP client utilities. wreck is part of the hapi ecosystem and was designed to work seamlessly with the hapi web framework and its other c

hapi.js 383 Nov 1, 2022
Full-featured, middleware-oriented, programmatic HTTP and WebSocket proxy for node.js

rocky A multipurpose, full-featured, middleware-oriented and hackable HTTP/S and WebSocket proxy with powerful built-in features such as versatile rou

Tom 370 Nov 24, 2022