UNPKG

2.93 kBJavaScriptView Raw
1Object.defineProperty(exports, '__esModule', { value: true });
2
3var utils = require('@sentry/utils');
4
5var DEFAULT_TRANSPORT_BUFFER_SIZE = 30;
6
7/**
8 * Creates an instance of a Sentry `Transport`
9 *
10 * @param options
11 * @param makeRequest
12 */
13function createTransport(
14 options,
15 makeRequest,
16 buffer = utils.makePromiseBuffer(options.bufferSize || DEFAULT_TRANSPORT_BUFFER_SIZE),
17) {
18 let rateLimits = {};
19
20 var flush = (timeout) => buffer.drain(timeout);
21
22 function send(envelope) {
23 var filteredEnvelopeItems = [];
24
25 // Drop rate limited items from envelope
26 utils.forEachEnvelopeItem(envelope, (item, type) => {
27 var envelopeItemDataCategory = utils.envelopeItemTypeToDataCategory(type);
28 if (utils.isRateLimited(rateLimits, envelopeItemDataCategory)) {
29 options.recordDroppedEvent('ratelimit_backoff', envelopeItemDataCategory);
30 } else {
31 filteredEnvelopeItems.push(item);
32 }
33 });
34
35 // Skip sending if envelope is empty after filtering out rate limited events
36 if (filteredEnvelopeItems.length === 0) {
37 return utils.resolvedSyncPromise();
38 }
39
40 var filteredEnvelope = utils.createEnvelope(envelope[0], filteredEnvelopeItems );
41
42 // Creates client report for each item in an envelope
43 var recordEnvelopeLoss = (reason) => {
44 utils.forEachEnvelopeItem(filteredEnvelope, (_, type) => {
45 options.recordDroppedEvent(reason, utils.envelopeItemTypeToDataCategory(type));
46 });
47 };
48
49 var requestTask = () =>
50 makeRequest({ body: utils.serializeEnvelope(filteredEnvelope, options.textEncoder) }).then(
51 response => {
52 // We don't want to throw on NOK responses, but we want to at least log them
53 if (response.statusCode !== undefined && (response.statusCode < 200 || response.statusCode >= 300)) {
54 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn(`Sentry responded with status code ${response.statusCode} to sent event.`);
55 }
56
57 rateLimits = utils.updateRateLimits(rateLimits, response);
58 },
59 error => {
60 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.error('Failed while sending event:', error);
61 recordEnvelopeLoss('network_error');
62 },
63 );
64
65 return buffer.add(requestTask).then(
66 result => result,
67 error => {
68 if (error instanceof utils.SentryError) {
69 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.error('Skipped sending event due to full buffer');
70 recordEnvelopeLoss('queue_overflow');
71 return utils.resolvedSyncPromise();
72 } else {
73 throw error;
74 }
75 },
76 );
77 }
78
79 return {
80 send,
81 flush,
82 };
83}
84
85exports.DEFAULT_TRANSPORT_BUFFER_SIZE = DEFAULT_TRANSPORT_BUFFER_SIZE;
86exports.createTransport = createTransport;
87//# sourceMappingURL=base.js.map