@dynatrace/opentelemetry-core
Version:
Dynatrace OpenTelemetry core package
71 lines (68 loc) • 2.5 kB
JavaScript
;
/*
Copyright 2022 Dynatrace LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getServerId = void 0;
// x-dtc format: sn="<Session State>"[, pc="<Page Context>"], v="<Visitor Id>"[], r="<Referer>"]
// Session State format: v_4_key1_value1_key2_value2_key3_value3
// key name for serverId in Session State is "srv"
const keyOrVal = "[a-zA-Z0-9-]+";
const keyValPair = `${keyOrVal}_${keyOrVal}`;
const SrvIdRegEx = new RegExp(`^sn="v_4_(?:${keyValPair}_)*?srv_(${keyOrVal})(?:_${keyOrVal})*"`);
/**
* Extracts the server id from an x-dtc HTTP header used by RUM.
*
* @param tagValue the tag value to parse
* @returns the extracted serverid if tag is valid or undefined
*/
function getServerId(tagValue) {
const match = SrvIdRegEx.exec(tagValue);
if (match == null) {
return undefined;
}
const val = dashDecode(match[1]);
if (val == null) {
return undefined;
}
const serverId = parseInt(val, 10);
return isNaN(serverId) ? undefined : serverId;
}
exports.getServerId = getServerId;
/**
* Helper function for dash decoding.
* Input is already pre-checked above so we don't have any unexpected characters
* @param encoded the dash encoded string to decode
* @returns dash decoded string or undefined if decoding fails
*/
function dashDecode(encoded) {
let decoded = "";
for (let i = 0; i < encoded.length; i++) {
const char = encoded.charAt(i);
if (char === "-") {
if (i + 2 > encoded.length) {
return undefined;
}
const charCode = parseInt(encoded.substring(i + 1, i + 3), 16);
if (isNaN(charCode)) {
return undefined;
}
i += 2;
decoded += String.fromCharCode(charCode);
}
else {
decoded += char;
}
}
return decoded;
}
//# sourceMappingURL=XDtcHeader.js.map