@ipsme/msgenv-mqtt 0.0.24 → 0.1.7

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.
package/package.json CHANGED
@@ -1,40 +1,45 @@
1
- {
2
- "name": "@ipsme/msgenv-mqtt",
3
- "version": "0.0.24",
4
- "description": "",
5
- "exports": {
6
- "node": {
7
- "require": "./dist/ipsme_msgenv.cjs.js"
8
- },
9
- "default": "./dist/ipsme_msgenv.cjs.js"
10
- },
11
- "type": "commonjs",
12
- "scripts": {
13
- "build": "rollup --config",
14
- "test": "node tests/test_ipsme_msgenv.cjs",
15
- "clean": "del dist && echo OK"
16
- },
17
- "repository": {
18
- "type": "git",
19
- "url": "git+https://github.com/IPSME/npm-msgenv-MQTT.git"
20
- },
21
- "keywords": [
22
- "ipsme"
23
- ],
24
- "author": "K.Nevelsteen, PhD",
25
- "license": "ISC",
26
- "bugs": {
27
- "url": "https://github.com/IPSME/npm-msgenv-MQTT/issues"
28
- },
29
- "homepage": "https://github.com/IPSME/npm-msgenv-MQTT#readme",
30
- "dependencies": {
31
- "@knev/bitlogr": "^0.2.1",
32
- "mqtt": "^5.1.4"
33
- },
34
- "devDependencies": {
35
- "@rollup/plugin-commonjs": "^28.0.1",
36
- "@rollup/plugin-node-resolve": "^15.0.1",
37
- "del-cli": "^6.0.0",
38
- "rollup": "^4.27.3"
39
- }
40
- }
1
+ {
2
+ "name": "@ipsme/msgenv-mqtt",
3
+ "version": "0.1.7",
4
+ "description": "",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./dist/ipsme_msgenv.d.ts",
8
+ "import": "./dist/ipsme_msgenv.es.mjs",
9
+ "require": "./dist/ipsme_msgenv.cjs.js"
10
+ }
11
+ },
12
+ "scripts": {
13
+ "build": "rollup --config",
14
+ "test": "node tests/test_ipsme_msgenv.cjs",
15
+ "clean": "del dist && echo OK"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/IPSME/npm-msgenv-MQTT.git"
20
+ },
21
+ "keywords": [
22
+ "ipsme"
23
+ ],
24
+ "author": "K.Nevelsteen, PhD",
25
+ "license": "ISC",
26
+ "bugs": {
27
+ "url": "https://github.com/IPSME/npm-msgenv-MQTT/issues"
28
+ },
29
+ "homepage": "https://github.com/IPSME/npm-msgenv-MQTT#readme",
30
+ "dependencies": {
31
+ "@knev/bitlogr": "^3.1.3",
32
+ "mqtt": "^5.14.1"
33
+ },
34
+ "devDependencies": {
35
+ "@rollup/plugin-commonjs": "^29.0.0",
36
+ "@rollup/plugin-json": "^6.1.0",
37
+ "@rollup/plugin-node-resolve": "^16.0.3",
38
+ "@rollup/plugin-typescript": "^12.3.0",
39
+ "@types/node": "^25.0.9",
40
+ "del-cli": "^7.0.0",
41
+ "rollup": "^4.55.3",
42
+ "tslib": "^2.8.1",
43
+ "typescript": "^5.9.3"
44
+ }
45
+ }
package/rollup.config.mjs CHANGED
@@ -1,24 +1,25 @@
1
- // import resolve from '@rollup/plugin-node-resolve';
2
- // import commonjs from '@rollup/plugin-commonjs';
3
- // import json from '@rollup/plugin-json';
1
+ import resolve from '@rollup/plugin-node-resolve';
2
+ import commonjs from '@rollup/plugin-commonjs';
3
+ import json from '@rollup/plugin-json';
4
+ import typescript from '@rollup/plugin-typescript';
4
5
 
