@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/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,
@@ -13,26 +19,29 @@ import type {
13
19
  import { PacketNameByType, PacketType } from "./PacketType.ts";
14
20
  import { invalidTopic, invalidTopicFilter, invalidUTF8 } from "./validators.ts";
15
21
  import { decodeLength, encodeLength } from "./length.ts";
16
- import connect, { type ConnectPacket } from "./connect.ts";
17
- import connack, { type ConnackPacket } from "./connack.ts";
22
+ import { connect, type ConnectPacket } from "./connect.ts";
23
+ import { connack, type ConnackPacket } from "./connack.ts";
18
24
  import {
19
25
  AuthenticationResult,
20
26
  AuthenticationResultByNumber,
21
27
  } from "./AuthenticationResult.ts";
22
- import publish, { type PublishPacket } from "./publish.ts";
23
- import puback, { type PubackPacket } from "./puback.ts";
24
- import pubrec, { type PubrecPacket } from "./pubrec.ts";
25
- import pubrel, { type PubrelPacket } from "./pubrel.ts";
26
- import pubcomp, { type PubcompPacket } from "./pubcomp.ts";
27
- import subscribe, { type SubscribePacket } from "./subscribe.ts";
28
- import suback, { type SubackPacket } from "./suback.ts";
29
- import unsubscribe, { type UnsubscribePacket } from "./unsubscribe.ts";
30
- import unsuback, { type UnsubackPacket } from "./unsuback.ts";
31
- import pingreq, { type PingreqPacket } from "./pingreq.ts";
32
- import pingres, { type PingresPacket } from "./pingres.ts";
33
- import disconnect, { type DisconnectPacket } from "./disconnect.ts";
28
+ import { publish, type PublishPacket } from "./publish.ts";
29
+ import { puback, type PubackPacket } from "./puback.ts";
30
+ import { pubrec, type PubrecPacket } from "./pubrec.ts";
31
+ import { pubrel, type PubrelPacket } from "./pubrel.ts";
32
+ import { pubcomp, type PubcompPacket } from "./pubcomp.ts";
33
+ import { subscribe, type SubscribePacket } from "./subscribe.ts";
34
+ import { suback, type SubackPacket } from "./suback.ts";
35
+ import { unsubscribe, type UnsubscribePacket } from "./unsubscribe.ts";
36
+ import { unsuback, type UnsubackPacket } from "./unsuback.ts";
37
+ import { pingreq, type PingreqPacket } from "./pingreq.ts";
38
+ import { pingres, type PingresPacket } from "./pingres.ts";
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
@@ -108,7 +117,7 @@ export const packetsByType = [
108
117
  disconnect, // 14
109
118
  ] as const;
110
119
 
111
- export function encode(packet: AnyPacket) {
120
+ export function encode(packet: AnyPacket): Uint8Array {
112
121
  const packetType: number = packet.type;
113
122
  // deno-lint-ignore no-explicit-any
114
123
  const pkt: any = packet;
@@ -2,12 +2,18 @@ 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
  };
8
11
 
9
- export default {
10
- encode(_packet: PingreqPacket) {
12
+ export const pingreq: {
13
+ encode(_packet: PingreqPacket): { flags: number; bytes: number[] };
14
+ decode(buffer: Uint8Array, flags: number): PingreqPacket;
15
+ } = {
16
+ encode(_packet: PingreqPacket): { flags: number; bytes: number[] } {
11
17
  const flags = 0;
12
18
  return { flags, bytes: [] };
13
19
  },
@@ -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
+ * 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
  };
8
12
 
9
- export default {
10
- encode(_packet: PingresPacket) {
13
+ export const pingres: {
14
+ encode(_packet: PingresPacket): { flags: number; bytes: number[] };
15
+ decode(buffer: Uint8Array): PingresPacket;
16
+ } = {
17
+ encode(_packet: PingresPacket): { flags: number; bytes: number[] } {
11
18
  const flags = 0;
12
19
  return { flags, bytes: [] };
13
20
  },
@@ -3,13 +3,19 @@ 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;
9
12
  };
10
13
 
11
- export default {
12
- encode(packet: PubackPacket) {
14
+ export const puback: {
15
+ encode(packet: PubackPacket): { flags: number; bytes: number[] };
16
+ decode(buffer: Uint8Array): PubackPacket;
17
+ } = {
18
+ encode(packet: PubackPacket): { flags: number; bytes: number[] } {
13
19
  const flags = 0;
14
20
  const encoder = new Encoder();
15
21
  encoder.setInt16(packet.id);
@@ -3,13 +3,20 @@ 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;
9
13
  };
10
14
 
11
- export default {
12
- encode(packet: PubcompPacket) {
15
+ export const pubcomp: {
16
+ encode(packet: PubcompPacket): { flags: number; bytes: number[] };
17
+ decode(buffer: Uint8Array): PubcompPacket;
18
+ } = {
19
+ encode(packet: PubcompPacket): { flags: number; bytes: number[] } {
13
20
  const flags = 0;
14
21
  const encoder = new Encoder();
15
22
  encoder.setInt16(packet.id);
@@ -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;
@@ -14,8 +18,11 @@ export type PublishPacket = {
14
18
  id?: number;
15
19
  };
16
20
 
17
- export default {
18
- encode(packet: PublishPacket) {
21
+ export const publish: {
22
+ encode(packet: PublishPacket): { flags: number; bytes: number[] };
23
+ decode(buffer: Uint8Array, flags: number): PublishPacket;
24
+ } = {
25
+ encode(packet: PublishPacket): { flags: number; bytes: number[] } {
19
26
  const qos = packet.qos || 0;
20
27
 
21
28
  const flags = (packet.dup ? BitMask.bit3 : 0) +
@@ -3,13 +3,20 @@ 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;
9
13
  };
10
14
 
11
- export default {
12
- encode(packet: PubrecPacket) {
15
+ export const pubrec: {
16
+ encode(packet: PubrecPacket): { flags: number; bytes: number[] };
17
+ decode(buffer: Uint8Array): PubrecPacket;
18
+ } = {
19
+ encode(packet: PubrecPacket): { flags: number; bytes: number[] } {
13
20
  const flags = 0;
14
21
  const encoder = new Encoder();
15
22
  encoder.setInt16(packet.id);
@@ -3,13 +3,20 @@ 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;
9
13
  };
10
14
 
11
- export default {
12
- encode(packet: PubrelPacket) {
15
+ export const pubrel: {
16
+ encode(packet: PubrelPacket): { flags: number; bytes: number[] };
17
+ decode(buffer: Uint8Array): PubrelPacket;
18
+ } = {
19
+ encode(packet: PubrelPacket): { flags: number; bytes: number[] } {
13
20
  const flags = 0;
14
21
  const encoder = new Encoder();
15
22
  encoder.setInt16(packet.id);
@@ -3,14 +3,21 @@ 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;
9
13
  returnCodes: ReturnCodes;
10
14
  };
11
15
 
12
- export default {
13
- encode(packet: SubackPacket) {
16
+ export const suback: {
17
+ encode(packet: SubackPacket): { flags: number; bytes: number[] };
18
+ decode(buffer: Uint8Array): SubackPacket;
19
+ } = {
20
+ encode(packet: SubackPacket): { flags: number; bytes: number[] } {
14
21
  const flags = 0;
15
22
  const encoder = new Encoder();
16
23
  encoder.setInt16(packet.id);
@@ -4,19 +4,28 @@ 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;
16
22
  };
17
23
 
18
- export default {
19
- encode(packet: SubscribePacket) {
24
+ export const subscribe: {
25
+ encode(packet: SubscribePacket): { flags: number; bytes: number[] };
26
+ decode(buffer: Uint8Array, flags: number): SubscribePacket;
27
+ } = {
28
+ encode(packet: SubscribePacket): { flags: number; bytes: number[] } {
20
29
  // Bits 3,2,1 and 0 of the fixed header of the SUBSCRIBE Control Packet are reserved and
21
30
  // MUST be set to 0,0,1 and 0 respectively. The Server MUST treat any other value as
22
31
  // malformed and close the Network Connection [MQTT-3.8.1-1].
@@ -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,13 +3,20 @@ 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;
9
13
  };
10
14
 
11
- export default {
12
- encode(packet: UnsubackPacket) {
15
+ export const unsuback: {
16
+ encode(packet: UnsubackPacket): { flags: number; bytes: number[] };
17
+ decode(buffer: Uint8Array): UnsubackPacket;
18
+ } = {
19
+ encode(packet: UnsubackPacket): { flags: number; bytes: number[] } {
13
20
  const flags = 0;
14
21
  const encoder = new Encoder();
15
22
  encoder.setInt16(packet.id);
@@ -4,14 +4,20 @@ 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;
10
13
  topicFilters: TopicFilter[];
11
14
  };
12
15
 
13
- export default {
14
- encode(packet: UnsubscribePacket) {
16
+ export const unsubscribe: {
17
+ encode(packet: UnsubscribePacket): { flags: number; bytes: number[] };
18
+ decode(buffer: Uint8Array, flags: number): UnsubscribePacket;
19
+ } = {
20
+ encode(packet: UnsubscribePacket): { flags: number; bytes: number[] } {
15
21
  // Bits 3,2,1 and 0 of the fixed header of the UNSUBSCRIBE Control Packet are reserved and
16
22
  // MUST be set to 0,0,1 and 0 respectively. The Server MUST treat any other value as
17
23
  // malformed and close the Network Connection [MQTT-3.10.1-1].
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.0",
3
+ "version": "1.6.2",
4
4
  "description": "MQTT client & server for Deno & NodeJS",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -25,7 +25,7 @@
25
25
  "./server": "./dist/server/mod.js",
26
26
  "./client": "./dist/client/mod.js",
27
27
  "./mqttPacket": "./dist/mqttPacket/mod.js",
28
- "./utils": "./dist/utils/mod.js,",
28
+ "./utils": "./dist/utils/mod.js",
29
29
  "./tcpClient": "./dist/node/tcpClient.js",
30
30
  "./tcpServer": "./dist/node/tcpServer.js"
31
31
  },
@@ -34,7 +34,7 @@
34
34
  "./server": "./server/mod.ts",
35
35
  "./client": "./client/mod.ts",
36
36
  "./mqttPacket": "./mqttPacket/mod.ts",
37
- "./utils": "./utils/mod.ts,",
37
+ "./utils": "./utils/mod.ts",
38
38
  "./tcpClient": "./deno/tcpClient.ts",
39
39
  "./tcpServer": "./deno/tcpServer.ts"
40
40
  },
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,7 +1,12 @@
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";
4
8
  export { AsyncQueue } from "./asyncQueue.ts";
5
9
  export { nextTick } from "./nextTick.ts";
6
10
  export { parseArgs } from "./args.ts";
11
+ export type { Args } from "./args.ts";
7
12
  export { getArgs } from "./platform.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
- export function getArgs() {
2
+
3
+ /**
4
+ * @returns an array of command line arguments
5
+ */
6
+ export function getArgs(): string[] {
3
7
  return argv.slice(2);
4
8
  }