@seayoo-io/combo-sdk-node 0.3.1 → 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 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 每次 GM 请求的唯一 ID。游戏侧可用此值来对请求进行去重。
375
- // version 对应的是世游 GM 服务的版本号,目前固定是 1.0
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.ThrottleError, message: "请求过于频繁,请稍后再试" }
396
+ return { error: GMError.ThrottlingError, message: "请求过于频繁,请稍后再试" }
388
397
  // 3. 直接抛出错误,用于处理异常场景,最终响应以 500 作为 http status
389
398
  // 相比于方式1,此流程会打印一条错误日志
390
399
  throw new Error("some unknown error")
@@ -396,9 +405,9 @@ function gmCommandHandler(command, args, requestId, version) {
396
405
 
397
406
  /** GM 预设错误类型枚举 */
398
407
  const enum GMError {
399
- /** 请求的 HTTP 方法不正确。例如,期望收到 POST 请求,但实际收到了 GET 请求。*/
408
+ /** 请求中的 HTTP method 不正确,没有按照预期使用 POST。*/
400
409
  InvalidHttpMethod = "invalid_http_method",
401
- /** 请求的 Content-Type 不正确。例如,期望收到 application/json,但实际收到了 text/plain。*/
410
+ /** 请求中的 Content-Type 不是 application/json。*/
402
411
  InvalidContentType = "invalid_content_type",
403
412
  /** 对 HTTP 请求的签名验证不通过。这意味着 HTTP 请求不可信。 */
404
413
  InvalidSignature = "invalid_signature",
@@ -406,18 +415,24 @@ const enum GMError {
406
415
  InvalidRequest = "invalid_request",
407
416
  /** 游戏侧不认识请求中的 GM 命令。 */
408
417
  InvalidCommand = "invalid_command",
409
- /** GM 命令的参数不正确。例如,缺少必要的字段,或字段类型不正确。 */
418
+ /** GM 命令发送频率过高,被游戏侧限流,命令未被处理。 */
419
+ ThrottlingError = "throttling_error",
420
+ /** 幂等处理重试请求时,idempotency_key 所对应的原始请求尚未处理完毕。*/
421
+ IdempotencyConflict = "idempotency_conflict",
422
+ /** 幂等处理重试请求时,请求内容和 idempotency_key 所对应的原始请求内容不一致。*/
423
+ IdempotencyMismatch = "idempotency_mismatch",
424
+ /** GM 命令的参数不正确。例如,参数缺少必要的字段,或参数的字段类型不正确。 */
410
425
  InvalidArgs = "invalid_args",
411
426
 
427
+ /** 游戏当前处于停服维护状态,无法处理收到的 GM 命令。*/
428
+ MaintenanceError = "maintenance_error",
412
429
  /** 网络通信错误。 */
413
430
  NetworkError = "network_error",
414
- /** 数据库操作异常。 */
431
+ /** 数据库操作异常导致 GM 命令执行失败。 */
415
432
  DatabaseError = "database_error",
416
433
  /** GM 命令处理超时。 */
417
434
  TimeoutError = "timeout_error",
418
- /** GM 命令发送频率过高,被游戏侧限流,命令未被处理。 */
419
- ThrottleError = "throttle_error",
420
- /** 处理 GM 命令时内部出错。可作为通用错误类型。*/
435
+ /** 处理 GM 命令时内部出错。可作为兜底的通用错误类型。*/
421
436
  InternalError = "internal_error",
422
437
  }
423
438
 
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.3.1";
4
+ export declare const SDKVersion = "1.0.0";
5
5
  /**
6
6
  * 世游支持的服务端点列表
7
7
  */
@@ -43,9 +43,9 @@ export declare enum IdP {
43
43
  }
44
44
  /** GM 错误类型枚举 */
45
45
  export declare const enum GMError {
46
- /** 请求的 HTTP 方法不正确。例如,期望收到 POST 请求,但实际收到了 GET 请求。*/
46
+ /** 请求中的 HTTP method 不正确,没有按照预期使用 POST。*/
47
47
  InvalidHttpMethod = "invalid_http_method",
48
- /** 请求的 Content-Type 不正确。例如,期望收到 application/json,但实际收到了 text/plain。*/
48
+ /** 请求中的 Content-Type 不是 application/json。*/
49
49
  InvalidContentType = "invalid_content_type",
50
50
  /** 对 HTTP 请求的签名验证不通过。这意味着 HTTP 请求不可信。 */
51
51
  InvalidSignature = "invalid_signature",
@@ -53,17 +53,23 @@ export declare const enum GMError {
53
53
  InvalidRequest = "invalid_request",
54
54
  /** 游戏侧不认识请求中的 GM 命令。 */
55
55
  InvalidCommand = "invalid_command",
56
- /** GM 命令的参数不正确。例如,缺少必要的字段,或字段类型不正确。 */
56
+ /** GM 命令发送频率过高,被游戏侧限流,命令未被处理。 */
57
+ ThrottlingError = "throttling_error",
58
+ /** 幂等处理重试请求时,idempotency_key 所对应的原始请求尚未处理完毕。*/
59
+ IdempotencyConflict = "idempotency_conflict",
60
+ /** 幂等处理重试请求时,请求内容和 idempotency_key 所对应的原始请求内容不一致。*/
61
+ IdempotencyMismatch = "idempotency_mismatch",
62
+ /** GM 命令的参数不正确。例如,参数缺少必要的字段,或参数的字段类型不正确。 */
57
63
  InvalidArgs = "invalid_args",
64
+ /** 游戏当前处于停服维护状态,无法处理收到的 GM 命令。*/
65
+ MaintenanceError = "maintenance_error",
58
66
  /** 网络通信错误。 */
59
67
  NetworkError = "network_error",
60
- /** 数据库操作异常。 */
68
+ /** 数据库操作异常导致 GM 命令执行失败。 */
61
69
  DatabaseError = "database_error",
62
70
  /** GM 命令处理超时。 */
63
71
  TimeoutError = "timeout_error",
64
- /** GM 命令发送频率过高,被游戏侧限流,命令未被处理。 */
65
- ThrottleError = "throttle_error",
66
- /** 处理 GM 命令时内部出错。可作为通用错误类型。*/
72
+ /** 处理 GM 命令时内部出错。可作为兜底的通用错误类型。*/
67
73
  InternalError = "internal_error"
68
74
  }
69
75
  export declare const enum HttpStatus {
@@ -79,6 +85,7 @@ export declare const enum HttpStatus {
79
85
  RequestTimeout = 408,
80
86
  Conflict = 409,
81
87
  UnsupportedMediaType = 415,
88
+ UnprocessableEntity = 422,
82
89
  TooManyRequests = 429,
83
90
  InternalServerError = 500,
84
91
  BadGateway = 502,
@@ -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.3.1";
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,8 +1334,11 @@ 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
- ["throttle_error" /* GMError.ThrottleError */]: 429 /* HttpStatus.TooManyRequests */,
1340
+ ["maintenance_error" /* GMError.MaintenanceError */]: 503 /* HttpStatus.ServiceUnavailable */,
1341
+ ["throttling_error" /* GMError.ThrottlingError */]: 429 /* HttpStatus.TooManyRequests */,
1339
1342
  ["internal_error" /* GMError.InternalError */]: 500 /* HttpStatus.InternalServerError */
1340
1343
  };
1341
1344
  /**
@@ -1378,7 +1381,7 @@ function getGMCommandHandler(config, handler) {
1378
1381
  }
1379
1382
  // 所有错误检查完毕,通知游戏处理函数
1380
1383
  try {
1381
- 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);
1382
1385
  // 游戏主动抛错不打印日志信息
1383
1386
  if (result instanceof Error) {
1384
1387
  responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, result.message);
@@ -1400,7 +1403,7 @@ function getGMCommandHandler(config, handler) {
1400
1403
  }
1401
1404
  repsonseJson(res, result != null ? result : {});
1402
1405
  } catch (e) {
1403
- 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);
1404
1407
  responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, e instanceof Error ? e.message : String(e));
1405
1408
  }
1406
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.3.1";
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,8 +1329,11 @@ 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
- ["throttle_error" /* GMError.ThrottleError */]: 429 /* HttpStatus.TooManyRequests */,
1335
+ ["maintenance_error" /* GMError.MaintenanceError */]: 503 /* HttpStatus.ServiceUnavailable */,
1336
+ ["throttling_error" /* GMError.ThrottlingError */]: 429 /* HttpStatus.TooManyRequests */,
1334
1337
  ["internal_error" /* GMError.InternalError */]: 500 /* HttpStatus.InternalServerError */
1335
1338
  };
1336
1339
  /**
@@ -1373,7 +1376,7 @@ function getGMCommandHandler(config, handler) {
1373
1376
  }
1374
1377
  // 所有错误检查完毕,通知游戏处理函数
1375
1378
  try {
1376
- 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);
1377
1380
  // 游戏主动抛错不打印日志信息
1378
1381
  if (result instanceof Error) {
1379
1382
  responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, result.message);
@@ -1395,7 +1398,7 @@ function getGMCommandHandler(config, handler) {
1395
1398
  }
1396
1399
  repsonseJson(res, result != null ? result : {});
1397
1400
  } catch (e) {
1398
- 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);
1399
1402
  responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, e instanceof Error ? e.message : String(e));
1400
1403
  }
1401
1404
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seayoo-io/combo-sdk-node",
3
- "version": "0.3.1",
3
+ "version": "1.0.0",
4
4
  "description": "Combo SDK for Node.js",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",