@seayoo-io/combo-sdk-node 0.3.2 → 1.1.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")
@@ -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.3.2";
4
+ export declare const SDKVersion = "1.1.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,
@@ -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.2";
15
+ const SDKVersion = "1.1.0";
16
16
  /**
17
17
  * 世游支持的服务端点列表
18
18
  */
@@ -1133,7 +1133,8 @@ class TokenVerifier {
1133
1133
  external_name: payload.external_name,
1134
1134
  weixin_unionid: payload.weixin_unionid || "",
1135
1135
  distro: payload.distro || "",
1136
- variant: payload.variant || ""
1136
+ variant: payload.variant || "",
1137
+ age: payload.age || 0
1137
1138
  };
1138
1139
  } catch (error) {
1139
1140
  return new Error(`IdentityToken 验证失败: ${error instanceof Error ? error.message : String(error)}`);
@@ -1320,7 +1321,7 @@ function getNotificationMiddlewareForKoa(paths, config, handler) {
1320
1321
  }
1321
1322
 
1322
1323
  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;
1324
+ 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
1325
  }
1325
1326
  function isGMErrorResponse(data) {
1326
1327
  return isObject(data) && Object.keys(data).length === 2 && "error" in data && "message" in data;
@@ -1334,6 +1335,8 @@ const error2httpStatus = {
1334
1335
  ["invalid_args" /* GMError.InvalidArgs */]: 400 /* HttpStatus.BadRequest */,
1335
1336
  ["network_error" /* GMError.NetworkError */]: 500 /* HttpStatus.InternalServerError */,
1336
1337
  ["database_error" /* GMError.DatabaseError */]: 500 /* HttpStatus.InternalServerError */,
1338
+ ["idempotency_conflict" /* GMError.IdempotencyConflict */]: 409 /* HttpStatus.Conflict */,
1339
+ ["idempotency_mismatch" /* GMError.IdempotencyMismatch */]: 422 /* HttpStatus.UnprocessableEntity */,
1337
1340
  ["timeout_error" /* GMError.TimeoutError */]: 500 /* HttpStatus.InternalServerError */,
1338
1341
  ["maintenance_error" /* GMError.MaintenanceError */]: 503 /* HttpStatus.ServiceUnavailable */,
1339
1342
  ["throttling_error" /* GMError.ThrottlingError */]: 429 /* HttpStatus.TooManyRequests */,
@@ -1379,7 +1382,7 @@ function getGMCommandHandler(config, handler) {
1379
1382
  }
1380
1383
  // 所有错误检查完毕,通知游戏处理函数
1381
1384
  try {
1382
- const result = await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.version);
1385
+ const result = await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.idempotency_key || "", gmRequest.version);
1383
1386
  // 游戏主动抛错不打印日志信息
1384
1387
  if (result instanceof Error) {
1385
1388
  responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, result.message);
@@ -1401,7 +1404,7 @@ function getGMCommandHandler(config, handler) {
1401
1404
  }
1402
1405
  repsonseJson(res, result != null ? result : {});
1403
1406
  } catch (e) {
1404
- console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}) exec error`, e);
1407
+ console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
1405
1408
  responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, e instanceof Error ? e.message : String(e));
1406
1409
  }
1407
1410
  };
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.2";
10
+ const SDKVersion = "1.1.0";
11
11
  /**
12
12
  * 世游支持的服务端点列表
13
13
  */
@@ -1128,7 +1128,8 @@ class TokenVerifier {
1128
1128
  external_name: payload.external_name,
1129
1129
  weixin_unionid: payload.weixin_unionid || "",
1130
1130
  distro: payload.distro || "",
1131
- variant: payload.variant || ""
1131
+ variant: payload.variant || "",
1132
+ age: payload.age || 0
1132
1133
  };
1133
1134
  } catch (error) {
1134
1135
  return new Error(`IdentityToken 验证失败: ${error instanceof Error ? error.message : String(error)}`);
@@ -1315,7 +1316,7 @@ function getNotificationMiddlewareForKoa(paths, config, handler) {
1315
1316
  }
1316
1317
 
1317
1318
  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;
1319
+ 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
1320
  }
1320
1321
  function isGMErrorResponse(data) {
1321
1322
  return isObject(data) && Object.keys(data).length === 2 && "error" in data && "message" in data;
@@ -1329,6 +1330,8 @@ const error2httpStatus = {
1329
1330
  ["invalid_args" /* GMError.InvalidArgs */]: 400 /* HttpStatus.BadRequest */,
1330
1331
  ["network_error" /* GMError.NetworkError */]: 500 /* HttpStatus.InternalServerError */,
1331
1332
  ["database_error" /* GMError.DatabaseError */]: 500 /* HttpStatus.InternalServerError */,
1333
+ ["idempotency_conflict" /* GMError.IdempotencyConflict */]: 409 /* HttpStatus.Conflict */,
1334
+ ["idempotency_mismatch" /* GMError.IdempotencyMismatch */]: 422 /* HttpStatus.UnprocessableEntity */,
1332
1335
  ["timeout_error" /* GMError.TimeoutError */]: 500 /* HttpStatus.InternalServerError */,
1333
1336
  ["maintenance_error" /* GMError.MaintenanceError */]: 503 /* HttpStatus.ServiceUnavailable */,
1334
1337
  ["throttling_error" /* GMError.ThrottlingError */]: 429 /* HttpStatus.TooManyRequests */,
@@ -1374,7 +1377,7 @@ function getGMCommandHandler(config, handler) {
1374
1377
  }
1375
1378
  // 所有错误检查完毕,通知游戏处理函数
1376
1379
  try {
1377
- const result = await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.version);
1380
+ const result = await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.idempotency_key || "", gmRequest.version);
1378
1381
  // 游戏主动抛错不打印日志信息
1379
1382
  if (result instanceof Error) {
1380
1383
  responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, result.message);
@@ -1396,7 +1399,7 @@ function getGMCommandHandler(config, handler) {
1396
1399
  }
1397
1400
  repsonseJson(res, result != null ? result : {});
1398
1401
  } catch (e) {
1399
- console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}) exec error`, e);
1402
+ console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
1400
1403
  responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, e instanceof Error ? e.message : String(e));
