insight-api
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -6,3 +6,2 @@ 'use strict'; | ||
var bitcore = require('bitcore-lib'); | ||
var BufferUtil = bitcore.util.buffer; | ||
var pools = require('../pools.json'); | ||
@@ -9,0 +8,0 @@ var BN = bitcore.crypto.BN; |
@@ -18,2 +18,11 @@ 'use strict'; | ||
/** | ||
* A service for Bitcore to enable HTTP routes to query information about the blockchain. | ||
* | ||
* @param {Object} options | ||
* @param {Boolean} options.enableCache - This will enable cache-control headers | ||
* @param {Number} options.cacheShortSeconds - The time to cache short lived cache responses. | ||
* @param {Number} options.cacheLongSeconds - The time to cache long lived cache responses. | ||
* @param {String} options.routePrefix - The URL route prefix | ||
*/ | ||
var InsightAPI = function(options) { | ||
@@ -29,2 +38,9 @@ BaseService.call(this, options); | ||
if (!_.isUndefined(options.enableCache)) { | ||
$.checkArgument(_.isBoolean(options.enableCache)); | ||
this.enableCache = options.enableCache; | ||
} | ||
this.cacheShortSeconds = options.cacheShortSeconds; | ||
this.cacheLongSeconds = options.cacheLongSeconds; | ||
if (!_.isUndefined(options.routePrefix)) { | ||
@@ -43,2 +59,22 @@ this.routePrefix = options.routePrefix; | ||
InsightAPI.prototype.cache = function(maxAge) { | ||
var self = this; | ||
return function(req, res, next) { | ||
if (self.enableCache) { | ||
res.header('Cache-Control', 'public, max-age=' + maxAge); | ||
} | ||
next(); | ||
}; | ||
}; | ||
InsightAPI.prototype.cacheShort = function() { | ||
var seconds = this.cacheShortSeconds || 30; // thirty seconds | ||
return this.cache(seconds); | ||
}; | ||
InsightAPI.prototype.cacheLong = function() { | ||
var seconds = this.cacheLongSeconds || 86400; // one day | ||
return this.cache(seconds); | ||
}; | ||
InsightAPI.prototype.getRoutePrefix = function() { | ||
@@ -55,2 +91,3 @@ return this.routePrefix; | ||
InsightAPI.prototype.setupRoutes = function(app) { | ||
//Enable CORS | ||
@@ -65,21 +102,20 @@ app.use(function(req, res, next) { | ||
var blocks = new BlockController(this.node); | ||
app.get('/blocks', blocks.list.bind(blocks)); | ||
app.get('/blocks', this.cacheShort(), blocks.list.bind(blocks)); | ||
app.get('/block/:blockHash', blocks.show.bind(blocks)); | ||
app.get('/block/:blockHash', this.cacheLong(), blocks.show.bind(blocks)); | ||
app.param('blockHash', blocks.block.bind(blocks)); | ||
app.get('/block-index/:height', blocks.blockIndex.bind(blocks)); | ||
app.get('/block-index/:height', this.cacheLong(), blocks.blockIndex.bind(blocks)); | ||
app.param('height', blocks.blockIndex.bind(blocks)); | ||
// Transaction routes | ||
var transactions = new TxController(this.node); | ||
app.get('/tx/:txid', transactions.show.bind(transactions)); | ||
app.get('/tx/:txid', this.cacheLong(), transactions.show.bind(transactions)); | ||
app.param('txid', transactions.transaction.bind(transactions)); | ||
app.get('/txs', transactions.list.bind(transactions)); | ||
app.get('/txs', this.cacheShort(), transactions.list.bind(transactions)); | ||
app.post('/tx/send', transactions.send.bind(transactions)); | ||
// Raw Routes | ||
app.get('/rawtx/:txid', transactions.showRaw.bind(transactions)); | ||
app.get('/rawtx/:txid', this.cacheLong(), transactions.showRaw.bind(transactions)); | ||
app.param('txid', transactions.rawTransaction.bind(transactions)); | ||
@@ -89,21 +125,21 @@ | ||
var addresses = new AddressController(this.node); | ||
app.get('/addr/:addr', addresses.checkAddr.bind(addresses), addresses.show.bind(addresses)); | ||
app.get('/addr/:addr/utxo', addresses.checkAddr.bind(addresses), addresses.utxo.bind(addresses)); | ||
app.get('/addrs/:addrs/utxo', addresses.checkAddrs.bind(addresses), addresses.multiutxo.bind(addresses)); | ||
app.post('/addrs/utxo', addresses.checkAddrs.bind(addresses), addresses.multiutxo.bind(addresses)); | ||
app.get('/addrs/:addrs/txs', addresses.checkAddrs.bind(addresses), addresses.multitxs.bind(addresses)); | ||
app.post('/addrs/txs', addresses.checkAddrs.bind(addresses), addresses.multitxs.bind(addresses)); | ||
app.get('/addr/:addr', this.cacheShort(), addresses.checkAddr.bind(addresses), addresses.show.bind(addresses)); | ||
app.get('/addr/:addr/utxo', this.cacheShort(), addresses.checkAddr.bind(addresses), addresses.utxo.bind(addresses)); | ||
app.get('/addrs/:addrs/utxo', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.multiutxo.bind(addresses)); | ||
app.post('/addrs/utxo', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.multiutxo.bind(addresses)); | ||
app.get('/addrs/:addrs/txs', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.multitxs.bind(addresses)); | ||
app.post('/addrs/txs', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.multitxs.bind(addresses)); | ||
// Address property routes | ||
app.get('/addr/:addr/balance', addresses.checkAddr.bind(addresses), addresses.balance.bind(addresses)); | ||
app.get('/addr/:addr/totalReceived', addresses.checkAddr.bind(addresses), addresses.totalReceived.bind(addresses)); | ||
app.get('/addr/:addr/totalSent', addresses.checkAddr.bind(addresses), addresses.totalSent.bind(addresses)); | ||
app.get('/addr/:addr/unconfirmedBalance', addresses.checkAddr.bind(addresses), addresses.unconfirmedBalance.bind(addresses)); | ||
app.get('/addr/:addr/balance', this.cacheShort(), addresses.checkAddr.bind(addresses), addresses.balance.bind(addresses)); | ||
app.get('/addr/:addr/totalReceived', this.cacheShort(), addresses.checkAddr.bind(addresses), addresses.totalReceived.bind(addresses)); | ||
app.get('/addr/:addr/totalSent', this.cacheShort(), addresses.checkAddr.bind(addresses), addresses.totalSent.bind(addresses)); | ||
app.get('/addr/:addr/unconfirmedBalance', this.cacheShort(), addresses.checkAddr.bind(addresses), addresses.unconfirmedBalance.bind(addresses)); | ||
// Status route | ||
var status = new StatusController(this.node); | ||
app.get('/status', status.show.bind(status)); | ||
app.get('/sync', status.sync.bind(status)); | ||
app.get('/peer', status.peer.bind(status)); | ||
app.get('/version', status.version.bind(status)); | ||
app.get('/status', this.cacheShort(), status.show.bind(status)); | ||
app.get('/sync', this.cacheShort(), status.sync.bind(status)); | ||
app.get('/peer', this.cacheShort(), status.peer.bind(status)); | ||
app.get('/version', this.cacheShort(), status.version.bind(status)); | ||
@@ -110,0 +146,0 @@ // Address routes |
@@ -62,6 +62,10 @@ 'use strict'; | ||
// Not exactly the total blockchain height, | ||
// but we will reach 100% when our db and bitcoind are both fully synced | ||
var totalHeight = this.node.services.bitcoind.height / (this.node.services.bitcoind.syncPercentage() / 100); | ||
var info = { | ||
status: status, | ||
blockChainHeight: this.node.services.bitcoind.height, | ||
syncPercentage: this.node.services.db.tip.__height / this.node.services.bitcoind.height * 100, | ||
syncPercentage: Math.round(this.node.services.db.tip.__height / totalHeight * 100), | ||
height: this.node.services.db.tip.__height, | ||
@@ -68,0 +72,0 @@ error: null, |
{ | ||
"name": "insight-api", | ||
"description": "A Bitcoin blockchain REST and web socket API service for Bitcore Node.", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"repository": "git://github.com/bitpay/insight-api.git", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -128,3 +128,4 @@ 'use strict'; | ||
height: 500000, | ||
isSynced: sinon.stub().returns(true) | ||
isSynced: sinon.stub().returns(true), | ||
syncPercentage: sinon.stub().returns(99.99) | ||
} | ||
@@ -131,0 +132,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
638716
25
5433
0