Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Static pkg name

Manav Rathi54c54d50c6ce
diff --git a/web/apps/accounts/src/services/passkey.ts b/web/apps/accounts/src/services/passkey.ts
index b8c1ec3e56..c729a2b3c7 100644
--- a/web/apps/accounts/src/services/passkey.ts
+++ b/web/apps/accounts/src/services/passkey.ts
@@ -22,12 +22,10 @@ export const isWebAuthnSupported = () => !!navigator.credentials;
*/
const accountsAuthenticatedRequestHeaders = (
token: string,
-): Record<string, string> => {
- return {
- "X-Auth-Token": token,
- "X-Client-Package": clientPackageName("accounts"),
- };
-};
+): Record<string, string> => ({
+ "X-Auth-Token": token,
+ "X-Client-Package": clientPackageName,
+});
const Passkey = z.object({
/** A unique ID for the passkey */
diff --git a/web/apps/auth/src/pages/_app.tsx b/web/apps/auth/src/pages/_app.tsx
index a8063a44ea..4c24613308 100644
--- a/web/apps/auth/src/pages/_app.tsx
+++ b/web/apps/auth/src/pages/_app.tsx
@@ -1,14 +1,12 @@
import { CustomHead } from "@/next/components/Head";
-import { setAppNameForAuthenticatedRequests } from "@/next/http";
import { setupI18n } from "@/next/i18n";
import {
logStartupBanner,
logUnhandledErrorsAndRejections,
} from "@/next/log-web";
import {
- appName,
- appTitle,
clientPackageName,
+ staticAppTitle,
type BaseAppContextT,
} from "@/next/types/app";
import { ensure } from "@/utils/ensure";
@@ -82,11 +80,8 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
void setupI18n().finally(() => setIsI18nReady(true));
const userID = (getData(LS_KEYS.USER) as User)?.id;
logStartupBanner(userID);
+ HTTPService.setHeaders({ "X-Client-Package": clientPackageName });
logUnhandledErrorsAndRejections(true);
- setAppNameForAuthenticatedRequests(appName);
- HTTPService.setHeaders({
- "X-Client-Package": clientPackageName(appName),
- });
return () => logUnhandledErrorsAndRejections(false);
}, []);
@@ -145,7 +140,6 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
};
const appContext = {
- appName,
logout,
showNavBar,
isMobile,
@@ -157,7 +151,9 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
somethingWentWrong,
};
- const title = isI18nReady ? t("title", { context: "auth" }) : staticAppTitle;
+ const title = isI18nReady
+ ? t("title", { context: "auth" })
+ : staticAppTitle;
return (
<>
diff --git a/web/apps/photos/src/pages/_app.tsx b/web/apps/photos/src/pages/_app.tsx
index 301797b3f8..4cba5e1845 100644
--- a/web/apps/photos/src/pages/_app.tsx
+++ b/web/apps/photos/src/pages/_app.tsx
@@ -1,5 +1,4 @@
import { CustomHead } from "@/next/components/Head";
-import { setAppNameForAuthenticatedRequests } from "@/next/http";
import { setupI18n } from "@/next/i18n";
import log from "@/next/log";
import {
@@ -7,7 +6,6 @@ import {
logUnhandledErrorsAndRejections,
} from "@/next/log-web";
import {
- appName,
clientPackageName,
staticAppTitle,
type BaseAppContextT,
@@ -148,11 +146,8 @@ export default function App({ Component, pageProps }: AppProps) {
void setupI18n().finally(() => setIsI18nReady(true));
const userID = (getData(LS_KEYS.USER) as User)?.id;
logStartupBanner(userID);
+ HTTPService.setHeaders({ "X-Client-Package": clientPackageName });
logUnhandledErrorsAndRejections(true);
- setAppNameForAuthenticatedRequests(appName);
- HTTPService.setHeaders({
- "X-Client-Package": clientPackageName(appName),
- });
return () => logUnhandledErrorsAndRejections(false);
}, []);
@@ -345,7 +340,6 @@ export default function App({ Component, pageProps }: AppProps) {
};
const appContext = {
- appName,
showNavBar,
mlSearchEnabled,
updateMlSearchEnabled,
diff --git a/web/packages/accounts/services/logout.ts b/web/packages/accounts/services/logout.ts
index 079366524e..21999e2bb8 100644
--- a/web/packages/accounts/services/logout.ts
+++ b/web/packages/accounts/services/logout.ts
@@ -1,5 +1,4 @@
import { clearBlobCaches } from "@/next/blob-cache";
-import { clearHTTPState } from "@/next/http";
import { clearKVDB } from "@/next/kv";
import log from "@/next/log";
import InMemoryStore from "@ente/shared/storage/InMemoryStore";
@@ -52,11 +51,6 @@ export const accountLogout = async () => {
} catch (e) {
ignoreError("Blob cache", e);
}
- try {
- clearHTTPState();
- } catch (e) {
- ignoreError("HTTP", e);
- }
try {
await clearKVDB();
} catch (e) {
diff --git a/web/packages/accounts/services/passkey.ts b/web/packages/accounts/services/passkey.ts
index 2b3837ba4f..d507c01c68 100644
--- a/web/packages/accounts/services/passkey.ts
+++ b/web/packages/accounts/services/passkey.ts
@@ -1,4 +1,4 @@
-import { clientPackageHeaderIfPresent } from "@/next/http";
+import { clientPackageHeader } from "@/next/http";
import log from "@/next/log";
import { accountsAppOrigin, apiURL } from "@/next/origins";
import { appName, clientPackageName } from "@/next/types/app";
@@ -231,7 +231,7 @@ export const checkPasskeyVerificationStatus = async (
const url = await apiURL("/users/two-factor/passkeys/get-token");
const params = new URLSearchParams({ sessionID });
const res = await fetch(`${url}?${params.toString()}`, {
- headers: clientPackageHeaderIfPresent(),
+ headers: clientPackageHeader(),
});
if (!res.ok) {
if (res.status == 404 || res.status == 410)
diff --git a/web/packages/next/http.ts b/web/packages/next/http.ts
index bf64a9b3a8..5fb8e3fe68 100644
--- a/web/packages/next/http.ts
+++ b/web/packages/next/http.ts
@@ -1,60 +1,22 @@
import { ensureAuthToken } from "./local-user";
-import { clientPackageName, type AppName } from "./types/app";
-
-/**
- * Value for the the "X-Client-Package" header in authenticated requests.
- */
-let _clientPackage: string | undefined;
-
-/**
- * Remember that we should include the client package corresponding to the given
- * {@link appName} as the "X-Client-Package" header in authenticated requests.
- *
- * This state is persisted in memory, and can be cleared using
- * {@link clearHTTPState}.
- *
- * @param appName The {@link AppName} of the current app.
- */
-export const setAppNameForAuthenticatedRequests = (appName: AppName) => {
- _clientPackage = clientPackageName(appName);
-};
-
-/**
- * Variant of {@link setAppNameForAuthenticatedRequests} that sets directly sets
- * the client package to the provided string.
- */
-export const setClientPackageForAuthenticatedRequests = (p: string) => {
- _clientPackage = p;
-};
-
-/**
- * Forget the effects of a previous {@link setAppNameForAuthenticatedRequests}
- * or {@link setClientPackageForAuthenticatedRequests}.
- */
-export const clearHTTPState = () => {
- _clientPackage = undefined;
-};
+import { clientPackageName } from "./types/app";
/**
* Return headers that should be passed alongwith (almost) all authenticated
* `fetch` calls that we make to our API servers.
*
- * This uses in-memory state (See {@link clearHTTPState}).
+ * - The auth token
+ * - The client package name.
*/
-export const authenticatedRequestHeaders = (): Record<string, string> => {
- const headers: Record<string, string> = {
- "X-Auth-Token": ensureAuthToken(),
- };
- if (_clientPackage) headers["X-Client-Package"] = _clientPackage;
- return headers;
-};
+export const authenticatedRequestHeaders = (): Record<string, string> => ({
+ "X-Auth-Token": ensureAuthToken(),
+ "X-Client-Package": clientPackageName,
+});
/**
- * Return a headers object with "X-Client-Package" header if we have the client
- * package value available to us from local storage.
+ * Return a headers object with "X-Client-Package" header set to the client
+ * package name of the current app.
*/
-export const clientPackageHeaderIfPresent = (): Record<string, string> => {
- const headers: Record<string, string> = {};
- if (_clientPackage) headers["X-Client-Package"] = _clientPackage;
- return headers;
-};
+export const clientPackageHeader = (): Record<string, string> => ({
+ "X-Client-Package": clientPackageName,
+});
diff --git a/web/packages/next/types/app.ts b/web/packages/next/types/app.ts
index dec4e5e510..ff9a211b9e 100644
--- a/web/packages/next/types/app.ts
+++ b/web/packages/next/types/app.ts
@@ -38,32 +38,32 @@ export const staticAppTitle = {
photos: "Ente Photos",
}[appName];
+export const _clientPackageName: Record<AppName, string> = {
+ accounts: "io.ente.accounts.web",
+ auth: "io.ente.auth.web",
+ photos: "io.ente.photos.web",
+};
+
+/** Client package name for the Photos desktop app */
+export const clientPackageNamePhotosDesktop = "io.ente.photos.desktop";
+
/**
- * Client "package names" for each of our apps.
+ * Client "package names" for our app.
*
- * These are used as the identifier in the user agent strings that we send to
- * our own servers.
+ * The package name is used as the (a) "X-Client-Package" header in API
+ * requests, and (b) as the identifier in inline user agent strings in payloads.
*
* In cases where this code works for both a web and a desktop app for the same
- * app (currently only photos), return the platform specific package name.
+ * app (currently only photos), this will be the platform specific package name.
*/
-export const clientPackageName = (appName: AppName): string => {
- if (globalThis.electron) {
+export const clientPackageName = (() => {
+ if (process.env.isDesktop) {
if (appName != "photos")
throw new Error(`Unsupported desktop appName ${appName}`);
return clientPackageNamePhotosDesktop;
}
return _clientPackageName[appName];
-};
-
-export const _clientPackageName: Record<AppName, string> = {
- accounts: "io.ente.accounts.web",
- auth: "io.ente.auth.web",
- photos: "io.ente.photos.web",
-};
-
-/** Client package name for the Photos desktop app */
-export const clientPackageNamePhotosDesktop = "io.ente.photos.desktop";
+})();
/**
* Properties guaranteed to be present in the AppContext types for apps that are