@seriousme/opifex 1.6.1 → 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 (44) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/deno/tcpClient.ts +19 -2
  3. package/deno/tcpServer.ts +9 -2
  4. package/deno.json +1 -1
  5. package/dist/mqttPacket/AuthenticationResult.js +3 -0
  6. package/dist/mqttPacket/mod.js +5 -0
  7. package/dist/node/tcpClient.js +19 -2
  8. package/dist/node/tcpServer.js +3 -0
  9. package/dist/utils/args.js +14 -0
  10. package/dist/utils/assert.js +1 -0
  11. package/dist/utils/asyncQueue.js +11 -0
  12. package/dist/utils/deferred.js +3 -0
  13. package/dist/utils/logger.js +29 -0
  14. package/dist/utils/mod.js +4 -0
  15. package/dist/utils/nextTick.js +3 -0
  16. package/dist/utils/platform.js +3 -0
  17. package/mqttPacket/AuthenticationResult.ts +3 -1
  18. package/mqttPacket/connack.ts +4 -0
  19. package/mqttPacket/connect.ts +3 -0
  20. package/mqttPacket/disconnect.ts +4 -0
  21. package/mqttPacket/mod.ts +9 -0
  22. package/mqttPacket/pingreq.ts +3 -0
  23. package/mqttPacket/pingres.ts +4 -0
  24. package/mqttPacket/puback.ts +3 -0
  25. package/mqttPacket/pubcomp.ts +4 -0
  26. package/mqttPacket/publish.ts +4 -0
  27. package/mqttPacket/pubrec.ts +4 -0
  28. package/mqttPacket/pubrel.ts +4 -0
  29. package/mqttPacket/suback.ts +4 -0
  30. package/mqttPacket/subscribe.ts +6 -0
  31. package/mqttPacket/types.ts +32 -1
  32. package/mqttPacket/unsuback.ts +4 -0
  33. package/mqttPacket/unsubscribe.ts +3 -0
  34. package/node/tcpClient.ts +20 -2
  35. package/node/tcpServer.ts +8 -2
  36. package/package.json +1 -1
  37. package/utils/args.ts +17 -0
  38. package/utils/assert.ts +1 -0
  39. package/utils/asyncQueue.ts +11 -0
  40. package/utils/deferred.ts +4 -0
  41. package/utils/logger.ts +29 -0
  42. package/utils/mod.ts +4 -0
  43. package/utils/nextTick.ts +3 -0
  44. package/utils/platform.ts +4 -0
package/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@
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
+
7
13
  ## [v1.6.1] 18-01-2025
8
14
 
9
15
  ### Changed
