-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
143 lines (138 loc) · 4.17 KB
/
index.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
#!/usr/bin/env node
const glob = require('glob')
const { planaria } = require("neonplanaria")
const fs = require('fs');
const path = require('path');
const stream = require('stream')
const minimist = require('minimist')
const requireStr = require('require-from-string');
// Implement log stream - must define _read() callback or errs
const log = new stream.Readable()
log._read = function() {}
const validate = function(config, vmode) {
let errors = [];
if (!config.eventchain && vmode !== "build") {
errors.push("requires an \"eventchain\": 1 key pair")
}
if (!config.name && vmode !== "build") {
errors.push("requires a \"name\" attribute")
}
if (config.q) {
let keys = Object.keys(config.q)
if (keys.length > 0) {
// keys must be either 'find' or 'project'
keys.forEach(function(key) {
if (!["find", "project"].includes(key)) {
errors.push("\"q\" currently supports only \"find\" and \"project\"")
}
})
} else {
errors.push("\"q\" should have \"find\" attribute");
}
} else {
errors.push("requires a 'q' attribute")
}
return errors;
}
const loadAndValidateConfig = async function(options) {
const configs = await new Promise(function(resolve, reject) {
if (options.strconfig) {
let config;
if (options.strconfig.startsWith("module.exports")) {
console.log("EVENTCHAIN", "Using JavaScript config")
config = requireStr(options.strconfig)
} else {
console.log("EVENTCHAIN", "Using JSON config")
config = JSON.parse(options.strconfig)
}
resolve([config])
} else if (options.fileconfig) {
console.log("EVENTCHAIN", "Loading config:", options.fileconfig)
let config = require(path.resolve(options.fileconfig))
resolve([config])
} else {
console.log("EVENTCHAIN", "Searching for config file")
glob(process.cwd() + "/*.@(js|json)", async function(err, files) {
if (err) reject(err);
let configs = files.map(function(f) {
return require(f)
}).filter(function(f) {
return f.eventchain
})
resolve(configs)
})
}
})
if (configs.length === 0) {
console.log("EVENTCHAIN", "Couldn't find a JSON file with an 'eventchain' attribute")
process.exit();
return;
}
if (configs.length > 1) {
console.log("EVENTCHAIN", "Only one config JSON supported per Eventchain.")
process.exit();
return;
}
let config = configs[0];
let v = validate(config)
if (v.length > 0) {
console.log(v.join("\n"))
process.exit();
}
return config;
}
const start = async function(options) {
const config = await loadAndValidateConfig(options)
let chaindir;
let gene = {
filter: config,
onmempool: async function(e) {
log.push("ONMEMPOOL " + Date.now() + " " + e.tx.tx.h + " " + JSON.stringify(e.tx) + "\n")
},
onblock: async function(e) {
if (e.tx.length > 0) {
log.push("ONBLOCK " + Date.now() + " " + e.tx[0].blk.h + " " + JSON.stringify(e.tx) + "\n")
}
},
onstart: async function(e) {
if (options.pipe) {
log.pipe(process.stdout)
} else {
if (!fs.existsSync(chaindir)) {
fs.mkdirSync(chaindir, { recursive: true })
}
const logfile = fs.createWriteStream(chaindir + "/chain.txt", { flags: 'a+' })
log.pipe(logfile)
}
log.push("ONSTART " + Date.now() + " " + JSON.stringify(e) + "\n")
},
};
if (options && options.dest) gene.tape = options.dest;
if (gene.tape) {
chaindir = path.resolve(process.cwd(), gene.tape, "eventchain")
} else {
chaindir = path.resolve(process.cwd(), "eventchain")
}
planaria.start(gene)
}
if (require.main === module && process.argv.length > 2) {
const cmd = process.argv[2].toLowerCase();
const opts = minimist(process.argv.slice(3), {
alias: {
fileconfig: 'f',
strconfig: 's',
pipe: 'p',
dest: 'd',
},
boolean: ['pipe']
});
if (cmd === 'rewind') {
} else if (cmd === 'start') {
start(opts)
} else if (cmd === 'pipe') {
start({...opts, pipe: true})
} else if (cmd === 'serve') {
} else if (cmd === 'whoami') {
} else if (cmd === 'ls') {
}
}