@seriousme/opifex 1.6.0 → 1.6.2

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 (63) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/bin/conformanceServer.ts +1 -2
  3. package/bin/mqtt.ts +2 -3
  4. package/deno/tcpClient.ts +27 -5
  5. package/deno/tcpServer.ts +11 -4
  6. package/deno.json +2 -2
  7. package/dist/bin/conformanceServer.js +1 -2
  8. package/dist/bin/mqtt.js +1 -2
  9. package/dist/mqttPacket/AuthenticationResult.js +5 -1
  10. package/dist/mqttPacket/connack.js +1 -1
  11. package/dist/mqttPacket/connect.js +1 -1
  12. package/dist/mqttPacket/disconnect.js +1 -1
  13. package/dist/mqttPacket/mod.js +19 -14
  14. package/dist/mqttPacket/pingreq.js +1 -1
  15. package/dist/mqttPacket/pingres.js +1 -1
  16. package/dist/mqttPacket/puback.js +1 -1
  17. package/dist/mqttPacket/pubcomp.js +1 -1
  18. package/dist/mqttPacket/publish.js +1 -1
  19. package/dist/mqttPacket/pubrec.js +1 -1
  20. package/dist/mqttPacket/pubrel.js +1 -1
  21. package/dist/mqttPacket/suback.js +1 -1
  22. package/dist/mqttPacket/subscribe.js +1 -1
  23. package/dist/mqttPacket/unsuback.js +1 -1
  24. package/dist/mqttPacket/unsubscribe.js +1 -1
  25. package/dist/node/tcpClient.js +19 -2
  26. package/dist/node/tcpServer.js +3 -0
  27. package/dist/utils/args.js +14 -0
  28. package/dist/utils/assert.js +1 -0
  29. package/dist/utils/asyncQueue.js +11 -0
  30. package/dist/utils/deferred.js +3 -0
  31. package/dist/utils/logger.js +29 -0
  32. package/dist/utils/mod.js +4 -0
  33. package/dist/utils/nextTick.js +3 -0
  34. package/dist/utils/platform.js +3 -0
  35. package/mqttPacket/AuthenticationResult.ts +7 -4
  36. package/mqttPacket/PacketType.ts +1 -1
  37. package/mqttPacket/connack.ts +8 -1
  38. package/mqttPacket/connect.ts +8 -2
  39. package/mqttPacket/disconnect.ts +9 -2
  40. package/mqttPacket/mod.ts +24 -15
  41. package/mqttPacket/pingreq.ts +8 -2
  42. package/mqttPacket/pingres.ts +9 -2
  43. package/mqttPacket/puback.ts +8 -2
  44. package/mqttPacket/pubcomp.ts +9 -2
  45. package/mqttPacket/publish.ts +9 -2
  46. package/mqttPacket/pubrec.ts +9 -2
  47. package/mqttPacket/pubrel.ts +9 -2
  48. package/mqttPacket/suback.ts +9 -2
  49. package/mqttPacket/subscribe.ts +11 -2
  50. package/mqttPacket/types.ts +32 -1
  51. package/mqttPacket/unsuback.ts +9 -2
  52. package/mqttPacket/unsubscribe.ts +8 -2
  53. package/node/tcpClient.ts +20 -2
  54. package/node/tcpServer.ts +8 -2
  55. package/package.json +3 -3
  56. package/utils/args.ts +17 -0
  57. package/utils/assert.ts +1 -0
  58. package/utils/asyncQueue.ts +11 -0
  59. package/utils/deferred.ts +4 -0
  60. package/utils/logger.ts +29 -0
  61. package/utils/mod.ts +5 -0
  62. package/utils/nextTick.ts +3 -0
  63. package/utils/platform.ts +5 -1
package/CHANGELOG.md CHANGED
@@ -4,6 +4,18 @@
4
4
 
5
5
  ### Changed
6
6
 
7
+ ## [v1.6.2] 19-01-2025
8
+
9
+ ### Changed
10
+
11
+ - fix: added more JSdoc comments
12
+
13
+ ## [v1.6.1] 18-01-2025
14
+
15
+ ### Changed
16
+
17
+ - fix: fixed Deno lint type errors
18
+
7
19
  ## [v1.6.0] 18-01-2025
8
20
 
9
21
  ### Changed
