@seriousme/opifex 1.1.0 → 1.2.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.
@@ -0,0 +1,115 @@
1
+ import { argv } from "node:process";
2
+ import { Writable } from "node:stream";
3
+
4
+ function formatValue(key, value, boolean, string) {
5
+ if (key === "__proto__" || key === "constructor") {
6
+ throw new Error(`Invalid argument: ${key}`);
7
+ }
8
+ if (boolean.includes(key)) {
9
+ if (value !== "false") {
10
+ return true;
11
+ }
12
+ return false;
13
+ }
14
+ if (!string.includes(key)) {
15
+ const num = Number(value);
16
+ if (!Number.isNaN(num)) {
17
+ return num;
18
+ }
19
+ }
20
+ return value;
21
+ }
22
+ function parseKeyValue(arg, alias) {
23
+ if (arg.startsWith("--no-")) {
24
+ const key = arg.slice(5);
25
+ return [key, false];
26
+ }
27
+ if (arg.startsWith("--")) {
28
+ const [key, value] = arg.slice(2).split("=", 2);
29
+ return [key, value];
30
+ }
31
+ if (arg.startsWith("-")) {
32
+ const [flag, value] = arg.slice(1).split("=", 2);
33
+ const key = alias[flag] || flag;
34
+ return [key, value];
35
+ }
36
+ return ["_", arg];
37
+ }
38
+ function parseArgs(args, opts = {}) {
39
+ const { alias = {}, boolean = [], string = [], default: defaults = {} } =
40
+ opts;
41
+ const result = { ...defaults };
42
+ result["_"] = [];
43
+ for (let i = 0; i < args.length; i++) {
44
+ const [key, value] = parseKeyValue(args[i], alias);
45
+ if (key === "_") {
46
+ result["_"].push(formatValue(key, value, boolean, string));
47
+ continue;
48
+ }
49
+ if (value === void 0) {
50
+ const nextArg = args[i + 1];
51
+ if (
52
+ nextArg !== void 0 && !nextArg.startsWith("-") && !boolean.includes(key)
53
+ ) {
54
+ result[key] = formatValue(key, nextArg, boolean, string);
55
+ i++;
56
+ } else {
57
+ result[key] = formatValue(key, value, boolean, string);
58
+ }
59
+ continue;
60
+ }
61
+ result[key] = formatValue(key, value, boolean, string);
62
+ }
63
+ return result;
64
+ }
65
+
66
+ function getArgs() {
67
+ return argv.slice(2);
68
+ }
69
+
70
+ function closer(sock) {
71
+ if (!sock.closed) {
72
+ sock.end();
73
+ }
74
+ }
75
+ function wrapNodeSocket(socket) {
76
+ const readable = new ReadableStream(
77
+ {
78
+ type: "bytes",
79
+ start(controller) {
80
+ socket.on("data", (data) => {
81
+ controller.enqueue(data);
82
+ const desiredSize = controller.desiredSize ?? 0;
83
+ if (desiredSize <= 0) {
84
+ socket.pause();
85
+ }
86
+ });
87
+ socket.on("error", (err) => controller.error(err));
88
+ socket.on("end", () => {
89
+ controller.byobRequest?.respond(1);
90
+ controller.close();
91
+ });
92
+ },
93
+ pull: () => {
94
+ socket.resume();
95
+ },
96
+ cancel: () => {
97
+ socket.end();
98
+ },
99
+ },
100
+ );
101
+ const writable = Writable.toWeb(socket);
102
+ const remoteAddr = {
103
+ hostname: socket.remoteAddress || "",
104
+ port: socket.remotePort || 0,
105
+ };
106
+ const conn = {
107
+ readable,
108
+ writable,
109
+ closer: () => closer(socket),
110
+ remoteAddr,
111
+ };
112
+ return conn;
113
+ }
114
+
115
+ export { getArgs as g, parseArgs as p, wrapNodeSocket as w };
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@seriousme/opifex",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "MQTT client & server for Deno & NodeJS",
5
5
  "type": "module",
6
- "main": "index.js",
7
6
  "scripts": {
8
- "test": "node --experimental-strip-types --test --test-skip-pattern='/^Deno/'"
7
+ "test": "node --experimental-strip-types --test --test-skip-pattern='/^Deno/'",
8
+ "build": "npx pkgroll --clean-dist --src .",
9
+ "postbuild": "deno fmt"
9
10
  },
10
11
  "keywords": [
11
12
  "MQTT",
@@ -13,8 +14,12 @@
13
14
  "mqtt-broker"
14
15
  ],
15
16
  "bin": {
16
- "mqtt": "bin/mqtt.ts",
17
- "demoServer": "bin/demoServer.ts"
17
+ "mqtt": "dist/bin/mqtt.js",
18
+ "demoServer": "dist/bin/demoServer.js"
19
+ },
20
+ "exports": {
21
+ "./server": "./dist/server/mod.js",
22
+ "./client": "./dist/client/mod.js"
18
23
  },
19
24
  "author": "Hans Klunder",
20
25
  "license": "MIT",
@@ -26,10 +31,7 @@
26
31
  "url": "https://github.com/seriousme/opifex/issues"
27
32
  },
28
33
  "homepage": "https://github.com/seriousme/opifex#readme",
29
- "engines": {
30
- "node": ">=22.6.0"
31
- },
32
- "dependencies": {
34
+ "devDependencies": {
33
35
  "@types/node": "^22.8.6"
34
36
  }
35
37
  }
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ // Node 18
4
+ "target": "ES2022",
5
+ "module": "esnext",
6
+ "moduleResolution": "node",
7
+ "resolveJsonModule": true,
8
+ "strict": true,
9
+ "outDir": "dist",
10
+ "esModuleInterop": true,
11
+ "isolatedModules": true,
12
+ "skipLibCheck": true
13
+ },
14
+ "exclude": [
15
+ "dist",
16
+ "deno",
17
+ "node_modules"
18
+ ]
19
+ }