tmc.js 0.3.4 → 0.3.5

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
@@ -67,7 +67,7 @@ new Tmc('your_app_key', 'your_app_secret')
67
67
  ```js
68
68
  new Tmc('your_app_key', 'your_app_secret', { autoReplyConfirmation: false })
69
69
  .taobao_trade_TradeDelayConfirmPay(function needDoubleReriesThenConfirm(msg) {
70
- if (msg instanceof Message && msg.content?.reried === 0) {
70
+ if (msg instanceof Message && msg.content?.retried === 0) {
71
71
  this.send(
72
72
  new Message(MessageType.SENDACK, MessageKind.Failed)
73
73
  .with(MessageFields.ID, msg.content?.id)
package/lib/consumer.js CHANGED
@@ -3,7 +3,7 @@ const { networkInterfaces } = require('os');
3
3
  const { EventEmitter, captureRejectionSymbol } = require('events');
4
4
 
5
5
  const WebSocket = require('ws');
6
- const JSONB = require('json-bigint')({ useNativeBigInt: true });
6
+ const { stringify, parse } = require('json-bigint')({ useNativeBigInt: true });
7
7
 
8
8
  const logger = require('./logger');
9
9
  const Message = require('./message');
@@ -92,7 +92,13 @@ class TaoMessageConsumer extends EventEmitter {
92
92
  ).update(sk).digest('hex').toUpperCase();
93
93
  }
94
94
 
95
- /** @since v0.3.4 */
95
+ /**
96
+ * @param {Message} data - The data
97
+ * @param {object|Function} [options] - The options
98
+ * @param {Function} [cb] - The Callback
99
+ * @returns {void}
100
+ * @since v0.3.4
101
+ */
96
102
  send(data, options = { mask: true, binary: true }, cb = undefined) {
97
103
  if (data instanceof Message) { this[kWebSocket]?.send(data.with(TOKEN, this[kToken]).buffer, options, cb); }
98
104
  }
@@ -107,7 +113,7 @@ class TaoMessageConsumer extends EventEmitter {
107
113
  .with(SDK, this.constructor.name)
108
114
  .with(INTRANET_IP, ipAddr());
109
115
 
110
- logger.onopen(JSONB.stringify(msg));
116
+ logger.onopen(stringify(msg));
111
117
 
112
118
  this.send(msg);
113
119
 
@@ -118,22 +124,22 @@ class TaoMessageConsumer extends EventEmitter {
118
124
 
119
125
  onpull() {
120
126
  const msg = new Message(SEND, PullRequest).with(TOKEN, this[kToken]);
121
- logger.onpull(JSONB.stringify(msg));
127
+ logger.onpull(stringify(msg));
122
128
  this.send(msg);
123
129
  }
124
130
 
125
131
  onping(data) {
126
- logger.onping('The channel is onping with data [%s].', JSONB.stringify(data));
127
- this[kWebSocket]?.pong(data, { mask: true }, true);
132
+ logger.onping('The channel is onping with data [%s].', stringify(data));
133
+ this[kWebSocket]?.pong(data, true);
128
134
  }
129
135
 
130
136
  onerror(err) {
131
- logger.onerror('The channel is onerror(%s), let us reconnect.', JSONB.stringify(err));
137
+ logger.onerror('The channel is onerror(%s), let us reconnect.', stringify(err));
132
138
  this.reconnect(this[kOptions].onErrorReconnection);
133
139
  }
134
140
 
135
141
  onclose(...arg) {
136
- logger.onclose('The channel is onclose(%s), let us reconnect.', JSONB.stringify(arg));
142
+ logger.onclose('The channel is onclose(%s), let us reconnect.', stringify(arg));
137
143
  this.reconnect(this[kOptions].onCloseReconnection);
138
144
  }
139
145
 
@@ -146,7 +152,7 @@ class TaoMessageConsumer extends EventEmitter {
146
152
 
147
153
  if (this[kOptions].autoReplyConfirmation && msg && msg[CONTENT] && msg[CONTENT][ID]) {
148
154
  const reply = new Message(SEND, Confirm).with(TOKEN, this[kToken]).with(ID, msg[CONTENT][ID]);
149
- logger.onmessage[SEND][Confirm](JSONB.stringify(reply));
155
+ logger.onmessage[SEND][Confirm](stringify(reply));
150
156
  this.send(reply);
151
157
  }
152
158
  }
@@ -169,10 +175,10 @@ class TaoMessageConsumer extends EventEmitter {
169
175
  && msg[CONTENT][CONTENT]
170
176
  && typeof msg[CONTENT][CONTENT] === 'string'
171
177
  && /^\{.*\}$/.test(msg[CONTENT][CONTENT])) {
172
- Reflect.set(msg[CONTENT], CONTENT, JSONB.parse(msg[CONTENT][CONTENT]));
178
+ Reflect.set(msg[CONTENT], CONTENT, parse(msg[CONTENT][CONTENT]));
173
179
  }
174
180
 
175
- logger.onmessage[msg[TYPE]](JSONB.stringify(msg));
181
+ logger.onmessage[msg[TYPE]](stringify(msg));
176
182
 
177
183
  this[`process${msg[TYPE]}`](msg);
178
184
  }
package/lib/message.js CHANGED
@@ -29,10 +29,8 @@ class Message {
29
29
  [CONTENT] = {};
30
30
 
31
31
  constructor(type, kind) {
32
- /* eslint-disable-next-line no-unused-expressions */
33
- MessageType.includes(type) && Reflect.set(this, TYPE, type);
34
- /* eslint-disable-next-line no-unused-expressions */
35
- MessageKind.includes(kind) && Reflect.set(this[CONTENT], MessageKind, kind);
32
+ if (MessageType.includes(type)) { this[TYPE] = type; }
33
+ if (MessageKind.includes(kind)) { this[CONTENT][MessageKind] = kind; }
36
34
  }
37
35
 
38
36
  with(key, value) {
@@ -93,14 +91,10 @@ class Encoder {
93
91
 
94
92
  compress() {
95
93
  this.put(this[kData].protocolVersion).put(this[kData][TYPE]);
96
- /* eslint-disable-next-line no-unused-expressions */
97
- this[kData][CODE] && this.putShort(StatusCode).putShort(this[kData][CODE]);
98
- /* eslint-disable-next-line no-unused-expressions */
99
- this[kData][PHRASE] && this.putShort(StatusPhrase).writeCountedString(this[kData][PHRASE]);
100
- /* eslint-disable-next-line no-unused-expressions */
101
- this[kData][FLAG] && this.putShort(Flag).putInt(this[kData][FLAG]);
102
- /* eslint-disable-next-line no-unused-expressions */
103
- this[kData][TOKEN] && this.putShort(Token).writeCountedString(this[kData][TOKEN]);
94
+ if (this[kData][CODE]) { this.putShort(StatusCode).putShort(this[kData][CODE]); }
95
+ if (this[kData][PHRASE]) { this.putShort(StatusPhrase).writeCountedString(this[kData][PHRASE]); }
96
+ if (this[kData][FLAG]) { this.putShort(Flag).putInt(this[kData][FLAG]); }
97
+ if (this[kData][TOKEN]) { this.putShort(Token).writeCountedString(this[kData][TOKEN]); }
104
98
  Object.entries(this[kData][CONTENT] || {}).forEach(([k, v]) => this.putShort(Custom).writeCountedString(k).writeCustomValue(v));
105
99
  this.putShort(EndOfHeaders);
106
100
 
@@ -134,8 +128,7 @@ class Encoder {
134
128
  writeCountedString(value) {
135
129
  const len = value ? Buffer.byteLength(value) : 0;
136
130
  this.putInt(len);
137
- /* eslint-disable-next-line no-unused-expressions */
138
- len && this[kBuffer].push(pick(len, write, value, utf8));
131
+ if (len) { this[kBuffer].push(pick(len, write, value, utf8)); }
139
132
 
140
133
  return this;
141
134
  }
@@ -187,18 +180,6 @@ class Encoder {
187
180
 
188
181
  const kOffset = Symbol('kOffset');
189
182
 
190
- const readInt8 = 'readInt8';
191
-
192
- const readInt16LE = 'readInt16LE';
193
-
194
- const readInt32LE = 'readInt32LE';
195
-
196
- const readBigInt64LE = 'readBigInt64LE';
197
-
198
- function read(buffer, method, offset, ...extra) {
199
- return buffer[method](offset, ...extra);
200
- }
201
-
202
183
  class Decoder {
203
184
  [kOffset] = 1;
204
185
 
@@ -245,25 +226,25 @@ class Decoder {
245
226
  }
246
227
 
247
228
  get() {
248
- const value = read(this[kBuffer], readInt8, this[kOffset]);
229
+ const value = this[kBuffer].readInt8(this[kOffset]);
249
230
  this[kOffset] += 1;
250
231
  return Number(value);
251
232
  }
252
233
 
253
234
  getShort() {
254
- const value = read(this[kBuffer], readInt16LE, this[kOffset]);
235
+ const value = this[kBuffer].readInt16LE(this[kOffset]);
255
236
  this[kOffset] += 2;
256
237
  return Number(value);
257
238
  }
258
239
 
259
240
  getInt() {
260
- const value = read(this[kBuffer], readInt32LE, this[kOffset]);
241
+ const value = this[kBuffer].readInt32LE(this[kOffset]);
261
242
  this[kOffset] += 4;
262
243
  return Number(value);
263
244
  }
264
245
 
265
246
  getLong() {
266
- const value = read(this[kBuffer], readBigInt64LE, this[kOffset]);
247
+ const value = this[kBuffer].readBigInt64LE(this[kOffset]);
267
248
  this[kOffset] += 8;
268
249
  return BigInt(value);
269
250
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tmc.js",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "description": "Events driven and chained Taobao Message Channel(TMC) for NodeJS",
5
5
  "author": "James ZHANG",
6
6
  "license": "MIT",
@@ -30,11 +30,11 @@
30
30
  ],
31
31
  "dependencies": {
32
32
  "json-bigint": "^1.0.0",
33
- "ws": "^8.11.0"
33
+ "ws": "^5.2.2 || ^6.2.1 || ^7.4.5 || ^8.0.0"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@babel/eslint-parser": "^7.19.1",
37
- "@types/node": "^18.8.3",
37
+ "@types/node": ">=11.9.0",
38
38
  "eslint": "^8.25.0",
39
39
  "eslint-config-airbnb-base": "^15.0.0"
40
40
  },