nicolium: use URL.canParse instead of a try/catch block

Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
nicole mikołajczyk
2026-03-18 18:28:59 +01:00
parent 99f57c65d8
commit deb0a7e919
4 changed files with 14 additions and 28 deletions

View File

@ -1,5 +1,4 @@
import * as BuildConfig from '@/build-config';
import { isURL } from '@/utils/auth';
import sourceCode from '@/utils/code';
import { getScopes } from '@/utils/scopes';
@ -19,7 +18,7 @@ const createProviderApp = () => {
};
const prepareRequest = async (provider: string) => {
const baseURL = isURL(BuildConfig.BACKEND_URL) ? BuildConfig.BACKEND_URL : '';
const baseURL = URL.canParse(BuildConfig.BACKEND_URL) ? BuildConfig.BACKEND_URL : '';
const scopes = getScopes(undefined, true);
const app = await createProviderApp();

View File

@ -26,7 +26,7 @@ import { coerceObject } from '@/schemas/utils';
import { setSentryAccount, unsetSentryAccount } from '@/sentry';
import KVStore from '@/storage/kv-store';
import toast from '@/toast';
import { validId, isURL, parseBaseURL } from '@/utils/auth';
import { validId, parseBaseURL } from '@/utils/auth';
import sourceCode from '@/utils/code';
import { normalizeUsername } from '@/utils/input';
import { getScopes } from '@/utils/scopes';
@ -68,7 +68,7 @@ const instance = (() => {
}
})();
const backendUrl = isURL(BuildConfig.BACKEND_URL) ? BuildConfig.BACKEND_URL : '';
const backendUrl = URL.canParse(BuildConfig.BACKEND_URL) ? BuildConfig.BACKEND_URL : '';
const mastodonPreloadSchema = coerceObject({
meta: coerceObject({
@ -189,7 +189,7 @@ const setSessionUser = (state: AuthData) => {
const isUpgradingUrlId = (state: AuthData) => {
const me = state.me;
const user = state.users[me!];
return validId(me) && user && !isURL(me);
return validId(me) && user && !URL.canParse(me as string);
};
const sanitizeState = (state: AuthData) => {
@ -263,7 +263,7 @@ const importTokenData = (state: AuthData, token: Token, app?: CredentialApplicat
};
const upgradeNonUrlId = (state: AuthData, account: CredentialAccount) => {
if (isURL(state.me)) return;
if (state.me && URL.canParse(state.me)) return;
state.me = state.me === account.id ? account.url : state.me;
delete state.users[account.id];
};
@ -312,7 +312,7 @@ const importMastodonPreloadData = (state: AuthData, data: Record<string, any>) =
const accountUrl = parsedData.accounts[accountId]?.url;
const accessToken = parsedData.meta.access_token;
if (validId(accessToken) && validId(accountId) && isURL(accountUrl)) {
if (validId(accessToken) && validId(accountId) && URL.canParse(accountUrl)) {
state.tokens[accessToken] = v.parse(tokenSchema, {
access_token: accessToken,
account: accountId,
@ -830,7 +830,9 @@ const isLoggedIn = () => validId(getCurrentAccountId());
const getAuthUserUrl = () => {
const { me, users } = useAuthStore.getState();
return [users[me!]?.url, me].filter((url) => url).find(isURL);
return [users[me!]?.url, me]
.filter((url): url is string => !!url)
.find((url) => URL.canParse(url));
};
const getMeUrl = () => getOwnAccount()?.url;

View File

@ -1,23 +1,9 @@
const validId = (id?: string | null | false) =>
typeof id === 'string' && id !== 'null' && id !== 'undefined';
const isURL = (url?: string | null) => {
if (typeof url !== 'string') return false;
try {
new URL(url);
return true;
} catch {
return false;
}
};
const parseBaseURL = (url?: string) => {
if (typeof url !== 'string') return '';
try {
return new URL(url).origin;
} catch {
return '';
}
if (!url || !URL.canParse(url)) return '';
return new URL(url).origin;
};
export { validId, isURL, parseBaseURL };
export { validId, parseBaseURL };

View File

@ -6,7 +6,6 @@
import * as BuildConfig from '@/build-config';
import { isPrerendered } from '@/precheck';
import { useInstanceStore } from '@/stores/instance';
import { isURL } from '@/utils/auth';
/**
* Determine whether Nicolium is running in standalone mode.
@ -14,12 +13,12 @@ import { isURL } from '@/utils/auth';
*/
const isStandalone = (): boolean => {
const instanceFetchFailed = useInstanceStore.getState().instanceFetchFailed;
return isURL(BuildConfig.BACKEND_URL) ? false : !isPrerendered && instanceFetchFailed;
return URL.canParse(BuildConfig.BACKEND_URL) ? false : !isPrerendered && instanceFetchFailed;
};
const useIsStandalone = () => {
const instanceFetchFailed = useInstanceStore((state) => state.instanceFetchFailed);
return isURL(BuildConfig.BACKEND_URL) ? false : !isPrerendered && instanceFetchFailed;
return URL.canParse(BuildConfig.BACKEND_URL) ? false : !isPrerendered && instanceFetchFailed;
};
const useFederationRestrictionsDisclosed = () =>