@seayoo-io/combo-sdk-node 1.3.0 → 1.4.1

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
@@ -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
- // command 对应的是 GM 协议中定义的方法名,区分大小写
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
@@ -1,7 +1,7 @@
1
1
  /** SDK 名称 */
2
2
  export declare const SDKName = "combo-sdk-node";
3
3
  /** SDK 版本,在编译前会被动态替换 */
4
- export declare const SDKVersion = "1.3.0";
4
+ export declare const SDKVersion = "1.4.1";
5
5
  /**
6
6
  * 世游支持的服务端点列表
7
7
  */
@@ -22,7 +22,9 @@ export declare enum Platform {
22
22
  /** macOS 桌面平台 */
23
23
  macOS = "macos",
24
24
  /** 微信小游戏 */
25
- Weixin = "weixin"
25
+ Weixin = "weixin",
26
+ /** WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏 */
27
+ WebGL = "webgl"
26
28
  }
27
29
  /** 支持的登录提供方 */
28
30
  export declare enum IdP {
@@ -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
- noce: string;
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.3.0";
15
+ const SDKVersion = "1.4.1";
16
16
  /**
17
17
  * 世游支持的服务端点列表
18
18
  */
@@ -36,6 +36,8 @@ exports.Platform = void 0;
36
36
  Platform["macOS"] = "macos";
37
37
  /** 微信小游戏 */
38
38
  Platform["Weixin"] = "weixin";
39
+ /** WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏 */
40
+ Platform["WebGL"] = "webgl";
39
41
  })(exports.Platform || (exports.Platform = {}));
40
42
  /** 支持的登录提供方 */
41
43
  exports.IdP = void 0;
@@ -126,8 +128,8 @@ function isStringRecord(data) {
126
128
  /**
127
129
  * 检查一个url地址是否以 http/https 开头,如果第二个参数传递 true,则自适应协议(//....)也被认为是完整 url
128
130
  */
129
- function isFullURL(url, ignoreProtocal = false) {
130
- return ignoreProtocal ? urlReg.test(url) : urlReg2.test(url);
131
+ function isFullURL(url, ignoreProtocol = false) {
132
+ return ignoreProtocol ? urlReg.test(url) : urlReg2.test(url);
131
133
  }
132
134
  /**
133
135
  * 检测文本是否有可能转化为 object 或 array,注意,检测通过也未必能被正确格式化为 json
@@ -305,8 +307,8 @@ function checkHttpAuthInfo(authString, option) {
305
307
  if (!authString) {
306
308
  return false;
307
309
  }
308
- const cfgs = Object.values(SigningDefinitions);
309
- for (const cfg of cfgs) {
310
+ const configs = Object.values(SigningDefinitions);
311
+ for (const cfg of configs) {
310
312
  if (authString.startsWith(cfg.prefix + " ")) {
311
313
  return cfg.verify(authString, option);
312
314
  }
@@ -395,7 +397,7 @@ async function responseError(res, statusCode, error, message) {
395
397
  }
396
398
  res.end();
397
399
  }
398
- async function repsonseJson(res, data, statusCode = 200) {
400
+ async function responseJson(res, data, statusCode = 200) {
399
401
  res.statusCode = statusCode;
400
402
  if (statusCode !== 204) {
401
403
  res.setHeader("Content-Type", "application/json");
@@ -1356,7 +1358,7 @@ function getNotificationMiddlewareForKoa(paths, config, handler) {
1356
1358
  }
1357
1359
 
1358
1360
  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;
1361
+ 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
1362
  }
1361
1363
  function isGMErrorResponse(data) {
1362
1364
  return isObject(data) && Object.keys(data).length === 2 && "error" in data && "message" in data;
@@ -1482,7 +1484,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
1482
1484
  }
1483
1485
  // 处理幂等key
1484
1486
  const currentRecord = {
1485
- noce: genNonceString(),
1487
+ nonce: genNonceString(),
1486
1488
  key: gmRequest.idempotency_key || "",
1487
1489
  id: gmRequest.request_id,
1488
1490
  cmd: gmRequest.command,
@@ -1502,8 +1504,14 @@ function getGMCommandHandler(config, handler, storeHelper) {
1502
1504
  responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, "parse old idempotency record failed");
1503
1505
  return;
1504
1506
  }
1507
+ // spell-checker:disable
1508
+ // fix spell error
1509
+ if (oldRecord && "noce" in oldRecord) {
1510
+ oldRecord.nonce = oldRecord.noce + "";
1511
+ }
1512
+ // spell-checker:enable
1505
1513
  // 检查是否为首次请求
1506
- const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.noce) === currentRecord.noce;
1514
+ const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.nonce) === currentRecord.nonce;
1507
1515
  if (!isFirstRequest && oldRecord) {
1508
1516
  if (!oldRecord.resp) {
1509
1517
  responseError(res, error2httpStatus["idempotency_conflict" /* GMError.IdempotencyConflict */], "idempotency_conflict" /* GMError.IdempotencyConflict */, "previous request is not completed");
