UNPKG

bitcore-node

Version:

A blockchain indexing node with extended capabilities using bitcore

82 lines 3.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const cors_1 = __importDefault(require("cors")); const express_1 = __importDefault(require("express")); const config_1 = __importDefault(require("../config")); const config_2 = require("../services/config"); const middleware_1 = require("./middleware"); const web3_1 = require("./web3"); const app = (0, express_1.default)(); const bodyLimit = 100 * 1024 * 1024; // 100 MB app.use(express_1.default.json({ limit: bodyLimit })); app.use(express_1.default.raw({ limit: bodyLimit })); const chains = config_2.Config.chains(); const networks = {}; for (let chain of chains) { for (let network of Object.keys(config_1.default.chains[chain])) { networks[chain] = networks[chain] || {}; Object.assign(networks[chain], { [network]: true }); } } function bootstrap(path) { const fs = require('fs'); const router = express_1.default.Router({ mergeParams: true }); const folder = path ? path + '/' : ''; fs.readdirSync(__dirname + '/' + path).forEach(function (file) { if (file.match(/\.js$/) !== null && file !== 'index.js') { var route = require('./' + folder + file); router.use(route.path, route.router); } }); return router; } function getRouterFromFile(path) { const router = express_1.default.Router({ mergeParams: true }); var route = require('./' + path); router.use(route.path, route.router); return router; } app.use((0, cors_1.default)()); app.use((0, middleware_1.LogMiddleware)()); app.use((0, middleware_1.CacheMiddleware)(middleware_1.CacheTimes.Second, middleware_1.CacheTimes.Second)); app.use((0, middleware_1.RateLimiter)('GLOBAL', 10, 200, 4000)); app.use('/api', getRouterFromFile('status')); // Change aliased chain and network params app.param(['chain', 'network'], (req, _, next) => { const { chain: beforeChain, network: beforeNetwork } = req.params; const { chain, network } = config_2.Config.aliasFor({ chain: beforeChain, network: beforeNetwork }); req.params.chain = chain; req.params.network = network; next(); }); app.use('/api/:chain/:network', (req, resp, next) => { let { chain, network } = req.params; const hasChain = chains.includes(chain); const chainNetworks = networks[chain] || null; const hasChainNetworks = chainNetworks != null; const hasNetworkForChain = hasChainNetworks ? chainNetworks[network] : false; if (chain && !hasChain) { return resp.status(500).send(`This node is not configured for the chain ${chain}`); } if (network && (!hasChainNetworks || !hasNetworkForChain)) { return resp.status(500).send(`This node is not configured for the network ${network} on chain ${chain}`); } return next(); }); app.use('/api/:chain/:network', bootstrap('api')); app.use('/web3/:chain/:network', web3_1.Web3Proxy); exports.default = app; //# sourceMappingURL=index.js.map