request-ssl
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -20,3 +20,6 @@ /** | ||
urlib = require('url'), | ||
debug = require('debug')('request-ssl'); | ||
debug = require('debug')('request-ssl'), | ||
initialized, | ||
initializing, | ||
hooks; | ||
@@ -259,2 +262,59 @@ // internal variables | ||
// patch our initializer so we can run our hooks | ||
var patchedInit = request.Request.prototype.init; | ||
request.Request.prototype.init = function() { | ||
if (initialized || initializing || !hooks || hooks.length === 0) { | ||
return patchedInit.apply(this, arguments); | ||
} | ||
// run our hooks until we're done and then run our initializer | ||
var args = arguments, | ||
self = this, | ||
done = function() { | ||
initializing = false; | ||
initialized = true; | ||
hooks = null; | ||
// unpatch ourselves | ||
request.Request.prototype.init = patchedInit; | ||
return patchedInit.apply(self,args); | ||
}, | ||
index = 0, | ||
nextHook = function() { | ||
var hook = hooks[index++]; | ||
if (hook) { | ||
// if async | ||
if (hook.length > 0) { | ||
hook(nextHook); | ||
} | ||
// else sync | ||
else { | ||
hook(); | ||
nextHook(); | ||
} | ||
} | ||
else { | ||
done(); | ||
} | ||
}; | ||
initializing = true; | ||
nextHook(); | ||
}; | ||
/** | ||
* register an initializer callback. this callback will get called only | ||
* once before any requests are executed using this library. this will | ||
* allow clients to run initialization type routines (for example, to | ||
* register a fingerprint, etc.) before the request actually is invoked. | ||
* | ||
* the initializer will only work in the case where you are passing a | ||
* callback to the first use of the request library | ||
*/ | ||
Request.registerInitializer = function registerInitializer(callback) { | ||
if (initialized) { | ||
throw new Error("cannot register initializer callback. the request library has already been initialized"); | ||
} | ||
debug('registerInitializer %o',callback); | ||
hooks = hooks || []; | ||
hooks.push(callback); | ||
}; | ||
module.exports = Request; | ||
@@ -265,8 +325,9 @@ | ||
Request[k] = function() { | ||
var args = Array.prototype.slice.call(arguments); | ||
var args = Array.prototype.slice.call(arguments), | ||
method = _translateMethod(k); | ||
if (_.isObject(args[0])) { | ||
args[0].method = _translateMethod(k); | ||
args[0].method = method; | ||
} | ||
else { | ||
args[0] = {url:args[0], method: _translateMethod(k)}; | ||
args[0] = {url:args[0], method: method}; | ||
} | ||
@@ -273,0 +334,0 @@ return Request.apply(null, args); |
{ | ||
"name": "request-ssl", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Pinned SSL version of the Request library", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
32827
10
461