@aws/aws-distro-opentelemetry-node-autoinstrumentation
Version:
This package provides Amazon Web Services distribution of the OpenTelemetry Node Instrumentation, which allows for auto-instrumentation of NodeJS applications.
95 lines • 3.74 kB
JavaScript
;
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.SqsUrlParser = void 0;
const utils_1 = require("./utils");
const HTTP_SCHEMA = 'http://';
const HTTPS_SCHEMA = 'https://';
// Cannot define type for regex variables
// eslint-disable-next-line @typescript-eslint/typedef
const ALPHABET_REGEX = /^[a-zA-Z]+$/;
class SqsUrlParser {
/**
* Best-effort logic to extract queue name from an HTTP url. This method should only be used with
* a string that is, with reasonably high confidence, an SQS queue URL. Handles new/legacy/some
* custom URLs. Essentially, we require that the URL should have exactly three parts, delimited by
* /'s (excluding schema), the second part should be a account id consisting of digits, and the third part
* should be a valid queue name, per SQS naming conventions.
*
* Unlike parseUrl which only handles new URLs and their queuename parsing, this
* implements its own queue name parsing logic to support multiple URL formats.
*/
static getQueueName(url) {
if (typeof url !== 'string') {
return undefined;
}
const urlWithoutProtocol = url.replace(HTTP_SCHEMA, '').replace(HTTPS_SCHEMA, '');
const splitUrl = urlWithoutProtocol.split('/');
if (splitUrl.length === 3 && (0, utils_1.isAccountId)(splitUrl[1]) && this.isValidQueueName(splitUrl[2])) {
return splitUrl[2];
}
return undefined;
}
/**
* Extracts the account ID from an SQS URL.
*/
static getAccountId(url) {
if (typeof url !== 'string') {
return undefined;
}
const parsedUrl = this.parseUrl(url);
return parsedUrl === null || parsedUrl === void 0 ? void 0 : parsedUrl.accountId;
}
/**
* Extracts the region from an SQS URL.
*/
static getRegion(url) {
if (typeof url !== 'string') {
return undefined;
}
const parsedUrl = this.parseUrl(url);
return parsedUrl === null || parsedUrl === void 0 ? void 0 : parsedUrl.region;
}
/**
* Parses an SQS URL and extracts its components.
* Format: https://sqs.<region>.amazonaws.com/<accountId>/<queueName>
* @param url - The SQS URL to parse
* @returns Object containing queue name, account ID and region, or undefined if invalid
* @private
*/
static parseUrl(url) {
const urlWithoutProtocol = url.replace(HTTP_SCHEMA, '').replace(HTTPS_SCHEMA, '');
const splitUrl = urlWithoutProtocol.split('/');
if (splitUrl.length !== 3 ||
!splitUrl[0].toLowerCase().startsWith('sqs') ||
!(0, utils_1.isAccountId)(splitUrl[1]) ||
!this.isValidQueueName(splitUrl[2])) {
return undefined;
}
const domain = splitUrl[0];
const domainParts = domain.split('.');
return {
queueName: splitUrl[2],
accountId: splitUrl[1],
region: domainParts.length === 4 ? domainParts[1] : undefined,
};
}
/**
* Checks if the URL is a valid SQS URL.
*/
static isValidQueueName(input) {
if (input == null || input.length === 0 || input.length > 80) {
return false;
}
for (let i = 0; i < input.length; i++) {
const c = input.charAt(i);
if (c !== '_' && c !== '-' && !ALPHABET_REGEX.test(c) && !(c >= '0' && c <= '9')) {
return false;
}
}
return true;
}
}
exports.SqsUrlParser = SqsUrlParser;
//# sourceMappingURL=sqs-url-parser.js.map