@midwayjs/mqtt 3.15.8 → 3.16.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.
@@ -1,5 +1,5 @@
1
1
  import { BaseFramework } from '@midwayjs/core';
2
- import { IMidwayMQTTApplication, IMidwayMQTTConfigurationOptions, IMidwayMQTTContext } from './interface';
2
+ import { IMidwayMQTTApplication, IMidwayMQTTConfigurationOptions, IMidwayMQTTContext, IMqttSubscriber, MqttSubscriberOptions } from './interface';
3
3
  import { IClientOptions, MqttClient } from 'mqtt';
4
4
  export declare class MidwayMQTTFramework extends BaseFramework<IMidwayMQTTApplication, IMidwayMQTTContext, IMidwayMQTTConfigurationOptions> {
5
5
  app: IMidwayMQTTApplication;
@@ -9,7 +9,26 @@ export declare class MidwayMQTTFramework extends BaseFramework<IMidwayMQTTApplic
9
9
  applicationInitialize(options: any): Promise<void>;
10
10
  run(): Promise<void>;
11
11
  protected beforeStop(): Promise<void>;
12
- createSubscriber(connectionOptions: IClientOptions, clientName?: string): Promise<MqttClient>;
12
+ /**
13
+ * dynamic create subscriber
14
+ */
15
+ createSubscriber(
16
+ /**
17
+ * mqtt connection options
18
+ */
19
+ connectionOptions: IClientOptions,
20
+ /**
21
+ * mqtt subscribe options
22
+ */
23
+ subscribeOptions: MqttSubscriberOptions,
24
+ /**
25
+ * midway mqtt subscriber class
26
+ */
27
+ ClzProvider: new () => IMqttSubscriber,
28
+ /**
29
+ * midway mqtt component instance name, if not set, will be manager by your self
30
+ */
31
+ clientName?: string): Promise<MqttClient>;
13
32
  getSubscriber(name: string): MqttClient;
14
33
  getSubscribers(): MqttClient[];
15
34
  getFrameworkName(): string;
package/dist/framework.js CHANGED
@@ -37,20 +37,7 @@ let MidwayMQTTFramework = class MidwayMQTTFramework extends core_1.BaseFramework
37
37
  mqttSubscriberMap[subscriberName] = subscriberModule;
38
38
  }
39
39
  for (const customKey in sub) {
40
- const consumer = await this.createSubscriber(sub[customKey].connectOptions, customKey);
41
- await consumer.subscribeAsync(sub[customKey].subscribeOptions.topicObject, sub[customKey].subscribeOptions.opts);
42
- consumer.on('message', async (topic, message, packet) => {
43
- const ctx = this.app.createAnonymousContext();
44
- ctx.topic = topic;
45
- ctx.packet = packet;
46
- ctx.message = message;
47
- const fn = await this.applyMiddleware(async (ctx) => {
48
- const instance = await ctx.requestContext.getAsync(mqttSubscriberMap[customKey]);
49
- // eslint-disable-next-line prefer-spread
50
- return await instance['subscribe'].call(instance, ctx);
51
- });
52
- return await fn(ctx);
53
- });
40
+ await this.createSubscriber(sub[customKey].connectOptions, sub[customKey].subscribeOptions, mqttSubscriberMap[customKey], customKey);
54
41
  }
55
42
  }
56
43
  async beforeStop() {
@@ -59,8 +46,27 @@ let MidwayMQTTFramework = class MidwayMQTTFramework extends core_1.BaseFramework
59
46
  this.mqttLogger.info(`[midway-mqtt] subscriber: ${name} is closed`);
60
47
  }
61
48
  }
