@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.
Files changed (225) hide show
  1. package/client/ConnectionState.ts +1 -1
  2. package/client/client.ts +41 -9
  3. package/client/context.ts +11 -13
  4. package/client/deps.ts +8 -5
  5. package/client/handlers/handleConnack.ts +2 -2
  6. package/client/mod.ts +13 -2
  7. package/client/store/memoryStore.ts +11 -10
  8. package/client/store/store.ts +1 -1
  9. package/deno/mod.ts +7 -0
  10. package/deno/tcpClient.ts +21 -8
  11. package/deno/tcpServer.ts +21 -4
  12. package/deno/tlsServer.ts +20 -4
  13. package/deno/wsServer.ts +23 -0
  14. package/dist/client/ConnectionState.d.ts +1 -1
  15. package/dist/client/ConnectionState.d.ts.map +1 -1
  16. package/dist/client/client.d.ts +39 -9
  17. package/dist/client/client.d.ts.map +1 -1
  18. package/dist/client/client.js +24 -0
  19. package/dist/client/client.js.map +1 -1
  20. package/dist/client/context.d.ts +8 -8
  21. package/dist/client/context.d.ts.map +1 -1
  22. package/dist/client/context.js.map +1 -1
  23. package/dist/client/deps.d.ts +3 -3
  24. package/dist/client/deps.d.ts.map +1 -1
  25. package/dist/client/deps.js.map +1 -1
  26. package/dist/client/handlers/handleConnack.d.ts.map +1 -1
  27. package/dist/client/handlers/handleConnack.js.map +1 -1
  28. package/dist/client/mod.d.ts +3 -2
  29. package/dist/client/mod.d.ts.map +1 -1
  30. package/dist/client/mod.js +1 -1
  31. package/dist/client/mod.js.map +1 -1
  32. package/dist/client/store/memoryStore.d.ts +3 -3
  33. package/dist/client/store/memoryStore.d.ts.map +1 -1
  34. package/dist/client/store/memoryStore.js +2 -2
  35. package/dist/client/store/memoryStore.js.map +1 -1
  36. package/dist/client/store/store.d.ts +1 -1
  37. package/dist/client/store/store.d.ts.map +1 -1
  38. package/dist/examples/authHandler.test.js +3 -3
  39. package/dist/examples/authHandler.test.js.map +1 -1
  40. package/dist/mqttConn/mqttConn.d.ts +2 -17
  41. package/dist/mqttConn/mqttConn.d.ts.map +1 -1
  42. package/dist/mqttConn/mqttConn.js +1 -1
  43. package/dist/mqttConn/mqttConn.js.map +1 -1
  44. package/dist/mqttPacket/AuthenticationResult.d.ts +4 -0
  45. package/dist/mqttPacket/AuthenticationResult.d.ts.map +1 -1
  46. package/dist/mqttPacket/AuthenticationResult.js.map +1 -1
  47. package/dist/mqttPacket/PacketType.d.ts +4 -0
  48. package/dist/mqttPacket/PacketType.d.ts.map +1 -1
  49. package/dist/mqttPacket/Properties.d.ts +2 -2
  50. package/dist/mqttPacket/Properties.d.ts.map +1 -1
  51. package/dist/mqttPacket/ReasonCode.d.ts +3 -5
  52. package/dist/mqttPacket/ReasonCode.d.ts.map +1 -1
  53. package/dist/mqttPacket/ReasonCode.js.map +1 -1
  54. package/dist/mqttPacket/RetainHandling.d.ts +4 -0
  55. package/dist/mqttPacket/RetainHandling.d.ts.map +1 -1
  56. package/dist/mqttPacket/auth.d.ts +6 -5
  57. package/dist/mqttPacket/auth.d.ts.map +1 -1
  58. package/dist/mqttPacket/auth.js +2 -2
  59. package/dist/mqttPacket/auth.js.map +1 -1
  60. package/dist/mqttPacket/codec.d.ts.map +1 -1
  61. package/dist/mqttPacket/codec.js.map +1 -1
  62. package/dist/mqttPacket/connack.d.ts +9 -7
  63. package/dist/mqttPacket/connack.d.ts.map +1 -1
  64. package/dist/mqttPacket/connack.js +2 -2
  65. package/dist/mqttPacket/connack.js.map +1 -1
  66. package/dist/mqttPacket/connect.d.ts +5 -4
  67. package/dist/mqttPacket/connect.d.ts.map +1 -1
  68. package/dist/mqttPacket/connect.js.map +1 -1
  69. package/dist/mqttPacket/decoder.d.ts +6 -4
  70. package/dist/mqttPacket/decoder.d.ts.map +1 -1
  71. package/dist/mqttPacket/decoder.js +1 -1
  72. package/dist/mqttPacket/decoder.js.map +1 -1
  73. package/dist/mqttPacket/disconnect.d.ts +7 -5
  74. package/dist/mqttPacket/disconnect.d.ts.map +1 -1
  75. package/dist/mqttPacket/disconnect.js +2 -2
  76. package/dist/mqttPacket/disconnect.js.map +1 -1
  77. package/dist/mqttPacket/encoder.d.ts +7 -5
  78. package/dist/mqttPacket/encoder.d.ts.map +1 -1
  79. package/dist/mqttPacket/encoder.js +1 -1
  80. package/dist/mqttPacket/encoder.js.map +1 -1
  81. package/dist/mqttPacket/mod.d.ts +1 -1
  82. package/dist/mqttPacket/mod.d.ts.map +1 -1
  83. package/dist/mqttPacket/pingreq.d.ts +4 -3
  84. package/dist/mqttPacket/pingreq.d.ts.map +1 -1
  85. package/dist/mqttPacket/pingreq.js.map +1 -1
  86. package/dist/mqttPacket/pingres.d.ts +4 -3
  87. package/dist/mqttPacket/pingres.d.ts.map +1 -1
  88. package/dist/mqttPacket/pingres.js.map +1 -1
  89. package/dist/mqttPacket/protocolLevels.d.ts +2 -0
  90. package/dist/mqttPacket/protocolLevels.d.ts.map +1 -1
  91. package/dist/mqttPacket/pubblishAcks.d.ts +7 -7
  92. package/dist/mqttPacket/pubblishAcks.d.ts.map +1 -1
  93. package/dist/mqttPacket/pubblishAcks.js +2 -2
  94. package/dist/mqttPacket/pubblishAcks.js.map +1 -1
  95. package/dist/mqttPacket/publish.d.ts +5 -4
  96. package/dist/mqttPacket/publish.d.ts.map +1 -1
  97. package/dist/mqttPacket/publish.js.map +1 -1
  98. package/dist/mqttPacket/suback.d.ts +7 -5
  99. package/dist/mqttPacket/suback.d.ts.map +1 -1
  100. package/dist/mqttPacket/suback.js +2 -2
  101. package/dist/mqttPacket/suback.js.map +1 -1
  102. package/dist/mqttPacket/subscribe.d.ts +7 -5
  103. package/dist/mqttPacket/subscribe.d.ts.map +1 -1
  104. package/dist/mqttPacket/subscribe.js.map +1 -1
  105. package/dist/mqttPacket/types.d.ts +1 -21
  106. package/dist/mqttPacket/types.d.ts.map +1 -1
  107. package/dist/mqttPacket/unsuback.d.ts +7 -5
  108. package/dist/mqttPacket/unsuback.d.ts.map +1 -1
  109. package/dist/mqttPacket/unsuback.js +1 -1
  110. package/dist/mqttPacket/unsuback.js.map +1 -1
  111. package/dist/mqttPacket/unsubscribe.d.ts +5 -4
  112. package/dist/mqttPacket/unsubscribe.d.ts.map +1 -1
  113. package/dist/mqttPacket/unsubscribe.js.map +1 -1
  114. package/dist/node/tcpClient.d.ts +21 -1
  115. package/dist/node/tcpClient.d.ts.map +1 -1
  116. package/dist/node/tcpClient.js +18 -4
  117. package/dist/node/tcpClient.js.map +1 -1
  118. package/dist/node/tcpServer.d.ts +18 -0
  119. package/dist/node/tcpServer.d.ts.map +1 -1
  120. package/dist/node/tcpServer.js +17 -2
  121. package/dist/node/tcpServer.js.map +1 -1
  122. package/dist/node/tlsServer.d.ts +18 -0
  123. package/dist/node/tlsServer.d.ts.map +1 -1
  124. package/dist/node/tlsServer.js +17 -2
  125. package/dist/node/tlsServer.js.map +1 -1
  126. package/dist/persistence/basicPersistence.d.ts +4 -4
  127. package/dist/persistence/basicPersistence.d.ts.map +1 -1
  128. package/dist/persistence/basicPersistence.js.map +1 -1
  129. package/dist/persistence/deps.d.ts +1 -1
  130. package/dist/persistence/deps.d.ts.map +1 -1
  131. package/dist/persistence/memory/memoryStorage.d.ts +3 -3
  132. package/dist/persistence/memory/memoryStorage.d.ts.map +1 -1
  133. package/dist/persistence/mod.d.ts +1 -1
  134. package/dist/persistence/mod.d.ts.map +1 -1
  135. package/dist/persistence/persistence.d.ts +3 -3
  136. package/dist/persistence/persistence.d.ts.map +1 -1
  137. package/dist/persistence/sqlite/sqliteStorage.d.ts +2 -2
  138. package/dist/persistence/sqlite/sqliteStorage.d.ts.map +1 -1
  139. package/dist/persistence/storage.d.ts +1 -1
  140. package/dist/persistence/storage.d.ts.map +1 -1
  141. package/dist/server/context.d.ts +4 -4
  142. package/dist/server/context.d.ts.map +1 -1
  143. package/dist/server/context.js.map +1 -1
  144. package/dist/server/deps.d.ts +2 -2
  145. package/dist/server/deps.d.ts.map +1 -1
  146. package/dist/server/deps.js.map +1 -1
  147. package/dist/server/handlers/completeConnect.d.ts +3 -2
  148. package/dist/server/handlers/completeConnect.d.ts.map +1 -1
  149. package/dist/server/handlers/completeConnect.js.map +1 -1
  150. package/dist/server/handlers/handlePublish.d.ts.map +1 -1
  151. package/dist/server/handlers/handlePublish.js.map +1 -1
  152. package/dist/server/handlers/handleSubscribe.d.ts.map +1 -1
  153. package/dist/server/handlers/handleSubscribe.js.map +1 -1
  154. package/dist/server/handlers/handleUnsubscribe.d.ts.map +1 -1
  155. package/dist/server/handlers/handleUnsubscribe.js.map +1 -1
  156. package/dist/server/mod.d.ts +1 -1
  157. package/dist/server/mod.d.ts.map +1 -1
  158. package/dist/server/server.d.ts +3 -3
  159. package/dist/server/server.d.ts.map +1 -1
  160. package/dist/server/server.js.map +1 -1
  161. package/dist/socket/socket.d.ts +22 -0
  162. package/dist/socket/socket.d.ts.map +1 -1
  163. package/dist/socket/socket.js +5 -0
  164. package/dist/socket/socket.js.map +1 -1
  165. package/dist/utils/logger.d.ts +3 -3
  166. package/dist/utils/logger.d.ts.map +1 -1
  167. package/dist/utils/logger.js +1 -1
  168. package/dist/utils/logger.js.map +1 -1
  169. package/dist/web/wsClient.d.ts +12 -0
  170. package/dist/web/wsClient.d.ts.map +1 -1
  171. package/dist/web/wsClient.js +12 -1
  172. package/dist/web/wsClient.js.map +1 -1
  173. package/examples/authHandler.test.ts +3 -3
  174. package/mqttConn/mqttConn.ts +2 -18
  175. package/mqttPacket/AuthenticationResult.ts +6 -0
  176. package/mqttPacket/PacketType.ts +5 -0
  177. package/mqttPacket/Properties.ts +2 -2
  178. package/mqttPacket/ReasonCode.ts +6 -8
  179. package/mqttPacket/RetainHandling.ts +5 -0
  180. package/mqttPacket/auth.ts +7 -7
  181. package/mqttPacket/codec.ts +3 -2
  182. package/mqttPacket/connack.ts +11 -10
  183. package/mqttPacket/connect.ts +4 -11
  184. package/mqttPacket/decoder.ts +9 -11
  185. package/mqttPacket/disconnect.ts +7 -7
  186. package/mqttPacket/encoder.test.ts +2 -2
  187. package/mqttPacket/encoder.ts +9 -7
  188. package/mqttPacket/mod.ts +0 -4
  189. package/mqttPacket/pingreq.ts +3 -3
  190. package/mqttPacket/pingres.ts +3 -3
  191. package/mqttPacket/protocolLevels.ts +2 -0
  192. package/mqttPacket/pubblishAcks.ts +9 -9
  193. package/mqttPacket/publish.ts +4 -11
  194. package/mqttPacket/publishAcks.test.ts +3 -3
  195. package/mqttPacket/suback.ts +8 -13
  196. package/mqttPacket/subscribe.ts +6 -12
  197. package/mqttPacket/types.ts +1 -26
  198. package/mqttPacket/unsuback.ts +8 -7
  199. package/mqttPacket/unsubscribe.ts +4 -10
  200. package/node/mod.ts +7 -0
  201. package/node/tcpClient.ts +21 -6
  202. package/node/tcpServer.ts +23 -6
  203. package/node/tlsServer.ts +23 -7
  204. package/package.json +6 -1
  205. package/persistence/basicPersistence.ts +5 -5
  206. package/persistence/deps.ts +1 -1
  207. package/persistence/memory/memoryStorage.ts +3 -3
  208. package/persistence/mod.ts +1 -1
  209. package/persistence/persistence.testing.ts +4 -4
  210. package/persistence/persistence.ts +3 -3
  211. package/persistence/sqlite/sqliteStorage.ts +2 -2
  212. package/persistence/storage.ts +1 -1
  213. package/server/context.ts +5 -5
  214. package/server/deps.ts +1 -2
  215. package/server/handlers/completeConnect.ts +2 -2
  216. package/server/handlers/handlePublish.ts +3 -4
  217. package/server/handlers/handleSubscribe.ts +3 -4
  218. package/server/handlers/handleUnsubscribe.ts +2 -2
  219. package/server/mod.ts +1 -1
  220. package/server/server.ts +3 -3
  221. package/socket/socket.ts +31 -2
  222. package/tools/benchmarks/receiver.ts +2 -2
  223. package/tools/benchmarks/sender.ts +2 -2
  224. package/utils/logger.ts +3 -3
  225. package/web/wsClient.ts +13 -1
