@seayoo-io/combo-sdk-node 1.3.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 +4 -2
- package/dist/const.d.ts +1 -1
- package/dist/gm/types.d.ts +4 -2
- package/dist/index.cjs +17 -11
- package/dist/index.js +17 -11
- package/dist/utils/base.d.ts +1 -1
- package/dist/utils/http.d.ts +1 -1
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -387,12 +387,14 @@ GM 模块使用方式类似于 Notify 模块。
|
|
|
387
387
|
import { GMError } from "@seayoo-io/combo-sdk-node"
|
|
388
388
|
|
|
389
389
|
// 1.1 定义 GM 处理函数
|
|
390
|
-
function gmCommandHandler(command, args, requestId, idempotencyKey, version) {
|
|
390
|
+
function gmCommandHandler(command, args, requestId, idempotencyKey, version, origin) {
|
|
391
|
+
// command 对应的是 GM 协议中定义的方法名,区分大小写,args 为该方法的参数。
|
|
391
392
|
// requestId 本次 GM 请求的唯一 ID。游戏侧可用此值来对请求进行去重。
|
|
392
393
|
// idempotencyKey 本次 GM 请求的 Idempotency Key。如果有非空值则应当执行幂等处理逻辑。
|
|
393
394
|
// SDK 已经内置了 idempotencyKey 的处理工具和配置,详细见下描述
|
|
394
395
|
// version 对应的是世游 GM 服务的版本号,目前固定是 2.0
|
|
395
|
-
//
|
|
396
|
+
// origin 对应的是 GM 请求的来源标记,区分大小写,如 combo/console
|
|
397
|
+
// 详见 https://docs.seayoo.com/combo/server/gm/#gm-request
|
|
396
398
|
switch(command) {
|
|
397
399
|
case "SomeCmdName":
|
|
398
400
|
// 参数 args 由协议定义,在使用前需要做严格的格式和逻辑校验
|
package/dist/const.d.ts
CHANGED
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/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
|
*/
|
|
@@ -126,8 +126,8 @@ function isStringRecord(data) {
|
|
|
126
126
|
/**
|
|
127
127
|
* 检查一个url地址是否以 http/https 开头,如果第二个参数传递 true,则自适应协议(//....)也被认为是完整 url
|
|
128
128
|
*/
|
|
129
|
-
function isFullURL(url,
|
|
130
|
-
return
|
|
129
|
+
function isFullURL(url, ignoreProtocol = false) {
|
|
130
|
+
return ignoreProtocol ? urlReg.test(url) : urlReg2.test(url);
|
|
131
131
|
}
|
|
132
132
|
/**
|
|
133
133
|
* 检测文本是否有可能转化为 object 或 array,注意,检测通过也未必能被正确格式化为 json
|
|
@@ -305,8 +305,8 @@ function checkHttpAuthInfo(authString, option) {
|
|
|
305
305
|
if (!authString) {
|
|
306
306
|
return false;
|
|
307
307
|
}
|
|
308
|
-
const
|
|
309
|
-
for (const cfg of
|
|
308
|
+
const configs = Object.values(SigningDefinitions);
|
|
309
|
+
for (const cfg of configs) {
|
|
310
310
|
if (authString.startsWith(cfg.prefix + " ")) {
|
|
311
311
|
return cfg.verify(authString, option);
|
|
312
312
|
}
|
|
@@ -395,7 +395,7 @@ async function responseError(res, statusCode, error, message) {
|
|
|
395
395
|
}
|
|
396
396
|
res.end();
|
|
397
397
|
}
|
|
398
|
-
async function
|
|
398
|
+
async function responseJson(res, data, statusCode = 200) {
|
|
399
399
|
res.statusCode = statusCode;
|
|
400
400
|
if (statusCode !== 204) {
|
|
401
401
|
res.setHeader("Content-Type", "application/json");
|
|
@@ -1356,7 +1356,7 @@ function getNotificationMiddlewareForKoa(paths, config, handler) {
|
|
|
1356
1356
|
}
|
|
1357
1357
|
|
|
1358
1358
|
function isGMRequestBody(data) {
|
|
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) && 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;
|
|
1360
1360
|
}
|
|
1361
1361
|
function isGMErrorResponse(data) {
|
|
1362
1362
|
return isObject(data) && Object.keys(data).length === 2 && "error" in data && "message" in data;
|
|
@@ -1482,7 +1482,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1482
1482
|
}
|
|
1483
1483
|
// 处理幂等key
|
|
1484
1484
|
const currentRecord = {
|
|
1485
|
-
|
|
1485
|
+
nonce: genNonceString(),
|
|
1486
1486
|
key: gmRequest.idempotency_key || "",
|
|
1487
1487
|
id: gmRequest.request_id,
|
|
1488
1488
|
cmd: gmRequest.command,
|
|
@@ -1502,8 +1502,14 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1502
1502
|
responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, "parse old idempotency record failed");
|
|
1503
1503
|
return;
|
|
1504
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
|
|
1505
1511
|
// 检查是否为首次请求
|
|
1506
|
-
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.
|
|
1512
|
+
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.nonce) === currentRecord.nonce;
|
|
1507
1513
|
if (!isFirstRequest && oldRecord) {
|
|
1508
1514
|
if (!oldRecord.resp) {
|
|
1509
1515
|
responseError(res, error2httpStatus["idempotency_conflict" /* GMError.IdempotencyConflict */], "idempotency_conflict" /* GMError.IdempotencyConflict */, "previous request is not completed");
|
|
@@ -1522,7 +1528,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1522
1528
|
}
|
|
1523
1529
|
// 所有错误检查完毕,通知游戏处理函数
|
|
1524
1530
|
try {
|
|
1525
|
-
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 || ""));
|
|
1526
1532
|
// 记录handler返回的结果以备下次检查使用,setXX出现错误也不再中断请求
|
|
1527
1533
|
if (storeHelper && gmRequest.idempotency_key) {
|
|
1528
1534
|
currentRecord.resp = result;
|
|
@@ -1549,7 +1555,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1549
1555
|
if (!result) {
|
|
1550
1556
|
console.warn(`gm command "${gmRequest.command}" should give back some data`);
|
|
1551
1557
|
}
|
|
1552
|
-
|
|
1558
|
+
responseJson(res, result != null ? result : {});
|
|
1553
1559
|
} catch (e) {
|
|
1554
1560
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1555
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
|
*/
|
|
@@ -121,8 +121,8 @@ function isStringRecord(data) {
|
|
|
121
121
|
/**
|
|
122
122
|
* 检查一个url地址是否以 http/https 开头,如果第二个参数传递 true,则自适应协议(//....)也被认为是完整 url
|
|
123
123
|
*/
|
|
124
|
-
function isFullURL(url,
|
|
125
|
-
return
|
|
124
|
+
function isFullURL(url, ignoreProtocol = false) {
|
|
125
|
+
return ignoreProtocol ? urlReg.test(url) : urlReg2.test(url);
|
|
126
126
|
}
|
|
127
127
|
/**
|
|
128
128
|
* 检测文本是否有可能转化为 object 或 array,注意,检测通过也未必能被正确格式化为 json
|
|
@@ -300,8 +300,8 @@ function checkHttpAuthInfo(authString, option) {
|
|
|
300
300
|
if (!authString) {
|
|
301
301
|
return false;
|
|
302
302
|
}
|
|
303
|
-
const
|
|
304
|
-
for (const cfg of
|
|
303
|
+
const configs = Object.values(SigningDefinitions);
|
|
304
|
+
for (const cfg of configs) {
|
|
305
305
|
if (authString.startsWith(cfg.prefix + " ")) {
|
|
306
306
|
return cfg.verify(authString, option);
|
|
307
307
|
}
|
|
@@ -390,7 +390,7 @@ async function responseError(res, statusCode, error, message) {
|
|
|
390
390
|
}
|
|
391
391
|
res.end();
|
|
392
392
|
}
|
|
393
|
-
async function
|
|
393
|
+
async function responseJson(res, data, statusCode = 200) {
|
|
394
394
|
res.statusCode = statusCode;
|
|
395
395
|
if (statusCode !== 204) {
|
|
396
396
|
res.setHeader("Content-Type", "application/json");
|
|
@@ -1351,7 +1351,7 @@ function getNotificationMiddlewareForKoa(paths, config, handler) {
|
|
|
1351
1351
|
}
|
|
1352
1352
|
|
|
1353
1353
|
function isGMRequestBody(data) {
|
|
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) && 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;
|
|
1355
1355
|
}
|
|
1356
1356
|
function isGMErrorResponse(data) {
|
|
1357
1357
|
return isObject(data) && Object.keys(data).length === 2 && "error" in data && "message" in data;
|
|
@@ -1477,7 +1477,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1477
1477
|
}
|
|
1478
1478
|
// 处理幂等key
|
|
1479
1479
|
const currentRecord = {
|
|
1480
|
-
|
|
1480
|
+
nonce: genNonceString(),
|
|
1481
1481
|
key: gmRequest.idempotency_key || "",
|
|
1482
1482
|
id: gmRequest.request_id,
|
|
1483
1483
|
cmd: gmRequest.command,
|
|
@@ -1497,8 +1497,14 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1497
1497
|
responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, "parse old idempotency record failed");
|
|
1498
1498
|
return;
|
|
1499
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
|
|
1500
1506
|
// 检查是否为首次请求
|
|
1501
|
-
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.
|
|
1507
|
+
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.nonce) === currentRecord.nonce;
|
|
1502
1508
|
if (!isFirstRequest && oldRecord) {
|
|
1503
1509
|
if (!oldRecord.resp) {
|
|
1504
1510
|
responseError(res, error2httpStatus["idempotency_conflict" /* GMError.IdempotencyConflict */], "idempotency_conflict" /* GMError.IdempotencyConflict */, "previous request is not completed");
|
|
@@ -1517,7 +1523,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1517
1523
|
}
|
|
1518
1524
|
// 所有错误检查完毕,通知游戏处理函数
|
|
1519
1525
|
try {
|
|
1520
|
-
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 || ""));
|
|
1521
1527
|
// 记录handler返回的结果以备下次检查使用,setXX出现错误也不再中断请求
|
|
1522
1528
|
if (storeHelper && gmRequest.idempotency_key) {
|
|
1523
1529
|
currentRecord.resp = result;
|
|
@@ -1544,7 +1550,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1544
1550
|
if (!result) {
|
|
1545
1551
|
console.warn(`gm command "${gmRequest.command}" should give back some data`);
|
|
1546
1552
|
}
|
|
1547
|
-
|
|
1553
|
+
responseJson(res, result != null ? result : {});
|
|
1548
1554
|
} catch (e) {
|
|
1549
1555
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1550
1556
|
responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, e instanceof Error ? e.message : String(e));
|
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/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
|
+
}
|