@@ -3,8 +3,7 @@
3
3
 
4
4
  import { AuthenticationResult } from "../server/mod.ts";
5
5
  import type { Context, TAuthenticationResult, Topic } from "../server/mod.ts";
6
- import { logger, LogLevel } from "../utils/mod.ts";
7
- import { getArgs, parseArgs } from "../utils/mod.ts";
6
+ import { getArgs, logger, LogLevel, parseArgs } from "../utils/mod.ts";
8
7
  import { TcpServer } from "../node/tcpServer.ts";
9
8
 
10
9
  const utf8Decoder = new TextDecoder();
package/bin/mqtt.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  #!/usr/bin/env -S node --experimental-strip-types
2
2
  import { DEFAULT_URL } from "../client/mod.ts";
3
- import { logger, LogLevel } from "../utils/mod.ts";
4
- import { getArgs, parseArgs } from "../utils/mod.ts";
5
3
  import { getFileData, TcpClient } from "../node/tcpClient.ts";
6
- import type { Args } from "../utils/args.ts";
4
+ import { getArgs, logger, LogLevel, parseArgs } from "../utils/mod.ts";
5
+ import type { Args } from "../utils/mod.ts";
7
6
 
8
7
  const client = new TcpClient();
9
8
  const encoder = new TextEncoder();
package/deno/tcpClient.ts CHANGED
@@ -1,10 +1,24 @@
1
- // This a Deno specific implementation of TCP client sockets
2
- // it extends the platform agnostic Client class
1
+ /**
2
+ * This a Deno specific implementation of TCP client sockets
3
+ * it extends the platform agnostic Client class
4
+ * @module
5
+ */
3
6
  import { Client } from "../client/client.ts";
4
7
  import { logger } from "../client/deps.ts";
5
8
  import type { SockConn } from "../client/deps.ts";
6
9
 
