Matrix: fix runtime encryption loading

This commit is contained in:
Gustavo Madeira Santana 2026-03-19 11:08:12 -04:00
parent 8268c28053
commit 12ad809e79
No known key found for this signature in database
5 changed files with 27 additions and 7 deletions

View File

@ -1,6 +1,10 @@
import path from "node:path";
import { createJiti } from "jiti";
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
buildPluginLoaderJitiOptions,
resolvePluginSdkScopedAliasMap,
} from "../../src/plugins/sdk-alias.ts";
const setMatrixRuntimeMock = vi.hoisted(() => vi.fn());
const registerChannelMock = vi.hoisted(() => vi.fn());
@ -17,12 +21,13 @@ describe("matrix plugin registration", () => {
});
it("loads the matrix runtime api through Jiti", () => {
const jiti = createJiti(import.meta.url, {
interopDefault: true,
tryNative: false,
extensions: [".ts", ".tsx", ".mts", ".cts", ".js", ".mjs", ".cjs", ".json"],
});
const runtimeApiPath = path.join(process.cwd(), "extensions", "matrix", "runtime-api.ts");
const jiti = createJiti(import.meta.url, {
...buildPluginLoaderJitiOptions(
resolvePluginSdkScopedAliasMap({ modulePath: runtimeApiPath }),
),
tryNative: false,
});
expect(jiti(runtimeApiPath)).toMatchObject({
requiresExplicitMatrixDefaultAccount: expect.any(Function),

View File

@ -1,4 +1,3 @@
import { Attachment, EncryptedAttachment } from "@matrix-org/matrix-sdk-crypto-nodejs";
import type { MatrixRecoveryKeyStore } from "./recovery-key-store.js";
import type { EncryptedFile } from "./types.js";
import type {
@ -64,6 +63,15 @@ export type MatrixCryptoFacade = {
) => Promise<{ decimal?: [number, number, number]; emoji?: Array<[string, string]> }>;
};
type MatrixCryptoNodeRuntime = typeof import("./crypto-node.runtime.js");
let matrixCryptoNodeRuntimePromise: Promise<MatrixCryptoNodeRuntime> | null = null;
async function loadMatrixCryptoNodeRuntime(): Promise<MatrixCryptoNodeRuntime> {
// Keep the native crypto package out of the main CLI startup graph.
matrixCryptoNodeRuntimePromise ??= import("./crypto-node.runtime.js");
return await matrixCryptoNodeRuntimePromise;
}
export function createMatrixCryptoFacade(deps: {
client: MatrixCryptoFacadeClient;
verificationManager: MatrixVerificationManager;
@ -110,6 +118,7 @@ export function createMatrixCryptoFacade(deps: {
encryptMedia: async (
buffer: Buffer,
): Promise<{ buffer: Buffer; file: Omit<EncryptedFile, "url"> }> => {
const { Attachment } = await loadMatrixCryptoNodeRuntime();
const encrypted = Attachment.encrypt(new Uint8Array(buffer));
const mediaInfoJson = encrypted.mediaEncryptionInfo;
if (!mediaInfoJson) {
@ -130,6 +139,7 @@ export function createMatrixCryptoFacade(deps: {
file: EncryptedFile,
opts?: { maxBytes?: number; readIdleTimeoutMs?: number },
): Promise<Buffer> => {
const { Attachment, EncryptedAttachment } = await loadMatrixCryptoNodeRuntime();
const encrypted = await deps.downloadContent(file.url, opts);
const metadata: EncryptedFile = {
url: file.url,

View File

@ -0,0 +1,3 @@
import { Attachment, EncryptedAttachment } from "@matrix-org/matrix-sdk-crypto-nodejs";
export { Attachment, EncryptedAttachment };

View File

@ -1,5 +1,6 @@
import { format } from "node:util";
import { redactSensitiveText, type RuntimeLogger } from "../../runtime-api.js";
import { redactSensitiveText } from "openclaw/plugin-sdk/diagnostics-otel";
import type { RuntimeLogger } from "openclaw/plugin-sdk/plugin-runtime";
import { getMatrixRuntime } from "../../runtime.js";
export type Logger = {

View File

@ -6,3 +6,4 @@ export * from "../plugins/http-path.js";
export * from "../plugins/http-registry.js";
export * from "../plugins/interactive.js";
export * from "../plugins/types.js";
export type { RuntimeLogger } from "../plugins/runtime/types.js";