Adding websocket api calls

The following api now have ws support for large data transmission
- /addr/:addr/
- /addrs/:addrs/txs
This commit is contained in:
sairajzero 2023-01-27 18:03:17 +05:30
parent da5c5143ba
commit 79ed3ad330
3 changed files with 111 additions and 1 deletions

View File

@ -40,6 +40,40 @@ AddressController.prototype.show = function(req, res) {
});
};
AddressController.prototype.show_ws = function(req, ws) {
var self = this;
/*
var options = {
noTxList: parseInt(req.query.noTxList)
};
*/
if (req.query.from && req.query.to) {
options.from = parseInt(req.query.from);
options.to = parseInt(req.query.to);
}
self._address.getAddressSummary(req.addr, options, function (err, data) {
if(err) {
return self.common.handleErrors_ws(err, ws);
}
ws.send({data});
}, function(err, result) {
if(err) {
return self.common.handleErrors_ws(err, ws);
}
if(ws.readyState === ws.OPEN){
ws.send({result});
ws.close();
}
});
};
AddressController.prototype.balance = function(req, res) {
this.addressSummarySubQuery(req, res, 'balanceSat');
};
@ -278,6 +312,54 @@ AddressController.prototype.multitxs = function(req, res) {
});
};
AddressController.prototype.multitxs_ws = function(req, ws) {
var self = this;
var options = {
from: parseInt(req.query.from) || parseInt(req.body.from) || 0
};
options.to = parseInt(req.query.to) || parseInt(req.body.to) || parseInt(options.from) + 10;
options.txNotNeeded = true;
var transformOptions = self._getTransformOptions(req);
self._address.getAddressHistory(req.addrs, options, function (err, data) {
if(err) {
return self.common.handleErrors_ws(err, ws, false);
}
self.txController.transformTransaction(tx, transformOptions, function(err, data){
if(err) {
return self.common.handleErrors_ws(err, ws, false);
}
ws.send({data})
});
}, function(err, result) {
if(err) {
return self.common.handleErrors_ws(err, ws);
}
var ret = {
totalItems: result.totalCount,
from: options.from,
to: Math.min(options.to, result.totalCount)
}
if(ws.readyState === ws.OPEN){
ws.send({result: ret});
ws.close();
}
});
};
AddressController.prototype.transformAddressHistoryForMultiTxs = function(txs, options, callback) {
var self = this;

View File

@ -13,6 +13,13 @@ Common.prototype.notReady = function (err, res, p) {
res.status(503).send('Server not yet ready. Sync Percentage:' + p);
};
Common.prototype.notReady_ws = function (err, ws, p) {
if(ws.readyState !== ws.OPEN)
return;
ws.send({error: {message: 'Server not yet ready. Sync Percentage:' + p, code: 503}});
ws.close();
};
Common.prototype.handleErrors = function (err, res) {
if (err) {
if (err.code) {
@ -29,6 +36,22 @@ Common.prototype.handleErrors = function (err, res) {
}
};
Common.prototype.handleErrors_ws = function (err, ws, close = true) {
if(ws.readyState !== ws.OPEN)
return;
if (err) {
if (err.code)
ws.send({error: {message: err.message, code: err.code}});
else {
this.log.error(err.stack);
ws.send({error: {message: err.message, code: 503}});
}
} else {
ws.send({error: {message: 'Not found', code: 404}});
}
if(close)
ws.close();
}
Common.prototype.translateInputAddresses= function(addresses) {
var self = this;

View File

@ -143,7 +143,7 @@ FlosightAPI.prototype._getRateLimiter = function() {
return limiter;
};
FlosightAPI.prototype.setupRoutes = function(app) {
FlosightAPI.prototype.setupRoutes = function(app, express, express_ws) {
var self = this;
@ -184,6 +184,9 @@ FlosightAPI.prototype.setupRoutes = function(app) {
}
});
//Add ws listener to app
var expressWS = express_ws(app);
//Block routes
var blockOptions = {
node: this.node,
@ -216,10 +219,12 @@ FlosightAPI.prototype.setupRoutes = function(app) {
// Address routes
var addresses = new AddressController(this.node, this.translateAddresses);
app.get('/addr/:addr', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.show.bind(addresses));
app.ws('/addr/:addr', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.show_ws.bind(addresses));
app.get('/addr/:addr/utxo', this.cacheShort(), addresses.checkAddrs.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.ws('/addrs/:addrs/txs', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.multitxs_ws.bind(addresses));
app.post('/addrs/txs', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.multitxs.bind(addresses));
// Address property routes