From 8f077ffaba64dcd9d82dbd71201c1a4c20b592f2 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Sun, 15 Nov 2026 12:55:00 -0400 Subject: [PATCH] fix: apply configured limits in bulk mode --- lib/countly-bulk.js | 6 ++--- test/tests_bulk_limits.js | 53 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 test/tests_bulk_limits.js diff --git a/lib/countly-bulk.js b/lib/countly-bulk.js index 16ce0f4..25ca376 100644 --- a/lib/countly-bulk.js +++ b/lib/countly-bulk.js @@ -252,8 +252,8 @@ function CountlyBulk(conf) { if (!event.count) { event.count = 1; } - event.key = cc.truncateSingleValue(event.key, self.maxKeyLength, "add_event", self.debug); - event.segmentation = cc.truncateObject(event.segmentation, self.maxKeyLength, self.maxValueSize, self.maxSegmentationValues, "add_event", self.debug); + event.key = cc.truncateSingleValue(event.key, conf.maxKeyLength, "add_event", self.debug); + event.segmentation = cc.truncateObject(event.segmentation, conf.maxKeyLength, conf.maxValueSize, conf.maxSegmentationValues, "add_event", self.debug); var props = ["key", "count", "sum", "dur", "segmentation", "timestamp"]; var e = cc.makeJsonSafe(cc.getProperties(event, props), "add_event"); e.timestamp = e.timestamp || getMsTimestamp(); @@ -352,7 +352,7 @@ function CountlyBulk(conf) { * var user = server.add_user({device_id:"my_device_id"}); * */ this.add_user = function(userConf) { - userConf = cc.truncateObject(userConf, self.maxKeyLength, self.maxValueSize, self.maxSegmentationValues, "add_user", self.debug); + userConf = cc.truncateObject(userConf, conf.maxKeyLength, conf.maxValueSize, conf.maxSegmentationValues, "add_user", self.debug); userConf.server = this; return new BulkUser(userConf); }; diff --git a/test/tests_bulk_limits.js b/test/tests_bulk_limits.js new file mode 100644 index 0000000..cfdd97b --- /dev/null +++ b/test/tests_bulk_limits.js @@ -0,0 +1,53 @@ +const assert = require("assert"); +const CountlyBulk = require("../lib/countly-bulk"); + +describe("Bulk internal limits", () => { + it("applies configured event key, segmentation key, value and count limits", () => { + const bulk = new CountlyBulk({ + app_key: "test", + url: "http://127.0.0.1:1", + max_key_length: 4, + max_value_size: 5, + max_segmentation_values: 1, + }); + bulk.add_event("user", { + key: "checkout_completed", + segmentation: { category: "overlong-value", another: "second-value" }, + }); + const event = bulk._getBulkEventQueue().user[0]; + assert.strictEqual(event.key, "chec"); + assert.deepStrictEqual(event.segmentation, { cate: "overl" }); + }); + + it("applies default event limits when no overrides are supplied", () => { + const bulk = new CountlyBulk({ app_key: "test", url: "http://127.0.0.1:1" }); + const segmentation = {}; + for (let i = 0; i < 101; i++) { + segmentation[`${i}-${"k".repeat(130)}`] = "v".repeat(300); + } + bulk.add_event("user", { key: "e".repeat(130), segmentation }); + const event = bulk._getBulkEventQueue().user[0]; + assert.strictEqual(event.key.length, 128); + assert.strictEqual(Object.keys(event.segmentation).length, 100); + Object.keys(event.segmentation).forEach((key) => { + assert.strictEqual(key.length, 128); + assert.strictEqual(event.segmentation[key].length, 256); + }); + }); + + it("applies configured limits to bulk user configuration", () => { + const bulk = new CountlyBulk({ + app_key: "test", + url: "http://127.0.0.1:1", + max_key_length: 12, + max_value_size: 5, + max_segmentation_values: 2, + }); + const user = bulk.add_user({ device_id: "user", city: "Long city name", country_code: "US" }); + user.begin_session(); + const request = bulk._getBulkRequestQueue()[0]; + assert.strictEqual(request.device_id, "user"); + assert.strictEqual(request.city, "Long "); + assert.strictEqual(request.country_code, undefined); + }); +});