tmc.js 0.3.3 → 0.3.4

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
@@ -60,6 +60,24 @@ new Tmc('your_app_key', 'your_app_secret')
60
60
 
61
61
  消费端发起建立连接 `onopen` 事件,`address` 默认为 `ws://mc.api.taobao.com/`。
62
62
 
63
+ **`tmc.send(msg: Message, options?: { mask?: true, binary?: true }, cb?: (err: Error) => void) => void`**
64
+
65
+ 实例化后,当自动应答确认消息无法满足需求的时候,比如消息处理失败,需要`Publisher`再次重推消息,在实例初始化时置`options.autoReplyConfirmation=false`,则在消息处理函数内,可以通过 `this.send()` 函数回复`确认`或者`失败`消息。例如:
66
+
67
+ ```js
68
+ new Tmc('your_app_key', 'your_app_secret', { autoReplyConfirmation: false })
69
+ .taobao_trade_TradeDelayConfirmPay(function needDoubleReriesThenConfirm(msg) {
70
+ if (msg instanceof Message && msg.content?.reried === 0) {
71
+ this.send(
72
+ new Message(MessageType.SENDACK, MessageKind.Failed)
73
+ .with(MessageFields.ID, msg.content?.id)
74
+ .with(MessageFields.MSG, 'Something went wrong, please retries this ID.')
75
+ );
76
+ }
77
+ })
78
+ .connect();
79
+ ```
80
+
63
81
  <details><summary>可选设置的 NODE_DEBUG=< label > 环境变量</summary>
64
82
 
65
83
  | label | 说明 |
@@ -69,7 +87,7 @@ new Tmc('your_app_key', 'your_app_secret')
69
87
  | `tmc:onpull` | 开启 `onpull` 时的日志
70
88
  | `tmc:onerror` | 开启 `onerror` 时的日志
71
89
  | `tmc:onclose` | 开启 `onclose` 时的日志
72
- | `tmc:onmessage` | 开启全部 `onmessage` 时的日志(即`From`淘宝消息)
90
+ | `tmc:onmessage*` | 开启全部 `onmessage` 时的日志(即`From`淘宝消息)
73
91
  | `tmc:onmessage:connect` | 开启消费端发起连接 `connect` 时的日志
74
92
  | `tmc:onmessage:connectack` | 开启消费端回复连接 `connectack` 时的日志
75
93
  | `tmc:onmessage:send` | 开启消费端接收到(即`From`淘宝) `send` 的消息时的日志
package/lib/consumer.js CHANGED
@@ -92,6 +92,11 @@ class TaoMessageConsumer extends EventEmitter {
92
92
  ).update(sk).digest('hex').toUpperCase();
93
93
  }
94
94
 
95
+ /** @since v0.3.4 */
96
+ send(data, options = { mask: true, binary: true }, cb = undefined) {
97
+ if (data instanceof Message) { this[kWebSocket]?.send(data.with(TOKEN, this[kToken]).buffer, options, cb); }
98
+ }
99
+
95
100
  onopen() {
96
101
  const now = `${Date.now()}`;
97
102
  const msg = new Message(CONNECT)
@@ -104,7 +109,7 @@ class TaoMessageConsumer extends EventEmitter {
104
109
 
105
110
  logger.onopen(JSONB.stringify(msg));
106
111
 
107
- this[kWebSocket]?.send(msg.buffer, { mask: true, binary: true });
112
+ this.send(msg);
108
113
 
109
114
  if (this[kOptions].intervalId) { clearInterval(this[kOptions].intervalId); }
110
115
 
@@ -114,7 +119,7 @@ class TaoMessageConsumer extends EventEmitter {
114
119
  onpull() {
115
120
  const msg = new Message(SEND, PullRequest).with(TOKEN, this[kToken]);
116
121
  logger.onpull(JSONB.stringify(msg));
117
- this[kWebSocket]?.send(msg.buffer, { mask: true, binary: true });
122
+ this.send(msg);
118
123
  }
119
124
 
120
125
  onping(data) {
@@ -142,7 +147,7 @@ class TaoMessageConsumer extends EventEmitter {
142
147
  if (this[kOptions].autoReplyConfirmation && msg && msg[CONTENT] && msg[CONTENT][ID]) {
143
148
  const reply = new Message(SEND, Confirm).with(TOKEN, this[kToken]).with(ID, msg[CONTENT][ID]);
144
149
  logger.onmessage[SEND][Confirm](JSONB.stringify(reply));
145
- this[kWebSocket]?.send(reply.buffer, { mask: true, binary: true });
150
+ this.send(reply);
146
151
  }
147
152
  }
148
153
 
@@ -178,6 +183,7 @@ class TaoMessageConsumer extends EventEmitter {
178
183
  if (this[kOptions].intervalId) { this[kOptions].intervalId = clearInterval(this[kOptions].intervalId); }
179
184
  if (this[kOptions].timeoutId) { this[kOptions].timeoutId = clearTimeout(this[kOptions].timeoutId); }
180
185
 
186
+ this[kToken] = undefined;
181
187
  this[kWebSocket] = undefined;
182
188
  this[kOptions].timeoutId = setTimeout(() => this.connect(), ms);
183
189
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tmc.js",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "Events driven and chained Taobao Message Channel(TMC) for NodeJS",
5
5
  "author": "James ZHANG",
6
6
  "license": "MIT",
package/types/index.d.ts CHANGED
@@ -26,6 +26,10 @@ declare interface ConsumerOptions {
26
26
  declare interface TaoMessageConstractor extends EventEmitter {
27
27
  new (appKey: string, appSecret: BinaryLike, groupName?: string | ConsumerOptions, options?: ConsumerOptions): TaoMessageConsumer;
28
28
  sign(timestamp: string): string;
29
+ /** @since v0.3.4 */
30
+ send(data: Message, cb?: (err?: Error) => void): void;
31
+ /** @since v0.3.4 */
32
+ send(data: Message, options?: { mask?: true, binary?: true, compress?: boolean, fin?: boolean }, cb?: (err: Error) => void): void;
29
33
  onopen(): void;
30
34
  onpull(): void;
31
35
  onping(data: Buffer): void;