UNPKG

@sentry/core

Version:
109 lines (105 loc) 3.47 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const debugBuild = require('../debug-build.js'); const debugLogger = require('./debug-logger.js'); const ORG_ID_REGEX = /^o(\d+)\./; const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+)?)?@)((?:\[[:.%\w]+\]|[\w.-]+))(?::(\d+))?\/(.+)/; function isValidProtocol(protocol) { return protocol === "http" || protocol === "https"; } function dsnToString(dsn, withPassword = false) { const { host, path, pass, port, projectId, protocol, publicKey } = dsn; return `${protocol}://${publicKey}${withPassword && pass ? `:${pass}` : ""}@${host}${port ? `:${port}` : ""}/${path ? `${path}/` : path}${projectId}`; } function dsnFromString(str) { const match = DSN_REGEX.exec(str); if (!match) { debugLogger.consoleSandbox(() => { console.error(`Invalid Sentry Dsn: ${str}`); }); return void 0; } const [protocol, publicKey, pass = "", host = "", port = "", lastPath = ""] = match.slice(1); let path = ""; let projectId = lastPath; const split = projectId.split("/"); if (split.length > 1) { path = split.slice(0, -1).join("/"); projectId = split.pop(); } if (projectId) { const projectMatch = projectId.match(/^\d+/); if (projectMatch) { projectId = projectMatch[0]; } } return dsnFromComponents({ host, pass, path, projectId, port, protocol, publicKey }); } function dsnFromComponents(components) { return { protocol: components.protocol, publicKey: components.publicKey || "", pass: components.pass || "", host: components.host, port: components.port || "", path: components.path || "", projectId: components.projectId }; } function validateDsn(dsn) { if (!debugBuild.DEBUG_BUILD) { return true; } const { port, projectId, protocol } = dsn; const requiredComponents = ["protocol", "publicKey", "host", "projectId"]; const hasMissingRequiredComponent = requiredComponents.find((component) => { if (!dsn[component]) { debugLogger.debug.error(`Invalid Sentry Dsn: ${component} missing`); return true; } return false; }); if (hasMissingRequiredComponent) { return false; } if (!projectId.match(/^\d+$/)) { debugLogger.debug.error(`Invalid Sentry Dsn: Invalid projectId ${projectId}`); return false; } if (!isValidProtocol(protocol)) { debugLogger.debug.error(`Invalid Sentry Dsn: Invalid protocol ${protocol}`); return false; } if (port && isNaN(parseInt(port, 10))) { debugLogger.debug.error(`Invalid Sentry Dsn: Invalid port ${port}`); return false; } return true; } function extractOrgIdFromDsnHost(host) { const match = host.match(ORG_ID_REGEX); return match?.[1]; } function extractOrgIdFromClient(client) { const options = client.getOptions(); const { host } = client.getDsn() || {}; let org_id; if (options.orgId) { org_id = String(options.orgId); } else if (host) { org_id = extractOrgIdFromDsnHost(host); } return org_id; } function makeDsn(from) { const components = typeof from === "string" ? dsnFromString(from) : dsnFromComponents(from); if (!components || !validateDsn(components)) { return void 0; } return components; } exports.dsnFromString = dsnFromString; exports.dsnToString = dsnToString; exports.extractOrgIdFromClient = extractOrgIdFromClient; exports.extractOrgIdFromDsnHost = extractOrgIdFromDsnHost; exports.makeDsn = makeDsn; //# sourceMappingURL=dsn.js.map