superagent-defaults
Advanced tools
Comparing version 0.1.8 to 0.1.9
@@ -5,15 +5,16 @@ { | ||
"description": "Create some defaults for superagent requests", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"keywords": [], | ||
"dependencies": { | ||
"tj/superagent": "*", | ||
"component/emitter": "*", | ||
"camshaft/require-component": "*" | ||
"visionmedia/superagent": "*", | ||
"component/emitter": "*" | ||
}, | ||
"development": {}, | ||
"license": "MIT", | ||
"main": "component.js", | ||
"scripts": [ | ||
"index.js", | ||
"methods.js" | ||
"component.js", | ||
"lib/methods.js", | ||
"lib/context" | ||
] | ||
} |
104
index.js
/** | ||
* Module dependencies. | ||
* Module dependencies | ||
*/ | ||
require = require('require-component')(require); | ||
var Context = module.exports = require('./lib/context'); | ||
var Emitter = require('emitter-component'); | ||
var request = require('superagent'); | ||
var Emitter = require('emitter'); | ||
var methods = require('./methods'); | ||
/** | ||
* Expose `Context`. | ||
* Mixin the emitter | ||
*/ | ||
module.exports = Context; | ||
/** | ||
* Initialize a new `Context`. | ||
* | ||
* @api public | ||
*/ | ||
function Context(superagent) { | ||
if (!(this instanceof Context)) return new Context(superagent); | ||
this.headers = []; | ||
this.authCredentials = {}; | ||
this.request = superagent || request; | ||
} | ||
/** | ||
* Inherit from `Emitter` | ||
*/ | ||
Emitter(Context.prototype); | ||
/** | ||
* Set default auth credentials | ||
* | ||
* @api public | ||
*/ | ||
Context.prototype.auth = function (user, pass) { | ||
this.authCredentials.user = user; | ||
this.authCredentials.pass = pass; | ||
}; | ||
/** | ||
* Add a default header to the context | ||
* | ||
* @api public | ||
*/ | ||
Context.prototype.set = function() { | ||
this.headers.push(arguments); | ||
return this; | ||
}; | ||
/** | ||
* Set the default headers on the req | ||
* | ||
* @api private | ||
*/ | ||
Context.prototype.applyHeaders = function(req) { | ||
each(this.headers, function(header) { | ||
req.set.apply(req, header); | ||
}); | ||
}; | ||
// generate HTTP verb methods | ||
each(methods, function(method){ | ||
var name = 'delete' == method ? 'del' : method; | ||
method = method.toUpperCase(); | ||
Context.prototype[name] = function(url, fn){ | ||
var req = this.request(method, url); | ||
var auth = this.authCredentials; | ||
// Do the attaching here | ||
this.applyHeaders(req); | ||
// Call superagent's auth method | ||
if(auth.hasOwnProperty('user') && auth.hasOwnProperty('pass')) { | ||
req.auth(auth.user, auth.pass); | ||
} | ||
// Tell the listeners we've created a new request | ||
this.emit('request', req); | ||
fn && req.end(fn); | ||
return req; | ||
}; | ||
}); | ||
/** | ||
* Iterate array-ish. | ||
* | ||
* @param {Array|Object} arr | ||
* @param {Function} fn | ||
* @api private | ||
*/ | ||
function each(arr, fn) { | ||
for (var i = 0; i < arr.length; ++i) { | ||
fn(arr[i], i); | ||
} | ||
} |
{ | ||
"name": "superagent-defaults", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "Create some defaults for superagent requests", | ||
@@ -21,4 +21,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"emitter-component": "~1.0.1", | ||
"require-component": "~0.1.0" | ||
"emitter-component": "~1.0.1" | ||
}, | ||
@@ -25,0 +24,0 @@ "devDependencies": { |
@@ -1,7 +0,5 @@ | ||
;(function(){ | ||
/** | ||
* Require the given path. | ||
* Require the module at `name`. | ||
* | ||
* @param {String} path | ||
* @param {String} name | ||
* @return {Object} exports | ||
@@ -11,25 +9,10 @@ * @api public | ||
function require(path, parent, orig) { | ||
var resolved = require.resolve(path); | ||
function require(name) { | ||
var module = require.modules[name]; | ||
if (!module) throw new Error('failed to require "' + name + '"'); | ||
// lookup failed | ||
if (null == resolved) { | ||
orig = orig || path; | ||
parent = parent || 'root'; | ||
var err = new Error('Failed to require "' + orig + '" from "' + parent + '"'); | ||
err.path = orig; | ||
err.parent = parent; | ||
err.require = true; | ||
throw err; | ||
} | ||
var module = require.modules[resolved]; | ||
// perform real require() | ||
// by invoking the module's | ||
// registered function | ||
if (!module.exports) { | ||
module.exports = {}; | ||
if (!('exports' in module) && typeof module.definition === 'function') { | ||
module.client = module.component = true; | ||
module.call(this, module.exports, require.relative(resolved), module); | ||
module.definition.call(this, module.exports = {}, module); | ||
delete module.definition; | ||
} | ||
@@ -41,163 +24,287 @@ | ||
/** | ||
* Registered modules. | ||
* Meta info, accessible in the global scope unless you use AMD option. | ||
*/ | ||
require.modules = {}; | ||
require.loader = 'component'; | ||
/** | ||
* Registered aliases. | ||
* Internal helper object, contains a sorting function for semantiv versioning | ||
*/ | ||
require.helper = {}; | ||
require.helper.semVerSort = function(a, b) { | ||
var aArray = a.version.split('.'); | ||
var bArray = b.version.split('.'); | ||
for (var i=0; i<aArray.length; ++i) { | ||
var aInt = parseInt(aArray[i], 10); | ||
var bInt = parseInt(bArray[i], 10); | ||
if (aInt === bInt) { | ||
var aLex = aArray[i].substr((""+aInt).length); | ||
var bLex = bArray[i].substr((""+bInt).length); | ||
if (aLex === '' && bLex !== '') return 1; | ||
if (aLex !== '' && bLex === '') return -1; | ||
if (aLex !== '' && bLex !== '') return aLex > bLex ? 1 : -1; | ||
continue; | ||
} else if (aInt > bInt) { | ||
return 1; | ||
} else { | ||
return -1; | ||
} | ||
} | ||
return 0; | ||
} | ||
require.aliases = {}; | ||
/** | ||
* Find and require a module which name starts with the provided name. | ||
* If multiple modules exists, the highest semver is used. | ||
* This function can only be used for remote dependencies. | ||
* @param {String} name - module name: `user~repo` | ||
* @param {Boolean} returnPath - returns the canonical require path if true, | ||
* otherwise it returns the epxorted module | ||
*/ | ||
require.latest = function (name, returnPath) { | ||
function showError(name) { | ||
throw new Error('failed to find latest module of "' + name + '"'); | ||
} | ||
// only remotes with semvers, ignore local files conataining a '/' | ||
var versionRegexp = /(.*)~(.*)@v?(\d+\.\d+\.\d+[^\/]*)$/; | ||
var remoteRegexp = /(.*)~(.*)/; | ||
if (!remoteRegexp.test(name)) showError(name); | ||
var moduleNames = Object.keys(require.modules); | ||
var semVerCandidates = []; | ||
var otherCandidates = []; // for instance: name of the git branch | ||
for (var i=0; i<moduleNames.length; i++) { | ||
var moduleName = moduleNames[i]; | ||
if (new RegExp(name + '@').test(moduleName)) { | ||
var version = moduleName.substr(name.length+1); | ||
var semVerMatch = versionRegexp.exec(moduleName); | ||
if (semVerMatch != null) { | ||
semVerCandidates.push({version: version, name: moduleName}); | ||
} else { | ||
otherCandidates.push({version: version, name: moduleName}); | ||
} | ||
} | ||
} | ||
if (semVerCandidates.concat(otherCandidates).length === 0) { | ||
showError(name); | ||
} | ||
if (semVerCandidates.length > 0) { | ||
var module = semVerCandidates.sort(require.helper.semVerSort).pop().name; | ||
if (returnPath === true) { | ||
return module; | ||
} | ||
return require(module); | ||
} | ||
// if the build contains more than one branch of the same module | ||
// you should not use this funciton | ||
var module = otherCandidates.pop().name; | ||
if (returnPath === true) { | ||
return module; | ||
} | ||
return require(module); | ||
} | ||
/** | ||
* Resolve `path`. | ||
* Registered modules. | ||
*/ | ||
require.modules = {}; | ||
/** | ||
* Register module at `name` with callback `definition`. | ||
* | ||
* Lookup: | ||
* | ||
* - PATH/index.js | ||
* - PATH.js | ||
* - PATH | ||
* | ||
* @param {String} path | ||
* @return {String} path or null | ||
* @param {String} name | ||
* @param {Function} definition | ||
* @api private | ||
*/ | ||
require.resolve = function(path) { | ||
if (path.charAt(0) === '/') path = path.slice(1); | ||
var paths = [ | ||
path, | ||
path + '.js', | ||
path + '.json', | ||
path + '/index.js', | ||
path + '/index.json' | ||
]; | ||
for (var i = 0; i < paths.length; i++) { | ||
var path = paths[i]; | ||
if (require.modules.hasOwnProperty(path)) return path; | ||
if (require.aliases.hasOwnProperty(path)) return require.aliases[path]; | ||
} | ||
require.register = function (name, definition) { | ||
require.modules[name] = { | ||
definition: definition | ||
}; | ||
}; | ||
/** | ||
* Normalize `path` relative to the current path. | ||
* Define a module's exports immediately with `exports`. | ||
* | ||
* @param {String} curr | ||
* @param {String} path | ||
* @return {String} | ||
* @param {String} name | ||
* @param {Generic} exports | ||
* @api private | ||
*/ | ||
require.normalize = function(curr, path) { | ||
var segs = []; | ||
require.define = function (name, exports) { | ||
require.modules[name] = { | ||
exports: exports | ||
}; | ||
}; | ||
require.register("component~emitter@1.1.3", function (exports, module) { | ||
if ('.' != path.charAt(0)) return path; | ||
/** | ||
* Expose `Emitter`. | ||
*/ | ||
curr = curr.split('/'); | ||
path = path.split('/'); | ||
module.exports = Emitter; | ||
for (var i = 0; i < path.length; ++i) { | ||
if ('..' == path[i]) { | ||
curr.pop(); | ||
} else if ('.' != path[i] && '' != path[i]) { | ||
segs.push(path[i]); | ||
} | ||
} | ||
/** | ||
* Initialize a new `Emitter`. | ||
* | ||
* @api public | ||
*/ | ||
return curr.concat(segs).join('/'); | ||
function Emitter(obj) { | ||
if (obj) return mixin(obj); | ||
}; | ||
/** | ||
* Register module at `path` with callback `definition`. | ||
* Mixin the emitter properties. | ||
* | ||
* @param {String} path | ||
* @param {Function} definition | ||
* @param {Object} obj | ||
* @return {Object} | ||
* @api private | ||
*/ | ||
require.register = function(path, definition) { | ||
require.modules[path] = definition; | ||
function mixin(obj) { | ||
for (var key in Emitter.prototype) { | ||
obj[key] = Emitter.prototype[key]; | ||
} | ||
return obj; | ||
} | ||
/** | ||
* Listen on the given `event` with `fn`. | ||
* | ||
* @param {String} event | ||
* @param {Function} fn | ||
* @return {Emitter} | ||
* @api public | ||
*/ | ||
Emitter.prototype.on = | ||
Emitter.prototype.addEventListener = function(event, fn){ | ||
this._callbacks = this._callbacks || {}; | ||
(this._callbacks[event] = this._callbacks[event] || []) | ||
.push(fn); | ||
return this; | ||
}; | ||
/** | ||
* Alias a module definition. | ||
* Adds an `event` listener that will be invoked a single | ||
* time then automatically removed. | ||
* | ||
* @param {String} from | ||
* @param {String} to | ||
* @api private | ||
* @param {String} event | ||
* @param {Function} fn | ||
* @return {Emitter} | ||
* @api public | ||
*/ | ||
require.alias = function(from, to) { | ||
if (!require.modules.hasOwnProperty(from)) { | ||
throw new Error('Failed to alias "' + from + '", it does not exist'); | ||
Emitter.prototype.once = function(event, fn){ | ||
var self = this; | ||
this._callbacks = this._callbacks || {}; | ||
function on() { | ||
self.off(event, on); | ||
fn.apply(this, arguments); | ||
} | ||
require.aliases[to] = from; | ||
on.fn = fn; | ||
this.on(event, on); | ||
return this; | ||
}; | ||
/** | ||
* Return a require function relative to the `parent` path. | ||
* Remove the given callback for `event` or all | ||
* registered callbacks. | ||
* | ||
* @param {String} parent | ||
* @return {Function} | ||
* @api private | ||
* @param {String} event | ||
* @param {Function} fn | ||
* @return {Emitter} | ||
* @api public | ||
*/ | ||
require.relative = function(parent) { | ||
var p = require.normalize(parent, '..'); | ||
Emitter.prototype.off = | ||
Emitter.prototype.removeListener = | ||
Emitter.prototype.removeAllListeners = | ||
Emitter.prototype.removeEventListener = function(event, fn){ | ||
this._callbacks = this._callbacks || {}; | ||
/** | ||
* lastIndexOf helper. | ||
*/ | ||
// all | ||
if (0 == arguments.length) { | ||
this._callbacks = {}; | ||
return this; | ||
} | ||
function lastIndexOf(arr, obj) { | ||
var i = arr.length; | ||
while (i--) { | ||
if (arr[i] === obj) return i; | ||
// specific event | ||
var callbacks = this._callbacks[event]; | ||
if (!callbacks) return this; | ||
// remove all handlers | ||
if (1 == arguments.length) { | ||
delete this._callbacks[event]; | ||
return this; | ||
} | ||
// remove specific handler | ||
var cb; | ||
for (var i = 0; i < callbacks.length; i++) { | ||
cb = callbacks[i]; | ||
if (cb === fn || cb.fn === fn) { | ||
callbacks.splice(i, 1); | ||
break; | ||
} | ||
return -1; | ||
} | ||
return this; | ||
}; | ||
/** | ||
* The relative require() itself. | ||
*/ | ||
/** | ||
* Emit `event` with the given args. | ||
* | ||
* @param {String} event | ||
* @param {Mixed} ... | ||
* @return {Emitter} | ||
*/ | ||
function localRequire(path) { | ||
var resolved = localRequire.resolve(path); | ||
return require(resolved, parent, path); | ||
Emitter.prototype.emit = function(event){ | ||
this._callbacks = this._callbacks || {}; | ||
var args = [].slice.call(arguments, 1) | ||
, callbacks = this._callbacks[event]; | ||
if (callbacks) { | ||
callbacks = callbacks.slice(0); | ||
for (var i = 0, len = callbacks.length; i < len; ++i) { | ||
callbacks[i].apply(this, args); | ||
} | ||
} | ||
/** | ||
* Resolve relative to the parent. | ||
*/ | ||
return this; | ||
}; | ||
localRequire.resolve = function(path) { | ||
var c = path.charAt(0); | ||
if ('/' == c) return path.slice(1); | ||
if ('.' == c) return require.normalize(p, path); | ||
/** | ||
* Return array of callbacks for `event`. | ||
* | ||
* @param {String} event | ||
* @return {Array} | ||
* @api public | ||
*/ | ||
// resolve deps by returning | ||
// the dep in the nearest "deps" | ||
// directory | ||
var segs = parent.split('/'); | ||
var i = lastIndexOf(segs, 'deps') + 1; | ||
if (!i) i = 0; | ||
path = segs.slice(0, i + 1).join('/') + '/deps/' + path; | ||
return path; | ||
}; | ||
Emitter.prototype.listeners = function(event){ | ||
this._callbacks = this._callbacks || {}; | ||
return this._callbacks[event] || []; | ||
}; | ||
/** | ||
* Check if module is defined at `path`. | ||
*/ | ||
/** | ||
* Check if this emitter has `event` handlers. | ||
* | ||
* @param {String} event | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
localRequire.exists = function(path) { | ||
return require.modules.hasOwnProperty(localRequire.resolve(path)); | ||
}; | ||
return localRequire; | ||
Emitter.prototype.hasListeners = function(event){ | ||
return !! this.listeners(event).length; | ||
}; | ||
require.register("RedVentures-reduce/index.js", function(exports, require, module){ | ||
}); | ||
require.register("component~reduce@1.0.1", function (exports, module) { | ||
/** | ||
@@ -227,3 +334,4 @@ * Reduce `arr` with `fn`. | ||
}); | ||
require.register("visionmedia-superagent/lib/client.js", function(exports, require, module){ | ||
require.register("visionmedia~superagent@0.21.0", function (exports, module) { | ||
/** | ||
@@ -233,4 +341,4 @@ * Module dependencies. | ||
var Emitter = require('emitter'); | ||
var reduce = require('reduce'); | ||
var Emitter = require('component~emitter@1.1.3'); | ||
var reduce = require('component~reduce@1.0.1'); | ||
@@ -328,4 +436,6 @@ /** | ||
for (var key in obj) { | ||
pairs.push(encodeURIComponent(key) | ||
+ '=' + encodeURIComponent(obj[key])); | ||
if (null != obj[key]) { | ||
pairs.push(encodeURIComponent(key) | ||
+ '=' + encodeURIComponent(obj[key])); | ||
} | ||
} | ||
@@ -380,2 +490,3 @@ return pairs.join('&'); | ||
json: 'application/json', | ||
xml: 'application/xml', | ||
urlencoded: 'application/x-www-form-urlencoded', | ||
@@ -525,3 +636,5 @@ 'form': 'application/x-www-form-urlencoded', | ||
this.xhr = this.req.xhr; | ||
this.text = this.xhr.responseText; | ||
this.text = this.req.method !='HEAD' | ||
? this.xhr.responseText | ||
: null; | ||
this.setStatusProperties(this.xhr.status); | ||
@@ -534,3 +647,5 @@ this.header = this.headers = parseHeader(this.xhr.getAllResponseHeaders()); | ||
this.setHeaderProperties(this.header); | ||
this.body = this.parseBody(this.text); | ||
this.body = this.req.method != 'HEAD' | ||
? this.parseBody(this.text) | ||
: null; | ||
} | ||
@@ -585,3 +700,3 @@ | ||
var parse = request.parse[this.type]; | ||
return parse | ||
return parse && str && str.length | ||
? parse(str) | ||
@@ -648,9 +763,9 @@ : null; | ||
var method = req.method; | ||
var path = req.path; | ||
var url = req.url; | ||
var msg = 'cannot ' + method + ' ' + path + ' (' + this.status + ')'; | ||
var msg = 'cannot ' + method + ' ' + url + ' (' + this.status + ')'; | ||
var err = new Error(msg); | ||
err.status = this.status; | ||
err.method = method; | ||
err.path = path; | ||
err.url = url; | ||
@@ -683,5 +798,14 @@ return err; | ||
this.on('end', function(){ | ||
var res = new Response(self); | ||
if ('HEAD' == method) res.text = null; | ||
self.callback(null, res); | ||
var err = null; | ||
var res = null; | ||
try { | ||
res = new Response(self); | ||
} catch(e) { | ||
err = new Error('Parser is unable to parse the response'); | ||
err.parse = true; | ||
err.original = e; | ||
} | ||
self.callback(err, res); | ||
}); | ||
@@ -697,2 +821,11 @@ } | ||
/** | ||
* Allow for extension | ||
*/ | ||
Request.prototype.use = function(fn) { | ||
fn(this); | ||
return this; | ||
} | ||
/** | ||
* Set timeout to `ms`. | ||
@@ -772,2 +905,22 @@ * | ||
/** | ||
* Remove header `field`. | ||
* | ||
* Example: | ||
* | ||
* req.get('/') | ||
* .unset('User-Agent') | ||
* .end(callback); | ||
* | ||
* @param {String} field | ||
* @return {Request} for chaining | ||
* @api public | ||
*/ | ||
Request.prototype.unset = function(field){ | ||
delete this._header[field.toLowerCase()]; | ||
delete this.header[field]; | ||
return this; | ||
}; | ||
/** | ||
* Get case-insensitive header `field` value. | ||
@@ -812,2 +965,27 @@ * | ||
/** | ||
* Set Accept to `type`, mapping values from `request.types`. | ||
* | ||
* Examples: | ||
* | ||
* superagent.types.json = 'application/json'; | ||
* | ||
* request.get('/agent') | ||
* .accept('json') | ||
* .end(callback); | ||
* | ||
* request.get('/agent') | ||
* .accept('application/json') | ||
* .end(callback); | ||
* | ||
* @param {String} accept | ||
* @return {Request} for chaining | ||
* @api public | ||
*/ | ||
Request.prototype.accept = function(type){ | ||
this.set('Accept', request.types[type] || type); | ||
return this; | ||
}; | ||
/** | ||
* Set Authorization field value with `user` and `pass`. | ||
@@ -848,2 +1026,47 @@ * | ||
/** | ||
* Write the field `name` and `val` for "multipart/form-data" | ||
* request bodies. | ||
* | ||
* ``` js | ||
* request.post('/upload') | ||
* .field('foo', 'bar') | ||
* .end(callback); | ||
* ``` | ||
* | ||
* @param {String} name | ||
* @param {String|Blob|File} val | ||
* @return {Request} for chaining | ||
* @api public | ||
*/ | ||
Request.prototype.field = function(name, val){ | ||
if (!this._formData) this._formData = new FormData(); | ||
this._formData.append(name, val); | ||
return this; | ||
}; | ||
/** | ||
* Queue the given `file` as an attachment to the specified `field`, | ||
* with optional `filename`. | ||
* | ||
* ``` js | ||
* request.post('/upload') | ||
* .attach(new Blob(['<a id="a"><b id="b">hey!</b></a>'], { type: "text/html"})) | ||
* .end(callback); | ||
* ``` | ||
* | ||
* @param {String} field | ||
* @param {Blob|File} file | ||
* @param {String} filename | ||
* @return {Request} for chaining | ||
* @api public | ||
*/ | ||
Request.prototype.attach = function(field, file, filename){ | ||
if (!this._formData) this._formData = new FormData(); | ||
this._formData.append(field, file, filename); | ||
return this; | ||
}; | ||
/** | ||
* Send `data`, defaulting the `.type()` to "json" when | ||
@@ -938,2 +1161,3 @@ * an object is given. | ||
var fn = this._callback; | ||
this.clearTimeout(); | ||
if (2 == fn.length) return fn(err, res); | ||
@@ -999,3 +1223,3 @@ if (err) return this.emit('error', err); | ||
var timeout = this._timeout; | ||
var data = this._data; | ||
var data = this._formData || this._data; | ||
@@ -1005,5 +1229,2 @@ // store callback | ||
// CORS | ||
if (this._withCredentials) xhr.withCredentials = true; | ||
// state change | ||
@@ -1045,2 +1266,5 @@ xhr.onreadystatechange = function(){ | ||
// CORS | ||
if (this._withCredentials) xhr.withCredentials = true; | ||
// body | ||
@@ -1060,2 +1284,3 @@ if ('GET' != this.method && 'HEAD' != this.method && 'string' != typeof data && !isHost(data)) { | ||
// send stuff | ||
this.emit('request', this); | ||
xhr.send(data); | ||
@@ -1119,3 +1344,3 @@ return this; | ||
/** | ||
* GET `url` with optional callback `fn(res)`. | ||
* HEAD `url` with optional callback `fn(res)`. | ||
* | ||
@@ -1213,297 +1438,20 @@ * @param {String} url | ||
}); | ||
require.register("component-indexof/index.js", function(exports, require, module){ | ||
module.exports = function(arr, obj){ | ||
if (arr.indexOf) return arr.indexOf(obj); | ||
for (var i = 0; i < arr.length; ++i) { | ||
if (arr[i] === obj) return i; | ||
} | ||
return -1; | ||
}; | ||
}); | ||
require.register("component-emitter/index.js", function(exports, require, module){ | ||
require.register("superagent-defaults", function (exports, module) { | ||
/** | ||
* Module dependencies. | ||
* Module dependencies | ||
*/ | ||
var index = require('indexof'); | ||
var Context = require('superagent-defaults/lib/context'); | ||
var Emitter = require('component~emitter@1.1.3'); | ||
/** | ||
* Expose `Emitter`. | ||
* Mixin the emitter | ||
*/ | ||
module.exports = Emitter; | ||
Emitter(Context.prototype); | ||
/** | ||
* Initialize a new `Emitter`. | ||
* | ||
* @api public | ||
*/ | ||
function Emitter(obj) { | ||
if (obj) return mixin(obj); | ||
}; | ||
/** | ||
* Mixin the emitter properties. | ||
* | ||
* @param {Object} obj | ||
* @return {Object} | ||
* @api private | ||
*/ | ||
function mixin(obj) { | ||
for (var key in Emitter.prototype) { | ||
obj[key] = Emitter.prototype[key]; | ||
} | ||
return obj; | ||
} | ||
/** | ||
* Listen on the given `event` with `fn`. | ||
* | ||
* @param {String} event | ||
* @param {Function} fn | ||
* @return {Emitter} | ||
* @api public | ||
*/ | ||
Emitter.prototype.on = function(event, fn){ | ||
this._callbacks = this._callbacks || {}; | ||
(this._callbacks[event] = this._callbacks[event] || []) | ||
.push(fn); | ||
return this; | ||
}; | ||
/** | ||
* Adds an `event` listener that will be invoked a single | ||
* time then automatically removed. | ||
* | ||
* @param {String} event | ||
* @param {Function} fn | ||
* @return {Emitter} | ||
* @api public | ||
*/ | ||
Emitter.prototype.once = function(event, fn){ | ||
var self = this; | ||
this._callbacks = this._callbacks || {}; | ||
function on() { | ||
self.off(event, on); | ||
fn.apply(this, arguments); | ||
} | ||
fn._off = on; | ||
this.on(event, on); | ||
return this; | ||
}; | ||
/** | ||
* Remove the given callback for `event` or all | ||
* registered callbacks. | ||
* | ||
* @param {String} event | ||
* @param {Function} fn | ||
* @return {Emitter} | ||
* @api public | ||
*/ | ||
Emitter.prototype.off = | ||
Emitter.prototype.removeListener = | ||
Emitter.prototype.removeAllListeners = function(event, fn){ | ||
this._callbacks = this._callbacks || {}; | ||
// all | ||
if (0 == arguments.length) { | ||
this._callbacks = {}; | ||
return this; | ||
} | ||
// specific event | ||
var callbacks = this._callbacks[event]; | ||
if (!callbacks) return this; | ||
// remove all handlers | ||
if (1 == arguments.length) { | ||
delete this._callbacks[event]; | ||
return this; | ||
} | ||
// remove specific handler | ||
var i = index(callbacks, fn._off || fn); | ||
if (~i) callbacks.splice(i, 1); | ||
return this; | ||
}; | ||
/** | ||
* Emit `event` with the given args. | ||
* | ||
* @param {String} event | ||
* @param {Mixed} ... | ||
* @return {Emitter} | ||
*/ | ||
Emitter.prototype.emit = function(event){ | ||
this._callbacks = this._callbacks || {}; | ||
var args = [].slice.call(arguments, 1) | ||
, callbacks = this._callbacks[event]; | ||
if (callbacks) { | ||
callbacks = callbacks.slice(0); | ||
for (var i = 0, len = callbacks.length; i < len; ++i) { | ||
callbacks[i].apply(this, args); | ||
} | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Return array of callbacks for `event`. | ||
* | ||
* @param {String} event | ||
* @return {Array} | ||
* @api public | ||
*/ | ||
Emitter.prototype.listeners = function(event){ | ||
this._callbacks = this._callbacks || {}; | ||
return this._callbacks[event] || []; | ||
}; | ||
/** | ||
* Check if this emitter has `event` handlers. | ||
* | ||
* @param {String} event | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
Emitter.prototype.hasListeners = function(event){ | ||
return !! this.listeners(event).length; | ||
}; | ||
}); | ||
require.register("CamShaft-require-component/index.js", function(exports, require, module){ | ||
/** | ||
* Require a module with a fallback | ||
*/ | ||
module.exports = function(parent) { | ||
function require(name, fallback) { | ||
try { | ||
return parent(name); | ||
} | ||
catch (e) { | ||
try { | ||
return parent(fallback || name+"-component"); | ||
} | ||
catch(e2) { | ||
throw e; | ||
} | ||
} | ||
}; | ||
// Merge the old properties | ||
for (var key in parent) { | ||
require[key] = parent[key]; | ||
} | ||
return require; | ||
}; | ||
}); | ||
require.register("superagent-defaults/index.js", function(exports, require, module){ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
require = require('require-component')(require); | ||
var request = require('superagent') | ||
, Emitter = require('emitter') | ||
, methods = require('methods', './methods'); | ||
/** | ||
* Expose `Context`. | ||
*/ | ||
module.exports = Context; | ||
/** | ||
* Initialize a new `Context`. | ||
* | ||
* @api public | ||
*/ | ||
function Context() { | ||
if (!(this instanceof Context)) return new Context; | ||
this.headers = []; | ||
} | ||
/** | ||
* Inherit from `Emitter` | ||
*/ | ||
Emitter(Context.prototype); | ||
/** | ||
* Add a default header to the context | ||
* | ||
* @api public | ||
*/ | ||
Context.prototype.set = function() { | ||
this.headers.push(arguments); | ||
return this; | ||
} | ||
/** | ||
* Set the default headers on the req | ||
* | ||
* @api private | ||
*/ | ||
Context.prototype.applyHeaders = function(req) { | ||
each(this.headers, function(header) { | ||
req.set.apply(req, header); | ||
}); | ||
} | ||
// generate HTTP verb methods | ||
each(methods, function(method){ | ||
var name = 'delete' == method ? 'del' : method; | ||
method = method.toUpperCase(); | ||
Context.prototype[name] = function(url, fn){ | ||
var req = request(method, url); | ||
// Do the attaching here | ||
this.applyHeaders(req); | ||
// Tell the listeners we've created a new request | ||
this.emit('request', req); | ||
fn && req.end(fn); | ||
return req; | ||
}; | ||
}); | ||
/** | ||
* Iterate array-ish. | ||
* | ||
* @param {Array|Object} arr | ||
* @param {Function} fn | ||
* @api private | ||
*/ | ||
function each(arr, fn) { | ||
for (var i = 0; i < arr.length; ++i) { | ||
fn(arr[i], i); | ||
} | ||
} | ||
}); | ||
require.register("superagent-defaults/methods.js", function(exports, require, module){ | ||
require.register("superagent-defaults/lib/methods.js", function (exports, module) { | ||
module.exports = [ | ||
@@ -1517,29 +1465,25 @@ 'get', | ||
'trace', | ||
'patch' | ||
'copy', | ||
'lock', | ||
'mkcol', | ||
'move', | ||
'purge', | ||
'propfind', | ||
'proppatch', | ||
'unlock', | ||
'report', | ||
'mkactivity', | ||
'checkout', | ||
'merge', | ||
'm-search', | ||
'notify', | ||
'subscribe', | ||
'unsubscribe', | ||
'patch', | ||
'search', | ||
'connect' | ||
]; | ||
}); | ||
require.alias("visionmedia-superagent/lib/client.js", "superagent-defaults/deps/superagent/lib/client.js"); | ||
require.alias("visionmedia-superagent/lib/client.js", "superagent-defaults/deps/superagent/index.js"); | ||
require.alias("visionmedia-superagent/lib/client.js", "superagent/index.js"); | ||
require.alias("component-emitter/index.js", "visionmedia-superagent/deps/emitter/index.js"); | ||
require.alias("component-indexof/index.js", "component-emitter/deps/indexof/index.js"); | ||
require.alias("RedVentures-reduce/index.js", "visionmedia-superagent/deps/reduce/index.js"); | ||
require.alias("visionmedia-superagent/lib/client.js", "visionmedia-superagent/index.js"); | ||
require.alias("component-emitter/index.js", "superagent-defaults/deps/emitter/index.js"); | ||
require.alias("component-emitter/index.js", "emitter/index.js"); | ||
require.alias("component-indexof/index.js", "component-emitter/deps/indexof/index.js"); | ||
require.alias("CamShaft-require-component/index.js", "superagent-defaults/deps/require-component/index.js"); | ||
require.alias("CamShaft-require-component/index.js", "require-component/index.js"); | ||
if (typeof exports == "object") { | ||
module.exports = require("superagent-defaults"); | ||
} else if (typeof define == "function" && define.amd) { | ||
define(function(){ return require("superagent-defaults"); }); | ||
} else { | ||
this["superagent-defaults"] = require("superagent-defaults"); | ||
}})(); | ||
require("superagent-defaults"); |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
2
11
3
0
36303
1462
- Removedrequire-component@~0.1.0
- Removedrequire-component@0.1.1(transitive)