Skip to content

Commit 590e744

Browse files
author
Bret Ambrose
committed
Merge branch 'JsMqtt-9-5' into JsMqtt-10-ManualAck
2 parents 5983205 + ef24477 commit 590e744

16 files changed

Lines changed: 326 additions & 300 deletions

lib/browser/aws_iot.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { SocketOptions } from "./io";
1414
import { MqttClientConnection, MqttConnectionConfig, MqttWill } from "./mqtt";
1515
import * as platform from "../common/platform";
1616
import * as iot_shared from "../common/aws_iot_shared"
17+
import { SDK_NAME } from "../common/mqtt_shared";
1718

1819
/**
1920
* Builder functions to create a {@link MqttConnectionConfig} which can then be used to create
@@ -328,12 +329,12 @@ export class AwsIotMqttConnectionConfigBuilder {
328329

329330
// Add the metrics string
330331
if (this.params.username == undefined || this.params.username == null || this.params.username == "") {
331-
this.params.username = "?SDK=NodeJSv2&Version="
332+
this.params.username = `?SDK=${SDK_NAME}&Version=`
332333
} else {
333334
if (this.params.username.indexOf("?") != -1) {
334-
this.params.username += "&SDK=NodeJSv2&Version="
335+
this.params.username += `&SDK=${SDK_NAME}&Version=`
335336
} else {
336-
this.params.username += "?SDK=NodeJSv2&Version="
337+
this.params.username += `?SDK=${SDK_NAME}&Version=`
337338
}
338339
}
339340
this.params.username += platform.crt_version()

lib/browser/mqtt.ts

Lines changed: 6 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,22 @@ import {
3030
MqttConnectionResumed,
3131
MqttRequest,
3232
MqttSubscribeRequest,
33-
MqttWill,
3433
OnConnectionClosedResult,
3534
OnConnectionFailedResult,
3635
OnConnectionSuccessResult,
3736
OnMessageCallback,
3837
Payload,
3938
QoS
4039
} from "../common/mqtt";
41-
import {normalize_payload, normalize_payload_to_buffer} from "../common/mqtt_shared";
40+
41+
import {normalize_payload, normalize_payload_to_buffer, MqttConnectionConfigBase} from "../common/mqtt_shared";
4242
import {once} from "events";
4343

4444
export {
4545
QoS, Payload, MqttRequest, MqttSubscribeRequest, MqttWill, OnMessageCallback, MqttConnectionConnected, MqttConnectionDisconnected,
4646
MqttConnectionResumed, OnConnectionSuccessResult, OnConnectionFailedResult, OnConnectionClosedResult
4747
} from "../common/mqtt";
48+
export { MqttConnectionConfigBase } from "../common/mqtt_shared";
4849

4950
/**
5051
* Listener signature for event emitted from an {@link MqttClientConnection} when an error occurs
@@ -113,88 +114,14 @@ export type AWSCredentials = auth.AWSCredentials;
113114
/**
114115
* Configuration options for an MQTT connection
115116
*
117+
* Extends {@link MqttConnectionConfigBase} with browser-specific options.
118+
*
116119
* @category MQTT
117120
*/
118-
export interface MqttConnectionConfig {
119-
/**
120-
* ID to place in CONNECT packet. Must be unique across all devices/clients.
121-
* If an ID is already in use, the other client will be disconnected.
122-
*/
123-
client_id: string;
124-
125-
/** Server name to connect to */
126-
host_name: string;
127-
128-
/** Server port to connect to */
129-
port: number;
130-
121+
export interface MqttConnectionConfig extends MqttConnectionConfigBase {
131122
/** Socket options, ignored in browser */
132123
socket_options: SocketOptions;
133124

134-
/**
135-
* Whether or not to start a clean session with each reconnect.
136-
* If True, the server will forget all subscriptions with each reconnect.
137-
* Set False to request that the server resume an existing session
138-
* or start a new session that may be resumed after a connection loss.
139-
* The `session_present` bool in the connection callback informs
140-
* whether an existing session was successfully resumed.
141-
* If an existing session is resumed, the server remembers previous subscriptions
142-
* and sends messages (with QoS1 or higher) that were published while the client was offline.
143-
*/
144-
clean_session?: boolean;
145-
146-
/**
147-
* The keep alive value, in seconds, to send in CONNECT packet.
148-
* A PING will automatically be sent at this interval.
149-
* The server will assume the connection is lost if no PING is received after 1.5X this value.
150-
* This duration must be longer than {@link ping_timeout}.
151-
*/
152-
keep_alive?: number;
153-
154-
/**
155-
* Milliseconds to wait for ping response before client assumes
156-
* the connection is invalid and attempts to reconnect.
157-
* This duration must be shorter than keep_alive_secs.
158-
* Alternatively, TCP keep-alive via :attr:`SocketOptions.keep_alive`
159-
* may accomplish this in a more efficient (low-power) scenario,
160-
* but keep-alive options may not work the same way on every platform and OS version.
161-
*/
162-
ping_timeout?: number;
163-
164-
/**
165-
* Milliseconds to wait for the response to the operation requires response by protocol.
166-
* Set to zero to disable timeout. Otherwise, the operation will fail if no response is
167-
* received within this amount of time after the packet is written to the socket.
168-
* It applied to PUBLISH (QoS>0) and UNSUBSCRIBE now.
169-
*/
170-
protocol_operation_timeout?: number;
171-
172-
/**
173-
* Minimum seconds to wait between reconnect attempts.
174-
* Must be <= {@link reconnect_max_sec}.
175-
* Wait starts at min and doubles with each attempt until max is reached.
176-
*/
177-
reconnect_min_sec?: number;
178-
179-
/**
180-
* Maximum seconds to wait between reconnect attempts.
181-
* Must be >= {@link reconnect_min_sec}.
182-
* Wait starts at min and doubles with each attempt until max is reached.
183-
*/
184-
reconnect_max_sec?: number;
185-
186-
/**
187-
* Will to send with CONNECT packet. The will is
188-
* published by the server when its connection to the client is unexpectedly lost.
189-
*/
190-
will?: MqttWill;
191-
192-
/** Username to connect with */
193-
username?: string;
194-
195-
/** Password to connect with */
196-
password?: string;
197-
198125
/** Options for the underlying websocket connection */
199126
websocket?: WebsocketOptions;
200127

lib/browser/mqtt5.ts

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import * as validate from "./mqtt_internal/validate";
2525

2626
export * from "../common/mqtt5";
2727
export * from '../common/mqtt5_packet';
28+
export { Mqtt5ClientConfigBase } from "../common/mqtt_shared";
2829

2930

3031
/**
@@ -125,66 +126,16 @@ export interface Mqtt5WebsocketConfig {
125126
/**
126127
* Configuration options for mqtt5 client creation.
127128
*/
128-
export interface Mqtt5ClientConfig {
129-
130-
/**
131-
* Host name of the MQTT server to connect to.
132-
*/
133-
hostName: string;
134-
135-
/**
136-
* Network port of the MQTT server to connect to.
137-
*/
138-
port: number;
139-
140-
/**
141-
* Controls how the MQTT5 client should behave with respect to MQTT sessions.
142-
*/
143-
sessionBehavior? : mqtt5.ClientSessionBehavior;
144-
145-
/**
146-
* Controls how the reconnect delay is modified in order to smooth out the distribution of reconnection attempt
147-
* timepoints for a large set of reconnecting clients.
148-
*/
149-
retryJitterMode? : mqtt5.RetryJitterType;
150-
151-
/**
152-
* Minimum amount of time to wait to reconnect after a disconnect. Exponential backoff is performed with jitter
153-
* after each connection failure.
154-
*/
155-
minReconnectDelayMs? : number;
156-
157-
/**
158-
* Maximum amount of time to wait to reconnect after a disconnect. Exponential backoff is performed with jitter
159-
* after each connection failure.
160-
*/
161-
maxReconnectDelayMs? : number;
162-
163-
/**
164-
* Amount of time that must elapse with an established connection before the reconnect delay is reset to the minimum.
165-
* This helps alleviate bandwidth-waste in fast reconnect cycles due to permission failures on operations.
166-
*/
167-
minConnectedTimeToResetReconnectDelayMs? : number;
168-
169-
/**
170-
* All configurable options with respect to the CONNECT packet sent by the client, including the will. These
171-
* connect properties will be used for every connection attempt made by the client.
172-
*/
173-
connectProperties?: mqtt5_packet.ConnectPacket;
129+
export interface Mqtt5ClientConfig extends mqtt_shared.Mqtt5ClientConfigBase {
174130

175131
/**
176132
* Overall time interval to wait to establish an MQTT connection. If a complete MQTT connection (from socket
177133
* establishment all the way up to CONNACK receipt) has not been established before this timeout expires,
178134
* the connection attempt will be considered a failure.
179-
*/
180-
connectTimeoutMs? : number;
181-
182-
/**
183-
* Additional controls for client behavior with respect to topic alias usage.
184135
*
185-
* If this setting is left undefined, then topic aliasing behavior will be disabled.
136+
* @group Browser-only
186137
*/
187-
topicAliasingOptions? : mqtt5.TopicAliasingOptions
138+
connectTimeoutMs? : number;
188139

189140
/**
190141
* Options for the underlying websocket connection

lib/common/aws_iot_shared.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,36 @@
44
*/
55

66
import * as iot_shared from "./aws_iot_shared";
7+
import { SDK_NAME } from "./mqtt_shared";
78

89
jest.setTimeout(10000);
910

1011
test('Aws IoT Mqtt5 Username Construction - No Custom Auth', async () => {
1112
let finalUsername : string = iot_shared.buildMqtt5FinalUsername(undefined);
1213

13-
expect(finalUsername).toEqual(expect.stringContaining("?SDK=NodeJSv2&Version="));
14+
expect(finalUsername).toEqual(expect.stringContaining(`?SDK=${SDK_NAME}&Version=`));
1415
});
1516

1617
test('Aws IoT Mqtt5 Username Construction - Empty custom auth', async () => {
1718
let finalUsername : string = iot_shared.buildMqtt5FinalUsername({});
1819

19-
expect(finalUsername).toEqual(expect.stringContaining("?SDK=NodeJSv2&Version="));
20+
expect(finalUsername).toEqual(expect.stringContaining(`?SDK=${SDK_NAME}&Version=`));
2021
});
2122

22-
2323
test('Aws IoT Mqtt5 Username Construction - Simple username', async () => {
2424
let finalUsername : string = iot_shared.buildMqtt5FinalUsername({
2525
username: "Derp"
2626
});
2727

28-
expect(finalUsername).toEqual(expect.stringContaining("Derp?SDK=NodeJSv2&Version="));
28+
expect(finalUsername).toEqual(expect.stringContaining(`Derp?SDK=${SDK_NAME}&Version=`));
2929
});
3030

3131
test('Aws IoT Mqtt5 Username Construction - Query param username', async () => {
3232
let finalUsername : string = iot_shared.buildMqtt5FinalUsername({
3333
username: "Derp?Param1=Value1"
3434
});
3535

36-
expect(finalUsername).toEqual(expect.stringContaining("Derp?Param1=Value1&SDK=NodeJSv2&Version="));
36+
expect(finalUsername).toEqual(expect.stringContaining(`Derp?Param1=Value1&SDK=${SDK_NAME}&Version=`));
3737
});
3838

3939
test('Aws IoT Mqtt5 Username Construction - Authorizer Name', async () => {
@@ -42,7 +42,7 @@ test('Aws IoT Mqtt5 Username Construction - Authorizer Name', async () => {
4242
authorizerName: "MyAuthorizer"
4343
});
4444

45-
expect(finalUsername).toEqual(expect.stringContaining("Hello?x-amz-customauthorizer-name=MyAuthorizer&SDK=NodeJSv2&Version="));
45+
expect(finalUsername).toEqual(expect.stringContaining(`Hello?x-amz-customauthorizer-name=MyAuthorizer&SDK=${SDK_NAME}&Version=`));
4646
});
4747

4848
test('Aws IoT Mqtt5 Username Construction - Token Signing', async () => {
@@ -54,7 +54,7 @@ test('Aws IoT Mqtt5 Username Construction - Token Signing', async () => {
5454
tokenSignature: "SignedToken"
5555
});
5656

57-
expect(finalUsername).toEqual(expect.stringContaining("Hello?x-amz-customauthorizer-name=MyAuthorizer&MyToken=TheToken&x-amz-customauthorizer-signature=SignedToken&SDK=NodeJSv2&Version="));
57+
expect(finalUsername).toEqual(expect.stringContaining(`Hello?x-amz-customauthorizer-name=MyAuthorizer&MyToken=TheToken&x-amz-customauthorizer-signature=SignedToken&SDK=${SDK_NAME}&Version=`));
5858
});
5959

6060
test('Aws IoT Mqtt5 Username Construction Failure - Missing token key name', async () => {

lib/common/aws_iot_shared.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import * as platform from "./platform";
1616
import * as mqtt5_packet from "./mqtt5_packet";
1717
import * as utils from "./utils";
18+
import { SDK_NAME } from "./mqtt_shared";
1819

1920
/**
2021
* A helper function to add parameters to the username in with_custom_authorizer function
@@ -204,7 +205,7 @@ function addParam(paramName: string, paramValue: string | undefined, paramSet: [
204205
*
205206
* @internal
206207
*/
207-
export function buildMqtt5FinalUsername(customAuthConfig?: MqttConnectCustomAuthConfig) : string {
208+
export function buildMqtt5FinalUsername(customAuthConfig?: MqttConnectCustomAuthConfig): string {
208209

209210
let path : string = "";
210211
let paramList : [string, string][] = [];
@@ -241,7 +242,7 @@ export function buildMqtt5FinalUsername(customAuthConfig?: MqttConnectCustomAuth
241242
}
242243
}
243244

244-
paramList.push(["SDK", "NodeJSv2"]);
245+
paramList.push(["SDK", SDK_NAME]);
245246
paramList.push(["Version", platform.crt_version()]);
246247

247248
return (path ?? "") + "?" + paramList.map((value : [string, string]) => `${value[0]}=${value[1]}`).join("&");

lib/common/mqtt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,4 @@ export const DEFAULT_RECONNECT_MAX_SEC = 128;
197197
*
198198
* @category MQTT
199199
*/
200-
export const DEFAULT_RECONNECT_MIN_SEC = 1;
200+
export const DEFAULT_RECONNECT_MIN_SEC = 1;

0 commit comments

Comments
 (0)