Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

q-http

Package Overview
Dependencies
Maintainers
0
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

q-http - npm Package Compare versions

Comparing version 0.0.7 to 0.1.0

test.js

7

package.json
{
"name": "q-http",
"description": "Q promise based HTTP client and server interface",
"version": "0.0.7",
"version": "0.1.0",
"homepage": "http://github.com/kriskowal/q-http/",

@@ -20,3 +20,3 @@ "author": "Kris Kowal <kris@cixar.com> (http://github.com/kriskowal/)",

"q-util": ">=0.0.1",
"q-io": ">=0.0.1"
"q-io": ">=0.0.4"
},

@@ -26,3 +26,6 @@ "repository": {

"url": "http://github.com/kriskowal/q-http.git"
},
"engines": {
"node": ">=0.4.0"
}
}

@@ -10,2 +10,3 @@

var HTTP = require("http"); // node
var HTTPS = require("https"); // node
var URL = require("url"); // node

@@ -167,55 +168,34 @@ var Q = require("q-util");

/**
* Creates an HTTP client for issuing requests to
* the given host on the given port.
* @param {Number} port
* @param {String} host
* Issues an HTTP request.
*
* @param {Request {host, port, method, path, headers,
* body}} request (may be a promise)
* @returns {Promise * Response} promise for a response
*/
exports.Client = function (port, host) {
var self = Object.create(exports.Client.prototype);
exports.request = function (request) {
return Q.when(request, function (request) {
var _client = HTTP.createClient(port, host);
var error = Q.defer();
_client.on("error", function (_error) {
error.resolve(_error);
});
/***
* Issues an HTTP request. The request may be
* any object that has `method`, `path`, `headers`
* and `body` properties.
*
* * `method` `String` is optional, defaults to `"GET"`.
* * `path` `String` is optional, defaults to `"/"`.
* * `headers` `Object` is optional, defaults to `{}`.
* * `body` is optional, defaults to `[]`. Body must
* be an object with a `forEach` method that accepts a
* `write` callback. `forEach` may return a promise,
* and may send promises to `write`. `body` may be a
* promise.
*
* The Q HTTP `Server` responder receives a `Request`
* object that is suitable for `request`, and `request`
* returns a `Response` suitable for returning to the
* `Server`.
*
* @param {{method, path, headers, body}}
* @returns {Promise * Response}
*/
self.request = function (request) {
// host, port, method, path, headers, body
var deferred = Q.defer();
Q.when(error.promise, deferred.reject);
var _request = _client.request(
request.method || 'GET',
request.path || '/',
request.headers || {}
);
_request.on('response', function (_response) {
var response = exports.Response(_response);
deferred.resolve(response);
var ssl = request.ssl;
var http = ssl ? HTTPS : HTTP;
var _request = http.request({
"host": request.host,
"port": request.port || (ssl ? 443 : 80),
"path": request.path || "/",
"method": request.method || "GET",
"headers": request.headers || {}
}, function (_response) {
deferred.resolve(exports.Response(_response));
_response.on("error", function (error) {
// XXX find a better way to channel
// this into the response
console.warn(error && error.stack || error);
deferred.reject(error);
});
});
_request.on("error", function (error) {
deferred.reject(error);
});
Q.when(request.body, function (body) {

@@ -232,19 +212,4 @@ var end;

});
return deferred.promise;
};
return self;
};
/**
* Issues an HTTP request.
*
* @param {Request {host, port, method, path, headers,
* body}} request (may be a promise)
* @returns {Promise * Response} promise for a response
*/
exports.request = function (request) {
return Q.when(request, function (request) {
var client = exports.Client(request.port || 80, request.host);
return client.request(request);
});

@@ -265,5 +230,7 @@ };

url = URL.parse(url);
var ssl = url.protocol === "https:";
return Q.when(exports.request({
"host": url.hostname,
"port": url.port,
"ssl": ssl,
"method": "GET",

@@ -270,0 +237,0 @@ "path": (url.pathname || "") + (url.search || ""),

Sorry, the diff of this file is not supported yet

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