(feat): add auth() function
All checks were successful
CI / checks (push) Successful in 4m52s

(fix): wasm error
This commit is contained in:
Alois 2026-07-02 23:38:38 +02:00
commit 8ae8377662
5 changed files with 28 additions and 19 deletions

View file

@ -128,6 +128,7 @@ type NormalizedMTPClientOptions = Omit<MTPClientOptions, "hostPublicKey"> & {
};
const DEFAULT_CREDENTIALS_KEY = "mtp:credentials";
let wasmInitPromise: Promise<Awaited<ReturnType<typeof initWasm>>> | undefined;
function emit(logger, event) {
if (typeof logger === "function") {
@ -394,7 +395,8 @@ export class MTPClient {
}
static async init(wasm?: MTPClientOptions["wasm"]): Promise<Awaited<ReturnType<typeof initWasm>>> {
return await initWasm(wasm);
wasmInitPromise ??= initWasm(wasm);
return await wasmInitPromise;
}
get credentials(): MTPClientCredentials | null {
@ -430,7 +432,7 @@ export class MTPClient {
async connect(): Promise<void> {
if (this.#credentials?.clientId != null && this.#options.hostPublicKey) {
await this.#connectAuthenticated();
await this.auth();
return;
}
@ -447,6 +449,15 @@ export class MTPClient {
}
}
async auth(): Promise<bigint> {
if (!this.#options.hostPublicKey) {
throw new Error("MTPClient.auth requires hostPublicKey");
}
return this.#credentials?.clientId == null
? await this.register()
: await this.#connectAuthenticated();
}
async #connectAuthenticated() {
if (!this.#options.hostPublicKey) {
throw new Error("MTPClient.connect requires hostPublicKey for authenticated connections");