1401
1404
  }
1402
1405
  };
@@ -45,9 +45,24 @@ export interface IdentityPayload {
45
45
  * variant 是游戏客户端的分包标识。
46
46
  * 游戏侧可将 variant 用于服务端数据埋点,以及特定的业务逻辑判断。
47
47
  *
48
- * 注意:Variant 只在客户端是分包时才会有值。当客户端不是分包的情况下,variant 为空字符串。
48
+ * 注意:variant 只在客户端是分包时才会有值。当客户端不是分包的情况下,variant 为空字符串。
49
49
  */
50
50
  variant: string;
51
+ /**
52
+ * age 是根据用户的实名认证信息得到的年龄。
53
+ *
54
+ * 0 表示未知。
55
+ *
56
+ * 在某些特殊场景下,游戏侧可用 age 来自行处理防沉迷。
57
+ *
58
+ * 注意:age 不保证返回精确的年龄信息,仅保证用于防沉迷处理时的准确度够用。
59
+ *
60
+ * 例如:
61
+ *
62
+ * 当某个用户真实年龄为 35 岁时,age 可能返回 18,
63
+ * 当某个用户真实年龄为 17 岁时,age 可能返回 16。
64
+ */
65
+ age: number;
51
66
  }
52
67
  export type IdentityJwtPayload = JwtPayload & Omit<IdentityPayload, "combo_id"> & {
53
68
  scope: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seayoo-io/combo-sdk-node",
3
- "version": "0.3.2",
3
+ "version": "1.1.0",
4
4
  "description": "Combo SDK for Node.js",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",