mm_mqtt_server 1.0.1 → 1.0.2

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # mm_mqtt_server
2
2
 
3
- MQTT 代理服务器入站端口模块。继承 [mm_corejs](https://gitee.com/qiuwenwu91/mm_corejs) 的 `Mod` 基类,基于 [aedes](https://www.npmjs.com/package/aedes) 实现 MQTT Broker,提供连接管理、消息处理管道与 JSON-RPC 2.0 消息插件,可脱离完整框架独立运行。
3
+ MQTT 代理服务器入站适配器模块。继承 [mm_corejs](https://gitee.com/qiuwenwu91/mm_corejs) 的 `Mod` 基类,基于 [aedes](https://www.npmjs.com/package/aedes) 实现 MQTT Broker,提供连接管理、消息处理管道与 JSON-RPC 2.0 消息插件,可脱离完整框架独立运行。
4
4
 
5
5
  - 入口:`index.js`(CommonJS)
6
6
  - 导出:`MqttServer`(代理模块)、`MessagePlugin`(JSON-RPC 消息插件)
package/index.js CHANGED
@@ -2,8 +2,25 @@
2
2
 
3
3
  const MqttServer = require('./lib/mqtt_server');
4
4
  const MessagePlugin = require('./lib/message');
5
+ const Admin = require('mm_corejs').Admin;
6
+
7
+ const admin = new Admin();
8
+ admin.setClass(MqttServer);
5
9
 
6
10
  module.exports = {
7
11
  MqttServer,
8
- MessagePlugin
12
+ MessagePlugin,
13
+ admin,
14
+ /**
15
+ * 获取指定作用域的 MQTT 服务器
16
+ * @param {string} [scope='sys'] 作用域名称
17
+ * @param {object} [config] 配置参数
18
+ * @returns {MqttServer} MQTT 服务器实例
19
+ */
20
+ mqttServerAdmin(scope, config) {
21
+ if (!scope) {
22
+ scope = 'os';
23
+ }
24
+ return admin.get(scope, { name: scope, ...config });
25
+ }
9
26
  };
package/lib/message.js CHANGED
@@ -85,8 +85,8 @@ class MessagePlugin {
85
85
  throw new TypeError('[MessagePlugin] server must provide getHandle()');
86
86
  }
87
87
  this.#server = server;
88
- server.getHandle().on('main', (ctx, params, options) => {
89
- return this._execute(ctx, params, options);
88
+ server.getHandle().on('main', (params, ctx, options) => {
89
+ return this._execute(params, ctx, options);
90
90
  }, 100, 'json_rpc');
91
91
  return this;
92
92
  }
@@ -164,12 +164,12 @@ class MessagePlugin {
164
164
 
165
165
  /**
166
166
  * 内置 main 钩子逻辑:优先识别客户端反馈,其次执行 RPC 请求并产出应答重发描述
167
- * @param {Object} ctx 执行上下文
168
167
  * @param {Object} params 消息参数,含 client_id / topic / payload
168
+ * @param {Object} ctx 执行上下文
169
169
  * @param {Object} [options] 管道请求选项(连接级,如 user_id / device_id / agent_id),透传给处理函数
170
170
  * @returns {Promise<Object|undefined>} 重发描述 { topic, payload, qos, retain };反馈、透传与无结果通知时为 undefined
171
171
  */
172
- async _execute(ctx, params, options) {
172
+ async _execute(params, ctx, options) {
173
173
  if (this.#rpc.acceptResponse(params.payload)) return undefined;
174
174
  const extra = { client_id: params.client_id, topic: params.topic };
175
175
  const response = await this.#rpc.handle(params.payload, extra, options);
@@ -2,7 +2,8 @@
2
2
 
3
3
  const net = require('net');
4
4
  const { Aedes } = require('aedes');
5
- const { Mod, Handle, Store } = require('mm_corejs');
5
+ const { Mod, Store } = require('mm_corejs');
6
+ const { Handle } = require('mm_handle');
6
7
 
7
8
  /**
8
9
  * 服务器设置校验 schema(校验对象为顶层 config)
@@ -19,7 +20,7 @@ const SERVER_SCHEMA = {
19
20
  };
20
21
 
21
22
  /**
22
- * MqttServer —— MQTT 代理服务器(入站端口模块)。
23
+ * MqttServer —— MQTT 代理服务器(入站适配器模块)。
23
24
  *
24
25
  * 继承 mm_corejs 的 Mod:生命周期由 Lifecycle 模板方法驱动,
25
26
  * 调用参数校验由 config.params 声明式定义;
@@ -50,7 +51,7 @@ class MqttServer extends Mod {
50
51
  * 模块默认配置
51
52
  */
52
53
  static config = {
53
- name: 'mqtt_server',
54
+ name: 'os',
54
55
  title: 'MQTT代理服务器模块',
55
56
  description: '基于 aedes 的 MQTT 代理:管理客户端连接、主题订阅与消息路由',
56
57
  version: '1.0.0',
@@ -92,6 +93,7 @@ class MqttServer extends Mod {
92
93
 
93
94
  /**
94
95
  * 消息处理管道(before/check/main/render/error/success/after)
96
+ * 每个实例独立 Handle,避免多实例共享管道导致钩子泄漏
95
97
  */
96
98
  #handle = new Handle();
97
99
 
@@ -136,12 +138,12 @@ class MqttServer extends Mod {
136
138
  // ---------- 执行入口 ----------
137
139
  /**
138
140
  * 模块主逻辑:按 action 派发操作
139
- * @param {Object} ctx 执行上下文
140
141
  * @param {Object} params 调用参数,含 action 及操作所需字段
142
+ * @param {Object} ctx 执行上下文
141
143
  * @param {Object} options 合并后的选项
142
144
  * @returns {Promise<Object>} 操作结果
143
145
  */
144
- async main(ctx, params, options) {
146
+ async main(params, ctx, options) {
145
147
  const action = params.action;
146
148
  if (action === 'publish') return this._actionPublish(params);
147
149
  if (action === 'close_client') return this._actionCloseClient(params);
@@ -525,7 +527,7 @@ class MqttServer extends Mod {
525
527
  * 装配内置管道钩子:main 发 message 事件保持向后兼容
526
528
  */
527
529
  _initHandle() {
528
- this.#handle.on('main', (ctx, params) => {
530
+ this.#handle.on('main', (params, ctx) => {
529
531
  this._safeEmit('message', params.client_id, {
530
532
  topic: params.topic,
531
533
  payload: params.payload,
package/package.json CHANGED
@@ -1,18 +1,21 @@
1
1
  {
2
2
  "name": "mm_mqtt_server",
3
- "version": "1.0.1",
4
- "description": "MQTT 代理服务器入站端口模块,基于 aedes,继承 mm_corejs 的 Mod 基类,支持独立运行。",
3
+ "version": "1.0.2",
4
+ "description": "MQTT 代理服务器入站适配器模块,基于 aedes,继承 mm_corejs 的 Mod 基类,支持独立运行。",
5
5
  "keywords": [
6
6
  "超级美眉",
7
- "入站端口",
7
+ "入站适配器",
8
8
  "mm_mod",
9
- "mm_in_port",
9
+ "mm_adapter",
10
+ "mm_in_adapter",
10
11
  "mm_receiver",
11
12
  "mqtt",
12
13
  "broker",
13
14
  "aedes",
14
15
  "server"
15
16
  ],
17
+ "layer": "adapter",
18
+ "group": "in_adapter",
16
19
  "license": "ISC",
17
20
  "author": "qww",
18
21
  "type": "commonjs",
@@ -39,7 +42,7 @@
39
42
  },
40
43
  "dependencies": {
41
44
  "aedes": "^1.2.0",
42
- "mm_corejs": "^1.1.0"
45
+ "mm_corejs": "^1.1.4"
43
46
  },
44
47
  "devDependencies": {
45
48
  "jest": "^30.5.2"