raygun
Version:
Raygun package for Node.js, written in TypeScript
123 lines (122 loc) • 5.56 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.RaygunBatchTransport = exports.MAX_BATCH_SIZE_BYTES = exports.MAX_MESSAGES_IN_BATCH = void 0;
var raygun_transport_1 = require("./raygun.transport");
var timer_1 = require("./timer");
var debug = require("debug")("raygun");
exports.MAX_MESSAGES_IN_BATCH = 100;
exports.MAX_BATCH_SIZE_BYTES = 1638400;
var MAX_BATCH_INNER_SIZE_BYTES = exports.MAX_BATCH_SIZE_BYTES - 2; // for the starting and ending byte
var RaygunBatchTransport = /** @class */ (function () {
function RaygunBatchTransport(options) {
this.timerId = null;
this.batchId = 0;
this.batchState = { messages: [], messageSizeInBytes: 0 };
this.interval = options.interval;
this.httpOptions = options.httpOptions;
}
/**
* Enqueues send request to batch processor.
* @param options - send options without callback
* @returns Promise with response or error if rejected
*/
RaygunBatchTransport.prototype.send = function (options) {
var _this = this;
var promise = this.onIncomingMessage(options.message);
if (!this.timerId) {
this.timerId = setTimeout(function () { return _this.processBatch(); }, 1000);
}
return promise;
};
/**
* Stops batch transport and clears timerId
*/
RaygunBatchTransport.prototype.stopProcessing = function () {
if (this.timerId) {
debug("[raygun.batch.ts] Batch transport - stopping");
clearInterval(this.timerId);
this.timerId = null;
}
};
RaygunBatchTransport.prototype.onIncomingMessage = function (serializedMessage) {
var _this = this;
var messageLength = Buffer.byteLength(serializedMessage, "utf-8");
if (messageLength >= MAX_BATCH_INNER_SIZE_BYTES) {
var messageSize = Math.ceil(messageLength / 1024);
var startOfMessage = serializedMessage.slice(0, 1000);
var errorMessage = "Error is too large to send to Raygun (".concat(messageSize, "kb)\nStart of error: ").concat(startOfMessage);
console.error("[Raygun4Node] ".concat(errorMessage));
throw Error(errorMessage);
}
var messageIsTooLargeToAddToBatch = this.batchState.messageSizeInBytes + messageLength >
MAX_BATCH_INNER_SIZE_BYTES;
if (messageIsTooLargeToAddToBatch) {
this.processBatch();
}
if (this.batchState.messages.length === 0) {
this.batchState.messageSizeInBytes += messageLength;
}
else {
this.batchState.messageSizeInBytes += messageLength + 1; // to account for the commas between items
}
var promise = new Promise(function (resolve, reject) {
var promise = { resolve: resolve, reject: reject };
_this.batchState.messages.push({ serializedMessage: serializedMessage, promise: promise });
});
var batchIsFull = this.batchState.messages.length === 100;
if (batchIsFull) {
this.processBatch();
}
return promise;
};
RaygunBatchTransport.prototype.processBatch = function () {
var payload = this.batchState.messages
.map(function (m) { return m.serializedMessage; })
.join(",");
var batch = {
payload: "[".concat(payload, "]"),
messageCount: this.batchState.messages.length,
promises: this.batchState.messages.map(function (m) { return m.promise; }),
};
this.sendBatch(batch);
this.batchState = { messages: [], messageSizeInBytes: 0 };
this.stopProcessing();
};
RaygunBatchTransport.prototype.sendBatch = function (batch) {
var payload = batch.payload, messageCount = batch.messageCount, promises = batch.promises;
debug("[raygun.batch.ts] Batch transport - processing (".concat(messageCount, " message(s) in batch)"));
var batchId = this.batchId;
this.batchId++;
var resolvePromises = function (err, response) {
var durationInMs = stopTimer();
if (err) {
debug("[raygun.batch.ts] Batch transport - error sending batch (id=".concat(batchId, ", duration=").concat(durationInMs, "ms): ").concat(err));
for (var _i = 0, promises_1 = promises; _i < promises_1.length; _i++) {
var promise = promises_1[_i];
promise.reject(err);
}
}
else {
debug("[raygun.batch.ts] Batch transport - successfully sent batch (id=".concat(batchId, ", duration=").concat(durationInMs, "ms)"));
for (var _a = 0, promises_2 = promises; _a < promises_2.length; _a++) {
var promise = promises_2[_a];
promise.resolve(response);
}
}
};
debug("[raygun.batch.ts] Batch transport - sending batch (id=".concat(batchId, ", ").concat(messageCount, " messages, ").concat(payload.length, " bytes)"));
var stopTimer = (0, timer_1.startTimer)();
(0, raygun_transport_1.sendBatch)({
message: payload,
http: this.httpOptions,
})
.then(function (response) {
resolvePromises(null, response);
})
.catch(function (error) {
resolvePromises(error, null);
});
};
return RaygunBatchTransport;
}());
exports.RaygunBatchTransport = RaygunBatchTransport;
;