-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-lib.js
112 lines (98 loc) · 3.47 KB
/
client-lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var Resource = require('deployd/lib/resource')
, util = require('util')
, path = require('path')
, fs = require('fs')
, async = require('async');
function ClientLib(name, options) {
Resource.apply(this, arguments);
}
util.inherits(ClientLib, Resource);
ClientLib.selfHost = function(options) {
return new ClientLib('dpd.js', options);
}
ClientLib.prototype.load = function(fn) {
var resource = this;
async.parallel({
dpdJs: function(callback) {
fs.readFile(path.join(__dirname, './clib/dpd.js'), 'utf-8', callback);
},
socketIo: function(callback) {
fs.readFile(path.join(__dirname, './clib/socket.io.min.js'), 'utf-8', callback);
},
promise: function(callback) {
fs.readFile(path.join(__dirname, './clib/ayepromise.min.js'), 'utf-8', callback);
},
ajax: function(callback) {
fs.readFile(path.join(__dirname, './clib/ajax.js'), 'utf-8', callback);
}
}, function(err, results) {
if (err) return fn(err);
var file = results.socketIo + "\n\n"
+ results.promise + "\n\n"
+ results.ajax + "\n\n"
+ results.dpdJs;
resource.clientLib = file;
fn();
});
};
ClientLib.prototype.handle = function (ctx, next) {
var resource = this;
if (ctx.url === '/') {
ctx.res.setHeader('Content-Type', 'text/javascript');
var lib = resource.clientLib;
ctx.res.write(lib);
resource.generate(ctx.res, function() {
ctx.res.end();
});
} else {
next();
}
};
ClientLib.prototype.generate = function(res, fn) {
var clientLib = this;
res.write('\n\n// automatically generated code\n\n');
this.config.resources.forEach(function(r) {
clientLib.generateResource(r, res);
});
fn();
};
ClientLib.prototype.generateResource = function(r, res) {
var jsName = r.path.replace(/[^A-Za-z0-9]/g, '')
, i;
if (r.clientGeneration && jsName) {
res.write('dpd.' + jsName + ' = dpd("' + r.path + '");\n');
if (r.clientGenerationExec) {
for (i = 0; i < r.clientGenerationExec.length; i++) {
res.write('dpd.' + jsName + '.' + r.clientGenerationExec[i] + ' = function(path, body, fn) {\n');
res.write(' return dpd.' + jsName + '.exec("' + r.clientGenerationExec[i] + '", path, body, fn);\n');
res.write('}\n');
}
}
if (r.clientGenerationGet) {
for (i = 0; i < r.clientGenerationGet.length; i++) {
res.write('dpd.' + jsName + '.' + r.clientGenerationGet[i] + ' = function(path, query, fn) {\n');
res.write(' return dpd.' + jsName + '.get("' + r.clientGenerationGet[i] + '", path, query, fn);\n');
res.write('}\n');
}
}
// resource event namespacing sugar
res.write('dpd.' + jsName + '.on = function(ev, fn) {\n');
res.write(' return dpd.on("' + r.path.replace('/', '') + '" + ":" + ev, fn);\n');
res.write('}\n');
res.write('dpd.' + jsName + '.once = function(ev, fn) {\n');
res.write(' return dpd.once("' + r.path.replace('/', '') + '" + ":" + ev, fn);\n');
res.write('}\n');
res.write('dpd.' + jsName + '.off = function(ev, fn) {\n');
res.write(' return dpd.off("' + r.path.replace('/', '') + '" + ":" + ev, fn);\n');
res.write('}\n');
}
if(r.external) {
Object.keys(r.external).forEach(function (name) {
res.write('dpd.' + jsName + '.' + name + ' = function (path, body, fn) {\n');
res.write(' dpd.' + jsName + '.exec("' + name + '", path, body, fn);\n');
res.write('}\n');
});
}
res.write('\n');
};
module.exports = ClientLib;