@seriousme/opifex 1.6.2 → 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 (128) hide show
  1. package/README.md +6 -7
  2. package/bin/conformanceServer.ts +67 -16
  3. package/bin/demoServer.ts +69 -14
  4. package/bin/mqtt.ts +30 -0
  5. package/client/ConnectionState.ts +12 -0
  6. package/client/client.ts +56 -2
  7. package/client/context.ts +5 -0
  8. package/client/handlers/handleConnack.ts +6 -0
  9. package/client/handlers/handlePacket.ts +7 -0
  10. package/client/handlers/handlePuback.ts +8 -2
  11. package/client/handlers/handlePubcomp.ts +10 -4
  12. package/client/handlers/handlePublish.ts +6 -2
  13. package/client/handlers/handlePubrec.ts +13 -6
  14. package/client/handlers/handlePubrel.ts +16 -6
  15. package/client/handlers/handleSuback.ts +9 -6
  16. package/client/handlers/handleUnsuback.ts +10 -3
  17. package/client/store/memoryStore.ts +25 -0
  18. package/client/store/store.ts +39 -0
  19. package/deno/tcpServer.test.ts +137 -2
  20. package/node/tcpServer.test.ts +61 -6
  21. package/node/tcpServer.ts +9 -2
  22. package/node/wrapNodeSocket.ts +13 -2
  23. package/package.json +13 -3
  24. package/persistence/memory/memoryPersistence.test.ts +2 -1
  25. package/persistence/memory/memoryPersistence.ts +1 -0
  26. package/persistence/store.ts +1 -0
  27. package/server/handlers/handleConnect.ts +49 -6
  28. package/server/handlers/handleDisconnect.ts +5 -0
  29. package/server/handlers/handlePacket.ts +7 -0
  30. package/server/handlers/handlePingreq.ts +6 -0
  31. package/server/handlers/handlePuback.ts +9 -2
  32. package/server/handlers/handlePubcomp.ts +8 -3
  33. package/server/handlers/handlePublish.ts +13 -0
  34. package/server/handlers/handlePubrec.ts +11 -3
  35. package/server/handlers/handlePubrel.ts +12 -4
  36. package/server/handlers/handleSubscribe.ts +28 -5
  37. package/server/handlers/handleUnsubscribe.ts +6 -0
  38. package/utils/asyncQueue.ts +2 -1
  39. package/utils/nextTick.ts +13 -1
  40. package/.github/workflows/closeStale.yml +0 -15
  41. package/.github/workflows/codeql-analysis.yml +0 -71
  42. package/.github/workflows/deno-ci.yml +0 -16
  43. package/.github/workflows/nodejs-ci.yml +0 -23
  44. package/.github/workflows/publish.yml +0 -16
  45. package/.github/workflows/update-license-year.sh +0 -14
  46. package/.github/workflows/update-license-year.yml +0 -13
  47. package/CHANGELOG.md +0 -74
  48. package/deno.json +0 -15
  49. package/deno.lock +0 -34
  50. package/dist/bin/conformanceServer.js +0 -51
  51. package/dist/bin/demoServer.js +0 -46
  52. package/dist/bin/mqtt.js +0 -227
  53. package/dist/client/ConnectionState.js +0 -7
  54. package/dist/client/client.js +0 -132
  55. package/dist/client/context.js +0 -148
  56. package/dist/client/deps.js +0 -5
  57. package/dist/client/handlers/handleConnack.js +0 -18
  58. package/dist/client/handlers/handlePacket.js +0 -50
  59. package/dist/client/handlers/handlePuback.js +0 -6
  60. package/dist/client/handlers/handlePubcomp.js +0 -9
  61. package/dist/client/handlers/handlePublish.js +0 -28
  62. package/dist/client/handlers/handlePubrec.js +0 -17
  63. package/dist/client/handlers/handlePubrel.js +0 -19
  64. package/dist/client/handlers/handleSuback.js +0 -9
  65. package/dist/client/handlers/handleUnsuback.js +0 -7
  66. package/dist/client/mod.js +0 -5
  67. package/dist/client/store/deps.js +0 -1
  68. package/dist/client/store/memoryStore.js +0 -36
  69. package/dist/client/store/store.js +0 -1
  70. package/dist/mqttConn/mqttConn.js +0 -104
  71. package/dist/mqttPacket/AuthenticationResult.js +0 -16
  72. package/dist/mqttPacket/BitMask.js +0 -10
  73. package/dist/mqttPacket/PacketType.js +0 -18
  74. package/dist/mqttPacket/connack.js +0 -27
  75. package/dist/mqttPacket/connect.js +0 -115
  76. package/dist/mqttPacket/decoder.js +0 -89
  77. package/dist/mqttPacket/disconnect.js +0 -14
  78. package/dist/mqttPacket/encoder.js +0 -56
  79. package/dist/mqttPacket/length.js +0 -43
  80. package/dist/mqttPacket/mod.js +0 -76
  81. package/dist/mqttPacket/pingreq.js +0 -15
  82. package/dist/mqttPacket/pingres.js +0 -14
  83. package/dist/mqttPacket/puback.js +0 -20
  84. package/dist/mqttPacket/pubcomp.js +0 -20
  85. package/dist/mqttPacket/publish.js +0 -51
  86. package/dist/mqttPacket/pubrec.js +0 -20
  87. package/dist/mqttPacket/pubrel.js +0 -20
  88. package/dist/mqttPacket/suback.js +0 -29
  89. package/dist/mqttPacket/subscribe.js +0 -52
  90. package/dist/mqttPacket/types.js +0 -1
  91. package/dist/mqttPacket/unsuback.js +0 -20
  92. package/dist/mqttPacket/unsubscribe.js +0 -36
  93. package/dist/mqttPacket/validators.js +0 -15
  94. package/dist/node/tcpClient.js +0 -81
  95. package/dist/node/tcpServer.js +0 -43
  96. package/dist/node/wrapNodeSocket.js +0 -47
  97. package/dist/persistence/deps.js +0 -3
  98. package/dist/persistence/memory/memoryPersistence.js +0 -117
  99. package/dist/persistence/persistence.js +0 -1
  100. package/dist/persistence/store.js +0 -1
  101. package/dist/server/context.js +0 -100
  102. package/dist/server/deps.js +0 -5
  103. package/dist/server/handlers/handleConnect.js +0 -46
  104. package/dist/server/handlers/handleDisconnect.js +0 -4
  105. package/dist/server/handlers/handlePacket.js +0 -58
  106. package/dist/server/handlers/handlePingreq.js +0 -6
  107. package/dist/server/handlers/handlePuback.js +0 -8
  108. package/dist/server/handlers/handlePubcomp.js +0 -8
  109. package/dist/server/handlers/handlePublish.js +0 -41
  110. package/dist/server/handlers/handlePubrec.js +0 -15
  111. package/dist/server/handlers/handlePubrel.js +0 -18
  112. package/dist/server/handlers/handleSubscribe.js +0 -33
  113. package/dist/server/handlers/handleUnsubscribe.js +0 -12
  114. package/dist/server/mod.js +0 -7
  115. package/dist/server/server.js +0 -47
  116. package/dist/socket/socket.js +0 -36
  117. package/dist/timer/timer.js +0 -40
  118. package/dist/trie/trie.js +0 -95
  119. package/dist/tsconfig.tsbuildinfo +0 -1
  120. package/dist/utils/args.js +0 -75
  121. package/dist/utils/assert.js +0 -20
  122. package/dist/utils/asyncQueue.js +0 -67
  123. package/dist/utils/deferred.js +0 -14
  124. package/dist/utils/logger.js +0 -58
  125. package/dist/utils/mod.js +0 -11
  126. package/dist/utils/nextTick.js +0 -6
  127. package/dist/utils/platform.js +0 -7
  128. package/tsconfig.json +0 -22
