@seriousme/opifex 1.6.3 → 1.7.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 (91) hide show
  1. package/package.json +12 -2
  2. package/.github/dependabot.yml +0 -7
  3. package/.github/workflows/closeStale.yml +0 -15
  4. package/.github/workflows/codeql-analysis.yml +0 -71
  5. package/.github/workflows/deno-ci.yml +0 -16
  6. package/.github/workflows/nodejs-ci.yml +0 -23
  7. package/.github/workflows/publish.yml +0 -16
  8. package/.github/workflows/update-license-year.sh +0 -14
  9. package/.github/workflows/update-license-year.yml +0 -13
  10. package/CHANGELOG.md +0 -80
  11. package/deno.json +0 -16
  12. package/deno.lock +0 -34
  13. package/dist/bin/conformanceServer.js +0 -97
  14. package/dist/bin/demoServer.js +0 -96
  15. package/dist/bin/mqtt.js +0 -256
  16. package/dist/client/ConnectionState.js +0 -16
  17. package/dist/client/client.js +0 -187
  18. package/dist/client/context.js +0 -152
  19. package/dist/client/deps.js +0 -5
  20. package/dist/client/handlers/handleConnack.js +0 -24
  21. package/dist/client/handlers/handlePacket.js +0 -57
  22. package/dist/client/handlers/handlePuback.js +0 -13
  23. package/dist/client/handlers/handlePubcomp.js +0 -16
  24. package/dist/client/handlers/handlePublish.js +0 -33
  25. package/dist/client/handlers/handlePubrec.js +0 -26
  26. package/dist/client/handlers/handlePubrel.js +0 -31
  27. package/dist/client/handlers/handleSuback.js +0 -14
  28. package/dist/client/handlers/handleUnsuback.js +0 -15
  29. package/dist/client/mod.js +0 -5
  30. package/dist/client/store/deps.js +0 -1
  31. package/dist/client/store/memoryStore.js +0 -58
  32. package/dist/client/store/store.js +0 -4
  33. package/dist/mqttConn/mqttConn.js +0 -104
  34. package/dist/mqttPacket/AuthenticationResult.js +0 -16
  35. package/dist/mqttPacket/BitMask.js +0 -10
  36. package/dist/mqttPacket/PacketType.js +0 -18
  37. package/dist/mqttPacket/connack.js +0 -27
  38. package/dist/mqttPacket/connect.js +0 -115
  39. package/dist/mqttPacket/decoder.js +0 -89
  40. package/dist/mqttPacket/disconnect.js +0 -14
  41. package/dist/mqttPacket/encoder.js +0 -56
  42. package/dist/mqttPacket/length.js +0 -43
  43. package/dist/mqttPacket/mod.js +0 -76
  44. package/dist/mqttPacket/pingreq.js +0 -15
  45. package/dist/mqttPacket/pingres.js +0 -14
  46. package/dist/mqttPacket/puback.js +0 -20
  47. package/dist/mqttPacket/pubcomp.js +0 -20
  48. package/dist/mqttPacket/publish.js +0 -51
  49. package/dist/mqttPacket/pubrec.js +0 -20
  50. package/dist/mqttPacket/pubrel.js +0 -20
  51. package/dist/mqttPacket/suback.js +0 -29
  52. package/dist/mqttPacket/subscribe.js +0 -52
  53. package/dist/mqttPacket/types.js +0 -1
  54. package/dist/mqttPacket/unsuback.js +0 -20
  55. package/dist/mqttPacket/unsubscribe.js +0 -36
  56. package/dist/mqttPacket/validators.js +0 -15
  57. package/dist/node/tcpClient.js +0 -81
  58. package/dist/node/tcpServer.js +0 -50
  59. package/dist/node/wrapNodeSocket.js +0 -56
  60. package/dist/persistence/deps.js +0 -3
  61. package/dist/persistence/memory/memoryPersistence.js +0 -118
  62. package/dist/persistence/persistence.js +0 -1
  63. package/dist/persistence/store.js +0 -1
  64. package/dist/server/context.js +0 -100
  65. package/dist/server/deps.js +0 -5
  66. package/dist/server/handlers/handleConnect.js +0 -80
  67. package/dist/server/handlers/handleDisconnect.js +0 -9
  68. package/dist/server/handlers/handlePacket.js +0 -65
  69. package/dist/server/handlers/handlePingreq.js +0 -11
  70. package/dist/server/handlers/handlePuback.js +0 -16
  71. package/dist/server/handlers/handlePubcomp.js +0 -14
  72. package/dist/server/handlers/handlePublish.js +0 -54
  73. package/dist/server/handlers/handlePubrec.js +0 -23
  74. package/dist/server/handlers/handlePubrel.js +0 -27
  75. package/dist/server/handlers/handleSubscribe.js +0 -56
  76. package/dist/server/handlers/handleUnsubscribe.js +0 -18
  77. package/dist/server/mod.js +0 -7
  78. package/dist/server/server.js +0 -47
  79. package/dist/socket/socket.js +0 -36
  80. package/dist/timer/timer.js +0 -40
  81. package/dist/trie/trie.js +0 -95
  82. package/dist/tsconfig.tsbuildinfo +0 -1
  83. package/dist/utils/args.js +0 -75
  84. package/dist/utils/assert.js +0 -20
  85. package/dist/utils/asyncQueue.js +0 -69
  86. package/dist/utils/deferred.js +0 -14
  87. package/dist/utils/logger.js +0 -58
  88. package/dist/utils/mod.js +0 -11
  89. package/dist/utils/nextTick.js +0 -18
  90. package/dist/utils/platform.js +0 -7
  91. package/tsconfig.json +0 -22