5
6
  export default [
6
7
  {
7
- input: 'src/ipsme_msgenv.cjs',
8
- output: {
9
- name: "ipsme_msgenv",
10
- file: 'dist/ipsme_msgenv.cjs.js',
11
- format: 'cjs'
12
- },
13
- // plugins: [resolve()]
14
- },
15
- // {
16
- // input: 'src/ipsme_msgenv.cjs',
17
- // output: {
18
- // name: "ipsme_msgenv",
19
- // file: 'dist/ipsme_msgenv.es.mjs',
20
- // format: 'es'
21
- // },
22
- // plugins: [resolve(), commonjs()]
23
- // }
8
+ input: 'src/ipsme_msgenv.ts',
9
+ output: [
10
+ { file: 'dist/ipsme_msgenv.cjs.js', format: 'cjs' },
11
+ { file: 'dist/ipsme_msgenv.es.mjs', format: 'es' },
12
+ ],
13
+ plugins: [
14
+ resolve({ preferBuiltins: true }),
15
+ commonjs(),
16
+ json(),
17
+ typescript({ tsconfig: './tsconfig.json' })
18
+ ],
19
+ onwarn(warning, warn) {
20
+ if (warning.code === 'THIS_IS_UNDEFINED') return;
21
+ if (warning.code === 'CIRCULAR_DEPENDENCY') return;
22
+ warn(warning);
23
+ }
24
+ }
24
25
  ];
@@ -1,34 +1,28 @@
1
1
 
2
- const mqtt = require('mqtt');
3
- const { BitLogr } = require ('@knev/bitlogr');
2
+ import { LOGR, l_array } from '@knev/bitlogr';
3
+ import mqtt, { MqttClient, IClientOptions } from 'mqtt';
4
+
5
+ // const mqtt = require('mqtt');
6
+ // const { BitLogr } = require ('@knev/bitlogr');
4
7
 
5
8
  // https://www.tutorialsteacher.com/nodejs/nodejs-module-exports
6
9
 
7
10
  //-------------------------------------------------------------------------------------------------
8
11
 
9
- let LOGR_= new BitLogr();
10
-
11
- const l_ = {
12
- MsgEnv : 0b1 << 0, // MsgEnv
13
- CXNS : 0b1 << 1, // connections
14
- REFL : 0b1 << 2, // reflection
15
- }
16
- LOGR_.labels= l_;
17
-
18
- // https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript
19
- const __name = obj => Object.keys(obj)[0];
20
- // console.log('OUT', __name({variableName}) );
12
+ const LOGR_= LOGR.get_instance();
13
+ const logr_= LOGR_.create({ labels: l_array(['CONNECTIONS', 'REFLECTION']) });
14
+ const l_= logr_.l;
21
15
 
22
16
  //-------------------------------------------------------------------------------------------------
23
17
 