@@ -1522,7 +1530,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
1522
1530
  }
1523
1531
  // 所有错误检查完毕,通知游戏处理函数
1524
1532
  try {
1525
- const result = oldResponse || (await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.idempotency_key || "", gmRequest.version));
1533
+ const result = oldResponse || (await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.idempotency_key || "", gmRequest.version, gmRequest.origin || ""));
1526
1534
  // 记录handler返回的结果以备下次检查使用,setXX出现错误也不再中断请求
1527
1535
  if (storeHelper && gmRequest.idempotency_key) {
1528
1536
  currentRecord.resp = result;
@@ -1549,7 +1557,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
1549
1557
  if (!result) {
1550
1558
  console.warn(`gm command "${gmRequest.command}" should give back some data`);
1551
1559
  }
1552
- repsonseJson(res, result != null ? result : {});
1560
+ responseJson(res, result != null ? result : {});
1553
1561
  } catch (e) {
1554
1562
  console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
1555
1563
  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.3.0";
10
+ const SDKVersion = "1.4.1";
11
11
  /**
12
12
  * 世游支持的服务端点列表
13
13
  */
@@ -31,6 +31,8 @@ var Platform;
31
31
  Platform["macOS"] = "macos";
32
32
  /** 微信小游戏 */
33
33
  Platform["Weixin"] = "weixin";
34
+ /** WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏 */
35
+ Platform["WebGL"] = "webgl";
34
36
  })(Platform || (Platform = {}));
35
37
  /** 支持的登录提供方 */
36
38
  var IdP;
