123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- Object.defineProperty(exports, '__esModule', { value: true });
- const utils = require('@sentry/utils');
- const localForage = require('localforage');
- const debugBuild = require('./debug-build.js');
- const WINDOW = utils.GLOBAL_OBJ ;
- class Offline {
-
- static __initStatic() {this.id = 'Offline';}
-
-
-
-
-
- constructor(options = {}) {
- this.name = Offline.id;
- this.maxStoredEvents = options.maxStoredEvents || 30;
-
- this.offlineEventStore = localForage.default.createInstance({
- name: 'sentry/offlineEventStore',
- });
- }
-
- setupOnce(addGlobalEventProcessor, getCurrentHub) {
- this.hub = getCurrentHub();
- if ('addEventListener' in WINDOW) {
- WINDOW.addEventListener('online', () => {
- void this._sendEvents().catch(() => {
- debugBuild.DEBUG_BUILD && utils.logger.warn('could not send cached events');
- });
- });
- }
- const eventProcessor = event => {
-
- if (this.hub && this.hub.getIntegration(Offline)) {
-
- if ('navigator' in WINDOW && 'onLine' in WINDOW.navigator && !WINDOW.navigator.onLine) {
- debugBuild.DEBUG_BUILD && utils.logger.log('Event dropped due to being a offline - caching instead');
- void this._cacheEvent(event)
- .then((_event) => this._enforceMaxEvents())
- .catch((_error) => {
- debugBuild.DEBUG_BUILD && utils.logger.warn('could not cache event while offline');
- });
-
- return null;
- }
- }
- return event;
- };
- eventProcessor.id = this.name;
- addGlobalEventProcessor(eventProcessor);
-
- if ('navigator' in WINDOW && 'onLine' in WINDOW.navigator && WINDOW.navigator.onLine) {
- void this._sendEvents().catch(() => {
- debugBuild.DEBUG_BUILD && utils.logger.warn('could not send cached events');
- });
- }
- }
-
- async _cacheEvent(event) {
- return this.offlineEventStore.setItem(utils.uuid4(), utils.normalize(event));
- }
-
- async _enforceMaxEvents() {
- const events = [];
- return this.offlineEventStore
- .iterate((event, cacheKey, _index) => {
-
- events.push({ cacheKey, event });
- })
- .then(
- () =>
-
- this._purgeEvents(
-
- events
- .sort((a, b) => (b.event.timestamp || 0) - (a.event.timestamp || 0))
- .slice(this.maxStoredEvents < events.length ? this.maxStoredEvents : events.length)
- .map(event => event.cacheKey),
- ),
- )
- .catch((_error) => {
- debugBuild.DEBUG_BUILD && utils.logger.warn('could not enforce max events');
- });
- }
-
- async _purgeEvent(cacheKey) {
- return this.offlineEventStore.removeItem(cacheKey);
- }
-
- async _purgeEvents(cacheKeys) {
-
- return Promise.all(cacheKeys.map(cacheKey => this._purgeEvent(cacheKey))).then();
- }
-
- async _sendEvents() {
- return this.offlineEventStore.iterate((event, cacheKey, _index) => {
- if (this.hub) {
- this.hub.captureEvent(event);
- void this._purgeEvent(cacheKey).catch((_error) => {
- debugBuild.DEBUG_BUILD && utils.logger.warn('could not purge event from cache');
- });
- } else {
- debugBuild.DEBUG_BUILD && utils.logger.warn('no hub found - could not send cached event');
- }
- });
- }
- } Offline.__initStatic();
- exports.Offline = Offline;
|