@seayoo-io/combo-sdk-node 1.10.0 → 1.11.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
@@ -39,6 +39,27 @@ import { ApiClient } from "@seayoo-io/combo-sdk-node"
39
39
  const { ApiClient } = require("@seayoo-io/combo-sdk-node")
40
40
  ```
41
41
 
42
+ ## 工具函数
43
+
44
+ 除上述模块外,SDK 还导出一组通用工具函数,详细说明见 [Utils](./docs/utils.md)。
45
+
46
+ ```js
47
+ import { mayBeComboId } from "@seayoo-io/combo-sdk-node"
48
+
49
+ // 校验一个值是否为 Combo ID 格式
50
+ if (mayBeComboId(combo_id)) {
51
+ // ...
52
+ }
53
+ ```
54
+
55
+ | 函数 | 说明 |
56
+ | -------------------------- | ---------------------------------- |
57
+ | `genSessionID` | 生成用于标记用户上下线的 SessionID |
58
+ | `mayBeComboId` | 检查值是否符合 Combo ID 格式 |
59
+ | `parseAuthorizationHeader` | 解析 Authorization 请求头 |
60
+ | `calcAuthorizationHeader` | 计算 HTTP 请求的 Authorization 头 |
61
+ | `checkHttpAuthInfo` | 校验 HTTP 请求的签名信息 |
62
+
42
63
  ## 文档
43
64
 
44
65
  各模块的详细用法见 `docs` 目录:
@@ -61,3 +82,4 @@ const { ApiClient } = require("@seayoo-io/combo-sdk-node")
61
82
  - [Step 1 准备](./docs/gm.md#step-1-准备)
62
83
  - [Step 2 创建 Store Helper 以启用 idempotencyKey 处理逻辑](./docs/gm.md#step-2-创建-store-helper-以启用-idempotencykey-处理逻辑)
63
84
  - [Step 3 绑定处理函数](./docs/gm.md#step-3-绑定处理函数)
85
+ - [Utils](./docs/utils.md) — 通用工具函数
package/dist/index.cjs CHANGED
@@ -33,7 +33,7 @@ jsonwebtoken = __toESM(jsonwebtoken);
33
33
  /** SDK 名称 */
34
34
  const SDKName = "combo-sdk-node";
35
35
  /** SDK 版本,在编译前会被动态替换 */
36
- const SDKVersion = "1.10.0";
36
+ const SDKVersion = "1.11.0";
37
37
  /**
38
38
  * 世游支持的服务端点列表
39
39
  */
@@ -413,7 +413,7 @@ async function readBody(req, backup) {
413
413
  backup || console.error({
414
414
  type: "RequestUnReadable",
415
415
  url: req.url,
416
- methe: req.method,
416
+ method: req.method,
417
417
  message: "Cannot read body, stream not readable"
418
418
  });
419
419
  return backup || "";
@@ -464,6 +464,24 @@ function genNonceString() {
464
464
  const r = Math.random() + Math.random();
465
465
  return (0, crypto.createHash)("md5").update(`${ts}${r}`).digest("hex").toLowerCase();
466
466
  }
467
+ /**
468
+ * 检查一个值是否为 Combo ID 的格式。
469
+ *
470
+ * 仅做格式检查,不检查该 ID 是否真实存在。
471
+ */
472
+ function mayBeComboId(id) {
473
+ if (typeof id !== "string") return false;
474
+ const m = id.match(/^9?[15](\d{2})(\d{2})(\d{2})(\d{5})(\d{4})$/);
475
+ if (!m) return false;
476
+ const year = 2e3 + Number(m[1]);
477
+ const month = Number(m[2]);
478
+ const date = Number(m[3]);
479
+ const seconds = Number(m[4]);
480
+ const sn = Number(m[5]);
481
+ if (year <= 2020 || seconds >= 24 * 3600 || sn === 0) return false;
482
+ const d = new Date(Date.UTC(year, month - 1, date));
483
+ return d.getUTCFullYear() === year && d.getUTCMonth() === month - 1 && d.getUTCDate() === date;
484
+ }
467
485
  //#endregion
468
486
  //#region src/request/inc/rule.ts
469
487
  const defaultDataField = "data";
@@ -1710,4 +1728,5 @@ exports.getNotificationHandlerForExpress = getNotificationHandlerForExpress;
1710
1728
  exports.getNotificationHandlerForKoa = getNotificationHandlerForKoa;
1711
1729
  exports.getNotificationMiddlewareForExpress = getNotificationMiddlewareForExpress;
1712
1730
  exports.getNotificationMiddlewareForKoa = getNotificationMiddlewareForKoa;
1731
+ exports.mayBeComboId = mayBeComboId;
1713
1732
  exports.parseAuthorizationHeader = parseAuthorizationHeader;
package/dist/index.d.cts CHANGED
@@ -60,6 +60,12 @@ declare function checkHttpAuthInfo(authString: string, option: SignDataOptionFor
60
60
  //#region src/utils/index.d.ts
61
61
  /** 生成一个 SessionID 以用作用户上下线的上报标记 */
62
62
  declare function genSessionID(comboId: string): string;
63
+ /**
64
+ * 检查一个值是否为 Combo ID 的格式。
65
+ *
66
+ * 仅做格式检查,不检查该 ID 是否真实存在。
67
+ */
68
+ declare function mayBeComboId(id: unknown): id is string;
63
69
  //#endregion
64
70
  //#region src/request/inc/type.d.ts
65
71
  type IBaseRequestBody = URLSearchParams | string;
@@ -904,4 +910,4 @@ declare class RedisIdempotencyStore implements IdempotencyKeyStoreHelper {
904
910
  setXX(key: string, value: string): Promise<void>;
905
911
  }
906
912
  //#endregion
907
- export { type AdPayload, ApiClient, type ApiClientConfig, type CreateOrderOption, type CreateOrderResponse, type DataTag, type DataTagsNotification, type ENotificationPayload, Endpoint, GMError, type GetMiniGameWeixinAccessTokenOption, type GetMiniGameWeixinAccessTokenResponse, IdP, type IdentityPayload, MemoryIdempotencyStore, type NotificationHandler, NotificationType, type OrderMetaData, Platform, RedisIdempotencyStore, type RefundNotification, type SDKBaseConfig, type SendOtpMetaData, type SendOtpOption, type SendOtpResponse, type ShipOrderNotification, type SupportedOtpChannel, TokenVerifier, type VerifyOtpOption, type VerifyOtpResponse, type VoiceModerationRequestOption, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getGMCommandHandler, getGMHandlerForExpress, getGMHandlerForKoa, getGMMiddlewareForExpress, getGMMiddlewareForKoa, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
913
+ export { type AdPayload, ApiClient, type ApiClientConfig, type CreateOrderOption, type CreateOrderResponse, type DataTag, type DataTagsNotification, type ENotificationPayload, Endpoint, GMError, type GetMiniGameWeixinAccessTokenOption, type GetMiniGameWeixinAccessTokenResponse, IdP, type IdentityPayload, MemoryIdempotencyStore, type NotificationHandler, NotificationType, type OrderMetaData, Platform, RedisIdempotencyStore, type RefundNotification, type SDKBaseConfig, type SendOtpMetaData, type SendOtpOption, type SendOtpResponse, type ShipOrderNotification, type SupportedOtpChannel, TokenVerifier, type VerifyOtpOption, type VerifyOtpResponse, type VoiceModerationRequestOption, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getGMCommandHandler, getGMHandlerForExpress, getGMHandlerForKoa, getGMMiddlewareForExpress, getGMMiddlewareForKoa, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, mayBeComboId, parseAuthorizationHeader };
package/dist/index.d.ts CHANGED
@@ -60,6 +60,12 @@ declare function checkHttpAuthInfo(authString: string, option: SignDataOptionFor
60
60
  //#region src/utils/index.d.ts
61
61
  /** 生成一个 SessionID 以用作用户上下线的上报标记 */
62
62
  declare function genSessionID(comboId: string): string;
63
+ /**
64
+ * 检查一个值是否为 Combo ID 的格式。
65
+ *
66
+ * 仅做格式检查,不检查该 ID 是否真实存在。
67
+ */
68
+ declare function mayBeComboId(id: unknown): id is string;
63
69
  //#endregion
64
70
  //#region src/request/inc/type.d.ts
65
71
  type IBaseRequestBody = URLSearchParams | string;
@@ -904,4 +910,4 @@ declare class RedisIdempotencyStore implements IdempotencyKeyStoreHelper {
904
910
  setXX(key: string, value: string): Promise<void>;
905
911
  }
906
912
  //#endregion
907
- export { type AdPayload, ApiClient, type ApiClientConfig, type CreateOrderOption, type CreateOrderResponse, type DataTag, type DataTagsNotification, type ENotificationPayload, Endpoint, GMError, type GetMiniGameWeixinAccessTokenOption, type GetMiniGameWeixinAccessTokenResponse, IdP, type IdentityPayload, MemoryIdempotencyStore, type NotificationHandler, NotificationType, type OrderMetaData, Platform, RedisIdempotencyStore, type RefundNotification, type SDKBaseConfig, type SendOtpMetaData, type SendOtpOption, type SendOtpResponse, type ShipOrderNotification, type SupportedOtpChannel, TokenVerifier, type VerifyOtpOption, type VerifyOtpResponse, type VoiceModerationRequestOption, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getGMCommandHandler, getGMHandlerForExpress, getGMHandlerForKoa, getGMMiddlewareForExpress, getGMMiddlewareForKoa, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
913
+ export { type AdPayload, ApiClient, type ApiClientConfig, type CreateOrderOption, type CreateOrderResponse, type DataTag, type DataTagsNotification, type ENotificationPayload, Endpoint, GMError, type GetMiniGameWeixinAccessTokenOption, type GetMiniGameWeixinAccessTokenResponse, IdP, type IdentityPayload, MemoryIdempotencyStore, type NotificationHandler, NotificationType, type OrderMetaData, Platform, RedisIdempotencyStore, type RefundNotification, type SDKBaseConfig, type SendOtpMetaData, type SendOtpOption, type SendOtpResponse, type ShipOrderNotification, type SupportedOtpChannel, TokenVerifier, type VerifyOtpOption, type VerifyOtpResponse, type VoiceModerationRequestOption, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getGMCommandHandler, getGMHandlerForExpress, getGMHandlerForKoa, getGMMiddlewareForExpress, getGMMiddlewareForKoa, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, mayBeComboId, parseAuthorizationHeader };
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import jwt from "jsonwebtoken";
7
7
  /** SDK 名称 */
8
8
  const SDKName = "combo-sdk-node";
9
9
  /** SDK 版本,在编译前会被动态替换 */
10
- const SDKVersion = "1.10.0";
10
+ const SDKVersion = "1.11.0";
11
11
  /**
12
12
  * 世游支持的服务端点列表
13
13
  */
@@ -387,7 +387,7 @@ async function readBody(req, backup) {
387
387
  backup || console.error({
388
388
  type: "RequestUnReadable",
389
389
  url: req.url,
390
- methe: req.method,
390
+ method: req.method,
391
391
  message: "Cannot read body, stream not readable"
392
392
  });
393
393
  return backup || "";
@@ -438,6 +438,24 @@ function genNonceString() {
438
438
  const r = Math.random() + Math.random();
439
439
  return createHash("md5").update(`${ts}${r}`).digest("hex").toLowerCase();
440
440
  }
441
+ /**
442
+ * 检查一个值是否为 Combo ID 的格式。
443
+ *
444
+ * 仅做格式检查,不检查该 ID 是否真实存在。
445
+ */
446
+ function mayBeComboId(id) {
447
+ if (typeof id !== "string") return false;
448
+ const m = id.match(/^9?[15](\d{2})(\d{2})(\d{2})(\d{5})(\d{4})$/);
449
+ if (!m) return false;
450
+ const year = 2e3 + Number(m[1]);
451
+ const month = Number(m[2]);
452
+ const date = Number(m[3]);
453
+ const seconds = Number(m[4]);
454
+ const sn = Number(m[5]);
455
+ if (year <= 2020 || seconds >= 24 * 3600 || sn === 0) return false;
456
+ const d = new Date(Date.UTC(year, month - 1, date));
457
+ return d.getUTCFullYear() === year && d.getUTCMonth() === month - 1 && d.getUTCDate() === date;
458
+ }
441
459
  //#endregion
442
460
  //#region src/request/inc/rule.ts
443
461
  const defaultDataField = "data";
@@ -1662,4 +1680,4 @@ function getGMMiddlewareForKoa(paths, config, handler, storeHelper) {
1662
1680
  };
1663
1681
  }
1664
1682
  //#endregion
1665
- export { ApiClient, Endpoint, GMError, IdP, MemoryIdempotencyStore, NotificationType, Platform, RedisIdempotencyStore, TokenVerifier, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getGMCommandHandler, getGMHandlerForExpress, getGMHandlerForKoa, getGMMiddlewareForExpress, getGMMiddlewareForKoa, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
1683
+ export { ApiClient, Endpoint, GMError, IdP, MemoryIdempotencyStore, NotificationType, Platform, RedisIdempotencyStore, TokenVerifier, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getGMCommandHandler, getGMHandlerForExpress, getGMHandlerForKoa, getGMMiddlewareForExpress, getGMMiddlewareForKoa, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, mayBeComboId, parseAuthorizationHeader };
package/docs/utils.md ADDED
@@ -0,0 +1,74 @@
1
+ > [Combo SDK for Node.js](../README.md) / Utils
2
+
3
+ # Utils
4
+
5
+ ## genSessionID
6
+
7
+ 生成用于标记用户上下线的 SessionID。
8
+
9
+ ```js
10
+ import { genSessionID } from "@seayoo-io/combo-sdk-node"
11
+
12
+ const sessionID = genSessionID(combo_id)
13
+ // 32 位小写十六进制字符串
14
+ ```
15
+
16
+ ## mayBeComboId
17
+
18
+ 检查一个值是否为 Combo ID 的格式,仅做格式检查,不检查该 ID 是否真实存在。
19
+
20
+ ```js
21
+ import { mayBeComboId } from "@seayoo-io/combo-sdk-node"
22
+
23
+ mayBeComboId("1260418509000005") // true
24
+ mayBeComboId("95231223346130001") // true
25
+ mayBeComboId("1260418509000000") // false
26
+ mayBeComboId(1260418509000005) // false,只接受字符串
27
+ ```
28
+
29
+ ## parseAuthorizationHeader
30
+
31
+ 解析 Authorization 请求头,返回 `{ Game, Timestamp, Signature }`,格式不正确时返回 `null`。
32
+
33
+ ```js
34
+ import { parseAuthorizationHeader } from "@seayoo-io/combo-sdk-node"
35
+
36
+ const info = parseAuthorizationHeader(authString)
37
+ if (!info) {
38
+ // 请求头格式不正确
39
+ }
40
+ ```
41
+
42
+ ## calcAuthorizationHeader
43
+
44
+ 计算 HTTP 请求的 Authorization 请求头。
45
+
46
+ ```js
47
+ import { calcAuthorizationHeader } from "@seayoo-io/combo-sdk-node"
48
+
49
+ const authString = calcAuthorizationHeader({
50
+ game: "<GameId>",
51
+ secret: "<SecretKey>",
52
+ endpoint: "https://api.seayoo.com",
53
+ method: "POST",
54
+ url: "/path/to/api",
55
+ data: { combo_id: "<ComboID>" },
56
+ })
57
+ ```
58
+
59
+ ## checkHttpAuthInfo
60
+
61
+ 校验 HTTP 请求的签名信息是否正确,签名不合法或时间戳超出允许范围时返回 `false`。
62
+
63
+ ```js
64
+ import { checkHttpAuthInfo } from "@seayoo-io/combo-sdk-node"
65
+
66
+ const ok = checkHttpAuthInfo(authString, {
67
+ // 省略 game 时不校验签发方
68
+ secret: "<SecretKey>",
69
+ endpoint: "https://api.seayoo.com",
70
+ method: "POST",
71
+ url: "/path/to/api",
72
+ data: { combo_id: "<ComboID>" },
73
+ })
74
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seayoo-io/combo-sdk-node",
3
- "version": "1.10.0",
3
+ "version": "1.11.0",
4
4
  "description": "Combo SDK for Node.js",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",