1 |
|
2 |
|
3 | import { IAuthPacket, IConnackPacket, IDisconnectPacket, IPublishPacket, ISubscribePacket, Packet, QoS, IConnectPacket } from 'mqtt-packet';
|
4 | import { IMessageIdProvider } from './default-message-id-provider';
|
5 | import { DuplexOptions } from 'readable-stream';
|
6 | import Store, { IStore } from './store';
|
7 | import { ClientOptions } from 'ws';
|
8 | import { ClientRequestArgs } from 'http';
|
9 | import { DoneCallback, ErrorWithReasonCode, IStream, StreamBuilder, TimerVariant, VoidCallback } from './shared';
|
10 | import { TypedEventEmitter } from './TypedEmitter';
|
11 | import KeepaliveManager from './KeepaliveManager';
|
12 | export type BaseMqttProtocol = 'wss' | 'ws' | 'mqtt' | 'mqtts' | 'tcp' | 'ssl' | 'wx' | 'wxs' | 'ali' | 'alis';
|
13 | export type MqttProtocolWithUnix = `${BaseMqttProtocol}+unix`;
|
14 | export type MqttProtocol = BaseMqttProtocol | MqttProtocolWithUnix;
|
15 | export type StorePutCallback = () => void;
|
16 | export interface ISecureClientOptions {
|
17 | key?: string | string[] | Buffer | Buffer[] | any[];
|
18 | keyPath?: string;
|
19 | cert?: string | string[] | Buffer | Buffer[];
|
20 | certPath?: string;
|
21 | ca?: string | string[] | Buffer | Buffer[];
|
22 | caPaths?: string | string[];
|
23 | rejectUnauthorized?: boolean;
|
24 | ALPNProtocols?: string[] | Buffer[] | Uint8Array[] | Buffer | Uint8Array;
|
25 | }
|
26 | export type AckHandler = (topic: string, message: Buffer, packet: any, cb: (error: Error | number, code?: number) => void) => void;
|
27 | export interface IClientOptions extends ISecureClientOptions {
|
28 | encoding?: BufferEncoding;
|
29 | browserBufferSize?: number;
|
30 | binary?: boolean;
|
31 | my?: any;
|
32 | manualConnect?: boolean;
|
33 | authPacket?: Partial<IAuthPacket>;
|
34 | writeCache?: boolean;
|
35 | servername?: string;
|
36 | defaultProtocol?: MqttProtocol;
|
37 | query?: Record<string, string>;
|
38 | auth?: string;
|
39 | customHandleAcks?: AckHandler;
|
40 | port?: number;
|
41 | host?: string;
|
42 | hostname?: string;
|
43 | unixSocket?: boolean;
|
44 | path?: string;
|
45 | protocol?: MqttProtocol;
|
46 | wsOptions?: ClientOptions | ClientRequestArgs | DuplexOptions;
|
47 | reconnectPeriod?: number;
|
48 | connectTimeout?: number;
|
49 | incomingStore?: IStore;
|
50 | outgoingStore?: IStore;
|
51 | queueQoSZero?: boolean;
|
52 | log?: (...args: any[]) => void;
|
53 | autoUseTopicAlias?: boolean;
|
54 | autoAssignTopicAlias?: boolean;
|
55 | reschedulePings?: boolean;
|
56 | servers?: Array<{
|
57 | host: string;
|
58 | port: number;
|
59 | protocol?: 'wss' | 'ws' | 'mqtt' | 'mqtts' | 'tcp' | 'ssl' | 'wx' | 'wxs';
|
60 | }>;
|
61 | resubscribe?: boolean;
|
62 | transformWsUrl?: (url: string, options: IClientOptions, client: MqttClient) => string;
|
63 | createWebsocket?: (url: string, websocketSubProtocols: string[], options: IClientOptions) => any;
|
64 | messageIdProvider?: IMessageIdProvider;
|
65 | browserBufferTimeout?: number;
|
66 | objectMode?: boolean;
|
67 | clientId?: string;
|
68 | protocolVersion?: IConnectPacket['protocolVersion'];
|
69 | protocolId?: IConnectPacket['protocolId'];
|
70 | clean?: boolean;
|
71 | keepalive?: number;
|
72 | username?: string;
|
73 | password?: Buffer | string;
|
74 | will?: IConnectPacket['will'];
|
75 | properties?: IConnectPacket['properties'];
|
76 | timerVariant?: TimerVariant;
|
77 | }
|
78 | export interface IClientPublishOptions {
|
79 | qos?: QoS;
|
80 | retain?: boolean;
|
81 | dup?: boolean;
|
82 | properties?: IPublishPacket['properties'];
|
83 | cbStorePut?: StorePutCallback;
|
84 | }
|
85 | export interface IClientReconnectOptions {
|
86 | incomingStore?: Store;
|
87 | outgoingStore?: Store;
|
88 | }
|
89 | export interface IClientSubscribeProperties {
|
90 | properties?: ISubscribePacket['properties'];
|
91 | }
|
92 | export interface IClientSubscribeOptions extends IClientSubscribeProperties {
|
93 | qos: QoS;
|
94 | nl?: boolean;
|
95 | rap?: boolean;
|
96 | rh?: number;
|
97 | }
|
98 | export interface ISubscriptionRequest extends IClientSubscribeOptions {
|
99 | topic: string;
|
100 | }
|
101 | export interface ISubscriptionGrant extends Omit<ISubscriptionRequest, 'qos' | 'properties'> {
|
102 | qos: QoS | 128;
|
103 | }
|
104 | export type ISubscriptionMap = {
|
105 | [topic: string]: IClientSubscribeOptions;
|
106 | } & {
|
107 | resubscribe?: boolean;
|
108 | };
|
109 | export { IConnackPacket, IDisconnectPacket, IPublishPacket, Packet };
|
110 | export type OnConnectCallback = (packet: IConnackPacket) => void;
|
111 | export type OnDisconnectCallback = (packet: IDisconnectPacket) => void;
|
112 | export type ClientSubscribeCallback = (err: Error | null, granted?: ISubscriptionGrant[]) => void;
|
113 | export type OnMessageCallback = (topic: string, payload: Buffer, packet: IPublishPacket) => void;
|
114 | export type OnPacketCallback = (packet: Packet) => void;
|
115 | export type OnCloseCallback = () => void;
|
116 | export type OnErrorCallback = (error: Error | ErrorWithReasonCode) => void;
|
117 | export type PacketCallback = (error?: Error, packet?: Packet) => any;
|
118 | export type CloseCallback = (error?: Error) => void;
|
119 | export interface MqttClientEventCallbacks {
|
120 | connect: OnConnectCallback;
|
121 | message: OnMessageCallback;
|
122 | packetsend: OnPacketCallback;
|
123 | packetreceive: OnPacketCallback;
|
124 | disconnect: OnDisconnectCallback;
|
125 | error: OnErrorCallback;
|
126 | close: OnCloseCallback;
|
127 | end: VoidCallback;
|
128 | reconnect: VoidCallback;
|
129 | offline: VoidCallback;
|
130 | outgoingEmpty: VoidCallback;
|
131 | }
|
132 | export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbacks> {
|
133 | static VERSION: any;
|
134 | connected: boolean;
|
135 | disconnecting: boolean;
|
136 | disconnected: boolean;
|
137 | reconnecting: boolean;
|
138 | incomingStore: IStore;
|
139 | outgoingStore: IStore;
|
140 | options: IClientOptions;
|
141 | queueQoSZero: boolean;
|
142 | _reconnectCount: number;
|
143 | log: (...args: any[]) => void;
|
144 | messageIdProvider: IMessageIdProvider;
|
145 | outgoing: Record<number, {
|
146 | volatile: boolean;
|
147 | cb: (err: Error, packet?: Packet) => void;
|
148 | }>;
|
149 | messageIdToTopic: Record<number, string[]>;
|
150 | noop: (error?: any) => void;
|
151 | keepaliveManager: KeepaliveManager;
|
152 | stream: IStream;
|
153 | queue: {
|
154 | packet: Packet;
|
155 | cb: PacketCallback;
|
156 | }[];
|
157 | private streamBuilder;
|
158 | private _resubscribeTopics;
|
159 | private connackTimer;
|
160 | private reconnectTimer;
|
161 | private _storeProcessing;
|
162 | private _packetIdsDuringStoreProcessing;
|
163 | private _storeProcessingQueue;
|
164 | private _firstConnection;
|
165 | private topicAliasRecv;
|
166 | private topicAliasSend;
|
167 | private _deferredReconnect;
|
168 | private connackPacket;
|
169 | static defaultId(): string;
|
170 | constructor(streamBuilder: StreamBuilder, options: IClientOptions);
|
171 | handleAuth(packet: IAuthPacket, callback: PacketCallback): void;
|
172 | handleMessage(packet: IPublishPacket, callback: DoneCallback): void;
|
173 | private _nextId;
|
174 | getLastMessageId(): number;
|
175 | connect(): this;
|
176 | publish(topic: string, message: string | Buffer): MqttClient;
|
177 | publish(topic: string, message: string | Buffer, callback?: PacketCallback): MqttClient;
|
178 | publish(topic: string, message: string | Buffer, opts?: IClientPublishOptions, callback?: PacketCallback): MqttClient;
|
179 | publishAsync(topic: string, message: string | Buffer): Promise<Packet | undefined>;
|
180 | publishAsync(topic: string, message: string | Buffer, opts?: IClientPublishOptions): Promise<Packet | undefined>;
|
181 | subscribe(topicObject: string | string[] | ISubscriptionMap): MqttClient;
|
182 | subscribe(topicObject: string | string[] | ISubscriptionMap, callback?: ClientSubscribeCallback): MqttClient;
|
183 | subscribe(topicObject: string | string[] | ISubscriptionMap, opts?: IClientSubscribeOptions | IClientSubscribeProperties): MqttClient;
|
184 | subscribe(topicObject: string | string[] | ISubscriptionMap, opts?: IClientSubscribeOptions | IClientSubscribeProperties, callback?: ClientSubscribeCallback): MqttClient;
|
185 | subscribeAsync(topicObject: string | string[] | ISubscriptionMap): Promise<ISubscriptionGrant[]>;
|
186 | subscribeAsync(topicObject: string | string[] | ISubscriptionMap, opts?: IClientSubscribeOptions | IClientSubscribeProperties): Promise<ISubscriptionGrant[]>;
|
187 | unsubscribe(topic: string | string[]): MqttClient;
|
188 | unsubscribe(topic: string | string[], opts?: IClientSubscribeOptions): MqttClient;
|
189 | unsubscribe(topic: string | string[], callback?: PacketCallback): MqttClient;
|
190 | unsubscribe(topic: string | string[], opts?: IClientSubscribeOptions, callback?: PacketCallback): MqttClient;
|
191 | unsubscribeAsync(topic: string | string[]): Promise<Packet | undefined>;
|
192 | unsubscribeAsync(topic: string | string[], opts?: IClientSubscribeOptions): Promise<Packet | undefined>;
|
193 | end(cb?: DoneCallback): MqttClient;
|
194 | end(force?: boolean): MqttClient;
|
195 | end(opts?: Partial<IDisconnectPacket>, cb?: DoneCallback): MqttClient;
|
196 | end(force?: boolean, cb?: DoneCallback): MqttClient;
|
197 | end(force?: boolean, opts?: Partial<IDisconnectPacket>, cb?: DoneCallback): MqttClient;
|
198 | endAsync(): Promise<void>;
|
199 | endAsync(force?: boolean): Promise<void>;
|
200 | endAsync(opts?: Partial<IDisconnectPacket>): Promise<void>;
|
201 | endAsync(force?: boolean, opts?: Partial<IDisconnectPacket>): Promise<void>;
|
202 | removeOutgoingMessage(messageId: number): MqttClient;
|
203 | reconnect(opts?: Pick<IClientOptions, 'incomingStore' | 'outgoingStore'>): MqttClient;
|
204 | private _flushVolatile;
|
205 | private _flush;
|
206 | private _removeTopicAliasAndRecoverTopicName;
|
207 | private _checkDisconnecting;
|
208 | private _reconnect;
|
209 | private _setupReconnect;
|
210 | private _clearReconnect;
|
211 | private _cleanUp;
|
212 | private _storeAndSend;
|
213 | private _applyTopicAlias;
|
214 | private _noop;
|
215 | private _writePacket;
|
216 | private _sendPacket;
|
217 | private _storePacket;
|
218 | private _setupKeepaliveManager;
|
219 | private _destroyKeepaliveManager;
|
220 | reschedulePing(): void;
|
221 | private _reschedulePing;
|
222 | sendPing(): void;
|
223 | onKeepaliveTimeout(): void;
|
224 | private _resubscribe;
|
225 | private _onConnect;
|
226 | private _invokeStoreProcessingQueue;
|
227 | private _invokeAllStoreProcessingQueue;
|
228 | private _flushStoreProcessingQueue;
|
229 | private _removeOutgoingAndStoreMessage;
|
230 | }
|