swagger-client
Version:
swagger.js is a javascript client for use with swaggering APIs.
1,646 lines (1,403 loc) • 679 kB
JavaScript
var require = function (file, cwd) {
var resolved = require.resolve(file, cwd || '/');
var mod = require.modules[resolved];
if (!mod) throw new Error(
'Failed to resolve module ' + file + ', tried ' + resolved
);
var res = mod._cached ? mod._cached : mod();
return res;
}
require.paths = [];
require.modules = {};
require.extensions = [".js",".coffee"];
require._core = {
'assert': true,
'events': true,
'fs': true,
'path': true,
'vm': true
};
require.resolve = (function () {
return function (x, cwd) {
if (!cwd) cwd = '/';
if (require._core[x]) return x;
var path = require.modules.path();
var y = cwd || '.';
if (x.match(/^(?:\.\.?\/|\/)/)) {
var m = loadAsFileSync(path.resolve(y, x))
|| loadAsDirectorySync(path.resolve(y, x));
if (m) return m;
}
var n = loadNodeModulesSync(x, y);
if (n) return n;
throw new Error("Cannot find module '" + x + "'");
function loadAsFileSync (x) {
if (require.modules[x]) {
return x;
}
for (var i = 0; i < require.extensions.length; i++) {
var ext = require.extensions[i];
if (require.modules[x + ext]) return x + ext;
}
}
function loadAsDirectorySync (x) {
x = x.replace(/\/+$/, '');
var pkgfile = x + '/package.json';
if (require.modules[pkgfile]) {
var pkg = require.modules[pkgfile]();
var b = pkg.browserify;
if (typeof b === 'object' && b.main) {
var m = loadAsFileSync(path.resolve(x, b.main));
if (m) return m;
}
else if (typeof b === 'string') {
var m = loadAsFileSync(path.resolve(x, b));
if (m) return m;
}
else if (pkg.main) {
var m = loadAsFileSync(path.resolve(x, pkg.main));
if (m) return m;
}
}
return loadAsFileSync(x + '/index');
}
function loadNodeModulesSync (x, start) {
var dirs = nodeModulesPathsSync(start);
for (var i = 0; i < dirs.length; i++) {
var dir = dirs[i];
var m = loadAsFileSync(dir + '/' + x);
if (m) return m;
var n = loadAsDirectorySync(dir + '/' + x);
if (n) return n;
}
var m = loadAsFileSync(x);
if (m) return m;
}
function nodeModulesPathsSync (start) {
var parts;
if (start === '/') parts = [ '' ];
else parts = path.normalize(start).split('/');
var dirs = [];
for (var i = parts.length - 1; i >= 0; i--) {
if (parts[i] === 'node_modules') continue;
var dir = parts.slice(0, i + 1).join('/') + '/node_modules';
dirs.push(dir);
}
return dirs;
}
};
})();
require.alias = function (from, to) {
var path = require.modules.path();
var res = null;
try {
res = require.resolve(from + '/package.json', '/');
}
catch (err) {
res = require.resolve(from, '/');
}
var basedir = path.dirname(res);
var keys = (Object.keys || function (obj) {
var res = [];
for (var key in obj) res.push(key)
return res;
})(require.modules);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (key.slice(0, basedir.length + 1) === basedir + '/') {
var f = key.slice(basedir.length);
require.modules[to + f] = require.modules[basedir + f];
}
else if (key === basedir) {
require.modules[to] = require.modules[basedir];
}
}
};
require.define = function (filename, fn) {
var dirname = require._core[filename]
? ''
: require.modules.path().dirname(filename)
;
var require_ = function (file) {
return require(file, dirname)
};
require_.resolve = function (name) {
return require.resolve(name, dirname);
};
require_.modules = require.modules;
require_.define = require.define;
var module_ = { exports : {} };
require.modules[filename] = function () {
require.modules[filename]._cached = module_.exports;
fn.call(
module_.exports,
require_,
module_,
module_.exports,
dirname,
filename
);
require.modules[filename]._cached = module_.exports;
return module_.exports;
};
};
if (typeof process === 'undefined') process = {};
if (!process.nextTick) process.nextTick = (function () {
var queue = [];
var canPost = typeof window !== 'undefined'
&& window.postMessage && window.addEventListener
;
if (canPost) {
window.addEventListener('message', function (ev) {
if (ev.source === window && ev.data === 'browserify-tick') {
ev.stopPropagation();
if (queue.length > 0) {
var fn = queue.shift();
fn();
}
}
}, true);
}
return function (fn) {
if (canPost) {
queue.push(fn);
window.postMessage('browserify-tick', '*');
}
else setTimeout(fn, 0);
};
})();
if (!process.title) process.title = 'browser';
if (!process.binding) process.binding = function (name) {
if (name === 'evals') return require('vm')
else throw new Error('No such module')
};
if (!process.cwd) process.cwd = function () { return '.' };
require.define("path", function (require, module, exports, __dirname, __filename) {
function filter (xs, fn) {
var res = [];
for (var i = 0; i < xs.length; i++) {
if (fn(xs[i], i, xs)) res.push(xs[i]);
}
return res;
}
// resolves . and .. elements in a path array with directory names there
// must be no slashes, empty elements, or device names (c:\) in the array
// (so also no leading and trailing slashes - it does not distinguish
// relative and absolute paths)
function normalizeArray(parts, allowAboveRoot) {
// if the path tries to go above the root, `up` ends up > 0
var up = 0;
for (var i = parts.length; i >= 0; i--) {
var last = parts[i];
if (last == '.') {
parts.splice(i, 1);
} else if (last === '..') {
parts.splice(i, 1);
up++;
} else if (up) {
parts.splice(i, 1);
up--;
}
}
// if the path is allowed to go above the root, restore leading ..s
if (allowAboveRoot) {
for (; up--; up) {
parts.unshift('..');
}
}
return parts;
}
// Regex to split a filename into [*, dir, basename, ext]
// posix version
var splitPathRe = /^(.+\/(?!$)|\/)?((?:.+?)?(\.[^.]*)?)$/;
// path.resolve([from ...], to)
// posix version
exports.resolve = function() {
var resolvedPath = '',
resolvedAbsolute = false;
for (var i = arguments.length; i >= -1 && !resolvedAbsolute; i--) {
var path = (i >= 0)
? arguments[i]
: process.cwd();
// Skip empty and invalid entries
if (typeof path !== 'string' || !path) {
continue;
}
resolvedPath = path + '/' + resolvedPath;
resolvedAbsolute = path.charAt(0) === '/';
}
// At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe (might happen when process.cwd() fails)
// Normalize the path
resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) {
return !!p;
}), !resolvedAbsolute).join('/');
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
};
// path.normalize(path)
// posix version
exports.normalize = function(path) {
var isAbsolute = path.charAt(0) === '/',
trailingSlash = path.slice(-1) === '/';
// Normalize the path
path = normalizeArray(filter(path.split('/'), function(p) {
return !!p;
}), !isAbsolute).join('/');
if (!path && !isAbsolute) {
path = '.';
}
if (path && trailingSlash) {
path += '/';
}
return (isAbsolute ? '/' : '') + path;
};
// posix version
exports.join = function() {
var paths = Array.prototype.slice.call(arguments, 0);
return exports.normalize(filter(paths, function(p, index) {
return p && typeof p === 'string';
}).join('/'));
};
exports.dirname = function(path) {
var dir = splitPathRe.exec(path)[1] || '';
var isWindows = false;
if (!dir) {
// No dirname
return '.';
} else if (dir.length === 1 ||
(isWindows && dir.length <= 3 && dir.charAt(1) === ':')) {
// It is just a slash or a drive letter with a slash
return dir;
} else {
// It is a full dirname, strip trailing slash
return dir.substring(0, dir.length - 1);
}
};
exports.basename = function(path, ext) {
var f = splitPathRe.exec(path)[2] || '';
// TODO: make this comparison case-insensitive on windows?
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
}
return f;
};
exports.extname = function(path) {
return splitPathRe.exec(path)[3] || '';
};
});
require.define("/shred.js", function (require, module, exports, __dirname, __filename) {
// Shred is an HTTP client library intended to simplify the use of Node's
// built-in HTTP library. In particular, we wanted to make it easier to interact
// with HTTP-based APIs.
//
// See the [examples](./examples.html) for more details.
// Ax is a nice logging library we wrote. You can use any logger, providing it
// has `info`, `warn`, `debug`, and `error` methods that take a string.
var Ax = require("ax")
, CookieJarLib = require( "cookiejar" )
, CookieJar = CookieJarLib.CookieJar
;
// Shred takes some options, including a logger and request defaults.
var Shred = function(options) {
options = (options||{});
this.agent = options.agent;
this.defaults = options.defaults||{};
this.log = options.logger||(new Ax({ level: "info" }));
this._sharedCookieJar = new CookieJar();
this.logCurl = options.logCurl || false;
};
// Most of the real work is done in the request and reponse classes.
Shred.Request = require("./shred/request");
Shred.Response = require("./shred/response");
Shred.registerProcessor = require("./shred/content").registerProcessor;
// The `request` method kicks off a new request, instantiating a new `Request`
// object and passing along whatever default options we were given.
Shred.prototype = {
request: function(options) {
options.logger = options.logger || this.log;
options.logCurl = options.logCurl || this.logCurl;
// allow users to set cookieJar = null
options.cookieJar = ( 'cookieJar' in options ) ? options.cookieJar : this._sharedCookieJar;
options.agent = options.agent || this.agent;
// fill in default options
for (var key in this.defaults) {
if (this.defaults.hasOwnProperty(key) && !options[key]) {
options[key] = this.defaults[key]
}
}
return new Shred.Request(options);
}
};
// Define a bunch of convenience methods so that you don't have to include
// a `method` property in your request options.
"get put post delete".split(" ").forEach(function(method) {
Shred.prototype[method] = function(options) {
options.method = method;
return this.request(options);
};
});
module.exports = Shred;
});
require.define("/node_modules/ax/package.json", function (require, module, exports, __dirname, __filename) {
module.exports = {"main":"./lib/ax.js"}
});
require.define("/node_modules/ax/lib/ax.js", function (require, module, exports, __dirname, __filename) {
// Copyright (c) 2011 Border Stylo
var inspect = require("util").inspect
, fs = require("fs")
;
// this is a quick-and-dirty logger. there are other nicer loggers out there
// but the ones i found were also somewhat involved. this one has a Ruby
// logger type interface
//
// we can easily replace this, provide the info, debug, etc. methods are the
// same. or, we can change Haiku to use a more standard node.js interface
var format = function(level,message) {
var debug = (level=="debug"||level=="error");
if (!message) { return message.toString(); }
if (typeof(message) == "object") {
if (message instanceof Error && debug) {
return message.stack;
} else {
return inspect(message);
}
} else {
return message.toString();
}
};
var noOp = function(message) { return this; }
var makeLogger = function(level,fn) {
return function(message) {
this.stream.write(this.format(level, message)+"\n");
return this;
}
};
var Logger = function(options) {
var logger = this;
var options = options||{};
// Default options
options.level = options.level || "info";
options.timestamp = options.timestamp || true;
options.prefix = options.prefix || "";
logger.options = options;
// Allows a prefix to be added to the message.
//
// var logger = new Ax({ module: 'Haiku' })
// logger.warn('this is going to be awesome!');
// //=> Haiku: this is going to be awesome!
//
if (logger.options.module){
logger.options.prefix = logger.options.module;
}
// Write to stdout or a file
if (logger.options.file){
logger.stream = fs.createWriteStream(logger.options.file, {"flags": "a"});
} else {
if(process.title === "node")
logger.stream = process.stdout;
else if(process.title === "browser")
logger.stream = function () {
// Work around weird console context issue: http://code.google.com/p/chromium/issues/detail?id=48662
return console[logger.options.level].apply(console, arguments);
};
}
switch(logger.options.level){
case 'debug':
['debug', 'info', 'warn'].forEach(function (level) {
logger[level] = Logger.writer(level);
});
case 'info':
['info', 'warn'].forEach(function (level) {
logger[level] = Logger.writer(level);
});
case 'warn':
logger.warn = Logger.writer('warn');
}
}
// Used to define logger methods
Logger.writer = function(level){
return function(message){
var logger = this;
if(process.title === "node")
logger.stream.write(logger.format(level, message) + '\n');
else if(process.title === "browser")
logger.stream(logger.format(level, message) + '\n');
};
}
Logger.prototype = {
info: function(){},
debug: function(){},
warn: function(){},
error: Logger.writer('error'),
format: function(level, message){
if (! message) return '';
var logger = this
, prefix = logger.options.prefix
, timestamp = logger.options.timestamp ? " " + (new Date().toISOString()) : ""
;
return (prefix + timestamp + ": " + message);
}
};
module.exports = Logger;
});
require.define("util", function (require, module, exports, __dirname, __filename) {
// todo
});
require.define("fs", function (require, module, exports, __dirname, __filename) {
// nothing to see here... no file methods for the browser
});
require.define("/node_modules/cookiejar/package.json", function (require, module, exports, __dirname, __filename) {
module.exports = {"main":"cookiejar.js"}
});
require.define("/node_modules/cookiejar/cookiejar.js", function (require, module, exports, __dirname, __filename) {
exports.CookieAccessInfo=CookieAccessInfo=function CookieAccessInfo(domain,path,secure,script) {
if(this instanceof CookieAccessInfo) {
this.domain=domain||undefined;
this.path=path||"/";
this.secure=!!secure;
this.script=!!script;
return this;
}
else {
return new CookieAccessInfo(domain,path,secure,script)
}
}
exports.Cookie=Cookie=function Cookie(cookiestr) {
if(cookiestr instanceof Cookie) {
return cookiestr;
}
else {
if(this instanceof Cookie) {
this.name = null;
this.value = null;
this.expiration_date = Infinity;
this.path = "/";
this.domain = null;
this.secure = false; //how to define?
this.noscript = false; //httponly
if(cookiestr) {
this.parse(cookiestr)
}
return this;
}
return new Cookie(cookiestr)
}
}
Cookie.prototype.toString = function toString() {
var str=[this.name+"="+this.value];
if(this.expiration_date !== Infinity) {
str.push("expires="+(new Date(this.expiration_date)).toGMTString());
}
if(this.domain) {
str.push("domain="+this.domain);
}
if(this.path) {
str.push("path="+this.path);
}
if(this.secure) {
str.push("secure");
}
if(this.noscript) {
str.push("httponly");
}
return str.join("; ");
}
Cookie.prototype.toValueString = function toValueString() {
return this.name+"="+this.value;
}
var cookie_str_splitter=/[:](?=\s*[a-zA-Z0-9_\-]+\s*[=])/g
Cookie.prototype.parse = function parse(str) {
if(this instanceof Cookie) {
var parts=str.split(";")
, pair=parts[0].match(/([^=]+)=((?:.|\n)*)/)
, key=pair[1]
, value=pair[2];
this.name = key;
this.value = value;
for(var i=1;i<parts.length;i++) {
pair=parts[i].match(/([^=]+)(?:=((?:.|\n)*))?/)
, key=pair[1].trim().toLowerCase()
, value=pair[2];
switch(key) {
case "httponly":
this.noscript = true;
break;
case "expires":
this.expiration_date = value
? Number(Date.parse(value))
: Infinity;
break;
case "path":
this.path = value
? value.trim()
: "";
break;
case "domain":
this.domain = value
? value.trim()
: "";
break;
case "secure":
this.secure = true;
break
}
}
return this;
}
return new Cookie().parse(str)
}
Cookie.prototype.matches = function matches(access_info) {
if(this.noscript && access_info.script
|| this.secure && !access_info.secure
|| !this.collidesWith(access_info)) {
return false
}
return true;
}
Cookie.prototype.collidesWith = function collidesWith(access_info) {
if((this.path && !access_info.path) || (this.domain && !access_info.domain)) {
return false
}
if(this.path && access_info.path.indexOf(this.path) !== 0) {
return false;
}
if (this.domain===access_info.domain) {
return true;
}
else if(this.domain && this.domain.charAt(0)===".")
{
var wildcard=access_info.domain.indexOf(this.domain.slice(1))
if(wildcard===-1 || wildcard!==access_info.domain.length-this.domain.length+1) {
return false;
}
}
else if(this.domain){
return false
}
return true;
}
exports.CookieJar=CookieJar=function CookieJar() {
if(this instanceof CookieJar) {
var cookies = {} //name: [Cookie]
this.setCookie = function setCookie(cookie) {
cookie = Cookie(cookie);
//Delete the cookie if the set is past the current time
var remove = cookie.expiration_date <= Date.now();
if(cookie.name in cookies) {
var cookies_list = cookies[cookie.name];
for(var i=0;i<cookies_list.length;i++) {
var collidable_cookie = cookies_list[i];
if(collidable_cookie.collidesWith(cookie)) {
if(remove) {
cookies_list.splice(i,1);
if(cookies_list.length===0) {
delete cookies[cookie.name]
}
return false;
}
else {
return cookies_list[i]=cookie;
}
}
}
if(remove) {
return false;
}
cookies_list.push(cookie);
return cookie;
}
else if(remove){
return false;
}
else {
return cookies[cookie.name]=[cookie];
}
}
//returns a cookie
this.getCookie = function getCookie(cookie_name,access_info) {
var cookies_list = cookies[cookie_name];
for(var i=0;i<cookies_list.length;i++) {
var cookie = cookies_list[i];
if(cookie.expiration_date <= Date.now()) {
if(cookies_list.length===0) {
delete cookies[cookie.name]
}
continue;
}
if(cookie.matches(access_info)) {
return cookie;
}
}
}
//returns a list of cookies
this.getCookies = function getCookies(access_info) {
var matches=[];
for(var cookie_name in cookies) {
var cookie=this.getCookie(cookie_name,access_info);
if (cookie) {
matches.push(cookie);
}
}
matches.toString=function toString(){return matches.join(":");}
matches.toValueString=function() {return matches.map(function(c){return c.toValueString();}).join(';');}
return matches;
}
return this;
}
return new CookieJar()
}
//returns list of cookies that were set correctly
CookieJar.prototype.setCookies = function setCookies(cookies) {
cookies=Array.isArray(cookies)
?cookies
:cookies.split(cookie_str_splitter);
var successful=[]
for(var i=0;i<cookies.length;i++) {
var cookie = Cookie(cookies[i]);
if(this.setCookie(cookie)) {
successful.push(cookie);
}
}
return successful;
}
});
require.define("/shred/request.js", function (require, module, exports, __dirname, __filename) {
// The request object encapsulates a request, creating a Node.js HTTP request and
// then handling the response.
var HTTP = require("http")
, HTTPS = require("https")
, parseUri = require("./parseUri")
, Emitter = require('events').EventEmitter
, sprintf = require("sprintf").sprintf
, Response = require("./response")
, HeaderMixins = require("./mixins/headers")
, Content = require("./content")
;
var STATUS_CODES = HTTP.STATUS_CODES || {
100 : 'Continue',
101 : 'Switching Protocols',
102 : 'Processing', // RFC 2518, obsoleted by RFC 4918
200 : 'OK',
201 : 'Created',
202 : 'Accepted',
203 : 'Non-Authoritative Information',
204 : 'No Content',
205 : 'Reset Content',
206 : 'Partial Content',
207 : 'Multi-Status', // RFC 4918
300 : 'Multiple Choices',
301 : 'Moved Permanently',
302 : 'Moved Temporarily',
303 : 'See Other',
304 : 'Not Modified',
305 : 'Use Proxy',
307 : 'Temporary Redirect',
400 : 'Bad Request',
401 : 'Unauthorized',
402 : 'Payment Required',
403 : 'Forbidden',
404 : 'Not Found',
405 : 'Method Not Allowed',
406 : 'Not Acceptable',
407 : 'Proxy Authentication Required',
408 : 'Request Time-out',
409 : 'Conflict',
410 : 'Gone',
411 : 'Length Required',
412 : 'Precondition Failed',
413 : 'Request Entity Too Large',
414 : 'Request-URI Too Large',
415 : 'Unsupported Media Type',
416 : 'Requested Range Not Satisfiable',
417 : 'Expectation Failed',
418 : 'I\'m a teapot', // RFC 2324
422 : 'Unprocessable Entity', // RFC 4918
423 : 'Locked', // RFC 4918
424 : 'Failed Dependency', // RFC 4918
425 : 'Unordered Collection', // RFC 4918
426 : 'Upgrade Required', // RFC 2817
500 : 'Internal Server Error',
501 : 'Not Implemented',
502 : 'Bad Gateway',
503 : 'Service Unavailable',
504 : 'Gateway Time-out',
505 : 'HTTP Version not supported',
506 : 'Variant Also Negotiates', // RFC 2295
507 : 'Insufficient Storage', // RFC 4918
509 : 'Bandwidth Limit Exceeded',
510 : 'Not Extended' // RFC 2774
};
// The Shred object itself constructs the `Request` object. You should rarely
// need to do this directly.
var Request = function(options) {
this.log = options.logger;
this.cookieJar = options.cookieJar;
this.encoding = options.encoding;
this.logCurl = options.logCurl;
processOptions(this,options||{});
createRequest(this);
};
// A `Request` has a number of properties, many of which help with details like
// URL parsing or defaulting the port for the request.
Object.defineProperties(Request.prototype, {
// - **url**. You can set the `url` property with a valid URL string and all the
// URL-related properties (host, port, etc.) will be automatically set on the
// request object.
url: {
get: function() {
if (!this.scheme) { return null; }
return sprintf("%s://%s:%s%s",
this.scheme, this.host, this.port,
(this.proxy ? "/" : this.path) +
(this.query ? ("?" + this.query) : ""));
},
set: function(_url) {
_url = parseUri(_url);
this.scheme = _url.protocol;
this.host = _url.host;
this.port = _url.port;
this.path = _url.path;
this.query = _url.query;
return this;
},
enumerable: true
},
// - **headers**. Returns a hash representing the request headers. You can't set
// this directly, only get it. You can add or modify headers by using the
// `setHeader` or `setHeaders` method. This ensures that the headers are
// normalized - that is, you don't accidentally send `Content-Type` and
// `content-type` headers. Keep in mind that if you modify the returned hash,
// it will *not* modify the request headers.
headers: {
get: function() {
return this.getHeaders();
},
enumerable: true
},
// - **port**. Unless you set the `port` explicitly or include it in the URL, it
// will default based on the scheme.
port: {
get: function() {
if (!this._port) {
switch(this.scheme) {
case "https": return this._port = 443;
case "http":
default: return this._port = 80;
}
}
return this._port;
},
set: function(value) { this._port = value; return this; },
enumerable: true
},
// - **method**. The request method - `get`, `put`, `post`, etc. that will be
// used to make the request. Defaults to `get`.
method: {
get: function() {
return this._method = (this._method||"GET");
},
set: function(value) {
this._method = value; return this;
},
enumerable: true
},
// - **query**. Can be set either with a query string or a hash (object). Get
// will always return a properly escaped query string or null if there is no
// query component for the request.
query: {
get: function() {return this._query;},
set: function(value) {
var stringify = function (hash) {
var query = "";
for (var key in hash) {
query += encodeURIComponent(key) + '=' + encodeURIComponent(hash[key]) + '&';
}
// Remove the last '&'
query = query.slice(0, -1);
return query;
}
if (value) {
if (typeof value === 'object') {
value = stringify(value);
}
this._query = value;
} else {
this._query = "";
}
return this;
},
enumerable: true
},
// - **parameters**. This will return the query parameters in the form of a hash
// (object).
parameters: {
get: function() { return QueryString.parse(this._query||""); },
enumerable: true
},
// - **content**. (Aliased as `body`.) Set this to add a content entity to the
// request. Attempts to use the `content-type` header to determine what to do
// with the content value. Get this to get back a [`Content`
// object](./content.html).
body: {
get: function() { return this._body; },
set: function(value) {
this._body = new Content({
data: value,
type: this.getHeader("Content-Type")
});
this.setHeader("Content-Type",this.content.type);
this.setHeader("Content-Length",this.content.length);
return this;
},
enumerable: true
},
// - **timeout**. Used to determine how long to wait for a response. Does not
// distinguish between connect timeouts versus request timeouts. Set either in
// milliseconds or with an object with temporal attributes (hours, minutes,
// seconds) and convert it into milliseconds. Get will always return
// milliseconds.
timeout: {
get: function() { return this._timeout; }, // in milliseconds
set: function(timeout) {
var request = this
, milliseconds = 0;
;
if (!timeout) return this;
if (typeof timeout==="number") { milliseconds = timeout; }
else {
milliseconds = (timeout.milliseconds||0) +
(1000 * ((timeout.seconds||0) +
(60 * ((timeout.minutes||0) +
(60 * (timeout.hours||0))))));
}
this._timeout = milliseconds;
return this;
},
enumerable: true
},
// - **sslStrict***. Used to disable to auth check for ssl certificataes,
// set to true to use self signed certs
sslStrict: {
get: function() { return this._sslStrict; },
set: function(sslStrict) {
if(typeof(sslStrict) !== 'boolean')
return this;
this._sslStrict = sslStrict;
return this;
},
enumerable: true
}
});
// Alias `body` property to `content`. Since the [content object](./content.html)
// has a `body` attribute, it's preferable to use `content` since you can then
// access the raw content data using `content.body`.
Object.defineProperty(Request.prototype,"content",
Object.getOwnPropertyDescriptor(Request.prototype, "body"));
// The `Request` object can be pretty overwhelming to view using the built-in
// Node.js inspect method. We want to make it a bit more manageable. This
// probably goes [too far in the other
// direction](https://github.com/spire-io/shred/issues/2).
Request.prototype.inspect = function () {
var request = this;
var headers = this.format_headers();
var summary = ["<Shred Request> ", request.method.toUpperCase(),
request.url].join(" ")
return [ summary, "- Headers:", headers].join("\n");
};
Request.prototype.format_headers = function () {
var array = []
var headers = this._headers
for (var key in headers) {
if (headers.hasOwnProperty(key)) {
var value = headers[key]
array.push("\t" + key + ": " + value);
}
}
return array.join("\n");
};
// Allow chainable 'on's: shred.get({ ... }).on( ... ). You can pass in a
// single function, a pair (event, function), or a hash:
// { event: function, event: function }
Request.prototype.on = function (eventOrHash, listener) {
var emitter = this.emitter;
// Pass in a single argument as a function then make it the default response handler
if (arguments.length === 1 && typeof(eventOrHash) === 'function') {
emitter.on('response', eventOrHash);
} else if (arguments.length === 1 && typeof(eventOrHash) === 'object') {
for (var key in eventOrHash) {
if (eventOrHash.hasOwnProperty(key)) {
emitter.on(key, eventOrHash[key]);
}
}
} else {
emitter.on(eventOrHash, listener);
}
return this;
};
// Add in the header methods. Again, these ensure we don't get the same header
// multiple times with different case conventions.
HeaderMixins.gettersAndSetters(Request);
// `processOptions` is called from the constructor to handle all the work
// associated with making sure we do our best to ensure we have a valid request.
var processOptions = function(request,options) {
request.log.debug("Processing request options ..");
// We'll use `request.emitter` to manage the `on` event handlers.
request.emitter = (new Emitter);
request.agent = options.agent;
// Set up the handlers ...
if (options.on) {
for (var key in options.on) {
if (options.on.hasOwnProperty(key)) {
request.emitter.on(key, options.on[key]);
}
}
}
// Make sure we were give a URL or a host
if (!options.url && !options.host) {
request.emitter.emit("request_error",
new Error("No url or url options (host, port, etc.)"));
return;
}
// Allow for the [use of a proxy](http://www.jmarshall.com/easy/http/#proxies).
if (options.url) {
if (options.proxy) {
request.url = options.proxy;
request.path = options.url;
} else {
request.url = options.url;
}
}
// Set the remaining options.
request.query = options.query||options.parameters||request.query ;
request.method = options.method;
// FIXME: options.agent is supposed to be a Node http.Agent, not the
// User-Agent string.
request.setHeader("user-agent",options.agent||"Shred");
request.setHeaders(options.headers);
if (request.cookieJar) {
var cookies = request.cookieJar.getCookies( CookieAccessInfo( request.host, request.path ) );
if (cookies.length) {
var cookieString = request.getHeader('cookie')||'';
for (var cookieIndex = 0; cookieIndex < cookies.length; ++cookieIndex) {
if ( cookieString.length && cookieString[ cookieString.length - 1 ] != ';' )
{
cookieString += ';';
}
cookieString += cookies[ cookieIndex ].name + '=' + cookies[ cookieIndex ].value + ';';
}
request.setHeader("cookie", cookieString);
}
}
// The content entity can be set either using the `body` or `content` attributes.
if (options.body||options.content) {
request.content = options.body||options.content;
}
request.timeout = options.timeout;
request.sslStrict = true;
if(typeof(options.sslStrict) !== undefined){
request.sslStrict = options.sslStrict;
}
};
// `createRequest` is also called by the constructor, after `processOptions`.
// This actually makes the request and processes the response, so `createRequest`
// is a bit of a misnomer.
var createRequest = function(request) {
var timeoutId ;
request.log.debug("Creating request ..");
request.log.debug(request);
var reqParams = {
host: request.host,
port: request.port,
method: request.method,
path: request.path + (request.query ? '?'+request.query : ""),
headers: request.getHeaders(),
rejectUnauthorized: request._sslStrict,
// Node's HTTP/S modules will ignore this, but we are using the
// browserify-http module in the browser for both HTTP and HTTPS, and this
// is how you differentiate the two.
scheme: request.scheme,
// Use a provided agent. 'Undefined' is the default, which uses a global
// agent.
agent: request.agent
};
if (request.logCurl) {
logCurl(request);
}
var http = request.scheme == "http" ? HTTP : HTTPS;
// Set up the real request using the selected library. The request won't be
// sent until we call `.end()`.
request._raw = http.request(reqParams, function(response) {
// The "cleanup" event signifies that any timeout or error handlers
// that have been set for this request should now be disposed of.
request.emitter.emit("cleanup");
request.log.debug("Received response ..");
// We haven't timed out and we have a response, so make sure we clear the
// timeout so it doesn't fire while we're processing the response.
clearTimeout(timeoutId);
// Construct a Shred `Response` object from the response. This will stream
// the response, thus the need for the callback. We can access the response
// entity safely once we're in the callback.
response = new Response(response, request, function(response) {
// Set up some event magic. The precedence is given first to
// status-specific handlers, then to responses for a given event, and then
// finally to the more general `response` handler. In the last case, we
// need to first make sure we're not dealing with a a redirect.
var emit = function(event) {
var emitter = request.emitter;
var textStatus = STATUS_CODES[response.status] ? STATUS_CODES[response.status].toLowerCase() : null;
if (emitter.listeners(response.status).length > 0 || emitter.listeners(textStatus).length > 0) {
emitter.emit(response.status, response);
emitter.emit(textStatus, response);
} else {
if (emitter.listeners(event).length>0) {
emitter.emit(event, response);
} else if (!response.isRedirect) {
emitter.emit("response", response);
//console.warn("Request has no event listener for status code " + response.status);
}
}
};
// Next, check for a redirect. We simply repeat the request with the URL
// given in the `Location` header. We fire a `redirect` event.
if (response.isRedirect) {
request.log.debug("Redirecting to "
+ response.getHeader("Location"));
request.url = response.getHeader("Location");
emit("redirect");
createRequest(request);
// Okay, it's not a redirect. Is it an error of some kind?
} else if (response.isError) {
emit("error");
} else {
// It looks like we're good shape. Trigger the `success` event.
emit("success");
}
});
});
request._raw.setMaxListeners( 30 ); // avoid warnings
// We're still setting up the request. Next, we're going to handle error cases
// where we have no response. We don't emit an error event because that event
// takes a response. We don't response handlers to have to check for a null
// value. However, we [should introduce a different event
// type](https://github.com/spire-io/shred/issues/3) for this type of error.
request._raw.on("error", function(error) {
if (!timeoutId) { request.emitter.emit("request_error", error); }
request.emitter.emit("cleanup", error);
});
request._raw.on("socket", function(socket) {
request.emitter.emit("socket", socket);
});
// TCP timeouts should also trigger the "response_error" event.
request._raw.on('socket', function () {
var timeout_handler = function () { request._raw.abort(); };
request.emitter.once("cleanup", function () {
request._raw.socket.removeListener("timeout", timeout_handler);
});
// This should trigger the "error" event on the raw request, which will
// trigger the "response_error" on the shred request.
request._raw.socket.on('timeout', timeout_handler);
});
// We're almost there. Next, we need to write the request entity to the
// underlying request object.
if (request.content) {
request.log.debug("Streaming body: '" +
request.content.body.slice(0,59) + "' ... ");
request._raw.write(request.content.body);
}
// Finally, we need to set up the timeout. We do this last so that we don't
// start the clock ticking until the last possible moment.
if (request.timeout) {
timeoutId = setTimeout(function() {
request.log.debug("Timeout fired, aborting request ...");
request._raw.abort();
request.emitter.emit("timeout", request);
}, request.timeout);
}
// The `.end()` method will cause the request to fire. Technically, it might
// have already sent the headers and body.
request.log.debug("Sending request ...");
request._raw.end();
};
// Logs the curl command for the request.
var logCurl = function (req) {
var headers = req.getHeaders();
var headerString = "";
for (var key in headers) {
headerString += '-H "' + key + ": " + headers[key] + '" ';
}
var bodyString = ""
if (req.content) {
bodyString += "-d '" + req.content.body + "' ";
}
var query = req.query ? '?' + req.query : "";
console.log("curl " +
"-X " + req.method.toUpperCase() + " " +
req.scheme + "://" + req.host + ":" + req.port + req.path + query + " " +
headerString +
bodyString
);
};
module.exports = Request;
});
require.define("http", function (require, module, exports, __dirname, __filename) {
// todo
});
require.define("https", function (require, module, exports, __dirname, __filename) {
// todo
});
require.define("/shred/parseUri.js", function (require, module, exports, __dirname, __filename) {
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
function parseUri (str) {
var o = parseUri.options,
m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
uri = {},
i = 14;
while (i--) uri[o.key[i]] = m[i] || "";
uri[o.q.name] = {};
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
if ($1) uri[o.q.name][$1] = $2;
});
return uri;
};
parseUri.options = {
strictMode: false,
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};
module.exports = parseUri;
});
require.define("events", function (require, module, exports, __dirname, __filename) {
if (!process.EventEmitter) process.EventEmitter = function () {};
var EventEmitter = exports.EventEmitter = process.EventEmitter;
var isArray = typeof Array.isArray === 'function'
? Array.isArray
: function (xs) {
return Object.toString.call(xs) === '[object Array]'
}
;
// By default EventEmitters will print a warning if more than
// 10 listeners are added to it. This is a useful default which
// helps finding memory leaks.
//
// Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited.
var defaultMaxListeners = 10;
EventEmitter.prototype.setMaxListeners = function(n) {
if (!this._events) this._events = {};
this._events.maxListeners = n;
};
EventEmitter.prototype.emit = function(type) {
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events || !this._events.error ||
(isArray(this._events.error) && !this._events.error.length))
{
if (arguments[1] instanceof Error) {
throw arguments[1]; // Unhandled 'error' event
} else {
throw new Error("Uncaught, unspecified 'error' event.");
}
return false;
}
}
if (!this._events) return false;
var handler = this._events[type];
if (!handler) return false;
if (typeof handler == 'function') {
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
var args = Array.prototype.slice.call(arguments, 1);
handler.apply(this, args);
}
return true;
} else if (isArray(handler)) {
var args = Array.prototype.slice.call(arguments, 1);
var listeners = handler.slice();
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i].apply(this, args);
}
return true;
} else {
return false;
}
};
// EventEmitter is defined in src/node_events.cc
// EventEmitter.prototype.emit() is also defined there.
EventEmitter.prototype.addListener = function(type, listener) {
if ('function' !== typeof listener) {
throw new Error('addListener only takes instances of Function');
}
if (!this._events) this._events = {};
// To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners".
this.emit('newListener', type, listener);
if (!this._events[type]) {
// Optimize the case of one listener. Don't need the extra array object.
this._events[type] = listener;
} else if (isArray(this._events[type])) {
// Check for listener leak
if (!this._events[type].warned) {
var m;
if (this._events.maxListeners !== undefined) {
m = this._events.maxListeners;
} else {
m = defaultMaxListeners;
}
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +
'leak detected. %d listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.',
this._events[type].length);
console.trace();
}
}
// If we've already got an array, just append.
this._events[type].push(listener);
} else {
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
}
return this;
};
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
EventEmitter.prototype.once = function(type, listener) {
var self = this;
self.on(type, function g() {
self.removeListener(type, g);
listener.apply(this, arguments);
});
return this;
};
EventEmitter.prototype.removeListener = function(type, listener) {
if ('function' !== typeof listener) {
throw new Error('removeListener only takes instances of Function');
}
// does not use listeners(), so no side effect of creating _events[type]
if (!this._events || !this._events[type]) return this;
var list = this._events[type];
if (isArray(list)) {
var i = list.indexOf(listener);
if (i < 0) return this;
list.splice(i, 1);
if (list.length == 0)
delete this._events[type];
} else if (this._events[type] === listener) {
delete this._events[type];
}
return this;
};
EventEmitter.prototype.removeAllListeners = function(type) {
// does not use listeners(), so no side effect of creating _events[type]
if (type && this._events && this._events[type]) this._events[type] = null;
return this;
};
EventEmitter.prototype.listeners = function(type) {
if (!this._events) this._events = {};
if (!this._events[type]) this._events[type] = [];
if (!isArray(this._events[type])) {
this._events[type] = [this._events[type]];
}
return this._events[type];
};
});
require.define("/node_modules/sprintf/package.json", function (require, module, exports, __dirname, __filename) {
module.exports = {"main":"./lib/sprintf"}
});
require.define("/node_modules/sprintf/lib/sprintf.js", function (require, module, exports, __dirname, __filename) {
/**
sprintf() for JavaScript 0.7-beta1
http://www.diveintojavascript.com/projects/javascript-sprintf
Copyright (c) Alexandru Marasteanu <alexaholic [at) gmail (dot] com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of sprintf() for JavaScript nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Alexandru Marasteanu BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Changelog:
2010.11.07 - 0.7-beta1-node
- converted it to a node.js compatible module
2010.09.06 - 0.7-beta1
- features: vsprintf, support for named placeholders
- enhancements: format cache, reduced global namespace pollution
2010.05.22 - 0.6:
- reverted to 0.4 and fixed the bug regarding the sign of the number 0
Note:
Thanks to Raphael Pigulla <raph (at] n3rd [dot) org> (http://www.n3rd.org/)
who warned me about a bug in 0.5, I discovered that the last update was
a regress. I appologize for that.
2010.05.09 - 0.5:
- bug fix: 0 is now preceeded with a + sign
- bug fix: the sign was not at the right position on padded results (Kamal Abdali)
- switched from GPL to BSD license
2007.10.21 - 0.4:
- unit test and patch (David Baird)
2007.09.17 - 0.3:
- bug fix: no longer throws exception on empty paramenters (Hans Pufal)
2007.09.11 - 0.2:
- feature: added argument swapping
2007.04.03 - 0.1:
- initial release
**/
var sprintf = (function() {
function get_type(variable) {
return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase();
}
function str_repeat(input, multiplier) {
for (var output = []; multiplier > 0; output[--multiplier] = input) {/* do nothing */}
return output.join('');
}
var str_format = function() {
if (!str_format.cache.hasOwnProperty(arguments[0])) {
str_format.cache[arguments[0]] = str_format.parse(arguments[0]);
}
return str_format.format.call(null, str_format.cache[arguments[0]], arguments);
};
str_format.format = function(parse_tree, argv) {
var cursor = 1, tree_length = parse_tree.length, node_type = '', arg, output = [], i, k, match, pad, pad_character, pad_length;
for (i = 0; i < tree_length; i++) {
node_type = get_type(parse_tree[i]);
if (node_type === 'string') {
output.push(parse_tree[i]);
}
else if (node_type === 'array') {
match = parse_tree[i]; // convenience purposes only
if (match[2]) { // keyword argument
arg = argv[cursor];
for (k = 0; k < match[2]