Socket
Socket
Sign inDemoInstall

@datadog/browser-core

Package Overview
Dependencies
0
Maintainers
1
Versions
248
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.30.1 to 4.31.0

2

cjs/boot/init.js

@@ -9,3 +9,3 @@ "use strict";

var publicApi = (0, utils_1.assign)({
version: "4.30.1",
version: "4.31.0",
// This API method is intentionally not monitored, since the only thing executed is the

@@ -12,0 +12,0 @@ // user-provided 'callback'. All SDK usages executed in the callback should be monitored, and

@@ -25,3 +25,3 @@ "use strict";

build: function (api, retry) {
var tags = ["sdk_version:".concat("4.30.1"), "api:".concat(api)].concat(configurationTags);
var tags = ["sdk_version:".concat("4.31.0"), "api:".concat(api)].concat(configurationTags);
if (retry) {

@@ -34,3 +34,3 @@ tags.push("retry_count:".concat(retry.count), "retry_after:".concat(retry.lastFailureStatus));

"dd-api-key=".concat(clientToken),
"dd-evp-origin-version=".concat(encodeURIComponent("4.30.1")),
"dd-evp-origin-version=".concat(encodeURIComponent("4.31.0")),
'dd-evp-origin=browser',

@@ -37,0 +37,0 @@ "dd-request-id=".concat((0, utils_1.generateUUID)()),

@@ -14,2 +14,3 @@ import type { Context } from '../../tools/context';

observable: Observable<TelemetryEvent & Context>;
enabled: boolean;
}

@@ -16,0 +17,0 @@ export declare function startTelemetry(telemetryService: TelemetryService, configuration: Configuration): Telemetry;

@@ -26,7 +26,8 @@ "use strict";

var observable = new observable_1.Observable();
telemetryConfiguration.telemetryEnabled = (0, utils_1.performDraw)(configuration.telemetrySampleRate);
telemetryConfiguration.telemetryEnabled =
!(0, utils_1.includes)(TELEMETRY_EXCLUDED_SITES, configuration.site) && (0, utils_1.performDraw)(configuration.telemetrySampleRate);
telemetryConfiguration.telemetryConfigurationEnabled =
telemetryConfiguration.telemetryEnabled && (0, utils_1.performDraw)(configuration.telemetryConfigurationSampleRate);
onRawTelemetryEventCollected = function (rawEvent) {
if (!(0, utils_1.includes)(TELEMETRY_EXCLUDED_SITES, configuration.site) && telemetryConfiguration.telemetryEnabled) {
if (telemetryConfiguration.telemetryEnabled) {
var event_1 = toTelemetryEvent(telemetryService, rawEvent);

@@ -47,3 +48,3 @@ observable.notify(event_1);

service: telemetryService,
version: "4.30.1",
version: "4.31.0",
source: 'browser',

@@ -62,2 +63,3 @@ _dd: {

observable: observable,
enabled: telemetryConfiguration.telemetryEnabled,
};

@@ -64,0 +66,0 @@ }

@@ -11,3 +11,3 @@ export { Configuration, InitConfiguration, buildCookieOptions, validateAndBuildConfiguration, DefaultPrivacyLevel, EndpointBuilder, isExperimentalFeatureEnabled, updateExperimentalFeatures, resetExperimentalFeatures, serializeConfiguration, } from './domain/configuration';

export { SESSION_TIME_OUT_DELAY, } from './domain/session/sessionConstants';
export { HttpRequest, Payload, createHttpRequest, Batch, canUseEventBridge, getEventBridge, startBatchWithReplica, } from './transport';
export { HttpRequest, Payload, createHttpRequest, Batch, BatchFlushEvent, canUseEventBridge, getEventBridge, startBatchWithReplica, } from './transport';
export * from './tools/display';

@@ -33,3 +33,3 @@ export * from './tools/urlPolyfill';

export { catchUserErrors } from './tools/catchUserErrors';
export { createContextManager } from './tools/contextManager';
export { createContextManager, ContextManager } from './tools/contextManager';
export { limitModification } from './tools/limitModification';

@@ -36,0 +36,0 @@ export { ContextHistory, ContextHistoryEntry, CLEAR_OLD_CONTEXTS_INTERVAL } from './tools/contextHistory';

@@ -0,3 +1,6 @@

import { computeBytesCount } from './utils';
import type { Context } from './context';
export declare function createContextManager(): {
export declare type ContextManager = ReturnType<typeof createContextManager>;
export declare function createContextManager(computeBytesCountImpl?: typeof computeBytesCount): {
getBytesCount: () => number;
/** @deprecated use getContext instead */

@@ -4,0 +7,0 @@ get: () => Context;

@@ -5,5 +5,13 @@ "use strict";

var utils_1 = require("./utils");
function createContextManager() {
function createContextManager(computeBytesCountImpl) {
if (computeBytesCountImpl === void 0) { computeBytesCountImpl = utils_1.computeBytesCount; }
var context = {};
var bytesCountCache;
return {
getBytesCount: function () {
if (bytesCountCache === undefined) {
bytesCountCache = computeBytesCountImpl((0, utils_1.jsonStringify)(context));
}
return bytesCountCache;
},
/** @deprecated use getContext instead */

@@ -14,2 +22,3 @@ get: function () { return context; },

context[key] = value;
bytesCountCache = undefined;
},

@@ -19,2 +28,3 @@ /** @deprecated renamed to removeContextProperty */

delete context[key];
bytesCountCache = undefined;
},

@@ -24,2 +34,3 @@ /** @deprecated use setContext instead */

context = newContext;
bytesCountCache = undefined;
},

@@ -29,11 +40,15 @@ getContext: function () { return (0, utils_1.deepClone)(context); },

context = (0, utils_1.deepClone)(newContext);
bytesCountCache = undefined;
},
setContextProperty: function (key, property) {
context[key] = (0, utils_1.deepClone)(property);
bytesCountCache = undefined;
},
removeContextProperty: function (key) {
delete context[key];
bytesCountCache = undefined;
},
clearContext: function () {
context = {};
bytesCountCache = undefined;
},

@@ -40,0 +55,0 @@ };

@@ -138,2 +138,3 @@ export declare const ONE_SECOND = 1000;

export declare function cssEscape(str: string): string;
export declare function computeBytesCount(candidate: string): number;
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cssEscape = exports.matchList = exports.isMatchOption = exports.removeDuplicates = exports.requestIdleCallback = exports.combine = exports.deepClone = exports.mergeInto = exports.getType = exports.elementMatches = exports.safeTruncate = exports.findCommaSeparatedValue = exports.getLinkElementOrigin = exports.getLocationOrigin = exports.getGlobalObject = exports.endsWith = exports.startsWith = exports.mapValues = exports.isEmptyObject = exports.objectEntries = exports.objectHasValue = exports.objectValues = exports.isNumber = exports.isPercentage = exports.findLast = exports.find = exports.arrayFrom = exports.includes = exports.jsonStringify = exports.noop = exports.round = exports.performDraw = exports.generateUUID = exports.shallowClone = exports.assign = exports.throttle = exports.ONE_MEBI_BYTE = exports.ONE_KIBI_BYTE = exports.ONE_YEAR = exports.ONE_DAY = exports.ONE_HOUR = exports.ONE_MINUTE = exports.ONE_SECOND = void 0;
exports.computeBytesCount = exports.cssEscape = exports.matchList = exports.isMatchOption = exports.removeDuplicates = exports.requestIdleCallback = exports.combine = exports.deepClone = exports.mergeInto = exports.getType = exports.elementMatches = exports.safeTruncate = exports.findCommaSeparatedValue = exports.getLinkElementOrigin = exports.getLocationOrigin = exports.getGlobalObject = exports.endsWith = exports.startsWith = exports.mapValues = exports.isEmptyObject = exports.objectEntries = exports.objectHasValue = exports.objectValues = exports.isNumber = exports.isPercentage = exports.findLast = exports.find = exports.arrayFrom = exports.includes = exports.jsonStringify = exports.noop = exports.round = exports.performDraw = exports.generateUUID = exports.shallowClone = exports.assign = exports.throttle = exports.ONE_MEBI_BYTE = exports.ONE_KIBI_BYTE = exports.ONE_YEAR = exports.ONE_DAY = exports.ONE_HOUR = exports.ONE_MINUTE = exports.ONE_SECOND = void 0;
var display_1 = require("./display");

@@ -483,2 +483,15 @@ var monitor_1 = require("./monitor");

exports.cssEscape = cssEscape;
// eslint-disable-next-line no-control-regex
var HAS_MULTI_BYTES_CHARACTERS = /[^\u0000-\u007F]/;
function computeBytesCount(candidate) {
// Accurate bytes count computations can degrade performances when there is a lot of events to process
if (!HAS_MULTI_BYTES_CHARACTERS.test(candidate)) {
return candidate.length;
}
if (window.TextEncoder !== undefined) {
return new TextEncoder().encode(candidate).length;
}
return new Blob([candidate]).size;
}
exports.computeBytesCount = computeBytesCount;
//# sourceMappingURL=utils.js.map
import type { Context } from '../tools/context';
import type { Observable } from '../tools/observable';
import { Observable } from '../tools/observable';
import type { PageExitEvent } from '../browser/pageExitObservable';
import type { HttpRequest } from './httpRequest';
export interface BatchFlushEvent {
bufferBytesCount: number;
bufferMessagesCount: number;
}
export declare class Batch {

@@ -12,2 +16,3 @@ private request;

private pageExitObservable;
flushObservable: Observable<BatchFlushEvent>;
private pushOnlyBuffer;

@@ -21,3 +26,2 @@ private upsertBuffer;

flush(sendFn?: (payload: import("./httpRequest").Payload) => void): void;
computeBytesCount(candidate: string): number;
private addOrUpdate;

@@ -24,0 +28,0 @@ private process;

@@ -7,5 +7,3 @@ "use strict";

var monitor_1 = require("../tools/monitor");
// https://en.wikipedia.org/wiki/UTF-8
// eslint-disable-next-line no-control-regex
var HAS_MULTI_BYTES_CHARACTERS = /[^\u0000-\u007F]/;
var observable_1 = require("../tools/observable");
var Batch = /** @class */ (function () {

@@ -20,2 +18,3 @@ function Batch(request, batchMessagesLimit, batchBytesLimit, messageBytesLimit, flushTimeout, pageExitObservable) {

this.pageExitObservable = pageExitObservable;
this.flushObservable = new observable_1.Observable();
this.pushOnlyBuffer = [];

@@ -39,2 +38,6 @@ this.upsertBuffer = {};

var bytesCount = this.bufferBytesCount;
this.flushObservable.notify({
bufferBytesCount: this.bufferBytesCount,
bufferMessagesCount: this.bufferMessagesCount,
});
this.pushOnlyBuffer = [];

@@ -47,12 +50,2 @@ this.upsertBuffer = {};

};
Batch.prototype.computeBytesCount = function (candidate) {
// Accurate bytes count computations can degrade performances when there is a lot of events to process
if (!HAS_MULTI_BYTES_CHARACTERS.test(candidate)) {
return candidate.length;
}
if (window.TextEncoder !== undefined) {
return new TextEncoder().encode(candidate).length;
}
return new Blob([candidate]).size;
};
Batch.prototype.addOrUpdate = function (message, key) {

@@ -77,3 +70,3 @@ var _a = this.process(message), processedMessage = _a.processedMessage, messageBytesCount = _a.messageBytesCount;

var processedMessage = (0, utils_1.jsonStringify)(message);
var messageBytesCount = this.computeBytesCount(processedMessage);
var messageBytesCount = (0, utils_1.computeBytesCount)(processedMessage);
return { processedMessage: processedMessage, messageBytesCount: messageBytesCount };

@@ -98,3 +91,3 @@ };

delete this.upsertBuffer[key];
var messageBytesCount = this.computeBytesCount(removedMessage);
var messageBytesCount = (0, utils_1.computeBytesCount)(removedMessage);
this.bufferBytesCount -= messageBytesCount;

@@ -101,0 +94,0 @@ this.bufferMessagesCount -= 1;

export { HttpRequest, createHttpRequest, Payload, RetryInfo } from './httpRequest';
export { Batch } from './batch';
export { Batch, BatchFlushEvent } from './batch';
export { canUseEventBridge, getEventBridge, BrowserWindowWithEventBridge } from './eventBridge';
export { startBatchWithReplica } from './startBatchWithReplica';

@@ -6,3 +6,3 @@ import { catchUserErrors } from '../tools/catchUserErrors';

var publicApi = assign({
version: "4.30.1",
version: "4.31.0",
// This API method is intentionally not monitored, since the only thing executed is the

@@ -9,0 +9,0 @@ // user-provided 'callback'. All SDK usages executed in the callback should be monitored, and

@@ -22,3 +22,3 @@ import { timeStampNow } from '../../tools/timeUtils';

build: function (api, retry) {
var tags = ["sdk_version:".concat("4.30.1"), "api:".concat(api)].concat(configurationTags);
var tags = ["sdk_version:".concat("4.31.0"), "api:".concat(api)].concat(configurationTags);
if (retry) {

@@ -31,3 +31,3 @@ tags.push("retry_count:".concat(retry.count), "retry_after:".concat(retry.lastFailureStatus));

"dd-api-key=".concat(clientToken),
"dd-evp-origin-version=".concat(encodeURIComponent("4.30.1")),
"dd-evp-origin-version=".concat(encodeURIComponent("4.31.0")),
'dd-evp-origin=browser',

@@ -34,0 +34,0 @@ "dd-request-id=".concat(generateUUID()),

@@ -14,2 +14,3 @@ import type { Context } from '../../tools/context';

observable: Observable<TelemetryEvent & Context>;
enabled: boolean;
}

@@ -16,0 +17,0 @@ export declare function startTelemetry(telemetryService: TelemetryService, configuration: Configuration): Telemetry;

@@ -23,7 +23,8 @@ import { ConsoleApiName } from '../../tools/display';

var observable = new Observable();
telemetryConfiguration.telemetryEnabled = performDraw(configuration.telemetrySampleRate);
telemetryConfiguration.telemetryEnabled =
!includes(TELEMETRY_EXCLUDED_SITES, configuration.site) && performDraw(configuration.telemetrySampleRate);
telemetryConfiguration.telemetryConfigurationEnabled =
telemetryConfiguration.telemetryEnabled && performDraw(configuration.telemetryConfigurationSampleRate);
onRawTelemetryEventCollected = function (rawEvent) {
if (!includes(TELEMETRY_EXCLUDED_SITES, configuration.site) && telemetryConfiguration.telemetryEnabled) {
if (telemetryConfiguration.telemetryEnabled) {
var event_1 = toTelemetryEvent(telemetryService, rawEvent);

@@ -44,3 +45,3 @@ observable.notify(event_1);

service: telemetryService,
version: "4.30.1",
version: "4.31.0",
source: 'browser',

@@ -59,2 +60,3 @@ _dd: {

observable: observable,
enabled: telemetryConfiguration.telemetryEnabled,
};

@@ -61,0 +63,0 @@ }

@@ -11,3 +11,3 @@ export { Configuration, InitConfiguration, buildCookieOptions, validateAndBuildConfiguration, DefaultPrivacyLevel, EndpointBuilder, isExperimentalFeatureEnabled, updateExperimentalFeatures, resetExperimentalFeatures, serializeConfiguration, } from './domain/configuration';

export { SESSION_TIME_OUT_DELAY, } from './domain/session/sessionConstants';
export { HttpRequest, Payload, createHttpRequest, Batch, canUseEventBridge, getEventBridge, startBatchWithReplica, } from './transport';
export { HttpRequest, Payload, createHttpRequest, Batch, BatchFlushEvent, canUseEventBridge, getEventBridge, startBatchWithReplica, } from './transport';
export * from './tools/display';

@@ -33,3 +33,3 @@ export * from './tools/urlPolyfill';

export { catchUserErrors } from './tools/catchUserErrors';
export { createContextManager } from './tools/contextManager';
export { createContextManager, ContextManager } from './tools/contextManager';
export { limitModification } from './tools/limitModification';

@@ -36,0 +36,0 @@ export { ContextHistory, ContextHistoryEntry, CLEAR_OLD_CONTEXTS_INTERVAL } from './tools/contextHistory';

@@ -0,3 +1,6 @@

import { computeBytesCount } from './utils';
import type { Context } from './context';
export declare function createContextManager(): {
export declare type ContextManager = ReturnType<typeof createContextManager>;
export declare function createContextManager(computeBytesCountImpl?: typeof computeBytesCount): {
getBytesCount: () => number;
/** @deprecated use getContext instead */

@@ -4,0 +7,0 @@ get: () => Context;

@@ -1,5 +0,13 @@

import { deepClone } from './utils';
export function createContextManager() {
import { computeBytesCount, deepClone, jsonStringify } from './utils';
export function createContextManager(computeBytesCountImpl) {
if (computeBytesCountImpl === void 0) { computeBytesCountImpl = computeBytesCount; }
var context = {};
var bytesCountCache;
return {
getBytesCount: function () {
if (bytesCountCache === undefined) {
bytesCountCache = computeBytesCountImpl(jsonStringify(context));
}
return bytesCountCache;
},
/** @deprecated use getContext instead */

@@ -10,2 +18,3 @@ get: function () { return context; },

context[key] = value;
bytesCountCache = undefined;
},

@@ -15,2 +24,3 @@ /** @deprecated renamed to removeContextProperty */

delete context[key];
bytesCountCache = undefined;
},

@@ -20,2 +30,3 @@ /** @deprecated use setContext instead */

context = newContext;
bytesCountCache = undefined;
},

@@ -25,11 +36,15 @@ getContext: function () { return deepClone(context); },

context = deepClone(newContext);
bytesCountCache = undefined;
},
setContextProperty: function (key, property) {
context[key] = deepClone(property);
bytesCountCache = undefined;
},
removeContextProperty: function (key) {
delete context[key];
bytesCountCache = undefined;
},
clearContext: function () {
context = {};
bytesCountCache = undefined;
},

@@ -36,0 +51,0 @@ };

@@ -138,2 +138,3 @@ export declare const ONE_SECOND = 1000;

export declare function cssEscape(str: string): string;
export declare function computeBytesCount(candidate: string): number;
export {};

@@ -444,2 +444,14 @@ import { display } from './display';

}
// eslint-disable-next-line no-control-regex
var HAS_MULTI_BYTES_CHARACTERS = /[^\u0000-\u007F]/;
export function computeBytesCount(candidate) {
// Accurate bytes count computations can degrade performances when there is a lot of events to process
if (!HAS_MULTI_BYTES_CHARACTERS.test(candidate)) {
return candidate.length;
}
if (window.TextEncoder !== undefined) {
return new TextEncoder().encode(candidate).length;
}
return new Blob([candidate]).size;
}
//# sourceMappingURL=utils.js.map
import type { Context } from '../tools/context';
import type { Observable } from '../tools/observable';
import { Observable } from '../tools/observable';
import type { PageExitEvent } from '../browser/pageExitObservable';
import type { HttpRequest } from './httpRequest';
export interface BatchFlushEvent {
bufferBytesCount: number;
bufferMessagesCount: number;
}
export declare class Batch {

@@ -12,2 +16,3 @@ private request;

private pageExitObservable;
flushObservable: Observable<BatchFlushEvent>;
private pushOnlyBuffer;

@@ -21,3 +26,2 @@ private upsertBuffer;

flush(sendFn?: (payload: import("./httpRequest").Payload) => void): void;
computeBytesCount(candidate: string): number;
private addOrUpdate;

@@ -24,0 +28,0 @@ private process;

import { display } from '../tools/display';
import { jsonStringify, objectValues } from '../tools/utils';
import { computeBytesCount, jsonStringify, objectValues } from '../tools/utils';
import { monitor } from '../tools/monitor';
// https://en.wikipedia.org/wiki/UTF-8
// eslint-disable-next-line no-control-regex
var HAS_MULTI_BYTES_CHARACTERS = /[^\u0000-\u007F]/;
import { Observable } from '../tools/observable';
var Batch = /** @class */ (function () {

@@ -16,2 +14,3 @@ function Batch(request, batchMessagesLimit, batchBytesLimit, messageBytesLimit, flushTimeout, pageExitObservable) {

this.pageExitObservable = pageExitObservable;
this.flushObservable = new Observable();
this.pushOnlyBuffer = [];

@@ -35,2 +34,6 @@ this.upsertBuffer = {};

var bytesCount = this.bufferBytesCount;
this.flushObservable.notify({
bufferBytesCount: this.bufferBytesCount,
bufferMessagesCount: this.bufferMessagesCount,
});
this.pushOnlyBuffer = [];

@@ -43,12 +46,2 @@ this.upsertBuffer = {};

};
Batch.prototype.computeBytesCount = function (candidate) {
// Accurate bytes count computations can degrade performances when there is a lot of events to process
if (!HAS_MULTI_BYTES_CHARACTERS.test(candidate)) {
return candidate.length;
}
if (window.TextEncoder !== undefined) {
return new TextEncoder().encode(candidate).length;
}
return new Blob([candidate]).size;
};
Batch.prototype.addOrUpdate = function (message, key) {

@@ -73,3 +66,3 @@ var _a = this.process(message), processedMessage = _a.processedMessage, messageBytesCount = _a.messageBytesCount;

var processedMessage = jsonStringify(message);
var messageBytesCount = this.computeBytesCount(processedMessage);
var messageBytesCount = computeBytesCount(processedMessage);
return { processedMessage: processedMessage, messageBytesCount: messageBytesCount };

@@ -94,3 +87,3 @@ };

delete this.upsertBuffer[key];
var messageBytesCount = this.computeBytesCount(removedMessage);
var messageBytesCount = computeBytesCount(removedMessage);
this.bufferBytesCount -= messageBytesCount;

@@ -97,0 +90,0 @@ this.bufferMessagesCount -= 1;

export { HttpRequest, createHttpRequest, Payload, RetryInfo } from './httpRequest';
export { Batch } from './batch';
export { Batch, BatchFlushEvent } from './batch';
export { canUseEventBridge, getEventBridge, BrowserWindowWithEventBridge } from './eventBridge';
export { startBatchWithReplica } from './startBatchWithReplica';
{
"name": "@datadog/browser-core",
"version": "4.30.1",
"version": "4.31.0",
"license": "Apache-2.0",

@@ -24,3 +24,3 @@ "main": "cjs/index.js",

},
"gitHead": "2427f67053ca6041dd81eb565ea0d49483811f81"
"gitHead": "7e55e95c33caaae9c7fb905b421fe319c22ad17e"
}

@@ -35,2 +35,3 @@ import type { Context } from '../../tools/context'

observable: Observable<TelemetryEvent & Context>
enabled: boolean
}

@@ -53,3 +54,4 @@

telemetryConfiguration.telemetryEnabled = performDraw(configuration.telemetrySampleRate)
telemetryConfiguration.telemetryEnabled =
!includes(TELEMETRY_EXCLUDED_SITES, configuration.site) && performDraw(configuration.telemetrySampleRate)
telemetryConfiguration.telemetryConfigurationEnabled =

@@ -59,3 +61,3 @@ telemetryConfiguration.telemetryEnabled && performDraw(configuration.telemetryConfigurationSampleRate)

onRawTelemetryEventCollected = (rawEvent: RawTelemetryEvent) => {
if (!includes(TELEMETRY_EXCLUDED_SITES, configuration.site) && telemetryConfiguration.telemetryEnabled) {
if (telemetryConfiguration.telemetryEnabled) {
const event = toTelemetryEvent(telemetryService, rawEvent)

@@ -96,2 +98,3 @@ observable.notify(event)

observable,
enabled: telemetryConfiguration.telemetryEnabled,
}

@@ -98,0 +101,0 @@ }

@@ -50,2 +50,3 @@ export {

Batch,
BatchFlushEvent,
canUseEventBridge,

@@ -85,3 +86,3 @@ getEventBridge,

export { catchUserErrors } from './tools/catchUserErrors'
export { createContextManager } from './tools/contextManager'
export { createContextManager, ContextManager } from './tools/contextManager'
export { limitModification } from './tools/limitModification'

@@ -88,0 +89,0 @@ export { ContextHistory, ContextHistoryEntry, CLEAR_OLD_CONTEXTS_INTERVAL } from './tools/contextHistory'

@@ -76,2 +76,28 @@ import { createContextManager } from './contextManager'

})
it('should compute the bytes count only if the context has been updated', () => {
const computeBytesCountStub = jasmine.createSpy('computeBytesCountStub').and.returnValue(1)
const manager = createContextManager(computeBytesCountStub)
manager.getBytesCount()
manager.remove('foo')
manager.getBytesCount()
manager.set({ foo: 'bar' })
manager.getBytesCount()
manager.removeContextProperty('foo')
manager.getBytesCount()
manager.setContext({ foo: 'bar' })
manager.getBytesCount()
manager.clearContext()
manager.getBytesCount()
const bytesCount = manager.getBytesCount()
expect(bytesCount).toEqual(1)
expect(computeBytesCountStub).toHaveBeenCalledTimes(6)
})
})

@@ -1,9 +0,17 @@

import { deepClone } from './utils'
import { computeBytesCount, deepClone, jsonStringify } from './utils'
import type { Context, ContextValue } from './context'
export function createContextManager() {
export type ContextManager = ReturnType<typeof createContextManager>
export function createContextManager(computeBytesCountImpl = computeBytesCount) {
let context: Context = {}
let bytesCountCache: number | undefined
return {
getBytesCount: () => {
if (bytesCountCache === undefined) {
bytesCountCache = computeBytesCountImpl(jsonStringify(context)!)
}
return bytesCountCache
},
/** @deprecated use getContext instead */

@@ -15,2 +23,3 @@ get: () => context,

context[key] = value as ContextValue
bytesCountCache = undefined
},

@@ -21,2 +30,3 @@

delete context[key]
bytesCountCache = undefined
},

@@ -27,2 +37,3 @@

context = newContext as Context
bytesCountCache = undefined
},

@@ -34,2 +45,3 @@

context = deepClone(newContext)
bytesCountCache = undefined
},

@@ -39,2 +51,3 @@

context[key] = deepClone(property)
bytesCountCache = undefined
},

@@ -44,2 +57,3 @@

delete context[key]
bytesCountCache = undefined
},

@@ -49,4 +63,5 @@

context = {}
bytesCountCache = undefined
},
}
}

@@ -7,2 +7,3 @@ import type { Clock } from '../../test/specHelper'

combine,
computeBytesCount,
cssEscape,

@@ -693,1 +694,11 @@ deepClone,

})
describe('computeBytesCount', () => {
it('should count the bytes of a message composed of 1 byte characters', () => {
expect(computeBytesCount('1234')).toEqual(4)
})
it('should count the bytes of a message composed of multiple bytes characters', () => {
expect(computeBytesCount('🪐')).toEqual(4)
})
})

@@ -597,1 +597,17 @@ import { display } from './display'

}
// eslint-disable-next-line no-control-regex
const HAS_MULTI_BYTES_CHARACTERS = /[^\u0000-\u007F]/
export function computeBytesCount(candidate: string): number {
// Accurate bytes count computations can degrade performances when there is a lot of events to process
if (!HAS_MULTI_BYTES_CHARACTERS.test(candidate)) {
return candidate.length
}
if (window.TextEncoder !== undefined) {
return new TextEncoder().encode(candidate).length
}
return new Blob([candidate]).size
}

@@ -18,2 +18,3 @@ import sinon from 'sinon'

let pageExitObservable: Observable<PageExitEvent>
let flushNotifySpy: jasmine.Spy

@@ -32,2 +33,3 @@ beforeEach(() => {

)
flushNotifySpy = spyOn(batch.flushObservable, 'notify')
})

@@ -56,10 +58,2 @@

it('should count the bytes of a message composed of 1 byte characters', () => {
expect(batch.computeBytesCount('1234')).toEqual(4)
})
it('should count the bytes of a message composed of multiple bytes characters', () => {
expect(batch.computeBytesCount('🪐')).toEqual(4)
})
it('should flush when the message count limit is reached', () => {

@@ -195,2 +189,8 @@ batch.add({ message: '1' })

})
it('should notify when the batch is flushed', () => {
batch.add({})
batch.flush()
expect(flushNotifySpy).toHaveBeenCalledOnceWith({ bufferBytesCount: 2, bufferMessagesCount: 1 })
})
})
import { display } from '../tools/display'
import type { Context } from '../tools/context'
import { jsonStringify, objectValues } from '../tools/utils'
import { computeBytesCount, jsonStringify, objectValues } from '../tools/utils'
import { monitor } from '../tools/monitor'
import type { Observable } from '../tools/observable'
import { Observable } from '../tools/observable'
import type { PageExitEvent } from '../browser/pageExitObservable'
import type { HttpRequest } from './httpRequest'
// https://en.wikipedia.org/wiki/UTF-8
// eslint-disable-next-line no-control-regex
const HAS_MULTI_BYTES_CHARACTERS = /[^\u0000-\u007F]/
export interface BatchFlushEvent {
bufferBytesCount: number
bufferMessagesCount: number
}
export class Batch {
flushObservable = new Observable<BatchFlushEvent>()
private pushOnlyBuffer: string[] = []

@@ -44,2 +47,7 @@ private upsertBuffer: { [key: string]: string } = {}

this.flushObservable.notify({
bufferBytesCount: this.bufferBytesCount,
bufferMessagesCount: this.bufferMessagesCount,
})
this.pushOnlyBuffer = []

@@ -54,15 +62,2 @@ this.upsertBuffer = {}

computeBytesCount(candidate: string) {
// Accurate bytes count computations can degrade performances when there is a lot of events to process
if (!HAS_MULTI_BYTES_CHARACTERS.test(candidate)) {
return candidate.length
}
if (window.TextEncoder !== undefined) {
return new TextEncoder().encode(candidate).length
}
return new Blob([candidate]).size
}
private addOrUpdate(message: Context, key?: string) {

@@ -91,3 +86,3 @@ const { processedMessage, messageBytesCount } = this.process(message)

const processedMessage = jsonStringify(message)!
const messageBytesCount = this.computeBytesCount(processedMessage)
const messageBytesCount = computeBytesCount(processedMessage)
return { processedMessage, messageBytesCount }

@@ -113,3 +108,3 @@ }

delete this.upsertBuffer[key]
const messageBytesCount = this.computeBytesCount(removedMessage)
const messageBytesCount = computeBytesCount(removedMessage)
this.bufferBytesCount -= messageBytesCount

@@ -116,0 +111,0 @@ this.bufferMessagesCount -= 1

export { HttpRequest, createHttpRequest, Payload, RetryInfo } from './httpRequest'
export { Batch } from './batch'
export { Batch, BatchFlushEvent } from './batch'
export { canUseEventBridge, getEventBridge, BrowserWindowWithEventBridge } from './eventBridge'
export { startBatchWithReplica } from './startBatchWithReplica'

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc