Skip to content

Commit 8ff7f56

Browse files
refactor: extract ReportingTransport, compose into services
Extract shared transport logic (headers, rate limiting, request building, fetch) into ReportingTransport. ErrorReportingService and LoggingService now each hold a transport instance and delegate sending to it. - ErrorReportingService: owns errorUrl only, handles ERROR/WARNING - LoggingService: owns loggingUrl only, handles INFO, falls back to error reporter on failure via onError callback - Each service gets its own transport with independent rate limiting
1 parent a05157e commit 8ff7f56

2 files changed

Lines changed: 116 additions & 133 deletions

File tree

src/Rokt-Kit.js

Lines changed: 104 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ var constructor = function () {
275275
reportingConfig,
276276
errorReportingService,
277277
self.integrationName,
278+
window.__rokt_li_guid__,
278279
settings.accountId
279280
);
280281

@@ -308,6 +309,7 @@ var constructor = function () {
308309
setAllowedOriginHash: function (hash) {
309310
_allowedOriginHash = hash;
310311
},
312+
ReportingTransport: ReportingTransport,
311313
ErrorReportingService: ErrorReportingService,
312314
LoggingService: LoggingService,
313315
RateLimiter: RateLimiter,
@@ -925,18 +927,16 @@ RateLimiter.prototype.incrementAndCheck = function (severity) {
925927
return newCount > RATE_LIMIT_PER_SEVERITY;
926928
};
927929

928-
function ErrorReportingService(
930+
// --- ReportingTransport: shared transport layer for reporting services ---
931+
932+
function ReportingTransport(
929933
config,
930934
integrationName,
931935
launcherInstanceGuid,
932936
accountId,
933937
rateLimiter
934938
) {
935939
var self = this;
936-
self._loggingUrl =
937-
'https://' + ((config && config.loggingUrl) || DEFAULT_LOGGING_URL);
938-
self._errorUrl =
939-
'https://' + ((config && config.errorUrl) || DEFAULT_ERROR_URL);
940940
self._isLoggingEnabled = (config && config.isLoggingEnabled) || false;
941941
self._integrationName = integrationName || '';
942942
self._launcherInstanceGuid = launcherInstanceGuid;
@@ -946,10 +946,67 @@ function ErrorReportingService(
946946
self._isEnabled = _isReportingEnabled(self);
947947
}
948948

949-
function _isReportingEnabled(svc) {
949+
ReportingTransport.prototype.send = function (
950+
url,
951+
severity,
952+
msg,
953+
code,
954+
stackTrace,
955+
onError
956+
) {
957+
if (!this._isEnabled || this._rateLimiter.incrementAndCheck(severity)) {
958+
return;
959+
}
960+
961+
try {
962+
var logRequest = {
963+
additionalInformation: {
964+
message: msg,
965+
version: this._integrationName || '',
966+
},
967+
severity: severity,
968+
code: code || ErrorCodes.UNKNOWN_ERROR,
969+
url: _getUrl(),
970+
deviceInfo: _getUserAgent(),
971+
stackTrace: stackTrace,
972+
reporter: this._reporter,
973+
integration: this._integrationName || '',
974+
};
975+
var headers = {
976+
Accept: 'text/plain;charset=UTF-8',
977+
'Content-Type': 'application/json',
978+
'rokt-launcher-version': this._integrationName || '',
979+
'rokt-wsdk-version': 'joint',
980+
};
981+
if (this._launcherInstanceGuid) {
982+
headers['rokt-launcher-instance-guid'] = this._launcherInstanceGuid;
983+
}
984+
if (this._accountId) {
985+
headers['rokt-account-id'] = this._accountId;
986+
}
987+
var payload = {
988+
method: 'POST',
989+
headers: headers,
990+
body: JSON.stringify(logRequest),
991+
};
992+
fetch(url, payload).catch(function (error) {
993+
console.error('ReportingTransport: Failed to send log', error);
994+
if (onError) {
995+
onError(error);
996+
}
997+
});
998+
} catch (error) {
999+
console.error('ReportingTransport: Failed to send log', error);
1000+
if (onError) {
1001+
onError(error);
1002+
}
1003+
}
1004+
};
1005+
1006+
function _isReportingEnabled(transport) {
9501007
return (
9511008
_isDebugModeEnabled() ||
952-
(_isRoktDomainPresent() && svc._isLoggingEnabled)
1009+
(_isRoktDomainPresent() && transport._isLoggingEnabled)
9531010
);
9541011
}
9551012

@@ -980,134 +1037,74 @@ function _getUserAgent() {
9801037
: undefined;
9811038
}
9821039

983-
function _getVersion(svc) {
984-
return svc._integrationName || '';
985-
}
986-
987-
function _getIntegration(svc) {
988-
return svc._integrationName || '';
989-
}
990-
991-
function _getHeaders(svc) {
992-
var headers = {
993-
Accept: 'text/plain;charset=UTF-8',
994-
'Content-Type': 'application/json',
995-
'rokt-launcher-version': _getVersion(svc),
996-
'rokt-wsdk-version': 'joint',
997-
};
998-
999-
if (svc._launcherInstanceGuid) {
1000-
headers['rokt-launcher-instance-guid'] = svc._launcherInstanceGuid;
1001-
}
1002-
1003-
if (svc._accountId) {
1004-
headers['rokt-account-id'] = svc._accountId;
1005-
}
1006-
1007-
return headers;
1008-
}
1009-
1010-
function _buildLogRequest(svc, severity, msg, code, stackTrace) {
1011-
return {
1012-
additionalInformation: {
1013-
message: msg,
1014-
version: _getVersion(svc),
1015-
},
1016-
severity: severity,
1017-
code: code || ErrorCodes.UNKNOWN_ERROR,
1018-
url: _getUrl(),
1019-
deviceInfo: _getUserAgent(),
1020-
stackTrace: stackTrace,
1021-
reporter: svc._reporter,
1022-
integration: _getIntegration(svc),
1023-
};
1024-
}
1025-
1026-
function _canSendLog(svc, severity) {
1027-
return svc._isEnabled && !svc._rateLimiter.incrementAndCheck(severity);
1028-
}
1029-
1030-
function _sendToServer(svc, url, severity, msg, code, stackTrace) {
1031-
if (!_canSendLog(svc, severity)) {
1032-
return;
1033-
}
1040+
// --- ErrorReportingService: handles ERROR and WARNING severity ---
10341041

1035-
try {
1036-
var logRequest = _buildLogRequest(svc, severity, msg, code, stackTrace);
1037-
var payload = {
1038-
method: 'POST',
1039-
headers: _getHeaders(svc),
1040-
body: JSON.stringify(logRequest),
1041-
};
1042-
fetch(url, payload).catch(function (error) {
1043-
console.error('ErrorReportingService: Failed to send log', error);
1044-
});
1045-
} catch (error) {
1046-
console.error('ErrorReportingService: Failed to send log', error);
1047-
}
1042+
function ErrorReportingService(
1043+
config,
1044+
integrationName,
1045+
launcherInstanceGuid,
1046+
accountId,
1047+
rateLimiter
1048+
) {
1049+
this._transport = new ReportingTransport(
1050+
config,
1051+
integrationName,
1052+
launcherInstanceGuid,
1053+
accountId,
1054+
rateLimiter
1055+
);
1056+
this._errorUrl =
1057+
'https://' + ((config && config.errorUrl) || DEFAULT_ERROR_URL);
10481058
}
10491059

10501060
ErrorReportingService.prototype.report = function (error) {
10511061
if (!error) {
10521062
return;
10531063
}
10541064
var severity = error.severity || WSDKErrorSeverity.ERROR;
1055-
var url =
1056-
severity === WSDKErrorSeverity.INFO ? this._loggingUrl : this._errorUrl;
1057-
_sendToServer(
1058-
this,
1059-
url,
1065+
this._transport.send(
1066+
this._errorUrl,
10601067
severity,
10611068
error.message,
10621069
error.code,
10631070
error.stackTrace
10641071
);
10651072
};
10661073

1074+
// --- LoggingService: handles INFO severity ---
1075+
10671076
function LoggingService(
10681077
config,
10691078
errorReportingService,
10701079
integrationName,
1080+
launcherInstanceGuid,
10711081
accountId,
10721082
rateLimiter
10731083
) {
1074-
var self = this;
1075-
self._loggingUrl =
1084+
this._transport = new ReportingTransport(
1085+
config,
1086+
integrationName,
1087+
launcherInstanceGuid,
1088+
accountId,
1089+
rateLimiter
1090+
);
1091+
this._loggingUrl =
10761092
'https://' + ((config && config.loggingUrl) || DEFAULT_LOGGING_URL);
1077-
self._errorReportingService = errorReportingService;
1078-
self._integrationName = integrationName || '';
1079-
self._accountId = accountId || null;
1080-
self._reporter = 'mp-wsdk';
1081-
self._isLoggingEnabled = (config && config.isLoggingEnabled) || false;
1082-
self._rateLimiter = rateLimiter || new RateLimiter();
1083-
self._isEnabled = _isReportingEnabled(self);
1084-
self._launcherInstanceGuid =
1085-
errorReportingService && errorReportingService._launcherInstanceGuid;
1093+
this._errorReportingService = errorReportingService;
10861094
}
10871095

10881096
LoggingService.prototype.log = function (entry) {
10891097
if (!entry) {
10901098
return;
10911099
}
10921100
var self = this;
1093-
if (!_canSendLog(self, WSDKErrorSeverity.INFO)) {
1094-
return;
1095-
}
1096-
1097-
try {
1098-
var logRequest = _buildLogRequest(
1099-
self,
1100-
WSDKErrorSeverity.INFO,
1101-
entry.message,
1102-
entry.code
1103-
);
1104-
var payload = {
1105-
method: 'POST',
1106-
headers: _getHeaders(self),
1107-
body: JSON.stringify(logRequest),
1108-
};
1109-
fetch(self._loggingUrl, payload).catch(function (error) {
1110-
console.error('LoggingService: Failed to send log', error);
1101+
self._transport.send(
1102+
self._loggingUrl,
1103+
WSDKErrorSeverity.INFO,
1104+
entry.message,
1105+
entry.code,
1106+
undefined,
1107+
function (error) {
11111108
if (self._errorReportingService) {
11121109
self._errorReportingService.report({
11131110
message:
@@ -1116,17 +1113,8 @@ LoggingService.prototype.log = function (entry) {
11161113
severity: WSDKErrorSeverity.ERROR,
11171114
});
11181115
}
1119-
});
1120-
} catch (error) {
1121-
console.error('LoggingService: Failed to send log', error);
1122-
if (self._errorReportingService) {
1123-
self._errorReportingService.report({
1124-
message: 'LoggingService: Failed to send log: ' + error.message,
1125-
code: ErrorCodes.UNKNOWN_ERROR,
1126-
severity: WSDKErrorSeverity.ERROR,
1127-
});
11281116
}
1129-
}
1117+
);
11301118
};
11311119

11321120
if (window && window.mParticle && window.mParticle.addForwarder) {
@@ -1139,6 +1127,7 @@ if (window && window.mParticle && window.mParticle.addForwarder) {
11391127

11401128
module.exports = {
11411129
register: register,
1130+
ReportingTransport: ReportingTransport,
11421131
ErrorReportingService: ErrorReportingService,
11431132
LoggingService: LoggingService,
11441133
RateLimiter: RateLimiter,

0 commit comments

Comments
 (0)