@seayoo-io/combo-sdk-node 0.3.2 → 1.0.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 +17 -4
- package/dist/const.d.ts +6 -1
- package/dist/gm/types.d.ts +1 -1
- package/dist/index.cjs +6 -4
- package/dist/index.js +6 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,6 +9,14 @@
|
|
|
9
9
|
- 验证世游服务端签发的 Identity Token 和 Ads Token
|
|
10
10
|
- 接收 Server GM Command 指令并回复响应
|
|
11
11
|
|
|
12
|
+
## breaking change
|
|
13
|
+
|
|
14
|
+
v1 新增 gm 模块的 [IdempotencyKey](https://docs.seayoo.com/combo/server/gm/#idempotency),主要改动包括:
|
|
15
|
+
|
|
16
|
+
1. 修改了 commandHandler 的参数,插入 idempotencyKey 参数;当此参数为非空字符串时需要处理幂等性;
|
|
17
|
+
2. 新增预设错误 IdempotencyConflict / IdempotencyMismatch
|
|
18
|
+
3. gm 服务版本号升级到 2.0
|
|
19
|
+
|
|
12
20
|
## 安装
|
|
13
21
|
|
|
14
22
|
```js
|
|
@@ -370,9 +378,10 @@ GM 模块使用方式类似于 Notify 模块。
|
|
|
370
378
|
import { GMError } from "@seayoo-io/combo-sdk-node"
|
|
371
379
|
|
|
372
380
|
// 1.1 定义 GM 处理函数
|
|
373
|
-
function gmCommandHandler(command, args, requestId, version) {
|
|
374
|
-
// requestId
|
|
375
|
-
//
|
|
381
|
+
function gmCommandHandler(command, args, requestId, idempotencyKey, version) {
|
|
382
|
+
// requestId 本次 GM 请求的唯一 ID。游戏侧可用此值来对请求进行去重。
|
|
383
|
+
// idempotencyKey 本次 GM 请求的 Idempotency Key。如果有非空值则应当执行幂等处理逻辑。
|
|
384
|
+
// version 对应的是世游 GM 服务的版本号,目前固定是 2.0
|
|
376
385
|
// command 对应的是 GM 协议中定义的方法名,区分大小写
|
|
377
386
|
switch(command) {
|
|
378
387
|
case "SomeCmdName":
|
|
@@ -384,7 +393,7 @@ function gmCommandHandler(command, args, requestId, version) {
|
|
|
384
393
|
// 1. 返回一个 Error 对象,最终响应以 500(internalServerError)作为 http status
|
|
385
394
|
return new Error("自定义错误信息")
|
|
386
395
|
// 2. 返回一个自定义消息体 (预设的消息类型见下描述),推荐使用此方式
|
|
387
|
-
return { error: GMError.
|
|
396
|
+
return { error: GMError.ThrottlingError, message: "请求过于频繁,请稍后再试" }
|
|
388
397
|
// 3. 直接抛出错误,用于处理异常场景,最终响应以 500 作为 http status
|
|
389
398
|
// 相比于方式1,此流程会打印一条错误日志
|
|
390
399
|
throw new Error("some unknown error")
|
|
@@ -408,6 +417,10 @@ const enum GMError {
|
|
|
408
417
|
InvalidCommand = "invalid_command",
|
|
409
418
|
/** GM 命令发送频率过高,被游戏侧限流,命令未被处理。 */
|
|
410
419
|
ThrottlingError = "throttling_error",
|
|
420
|
+
/** 幂等处理重试请求时,idempotency_key 所对应的原始请求尚未处理完毕。*/
|
|
421
|
+
IdempotencyConflict = "idempotency_conflict",
|
|
422
|
+
/** 幂等处理重试请求时,请求内容和 idempotency_key 所对应的原始请求内容不一致。*/
|
|
423
|
+
IdempotencyMismatch = "idempotency_mismatch",
|
|
411
424
|
/** GM 命令的参数不正确。例如,参数缺少必要的字段,或参数的字段类型不正确。 */
|
|
412
425
|
InvalidArgs = "invalid_args",
|
|
413
426
|
|
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 = "0.
|
|
4
|
+
export declare const SDKVersion = "1.0.0";
|
|
5
5
|
/**
|
|
6
6
|
* 世游支持的服务端点列表
|
|
7
7
|
*/
|
|
@@ -55,6 +55,10 @@ export declare const enum GMError {
|
|
|
55
55
|
InvalidCommand = "invalid_command",
|
|
56
56
|
/** GM 命令发送频率过高,被游戏侧限流,命令未被处理。 */
|
|
57
57
|
ThrottlingError = "throttling_error",
|
|
58
|
+
/** 幂等处理重试请求时,idempotency_key 所对应的原始请求尚未处理完毕。*/
|
|
59
|
+
IdempotencyConflict = "idempotency_conflict",
|
|
60
|
+
/** 幂等处理重试请求时,请求内容和 idempotency_key 所对应的原始请求内容不一致。*/
|
|
61
|
+
IdempotencyMismatch = "idempotency_mismatch",
|
|
58
62
|
/** GM 命令的参数不正确。例如,参数缺少必要的字段,或参数的字段类型不正确。 */
|
|
59
63
|
InvalidArgs = "invalid_args",
|
|
60
64
|
/** 游戏当前处于停服维护状态,无法处理收到的 GM 命令。*/
|
|
@@ -81,6 +85,7 @@ export declare const enum HttpStatus {
|
|
|
81
85
|
RequestTimeout = 408,
|
|
82
86
|
Conflict = 409,
|
|
83
87
|
UnsupportedMediaType = 415,
|
|
88
|
+
UnprocessableEntity = 422,
|
|
84
89
|
TooManyRequests = 429,
|
|
85
90
|
InternalServerError = 500,
|
|
86
91
|
BadGateway = 502,
|
package/dist/gm/types.d.ts
CHANGED
|
@@ -12,5 +12,5 @@ export interface GMErrorResponse {
|
|
|
12
12
|
* 如果遇到错误可以返回错误结构 ({ error, message }) 或 Error 对象
|
|
13
13
|
*/
|
|
14
14
|
export type GMCommandHandler = {
|
|
15
|
-
(command: string, args: Record<string, unknown>, requestId: string, version: string): MaybePromise<Record<string, unknown> | GMErrorResponse | Error>;
|
|
15
|
+
(command: string, args: Record<string, unknown>, requestId: string, idempotencyKey: string, version: string): MaybePromise<Record<string, unknown> | GMErrorResponse | Error>;
|
|
16
16
|
};
|
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 = "0.
|
|
15
|
+
const SDKVersion = "1.0.0";
|
|
16
16
|
/**
|
|
17
17
|
* 世游支持的服务端点列表
|
|
18
18
|
*/
|
|
@@ -1320,7 +1320,7 @@ function getNotificationMiddlewareForKoa(paths, config, handler) {
|
|
|
1320
1320
|
}
|
|
1321
1321
|
|
|
1322
1322
|
function isGMRequestBody(data) {
|
|
1323
|
-
return isObject(data) && "version" in data && "request_id" in data && "command" in data && "args" in data && typeof data.request_id === "string" && typeof data.command === "string" && typeof data.args === "object" && data.args !== null;
|
|
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;
|
|
1324
1324
|
}
|
|
1325
1325
|
function isGMErrorResponse(data) {
|
|
1326
1326
|
return isObject(data) && Object.keys(data).length === 2 && "error" in data && "message" in data;
|
|
@@ -1334,6 +1334,8 @@ const error2httpStatus = {
|
|
|
1334
1334
|
["invalid_args" /* GMError.InvalidArgs */]: 400 /* HttpStatus.BadRequest */,
|
|
1335
1335
|
["network_error" /* GMError.NetworkError */]: 500 /* HttpStatus.InternalServerError */,
|
|
1336
1336
|
["database_error" /* GMError.DatabaseError */]: 500 /* HttpStatus.InternalServerError */,
|
|
1337
|
+
["idempotency_conflict" /* GMError.IdempotencyConflict */]: 409 /* HttpStatus.Conflict */,
|
|
1338
|
+
["idempotency_mismatch" /* GMError.IdempotencyMismatch */]: 422 /* HttpStatus.UnprocessableEntity */,
|
|
1337
1339
|
["timeout_error" /* GMError.TimeoutError */]: 500 /* HttpStatus.InternalServerError */,
|
|
1338
1340
|
["maintenance_error" /* GMError.MaintenanceError */]: 503 /* HttpStatus.ServiceUnavailable */,
|
|
1339
1341
|
["throttling_error" /* GMError.ThrottlingError */]: 429 /* HttpStatus.TooManyRequests */,
|
|
@@ -1379,7 +1381,7 @@ function getGMCommandHandler(config, handler) {
|
|
|
1379
1381
|
}
|
|
1380
1382
|
// 所有错误检查完毕,通知游戏处理函数
|
|
1381
1383
|
try {
|
|
1382
|
-
const result = await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.version);
|
|
1384
|
+
const result = await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.idempotency_key || "", gmRequest.version);
|
|
1383
1385
|
// 游戏主动抛错不打印日志信息
|
|
1384
1386
|
if (result instanceof Error) {
|
|
1385
1387
|
responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, result.message);
|
|
@@ -1401,7 +1403,7 @@ function getGMCommandHandler(config, handler) {
|
|
|
1401
1403
|
}
|
|
1402
1404
|
repsonseJson(res, result != null ? result : {});
|
|
1403
1405
|
} catch (e) {
|
|
1404
|
-
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}) exec error`, e);
|
|
1406
|
+
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1405
1407
|
responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, e instanceof Error ? e.message : String(e));
|
|
1406
1408
|
}
|
|
1407
1409
|
};
|
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 = "0.
|
|
10
|
+
const SDKVersion = "1.0.0";
|
|
11
11
|
/**
|
|
12
12
|
* 世游支持的服务端点列表
|
|
13
13
|
*/
|
|
@@ -1315,7 +1315,7 @@ function getNotificationMiddlewareForKoa(paths, config, handler) {
|
|
|
1315
1315
|
}
|
|
1316
1316
|
|
|
1317
1317
|
function isGMRequestBody(data) {
|
|
1318
|
-
return isObject(data) && "version" in data && "request_id" in data && "command" in data && "args" in data && typeof data.request_id === "string" && typeof data.command === "string" && typeof data.args === "object" && data.args !== null;
|
|
1318
|
+
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;
|
|
1319
1319
|
}
|
|
1320
1320
|
function isGMErrorResponse(data) {
|
|
1321
1321
|
return isObject(data) && Object.keys(data).length === 2 && "error" in data && "message" in data;
|
|
@@ -1329,6 +1329,8 @@ const error2httpStatus = {
|
|
|
1329
1329
|
["invalid_args" /* GMError.InvalidArgs */]: 400 /* HttpStatus.BadRequest */,
|
|
1330
1330
|
["network_error" /* GMError.NetworkError */]: 500 /* HttpStatus.InternalServerError */,
|
|
1331
1331
|
["database_error" /* GMError.DatabaseError */]: 500 /* HttpStatus.InternalServerError */,
|
|
1332
|
+
["idempotency_conflict" /* GMError.IdempotencyConflict */]: 409 /* HttpStatus.Conflict */,
|
|
1333
|
+
["idempotency_mismatch" /* GMError.IdempotencyMismatch */]: 422 /* HttpStatus.UnprocessableEntity */,
|
|
1332
1334
|
["timeout_error" /* GMError.TimeoutError */]: 500 /* HttpStatus.InternalServerError */,
|
|
1333
1335
|
["maintenance_error" /* GMError.MaintenanceError */]: 503 /* HttpStatus.ServiceUnavailable */,
|
|
1334
1336
|
["throttling_error" /* GMError.ThrottlingError */]: 429 /* HttpStatus.TooManyRequests */,
|
|
@@ -1374,7 +1376,7 @@ function getGMCommandHandler(config, handler) {
|
|
|
1374
1376
|
}
|
|
1375
1377
|
// 所有错误检查完毕,通知游戏处理函数
|
|
1376
1378
|
try {
|
|
1377
|
-
const result = await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.version);
|
|
1379
|
+
const result = await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.idempotency_key || "", gmRequest.version);
|
|
1378
1380
|
// 游戏主动抛错不打印日志信息
|
|
1379
1381
|
if (result instanceof Error) {
|
|
1380
1382
|
responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, result.message);
|
|
@@ -1396,7 +1398,7 @@ function getGMCommandHandler(config, handler) {
|
|
|
1396
1398
|
}
|
|
1397
1399
|
repsonseJson(res, result != null ? result : {});
|
|
1398
1400
|
} catch (e) {
|
|
1399
|
-
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}) exec error`, e);
|
|
1401
|
+
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1400
1402
|
responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, e instanceof Error ? e.message : String(e));
|
|
1401
1403
|
}
|
|
1402
1404
|
};
|