@@ -1,9 +1,17 @@
1
1
  import type { Context } from "../context.ts";
2
2
  import { PacketType, type PubrecPacket } from "../deps.ts";
3
3
 
4
- // qos 2
5
- // Discard message, Store PUBREC received <Packet Identifier>
6
- // send PUBREL <Packet Identifier>
4
+ /**
5
+ * Handles PUBREC (QoS 2 Publish Received) packets
6
+ * @param ctx - The connection context
7
+ * @param packet - The PUBREC packet received from the client
8
+ * @returns Promise that resolves when handling is complete
9
+ * @description
10
+ * For QoS 2 message flow:
11
+ * 1. Discards the original publish message
12
+ * 2. Stores that PUBREC was received for the packet ID
13
+ * 3. Sends PUBREL packet in response
14
+ */
7
15
  export async function handlePubrec(
8
16
  ctx: Context,
9
17
  packet: PubrecPacket,
@@ -1,10 +1,18 @@
1
1
  import type { Context } from "../context.ts";
2
2
  import { PacketType, type PubrelPacket } from "../deps.ts";
3
3
 
4
- // qos 2 only
5
- // Method A, Initiate onward delivery of the Application Message1 then discard message
6
- // Send PUBCOMP <Packet Identifier>
7
-
4
+ /**
5
+ * Handles PUBREL (QoS 2 publish release) packets
6
+ *
7
+ * @param ctx - The connection context
8
+ * @param packet - The PUBREL packet received from the client
9
+ * @returns Promise that resolves when handling is complete
10
+ * @description
11
+ * For QoS 2 message delivery:
12
+ * 1. Initiates onward delivery of the Application Message
13
+ * 2. Discards the stored message
14
+ * 3. Sends PUBCOMP packet with the Packet Identifier
15
+ */
8
16
  export async function handlePubrel(
9
17
  ctx: Context,
10
18
  packet: PubrelPacket,
@@ -6,7 +6,18 @@ import {
6
6
  type Topic,
7
7
  } from "../deps.ts";
8
8
 
9
+ /**
10
+ * @constant {number} SubscriptionFailure
11
+ * @description Code indicating a failed subscription attempt
12
+ */
9
13
  const SubscriptionFailure = 0x80;
14
+
15
+ /**
16
+ * Checks if a client is authorized to subscribe to a topic
17
+ * @param ctx - The connection context
18
+ * @param topicFilter - The topic filter to check authorization for
19
+ * @returns True if authorized, false otherwise
20
+ */
10
21
  function authorizedToSubscribe(ctx: Context, topicFilter: Topic) {
11
22
  if (ctx.handlers.isAuthorizedToSubscribe) {
12
23
  return ctx.handlers.isAuthorizedToSubscribe(ctx, topicFilter);
@@ -14,13 +25,23 @@ function authorizedToSubscribe(ctx: Context, topicFilter: Topic) {
14
25
  return true;
15
26
  }
16
27
 
28
+ /**
29
+ * @function handleSubscribe
30
+ * @description Processes an MQTT SUBSCRIBE packet
31
+ * @param {Context} ctx - The connection context
32
+ * @param {SubscribePacket} packet - The SUBSCRIBE packet received from the client
33
+ * @returns {Promise<void>}
34
+ * @throws {Error} If subscription processing fails
35
+ * @remarks The order of return codes in the SUBACK Packet MUST match the order of Topic Filters in the SUBSCRIBE Packet [MQTT-3.9.3-1]
36
+ */
17
37
  export async function handleSubscribe(
18
38
  ctx: Context,
19
39
  packet: SubscribePacket,
20
40
  ): Promise<void> {
21
- // The order of return codes in the SUBACK Packet MUST match the order of
22
- // Topic Filters in the SUBSCRIBE Packet [MQTT-3.9.3-1].
23
-
41
+ /*
42
+ * The order of return codes in the SUBACK Packet MUST match the order of
43
+ * Topic Filters in the SUBSCRIBE Packet [MQTT-3.9.3-1].
44
+ */
24
45
  const validSubscriptions: Subscription[] = [];
25
46
  const returnCodes = packet.subscriptions.map((sub) => {
26
47
  if (ctx.store) {
@@ -31,7 +52,7 @@ export async function handleSubscribe(
31
52
  validSubscriptions.push(sub);
32
53
  return sub.qos;
33
54
  }
34
- return SubscriptionFailure; // failure
55
+ return SubscriptionFailure;
35
56
  });
36
57
 
37
58
  await ctx.send({
@@ -40,7 +61,9 @@ export async function handleSubscribe(
40
61
  returnCodes: returnCodes,
41
62
  });
42
63
 
43
- // send any retained messages that match these subscriptions
64
+ /*
65
+ * send any retained messages that match these subscriptions
66
+ */
44
67
  if (ctx.store) {
45
68
  ctx.persistence.handleRetained(ctx.store.clientId);
46
69
  }
@@ -1,6 +1,12 @@
1
1
  import type { Context } from "../context.ts";
2
2
  import { PacketType, type UnsubscribePacket } from "../deps.ts";
3
3
 
4
+ /**
5
+ * Handles MQTT unsubscribe packets by removing subscriptions and sending acknowledgement
6
+ * @param ctx - The connection context containing client information and methods
7
+ * @param packet - The MQTT unsubscribe packet containing topics to unsubscribe from
8
+ * @returns Promise that resolves when unsubscribe is complete and acknowledged
9
+ */
4
10
  export async function handleUnsubscribe(
5
11
  ctx: Context,
6
12
  packet: UnsubscribePacket,
@@ -1,15 +1,16 @@
1
1
  import { nextTick } from "./nextTick.ts";
2
-
3
2
  /**
4
3
  * An Async Queue is a queue that can be used to push items to it and then wait
5
4
  * for them to be consumed.
6
5
  *
7
6
  * @example
7
+ * ```ts
8
8
  * const queue = new AsyncQueue<string>();
9
9
  * queue.push("hello");
10
10
  * for await (const item of queue) {
11
11
  * console.log(item); // "hello"
12
12
  * }
13
+ * ```
13
14
  */
14
15
  export class AsyncQueue<T> {
15
16
  private queue: T[] = [];
package/utils/nextTick.ts CHANGED
@@ -1,5 +1,17 @@
1
1
  /**
2
- * @returns a promise that resolves on the nextTick of the event loop
2
+ * Returns a promise that resolves on the next tick of the Node.js event loop.
3
+ * This is useful for deferring execution of code until after the current call stack is cleared.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * async function example() {
8
+ * console.log('Before nextTick');
9
+ * await nextTick();
10
+ * console.log('After nextTick');
11
+ * }
12
+ * ```
13
+ *
14
+ * @returns {Promise<void>} A promise that resolves on the next tick of the event loop
3
15
  */
4
16
  export function nextTick(): Promise<void> {
5
17
  return new Promise((resolve) => setTimeout(resolve, 0));
@@ -1,15 +0,0 @@
1
- name: "Close stale issues"
2
- on:
3
- schedule:
4
- - cron: "5 0 * * *"
5
-
6
- jobs:
7
- stale:
8
- runs-on: ubuntu-latest
9
- steps:
10
- - uses: actions/stale@v3
11
- with:
12
- repo-token: ${{ secrets.GITHUB_TOKEN }}
13
- days-before-stale: 30
14
- days-before-close: 5
15
- stale-issue-message: This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days'
@@ -1,71 +0,0 @@
1
- # For most projects, this workflow file will not need changing; you simply need
2
- # to commit it to your repository.
3
- #
4
- # You may wish to alter this file to override the set of languages analyzed,
5
- # or to provide custom queries or build logic.
6
- #
7
- # ******** NOTE ********
8
- # We have attempted to detect the languages in your repository. Please check
9
- # the `language` matrix defined below to confirm you have the correct set of
10
- # supported CodeQL languages.
11
- #
12
- name: "CodeQL"
13
-
14
- on:
15
- push:
16
- branches: [master]
17
- pull_request:
18
- # The branches below must be a subset of the branches above
19
- branches: [master]
20
- schedule:
21
- - cron: "27 16 * * 3"
22
-
23
- jobs:
24
- analyze:
25
- name: Analyze
26
- runs-on: ubuntu-latest
27
- permissions:
28
- actions: read
29
- contents: read
30
- security-events: write
31
-
32
- strategy:
33
- fail-fast: false
34
- matrix:
35
- language: ["javascript"]
36
- # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
37
- # Learn more:
38
- # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
39
-
40
- steps:
41
- - name: Checkout repository
42
- uses: actions/checkout@v2
43
-
44
- # Initializes the CodeQL tools for scanning.
45
- - name: Initialize CodeQL
46
- uses: github/codeql-action/init@v1
47
- with:
48
- languages: ${{ matrix.language }}
49
- # If you wish to specify custom queries, you can do so here or in a config file.
50
- # By default, queries listed here will override any specified in a config file.
51
- # Prefix the list here with "+" to use these queries and those in the config file.
52
- # queries: ./path/to/local/query, your-org/your-repo/queries@main
53
-
54
- # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
55
- # If this step fails, then you should remove it and run the build manually (see below)
56
- - name: Autobuild
57
- uses: github/codeql-action/autobuild@v1
58
-
59
- # ℹ️ Command-line programs to run using the OS shell.
60
- # 📚 https://git.io/JvXDl
61
-
62
- # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
63
- # and modify them (or add more) to build your code if your project
64
- # uses a compiled language
65
-
66
- #- run: |
67
- # make bootstrap
68
- # make release
69
-
70
- - name: Perform CodeQL Analysis
71
- uses: github/codeql-action/analyze@v1
@@ -1,16 +0,0 @@
1
- # This workflow will test on Deno lastest stable major and canary
2
- # see https://github.com/marketplace/actions/setup-deno for details
3
- name: Deno CI
4
- on: [push, pull_request]
5
- jobs:
6
- build:
7
- runs-on: ubuntu-latest
8
- steps:
9
- - uses: actions/checkout@v4
10
- - uses: denoland/setup-deno@v2
11
- with:
12
- deno-version: vx.x.x # Run with latest stable Deno.
13
- - run: deno install
14
- - run: deno fmt --check
15
- - run: deno lint
16
- - run: deno test -A
@@ -1,23 +0,0 @@
1
- # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2
- # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3
-
4
- name: Node.js CI
5
-
6
- on: [push, pull_request]
7
-
8
- permissions: {}
9
-
10
- jobs:
11
- build:
12
- runs-on: ubuntu-latest
13
-
14
- strategy:
15
- matrix:
16
- node-version: ["22", "23"]
17
- steps:
18
- - uses: actions/checkout@v4
19
- - name: Use Node.js ${{ matrix.node-version }}
20
- uses: actions/setup-node@v4
21
- with:
22
- node-version: ${{ matrix.node-version }}
23
- - run: npm run test
@@ -1,16 +0,0 @@
1
- name: Publish
2
-
3
- on:
4
- push:
5
- branches:
6
- - main
7
-
8
- jobs:
9
- publish:
10
- runs-on: ubuntu-latest
11
- permissions:
12
- contents: read
13
- id-token: write # The OIDC ID token is used for authentication with JSR.
14
- steps:
15
- - uses: actions/checkout@v4
16
- - run: npx jsr publish
@@ -1,14 +0,0 @@
1
- #!/bin/bash
2
-
3
- license='LICENSE.txt'
4
- prefix='2016-'
5
- current_year=$(date +%Y)
6
- sed -i -e "s/$prefix\([0-9\]\+\)/$prefix$current_year/" $license
7
-
8
- if [ "$(git diff $license)" ]; then
9
- git add $license
10
- git commit -m "Update license year to $current_year"
11
- git push
12
- else
13
- echo "No changes detected in $license"
14
- fi
@@ -1,13 +0,0 @@
1
- name: Update License Year
2
-
3
- on:
4
- schedule:
5
- - cron: "5 12 1 1 *"
6
-
7
- jobs:
8
- update_license:
9
- runs-on: ubuntu-latest
10
- steps:
11
- - uses: actions/checkout@v4
12
- - name: Update license year
13
- run: ./update_license.sh
package/CHANGELOG.md DELETED
@@ -1,74 +0,0 @@
1
- # Changelog
2
-
3
- ## [Unreleased]
4
-
5
- ### Changed
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
-
19
- ## [v1.6.0] 18-01-2025
20
-
21
- ### Changed
22
-
23
- - added: export TcpClient and TcpServer for both NodeJS and Deno
24
- - added: export mqttPacket and utils
25
-
26
- ## [v1.5.1] 16-01-2025
27
-
28
- ### Changed
29
-
30
- - fixed issue with QoS=1
31
-
32
- ## [v1.5.0] 02-01-2025
33
-
34
- ### Changed
35
-
36
- - added client certificate support to mqtts.ts
37
-
38
- ## [v1.4.0] 02-01-2025
39
-
40
- ### Changed
41
-
42
- - added client certificate support (w0otness)
43
-
44
- ## [v1.3.0] 19-11-2024
45
-
46
- ### Changed
47
-
48
- - update: added architectural description of Opifex to documentation
49
- - update: added comments to platform specific code to explain boundary between
50
- platform agnostic and platform specific.
51
- - update: set default clientId in client
52
- - fix: improve handling of errors if client connect fails
53
-
54
- ## [v1.2.1] 01-11-2024
55
-
56
- ### Changed
57
-
58
- - fix: unlock last BYOB request using respond(0)
59
-
60
- ## [v1.2.0] 01-11-2024
61
-
62
- ### Changed
63
-
64
- - added support for NodeJS < 22.0.6 by adding transpiled Javascript in `/dist`
65
-
66
- ## [v1.1.0] 01-11-2024
67
-
68
- ### Changed
69
-
70
- - added support for NodeJS >= 22.0.6 using --experimental-strip-types
71
-
72
- ## [v1.0.0] 09-10-2022
73
-
74
- Initial version
package/deno.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "name": "@seriousme/opifex",
3
- "version": "1.6.2",
4
- "exports": {
5
- "./server": "./server/mod.ts",
6
- "./client": "./client/mod.ts",
7
- "./mqttPacket": "./mqttPacket/mod.ts",
8
- "./utils": "./utils/mod.ts",
9
- "./tcpClient": "./deno/tcpClient.ts",
10
- "./tcpServer": "./deno/tcpServer.ts"
11
- },
12
- "exclude": [
13
- "dist"
14
- ]
15
- }
package/deno.lock DELETED
@@ -1,34 +0,0 @@
1
- {
2
- "version": "4",
3
- "specifiers": {
4
- "npm:@types/node@*": "22.5.4",
5
- "npm:@types/node@^22.10.0": "22.10.0"
6
- },
7
- "npm": {
8
- "@types/node@22.10.0": {
9
- "integrity": "sha512-XC70cRZVElFHfIUB40FgZOBbgJYFKKMa5nb9lxcwYstFG/Mi+/Y0bGS+rs6Dmhmkpq4pnNiLiuZAbc02YCOnmA==",
10
- "dependencies": [
11
- "undici-types@6.20.0"
12
- ]
13
- },
14
- "@types/node@22.5.4": {
15
- "integrity": "sha512-FDuKUJQm/ju9fT/SeX/6+gBzoPzlVCzfzmGkwKvRHQVxi4BntVbyIwf6a4Xn62mrvndLiml6z/UBXIdEVjQLXg==",
16
- "dependencies": [
17
- "undici-types@6.19.8"
18
- ]
19
- },
20
- "undici-types@6.19.8": {
21
- "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw=="
22
- },
23
- "undici-types@6.20.0": {
24
- "integrity": "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg=="
25
- }
26
- },
27
- "workspace": {
28
- "packageJson": {
29
- "dependencies": [
30
- "npm:@types/node@^22.10.0"
31
- ]
32
- }
33
- }
34
- }
@@ -1,51 +0,0 @@
1
- #!/usr/bin/env -S node --experimental-strip-types
2
- // this server is intended to be used with https://github.com/eclipse/iottestware
3
- import { AuthenticationResult } from "../server/mod.js";
4
- import { getArgs, logger, LogLevel, parseArgs } from "../utils/mod.js";
5
- import { TcpServer } from "../node/tcpServer.js";
6
- const utf8Decoder = new TextDecoder();
7
- const userTable = new Map();
8
- userTable.set("IoTester_1", "strong_password");
9
- userTable.set("IoTester_2", "strong_password");
10
- const strictUsername = new RegExp(/^[a-zA-Z0-9]{0,23}$/);
11
- const notAuthorizedTable = new Set();
12
- notAuthorizedTable.add(["123-456-789", "eclipse/iot/tesware/0data"]);
13
- function isAuthenticated(_ctx, clientId, username, password) {
14
- const pwd = utf8Decoder.decode(password);
15
- logger.debug(`Verifying authentication of client '${clientId}' with username '${username}' and password '${pwd}'`);
16
- if (!userTable.has(username)) {
17
- if (!strictUsername.test(username)) {
18
- return AuthenticationResult.badUsernameOrPassword;
19
- }
20
- }
21
- const pass = userTable.get(username);
22
- if (pwd === pass) {
23
- return AuthenticationResult.ok;
24
- }
25
- return AuthenticationResult.notAuthorized;
26
- }
27
- function isAuthorizedToPublish(ctx, topic) {
28
- logger.debug(`Checking authorization of client '${ctx.store?.clientId}' to publish on topic '${topic}'`);
29
- // if (notAuthorizedTable.has([ctx.client,topic])){
30
- // return false;
31
- // }
32
- return true;
33
- }
34
- function isAuthorizedToSubscribe(ctx, topic) {
35
- logger.debug(`Checking authorization of client '${ctx.store?.clientId}' to subscribe to topic '${topic}'`);
36
- return true;
37
- }
38
- /** start the server **/
39
- const { _: [portNum] } = parseArgs(getArgs());
40
- const port = Number(portNum ?? 1883);
41
- const hostname = "::";
42
- logger.level(LogLevel.info);
43
- const tcpServer = new TcpServer({ port, hostname }, {
44
- handlers: {
45
- isAuthenticated,
46
- isAuthorizedToPublish,
47
- isAuthorizedToSubscribe,
48
- },
49
- });
50
- tcpServer.start();
51
- logger.info(`Server started on port ${tcpServer.port}`);
@@ -1,46 +0,0 @@
1
- #!/usr/bin/env -S node --experimental-strip-types
2
- import { TcpServer } from "../node/tcpServer.js";
3
- import { AuthenticationResult } from "../server/mod.js";
4
- import { getArgs, logger, LogLevel, parseArgs } from "../utils/mod.js";
5
- const utf8Decoder = new TextDecoder();
6
- const userTable = new Map();
7
- userTable.set("IoTester_1", "strong_password");
8
- userTable.set("IoTester_2", "strong_password");
9
- // const strictUsername = new RegExp(/^[a-zA-Z0-9]{0,23}$/);
10
- function isAuthenticated(_ctx, clientId, username, password) {
11
- const pwd = utf8Decoder.decode(password);
12
- logger.info(`Verifying authentication of client '${clientId}' with username '${username}' and password '${pwd}'`);
13
- return AuthenticationResult.ok;
14
- // if (!userTable.has(username)) {
15
- // if (!strictUsername.test(username)) {
16
- // return AuthenticationResult.badUsernameOrPassword;
17
- // }
18
- // }
19
- // const pass = userTable.get(username);
20
- // if (pwd === pass) {
21
- // return AuthenticationResult.ok;
22
- // }
23
- // return AuthenticationResult.badUsernameOrPassword;
24
- }
25
- function isAuthorizedToPublish(ctx, topic) {
26
- logger.debug(`Checking authorization of client '${ctx.store?.clientId}' to publish on topic '${topic}'`);
27
- return true;
28
- }
29
- function isAuthorizedToSubscribe(ctx, topic) {
30
- logger.debug(`Checking authorization of client '${ctx.store?.clientId}' to subscribe to topic '${topic}'`);
31
- return true;
32
- }
33
- /** start the server **/
34
- const { _: [portNum] } = parseArgs(getArgs());
35
- const port = Number(portNum ?? 1883);
36
- const hostname = "::";
37
- logger.level(LogLevel.info);
38
- const tcpServer = new TcpServer({ port, hostname }, {
39
- handlers: {
40
- isAuthenticated,
41
- isAuthorizedToPublish,
42
- isAuthorizedToSubscribe,
43
- },
44
- });
45
- tcpServer.start();
46
- logger.info(`Server started on port ${tcpServer.port}`);