@seayoo-io/combo-sdk-node 1.2.0 → 1.4.0
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 +16 -5
- package/dist/const.d.ts +34 -4
- package/dist/gm/types.d.ts +4 -2
- package/dist/gm/utils.d.ts +5 -1
- package/dist/index.cjs +57 -16
- package/dist/index.js +57 -16
- package/dist/notify/msgShipOrder.d.ts +6 -0
- package/dist/utils/base.d.ts +1 -1
- package/dist/utils/http.d.ts +1 -1
- package/dist/verify/id.d.ts +5 -1
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
- 验证世游服务端签发的 Identity Token 和 Ads Token
|
|
10
10
|
- 接收 Server GM Command 指令并回复响应
|
|
11
11
|
|
|
12
|
-
## breaking
|
|
12
|
+
## breaking changes
|
|
13
13
|
|
|
14
14
|
v1 新增 gm 模块的 [IdempotencyKey](https://docs.seayoo.com/combo/server/gm/#idempotency),主要改动包括:
|
|
15
15
|
|
|
@@ -184,6 +184,11 @@ interface ShipOrderNotification {
|
|
|
184
184
|
amount: number
|
|
185
185
|
/** 游戏侧创建订单时提供的订单上下文,透传回游戏 */
|
|
186
186
|
context?: string
|
|
187
|
+
/**
|
|
188
|
+
* 是否是沙盒订单。沙盒订单意味着此订单并未产生真实的付款。
|
|
189
|
+
* 预期此字段仅用于记录日志和数据埋点。无论是否是沙盒订单,游戏侧都应当发货。
|
|
190
|
+
*/
|
|
191
|
+
is_sandbox: boolean
|
|
187
192
|
}
|
|
188
193
|
|
|
189
194
|
interface RefundNotification {
|
|
@@ -299,7 +304,7 @@ if(identityPayload instanceof Error) {
|
|
|
299
304
|
// do something
|
|
300
305
|
}
|
|
301
306
|
// 游客登录判断
|
|
302
|
-
if(identityPayload.idp === IdP.
|
|
307
|
+
if(identityPayload.idp === IdP.Guest) {
|
|
303
308
|
// do something
|
|
304
309
|
}
|
|
305
310
|
// 更多 IdP 枚举可以查看源码定义 src/const.ts
|
|
@@ -331,6 +336,10 @@ interface IdentityPayload {
|
|
|
331
336
|
* 注意:weixin_unionid 只在 IdP 为 weixin 时才会有值。
|
|
332
337
|
*/
|
|
333
338
|
weixin_unionid: string
|
|
339
|
+
/**
|
|
340
|
+
* device_id 是用户在登录时使用的设备的唯一 ID。
|
|
341
|
+
*/
|
|
342
|
+
device_id: string
|
|
334
343
|
/**
|
|
335
344
|
* distro 是游戏客户端的发行版本标识。
|
|
336
345
|
* 游戏侧可将 distro 用于服务端数据埋点,以及特定的业务逻辑判断。
|
|
@@ -378,12 +387,14 @@ GM 模块使用方式类似于 Notify 模块。
|
|
|
378
387
|
import { GMError } from "@seayoo-io/combo-sdk-node"
|
|
379
388
|
|
|
380
389
|
// 1.1 定义 GM 处理函数
|
|
381
|
-
function gmCommandHandler(command, args, requestId, idempotencyKey, version) {
|
|
390
|
+
function gmCommandHandler(command, args, requestId, idempotencyKey, version, origin) {
|
|
391
|
+
// command 对应的是 GM 协议中定义的方法名,区分大小写,args 为该方法的参数。
|
|
382
392
|
// requestId 本次 GM 请求的唯一 ID。游戏侧可用此值来对请求进行去重。
|
|
383
393
|
// idempotencyKey 本次 GM 请求的 Idempotency Key。如果有非空值则应当执行幂等处理逻辑。
|
|
384
394
|
// SDK 已经内置了 idempotencyKey 的处理工具和配置,详细见下描述
|
|
385
395
|
// version 对应的是世游 GM 服务的版本号,目前固定是 2.0
|
|
386
|
-
//
|
|
396
|
+
// origin 对应的是 GM 请求的来源标记,区分大小写,如 combo/console
|
|
397
|
+
// 详见 https://docs.seayoo.com/combo/server/gm/#gm-request
|
|
387
398
|
switch(command) {
|
|
388
399
|
case "SomeCmdName":
|
|
389
400
|
// 参数 args 由协议定义,在使用前需要做严格的格式和逻辑校验
|
|
@@ -470,7 +481,7 @@ import { MemoryIdempotencyStore, RedisIdempotencyStore } from "@seayoo-io/combo-
|
|
|
470
481
|
|
|
471
482
|
// MemoryIdempotencyStore 仅仅用于本地调试
|
|
472
483
|
const storeHelper = new MemoryIdempotencyStore()
|
|
473
|
-
// RedisIdempotencyStore 基于 ioredis
|
|
484
|
+
// RedisIdempotencyStore 基于 ioredis 实现,需要 Redis 版本大于 7: https://redis.io/docs/latest/commands/set/
|
|
474
485
|
const storeHelper = new RedisIdempotencyStore({
|
|
475
486
|
/** 超时设定,单位秒,推荐不低于24小时 */
|
|
476
487
|
ttl?: number
|
package/dist/const.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** SDK 名称 */
|
|
2
2
|
export declare const SDKName = "combo-sdk-node";
|
|
3
3
|
/** SDK 版本,在编译前会被动态替换 */
|
|
4
|
-
export declare const SDKVersion = "1.
|
|
4
|
+
export declare const SDKVersion = "1.4.0";
|
|
5
5
|
/**
|
|
6
6
|
* 世游支持的服务端点列表
|
|
7
7
|
*/
|
|
@@ -26,8 +26,8 @@ export declare enum Platform {
|
|
|
26
26
|
}
|
|
27
27
|
/** 支持的登录提供方 */
|
|
28
28
|
export declare enum IdP {
|
|
29
|
-
/**
|
|
30
|
-
|
|
29
|
+
/** 游客登录 */
|
|
30
|
+
Guest = "guest",
|
|
31
31
|
/** 世游通行证 */
|
|
32
32
|
Seayoo = "seayoo",
|
|
33
33
|
/** Sign-in with Apple */
|
|
@@ -39,7 +39,37 @@ export declare enum IdP {
|
|
|
39
39
|
/** 小米账号 */
|
|
40
40
|
Xiaomi = "xiaomi",
|
|
41
41
|
/** 微信登录 */
|
|
42
|
-
Weixin = "weixin"
|
|
42
|
+
Weixin = "weixin",
|
|
43
|
+
/** OPPO 账号 */
|
|
44
|
+
Oppo = "oppo",
|
|
45
|
+
/** VIVO 账号 */
|
|
46
|
+
Vivo = "vivo",
|
|
47
|
+
/** 华为账号 */
|
|
48
|
+
Huawei = "huawei",
|
|
49
|
+
/** 荣耀账号 */
|
|
50
|
+
Honor = "honor",
|
|
51
|
+
/** UC(九游)登录 */
|
|
52
|
+
UC = "uc",
|
|
53
|
+
/** TapTap 登录 */
|
|
54
|
+
TapTap = "taptap",
|
|
55
|
+
/** 哔哩哔哩(B站)账号 */
|
|
56
|
+
Bilibili = "bilibili",
|
|
57
|
+
/** 应用宝 YSDK 登录 */
|
|
58
|
+
Yingyongbao = "yingyongbao",
|
|
59
|
+
/** 4399 账号登录 */
|
|
60
|
+
F4399 = "4399",
|
|
61
|
+
/** 雷电模拟器账号 */
|
|
62
|
+
Leidian = "leidian",
|
|
63
|
+
/** 猫窝游戏 */
|
|
64
|
+
Maowo = "maowo",
|
|
65
|
+
/** 联想 */
|
|
66
|
+
Lenovo = "lenovo",
|
|
67
|
+
/** 魅族 */
|
|
68
|
+
Meizu = "meizu",
|
|
69
|
+
/** 酷派 */
|
|
70
|
+
Coolpad = "coolpad",
|
|
71
|
+
/** 努比亚 */
|
|
72
|
+
Nubia = "nubia"
|
|
43
73
|
}
|
|
44
74
|
/** GM 错误类型枚举 */
|
|
45
75
|
export declare const enum GMError {
|
package/dist/gm/types.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ export interface GMRequestBody {
|
|
|
7
7
|
request_id: string;
|
|
8
8
|
/** 本次 GM 请求的 Idempotency Key。如果有非空值则应当执行幂等处理逻辑。 */
|
|
9
9
|
idempotency_key?: string;
|
|
10
|
+
/** 本次 GM 请求的来源,大小写敏感,比如 combo/console。 */
|
|
11
|
+
origin?: string;
|
|
10
12
|
/** GM 命令标识。取值和 GM 协议文件中的 rpc 名称对应,大小写敏感 */
|
|
11
13
|
command: string;
|
|
12
14
|
/** args 是和 cmd 对应的命令参数,具体值取决于协议中对应 rpc 的 Request 定义 */
|
|
@@ -26,7 +28,7 @@ export declare function isGMErrorResponse(data: unknown): data is GMErrorRespons
|
|
|
26
28
|
* 如果遇到错误可以返回错误结构 ({ error, message }) 或 Error 对象
|
|
27
29
|
*/
|
|
28
30
|
export type GMCommandHandler = {
|
|
29
|
-
(command: string, args: Record<string, unknown>, requestId: string, idempotencyKey: string, version: string): MaybePromise<Record<string, unknown> | GMErrorResponse | Error>;
|
|
31
|
+
(command: string, args: Record<string, unknown>, requestId: string, idempotencyKey: string, version: string, origin: string): MaybePromise<Record<string, unknown> | GMErrorResponse | Error>;
|
|
30
32
|
};
|
|
31
33
|
/**
|
|
32
34
|
* 在处理幂等性时,需要使用储存支持,比如 Reis
|
|
@@ -46,7 +48,7 @@ export interface IdempotencyKeyStoreHelper {
|
|
|
46
48
|
}
|
|
47
49
|
/** 幂等key 记录 */
|
|
48
50
|
export interface IdempotencyRecord {
|
|
49
|
-
|
|
51
|
+
nonce: string;
|
|
50
52
|
key: string;
|
|
51
53
|
id: string;
|
|
52
54
|
cmd: string;
|
package/dist/gm/utils.d.ts
CHANGED
|
@@ -9,7 +9,11 @@ export declare class MemoryIdempotencyStore implements IdempotencyKeyStoreHelper
|
|
|
9
9
|
setNX(key: string, value: string): Promise<string>;
|
|
10
10
|
setXX(key: string, value: string): Promise<void>;
|
|
11
11
|
}
|
|
12
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* 基于 ioredis 的 store helper,
|
|
14
|
+
*
|
|
15
|
+
* 需要 Redis >= 7.0,见 https://redis.io/docs/latest/commands/set/
|
|
16
|
+
*/
|
|
13
17
|
export declare class RedisIdempotencyStore implements IdempotencyKeyStoreHelper {
|
|
14
18
|
client: Redis;
|
|
15
19
|
ttl: number;
|
package/dist/index.cjs
CHANGED
|
@@ -12,7 +12,7 @@ var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
|
|
|
12
12
|
/** SDK 名称 */
|
|
13
13
|
const SDKName = "combo-sdk-node";
|
|
14
14
|
/** SDK 版本,在编译前会被动态替换 */
|
|
15
|
-
const SDKVersion = "1.
|
|
15
|
+
const SDKVersion = "1.4.0";
|
|
16
16
|
/**
|
|
17
17
|
* 世游支持的服务端点列表
|
|
18
18
|
*/
|
|
@@ -40,8 +40,8 @@ exports.Platform = void 0;
|
|
|
40
40
|
/** 支持的登录提供方 */
|
|
41
41
|
exports.IdP = void 0;
|
|
42
42
|
(function (IdP) {
|
|
43
|
-
/**
|
|
44
|
-
IdP["
|
|
43
|
+
/** 游客登录 */
|
|
44
|
+
IdP["Guest"] = "guest";
|
|
45
45
|
/** 世游通行证 */
|
|
46
46
|
IdP["Seayoo"] = "seayoo";
|
|
47
47
|
/** Sign-in with Apple */
|
|
@@ -54,6 +54,36 @@ exports.IdP = void 0;
|
|
|
54
54
|
IdP["Xiaomi"] = "xiaomi";
|
|
55
55
|
/** 微信登录 */
|
|
56
56
|
IdP["Weixin"] = "weixin";
|
|
57
|
+
/** OPPO 账号 */
|
|
58
|
+
IdP["Oppo"] = "oppo";
|
|
59
|
+
/** VIVO 账号 */
|
|
60
|
+
IdP["Vivo"] = "vivo";
|
|
61
|
+
/** 华为账号 */
|
|
62
|
+
IdP["Huawei"] = "huawei";
|
|
63
|
+
/** 荣耀账号 */
|
|
64
|
+
IdP["Honor"] = "honor";
|
|
65
|
+
/** UC(九游)登录 */
|
|
66
|
+
IdP["UC"] = "uc";
|
|
67
|
+
/** TapTap 登录 */
|
|
68
|
+
IdP["TapTap"] = "taptap";
|
|
69
|
+
/** 哔哩哔哩(B站)账号 */
|
|
70
|
+
IdP["Bilibili"] = "bilibili";
|
|
71
|
+
/** 应用宝 YSDK 登录 */
|
|
72
|
+
IdP["Yingyongbao"] = "yingyongbao";
|
|
73
|
+
/** 4399 账号登录 */
|
|
74
|
+
IdP["F4399"] = "4399";
|
|
75
|
+
/** 雷电模拟器账号 */
|
|
76
|
+
IdP["Leidian"] = "leidian";
|
|
77
|
+
/** 猫窝游戏 */
|
|
78
|
+
IdP["Maowo"] = "maowo";
|
|
79
|
+
/** 联想 */
|
|
80
|
+
IdP["Lenovo"] = "lenovo";
|
|
81
|
+
/** 魅族 */
|
|
82
|
+
IdP["Meizu"] = "meizu";
|
|
83
|
+
/** 酷派 */
|
|
84
|
+
IdP["Coolpad"] = "coolpad";
|
|
85
|
+
/** 努比亚 */
|
|
86
|
+
IdP["Nubia"] = "nubia";
|
|
57
87
|
})(exports.IdP || (exports.IdP = {}));
|
|
58
88
|
|
|
59
89
|
function getUserAgent(game) {
|
|
@@ -96,8 +126,8 @@ function isStringRecord(data) {
|
|
|
96
126
|
/**
|
|
97
127
|
* 检查一个url地址是否以 http/https 开头,如果第二个参数传递 true,则自适应协议(//....)也被认为是完整 url
|
|
98
128
|
*/
|
|
99
|
-
function isFullURL(url,
|
|
100
|
-
return
|
|
129
|
+
function isFullURL(url, ignoreProtocol = false) {
|
|
130
|
+
return ignoreProtocol ? urlReg.test(url) : urlReg2.test(url);
|
|
101
131
|
}
|
|
102
132
|
/**
|
|
103
133
|
* 检测文本是否有可能转化为 object 或 array,注意,检测通过也未必能被正确格式化为 json
|
|
@@ -275,8 +305,8 @@ function checkHttpAuthInfo(authString, option) {
|
|
|
275
305
|
if (!authString) {
|
|
276
306
|
return false;
|
|
277
307
|
}
|
|
278
|
-
const
|
|
279
|
-
for (const cfg of
|
|
308
|
+
const configs = Object.values(SigningDefinitions);
|
|
309
|
+
for (const cfg of configs) {
|
|
280
310
|
if (authString.startsWith(cfg.prefix + " ")) {
|
|
281
311
|
return cfg.verify(authString, option);
|
|
282
312
|
}
|
|
@@ -365,7 +395,7 @@ async function responseError(res, statusCode, error, message) {
|
|
|
365
395
|
}
|
|
366
396
|
res.end();
|
|
367
397
|
}
|
|
368
|
-
async function
|
|
398
|
+
async function responseJson(res, data, statusCode = 200) {
|
|
369
399
|
res.statusCode = statusCode;
|
|
370
400
|
if (statusCode !== 204) {
|
|
371
401
|
res.setHeader("Content-Type", "application/json");
|
|
@@ -1087,7 +1117,7 @@ function isCreateOrderResponse(data) {
|
|
|
1087
1117
|
}
|
|
1088
1118
|
|
|
1089
1119
|
function isIdentityJwtPayload(payload) {
|
|
1090
|
-
return isObject(payload) && "sub" in payload && "iss" in payload && "scope" in payload && "idp" in payload && "external_id" in payload && "external_name" in payload;
|
|
1120
|
+
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;
|
|
1091
1121
|
}
|
|
1092
1122
|
|
|
1093
1123
|
function isAdJwtPayload(payload) {
|
|
@@ -1138,6 +1168,7 @@ class TokenVerifier {
|
|
|
1138
1168
|
weixin_unionid: payload.weixin_unionid || "",
|
|
1139
1169
|
distro: payload.distro || "",
|
|
1140
1170
|
variant: payload.variant || "",
|
|
1171
|
+
device_id: payload.device_id || "",
|
|
1141
1172
|
age: payload.age || 0
|
|
1142
1173
|
};
|
|
1143
1174
|
} catch (error) {
|
|
@@ -1173,7 +1204,7 @@ class TokenVerifier {
|
|
|
1173
1204
|
}
|
|
1174
1205
|
|
|
1175
1206
|
function isShipOrderPayload(data) {
|
|
1176
|
-
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;
|
|
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 && "is_sandbox" in data && typeof data.order_id === "string" && typeof data.combo_id === "string" && typeof data.quantity === "number" && typeof data.amount === "number" && typeof data.is_sandbox === "boolean" && Number.isSafeInteger(data.quantity) && Number.isSafeInteger(data.amount) && !!data.order_id && !!data.combo_id;
|
|
1177
1208
|
}
|
|
1178
1209
|
|
|
1179
1210
|
function isRefundPayload(data) {
|
|
@@ -1325,7 +1356,7 @@ function getNotificationMiddlewareForKoa(paths, config, handler) {
|
|
|
1325
1356
|
}
|
|
1326
1357
|
|
|
1327
1358
|
function isGMRequestBody(data) {
|
|
1328
|
-
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) && typeof data.request_id === "string" && typeof data.command === "string" && typeof data.args === "object" && data.args !== null;
|
|
1359
|
+
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;
|
|
1329
1360
|
}
|
|
1330
1361
|
function isGMErrorResponse(data) {
|
|
1331
1362
|
return isObject(data) && Object.keys(data).length === 2 && "error" in data && "message" in data;
|
|
@@ -1371,7 +1402,11 @@ class MemoryIdempotencyStore {
|
|
|
1371
1402
|
}
|
|
1372
1403
|
}
|
|
1373
1404
|
}
|
|
1374
|
-
/**
|
|
1405
|
+
/**
|
|
1406
|
+
* 基于 ioredis 的 store helper,
|
|
1407
|
+
*
|
|
1408
|
+
* 需要 Redis >= 7.0,见 https://redis.io/docs/latest/commands/set/
|
|
1409
|
+
*/
|
|
1375
1410
|
class RedisIdempotencyStore {
|
|
1376
1411
|
constructor(options) {
|
|
1377
1412
|
var _options$ttl;
|
|
@@ -1447,7 +1482,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1447
1482
|
}
|
|
1448
1483
|
// 处理幂等key
|
|
1449
1484
|
const currentRecord = {
|
|
1450
|
-
|
|
1485
|
+
nonce: genNonceString(),
|
|
1451
1486
|
key: gmRequest.idempotency_key || "",
|
|
1452
1487
|
id: gmRequest.request_id,
|
|
1453
1488
|
cmd: gmRequest.command,
|
|
@@ -1467,8 +1502,14 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1467
1502
|
responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, "parse old idempotency record failed");
|
|
1468
1503
|
return;
|
|
1469
1504
|
}
|
|
1505
|
+
// spell-checker:disable
|
|
1506
|
+
// fix spell error
|
|
1507
|
+
if (oldRecord && "noce" in oldRecord) {
|
|
1508
|
+
oldRecord.nonce = oldRecord.noce + "";
|
|
1509
|
+
}
|
|
1510
|
+
// spell-checker:enable
|
|
1470
1511
|
// 检查是否为首次请求
|
|
1471
|
-
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.
|
|
1512
|
+
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.nonce) === currentRecord.nonce;
|
|
1472
1513
|
if (!isFirstRequest && oldRecord) {
|
|
1473
1514
|
if (!oldRecord.resp) {
|
|
1474
1515
|
responseError(res, error2httpStatus["idempotency_conflict" /* GMError.IdempotencyConflict */], "idempotency_conflict" /* GMError.IdempotencyConflict */, "previous request is not completed");
|
|
@@ -1487,7 +1528,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1487
1528
|
}
|
|
1488
1529
|
// 所有错误检查完毕,通知游戏处理函数
|
|
1489
1530
|
try {
|
|
1490
|
-
const result = oldResponse || (await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.idempotency_key || "", gmRequest.version));
|
|
1531
|
+
const result = oldResponse || (await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.idempotency_key || "", gmRequest.version, gmRequest.origin || ""));
|
|
1491
1532
|
// 记录handler返回的结果以备下次检查使用,setXX出现错误也不再中断请求
|
|
1492
1533
|
if (storeHelper && gmRequest.idempotency_key) {
|
|
1493
1534
|
currentRecord.resp = result;
|
|
@@ -1514,7 +1555,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1514
1555
|
if (!result) {
|
|
1515
1556
|
console.warn(`gm command "${gmRequest.command}" should give back some data`);
|
|
1516
1557
|
}
|
|
1517
|
-
|
|
1558
|
+
responseJson(res, result != null ? result : {});
|
|
1518
1559
|
} catch (e) {
|
|
1519
1560
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1520
1561
|
responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, e instanceof Error ? e.message : String(e));
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { verify } from 'jsonwebtoken';
|
|
|
7
7
|
/** SDK 名称 */
|
|
8
8
|
const SDKName = "combo-sdk-node";
|
|
9
9
|
/** SDK 版本,在编译前会被动态替换 */
|
|
10
|
-
const SDKVersion = "1.
|
|
10
|
+
const SDKVersion = "1.4.0";
|
|
11
11
|
/**
|
|
12
12
|
* 世游支持的服务端点列表
|
|
13
13
|
*/
|
|
@@ -35,8 +35,8 @@ var Platform;
|
|
|
35
35
|
/** 支持的登录提供方 */
|
|
36
36
|
var IdP;
|
|
37
37
|
(function (IdP) {
|
|
38
|
-
/**
|
|
39
|
-
IdP["
|
|
38
|
+
/** 游客登录 */
|
|
39
|
+
IdP["Guest"] = "guest";
|
|
40
40
|
/** 世游通行证 */
|
|
41
41
|
IdP["Seayoo"] = "seayoo";
|
|
42
42
|
/** Sign-in with Apple */
|
|
@@ -49,6 +49,36 @@ var IdP;
|
|
|
49
49
|
IdP["Xiaomi"] = "xiaomi";
|
|
50
50
|
/** 微信登录 */
|
|
51
51
|
IdP["Weixin"] = "weixin";
|
|
52
|
+
/** OPPO 账号 */
|
|
53
|
+
IdP["Oppo"] = "oppo";
|
|
54
|
+
/** VIVO 账号 */
|
|
55
|
+
IdP["Vivo"] = "vivo";
|
|
56
|
+
/** 华为账号 */
|
|
57
|
+
IdP["Huawei"] = "huawei";
|
|
58
|
+
/** 荣耀账号 */
|
|
59
|
+
IdP["Honor"] = "honor";
|
|
60
|
+
/** UC(九游)登录 */
|
|
61
|
+
IdP["UC"] = "uc";
|
|
62
|
+
/** TapTap 登录 */
|
|
63
|
+
IdP["TapTap"] = "taptap";
|
|
64
|
+
/** 哔哩哔哩(B站)账号 */
|
|
65
|
+
IdP["Bilibili"] = "bilibili";
|
|
66
|
+
/** 应用宝 YSDK 登录 */
|
|
67
|
+
IdP["Yingyongbao"] = "yingyongbao";
|
|
68
|
+
/** 4399 账号登录 */
|
|
69
|
+
IdP["F4399"] = "4399";
|
|
70
|
+
/** 雷电模拟器账号 */
|
|
71
|
+
IdP["Leidian"] = "leidian";
|
|
72
|
+
/** 猫窝游戏 */
|
|
73
|
+
IdP["Maowo"] = "maowo";
|
|
74
|
+
/** 联想 */
|
|
75
|
+
IdP["Lenovo"] = "lenovo";
|
|
76
|
+
/** 魅族 */
|
|
77
|
+
IdP["Meizu"] = "meizu";
|
|
78
|
+
/** 酷派 */
|
|
79
|
+
IdP["Coolpad"] = "coolpad";
|
|
80
|
+
/** 努比亚 */
|
|
81
|
+
IdP["Nubia"] = "nubia";
|
|
52
82
|
})(IdP || (IdP = {}));
|
|
53
83
|
|
|
54
84
|
function getUserAgent(game) {
|
|
@@ -91,8 +121,8 @@ function isStringRecord(data) {
|
|
|
91
121
|
/**
|
|
92
122
|
* 检查一个url地址是否以 http/https 开头,如果第二个参数传递 true,则自适应协议(//....)也被认为是完整 url
|
|
93
123
|
*/
|
|
94
|
-
function isFullURL(url,
|
|
95
|
-
return
|
|
124
|
+
function isFullURL(url, ignoreProtocol = false) {
|
|
125
|
+
return ignoreProtocol ? urlReg.test(url) : urlReg2.test(url);
|
|
96
126
|
}
|
|
97
127
|
/**
|
|
98
128
|
* 检测文本是否有可能转化为 object 或 array,注意,检测通过也未必能被正确格式化为 json
|
|
@@ -270,8 +300,8 @@ function checkHttpAuthInfo(authString, option) {
|
|
|
270
300
|
if (!authString) {
|
|
271
301
|
return false;
|
|
272
302
|
}
|
|
273
|
-
const
|
|
274
|
-
for (const cfg of
|
|
303
|
+
const configs = Object.values(SigningDefinitions);
|
|
304
|
+
for (const cfg of configs) {
|
|
275
305
|
if (authString.startsWith(cfg.prefix + " ")) {
|
|
276
306
|
return cfg.verify(authString, option);
|
|
277
307
|
}
|
|
@@ -360,7 +390,7 @@ async function responseError(res, statusCode, error, message) {
|
|
|
360
390
|
}
|
|
361
391
|
res.end();
|
|
362
392
|
}
|
|
363
|
-
async function
|
|
393
|
+
async function responseJson(res, data, statusCode = 200) {
|
|
364
394
|
res.statusCode = statusCode;
|
|
365
395
|
if (statusCode !== 204) {
|
|
366
396
|
res.setHeader("Content-Type", "application/json");
|
|
@@ -1082,7 +1112,7 @@ function isCreateOrderResponse(data) {
|
|
|
1082
1112
|
}
|
|
1083
1113
|
|
|
1084
1114
|
function isIdentityJwtPayload(payload) {
|
|
1085
|
-
return isObject(payload) && "sub" in payload && "iss" in payload && "scope" in payload && "idp" in payload && "external_id" in payload && "external_name" in payload;
|
|
1115
|
+
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;
|
|
1086
1116
|
}
|
|
1087
1117
|
|
|
1088
1118
|
function isAdJwtPayload(payload) {
|
|
@@ -1133,6 +1163,7 @@ class TokenVerifier {
|
|
|
1133
1163
|
weixin_unionid: payload.weixin_unionid || "",
|
|
1134
1164
|
distro: payload.distro || "",
|
|
1135
1165
|
variant: payload.variant || "",
|
|
1166
|
+
device_id: payload.device_id || "",
|
|
1136
1167
|
age: payload.age || 0
|
|
1137
1168
|
};
|
|
1138
1169
|
} catch (error) {
|
|
@@ -1168,7 +1199,7 @@ class TokenVerifier {
|
|
|
1168
1199
|
}
|
|
1169
1200
|
|
|
1170
1201
|
function isShipOrderPayload(data) {
|
|
1171
|
-
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;
|
|
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 && "is_sandbox" in data && typeof data.order_id === "string" && typeof data.combo_id === "string" && typeof data.quantity === "number" && typeof data.amount === "number" && typeof data.is_sandbox === "boolean" && Number.isSafeInteger(data.quantity) && Number.isSafeInteger(data.amount) && !!data.order_id && !!data.combo_id;
|
|
1172
1203
|
}
|
|
1173
1204
|
|
|
1174
1205
|
function isRefundPayload(data) {
|
|
@@ -1320,7 +1351,7 @@ function getNotificationMiddlewareForKoa(paths, config, handler) {
|
|
|
1320
1351
|
}
|
|
1321
1352
|
|
|
1322
1353
|
function isGMRequestBody(data) {
|
|
1323
|
-
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) && typeof data.request_id === "string" && typeof data.command === "string" && typeof data.args === "object" && data.args !== null;
|
|
1354
|
+
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;
|
|
1324
1355
|
}
|
|
1325
1356
|
function isGMErrorResponse(data) {
|
|
1326
1357
|
return isObject(data) && Object.keys(data).length === 2 && "error" in data && "message" in data;
|
|
@@ -1366,7 +1397,11 @@ class MemoryIdempotencyStore {
|
|
|
1366
1397
|
}
|
|
1367
1398
|
}
|
|
1368
1399
|
}
|
|
1369
|
-
/**
|
|
1400
|
+
/**
|
|
1401
|
+
* 基于 ioredis 的 store helper,
|
|
1402
|
+
*
|
|
1403
|
+
* 需要 Redis >= 7.0,见 https://redis.io/docs/latest/commands/set/
|
|
1404
|
+
*/
|
|
1370
1405
|
class RedisIdempotencyStore {
|
|
1371
1406
|
constructor(options) {
|
|
1372
1407
|
var _options$ttl;
|
|
@@ -1442,7 +1477,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1442
1477
|
}
|
|
1443
1478
|
// 处理幂等key
|
|
1444
1479
|
const currentRecord = {
|
|
1445
|
-
|
|
1480
|
+
nonce: genNonceString(),
|
|
1446
1481
|
key: gmRequest.idempotency_key || "",
|
|
1447
1482
|
id: gmRequest.request_id,
|
|
1448
1483
|
cmd: gmRequest.command,
|
|
@@ -1462,8 +1497,14 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1462
1497
|
responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, "parse old idempotency record failed");
|
|
1463
1498
|
return;
|
|
1464
1499
|
}
|
|
1500
|
+
// spell-checker:disable
|
|
1501
|
+
// fix spell error
|
|
1502
|
+
if (oldRecord && "noce" in oldRecord) {
|
|
1503
|
+
oldRecord.nonce = oldRecord.noce + "";
|
|
1504
|
+
}
|
|
1505
|
+
// spell-checker:enable
|
|
1465
1506
|
// 检查是否为首次请求
|
|
1466
|
-
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.
|
|
1507
|
+
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.nonce) === currentRecord.nonce;
|
|
1467
1508
|
if (!isFirstRequest && oldRecord) {
|
|
1468
1509
|
if (!oldRecord.resp) {
|
|
1469
1510
|
responseError(res, error2httpStatus["idempotency_conflict" /* GMError.IdempotencyConflict */], "idempotency_conflict" /* GMError.IdempotencyConflict */, "previous request is not completed");
|
|
@@ -1482,7 +1523,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1482
1523
|
}
|
|
1483
1524
|
// 所有错误检查完毕,通知游戏处理函数
|
|
1484
1525
|
try {
|
|
1485
|
-
const result = oldResponse || (await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.idempotency_key || "", gmRequest.version));
|
|
1526
|
+
const result = oldResponse || (await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.idempotency_key || "", gmRequest.version, gmRequest.origin || ""));
|
|
1486
1527
|
// 记录handler返回的结果以备下次检查使用,setXX出现错误也不再中断请求
|
|
1487
1528
|
if (storeHelper && gmRequest.idempotency_key) {
|
|
1488
1529
|
currentRecord.resp = result;
|
|
@@ -1509,7 +1550,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1509
1550
|
if (!result) {
|
|
1510
1551
|
console.warn(`gm command "${gmRequest.command}" should give back some data`);
|
|
1511
1552
|
}
|
|
1512
|
-
|
|
1553
|
+
responseJson(res, result != null ? result : {});
|
|
1513
1554
|
} catch (e) {
|
|
1514
1555
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1515
1556
|
responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, e instanceof Error ? e.message : String(e));
|
|
@@ -16,5 +16,11 @@ export interface ShipOrderNotification {
|
|
|
16
16
|
amount: number;
|
|
17
17
|
/** 游戏侧创建订单时提供的订单上下文,透传回游戏 */
|
|
18
18
|
context?: string;
|
|
19
|
+
/**
|
|
20
|
+
* 是否是沙盒订单。沙盒订单意味着此订单并未产生真实的付款。
|
|
21
|
+
*
|
|
22
|
+
* 预期此字段仅用于记录日志和数据埋点。无论是否是沙盒订单,游戏侧都应当发货。
|
|
23
|
+
*/
|
|
24
|
+
is_sandbox: boolean;
|
|
19
25
|
}
|
|
20
26
|
export declare function isShipOrderPayload(data: unknown): data is ShipOrderNotification;
|
package/dist/utils/base.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ export declare function isStringRecord(data: unknown): data is Record<string, un
|
|
|
23
23
|
/**
|
|
24
24
|
* 检查一个url地址是否以 http/https 开头,如果第二个参数传递 true,则自适应协议(//....)也被认为是完整 url
|
|
25
25
|
*/
|
|
26
|
-
export declare function isFullURL(url: string,
|
|
26
|
+
export declare function isFullURL(url: string, ignoreProtocol?: boolean): boolean;
|
|
27
27
|
/**
|
|
28
28
|
* 检测文本是否有可能转化为 object 或 array,注意,检测通过也未必能被正确格式化为 json
|
|
29
29
|
*/
|
package/dist/utils/http.d.ts
CHANGED
|
@@ -3,4 +3,4 @@ import type { IncomingMessage, ServerResponse } from "http";
|
|
|
3
3
|
export declare function readBody(req: IncomingMessage, backup?: string): Promise<string>;
|
|
4
4
|
export declare function done(res: ServerResponse, statusCode: number, message?: string): Promise<void>;
|
|
5
5
|
export declare function responseError(res: ServerResponse, statusCode: number, error: string, message: string): Promise<void>;
|
|
6
|
-
export declare function
|
|
6
|
+
export declare function responseJson(res: ServerResponse, data: unknown, statusCode?: number): Promise<void>;
|
package/dist/verify/id.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export interface IdentityPayload {
|
|
|
4
4
|
/**
|
|
5
5
|
* combo_id 是世游分配的聚合用户 ID
|
|
6
6
|
*
|
|
7
|
-
* 游戏侧应当使用 combo_id
|
|
7
|
+
* 游戏侧应当使用 combo_id 作为用户的唯一标识,即游戏帐号。
|
|
8
8
|
*/
|
|
9
9
|
combo_id: string;
|
|
10
10
|
/**
|
|
@@ -36,6 +36,10 @@ export interface IdentityPayload {
|
|
|
36
36
|
* 注意:weixin_unionid 只在 IdP 为 weixin 时才会有值。
|
|
37
37
|
*/
|
|
38
38
|
weixin_unionid: string;
|
|
39
|
+
/**
|
|
40
|
+
* DeviceId 是用户在登录时使用的设备的唯一 ID。
|
|
41
|
+
*/
|
|
42
|
+
device_id: string;
|
|
39
43
|
/**
|
|
40
44
|
* distro 是游戏客户端的发行版本标识。
|
|
41
45
|
* 游戏侧可将 distro 用于服务端数据埋点,以及特定的业务逻辑判断。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seayoo-io/combo-sdk-node",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Combo SDK for Node.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -15,17 +15,6 @@
|
|
|
15
15
|
"publishConfig": {
|
|
16
16
|
"access": "public"
|
|
17
17
|
},
|
|
18
|
-
"scripts": {
|
|
19
|
-
"build": "rimraf ./dist && node preconvert.cjs && microbundle --sourcemap false --format cjs,es --target node",
|
|
20
|
-
"preconvert": "node preconvert.cjs",
|
|
21
|
-
"dev": "microbundle watch --target node",
|
|
22
|
-
"clear": "rimraf dist && rimraf node_modules",
|
|
23
|
-
"type-check": "tsc --noEmit",
|
|
24
|
-
"lint": "eslint ./src/**/*.ts",
|
|
25
|
-
"lint:fix": "eslint ./src/**/*.{ts,js} --fix",
|
|
26
|
-
"test": "vitest",
|
|
27
|
-
"pub": "pnpm type-check && pnpm lint:fix && vitest run && pnpm build && npm publish"
|
|
28
|
-
},
|
|
29
18
|
"engines": {
|
|
30
19
|
"node": ">=12"
|
|
31
20
|
},
|
|
@@ -62,5 +51,16 @@
|
|
|
62
51
|
},
|
|
63
52
|
"dependencies": {
|
|
64
53
|
"jsonwebtoken": "^9.0.2"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "rimraf ./dist && node preconvert.cjs && microbundle --sourcemap false --format cjs,es --target node",
|
|
57
|
+
"preconvert": "node preconvert.cjs",
|
|
58
|
+
"dev": "microbundle watch --target node",
|
|
59
|
+
"clear": "rimraf dist && rimraf node_modules",
|
|
60
|
+
"type-check": "tsc --noEmit",
|
|
61
|
+
"lint": "eslint ./src/**/*.ts",
|
|
62
|
+
"lint:fix": "eslint ./src/**/*.{ts,js} --fix",
|
|
63
|
+
"test": "vitest",
|
|
64
|
+
"prepublish": "pnpm type-check && pnpm lint:fix && vitest run && pnpm build"
|
|
65
65
|
}
|
|
66
|
-
}
|
|
66
|
+
}
|