24
- var cfg_= (function() {
25
- let _options= {};
18
+ interface MsgEnvOptions {
19
+ channel?: string;
20
+ prefix?: string;
21
+ // logr?: any; // uncomment + type if you bring BitLogr back
22
+ }
26
23
 
27
- // options= {
28
- // channel : 'IPSME',
29
- // prefix : '',
30
- // logr : ...
31
- // }
24
+ var cfg_= (function() {
25
+ let _options: MsgEnvOptions = {};
32
26
 
33
27
  return {
34
28
  get channel() {
@@ -40,8 +34,8 @@ var cfg_= (function() {
40
34
  get options() { return _options; },
41
35
  set options(obj) {
42
36
  _options= obj;
43
- if (_options.logr && _options.logr[ __name(l_) ] )
44
- LOGR_.toggled= _options.logr
37
+ // if (_options.logr && _options.logr[ __name(l_) ] )
38
+ // // LOGR_.toggled= _options.logr
45
39
  }
46
40
  }
47
41
  })();
@@ -49,13 +43,19 @@ var cfg_= (function() {
49
43
  //-------------------------------------------------------------------------------------------------
50
44
  // MsgEnv:
51
45
 
52
- let singleton;
53
-
54
46
  const kstr_TOPIC = 'IPSME';
55
47
 
56
- class Singleton {
48
+ class MsgEnvSingleton {
49
+ private static _instance: MsgEnvSingleton | null = null;
50
+
51
+ public readonly client: MqttClient;
52
+
57
53
  constructor() {
58
- this.client = mqtt.connect('mqtt://localhost:1883');
54
+ this.client = mqtt.connect({
55
+ protocol: 'mqtt',
56
+ hostname: 'localhost', // or '127.0.0.1'
57
+ port: 1883
58
+ });
59
59
 
60
60
  this.client.on('error', function (err) {
61
61
  console.error('MQTT Connection Error:', err);
@@ -66,11 +66,11 @@ class Singleton {
66
66
  });
67
67
  }
68
68
 
69
- static getInstance() {
70
- if (!singleton) {
71
- singleton = new Singleton();
69
+ public static getInstance() : MsgEnvSingleton {
70
+ if (! MsgEnvSingleton._instance) {
71
+ MsgEnvSingleton._instance = new MsgEnvSingleton();
72
72
  }
73
- return singleton;
73
+ return MsgEnvSingleton._instance;
74
74
  }
75
75
  }
76
76
 
@@ -79,13 +79,13 @@ class Singleton {
79
79
  function subscribe_(handler) {
80
80
  if (handler.subscription_ID !== undefined)
81
81
  return;
82
- LOGR_.log(l_.CXNS, cfg_.prefix +'MsgEnv: subscribe');
82
+ logr_.log(l_.CONNECTIONS, () => [cfg_.prefix +'MsgEnv: subscribe'] );
83
83
  // handler.subscription_ID= systemPreferences.subscribeNotification(cfg_.channel, function(event, userInfo, object) {
84
84
  // LOGR_.log(l_.REFL, cfg_.prefix +'MsgEnv: onNotification: ', userInfo.msg);
85
85
  // this(userInfo.msg);
86
86
  // }.bind(handler));
87
87
 
88
- let instance = Singleton.getInstance();
88
+ let instance = MsgEnvSingleton.getInstance();
89
89
  handler.subscription_ID = function (topic, message) {
90
90
  handler(message.toString());
91
91
  };
@@ -96,9 +96,9 @@ function subscribe_(handler) {
96
96
 
97
97
  // we have to use the ID, rather than the handler itself to unsubsribe
98
98
  function unsubscribe_(handler) {
99
- LOGR_.log(l_.CXNS, cfg_.prefix +'MsgEnv: unsubscribe');
99
+ logr_.log(l_.CONNECTIONS, () => [cfg_.prefix +'MsgEnv: unsubscribe'] );
100
100
  // systemPreferences.unsubscribeNotification(handler.subscription_ID);
101
- let instance = Singleton.getInstance();
101
+ let instance = MsgEnvSingleton.getInstance();
102
102
  instance.client.removeListener('message', handler.subscription_ID);
103
103
  instance.client.unsubscribe(kstr_TOPIC);
104
104
  delete handler.subscription_ID;
@@ -107,18 +107,18 @@ function unsubscribe_(handler) {
107
107
  // Normally a {name, object, userInfo} tuple, but in electron it is apparently not possible
108
108
  // to specify the (sender) object explicitly; gets set automagically?!
109
109
  function publish_(msg) {
110
- LOGR_.log(l_.REFL, cfg_.prefix +'MsgEnv: postNotification: ', msg);
110
+ logr_.log(l_.REFLECTION, () => [cfg_.prefix +'MsgEnv: postNotification: ', msg] );
111
111
  // systemPreferences.postNotification(cfg_.channel, { "msg" : msg }, true);
112
- let instance = Singleton.getInstance();
112
+ let instance = MsgEnvSingleton.getInstance();
113
113
  instance.client.publish(kstr_TOPIC, msg);
114
114
  }
115
115
 
116
116
  //-------------------------------------------------------------------------------------------------
117
117
 
118
- module.exports= {
119
- config : cfg_,
120
- subscribe : subscribe_,
121
- unsubscribe : unsubscribe_,
122
- publish : publish_,
123
- l : l_,
118
+ export {
119
+ cfg_ as config,
120
+ subscribe_ as subscribe,
121
+ unsubscribe_ as unsubscribe,
122
+ publish_ as publish,
123
+ logr_ as logr
124
124
  }
package/tsconfig.json ADDED
@@ -0,0 +1,115 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Visit https://aka.ms/tsconfig to read more about this file */
4
+
5
+ /* Projects */
6
+ // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
7
+ // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
8
+ // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
9
+ // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
10
+ // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
11
+ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12
+
13
+ /* Language and Environment */
14
+ "target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
15
+ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
16
+ // "jsx": "preserve", /* Specify what JSX code is generated. */
17
+ // "libReplacement": true, /* Enable lib replacement. */
18
+ // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
19
+ // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
20
+ // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
21
+ // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
22
+ // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
23
+ // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
24
+ // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
25
+ // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
26
+ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
27
+
28
+ /* Modules */
29
+ "module": "ESNext", /* Specify what module code is generated. */
30
+ "rootDir": "./src", /* Specify the root folder within your source files. */
31
+ "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
32
+ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
33
+ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
34
+ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
35
+ // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
36
+ "types": [ "node" ], /* Specify type package names to be included without being referenced in a source file. */
37
+ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
38
+ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
39
+ // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
40
+ // "rewriteRelativeImportExtensions": true, /* Rewrite '.ts', '.tsx', '.mts', and '.cts' file extensions in relative import paths to their JavaScript equivalent in output files. */
41
+ // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
42
+ // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
43
+ // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
44
+ // "noUncheckedSideEffectImports": true, /* Check side effect imports. */
45
+ // "resolveJsonModule": true, /* Enable importing .json files. */
46
+ // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
47
+ // "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
48
+
49
+ /* JavaScript Support */
50
+ "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
51
+ "checkJs": false, /* Enable error reporting in type-checked JavaScript files. */
52
+ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
53
+
54
+ /* Emit */
55
+ "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
56
+ // "declarationMap": true, /* Create sourcemaps for d.ts files. */
57
+ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
58
+ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
59
+ // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
60
+ // "noEmit": true, /* Disable emitting files from a compilation. */
61
+ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
62
+ "outDir": "./dist", /* Specify an output folder for all emitted files. */
63
+ // "removeComments": true, /* Disable emitting comments. */
64
+ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
65
+ // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
66
+ // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
67
+ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
68
+ // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
69
+ // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
70
+ // "newLine": "crlf", /* Set the newline character for emitting files. */
71
+ // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
72
+ // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
73
+ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
74
+ // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
75
+ "declarationDir": "./dist", /* Specify the output directory for generated declaration files. */
76
+
77
+ /* Interop Constraints */
78
+ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
79
+ // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
80
+ // "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
81
+ // "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
82
+ // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
83
+ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
84
+ // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
85
+ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
86
+
87
+ /* Type Checking */
88
+ "strict": false, /* Enable all strict type-checking options. */
89
+ // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
90
+ // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
91
+ // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
92
+ // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
93
+ // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
94
+ // "strictBuiltinIteratorReturn": true, /* Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'. */
95
+ // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
96
+ // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
97
+ // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
98
+ // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
99
+ // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
100
+ // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
101
+ // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
102
+ // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
103
+ // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
104
+ // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
105
+ // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
106
+ // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
107
+ // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
108
+
109
+ /* Completeness */
110
+ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
111
+ "skipLibCheck": true /* Skip type checking all .d.ts files. */
112
+ },
113
+ "include": ["src/**/*"],
114
+ "exclude": ["node_modules", "dist"]
115
+ }