@@ -121,8 +123,8 @@ function isStringRecord(data) {
121
123
  /**
122
124
  * 检查一个url地址是否以 http/https 开头,如果第二个参数传递 true,则自适应协议(//....)也被认为是完整 url
123
125
  */
124
- function isFullURL(url, ignoreProtocal = false) {
125
- return ignoreProtocal ? urlReg.test(url) : urlReg2.test(url);
126
+ function isFullURL(url, ignoreProtocol = false) {
127
+ return ignoreProtocol ? urlReg.test(url) : urlReg2.test(url);
126
128
  }
127
129
  /**
128
130
  * 检测文本是否有可能转化为 object 或 array,注意,检测通过也未必能被正确格式化为 json
@@ -300,8 +302,8 @@ function checkHttpAuthInfo(authString, option) {
300
302
  if (!authString) {
301
303
  return false;
302
304
  }
303
- const cfgs = Object.values(SigningDefinitions);
304
- for (const cfg of cfgs) {
305
+ const configs = Object.values(SigningDefinitions);
306
+ for (const cfg of configs) {
305
307
  if (authString.startsWith(cfg.prefix + " ")) {
306
308
  return cfg.verify(authString, option);
307
309
  }
@@ -390,7 +392,7 @@ async function responseError(res, statusCode, error, message) {
390
392
  }
391
393
  res.end();
392
394
  }
393
- async function repsonseJson(res, data, statusCode = 200) {
395
+ async function responseJson(res, data, statusCode = 200) {
394
396
  res.statusCode = statusCode;
395
397
  if (statusCode !== 204) {
396
398
  res.setHeader("Content-Type", "application/json");
@@ -1351,7 +1353,7 @@ function getNotificationMiddlewareForKoa(paths, config, handler) {
1351
1353
  }
1352
1354
 
1353
1355
  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;
1356
+ 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
1357
  }
1356
1358
  function isGMErrorResponse(data) {
1357
1359
  return isObject(data) && Object.keys(data).length === 2 && "error" in data && "message" in data;
@@ -1477,7 +1479,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
1477
1479
  }
1478
1480
  // 处理幂等key
1479
1481
  const currentRecord = {
1480
- noce: genNonceString(),
1482
+ nonce: genNonceString(),
1481
1483
  key: gmRequest.idempotency_key || "",
1482
1484
  id: gmRequest.request_id,
1483
1485
  cmd: gmRequest.command,
@@ -1497,8 +1499,14 @@ function getGMCommandHandler(config, handler, storeHelper) {
1497
1499
  responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, "parse old idempotency record failed");
1498
1500
  return;
1499
1501
  }
1502
+ // spell-checker:disable
1503
+ // fix spell error
1504
+ if (oldRecord && "noce" in oldRecord) {
1505
+ oldRecord.nonce = oldRecord.noce + "";
1506
+ }
1507
+ // spell-checker:enable
1500
1508
  // 检查是否为首次请求
1501
- const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.noce) === currentRecord.noce;
1509
+ const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.nonce) === currentRecord.nonce;
1502
1510
  if (!isFirstRequest && oldRecord) {
1503
1511
  if (!oldRecord.resp) {
1504
1512
  responseError(res, error2httpStatus["idempotency_conflict" /* GMError.IdempotencyConflict */], "idempotency_conflict" /* GMError.IdempotencyConflict */, "previous request is not completed");
@@ -1517,7 +1525,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
1517
1525
  }
1518
1526
  // 所有错误检查完毕,通知游戏处理函数
1519
1527
  try {
1520
- const result = oldResponse || (await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.idempotency_key || "", gmRequest.version));
1528
+ const result = oldResponse || (await handler(gmRequest.command, gmRequest.args, gmRequest.request_id, gmRequest.idempotency_key || "", gmRequest.version, gmRequest.origin || ""));
1521
1529
  // 记录handler返回的结果以备下次检查使用,setXX出现错误也不再中断请求
1522
1530
  if (storeHelper && gmRequest.idempotency_key) {
1523
1531
  currentRecord.resp = result;
@@ -1544,7 +1552,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
1544
1552
  if (!result) {
1545
1553
  console.warn(`gm command "${gmRequest.command}" should give back some data`);
1546
1554
  }
1547
- repsonseJson(res, result != null ? result : {});
1555
+ responseJson(res, result != null ? result : {});
1548
1556
  } catch (e) {
1549
1557
  console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
1550
1558
  responseError(res, 500 /* HttpStatus.InternalServerError */, "internal_error" /* GMError.InternalError */, e instanceof Error ? e.message : String(e));
@@ -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, ignoreProtocal?: boolean): boolean;
26
+ export declare function isFullURL(url: string, ignoreProtocol?: boolean): boolean;
27
27
  /**
28
28
  * 检测文本是否有可能转化为 object 或 array,注意,检测通过也未必能被正确格式化为 json
29
29
  */
@@ -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 repsonseJson(res: ServerResponse, data: unknown, statusCode?: number): Promise<void>;
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.0",
3
+ "version": "1.4.1",
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
+ }