@azure/monitor-opentelemetry
Version:
Azure Monitor OpenTelemetry (Node.js)
139 lines • 5.06 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import * as zlib from "zlib";
import { promisify } from "node:util";
// currently support the following encoding types
export var contentEncodingMethod;
(function (contentEncodingMethod) {
contentEncodingMethod["GZIP"] = "gzip";
contentEncodingMethod["DEFLATE"] = "deflate";
contentEncodingMethod["BR"] = "br";
})(contentEncodingMethod || (contentEncodingMethod = {}));
// current supported encoding types
export const bufferEncodingTypes = [
"utf8",
"utf16le",
"latin1",
"base64",
"hex",
"ascii",
"binary",
"ucs2",
];
// for node version under 10, Brotli compression is not supported.
export const isBrotliSupported = () => {
const majVer = process.versions.node.split(".")[0];
return parseInt(majVer) >= 10;
};
export const gzipAsync = promisify(zlib.gzip);
export const gunzipAsync = promisify(zlib.gunzip);
export const deflateAsync = promisify(zlib.deflate);
export const inflateAsync = promisify(zlib.inflate);
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export const getBrotliCompressAsync = (zlibObject) => {
const isMajorVer = isBrotliSupported();
if (isMajorVer && typeof zlibObject.brotliCompress === "function") {
return promisify(zlibObject.brotliCompress);
}
return null;
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export const getBrotliCompressSync = (zlibObject) => {
const isMajorVer = isBrotliSupported();
if (isMajorVer && typeof zlibObject.brotliCompressSync === "function") {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return zlibObject.brotliCompressSync;
}
return null;
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export const getBrotliDecompressAsync = (zlibObject) => {
const isMajorVer = isBrotliSupported();
if (isMajorVer && typeof zlibObject.brotliDecompress === "function") {
return promisify(zlibObject.brotliDecompress);
}
return null;
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export const getBrotliDecompressSync = (zlibObject) => {
const isMajorVer = isBrotliSupported();
if (isMajorVer && typeof zlibObject.brotliDecompressSync === "function") {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return zlibObject.brotliDecompressSync;
}
return null;
};
export const isBufferType = (buffer, type) => {
const encodingType = type ? type : "utf8";
let result = false;
if (Buffer.isEncoding(encodingType)) {
const newBuffer = Buffer.from(buffer.toString(encodingType), encodingType);
result = newBuffer.toJSON().data.toString() === buffer.toJSON().data.toString();
}
return result;
};
export const findBufferEncodingType = (buffer) => {
let bufferType = null;
for (const type of bufferEncodingTypes) {
if (Buffer.isEncoding(type) && isBufferType(buffer, type)) {
bufferType = type;
break;
}
}
return bufferType;
};
export const isSupportedContentEncoding = (encodingMethod) => {
let encodingType = null;
switch (encodingMethod) {
case "gzip":
encodingType = contentEncodingMethod.GZIP;
break;
case "br":
encodingType = contentEncodingMethod.BR;
break;
case "deflate":
encodingType = contentEncodingMethod.DEFLATE;
break;
default:
}
return encodingType;
};
// mutiple content-encoding is not supported
// for mutiple content-encoding, this method will return any empty array
export const getContentEncodingFromHeaders = (response) => {
const headers = [];
const contentEncodingHeaders = response.getHeader("Content-Encoding");
if (!contentEncodingHeaders)
return null;
if (typeof contentEncodingHeaders === "string") {
const supportedContentEncoding = isSupportedContentEncoding(contentEncodingHeaders);
if (supportedContentEncoding) {
headers.push(supportedContentEncoding);
}
}
return headers;
};
export const insertBrowserSdkLoaderByIndex = (index, html, snippet) => {
if (index < 0)
return null;
let newHtml = null;
const subStart = html.substring(0, index);
const subEnd = html.substring(index);
newHtml = subStart + '<script type="text/javascript">' + snippet + "</script>" + subEnd;
return newHtml;
};
export const isContentTypeHeaderHtml = (response) => {
let isHtml = false;
const contentType = response.getHeader("Content-Type");
if (contentType) {
if (typeof contentType === "string") {
isHtml = contentType.indexOf("html") >= 0;
}
else {
isHtml = contentType.toString().indexOf("html") >= 0;
}
}
return isHtml;
};
//# sourceMappingURL=browserSdkLoaderHelper.js.map