-
Notifications
You must be signed in to change notification settings - Fork 1
/
index_bk.js
183 lines (153 loc) · 4.42 KB
/
index_bk.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
console.log("Dapp loading process pid " + process.pid)
// require("longjohn");
var async = require("async");
var path = require("path");
var ZSchema = require("z-schema");
var extend = require("extend");
var util = require("util");
var modules = {};
var ready = false;
process.on("uncaughtException", function (err) {
console.log("Dapp system error", {message: err.message, stack: err.stack});
});
var d = require("domain").create();
d.on("error", function (err) {
console.log("Domain master", {message: err.message, stack: err.stack});
});
d.run(function () {
async.auto({
sandbox: function (cb) {
cb(null, process.binding("sandbox"));
},
logger: function (cb) {
cb(null, console.log);
},
config: function (cb) {
cb(null, require("./config.json"));
},
scheme: ["logger", function (cb, scope) {
try {
var db = require("./blockchain.json");
} catch (e) {
scope.logger("Failed to load blockchain.json");
}
var fields = [],
aliasedFields = [],
types = {},
selector = {};
function getType(type) {
var nativeType;
switch (type) {
case "BigInt":
nativeType = Number;
break;
default:
nativeType = String;
}
return nativeType;
}
var i, n, __field, __alias, __type;
for (i = 0; i < db.length; i++) {
for (n = 0; n < db[i].tableFields.length; n++) {
__field = db[i].alias + "." + db[i].tableFields[n].name;;
__alias = db[i].alias + "_" + db[i].tableFields[n].name;
__type = db[i].tableFields[n].type;
fields.push(__field);
aliasedFields.push({ field: __field, alias: __alias });
types[__alias] = getType(__type);
}
selector[db[i].table] = extend(db[i], {tableFields: undefined});
}
cb(null, {scheme: db, fields: fields, aliasedFields: aliasedFields, types: types, selector: selector});
}],
validator: function (cb) {
ZSchema.registerFormat("publicKey", function (value) {
try {
var b = new Buffer(value, "hex");
return b.length == 32;
} catch (e) {
return false;
}
});
ZSchema.registerFormat("signature", function (value) {
try {
var b = new Buffer(value, "hex");
return b.length == 64;
} catch (e) {
return false;
}
});
ZSchema.registerFormat("hex", function (value) {
try {
new Buffer(value, "hex");
} catch (e) {
return false;
}
return true;
});
var validator = new ZSchema();
cb(null, validator);
},
bus: function (cb) {
var changeCase = require("change-case");
var bus = function () {
this.message = function () {
if (ready) {
var args = [];
Array.prototype.push.apply(args, arguments);
var topic = args.shift();
Object.keys(modules).forEach(function (namespace) {
Object.keys(modules[namespace]).forEach(function (moduleName) {
var eventName = "on" + changeCase.pascalCase(topic);
if (typeof(modules[namespace][moduleName][eventName]) == "function") {
modules[namespace][moduleName][eventName].apply(modules[namespace][moduleName][eventName], args);
}
});
});
}
}
}
cb(null, new bus)
},
sequence: function (cb) {
var Sequence = require("./modules/helpers/sequence.js");
var sequence = new Sequence({
onWarning: function(current, limit){
scope.logger.warn("Main queue", current)
}
});
cb(null, sequence);
},
modules: ["sandbox", "config", "logger", "bus", "sequence", function (cb, scope) {
var module = path.join(__dirname, process.argv[3] || "modules.full.json");
var lib = require(module);
var tasks = [];
Object.keys(lib).forEach(function (path) {
var raw = path.split("/");
var namespace = raw[0];
var moduleName = raw[1];
tasks.push(function (cb) {
var d = require("domain").create();
d.on("error", function (err) {
scope.logger("Domain " + moduleName, {message: err.message, stack: err.stack});
});
d.run(function () {
var library = require(lib[path]);
var obj = new library(cb, scope);
modules[namespace] = modules[namespace] || {};
modules[namespace][moduleName] = obj;
});
});
})
async.parallel(tasks, function (err) {
async.setImmediate(cb, err, modules);
});
}],
ready: ["modules", "bus", "logger", function (cb, scope) {
ready = true;
scope.bus.message("bind", scope.modules);
scope.logger("Dapp loaded process pid " + process.pid)
cb();
}]
});
});