Name: js-handler/node_modules/restify/lib/index.js 
1:
// Copyright 2012 Mark Cavage, Inc.  All rights reserved.
2:
//
3:
// Restify supports both a client and server API, and in the essence of not
4:
// loading the kitchen sink on clients, the exports here is chunked up into
5:
// client and server; note clients will have to opt in by setting the env
6:
// var "RESTIFY_CLIENT_ONLY", but if you're in that boat, it's not hard to do,
7:
// and enables much faster load times
8:
//
9:
 
10:
var shallowCopy = require('./utils').shallowCopy;
11:
 
12:
 
13:
function createClient(options) {
14:
        var assert = require('assert-plus');
15:
        var bunyan = require('./bunyan_helper');
16:
        var clients = require('./clients');
17:
 
18:
        assert.object(options, 'options');
19:
 
20:
        var client;
21:
        var opts = shallowCopy(options);
22:
        opts.agent = options.agent;
23:
        opts.name = opts.name || 'restify';
24:
        opts.type = opts.type || 'application/octet-stream';
25:
        opts.log = opts.log || bunyan.createLogger(opts.name);
26:
 
27:
        switch (opts.type) {
28:
        case 'json':
29:
                client = new clients.JsonClient(opts);
30:
                break;
31:
 
32:
        case 'string':
33:
                client = new clients.StringClient(opts);
34:
                break;
35:
 
36:
        case 'http':
37:
        default:
38:
                client = new clients.HttpClient(opts);
39:
                break;
40:
        }
41:
 
42:
        return (client);
43:
}
44:
 
45:
 
46:
function createJsonClient(options) {
47:
        options = options ? shallowCopy(options) : {};
48:
        options.type = 'json';
49:
        return (createClient(options));
50:
}
51:
 
52:
 
53:
function createStringClient(options) {
54:
        options = options ? shallowCopy(options) : {};
55:
        options.type = 'string';
56:
        return (createClient(options));
57:
}
58:
 
59:
 
60:
function createHttpClient(options) {
61:
        options = options ? shallowCopy(options) : {};
62:
        options.type = 'http';
63:
        return (createClient(options));
64:
}
65:
 
66:
 
67:
function createServer(options) {
68:
        var bunyan = require('./bunyan_helper');
69:
        var InternalError = require('./errors').InternalError;
70:
        var Router = require('./router');
71:
        var Server = require('./server');
72:
 
73:
        var opts = shallowCopy(options || {});
74:
        var server;
75:
 
76:
        opts.name = opts.name || 'restify';
77:
        opts.log = opts.log || bunyan.createLogger(opts.name);
78:
        opts.router = opts.router || new Router(opts);
79:
 
80:
        server = new Server(opts);
81:
        server.on('uncaughtException', function (req, res, route, e) {
82:
                if (this.listeners('uncaughtException').length > 1 ||
83:
                    res._headerSent) {
84:
                        return (false);
85:
                }
86:
 
87:
                res.send(new InternalError(e, e.message || 'unexpected error'));
88:
                return (true);
89:
        });
90:
 
91:
        return (server);
92:
}
93:
 
94:
 
95:
/**
96:
 * Returns a string representation of a URL pattern , with its
97:
 * parameters filled in by the passed hash.
98:
 *
99:
 * If a key is not found in the hash for a param, it is left alone.
100:
 *
101:
 * @param {Object} a hash of parameter names to values for substitution.
102:
 */
103:
function realizeUrl(pattern, params) {
104:
        var p = pattern.replace(/\/:([^/]+)/g, function (match, k) {
105:
                return (params.hasOwnProperty(k) ? '/' + params[k] : match);
106:
        });
107:
 
108:
 
109:
        return (require('./utils').sanitizePath(p));
110:
}
111:
 
112:
 
113:
 
114:
///--- Exports
115:
 
116:
module.exports = {
117:
        // Client API
118:
        createClient: createClient,
119:
        createJsonClient: createJsonClient,
120:
        createJSONClient: createJsonClient,
121:
        createStringClient: createStringClient,
122:
        createHttpClient: createHttpClient,
123:
        get HttpClient() {
124:
                return (require('./clients').HttpClient);
125:
        },
126:
        get JsonClient() {
127:
                return (require('./clients').JsonClient);
128:
        },
129:
        get StringClient() {
130:
                return (require('./clients').StringClient);
131:
        },
132:
 
133:
        // Miscellaneous API
134:
        get bunyan() {
135:
                return (require('./bunyan_helper'));
136:
        },
137:
 
138:
        errors: {}
139:
 
140:
};
141:
 
142:
var errors = require('./errors');
143:
Object.keys(errors).forEach(function (k) {
144:
        module.exports.errors[k] = errors[k];
145:
        module.exports[k] = errors[k];
146:
});
147:
 
148:
if (!process.env.RESTIFY_CLIENT_ONLY) {
149:
 
150:
        module.exports.createServer = createServer;
151:
        module.exports.httpDate = require('./http_date');
152:
        module.exports.realizeUrl = realizeUrl;
153:
        module.exports.formatters = require('./formatters');
154:
        module.exports.plugins = {};
155:
        var plugins = require('./plugins');
156:
        Object.keys(plugins).forEach(function (k) {
157:
                module.exports.plugins[k] = plugins[k];
158:
                module.exports[k] = plugins[k];
159:
        });
160:
}