@@ -1,56 +0,0 @@
1
- const utf8Encoder = new TextEncoder();
2
- import { invalidTopic, invalidTopicFilter } from "./validators.js";
3
- export class EncoderError extends Error {
4
- constructor(message) {
5
- super(message);
6
- this.name = "EncoderError";
7
- }
8
- }
9
- export class Encoder {
10
- buffer;
11
- constructor() {
12
- this.buffer = [];
13
- }
14
- setByte(value) {
15
- this.buffer.push(value);
16
- return this;
17
- }
18
- setInt16(value) {
19
- this.setByte(value >> 8);
20
- this.setByte(value & 0xff);
21
- return this;
22
- }
23
- setByteArray(value) {
24
- if (value.length > 0xffff) {
25
- throw new EncoderError("More than 0xffff bytes of data");
26
- }
27
- this.setInt16(value.length);
28
- this.buffer.push(...value);
29
- return this;
30
- }
31
- setUtf8String(value) {
32
- this.setByteArray(utf8Encoder.encode(value));
33
- return this;
34
- }
35
- setTopic(value) {
36
- if (invalidTopic(value)) {
37
- throw new EncoderError("Topic must contain valid UTF-8 and contain more than 1 byte and no wildcards");
38
- }
39
- this.setUtf8String(value);
40
- return this;
41
- }
42
- setTopicFilter(value) {
43
- if (invalidTopicFilter(value)) {
44
- throw new EncoderError("Topicfilter must contain valid UTF-8 and contain more than 1 byte and valid wildcards");
45
- }
46
- this.setUtf8String(value);
47
- return this;
48
- }
49
- setRemainder(value) {
50
- this.buffer.push(...value);
51
- return this;
52
- }
53
- done() {
54
- return this.buffer;
55
- }
56
- }
@@ -1,43 +0,0 @@
1
- export function encodeLength(n) {
2
- const output = [];
3
- let x = n;
4
- do {
5
- let encodedByte = x % 0x80;
6
- x = Math.floor(x / 0x80);
7
- if (x > 0) {
8
- encodedByte = encodedByte | 0x80;
9
- }
10
- output.push(encodedByte);
11
- } while (x > 0);
12
- if (output.length > 4) {
13
- throw Error("length encoding failed");
14
- }
15
- return output;
16
- }
17
- export function decodeLength(buf, start) {
18
- const decode = getLengthDecoder();
19
- const lenBuf = buf.subarray(start);
20
- for (const byte of lenBuf) {
21
- const { done, length, numLengthBytes } = decode(byte);
22
- if (done) {
23
- return { length, numLengthBytes };
24
- }
25
- }
26
- throw Error("length decoding failed");
27
- }
28
- export function getLengthDecoder() {
29
- let numLengthBytes = 1;
30
- let length = 0;
31
- let multiplier = 1;
32
- return function addLengthByte(encodedByte) {
33
- length += (encodedByte & 0x7f) * multiplier;
34
- multiplier *= 0x80;
35
- if ((encodedByte & 0x80) === 0) {
36
- return { done: true, length, numLengthBytes };
37
- }
38
- if (numLengthBytes++ >= 4) {
39
- throw Error("length decoding failed");
40
- }
41
- return { done: false, length, numLengthBytes };
42
- };
43
- }
@@ -1,76 +0,0 @@
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
- import { PacketNameByType, PacketType } from "./PacketType.js";
7
- import { invalidTopic, invalidTopicFilter, invalidUTF8 } from "./validators.js";
8
- import { decodeLength, encodeLength } from "./length.js";
9
- import { connect } from "./connect.js";
10
- import { connack } from "./connack.js";
11
- import { AuthenticationResult, AuthenticationResultByNumber, } from "./AuthenticationResult.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";
24
- import { DecoderError } from "./decoder.js";
25
- export { AuthenticationResult, AuthenticationResultByNumber, decodeLength, encodeLength, invalidTopic, invalidTopicFilter, invalidUTF8, PacketNameByType, PacketType, };
26
- export const packetsByType = [
27
- null,
28
- connect, // 1
29
- connack, // 2
30
- publish, // 3
31
- puback, // 4
32
- pubrec, // 5
33
- pubrel, // 6
34
- pubcomp, // 7
35
- subscribe, // 8
36
- suback, // 9
37
- unsubscribe, // 10
38
- unsuback, // 11
39
- pingreq, // 12
40
- pingres, // 13
41
- disconnect, // 14
42
- ];
43
- export function encode(packet) {
44
- const packetType = packet.type;
45
- // deno-lint-ignore no-explicit-any
46
- const pkt = packet;
47
- const encoded = packetsByType[packetType]?.encode(pkt);
48
- if (!encoded) {
49
- throw Error("Packet encoding failed");
50
- }
51
- const { flags, bytes } = encoded;
52
- return Uint8Array.from([
53
- (packetType << 4) | flags,
54
- ...encodeLength(bytes.length),
55
- ...bytes,
56
- ]);
57
- }
58
- export function decodePayload(firstByte, buffer) {
59
- const packetType = firstByte >> 4;
60
- const flags = firstByte & 0x0f;
61
- const packet = packetsByType[packetType]?.decode(buffer, flags);
62
- if (packet !== undefined) {
63
- return packet;
64
- }
65
- throw new Error("packet decoding failed");
66
- }
67
- export function decode(buffer) {
68
- if (buffer.length < 2) {
69
- throw new DecoderError("Packet decoding failed");
70
- }
71
- const { length, numLengthBytes } = decodeLength(buffer, 1);
72
- const start = numLengthBytes + 1;
73
- const end = start + length;
74
- return decodePayload(buffer[0], buffer.subarray(start, end));
75
- }
76
- export { getLengthDecoder } from "../mqttPacket/length.js";
@@ -1,15 +0,0 @@
1
- import { PacketType } from "./PacketType.js";
2
- import { hasEmptyFlags, isEmptyBuf } from "./decoder.js";
3
- export const pingreq = {
4
- encode(_packet) {
5
- const flags = 0;
6
- return { flags, bytes: [] };
7
- },
8
- decode(buffer, flags) {
9
- hasEmptyFlags(flags);
10
- isEmptyBuf(buffer);
11
- return {
12
- type: PacketType.pingreq,
13
- };
14
- },
15
- };
@@ -1,14 +0,0 @@
1
- import { PacketType } from "./PacketType.js";
2
- import { isEmptyBuf } from "./decoder.js";
3
- export const pingres = {
4
- encode(_packet) {
5
- const flags = 0;
6
- return { flags, bytes: [] };
7
- },
8
- decode(buffer) {
9
- isEmptyBuf(buffer);
10
- return {
11
- type: PacketType.pingres,
12
- };
13
- },
14
- };
@@ -1,20 +0,0 @@
1
- import { PacketType } from "./PacketType.js";
2
- import { Decoder } from "./decoder.js";
3
- import { Encoder } from "./encoder.js";
4
- export const puback = {
5
- encode(packet) {
6
- const flags = 0;
7
- const encoder = new Encoder();
8
- encoder.setInt16(packet.id);
9
- return { flags, bytes: encoder.done() };
10
- },
11
- decode(buffer) {
12
- const decoder = new Decoder(buffer);
13
- const id = decoder.getInt16();
14
- decoder.done();
15
- return {
16
- type: PacketType.puback,
17
- id,
18
- };
19
- },
20
- };
@@ -1,20 +0,0 @@
1
- import { PacketType } from "./PacketType.js";
2
- import { Decoder } from "./decoder.js";
3
- import { Encoder } from "./encoder.js";
4
- export const pubcomp = {
5
- encode(packet) {
6
- const flags = 0;
7
- const encoder = new Encoder();
8
- encoder.setInt16(packet.id);
9
- return { flags, bytes: encoder.done() };
10
- },
11
- decode(buffer) {
12
- const decoder = new Decoder(buffer);
13
- const id = decoder.getInt16();
14
- decoder.done();
15
- return {
16
- type: PacketType.pubcomp,
17
- id,
18
- };
19
- },
20
- };
@@ -1,51 +0,0 @@
1
- import { PacketType } from "./PacketType.js";
2
- import { BitMask } from "./BitMask.js";
3
- import { Encoder, EncoderError } from "./encoder.js";
4
- import { booleanFlag, Decoder, DecoderError } from "./decoder.js";
5
- export const publish = {
6
- encode(packet) {
7
- const qos = packet.qos || 0;
8
- const flags = (packet.dup ? BitMask.bit3 : 0) +
9
- (qos & 2 ? BitMask.bit2 : 0) +
10
- (qos & 1 ? BitMask.bit1 : 0) +
11
- (packet.retain ? BitMask.bit0 : 0);
12
- const encoder = new Encoder();
13
- encoder.setTopic(packet.topic);
14
- if (qos === 1 || qos === 2) {
15
- if (typeof packet.id !== "number" || packet.id < 1) {
16
- throw new EncoderError("when qos is 1 or 2, packet must have id");
17
- }
18
- encoder.setInt16(packet.id);
19
- }
20
- encoder.setRemainder(packet.payload);
21
- return { flags, bytes: encoder.done() };
22
- },
23
- decode(buffer, flags) {
24
- const dup = booleanFlag(flags, BitMask.bit3);
25
- const qos = (flags & 6) >> 1;
26
- const retain = booleanFlag(flags, BitMask.bit0);
27
- if (qos !== 0 && qos !== 1 && qos !== 2) {
28
- throw new DecoderError("Invalid qos");
29
- }
30
- if (dup && qos === 0) {
31
- throw new DecoderError("Invalid qos for possible duplicate");
32
- }
33
- const decoder = new Decoder(buffer);
34
- const topic = decoder.getTopic();
35
- let id = 0;
36
- if (qos > 0) {
37
- id = decoder.getInt16();
38
- }
39
- const payload = decoder.getRemainder();
40
- // no decoder.done() required because of getRemainder
41
- return {
42
- type: PacketType.publish,
43
- topic,
44
- payload,
45
- dup,
46
- retain,
47
- qos,
48
- id,
49
- };
50
- },
51
- };
@@ -1,20 +0,0 @@
1
- import { PacketType } from "./PacketType.js";
2
- import { Decoder } from "./decoder.js";
3
- import { Encoder } from "./encoder.js";
4
- export const pubrec = {
5
- encode(packet) {
6
- const flags = 0;
7
- const encoder = new Encoder();
8
- encoder.setInt16(packet.id);
9
- return { flags, bytes: encoder.done() };
10
- },
11
- decode(buffer) {
12
- const decoder = new Decoder(buffer);
13
- const id = decoder.getInt16();
14
- decoder.done();
15
- return {
16
- type: PacketType.pubrec,
17
- id,
18
- };
19
- },
20
- };
@@ -1,20 +0,0 @@
1
- import { PacketType } from "./PacketType.js";
2
- import { Decoder } from "./decoder.js";
3
- import { Encoder } from "./encoder.js";
4
- export const pubrel = {
5
- encode(packet) {
6
- const flags = 0;
7
- const encoder = new Encoder();
8
- encoder.setInt16(packet.id);
9
- return { flags, bytes: encoder.done() };
10
- },
11
- decode(buffer) {
12
- const decoder = new Decoder(buffer);
13
- const id = decoder.getInt16();
14
- decoder.done();
15
- return {
16
- type: PacketType.pubrel,
17
- id,
18
- };
19
- },
20
- };
@@ -1,29 +0,0 @@
1
- import { PacketType } from "./PacketType.js";
2
- import { Encoder } from "./encoder.js";
3
- import { Decoder } from "./decoder.js";
4
- export const suback = {
5
- encode(packet) {
6
- const flags = 0;
7
- const encoder = new Encoder();
8
- encoder.setInt16(packet.id);
9
- encoder.setRemainder(packet.returnCodes);
10
- return {
11
- flags,
12
- bytes: encoder.done(),
13
- };
14
- },
15
- decode(buffer) {
16
- const decoder = new Decoder(buffer);
17
- const id = decoder.getInt16();
18
- const payload = decoder.getRemainder();
19
- const returnCodes = [];
20
- for (const code of payload) {
21
- returnCodes.push(code);
22
- }
23
- return {
24
- type: PacketType.suback,
25
- id,
26
- returnCodes,
27
- };
28
- },
29
- };
@@ -1,52 +0,0 @@
1
- import { PacketType } from "./PacketType.js";
2
- import { BitMask } from "./BitMask.js";
3
- import { Encoder } from "./encoder.js";
4
- import { booleanFlag, Decoder, DecoderError } from "./decoder.js";
5
- export const subscribe = {
6
- encode(packet) {
7
- // Bits 3,2,1 and 0 of the fixed header of the SUBSCRIBE Control Packet are reserved and
8
- // MUST be set to 0,0,1 and 0 respectively. The Server MUST treat any other value as
9
- // malformed and close the Network Connection [MQTT-3.8.1-1].
10
- const flags = 0b0010;
11
- const encoder = new Encoder();
12
- encoder.setInt16(packet.id);
13
- for (const sub of packet.subscriptions) {
14
- encoder.setTopic(sub.topicFilter);
15
- encoder.setByte(sub.qos);
16
- }
17
- return { flags, bytes: encoder.done() };
18
- },
19
- decode(buffer, flags) {
20
- // Bits 3,2,1 and 0 of the fixed header of the SUBSCRIBE Control Packet are reserved and
21
- // MUST be set to 0,0,1 and 0 respectively. The Server MUST treat any other value as
22
- // malformed and close the Network Connection [MQTT-3.8.1-1].
23
- if (!booleanFlag(flags, BitMask.bit1)) {
24
- throw new DecoderError("Invalid header");
25
- }
26
- const decoder = new Decoder(buffer);
27
- const id = decoder.getInt16();
28
- const subscriptions = [];
29
- // The payload of a SUBSCRIBE packet MUST contain at least one Topic Filter / QoS pair.
30
- // A SUBSCRIBE packet with no payload is a protocol violation [MQTT-3.8.3-3].
31
- do {
32
- const topicFilter = decoder.getTopicFilter();
33
- const qos = decoder.getByte();
34
- if (qos !== 0 && qos !== 1 && qos !== 2) {
35
- throw new DecoderError("Invalid qos");
36
- }
37
- if (qos > 0 && id === 0) {
38
- throw new DecoderError("Invalid packet identifier");
39
- }
40
- subscriptions.push({
41
- topicFilter: topicFilter,
42
- qos,
43
- });
44
- } while (!decoder.atEnd());
45
- decoder.done();
46
- return {
47
- type: PacketType.subscribe,
48
- id,
49
- subscriptions,
50
- };
51
- },
52
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,20 +0,0 @@
1
- import { PacketType } from "./PacketType.js";
2
- import { Decoder } from "./decoder.js";
3
- import { Encoder } from "./encoder.js";
4
- export const unsuback = {
5
- encode(packet) {
6
- const flags = 0;
7
- const encoder = new Encoder();
8
- encoder.setInt16(packet.id);
9
- return { flags, bytes: encoder.done() };
10
- },
11
- decode(buffer) {
12
- const decoder = new Decoder(buffer);
13
- const id = decoder.getInt16();
14
- decoder.done();
15
- return {
16
- type: PacketType.unsuback,
17
- id,
18
- };
19
- },
20
- };
@@ -1,36 +0,0 @@
1
- import { PacketType } from "./PacketType.js";
2
- import { BitMask } from "./BitMask.js";
3
- import { Encoder } from "./encoder.js";
4
- import { booleanFlag, Decoder, DecoderError } from "./decoder.js";
5
- export const unsubscribe = {
6
- encode(packet) {
7
- // Bits 3,2,1 and 0 of the fixed header of the UNSUBSCRIBE Control Packet are reserved and
8
- // MUST be set to 0,0,1 and 0 respectively. The Server MUST treat any other value as
9
- // malformed and close the Network Connection [MQTT-3.10.1-1].
10
- const flags = 0b0010;
11
- const encoder = new Encoder();
12
- encoder.setInt16(packet.id);
13
- for (const topicFilter of packet.topicFilters) {
14
- encoder.setTopicFilter(topicFilter);
15
- }
16
- return { flags, bytes: encoder.done() };
17
- },
18
- decode(buffer, flags) {
19
- if (!booleanFlag(flags, BitMask.bit1)) {
20
- throw new DecoderError("Invalid header");
21
- }
22
- const decoder = new Decoder(buffer);
23
- const id = decoder.getInt16();
24
- const topicFilters = [];
25
- do {
26
- const topicFilter = decoder.getTopicFilter();
27
- topicFilters.push(topicFilter);
28
- } while (!decoder.atEnd());
29
- decoder.done();
30
- return {
31
- type: PacketType.unsubscribe,
32
- id,
33
- topicFilters,
34
- };
35
- },
36
- };
@@ -1,15 +0,0 @@
1
- // deno-lint-ignore no-control-regex
2
- const invalidUTF8regEx = new RegExp(/\x00|\uFFFD/);
3
- // deno-lint-ignore no-control-regex
4
- const invalidTopicRegEx = new RegExp(/^$|\+|#|\x00|\uFFFD/);
5
- // deno-lint-ignore no-control-regex
6
- const invalidTopicFilterRegEx = new RegExp(/^$|#.|[^\/]\+|\+[^\/]|\x00|\uFFFD/);
7
- export function invalidUTF8(value) {
8
- return invalidUTF8regEx.test(value);
9
- }
10
- export function invalidTopic(value) {
11
- return invalidTopicRegEx.test(value);
12
- }
13
- export function invalidTopicFilter(value) {
14
- return invalidTopicFilterRegEx.test(value);
15
- }
@@ -1,81 +0,0 @@
1
- /*
2
- * this a NodeJS specific implementation of TCP client sockets
3
- * it extends the platform agnostic Client class
4
- * @module
5
- */
6
- import { Client } from "../client/client.js";
7
- import { logger } from "../client/deps.js";
8
- import { readFile } from "node:fs/promises";
9
- import { createConnection } from "node:net";
10
- import * as tls from "node:tls";
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
- */
21
- export async function getFileData(filename) {
22
- if (!filename) {
23
- return;
24
- }
25
- const data = await readFile(filename, { encoding: "utf-8" });
26
- if (data === "") {
27
- return;
28
- }
29
- return data;
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
- */
36
- export class TcpClient extends Client {
37
- connectMQTT(hostname, port = 1883) {
38
- logger.debug({ hostname, port });
39
- return new Promise((resolve, reject) => {
40
- const socket = createConnection({ port, host: hostname }, () => {
41
- logger.debug("Connected to server");
42
- resolve(wrapNodeSocket(socket));
43
- });
44
- socket.once("error", (err) => {
45
- logger.debug("Connection failed: ", err);
46
- reject(err);
47
- });
48
- });
49
- }
50
- connectMQTTS(hostname, port = 8883, ca, cert, key) {
51
- const opts = {
52
- host: hostname,
53
- port,
54
- secureContext: (ca || key || cert)
55
- ? tls.createSecureContext({ ca, cert, key })
56
- : undefined,
57
- };
58
- logger.debug({ hostname, port, ca, cert });
59
- return new Promise((resolve, reject) => {
60
- const socket = tls.connect(opts, () => {
61
- logger.debug("Connected to server");
62
- resolve(wrapNodeSocket(socket));
63
- });
64
- socket.once("error", (err) => {
65
- logger.debug("Connection failed", err);
66
- reject(err);
67
- });
68
- });
69
- }
70
- createConn(protocol, hostname, port, caCerts) {
71
- // if you need to support alternative connection types just
72
- // overload this method in your subclass
73
- if (protocol === "mqtts:") {
74
- return this.connectMQTTS(hostname, port, caCerts);
75
- }
76
- if (protocol === "mqtt:") {
77
- return this.connectMQTT(hostname, port);
78
- }
79
- throw `Unsupported protocol: ${protocol}`;
80
- }
81
- }
@@ -1,50 +0,0 @@
1
- import { createServer } from "node:net";
2
- import { MqttServer } from "../server/mod.js";
3
- import { wrapNodeSocket } from "./wrapNodeSocket.js";
4
- /*
5
- * TCP server that wraps a MqttServer, see demoServer.ts in the /bin folder
6
- */
7
- export class TcpServer {
8
- mqttServer;
9
- server;
10
- serverOptions;
11
- constructor(serverOptions, mqttOptions) {
12
- this.mqttServer = new MqttServer(mqttOptions);
13
- this.serverOptions = serverOptions;
14
- }
15
- async start() {
16
- this.server = createServer((sock) => this.mqttServer.serve(wrapNodeSocket(sock)));
17
- const isListening = new Promise((resolve) => {
18
- this.server?.on("listening", () => {
19
- resolve(true);
20
- });
21
- });
22
- this.server.listen(this.serverOptions.port, this.serverOptions.hostname);
23
- await isListening;
24
- return;
25
- }
26
- stop() {
27
- this.server?.close();
28
- }
29
- get port() {
30
- const address = this.server?.address();
31
- if (typeof address === "object" && address !== null) {
32
- return address?.port;
33
- }
34
- return this.serverOptions?.port;
35
- }
36
- get address() {
37
- const addressResult = this.server?.address();
38
- if (typeof addressResult === "object") {
39
- const address = addressResult?.address;
40
- if (address === "::") {
41
- return "localhost";
42
- }
43
- if (address?.includes(":")) {
44
- return `[${address}]`;
45
- }
46
- return address;
47
- }
48
- return addressResult;
49
- }
50
- }