From 1769d76e817c7fe6aa144657cf6eabc9e4d24928 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Sun, 29 Mar 2026 12:38:49 -0400 Subject: [PATCH] Build: ship Matrix crypto WASM pkg --- scripts/copy-matrix-crypto-wasm-pkg.mjs | 34 ++++++++++++++ scripts/release-check.ts | 1 + scripts/runtime-postbuild.mjs | 2 + test/release-check.test.ts | 3 ++ .../copy-matrix-crypto-wasm-pkg.test.ts | 45 +++++++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 scripts/copy-matrix-crypto-wasm-pkg.mjs create mode 100644 test/scripts/copy-matrix-crypto-wasm-pkg.test.ts diff --git a/scripts/copy-matrix-crypto-wasm-pkg.mjs b/scripts/copy-matrix-crypto-wasm-pkg.mjs new file mode 100644 index 00000000000..e0ef0869ee7 --- /dev/null +++ b/scripts/copy-matrix-crypto-wasm-pkg.mjs @@ -0,0 +1,34 @@ +import fs from "node:fs"; +import path from "node:path"; +import { pathToFileURL } from "node:url"; +import { removePathIfExists } from "./runtime-postbuild-shared.mjs"; + +/** + * @param {{ + * cwd?: string; + * repoRoot?: string; + * }} [params] + */ +export function copyMatrixCryptoWasmPkg(params = {}) { + const repoRoot = params.cwd ?? params.repoRoot ?? process.cwd(); + const sourcePkgDir = path.join( + repoRoot, + "node_modules", + "@matrix-org", + "matrix-sdk-crypto-wasm", + "pkg", + ); + const targetPkgDir = path.join(repoRoot, "dist", "pkg"); + + removePathIfExists(targetPkgDir); + if (!fs.existsSync(sourcePkgDir)) { + return; + } + + fs.mkdirSync(path.dirname(targetPkgDir), { recursive: true }); + fs.cpSync(sourcePkgDir, targetPkgDir, { force: true, recursive: true }); +} + +if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) { + copyMatrixCryptoWasmPkg(); +} diff --git a/scripts/release-check.ts b/scripts/release-check.ts index 618e6901254..2601ecc47e6 100755 --- a/scripts/release-check.ts +++ b/scripts/release-check.ts @@ -28,6 +28,7 @@ const requiredPathGroups = [ "dist/build-info.json", "dist/channel-catalog.json", "dist/control-ui/index.html", + "dist/pkg/matrix_sdk_crypto_wasm_bg.wasm", ]; const forbiddenPrefixes = ["dist-runtime/", "dist/OpenClaw.app/"]; // 2026.3.12 ballooned to ~213.6 MiB unpacked and correlated with low-memory diff --git a/scripts/runtime-postbuild.mjs b/scripts/runtime-postbuild.mjs index ef78dd0d030..b50cbf1f57c 100644 --- a/scripts/runtime-postbuild.mjs +++ b/scripts/runtime-postbuild.mjs @@ -1,5 +1,6 @@ import { pathToFileURL } from "node:url"; import { copyBundledPluginMetadata } from "./copy-bundled-plugin-metadata.mjs"; +import { copyMatrixCryptoWasmPkg } from "./copy-matrix-crypto-wasm-pkg.mjs"; import { copyPluginSdkRootAlias } from "./copy-plugin-sdk-root-alias.mjs"; import { stageBundledPluginRuntimeDeps } from "./stage-bundled-plugin-runtime-deps.mjs"; import { stageBundledPluginRuntime } from "./stage-bundled-plugin-runtime.mjs"; @@ -8,6 +9,7 @@ import { writeOfficialChannelCatalog } from "./write-official-channel-catalog.mj export function runRuntimePostBuild(params = {}) { copyPluginSdkRootAlias(params); copyBundledPluginMetadata(params); + copyMatrixCryptoWasmPkg(params); writeOfficialChannelCatalog(params); stageBundledPluginRuntimeDeps(params); stageBundledPluginRuntime(params); diff --git a/test/release-check.test.ts b/test/release-check.test.ts index e0e6bd17b06..40b0a8a22b3 100644 --- a/test/release-check.test.ts +++ b/test/release-check.test.ts @@ -20,6 +20,7 @@ function makePackResult(filename: string, unpackedSize: number) { const requiredPluginSdkPackPaths = [...listPluginSdkDistArtifacts(), "dist/plugin-sdk/compat.js"]; const requiredBundledPluginPackPaths = listBundledPluginPackArtifacts(); +const requiredMatrixCryptoWasmPackPaths = ["dist/pkg/matrix_sdk_crypto_wasm_bg.wasm"]; describe("collectAppcastSparkleVersionErrors", () => { it("accepts legacy 9-digit calver builds before lane-floor cutover", () => { @@ -138,6 +139,7 @@ describe("collectMissingPackPaths", () => { expect.arrayContaining([ "dist/channel-catalog.json", "dist/control-ui/index.html", + "dist/pkg/matrix_sdk_crypto_wasm_bg.wasm", bundledDistPluginFile("matrix", "helper-api.js"), bundledDistPluginFile("matrix", "runtime-api.js"), bundledDistPluginFile("matrix", "thread-bindings-runtime.js"), @@ -158,6 +160,7 @@ describe("collectMissingPackPaths", () => { "dist/entry.js", "dist/control-ui/index.html", ...requiredBundledPluginPackPaths, + ...requiredMatrixCryptoWasmPackPaths, ...requiredPluginSdkPackPaths, "dist/plugin-sdk/root-alias.cjs", "dist/build-info.json", diff --git a/test/scripts/copy-matrix-crypto-wasm-pkg.test.ts b/test/scripts/copy-matrix-crypto-wasm-pkg.test.ts new file mode 100644 index 00000000000..7ccc56a11e0 --- /dev/null +++ b/test/scripts/copy-matrix-crypto-wasm-pkg.test.ts @@ -0,0 +1,45 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { describe, expect, it } from "vitest"; +import { copyMatrixCryptoWasmPkg } from "../../scripts/copy-matrix-crypto-wasm-pkg.mjs"; + +function createRepoFixture() { + return fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-matrix-wasm-pkg-")); +} + +describe("copyMatrixCryptoWasmPkg", () => { + it("stages the matrix crypto wasm package into dist/pkg", () => { + const repoRoot = createRepoFixture(); + const sourcePkgDir = path.join( + repoRoot, + "node_modules", + "@matrix-org", + "matrix-sdk-crypto-wasm", + "pkg", + ); + fs.mkdirSync(sourcePkgDir, { recursive: true }); + fs.writeFileSync(path.join(sourcePkgDir, "matrix_sdk_crypto_wasm_bg.wasm"), "wasm\n", "utf8"); + fs.writeFileSync(path.join(sourcePkgDir, "matrix_sdk_crypto_wasm_bg.js"), "js\n", "utf8"); + + copyMatrixCryptoWasmPkg({ cwd: repoRoot }); + + expect( + fs.readFileSync(path.join(repoRoot, "dist", "pkg", "matrix_sdk_crypto_wasm_bg.wasm"), "utf8"), + ).toBe("wasm\n"); + expect( + fs.readFileSync(path.join(repoRoot, "dist", "pkg", "matrix_sdk_crypto_wasm_bg.js"), "utf8"), + ).toBe("js\n"); + }); + + it("removes stale dist/pkg output when the source package is unavailable", () => { + const repoRoot = createRepoFixture(); + const staleTargetDir = path.join(repoRoot, "dist", "pkg"); + fs.mkdirSync(staleTargetDir, { recursive: true }); + fs.writeFileSync(path.join(staleTargetDir, "stale.txt"), "stale\n", "utf8"); + + copyMatrixCryptoWasmPkg({ cwd: repoRoot }); + + expect(fs.existsSync(staleTargetDir)).toBe(false); + }); +});