@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,7 +0,0 @@
1
- /**
2
- * This module provides an MQTT Server interface see the /bin folder for examples
3
- * @module
4
- */
5
- export { AuthenticationResult } from "./deps.js";
6
- export { MqttServer } from "./server.js";
7
- export { Context } from "./context.js";
@@ -1,47 +0,0 @@
1
- import { Context } from "./context.js";
2
- import { AuthenticationResult, logger, MemoryPersistence, } from "./deps.js";
3
- import { handlePacket } from "./handlers/handlePacket.js";
4
- const defaultIsAuthenticated = (_ctx, _clientId, _username, _password) => AuthenticationResult.ok;
5
- const defaultIsAuthorized = (_ctx, _topic) => true;
6
- /** The MqttServer class provides a MQTT server with configurable persistence and
7
- * authentication/authorization handlers.
8
- *
9
- * The default handlers are:
10
- * - isAuthenticated: always returns ok
11
- * - isAuthorizedToPublish: always returns true
12
- * - isAuthorizedToSubscribe: always returns true
13
- *
14
- * To customize the handlers, pass in a Handlers object.
15
- * To customize the persistence, pass in a Persistence object.
16
- */
17
- export class MqttServer {
18
- handlers;
19
- persistence;
20
- constructor({ persistence, handlers, }) {
21
- this.persistence = persistence || new MemoryPersistence();
22
- this.handlers = {
23
- isAuthenticated: handlers?.isAuthenticated || defaultIsAuthenticated,
24
- isAuthorizedToPublish: handlers?.isAuthorizedToPublish ||
25
- defaultIsAuthorized,
26
- isAuthorizedToSubscribe: handlers?.isAuthorizedToSubscribe ||
27
- defaultIsAuthorized,
28
- };
29
- }
30
- async serve(conn) {
31
- const ctx = new Context(this.persistence, conn, this.handlers);
32
- if (conn.remoteAddr?.transport === "tcp") {
33
- logger.debug(`socket connected from ${conn.remoteAddr.hostname}`);
34
- }
35
- try {
36
- for await (const packet of ctx.mqttConn) {
37
- handlePacket(ctx, packet);
38
- }
39
- }
40
- catch (err) {
41
- logger.debug(`Error while serving:${err}`);
42
- }
43
- finally {
44
- ctx.close();
45
- }
46
- }
47
- }
@@ -1,36 +0,0 @@
1
- export class Conn {
2
- reader;
3
- writer;
4
- closed;
5
- remoteAddr;
6
- closer;
7
- constructor(sockConn) {
8
- this.closed = false;
9
- this.reader = sockConn.readable.getReader({ mode: "byob" });
10
- this.writer = sockConn.writable.getWriter();
11
- // this.writer.closed.then(() => this.closed = true);
12
- this.closer = sockConn.close.bind(sockConn);
13
- this.remoteAddr = sockConn.remoteAddr;
14
- }
15
- async read(buff) {
16
- const buff2 = new Uint8Array(buff.length);
17
- const result = await this.reader.read(buff2);
18
- if (!result.done) {
19
- buff.set(result.value);
20
- }
21
- return result.value?.byteLength || null;
22
- }
23
- write(data) {
24
- if (this.closed) {
25
- return Promise.reject();
26
- }
27
- this.writer.write(data);
28
- return new Promise((resolve) => resolve(data.length));
29
- }
30
- close() {
31
- if (!this.closed) {
32
- this.closed = true;
33
- this.closer();
34
- }
35
- }
36
- }
@@ -1,40 +0,0 @@
1
- export class Timer {
2
- delay; // delay in microseconds
3
- // deno-lint-ignore ban-types
4
- action; // function to perform when timer expires
5
- timer;
6
- end = 0;
7
- running = false;
8
- // deno-lint-ignore ban-types
9
- constructor(action, delay, wait = false) {
10
- this.delay = delay;
11
- this.action = action;
12
- if (!wait) {
13
- this.reset();
14
- }
15
- }
16
- startTimer(delay) {
17
- this.running = true;
18
- this.timer = setTimeout(() => this.ring(), delay);
19
- }
20
- ring() {
21
- const timeLeft = this.end - Date.now();
22
- // check if delay has passed, else execute action
23
- if (timeLeft > 0) {
24
- this.startTimer(timeLeft);
25
- return;
26
- }
27
- this.running = false;
28
- this.action();
29
- }
30
- reset() {
31
- this.end = Date.now() + this.delay;
32
- if (!this.running) {
33
- this.startTimer(this.delay);
34
- }
35
- }
36
- clear() {
37
- clearTimeout(this.timer);
38
- this.running = false;
39
- }
40
- }
package/dist/trie/trie.js DELETED
@@ -1,95 +0,0 @@
1
- export class Trie {
2
- #value;
3
- #children;
4
- separator = "/";
5
- wildcardOne = "+";
6
- wildcardSubtree = "#";
7
- reservedPrefix = "$";
8
- looseCompare;
9
- constructor(looseCompare = false) {
10
- this.#value = [];
11
- this.#children = new Map();
12
- this.looseCompare = looseCompare;
13
- }
14
- matchChild(child, parts) {
15
- const childNode = this.#children.get(child);
16
- return childNode ? childNode._match(parts) : [];
17
- }
18
- match(key) {
19
- const parts = key.split(this.separator);
20
- // toplevel wildcards are not allowed to match toplevel reserved topics
21
- if (parts.length > 0 && parts[0].charAt(0) === this.reservedPrefix) {
22
- return this._matchPrefix(parts);
23
- }
24
- return this._match(parts);
25
- }
26
- _matchPrefix(parts) {
27
- if (parts.length === 0) {
28
- return this.#value ? this.#value : [];
29
- }
30
- const [first, ...rest] = parts;
31
- return this.matchChild(first, rest);
32
- }
33
- _match(parts) {
34
- if (parts.length === 0) {
35
- return this.#value ? this.#value : [];
36
- }
37
- const [first, ...rest] = parts;
38
- const exact = this.matchChild(first, rest);
39
- const single = this.matchChild(this.wildcardOne, rest);
40
- const subtree = this.matchChild(this.wildcardSubtree, []);
41
- const results = exact.concat(single, subtree);
42
- return results;
43
- }
44
- add(key, value) {
45
- return this._add(key.split(this.separator), value);
46
- }
47
- _add(parts, value) {
48
- if (parts.length === 0) {
49
- this.#value = this.#value.concat(value);
50
- return;
51
- }
52
- const [first, ...rest] = parts;
53
- const child = this.#children.get(first);
54
- if (child instanceof Trie) {
55
- child._add(rest, value);
56
- }
57
- else {
58
- const node = new Trie(this.looseCompare);
59
- this.#children.set(first, node);
60
- node._add(rest, value);
61
- }
62
- return;
63
- }
64
- remove(key, value) {
65
- return this._remove(key.split(this.separator), value);
66
- }
67
- _remove(parts, value) {
68
- if (parts.length === 0) {
69
- const arr = this.#value || [];
70
- this.#value = arr.filter(this.filter(value));
71
- return;
72
- }
73
- const [first, ...rest] = parts;
74
- const node = this.#children.get(first);
75
- if (node) {
76
- node._remove(rest, value);
77
- if (node.#value.length === 0 && this.#children.size === 0) {
78
- this.#children.delete(first);
79
- }
80
- }
81
- }
82
- filter(value) {
83
- if (this.looseCompare && typeof value === "object") {
84
- return (item) => {
85
- for (const key in value) {
86
- if (value[key] !== item[key]) {
87
- return true;
88
- }
89
- }
90
- return false;
91
- };
92
- }
93
- return (item) => item !== value;
94
- }
95
- }
@@ -1 +0,0 @@
1
- {"root":["../bin/conformanceServer.ts","../bin/demoServer.ts","../bin/mqtt.ts"],"version":"5.7.3"}
@@ -1,75 +0,0 @@
1
- function formatValue(key, value, boolean, string) {
2
- if (key === "__proto__" || key === "constructor") {
3
- throw new Error(`Invalid argument: ${key}`);
4
- }
5
- if (boolean.includes(key)) {
6
- if (value !== "false") {
7
- return true;
8
- }
9
- return false;
10
- }
11
- if (!string.includes(key)) {
12
- const num = Number(value);
13
- if (!Number.isNaN(num)) {
14
- return num;
15
- }
16
- }
17
- return value;
18
- }
19
- function parseKeyValue(arg, alias) {
20
- if (arg.startsWith("--no-")) {
21
- const key = arg.slice(5);
22
- return [key, false];
23
- }
24
- if (arg.startsWith("--")) {
25
- const [key, value] = arg.slice(2).split("=", 2);
26
- return [key, value];
27
- }
28
- if (arg.startsWith("-")) {
29
- const [flag, value] = arg.slice(1).split("=", 2);
30
- const key = alias[flag] || flag;
31
- return [key, value];
32
- }
33
- return ["_", arg];
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
- */
49
- export function parseArgs(args, opts = {}) {
50
- const { alias = {}, boolean = [], string = [], default: defaults = {} } = opts;
51
- const result = { ...defaults };
52
- result["_"] = [];
53
- for (let i = 0; i < args.length; i++) {
54
- const [key, value] = parseKeyValue(args[i], alias);
55
- if (key === "_") {
56
- result["_"].push(formatValue(key, value, boolean, string));
57
- continue;
58
- }
59
- if (value === undefined) {
60
- const nextArg = args[i + 1];
61
- if (nextArg !== undefined &&
62
- !nextArg.startsWith("-") &&
63
- !boolean.includes(key)) {
64
- result[key] = formatValue(key, nextArg, boolean, string);
65
- i++;
66
- }
67
- else {
68
- result[key] = formatValue(key, value, boolean, string);
69
- }
70
- continue;
71
- }
72
- result[key] = formatValue(key, value, boolean, string);
73
- }
74
- return result;
75
- }
@@ -1,20 +0,0 @@
1
- export class AssertionError extends Error {
2
- /** Constructs a new instance. */
3
- constructor(message) {
4
- super(message);
5
- this.name = "AssertionError";
6
- }
7
- }
8
- /** assert(expr, msg) throws AssertionError if expr is falsy. */
9
- export function assert(expr, msg = "") {
10
- if (!expr) {
11
- throw new AssertionError(msg);
12
- }
13
- }
14
- export function assertArrayIncludes(actual, expected, msg) {
15
- const actualArr = Array.from(actual);
16
- const expectedArr = Array.from(expected);
17
- for (const item of expectedArr) {
18
- assert(actualArr.includes(item), msg + `\nExpected: ${expectedArr}\nActual: ${actualArr}`);
19
- }
20
- }
@@ -1,69 +0,0 @@
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
- * ```ts
8
- * const queue = new AsyncQueue<string>();
9
- * queue.push("hello");
10
- * for await (const item of queue) {
11
- * console.log(item); // "hello"
12
- * }
13
- * ```
14
- */
15
- export class AsyncQueue {
16
- queue = [];
17
- maxQueueLength = Infinity;
18
- nextResolve = (_value) => { };
19
- nextReject = (_reason) => { };
20
- done = false;
21
- hasNext = false;
22
- constructor(maxQueueLength) {
23
- if (maxQueueLength) {
24
- this.maxQueueLength = maxQueueLength;
25
- }
26
- }
27
- async next() {
28
- await nextTick();
29
- if (this.done && this.queue.length === 0) {
30
- return Promise.reject("Closed");
31
- }
32
- return new Promise((resolve, reject) => {
33
- if (this.queue.length > 0) {
34
- const item = this.queue.shift();
35
- if (item) {
36
- return resolve(item);
37
- }
38
- }
39
- this.nextResolve = resolve;
40
- this.nextReject = reject;
41
- this.hasNext = true;
42
- });
43
- }
44
- close(reason = "closed") {
45
- this.done = true;
46
- if (this.hasNext) {
47
- this.nextReject(reason);
48
- }
49
- }
50
- async *[Symbol.asyncIterator]() {
51
- while (true) {
52
- yield this.next();
53
- }
54
- }
55
- push(item) {
56
- if (this.hasNext) {
57
- this.nextResolve(item);
58
- this.hasNext = false;
59
- return;
60
- }
61
- if (this.queue.length > this.maxQueueLength) {
62
- this.queue.shift();
63
- }
64
- this.queue.push(item);
65
- }
66
- get isDone() {
67
- return this.done;
68
- }
69
- }
@@ -1,14 +0,0 @@
1
- /**
2
- * Create a promise that can be resolved/rejected later
3
- */
4
- export class Deferred {
5
- promise;
6
- resolve;
7
- reject;
8
- constructor() {
9
- this.promise = new Promise((resolve, reject) => {
10
- this.resolve = resolve;
11
- this.reject = reject;
12
- });
13
- }
14
- }
@@ -1,58 +0,0 @@
1
- /**
2
- * The available log levels
3
- */
4
- export const LogLevel = {
5
- error: 0,
6
- warn: 1,
7
- info: 2,
8
- verbose: 3,
9
- debug: 4,
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
- */
24
- export class Logger {
25
- defaultError = console.error;
26
- defaultWarn = console.warn;
27
- defaultInfo = console.info;
28
- defaultVerbose = console.log;
29
- defaultDebug = console.log;
30
- // deno-lint-ignore no-explicit-any
31
- noop = (..._data) => { };
32
- error = this.defaultError;
33
- warn = this.defaultWarn;
34
- info = this.defaultInfo;
35
- verbose = this.noop;
36
- debug = this.noop;
37
- constructor() { }
38
- level(logLevel) {
39
- this.warn = logLevel > 0 ? this.defaultWarn : this.noop;
40
- this.info = logLevel > 1 ? this.defaultInfo : this.noop;
41
- this.verbose = logLevel > 2 ? this.defaultVerbose : this.noop;
42
- this.debug = logLevel > 3 ? this.defaultDebug : this.noop;
43
- }
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
- */
58
- export const logger = new Logger();
package/dist/utils/mod.js DELETED
@@ -1,11 +0,0 @@
1
- /**
2
- * This module provides various utilities
3
- * @module
4
- */
5
- export { assert } from "./assert.js";
6
- export { Deferred } from "./deferred.js";
7
- export { Logger, logger, LogLevel } from "./logger.js";
8
- export { AsyncQueue } from "./asyncQueue.js";
9
- export { nextTick } from "./nextTick.js";
10
- export { parseArgs } from "./args.js";
11
- export { getArgs } from "./platform.js";
@@ -1,18 +0,0 @@
1
- /**
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
15
- */
16
- export function nextTick() {
17
- return new Promise((resolve) => setTimeout(resolve, 0));
18
- }
@@ -1,7 +0,0 @@
1
- import { argv } from "node:process";
2
- /**
3
- * @returns an array of command line arguments
4
- */
5
- export function getArgs() {
6
- return argv.slice(2);
7
- }
package/tsconfig.json DELETED
@@ -1,22 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- // Node 18
4
- "target": "ES2022",
5
- "module": "esnext",
6
- "moduleResolution": "node",
7
- "resolveJsonModule": true,
8
- "allowImportingTsExtensions": true,
9
- "rewriteRelativeImportExtensions": true,
10
- "strict": true,
11
- "outDir": "dist",
12
- "esModuleInterop": true,
13
- "isolatedModules": true,
14
- "skipLibCheck": true
15
- },
16
- "include": ["bin/*.ts"],
17
- "exclude": [
18
- "dist",
19
- "deno",
20
- "node_modules"
21
- ]
22
- }