UNPKG

react-on-rails

Version:

react-on-rails JavaScript for react_on_rails Ruby gem

43 lines (42 loc) 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.consoleReplay = consoleReplay; exports.default = buildConsoleReplay; const RenderUtils_1 = require("./RenderUtils"); const scriptSanitizedVal_1 = require("./scriptSanitizedVal"); /** @internal Exported only for tests */ function consoleReplay(customConsoleHistory = undefined, numberOfMessagesToSkip = 0) { // console.history is a global polyfill used in server rendering. const consoleHistory = customConsoleHistory ?? console.history; if (!(Array.isArray(consoleHistory))) { return ''; } const lines = consoleHistory.slice(numberOfMessagesToSkip).map(msg => { const stringifiedList = msg.arguments.map(arg => { let val; try { if (typeof arg === 'string') { val = arg; } else if (arg instanceof String) { val = String(arg); } else { val = JSON.stringify(arg); } if (val === undefined) { val = 'undefined'; } } catch (e) { val = `${e.message}: ${arg}`; } return (0, scriptSanitizedVal_1.default)(val); }); return `console.${msg.level}.apply(console, ${JSON.stringify(stringifiedList)});`; }); return lines.join('\n'); } function buildConsoleReplay(customConsoleHistory = undefined, numberOfMessagesToSkip = 0) { return RenderUtils_1.default.wrapInScriptTags('consoleReplayLog', consoleReplay(customConsoleHistory, numberOfMessagesToSkip)); }