forked from interplanaria/bitbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.js
58 lines (58 loc) · 1.63 KB
/
block.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
const fs = require('fs')
const es = require('event-stream')
const crypto = require('crypto')
const crawl = function(stream, path, cb) {
let str = stream
.pipe(es.split())
.pipe(es.filterSync(function(data) { return !(["[", ",", "]"].includes(data.toString())) }))
.pipe(es.parse())
let current_block;
let fileStream
str.on('data', function(data) {
if (!current_block) {
current_block = data.blk.i;
fileStream = fs.createWriteStream(path + "/" + data.blk.i + ".json")
console.log("block = ", current_block)
fileStream.write("[\n" + JSON.stringify(data))
} else {
if (current_block < data.blk.i) {
fileStream.write("\n]")
fileStream.close();
current_block = data.blk.i;
fileStream = fs.createWriteStream(path + "/" + data.blk.i + ".json")
console.log("block = ", current_block)
fileStream.write("[\n" + JSON.stringify(data))
} else {
fileStream.write(",\n" + JSON.stringify(data))
}
}
});
str.on('close', function() {
console.log("closing")
fileStream.write("]")
fileStream.close();
cb()
})
}
const save = function(stream, cb) {
let filestream = fs.createWriteStream(filename, {autoClose: false})
filestream.write("{\"bus\":")
stream.on("error", function() {
console.log("ERror")
filestream.close();
})
stream.on("end", function() {
console.log("Finish")
filestream.write("}")
filestream.close();
})
stream.on("close", function() {
console.log("close")
filestream.close();
o.onfinish();
})
stream.pipe(filestream)
}
module.exports = {
crawl: crawl, save: save
}