UNPKG

posthog-js

Version:

Posthog-js allows you to automatically capture usage and send events to PostHog.

81 lines • 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var globals_1 = require("../utils/globals"); var extension_tokens_1 = require("../extension-tokens"); var core_1 = require("@posthog/core"); var console_logs_1 = require("@posthog/browser-common/console-logs"); var isClient = function (host) { return 'canCapture' in host; }; // Compatibility for a bug where `_captureConsoleLog` was inadvertently used across // independently built bundles and received different mangled names. Pre-stable-ABI // cores used `le` through 1.410.4, `de` through 1.410.10, `he` through 1.418.3, `ui` // through 1.418.10, `ci` through 1.418.14, and `vi` through 1.419.2. var historicalCaptureConsoleLogName = function (version) { var match = /^1\.(\d+)\.(\d+)$/.exec(version); if (!match) { return undefined; } var minor = Number(match[1]); var patch = Number(match[2]); if (minor < 392 || minor > 419) { return undefined; } if (minor === 410) { return patch <= 4 ? 'le' : patch <= 10 ? 'de' : undefined; } if (minor === 418) { return patch <= 3 ? 'he' : patch <= 10 ? 'ui' : patch <= 14 ? 'ci' : patch <= 17 ? 'vi' : undefined; } if (minor === 419) { return patch <= 2 ? 'vi' : undefined; } return minor < 410 ? 'le' : 'he'; }; var captureConsoleLogForHost = function (host, logs, options) { if (isClient(host)) { logs.captureConsoleLog(options); return; } // `_captureConsoleLog` had six generated names across core-backed releases. // Select by the stable SDK version instead of probing generated names, // because the same name can identify a different method in another release. // Keep this historical ABI isolated here. A `captureLog` fallback would change the // service name, scope, queue, and rate limits. var name = historicalCaptureConsoleLogName(host.version); var historicalCaptureConsoleLog = name ? logs[name] : undefined; if (name) { if ((0, core_1.isFunction)(historicalCaptureConsoleLog)) historicalCaptureConsoleLog.call(logs, options); } else if ((0, core_1.isFunction)(logs.captureConsoleLog)) { logs.captureConsoleLog(options); } }; var consoleHost = function (host) { return { console: globals_1.assignableWindow.console, hostname: globals_1.assignableWindow.location.host, getCapturingLogs: function () { var logs; if (isClient(host)) { logs = host.canCapture ? host.getExtension(extension_tokens_1.LogsExtension) : undefined; } else { var canCapture = (0, core_1.isFunction)(host.is_capturing) ? host.is_capturing() : !host.has_opted_out_capturing(); logs = canCapture ? host.logs : undefined; } return logs ? { captureConsoleLog: function (options) { return captureConsoleLogForHost(host, logs, options); }, captureBufferedConsoleLog: function (options, context, occurredAtMs) { var _a; return (_a = logs.captureBufferedConsoleLog) === null || _a === void 0 ? void 0 : _a.call(logs, options, context, occurredAtMs); }, } : undefined; }, }; }; var initializeLogs = function (host) { return (0, console_logs_1.initializeLogs)(consoleHost(host)); }; var replayConsoleBuffer = function (host, entries) { return (0, console_logs_1.replayConsoleBuffer)(consoleHost(host), entries); }; globals_1.assignableWindow.__PosthogExtensions__ = globals_1.assignableWindow.__PosthogExtensions__ || {}; globals_1.assignableWindow.__PosthogExtensions__.logs = { initializeLogs: initializeLogs, replayConsoleBuffer: replayConsoleBuffer }; //# sourceMappingURL=logs.js.map