Compare commits

...
5 changed files with 65 additions and 6 deletions
+7 -2
View File
@@ -5,9 +5,10 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { test as setup } from "@playwright/test";
import { expect, test as setup } from "@playwright/test";
import { execFileSync } from "node:child_process";
import { mkdirSync } from "node:fs";
import { mkdirSync, readdirSync } from "node:fs";
import { join } from "node:path";
const EMBEDDED_DIST = "embedded/web/dist";
@@ -17,4 +18,8 @@ setup("build the embedded package", () => {
execFileSync("pnpm", ["build:embedded", "--outDir", EMBEDDED_DIST], {
stdio: "inherit",
});
// The host does the crypto, and nothing runs the standalone client
expect(readdirSync(join(EMBEDDED_DIST, "assets"))).not.toContainEqual(
expect.stringMatching(/rust-crypto|matrix_sdk_crypto|IndexedDBWorker/),
);
});
+37
View File
@@ -0,0 +1,37 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { afterEach, expect, test, vi } from "vitest";
const initSPA = vi.hoisted(() => vi.fn());
vi.mock("./utils/spa", () => ({ initSPA }));
afterEach(() => {
vi.unstubAllEnvs();
vi.resetModules();
initSPA.mockReset();
});
test("the app restores its own session", async () => {
vi.stubEnv("VITE_PACKAGE", "full");
const { loadStandaloneClient } = await import("./App");
const loadSession = vi.fn();
const clearSession = vi.fn();
initSPA.mockResolvedValue(null);
await expect(loadStandaloneClient!(loadSession, clearSession)).resolves.toBe(
null,
);
expect(initSPA).toHaveBeenCalledWith(loadSession, clearSession);
});
test("the embedded package has no session of its own", async () => {
vi.stubEnv("VITE_PACKAGE", "embedded");
const { loadStandaloneClient } = await import("./App");
expect(loadStandaloneClient).toBeUndefined();
});
+6 -2
View File
@@ -64,8 +64,12 @@ const SentryRoute = Sentry.withSentryReactRouterV7Routing(Route);
// - Imported on demand, as it was before, so that a widget, which gets its
// client from the host, never downloads it.
// - Module-level, so that the provider's effect sees one stable loader.
const loadStandaloneClient: ClientLoader = async (...args) =>
(await import("./utils/spa")).initSPA(...args);
// - None in the embedded package, which only runs as a widget: the constant
// condition lets its build drop the client setup and the store worker.
export const loadStandaloneClient: ClientLoader | undefined =
import.meta.env.VITE_PACKAGE === "embedded"
? undefined
: async (...args) => (await import("./utils/spa")).initSPA(...args);
interface SimpleProviderProps {
children: JSX.Element;
+2 -1
View File
@@ -7,7 +7,7 @@ Please see LICENSE in the repository root for full details.
import { defineConfig, mergeConfig } from "vite";
import fullConfig from "./vite.config.ts";
import fullConfig, { withoutMatrixCrypto } from "./vite.config.ts";
const base = "./";
@@ -18,6 +18,7 @@ export default defineConfig((env) =>
defineConfig({
base, // Use relative URLs to allow the app to be hosted under any path
publicDir: false, // Don't serve the public directory which only contains the favicon
plugins: [withoutMatrixCrypto({ withoutStoreWorker: true })],
}),
),
);
+13 -1
View File
@@ -201,9 +201,17 @@ export default ({
* encrypts and decrypts for it, so the Rust crypto chunk and its WASM (about
* 10 MB) would only be shipped, never loaded. The SDK imports it dynamically,
* from `initRustCrypto` and `preloadRustCrypto`; here those reject instead.
*
* With `withoutStoreWorker`, the worker behind the standalone client's sync
* store goes too, for a build that contains the standalone client's code but
* never runs it. The worker is a second copy of the SDK with the crypto, and
* the bundler emits it as soon as anything imports it.
*/
export function withoutMatrixCrypto(): Plugin {
export function withoutMatrixCrypto({
withoutStoreWorker = false,
}: { withoutStoreWorker?: boolean } = {}): Plugin {
const crypto = "\0without-matrix-crypto";
const worker = "\0without-matrix-store-worker";
const unavailable =
"new Error('This build uses the client of its host, which does the crypto')";
return {
@@ -215,6 +223,8 @@ export function withoutMatrixCrypto(): Plugin {
/(^|\/)rust-crypto\/index(\.js)?$/.test(source)
)
return crypto;
if (withoutStoreWorker && source.endsWith("/IndexedDBWorker?worker"))
return worker;
return null;
},
load(id): string | null {
@@ -223,6 +233,8 @@ export function withoutMatrixCrypto(): Plugin {
`export async function initRustCrypto() { throw ${unavailable}; }`,
`export async function preloadRustCrypto() { throw ${unavailable}; }`,
].join("\n");
if (id === worker)
return `export default class { constructor() { throw ${unavailable}; } }`;
return null;
},
};