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