-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathservice.js
102 lines (91 loc) · 2.65 KB
/
service.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
var listener = require('../changes/listener'),
changeService = require('../changes/service'),
url = require('url'),
sys = require('sys');
var ExternalDeligation = function (baseurl) {
if (baseurl[baseurl.length - 1] != '/') {
baseurl += '/';
}
this.baseurl = baseurl;
this.modules = {};
this.changes = {};
this.test = 'asdf';
}
sys.inherits(ExternalDeligation, changeService.Deligation);
ExternalDeligation.prototype.handleDesignDoc = function (dbname, doc) {
var d = this;
if (doc.externals) {
d.modules[dname][doc._id] = {}
for (name in doc.externals) {
loadModule(doc.externals[name], dbname+'/'+doc._id+'.external.'+name)
.addCallback(function(module) {
d.modules[dname][doc._id][name](module);
})
.addErrback(function(){
sys.puts('Cannot import module from '+JSON.stringify(doc._id)+'.external.'+name);
});
}
}
}
ExternalDeligation.prototype.cleanup = function (dbname, id) {
if (this.modules[dbname] && this.modules[dbname][id]) {
delete this.modules[dbname][id];
}
}
var start = function (uri) {
var deligation = new ExternalDeligation(uri);
changeService.start(url.parse(uri), deligation);
return deligation;
}
var ejsgipath = '/Users/mikeal/Documents/git/ejsgi/lib/ejsgi';
var ejsgi = require(ejsgipath);
buffer = ''
var dataHandler = function (data, callback) {
if (data.indexOf('\n')) {
var chunks = data.split('\n');
if (buffer) {
chunks[0] = buffer + chunks[0];
buffer = null;
}
} else {
if (buffer) {
data = buffer + data;
buffer = null;
}
var chunks = [data];
}
for (i = 0; i < chunks.length; i += 1) {
var chunk = chunks[i];
if (chunk) {
try {
var obj = JSON.parse(chunk);
} catch(e) {
if (i != (chunks.length -1)) {
throw "For some reason I think this is a chunk "+chunk;
} else {
buffer = chunk;
}
}
if (obj) { callback(obj); }
}
}
}
var application = function (obj, deligation) {
// sys.puts(JSON.stringify({code:200,json:obj}))
var req = new EventEmitter();
var res = new EventEmitter();
req.headers = obj.headers;
var request new ejsgi.Request(req, res, 'localhost', 5984, null, false);
if (obj.body && obj.body != "undefined") {
req.emit("body", obj.body)
}
}
if (require.main === module && process.argv[process.argv.length - 1].startsWith('http')) {
var deligation = start(process.argv[process.argv.length - 1]);
process.stdio.addListener("data", function(data) {
dataHandler(data, function(obj) {
application(obj, deligation);
})
});
process.stdio.open();
}