1 | var Gun = require('../gun');
|
2 | module.exports = function(req, cb){
|
3 | if(!req.url || !req.url.query || !req.url.query.jsonp){ return cb }
|
4 | cb.jsonp = req.url.query.jsonp;
|
5 | delete req.url.query.jsonp;
|
6 | Gun.obj.map(Gun.obj.ify(req.url.query['`']), function(val, i){
|
7 | req.headers[i] = val;
|
8 | });
|
9 | delete req.url.query['`'];
|
10 | if(req.url.query.$){
|
11 | req.body = req.url.query.$;
|
12 | if(!Gun.obj.has(req.url.query, '^') || 'json' == req.url.query['^']){
|
13 | req.body = Gun.obj.ify(req.body);
|
14 | }
|
15 | }
|
16 | delete req.url.query.$;
|
17 | delete req.url.query['^'];
|
18 | delete req.url.query['%'];
|
19 | var reply = {headers:{}};
|
20 | return function(res){
|
21 | if(!res){ return }
|
22 | if(res.headers){
|
23 | Gun.obj.map(res.headers, function(val, field){
|
24 | reply.headers[field] = val;
|
25 | });
|
26 | }
|
27 | reply.headers['Content-Type'] = "text/javascript";
|
28 | if(Gun.obj.has(res,'chunk') && (!reply.body || Gun.list.is(reply.chunks))){
|
29 | (reply.chunks = reply.chunks || []).push(res.chunk);
|
30 | }
|
31 | if(Gun.obj.has(res,'body')){
|
32 | reply.body = res.body;
|
33 | reply.body = ';'+ cb.jsonp + '(' + Gun.text.ify(reply) + ');';
|
34 | cb(reply);
|
35 | }
|
36 | }
|
37 | }
|