Skip to content

Commit

Permalink
Checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bretambrose committed Nov 15, 2024
1 parent a4c6563 commit 3b30f89
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 304 deletions.
3 changes: 1 addition & 2 deletions lib/browser/mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/

import * as mqtt from "mqtt";
import * as mqtt_packet from "mqtt-packet";
import * as WebsocketUtils from "./ws";
import * as auth from "./auth";
import { Trie, TrieOp, Node as TrieNode } from "./trie";
Expand Down Expand Up @@ -615,7 +614,7 @@ export class MqttClientConnection extends BufferedEventEmitter {
}
resolve({
packet_id: packet
? (packet as mqtt_packet.IUnsubackPacket).messageId
? (packet as mqtt.IUnsubackPacket).messageId
: undefined,
});
});
Expand Down
4 changes: 2 additions & 2 deletions lib/browser/mqtt5.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import url from "url";
import {HttpsProxyAgent} from "https-proxy-agent";
import * as auth from "./auth";

jest.setTimeout(1000000);
jest.setTimeout(10000);

function createBrowserSpecificTestConfig (testType: test_utils.SuccessfulConnectionTestType) : mqtt5.Mqtt5ClientConfig {

Expand Down Expand Up @@ -405,7 +405,7 @@ test_utils.conditional_test(test_utils.ClientEnvironmentalConfig.hasIotCoreEnvir
await test_utils.testNegotiatedSettings(forcedRejoinClient, true);
});

test('Sub - Pub QoS 0 - Unsub', async () => {
test_utils.conditional_test(test_utils.ClientEnvironmentalConfig.hasIotCoreEnvironment())('Sub - Pub QoS 0 - Unsub', async () => {
let topic : string = `test/${uuid()}`;
let testPayload : Buffer = Buffer.from("Derp", "utf-8");

Expand Down
17 changes: 6 additions & 11 deletions lib/browser/mqtt5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import {BufferedEventEmitter} from "../common/event";
import * as mqtt from "mqtt"; /* The mqtt-js external dependency */
import * as mqtt_packet from "mqtt-packet";
import * as mqtt5 from "../common/mqtt5";
import {OutboundTopicAliasBehaviorType} from "../common/mqtt5";
import * as mqtt5_packet from "../common/mqtt5_packet"
Expand Down Expand Up @@ -446,19 +445,15 @@ export class Mqtt5Client extends BufferedEventEmitter implements mqtt5.IMqtt5Cli
let subMap: mqtt.ISubscriptionMap = mqtt_utils.transform_crt_subscribe_to_mqtt_js_subscription_map(packet);
let subOptions: mqtt.IClientSubscribeOptions = mqtt_utils.transform_crt_subscribe_to_mqtt_js_subscribe_options(packet);

this.browserClient.subscribe(subMap, subOptions, (error, grants, suback) => {
// @ts-ignore
this.browserClient.subscribe(subMap, subOptions, (error, grants) => {
if (error) {
reject(error);
return;
}

if (suback) {
const crtSubackFromMqttjsSuback = mqtt_utils.transform_mqtt_js_suback_to_crt_suback(suback);
resolve(crtSubackFromMqttjsSuback);
} else {
const crtSubackFromGrants: mqtt5_packet.SubackPacket = mqtt_utils.transform_mqtt_js_subscription_grants_to_crt_suback(grants ?? []);
resolve(crtSubackFromGrants);
}
const suback: mqtt5_packet.SubackPacket = mqtt_utils.transform_mqtt_js_subscription_grants_to_crt_suback(grants);
resolve(suback);
});
} catch (err) {
reject(err);
Expand Down Expand Up @@ -510,7 +505,7 @@ export class Mqtt5Client extends BufferedEventEmitter implements mqtt5.IMqtt5Cli
};
resolve(unsuback);
} else {
const unsuback: mqtt5_packet.UnsubackPacket = mqtt_utils.transform_mqtt_js_unsuback_to_crt_unsuback(packet as mqtt_packet.IUnsubackPacket);
const unsuback: mqtt5_packet.UnsubackPacket = mqtt_utils.transform_mqtt_js_unsuback_to_crt_unsuback(packet as mqtt.IUnsubackPacket);
resolve(unsuback);
}
});
Expand Down Expand Up @@ -612,7 +607,7 @@ export class Mqtt5Client extends BufferedEventEmitter implements mqtt5.IMqtt5Cli
})
}

