UNPKG

reveal-sdk-node

Version:

RevealBI Node.js SDK

76 lines (65 loc) 2.35 kB
const revealCore = require("./lib/reveal"); const {IDashboardExporter} = require("./lib/exporter/exporter"); const exportOptions = require("./lib/exporter/ExportOptions"); const dashboardTheme = require("./lib/exporter/DashboardTheme"); const { pipeline } = require("node:stream").promises; const { Transform } = require("node:stream"); const fs = require('fs'); const validateDID = (dashboardId) => { if (!/^[a-zA-Z0-9\.\-_]+$/.test(dashboardId)) { throw new Error("The dashboardId contains invalid characters"); } } const dashboardProvider = (_, dashboardId) => { validateDID(); try { return fs.createReadStream(`./dashboards/${dashboardId}.rdash`); } catch (e) { if (e.name == 'ENOENT') { return null; } else { throw e; } } }; const dashboardStorageProvider = async (_, dashboardId, stream) => { validateDID(); var totalSize = 0; const path = './dashboards'; var dashboardPath = `${path}/${dashboardId}.rdash`; if (!fs.existsSync(dashboardPath)) { var count = (await fs.promises.readdir(path)).length; if (count > 100) { throw new Error("too many dashboard files"); } } await pipeline( stream, new Transform({ transform: function(chunk, _, callback) { totalSize += chunk.length; if (totalSize > 10*1024*1024) { console.error("The input dashboard stream was too large"); callback(new Error("Dashboard is too large")); } else { callback(null, chunk); } } }), fs.createWriteStream(dashboardPath) ); } module.exports = function(revealOptions) { if (!revealOptions) { revealOptions = {}; } if (!revealOptions.dashboardProvider) { revealOptions.dashboardProvider = dashboardProvider; } if (!revealOptions.dashboardStorageProvider) { revealOptions.dashboardStorageProvider = dashboardStorageProvider; } return revealCore(revealOptions); } Object.assign(module.exports, revealCore); Object.assign(module.exports, IDashboardExporter); Object.assign(module.exports, exportOptions); Object.assign(module.exports, dashboardTheme);