@@ -227,14 +227,12 @@ const reasonCodesToString = {
227
227
  [0xa2]: "Wildcard Subscriptions not supported",
228
228
  } as const;
229
229
 
230
- // Can't import this from types as this would create an import loop
231
- type TPacketType = typeof PacketType[keyof typeof PacketType];
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: TPacketType,
237
- code: TReasonCode,
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: TPacketType,
249
+ packetType: PacketType,
252
250
  code: number,
253
251
  ): boolean {
254
252
  const validReasonCodes =
255
- ReasonCodebyPacket[packetType as TReasonCodeByPacket] as readonly number[];
253
+ ReasonCodebyPacket[packetType as ReasonCodeByPacket] as readonly number[];
256
254
  return validReasonCodes.includes(code);
257
255
  }
@@ -9,3 +9,8 @@ export const RetainHandling = {
9
9
  ifSubscriptionNotExists: 1,
10
10
  noRetain: 2,
11
11
  } as const;
12
+
13
+ /**
14
+ * helper type to limit retain handling values
15
+ */
16
+ export type RetainHandling = typeof RetainHandling[keyof typeof RetainHandling];
@@ -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 { TReasonCode } from "./ReasonCode.ts";
4
+ import type { ReasonCode } from "./ReasonCode.ts";
5
5
  import type { AuthProperties } from "./Properties.ts";
