diff --git a/client/base_client.ts b/client/base_client.ts index cac1334..18da216 100644 --- a/client/base_client.ts +++ b/client/base_client.ts @@ -34,6 +34,12 @@ export type ClientOptions = { incomingStore?: IncomingStore; outgoingStore?: OutgoingStore; logger?: (msg: string, ...args: unknown[]) => void; + /** Path to the ca.crt files */ + caCerts?: string[]; + /** Content of the client.crt file */ + certChain?: string; + /** Content of the client.key file */ + privateKey?: string; }; export type RetryOptions = { @@ -253,7 +259,10 @@ export abstract class Client { } public async connect(): Promise { + await this.disconnect(); switch (this.connectionState) { + case 'connecting': + case 'disconnecting': case 'offline': case 'disconnected': break; @@ -364,6 +373,7 @@ export abstract class Client { input: SubscriptionOption | string | (SubscriptionOption | string)[], qos?: QoS ): Promise { + await this.connect(); switch (this.connectionState) { case 'disconnecting': case 'disconnected': @@ -566,6 +576,8 @@ export abstract class Client { this.changeState('disconnected'); this.stopTimers(); break; + case 'disconnected': + break; default: throw new Error( `should not be disconnecting in ${this.connectionState} state` diff --git a/deno/deno_client.ts b/deno/deno_client.ts index abaa8e0..21b0ec1 100644 --- a/deno/deno_client.ts +++ b/deno/deno_client.ts @@ -1,20 +1,15 @@ import { Client as BaseClient, - ClientOptions as BaseClientOptions, + ClientOptions, } from '../client/base_client.ts'; import { AnyPacket } from '../packets/mod.ts'; -export type ClientOptions = BaseClientOptions & { - certFile?: string; -}; - const DEFAULT_BUF_SIZE = 4096; const utf8Encoder = new TextEncoder(); const utf8Decoder = new TextDecoder(); export class Client extends BaseClient { - declare options: ClientOptions; private conn: Deno.Conn | undefined; private closing = false; @@ -43,13 +38,13 @@ export class Client extends BaseClient { port: Number(url.port), }); } else if (url.protocol === 'mqtts:') { - // console.log(this.options.certFile); - conn = await Deno.connectTls({ hostname: url.hostname, port: Number(url.port), - certFile: this.options.certFile, - }); + caCerts: this.options?.caCerts, + certChain: this.options?.certChain, + privateKey: this.options?.privateKey + } as any); } else { throw new Error(`unknown URL protocol ${url.protocol.slice(0, -1)}`); }