const puback: mqtt5_packet.PubackPacket = mqtt_utils.transform_mqtt_js_puback_to_crt_puback(completionPacket as mqtt_packet.IPubackPacket);
const puback: mqtt5_packet.PubackPacket = mqtt_utils.transform_mqtt_js_puback_to_crt_puback(completionPacket as mqtt.IPubackPacket);
resolve(puback);
break;

Expand Down
64 changes: 13 additions & 51 deletions lib/browser/mqtt5_utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

import * as mqtt from "mqtt";
import * as mqtt_packet from "mqtt-packet";
import * as mqtt5 from "./mqtt5";
import {InboundTopicAliasBehaviorType, OutboundTopicAliasBehaviorType} from "./mqtt5";
import * as mqtt5_utils from "./mqtt5_utils";
Expand All @@ -19,7 +18,7 @@ test('MQTT.JS User Properties to CRT User Properties undefined', async () => {
});

test('MQTT.JS User Properties to CRT User Properties single', async () => {
let mqttJsUserProperties : mqtt_packet.UserProperties = {
let mqttJsUserProperties : mqtt.UserProperties = {
prop1 : "value1",
prop2 : "value2"
}
Expand All @@ -42,7 +41,7 @@ test('MQTT.JS User Properties to CRT User Properties single', async () => {
});

test('MQTT.JS User Properties to CRT User Properties multi', async () => {
let mqttJsUserProperties : mqtt_packet.UserProperties = {
let mqttJsUserProperties : mqtt.UserProperties = {
prop1 : "value1",
prop2 : ["value2_1", "value2_2", "value2_3"]
}
Expand Down Expand Up @@ -73,7 +72,7 @@ test('MQTT.JS User Properties to CRT User Properties multi', async () => {
});

test('CRT User Properties to MQTT.js User Properties undefined', async () => {
let mqttJsUserProperties : mqtt_packet.UserProperties | undefined = mqtt5_utils.transform_crt_user_properties_to_mqtt_js_user_properties(undefined);
let mqttJsUserProperties : mqtt.UserProperties | undefined = mqtt5_utils.transform_crt_user_properties_to_mqtt_js_user_properties(undefined);

expect(mqttJsUserProperties).toBeUndefined();
});
Expand All @@ -84,7 +83,7 @@ test('CRT User Properties to MQTT.js User Properties single', async () => {
{ name : "prop2", value: "value2"}
]

let mqttJsUserProperties : mqtt_packet.UserProperties | undefined = mqtt5_utils.transform_crt_user_properties_to_mqtt_js_user_properties(crtUserProperties);
let mqttJsUserProperties : mqtt.UserProperties | undefined = mqtt5_utils.transform_crt_user_properties_to_mqtt_js_user_properties(crtUserProperties);

expect(mqttJsUserProperties).toEqual(
{
Expand All @@ -101,9 +100,9 @@ test('CRT User Properties to MQTT.js User Properties single', async () => {
{ name : "prop2", value: "value2_3"}
]

let mqttJsUserProperties : mqtt_packet.UserProperties | undefined = mqtt5_utils.transform_crt_user_properties_to_mqtt_js_user_properties(crtUserProperties);
let mqttJsUserProperties : mqtt.UserProperties | undefined = mqtt5_utils.transform_crt_user_properties_to_mqtt_js_user_properties(crtUserProperties);
expect(mqttJsUserProperties).toBeDefined();
let definedProperties : mqtt_packet.UserProperties = mqttJsUserProperties ?? {};
let definedProperties : mqtt.UserProperties = mqttJsUserProperties ?? {};

const {prop1 : propOne, prop2: propTwo, ...rest} = definedProperties;

Expand Down Expand Up @@ -707,44 +706,6 @@ test('transform_mqtt_js_subscription_grants_to_crt_suback', async() => {
});
});

test('transform_mqtt_js_suback_to_crt_suback - minimal', async() => {
let mqttJsSuback : mqtt_packet.ISubackPacket = {
cmd: "suback",
granted: [1]
};

let suback : mqtt5.SubackPacket = mqtt5_utils.transform_mqtt_js_suback_to_crt_suback(mqttJsSuback);

expect(suback).toEqual({
type: mqtt5.PacketType.Suback,
reasonCodes: [mqtt5.SubackReasonCode.GrantedQoS1]
});
});

test('transform_mqtt_js_suback_to_crt_suback - maximal', async() => {
let mqttJsSuback : mqtt_packet.ISubackPacket = {
cmd: "suback",
granted: [2, 128],
properties : {
reasonString: "Misadventure",
userProperties: {
world: ["hello"]
}
}
};

let suback : mqtt5.SubackPacket = mqtt5_utils.transform_mqtt_js_suback_to_crt_suback(mqttJsSuback);

expect(suback).toEqual({
type: mqtt5.PacketType.Suback,
reasonCodes: [mqtt5.SubackReasonCode.GrantedQoS2, mqtt5.SubackReasonCode.UnspecifiedError],
reasonString: "Misadventure",
userProperties: [
{name: "world", value: "hello"}
]
});
});

test('transform_crt_publish_to_mqtt_js_publish_options minimal', async() => {
let publish : mqtt5.PublishPacket = {
topicName: "hello/there",
Expand Down Expand Up @@ -865,7 +826,7 @@ test('transform_mqtt_js_publish_to_crt_publish maximal', async() => {
});

test('transform_mqtt_js_puback_to_crt_puback minimal', async() => {
let mqttJsPuback : mqtt_packet.IPubackPacket = {
let mqttJsPuback : mqtt.IPubackPacket = {
cmd: 'puback'
};

Expand All @@ -878,7 +839,7 @@ test('transform_mqtt_js_puback_to_crt_puback minimal', async() => {
});

test('transform_mqtt_js_puback_to_crt_puback maximal', async() => {
let mqttJsPuback : mqtt_packet.IPubackPacket = {
let mqttJsPuback : mqtt.IPubackPacket = {
cmd: 'puback',
reasonCode: mqtt5.PubackReasonCode.NotAuthorized,
properties: {
Expand Down Expand Up @@ -931,9 +892,9 @@ test('transform_crt_unsubscribe_to_mqtt_js_unsubscribe_options maximal', async()
});

test('transform_mqtt_js_unsuback_to_crt_unsuback minimal', async() => {
let mqttJsUnsuback : mqtt_packet.IUnsubackPacket = {
let mqttJsUnsuback : mqtt.IUnsubackPacket = {
cmd: 'unsuback',
granted: [mqtt5.UnsubackReasonCode.NoSubscriptionExisted]
reasonCode: mqtt5.UnsubackReasonCode.NoSubscriptionExisted
};

let crtUnsuback : mqtt5.UnsubackPacket = mqtt5_utils.transform_mqtt_js_unsuback_to_crt_unsuback(mqttJsUnsuback);
Expand All @@ -945,9 +906,10 @@ test('transform_mqtt_js_unsuback_to_crt_unsuback minimal', async() => {
});

test('transform_mqtt_js_unsuback_to_crt_unsuback maximal', async() => {
let mqttJsUnsuback : mqtt_packet.IUnsubackPacket = {
let mqttJsUnsuback : mqtt.IUnsubackPacket = {
cmd: 'unsuback',
granted: [mqtt5.UnsubackReasonCode.NoSubscriptionExisted, mqtt5.UnsubackReasonCode.ImplementationSpecificError],
// @ts-ignore
reasonCode: [mqtt5.UnsubackReasonCode.NoSubscriptionExisted, mqtt5.UnsubackReasonCode.ImplementationSpecificError],
properties: {
reasonString: "Dunno",
userProperties: {
Expand Down
23 changes: 16 additions & 7 deletions lib/browser/mqtt5_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import * as mqtt from "mqtt";
import * as mqtt_packet from "mqtt-packet";
import * as mqtt_shared from "../common/mqtt_shared";
import * as mqtt5 from "./mqtt5";
import * as utils from "../common/utils";
Expand Down Expand Up @@ -252,7 +251,7 @@ export function create_mqtt_js_client_config_from_crt_client_config(crtConfig :
}

/** @internal */
export function transform_crt_user_properties_to_mqtt_js_user_properties(userProperties?: mqtt5.UserProperty[]) : mqtt_packet.UserProperties | undefined {
export function transform_crt_user_properties_to_mqtt_js_user_properties(userProperties?: mqtt5.UserProperty[]) : mqtt.UserProperties | undefined {
if (!userProperties) {
return undefined;
}
Expand All @@ -275,7 +274,7 @@ export function transform_crt_user_properties_to_mqtt_js_user_properties(userPro
}

/** @internal */
export function transform_mqtt_js_user_properties_to_crt_user_properties(userProperties?: mqtt_packet.UserProperties) : mqtt5.UserProperty[] | undefined {
export function transform_mqtt_js_user_properties_to_crt_user_properties(userProperties?: mqtt.UserProperties) : mqtt5.UserProperty[] | undefined {
if (!userProperties) {
return undefined;
}
Expand Down Expand Up @@ -403,7 +402,7 @@ export function transform_crt_subscribe_to_mqtt_js_subscribe_options(subscribe:
}

/** @internal **/
export function transform_mqtt_js_suback_to_crt_suback(mqttJsSuback: mqtt_packet.ISubackPacket) : mqtt5.SubackPacket {
export function transform_mqtt_js_suback_to_crt_suback(mqttJsSuback: mqtt.ISubackPacket) : mqtt5.SubackPacket {
if (!mqttJsSuback) {
throw new CrtError("transform_mqtt_js_suback_to_crt_suback: mqttJsSuback not defined");
}
Expand Down Expand Up @@ -521,7 +520,7 @@ export function transform_mqtt_js_publish_to_crt_publish(publish: mqtt.IPublishP
}

/** @internal **/
export function transform_mqtt_js_puback_to_crt_puback(puback: mqtt_packet.IPubackPacket) : mqtt5.PubackPacket {
export function transform_mqtt_js_puback_to_crt_puback(puback: mqtt.IPubackPacket) : mqtt5.PubackPacket {

if (puback == null || puback == undefined) {
throw new CrtError("transform_mqtt_js_puback_to_crt_puback: puback not defined");
Expand Down Expand Up @@ -562,15 +561,25 @@ export function transform_crt_unsubscribe_to_mqtt_js_unsubscribe_options(unsubsc
}

/** @internal **/
export function transform_mqtt_js_unsuback_to_crt_unsuback(packet: mqtt_packet.IUnsubackPacket) : mqtt5.UnsubackPacket {
export function transform_mqtt_js_unsuback_to_crt_unsuback(packet: mqtt.IUnsubackPacket) : mqtt5.UnsubackPacket {

if (packet == null || packet == undefined) {
throw new CrtError("transform_mqtt_js_unsuback_to_crt_unsuback: packet not defined");
}

let reasonCodes : number | number[] | undefined = packet.reasonCode;
let codes : number[];
if (Array.isArray(reasonCodes)) {
codes = reasonCodes;
} else if (typeof reasonCodes == 'number') {
codes = [reasonCodes];
} else {
codes = [];
}

let crtUnsuback : mqtt5.UnsubackPacket = {
type: mqtt5.PacketType.Unsuback,
reasonCodes : packet.granted
reasonCodes : codes
}

if (packet.properties) {
Expand Down
Loading

0 comments on commit 3b30f89

Please sign in to comment.