62
- async createSubscriber(connectionOptions, clientName) {
63
- return new Promise(resolve => {
49
+ /**
50
+ * dynamic create subscriber
51
+ */
52
+ async createSubscriber(
53
+ /**
54
+ * mqtt connection options
55
+ */
56
+ connectionOptions,
57
+ /**
58
+ * mqtt subscribe options
59
+ */
60
+ subscribeOptions,
61
+ /**
62
+ * midway mqtt subscriber class
63
+ */
64
+ ClzProvider,
65
+ /**
66
+ * midway mqtt component instance name, if not set, will be manager by your self
67
+ */
68
+ clientName) {
69
+ const consumer = await new Promise(resolve => {
64
70
  const client = (0, mqtt_1.connect)(connectionOptions);
65
71
  client.on('connect', () => {
66
72
  if (clientName) {
@@ -73,6 +79,20 @@ let MidwayMQTTFramework = class MidwayMQTTFramework extends core_1.BaseFramework
73
79
  this.mqttLogger.error(err);
74
80
  });
75
81
  });
82
+ consumer.on('message', async (topic, message, packet) => {
83
+ const ctx = this.app.createAnonymousContext();
84
+ ctx.topic = topic;
85
+ ctx.packet = packet;
86
+ ctx.message = message;
87
+ const fn = await this.applyMiddleware(async (ctx) => {
88
+ const instance = await ctx.requestContext.getAsync(ClzProvider);
89
+ // eslint-disable-next-line prefer-spread
90
+ return await instance['subscribe'].call(instance, ctx);
91
+ });
92
+ return await fn(ctx);
93
+ });
94
+ await consumer.subscribeAsync(subscribeOptions.topicObject, subscribeOptions.opts);
95
+ return consumer;
76
96
  }
77
97
  getSubscriber(name) {
78
98
  return this.subscriberMap.get(name);
package/dist/index.d.ts CHANGED
@@ -3,4 +3,5 @@ export { MidwayMQTTFramework as Framework } from './framework';
3
3
  export { MQTTConfiguration as Configuration } from './configuration';
4
4
  export * from './decorator';
5
5
  export * from './service';
6
+ export * as Mqtt from 'mqtt';
6
7
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.Configuration = exports.Framework = void 0;
17
+ exports.Mqtt = exports.Configuration = exports.Framework = void 0;
18
18
  __exportStar(require("./interface"), exports);
19
19
  var framework_1 = require("./framework");
20
20
  Object.defineProperty(exports, "Framework", { enumerable: true, get: function () { return framework_1.MidwayMQTTFramework; } });
@@ -22,4 +22,5 @@ var configuration_1 = require("./configuration");
22
22
  Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.MQTTConfiguration; } });
23
23
  __exportStar(require("./decorator"), exports);
24
24
  __exportStar(require("./service"), exports);
25
+ exports.Mqtt = require("mqtt");
25
26
  //# sourceMappingURL=index.js.map
@@ -25,6 +25,6 @@ export interface Context extends IMidwayMQTTContext {
25
25
  }
26
26
  export type NextFunction = BaseNextFunction;
27
27
  export interface IMqttSubscriber {
28
- subscribe(ctx: IMidwayMQTTContext): Promise<void>;
28
+ subscribe(ctx: IMidwayMQTTContext): Promise<any>;
29
29
  }
30
30
  //# sourceMappingURL=interface.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/mqtt",
3
- "version": "3.15.8",
3
+ "version": "3.16.2",
4
4
  "description": "Midway Framework for mqtt",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -23,11 +23,11 @@
23
23
  ],
24
24
  "license": "MIT",
25
25
  "devDependencies": {
26
- "@midwayjs/core": "^3.15.8",
27
- "@midwayjs/mock": "^3.15.8"
26
+ "@midwayjs/core": "^3.16.2",
27
+ "@midwayjs/mock": "^3.16.2"
28
28
  },
29
29
  "dependencies": {
30
- "mqtt": "5.5.1"
30
+ "mqtt": "5.5.5"
31
31
  },
32
32
  "author": "Harry Chen <czy88840616@gmail.com>",
33
33
  "repository": {
@@ -37,5 +37,5 @@
37
37
  "engines": {
38
38
  "node": ">=16"
39
39
  },
40
- "gitHead": "b9790dce4fac050c747893bc86dda0f044d827a9"
40
+ "gitHead": "45af90c8055ed62b4a0e811d3b5ce42a796b6d65"
41
41
  }