@alemonjs/kook 2.1.0-alpha.1 → 2.1.0-alpha.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.
@@ -8,50 +8,50 @@ const ConversationMap = {
8
8
  * 文字消息
9
9
  */
10
10
  [1]: {
11
- public: e => KOOKEventKey['MESSAGES_PUBLIC'],
12
- direct: e => KOOKEventKey['MESSAGES_DIRECT']
11
+ public: () => KOOKEventKey['MESSAGES_PUBLIC'],
12
+ direct: () => KOOKEventKey['MESSAGES_DIRECT']
13
13
  },
14
14
  /**
15
15
  * 图片消息,
16
16
  */
17
17
  [2]: {
18
- public: e => KOOKEventKey['MESSAGES_PUBLIC'],
19
- direct: e => KOOKEventKey['MESSAGES_DIRECT']
18
+ public: () => KOOKEventKey['MESSAGES_PUBLIC'],
19
+ direct: () => KOOKEventKey['MESSAGES_DIRECT']
20
20
  },
21
21
  /**
22
22
  * 视频消息,
23
23
  */
24
24
  [3]: {
25
- public: e => KOOKEventKey['MESSAGES_PUBLIC'],
26
- direct: e => KOOKEventKey['MESSAGES_DIRECT']
25
+ public: () => KOOKEventKey['MESSAGES_PUBLIC'],
26
+ direct: () => KOOKEventKey['MESSAGES_DIRECT']
27
27
  },
28
28
  /**
29
29
  * 文件消息,
30
30
  */
31
31
  [4]: {
32
- public: e => KOOKEventKey['MESSAGES_PUBLIC'],
33
- direct: e => KOOKEventKey['MESSAGES_DIRECT']
32
+ public: () => KOOKEventKey['MESSAGES_PUBLIC'],
33
+ direct: () => KOOKEventKey['MESSAGES_DIRECT']
34
34
  },
35
35
  /**
36
36
  * 音频消息,
37
37
  */
38
38
  [8]: {
39
- public: e => KOOKEventKey['MESSAGES_PUBLIC'],
40
- direct: e => KOOKEventKey['MESSAGES_DIRECT']
39
+ public: () => KOOKEventKey['MESSAGES_PUBLIC'],
40
+ direct: () => KOOKEventKey['MESSAGES_DIRECT']
41
41
  },
42
42
  /**
43
43
  * mk消息
44
44
  */
45
45
  [9]: {
46
- public: e => KOOKEventKey['MESSAGES_PUBLIC'],
47
- direct: e => KOOKEventKey['MESSAGES_DIRECT']
46
+ public: () => KOOKEventKey['MESSAGES_PUBLIC'],
47
+ direct: () => KOOKEventKey['MESSAGES_DIRECT']
48
48
  },
49
49
  /**
50
50
  * card消息,
51
51
  */
52
52
  [10]: {
53
- public: e => KOOKEventKey['MESSAGES_PUBLIC'],
54
- direct: e => KOOKEventKey['MESSAGES_DIRECT']
53
+ public: () => KOOKEventKey['MESSAGES_PUBLIC'],
54
+ direct: () => KOOKEventKey['MESSAGES_DIRECT']
55
55
  },
56
56
  /**
57
57
  * 系统消息
@@ -59,18 +59,18 @@ const ConversationMap = {
59
59
  */
60
60
  [255]: {
61
61
  public: (event) => {
62
- if (event.extra.type == 'added_reaction' || event.extra.type == 'deleted_reaction') {
62
+ if (event.extra.type === 'added_reaction' || event.extra.type === 'deleted_reaction') {
63
63
  return KOOKEventKey['REACTIONS'];
64
64
  }
65
- else if (event.extra.type == 'joined_channel') {
65
+ else if (event.extra.type === 'joined_channel') {
66
66
  console.info('joined_channel');
67
67
  return '';
68
68
  }
69
- else if (event.extra.type == 'exited_channel') {
69
+ else if (event.extra.type === 'exited_channel') {
70
70
  console.info('111exited_channel');
71
71
  return '';
72
72
  }
73
- else if (event.extra.type == 'updated_channel') {
73
+ else if (event.extra.type === 'updated_channel') {
74
74
  // ChannelData
75
75
  console.info('updated_channel');
76
76
  return '';
@@ -80,11 +80,11 @@ const ConversationMap = {
80
80
  * *******
81
81
  */
82
82
  }
83
- else if (event.extra.type == 'joined_guild') {
83
+ else if (event.extra.type === 'joined_guild') {
84
84
  console.info('joined_guild');
85
85
  return KOOKEventKey['MEMBER_ADD'];
86
86
  }
87
- else if (event.extra.type == 'exited_guild') {
87
+ else if (event.extra.type === 'exited_guild') {
88
88
  console.info('exited_guild');
89
89
  return KOOKEventKey['MEMBER_REMOVE'];
90
90
  /**
@@ -93,13 +93,13 @@ const ConversationMap = {
93
93
  * ********
94
94
  */
95
95
  }
96
- else if (event.extra.type == 'updated_message') {
96
+ else if (event.extra.type === 'updated_message') {
97
97
  // 消息更新
98
98
  // EditingData
99
99
  console.info('updated_message');
100
100
  return '';
101
101
  }
102
- else if (event.extra.type == 'pinned_message') {
102
+ else if (event.extra.type === 'pinned_message') {
103
103
  // 顶置消息
104
104
  // overheadData
105
105
  console.info('pinned_message');
@@ -107,13 +107,13 @@ const ConversationMap = {
107
107
  }
108
108
  },
109
109
  direct: (event) => {
110
- if (event.extra.type == 'guild_member_online') {
111
- //OnLineData
110
+ if (event.extra.type === 'guild_member_online') {
111
+ // OnLineData
112
112
  console.info('exited_guild');
113
113
  return '';
114
114
  }
115
- else if (event.extra.type == 'message_btn_click') {
116
- //按钮事件
115
+ else if (event.extra.type === 'message_btn_click') {
116
+ // 按钮事件
117
117
  console.info('message_btn_click');
118
118
  return KOOKEventKey['INTERACTION'];
119
119
  }
@@ -0,0 +1,109 @@
1
+ const filterHeaders = (headers = {}) => {
2
+ if (!headers) {
3
+ return headers;
4
+ }
5
+ const filtered = {};
6
+ const sensitiveKeys = [/^authorization$/i, /^cookie$/i, /^set-cookie$/i, /token/i, /key/i, /jwt/i, /^session[-_]id$/i, /^uid$/i, /^user[-_]id$/i];
7
+ for (const key in headers) {
8
+ if (/^_/.test(key)) {
9
+ continue; // 跳过 _ 开头
10
+ }
11
+ // 去掉 Symbol 类型的 key
12
+ if (typeof key === 'symbol') {
13
+ continue;
14
+ }
15
+ // 去掉函数
16
+ if (typeof headers[key] === 'function') {
17
+ continue;
18
+ }
19
+ // 如果是敏感字段全部替换为 ******
20
+ if (sensitiveKeys.some(re => re.test(key))) {
21
+ filtered[key] = '******';
22
+ }
23
+ else {
24
+ filtered[key] = headers[key];
25
+ }
26
+ }
27
+ return filtered;
28
+ };
29
+ const filterConfig = (config = {}) => {
30
+ if (!config) {
31
+ return config;
32
+ }
33
+ const filtered = {};
34
+ for (const key in config) {
35
+ if (/^_/.test(key)) {
36
+ continue; // 跳过 _ 开头
37
+ }
38
+ // 去掉 Symbol 类型的 key
39
+ if (typeof key === 'symbol') {
40
+ continue;
41
+ }
42
+ // 去掉函数
43
+ if (typeof config[key] === 'function') {
44
+ continue;
45
+ }
46
+ filtered[key] = config[key];
47
+ }
48
+ return filtered;
49
+ };
50
+ const filterRequest = (request = {}) => {
51
+ if (!request) {
52
+ return request;
53
+ }
54
+ const filtered = {};
55
+ for (const key in request) {
56
+ if (/^_/.test(key)) {
57
+ continue; // 跳过 _ 开头
58
+ }
59
+ // 去掉 Symbol 类型的 key
60
+ if (typeof key === 'symbol') {
61
+ continue;
62
+ }
63
+ // 去掉函数
64
+ if (typeof request[key] === 'function') {
65
+ continue;
66
+ }
67
+ filtered[key] = request[key];
68
+ }
69
+ return filtered;
70
+ };
71
+ // 处理axios错误
72
+ const loggerError = (err) => {
73
+ // 错误时的请求头
74
+ logger.error('[axios] error', {
75
+ config: {
76
+ headers: filterHeaders(err?.config?.headers),
77
+ params: err?.config?.params,
78
+ data: err?.config?.data
79
+ },
80
+ response: {
81
+ status: err?.response?.status,
82
+ statusText: err?.response?.statusText,
83
+ headers: filterHeaders(err?.response?.headers),
84
+ config: filterConfig(err?.response?.config),
85
+ request: filterRequest(err?.response?.request),
86
+ data: err?.response?.data
87
+ },
88
+ message: err?.message
89
+ });
90
+ };
91
+ /**
92
+ * 基础请求
93
+ * @param service
94
+ * @param options
95
+ * @returns
96
+ */
97
+ const createAxiosInstance = (service, options) => {
98
+ return new Promise((resolve, reject) => {
99
+ service(options)
100
+ .then(res => resolve(res?.data ?? {}))
101
+ .catch(err => {
102
+ loggerError(err);
103
+ // 丢出错误中携带的响应数据
104
+ reject(err?.response?.data);
105
+ });
106
+ });
107
+ };
108
+
109
+ export { createAxiosInstance };
@@ -1,6 +1,6 @@
1
1
  type MessageType = 1 | 2 | 3 | 4 | 8 | 9 | 10 | 255;
2
2
  /**
3
- * 消息通道类型, GROUP 为组播消息, PERSON 为单播消息, BROADCAST 为广播消息
3
+ * 消息通道类型, GROUP 为组播消息, PERSON 为单播消息, BROADCAST 为广播消息
4
4
  */
5
5
  type MessageChannelType = 'GROUP' | 'PERSON' | 'BROADCAST';
6
6
  /**
package/lib/sdk/wss.js CHANGED
@@ -39,11 +39,13 @@ class KOOKClient extends KOOKAPI {
39
39
  const gatewayUrl = await this.gateway()
40
40
  .then(res => res?.data?.url)
41
41
  .catch(err => {
42
- if (this.#events['ERROR'])
42
+ if (this.#events['ERROR']) {
43
43
  this.#events['ERROR'](err);
44
+ }
44
45
  });
45
- if (!gatewayUrl && gatewayUrl == '')
46
+ if (!gatewayUrl && gatewayUrl === '') {
46
47
  return;
48
+ }
47
49
  // 建立连接
48
50
  const map = {
49
51
  0: async ({ d, sn }) => {
@@ -59,20 +61,23 @@ class KOOKClient extends KOOKAPI {
59
61
  */
60
62
  this.#lastMessageSN = sn;
61
63
  try {
62
- if (d.channel_type == 'GROUP') {
64
+ if (d.channel_type === 'GROUP') {
63
65
  const t = ConversationMap[d.type]['public'](d);
64
- if (this.#events[t])
66
+ if (this.#events[t]) {
65
67
  await this.#events[t](d);
68
+ }
66
69
  }
67
70
  else {
68
71
  const t = ConversationMap[d.type]['direct'](d);
69
- if (this.#events[t])
72
+ if (this.#events[t]) {
70
73
  await this.#events[t](d);
74
+ }
71
75
  }
72
76
  }
73
77
  catch (err) {
74
- if (this.#events['ERROR'])
78
+ if (this.#events['ERROR']) {
75
79
  this.#events['ERROR'](err);
80
+ }
76
81
  }
77
82
  //
78
83
  }
@@ -123,13 +128,16 @@ class KOOKClient extends KOOKAPI {
123
128
  this.#ws.on('open', () => {
124
129
  console.info('[ws] open');
125
130
  });
126
- this.#ws.on('message', async (msg) => {
131
+ const onMessage = msg => {
127
132
  const message = JSON.parse(msg.toString('utf8'));
128
- if (process.env.KOOK_WS == 'dev')
133
+ if (process.env.KOOK_WS === 'dev') {
129
134
  console.info('message', message);
130
- if (map[message.s])
135
+ }
136
+ if (map[message.s]) {
131
137
  map[message.s](message);
132
- });
138
+ }
139
+ };
140
+ this.#ws.on('message', msg => void onMessage(msg));
133
141
  // 心跳定时发送
134
142
  setInterval(() => {
135
143
  if (this.#isConnected) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/kook",
3
- "version": "2.1.0-alpha.1",
3
+ "version": "2.1.0-alpha.2",
4
4
  "description": "kook platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
@@ -18,6 +18,9 @@
18
18
  "./package": "./package.json",
19
19
  "./desktop": "./lib/desktop.js"
20
20
  },
21
+ "peerDependencies": {
22
+ "alemonjs": "^2.1.0-alpha.15"
23
+ },
21
24
  "alemonjs": {
22
25
  "desktop": {
23
26
  "platform": [