From 7ee40d5edd6b929e49a135825b9d4eae03a197d2 Mon Sep 17 00:00:00 2001 From: "Hung-Han (Henry) Chen" Date: Wed, 7 Jul 2021 14:32:56 +0300 Subject: [PATCH] Refactor Signed-off-by: Hung-Han (Henry) Chen --- src/common/utils/index.ts | 2 ++ src/common/utils/isValidDsn.ts | 39 ++++++++++++++++++++++++++++++++++ src/common/vars.ts | 7 ++---- 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/common/utils/isValidDsn.ts diff --git a/src/common/utils/index.ts b/src/common/utils/index.ts index d652933341..866c6bfded 100644 --- a/src/common/utils/index.ts +++ b/src/common/utils/index.ts @@ -50,6 +50,8 @@ export * from "./tar"; export * from "./toggle-set"; export * from "./toJS"; export * from "./type-narrowing"; +export * from "./isValidURL"; +export * from "./isValidDsn"; import * as iter from "./iter"; diff --git a/src/common/utils/isValidDsn.ts b/src/common/utils/isValidDsn.ts new file mode 100644 index 0000000000..c9c6aacd0d --- /dev/null +++ b/src/common/utils/isValidDsn.ts @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + + +// Regular expression used to parse a Sentry Dsn. +// from https://github.com/getsentry/sentry-javascript/blob/f624f442fd76bf655de6def9fa4d7631735ebba0/packages/utils/src/dsn.ts#L6 +const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+))?@)([\w.-]+)(?::(\d+))?\/(.+)/; + +/** + * check if string is valid Sentry dsn + * + * @param {(string | undefined | null)} url + * @return {boolean} + */ +export const isValidDsn = (string: string | undefined | null) => { + + const valid = DSN_REGEX.exec(string); + + return valid; + +}; diff --git a/src/common/vars.ts b/src/common/vars.ts index ff6d0c9e2e..61a2d9559d 100644 --- a/src/common/vars.ts +++ b/src/common/vars.ts @@ -25,6 +25,7 @@ import { SemVer } from "semver"; import packageInfo from "../../package.json"; import { defineGlobal } from "./utils/defineGlobal"; import { isValidURL } from "./utils/isValidURL"; +import { isValidDsn } from "./utils/isValidDsn"; export const isMac = process.platform === "darwin"; export const isWindows = process.platform === "win32"; @@ -72,10 +73,6 @@ export const supportUrl = "https://docs.k8slens.dev/latest/support/" as string; export const appSemVer = new SemVer(packageInfo.version); export const docsUrl = `https://docs.k8slens.dev/main/` as string; -// Regular expression used to parse a Sentry Dsn. -// from https://github.com/getsentry/sentry-javascript/blob/f624f442fd76bf655de6def9fa4d7631735ebba0/packages/utils/src/dsn.ts#L6 -const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+))?@)([\w.-]+)(?::(\d+))?\/(.+)/; - export const dsn = packageInfo.config?.sentryDsn; -export const dsnIsValid = isValidURL(dsn) && DSN_REGEX.exec(dsn); +export const dsnIsValid = isValidURL(dsn) && isValidDsn(dsn);