refactor: cache repeated lazy imports

This commit is contained in:
Peter Steinberger
2026-04-18 16:31:48 +01:00
parent d13869aab9
commit cdaa70facb
13 changed files with 161 additions and 50 deletions

View File

@@ -44,12 +44,15 @@ type MatrixCredentialsReadDeps = {
credentialsMatchConfig: typeof import("../credentials-read.js").credentialsMatchConfig;
};
type MatrixCredentialsWriteRuntime = typeof import("../credentials-write.runtime.js");
type MatrixSecretInputDeps = {
resolveConfiguredSecretInputString: typeof import("./config-secret-input.runtime.js").resolveConfiguredSecretInputString;
};
let matrixAuthClientDepsPromise: Promise<MatrixAuthClientDeps> | undefined;
let matrixCredentialsReadDepsPromise: Promise<MatrixCredentialsReadDeps> | undefined;
let matrixCredentialsWriteRuntimePromise: Promise<MatrixCredentialsWriteRuntime> | undefined;
let matrixSecretInputDepsPromise: Promise<MatrixSecretInputDeps> | undefined;
let matrixAuthClientDepsForTest: MatrixAuthClientDeps | undefined;
@@ -87,6 +90,11 @@ async function loadMatrixCredentialsReadDeps(): Promise<MatrixCredentialsReadDep
return await matrixCredentialsReadDepsPromise;
}
async function loadMatrixCredentialsWriteRuntime(): Promise<MatrixCredentialsWriteRuntime> {
matrixCredentialsWriteRuntimePromise ??= import("../credentials-write.runtime.js");
return await matrixCredentialsWriteRuntimePromise;
}
async function loadMatrixSecretInputDeps(): Promise<MatrixSecretInputDeps> {
matrixSecretInputDepsPromise ??= import("./config-secret-input.runtime.js").then((runtime) => ({
resolveConfiguredSecretInputString: runtime.resolveConfiguredSecretInputString,
@@ -740,12 +748,6 @@ export async function resolveMatrixAuth(params?: {
const homeserver = await resolveValidatedMatrixHomeserverUrl(resolved.homeserver, {
dangerouslyAllowPrivateNetwork: resolved.allowPrivateNetwork,
});
let credentialsWriter: typeof import("../credentials-write.runtime.js") | undefined;
const loadCredentialsWriter = async () => {
credentialsWriter ??= await import("../credentials-write.runtime.js");
return credentialsWriter;
};
const { loadMatrixCredentials, credentialsMatchConfig } = await loadMatrixCredentialsReadDeps();
const cached = loadMatrixCredentials(env, accountId);
const cachedCredentials =
@@ -790,7 +792,7 @@ export async function resolveMatrixAuth(params?: {
cachedCredentials.userId !== userId ||
(cachedCredentials.deviceId || undefined) !== knownDeviceId;
if (shouldRefreshCachedCredentials) {
const { saveMatrixCredentials } = await loadCredentialsWriter();
const { saveMatrixCredentials } = await loadMatrixCredentialsWriteRuntime();
await saveMatrixCredentials(
{
homeserver,
@@ -802,7 +804,7 @@ export async function resolveMatrixAuth(params?: {
accountId,
);
} else if (hasMatchingCachedToken) {
const { touchMatrixCredentials } = await loadCredentialsWriter();
const { touchMatrixCredentials } = await loadMatrixCredentialsWriteRuntime();
await touchMatrixCredentials(env, accountId);
}
return {
@@ -823,7 +825,7 @@ export async function resolveMatrixAuth(params?: {
}
if (cachedCredentials) {
const { touchMatrixCredentials } = await loadCredentialsWriter();
const { touchMatrixCredentials } = await loadMatrixCredentialsWriteRuntime();
await touchMatrixCredentials(env, accountId);
return {
accountId,
@@ -905,7 +907,7 @@ export async function resolveMatrixAuth(params?: {
}),
};
const { saveMatrixCredentials } = await loadCredentialsWriter();
const { saveMatrixCredentials } = await loadMatrixCredentialsWriteRuntime();
await saveMatrixCredentials(
{
homeserver: auth.homeserver,
@@ -974,7 +976,7 @@ export async function backfillMatrixAuthDeviceIdAfterStartup(params: {
return undefined;
}
const credentialsWriter = await import("../credentials-write.runtime.js");
const credentialsWriter = await loadMatrixCredentialsWriteRuntime();
const saved = await credentialsWriter.saveBackfilledMatrixDeviceId(
{
homeserver: params.auth.homeserver,

View File

@@ -4,23 +4,32 @@ import type {
touchMatrixCredentials as touchMatrixCredentialsType,
} from "./credentials.js";
type MatrixCredentialsRuntime = typeof import("./credentials.js");
let matrixCredentialsRuntimePromise: Promise<MatrixCredentialsRuntime> | undefined;
function loadMatrixCredentialsRuntime(): Promise<MatrixCredentialsRuntime> {
matrixCredentialsRuntimePromise ??= import("./credentials.js");
return matrixCredentialsRuntimePromise;
}
export async function saveMatrixCredentials(
...args: Parameters<typeof saveMatrixCredentialsType>
): ReturnType<typeof saveMatrixCredentialsType> {
const runtime = await import("./credentials.js");
const runtime = await loadMatrixCredentialsRuntime();
return runtime.saveMatrixCredentials(...args);
}
export async function saveBackfilledMatrixDeviceId(
...args: Parameters<typeof saveBackfilledMatrixDeviceIdType>
): ReturnType<typeof saveBackfilledMatrixDeviceIdType> {
const runtime = await import("./credentials.js");
const runtime = await loadMatrixCredentialsRuntime();
return runtime.saveBackfilledMatrixDeviceId(...args);
}
export async function touchMatrixCredentials(
...args: Parameters<typeof touchMatrixCredentialsType>
): ReturnType<typeof touchMatrixCredentialsType> {
const runtime = await import("./credentials.js");
const runtime = await loadMatrixCredentialsRuntime();
return runtime.touchMatrixCredentials(...args);
}

View File

@@ -2,6 +2,15 @@ import type { GatewayRequestHandlerOptions } from "openclaw/plugin-sdk/gateway-r
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
import { formatMatrixErrorMessage } from "./matrix/errors.js";
type MatrixVerificationRuntime = typeof import("./matrix/actions/verification.js");
let matrixVerificationRuntimePromise: Promise<MatrixVerificationRuntime> | undefined;
function loadMatrixVerificationRuntime(): Promise<MatrixVerificationRuntime> {
matrixVerificationRuntimePromise ??= import("./matrix/actions/verification.js");
return matrixVerificationRuntimePromise;
}
function sendError(respond: (ok: boolean, payload?: unknown) => void, err: unknown) {
respond(false, { error: formatMatrixErrorMessage(err) });
}
@@ -18,7 +27,7 @@ export async function handleVerifyRecoveryKey({
respond,
}: GatewayRequestHandlerOptions): Promise<void> {
try {
const { verifyMatrixRecoveryKey } = await import("./matrix/actions/verification.js");
const { verifyMatrixRecoveryKey } = await loadMatrixVerificationRuntime();
const key = normalizeOptionalString(params?.key);
if (!key) {
respond(false, { error: "key required" });
@@ -37,7 +46,7 @@ export async function handleVerificationBootstrap({
respond,
}: GatewayRequestHandlerOptions): Promise<void> {
try {
const { bootstrapMatrixVerification } = await import("./matrix/actions/verification.js");
const { bootstrapMatrixVerification } = await loadMatrixVerificationRuntime();
const accountId = normalizeOptionalString(params?.accountId);
const recoveryKey = typeof params?.recoveryKey === "string" ? params.recoveryKey : undefined;
const forceResetCrossSigning = params?.forceResetCrossSigning === true;
@@ -57,7 +66,7 @@ export async function handleVerificationStatus({
respond,
}: GatewayRequestHandlerOptions): Promise<void> {
try {
const { getMatrixVerificationStatus } = await import("./matrix/actions/verification.js");
const { getMatrixVerificationStatus } = await loadMatrixVerificationRuntime();
const accountId = normalizeOptionalString(params?.accountId);
const includeRecoveryKey = params?.includeRecoveryKey === true;
const status = await getMatrixVerificationStatus({ accountId, includeRecoveryKey });