Comparing version 1.0.2 to 1.2.0
@@ -89,3 +89,3 @@ | ||
* `uri`: A convenient way to specify `options.uri` directly | ||
* `options`: Anything that is valid [configuration](#configuration) | ||
* `options`: Anything listed below under [options](#options) | ||
* `cb`: A callback function that receives the following arguments: | ||
@@ -175,2 +175,3 @@ - `error`: An instance of `Error` | ||
* `serviceVersion`: By convention the client version. Used in the user-agent | ||
* `pathParams`: If your `uri` includes `{tags}` they will be matched by this object. You can use this instead of string manipulation as this object is also logged | ||
@@ -177,0 +178,0 @@ In addition the following options are added that are useful for instrumentation but do not affect the actual HTTP request: |
414
lib/gofer.js
@@ -35,6 +35,5 @@ /* | ||
void function () { | ||
var applyBaseUrl, buildGofer, buildUserAgent, cache$, cache$1, Hub, isJsonResponse, merge, parseDefaults, registerEndpoint, resolveOptional, safeParseJSON; | ||
var addOptionMapper, applyBaseUrl, buildGofer, buildUserAgent, cache$, cache$1, clearOptionMappers, Gofer, Hub, isJsonResponse, merge, parseDefaults, registerEndpoints, replacePathParms, resolveOptional, safeParseJSON; | ||
Hub = require('./hub'); | ||
cache$ = require('./helpers'); | ||
registerEndpoint = cache$.registerEndpoint; | ||
resolveOptional = cache$.resolveOptional; | ||
@@ -44,2 +43,3 @@ parseDefaults = cache$.parseDefaults; | ||
buildUserAgent = cache$.buildUserAgent; | ||
replacePathParms = cache$.replacePathParms; | ||
merge = cache$.merge; | ||
@@ -49,188 +49,244 @@ cache$1 = require('./json'); | ||
isJsonResponse = cache$1.isJsonResponse; | ||
module.exports = buildGofer = function (serviceName, serviceVersion) { | ||
var Gofer; | ||
Gofer = function () { | ||
function Gofer(config, param$) { | ||
var cache$2, instance$; | ||
instance$ = this; | ||
this.request = function (a, b, c) { | ||
return Gofer.prototype.request.apply(instance$, arguments); | ||
}; | ||
this.hub = param$; | ||
cache$2 = parseDefaults(config, serviceName); | ||
this.defaults = cache$2.defaults; | ||
this.endpointDefaults = cache$2.endpointDefaults; | ||
if (null != this.hub) | ||
this.hub; | ||
else | ||
this.hub = Hub(); | ||
Gofer = function () { | ||
function Gofer(config, param$) { | ||
var cache$2, instance$; | ||
instance$ = this; | ||
this.request = function (a, b, c) { | ||
return Gofer.prototype.request.apply(instance$, arguments); | ||
}; | ||
this.hub = param$; | ||
cache$2 = parseDefaults(config, this.serviceName); | ||
this.defaults = cache$2.defaults; | ||
this.endpointDefaults = cache$2.endpointDefaults; | ||
if (null != this.hub) | ||
this.hub; | ||
else | ||
this.hub = Hub(); | ||
} | ||
Gofer.prototype['with'] = function (overrides) { | ||
var copy, endpointDefaults, endpointName; | ||
copy = new this.constructor({}, this.hub); | ||
copy.defaults = merge(this.defaults, overrides); | ||
copy.endpointDefaults = {}; | ||
for (endpointName in this.endpointDefaults) { | ||
endpointDefaults = this.endpointDefaults[endpointName]; | ||
copy.endpointDefaults[endpointName] = merge(endpointDefaults, overrides); | ||
} | ||
Gofer.prototype['with'] = function (overrides) { | ||
var copy, endpointDefaults, endpointName; | ||
copy = new Gofer({}, this.hub); | ||
copy.defaults = merge(this.defaults, overrides); | ||
copy.endpointDefaults = {}; | ||
for (endpointName in this.endpointDefaults) { | ||
endpointDefaults = this.endpointDefaults[endpointName]; | ||
copy.endpointDefaults[endpointName] = merge(endpointDefaults, overrides); | ||
return copy; | ||
}; | ||
Gofer.prototype.clone = function () { | ||
return this['with']({}); | ||
}; | ||
Gofer.prototype.request = function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
return this._request(options, cb); | ||
}; | ||
Gofer.prototype.put = function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
options.method = 'PUT'; | ||
return this._request(options, cb); | ||
}; | ||
Gofer.prototype.del = function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
options.method = 'DELETE'; | ||
return this._request(options, cb); | ||
}; | ||
Gofer.prototype.head = function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
options.method = 'HEAD'; | ||
return this._request(options, cb); | ||
}; | ||
Gofer.prototype.post = function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
options.method = 'POST'; | ||
return this._request(options, cb); | ||
}; | ||
Gofer.prototype.patch = function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
options.method = 'PATCH'; | ||
return this._request(options, cb); | ||
}; | ||
Gofer.prototype.registerEndpoint = function (endpointName, endpointFn) { | ||
Object.defineProperty(this, endpointName, { | ||
configurable: true, | ||
get: function () { | ||
var request, value; | ||
request = this.requestWithDefaults({ endpointName: endpointName }); | ||
value = endpointFn(request); | ||
Object.defineProperty(this, endpointName, { value: value }); | ||
return value; | ||
} | ||
return copy; | ||
}; | ||
Gofer.prototype.clone = function () { | ||
return this['with']({}); | ||
}; | ||
Gofer.prototype.request = function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
return this._request(options, cb); | ||
}; | ||
Gofer.prototype.put = function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
options.method = 'PUT'; | ||
return this._request(options, cb); | ||
}; | ||
Gofer.prototype.del = function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
options.method = 'DELETE'; | ||
return this._request(options, cb); | ||
}; | ||
Gofer.prototype.head = function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
options.method = 'HEAD'; | ||
return this._request(options, cb); | ||
}; | ||
Gofer.prototype.post = function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
options.method = 'POST'; | ||
return this._request(options, cb); | ||
}; | ||
Gofer.prototype.patch = function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
options.method = 'PATCH'; | ||
return this._request(options, cb); | ||
}; | ||
Gofer.prototype.addOptionMapper = function (mapper) { | ||
this._mappers = this._mappers.concat([mapper]); | ||
return this; | ||
}; | ||
Gofer.prototype.clearOptionMappers = function () { | ||
return this._mappers = []; | ||
}; | ||
Gofer.prototype.requestWithDefaults = function (defaults) { | ||
return function (this$) { | ||
return function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
options = merge(defaults, options); | ||
return this$._request(options, cb); | ||
}; | ||
}(this); | ||
}; | ||
Gofer.prototype._getDefaults = function (defaults, options) { | ||
var endpointName; | ||
endpointName = options.endpointName; | ||
if (null != endpointName && null != this.endpointDefaults[endpointName]) | ||
defaults = merge(defaults, this.endpointDefaults[endpointName]); | ||
return defaults; | ||
}; | ||
Gofer.prototype.applyBaseUrl = applyBaseUrl; | ||
Gofer.prototype._applyMappers = function (originalOptions) { | ||
return this._mappers.reduce(function (this$) { | ||
return function (options, mapper) { | ||
return mapper.call(this$, options); | ||
}; | ||
}(this), originalOptions); | ||
}; | ||
Gofer.prototype._request = function (options, cb) { | ||
var cleanedQs, defaults, err, key, value; | ||
defaults = this._getDefaults(this.defaults, options); | ||
if (null != options.methodName) | ||
options.methodName; | ||
else | ||
options.methodName = (null != options.method ? options.method : 'get').toLowerCase(); | ||
options.serviceName = serviceName; | ||
options.serviceVersion = serviceVersion; | ||
try { | ||
options = this._applyMappers(merge(defaults, options)); | ||
} catch (e$) { | ||
err = e$; | ||
return cb(err); | ||
} | ||
if (null != options.headers) | ||
options.headers; | ||
else | ||
options.headers = {}; | ||
if (null != options.headers['User-Agent']) | ||
options.headers['User-Agent']; | ||
else | ||
options.headers['User-Agent'] = buildUserAgent(options); | ||
if (null != options.qs && typeof options.qs === 'object') { | ||
cleanedQs = {}; | ||
for (key in options.qs) { | ||
value = options.qs[key]; | ||
if ('undefined' !== typeof value && null != value) | ||
cleanedQs[key] = value; | ||
} | ||
options.qs = cleanedQs; | ||
} | ||
return this.hub.fetch(options, function (err, body, response, responseData) { | ||
var data, parseJSON; | ||
parseJSON = null != options.parseJSON ? options.parseJSON : isJsonResponse(response, body); | ||
if (!parseJSON) | ||
return cb(err, body, responseData, response); | ||
data = safeParseJSON(body); | ||
return cb(null != err ? err : data.error, data.result, responseData, response); | ||
}); | ||
}; | ||
return Gofer; | ||
}(); | ||
Gofer.prototype.fetch = Gofer.prototype.request; | ||
Gofer.prototype.get = Gofer.prototype.request; | ||
Gofer.prototype._mappers = [function (opts) { | ||
var baseUrl; | ||
baseUrl = opts.baseUrl; | ||
if (null != baseUrl) { | ||
delete opts.baseUrl; | ||
return this.applyBaseUrl(baseUrl, opts); | ||
} else { | ||
return opts; | ||
} | ||
}]; | ||
Gofer.addOptionMapper = function (mapper) { | ||
return Gofer.prototype.addOptionMapper(mapper); | ||
}); | ||
return this; | ||
}; | ||
Gofer.clearOptionMappers = function () { | ||
return Gofer.prototype.clearOptionMappers(); | ||
}; | ||
Gofer.registerEndpoints = function (endpointMap) { | ||
var handler, name, proto; | ||
proto = Gofer.prototype; | ||
Gofer.prototype.registerEndpoints = function (endpointMap) { | ||
var handler, name; | ||
for (name in endpointMap) { | ||
handler = endpointMap[name]; | ||
registerEndpoint(proto, name, handler); | ||
this.registerEndpoint(name, handler); | ||
} | ||
return Gofer; | ||
return this; | ||
}; | ||
Gofer.serviceName = serviceName; | ||
Gofer.prototype.addOptionMapper = function (mapper) { | ||
this._mappers = this._mappers.concat([mapper]); | ||
return this; | ||
}; | ||
Gofer.prototype.clearOptionMappers = function () { | ||
return this._mappers = []; | ||
}; | ||
Gofer.prototype.requestWithDefaults = function (defaults) { | ||
return function (this$) { | ||
return function (uri, options, cb) { | ||
var cache$2; | ||
cache$2 = resolveOptional(uri, options, cb); | ||
options = cache$2.options; | ||
cb = cache$2.cb; | ||
options = merge(defaults, options); | ||
return this$._request(options, cb); | ||
}; | ||
}(this); | ||
}; | ||
Gofer.prototype._getDefaults = function (defaults, options) { | ||
var endpointName; | ||
endpointName = options.endpointName; | ||
if (null != endpointName && null != this.endpointDefaults[endpointName]) | ||
defaults = merge(defaults, this.endpointDefaults[endpointName]); | ||
return defaults; | ||
}; | ||
Gofer.prototype.applyBaseUrl = applyBaseUrl; | ||
Gofer.prototype._applyMappers = function (originalOptions) { | ||
return this._mappers.reduce(function (this$) { | ||
return function (options, mapper) { | ||
return mapper.call(this$, options); | ||
}; | ||
}(this), originalOptions); | ||
}; | ||
Gofer.prototype._request = function (options, cb) { | ||
var cleanedQs, defaults, err, key, value; | ||
defaults = this._getDefaults(this.defaults, options); | ||
options.uri = replacePathParms(options.uri, options.pathParams); | ||
if (null != options.methodName) | ||
options.methodName; | ||
else | ||
options.methodName = (null != options.method ? options.method : 'get').toLowerCase(); | ||
if (null != this.serviceName) | ||
options.serviceName = this.serviceName; | ||
if (null != this.serviceVersion) | ||
options.serviceVersion = this.serviceVersion; | ||
try { | ||
options = this._applyMappers(merge(defaults, options)); | ||
} catch (e$) { | ||
err = e$; | ||
return cb(err); | ||
} | ||
if (null != options.headers) | ||
options.headers; | ||
else | ||
options.headers = {}; | ||
if (null != options.headers['User-Agent']) | ||
options.headers['User-Agent']; | ||
else | ||
options.headers['User-Agent'] = buildUserAgent(options); | ||
if (null != options.qs && typeof options.qs === 'object') { | ||
cleanedQs = {}; | ||
for (key in options.qs) { | ||
value = options.qs[key]; | ||
if ('undefined' !== typeof value && null != value) | ||
cleanedQs[key] = value; | ||
} | ||
options.qs = cleanedQs; | ||
} | ||
return this.hub.fetch(options, function (err, body, response, responseData) { | ||
var data, parseJSON; | ||
parseJSON = null != options.parseJSON ? options.parseJSON : isJsonResponse(response, body); | ||
if (!parseJSON) | ||
return cb(err, body, responseData, response); | ||
data = safeParseJSON(body); | ||
return cb(null != err ? err : data.error, data.result, responseData, response); | ||
}); | ||
}; | ||
return Gofer; | ||
}(); | ||
Gofer.prototype.fetch = Gofer.prototype.request; | ||
Gofer.prototype.get = Gofer.prototype.request; | ||
Gofer.prototype._mappers = [function (opts) { | ||
var baseUrl; | ||
baseUrl = opts.baseUrl; | ||
if (null != baseUrl) { | ||
delete opts.baseUrl; | ||
return this.applyBaseUrl(baseUrl, opts); | ||
} else { | ||
return opts; | ||
} | ||
}]; | ||
addOptionMapper = function (GoferClass, mapper) { | ||
return GoferClass.prototype.addOptionMapper(mapper); | ||
}; | ||
clearOptionMappers = function (GoferClass) { | ||
return GoferClass.prototype.clearOptionMappers(); | ||
}; | ||
registerEndpoints = function (GoferClass, endpointMap) { | ||
return GoferClass.prototype.registerEndpoints(endpointMap); | ||
}; | ||
module.exports = buildGofer = function (serviceName, serviceVersion) { | ||
var CustomGofer; | ||
CustomGofer = function (super$) { | ||
extends$(CustomGofer, super$); | ||
function CustomGofer(config, hub) { | ||
Gofer.call(this, config, hub); | ||
} | ||
CustomGofer.prototype.serviceName = serviceName; | ||
CustomGofer.prototype.serviceVersion = serviceVersion; | ||
return CustomGofer; | ||
}(Gofer); | ||
CustomGofer.addOptionMapper = function (mapper) { | ||
return addOptionMapper(CustomGofer, mapper); | ||
}; | ||
CustomGofer.clearOptionMappers = function () { | ||
return clearOptionMappers(CustomGofer); | ||
}; | ||
CustomGofer.registerEndpoints = function (endpointMap) { | ||
return registerEndpoints(CustomGofer, endpointMap); | ||
}; | ||
CustomGofer.serviceName = serviceName; | ||
return CustomGofer; | ||
}; | ||
buildGofer.Gofer = Gofer; | ||
buildGofer.addOptionMapper = addOptionMapper; | ||
buildGofer.clearOptionMappers = clearOptionMappers; | ||
buildGofer.registerEndpoints = registerEndpoints; | ||
buildGofer['default'] = buildGofer; | ||
function isOwn$(o, p) { | ||
return {}.hasOwnProperty.call(o, p); | ||
} | ||
function extends$(child, parent) { | ||
for (var key in parent) | ||
if (isOwn$(parent, key)) | ||
child[key] = parent[key]; | ||
function ctor() { | ||
this.constructor = child; | ||
} | ||
ctor.prototype = parent.prototype; | ||
child.prototype = new ctor; | ||
child.__super__ = parent.prototype; | ||
return child; | ||
} | ||
}.call(this); |
@@ -35,6 +35,8 @@ /* | ||
void function () { | ||
var cache$, cloneDeep, merge, mergeSafe, Url; | ||
var cache$, cloneDeep, isObject, merge, mergeSafe, reduce, Url; | ||
cache$ = require('lodash'); | ||
merge = cache$.merge; | ||
isObject = cache$.isObject; | ||
cloneDeep = cache$.cloneDeep; | ||
reduce = cache$.reduce; | ||
Url = require('url'); | ||
@@ -44,14 +46,2 @@ this.merge = mergeSafe = function (o1, o2) { | ||
}; | ||
this.registerEndpoint = function (proto, endpointName, endpointFn) { | ||
return Object.defineProperty(proto, endpointName, { | ||
configurable: true, | ||
get: function () { | ||
var request, value; | ||
request = this.requestWithDefaults({ endpointName: endpointName }); | ||
value = endpointFn(request); | ||
Object.defineProperty(this, endpointName, { value: value }); | ||
return value; | ||
} | ||
}); | ||
}; | ||
this.resolveOptional = function (uri, options, cb) { | ||
@@ -131,2 +121,18 @@ if ('function' === typeof options) { | ||
}; | ||
this.replacePathParms = function (uri, pathParams) { | ||
var regex, wrappedPathParams, wrappedTags; | ||
if (!isObject(pathParams)) | ||
return uri; | ||
wrappedPathParams = reduce(pathParams, function (acc, value, tag) { | ||
var wrappedTag; | ||
wrappedTag = '{' + tag + '}'; | ||
acc[wrappedTag] = encodeURIComponent(value); | ||
return acc; | ||
}, {}); | ||
wrappedTags = Object.keys(wrappedPathParams); | ||
regex = new RegExp(wrappedTags.join('|'), 'g'); | ||
return uri.replace(regex, function (match) { | ||
return wrappedPathParams[match]; | ||
}); | ||
}; | ||
}.call(this); |
@@ -47,6 +47,6 @@ /* | ||
module.exports = Hub = function () { | ||
var hub, logPendingRequests, setupConnectTimeout; | ||
var hub, logPendingRequests, setupCompletionTimeout, setupTimeouts; | ||
hub = new EventEmitter; | ||
hub.fetch = function (options, cb) { | ||
var fetchId, fetchStart, hubHeaders, req, responseData; | ||
var completionTimeoutInterval, connectTimeoutInterval, fetchId, fetchStart, hubHeaders, req, responseData; | ||
fetchStart = microtime.nowDouble(); | ||
@@ -118,3 +118,5 @@ fetchId = generateUUID(); | ||
}); | ||
setupConnectTimeout(null != options.connectTimeout ? options.connectTimeout : Hub.connectTimeout, req, responseData); | ||
connectTimeoutInterval = null != options.connectTimeout ? options.connectTimeout : Hub.connectTimeout; | ||
completionTimeoutInterval = options.completionTimeout; | ||
setupTimeouts(connectTimeoutInterval, completionTimeoutInterval, req, responseData); | ||
return req; | ||
@@ -144,3 +146,3 @@ }; | ||
}; | ||
setupConnectTimeout = function (connectTimeoutInterval, request, responseData) { | ||
setupTimeouts = function (connectTimeoutInterval, completionTimeoutInterval, request, responseData) { | ||
return request.on('request', function (req) { | ||
@@ -164,3 +166,4 @@ return req.on('socket', function (socket) { | ||
clearTimeout(connectTimeout); | ||
return connectTimeout = null; | ||
connectTimeout = null; | ||
return setupCompletionTimeout(completionTimeoutInterval, req, responseData); | ||
}; | ||
@@ -173,2 +176,25 @@ connectingSocket = null != socket.socket ? socket.socket : socket; | ||
}; | ||
setupCompletionTimeout = function (completionTimeoutInterval, req, responseData) { | ||
var completionSuccessful, completionTimedOut, completionTimeout; | ||
if (!completionTimeoutInterval) | ||
return; | ||
completionTimeout = void 0; | ||
completionTimedOut = function () { | ||
var err; | ||
req.abort(); | ||
responseData.completionDuration = microtime.nowDouble() - responseData.connectDuration; | ||
err = new Error('ETIMEDOUT'); | ||
err.code = 'ETIMEDOUT'; | ||
err.message = 'Response timed out after ' + completionTimeoutInterval + 'ms'; | ||
err.responseData = responseData; | ||
return req.emit('error', err); | ||
}; | ||
completionSuccessful = function () { | ||
responseData.completionDuration = microtime.nowDouble() - responseData.connectDuration; | ||
clearTimeout(completionTimeout); | ||
return completionTimeout = null; | ||
}; | ||
req.on('complete', completionSuccessful); | ||
return completionTimeout = setTimeout(completionTimedOut, completionTimeoutInterval); | ||
}; | ||
return hub; | ||
@@ -175,0 +201,0 @@ }; |
{ | ||
"name": "gofer", | ||
"version": "1.0.2", | ||
"version": "1.2.0", | ||
"description": "A general purpose service client library for node.js", | ||
@@ -44,17 +44,18 @@ "main": "lib/gofer.js", | ||
"dependencies": { | ||
"debug": "~0.8.1", | ||
"lodash": "~2.4.1", | ||
"microtime": "~0.5.1", | ||
"node-uuid": "~1.4.1", | ||
"request": "2.40.0", | ||
"debug": "~0.8.1" | ||
"request": "2.40.0" | ||
}, | ||
"devDependencies": { | ||
"assertive": "~1.4.0", | ||
"bondjs": "~1.1.1", | ||
"coffee-script-redux": "2.0.0-beta7", | ||
"deepmerge": "^0.2.7", | ||
"express": "~4.1.1", | ||
"mocha": "~1.18.2", | ||
"semver": "~2.1.0", | ||
"npub": "0.0.5", | ||
"assertive": "~1.4.0", | ||
"bondjs": "~1.1.1", | ||
"express": "~4.1.1" | ||
"semver": "~2.1.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
48597
681
2
8