7
- export async function getFileData(filename: string | undefined) {
10
+ /**
11
+ * @function getFileData
12
+ * @param filename
13
+ * @returns Promise
14
+ *
15
+ * Fetches data from a file and returns it as a string
16
+ * @example
17
+ * const data = await getFileData("data.txt");
18
+ */
19
+ export async function getFileData(
20
+ filename: string | undefined,
21
+ ): Promise<string | undefined> {
8
22
  if (!filename) {
9
23
  return;
10
24
  }
@@ -15,8 +29,16 @@ export async function getFileData(filename: string | undefined) {
15
29
  return data;
16
30
  }
17
31
 
32
+ /*
33
+ * TCPclient extends the Client class to provide TCP based clients
34
+ * it is used by the MQTTclient to connect to the broker
35
+ * see mqtt.ts in the /bin folder as an example
36
+ */
18
37
  export class TcpClient extends Client {
19
- protected async connectMQTT(hostname: string, port = 1883) {
38
+ protected async connectMQTT(
39
+ hostname: string,
40
+ port = 1883,
41
+ ): Promise<Deno.TcpConn> {
20
42
  logger.debug({ hostname, port });
21
43
  return await Deno.connect({ hostname, port });
22
44
  }
@@ -27,7 +49,7 @@ export class TcpClient extends Client {
27
49
  caCerts?: string[],
28
50
  cert?: string,
29
51
  key?: string,
30
- ) {
52
+ ): Promise<Deno.TlsConn> {
31
53
  logger.debug({ hostname, port, caCerts, cert });
32
54
  return await Deno.connectTls({ hostname, port, caCerts, cert, key });
33
55
  }
package/deno/tcpServer.ts CHANGED
@@ -1,8 +1,15 @@
1
- // This a Deno specific implementation of a TCP socket listener
2
- // it uses the platform agnostic MqttServer class
1
+ /**
2
+ * This module provides a Deno specific implementation of a TCP socket listener
3
+ * it uses the platform agnostic MqttServer class
4
+ * @module
5
+ */
6
+
3
7
  import { MqttServer } from "../server/mod.ts";
4
8
  import type { MqttServerOptions } from "../server/mod.ts";
5
9
 
10
+ /*
11
+ * TCP server that wraps a MqttServer, see demoServer.ts in the /bin folder
12
+ */
6
13
  export class TcpServer {
7
14
  private listener: Deno.Listener<Deno.Conn>;
8
15
  private mqttServer: MqttServer;
@@ -23,14 +30,14 @@ export class TcpServer {
23
30
  this.listener.close();
24
31
  }
25
32
 
26
- get port() {
33
+ get port(): number | undefined {
27
34
  if (this.listener.addr.transport === "tcp") {
28
35
  return this.listener.addr.port;
29
36
  }
30
37
  return undefined;
31
38
  }
32
39
 
33
- get address() {
40
+ get address(): string | undefined {
34
41
  if (this.listener.addr.transport === "tcp") {
35
42
  return this.listener.addr.hostname;
36
43
  }
package/deno.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@seriousme/opifex",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "exports": {
5
5
  "./server": "./server/mod.ts",
6
6
  "./client": "./client/mod.ts",
7
7
  "./mqttPacket": "./mqttPacket/mod.ts",
8
- "./utils": "./utils/mod.ts,",
8
+ "./utils": "./utils/mod.ts",
9
9
  "./tcpClient": "./deno/tcpClient.ts",
10
10
  "./tcpServer": "./deno/tcpServer.ts"
11
11
  },
@@ -1,8 +1,7 @@
1
1
  #!/usr/bin/env -S node --experimental-strip-types
2
2
  // this server is intended to be used with https://github.com/eclipse/iottestware
3
3
  import { AuthenticationResult } from "../server/mod.js";
4
- import { logger, LogLevel } from "../utils/mod.js";
5
- import { getArgs, parseArgs } from "../utils/mod.js";
4
+ import { getArgs, logger, LogLevel, parseArgs } from "../utils/mod.js";
6
5
  import { TcpServer } from "../node/tcpServer.js";
7
6
  const utf8Decoder = new TextDecoder();
8
7
  const userTable = new Map();
package/dist/bin/mqtt.js CHANGED
@@ -1,8 +1,7 @@
1
1
  #!/usr/bin/env -S node --experimental-strip-types
2
2
  import { DEFAULT_URL } from "../client/mod.js";
3
- import { logger, LogLevel } from "../utils/mod.js";
4
- import { getArgs, parseArgs } from "../utils/mod.js";
5
3
  import { getFileData, TcpClient } from "../node/tcpClient.js";
4
+ import { getArgs, logger, LogLevel, parseArgs } from "../utils/mod.js";
6
5
  const client = new TcpClient();
7
6
  const encoder = new TextEncoder();
8
7
  const decoder = new TextDecoder();
@@ -9,4 +9,8 @@ export const AuthenticationResult = {
9
9
  badUsernameOrPassword: 4,
10
10
  notAuthorized: 5,
11
11
  };
12
- export const AuthenticationResultByNumber = Object.fromEntries(Object.entries(AuthenticationResult).map(([k, v]) => [v, k]));
12
+ /**
13
+ * Reverse lookup for AuthenticationResult
14
+ */
15
+ export const AuthenticationResultByNumber = Object
16
+ .fromEntries(Object.entries(AuthenticationResult).map(([k, v]) => [v, k]));
@@ -2,7 +2,7 @@ import { PacketType } from "./PacketType.js";
2
2
  import { BitMask } from "./BitMask.js";
3
3
  import { booleanFlag, Decoder, DecoderError } from "./decoder.js";
4
4
  import { AuthenticationResultByNumber } from "./AuthenticationResult.js";
5
- export default {
5
+ export const connack = {
6
6
  encode(packet) {
7
7
  const flags = 0;
8
8
  return {
@@ -8,7 +8,7 @@ function invalidProtocolName(version, name) {
8
8
  }
9
9
  return false;
10
10
  }
11
- export default {
11
+ export const connect = {
12
12
  encode(packet) {
13
13
  const flags = 0;
14
14
  const protocolLevel = 4;
@@ -1,6 +1,6 @@
1
1
  import { PacketType } from "./PacketType.js";
2
2
  import { isEmptyBuf } from "./decoder.js";
3
- export default {
3
+ export const disconnect = {
4
4
  encode(_packet) {
5
5
  const flags = 0;
6
6
  return { flags, bytes: [] };
@@ -1,21 +1,26 @@
1
+ /**
2
+ * This module provides the encoding and decoding of MQTT packets
3
+ * it is used by the server and client modules
4
+ * @module
5
+ */
1
6
  import { PacketNameByType, PacketType } from "./PacketType.js";
2
7
  import { invalidTopic, invalidTopicFilter, invalidUTF8 } from "./validators.js";
3
8
  import { decodeLength, encodeLength } from "./length.js";
4
- import connect from "./connect.js";
5
- import connack from "./connack.js";
9
+ import { connect } from "./connect.js";
10
+ import { connack } from "./connack.js";
6
11
  import { AuthenticationResult, AuthenticationResultByNumber, } from "./AuthenticationResult.js";
7
- import publish from "./publish.js";
8
- import puback from "./puback.js";
9
- import pubrec from "./pubrec.js";
10
- import pubrel from "./pubrel.js";
11
- import pubcomp from "./pubcomp.js";
12
- import subscribe from "./subscribe.js";
13
- import suback from "./suback.js";
14
- import unsubscribe from "./unsubscribe.js";
15
- import unsuback from "./unsuback.js";
16
- import pingreq from "./pingreq.js";
17
- import pingres from "./pingres.js";
18
- import disconnect from "./disconnect.js";
12
+ import { publish } from "./publish.js";
13
+ import { puback } from "./puback.js";
14
+ import { pubrec } from "./pubrec.js";
15
+ import { pubrel } from "./pubrel.js";
16
+ import { pubcomp } from "./pubcomp.js";
17
+ import { subscribe } from "./subscribe.js";
18
+ import { suback } from "./suback.js";
19
+ import { unsubscribe } from "./unsubscribe.js";
20
+ import { unsuback } from "./unsuback.js";
21
+ import { pingreq } from "./pingreq.js";
22
+ import { pingres } from "./pingres.js";
23
+ import { disconnect } from "./disconnect.js";
19
24
  import { DecoderError } from "./decoder.js";
20
25
  export { AuthenticationResult, AuthenticationResultByNumber, decodeLength, encodeLength, invalidTopic, invalidTopicFilter, invalidUTF8, PacketNameByType, PacketType, };
21
26
  export const packetsByType = [
@@ -1,6 +1,6 @@
1
1
  import { PacketType } from "./PacketType.js";
2
2
  import { hasEmptyFlags, isEmptyBuf } from "./decoder.js";
3
- export default {
3
+ export const pingreq = {
4
4
  encode(_packet) {
5
5
  const flags = 0;
6
6
  return { flags, bytes: [] };
@@ -1,6 +1,6 @@
1
1
  import { PacketType } from "./PacketType.js";
2
2
  import { isEmptyBuf } from "./decoder.js";
3
- export default {
3
+ export const pingres = {
4
4
  encode(_packet) {
5
5
  const flags = 0;
6
6
  return { flags, bytes: [] };
@@ -1,7 +1,7 @@
1
1
  import { PacketType } from "./PacketType.js";
2
2
  import { Decoder } from "./decoder.js";
3
3
  import { Encoder } from "./encoder.js";
4
- export default {
4
+ export const puback = {
5
5
  encode(packet) {
6
6
  const flags = 0;
7
7
  const encoder = new Encoder();
@@ -1,7 +1,7 @@
1
1
  import { PacketType } from "./PacketType.js";
2
2
  import { Decoder } from "./decoder.js";
3
3
  import { Encoder } from "./encoder.js";
4
- export default {
4
+ export const pubcomp = {
5
5
  encode(packet) {
6
6
  const flags = 0;
7
7
  const encoder = new Encoder();
@@ -2,7 +2,7 @@ import { PacketType } from "./PacketType.js";
2
2
  import { BitMask } from "./BitMask.js";
3
3
  import { Encoder, EncoderError } from "./encoder.js";
4
4
  import { booleanFlag, Decoder, DecoderError } from "./decoder.js";
5
- export default {
5
+ export const publish = {
6
6
  encode(packet) {
7
7
  const qos = packet.qos || 0;
8
8
  const flags = (packet.dup ? BitMask.bit3 : 0) +
@@ -1,7 +1,7 @@
1
1
  import { PacketType } from "./PacketType.js";
2
2
  import { Decoder } from "./decoder.js";
3
3
  import { Encoder } from "./encoder.js";
4
- export default {
4
+ export const pubrec = {
5
5
  encode(packet) {
6
6
  const flags = 0;
7
7
  const encoder = new Encoder();
@@ -1,7 +1,7 @@
1
1
  import { PacketType } from "./PacketType.js";
2
2
  import { Decoder } from "./decoder.js";
3
3
  import { Encoder } from "./encoder.js";
4
- export default {
4
+ export const pubrel = {
5
5
  encode(packet) {
6
6
  const flags = 0;
7
7
  const encoder = new Encoder();
@@ -1,7 +1,7 @@
1
1
  import { PacketType } from "./PacketType.js";
2
2
  import { Encoder } from "./encoder.js";
3
3
  import { Decoder } from "./decoder.js";
4
- export default {
4
+ export const suback = {
5
5
  encode(packet) {
6
6
  const flags = 0;
7
7
  const encoder = new Encoder();
@@ -2,7 +2,7 @@ import { PacketType } from "./PacketType.js";
2
2
  import { BitMask } from "./BitMask.js";
3
3
  import { Encoder } from "./encoder.js";
4
4
  import { booleanFlag, Decoder, DecoderError } from "./decoder.js";
5
- export default {
5
+ export const subscribe = {
6
6
  encode(packet) {
7
7
  // Bits 3,2,1 and 0 of the fixed header of the SUBSCRIBE Control Packet are reserved and
8
8
  // MUST be set to 0,0,1 and 0 respectively. The Server MUST treat any other value as
@@ -1,7 +1,7 @@
1
1
  import { PacketType } from "./PacketType.js";
2
2
  import { Decoder } from "./decoder.js";
3
3
  import { Encoder } from "./encoder.js";
4
- export default {
4
+ export const unsuback = {
5
5
  encode(packet) {
6
6
  const flags = 0;
7
7
  const encoder = new Encoder();
@@ -2,7 +2,7 @@ import { PacketType } from "./PacketType.js";
2
2
  import { BitMask } from "./BitMask.js";
3
3
  import { Encoder } from "./encoder.js";
4
4
  import { booleanFlag, Decoder, DecoderError } from "./decoder.js";
5
- export default {
5
+ export const unsubscribe = {
6
6
  encode(packet) {
7
7
  // Bits 3,2,1 and 0 of the fixed header of the UNSUBSCRIBE Control Packet are reserved and
8
8
  // MUST be set to 0,0,1 and 0 respectively. The Server MUST treat any other value as
@@ -1,11 +1,23 @@
1
- // this a NodeJS specific implementation of TCP client sockets
2
- // it extends the platform agnostic Client class
1
+ /*
2
+ * this a NodeJS specific implementation of TCP client sockets
3
+ * it extends the platform agnostic Client class
4
+ * @module
5
+ */
3
6
  import { Client } from "../client/client.js";
4
7
  import { logger } from "../client/deps.js";
5
8
  import { readFile } from "node:fs/promises";
6
9
  import { createConnection } from "node:net";
7
10
  import * as tls from "node:tls";
8
11
  import { wrapNodeSocket } from "./wrapNodeSocket.js";
12
+ /**
13
+ * @function getFileData
14
+ * @param filename
15
+ * @returns Promise
16
+ *
17
+ * Fetches data from a file and returns it as a string
18
+ * @example
19
+ * const data = await getFileData("data.txt");
20
+ */
9
21
  export async function getFileData(filename) {
10
22
  if (!filename) {
11
23
  return;
@@ -16,6 +28,11 @@ export async function getFileData(filename) {
16
28
  }
17
29
  return data;
18
30
  }
31
+ /*
32
+ * TCPclient extends the Client class to provide TCP based clients
33
+ * it is used by the MQTTclient to connect to the broker
34
+ * see mqtt.ts in the /bin folder as an example
35
+ */
19
36
  export class TcpClient extends Client {
20
37
  connectMQTT(hostname, port = 1883) {
21
38
  logger.debug({ hostname, port });
@@ -1,6 +1,9 @@
1
1
  import { createServer } from "node:net";
2
2
  import { MqttServer } from "../server/mod.js";
3
3
  import { wrapNodeSocket } from "./wrapNodeSocket.js";
4
+ /*
5
+ * TCP server that wraps a MqttServer, see demoServer.ts in the /bin folder
6
+ */
4
7
  export class TcpServer {
5
8
  mqttServer;
6
9
  server;
@@ -32,6 +32,20 @@ function parseKeyValue(arg, alias) {
32
32
  }
33
33
  return ["_", arg];
34
34
  }
35
+ /**
36
+ * a parser for command line arguments
37
+ * @param args the arguments to parse
38
+ * @param opts the options for the parser
39
+ * @returns the parsed arguments
40
+ *
41
+ * @example
42
+ * const args = parseArgs(args, {
43
+ * alias: { h: "help" },
44
+ * boolean: ["help"],
45
+ * string: ["port"],
46
+ * default: { port: 3000 },
47
+ * });
48
+ */
35
49
  export function parseArgs(args, opts = {}) {
36
50
  const { alias = {}, boolean = [], string = [], default: defaults = {} } = opts;
37
51
  const result = { ...defaults };
@@ -5,6 +5,7 @@ export class AssertionError extends Error {
5
5
  this.name = "AssertionError";
6
6
  }
7
7
  }
8
+ /** assert(expr, msg) throws AssertionError if expr is falsy. */
8
9
  export function assert(expr, msg = "") {
9
10
  if (!expr) {
10
11
  throw new AssertionError(msg);
@@ -1,4 +1,15 @@
1
1
  import { nextTick } from "./nextTick.js";
2
+ /**
3
+ * An Async Queue is a queue that can be used to push items to it and then wait
4
+ * for them to be consumed.
5
+ *
6
+ * @example
7
+ * const queue = new AsyncQueue<string>();
8
+ * queue.push("hello");
9
+ * for await (const item of queue) {
10
+ * console.log(item); // "hello"
11
+ * }
12
+ */
2
13
  export class AsyncQueue {
3
14
  queue = [];
4
15
  maxQueueLength = Infinity;
@@ -1,3 +1,6 @@
1
+ /**
2
+ * Create a promise that can be resolved/rejected later
3
+ */
1
4
  export class Deferred {
2
5
  promise;
3
6
  resolve;
@@ -1,3 +1,6 @@
1
+ /**
2
+ * The available log levels
3
+ */
1
4
  export const LogLevel = {
2
5
  error: 0,
3
6
  warn: 1,
@@ -5,6 +8,19 @@ export const LogLevel = {
5
8
  verbose: 3,
6
9
  debug: 4,
7
10
  };
11
+ /**
12
+ * Simple logger class
13
+ * @example
14
+ * ```ts
15
+ * const logger = new Logger();
16
+ * logger.level(LogLevel.debug);
17
+ * logger.debug("debug message");
18
+ * logger.verbose("verbose message");
19
+ * logger.info("info message");
20
+ * logger.warn("warn message");
21
+ * logger.error("error message");
22
+ * ```
23
+ */
8
24
  export class Logger {
9
25
  defaultError = console.error;
10
26
  defaultWarn = console.warn;
@@ -26,4 +42,17 @@ export class Logger {
26
42
  this.debug = logLevel > 3 ? this.defaultDebug : this.noop;
27
43
  }
28
44
  }
45
+ /**
46
+ * logger provides a singleton logger instance
47
+ * @example
48
+ * ```ts
49
+ * import { logger } from "./utils/logger.ts";
50
+ * logger.level(LogLevel.debug);
51
+ * logger.debug("debug message");
52
+ * logger.verbose("verbose message");
53
+ * logger.info("info message");
54
+ * logger.warn("warn message");
55
+ * logger.error("error message");
56
+ * ```
57
+ */
29
58
  export const logger = new Logger();
package/dist/utils/mod.js CHANGED
@@ -1,3 +1,7 @@
1
+ /**
2
+ * This module provides various utilities
3
+ * @module
4
+ */
1
5
  export { assert } from "./assert.js";
2
6
  export { Deferred } from "./deferred.js";
3
7
  export { Logger, logger, LogLevel } from "./logger.js";
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @returns a promise that resolves on the nextTick of the event loop
3
+ */
1
4
  export function nextTick() {
2
5
  return new Promise((resolve) => setTimeout(resolve, 0));
3
6
  }
@@ -1,4 +1,7 @@
1
1
  import { argv } from "node:process";
2
+ /**
3
+ * @returns an array of command line arguments
4
+ */
2
5
  export function getArgs() {
3
6
  return argv.slice(2);
4
7
  }
@@ -1,7 +1,6 @@
1
1
  /**
2
2
  * Possible MQTT authentication results
3
3
  */
4
-
5
4
  export const AuthenticationResult = {
6
5
  ok: 0,
7
6
  unacceptableProtocol: 1,
@@ -11,6 +10,10 @@ export const AuthenticationResult = {
11
10
  notAuthorized: 5,
12
11
  } as const;
13
12
 
14
- export const AuthenticationResultByNumber = Object.fromEntries(
15
- Object.entries(AuthenticationResult).map(([k, v]) => [v, k]),
16
- );
13
+ /**
14
+ * Reverse lookup for AuthenticationResult
15
+ */
16
+ export const AuthenticationResultByNumber: Record<number, string> = Object
17
+ .fromEntries(
18
+ Object.entries(AuthenticationResult).map(([k, v]) => [v, k]),
19
+ );
@@ -16,6 +16,6 @@ export const PacketType = {
16
16
  disconnect: 14,
17
17
  } as const;
18
18
 
19
- export const PacketNameByType = Object.fromEntries(
19
+ export const PacketNameByType: Record<number, string> = Object.fromEntries(
20
20
  Object.entries(PacketType).map(([k, v]) => [v, k]),
21
21
  );
@@ -4,13 +4,20 @@ import { BitMask } from "./BitMask.ts";
4
4
  import { booleanFlag, Decoder, DecoderError } from "./decoder.ts";
5
5
  import { AuthenticationResultByNumber } from "./AuthenticationResult.ts";
6
6
 
7
+ /**
8
+ * ConnackPacket is sent from the server to the client in response to a connect packet.
9
+ * It indicates that the connect is accepted.
10
+ */
7
11
  export type ConnackPacket = {
8
12
  type: TPacketType;
9
13
  sessionPresent: boolean;
10
14
  returnCode: TAuthenticationResult;
11
15
  };
12
16
 
13
- export default {
17
+ export const connack: {
18
+ encode(packet: ConnackPacket): { flags: number; bytes: number[] };
19
+ decode(buffer: Uint8Array, _flags: number): ConnackPacket | undefined;
20
+ } = {
14
21
  encode(packet: ConnackPacket): { flags: number; bytes: number[] } {
15
22
  const flags = 0;
16
23
  return {
@@ -9,6 +9,9 @@ import {
9
9
  hasEmptyFlags,
10
10
  } from "./decoder.ts";
11
11
 
12
+ /**
13
+ * ConnectPacket is sent from the client to the server to initiate a connection.
14
+ */
12
15
  export type ConnectPacket = {
13
16
  type: TPacketType;
14
17
  protocolName?: string;
@@ -33,8 +36,11 @@ function invalidProtocolName(version: number, name: string): boolean {
33
36
  return false;
34
37
  }
35
38
 
36
- export default {
37
- encode(packet: ConnectPacket) {
39
+ export const connect: {
40
+ encode(packet: ConnectPacket): { flags: number; bytes: number[] };
41
+ decode(buffer: Uint8Array, flags: number): ConnectPacket;
42
+ } = {
43
+ encode(packet: ConnectPacket): { flags: number; bytes: number[] } {
38
44
  const flags = 0;
39
45
 
40
46
  const protocolLevel = 4;
@@ -2,12 +2,19 @@ import { PacketType } from "./PacketType.ts";
2
2
  import { isEmptyBuf } from "./decoder.ts";
3
3
  import type { TPacketType } from "./types.ts";
4
4
 
5
+ /**
6
+ * DisconnectPacket is the final control packet sent from the client to the server.
7
+ * It indicates that the client is disconnecting cleanly.
8
+ */
5
9
  export type DisconnectPacket = {
6
10
  type: TPacketType;
7
11
  };
8
12
 
9
- export default {
10
- encode(_packet: DisconnectPacket) {
13
+ export const disconnect: {
14
+ encode(_packet: DisconnectPacket): { flags: number; bytes: number[] };
15
+ decode(buffer: Uint8Array, _flags: number): DisconnectPacket;
16
+ } = {
17
+ encode(_packet: DisconnectPacket): { flags: number; bytes: number[] } {
11
18
  const flags = 0;
12
19
  return { flags, bytes: [] };
13
20
  },