6
- import type { CodecOpts, TPacketType } from "./types.ts";
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: TPacketType;
11
+ type: PacketType;
12
12
  protocolLevel: 5;
13
- reasonCode: TReasonCode;
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
- .setReasonCode(packet.reasonCode || 0)
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: TPacketType,
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.getReasonCode();
55
+ const reasonCode = decoder.geReasonCode();
56
56
  const properties = decoder.getProperties(PacketType.auth);
57
57
  decoder.done();
58
58
  return {
@@ -1,4 +1,5 @@
1
- import type { CodecOpts, TPacketType } from "./types.ts";
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 TPacketType,
121
+ packetType as PacketType,
121
122
  );
122
123
  if (packet !== undefined) {
123
124
  return packet;
@@ -1,4 +1,4 @@
1
- import type { CodecOpts, TAuthenticationResult, TPacketType } from "./types.ts";
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 { TReasonCode } from "./ReasonCode.ts";
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: TPacketType;
18
+ type: PacketType;
18
19
  protocolLevel: ProtocolLevelNoV5;
19
20
  sessionPresent: boolean;
20
- returnCode: TAuthenticationResult;
21
+ returnCode: AuthenticationResult;
21
22
  };
22
23
 
23
24
  export type ConnackPacketV5 = {
24
- type: TPacketType;
25
+ type: PacketType;
25
26
  protocolLevel: 5;
26
27
  sessionPresent: boolean;
27
- reasonCode: TReasonCode;
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
- .setReasonCode(packet.reasonCode || 0)
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: TPacketType,
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 TAuthenticationResult;
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.getReasonCode();
88
+ const reasonCode = decoder.geReasonCode();
88
89
  const properties = decoder.getProperties(PacketType.connack);
89
90
  decoder.done();
90
91
  return {
@@ -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: TPacketType;
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: TPacketType;
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: TPacketType,
163
+ packetType: PacketType,
171
164
  ): ConnectPacket {
172
165
  const decoder = new Decoder(packetType, buffer);
173
166
  const protocolName = decoder.getUTF8String();
@@ -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: TBitMask): boolean {
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: TPacketType;
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: TPacketType, buf: Uint8Array, pos: number = 0) {
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
- getReasonCode(): TReasonCode {
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 TReasonCode;
295
+ return reasonCode as ReasonCode;
298
296
  }
299
297
 
300
298
  /**
@@ -1,20 +1,20 @@
1
1
  import { PacketType } from "./PacketType.ts";
2
2
  import { Decoder, hasEmptyFlags, isEmptyBuf } from "./decoder.ts";
3
- import type { CodecOpts, TPacketType, TReasonCode } from "./types.ts";
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: TPacketType;
10
+ type: PacketType;
11
11
  protocolLevel: ProtocolLevelNoV5;
12
12
  };
13
13
 
14
14
  export type DisconnectPacketv5 = {
15
- type: TPacketType;
15
+ type: PacketType;
16
16
  protocolLevel: 5;
17
- reasonCode: TReasonCode;
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
- .setReasonCode(reasonCode);
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: TPacketType,
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.getReasonCode();
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.setReasonCode(reasonCode);
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.setReasonCode(ReasonCode.noMatchingSubscribers);
61
+ encoder.seReasonCode(ReasonCode.noMatchingSubscribers);
62
62
  }, /Reason code 16 not allowed for packet type 2/);
63
63
  });
@@ -1,10 +1,10 @@
1
- import type { TPacketType, TReasonCode, UTF8StringPair } from "./types.ts";
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: TPacketType;
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: TPacketType) {
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 TPropertySetType>(
258
+ setProperties<T extends PropertySetType>(
257
259
  props: PropsByPacketSetType[T],
258
- propertySetType: TPropertySetType,
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
- setReasonCode(reasonCode: TReasonCode) {
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
@@ -51,12 +51,8 @@ export type {
51
51
  Payload,
52
52
  QoS,
53
53
  ReturnCodes,
54
- TAuthenticationResult,
55
54
  Topic,
56
55
  TopicFilter,
57
- TPacketType,
58
- TReasonCode,
59
- TRetainHandling,
60
56
  UTF8StringPair,
61
57
  } from "./types.ts";
62
58
 
@@ -1,13 +1,13 @@
1
1
  import { PacketType } from "./PacketType.ts";
2
2
  import { hasEmptyFlags, isEmptyBuf } from "./decoder.ts";
3
- import type { CodecOpts, TPacketType } from "./types.ts";
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: TPacketType;
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: TPacketType,
27
+ _packetType: PacketType,
28
28
  ): PingreqPacket {
29
29
  hasEmptyFlags(flags);
30
30
  isEmptyBuf(buffer);
@@ -1,6 +1,6 @@
1
1
  import { PacketType } from "./PacketType.ts";
2
2
  import { hasEmptyFlags, isEmptyBuf } from "./decoder.ts";
3
- import type { CodecOpts, TPacketType } from "./types.ts";
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: TPacketType;
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: TPacketType,
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, TPacketType } from "./types.ts";
1
+ import type { CodecOpts, PacketId } from "./types.ts";
2
2
  import type { ProtocolLevelNoV5 } from "./protocolLevels.ts";
3
- import { ReasonCode, type TReasonCode } from "./ReasonCode.ts";
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 TPacketType> = {
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 TPacketType> = {
15
+ export type AckPacketV5<T extends PacketType> = {
16
16
  type: T;
17
17
  protocolLevel: 5;
18
18
  id: PacketId;
19
- reasonCode?: TReasonCode;
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 TPacketType> = AckPacketV4<T> | AckPacketV5<T>;
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.setReasonCode(reasonCode);
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: TPacketType,
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.getReasonCode();
105
+ const reasonCode = decoder.geReasonCode();
106
106
  const properties = decoder.getProperties(packetType);
107
107
  return {
108
108
  type: packetType as AnyAckPacket["type"],
@@ -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: TPacketType;
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: TPacketType;
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: TPacketType,
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, TPacketType, TReasonCode } from "./mod.ts";
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
- TPacketType,
27
+ PacketType,
28
28
  string,
29
- TReasonCode,
29
+ ReasonCode,
30
30
  ];
31
31
  const ackTypeByte = (ackType << 4) + (ackType === PacketType.pubrel ? 2 : 0);
32
32
  test(`encode/decode ${label} v4`, () => {
@@ -1,10 +1,5 @@
1
- import type {
2
- CodecOpts,
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: TPacketType;
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: TPacketType;
18
+ type: PacketType;
24
19
  protocolLevel: 5;
25
20
  id: PacketId;
26
21
  properties: SubackProperties;
27
- reasonCodes: Array<TReasonCode>;
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.setReasonCode(code);
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: TPacketType,
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.getReasonCode();
81
+ const code = decoder.geReasonCode();
87
82
  packet.reasonCodes.push(code);
88
83
  }
89
84
  return packet;