@seriousme/opifex 1.9.3 → 1.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -26
- package/bin/conformanceServer.ts +3 -2
- package/bin/demoServer.ts +3 -2
- package/bin/mqtt.ts +126 -117
- package/client/client.ts +35 -15
- package/client/context.ts +42 -22
- package/client/deps.ts +5 -0
- package/client/handlers/handleConnack.ts +17 -5
- package/client/handlers/handlePublish.ts +4 -1
- package/client/handlers/handlePubrec.ts +3 -1
- package/client/handlers/handlePubrel.ts +3 -1
- package/client/handlers/handleSuback.ts +3 -1
- package/client/mod.ts +8 -1
- package/client/store/memoryStore.ts +2 -1
- package/dev_utils/dummyConn.ts +17 -2
- package/dist/bin/conformanceServer.js +3 -2
- package/dist/bin/conformanceServer.js.map +1 -1
- package/dist/bin/demoServer.js +3 -2
- package/dist/bin/demoServer.js.map +1 -1
- package/dist/bin/mqtt.d.ts +29 -0
- package/dist/bin/mqtt.d.ts.map +1 -1
- package/dist/bin/mqtt.js +101 -96
- package/dist/bin/mqtt.js.map +1 -1
- package/dist/client/client.d.ts +7 -4
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +17 -9
- package/dist/client/client.js.map +1 -1
- package/dist/client/context.d.ts +3 -1
- package/dist/client/context.d.ts.map +1 -1
- package/dist/client/context.js +27 -13
- package/dist/client/context.js.map +1 -1
- package/dist/client/deps.d.ts +3 -2
- package/dist/client/deps.d.ts.map +1 -1
- package/dist/client/deps.js +2 -1
- package/dist/client/deps.js.map +1 -1
- package/dist/client/handlers/handleConnack.d.ts +1 -1
- package/dist/client/handlers/handleConnack.d.ts.map +1 -1
- package/dist/client/handlers/handleConnack.js +14 -5
- package/dist/client/handlers/handleConnack.js.map +1 -1
- package/dist/client/handlers/handlePublish.d.ts +1 -1
- package/dist/client/handlers/handlePublish.d.ts.map +1 -1
- package/dist/client/handlers/handlePublish.js +2 -0
- package/dist/client/handlers/handlePublish.js.map +1 -1
- package/dist/client/handlers/handlePubrec.d.ts +1 -1
- package/dist/client/handlers/handlePubrec.d.ts.map +1 -1
- package/dist/client/handlers/handlePubrec.js +1 -0
- package/dist/client/handlers/handlePubrec.js.map +1 -1
- package/dist/client/handlers/handlePubrel.d.ts +1 -1
- package/dist/client/handlers/handlePubrel.d.ts.map +1 -1
- package/dist/client/handlers/handlePubrel.js +1 -0
- package/dist/client/handlers/handlePubrel.js.map +1 -1
- package/dist/client/handlers/handleSuback.d.ts.map +1 -1
- package/dist/client/handlers/handleSuback.js +3 -1
- package/dist/client/handlers/handleSuback.js.map +1 -1
- package/dist/client/mod.d.ts +3 -1
- package/dist/client/mod.d.ts.map +1 -1
- package/dist/client/mod.js +2 -1
- package/dist/client/mod.js.map +1 -1
- package/dist/client/store/memoryStore.d.ts +1 -1
- package/dist/client/store/memoryStore.d.ts.map +1 -1
- package/dist/client/store/memoryStore.js.map +1 -1
- package/dist/mqttConn/mqttConn.d.ts +13 -8
- package/dist/mqttConn/mqttConn.d.ts.map +1 -1
- package/dist/mqttConn/mqttConn.js +27 -12
- package/dist/mqttConn/mqttConn.js.map +1 -1
- package/dist/mqttPacket/PacketType.d.ts +1 -0
- package/dist/mqttPacket/PacketType.d.ts.map +1 -1
- package/dist/mqttPacket/PacketType.js +1 -0
- package/dist/mqttPacket/PacketType.js.map +1 -1
- package/dist/mqttPacket/Properties.d.ts +157 -0
- package/dist/mqttPacket/Properties.d.ts.map +1 -0
- package/dist/mqttPacket/Properties.js +177 -0
- package/dist/mqttPacket/Properties.js.map +1 -0
- package/dist/mqttPacket/ReasonCode.d.ts +74 -0
- package/dist/mqttPacket/ReasonCode.d.ts.map +1 -0
- package/dist/mqttPacket/ReasonCode.js +230 -0
- package/dist/mqttPacket/ReasonCode.js.map +1 -0
- package/dist/mqttPacket/RetainHandling.d.ts +12 -0
- package/dist/mqttPacket/RetainHandling.d.ts.map +1 -0
- package/dist/mqttPacket/RetainHandling.js +12 -0
- package/dist/mqttPacket/RetainHandling.js.map +1 -0
- package/dist/mqttPacket/auth.d.ts +21 -0
- package/dist/mqttPacket/auth.d.ts.map +1 -0
- package/dist/mqttPacket/auth.js +34 -0
- package/dist/mqttPacket/auth.js.map +1 -0
- package/dist/mqttPacket/connack.d.ts +15 -7
- package/dist/mqttPacket/connack.d.ts.map +1 -1
- package/dist/mqttPacket/connack.js +43 -15
- package/dist/mqttPacket/connack.js.map +1 -1
- package/dist/mqttPacket/connect.d.ts +27 -8
- package/dist/mqttPacket/connect.d.ts.map +1 -1
- package/dist/mqttPacket/connect.js +106 -30
- package/dist/mqttPacket/connect.js.map +1 -1
- package/dist/mqttPacket/decoder.d.ts +42 -3
- package/dist/mqttPacket/decoder.d.ts.map +1 -1
- package/dist/mqttPacket/decoder.js +135 -4
- package/dist/mqttPacket/decoder.js.map +1 -1
- package/dist/mqttPacket/disconnect.d.ts +13 -7
- package/dist/mqttPacket/disconnect.d.ts.map +1 -1
- package/dist/mqttPacket/disconnect.js +45 -6
- package/dist/mqttPacket/disconnect.js.map +1 -1
- package/dist/mqttPacket/encoder.d.ts +57 -8
- package/dist/mqttPacket/encoder.d.ts.map +1 -1
- package/dist/mqttPacket/encoder.js +185 -13
- package/dist/mqttPacket/encoder.js.map +1 -1
- package/dist/mqttPacket/length.d.ts.map +1 -1
- package/dist/mqttPacket/length.js +3 -2
- package/dist/mqttPacket/length.js.map +1 -1
- package/dist/mqttPacket/mod.d.ts +56 -91
- package/dist/mqttPacket/mod.d.ts.map +1 -1
- package/dist/mqttPacket/mod.js +22 -22
- package/dist/mqttPacket/mod.js.map +1 -1
- package/dist/mqttPacket/pingreq.d.ts +4 -6
- package/dist/mqttPacket/pingreq.d.ts.map +1 -1
- package/dist/mqttPacket/pingreq.js +5 -4
- package/dist/mqttPacket/pingreq.js.map +1 -1
- package/dist/mqttPacket/pingres.d.ts +4 -6
- package/dist/mqttPacket/pingres.d.ts.map +1 -1
- package/dist/mqttPacket/pingres.js +7 -5
- package/dist/mqttPacket/pingres.js.map +1 -1
- package/dist/mqttPacket/protocolLevels.d.ts +11 -0
- package/dist/mqttPacket/protocolLevels.d.ts.map +1 -0
- package/dist/mqttPacket/protocolLevels.js +11 -0
- package/dist/mqttPacket/protocolLevels.js.map +1 -0
- package/dist/mqttPacket/pubblishAcks.d.ts +35 -0
- package/dist/mqttPacket/pubblishAcks.d.ts.map +1 -0
- package/dist/mqttPacket/pubblishAcks.js +56 -0
- package/dist/mqttPacket/pubblishAcks.js.map +1 -0
- package/dist/mqttPacket/publish.d.ts +19 -8
- package/dist/mqttPacket/publish.d.ts.map +1 -1
- package/dist/mqttPacket/publish.js +26 -7
- package/dist/mqttPacket/publish.js.map +1 -1
- package/dist/mqttPacket/suback.d.ts +14 -7
- package/dist/mqttPacket/suback.d.ts.map +1 -1
- package/dist/mqttPacket/suback.js +41 -20
- package/dist/mqttPacket/suback.js.map +1 -1
- package/dist/mqttPacket/subscribe.d.ts +24 -9
- package/dist/mqttPacket/subscribe.d.ts.map +1 -1
- package/dist/mqttPacket/subscribe.js +71 -15
- package/dist/mqttPacket/subscribe.js.map +1 -1
- package/dist/mqttPacket/types.d.ts +33 -0
- package/dist/mqttPacket/types.d.ts.map +1 -1
- package/dist/mqttPacket/unsuback.d.ts +14 -7
- package/dist/mqttPacket/unsuback.d.ts.map +1 -1
- package/dist/mqttPacket/unsuback.js +29 -6
- package/dist/mqttPacket/unsuback.js.map +1 -1
- package/dist/mqttPacket/unsubscribe.d.ts +14 -7
- package/dist/mqttPacket/unsubscribe.d.ts.map +1 -1
- package/dist/mqttPacket/unsubscribe.js +24 -8
- package/dist/mqttPacket/unsubscribe.js.map +1 -1
- package/dist/node/wrapNodeSocket.d.ts.map +1 -1
- package/dist/node/wrapNodeSocket.js.map +1 -1
- package/dist/persistence/deps.d.ts +1 -1
- package/dist/persistence/deps.d.ts.map +1 -1
- package/dist/persistence/deps.js +1 -1
- package/dist/persistence/deps.js.map +1 -1
- package/dist/server/context.d.ts +3 -1
- package/dist/server/context.d.ts.map +1 -1
- package/dist/server/context.js +8 -2
- package/dist/server/context.js.map +1 -1
- package/dist/server/deps.d.ts +2 -2
- package/dist/server/deps.d.ts.map +1 -1
- package/dist/server/deps.js +1 -1
- package/dist/server/deps.js.map +1 -1
- package/dist/server/handlers/handleConnect.d.ts +1 -1
- package/dist/server/handlers/handleConnect.d.ts.map +1 -1
- package/dist/server/handlers/handleConnect.js +9 -1
- package/dist/server/handlers/handleConnect.js.map +1 -1
- package/dist/server/handlers/handlePacket.d.ts +1 -1
- package/dist/server/handlers/handlePacket.d.ts.map +1 -1
- package/dist/server/handlers/handlePacket.js +1 -1
- package/dist/server/handlers/handlePacket.js.map +1 -1
- package/dist/server/handlers/handlePingreq.d.ts.map +1 -1
- package/dist/server/handlers/handlePingreq.js +1 -0
- package/dist/server/handlers/handlePingreq.js.map +1 -1
- package/dist/server/handlers/handlePublish.d.ts +2 -2
- package/dist/server/handlers/handlePublish.d.ts.map +1 -1
- package/dist/server/handlers/handlePublish.js +2 -0
- package/dist/server/handlers/handlePublish.js.map +1 -1
- package/dist/server/handlers/handlePubrec.d.ts +1 -1
- package/dist/server/handlers/handlePubrec.d.ts.map +1 -1
- package/dist/server/handlers/handlePubrec.js +1 -0
- package/dist/server/handlers/handlePubrec.js.map +1 -1
- package/dist/server/handlers/handlePubrel.d.ts +1 -1
- package/dist/server/handlers/handlePubrel.d.ts.map +1 -1
- package/dist/server/handlers/handlePubrel.js +1 -0
- package/dist/server/handlers/handlePubrel.js.map +1 -1
- package/dist/server/handlers/handleSubscribe.d.ts +1 -1
- package/dist/server/handlers/handleSubscribe.d.ts.map +1 -1
- package/dist/server/handlers/handleSubscribe.js +2 -1
- package/dist/server/handlers/handleSubscribe.js.map +1 -1
- package/dist/server/handlers/handleUnsubscribe.d.ts +1 -1
- package/dist/server/handlers/handleUnsubscribe.d.ts.map +1 -1
- package/dist/server/handlers/handleUnsubscribe.js +1 -0
- package/dist/server/handlers/handleUnsubscribe.js.map +1 -1
- package/dist/server/server.d.ts +2 -2
- package/dist/server/server.d.ts.map +1 -1
- package/dist/server/server.js +5 -3
- package/dist/server/server.js.map +1 -1
- package/dist/socket/socket.d.ts.map +1 -1
- package/dist/socket/socket.js +6 -1
- package/dist/socket/socket.js.map +1 -1
- package/dist/utils/assert.d.ts +0 -3
- package/dist/utils/assert.d.ts.map +1 -1
- package/dist/utils/assert.js +0 -7
- package/dist/utils/assert.js.map +1 -1
- package/dist/utils/mod.d.ts +0 -3
- package/dist/utils/mod.d.ts.map +1 -1
- package/dist/utils/mod.js +0 -2
- package/dist/utils/mod.js.map +1 -1
- package/mqttConn/mqttConn.test.ts +27 -12
- package/mqttConn/mqttConn.ts +45 -14
- package/mqttPacket/PacketType.ts +1 -0
- package/mqttPacket/Properties.ts +276 -0
- package/mqttPacket/ReasonCode.test.ts +22 -0
- package/mqttPacket/ReasonCode.ts +256 -0
- package/mqttPacket/RetainHandling.ts +11 -0
- package/mqttPacket/auth.test.ts +112 -0
- package/mqttPacket/auth.ts +72 -0
- package/mqttPacket/connack.test.ts +84 -64
- package/mqttPacket/connack.ts +84 -20
- package/mqttPacket/connect.test.ts +497 -18
- package/mqttPacket/connect.ts +167 -36
- package/mqttPacket/decoder.test.ts +33 -16
- package/mqttPacket/decoder.ts +173 -5
- package/mqttPacket/disconnect.test.ts +97 -18
- package/mqttPacket/disconnect.ts +82 -10
- package/mqttPacket/encoder.test.ts +32 -8
- package/mqttPacket/encoder.ts +236 -11
- package/mqttPacket/length.ts +3 -2
- package/mqttPacket/mod.ts +75 -32
- package/mqttPacket/pingreq.test.ts +52 -4
- package/mqttPacket/pingreq.ts +20 -7
- package/mqttPacket/pingres.test.ts +52 -4
- package/mqttPacket/pingres.ts +22 -8
- package/mqttPacket/properties.test.ts +193 -0
- package/mqttPacket/protocolLevels.ts +13 -0
- package/mqttPacket/pubblishAcks.ts +117 -0
- package/mqttPacket/publish.test.ts +212 -133
- package/mqttPacket/publish.ts +69 -14
- package/mqttPacket/publishAcks.test.ts +128 -0
- package/mqttPacket/suback.test.ts +71 -27
- package/mqttPacket/suback.ts +78 -26
- package/mqttPacket/subscribe.test.ts +132 -43
- package/mqttPacket/subscribe.ts +125 -22
- package/mqttPacket/types.ts +40 -0
- package/mqttPacket/unsuback.test.ts +80 -30
- package/mqttPacket/unsuback.ts +65 -10
- package/mqttPacket/unsubscribe.test.ts +59 -34
- package/mqttPacket/unsubscribe.ts +61 -14
- package/node/wrapNodeSocket.ts +2 -1
- package/package.json +2 -2
- package/persistence/deps.ts +1 -1
- package/persistence/memory/memoryPersistence.test.ts +5 -1
- package/server/context.ts +23 -10
- package/server/deps.ts +2 -0
- package/server/handlers/handleConnect.ts +12 -9
- package/server/handlers/handlePacket.ts +10 -11
- package/server/handlers/handlePingreq.ts +1 -0
- package/server/handlers/handlePublish.ts +6 -2
- package/server/handlers/handlePubrec.ts +3 -1
- package/server/handlers/handlePubrel.ts +3 -1
- package/server/handlers/handleSubscribe.ts +3 -6
- package/server/handlers/handleUnsubscribe.ts +3 -1
- package/server/server.ts +8 -12
- package/server/test/handleConnect.test.ts +2 -1
- package/server/test/test-handlers.ts +2 -1
- package/socket/socket.ts +5 -1
- package/utils/assert.ts +0 -18
- package/utils/mod.ts +0 -3
- package/dist/mqttPacket/puback.d.ts +0 -16
- package/dist/mqttPacket/puback.d.ts.map +0 -1
- package/dist/mqttPacket/puback.js +0 -21
- package/dist/mqttPacket/puback.js.map +0 -1
- package/dist/mqttPacket/pubcomp.d.ts +0 -17
- package/dist/mqttPacket/pubcomp.d.ts.map +0 -1
- package/dist/mqttPacket/pubcomp.js +0 -21
- package/dist/mqttPacket/pubcomp.js.map +0 -1
- package/dist/mqttPacket/pubrec.d.ts +0 -17
- package/dist/mqttPacket/pubrec.d.ts.map +0 -1
- package/dist/mqttPacket/pubrec.js +0 -21
- package/dist/mqttPacket/pubrec.js.map +0 -1
- package/dist/mqttPacket/pubrel.d.ts +0 -17
- package/dist/mqttPacket/pubrel.d.ts.map +0 -1
- package/dist/mqttPacket/pubrel.js +0 -21
- package/dist/mqttPacket/pubrel.js.map +0 -1
- package/dist/utils/args.d.ts +0 -28
- package/dist/utils/args.d.ts.map +0 -1
- package/dist/utils/args.js +0 -76
- package/dist/utils/args.js.map +0 -1
- package/dist/utils/platform.d.ts +0 -5
- package/dist/utils/platform.d.ts.map +0 -1
- package/dist/utils/platform.js +0 -8
- package/dist/utils/platform.js.map +0 -1
- package/mqttPacket/puback.test.ts +0 -55
- package/mqttPacket/puback.ts +0 -34
- package/mqttPacket/pubcomp.test.ts +0 -55
- package/mqttPacket/pubcomp.ts +0 -35
- package/mqttPacket/pubrec.test.ts +0 -55
- package/mqttPacket/pubrec.ts +0 -35
- package/mqttPacket/pubrel.test.ts +0 -55
- package/mqttPacket/pubrel.ts +0 -35
- package/utils/args.ts +0 -103
- package/utils/platform.ts +0 -8
package/mqttPacket/mod.ts
CHANGED
|
@@ -7,39 +7,64 @@
|
|
|
7
7
|
|
|
8
8
|
import type {
|
|
9
9
|
ClientId,
|
|
10
|
+
CodecOpts,
|
|
10
11
|
Dup,
|
|
11
12
|
PacketId,
|
|
12
13
|
Payload,
|
|
14
|
+
ProtocolLevel,
|
|
13
15
|
QoS,
|
|
14
16
|
ReturnCodes,
|
|
15
17
|
TAuthenticationResult,
|
|
16
18
|
Topic,
|
|
17
19
|
TopicFilter,
|
|
18
20
|
TPacketType,
|
|
21
|
+
TReasonCode,
|
|
22
|
+
TRetainHandling,
|
|
23
|
+
UTF8StringPair,
|
|
19
24
|
} from "./types.ts";
|
|
25
|
+
|
|
26
|
+
export { MQTTLevel } from "./protocolLevels.ts";
|
|
27
|
+
export { RetainHandling } from "./RetainHandling.ts";
|
|
28
|
+
export { ReasonCode, ReasonCodeByNumber } from "./ReasonCode.ts";
|
|
20
29
|
import { PacketNameByType, PacketType } from "./PacketType.ts";
|
|
21
30
|
import { invalidTopic, invalidTopicFilter, invalidUTF8 } from "./validators.ts";
|
|
22
31
|
import { decodeLength, encodeLength } from "./length.ts";
|
|
23
|
-
import { connect
|
|
24
|
-
import { connack
|
|
32
|
+
import { connect } from "./connect.ts";
|
|
33
|
+
import { connack } from "./connack.ts";
|
|
25
34
|
import {
|
|
26
35
|
AuthenticationResult,
|
|
27
36
|
AuthenticationResultByNumber,
|
|
28
37
|
} from "./AuthenticationResult.ts";
|
|
29
|
-
import { publish
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
32
|
-
import {
|
|
33
|
-
import {
|
|
34
|
-
import {
|
|
35
|
-
import {
|
|
36
|
-
import {
|
|
37
|
-
import {
|
|
38
|
-
import {
|
|
39
|
-
import { pingres, type PingresPacket } from "./pingres.ts";
|
|
40
|
-
import { disconnect, type DisconnectPacket } from "./disconnect.ts";
|
|
38
|
+
import { publish } from "./publish.ts";
|
|
39
|
+
import { anyAck } from "./pubblishAcks.ts";
|
|
40
|
+
import { subscribe } from "./subscribe.ts";
|
|
41
|
+
import { suback } from "./suback.ts";
|
|
42
|
+
import { unsubscribe } from "./unsubscribe.ts";
|
|
43
|
+
import { unsuback } from "./unsuback.ts";
|
|
44
|
+
import { pingreq } from "./pingreq.ts";
|
|
45
|
+
import { pingres } from "./pingres.ts";
|
|
46
|
+
import { disconnect } from "./disconnect.ts";
|
|
47
|
+
import { auth } from "./auth.ts";
|
|
41
48
|
import { DecoderError } from "./decoder.ts";
|
|
42
49
|
|
|
50
|
+
import type { ConnectPacket } from "./connect.ts";
|
|
51
|
+
import type { ConnackPacket } from "./connack.ts";
|
|
52
|
+
import type { PublishPacket } from "./publish.ts";
|
|
53
|
+
import type {
|
|
54
|
+
PubackPacket,
|
|
55
|
+
PubcompPacket,
|
|
56
|
+
PubrecPacket,
|
|
57
|
+
PubrelPacket,
|
|
58
|
+
} from "./pubblishAcks.ts";
|
|
59
|
+
import type { SubscribePacket } from "./subscribe.ts";
|
|
60
|
+
import type { SubackPacket } from "./suback.ts";
|
|
61
|
+
import type { UnsubscribePacket } from "./unsubscribe.ts";
|
|
62
|
+
import type { UnsubackPacket } from "./unsuback.ts";
|
|
63
|
+
import type { PingreqPacket } from "./pingreq.ts";
|
|
64
|
+
import type { PingresPacket } from "./pingres.ts";
|
|
65
|
+
import type { DisconnectPacket } from "./disconnect.ts";
|
|
66
|
+
import type { AuthPacket } from "./auth.ts";
|
|
67
|
+
|
|
43
68
|
/**
|
|
44
69
|
* this can be any possible MQTT packet
|
|
45
70
|
*/
|
|
@@ -57,10 +82,13 @@ export type AnyPacket =
|
|
|
57
82
|
| UnsubackPacket
|
|
58
83
|
| PingreqPacket
|
|
59
84
|
| PingresPacket
|
|
60
|
-
| DisconnectPacket
|
|
85
|
+
| DisconnectPacket
|
|
86
|
+
| AuthPacket;
|
|
61
87
|
|
|
62
88
|
export type {
|
|
89
|
+
AuthPacket,
|
|
63
90
|
ClientId,
|
|
91
|
+
CodecOpts,
|
|
64
92
|
ConnackPacket,
|
|
65
93
|
ConnectPacket,
|
|
66
94
|
DisconnectPacket,
|
|
@@ -69,6 +97,7 @@ export type {
|
|
|
69
97
|
Payload,
|
|
70
98
|
PingreqPacket,
|
|
71
99
|
PingresPacket,
|
|
100
|
+
ProtocolLevel,
|
|
72
101
|
PubackPacket,
|
|
73
102
|
PubcompPacket,
|
|
74
103
|
PublishPacket,
|
|
@@ -82,8 +111,11 @@ export type {
|
|
|
82
111
|
Topic,
|
|
83
112
|
TopicFilter,
|
|
84
113
|
TPacketType,
|
|
114
|
+
TReasonCode,
|
|
115
|
+
TRetainHandling,
|
|
85
116
|
UnsubackPacket,
|
|
86
117
|
UnsubscribePacket,
|
|
118
|
+
UTF8StringPair,
|
|
87
119
|
};
|
|
88
120
|
|
|
89
121
|
export type { Subscription } from "./subscribe.ts";
|
|
@@ -109,10 +141,10 @@ export const packetsByType = [
|
|
|
109
141
|
connect, // 1
|
|
110
142
|
connack, // 2
|
|
111
143
|
publish, // 3
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
144
|
+
anyAck, // 4 puback
|
|
145
|
+
anyAck, // 5 pubrec
|
|
146
|
+
anyAck, // 6 pubrel
|
|
147
|
+
anyAck, // 7 pubcomp
|
|
116
148
|
subscribe, // 8
|
|
117
149
|
suback, // 9
|
|
118
150
|
unsubscribe, // 10
|
|
@@ -120,29 +152,29 @@ export const packetsByType = [
|
|
|
120
152
|
pingreq, // 12
|
|
121
153
|
pingres, // 13
|
|
122
154
|
disconnect, // 14
|
|
155
|
+
auth, // 15
|
|
123
156
|
] as const;
|
|
124
157
|
|
|
125
158
|
/**
|
|
126
159
|
* @function encode
|
|
127
160
|
* @description Encodes an MQTT packet object into a binary Uint8Array format
|
|
128
161
|
* @param {AnyPacket} packet - The MQTT packet object to encode
|
|
162
|
+
* @param {CodecOpts} codecOpts - options to use during encoding
|
|
129
163
|
* @returns {Uint8Array} The encoded packet as a binary buffer
|
|
130
164
|
* @throws {Error} If packet encoding fails
|
|
131
165
|
*/
|
|
132
|
-
export function encode(
|
|
166
|
+
export function encode(
|
|
167
|
+
packet: AnyPacket,
|
|
168
|
+
codecOpts: CodecOpts,
|
|
169
|
+
): Uint8Array {
|
|
133
170
|
const packetType: number = packet.type;
|
|
134
171
|
// deno-lint-ignore no-explicit-any
|
|
135
172
|
const pkt: any = packet;
|
|
136
|
-
const encoded = packetsByType[packetType]?.encode(pkt);
|
|
173
|
+
const encoded = packetsByType[packetType]?.encode(pkt, codecOpts);
|
|
137
174
|
if (!encoded) {
|
|
138
175
|
throw Error("Packet encoding failed");
|
|
139
176
|
}
|
|
140
|
-
|
|
141
|
-
return Uint8Array.from([
|
|
142
|
-
(packetType << 4) | flags,
|
|
143
|
-
...encodeLength(bytes.length),
|
|
144
|
-
...bytes,
|
|
145
|
-
]);
|
|
177
|
+
return encoded;
|
|
146
178
|
}
|
|
147
179
|
|
|
148
180
|
/**
|
|
@@ -150,38 +182,49 @@ export function encode(packet: AnyPacket): Uint8Array {
|
|
|
150
182
|
* @description Decodes a packet payload from binary format into an MQTT packet object
|
|
151
183
|
* @param {number} firstByte - The first byte of the MQTT packet containing type and flags
|
|
152
184
|
* @param {Uint8Array} buffer - The binary buffer containing the packet payload
|
|
185
|
+
* @param {CodecOpts} codecOpts - options to use during encoding
|
|
153
186
|
* @returns {AnyPacket} The decoded MQTT packet object
|
|
154
|
-
* @throws {
|
|
187
|
+
* @throws {DecoderError} If packet decoding fails
|
|
155
188
|
*/
|
|
156
189
|
|
|
157
190
|
export function decodePayload(
|
|
158
191
|
firstByte: number,
|
|
159
192
|
buffer: Uint8Array,
|
|
193
|
+
codecOpts: CodecOpts,
|
|
160
194
|
): AnyPacket {
|
|
161
195
|
const packetType = firstByte >> 4;
|
|
162
196
|
const flags = firstByte & 0x0f;
|
|
163
|
-
const packet = packetsByType[packetType]?.decode(
|
|
197
|
+
const packet = packetsByType[packetType]?.decode(
|
|
198
|
+
buffer,
|
|
199
|
+
flags,
|
|
200
|
+
codecOpts,
|
|
201
|
+
packetType as TPacketType,
|
|
202
|
+
);
|
|
164
203
|
if (packet !== undefined) {
|
|
165
204
|
return packet;
|
|
166
205
|
}
|
|
167
|
-
throw new
|
|
206
|
+
throw new DecoderError("packet decoding failed");
|
|
168
207
|
}
|
|
169
208
|
|
|
170
209
|
/**
|
|
171
210
|
* @function decode
|
|
172
211
|
* @description Decodes a complete MQTT packet from binary format into a packet object
|
|
173
212
|
* @param {Uint8Array} buffer - The binary buffer containing the complete MQTT packet
|
|
213
|
+
* @param {CodecOpts} codecOpts - the options for the decoder
|
|
174
214
|
* @returns {AnyPacket} The decoded MQTT packet object
|
|
175
215
|
* @throws {DecoderError} If packet decoding fails due to invalid format or insufficient data
|
|
176
216
|
*/
|
|
177
|
-
export function decode(
|
|
217
|
+
export function decode(
|
|
218
|
+
buffer: Uint8Array,
|
|
219
|
+
codecOpts: CodecOpts,
|
|
220
|
+
): AnyPacket {
|
|
178
221
|
if (buffer.length < 2) {
|
|
179
222
|
throw new DecoderError("Packet decoding failed");
|
|
180
223
|
}
|
|
181
224
|
const { length, numLengthBytes } = decodeLength(buffer, 1);
|
|
182
225
|
const start = numLengthBytes + 1;
|
|
183
226
|
const end = start + length;
|
|
184
|
-
return decodePayload(buffer[0], buffer.subarray(start, end));
|
|
227
|
+
return decodePayload(buffer[0], buffer.subarray(start, end), codecOpts);
|
|
185
228
|
}
|
|
186
229
|
|
|
187
230
|
export { getLengthDecoder } from "../mqttPacket/length.ts";
|
|
@@ -2,13 +2,58 @@ import { PacketType } from "./PacketType.ts";
|
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
3
|
import { test } from "node:test";
|
|
4
4
|
|
|
5
|
-
import { decode, encode } from "./mod.ts";
|
|
5
|
+
import { decode, encode, MQTTLevel } from "./mod.ts";
|
|
6
|
+
import type { CodecOpts } from "./mod.ts";
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
const codecOptsV4: CodecOpts = {
|
|
9
|
+
protocolLevel: MQTTLevel.v4,
|
|
10
|
+
maxIncomingPacketSize: 0xffff,
|
|
11
|
+
maxOutgoingPacketSize: 0xffff,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const codecOptsV5: CodecOpts = {
|
|
15
|
+
protocolLevel: MQTTLevel.v5,
|
|
16
|
+
maxIncomingPacketSize: 0xffff,
|
|
17
|
+
maxOutgoingPacketSize: 0xffff,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
test("encode Pingreq v4", () => {
|
|
21
|
+
assert.deepStrictEqual(
|
|
22
|
+
encode({
|
|
23
|
+
type: PacketType.pingreq,
|
|
24
|
+
protocolLevel: MQTTLevel.v4,
|
|
25
|
+
}, codecOptsV4),
|
|
26
|
+
Uint8Array.from([
|
|
27
|
+
// fixedHeader
|
|
28
|
+
192, // packetType + flags
|
|
29
|
+
0, // remainingLength
|
|
30
|
+
]),
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("decode Pingreq v4", () => {
|
|
35
|
+
assert.deepStrictEqual(
|
|
36
|
+
decode(
|
|
37
|
+
Uint8Array.from([
|
|
38
|
+
// fixedHeader
|
|
39
|
+
192, // packetType + flags
|
|
40
|
+
0, // remainingLength
|
|
41
|
+
]),
|
|
42
|
+
codecOptsV4,
|
|
43
|
+
),
|
|
44
|
+
{
|
|
45
|
+
type: PacketType.pingreq,
|
|
46
|
+
protocolLevel: MQTTLevel.v4,
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("encode Pingreq v5", () => {
|
|
8
52
|
assert.deepStrictEqual(
|
|
9
53
|
encode({
|
|
10
54
|
type: PacketType.pingreq,
|
|
11
|
-
|
|
55
|
+
protocolLevel: MQTTLevel.v5,
|
|
56
|
+
}, codecOptsV5),
|
|
12
57
|
Uint8Array.from([
|
|
13
58
|
// fixedHeader
|
|
14
59
|
192, // packetType + flags
|
|
@@ -17,7 +62,7 @@ test("encode Pingreq", () => {
|
|
|
17
62
|
);
|
|
18
63
|
});
|
|
19
64
|
|
|
20
|
-
test("decode Pingreq", () => {
|
|
65
|
+
test("decode Pingreq v5", () => {
|
|
21
66
|
assert.deepStrictEqual(
|
|
22
67
|
decode(
|
|
23
68
|
Uint8Array.from([
|
|
@@ -25,9 +70,11 @@ test("decode Pingreq", () => {
|
|
|
25
70
|
192, // packetType + flags
|
|
26
71
|
0, // remainingLength
|
|
27
72
|
]),
|
|
73
|
+
codecOptsV5,
|
|
28
74
|
),
|
|
29
75
|
{
|
|
30
76
|
type: PacketType.pingreq,
|
|
77
|
+
protocolLevel: MQTTLevel.v5,
|
|
31
78
|
},
|
|
32
79
|
);
|
|
33
80
|
});
|
|
@@ -43,6 +90,7 @@ test("decode invalid Pingreq", () => {
|
|
|
43
90
|
0,
|
|
44
91
|
0,
|
|
45
92
|
]),
|
|
93
|
+
codecOptsV4,
|
|
46
94
|
),
|
|
47
95
|
Error,
|
|
48
96
|
"too long",
|
package/mqttPacket/pingreq.ts
CHANGED
|
@@ -1,28 +1,41 @@
|
|
|
1
1
|
import { PacketType } from "./PacketType.ts";
|
|
2
2
|
import { hasEmptyFlags, isEmptyBuf } from "./decoder.ts";
|
|
3
|
-
import type { TPacketType } from "./types.ts";
|
|
3
|
+
import type { CodecOpts, ProtocolLevel, TPacketType } from "./types.ts";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* PingreqPacket is a packet that is sent to the server to keep the connection alive
|
|
7
7
|
*/
|
|
8
8
|
export type PingreqPacket = {
|
|
9
9
|
type: TPacketType;
|
|
10
|
+
protocolLevel: ProtocolLevel;
|
|
10
11
|
};
|
|
11
12
|
|
|
13
|
+
const PINGRES_PACKET = new Uint8Array([PacketType.pingreq << 4, 0]);
|
|
14
|
+
|
|
12
15
|
export const pingreq: {
|
|
13
|
-
encode(_packet: PingreqPacket
|
|
14
|
-
decode(
|
|
16
|
+
encode(_packet: PingreqPacket, _codecOpts: CodecOpts): Uint8Array;
|
|
17
|
+
decode(
|
|
18
|
+
buffer: Uint8Array,
|
|
19
|
+
flags: number,
|
|
20
|
+
codecOpts: CodecOpts,
|
|
21
|
+
_packetType: TPacketType,
|
|
22
|
+
): PingreqPacket;
|
|
15
23
|
} = {
|
|
16
|
-
encode(_packet: PingreqPacket
|
|
17
|
-
|
|
18
|
-
return { flags, bytes: [] };
|
|
24
|
+
encode(_packet: PingreqPacket, _codecOpts: CodecOpts): Uint8Array {
|
|
25
|
+
return PINGRES_PACKET;
|
|
19
26
|
},
|
|
20
27
|
|
|
21
|
-
decode(
|
|
28
|
+
decode(
|
|
29
|
+
buffer: Uint8Array,
|
|
30
|
+
flags: number,
|
|
31
|
+
codecOpts: CodecOpts,
|
|
32
|
+
_packetType: TPacketType,
|
|
33
|
+
): PingreqPacket {
|
|
22
34
|
hasEmptyFlags(flags);
|
|
23
35
|
isEmptyBuf(buffer);
|
|
24
36
|
return {
|
|
25
37
|
type: PacketType.pingreq,
|
|
38
|
+
protocolLevel: codecOpts.protocolLevel,
|
|
26
39
|
};
|
|
27
40
|
},
|
|
28
41
|
};
|
|
@@ -2,13 +2,58 @@ import { PacketType } from "./PacketType.ts";
|
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
3
|
import { test } from "node:test";
|
|
4
4
|
|
|
5
|
-
import { decode, encode } from "./mod.ts";
|
|
5
|
+
import { decode, encode, MQTTLevel } from "./mod.ts";
|
|
6
|
+
import type { CodecOpts } from "./mod.ts";
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
const codecOptsV4: CodecOpts = {
|
|
9
|
+
protocolLevel: MQTTLevel.v4,
|
|
10
|
+
maxIncomingPacketSize: 0xffff,
|
|
11
|
+
maxOutgoingPacketSize: 0xffff,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const codecOptsV5: CodecOpts = {
|
|
15
|
+
protocolLevel: MQTTLevel.v5,
|
|
16
|
+
maxIncomingPacketSize: 0xffff,
|
|
17
|
+
maxOutgoingPacketSize: 0xffff,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
test("encode Pingres V4", () => {
|
|
21
|
+
assert.deepStrictEqual(
|
|
22
|
+
encode({
|
|
23
|
+
type: PacketType.pingres,
|
|
24
|
+
protocolLevel: MQTTLevel.v4,
|
|
25
|
+
}, codecOptsV4),
|
|
26
|
+
Uint8Array.from([
|
|
27
|
+
// fixedHeader
|
|
28
|
+
208, // packetType + flags
|
|
29
|
+
0, // remainingLength
|
|
30
|
+
]),
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("decode Pingres V4", () => {
|
|
35
|
+
assert.deepStrictEqual(
|
|
36
|
+
decode(
|
|
37
|
+
Uint8Array.from([
|
|
38
|
+
// fixedHeader
|
|
39
|
+
208, // packetType + flags
|
|
40
|
+
0, // remainingLength
|
|
41
|
+
]),
|
|
42
|
+
codecOptsV4,
|
|
43
|
+
),
|
|
44
|
+
{
|
|
45
|
+
type: PacketType.pingres,
|
|
46
|
+
protocolLevel: MQTTLevel.v4,
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("encode Pingres V5", () => {
|
|
8
52
|
assert.deepStrictEqual(
|
|
9
53
|
encode({
|
|
10
54
|
type: PacketType.pingres,
|
|
11
|
-
|
|
55
|
+
protocolLevel: MQTTLevel.v5,
|
|
56
|
+
}, codecOptsV5),
|
|
12
57
|
Uint8Array.from([
|
|
13
58
|
// fixedHeader
|
|
14
59
|
208, // packetType + flags
|
|
@@ -17,7 +62,7 @@ test("encode Pingres", () => {
|
|
|
17
62
|
);
|
|
18
63
|
});
|
|
19
64
|
|
|
20
|
-
test("decode Pingres", () => {
|
|
65
|
+
test("decode Pingres V5", () => {
|
|
21
66
|
assert.deepStrictEqual(
|
|
22
67
|
decode(
|
|
23
68
|
Uint8Array.from([
|
|
@@ -25,9 +70,11 @@ test("decode Pingres", () => {
|
|
|
25
70
|
208, // packetType + flags
|
|
26
71
|
0, // remainingLength
|
|
27
72
|
]),
|
|
73
|
+
codecOptsV5,
|
|
28
74
|
),
|
|
29
75
|
{
|
|
30
76
|
type: PacketType.pingres,
|
|
77
|
+
protocolLevel: MQTTLevel.v5,
|
|
31
78
|
},
|
|
32
79
|
);
|
|
33
80
|
});
|
|
@@ -43,6 +90,7 @@ test("decode invalid Pingres", () => {
|
|
|
43
90
|
0,
|
|
44
91
|
0,
|
|
45
92
|
]),
|
|
93
|
+
codecOptsV4,
|
|
46
94
|
),
|
|
47
95
|
Error,
|
|
48
96
|
"too long",
|
package/mqttPacket/pingres.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PacketType } from "./PacketType.ts";
|
|
2
|
-
import { isEmptyBuf } from "./decoder.ts";
|
|
3
|
-
import type { TPacketType } from "./types.ts";
|
|
2
|
+
import { hasEmptyFlags, isEmptyBuf } from "./decoder.ts";
|
|
3
|
+
import type { CodecOpts, ProtocolLevel, TPacketType } from "./types.ts";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* PingresPacket is an empty packet that is sent by the server in response to a PingreqPacket.
|
|
@@ -8,21 +8,35 @@ import type { TPacketType } from "./types.ts";
|
|
|
8
8
|
*/
|
|
9
9
|
export type PingresPacket = {
|
|
10
10
|
type: TPacketType;
|
|
11
|
+
protocolLevel: ProtocolLevel;
|
|
11
12
|
};
|
|
12
13
|
|
|
14
|
+
const PINGRES_PACKET = new Uint8Array([PacketType.pingres << 4, 0]);
|
|
15
|
+
|
|
13
16
|
export const pingres: {
|
|
14
|
-
encode(
|
|
15
|
-
decode(
|
|
17
|
+
encode(packet: PingresPacket, _codecOpts: CodecOpts): Uint8Array;
|
|
18
|
+
decode(
|
|
19
|
+
buffer: Uint8Array,
|
|
20
|
+
flags: number,
|
|
21
|
+
codecOpts: CodecOpts,
|
|
22
|
+
_packetType: TPacketType,
|
|
23
|
+
): PingresPacket;
|
|
16
24
|
} = {
|
|
17
|
-
encode(_packet: PingresPacket
|
|
18
|
-
|
|
19
|
-
return { flags, bytes: [] };
|
|
25
|
+
encode(_packet: PingresPacket, _codecOpts: CodecOpts): Uint8Array {
|
|
26
|
+
return PINGRES_PACKET;
|
|
20
27
|
},
|
|
21
28
|
|
|
22
|
-
decode(
|
|
29
|
+
decode(
|
|
30
|
+
buffer: Uint8Array,
|
|
31
|
+
flags: number,
|
|
32
|
+
codecOpts: CodecOpts,
|
|
33
|
+
_packetType: TPacketType,
|
|
34
|
+
): PingresPacket {
|
|
35
|
+
hasEmptyFlags(flags);
|
|
23
36
|
isEmptyBuf(buffer);
|
|
24
37
|
return {
|
|
25
38
|
type: PacketType.pingres,
|
|
39
|
+
protocolLevel: codecOpts.protocolLevel,
|
|
26
40
|
};
|
|
27
41
|
},
|
|
28
42
|
};
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
|
|
4
|
+
import { Encoder } from "./encoder.ts";
|
|
5
|
+
import { Decoder } from "./decoder.ts";
|
|
6
|
+
import { PacketType } from "./PacketType.ts";
|
|
7
|
+
|
|
8
|
+
test("encoder.setProperties encodes allowed properties for connect", () => {
|
|
9
|
+
const encoder = new Encoder(PacketType.connect);
|
|
10
|
+
const props = {
|
|
11
|
+
sessionExpiryInterval: 60,
|
|
12
|
+
authenticationMethod: "basic",
|
|
13
|
+
authenticationData: new Uint8Array([1, 2, 3]),
|
|
14
|
+
requestProblemInformation: true,
|
|
15
|
+
requestResponseInformation: false,
|
|
16
|
+
receiveMaximum: 100,
|
|
17
|
+
topicAliasMaximum: 10,
|
|
18
|
+
userProperty: [["key", "value"]],
|
|
19
|
+
maximumPacketSize: 1024,
|
|
20
|
+
};
|
|
21
|
+
encoder.setProperties(props, PacketType.connect, 2048);
|
|
22
|
+
assert.equal(encoder.encodedSize(), 48);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("decoder.getProperties decodes allowed properties for connect", () => {
|
|
26
|
+
const encoder = new Encoder(PacketType.connect);
|
|
27
|
+
const props = {
|
|
28
|
+
sessionExpiryInterval: 60,
|
|
29
|
+
authenticationMethod: "basic",
|
|
30
|
+
authenticationData: new Uint8Array([1, 2, 3]),
|
|
31
|
+
requestProblemInformation: true,
|
|
32
|
+
requestResponseInformation: false,
|
|
33
|
+
receiveMaximum: 100,
|
|
34
|
+
topicAliasMaximum: 10,
|
|
35
|
+
userProperty: [["key1", "value1"], ["key2", "value2"]],
|
|
36
|
+
maximumPacketSize: 1024,
|
|
37
|
+
};
|
|
38
|
+
encoder.setProperties(props, PacketType.connect, 2048);
|
|
39
|
+
const buf = encoder.done(0);
|
|
40
|
+
// skip the first byte which contains the packet identifier
|
|
41
|
+
// and the second byte that holds the packet length
|
|
42
|
+
const decoder = new Decoder(PacketType.connect, buf, 2);
|
|
43
|
+
const decoded = decoder.getProperties(PacketType.connect);
|
|
44
|
+
assert.equal(decoded.sessionExpiryInterval, 60);
|
|
45
|
+
assert.equal(decoded.authenticationMethod, "basic");
|
|
46
|
+
assert.deepEqual(decoded.authenticationData, new Uint8Array([1, 2, 3]));
|
|
47
|
+
assert.equal(decoded.requestProblemInformation, true);
|
|
48
|
+
assert.equal(decoded.requestResponseInformation, false);
|
|
49
|
+
assert.equal(decoded.receiveMaximum, 100);
|
|
50
|
+
assert.equal(decoded.topicAliasMaximum, 10);
|
|
51
|
+
assert.deepEqual(decoded.userProperty, [["key1", "value1"], [
|
|
52
|
+
"key2",
|
|
53
|
+
"value2",
|
|
54
|
+
]]);
|
|
55
|
+
assert.equal(decoded.maximumPacketSize, 1024);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("decoder.getProperties decodes allowed properties for connack", () => {
|
|
59
|
+
const encoder = new Encoder(PacketType.connack);
|
|
60
|
+
const props = {
|
|
61
|
+
maximumQos: 2,
|
|
62
|
+
reasonString: "very good reason",
|
|
63
|
+
};
|
|
64
|
+
encoder.setProperties(props, PacketType.connack, 2048);
|
|
65
|
+
const buf = encoder.done(0);
|
|
66
|
+
// skip the first byte which contains the packet identifier
|
|
67
|
+
// and the second byte that holds the packet length
|
|
68
|
+
const decoder = new Decoder(PacketType.connack, buf, 2);
|
|
69
|
+
const decoded = decoder.getProperties(PacketType.connack);
|
|
70
|
+
assert.equal(decoded.maximumQos, 2);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("encoder.setProperties does not encode properties not allowed for packet type", () => {
|
|
74
|
+
const encoder = new Encoder(PacketType.publish);
|
|
75
|
+
const props = {
|
|
76
|
+
userProperty: [["key", "value"]],
|
|
77
|
+
// Not allowed for publish
|
|
78
|
+
sessionExpiryInterval: 60,
|
|
79
|
+
authenticationMethod: "basic",
|
|
80
|
+
authenticationData: new Uint8Array([1, 2, 3]),
|
|
81
|
+
requestProblemInformation: true,
|
|
82
|
+
requestResponseInformation: false,
|
|
83
|
+
receiveMaximum: 100,
|
|
84
|
+
topicAliasMaximum: 10,
|
|
85
|
+
maximumPacketSize: 1024,
|
|
86
|
+
};
|
|
87
|
+
encoder.setProperties(props, PacketType.publish, 2048);
|
|
88
|
+
const buf = encoder.done(0);
|
|
89
|
+
const decoder = new Decoder(PacketType.publish, buf, 2);
|
|
90
|
+
const decoded = decoder.getProperties(PacketType.publish);
|
|
91
|
+
assert.deepStrictEqual(Object.keys(decoded), [
|
|
92
|
+
"userProperty",
|
|
93
|
+
]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("decoder.getProperties throws on duplicate property", () => {
|
|
97
|
+
const encoder = new Encoder(PacketType.publish);
|
|
98
|
+
// Manually encode duplicate properties
|
|
99
|
+
encoder.setVariableByteInteger(0x04); // propertyLength
|
|
100
|
+
encoder.setVariableByteInteger(0x01); // payloadFormatIndicator
|
|
101
|
+
encoder.setByte(1);
|
|
102
|
+
encoder.setVariableByteInteger(0x01); // payloadFormatIndicator again
|
|
103
|
+
encoder.setByte(1);
|
|
104
|
+
const buf = encoder.done(0);
|
|
105
|
+
const decoder = new Decoder(PacketType.publish, buf, 2);
|
|
106
|
+
assert.throws(
|
|
107
|
+
() => decoder.getProperties(PacketType.publish),
|
|
108
|
+
/Property payloadFormatIndicator only allowed once/,
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("decoder.getProperties throws on property not allowed for packet type", () => {
|
|
113
|
+
const encoder = new Encoder(PacketType.publish);
|
|
114
|
+
encoder.setVariableByteInteger(0x05); // propertyLength
|
|
115
|
+
encoder.setVariableByteInteger(0x13); // serverKeepAlive (not allowed for publish)
|
|
116
|
+
encoder.setInt32(30);
|
|
117
|
+
const buf = encoder.done(0);
|
|
118
|
+
const decoder = new Decoder(PacketType.publish, buf, 2);
|
|
119
|
+
assert.throws(
|
|
120
|
+
() => decoder.getProperties(PacketType.publish),
|
|
121
|
+
/Property type serverKeepAlive not allowed/,
|
|
122
|
+
);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("encoder.setProperties rewinds marker if encodedSize exceeds maximumPacketSize", () => {
|
|
126
|
+
const encoder = new Encoder(PacketType.puback);
|
|
127
|
+
const props = {
|
|
128
|
+
reasonString: "small",
|
|
129
|
+
userProperty: [["key", "value"]],
|
|
130
|
+
};
|
|
131
|
+
encoder.setProperties(props, PacketType.puback, 10); // very small max size
|
|
132
|
+
assert.equal(encoder.encodedSize(), 9);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("encoder.setProperties removes all properties if encodedSize exceeds maximumPacketSize", () => {
|
|
136
|
+
const encoder = new Encoder(PacketType.puback);
|
|
137
|
+
const props = {
|
|
138
|
+
reasonString: "a very long reason",
|
|
139
|
+
userProperty: [["key", "value"]],
|
|
140
|
+
};
|
|
141
|
+
encoder.setProperties(props, PacketType.puback, 10); // very small max size
|
|
142
|
+
assert.equal(encoder.encodedSize(), 1); // only propertyLength 0 remains
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("decodeProperty throws on invalid property kind", () => {
|
|
146
|
+
const encoder = new Encoder(PacketType.publish);
|
|
147
|
+
// Manually encode an invalid property kind
|
|
148
|
+
encoder.setVariableByteInteger(0x02); // propertyLength
|
|
149
|
+
encoder.setVariableByteInteger(0xF0); // Invalid property ID
|
|
150
|
+
encoder.setByte(1);
|
|
151
|
+
|
|
152
|
+
const buf = encoder.done(0);
|
|
153
|
+
const decoder = new Decoder(PacketType.publish, buf, 2);
|
|
154
|
+
|
|
155
|
+
assert.throws(
|
|
156
|
+
() => decoder.getProperties(PacketType.publish),
|
|
157
|
+
/Property type 240 not allowed/,
|
|
158
|
+
);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test("userProperty must be an array", () => {
|
|
162
|
+
const encoder = new Encoder(PacketType.puback);
|
|
163
|
+
const props = {
|
|
164
|
+
userProperty: "I am not an array",
|
|
165
|
+
};
|
|
166
|
+
assert.throws(
|
|
167
|
+
() => encoder.setProperties(props, PacketType.puback, 10),
|
|
168
|
+
/ userProperty must be an array/,
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("userProperty item must be an array", () => {
|
|
173
|
+
const encoder = new Encoder(PacketType.puback);
|
|
174
|
+
const props = {
|
|
175
|
+
userProperty: ["a", "b"],
|
|
176
|
+
};
|
|
177
|
+
assert.throws(
|
|
178
|
+
() => encoder.setProperties(props, PacketType.puback, 10),
|
|
179
|
+
/ userProperty item must be an array/,
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test("encode/decode subscriptionIdentifiers", () => {
|
|
184
|
+
const props = { subscriptionIdentifiers: [3, 5, 7] };
|
|
185
|
+
const bytes = new Uint8Array([48, 7, 6, 0xb, 3, 0xb, 5, 0xb, 7]);
|
|
186
|
+
const encoder = new Encoder(PacketType.publish);
|
|
187
|
+
encoder.setProperties(props, PacketType.publish, 1000);
|
|
188
|
+
const buf = encoder.done(0);
|
|
189
|
+
assert.deepStrictEqual(buf, bytes);
|
|
190
|
+
const decoder = new Decoder(PacketType.publish, buf, 2);
|
|
191
|
+
const decoded = decoder.getProperties(PacketType.publish);
|
|
192
|
+
assert.deepStrictEqual(decoded, props);
|
|
193
|
+
});
|