From 79ed3ad33086972d0041c924dc4a1505443d9a9f Mon Sep 17 00:00:00 2001 From: sairajzero Date: Fri, 27 Jan 2023 18:03:17 +0530 Subject: [PATCH] Adding websocket api calls The following api now have ws support for large data transmission - /addr/:addr/ - /addrs/:addrs/txs --- lib/addresses.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/common.js | 23 ++++++++++++++ lib/index.js | 7 ++++- 3 files changed, 111 insertions(+), 1 deletion(-) diff --git a/lib/addresses.js b/lib/addresses.js index a855a5f..dca175f 100644 --- a/lib/addresses.js +++ b/lib/addresses.js @@ -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; diff --git a/lib/common.js b/lib/common.js index d11ad79..844c724 100644 --- a/lib/common.js +++ b/lib/common.js @@ -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; diff --git a/lib/index.js b/lib/index.js index b65f649..b887c2c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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