@seayoo-io/combo-sdk-node 1.6.0 → 1.8.1
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 +71 -56
- package/dist/index.cjs +1448 -1585
- package/dist/index.d.cts +848 -0
- package/dist/index.d.ts +848 -15
- package/dist/index.js +1418 -1581
- package/package.json +40 -24
- package/dist/api/index.d.ts +0 -55
- package/dist/api/types.d.ts +0 -87
- package/dist/api/ua.d.ts +0 -1
- package/dist/const.d.ts +0 -143
- package/dist/gm/index.d.ts +0 -51
- package/dist/gm/types.d.ts +0 -58
- package/dist/gm/utils.d.ts +0 -31
- package/dist/notify/index.d.ts +0 -49
- package/dist/notify/msgRefund.d.ts +0 -20
- package/dist/notify/msgShipOrder.d.ts +0 -26
- package/dist/notify/types.d.ts +0 -49
- package/dist/request/inc/cache.d.ts +0 -10
- package/dist/request/inc/config.d.ts +0 -12
- package/dist/request/inc/guard.d.ts +0 -3
- package/dist/request/inc/main.d.ts +0 -60
- package/dist/request/inc/option.d.ts +0 -10
- package/dist/request/inc/request.d.ts +0 -7
- package/dist/request/inc/response.d.ts +0 -3
- package/dist/request/inc/retry.d.ts +0 -3
- package/dist/request/inc/rule.d.ts +0 -12
- package/dist/request/inc/type.d.ts +0 -181
- package/dist/request/index.d.ts +0 -4
- package/dist/utils/base.d.ts +0 -46
- package/dist/utils/config.d.ts +0 -17
- package/dist/utils/gcm.d.ts +0 -9
- package/dist/utils/http.d.ts +0 -6
- package/dist/utils/index.d.ts +0 -9
- package/dist/utils/signer.d.ts +0 -34
- package/dist/utils/types.d.ts +0 -18
- package/dist/verify/ads.d.ts +0 -21
- package/dist/verify/id.d.ts +0 -78
- package/dist/verify/index.d.ts +0 -23
- package/vitest.config.ts +0 -10
package/dist/index.js
CHANGED
|
@@ -1,1779 +1,1616 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import http from
|
|
3
|
-
import https from
|
|
4
|
-
import { createHash, createHmac
|
|
5
|
-
import
|
|
6
|
-
|
|
1
|
+
import { arch, platform } from "os";
|
|
2
|
+
import http from "http";
|
|
3
|
+
import https from "https";
|
|
4
|
+
import { createDecipheriv, createHash, createHmac } from "crypto";
|
|
5
|
+
import jwt from "jsonwebtoken";
|
|
6
|
+
//#region src/const.ts
|
|
7
7
|
/** SDK 名称 */
|
|
8
8
|
const SDKName = "combo-sdk-node";
|
|
9
9
|
/** SDK 版本,在编译前会被动态替换 */
|
|
10
|
-
const SDKVersion = "1.
|
|
10
|
+
const SDKVersion = "1.8.1";
|
|
11
11
|
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
12
|
+
* 世游支持的服务端点列表
|
|
13
|
+
*/
|
|
14
|
+
let Endpoint = /* @__PURE__ */ function(Endpoint) {
|
|
15
|
+
/** 中国大陆 API 端点,用于国内发行 */
|
|
16
|
+
Endpoint["China"] = "https://api.seayoo.com";
|
|
17
|
+
/** 全球的 API 端点,用于海外发行 */
|
|
18
|
+
Endpoint["Global"] = "https://api.seayoo.io";
|
|
19
|
+
return Endpoint;
|
|
20
|
+
}({});
|
|
21
21
|
/** 支持的系统平台 */
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
22
|
+
let Platform = /* @__PURE__ */ function(Platform) {
|
|
23
|
+
/** 安卓平台,包括华为鸿蒙系统、小米澎湃 OS 等基于 Android 的操作系统 */
|
|
24
|
+
Platform["Android"] = "android";
|
|
25
|
+
/** 苹果的 iOS 和 iPadOS */
|
|
26
|
+
Platform["iOS"] = "ios";
|
|
27
|
+
/** Windows (PC) 桌面平台 */
|
|
28
|
+
Platform["Windows"] = "windows";
|
|
29
|
+
/** macOS 桌面平台 */
|
|
30
|
+
Platform["macOS"] = "macos";
|
|
31
|
+
/** WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏 */
|
|
32
|
+
Platform["WebGL"] = "webgl";
|
|
33
|
+
/** 华为的 HarmonyOS NEXT 鸿蒙应用开发平台 */
|
|
34
|
+
Platform["HarmonyOS"] = "harmonyos";
|
|
35
|
+
return Platform;
|
|
36
|
+
}({});
|
|
37
37
|
/** 支持的登录提供方 */
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
38
|
+
let IdP = /* @__PURE__ */ function(IdP) {
|
|
39
|
+
/** 游客登录 */
|
|
40
|
+
IdP["Guest"] = "guest";
|
|
41
|
+
/** 世游通行证 */
|
|
42
|
+
IdP["Seayoo"] = "seayoo";
|
|
43
|
+
/** Sign-in with Apple */
|
|
44
|
+
IdP["Apple"] = "apple";
|
|
45
|
+
/** Google Account */
|
|
46
|
+
IdP["Google"] = "google";
|
|
47
|
+
/** Facebook Login */
|
|
48
|
+
IdP["Facebook"] = "facebook";
|
|
49
|
+
/** 小米账号 */
|
|
50
|
+
IdP["Xiaomi"] = "xiaomi";
|
|
51
|
+
/** 微信登录 */
|
|
52
|
+
IdP["Weixin"] = "weixin";
|
|
53
|
+
/** OPPO 账号 */
|
|
54
|
+
IdP["Oppo"] = "oppo";
|
|
55
|
+
/** VIVO 账号 */
|
|
56
|
+
IdP["Vivo"] = "vivo";
|
|
57
|
+
/** 华为账号 */
|
|
58
|
+
IdP["Huawei"] = "huawei";
|
|
59
|
+
/** 荣耀账号 */
|
|
60
|
+
IdP["Honor"] = "honor";
|
|
61
|
+
/** UC(九游)登录 */
|
|
62
|
+
IdP["UC"] = "uc";
|
|
63
|
+
/** TapTap 登录 */
|
|
64
|
+
IdP["TapTap"] = "taptap";
|
|
65
|
+
/** 哔哩哔哩(B站)账号 */
|
|
66
|
+
IdP["Bilibili"] = "bilibili";
|
|
67
|
+
/** 应用宝 YSDK 登录 */
|
|
68
|
+
IdP["Yingyongbao"] = "yingyongbao";
|
|
69
|
+
/** 4399 账号登录 */
|
|
70
|
+
IdP["F4399"] = "4399";
|
|
71
|
+
/** 抖音账号 */
|
|
72
|
+
IdP["Douyin"] = "douyin";
|
|
73
|
+
/** 雷电模拟器账号 */
|
|
74
|
+
IdP["Leidian"] = "leidian";
|
|
75
|
+
/** 猫窝游戏 */
|
|
76
|
+
IdP["Maowo"] = "maowo";
|
|
77
|
+
/** 联想 */
|
|
78
|
+
IdP["Lenovo"] = "lenovo";
|
|
79
|
+
/** 魅族 */
|
|
80
|
+
IdP["Meizu"] = "meizu";
|
|
81
|
+
/** 酷派 */
|
|
82
|
+
IdP["Coolpad"] = "coolpad";
|
|
83
|
+
/** 努比亚 */
|
|
84
|
+
IdP["Nubia"] = "nubia";
|
|
85
|
+
/** 绝峰游戏 */
|
|
86
|
+
IdP["Juefeng"] = "juefeng";
|
|
87
|
+
/** 魅拓游戏 */
|
|
88
|
+
IdP["Meituo"] = "meituo";
|
|
89
|
+
/** 微信小游戏 */
|
|
90
|
+
IdP["MinigameWeixin"] = "minigame_weixin";
|
|
91
|
+
/** 抖音小游戏 */
|
|
92
|
+
IdP["MinigameDouyin"] = "minigame_douyin";
|
|
93
|
+
/** MuMu 模拟器 */
|
|
94
|
+
IdP["Mumu"] = "mumu";
|
|
95
|
+
/** 虎牙游戏 */
|
|
96
|
+
IdP["Huya"] = "huya";
|
|
97
|
+
/** 好游快爆 */
|
|
98
|
+
IdP["Hykb"] = "hykb";
|
|
99
|
+
/** 233 乐园 */
|
|
100
|
+
IdP["L233"] = "233";
|
|
101
|
+
return IdP;
|
|
102
|
+
}({});
|
|
103
103
|
/** GM 错误类型枚举 */
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
|
|
104
|
+
let GMError = /* @__PURE__ */ function(GMError) {
|
|
105
|
+
/** 请求中的 HTTP method 不正确,没有按照预期使用 POST。*/
|
|
106
|
+
GMError["InvalidHttpMethod"] = "invalid_http_method";
|
|
107
|
+
/** 请求中的 Content-Type 不是 application/json。*/
|
|
108
|
+
GMError["InvalidContentType"] = "invalid_content_type";
|
|
109
|
+
/** 对 HTTP 请求的签名验证不通过。这意味着 HTTP 请求不可信。 */
|
|
110
|
+
GMError["InvalidSignature"] = "invalid_signature";
|
|
111
|
+
/** 请求的结构不正确。例如,缺少必要的字段,或字段类型不正确。 */
|
|
112
|
+
GMError["InvalidRequest"] = "invalid_request";
|
|
113
|
+
/** 游戏侧不认识请求中的 GM 命令。 */
|
|
114
|
+
GMError["InvalidCommand"] = "invalid_command";
|
|
115
|
+
/** GM 命令发送频率过高,被游戏侧限流,命令未被处理。 */
|
|
116
|
+
GMError["ThrottlingError"] = "throttling_error";
|
|
117
|
+
/** 幂等处理重试请求时,idempotency_key 所对应的原始请求尚未处理完毕。*/
|
|
118
|
+
GMError["IdempotencyConflict"] = "idempotency_conflict";
|
|
119
|
+
/** 幂等处理重试请求时,请求内容和 idempotency_key 所对应的原始请求内容不一致。*/
|
|
120
|
+
GMError["IdempotencyMismatch"] = "idempotency_mismatch";
|
|
121
|
+
/** GM 命令的参数不正确。例如,参数缺少必要的字段,或参数的字段类型不正确。 */
|
|
122
|
+
GMError["InvalidArgs"] = "invalid_args";
|
|
123
|
+
/** 游戏当前处于停服维护状态,无法处理收到的 GM 命令。*/
|
|
124
|
+
GMError["MaintenanceError"] = "maintenance_error";
|
|
125
|
+
/** 网络通信错误。 */
|
|
126
|
+
GMError["NetworkError"] = "network_error";
|
|
127
|
+
/** 数据库操作异常导致 GM 命令执行失败。 */
|
|
128
|
+
GMError["DatabaseError"] = "database_error";
|
|
129
|
+
/** GM 命令处理超时。 */
|
|
130
|
+
GMError["TimeoutError"] = "timeout_error";
|
|
131
|
+
/** 处理 GM 命令时内部出错。可作为兜底的通用错误类型。*/
|
|
132
|
+
GMError["InternalError"] = "internal_error";
|
|
133
|
+
return GMError;
|
|
134
|
+
}({});
|
|
135
|
+
let HttpStatus = /* @__PURE__ */ function(HttpStatus) {
|
|
136
|
+
HttpStatus[HttpStatus["OK"] = 200] = "OK";
|
|
137
|
+
HttpStatus[HttpStatus["Accepted"] = 202] = "Accepted";
|
|
138
|
+
HttpStatus[HttpStatus["NoContent"] = 204] = "NoContent";
|
|
139
|
+
HttpStatus[HttpStatus["BadRequest"] = 400] = "BadRequest";
|
|
140
|
+
HttpStatus[HttpStatus["Unauthorized"] = 401] = "Unauthorized";
|
|
141
|
+
HttpStatus[HttpStatus["Forbidden"] = 403] = "Forbidden";
|
|
142
|
+
HttpStatus[HttpStatus["NotFound"] = 404] = "NotFound";
|
|
143
|
+
HttpStatus[HttpStatus["MethodNotAllowed"] = 405] = "MethodNotAllowed";
|
|
144
|
+
HttpStatus[HttpStatus["NotAcceptable"] = 406] = "NotAcceptable";
|
|
145
|
+
HttpStatus[HttpStatus["RequestTimeout"] = 408] = "RequestTimeout";
|
|
146
|
+
HttpStatus[HttpStatus["Conflict"] = 409] = "Conflict";
|
|
147
|
+
HttpStatus[HttpStatus["UnsupportedMediaType"] = 415] = "UnsupportedMediaType";
|
|
148
|
+
HttpStatus[HttpStatus["UnprocessableEntity"] = 422] = "UnprocessableEntity";
|
|
149
|
+
HttpStatus[HttpStatus["TooManyRequests"] = 429] = "TooManyRequests";
|
|
150
|
+
HttpStatus[HttpStatus["InternalServerError"] = 500] = "InternalServerError";
|
|
151
|
+
HttpStatus[HttpStatus["BadGateway"] = 502] = "BadGateway";
|
|
152
|
+
HttpStatus[HttpStatus["ServiceUnavailable"] = 503] = "ServiceUnavailable";
|
|
153
|
+
return HttpStatus;
|
|
154
|
+
}({});
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/api/ua.ts
|
|
136
157
|
function getUserAgent(game) {
|
|
137
|
-
|
|
158
|
+
return `${SDKName}/${SDKVersion} game/${game} node/${process.version} os/${platform()} arch/${arch()}`;
|
|
138
159
|
}
|
|
139
|
-
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/utils/base.ts
|
|
140
162
|
const urlReg = /^(?:https?:)?\/\/.+/i;
|
|
141
163
|
const urlReg2 = /^https?:\/\//i;
|
|
142
164
|
const objReg = /^\{[\d\D]*\}$/;
|
|
143
165
|
const arrReg = /^\[[\d\D]*\]$/;
|
|
144
166
|
/**
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
167
|
+
* 暂停一段时间
|
|
168
|
+
* @param {number} ms 暂停的毫秒数
|
|
169
|
+
*/
|
|
148
170
|
function sleep(ms) {
|
|
149
|
-
|
|
171
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
150
172
|
}
|
|
151
173
|
/**
|
|
152
|
-
|
|
153
|
-
|
|
174
|
+
* 空函数,异步空函数为 asyncNoop
|
|
175
|
+
*/
|
|
154
176
|
function noop() {}
|
|
155
177
|
/**
|
|
156
|
-
|
|
157
|
-
|
|
178
|
+
* 检查是否为空值 null / undefined
|
|
179
|
+
*/
|
|
158
180
|
function isEmpty(data) {
|
|
159
|
-
|
|
181
|
+
return data === null || data === void 0;
|
|
160
182
|
}
|
|
161
183
|
/**
|
|
162
|
-
|
|
163
|
-
|
|
184
|
+
* 检查是否为 object(非 null / array)
|
|
185
|
+
*/
|
|
164
186
|
function isObject(data) {
|
|
165
|
-
|
|
187
|
+
return data !== null && typeof data === "object" && !Array.isArray(data);
|
|
166
188
|
}
|
|
167
189
|
/**
|
|
168
|
-
|
|
169
|
-
|
|
190
|
+
* 检测是否为 String Record
|
|
191
|
+
*/
|
|
170
192
|
function isStringRecord(data) {
|
|
171
|
-
|
|
193
|
+
return isObject(data) && Object.keys(data).every((key) => typeof key === "string");
|
|
172
194
|
}
|
|
173
195
|
/**
|
|
174
|
-
|
|
175
|
-
|
|
196
|
+
* 检查一个url地址是否以 http/https 开头,如果第二个参数传递 true,则自适应协议(//....)也被认为是完整 url
|
|
197
|
+
*/
|
|
176
198
|
function isFullURL(url, ignoreProtocol = false) {
|
|
177
|
-
|
|
199
|
+
return ignoreProtocol ? urlReg.test(url) : urlReg2.test(url);
|
|
178
200
|
}
|
|
179
201
|
/**
|
|
180
|
-
|
|
181
|
-
|
|
202
|
+
* 检测文本是否有可能转化为 object 或 array,注意,检测通过也未必能被正确格式化为 json
|
|
203
|
+
*/
|
|
182
204
|
function isJsonLike(text) {
|
|
183
|
-
|
|
205
|
+
return objReg.test(text) || arrReg.test(text);
|
|
184
206
|
}
|
|
185
207
|
/**
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
208
|
+
* 基于 baseURL 计算完整的 url 信息,仅可在浏览器环境使用
|
|
209
|
+
* @param url 要转化的 url,如果以 https: 或 http: 开头则直接返回
|
|
210
|
+
* @param baseURL 可选 baseURL
|
|
211
|
+
*/
|
|
190
212
|
function getFullURL(url, baseURL = "") {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
return (fillUrl(baseURL) + "/" + url).replace(/\/{2,}/g, "/").replace(/:\//, "://");
|
|
213
|
+
if (!baseURL || isFullURL(url, true)) return fillUrl(url);
|
|
214
|
+
return (fillUrl(baseURL) + "/" + url).replace(/\/{2,}/g, "/").replace(/:\//, "://");
|
|
195
215
|
}
|
|
196
216
|
/**
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
217
|
+
* 给 url 添加完整域名和协议,仅可在浏览器环境使用
|
|
218
|
+
* @param url 要检查的 url,如果是自适应协议 //... 则会添加当前页面的协议信息,否则添加域名和协议信息
|
|
219
|
+
*/
|
|
200
220
|
function fillUrl(url) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
}
|
|
207
|
-
return ("location" in globalThis ? location.origin + "/" : "http://127.0.0.1/") + url.replace(/^\/+/, "");
|
|
221
|
+
if (isFullURL(url, true)) {
|
|
222
|
+
if (url.startsWith("http")) return url;
|
|
223
|
+
return ("location" in globalThis ? location.protocol : "https:") + url;
|
|
224
|
+
}
|
|
225
|
+
return ("location" in globalThis ? location.origin + "/" : "http://127.0.0.1/") + url.replace(/^\/+/, "");
|
|
208
226
|
}
|
|
209
227
|
/**
|
|
210
|
-
|
|
211
|
-
|
|
228
|
+
* 简版 Object.fromEntries
|
|
229
|
+
*/
|
|
212
230
|
function fromEntries(kv) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
return result;
|
|
218
|
-
}, {});
|
|
231
|
+
return kv.reduce((result, [key, value]) => {
|
|
232
|
+
if (key) result[key] = value || "";
|
|
233
|
+
return result;
|
|
234
|
+
}, {});
|
|
219
235
|
}
|
|
220
236
|
function parseJSON(str, typeGuard) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
237
|
+
try {
|
|
238
|
+
const data = JSON.parse(str);
|
|
239
|
+
if (typeGuard) return typeGuard(data) ? data : null;
|
|
240
|
+
return data;
|
|
241
|
+
} catch (e) {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
//#endregion
|
|
246
|
+
//#region src/utils/types.ts
|
|
232
247
|
function getTypeGuard(typeGuard, defaultMessage = "数据未能正确识别") {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
248
|
+
if (typeof typeGuard === "function") return {
|
|
249
|
+
guard: typeGuard,
|
|
250
|
+
message: defaultMessage
|
|
251
|
+
};
|
|
252
|
+
return {
|
|
253
|
+
guard: typeGuard.guard,
|
|
254
|
+
message: typeGuard.message || defaultMessage
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
//#endregion
|
|
258
|
+
//#region src/utils/config.ts
|
|
245
259
|
/**
|
|
246
|
-
|
|
247
|
-
|
|
260
|
+
* 校验通用配置,如果有错误,则直接 throw Error
|
|
261
|
+
*/
|
|
248
262
|
function verifyConfig(config) {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
throw Error(`Config.endpoint Error, Should be url, Preset Values: ${Endpoint.China}, ${Endpoint.Global}}`);
|
|
252
|
-
}
|
|
263
|
+
verifyConfigWithoutEndpoint(config);
|
|
264
|
+
if (!isFullURL(config.endpoint)) throw Error(`Config.endpoint Error, Should be url, Preset Values: ${Endpoint.China}, ${Endpoint.Global}}`);
|
|
253
265
|
}
|
|
254
266
|
/**
|
|
255
|
-
|
|
256
|
-
|
|
267
|
+
* 校验通用配置,如果有错误,则直接 throw Error
|
|
268
|
+
*/
|
|
257
269
|
function verifyConfigWithoutEndpoint(config) {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
throw Error(`Config.secret Error, Missing Required Secret`);
|
|
266
|
-
}
|
|
267
|
-
if (!config.secret.startsWith("sk_")) {
|
|
268
|
-
throw Error(`Config.secret Error, Should be Start With sk_`);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
270
|
+
if (!isObject(config)) throw Error("Config Missing");
|
|
271
|
+
if (!config.game) throw Error(`Config.game Error, Missing Required GameId`);
|
|
272
|
+
if (!config.secret) throw Error(`Config.secret Error, Missing Required Secret`);
|
|
273
|
+
if (!config.secret.startsWith("sk_")) throw Error(`Config.secret Error, Should be Start With sk_`);
|
|
274
|
+
}
|
|
275
|
+
//#endregion
|
|
276
|
+
//#region src/utils/signer.ts
|
|
272
277
|
const AuthorizationField = "Authorization";
|
|
273
|
-
const MaxTimeDiff =
|
|
278
|
+
const MaxTimeDiff = 300 * 1e3;
|
|
274
279
|
const timestampReg = /^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z$/;
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
// 时间戳精度只到秒,需要忽略毫秒值
|
|
310
|
-
const now = Math.floor(Date.now() / 1000) * 1000;
|
|
311
|
-
if (!ts || ts < now - MaxTimeDiff || ts > now + MaxTimeDiff) {
|
|
312
|
-
return false;
|
|
313
|
-
}
|
|
314
|
-
return signature === this.sign({
|
|
315
|
-
...option,
|
|
316
|
-
game,
|
|
317
|
-
timestamp
|
|
318
|
-
}, true);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
};
|
|
280
|
+
const SigningDefinitions = { HS256: {
|
|
281
|
+
prefix: "SEAYOO-HMAC-SHA256",
|
|
282
|
+
sign({ game, secret, method, url, data, timestamp }, onlySignature = false) {
|
|
283
|
+
if (timestamp && !timestampReg.test(timestamp)) return "ErrorTimestamp";
|
|
284
|
+
const payloadHash = sha256(convertBodyData(data || ""));
|
|
285
|
+
const ts = timestamp || getTimestamp();
|
|
286
|
+
const fullURL = isFullURL(url) ? new URL(url) : new URL(url, "http://x.com");
|
|
287
|
+
const uri = fullURL.pathname + fullURL.search;
|
|
288
|
+
const signature = hS256(secret, [
|
|
289
|
+
this.prefix,
|
|
290
|
+
method.toUpperCase(),
|
|
291
|
+
uri,
|
|
292
|
+
ts,
|
|
293
|
+
payloadHash
|
|
294
|
+
].join("\n"));
|
|
295
|
+
return onlySignature ? signature : `${this.prefix} Game=${game},Timestamp=${ts},Signature=${signature}`;
|
|
296
|
+
},
|
|
297
|
+
verify(authString, option) {
|
|
298
|
+
const info = parseRawAuthHeader(authString, this.prefix);
|
|
299
|
+
if (!info) return false;
|
|
300
|
+
const game = "Game" in info ? info.Game : null;
|
|
301
|
+
const timestamp = "Timestamp" in info ? info.Timestamp : null;
|
|
302
|
+
const signature = "Signature" in info ? info.Signature : null;
|
|
303
|
+
if (!game || !signature || !timestamp || option.game && option.game !== game) return false;
|
|
304
|
+
const ts = parseTimestamp(timestamp);
|
|
305
|
+
const now = Math.floor(Date.now() / 1e3) * 1e3;
|
|
306
|
+
if (!ts || ts < now - MaxTimeDiff || ts > now + MaxTimeDiff) return false;
|
|
307
|
+
return signature === this.sign({
|
|
308
|
+
...option,
|
|
309
|
+
game,
|
|
310
|
+
timestamp
|
|
311
|
+
}, true);
|
|
312
|
+
}
|
|
313
|
+
} };
|
|
322
314
|
/**
|
|
323
|
-
|
|
324
|
-
|
|
315
|
+
* 格式化 AuthorizationHeader
|
|
316
|
+
*/
|
|
325
317
|
function parseAuthorizationHeader(authString, version = "HS256") {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
318
|
+
if (!version || !SigningDefinitions[version]) {
|
|
319
|
+
console.error({
|
|
320
|
+
type: "parseAuthorizationHeader Error",
|
|
321
|
+
message: `invalid version ${version}, support ${Object.keys(SigningDefinitions).join(", ")}`
|
|
322
|
+
});
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
325
|
+
return parseRawAuthHeader(authString, SigningDefinitions[version].prefix);
|
|
334
326
|
}
|
|
335
327
|
/**
|
|
336
|
-
|
|
337
|
-
|
|
328
|
+
* 获取请求签名值,默认签名方法是 HS256
|
|
329
|
+
*/
|
|
338
330
|
function calcAuthorizationHeader(option, onlySignature = false, version = "HS256") {
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
331
|
+
if (!version || !SigningDefinitions[version]) {
|
|
332
|
+
console.error({
|
|
333
|
+
type: "calcAuthorizationHeader Error",
|
|
334
|
+
message: `invalid version ${version}, support ${Object.keys(SigningDefinitions).join(", ")}`
|
|
335
|
+
});
|
|
336
|
+
return "ErrorSigningVersion";
|
|
337
|
+
}
|
|
338
|
+
return SigningDefinitions[version].sign(option, onlySignature);
|
|
347
339
|
}
|
|
348
340
|
/**
|
|
349
|
-
|
|
350
|
-
|
|
341
|
+
* 检查 http auth 信息是否正确
|
|
342
|
+
*/
|
|
351
343
|
function checkHttpAuthInfo(authString, option) {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
for (const cfg of configs) {
|
|
357
|
-
if (authString.startsWith(cfg.prefix + " ")) {
|
|
358
|
-
return cfg.verify(authString, option);
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
return false;
|
|
344
|
+
if (!authString) return false;
|
|
345
|
+
const configs = Object.values(SigningDefinitions);
|
|
346
|
+
for (const cfg of configs) if (authString.startsWith(cfg.prefix + " ")) return cfg.verify(authString, option);
|
|
347
|
+
return false;
|
|
362
348
|
}
|
|
363
349
|
function getTimestamp() {
|
|
364
|
-
|
|
365
|
-
return d.toISOString().replace(/\.\d+Z$/, "Z").replace(/[-:]/g, "");
|
|
350
|
+
return (/* @__PURE__ */ new Date()).toISOString().replace(/\.\d+Z$/, "Z").replace(/[-:]/g, "");
|
|
366
351
|
}
|
|
367
352
|
function parseTimestamp(ts) {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
372
|
-
return Date.UTC(+info[1], +info[2] - 1, +info[3], +info[4], +info[5], +info[6]);
|
|
353
|
+
const info = ts.match(timestampReg);
|
|
354
|
+
if (!info) return null;
|
|
355
|
+
return Date.UTC(+info[1], +info[2] - 1, +info[3], +info[4], +info[5], +info[6]);
|
|
373
356
|
}
|
|
374
357
|
function sha256(data) {
|
|
375
|
-
|
|
358
|
+
return createHash("sha256").update(data).digest("hex").toLowerCase();
|
|
376
359
|
}
|
|
377
360
|
function hS256(secret, data) {
|
|
378
|
-
|
|
361
|
+
return createHmac("sha256", secret).update(data).digest("hex").toLowerCase();
|
|
379
362
|
}
|
|
380
363
|
function convertBodyData(body) {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
}
|
|
384
|
-
return JSON.stringify(body);
|
|
364
|
+
if (typeof body === "string") return body;
|
|
365
|
+
return JSON.stringify(body);
|
|
385
366
|
}
|
|
386
367
|
function parseRawAuthHeader(authString, prefix) {
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
const value = getValue(kv[1]);
|
|
397
|
-
if (!key || !value) {
|
|
398
|
-
return null;
|
|
399
|
-
}
|
|
400
|
-
return [key, value];
|
|
401
|
-
}).filter(v => v !== null));
|
|
368
|
+
if (!authString || !authString.startsWith(prefix + " ")) return null;
|
|
369
|
+
return fromEntries(authString.substring(prefix.length + 1).split(",").map((kvs) => {
|
|
370
|
+
const kv = kvs.split("=");
|
|
371
|
+
if (kv.length !== 2) return null;
|
|
372
|
+
const key = kv[0].trim();
|
|
373
|
+
const value = getValue(kv[1]);
|
|
374
|
+
if (!key || !value) return null;
|
|
375
|
+
return [key, value];
|
|
376
|
+
}).filter((v) => v !== null));
|
|
402
377
|
}
|
|
403
378
|
function getValue(value) {
|
|
404
|
-
|
|
405
|
-
|
|
379
|
+
const v = value.trim();
|
|
380
|
+
return /^".+"$/.test(v) ? v.replace(/^"|"$/g, "") : v;
|
|
406
381
|
}
|
|
407
|
-
|
|
382
|
+
//#endregion
|
|
383
|
+
//#region src/utils/http.ts
|
|
408
384
|
async function readBody(req, backup) {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
385
|
+
const body = [];
|
|
386
|
+
if (!req.readable) {
|
|
387
|
+
backup || console.error({
|
|
388
|
+
type: "RequestUnReadable",
|
|
389
|
+
url: req.url,
|
|
390
|
+
methe: req.method,
|
|
391
|
+
message: "Cannot read body, stream not readable"
|
|
392
|
+
});
|
|
393
|
+
return backup || "";
|
|
394
|
+
}
|
|
395
|
+
return new Promise((resolve, reject) => {
|
|
396
|
+
req.on("error", reject);
|
|
397
|
+
req.on("data", (chunk) => body.push(chunk));
|
|
398
|
+
req.on("end", () => {
|
|
399
|
+
resolve(Buffer.concat(body).toString("utf-8"));
|
|
400
|
+
});
|
|
401
|
+
});
|
|
426
402
|
}
|
|
427
403
|
async function done(res, statusCode, message) {
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
}
|
|
432
|
-
res.end();
|
|
404
|
+
res.statusCode = statusCode;
|
|
405
|
+
if (statusCode !== 204) res.write(message || "");
|
|
406
|
+
res.end();
|
|
433
407
|
}
|
|
434
408
|
async function responseError(res, statusCode, error, message) {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
409
|
+
res.statusCode = statusCode;
|
|
410
|
+
if (statusCode !== 204) {
|
|
411
|
+
res.setHeader("Content-Type", "application/json");
|
|
412
|
+
res.write(JSON.stringify({
|
|
413
|
+
error,
|
|
414
|
+
message
|
|
415
|
+
}));
|
|
416
|
+
}
|
|
417
|
+
res.end();
|
|
444
418
|
}
|
|
445
419
|
async function responseJson(res, data, statusCode = 200) {
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
}
|
|
453
|
-
|
|
420
|
+
res.statusCode = statusCode;
|
|
421
|
+
if (statusCode !== 204) {
|
|
422
|
+
res.setHeader("Content-Type", "application/json");
|
|
423
|
+
res.write(JSON.stringify(data));
|
|
424
|
+
}
|
|
425
|
+
res.end();
|
|
426
|
+
}
|
|
427
|
+
//#endregion
|
|
428
|
+
//#region src/utils/index.ts
|
|
454
429
|
/** 生成一个 SessionID 以用作用户上下线的上报标记 */
|
|
455
430
|
function genSessionID(comboId) {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
431
|
+
const ts = process.hrtime.bigint();
|
|
432
|
+
const r = Math.random() + Math.random();
|
|
433
|
+
return createHash("md5").update(`${comboId}${ts}${r}`).digest("hex").toLowerCase();
|
|
459
434
|
}
|
|
460
435
|
/** 生成一个随机id */
|
|
461
436
|
function genNonceString() {
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
437
|
+
const ts = process.hrtime.bigint();
|
|
438
|
+
const r = Math.random() + Math.random();
|
|
439
|
+
return createHash("md5").update(`${ts}${r}`).digest("hex").toLowerCase();
|
|
465
440
|
}
|
|
466
|
-
|
|
441
|
+
//#endregion
|
|
442
|
+
//#region src/request/inc/rule.ts
|
|
467
443
|
const defaultDataField = "data";
|
|
468
444
|
const defaultMessageField = "message";
|
|
469
445
|
/**
|
|
470
|
-
|
|
471
|
-
|
|
446
|
+
* 分析响应内容中的错误信息和数据
|
|
447
|
+
*/
|
|
472
448
|
function analyseResponse(status, statusText, responseText, globalRule, rule) {
|
|
473
|
-
|
|
474
|
-
|
|
449
|
+
const responseRule = rule || globalRule;
|
|
450
|
+
return statusOK(status) ? getResponseOKInfo(responseRule.ok || globalRule.ok, status, statusText, responseText) : getReponseFailedInfo(responseRule.failed || globalRule.failed, statusText, responseText);
|
|
475
451
|
}
|
|
476
452
|
function getReponseFailedInfo(rule, statusText, responseText) {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
break;
|
|
499
|
-
}
|
|
500
|
-
return result;
|
|
453
|
+
const r = rule || {
|
|
454
|
+
resolve: "json",
|
|
455
|
+
messageField: defaultMessageField
|
|
456
|
+
};
|
|
457
|
+
const result = {
|
|
458
|
+
ok: false,
|
|
459
|
+
code: statusText,
|
|
460
|
+
message: responseText,
|
|
461
|
+
data: null
|
|
462
|
+
};
|
|
463
|
+
switch (r.resolve) {
|
|
464
|
+
case "body":
|
|
465
|
+
result.message = formatPresetBody(responseText) || responseText;
|
|
466
|
+
break;
|
|
467
|
+
case "json":
|
|
468
|
+
const { code, message } = formatJsonBody(responseText, r.statusField, r.messageField);
|
|
469
|
+
result.code = code || statusText;
|
|
470
|
+
result.message = formatPresetBody(responseText) || message;
|
|
471
|
+
break;
|
|
472
|
+
}
|
|
473
|
+
return result;
|
|
501
474
|
}
|
|
502
475
|
function formatJsonBody(body, statusField, msgField = defaultMessageField) {
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
return {
|
|
511
|
-
message: body
|
|
512
|
-
};
|
|
513
|
-
}
|
|
514
|
-
return {
|
|
515
|
-
code: statusField ? readMessageFrom(json, statusField) : "",
|
|
516
|
-
message: readMessageFrom(json, msgField) || body
|
|
517
|
-
};
|
|
476
|
+
if (!isJsonLike(body)) return { message: "" };
|
|
477
|
+
const json = parseJSON(body);
|
|
478
|
+
if (!json || !isStringRecord(json)) return { message: body };
|
|
479
|
+
return {
|
|
480
|
+
code: statusField ? readMessageFrom(json, statusField) : "",
|
|
481
|
+
message: readMessageFrom(json, msgField) || body
|
|
482
|
+
};
|
|
518
483
|
}
|
|
519
484
|
function readMessageFrom(json, field) {
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
return convert2str(json[field]);
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
return "";
|
|
485
|
+
const fields = Array.isArray(field) ? field : [field];
|
|
486
|
+
for (const field of fields) if (field in json) return convert2str(json[field]);
|
|
487
|
+
return "";
|
|
527
488
|
}
|
|
528
489
|
function convert2str(data) {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
if (typeof data === "string") {
|
|
533
|
-
return data;
|
|
534
|
-
}
|
|
535
|
-
return JSON.stringify(data);
|
|
490
|
+
if (!data) return "";
|
|
491
|
+
if (typeof data === "string") return data;
|
|
492
|
+
return JSON.stringify(data);
|
|
536
493
|
}
|
|
537
494
|
const htmlReg = /<title>([^<]+)<\/title>/i;
|
|
538
495
|
const xmlReg = /<message>([^<]+)<\/message>/i;
|
|
539
496
|
function formatPresetBody(str) {
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
if (xmlMatch) {
|
|
546
|
-
return xmlMatch[1];
|
|
547
|
-
}
|
|
548
|
-
return "";
|
|
497
|
+
const htmlMatch = str.match(htmlReg);
|
|
498
|
+
if (htmlMatch) return htmlMatch[1];
|
|
499
|
+
const xmlMatch = str.match(xmlReg);
|
|
500
|
+
if (xmlMatch) return xmlMatch[1];
|
|
501
|
+
return "";
|
|
549
502
|
}
|
|
550
503
|
function getResponseOKInfo(rule, status, statusText, responseText) {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
result.code = code || statusText;
|
|
591
|
-
result.data = dataField in response ? response[dataField] : null;
|
|
592
|
-
result.message = readMessageFrom(response, messageField);
|
|
593
|
-
if (ignoreMessage && result.message) {
|
|
594
|
-
if (Array.isArray(ignoreMessage) && ignoreMessage.includes(result.message) || typeof ignoreMessage === "string" && result.message === ignoreMessage) {
|
|
595
|
-
result.message = "";
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
return result;
|
|
504
|
+
const r = rule || { resolve: "body" };
|
|
505
|
+
const result = {
|
|
506
|
+
ok: true,
|
|
507
|
+
code: statusText,
|
|
508
|
+
message: "",
|
|
509
|
+
data: null
|
|
510
|
+
};
|
|
511
|
+
if (status === 202 || status === 204 || !responseText) return result;
|
|
512
|
+
if (r.resolve === "body") {
|
|
513
|
+
result.data = isJsonLike(responseText) ? parseJSON(responseText) : responseText;
|
|
514
|
+
return result;
|
|
515
|
+
}
|
|
516
|
+
const response = parseJSON(responseText);
|
|
517
|
+
if (!response || !isStringRecord(response)) {
|
|
518
|
+
result.ok = false;
|
|
519
|
+
result.code = "ResponseFormatError";
|
|
520
|
+
result.message = "响应内容无法格式化为 Object";
|
|
521
|
+
return result;
|
|
522
|
+
}
|
|
523
|
+
const statusField = r.statusField;
|
|
524
|
+
const statusOKValue = r.statusOKValue || "";
|
|
525
|
+
const dataField = r.dataField || defaultDataField;
|
|
526
|
+
const messageField = r.messageField || defaultMessageField;
|
|
527
|
+
const ignoreMessage = r.ignoreMessage || "";
|
|
528
|
+
if (statusField && !(statusField in response)) {
|
|
529
|
+
result.ok = false;
|
|
530
|
+
result.code = "ResponseFieldMissing";
|
|
531
|
+
result.message = "响应内容找不到状态字段 " + statusField;
|
|
532
|
+
return result;
|
|
533
|
+
}
|
|
534
|
+
const code = statusField ? response[statusField] + "" : "";
|
|
535
|
+
result.ok = statusField ? code === statusOKValue : true;
|
|
536
|
+
result.code = code || statusText;
|
|
537
|
+
result.data = dataField in response ? response[dataField] : null;
|
|
538
|
+
result.message = readMessageFrom(response, messageField);
|
|
539
|
+
if (ignoreMessage && result.message) {
|
|
540
|
+
if (Array.isArray(ignoreMessage) && ignoreMessage.includes(result.message) || typeof ignoreMessage === "string" && result.message === ignoreMessage) result.message = "";
|
|
541
|
+
}
|
|
542
|
+
return result;
|
|
599
543
|
}
|
|
600
544
|
function statusOK(status) {
|
|
601
|
-
|
|
545
|
+
return status >= 200 && status < 400;
|
|
602
546
|
}
|
|
603
|
-
|
|
547
|
+
//#endregion
|
|
548
|
+
//#region src/request/inc/retry.ts
|
|
604
549
|
async function retryRequest(agent, url, config, options, _try) {
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
return await retryRequest(agent, url, config, options, currentTryCount + 1);
|
|
645
|
-
}
|
|
646
|
-
|
|
550
|
+
var _ref, _options$maxRetry, _options$retryResolve, _options$retryInterva;
|
|
551
|
+
const currentTryCount = _try || 0;
|
|
552
|
+
const maxRetry = Math.min(10, (_ref = (_options$maxRetry = options === null || options === void 0 ? void 0 : options.maxRetry) !== null && _options$maxRetry !== void 0 ? _options$maxRetry : config.get("maxRetry")) !== null && _ref !== void 0 ? _ref : 0);
|
|
553
|
+
const retryResolve = (_options$retryResolve = options === null || options === void 0 ? void 0 : options.retryResolve) !== null && _options$retryResolve !== void 0 ? _options$retryResolve : config.get("retryResolve");
|
|
554
|
+
const logger = config.get("logHandler") || noop;
|
|
555
|
+
logger({
|
|
556
|
+
type: "prepear",
|
|
557
|
+
url,
|
|
558
|
+
method: (options === null || options === void 0 ? void 0 : options.method) || "GET",
|
|
559
|
+
retry: currentTryCount,
|
|
560
|
+
maxRetry,
|
|
561
|
+
message: currentTryCount === 0 ? "start" : `retry ${currentTryCount}/${maxRetry} start`,
|
|
562
|
+
headers: options === null || options === void 0 ? void 0 : options.headers,
|
|
563
|
+
options
|
|
564
|
+
});
|
|
565
|
+
const start = Date.now();
|
|
566
|
+
const result = await agent(url, config, options);
|
|
567
|
+
const status = result.status;
|
|
568
|
+
const cost = Date.now() - start;
|
|
569
|
+
const payload = `[cost ${cost}][${status}] ${status < 0 ? result.body : ""}`;
|
|
570
|
+
logger({
|
|
571
|
+
type: "finished",
|
|
572
|
+
url,
|
|
573
|
+
method: result.method,
|
|
574
|
+
retry: currentTryCount,
|
|
575
|
+
maxRetry,
|
|
576
|
+
message: currentTryCount === 0 ? `finish ${payload}` : `retry ${currentTryCount}/${maxRetry} finish ${payload}`,
|
|
577
|
+
response: result,
|
|
578
|
+
headers: result.headers,
|
|
579
|
+
cost
|
|
580
|
+
});
|
|
581
|
+
const ok = statusOK(status);
|
|
582
|
+
if (!maxRetry || retryResolve === "network" && status > 0 || retryResolve === "status" && ok || currentTryCount >= maxRetry) return result;
|
|
583
|
+
const retryInterval = (_options$retryInterva = options === null || options === void 0 ? void 0 : options.retryInterval) !== null && _options$retryInterva !== void 0 ? _options$retryInterva : config.get("retryInterval");
|
|
584
|
+
await sleep(Math.max(100, typeof retryInterval === "function" ? retryInterval(currentTryCount + 1) : retryInterval));
|
|
585
|
+
return await retryRequest(agent, url, config, options, currentTryCount + 1);
|
|
586
|
+
}
|
|
587
|
+
//#endregion
|
|
588
|
+
//#region src/request/inc/response.ts
|
|
647
589
|
function handleResponse(res, url, config, options) {
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
status: res.status,
|
|
669
|
-
headers: fromEntries(Object.entries(res.headers || {}).map(([key, value]) => [key.toLowerCase(), value]))
|
|
670
|
-
};
|
|
671
|
-
// 全局处理响应结果 (浅复制,防止被修改,同时保留修改 result.data 的可能性)
|
|
672
|
-
(_config$get2 = config.get("responseHandler")) == null || _config$get2({
|
|
673
|
-
...result
|
|
674
|
-
}, res.method, url);
|
|
675
|
-
// 检查并全局提示
|
|
676
|
-
const defaultMessage = result.ok ? result.message : `${res.method} ${url} [${result.code || res.statusText}] ${result.message || res.statusText}`;
|
|
677
|
-
return checkGlobalMessage(result, defaultMessage, res.method, url, config, options);
|
|
590
|
+
var _config$get2;
|
|
591
|
+
if (res.status < 0) return checkGlobalMessage({
|
|
592
|
+
ok: false,
|
|
593
|
+
status: res.status,
|
|
594
|
+
code: res.statusText,
|
|
595
|
+
headers: {},
|
|
596
|
+
message: "",
|
|
597
|
+
data: null
|
|
598
|
+
}, `${res.method} ${url} ${res.statusText}`, res.method, url, config, options);
|
|
599
|
+
if (!statusOK(res.status)) {
|
|
600
|
+
var _config$get;
|
|
601
|
+
(_config$get = config.get("errorHandler")) === null || _config$get === void 0 || _config$get(res.status, res.method, url);
|
|
602
|
+
}
|
|
603
|
+
const result = {
|
|
604
|
+
...analyseResponse(res.status, res.statusText, res.body, config.get("responseRule"), options === null || options === void 0 ? void 0 : options.responseRule),
|
|
605
|
+
status: res.status,
|
|
606
|
+
headers: fromEntries(Object.entries(res.headers || {}).map(([key, value]) => [key.toLowerCase(), value]))
|
|
607
|
+
};
|
|
608
|
+
(_config$get2 = config.get("responseHandler")) === null || _config$get2 === void 0 || _config$get2({ ...result }, res.method, url);
|
|
609
|
+
return checkGlobalMessage(result, result.ok ? result.message : `${res.method} ${url} [${result.code || res.statusText}] ${result.message || res.statusText}`, res.method, url, config, options);
|
|
678
610
|
}
|
|
679
611
|
function checkGlobalMessage(result, defaultMessage, method, url, config, options) {
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
612
|
+
const gmessageConvert = config.get("message");
|
|
613
|
+
const messageConvert = gmessageConvert === false || (options === null || options === void 0 ? void 0 : options.message) === false ? false : (options === null || options === void 0 ? void 0 : options.message) || gmessageConvert;
|
|
614
|
+
if (messageConvert !== false) config.showMessage(!result.ok, typeof messageConvert === "function" ? messageConvert(result, method, url, defaultMessage) : defaultMessage);
|
|
615
|
+
return result;
|
|
616
|
+
}
|
|
617
|
+
//#endregion
|
|
618
|
+
//#region src/request/inc/option.ts
|
|
688
619
|
async function convertOptions(url, config, options) {
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
// 返回
|
|
732
|
-
return {
|
|
733
|
-
url: finalUrl,
|
|
734
|
-
method,
|
|
735
|
-
body,
|
|
736
|
-
params,
|
|
737
|
-
headers,
|
|
738
|
-
timeout
|
|
739
|
-
};
|
|
620
|
+
var _config$get, _config$get2;
|
|
621
|
+
const opt = Object.assign({ method: "GET" }, options);
|
|
622
|
+
const method = opt.method;
|
|
623
|
+
if (method === "GET" || method === "HEAD" || method === "DELETE") {
|
|
624
|
+
if (opt.body !== void 0) {
|
|
625
|
+
console.warn("request body is invalid with method get, head, delete");
|
|
626
|
+
delete opt.body;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
const headers = Object.assign({ "Content-Type": "application/json;charset=utf-8" }, opt.headers);
|
|
630
|
+
const p = opt.params || {};
|
|
631
|
+
const params = {};
|
|
632
|
+
Object.keys(p).forEach((key) => {
|
|
633
|
+
if (p[key] !== void 0) params[key] = convertParam(p[key]);
|
|
634
|
+
});
|
|
635
|
+
const fullUrl = config.getFullUrl(url);
|
|
636
|
+
const body = convertBody(opt.body);
|
|
637
|
+
const timeout = opt.timeout || config.get("timeout");
|
|
638
|
+
const newURL = await ((_config$get = config.get("requestTransformer")) === null || _config$get === void 0 ? void 0 : _config$get({
|
|
639
|
+
headers,
|
|
640
|
+
params,
|
|
641
|
+
method,
|
|
642
|
+
url: fullUrl,
|
|
643
|
+
body
|
|
644
|
+
}));
|
|
645
|
+
const finalUrl = typeof newURL === "string" && newURL ? newURL : fullUrl;
|
|
646
|
+
(_config$get2 = config.get("logHandler")) === null || _config$get2 === void 0 || _config$get2({
|
|
647
|
+
type: "ready",
|
|
648
|
+
url: finalUrl,
|
|
649
|
+
method,
|
|
650
|
+
headers,
|
|
651
|
+
timeout,
|
|
652
|
+
body
|
|
653
|
+
});
|
|
654
|
+
return {
|
|
655
|
+
url: finalUrl,
|
|
656
|
+
method,
|
|
657
|
+
body,
|
|
658
|
+
params,
|
|
659
|
+
headers,
|
|
660
|
+
timeout
|
|
661
|
+
};
|
|
740
662
|
}
|
|
741
663
|
/** 将数据转成 string 格式表示,仅仅特殊处理 array 为 item,item... */
|
|
742
664
|
function convertParam(val) {
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
if (Array.isArray(val)) {
|
|
747
|
-
return val.join(",");
|
|
748
|
-
}
|
|
749
|
-
return val + "";
|
|
665
|
+
if (typeof val === "string") return val;
|
|
666
|
+
if (Array.isArray(val)) return val.join(",");
|
|
667
|
+
return val + "";
|
|
750
668
|
}
|
|
751
669
|
function convertBody(body) {
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
return JSON.stringify(body);
|
|
759
|
-
}
|
|
760
|
-
|
|
670
|
+
if (!body) return;
|
|
671
|
+
if (typeof body === "string" || body instanceof URLSearchParams) return body;
|
|
672
|
+
return JSON.stringify(body);
|
|
673
|
+
}
|
|
674
|
+
//#endregion
|
|
675
|
+
//#region src/request/inc/request.ts
|
|
761
676
|
/**
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
const nodeRequest = async function
|
|
767
|
-
|
|
677
|
+
* 基于 nodejs 的 http/https 包的网络请求包装函数,url 必须是一个完整 url
|
|
678
|
+
*
|
|
679
|
+
* 该方法必定 resolve,限制在 nodejs 环境中使用
|
|
680
|
+
*/
|
|
681
|
+
const nodeRequest = async function(url, config, options) {
|
|
682
|
+
return handleResponse(await retryRequest(coreNodeRequest, url, config, options), url, config, options);
|
|
768
683
|
};
|
|
769
|
-
const coreNodeRequest = async function
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
});
|
|
828
|
-
if (opt.body) {
|
|
829
|
-
req.write(opt.body);
|
|
830
|
-
}
|
|
831
|
-
req.end();
|
|
832
|
-
});
|
|
684
|
+
const coreNodeRequest = async function(url, config, options) {
|
|
685
|
+
const opt = await convertOptions(url, config, options);
|
|
686
|
+
if (!isFullURL(opt.url)) return {
|
|
687
|
+
url: opt.url,
|
|
688
|
+
method: opt.method,
|
|
689
|
+
status: -2,
|
|
690
|
+
statusText: "URLFormatError",
|
|
691
|
+
headers: {},
|
|
692
|
+
body: ""
|
|
693
|
+
};
|
|
694
|
+
const client = /^https:\/\//i.test(opt.url) ? https : http;
|
|
695
|
+
const iURL = new URL(opt.url);
|
|
696
|
+
const params = opt.params;
|
|
697
|
+
if (params instanceof Object) Object.keys(params).forEach((key) => iURL.searchParams.set(key, params[key]));
|
|
698
|
+
const isHeadRequest = opt.method === "HEAD";
|
|
699
|
+
return new Promise((resolve) => {
|
|
700
|
+
const req = client.request(iURL.toString(), {
|
|
701
|
+
headers: opt.headers,
|
|
702
|
+
method: opt.method,
|
|
703
|
+
timeout: opt.timeout > 0 ? opt.timeout : void 0
|
|
704
|
+
}, function(res) {
|
|
705
|
+
const resbody = [];
|
|
706
|
+
res.on("data", (chunk) => resbody.push(chunk));
|
|
707
|
+
res.on("end", () => {
|
|
708
|
+
const headers = fromEntries(Object.entries(res.headers).map(([key, value]) => {
|
|
709
|
+
return [key.toLowerCase(), Array.isArray(value) ? value.join(",") : value];
|
|
710
|
+
}));
|
|
711
|
+
resolve({
|
|
712
|
+
url: iURL.toString(),
|
|
713
|
+
method: opt.method,
|
|
714
|
+
status: res.statusCode || -3,
|
|
715
|
+
statusText: res.statusMessage || "Unknown",
|
|
716
|
+
headers,
|
|
717
|
+
body: isHeadRequest ? "" : Buffer.concat(resbody).toString("utf-8")
|
|
718
|
+
});
|
|
719
|
+
});
|
|
720
|
+
});
|
|
721
|
+
req.on("error", (e) => {
|
|
722
|
+
resolve({
|
|
723
|
+
url: iURL.toString(),
|
|
724
|
+
method: opt.method,
|
|
725
|
+
status: -1,
|
|
726
|
+
statusText: e.name || "Unknown",
|
|
727
|
+
body: e.message
|
|
728
|
+
});
|
|
729
|
+
});
|
|
730
|
+
req.on("timeout", () => {
|
|
731
|
+
resolve({
|
|
732
|
+
url: iURL.toString(),
|
|
733
|
+
method: opt.method,
|
|
734
|
+
status: -1,
|
|
735
|
+
statusText: "Timeout",
|
|
736
|
+
body: ""
|
|
737
|
+
});
|
|
738
|
+
});
|
|
739
|
+
if (opt.body) req.write(opt.body);
|
|
740
|
+
req.end();
|
|
741
|
+
});
|
|
833
742
|
};
|
|
834
|
-
|
|
743
|
+
//#endregion
|
|
744
|
+
//#region src/request/inc/cache.ts
|
|
835
745
|
/** 为 get 请求并发而设计的缓冲工具 */
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
}
|
|
868
|
-
this.cache[key] = {
|
|
869
|
-
ttl: Date.now() + this.ttl,
|
|
870
|
-
res: action
|
|
871
|
-
};
|
|
872
|
-
}
|
|
873
|
-
}
|
|
874
|
-
|
|
746
|
+
var Cache = class {
|
|
747
|
+
constructor(ttl = 500) {
|
|
748
|
+
this.cache = {};
|
|
749
|
+
this.ttl = Math.max(ttl, 0);
|
|
750
|
+
}
|
|
751
|
+
getKey(url, param) {
|
|
752
|
+
return url + Object.keys(param || {}).sort().map((key) => `${key}#${param === null || param === void 0 ? void 0 : param[key]}`).join(",");
|
|
753
|
+
}
|
|
754
|
+
updateTTL(ttl) {
|
|
755
|
+
this.ttl = Math.max(ttl, 0);
|
|
756
|
+
}
|
|
757
|
+
get(key) {
|
|
758
|
+
if (this.ttl === 0) return null;
|
|
759
|
+
const c = this.cache[key];
|
|
760
|
+
if (!c) return null;
|
|
761
|
+
if (c.ttl < Date.now()) {
|
|
762
|
+
delete this.cache[key];
|
|
763
|
+
return null;
|
|
764
|
+
}
|
|
765
|
+
return c.res;
|
|
766
|
+
}
|
|
767
|
+
set(key, action) {
|
|
768
|
+
if (this.ttl === 0) return;
|
|
769
|
+
this.cache[key] = {
|
|
770
|
+
ttl: Date.now() + this.ttl,
|
|
771
|
+
res: action
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
};
|
|
775
|
+
//#endregion
|
|
776
|
+
//#region src/request/inc/guard.ts
|
|
875
777
|
function checkTypedDataResult(url, result, config, typeGard) {
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
this.config.messageHandler(isError, message);
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
|
|
778
|
+
if (result.ok && !isEmpty(result.data) && typeGard) {
|
|
779
|
+
const guard = getTypeGuard(typeGard, "响应数据未能正确识别");
|
|
780
|
+
if (guard.guard(result.data)) return result;
|
|
781
|
+
config.showMessage(true, `${url} ${guard.message}`);
|
|
782
|
+
result.data = null;
|
|
783
|
+
return result;
|
|
784
|
+
}
|
|
785
|
+
return result;
|
|
786
|
+
}
|
|
787
|
+
//#endregion
|
|
788
|
+
//#region src/request/inc/config.ts
|
|
789
|
+
var RequestGlobalConfig = class {
|
|
790
|
+
constructor(config) {
|
|
791
|
+
this.config = {
|
|
792
|
+
baseURL: "",
|
|
793
|
+
maxRetry: 0,
|
|
794
|
+
retryInterval: 1e3,
|
|
795
|
+
retryResolve: "network",
|
|
796
|
+
timeout: 5e3,
|
|
797
|
+
cacheTTL: 500,
|
|
798
|
+
responseRule: {
|
|
799
|
+
ok: { resolve: "body" },
|
|
800
|
+
failed: {
|
|
801
|
+
resolve: "json",
|
|
802
|
+
messageField: "message"
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
};
|
|
806
|
+
config && this.set(config);
|
|
807
|
+
}
|
|
808
|
+
set(config) {
|
|
809
|
+
if (config.baseURL && !/^\/.+/.test(config.baseURL) && !isFullURL(config.baseURL)) {
|
|
810
|
+
console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址");
|
|
811
|
+
throw new Error("BaseURLError");
|
|
812
|
+
}
|
|
813
|
+
Object.assign(this.config, config);
|
|
814
|
+
}
|
|
815
|
+
get(key) {
|
|
816
|
+
return this.config[key];
|
|
817
|
+
}
|
|
818
|
+
/** 基于 baseURL 返回完整的 url 地址, 如果 url 地址以 / 开头则表示忽略 baseURL 的值 */
|
|
819
|
+
getFullUrl(url) {
|
|
820
|
+
return url.startsWith("/") ? getFullURL(url) : getFullURL(url, this.config.baseURL);
|
|
821
|
+
}
|
|
822
|
+
/** 提示消息 */
|
|
823
|
+
showMessage(isError, message) {
|
|
824
|
+
if (this.config.messageHandler && message) this.config.messageHandler(isError, message);
|
|
825
|
+
}
|
|
826
|
+
};
|
|
827
|
+
//#endregion
|
|
828
|
+
//#region src/request/inc/main.ts
|
|
932
829
|
/** 工具函数主类 */
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
ops.body = data;
|
|
1017
|
-
return this.guard(url, await this.exec(url, ops), typeGard || null);
|
|
1018
|
-
}
|
|
1019
|
-
}
|
|
1020
|
-
|
|
830
|
+
var NetRequestHandler = class {
|
|
831
|
+
constructor(agent, config) {
|
|
832
|
+
this.config = new RequestGlobalConfig(config);
|
|
833
|
+
this.agent = agent;
|
|
834
|
+
this.cache = new Cache(this.config.get("cacheTTL"));
|
|
835
|
+
this.setConfig = this.setConfig.bind(this);
|
|
836
|
+
this.getConfig = this.getConfig.bind(this);
|
|
837
|
+
this.get = this.get.bind(this);
|
|
838
|
+
this.post = this.post.bind(this);
|
|
839
|
+
this.del = this.del.bind(this);
|
|
840
|
+
this.patch = this.patch.bind(this);
|
|
841
|
+
this.put = this.put.bind(this);
|
|
842
|
+
this.head = this.head.bind(this);
|
|
843
|
+
}
|
|
844
|
+
/**
|
|
845
|
+
* 执行网络请求
|
|
846
|
+
*/
|
|
847
|
+
async exec(url, options) {
|
|
848
|
+
return await this.agent(url, this.config, options);
|
|
849
|
+
}
|
|
850
|
+
/**
|
|
851
|
+
* 检查响应的数据类型
|
|
852
|
+
*/
|
|
853
|
+
async guard(url, result, typeGard) {
|
|
854
|
+
return checkTypedDataResult(url, result, this.config, typeGard);
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* 修改默认请求配置: baesURL / timeout / credentials / errorHandler / messageHandler / responseHandler / logHandler / responseRule
|
|
858
|
+
*/
|
|
859
|
+
setConfig(config) {
|
|
860
|
+
this.config.set(config);
|
|
861
|
+
this.cache.updateTTL(this.config.get("cacheTTL"));
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
* 读取默认的请求配置
|
|
865
|
+
*/
|
|
866
|
+
getConfig(key) {
|
|
867
|
+
return this.config.get(key);
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* 发送一个 HEAD 请求,并且不处理响应 body
|
|
871
|
+
*/
|
|
872
|
+
async head(url, options) {
|
|
873
|
+
const ops = Object.assign({}, options || null);
|
|
874
|
+
ops.method = "HEAD";
|
|
875
|
+
return this.guard(url, await this.exec(url, ops), null);
|
|
876
|
+
}
|
|
877
|
+
async get(url, typeGard, options) {
|
|
878
|
+
const ops = Object.assign({}, options || null);
|
|
879
|
+
ops.method = "GET";
|
|
880
|
+
const cacheKey = this.cache.getKey(url, ops.params);
|
|
881
|
+
const cache = this.cache.get(cacheKey);
|
|
882
|
+
if (cache) return this.guard(url, await cache, typeGard || null);
|
|
883
|
+
const res = this.exec(url, ops);
|
|
884
|
+
this.cache.set(cacheKey, res);
|
|
885
|
+
return this.guard(url, await res, typeGard || null);
|
|
886
|
+
}
|
|
887
|
+
async post(url, data, typeGard, options) {
|
|
888
|
+
const ops = Object.assign({}, options || null);
|
|
889
|
+
ops.method = "POST";
|
|
890
|
+
ops.body = data;
|
|
891
|
+
return this.guard(url, await this.exec(url, ops), typeGard || null);
|
|
892
|
+
}
|
|
893
|
+
async del(url, typeGard, options) {
|
|
894
|
+
const ops = Object.assign({}, options || null);
|
|
895
|
+
ops.method = "DELETE";
|
|
896
|
+
return this.guard(url, await this.exec(url, ops), typeGard || null);
|
|
897
|
+
}
|
|
898
|
+
async put(url, data, typeGard, options) {
|
|
899
|
+
const ops = Object.assign({}, options || null);
|
|
900
|
+
ops.method = "PUT";
|
|
901
|
+
ops.body = data;
|
|
902
|
+
return this.guard(url, await this.exec(url, ops), typeGard || null);
|
|
903
|
+
}
|
|
904
|
+
async patch(url, data, typeGard, options) {
|
|
905
|
+
const ops = Object.assign({}, options || null);
|
|
906
|
+
ops.method = "PATCH";
|
|
907
|
+
ops.body = data;
|
|
908
|
+
return this.guard(url, await this.exec(url, ops), typeGard || null);
|
|
909
|
+
}
|
|
910
|
+
};
|
|
911
|
+
//#endregion
|
|
912
|
+
//#region src/request/index.ts
|
|
1021
913
|
function NetRequest(config) {
|
|
1022
|
-
|
|
914
|
+
return new NetRequestHandler(nodeRequest, config);
|
|
1023
915
|
}
|
|
1024
|
-
|
|
916
|
+
//#endregion
|
|
917
|
+
//#region src/api/index.ts
|
|
1025
918
|
const ApiPrefix = "/v1/server";
|
|
1026
919
|
const TraceIdField = "x-trace-id";
|
|
1027
920
|
/**
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
});
|
|
1183
|
-
throw new Error(`sendOtp: ${message || code || status}`);
|
|
1184
|
-
}
|
|
1185
|
-
return data;
|
|
1186
|
-
}
|
|
1187
|
-
/**
|
|
1188
|
-
* 验证验证码
|
|
1189
|
-
*
|
|
1190
|
-
* @param option 验证码验证参数
|
|
1191
|
-
*/
|
|
1192
|
-
async verifyOtp(option) {
|
|
1193
|
-
const {
|
|
1194
|
-
ok,
|
|
1195
|
-
data,
|
|
1196
|
-
code,
|
|
1197
|
-
status,
|
|
1198
|
-
message,
|
|
1199
|
-
headers
|
|
1200
|
-
} = await this.req.post("verify-otp", option, isVerifyOtpResponse);
|
|
1201
|
-
if (!ok || !data) {
|
|
1202
|
-
console.error({
|
|
1203
|
-
type: "verifyOtp Error",
|
|
1204
|
-
status,
|
|
1205
|
-
code,
|
|
1206
|
-
message,
|
|
1207
|
-
traceId: headers[TraceIdField]
|
|
1208
|
-
});
|
|
1209
|
-
throw new Error(`verifyOtp: ${message || code || status}`);
|
|
1210
|
-
}
|
|
1211
|
-
return data;
|
|
1212
|
-
}
|
|
1213
|
-
}
|
|
921
|
+
* Combo SDK For Server REST Api
|
|
922
|
+
*/
|
|
923
|
+
var ApiClient = class {
|
|
924
|
+
constructor(config) {
|
|
925
|
+
var _config$maxRetry;
|
|
926
|
+
verifyConfig(config);
|
|
927
|
+
const ua = getUserAgent(config.game);
|
|
928
|
+
this.req = NetRequest({
|
|
929
|
+
baseURL: `${config.endpoint.replace(/\/$/, "")}${ApiPrefix}`,
|
|
930
|
+
maxRetry: (_config$maxRetry = config.maxRetry) !== null && _config$maxRetry !== void 0 ? _config$maxRetry : 1,
|
|
931
|
+
retryInterval: config.retryInterval,
|
|
932
|
+
responseRule: {
|
|
933
|
+
ok: { resolve: "body" },
|
|
934
|
+
failed: {
|
|
935
|
+
resolve: "json",
|
|
936
|
+
statusField: "error",
|
|
937
|
+
messageField: "message"
|
|
938
|
+
}
|
|
939
|
+
},
|
|
940
|
+
requestTransformer({ headers, method, url, params, body }) {
|
|
941
|
+
const iURL = new URL(url);
|
|
942
|
+
if (params) {
|
|
943
|
+
if (params instanceof Object) Object.keys(params).forEach((key) => iURL.searchParams.set(key, params[key]));
|
|
944
|
+
}
|
|
945
|
+
headers["User-Agent"] = ua;
|
|
946
|
+
headers[AuthorizationField] = calcAuthorizationHeader({
|
|
947
|
+
game: config.game,
|
|
948
|
+
secret: config.secret,
|
|
949
|
+
endpoint: config.endpoint,
|
|
950
|
+
method,
|
|
951
|
+
url: iURL.toString(),
|
|
952
|
+
data: body || ""
|
|
953
|
+
});
|
|
954
|
+
},
|
|
955
|
+
logHandler: config.logger,
|
|
956
|
+
timeout: config.timeout || 5e3
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
/**
|
|
960
|
+
* 创建订单
|
|
961
|
+
*/
|
|
962
|
+
async createOrder(option) {
|
|
963
|
+
const { ok, data, code, status, message, headers } = await this.req.post("create-order", option, isCreateOrderResponse);
|
|
964
|
+
if (!ok || !data) {
|
|
965
|
+
console.error({
|
|
966
|
+
type: "createOrder Error",
|
|
967
|
+
status,
|
|
968
|
+
code,
|
|
969
|
+
message,
|
|
970
|
+
traceId: headers[TraceIdField]
|
|
971
|
+
});
|
|
972
|
+
throw new Error(`createOrder: ${message || code || status}`);
|
|
973
|
+
}
|
|
974
|
+
return data;
|
|
975
|
+
}
|
|
976
|
+
/**
|
|
977
|
+
* 通知世游服务端玩家进入游戏世界(上线)
|
|
978
|
+
*
|
|
979
|
+
* 此接口仅用于中宣部防沉迷系统的上下线数据上报
|
|
980
|
+
*
|
|
981
|
+
* @param comboId 聚合用户标识
|
|
982
|
+
* @param sessionId 游戏会话标识,单次游戏会话的上下线动作必须使用同一会话标识上报
|
|
983
|
+
*/
|
|
984
|
+
async enterGame(comboId, sessionId) {
|
|
985
|
+
const { ok, status, code, message, headers } = await this.req.post("enter-game", {
|
|
986
|
+
combo_id: comboId,
|
|
987
|
+
session_id: sessionId
|
|
988
|
+
});
|
|
989
|
+
if (!ok) console.error({
|
|
990
|
+
type: "enterGame Error",
|
|
991
|
+
status,
|
|
992
|
+
code,
|
|
993
|
+
message,
|
|
994
|
+
traceId: headers[TraceIdField]
|
|
995
|
+
});
|
|
996
|
+
return ok;
|
|
997
|
+
}
|
|
998
|
+
/**
|
|
999
|
+
* 通知世游服务端玩家离开游戏世界(下线)
|
|
1000
|
+
*
|
|
1001
|
+
* 此接口仅用于中宣部防沉迷系统的上下线数据上报
|
|
1002
|
+
*
|
|
1003
|
+
* @param comboId 聚合用户标识
|
|
1004
|
+
* @param sessionId 游戏会话标识,单次游戏会话的上下线动作必须使用同一会话标识上报
|
|
1005
|
+
*/
|
|
1006
|
+
async leaveGame(comboId, sessionId) {
|
|
1007
|
+
const { ok, status, code, message, headers } = await this.req.post("leave-game", {
|
|
1008
|
+
combo_id: comboId,
|
|
1009
|
+
session_id: sessionId
|
|
1010
|
+
});
|
|
1011
|
+
if (!ok) console.error({
|
|
1012
|
+
type: "leaveGame Error",
|
|
1013
|
+
status,
|
|
1014
|
+
code,
|
|
1015
|
+
message,
|
|
1016
|
+
traceId: headers[TraceIdField]
|
|
1017
|
+
});
|
|
1018
|
+
return ok;
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* 发送验证码
|
|
1022
|
+
*
|
|
1023
|
+
* @param option 发送验证码参数
|
|
1024
|
+
*/
|
|
1025
|
+
async sendOtp(option) {
|
|
1026
|
+
const { ok, data, code, status, message, headers } = await this.req.post("send-otp", option, isSendOtpResponse);
|
|
1027
|
+
if (!ok || !data) {
|
|
1028
|
+
console.error({
|
|
1029
|
+
type: "sendOtp Error",
|
|
1030
|
+
status,
|
|
1031
|
+
code,
|
|
1032
|
+
message,
|
|
1033
|
+
traceId: headers[TraceIdField]
|
|
1034
|
+
});
|
|
1035
|
+
throw new Error(`sendOtp: ${message || code || status}`);
|
|
1036
|
+
}
|
|
1037
|
+
return data;
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* 验证验证码
|
|
1041
|
+
*
|
|
1042
|
+
* @param option 验证码验证参数
|
|
1043
|
+
*/
|
|
1044
|
+
async verifyOtp(option) {
|
|
1045
|
+
const { ok, data, code, status, message, headers } = await this.req.post("verify-otp", option, isVerifyOtpResponse);
|
|
1046
|
+
if (!ok || !data) {
|
|
1047
|
+
console.error({
|
|
1048
|
+
type: "verifyOtp Error",
|
|
1049
|
+
status,
|
|
1050
|
+
code,
|
|
1051
|
+
message,
|
|
1052
|
+
traceId: headers[TraceIdField]
|
|
1053
|
+
});
|
|
1054
|
+
throw new Error(`verifyOtp: ${message || code || status}`);
|
|
1055
|
+
}
|
|
1056
|
+
return data;
|
|
1057
|
+
}
|
|
1058
|
+
/**
|
|
1059
|
+
* 申请语音审核。
|
|
1060
|
+
*
|
|
1061
|
+
* 玩家认为语音房间内某些玩家存在语音违规行为时,可提交语音审核申请。调用前提是:游戏已经在世游开启语音审核服务。
|
|
1062
|
+
*/
|
|
1063
|
+
async voiceModerationRequest(option) {
|
|
1064
|
+
const { ok, status, code, message, headers } = await this.req.post("voice-moderation-request", option);
|
|
1065
|
+
if (!ok) console.error({
|
|
1066
|
+
type: "voiceModerationRequest Error",
|
|
1067
|
+
status,
|
|
1068
|
+
code,
|
|
1069
|
+
message,
|
|
1070
|
+
traceId: headers[TraceIdField]
|
|
1071
|
+
});
|
|
1072
|
+
return ok;
|
|
1073
|
+
}
|
|
1074
|
+
};
|
|
1214
1075
|
function isCreateOrderResponse(data) {
|
|
1215
|
-
|
|
1076
|
+
return isObject(data) && ["order_id", "order_token"].every((field) => field in data) && "expires_at" in data && typeof data.expires_at === "number";
|
|
1216
1077
|
}
|
|
1217
|
-
// 目前 OTP 仅支持 sms 通道,mobile 恒为掩码后的手机号字符串,故此处统一按 string 校验。
|
|
1218
|
-
// 后续新增 channel 时,需要在此根据 channel 补充 mobile 的关联校验
|
|
1219
|
-
// (例如非 sms 通道下 mobile 可能为空),届时可考虑将 channel 传入本守卫。
|
|
1220
1078
|
function isSendOtpResponse(data) {
|
|
1221
|
-
|
|
1079
|
+
return isObject(data) && "mobile" in data && "otp_ttl" in data && "otp_cooldown" in data && typeof data.mobile === "string" && typeof data.otp_ttl === "number" && typeof data.otp_cooldown === "number";
|
|
1222
1080
|
}
|
|
1223
1081
|
function isVerifyOtpResponse(data) {
|
|
1224
|
-
|
|
1082
|
+
return isObject(data) && "valid" in data && typeof data.valid === "boolean";
|
|
1225
1083
|
}
|
|
1226
|
-
|
|
1084
|
+
//#endregion
|
|
1085
|
+
//#region src/verify/id.ts
|
|
1227
1086
|
function isIdentityJwtPayload(payload) {
|
|
1228
|
-
|
|
1087
|
+
return isObject(payload) && "sub" in payload && "iss" in payload && "scope" in payload && "idp" in payload && "device_id" in payload && "external_id" in payload && "external_name" in payload && "reg_time" in payload;
|
|
1229
1088
|
}
|
|
1230
|
-
|
|
1089
|
+
//#endregion
|
|
1090
|
+
//#region src/verify/ads.ts
|
|
1231
1091
|
function isAdJwtPayload(payload) {
|
|
1232
|
-
|
|
1092
|
+
return isObject(payload) && "sub" in payload && "iss" in payload && "scope" in payload && "placement_id" in payload && "impression_id" in payload;
|
|
1233
1093
|
}
|
|
1234
|
-
|
|
1094
|
+
//#endregion
|
|
1095
|
+
//#region src/utils/gcm.ts
|
|
1235
1096
|
/**
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1097
|
+
* decryptAESGCM 使用 AES-256-GCM 解密 base64 编码的密文。
|
|
1098
|
+
* 密文格式为 base64(nonce || ciphertext || authTag),其中 nonce 为 12 字节,authTag 为 16 字节。
|
|
1099
|
+
*/
|
|
1239
1100
|
function decryptAESGCM(sk, encoded) {
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
if (finalBuffer.length > 0) {
|
|
1265
|
-
plaintext = Buffer.concat([plaintext, finalBuffer]);
|
|
1266
|
-
}
|
|
1267
|
-
} catch (err) {
|
|
1268
|
-
throw new Error(`gcm decrypt error: ${err}`);
|
|
1269
|
-
}
|
|
1270
|
-
return plaintext.toString("utf8");
|
|
1101
|
+
let data;
|
|
1102
|
+
try {
|
|
1103
|
+
data = Buffer.from(encoded, "base64");
|
|
1104
|
+
} catch (err) {
|
|
1105
|
+
throw new Error(`base64 decode error: ${err}`);
|
|
1106
|
+
}
|
|
1107
|
+
const key = deriveAESKey(sk);
|
|
1108
|
+
const nonceSize = 12;
|
|
1109
|
+
const authTagSize = 16;
|
|
1110
|
+
if (data.length < nonceSize + authTagSize) throw new Error("ciphertext too short");
|
|
1111
|
+
const nonce = data.slice(0, nonceSize);
|
|
1112
|
+
const authTag = data.slice(data.length - authTagSize);
|
|
1113
|
+
const ciphertext = data.slice(nonceSize, data.length - authTagSize);
|
|
1114
|
+
const decipher = createDecipheriv("aes-256-gcm", key, nonce);
|
|
1115
|
+
decipher.setAuthTag(authTag);
|
|
1116
|
+
let plaintext;
|
|
1117
|
+
try {
|
|
1118
|
+
plaintext = decipher.update(ciphertext);
|
|
1119
|
+
const finalBuffer = decipher.final();
|
|
1120
|
+
if (finalBuffer.length > 0) plaintext = Buffer.concat([plaintext, finalBuffer]);
|
|
1121
|
+
} catch (err) {
|
|
1122
|
+
throw new Error(`gcm decrypt error: ${err}`);
|
|
1123
|
+
}
|
|
1124
|
+
return plaintext.toString("utf8");
|
|
1271
1125
|
}
|
|
1272
1126
|
function deriveAESKey(sk) {
|
|
1273
|
-
|
|
1127
|
+
return createHash("sha256").update(sk).digest();
|
|
1274
1128
|
}
|
|
1275
|
-
|
|
1129
|
+
//#endregion
|
|
1130
|
+
//#region src/verify/index.ts
|
|
1276
1131
|
const IdentityTokenScope = "auth";
|
|
1277
1132
|
const AdTokenScope = "ads";
|
|
1278
1133
|
/**
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
return {
|
|
1347
|
-
combo_id: payload.sub,
|
|
1348
|
-
placement_id: payload.placement_id,
|
|
1349
|
-
impression_id: payload.impression_id
|
|
1350
|
-
};
|
|
1351
|
-
} catch (error) {
|
|
1352
|
-
return new Error(`AdToken 验证失败: ${error instanceof Error ? error.message : String(error)}`);
|
|
1353
|
-
}
|
|
1354
|
-
}
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1134
|
+
* token 验证器
|
|
1135
|
+
*/
|
|
1136
|
+
var TokenVerifier = class {
|
|
1137
|
+
constructor(config) {
|
|
1138
|
+
verifyConfig(config);
|
|
1139
|
+
this.privateKey = config.secret;
|
|
1140
|
+
this.baseOption = {
|
|
1141
|
+
algorithms: ["HS256"],
|
|
1142
|
+
audience: config.game,
|
|
1143
|
+
issuer: config.endpoint,
|
|
1144
|
+
ignoreExpiration: false
|
|
1145
|
+
};
|
|
1146
|
+
}
|
|
1147
|
+
/**
|
|
1148
|
+
* verifyIdentityToken 对 IdentityToken 进行验证
|
|
1149
|
+
*
|
|
1150
|
+
* 验证失败会返回 Error 对象
|
|
1151
|
+
*/
|
|
1152
|
+
verifyIdentityToken(token) {
|
|
1153
|
+
try {
|
|
1154
|
+
const payload = jwt.verify(token, this.privateKey, {
|
|
1155
|
+
...this.baseOption,
|
|
1156
|
+
complete: false
|
|
1157
|
+
});
|
|
1158
|
+
if (!payload || !isIdentityJwtPayload(payload) || !payload.sub) return /* @__PURE__ */ new Error("verifyIdentityToken: token 格式化失败 " + JSON.stringify(payload));
|
|
1159
|
+
if (payload.scope !== IdentityTokenScope) return /* @__PURE__ */ new Error("verifyIdentityToken: 无效的 Scope");
|
|
1160
|
+
return {
|
|
1161
|
+
combo_id: payload.sub,
|
|
1162
|
+
idp: payload.idp,
|
|
1163
|
+
external_id: payload.external_id,
|
|
1164
|
+
external_name: payload.external_name,
|
|
1165
|
+
weixin_session_key: payload.weixin_session_key ? decryptAESGCM(this.privateKey, payload.weixin_session_key) : "",
|
|
1166
|
+
distro: payload.distro || "",
|
|
1167
|
+
variant: payload.variant || "",
|
|
1168
|
+
device_id: payload.device_id || "",
|
|
1169
|
+
age: payload.age || 0,
|
|
1170
|
+
reg_time: payload.reg_time || 0
|
|
1171
|
+
};
|
|
1172
|
+
} catch (error) {
|
|
1173
|
+
return /* @__PURE__ */ new Error(`IdentityToken 验证失败: ${error instanceof Error ? error.message : String(error)}`);
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
/**
|
|
1177
|
+
* verifyAdToken 对 AdToken 进行验证
|
|
1178
|
+
*
|
|
1179
|
+
* 验证失败会返回 Error 对象
|
|
1180
|
+
*/
|
|
1181
|
+
verifyAdToken(token) {
|
|
1182
|
+
try {
|
|
1183
|
+
const payload = jwt.verify(token, this.privateKey, {
|
|
1184
|
+
...this.baseOption,
|
|
1185
|
+
complete: false
|
|
1186
|
+
});
|
|
1187
|
+
if (!payload || !isAdJwtPayload(payload) || !payload.sub) return /* @__PURE__ */ new Error("verifyAdToken: token 格式化失败 " + JSON.stringify(payload));
|
|
1188
|
+
if (payload.scope !== AdTokenScope) return /* @__PURE__ */ new Error("verifyAdToken: 无效的 Scope");
|
|
1189
|
+
return {
|
|
1190
|
+
combo_id: payload.sub,
|
|
1191
|
+
placement_id: payload.placement_id,
|
|
1192
|
+
impression_id: payload.impression_id
|
|
1193
|
+
};
|
|
1194
|
+
} catch (error) {
|
|
1195
|
+
return /* @__PURE__ */ new Error(`AdToken 验证失败: ${error instanceof Error ? error.message : String(error)}`);
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
};
|
|
1199
|
+
//#endregion
|
|
1200
|
+
//#region src/notify/msgShipOrder.ts
|
|
1357
1201
|
function isShipOrderPayload(data) {
|
|
1358
|
-
|
|
1202
|
+
return isObject(data) && "order_id" in data && "reference_id" in data && "combo_id" in data && "product_id" in data && "quantity" in data && "currency" in data && "amount" in data && typeof data.order_id === "string" && typeof data.combo_id === "string" && typeof data.quantity === "number" && typeof data.amount === "number" && ("is_sandbox" in data ? typeof data.is_sandbox === "boolean" : true) && Number.isSafeInteger(data.quantity) && Number.isSafeInteger(data.amount) && !!data.order_id && !!data.combo_id;
|
|
1359
1203
|
}
|
|
1360
|
-
|
|
1204
|
+
//#endregion
|
|
1205
|
+
//#region src/notify/msgRefund.ts
|
|
1361
1206
|
function isRefundPayload(data) {
|
|
1362
|
-
|
|
1207
|
+
return isObject(data) && "order_id" in data && "reference_id" in data && "combo_id" in data && "product_id" in data && "quantity" in data && "currency" in data && "amount" in data && typeof data.order_id === "string" && typeof data.combo_id === "string" && typeof data.quantity === "number" && typeof data.amount === "number" && Number.isSafeInteger(data.quantity) && Number.isSafeInteger(data.amount) && !!data.order_id && !!data.combo_id;
|
|
1363
1208
|
}
|
|
1364
|
-
|
|
1209
|
+
//#endregion
|
|
1210
|
+
//#region src/notify/types.ts
|
|
1211
|
+
/**
|
|
1212
|
+
* 通知类型枚举
|
|
1213
|
+
*/
|
|
1214
|
+
let NotificationType = /* @__PURE__ */ function(NotificationType) {
|
|
1215
|
+
/**
|
|
1216
|
+
* 世游服务端会在订单状态变更为已支付时,向游戏侧推送发货通知。
|
|
1217
|
+
*
|
|
1218
|
+
* 游戏侧需要在收到通知后,根据通知中的订单信息,发货给用户:
|
|
1219
|
+
*
|
|
1220
|
+
* - 如果游戏内发货成功,则应当返回 void 或 Promise<void>
|
|
1221
|
+
* - 如果游戏内发货出现错误,则应当 throw Error。世游服务端会在稍后重试推送发货通知。
|
|
1222
|
+
*/
|
|
1223
|
+
NotificationType["ShipOrder"] = "ship_order";
|
|
1224
|
+
/**
|
|
1225
|
+
* 世游服务端会在订单状态发生退款时,向游戏侧推送退款通知。
|
|
1226
|
+
*/
|
|
1227
|
+
NotificationType["Refund"] = "refund";
|
|
1228
|
+
return NotificationType;
|
|
1229
|
+
}({});
|
|
1365
1230
|
/**
|
|
1366
|
-
|
|
1367
|
-
|
|
1231
|
+
* 消息处理分发配置
|
|
1232
|
+
*/
|
|
1368
1233
|
const messageDataGuards = {
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1234
|
+
[NotificationType.ShipOrder]: {
|
|
1235
|
+
guard: isShipOrderPayload,
|
|
1236
|
+
message: "ShipOrder Data Format Error"
|
|
1237
|
+
},
|
|
1238
|
+
[NotificationType.Refund]: {
|
|
1239
|
+
guard: isRefundPayload,
|
|
1240
|
+
message: "Refund Data Format Error"
|
|
1241
|
+
}
|
|
1377
1242
|
};
|
|
1378
|
-
|
|
1243
|
+
//#endregion
|
|
1244
|
+
//#region src/notify/index.ts
|
|
1379
1245
|
function isNotificationRequestBody(data) {
|
|
1380
|
-
|
|
1246
|
+
return isObject(data) && "version" in data && "notification_id" in data && "notification_type" in data && "data" in data && typeof data.version === "string" && typeof data.notification_id === "string" && typeof data.notification_type === "string";
|
|
1381
1247
|
}
|
|
1382
1248
|
/**
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1249
|
+
* 获取一个处理函数用于接收 Seayoo Server 推送过来的消息通知
|
|
1250
|
+
*
|
|
1251
|
+
* 另有 express 和 koa2 版本的处理函数可以使用。
|
|
1252
|
+
*
|
|
1253
|
+
* 由于签名需要原始请求的内容,如果 request 在函数执行之前已经被读取,则需要手工传入请求的 rawBody 字符串以核验签名
|
|
1254
|
+
*/
|
|
1389
1255
|
function getNotificationHandler(config, handler) {
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
}
|
|
1444
|
-
};
|
|
1256
|
+
verifyConfig(config);
|
|
1257
|
+
/**
|
|
1258
|
+
* 第三个可选参数 rawBodyString 用于在特定场景 request 为 unreadable 时作为容错兜底方案处理
|
|
1259
|
+
*
|
|
1260
|
+
* 具体方案可以参考
|
|
1261
|
+
*
|
|
1262
|
+
* https://cloud.tencent.com/developer/ask/sof/110062928
|
|
1263
|
+
*/
|
|
1264
|
+
return async function(req, res, rawBodyString) {
|
|
1265
|
+
if (req.method !== "POST") {
|
|
1266
|
+
done(res, HttpStatus.MethodNotAllowed);
|
|
1267
|
+
return;
|
|
1268
|
+
}
|
|
1269
|
+
const contentType = `${req.headers["Content-Type"] || req.headers["content-type"]}`.toLowerCase();
|
|
1270
|
+
if (!contentType || !contentType.startsWith("application/json")) {
|
|
1271
|
+
done(res, HttpStatus.UnsupportedMediaType);
|
|
1272
|
+
return;
|
|
1273
|
+
}
|
|
1274
|
+
const authString = req.headers["Authorization"] || req.headers["Authorization".toLowerCase()];
|
|
1275
|
+
const body = await readBody(req, rawBodyString);
|
|
1276
|
+
if (!body || !checkHttpAuthInfo(`${authString}`, {
|
|
1277
|
+
game: config.game,
|
|
1278
|
+
secret: config.secret,
|
|
1279
|
+
method: "POST",
|
|
1280
|
+
endpoint: `http://${req.headers.host}`,
|
|
1281
|
+
url: req.url || "",
|
|
1282
|
+
data: body
|
|
1283
|
+
})) {
|
|
1284
|
+
done(res, HttpStatus.Unauthorized);
|
|
1285
|
+
return;
|
|
1286
|
+
}
|
|
1287
|
+
const notification = parseJSON(body, isNotificationRequestBody);
|
|
1288
|
+
if (!notification) {
|
|
1289
|
+
done(res, HttpStatus.BadRequest, `Notification body format error`);
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
const guardCfg = messageDataGuards[notification.notification_type];
|
|
1293
|
+
if (!guardCfg) {
|
|
1294
|
+
done(res, HttpStatus.BadRequest, `Unknown notification type: ${notification.notification_type}`);
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
if (!guardCfg.guard(notification.data)) {
|
|
1298
|
+
done(res, HttpStatus.BadRequest, guardCfg.message);
|
|
1299
|
+
return;
|
|
1300
|
+
}
|
|
1301
|
+
try {
|
|
1302
|
+
await handler(notification.notification_type, notification.data);
|
|
1303
|
+
done(res, HttpStatus.OK, "OK");
|
|
1304
|
+
} catch (e) {
|
|
1305
|
+
console.error(`notify handler error`, e);
|
|
1306
|
+
done(res, HttpStatus.InternalServerError, e instanceof Error ? e.message : String(e));
|
|
1307
|
+
}
|
|
1308
|
+
};
|
|
1445
1309
|
}
|
|
1446
1310
|
/**
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1311
|
+
* 获取基于 express 的处理函数用于接收 Seayoo Server 推送过来的消息通知
|
|
1312
|
+
*
|
|
1313
|
+
* 如需自定义处理,可以使用 getNotificationHandler 方法,
|
|
1314
|
+
*
|
|
1315
|
+
* 如果 express 使用全局的 bodyParse 或类似插件,将需要做额外的处理以获取原始请求的内容 rawBody 以用以计算签名
|
|
1316
|
+
*
|
|
1317
|
+
* 处理方法可以参考
|
|
1318
|
+
*
|
|
1319
|
+
* https://cloud.tencent.com/developer/ask/sof/110062928
|
|
1320
|
+
*/
|
|
1457
1321
|
function getNotificationHandlerForExpress(config, handler) {
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1322
|
+
const func = getNotificationHandler(config, handler);
|
|
1323
|
+
return async function(req, res) {
|
|
1324
|
+
await func(req, res, "rawBody" in req ? `${req.rawBody}` : "");
|
|
1325
|
+
};
|
|
1462
1326
|
}
|
|
1463
1327
|
/**
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1328
|
+
* 获取基于 express 的中间件用于接收 Seayoo Server 推送过来的消息通知
|
|
1329
|
+
*
|
|
1330
|
+
* 需要先于 body-parse 等类似插件加载
|
|
1331
|
+
*/
|
|
1468
1332
|
function getNotificationMiddlewareForExpress(paths, config, handler) {
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1333
|
+
const func = getNotificationHandler(config, handler);
|
|
1334
|
+
const matchPaths = Array.isArray(paths) ? paths : [paths];
|
|
1335
|
+
return async function(req, res, next) {
|
|
1336
|
+
if (req.method.toUpperCase() === "POST" && matchPaths.includes(req.path)) {
|
|
1337
|
+
await func(req, res);
|
|
1338
|
+
return;
|
|
1339
|
+
}
|
|
1340
|
+
next();
|
|
1341
|
+
};
|
|
1478
1342
|
}
|
|
1479
1343
|
/**
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1344
|
+
* 获取基于 koa2 的处理函数用于接收 Seayoo Server 推送过来的消息通知
|
|
1345
|
+
*
|
|
1346
|
+
* 如需自定义处理,可以使用 getNotificationHandler 方法
|
|
1347
|
+
*
|
|
1348
|
+
* 如果 koa 使用全局的 koa-body 或类似插件,将需要做额外的处理以获取原始请求的内容 rawBody 以用以计算签名
|
|
1349
|
+
*/
|
|
1486
1350
|
function getNotificationHandlerForKoa(config, handler) {
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1351
|
+
const func = getNotificationHandler(config, handler);
|
|
1352
|
+
return async function(ctx) {
|
|
1353
|
+
await func(ctx.req, ctx.res, "rawBody" in ctx ? `${ctx.rawBody}` : "");
|
|
1354
|
+
};
|
|
1491
1355
|
}
|
|
1492
1356
|
/**
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1357
|
+
* 获取基于 koa2 的中间件用于接收 Seayoo Server 推送过来的消息通知
|
|
1358
|
+
*
|
|
1359
|
+
* 需要先于 koa-body 等类似插件加载
|
|
1360
|
+
*/
|
|
1497
1361
|
function getNotificationMiddlewareForKoa(paths, config, handler) {
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
}
|
|
1508
|
-
|
|
1362
|
+
const func = getNotificationHandler(config, handler);
|
|
1363
|
+
const matchPaths = Array.isArray(paths) ? paths : [paths];
|
|
1364
|
+
return async function(ctx, next) {
|
|
1365
|
+
if (ctx.method.toUpperCase() === "POST" && matchPaths.includes(ctx.path)) {
|
|
1366
|
+
await func(ctx.req, ctx.res);
|
|
1367
|
+
return;
|
|
1368
|
+
}
|
|
1369
|
+
await next();
|
|
1370
|
+
};
|
|
1371
|
+
}
|
|
1372
|
+
//#endregion
|
|
1373
|
+
//#region src/gm/types.ts
|
|
1509
1374
|
function isGMRequestBody(data) {
|
|
1510
|
-
|
|
1375
|
+
return isObject(data) && "version" in data && "request_id" in data && "command" in data && "args" in data && ("idempotency_key" in data ? typeof data.idempotency_key === "string" : true) && ("origin" in data ? typeof data.origin === "string" : true) && typeof data.request_id === "string" && typeof data.command === "string" && typeof data.args === "object" && data.args !== null;
|
|
1511
1376
|
}
|
|
1512
1377
|
function isGMErrorResponse(data) {
|
|
1513
|
-
|
|
1378
|
+
return isObject(data) && Object.keys(data).length === 2 && "error" in data && "message" in data;
|
|
1514
1379
|
}
|
|
1515
1380
|
function isIdempotencyRecord(data) {
|
|
1516
|
-
|
|
1381
|
+
return isObject(data) && "key" in data && "id" in data && "cmd" in data && "args" in data;
|
|
1517
1382
|
}
|
|
1518
|
-
|
|
1383
|
+
//#endregion
|
|
1384
|
+
//#region src/gm/utils.ts
|
|
1519
1385
|
function verifyStoreHelper(storeHelper) {
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
throw new Error("store.setNX should be (key, value) => string");
|
|
1526
|
-
}
|
|
1527
|
-
if (typeof storeHelper.setXX !== "function" || storeHelper.setXX.length !== 2) {
|
|
1528
|
-
throw new Error("store.setXX should be (key, value) => void");
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1386
|
+
if (storeHelper) {
|
|
1387
|
+
if (typeof storeHelper !== "object" || !("setXX" in storeHelper) || !("setNX" in storeHelper)) throw new Error("store helper should be { setXX, setNX }");
|
|
1388
|
+
if (typeof storeHelper.setNX !== "function" || storeHelper.setNX.length !== 2) throw new Error("store.setNX should be (key, value) => string");
|
|
1389
|
+
if (typeof storeHelper.setXX !== "function" || storeHelper.setXX.length !== 2) throw new Error("store.setXX should be (key, value) => void");
|
|
1390
|
+
}
|
|
1531
1391
|
}
|
|
1532
1392
|
function toString(data) {
|
|
1533
|
-
|
|
1393
|
+
return JSON.stringify(data);
|
|
1534
1394
|
}
|
|
1535
1395
|
/** 提供基于内存的 store helper,仅供本地调试使用,不支持 ttl 超时功能 */
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
this.mc[key] = value;
|
|
1553
|
-
}
|
|
1554
|
-
}
|
|
1555
|
-
}
|
|
1396
|
+
var MemoryIdempotencyStore = class {
|
|
1397
|
+
constructor() {
|
|
1398
|
+
this.mc = {};
|
|
1399
|
+
}
|
|
1400
|
+
async setNX(key, value) {
|
|
1401
|
+
const r = this.mc[key];
|
|
1402
|
+
if (!r) {
|
|
1403
|
+
this.mc[key] = value;
|
|
1404
|
+
return "";
|
|
1405
|
+
}
|
|
1406
|
+
return r;
|
|
1407
|
+
}
|
|
1408
|
+
async setXX(key, value) {
|
|
1409
|
+
if (this.mc[key]) this.mc[key] = value;
|
|
1410
|
+
}
|
|
1411
|
+
};
|
|
1556
1412
|
/**
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
}
|
|
1578
|
-
|
|
1413
|
+
* 基于 ioredis 的 store helper,
|
|
1414
|
+
*
|
|
1415
|
+
* 需要 Redis >= 7.0,见 https://redis.io/docs/latest/commands/set/
|
|
1416
|
+
*/
|
|
1417
|
+
var RedisIdempotencyStore = class {
|
|
1418
|
+
constructor(options) {
|
|
1419
|
+
var _options$ttl;
|
|
1420
|
+
this.client = options.client;
|
|
1421
|
+
this.ttl = Math.max(24 * 3600, (_options$ttl = options.ttl) !== null && _options$ttl !== void 0 ? _options$ttl : 0);
|
|
1422
|
+
this.prefix = options.prefix || "";
|
|
1423
|
+
}
|
|
1424
|
+
async setNX(key, value) {
|
|
1425
|
+
return await this.client.set(`${this.prefix}${key}`, value, "EX", this.ttl, "NX", "GET") || "";
|
|
1426
|
+
}
|
|
1427
|
+
async setXX(key, value) {
|
|
1428
|
+
await this.client.set(`${this.prefix}${key}`, value, "EX", this.ttl, "XX");
|
|
1429
|
+
}
|
|
1430
|
+
};
|
|
1431
|
+
//#endregion
|
|
1432
|
+
//#region src/gm/index.ts
|
|
1579
1433
|
const error2httpStatus = {
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1434
|
+
[GMError.InvalidHttpMethod]: HttpStatus.MethodNotAllowed,
|
|
1435
|
+
[GMError.InvalidContentType]: HttpStatus.UnsupportedMediaType,
|
|
1436
|
+
[GMError.InvalidSignature]: HttpStatus.Unauthorized,
|
|
1437
|
+
[GMError.InvalidRequest]: HttpStatus.BadRequest,
|
|
1438
|
+
[GMError.InvalidCommand]: HttpStatus.BadRequest,
|
|
1439
|
+
[GMError.InvalidArgs]: HttpStatus.BadRequest,
|
|
1440
|
+
[GMError.NetworkError]: HttpStatus.InternalServerError,
|
|
1441
|
+
[GMError.DatabaseError]: HttpStatus.InternalServerError,
|
|
1442
|
+
[GMError.IdempotencyConflict]: HttpStatus.Conflict,
|
|
1443
|
+
[GMError.IdempotencyMismatch]: HttpStatus.UnprocessableEntity,
|
|
1444
|
+
[GMError.TimeoutError]: HttpStatus.InternalServerError,
|
|
1445
|
+
[GMError.MaintenanceError]: HttpStatus.ServiceUnavailable,
|
|
1446
|
+
[GMError.ThrottlingError]: HttpStatus.TooManyRequests,
|
|
1447
|
+
[GMError.InternalError]: HttpStatus.InternalServerError
|
|
1594
1448
|
};
|
|
1595
1449
|
/**
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1450
|
+
* 获取一个处理函数用于响应 Seayoo Server 推送过来的 GM 执行通知,storeHelper 提供存储支持以实现幂等key(idempotencyKey)的自动化处理
|
|
1451
|
+
*
|
|
1452
|
+
* 另有 express 和 koa2 版本的处理函数可以使用。
|
|
1453
|
+
*
|
|
1454
|
+
* 由于签名需要原始请求的内容,如果 request 在函数执行之前已经被读取,则需要手工传入请求的 rawBody 字符串以核验签名
|
|
1455
|
+
*/
|
|
1602
1456
|
function getGMCommandHandler(config, handler, storeHelper) {
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
}
|
|
1699
|
-
// 对游戏处理函数进一步兼容处理,允许返回一个预定义的错误码
|
|
1700
|
-
// 但此逻辑不作为公开 api,仅作为内容兼容处理
|
|
1701
|
-
if (typeof result === "string" && result in error2httpStatus) {
|
|
1702
|
-
responseError(res, error2httpStatus[result], result, result);
|
|
1703
|
-
return;
|
|
1704
|
-
}
|
|
1705
|
-
// 如果 result 没有结果则进行提示然后继续
|
|
1706
|
-
if (!result) {
|
|
1707
|
-
console.warn(`gm command "${gmRequest.command}" should give back some data`);
|
|
1708
|
-
}
|
|
1709
|
-
responseJson(res, result != null ? result : {});
|
|
1710
|
-
} catch (e) {
|
|
1711
|
-
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1712
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */, GMError.InternalError, e instanceof Error ? e.message : String(e));
|
|
1713
|
-
}
|
|
1714
|
-
};
|
|
1457
|
+
verifyConfigWithoutEndpoint(config);
|
|
1458
|
+
verifyStoreHelper(storeHelper);
|
|
1459
|
+
return async function(req, res, rawBodyString) {
|
|
1460
|
+
if (req.method !== "POST") {
|
|
1461
|
+
responseError(res, HttpStatus.MethodNotAllowed, GMError.InvalidHttpMethod, `Expecting POST, got ${req.method}`);
|
|
1462
|
+
return;
|
|
1463
|
+
}
|
|
1464
|
+
const contentType = `${req.headers["Content-Type"] || req.headers["content-type"]}`.toLowerCase();
|
|
1465
|
+
if (!contentType || !contentType.startsWith("application/json")) {
|
|
1466
|
+
responseError(res, HttpStatus.UnsupportedMediaType, GMError.InvalidContentType, `Expecting application/json, got ${contentType}`);
|
|
1467
|
+
return;
|
|
1468
|
+
}
|
|
1469
|
+
const authString = req.headers["Authorization"] || req.headers["Authorization".toLowerCase()];
|
|
1470
|
+
const body = await readBody(req, rawBodyString);
|
|
1471
|
+
if (!body || !checkHttpAuthInfo(`${authString}`, {
|
|
1472
|
+
game: config.game,
|
|
1473
|
+
secret: config.secret,
|
|
1474
|
+
method: "POST",
|
|
1475
|
+
endpoint: `http://${req.headers.host}`,
|
|
1476
|
+
url: req.url || "",
|
|
1477
|
+
data: body
|
|
1478
|
+
})) {
|
|
1479
|
+
responseError(res, HttpStatus.Unauthorized, GMError.InvalidSignature, "Signature failed");
|
|
1480
|
+
return;
|
|
1481
|
+
}
|
|
1482
|
+
const gmRequest = parseJSON(body, isGMRequestBody);
|
|
1483
|
+
if (!gmRequest) {
|
|
1484
|
+
responseError(res, HttpStatus.BadRequest, GMError.InvalidRequest, `GM request body format error`);
|
|
1485
|
+
return;
|
|
1486
|
+
}
|
|
1487
|
+
const currentRecord = {
|
|
1488
|
+
nonce: genNonceString(),
|
|
1489
|
+
key: gmRequest.idempotency_key || "",
|
|
1490
|
+
id: gmRequest.request_id,
|
|
1491
|
+
cmd: gmRequest.command,
|
|
1492
|
+
args: toString(gmRequest.args)
|
|
1493
|
+
};
|
|
1494
|
+
let oldResponse = null;
|
|
1495
|
+
if (storeHelper && gmRequest.idempotency_key) {
|
|
1496
|
+
const oldRecordStr = await storeHelper.setNX(gmRequest.idempotency_key, toString(currentRecord)).catch((e) => e instanceof Error ? e : new Error(String(e)));
|
|
1497
|
+
if (oldRecordStr instanceof Error) {
|
|
1498
|
+
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) setNX error`, oldRecordStr);
|
|
1499
|
+
responseError(res, HttpStatus.InternalServerError, GMError.InternalError, oldRecordStr.message);
|
|
1500
|
+
return;
|
|
1501
|
+
}
|
|
1502
|
+
const oldRecord = oldRecordStr ? parseJSON(oldRecordStr, isIdempotencyRecord) : null;
|
|
1503
|
+
if (!oldRecord && oldRecordStr) {
|
|
1504
|
+
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) parse old idempotency record failed: ${oldRecordStr}`);
|
|
1505
|
+
responseError(res, HttpStatus.InternalServerError, GMError.InternalError, "parse old idempotency record failed");
|
|
1506
|
+
return;
|
|
1507
|
+
}
|
|
1508
|
+
if (oldRecord && "noce" in oldRecord) oldRecord.nonce = oldRecord.noce + "";
|
|
1509
|
+
if (!(oldRecordStr === "" || (oldRecord === null || oldRecord === void 0 ? void 0 : oldRecord.nonce) === currentRecord.nonce) && oldRecord) {
|
|
1510
|
+
if (!oldRecord.resp) {
|
|
1511
|
+
responseError(res, error2httpStatus[GMError.IdempotencyConflict], GMError.IdempotencyConflict, "previous request is not completed");
|
|
1512
|
+
return;
|
|
1513
|
+
}
|
|
1514
|
+
if (oldRecord.cmd !== gmRequest.command) {
|
|
1515
|
+
responseError(res, error2httpStatus[GMError.IdempotencyMismatch], GMError.IdempotencyMismatch, `cmd mismatch, expecting ${oldRecord.cmd}, got ${gmRequest.command}`);
|
|
1516
|
+
return;
|
|
1517
|
+
}
|
|
1518
|
+
if (oldRecord.args !== currentRecord.args) {
|
|
1519
|
+
responseError(res, error2httpStatus[GMError.IdempotencyMismatch], GMError.IdempotencyMismatch, `args mismatch, expecting ${oldRecord.args}, got ${currentRecord.args}`);
|
|
1520
|
+
return;
|
|
1521
|
+
}
|
|
1522
|
+
oldResponse = oldRecord.resp;
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
try {
|
|
1526
|
+
const result = oldResponse || await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.idempotency_key || "", gmRequest.version, gmRequest.origin || "");
|
|
1527
|
+
if (storeHelper && gmRequest.idempotency_key) {
|
|
1528
|
+
currentRecord.resp = result;
|
|
1529
|
+
storeHelper.setXX(gmRequest.idempotency_key, toString(currentRecord)).catch((err) => {
|
|
1530
|
+
console.error("failed to setXX idempotency record", err, currentRecord);
|
|
1531
|
+
});
|
|
1532
|
+
}
|
|
1533
|
+
if (result instanceof Error) {
|
|
1534
|
+
responseError(res, HttpStatus.InternalServerError, GMError.InternalError, result.message);
|
|
1535
|
+
return;
|
|
1536
|
+
}
|
|
1537
|
+
if (isGMErrorResponse(result)) {
|
|
1538
|
+
responseError(res, error2httpStatus[result.error] || HttpStatus.InternalServerError, result.error || GMError.InternalError, result.message);
|
|
1539
|
+
return;
|
|
1540
|
+
}
|
|
1541
|
+
if (typeof result === "string" && result in error2httpStatus) {
|
|
1542
|
+
responseError(res, error2httpStatus[result], result, result);
|
|
1543
|
+
return;
|
|
1544
|
+
}
|
|
1545
|
+
if (!result) console.warn(`gm command "${gmRequest.command}" should give back some data`);
|
|
1546
|
+
responseJson(res, result !== null && result !== void 0 ? result : {});
|
|
1547
|
+
} catch (e) {
|
|
1548
|
+
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1549
|
+
responseError(res, HttpStatus.InternalServerError, GMError.InternalError, e instanceof Error ? e.message : String(e));
|
|
1550
|
+
}
|
|
1551
|
+
};
|
|
1715
1552
|
}
|
|
1716
1553
|
/**
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1554
|
+
* 获取基于 express 的处理函数用于响应 Seayoo Server 推送过来的 GM 执行通知
|
|
1555
|
+
*
|
|
1556
|
+
* 如需自定义处理,可以使用 getGMCommandHandler 方法,
|
|
1557
|
+
*
|
|
1558
|
+
* 如果 express 使用全局的 bodyParse 或类似插件,将需要做额外的处理以获取原始请求的内容 rawBody 以用以计算签名
|
|
1559
|
+
*
|
|
1560
|
+
* 处理方法可以参考
|
|
1561
|
+
*
|
|
1562
|
+
* https://cloud.tencent.com/developer/ask/sof/110062928
|
|
1563
|
+
*/
|
|
1727
1564
|
function getGMHandlerForExpress(config, handler, storeHelper) {
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1565
|
+
const func = getGMCommandHandler(config, handler, storeHelper);
|
|
1566
|
+
return async function(req, res) {
|
|
1567
|
+
await func(req, res, "rawBody" in req ? `${req.rawBody}` : "");
|
|
1568
|
+
};
|
|
1732
1569
|
}
|
|
1733
1570
|
/**
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1571
|
+
* 获取基于 express 的中间件用于响应 Seayoo Server 推送过来的 GM 执行通知
|
|
1572
|
+
*
|
|
1573
|
+
* 需要先于 body-parse 等类似插件加载
|
|
1574
|
+
*/
|
|
1738
1575
|
function getGMMiddlewareForExpress(paths, config, handler, storeHelper) {
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1576
|
+
const func = getGMCommandHandler(config, handler, storeHelper);
|
|
1577
|
+
const matchPaths = Array.isArray(paths) ? paths : [paths];
|
|
1578
|
+
return async function(req, res, next) {
|
|
1579
|
+
if (req.method.toUpperCase() === "POST" && matchPaths.includes(req.path)) {
|
|
1580
|
+
await func(req, res);
|
|
1581
|
+
return;
|
|
1582
|
+
}
|
|
1583
|
+
next();
|
|
1584
|
+
};
|
|
1748
1585
|
}
|
|
1749
1586
|
/**
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1587
|
+
* 获取基于 koa2 的处理函数用于响应 Seayoo Server 推送过来的 GM 执行通知
|
|
1588
|
+
*
|
|
1589
|
+
* 如需自定义处理,可以使用 getGMCommandHandler 方法
|
|
1590
|
+
*
|
|
1591
|
+
* 如果 koa 使用全局的 koa-body 或类似插件,将需要做额外的处理以获取原始请求的内容 rawBody 以用以计算签名
|
|
1592
|
+
*/
|
|
1756
1593
|
function getGMHandlerForKoa(config, handler, storeHelper) {
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1594
|
+
const func = getGMCommandHandler(config, handler, storeHelper);
|
|
1595
|
+
return async function(ctx) {
|
|
1596
|
+
await func(ctx.req, ctx.res, "rawBody" in ctx ? `${ctx.rawBody}` : "");
|
|
1597
|
+
};
|
|
1761
1598
|
}
|
|
1762
1599
|
/**
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1600
|
+
* 获取基于 koa2 的中间件用于响应 Seayoo Server 推送过来的 GM 执行通知
|
|
1601
|
+
*
|
|
1602
|
+
* 需要先于 koa-body 等类似插件加载
|
|
1603
|
+
*/
|
|
1767
1604
|
function getGMMiddlewareForKoa(paths, config, handler, storeHelper) {
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
}
|
|
1778
|
-
|
|
1779
|
-
export { ApiClient, Endpoint, GMError, IdP, MemoryIdempotencyStore, Platform, RedisIdempotencyStore, TokenVerifier, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getGMCommandHandler, getGMHandlerForExpress, getGMHandlerForKoa, getGMMiddlewareForExpress, getGMMiddlewareForKoa, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
|
|
1605
|
+
const func = getGMCommandHandler(config, handler, storeHelper);
|
|
1606
|
+
const matchPaths = Array.isArray(paths) ? paths : [paths];
|
|
1607
|
+
return async function(ctx, next) {
|
|
1608
|
+
if (ctx.method.toUpperCase() === "POST" && matchPaths.includes(ctx.path)) {
|
|
1609
|
+
await func(ctx.req, ctx.res);
|
|
1610
|
+
return;
|
|
1611
|
+
}
|
|
1612
|
+
await next();
|
|
1613
|
+
};
|
|
1614
|
+
}
|
|
1615
|
+
//#endregion
|
|
1616
|
+
export { ApiClient, Endpoint, GMError, IdP, MemoryIdempotencyStore, NotificationType, Platform, RedisIdempotencyStore, TokenVerifier, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getGMCommandHandler, getGMHandlerForExpress, getGMHandlerForKoa, getGMMiddlewareForExpress, getGMMiddlewareForKoa, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
|