@sentry/rrweb
Version:
Integration between sentry and rrweb.
111 lines (102 loc) • 4.98 kB
JavaScript
;
var rrweb = require('rrweb');
var Sentry = require('@sentry/browser');
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var __assign = function() {
__assign = Object.assign || function __assign(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);
};
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
var SentryRRWeb = /** @class */ (function () {
function SentryRRWeb(_a) {
var _this = this;
if (_a === void 0) { _a = {}; }
var _b = _a.checkoutEveryNms, checkoutEveryNms = _b === void 0 ? 5 * 60 * 1000 : _b, _c = _a.maskAllInputs, maskAllInputs = _c === void 0 ? true : _c, recordOptions = __rest(_a, ["checkoutEveryNms", "maskAllInputs"]);
this.name = SentryRRWeb.id;
this.events = [];
// default checkout time of 5 minutes
this.recordOptions = __assign({ checkoutEveryNms: checkoutEveryNms,
maskAllInputs: maskAllInputs }, recordOptions);
this.events = [];
rrweb.record(__assign(__assign({}, this.recordOptions), { emit: function (event, isCheckout) {
if (isCheckout) {
_this.events = [event];
}
else {
_this.events.push(event);
}
} }));
}
SentryRRWeb.prototype.setupOnce = function () {
var _this = this;
Sentry.addGlobalEventProcessor(function (event) { return _this.processEvent(event); });
};
// In version 6.10 of the JS SDK, `dsn.publicKey` was introduced to replace
// `dsn.user`, and in version 7.0.0, `dsn.user` was removed. Further, 7.0.0
// removed the `Dsn` type in favor of the `DsnComponents` type. Since for now
// our peer dependency is marked as >=4.0.0, we type `dsn` as `any` and look
// for both properties, to cover our bases as much as possible.
SentryRRWeb.prototype.attachmentUrlFromDsn = function (dsn, eventId) {
var host = dsn.host, path = dsn.path, projectId = dsn.projectId, port = dsn.port, protocol = dsn.protocol;
var publicKey = dsn.publicKey || dsn.user;
return protocol + "://" + host + (port !== '' ? ":" + port : '') + (path !== '' ? "/" + path : '') + "/api/" + projectId + "/events/" + eventId + "/attachments/?sentry_key=" + publicKey + "&sentry_version=7&sentry_client=rrweb";
};
SentryRRWeb.prototype.processEvent = function (event) {
var self = Sentry.getCurrentHub().getIntegration(SentryRRWeb);
if (!self)
return;
try {
// short circuit if theres no events to replay
if (!this.events.length)
return;
var client = Sentry.getCurrentHub().getClient();
var endpoint = self.attachmentUrlFromDsn(client.getDsn(), event.event_id);
var formData = new FormData();
formData.append('rrweb', new Blob([JSON.stringify({ events: self.events })], {
type: 'application/json',
}), 'rrweb.json');
fetch(endpoint, {
method: 'POST',
body: formData,
}).catch(function (ex) {
// we have to catch this otherwise it throws an infinite loop in Sentry
console.error(ex);
});
return event;
}
catch (ex) {
console.error(ex);
}
};
SentryRRWeb.id = 'SentryRRWeb';
return SentryRRWeb;
}());
module.exports = SentryRRWeb;
//# sourceMappingURL=index.js.map