tmc.js 0.3.1 → 0.3.3
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 +14 -2
- package/lib/consumer.js +9 -13
- package/lib/message-fields.js +20 -0
- package/package.json +1 -1
- package/types/index.d.ts +27 -2
- package/types/message-fields.d.ts +10 -0
- package/types/message.d.ts +4 -4
- package/types/message.in.d.ts +4 -0
- package/types/tmall.d.ts +19 -0
package/README.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
Events driven and chained Taobao Message Channel(TMC) for NodeJS
|
|
4
4
|
|
|
5
|
+
[](https://github.com/TheNorthMemory/tmc.js/releases)
|
|
6
|
+
[](https://snyk.io/advisor/npm-package/tmc.js)
|
|
7
|
+
[](https://www.npmjs.com/package/tmc.js)
|
|
8
|
+
[](https://www.npmjs.com/package/tmc.js)
|
|
9
|
+
[](https://www.npmjs.com/package/tmc.js)
|
|
10
|
+
[](https://www.npmjs.com/package/tmc.js)
|
|
11
|
+
|
|
12
|
+
## 设计
|
|
13
|
+
|
|
14
|
+
核心包以事件作为驱动,通过注册`TOPIC`回调处理函数,实现了TMC产品服务的消息消费处理能力,同时通过`Proxy`代理,以`TOPIC`作为键值,平序注册消息消费处理函数,序图如下:
|
|
15
|
+
|
|
16
|
+
[](./.github/sdk-sequence.mmd)
|
|
17
|
+
|
|
5
18
|
## 使用
|
|
6
19
|
|
|
7
20
|
`npm i tmc.js`
|
|
@@ -51,7 +64,6 @@ new Tmc('your_app_key', 'your_app_secret')
|
|
|
51
64
|
|
|
52
65
|
| label | 说明 |
|
|
53
66
|
| --- | --- |
|
|
54
|
-
| `tmc` | 开启全部`DEBUG`日志模式
|
|
55
67
|
| `tmc:onping` | 开启 `onping` 时的日志
|
|
56
68
|
| `tmc:onopen` | 开启 `onopen` 时的日志
|
|
57
69
|
| `tmc:onpull` | 开启 `onpull` 时的日志
|
|
@@ -61,7 +73,7 @@ new Tmc('your_app_key', 'your_app_secret')
|
|
|
61
73
|
| `tmc:onmessage:connect` | 开启消费端发起连接 `connect` 时的日志
|
|
62
74
|
| `tmc:onmessage:connectack` | 开启消费端回复连接 `connectack` 时的日志
|
|
63
75
|
| `tmc:onmessage:send` | 开启消费端接收到(即`From`淘宝) `send` 的消息时的日志
|
|
64
|
-
| `tmc:onmessage:sendack` | (
|
|
76
|
+
| `tmc:onmessage:sendack` | 当消费端收到(`From`淘宝)消息,消费端消息处理失败,需要服务端重发,须回复`SENDACK(3)`及`FLAG`字典值时的日志
|
|
65
77
|
| `tmc:onmessage:send:confirm` | 开启消费端回复接收到的(即`From`淘宝消息),发送自动确认 `send:confirm` 时的日志
|
|
66
78
|
|
|
67
79
|
</details>
|
package/lib/consumer.js
CHANGED
|
@@ -83,9 +83,9 @@ class TaoMessageConsumer extends EventEmitter {
|
|
|
83
83
|
sign(timestamp) {
|
|
84
84
|
const sk = this[kAppSecret].export();
|
|
85
85
|
return [
|
|
86
|
-
[
|
|
87
|
-
[
|
|
88
|
-
[
|
|
86
|
+
[APP_KEY, this[kAppKey]],
|
|
87
|
+
[GROUP_NAME, this[kGroupName]],
|
|
88
|
+
[TIMESTAMP, `${timestamp}`],
|
|
89
89
|
].reduce(
|
|
90
90
|
(h, [k, v]) => h.update(k).update(v),
|
|
91
91
|
createHash('md5').update(sk),
|
|
@@ -187,16 +187,12 @@ class TaoMessageConsumer extends EventEmitter {
|
|
|
187
187
|
connect(address) {
|
|
188
188
|
if (address) { this[kAddress] = address; }
|
|
189
189
|
|
|
190
|
-
this[kWebSocket]
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
ws.on('ping', (...arg) => this.onping(...arg));
|
|
197
|
-
ws.on('error', (err) => this.onerror(err));
|
|
198
|
-
ws.on('close', (...arg) => this.onclose(...arg));
|
|
199
|
-
ws.on('message', (...arg) => this.onmessage(...arg));
|
|
190
|
+
this[kWebSocket] = new WebSocket(this[kAddress])
|
|
191
|
+
.on('message', (data, isBinary) => this.onmessage(data, isBinary))
|
|
192
|
+
.on('close', (code, reason) => this.onclose(code, reason))
|
|
193
|
+
.on('error', (err) => this.onerror(err))
|
|
194
|
+
.on('ping', (data) => this.onping(data))
|
|
195
|
+
.on('open', () => this.onopen());
|
|
200
196
|
|
|
201
197
|
return this;
|
|
202
198
|
}
|
package/lib/message-fields.js
CHANGED
|
@@ -12,6 +12,11 @@ const SDK = 'sdk';
|
|
|
12
12
|
const INTRANET_IP = 'intranet_ip';
|
|
13
13
|
const ID = 'id';
|
|
14
14
|
const TOPIC = 'topic';
|
|
15
|
+
const MSG = 'msg';
|
|
16
|
+
const DATAID = 'dataid';
|
|
17
|
+
const USERID = 'userid';
|
|
18
|
+
const OUTTIME = 'outtime';
|
|
19
|
+
const PUBLISHER = 'publisher';
|
|
15
20
|
|
|
16
21
|
class MessageFields {
|
|
17
22
|
static TYPE = TYPE;
|
|
@@ -41,6 +46,16 @@ class MessageFields {
|
|
|
41
46
|
static ID = ID;
|
|
42
47
|
|
|
43
48
|
static TOPIC = TOPIC;
|
|
49
|
+
|
|
50
|
+
static MSG = MSG;
|
|
51
|
+
|
|
52
|
+
static DATAID = DATAID;
|
|
53
|
+
|
|
54
|
+
static USERID = USERID;
|
|
55
|
+
|
|
56
|
+
static OUTTIME = OUTTIME;
|
|
57
|
+
|
|
58
|
+
static PUBLISHER = PUBLISHER;
|
|
44
59
|
}
|
|
45
60
|
|
|
46
61
|
module.exports = MessageFields;
|
|
@@ -59,3 +74,8 @@ module.exports.SDK = SDK;
|
|
|
59
74
|
module.exports.INTRANET_IP = INTRANET_IP;
|
|
60
75
|
module.exports.ID = ID;
|
|
61
76
|
module.exports.TOPIC = TOPIC;
|
|
77
|
+
module.exports.MSG = MSG;
|
|
78
|
+
module.exports.DATAID = DATAID;
|
|
79
|
+
module.exports.USERID = USERID;
|
|
80
|
+
module.exports.OUTTIME = OUTTIME;
|
|
81
|
+
module.exports.PUBLISHER = PUBLISHER;
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -42,10 +42,10 @@ declare interface TaoMessageConstractor extends EventEmitter {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
declare interface TaoMessageSubscriber {
|
|
45
|
-
[K: string]:
|
|
45
|
+
[K: string]: TaoTopicSubscriber;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
declare interface
|
|
48
|
+
declare interface TaoTopicSubscriber {
|
|
49
49
|
(fn: TaoMessageProcessor): TaoMessageConsumer;
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -784,6 +784,10 @@ declare interface TaoTopicsDescriptor {
|
|
|
784
784
|
tmall_auto_TradeModify(fn: (this: TaoMessageConsumer, message: IncomingMessage.TmallAutoTradeModify) => void): TaoMessageConsumer;
|
|
785
785
|
/** {@link IncomingMessage.TmallAutoTwoWheelsReceiptCreate 天猫汽车 > 天猫二轮车服务工单创建开放} */
|
|
786
786
|
tmall_auto_TwoWheelsReceiptCreate(fn: (this: TaoMessageConsumer, message: IncomingMessage.TmallAutoTwoWheelsReceiptCreate) => void): TaoMessageConsumer;
|
|
787
|
+
/** {@link IncomingMessage.TmallCarContractSign 天猫汽车 > 合同签署消息} */
|
|
788
|
+
tmall_car_ContractSign(fn: (this: TaoMessageConsumer, message: IncomingMessage.TmallCarContractSign) => void): TaoMessageConsumer;
|
|
789
|
+
/** {@link IncomingMessage.TmallCarFinanceMsg 天猫汽车 > 汽车金融消息} */
|
|
790
|
+
tmall_car_FinanceMsg(fn: (this: TaoMessageConsumer, message: IncomingMessage.TmallCarFinanceMsg) => void): TaoMessageConsumer;
|
|
787
791
|
/** {@link IncomingMessage.TmallChannelApplyOrderChange 渠道中心API > 申请单变更消息} */
|
|
788
792
|
tmall_channel_ApplyOrderChange(fn: (this: TaoMessageConsumer, message: IncomingMessage.TmallChannelApplyOrderChange) => void): TaoMessageConsumer;
|
|
789
793
|
/** {@link IncomingMessage.TmallChannelDeliverOrderChange 渠道中心API > 发货单消息变更} */
|
|
@@ -1609,6 +1613,10 @@ declare interface TaoEventsListener {
|
|
|
1609
1613
|
on(topic: 'tmall_auto_TradeModify', listener: (this: TaoMessageConsumer, message: IncomingMessage.TmallAutoTradeModify) => void): TaoMessageConsumer;
|
|
1610
1614
|
/** {@link IncomingMessage.TmallAutoTwoWheelsReceiptCreate 天猫汽车 > 天猫二轮车服务工单创建开放} */
|
|
1611
1615
|
on(topic: 'tmall_auto_TwoWheelsReceiptCreate', listener: (this: TaoMessageConsumer, message: IncomingMessage.TmallAutoTwoWheelsReceiptCreate) => void): TaoMessageConsumer;
|
|
1616
|
+
/** {@link IncomingMessage.TmallCarContractSign 天猫汽车 > 合同签署消息} */
|
|
1617
|
+
on(topic: 'tmall_car_ContractSign', listener: (this: TaoMessageConsumer, message: IncomingMessage.TmallCarContractSign) => void): TaoMessageConsumer;
|
|
1618
|
+
/** {@link IncomingMessage.TmallCarFinanceMsg 天猫汽车 > 汽车金融消息} */
|
|
1619
|
+
on(topic: 'tmall_car_FinanceMsg', listener: (this: TaoMessageConsumer, message: IncomingMessage.TmallCarFinanceMsg) => void): TaoMessageConsumer;
|
|
1612
1620
|
/** {@link IncomingMessage.TmallChannelApplyOrderChange 渠道中心API > 申请单变更消息} */
|
|
1613
1621
|
on(topic: 'tmall_channel_ApplyOrderChange', listener: (this: TaoMessageConsumer, message: IncomingMessage.TmallChannelApplyOrderChange) => void): TaoMessageConsumer;
|
|
1614
1622
|
/** {@link IncomingMessage.TmallChannelDeliverOrderChange 渠道中心API > 发货单消息变更} */
|
|
@@ -1707,4 +1715,21 @@ declare interface TaoEventsListener {
|
|
|
1707
1715
|
|
|
1708
1716
|
declare const TMC: TaoMessageConsumer;
|
|
1709
1717
|
|
|
1718
|
+
type internalMessage = Message;
|
|
1719
|
+
type internalMessageFields = MessageFields;
|
|
1720
|
+
type internalMessageKind = MessageKind;
|
|
1721
|
+
type internalMessageType = MessageType;
|
|
1722
|
+
type internalHeaderType = HeaderType;
|
|
1723
|
+
type internalValueFormat = ValueFormat;
|
|
1724
|
+
declare namespace TMC {
|
|
1725
|
+
export {
|
|
1726
|
+
internalMessage as Message,
|
|
1727
|
+
internalMessageFields as MessageFields,
|
|
1728
|
+
internalMessageKind as MessageKind,
|
|
1729
|
+
internalMessageType as MessageType,
|
|
1730
|
+
internalHeaderType as HeaderType,
|
|
1731
|
+
internalValueFormat as ValueFormat,
|
|
1732
|
+
};
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1710
1735
|
export = TMC;
|
|
@@ -13,6 +13,11 @@ declare namespace MessageFields {
|
|
|
13
13
|
type INTRANET_IP = 'intranet_ip';
|
|
14
14
|
type ID = 'id';
|
|
15
15
|
type TOPIC = 'topic';
|
|
16
|
+
type MSG = 'msg';
|
|
17
|
+
type DATAID = 'dataid';
|
|
18
|
+
type USERID = 'userid';
|
|
19
|
+
type OUTTIME = 'outtime';
|
|
20
|
+
type PUBLISHER = 'publisher';
|
|
16
21
|
}
|
|
17
22
|
|
|
18
23
|
declare class MessageFields {
|
|
@@ -30,4 +35,9 @@ declare class MessageFields {
|
|
|
30
35
|
static INTRANET_IP: MessageFields.INTRANET_IP;
|
|
31
36
|
static ID: MessageFields.ID;
|
|
32
37
|
static TOPIC: MessageFields.TOPIC;
|
|
38
|
+
static MSG: MessageFields.MSG;
|
|
39
|
+
static DATAID: MessageFields.DATAID;
|
|
40
|
+
static USERID: MessageFields.USERID;
|
|
41
|
+
static OUTTIME: MessageFields.OUTTIME;
|
|
42
|
+
static PUBLISHER: MessageFields.PUBLISHER;
|
|
33
43
|
}
|
package/types/message.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
/// <reference path="
|
|
2
|
+
/// <reference path="message-type.d.ts" />
|
|
3
3
|
/// <reference path="message-kind.d.ts" />
|
|
4
4
|
|
|
5
5
|
declare class Message {
|
|
@@ -7,14 +7,14 @@ declare class Message {
|
|
|
7
7
|
type?: MessageType.CONNECT | MessageType.CONNECTACK | MessageType.SEND | MessageType.SENDACK,
|
|
8
8
|
kind?: MessageKind.None | MessageKind.PullRequest | MessageKind.Confirm | MessageKind.Data | MessageKind.Failed
|
|
9
9
|
);
|
|
10
|
-
protocolVersion:
|
|
11
|
-
messageType:
|
|
10
|
+
protocolVersion: 2;
|
|
11
|
+
messageType: MessageType.CONNECT | MessageType.CONNECTACK | MessageType.SEND | MessageType.SENDACK;
|
|
12
12
|
statusCode?: number;
|
|
13
13
|
statusPhrase?: string;
|
|
14
14
|
flag?: number;
|
|
15
15
|
token?: string;
|
|
16
16
|
content?: MessageContent;
|
|
17
|
-
with(key: string, value?: number | bigint | string | Buffer | Date): this;
|
|
17
|
+
with(key: string | MessageKind, value?: number | bigint | string | Buffer | Date): this;
|
|
18
18
|
get buffer(): Buffer;
|
|
19
19
|
static from(buf: Buffer): Message;
|
|
20
20
|
}
|
package/types/message.in.d.ts
CHANGED
|
@@ -751,6 +751,10 @@ declare namespace IncomingMessage {
|
|
|
751
751
|
type TmallAutoTradeModify = Message & { content?: MessageContent & { content?: string | Tmall.Auto.TradeModify } };
|
|
752
752
|
/** {@link Tmall.Auto.TwoWheelsReceiptCreate 天猫汽车 > 天猫二轮车服务工单创建开放} */
|
|
753
753
|
type TmallAutoTwoWheelsReceiptCreate = Message & { content?: MessageContent & { content?: string | Tmall.Auto.TwoWheelsReceiptCreate } };
|
|
754
|
+
/** {@link Tmall.Car.ContractSign 天猫汽车 > 合同签署消息} */
|
|
755
|
+
type TmallCarContractSign = Message & { content?: MessageContent & { content?: string | Tmall.Car.ContractSign } };
|
|
756
|
+
/** {@link Tmall.Car.FinanceMsg 天猫汽车 > 汽车金融消息} */
|
|
757
|
+
type TmallCarFinanceMsg = Message & { content?: MessageContent & { content?: string | Tmall.Car.FinanceMsg } };
|
|
754
758
|
/** {@link Tmall.Channel.ApplyOrderChange 渠道中心API > 申请单变更消息} */
|
|
755
759
|
type TmallChannelApplyOrderChange = Message & { content?: MessageContent & { content?: string | Tmall.Channel.ApplyOrderChange } };
|
|
756
760
|
/** {@link Tmall.Channel.DeliverOrderChange 渠道中心API > 发货单消息变更} */
|
package/types/tmall.d.ts
CHANGED
|
@@ -28,6 +28,25 @@ declare namespace Tmall.Auto {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
/** 天猫汽车 */
|
|
32
|
+
declare namespace Tmall.Car {
|
|
33
|
+
/** 合同签署消息 */
|
|
34
|
+
interface ContractSign {
|
|
35
|
+
/** 合同下载地址,有效期10分钟 */
|
|
36
|
+
contract_url: string;
|
|
37
|
+
/** 订单id */
|
|
38
|
+
order_id: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 汽车金融消息 */
|
|
42
|
+
interface FinanceMsg {
|
|
43
|
+
/** 订单id */
|
|
44
|
+
order_id: number;
|
|
45
|
+
/** 消息类型: FULL_PURCHASE_FINANCE_CANCEL_MSG_TYPE?,因全款购车导致金融取消 REFUND_FINANCE_CANCEL,因退款导致的金融单关闭 */
|
|
46
|
+
type: string;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
31
50
|
/** 渠道中心API */
|
|
32
51
|
declare namespace Tmall.Channel {
|
|
33
52
|
/** 申请单变更消息 */
|