1 | var async = require("async");
|
2 | var ZSchema = require("z-schema");
|
3 | var extend = require("extend");
|
4 | var changeCase = require("change-case");
|
5 |
|
6 | var modules = {};
|
7 | var ready = false;
|
8 |
|
9 | module.exports = function (options, cb) {
|
10 | async.auto({
|
11 | sandbox: function (cb) {
|
12 | cb(null, options.sandbox);
|
13 | },
|
14 |
|
15 | logger: function (cb) {
|
16 | cb(null, console.log);
|
17 | },
|
18 |
|
19 | config: function (cb) {
|
20 | cb(null, require("./config.json"));
|
21 | },
|
22 |
|
23 | scheme: ["logger", function (cb, scope) {
|
24 | try {
|
25 | var db = require("./blockchain.json");
|
26 | } catch (e) {
|
27 | scope.logger("Failed to load blockchain.json");
|
28 | }
|
29 |
|
30 | var fields = [],
|
31 | aliasedFields = [],
|
32 | types = {},
|
33 | selector = {};
|
34 |
|
35 | function getType(type) {
|
36 | var nativeType;
|
37 |
|
38 | switch (type) {
|
39 | case "BigInt":
|
40 | nativeType = Number;
|
41 | break;
|
42 | default:
|
43 | nativeType = String;
|
44 | }
|
45 |
|
46 | return nativeType;
|
47 | }
|
48 |
|
49 | var i, n, __field, __alias, __type;
|
50 |
|
51 | for (i = 0; i < db.length; i++) {
|
52 | for (n = 0; n < db[i].tableFields.length; n++) {
|
53 | __field = db[i].alias + "." + db[i].tableFields[n].name;;
|
54 | __alias = db[i].alias + "_" + db[i].tableFields[n].name;
|
55 | __type = db[i].tableFields[n].type;
|
56 |
|
57 | fields.push(__field);
|
58 | aliasedFields.push({ field: __field, alias: __alias });
|
59 | types[__alias] = getType(__type);
|
60 | }
|
61 |
|
62 | selector[db[i].table] = extend(db[i], {tableFields: undefined});
|
63 | }
|
64 |
|
65 | cb(null, {scheme: db, fields: fields, aliasedFields: aliasedFields, types: types, selector: selector});
|
66 | }],
|
67 |
|
68 | validator: function (cb) {
|
69 | ZSchema.registerFormat("publicKey", function (value) {
|
70 | try {
|
71 | var b = new Buffer(value, "hex");
|
72 | return b.length == 32;
|
73 | } catch (e) {
|
74 | return false;
|
75 | }
|
76 | });
|
77 |
|
78 | ZSchema.registerFormat("signature", function (value) {
|
79 | try {
|
80 | var b = new Buffer(value, "hex");
|
81 | return b.length == 64;
|
82 | } catch (e) {
|
83 | return false;
|
84 | }
|
85 | });
|
86 |
|
87 | ZSchema.registerFormat("hex", function (value) {
|
88 | try {
|
89 | new Buffer(value, "hex");
|
90 | } catch (e) {
|
91 | return false;
|
92 | }
|
93 |
|
94 | return true;
|
95 | });
|
96 |
|
97 | var validator = new ZSchema();
|
98 | cb(null, validator);
|
99 | },
|
100 |
|
101 | bus: function (cb) {
|
102 | var bus = function () {
|
103 | this.message = function () {
|
104 | if (ready) {
|
105 | var args = [];
|
106 | Array.prototype.push.apply(args, arguments);
|
107 | var topic = args.shift();
|
108 | Object.keys(modules).forEach(function (namespace) {
|
109 | Object.keys(modules[namespace]).forEach(function (moduleName) {
|
110 | var eventName = "on" + changeCase.pascalCase(topic);
|
111 | if (typeof(modules[namespace][moduleName][eventName]) == "function") {
|
112 | modules[namespace][moduleName][eventName].apply(modules[namespace][moduleName][eventName], args);
|
113 | }
|
114 | });
|
115 | });
|
116 | }
|
117 | }
|
118 | }
|
119 | cb(null, new bus)
|
120 | },
|
121 |
|
122 | sequence: function (cb) {
|
123 | var Sequence = require("./modules/helpers/sequence.js");
|
124 | var sequence = new Sequence({
|
125 | onWarning: function(current, limit){
|
126 | scope.logger.warn("Main queue", current)
|
127 | }
|
128 | });
|
129 | cb(null, sequence);
|
130 | },
|
131 |
|
132 | modules: ["sandbox", "config", "logger", "bus", "sequence", function (cb, scope) {
|
133 | var lib = require("./modules.full.json");
|
134 | var tasks = [];
|
135 | Object.keys(lib).forEach(function (path) {
|
136 | var raw = path.split("/");
|
137 | var namespace = raw[0];
|
138 | var moduleName = raw[1];
|
139 | tasks.push(function (cb) {
|
140 | var library = require(lib[path]);
|
141 | var obj = new library(cb, scope);
|
142 | modules[namespace] = modules[namespace] || {};
|
143 | modules[namespace][moduleName] = obj;
|
144 | });
|
145 | })
|
146 |
|
147 | async.series(tasks, function (err) {
|
148 | cb(err, modules);
|
149 | });
|
150 | }],
|
151 |
|
152 | ready: ["modules", "bus", "logger", function (cb, scope) {
|
153 | ready = true;
|
154 | scope.bus.message("bind", scope.modules);
|
155 | cb();
|
156 | }]
|
157 | }, cb);
|
158 | }
|