package/deno/tcpClient.ts CHANGED
@@ -1,9 +1,21 @@
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
 
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
+ */
7
19
  export async function getFileData(
8
20
  filename: string | undefined,
9
21
  ): Promise<string | undefined> {
@@ -17,6 +29,11 @@ export async function getFileData(
17
29
  return data;
18
30
  }
19
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
+ */
20
37
  export class TcpClient extends Client {
21
38
  protected async connectMQTT(
22
39
  hostname: string,
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;
package/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seriousme/opifex",
3
- "version": "1.6.1",
3
+ "version": "1.6.2",
4
4
  "exports": {
5
5
  "./server": "./server/mod.ts",
6
6
  "./client": "./client/mod.ts",
@@ -9,5 +9,8 @@ export const AuthenticationResult = {
9
9
  badUsernameOrPassword: 4,
10
10
  notAuthorized: 5,
11
11
  };
12
+ /**
13
+ * Reverse lookup for AuthenticationResult
14
+ */
12
15
  export const AuthenticationResultByNumber = Object
13
16
  .fromEntries(Object.entries(AuthenticationResult).map(([k, v]) => [v, k]));
@@ -1,3 +1,8 @@
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";
@@ -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,9 @@ export const AuthenticationResult = {
11
10
  notAuthorized: 5,
12
11
  } as const;
13
12
 
13
+ /**
14
+ * Reverse lookup for AuthenticationResult
15
+ */
14
16
  export const AuthenticationResultByNumber: Record<number, string> = Object
15
17
  .fromEntries(
16
18
  Object.entries(AuthenticationResult).map(([k, v]) => [v, k]),
@@ -4,6 +4,10 @@ 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;
@@ -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;
@@ -2,6 +2,10 @@ 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
  };
package/mqttPacket/mod.ts CHANGED
@@ -1,3 +1,9 @@
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
+ */
6
+
1
7
  import type {
2
8
  ClientId,
3
9
  Dup,
@@ -33,6 +39,9 @@ import { pingres, type PingresPacket } from "./pingres.ts";
33
39
  import { disconnect, type DisconnectPacket } from "./disconnect.ts";
34
40
  import { DecoderError } from "./decoder.ts";
35
41
 
42
+ /**
43
+ * this can be any possible MQTT packet
44
+ */
36
45
  export type AnyPacket =
37
46
  | ConnectPacket
38
47
  | ConnackPacket
@@ -2,6 +2,9 @@ import { PacketType } from "./PacketType.ts";
2
2
  import { hasEmptyFlags, isEmptyBuf } from "./decoder.ts";
3
3
  import type { TPacketType } from "./types.ts";
4
4
 
5
+ /**
6
+ * PingreqPacket is a packet that is sent to the server to keep the connection alive
7
+ */
5
8
  export type PingreqPacket = {
6
9
  type: TPacketType;
7
10
  };
@@ -2,6 +2,10 @@ import { PacketType } from "./PacketType.ts";
2
2
  import { isEmptyBuf } from "./decoder.ts";
3
3
  import type { TPacketType } from "./types.ts";
4
4
 
5
+ /**
6
+ * PingresPacket is an empty packet that is sent by the server in response to a PingreqPacket.
7
+ * It is used to indicate that the client is still connected to the server.
8
+ */
5
9
  export type PingresPacket = {
6
10
  type: TPacketType;
7
11
  };
@@ -3,6 +3,9 @@ import { PacketType } from "./PacketType.ts";
3
3
  import { Decoder } from "./decoder.ts";
4
4
  import { Encoder } from "./encoder.ts";
5
5
 
6
+ /**
7
+ * PubackPacket is sent to indicate publish complete (QoS 1)
8
+ */
6
9
  export type PubackPacket = {
7
10
  type: TPacketType;
8
11
  id: PacketId;
@@ -3,6 +3,10 @@ import { PacketType } from "./PacketType.ts";
3
3
  import { Decoder } from "./decoder.ts";
4
4
  import { Encoder } from "./encoder.ts";
5
5
 
6
+ /**
7
+ * PubcompPacket is sent to indicate publish complete
8
+ * It is the fourth packet of the QoS 2 protocol exchange.
9
+ */
6
10
  export type PubcompPacket = {
7
11
  type: TPacketType;
8
12
  id: PacketId;
@@ -4,6 +4,10 @@ import { BitMask } from "./BitMask.ts";
4
4
  import { Encoder, EncoderError } from "./encoder.ts";
5
5
  import { booleanFlag, Decoder, DecoderError } from "./decoder.ts";
6
6
 
7
+ /**
8
+ * PublishPacket is used to send data from client to server and
9
+ * from server to subscribers
10
+ */
7
11
  export type PublishPacket = {
8
12
  type: TPacketType;
9
13
  topic: Topic;
@@ -3,6 +3,10 @@ import { PacketType } from "./PacketType.ts";
3
3
  import { Decoder } from "./decoder.ts";
4
4
  import { Encoder } from "./encoder.ts";
5
5
 
6
+ /**
7
+ * PubrecPacket indicates publish received
8
+ * It is the second packet of the QoS 2 protocol exchange.
9
+ */
6
10
  export type PubrecPacket = {
7
11
  type: TPacketType;
8
12
  id: PacketId;
@@ -3,6 +3,10 @@ import { PacketType } from "./PacketType.ts";
3
3
  import { Decoder } from "./decoder.ts";
4
4
  import { Encoder } from "./encoder.ts";
5
5
 
6
+ /**
7
+ * PubrelPacket is the packet sent by the Server to the Client in response to a
8
+ * PubrecPacket. It is the third packet of the QoS 2 protocol exchange.
9
+ */
6
10
  export type PubrelPacket = {
7
11
  type: TPacketType;
8
12
  id: PacketId;
@@ -3,6 +3,10 @@ import { PacketType } from "./PacketType.ts";
3
3
  import { Encoder } from "./encoder.ts";
4
4
  import { Decoder } from "./decoder.ts";
5
5
 
6
+ /**
7
+ * SubackPacket is sent by the server to the client to confirm receipt and
8
+ * processing of a SubscribePacket.
9
+ */
6
10
  export type SubackPacket = {
7
11
  type: TPacketType;
8
12
  id: PacketId;
@@ -4,12 +4,18 @@ import { BitMask } from "./BitMask.ts";
4
4
  import { Encoder } from "./encoder.ts";
5
5
  import { booleanFlag, Decoder, DecoderError } from "./decoder.ts";
6
6
 
7
+ /**
8
+ * SubscribePacket is sent from client to server to subscribe to topics
9
+ */
7
10
  export type SubscribePacket = {
8
11
  type: TPacketType;
9
12
  id: PacketId;
10
13
  subscriptions: Subscription[];
11
14
  };
12
15
 
16
+ /**
17
+ * The topic to subscribe to and the associated QoS
18
+ */
13
19
  export type Subscription = {
14
20
  topicFilter: TopicFilter;
15
21
  qos: QoS;
@@ -2,26 +2,57 @@ import type { BitMask } from "./BitMask.ts";
2
2
  import type { PacketType } from "./PacketType.ts";
3
3
  import type { AuthenticationResult } from "./AuthenticationResult.ts";
4
4
 
5
+ /**
6
+ * Type to limit bit mask to valid values
7
+ */
5
8
  export type TBitMask = typeof BitMask[keyof typeof BitMask];
9
+ /**
10
+ * Type to limit packet type to valid values
11
+ */
6
12
  export type TPacketType = typeof PacketType[keyof typeof PacketType];
13
+
14
+ /**
15
+ * Type to limit authentication result to valid values
16
+ */
7
17
  export type TAuthenticationResult =
8
18
  typeof AuthenticationResult[keyof typeof AuthenticationResult];
9
19
 
20
+ /**
21
+ * Quality of Service level
22
+ */
10
23
  export type QoS = 0 | 1 | 2;
11
24
 
25
+ /**
26
+ * Packet payload
27
+ */
12
28
  export type Payload = Uint8Array;
13
29
 
14
30
  /**
15
- * The MQTT topic to publish or subscribe to
31
+ * The MQTT topic to publish to
16
32
  */
17
33
  export type Topic = string;
18
34
 
35
+ /**
36
+ * The MQTT topic to subscribe/unsubscribe to
37
+ */
19
38
  export type TopicFilter = string;
20
39
 
40
+ /**
41
+ * Duplicate delivery flag
42
+ */
21
43
  export type Dup = boolean;
22
44
 
45
+ /**
46
+ * Packet identifier, unique per client session
47
+ */
23
48
  export type PacketId = number;
24
49
 
50
+ /**
51
+ * Return codes on to describe result of subscribe operation
52
+ */
25
53
  export type ReturnCodes = number[];
26
54
 
55
+ /**
56
+ * Client identifier that uniquely identifies a client
57
+ */
27
58
  export type ClientId = string;
@@ -3,6 +3,10 @@ import { PacketType } from "./PacketType.ts";
3
3
  import { Decoder } from "./decoder.ts";
4
4
  import { Encoder } from "./encoder.ts";
5
5
 
6
+ /**
7
+ * UnsubackPacket is sent by the server to the client to confirm receipt
8
+ * of an UnsubscribePacket.
9
+ */
6
10
  export type UnsubackPacket = {
7
11
  type: TPacketType;
8
12
  id: PacketId;
@@ -4,6 +4,9 @@ import { BitMask } from "./BitMask.ts";
4
4
  import { Encoder } from "./encoder.ts";
5
5
  import { booleanFlag, Decoder, DecoderError } from "./decoder.ts";
6
6
 
7
+ /**
8
+ * UnsubscribePacket is sent from client to server to unsubscribe from topics
9
+ */
7
10
  export type UnsubscribePacket = {
8
11
  type: TPacketType;
9
12
  id: PacketId;
package/node/tcpClient.ts CHANGED
@@ -1,5 +1,8 @@
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.ts";
4
7
  import { logger } from "../client/deps.ts";
5
8
  import { readFile } from "node:fs/promises";
@@ -8,6 +11,15 @@ import * as tls from "node:tls";
8
11
  import type { SockConn } from "../socket/socket.ts";
9
12
  import { wrapNodeSocket } from "./wrapNodeSocket.ts";
10
13
 
14
+ /**
15
+ * @function getFileData
16
+ * @param filename
17
+ * @returns Promise
18
+ *
19
+ * Fetches data from a file and returns it as a string
20
+ * @example
21
+ * const data = await getFileData("data.txt");
22
+ */
11
23
  export async function getFileData(filename: string | undefined) {
12
24
  if (!filename) {
13
25
  return;
@@ -19,6 +31,12 @@ export async function getFileData(filename: string | undefined) {
19
31
  return data;
20
32
  }
21
33
 
34
+ /*
35
+ * TCPclient extends the Client class to provide TCP based clients
36
+ * it is used by the MQTTclient to connect to the broker
37
+ * see mqtt.ts in the /bin folder as an example
38
+ */
39
+
22
40
  export class TcpClient extends Client {
23
41
  protected connectMQTT(hostname: string, port = 1883): Promise<SockConn> {
24
42
  logger.debug({ hostname, port });
package/node/tcpServer.ts CHANGED
@@ -1,5 +1,8 @@
1
- // This a NodeJS specific implementation of a TCP socket listener
2
- // it uses the platform agnostic MqttServer class
1
+ /*
2
+ * This module provides a NodeJS specific implementation of a TCP socket listener
3
+ * it uses the platform agnostic MqttServer class
4
+ * @module
5
+ */
3
6
  import type { Server } from "node:net";
4
7
  import { createServer } from "node:net";
5
8
  import type { MqttServerOptions } from "../server/mod.ts";
@@ -11,6 +14,9 @@ type ServerOptions = {
11
14
  port?: number;
12
15
  };
13
16
 
17
+ /*
18
+ * TCP server that wraps a MqttServer, see demoServer.ts in the /bin folder
19
+ */
14
20
  export class TcpServer {
15
21
  private mqttServer: MqttServer;
16
22
  private server?: Server;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seriousme/opifex",
3
- "version": "1.6.1",
3
+ "version": "1.6.2",
4
4
  "description": "MQTT client & server for Deno & NodeJS",
5
5
  "type": "module",
6
6
  "scripts": {
package/utils/args.ts CHANGED
@@ -1,3 +1,6 @@
1
+ /**
2
+ * Parsed arguments as key value
3
+ */
1
4
  // deno-lint-ignore no-explicit-any
2
5
  export type Args = Record<string, any>;
3
6
  export type ArgsValue = string | number | boolean;
@@ -54,6 +57,20 @@ function parseKeyValue(
54
57
  return ["_", arg];
55
58
  }
56
59
 
60
+ /**
61
+ * a parser for command line arguments
62
+ * @param args the arguments to parse
63
+ * @param opts the options for the parser
64
+ * @returns the parsed arguments
65
+ *
66
+ * @example
67
+ * const args = parseArgs(args, {
68
+ * alias: { h: "help" },
69
+ * boolean: ["help"],
70
+ * string: ["port"],
71
+ * default: { port: 3000 },
72
+ * });
73
+ */
57
74
  export function parseArgs(args: string[], opts: ArgOpts = {}): Args {
58
75
  const { alias = {}, boolean = [], string = [], default: defaults = {} } =
59
76
  opts;
package/utils/assert.ts CHANGED
@@ -6,6 +6,7 @@ export class AssertionError extends Error {
6
6
  }
7
7
  }
8
8
 
9
+ /** assert(expr, msg) throws AssertionError if expr is falsy. */
9
10
  export function assert(expr: unknown, msg = ""): asserts expr {
10
11
  if (!expr) {
11
12
  throw new AssertionError(msg);
@@ -1,5 +1,16 @@
1
1
  import { nextTick } from "./nextTick.ts";
2
2
 
3
+ /**
4
+ * An Async Queue is a queue that can be used to push items to it and then wait
5
+ * for them to be consumed.
6
+ *
7
+ * @example
8
+ * const queue = new AsyncQueue<string>();
9
+ * queue.push("hello");
10
+ * for await (const item of queue) {
11
+ * console.log(item); // "hello"
12
+ * }
13
+ */
3
14
  export class AsyncQueue<T> {
4
15
  private queue: T[] = [];
5
16
  private maxQueueLength = Infinity;
package/utils/deferred.ts CHANGED
@@ -1,3 +1,7 @@
1
+ /**
2
+ * Create a promise that can be resolved/rejected later
3
+ */
4
+
1
5
  export class Deferred<T> {
2
6
  promise: Promise<T>;
3
7
  resolve!: (val: T) => void;
package/utils/logger.ts CHANGED
@@ -1,3 +1,6 @@
1
+ /**
2
+ * The available log levels
3
+ */
1
4
  export const LogLevel = {
2
5
  error: 0,
3
6
  warn: 1,
@@ -8,6 +11,19 @@ export const LogLevel = {
8
11
 
9
12
  export type TLogLevel = typeof LogLevel[keyof typeof LogLevel];
10
13
 
14
+ /**
15
+ * Simple logger class
16
+ * @example
17
+ * ```ts
18
+ * const logger = new Logger();
19
+ * logger.level(LogLevel.debug);
20
+ * logger.debug("debug message");
21
+ * logger.verbose("verbose message");
22
+ * logger.info("info message");
23
+ * logger.warn("warn message");
24
+ * logger.error("error message");
25
+ * ```
26
+ */
11
27
  export class Logger {
12
28
  private defaultError = console.error;
13
29
  private defaultWarn = console.warn;
@@ -32,4 +48,17 @@ export class Logger {
32
48
  }
33
49
  }
34
50
 
51
+ /**
52
+ * logger provides a singleton logger instance
53
+ * @example
54
+ * ```ts
55
+ * import { logger } from "./utils/logger.ts";
56
+ * logger.level(LogLevel.debug);
57
+ * logger.debug("debug message");
58
+ * logger.verbose("verbose message");
59
+ * logger.info("info message");
60
+ * logger.warn("warn message");
61
+ * logger.error("error message");
62
+ * ```
63
+ */
35
64
  export const logger: Logger = new Logger();
package/utils/mod.ts CHANGED
@@ -1,3 +1,7 @@
1
+ /**
2
+ * This module provides various utilities
3
+ * @module
4
+ */
1
5
  export { assert } from "./assert.ts";
2
6
  export { Deferred } from "./deferred.ts";
3
7
  export { Logger, logger, LogLevel } from "./logger.ts";
package/utils/nextTick.ts CHANGED
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @returns a promise that resolves on the nextTick of the event loop
3
+ */
1
4
  export function nextTick(): Promise<void> {
2
5
  return new Promise((resolve) => setTimeout(resolve, 0));
3
6
  }
package/utils/platform.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  import { argv } from "node:process";
2
+
3
+ /**
4
+ * @returns an array of command line arguments
5
+ */
2
6
  export function getArgs(): string[] {
3
7
  return argv.slice(2);
4
8
  }