{"version":3,"file":"sql-common.js","sources":["../../../../../src/integrations/tracing/utils/sql-common.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n *\n * NOTICE from the Sentry authors:\n * - Vendored from: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/5a5918fd4f9f16b14c9ef4d3de08ab98c20e5b46/packages/sql-common\n * - Upstream version: @opentelemetry/sql-common@0.41.2\n */\n\n/* eslint-disable */\n\nimport { trace, Span, ROOT_CONTEXT, defaultTextMapSetter } from '@opentelemetry/api';\nimport { W3CTraceContextPropagator } from '@opentelemetry/core';\n\n// NOTE: This function currently is returning false-positives\n// in cases where comment characters appear in string literals\n// (\"SELECT '-- not a comment';\" would return true, although has no comment)\nfunction hasValidSqlComment(query: string): boolean {\n const indexOpeningDashDashComment = query.indexOf('--');\n if (indexOpeningDashDashComment >= 0) {\n return true;\n }\n\n const indexOpeningSlashComment = query.indexOf('/*');\n if (indexOpeningSlashComment < 0) {\n return false;\n }\n\n const indexClosingSlashComment = query.indexOf('*/');\n return indexOpeningDashDashComment < indexClosingSlashComment;\n}\n\n// sqlcommenter specification (https://google.github.io/sqlcommenter/spec/#value-serialization)\n// expects us to URL encode based on the RFC 3986 spec (https://en.wikipedia.org/wiki/Percent-encoding),\n// but encodeURIComponent does not handle some characters correctly (! ' ( ) *),\n// which means we need special handling for this\n// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent\nfunction fixedEncodeURIComponent(str: string) {\n return encodeURIComponent(str).replace(/[!'()*]/g, c => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);\n}\n\nexport function addSqlCommenterComment(span: Span, query: string): string {\n if (typeof query !== 'string' || query.length === 0) {\n return query;\n }\n\n // As per sqlcommenter spec we shall not add a comment if there already is a comment\n // in the query\n if (hasValidSqlComment(query)) {\n return query;\n }\n\n const propagator = new W3CTraceContextPropagator();\n const headers: { [key: string]: string } = {};\n propagator.inject(trace.setSpan(ROOT_CONTEXT, span), headers, defaultTextMapSetter);\n\n // sqlcommenter spec requires keys in the comment to be sorted lexicographically\n const sortedKeys = Object.keys(headers).sort();\n\n if (sortedKeys.length === 0) {\n return query;\n }\n\n const commentString = sortedKeys\n .map(key => {\n const encodedValue = fixedEncodeURIComponent(headers[key]!);\n return `${key}='${encodedValue}'`;\n })\n .join(',');\n\n return `${query} /*${commentString}*/`;\n}\n"],"names":["W3CTraceContextPropagator","trace","ROOT_CONTEXT","defaultTextMapSetter"],"mappings":";;;;;AA4BA,SAAS,mBAAmB,KAAA,EAAwB;AAClD,EAAA,MAAM,2BAAA,GAA8B,KAAA,CAAM,OAAA,CAAQ,IAAI,CAAA;AACtD,EAAA,IAAI,+BAA+B,CAAA,EAAG;AACpC,IAAA,OAAO,IAAA;AAAA,EACT;AAEA,EAAA,MAAM,wBAAA,GAA2B,KAAA,CAAM,OAAA,CAAQ,IAAI,CAAA;AACnD,EAAA,IAAI,2BAA2B,CAAA,EAAG;AAChC,IAAA,OAAO,KAAA;AAAA,EACT;AAEA,EAAA,MAAM,wBAAA,GAA2B,KAAA,CAAM,OAAA,CAAQ,IAAI,CAAA;AACnD,EAAA,OAAO,2BAAA,GAA8B,wBAAA;AACvC;AAOA,SAAS,wBAAwB,GAAA,EAAa;AAC5C,EAAA,OAAO,mBAAmB,GAAG,CAAA,CAAE,OAAA,CAAQ,UAAA,EAAY,OAAK,CAAA,CAAA,EAAI,CAAA,CAAE,UAAA,CAAW,CAAC,EAAE,QAAA,CAAS,EAAE,CAAA,CAAE,WAAA,EAAa,CAAA,CAAE,CAAA;AAC1G;AAEO,SAA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