Name: js-handler/node_modules/restify/lib/formatters/jsonp.js 
1:
// Copyright 2012 Mark Cavage, Inc.  All rights reserved.
2:
 
3:
 
4:
 
5:
///--- Exports
6:
 
7:
function formatJSONP(req, res, body) {
8:
        if (!body) {
9:
                res.setHeader('Content-Length', 0);
10:
                return (null);
11:
        }
12:
 
13:
        if (body instanceof Error) {
14:
                if ((body.restCode || body.httpCode) && body.body) {
15:
                        body = body.body;
16:
                } else {
17:
                        body = {
18:
                                message: body.message
19:
                        };
20:
                }
21:
        }
22:
 
23:
        if (Buffer.isBuffer(body))
24:
                body = body.toString('base64');
25:
 
26:
        var cb = req.query.callback || req.query.jsonp;
27:
        var data;
28:
        if (cb) {
29:
                data = cb + '(' + JSON.stringify(body) + ');';
30:
        } else {
31:
                data = JSON.stringify(body);
32:
        }
33:
 
34:
        res.setHeader('Content-Length', Buffer.byteLength(data));
35:
        return (data);
36:
}
37:
 
38:
module.exports = formatJSONP;