UNPKG

posthog-js

Version:

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

86 lines • 4.05 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.enableRequestSending = enableRequestSending; exports.sendRequest = sendRequest; var core_1 = require("@posthog/core"); var globals_1 = require("@posthog/browser-common/utils/globals"); var request_1 = require("./request"); /* * Dynamic... constants? Is that an oxymoron? */ // http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/ // https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#withCredentials // IE<10 does not support cross-origin XHR's but script tags // with defer won't block window.onload; ENQUEUE_REQUESTS // should only be true for Opera<12 var ENQUEUE_REQUESTS = !request_1.SUPPORTS_REQUEST && (globals_1.userAgent === null || globals_1.userAgent === void 0 ? void 0 : globals_1.userAgent.indexOf('MSIE')) === -1 && (globals_1.userAgent === null || globals_1.userAgent === void 0 ? void 0 : globals_1.userAgent.indexOf('Mozilla')) === -1; function enableRequestSending() { ENQUEUE_REQUESTS = false; } // Internal dispatch shared by PostHog and RetryQueue. Transport metadata never enters // RequestResponse or the public options/callback contract. function sendRequest(instance, options, onResponse) { var _a, _b, _c; if (onResponse) { // Drop callbacks and DOM-ready deferral retain a one-argument completion. // A deferred request is drained through a fresh RetryQueue attempt, which // owns the eventual transport's Retry-After delay. options = __assign(__assign({}, options), { callback: function (response) { return onResponse(response); } }); } if (!instance.__loaded) { if (options.fireCallbackOnDrop) { (_a = options.callback) === null || _a === void 0 ? void 0 : _a.call(options, { statusCode: 0 }); } return; } if (ENQUEUE_REQUESTS) { instance.__request_queue.push(options); return; } if (instance.rateLimiter.isServerRateLimited(options.batchKey)) { if (options.fireCallbackOnDrop) { (_b = options.callback) === null || _b === void 0 ? void 0 : _b.call(options, { statusCode: 429 }); } return; } options.transport = options.transport || instance.config.api_transport; options.headers = __assign(__assign({}, instance.config.request_headers), options.headers); options.compression = options.compression === 'best-available' ? ((_c = instance.compression) !== null && _c !== void 0 ? _c : options.compressionFallback) : options.compression; var disableBeacon = (0, core_1.isUndefined)(instance.config.disable_beacon) ? instance.config.__preview_disable_beacon : instance.config.disable_beacon; if (disableBeacon) { options.disableTransport = ['sendBeacon']; } // Specially useful if you're doing SSR with NextJS // Users must be careful when tweaking `cache` because they might get out-of-date feature flags options.fetchOptions = options.fetchOptions || instance.config.fetch_options; (0, request_1.request)(options, function (response, retryAfterMs) { var _a, _b, _c; instance.rateLimiter.checkForLimiting(response); if (response.statusCode >= 400) { (_b = (_a = instance.config).on_request_error) === null || _b === void 0 ? void 0 : _b.call(_a, response); } if (onResponse) { onResponse(response, retryAfterMs); } else { (_c = options.callback) === null || _c === void 0 ? void 0 : _c.call(options, response); } }); } //# sourceMappingURL=request-dispatch.js.map