mirror of
https://github.com/Ride-The-Lightning/RTL.git
synced 2026-08-13 12:33:07 +02:00
Follow-up to the second #1629 review: - F4: the limiter invokes its done callback outside the surrounding .then/.catch, so a throw in the response-send body became an unhandled rejection with no response (a 500 -> hang regression, notably on LND postPeer where the inner .catch was removed). Wrap each converted done body in try/catch that sends the error response, guarded by res.headersSent. - F5: CLN postPeer re-listed peers but never resolved their aliases, so a freshly connected CLN peer came back with a raw node id (the frontend uses this response directly). Resolve aliases through the same bounded limiter, matching LND postPeer. - F6: make runWithConcurrencyLimit fire 'done' exactly once via a one-shot guard, so multiple synchronous completions (e.g. non-function task elements) can't double-send the response.
86 lines
5.2 KiB
TypeScript
86 lines
5.2 KiB
TypeScript
import request from 'request-promise';
|
|
import { Logger, LoggerService } from '../../utils/logger.js';
|
|
import { Common, CommonService } from '../../utils/common.js';
|
|
import { getAlias } from './network.js';
|
|
|
|
let options = null;
|
|
const logger: LoggerService = Logger;
|
|
const common: CommonService = Common;
|
|
|
|
export const getPeers = (req, res, next) => {
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'List Peers..' });
|
|
options = common.getOptions(req);
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
|
options.url = req.session.selectedNode.settings.lnServerUrl + '/v1/listpeers';
|
|
request.post(options).then((body) => {
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Peers', msg: 'Peers List Received', data: body });
|
|
const peers = !body.peers ? [] : body.peers;
|
|
// Resolve peer aliases with a bounded number of concurrent listnodes calls. An unbounded
|
|
// Promise.all here fires one request per peer at once, which overwhelms clnrest on nodes
|
|
// with many peers and fails with "Resource temporarily unavailable (os error 11)" (#1501).
|
|
const getPeerAliasesTasks = peers.map((peer) => () => getAlias(req.session.selectedNode, peer, 'id'));
|
|
common.runWithConcurrencyLimit(getPeerAliasesTasks, 20, () => {
|
|
// The limiter invokes this outside the surrounding .then/.catch chain, so guard the
|
|
// response-send: a throw here would otherwise be an unhandled rejection with no response.
|
|
try {
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Sorted Peers List Received', data: body.peers });
|
|
res.status(200).json(body.peers || []);
|
|
} catch (e) {
|
|
const err = common.handleError(e, 'Peers', 'List Peers Error', req.session.selectedNode);
|
|
if (!res.headersSent) { res.status(err.statusCode).json({ message: err.message, error: err.error }); }
|
|
}
|
|
});
|
|
}).catch((errRes) => {
|
|
const err = common.handleError(errRes, 'Peers', 'List Peers Error', req.session.selectedNode);
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
});
|
|
};
|
|
|
|
export const postPeer = (req, res, next) => {
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Connecting Peer..' });
|
|
options = common.getOptions(req);
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
|
options.url = req.session.selectedNode.settings.lnServerUrl + '/v1/connect';
|
|
options.body = req.body;
|
|
request.post(options).then((connectRes) => {
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Peers', msg: 'Peer Connected', data: connectRes });
|
|
const listOptions = common.getOptions(req);
|
|
listOptions.url = req.session.selectedNode.settings.lnServerUrl + '/v1/listpeers';
|
|
request.post(listOptions).then((listPeersRes) => {
|
|
const peers = listPeersRes && listPeersRes.peers ? common.newestOnTop(listPeersRes.peers, 'id', connectRes.id) : [];
|
|
// Resolve aliases (bounded) for the returned peers so a freshly connected peer shows its
|
|
// alias rather than a raw node id, matching getPeers and the LND postPeer path (#1629 F5).
|
|
const getPeerAliasesTasks = peers.map((peer) => () => getAlias(req.session.selectedNode, peer, 'id'));
|
|
common.runWithConcurrencyLimit(getPeerAliasesTasks, 20, () => {
|
|
try {
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Peers List after Connect Received', data: peers });
|
|
res.status(201).json(peers);
|
|
} catch (e) {
|
|
const err = common.handleError(e, 'Peers', 'Connect Peer Error', req.session.selectedNode);
|
|
if (!res.headersSent) { res.status(err.statusCode).json({ message: err.message, error: err.error }); }
|
|
}
|
|
});
|
|
}).catch((errRes) => {
|
|
const err = common.handleError(errRes, 'Peers', 'Connect Peer Error', req.session.selectedNode);
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
});
|
|
}).catch((errRes) => {
|
|
const err = common.handleError(errRes, 'Peers', 'Connect Peer Error', req.session.selectedNode);
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
});
|
|
};
|
|
|
|
export const deletePeer = (req, res, next) => {
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Disconnecting Peer..' });
|
|
options = common.getOptions(req);
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
|
options.url = req.session.selectedNode.settings.lnServerUrl + '/v1/disconnect';
|
|
options.body = req.body;
|
|
request.post(options).then((body) => {
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Peer Disconnected', data: body });
|
|
res.status(204).json({});
|
|
}).catch((errRes) => {
|
|
const err = common.handleError(errRes, 'Peers', 'Detach Peer Error', req.session.selectedNode);
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
});
|
|
};
|