@seriousme/opifex 2.0.3 → 2.2.0
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/client/ConnectionState.ts +1 -1
- package/client/client.ts +41 -9
- package/client/context.ts +11 -13
- package/client/deps.ts +8 -5
- package/client/handlers/handleConnack.ts +2 -2
- package/client/mod.ts +13 -2
- package/client/store/memoryStore.ts +11 -10
- package/client/store/store.ts +1 -1
- package/deno/mod.ts +7 -0
- package/deno/tcpClient.ts +21 -8
- package/deno/tcpServer.ts +21 -4
- package/deno/tlsServer.ts +20 -4
- package/deno/wsServer.ts +23 -0
- package/dist/client/ConnectionState.d.ts +1 -1
- package/dist/client/ConnectionState.d.ts.map +1 -1
- package/dist/client/client.d.ts +39 -9
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +24 -0
- package/dist/client/client.js.map +1 -1
- package/dist/client/context.d.ts +8 -8
- package/dist/client/context.d.ts.map +1 -1
- package/dist/client/context.js.map +1 -1
- package/dist/client/deps.d.ts +3 -3
- package/dist/client/deps.d.ts.map +1 -1
- package/dist/client/deps.js.map +1 -1
- package/dist/client/handlers/handleConnack.d.ts.map +1 -1
- package/dist/client/handlers/handleConnack.js.map +1 -1
- package/dist/client/mod.d.ts +3 -2
- package/dist/client/mod.d.ts.map +1 -1
- package/dist/client/mod.js +1 -1
- package/dist/client/mod.js.map +1 -1
- package/dist/client/store/memoryStore.d.ts +3 -3
- package/dist/client/store/memoryStore.d.ts.map +1 -1
- package/dist/client/store/memoryStore.js +2 -2
- package/dist/client/store/memoryStore.js.map +1 -1
- package/dist/client/store/store.d.ts +1 -1
- package/dist/client/store/store.d.ts.map +1 -1
- package/dist/examples/authHandler.test.js +3 -3
- package/dist/examples/authHandler.test.js.map +1 -1
- package/dist/mqttConn/mqttConn.d.ts +2 -17
- package/dist/mqttConn/mqttConn.d.ts.map +1 -1
- package/dist/mqttConn/mqttConn.js +1 -1
- package/dist/mqttConn/mqttConn.js.map +1 -1
- package/dist/mqttPacket/AuthenticationResult.d.ts +4 -0
- package/dist/mqttPacket/AuthenticationResult.d.ts.map +1 -1
- package/dist/mqttPacket/AuthenticationResult.js.map +1 -1
- package/dist/mqttPacket/PacketType.d.ts +4 -0
- package/dist/mqttPacket/PacketType.d.ts.map +1 -1
- package/dist/mqttPacket/Properties.d.ts +2 -2
- package/dist/mqttPacket/Properties.d.ts.map +1 -1
- package/dist/mqttPacket/ReasonCode.d.ts +3 -5
- package/dist/mqttPacket/ReasonCode.d.ts.map +1 -1
- package/dist/mqttPacket/ReasonCode.js.map +1 -1
- package/dist/mqttPacket/RetainHandling.d.ts +4 -0
- package/dist/mqttPacket/RetainHandling.d.ts.map +1 -1
- package/dist/mqttPacket/auth.d.ts +6 -5
- package/dist/mqttPacket/auth.d.ts.map +1 -1
- package/dist/mqttPacket/auth.js +2 -2
- package/dist/mqttPacket/auth.js.map +1 -1
- package/dist/mqttPacket/codec.d.ts.map +1 -1
- package/dist/mqttPacket/codec.js.map +1 -1
- package/dist/mqttPacket/connack.d.ts +9 -7
- package/dist/mqttPacket/connack.d.ts.map +1 -1
- package/dist/mqttPacket/connack.js +2 -2
- package/dist/mqttPacket/connack.js.map +1 -1
- package/dist/mqttPacket/connect.d.ts +5 -4
- package/dist/mqttPacket/connect.d.ts.map +1 -1
- package/dist/mqttPacket/connect.js.map +1 -1
- package/dist/mqttPacket/decoder.d.ts +6 -4
- package/dist/mqttPacket/decoder.d.ts.map +1 -1
- package/dist/mqttPacket/decoder.js +1 -1
- package/dist/mqttPacket/decoder.js.map +1 -1
- package/dist/mqttPacket/disconnect.d.ts +7 -5
- package/dist/mqttPacket/disconnect.d.ts.map +1 -1
- package/dist/mqttPacket/disconnect.js +2 -2
- package/dist/mqttPacket/disconnect.js.map +1 -1
- package/dist/mqttPacket/encoder.d.ts +7 -5
- package/dist/mqttPacket/encoder.d.ts.map +1 -1
- package/dist/mqttPacket/encoder.js +1 -1
- package/dist/mqttPacket/encoder.js.map +1 -1
- package/dist/mqttPacket/mod.d.ts +1 -1
- package/dist/mqttPacket/mod.d.ts.map +1 -1
- package/dist/mqttPacket/pingreq.d.ts +4 -3
- package/dist/mqttPacket/pingreq.d.ts.map +1 -1
- package/dist/mqttPacket/pingreq.js.map +1 -1
- package/dist/mqttPacket/pingres.d.ts +4 -3
- package/dist/mqttPacket/pingres.d.ts.map +1 -1
- package/dist/mqttPacket/pingres.js.map +1 -1
- package/dist/mqttPacket/protocolLevels.d.ts +2 -0
- package/dist/mqttPacket/protocolLevels.d.ts.map +1 -1
- package/dist/mqttPacket/pubblishAcks.d.ts +7 -7
- package/dist/mqttPacket/pubblishAcks.d.ts.map +1 -1
- package/dist/mqttPacket/pubblishAcks.js +2 -2
- package/dist/mqttPacket/pubblishAcks.js.map +1 -1
- package/dist/mqttPacket/publish.d.ts +5 -4
- package/dist/mqttPacket/publish.d.ts.map +1 -1
- package/dist/mqttPacket/publish.js.map +1 -1
- package/dist/mqttPacket/suback.d.ts +7 -5
- package/dist/mqttPacket/suback.d.ts.map +1 -1
- package/dist/mqttPacket/suback.js +2 -2
- package/dist/mqttPacket/suback.js.map +1 -1
- package/dist/mqttPacket/subscribe.d.ts +7 -5
- package/dist/mqttPacket/subscribe.d.ts.map +1 -1
- package/dist/mqttPacket/subscribe.js.map +1 -1
- package/dist/mqttPacket/types.d.ts +1 -21
- package/dist/mqttPacket/types.d.ts.map +1 -1
- package/dist/mqttPacket/unsuback.d.ts +7 -5
- package/dist/mqttPacket/unsuback.d.ts.map +1 -1
- package/dist/mqttPacket/unsuback.js +1 -1
- package/dist/mqttPacket/unsuback.js.map +1 -1
- package/dist/mqttPacket/unsubscribe.d.ts +5 -4
- package/dist/mqttPacket/unsubscribe.d.ts.map +1 -1
- package/dist/mqttPacket/unsubscribe.js.map +1 -1
- package/dist/node/tcpClient.d.ts +21 -1
- package/dist/node/tcpClient.d.ts.map +1 -1
- package/dist/node/tcpClient.js +18 -4
- package/dist/node/tcpClient.js.map +1 -1
- package/dist/node/tcpServer.d.ts +18 -0
- package/dist/node/tcpServer.d.ts.map +1 -1
- package/dist/node/tcpServer.js +17 -2
- package/dist/node/tcpServer.js.map +1 -1
- package/dist/node/tlsServer.d.ts +18 -0
- package/dist/node/tlsServer.d.ts.map +1 -1
- package/dist/node/tlsServer.js +17 -2
- package/dist/node/tlsServer.js.map +1 -1
- package/dist/persistence/basicPersistence.d.ts +4 -4
- package/dist/persistence/basicPersistence.d.ts.map +1 -1
- package/dist/persistence/basicPersistence.js.map +1 -1
- package/dist/persistence/deps.d.ts +1 -1
- package/dist/persistence/deps.d.ts.map +1 -1
- package/dist/persistence/memory/memoryStorage.d.ts +3 -3
- package/dist/persistence/memory/memoryStorage.d.ts.map +1 -1
- package/dist/persistence/mod.d.ts +1 -1
- package/dist/persistence/mod.d.ts.map +1 -1
- package/dist/persistence/persistence.d.ts +3 -3
- package/dist/persistence/persistence.d.ts.map +1 -1
- package/dist/persistence/sqlite/sqliteStorage.d.ts +2 -2
- package/dist/persistence/sqlite/sqliteStorage.d.ts.map +1 -1
- package/dist/persistence/storage.d.ts +1 -1
- package/dist/persistence/storage.d.ts.map +1 -1
- package/dist/server/context.d.ts +4 -4
- package/dist/server/context.d.ts.map +1 -1
- 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.map +1 -1
- package/dist/server/handlers/completeConnect.d.ts +3 -2
- package/dist/server/handlers/completeConnect.d.ts.map +1 -1
- package/dist/server/handlers/completeConnect.js.map +1 -1
- package/dist/server/handlers/handlePublish.d.ts.map +1 -1
- package/dist/server/handlers/handlePublish.js.map +1 -1
- package/dist/server/handlers/handleSubscribe.d.ts.map +1 -1
- package/dist/server/handlers/handleSubscribe.js.map +1 -1
- package/dist/server/handlers/handleUnsubscribe.d.ts.map +1 -1
- package/dist/server/handlers/handleUnsubscribe.js.map +1 -1
- package/dist/server/mod.d.ts +1 -1
- package/dist/server/mod.d.ts.map +1 -1
- package/dist/server/server.d.ts +3 -3
- package/dist/server/server.d.ts.map +1 -1
- package/dist/server/server.js.map +1 -1
- package/dist/socket/socket.d.ts +22 -0
- package/dist/socket/socket.d.ts.map +1 -1
- package/dist/socket/socket.js +5 -0
- package/dist/socket/socket.js.map +1 -1
- package/dist/utils/logger.d.ts +3 -3
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +1 -1
- package/dist/utils/logger.js.map +1 -1
- package/dist/web/wsClient.d.ts +12 -0
- package/dist/web/wsClient.d.ts.map +1 -1
- package/dist/web/wsClient.js +12 -1
- package/dist/web/wsClient.js.map +1 -1
- package/examples/authHandler.test.ts +3 -3
- package/mqttConn/mqttConn.ts +2 -18
- package/mqttPacket/AuthenticationResult.ts +6 -0
- package/mqttPacket/PacketType.ts +5 -0
- package/mqttPacket/Properties.ts +2 -2
- package/mqttPacket/ReasonCode.ts +6 -8
- package/mqttPacket/RetainHandling.ts +5 -0
- package/mqttPacket/auth.ts +7 -7
- package/mqttPacket/codec.ts +3 -2
- package/mqttPacket/connack.ts +11 -10
- package/mqttPacket/connect.ts +4 -11
- package/mqttPacket/decoder.ts +9 -11
- package/mqttPacket/disconnect.ts +7 -7
- package/mqttPacket/encoder.test.ts +2 -2
- package/mqttPacket/encoder.ts +9 -7
- package/mqttPacket/mod.ts +0 -4
- package/mqttPacket/pingreq.ts +3 -3
- package/mqttPacket/pingres.ts +3 -3
- package/mqttPacket/protocolLevels.ts +2 -0
- package/mqttPacket/pubblishAcks.ts +9 -9
- package/mqttPacket/publish.ts +4 -11
- package/mqttPacket/publishAcks.test.ts +3 -3
- package/mqttPacket/suback.ts +8 -13
- package/mqttPacket/subscribe.ts +6 -12
- package/mqttPacket/types.ts +1 -26
- package/mqttPacket/unsuback.ts +8 -7
- package/mqttPacket/unsubscribe.ts +4 -10
- package/node/mod.ts +7 -0
- package/node/tcpClient.ts +21 -6
- package/node/tcpServer.ts +23 -6
- package/node/tlsServer.ts +23 -7
- package/package.json +6 -1
- package/persistence/basicPersistence.ts +5 -5
- package/persistence/deps.ts +1 -1
- package/persistence/memory/memoryStorage.ts +3 -3
- package/persistence/mod.ts +1 -1
- package/persistence/persistence.testing.ts +4 -4
- package/persistence/persistence.ts +3 -3
- package/persistence/sqlite/sqliteStorage.ts +2 -2
- package/persistence/storage.ts +1 -1
- package/server/context.ts +5 -5
- package/server/deps.ts +1 -2
- package/server/handlers/completeConnect.ts +2 -2
- package/server/handlers/handlePublish.ts +3 -4
- package/server/handlers/handleSubscribe.ts +3 -4
- package/server/handlers/handleUnsubscribe.ts +2 -2
- package/server/mod.ts +1 -1
- package/server/server.ts +3 -3
- package/socket/socket.ts +31 -2
- package/tools/benchmarks/receiver.ts +2 -2
- package/tools/benchmarks/sender.ts +2 -2
- package/utils/logger.ts +3 -3
- package/web/wsClient.ts +13 -1
package/mqttPacket/ReasonCode.ts
CHANGED
|
@@ -227,14 +227,12 @@ const reasonCodesToString = {
|
|
|
227
227
|
[0xa2]: "Wildcard Subscriptions not supported",
|
|
228
228
|
} as const;
|
|
229
229
|
|
|
230
|
-
|
|
231
|
-
type
|
|
232
|
-
export type TReasonCode = typeof ReasonCode[keyof typeof ReasonCode];
|
|
233
|
-
type TReasonCodeByPacket = keyof typeof ReasonCodebyPacket;
|
|
230
|
+
export type ReasonCode = typeof ReasonCode[keyof typeof ReasonCode];
|
|
231
|
+
type ReasonCodeByPacket = keyof typeof ReasonCodebyPacket;
|
|
234
232
|
|
|
235
233
|
export function reasonCodeToString(
|
|
236
|
-
packetType:
|
|
237
|
-
code:
|
|
234
|
+
packetType: PacketType,
|
|
235
|
+
code: ReasonCode,
|
|
238
236
|
): string {
|
|
239
237
|
if (code === 0) {
|
|
240
238
|
if (packetType === PacketType.disconnect) {
|
|
@@ -248,10 +246,10 @@ export function reasonCodeToString(
|
|
|
248
246
|
}
|
|
249
247
|
|
|
250
248
|
export function isValidReasonCode(
|
|
251
|
-
packetType:
|
|
249
|
+
packetType: PacketType,
|
|
252
250
|
code: number,
|
|
253
251
|
): boolean {
|
|
254
252
|
const validReasonCodes =
|
|
255
|
-
ReasonCodebyPacket[packetType as
|
|
253
|
+
ReasonCodebyPacket[packetType as ReasonCodeByPacket] as readonly number[];
|
|
256
254
|
return validReasonCodes.includes(code);
|
|
257
255
|
}
|
package/mqttPacket/auth.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { PacketType } from "./PacketType.ts";
|
|
2
2
|
import { Encoder, EncoderError } from "./encoder.ts";
|
|
3
3
|
import { Decoder, DecoderError, hasEmptyFlags } from "./decoder.ts";
|
|
4
|
-
import type {
|
|
4
|
+
import type { ReasonCode } from "./ReasonCode.ts";
|
|
5
5
|
import type { AuthProperties } from "./Properties.ts";
|
|
6
|
-
import type { CodecOpts
|
|
6
|
+
import type { CodecOpts } from "./types.ts";
|
|
7
7
|
import { MQTTLevel } from "./protocolLevels.ts";
|
|
8
8
|
|
|
9
9
|
// AuthPacket does not exist on protocol levels < 5
|
|
10
10
|
export type AuthPacketV5 = {
|
|
11
|
-
type:
|
|
11
|
+
type: PacketType;
|
|
12
12
|
protocolLevel: 5;
|
|
13
|
-
reasonCode:
|
|
13
|
+
reasonCode: ReasonCode;
|
|
14
14
|
properties?: AuthProperties;
|
|
15
15
|
};
|
|
16
16
|
|
|
@@ -30,7 +30,7 @@ export function encode(packet: AuthPacket, codecOpts: CodecOpts): Uint8Array {
|
|
|
30
30
|
// deno-coverage-ignore-stop
|
|
31
31
|
const encoder = new Encoder(packet.type);
|
|
32
32
|
encoder
|
|
33
|
-
.
|
|
33
|
+
.seReasonCode(packet.reasonCode || 0)
|
|
34
34
|
.setProperties(
|
|
35
35
|
packet.properties || {},
|
|
36
36
|
packet.type,
|
|
@@ -43,7 +43,7 @@ export function decode(
|
|
|
43
43
|
buffer: Uint8Array,
|
|
44
44
|
flags: number,
|
|
45
45
|
codecOpts: CodecOpts,
|
|
46
|
-
packetType:
|
|
46
|
+
packetType: PacketType,
|
|
47
47
|
): AuthPacket {
|
|
48
48
|
// Bits 3,2,1 and 0 of the Fixed Header of the AUTH packet are reserved and MUST all be set to 0.
|
|
49
49
|
// The Client or Server MUST treat any other value as malformed and close the Network Connection [MQTT-3.15.1-1].
|
|
@@ -52,7 +52,7 @@ export function decode(
|
|
|
52
52
|
throw new DecoderError("Invalid protocol level");
|
|
53
53
|
}
|
|
54
54
|
const decoder = new Decoder(packetType, buffer);
|
|
55
|
-
const reasonCode = decoder.
|
|
55
|
+
const reasonCode = decoder.geReasonCode();
|
|
56
56
|
const properties = decoder.getProperties(PacketType.auth);
|
|
57
57
|
decoder.done();
|
|
58
58
|
return {
|
package/mqttPacket/codec.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { CodecOpts
|
|
1
|
+
import type { CodecOpts } from "./types.ts";
|
|
2
|
+
import type { PacketType } from "./PacketType.ts";
|
|
2
3
|
import { DecoderError } from "./decoder.ts";
|
|
3
4
|
import { decodeLength } from "./length.ts";
|
|
4
5
|
|
|
@@ -117,7 +118,7 @@ export function decodePayload(
|
|
|
117
118
|
buffer,
|
|
118
119
|
flags,
|
|
119
120
|
codecOpts,
|
|
120
|
-
packetType as
|
|
121
|
+
packetType as PacketType,
|
|
121
122
|
);
|
|
122
123
|
if (packet !== undefined) {
|
|
123
124
|
return packet;
|
package/mqttPacket/connack.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CodecOpts
|
|
1
|
+
import type { CodecOpts } from "./types.ts";
|
|
2
2
|
import type { ProtocolLevelNoV5 } from "./protocolLevels.ts";
|
|
3
3
|
import { PacketType } from "./PacketType.ts";
|
|
4
4
|
import { BitMask } from "./BitMask.ts";
|
|
@@ -10,21 +10,22 @@ import {
|
|
|
10
10
|
hasEmptyFlags,
|
|
11
11
|
} from "./decoder.ts";
|
|
12
12
|
import { AuthenticationResultByNumber } from "./AuthenticationResult.ts";
|
|
13
|
-
import type {
|
|
13
|
+
import type { AuthenticationResult } from "./AuthenticationResult.ts";
|
|
14
|
+
import type { ReasonCode } from "./ReasonCode.ts";
|
|
14
15
|
import type { ConnackProperties } from "./Properties.ts";
|
|
15
16
|
|
|
16
17
|
export type ConnackPacketV4 = {
|
|
17
|
-
type:
|
|
18
|
+
type: PacketType;
|
|
18
19
|
protocolLevel: ProtocolLevelNoV5;
|
|
19
20
|
sessionPresent: boolean;
|
|
20
|
-
returnCode:
|
|
21
|
+
returnCode: AuthenticationResult;
|
|
21
22
|
};
|
|
22
23
|
|
|
23
24
|
export type ConnackPacketV5 = {
|
|
24
|
-
type:
|
|
25
|
+
type: PacketType;
|
|
25
26
|
protocolLevel: 5;
|
|
26
27
|
sessionPresent: boolean;
|
|
27
|
-
reasonCode:
|
|
28
|
+
reasonCode: ReasonCode;
|
|
28
29
|
properties?: ConnackProperties;
|
|
29
30
|
};
|
|
30
31
|
|
|
@@ -49,7 +50,7 @@ export function encode(
|
|
|
49
50
|
const encoder = new Encoder(packet.type);
|
|
50
51
|
encoder
|
|
51
52
|
.setByte(packet.sessionPresent ? 1 : 0)
|
|
52
|
-
.
|
|
53
|
+
.seReasonCode(packet.reasonCode || 0)
|
|
53
54
|
.setProperties(
|
|
54
55
|
packet.properties || {},
|
|
55
56
|
packet.type,
|
|
@@ -62,7 +63,7 @@ export function decode(
|
|
|
62
63
|
buffer: Uint8Array,
|
|
63
64
|
flags: number,
|
|
64
65
|
codecOpts: CodecOpts,
|
|
65
|
-
packetType:
|
|
66
|
+
packetType: PacketType,
|
|
66
67
|
): ConnackPacket {
|
|
67
68
|
const decoder = new Decoder(packetType, buffer);
|
|
68
69
|
hasEmptyFlags(flags);
|
|
@@ -72,7 +73,7 @@ export function decode(
|
|
|
72
73
|
}
|
|
73
74
|
const sessionPresent = booleanFlag(ackflags, BitMask.bit0);
|
|
74
75
|
if (codecOpts.protocolLevel !== 5) {
|
|
75
|
-
const returnCode = decoder.getByte() as
|
|
76
|
+
const returnCode = decoder.getByte() as AuthenticationResult;
|
|
76
77
|
decoder.done();
|
|
77
78
|
if (!AuthenticationResultByNumber[returnCode]) {
|
|
78
79
|
throw new DecoderError("Invalid return code");
|
|
@@ -84,7 +85,7 @@ export function decode(
|
|
|
84
85
|
returnCode,
|
|
85
86
|
};
|
|
86
87
|
}
|
|
87
|
-
const reasonCode = decoder.
|
|
88
|
+
const reasonCode = decoder.geReasonCode();
|
|
88
89
|
const properties = decoder.getProperties(PacketType.connack);
|
|
89
90
|
decoder.done();
|
|
90
91
|
return {
|
package/mqttPacket/connect.ts
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ClientId,
|
|
3
|
-
CodecOpts,
|
|
4
|
-
Payload,
|
|
5
|
-
QoS,
|
|
6
|
-
Topic,
|
|
7
|
-
TPacketType,
|
|
8
|
-
} from "./types.ts";
|
|
1
|
+
import type { ClientId, CodecOpts, Payload, QoS, Topic } from "./types.ts";
|
|
9
2
|
import type { ProtocolLevel, ProtocolLevelNoV5 } from "./protocolLevels.ts";
|
|
10
3
|
import type { ConnectProperties, WillProperties } from "./Properties.ts";
|
|
11
4
|
import { PropertySetType } from "./Properties.ts";
|
|
@@ -20,7 +13,7 @@ import {
|
|
|
20
13
|
} from "./decoder.ts";
|
|
21
14
|
|
|
22
15
|
export type ConnectPacketV4 = {
|
|
23
|
-
type:
|
|
16
|
+
type: PacketType;
|
|
24
17
|
protocolName?: string | undefined;
|
|
25
18
|
protocolLevel: ProtocolLevelNoV5;
|
|
26
19
|
clientId?: ClientId | undefined;
|
|
@@ -38,7 +31,7 @@ export type ConnectPacketV4 = {
|
|
|
38
31
|
};
|
|
39
32
|
|
|
40
33
|
export type ConnectPacketV5 = {
|
|
41
|
-
type:
|
|
34
|
+
type: PacketType;
|
|
42
35
|
protocolName?: string | undefined;
|
|
43
36
|
protocolLevel: 5;
|
|
44
37
|
clientId?: ClientId | undefined;
|
|
@@ -167,7 +160,7 @@ export function decode(
|
|
|
167
160
|
buffer: Uint8Array,
|
|
168
161
|
flags: number,
|
|
169
162
|
_codecOpts: CodecOpts,
|
|
170
|
-
packetType:
|
|
163
|
+
packetType: PacketType,
|
|
171
164
|
): ConnectPacket {
|
|
172
165
|
const decoder = new Decoder(packetType, buffer);
|
|
173
166
|
const protocolName = decoder.getUTF8String();
|
package/mqttPacket/decoder.ts
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
TBitMask,
|
|
3
|
-
TPacketType,
|
|
4
|
-
TReasonCode,
|
|
5
|
-
UTF8StringPair,
|
|
6
|
-
} from "./types.ts";
|
|
1
|
+
import type { BitMask, UTF8StringPair } from "./types.ts";
|
|
7
2
|
import { invalidUTF8 } from "./validators.ts";
|
|
8
3
|
import { isValidReasonCode } from "./ReasonCode.ts";
|
|
9
4
|
import type {
|
|
@@ -21,6 +16,9 @@ import {
|
|
|
21
16
|
propertyToKind,
|
|
22
17
|
} from "./Properties.ts";
|
|
23
18
|
|
|
19
|
+
import type { PacketType } from "./PacketType.ts";
|
|
20
|
+
import type { ReasonCode } from "./ReasonCode.ts";
|
|
21
|
+
|
|
24
22
|
const utf8Decoder = new TextDecoder("utf-8");
|
|
25
23
|
const userPropID = propertyToId.userProperty;
|
|
26
24
|
const subIdentID = propertyToId.subscriptionIdentifier;
|
|
@@ -31,7 +29,7 @@ const subIdentsID = propertyToId.subscriptionIdentifiers;
|
|
|
31
29
|
* @param mask - The bitmask to apply
|
|
32
30
|
* @returns True if the flag is set, false otherwise
|
|
33
31
|
*/
|
|
34
|
-
export function booleanFlag(byte: number, mask:
|
|
32
|
+
export function booleanFlag(byte: number, mask: BitMask): boolean {
|
|
35
33
|
return !!(byte & mask);
|
|
36
34
|
}
|
|
37
35
|
|
|
@@ -71,7 +69,7 @@ export class DecoderError extends Error {
|
|
|
71
69
|
* Decoder class for parsing MQTT packets
|
|
72
70
|
*/
|
|
73
71
|
export class Decoder {
|
|
74
|
-
private packetType:
|
|
72
|
+
private packetType: PacketType;
|
|
75
73
|
private buf: Uint8Array;
|
|
76
74
|
private pos: number;
|
|
77
75
|
private len: number;
|
|
@@ -81,7 +79,7 @@ export class Decoder {
|
|
|
81
79
|
* @param buf - The buffer to decode
|
|
82
80
|
* @param pos - Starting position in the buffer (default: 0)
|
|
83
81
|
*/
|
|
84
|
-
constructor(packetType:
|
|
82
|
+
constructor(packetType: PacketType, buf: Uint8Array, pos: number = 0) {
|
|
85
83
|
this.packetType = packetType;
|
|
86
84
|
this.buf = buf;
|
|
87
85
|
this.pos = pos;
|
|
@@ -289,12 +287,12 @@ export class Decoder {
|
|
|
289
287
|
* @returns The decoded reason code
|
|
290
288
|
* @throws {DecoderError} If reason code is invalid
|
|
291
289
|
*/
|
|
292
|
-
|
|
290
|
+
geReasonCode(): ReasonCode {
|
|
293
291
|
const reasonCode = this.getByte();
|
|
294
292
|
if (!isValidReasonCode(this.packetType, reasonCode)) {
|
|
295
293
|
throw new DecoderError("Invalid reason code");
|
|
296
294
|
}
|
|
297
|
-
return reasonCode as
|
|
295
|
+
return reasonCode as ReasonCode;
|
|
298
296
|
}
|
|
299
297
|
|
|
300
298
|
/**
|
package/mqttPacket/disconnect.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { PacketType } from "./PacketType.ts";
|
|
2
2
|
import { Decoder, hasEmptyFlags, isEmptyBuf } from "./decoder.ts";
|
|
3
|
-
import type { CodecOpts
|
|
3
|
+
import type { CodecOpts } from "./types.ts";
|
|
4
4
|
import type { ProtocolLevelNoV5 } from "./protocolLevels.ts";
|
|
5
5
|
import type { DisconnectProperties } from "./Properties.ts";
|
|
6
6
|
import { Encoder } from "./encoder.ts";
|
|
7
7
|
import { ReasonCode } from "./ReasonCode.ts";
|
|
8
8
|
|
|
9
9
|
export type DisconnectPacketv4 = {
|
|
10
|
-
type:
|
|
10
|
+
type: PacketType;
|
|
11
11
|
protocolLevel: ProtocolLevelNoV5;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
export type DisconnectPacketv5 = {
|
|
15
|
-
type:
|
|
15
|
+
type: PacketType;
|
|
16
16
|
protocolLevel: 5;
|
|
17
|
-
reasonCode:
|
|
17
|
+
reasonCode: ReasonCode;
|
|
18
18
|
properties?: DisconnectProperties;
|
|
19
19
|
};
|
|
20
20
|
|
|
@@ -44,7 +44,7 @@ export function encode(
|
|
|
44
44
|
return encoder.done(0);
|
|
45
45
|
}
|
|
46
46
|
encoder
|
|
47
|
-
.
|
|
47
|
+
.seReasonCode(reasonCode);
|
|
48
48
|
if (packet.properties) {
|
|
49
49
|
encoder.setProperties(
|
|
50
50
|
packet.properties,
|
|
@@ -59,7 +59,7 @@ export function decode(
|
|
|
59
59
|
buffer: Uint8Array,
|
|
60
60
|
flags: number,
|
|
61
61
|
codecOpts: CodecOpts,
|
|
62
|
-
packetType:
|
|
62
|
+
packetType: PacketType,
|
|
63
63
|
): DisconnectPacket {
|
|
64
64
|
hasEmptyFlags(flags);
|
|
65
65
|
if (codecOpts.protocolLevel !== 5) {
|
|
@@ -77,7 +77,7 @@ export function decode(
|
|
|
77
77
|
reasonCode: 0,
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
|
-
const reasonCode = decoder.
|
|
80
|
+
const reasonCode = decoder.geReasonCode();
|
|
81
81
|
const properties = decoder.getProperties(PacketType.disconnect);
|
|
82
82
|
decoder.done();
|
|
83
83
|
return {
|
|
@@ -51,13 +51,13 @@ test("encode string", () => {
|
|
|
51
51
|
test("encode reasonCode", () => {
|
|
52
52
|
const reasonCode = ReasonCode.success;
|
|
53
53
|
const encoder = new Encoder(PacketType.connack);
|
|
54
|
-
encoder.
|
|
54
|
+
encoder.seReasonCode(reasonCode);
|
|
55
55
|
assert.deepStrictEqual(encoder.done(0), new Uint8Array([32, 1, reasonCode]));
|
|
56
56
|
});
|
|
57
57
|
|
|
58
58
|
test("encode invalid reasonCode", () => {
|
|
59
59
|
const encoder = new Encoder(PacketType.connack);
|
|
60
60
|
assert.throws(() => {
|
|
61
|
-
encoder.
|
|
61
|
+
encoder.seReasonCode(ReasonCode.noMatchingSubscribers);
|
|
62
62
|
}, /Reason code 16 not allowed for packet type 2/);
|
|
63
63
|
});
|
package/mqttPacket/encoder.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { UTF8StringPair } from "./types.ts";
|
|
2
2
|
|
|
3
3
|
import type {
|
|
4
4
|
Mqttv5PropertyTypes,
|
|
5
|
+
PropertySetType,
|
|
5
6
|
PropsByPacketSetType,
|
|
6
7
|
SubscriptionIdentifiersType,
|
|
7
|
-
TPropertySetType,
|
|
8
8
|
UserPropertyType,
|
|
9
9
|
ValidPropertyNumber,
|
|
10
10
|
} from "./Properties.ts";
|
|
@@ -17,6 +17,8 @@ import {
|
|
|
17
17
|
propertyToKind,
|
|
18
18
|
} from "./Properties.ts";
|
|
19
19
|
|
|
20
|
+
import type { PacketType } from "./PacketType.ts";
|
|
21
|
+
import type { ReasonCode } from "./ReasonCode.ts";
|
|
20
22
|
import { encodeLength } from "./length.ts";
|
|
21
23
|
import { invalidUTF8 } from "./validators.ts";
|
|
22
24
|
import { isValidReasonCode } from "./ReasonCode.ts";
|
|
@@ -44,7 +46,7 @@ export class Encoder {
|
|
|
44
46
|
private numBytes: number;
|
|
45
47
|
|
|
46
48
|
/** Packet type */
|
|
47
|
-
private packetType:
|
|
49
|
+
private packetType: PacketType;
|
|
48
50
|
|
|
49
51
|
/** marker to rewind to */
|
|
50
52
|
private marker: number;
|
|
@@ -52,7 +54,7 @@ export class Encoder {
|
|
|
52
54
|
/**
|
|
53
55
|
* Creates a new Encoder instance
|
|
54
56
|
*/
|
|
55
|
-
constructor(packetType:
|
|
57
|
+
constructor(packetType: PacketType) {
|
|
56
58
|
this.buffers = [];
|
|
57
59
|
this.numBytes = 0;
|
|
58
60
|
this.packetType = packetType;
|
|
@@ -253,9 +255,9 @@ export class Encoder {
|
|
|
253
255
|
* @param propertySetType - the type of packet to encode for
|
|
254
256
|
* @param maximumPacketSize - the maximum packet size to be consumed by properties
|
|
255
257
|
*/
|
|
256
|
-
setProperties<T extends
|
|
258
|
+
setProperties<T extends PropertySetType>(
|
|
257
259
|
props: PropsByPacketSetType[T],
|
|
258
|
-
propertySetType:
|
|
260
|
+
propertySetType: PropertySetType,
|
|
259
261
|
maximumPacketSize: number,
|
|
260
262
|
) {
|
|
261
263
|
const propsEncoder = new Encoder(0);
|
|
@@ -282,7 +284,7 @@ export class Encoder {
|
|
|
282
284
|
propsEncoder.addToEncoder(this);
|
|
283
285
|
}
|
|
284
286
|
|
|
285
|
-
|
|
287
|
+
seReasonCode(reasonCode: ReasonCode) {
|
|
286
288
|
if (!isValidReasonCode(this.packetType, reasonCode)) {
|
|
287
289
|
throw new EncoderError(
|
|
288
290
|
`Reason code ${reasonCode} not allowed for packet type ${this.packetType}`,
|
package/mqttPacket/mod.ts
CHANGED
package/mqttPacket/pingreq.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { PacketType } from "./PacketType.ts";
|
|
2
2
|
import { hasEmptyFlags, isEmptyBuf } from "./decoder.ts";
|
|
3
|
-
import type { CodecOpts
|
|
3
|
+
import type { CodecOpts } from "./types.ts";
|
|
4
4
|
import type { ProtocolLevel } from "./protocolLevels.ts";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* PingreqPacket is a packet that is sent to the server to keep the connection alive
|
|
8
8
|
*/
|
|
9
9
|
export type PingreqPacket = {
|
|
10
|
-
type:
|
|
10
|
+
type: PacketType;
|
|
11
11
|
protocolLevel: ProtocolLevel;
|
|
12
12
|
};
|
|
13
13
|
|
|
@@ -24,7 +24,7 @@ export function decode(
|
|
|
24
24
|
buffer: Uint8Array,
|
|
25
25
|
flags: number,
|
|
26
26
|
codecOpts: CodecOpts,
|
|
27
|
-
_packetType:
|
|
27
|
+
_packetType: PacketType,
|
|
28
28
|
): PingreqPacket {
|
|
29
29
|
hasEmptyFlags(flags);
|
|
30
30
|
isEmptyBuf(buffer);
|
package/mqttPacket/pingres.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PacketType } from "./PacketType.ts";
|
|
2
2
|
import { hasEmptyFlags, isEmptyBuf } from "./decoder.ts";
|
|
3
|
-
import type { CodecOpts
|
|
3
|
+
import type { CodecOpts } from "./types.ts";
|
|
4
4
|
import type { ProtocolLevel } from "./protocolLevels.ts";
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -8,7 +8,7 @@ import type { ProtocolLevel } from "./protocolLevels.ts";
|
|
|
8
8
|
* It is used to indicate that the client is still connected to the server.
|
|
9
9
|
*/
|
|
10
10
|
export type PingresPacket = {
|
|
11
|
-
type:
|
|
11
|
+
type: PacketType;
|
|
12
12
|
protocolLevel: ProtocolLevel;
|
|
13
13
|
};
|
|
14
14
|
|
|
@@ -25,7 +25,7 @@ export function decode(
|
|
|
25
25
|
buffer: Uint8Array,
|
|
26
26
|
flags: number,
|
|
27
27
|
codecOpts: CodecOpts,
|
|
28
|
-
_packetType:
|
|
28
|
+
_packetType: PacketType,
|
|
29
29
|
): PingresPacket {
|
|
30
30
|
hasEmptyFlags(flags);
|
|
31
31
|
isEmptyBuf(buffer);
|
|
@@ -14,5 +14,7 @@ export const MQTTLevel = {
|
|
|
14
14
|
v5: 5,
|
|
15
15
|
} as const;
|
|
16
16
|
|
|
17
|
+
/** the MQTT protocol level: 3, 4, 5 or undefined */
|
|
17
18
|
export type ProtocolLevel = (typeof MQTTLevel)[keyof typeof MQTTLevel];
|
|
19
|
+
/** all MQTT protocol levels except 5 */
|
|
18
20
|
export type ProtocolLevelNoV5 = Exclude<ProtocolLevel, 5>;
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import type { CodecOpts, PacketId
|
|
1
|
+
import type { CodecOpts, PacketId } from "./types.ts";
|
|
2
2
|
import type { ProtocolLevelNoV5 } from "./protocolLevels.ts";
|
|
3
|
-
import { ReasonCode
|
|
3
|
+
import { ReasonCode } from "./ReasonCode.ts";
|
|
4
4
|
import type { PubackProperties } from "./Properties.ts";
|
|
5
5
|
import { PacketType } from "./PacketType.ts";
|
|
6
6
|
import { Decoder, DecoderError } from "./decoder.ts";
|
|
7
7
|
import { Encoder } from "./encoder.ts";
|
|
8
8
|
|
|
9
|
-
export type AckPacketV4<T extends
|
|
9
|
+
export type AckPacketV4<T extends PacketType> = {
|
|
10
10
|
type: T;
|
|
11
11
|
protocolLevel: ProtocolLevelNoV5;
|
|
12
12
|
id: PacketId;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
export type AckPacketV5<T extends
|
|
15
|
+
export type AckPacketV5<T extends PacketType> = {
|
|
16
16
|
type: T;
|
|
17
17
|
protocolLevel: 5;
|
|
18
18
|
id: PacketId;
|
|
19
|
-
reasonCode?:
|
|
19
|
+
reasonCode?: ReasonCode;
|
|
20
20
|
properties?: PubackProperties;
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* all 4 ack packets are identical except for packet type and flags of Pubrel
|
|
25
25
|
*/
|
|
26
|
-
export type AckPacket<T extends
|
|
26
|
+
export type AckPacket<T extends PacketType> = AckPacketV4<T> | AckPacketV5<T>;
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* PubackPacket is sent to indicate publish complete (QoS 1)
|
|
@@ -64,7 +64,7 @@ export function encode(packet: AnyAckPacket, codecOpts: CodecOpts): Uint8Array {
|
|
|
64
64
|
) {
|
|
65
65
|
return encoder.done(flags);
|
|
66
66
|
}
|
|
67
|
-
encoder.
|
|
67
|
+
encoder.seReasonCode(reasonCode);
|
|
68
68
|
encoder.setProperties(
|
|
69
69
|
packet.properties || {},
|
|
70
70
|
packet.type,
|
|
@@ -78,7 +78,7 @@ export function decode(
|
|
|
78
78
|
buffer: Uint8Array,
|
|
79
79
|
flags: number,
|
|
80
80
|
codecOpts: CodecOpts,
|
|
81
|
-
packetType:
|
|
81
|
+
packetType: PacketType,
|
|
82
82
|
): AnyAckPacket {
|
|
83
83
|
const expectedFlags = packetType === PacketType.pubrel ? 2 : 0;
|
|
84
84
|
if (flags !== expectedFlags) {
|
|
@@ -102,7 +102,7 @@ export function decode(
|
|
|
102
102
|
reasonCode: 0,
|
|
103
103
|
} as AnyAckPacket;
|
|
104
104
|
}
|
|
105
|
-
const reasonCode = decoder.
|
|
105
|
+
const reasonCode = decoder.geReasonCode();
|
|
106
106
|
const properties = decoder.getProperties(packetType);
|
|
107
107
|
return {
|
|
108
108
|
type: packetType as AnyAckPacket["type"],
|
package/mqttPacket/publish.ts
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CodecOpts,
|
|
3
|
-
Dup,
|
|
4
|
-
Payload,
|
|
5
|
-
QoS,
|
|
6
|
-
Topic,
|
|
7
|
-
TPacketType,
|
|
8
|
-
} from "./types.ts";
|
|
1
|
+
import type { CodecOpts, Dup, Payload, QoS, Topic } from "./types.ts";
|
|
9
2
|
import type { ProtocolLevelNoV5 } from "./protocolLevels.ts";
|
|
10
3
|
import type { PublishProperties } from "./Properties.ts";
|
|
11
4
|
import { PacketType } from "./PacketType.ts";
|
|
@@ -18,7 +11,7 @@ import { booleanFlag, Decoder, DecoderError } from "./decoder.ts";
|
|
|
18
11
|
* from server to subscribers
|
|
19
12
|
*/
|
|
20
13
|
export type PublishPacketV4 = {
|
|
21
|
-
type:
|
|
14
|
+
type: PacketType;
|
|
22
15
|
protocolLevel: ProtocolLevelNoV5;
|
|
23
16
|
topic: Topic;
|
|
24
17
|
payload: Payload;
|
|
@@ -28,7 +21,7 @@ export type PublishPacketV4 = {
|
|
|
28
21
|
id?: number | undefined;
|
|
29
22
|
};
|
|
30
23
|
export type PublishPacketV5 = {
|
|
31
|
-
type:
|
|
24
|
+
type: PacketType;
|
|
32
25
|
protocolLevel: 5;
|
|
33
26
|
topic: Topic;
|
|
34
27
|
payload: Payload;
|
|
@@ -77,7 +70,7 @@ export function decode(
|
|
|
77
70
|
buffer: Uint8Array,
|
|
78
71
|
flags: number,
|
|
79
72
|
codecOpts: CodecOpts,
|
|
80
|
-
packetType:
|
|
73
|
+
packetType: PacketType,
|
|
81
74
|
): PublishPacket {
|
|
82
75
|
const dup = booleanFlag(flags, BitMask.bit3);
|
|
83
76
|
const qos = (flags & 6) >> 1;
|
|
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
|
|
|
2
2
|
import { test } from "node:test";
|
|
3
3
|
|
|
4
4
|
import { decode, encode, MQTTLevel, PacketType, ReasonCode } from "./mod.ts";
|
|
5
|
-
import type { CodecOpts
|
|
5
|
+
import type { CodecOpts } from "./mod.ts";
|
|
6
6
|
|
|
7
7
|
const codecOptsV4: CodecOpts = {
|
|
8
8
|
protocolLevel: MQTTLevel.v4,
|
|
@@ -24,9 +24,9 @@ const packetTypesToTest = [
|
|
|
24
24
|
];
|
|
25
25
|
for (const item of packetTypesToTest) {
|
|
26
26
|
const [ackType, label, reasonCode] = item as [
|
|
27
|
-
|
|
27
|
+
PacketType,
|
|
28
28
|
string,
|
|
29
|
-
|
|
29
|
+
ReasonCode,
|
|
30
30
|
];
|
|
31
31
|
const ackTypeByte = (ackType << 4) + (ackType === PacketType.pubrel ? 2 : 0);
|
|
32
32
|
test(`encode/decode ${label} v4`, () => {
|
package/mqttPacket/suback.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
PacketId,
|
|
4
|
-
ReturnCodes,
|
|
5
|
-
TPacketType,
|
|
6
|
-
TReasonCode,
|
|
7
|
-
} from "./types.ts";
|
|
1
|
+
import type { CodecOpts, PacketId, ReturnCodes } from "./types.ts";
|
|
2
|
+
import type { ReasonCode } from "./ReasonCode.ts";
|
|
8
3
|
import type { ProtocolLevelNoV5 } from "./protocolLevels.ts";
|
|
9
4
|
import type { SubackProperties } from "./Properties.ts";
|
|
10
5
|
import { PacketType } from "./PacketType.ts";
|
|
@@ -13,18 +8,18 @@ import { Decoder, DecoderError } from "./decoder.ts";
|
|
|
13
8
|
const validReturnCodes = [0x00, 0x01, 0x02, 0x80];
|
|
14
9
|
|
|
15
10
|
export type SubackPacketV4 = {
|
|
16
|
-
type:
|
|
11
|
+
type: PacketType;
|
|
17
12
|
protocolLevel: ProtocolLevelNoV5;
|
|
18
13
|
id: PacketId;
|
|
19
14
|
returnCodes: ReturnCodes;
|
|
20
15
|
};
|
|
21
16
|
|
|
22
17
|
export type SubackPacketV5 = {
|
|
23
|
-
type:
|
|
18
|
+
type: PacketType;
|
|
24
19
|
protocolLevel: 5;
|
|
25
20
|
id: PacketId;
|
|
26
21
|
properties: SubackProperties;
|
|
27
|
-
reasonCodes: Array<
|
|
22
|
+
reasonCodes: Array<ReasonCode>;
|
|
28
23
|
};
|
|
29
24
|
|
|
30
25
|
/**
|
|
@@ -52,7 +47,7 @@ export function encode(packet: SubackPacket, codecOpts: CodecOpts): Uint8Array {
|
|
|
52
47
|
codecOpts.maxOutgoingPacketSize,
|
|
53
48
|
);
|
|
54
49
|
for (const code of packet.reasonCodes) {
|
|
55
|
-
encoder.
|
|
50
|
+
encoder.seReasonCode(code);
|
|
56
51
|
}
|
|
57
52
|
return encoder.done(flags);
|
|
58
53
|
}
|
|
@@ -61,7 +56,7 @@ export function decode(
|
|
|
61
56
|
buffer: Uint8Array,
|
|
62
57
|
_flags: number,
|
|
63
58
|
codecOpts: CodecOpts,
|
|
64
|
-
packetType:
|
|
59
|
+
packetType: PacketType,
|
|
65
60
|
): SubackPacket {
|
|
66
61
|
const packet = {} as SubackPacket;
|
|
67
62
|
const decoder = new Decoder(packetType, buffer);
|
|
@@ -83,7 +78,7 @@ export function decode(
|
|
|
83
78
|
packet.properties = decoder.getProperties(PacketType.suback);
|
|
84
79
|
packet.reasonCodes = [];
|
|
85
80
|
while (!decoder.atEnd()) {
|
|
86
|
-
const code = decoder.
|
|
81
|
+
const code = decoder.geReasonCode();
|
|
87
82
|
packet.reasonCodes.push(code);
|
|
88
83
|
}
|
|
89
84
|
return packet;
|