Use transform stream instead of async for /blocks
This commit is contained in:
parent
5bcfd8fcd8
commit
2122b01023
@ -1,5 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
var Stream = require('stream');
|
||||
var util = require('util');
|
||||
|
||||
var async = require('async');
|
||||
var bitcore = require('bitcore-lib');
|
||||
var _ = bitcore.deps._;
|
||||
@ -8,6 +11,7 @@ var BN = bitcore.crypto.BN;
|
||||
var LRU = require('lru-cache');
|
||||
var Common = require('./common');
|
||||
var bcoin = require('bcoin');
|
||||
var JsonStream = require('JSONStream');
|
||||
|
||||
function BlockController(options) {
|
||||
var self = this;
|
||||
@ -269,6 +273,16 @@ BlockController.prototype.list = function(req, res) {
|
||||
return self.common.handleErrors(err, res);
|
||||
}
|
||||
|
||||
function BlockBuilder() {
|
||||
Stream.Transform.call(this, {objectMode: true});
|
||||
}
|
||||
|
||||
util.inherits(BlockBuilder, Stream.Transform);
|
||||
|
||||
BlockBuilder.prototype._transform = function transformObject(hash, encoding, done) {
|
||||
self._getBlockSummary(hash, moreTimestamp, done);
|
||||
};
|
||||
|
||||
hashes.reverse();
|
||||
|
||||
if(hashes.length > limit) {
|
||||
@ -276,40 +290,30 @@ BlockController.prototype.list = function(req, res) {
|
||||
hashes = hashes.slice(0, limit);
|
||||
}
|
||||
|
||||
async.mapSeries(
|
||||
hashes,
|
||||
function(hash, next) {
|
||||
self._getBlockSummary(hash, moreTimestamp, next);
|
||||
},
|
||||
function(err, blocks) {
|
||||
if(err) {
|
||||
return self.common.handleErrors(err, res);
|
||||
}
|
||||
|
||||
blocks.sort(function(a, b) {
|
||||
return b.height - a.height;
|
||||
});
|
||||
|
||||
var data = {
|
||||
blocks: blocks,
|
||||
length: blocks.length,
|
||||
pagination: {
|
||||
next: next,
|
||||
prev: prev,
|
||||
currentTs: lte - 1,
|
||||
current: dateStr,
|
||||
isToday: isToday,
|
||||
more: more
|
||||
}
|
||||
};
|
||||
|
||||
if(more) {
|
||||
data.pagination.moreTs = moreTimestamp;
|
||||
}
|
||||
|
||||
res.jsonp(data);
|
||||
var data = JSON.stringify({
|
||||
length: hashes.length,
|
||||
pagination: {
|
||||
next: next,
|
||||
prev: prev,
|
||||
currentTs: lte - 1,
|
||||
current: dateStr,
|
||||
isToday: isToday,
|
||||
more: more,
|
||||
moreTs: more ? moreTimestamp : undefined
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
var readableStream = new Stream.Readable({objectMode: true});
|
||||
var blockBuilder = new BlockBuilder();
|
||||
readableStream
|
||||
.pipe(blockBuilder)
|
||||
.pipe(JsonStream.stringify('{"blocks":[', ',', '],' + data.substr(1)))
|
||||
.pipe(res);
|
||||
|
||||
hashes.forEach(function(hash) {
|
||||
readableStream.push(hash);
|
||||
});
|
||||
readableStream.push(null);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@ -60,6 +60,7 @@
|
||||
"main": "lib",
|
||||
"bitcoreNode": "lib",
|
||||
"dependencies": {
|
||||
"JSONStream": "^1.3.1",
|
||||
"async": "*",
|
||||
"bcoin": "bcoin-org/bcoin#886008a1822ce1da7fa8395ee7db4bcc1750a28a",
|
||||
"bitcore-lib": "bitpay/bitcore-lib#transitional",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user