@seayoo-io/combo-sdk-node 0.1.6 → 0.1.7

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/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.1.6";
4
+ export declare const SDKVersion = "0.1.7";
5
5
  /**
6
6
  * 世游支持的服务端点列表
7
7
  */
package/dist/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
- var node_os = require('node:os');
2
- var http = require('node:http');
3
- var https = require('node:https');
1
+ var os = require('os');
2
+ var http = require('http');
3
+ var https = require('https');
4
4
  var crypto = require('crypto');
5
5
  var jsonwebtoken = require('jsonwebtoken');
6
6
 
@@ -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.1.6";
15
+ const SDKVersion = "0.1.7";
16
16
  /**
17
17
  * 世游支持的服务端点列表
18
18
  */
@@ -57,7 +57,7 @@ exports.IdP = void 0;
57
57
  })(exports.IdP || (exports.IdP = {}));
58
58
 
59
59
  function getUserAgent(game) {
60
- return `${SDKName}/${SDKVersion} game/${game} node/${process.version} os/${node_os.platform()} arch/${node_os.arch()}`;
60
+ return `${SDKName}/${SDKVersion} game/${game} node/${process.version} os/${os.platform()} arch/${os.arch()}`;
61
61
  }
62
62
 
63
63
  const urlReg = /^(?:https?:)?\/\/.+/i;
@@ -121,7 +121,24 @@ function getFullURL(url, baseURL = "") {
121
121
  * @param url 要检查的 url,如果是自适应协议 //... 则会添加当前页面的协议信息,否则添加域名和协议信息
122
122
  */
123
123
  function fillUrl(url) {
124
- return isFullURL(url, true) ? url.startsWith("http") ? url : location.protocol + url : location.origin + "/" + url.replace(/^\/+/, "");
124
+ if (isFullURL(url, true)) {
125
+ if (url.startsWith("http")) {
126
+ return url;
127
+ }
128
+ return ("location" in globalThis ? location.protocol : "https:") + url;
129
+ }
130
+ return ("location" in globalThis ? location.origin + "/" : "http://127.0.0.1/") + url.replace(/^\/+/, "");
131
+ }
132
+ /**
133
+ * 简版 Object.fromEntries
134
+ */
135
+ function fromEntries(kv) {
136
+ return kv.reduce((result, [key, value]) => {
137
+ if (key) {
138
+ result[key] = value || "";
139
+ }
140
+ return result;
141
+ }, {});
125
142
  }
126
143
  function parseJSON(str, typeGuard) {
127
144
  try {
@@ -287,7 +304,7 @@ function parseRawAuthHeader(authString, prefix) {
287
304
  if (!authString || !authString.startsWith(prefix + " ")) {
288
305
  return null;
289
306
  }
290
- return Object.fromEntries(authString.substring(prefix.length + 1).split(",").map(kvs => {
307
+ return fromEntries(authString.substring(prefix.length + 1).split(",").map(kvs => {
291
308
  const kv = kvs.split("=");
292
309
  if (kv.length !== 2) {
293
310
  return null;
@@ -534,7 +551,7 @@ function handleResponse(res, url, config, options) {
534
551
  const result = {
535
552
  ...analyseResponse(res.status, res.statusText, res.body, config.get("responseRule"), options == null ? void 0 : options.responseRule),
536
553
  status: res.status,
537
- headers: Object.fromEntries(Object.entries(res.headers || {}).map(([key, value]) => [key.toLowerCase(), value]))
554
+ headers: fromEntries(Object.entries(res.headers || {}).map(([key, value]) => [key.toLowerCase(), value]))
538
555
  };
539
556
  // 全局处理响应结果 (浅复制,防止被修改,同时保留修改 result.data 的可能性)
540
557
  (_config$get2 = config.get("responseHandler")) == null || _config$get2({
@@ -655,7 +672,7 @@ const coreNodeRequest = async function (url, config, options) {
655
672
  }
656
673
  const isHeadRequest = opt.method === "HEAD";
657
674
  return new Promise(resolve => {
658
- const req = client.request(iURL, {
675
+ const req = client.request(iURL.toString(), {
659
676
  headers: opt.headers,
660
677
  method: opt.method,
661
678
  timeout: opt.timeout > 0 ? opt.timeout : undefined
@@ -663,7 +680,7 @@ const coreNodeRequest = async function (url, config, options) {
663
680
  const resbody = [];
664
681
  res.on("data", chunk => resbody.push(chunk));
665
682
  res.on("end", () => {
666
- const headers = Object.fromEntries(Object.entries(res.headers).map(([key, value]) => {
683
+ const headers = fromEntries(Object.entries(res.headers).map(([key, value]) => {
667
684
  return [key.toLowerCase(), Array.isArray(value) ? value.join(",") : value];
668
685
  }));
669
686
  resolve({
@@ -1155,7 +1172,7 @@ function isNotificationRequestBody(data) {
1155
1172
  *
1156
1173
  * 另有 express 和 koa2 版本的处理函数可以使用。
1157
1174
  *
1158
- * 由于签名需要原始请求的内容,如果 request 在函数执行之前已经被读取,则需要手工传入请求的 rawBody 字符串以用以签名
1175
+ * 由于签名需要原始请求的内容,如果 request 在函数执行之前已经被读取,则需要手工传入请求的 rawBody 字符串以核验签名
1159
1176
  */
1160
1177
  function getNotificationHandler(config, handler) {
1161
1178
  verifyConfig(config);
package/dist/index.js CHANGED
@@ -1,13 +1,13 @@
1
- import { platform, arch } from 'node:os';
2
- import http from 'node:http';
3
- import https from 'node:https';
1
+ import { platform, arch } from 'os';
2
+ import http from 'http';
3
+ import https from 'https';
4
4
  import { createHash, createHmac } from 'crypto';
5
5
  import { verify } from 'jsonwebtoken';
6
6
 
7
7
  /** SDK 名称 */
8
8
  const SDKName = "combo-sdk-node";
9
9
  /** SDK 版本,在编译前会被动态替换 */
10
- const SDKVersion = "0.1.6";
10
+ const SDKVersion = "0.1.7";
11
11
  /**
12
12
  * 世游支持的服务端点列表
13
13
  */
@@ -116,7 +116,24 @@ function getFullURL(url, baseURL = "") {
116
116
  * @param url 要检查的 url,如果是自适应协议 //... 则会添加当前页面的协议信息,否则添加域名和协议信息
117
117
  */
118
118
  function fillUrl(url) {
119
- return isFullURL(url, true) ? url.startsWith("http") ? url : location.protocol + url : location.origin + "/" + url.replace(/^\/+/, "");
119
+ if (isFullURL(url, true)) {
120
+ if (url.startsWith("http")) {
121
+ return url;
122
+ }
123
+ return ("location" in globalThis ? location.protocol : "https:") + url;
124
+ }
125
+ return ("location" in globalThis ? location.origin + "/" : "http://127.0.0.1/") + url.replace(/^\/+/, "");
126
+ }
127
+ /**
128
+ * 简版 Object.fromEntries
129
+ */
130
+ function fromEntries(kv) {
131
+ return kv.reduce((result, [key, value]) => {
132
+ if (key) {
133
+ result[key] = value || "";
134
+ }
135
+ return result;
136
+ }, {});
120
137
  }
121
138
  function parseJSON(str, typeGuard) {
122
139
  try {
@@ -282,7 +299,7 @@ function parseRawAuthHeader(authString, prefix) {
282
299
  if (!authString || !authString.startsWith(prefix + " ")) {
283
300
  return null;
284
301
  }
285
- return Object.fromEntries(authString.substring(prefix.length + 1).split(",").map(kvs => {
302
+ return fromEntries(authString.substring(prefix.length + 1).split(",").map(kvs => {
286
303
  const kv = kvs.split("=");
287
304
  if (kv.length !== 2) {
288
305
  return null;
@@ -529,7 +546,7 @@ function handleResponse(res, url, config, options) {
529
546
  const result = {
530
547
  ...analyseResponse(res.status, res.statusText, res.body, config.get("responseRule"), options == null ? void 0 : options.responseRule),
531
548
  status: res.status,
532
- headers: Object.fromEntries(Object.entries(res.headers || {}).map(([key, value]) => [key.toLowerCase(), value]))
549
+ headers: fromEntries(Object.entries(res.headers || {}).map(([key, value]) => [key.toLowerCase(), value]))
533
550
  };
534
551
  // 全局处理响应结果 (浅复制,防止被修改,同时保留修改 result.data 的可能性)
535
552
  (_config$get2 = config.get("responseHandler")) == null || _config$get2({
@@ -650,7 +667,7 @@ const coreNodeRequest = async function (url, config, options) {
650
667
  }
651
668
  const isHeadRequest = opt.method === "HEAD";
652
669
  return new Promise(resolve => {
653
- const req = client.request(iURL, {
670
+ const req = client.request(iURL.toString(), {
654
671
  headers: opt.headers,
655
672
  method: opt.method,
656
673
  timeout: opt.timeout > 0 ? opt.timeout : undefined
@@ -658,7 +675,7 @@ const coreNodeRequest = async function (url, config, options) {
658
675
  const resbody = [];
659
676
  res.on("data", chunk => resbody.push(chunk));
660
677
  res.on("end", () => {
661
- const headers = Object.fromEntries(Object.entries(res.headers).map(([key, value]) => {
678
+ const headers = fromEntries(Object.entries(res.headers).map(([key, value]) => {
662
679
  return [key.toLowerCase(), Array.isArray(value) ? value.join(",") : value];
663
680
  }));
664
681
  resolve({
@@ -1150,7 +1167,7 @@ function isNotificationRequestBody(data) {
1150
1167
  *
1151
1168
  * 另有 express 和 koa2 版本的处理函数可以使用。
1152
1169
  *
1153
- * 由于签名需要原始请求的内容,如果 request 在函数执行之前已经被读取,则需要手工传入请求的 rawBody 字符串以用以签名
1170
+ * 由于签名需要原始请求的内容,如果 request 在函数执行之前已经被读取,则需要手工传入请求的 rawBody 字符串以核验签名
1154
1171
  */
1155
1172
  function getNotificationHandler(config, handler) {
1156
1173
  verifyConfig(config);
@@ -1,14 +1,14 @@
1
1
  import { type SDKBaseConfig } from "../utils";
2
2
  import { type NotificationHandler } from "./types";
3
3
  import type { ParameterizedContext, Next } from "koa";
4
- import type { IncomingMessage, ServerResponse } from "node:http";
4
+ import type { IncomingMessage, ServerResponse } from "http";
5
5
  import type { Request as ExpressRequest, Response as ExpressResponse, NextFunction as ExpressNextFunction } from "express";
6
6
  /**
7
7
  * 获取一个处理函数用于接收 Seayoo Server 推送过来的消息通知
8
8
  *
9
9
  * 另有 express 和 koa2 版本的处理函数可以使用。
10
10
  *
11
- * 由于签名需要原始请求的内容,如果 request 在函数执行之前已经被读取,则需要手工传入请求的 rawBody 字符串以用以签名
11
+ * 由于签名需要原始请求的内容,如果 request 在函数执行之前已经被读取,则需要手工传入请求的 rawBody 字符串以核验签名
12
12
  */
13
13
  export declare function getNotificationHandler(config: SDKBaseConfig, handler: NotificationHandler): (req: IncomingMessage, res: ServerResponse, rawBodyString?: string) => Promise<void>;
14
14
  /**
@@ -22,13 +22,15 @@ export declare function getNotificationHandler(config: SDKBaseConfig, handler: N
22
22
  *
23
23
  * https://cloud.tencent.com/developer/ask/sof/110062928
24
24
  */
25
- export declare function getNotificationHandlerForExpress(config: SDKBaseConfig, handler: NotificationHandler): (req: ExpressRequest, res: ExpressResponse) => Promise<void>;
25
+ export declare function getNotificationHandlerForExpress(config: SDKBaseConfig, handler: NotificationHandler): (req: ExpressRequest & {
26
+ rawBody?: string;
27
+ }, res: ExpressResponse) => Promise<void>;
26
28
  /**
27
29
  * 获取基于 express 的中间件用于接收 Seayoo Server 推送过来的消息通知
28
30
  *
29
31
  * 需要先于 body-parse 等类似插件加载
30
32
  */
31
- export declare function getNotificationMiddlewareForExpress(paths: string | string[], config: SDKBaseConfig, handler: NotificationHandler): (req: ExpressRequest, res: ExpressResponse, next: ExpressNextFunction) => Promise<void>;
33
+ export declare function getNotificationMiddlewareForExpress(paths: string | string[], config: SDKBaseConfig, handler: NotificationHandler): (req: ExpressRequest, res: ServerResponse, next: ExpressNextFunction) => Promise<void>;
32
34
  /**
33
35
  * 获取基于 koa2 的处理函数用于接收 Seayoo Server 推送过来的消息通知
34
36
  *
@@ -36,10 +38,12 @@ export declare function getNotificationMiddlewareForExpress(paths: string | stri
36
38
  *
37
39
  * 如果 koa 使用全局的 koa-body 或类似插件,将需要做额外的处理以获取原始请求的内容 rawBody 以用以计算签名
38
40
  */
39
- export declare function getNotificationHandlerForKoa(config: SDKBaseConfig, handler: NotificationHandler): (ctx: ParameterizedContext) => Promise<void>;
41
+ export declare function getNotificationHandlerForKoa(config: SDKBaseConfig, handler: NotificationHandler): (ctx: Pick<ParameterizedContext, "req" | "res"> & {
42
+ rawBody?: string;
43
+ }) => Promise<void>;
40
44
  /**
41
45
  * 获取基于 koa2 的中间件用于接收 Seayoo Server 推送过来的消息通知
42
46
  *
43
47
  * 需要先于 koa-body 等类似插件加载
44
48
  */
45
- export declare function getNotificationMiddlewareForKoa(paths: string | string[], config: SDKBaseConfig, handler: NotificationHandler): (ctx: ParameterizedContext, next: Next) => Promise<void>;
49
+ export declare function getNotificationMiddlewareForKoa(paths: string | string[], config: SDKBaseConfig, handler: NotificationHandler): (ctx: Pick<ParameterizedContext, "method" | "path" | "req" | "res">, next: Next) => Promise<void>;
@@ -1,4 +1,4 @@
1
- import type { TypeGuardParam, IRequestOptions, IRequestGlobalConfig, NetRequestAgent, ResponseWithType, ResponseWithoutType } from "./type";
1
+ import type { TypeGuardParam, IRequestOptions, IRequestGlobalConfig, IResponseResult, NetRequestAgent } from "./type";
2
2
  type RequestBody = NonNullable<IRequestOptions["body"]>;
3
3
  /** 工具函数主类 */
4
4
  export declare class NetRequestHandler {
@@ -25,36 +25,36 @@ export declare class NetRequestHandler {
25
25
  /**
26
26
  * 发送一个 HEAD 请求,并且不处理响应 body
27
27
  */
28
- head(url: string, options?: IRequestOptions): ResponseWithoutType;
28
+ head(url: string, options?: IRequestOptions): Promise<IResponseResult>;
29
29
  /**
30
30
  * 发送一个 GET 请求,请求自带 500ms 缓冲控制以应对并发场景
31
31
  */
32
- get(url: string): ResponseWithoutType;
33
- get(url: string, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
34
- get<T>(url: string, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
32
+ get(url: string): Promise<IResponseResult>;
33
+ get(url: string, typeGard: null, options?: IRequestOptions): Promise<IResponseResult>;
34
+ get<T>(url: string, typeGard: TypeGuardParam<T>, options?: IRequestOptions): Promise<IResponseResult<T | null>>;
35
35
  /**
36
36
  * 发送一个 POST 请求,可选 typeGuard 用于检查数据类型
37
37
  */
38
- post(url: string, data: RequestBody): ResponseWithoutType;
39
- post(url: string, data: RequestBody, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
40
- post<T>(url: string, data: RequestBody, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
38
+ post(url: string, data: RequestBody): Promise<IResponseResult>;
39
+ post(url: string, data: RequestBody, typeGard: null, options?: IRequestOptions): Promise<IResponseResult>;
40
+ post<T>(url: string, data: RequestBody, typeGard: TypeGuardParam<T>, options?: IRequestOptions): Promise<IResponseResult<T | null>>;
41
41
  /**
42
42
  * 发送一个 DELETE 请求,可选 typeGuard 用于检查数据类型
43
43
  */
44
- del(url: string): ResponseWithoutType;
45
- del(url: string, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
46
- del<T>(url: string, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
44
+ del(url: string): Promise<IResponseResult>;
45
+ del(url: string, typeGard: null, options?: IRequestOptions): Promise<IResponseResult>;
46
+ del<T>(url: string, typeGard: TypeGuardParam<T>, options?: IRequestOptions): Promise<IResponseResult<T | null>>;
47
47
  /**
48
48
  * 发送一个 PUT 请求,可选 typeGuard 用于检查数据类型
49
49
  */
50
- put(url: string, data: RequestBody): ResponseWithoutType;
51
- put(url: string, data: RequestBody, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
52
- put<T>(url: string, data: RequestBody, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
50
+ put(url: string, data: RequestBody): Promise<IResponseResult>;
51
+ put(url: string, data: RequestBody, typeGard: null, options?: IRequestOptions): Promise<IResponseResult>;
52
+ put<T>(url: string, data: RequestBody, typeGard: TypeGuardParam<T>, options?: IRequestOptions): Promise<IResponseResult<T | null>>;
53
53
  /**
54
54
  * 发送一个 PATCH 请求,可选 typeGuard 用于检查数据类型
55
55
  */
56
- patch(url: string, data: RequestBody): ResponseWithoutType;
57
- patch(url: string, data: RequestBody, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
58
- patch<T>(url: string, data: RequestBody, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
56
+ patch(url: string, data: RequestBody): Promise<IResponseResult>;
57
+ patch(url: string, data: RequestBody, typeGard: null, options?: IRequestOptions): Promise<IResponseResult>;
58
+ patch<T>(url: string, data: RequestBody, typeGard: TypeGuardParam<T>, options?: IRequestOptions): Promise<IResponseResult<T | null>>;
59
59
  }
60
60
  export {};
@@ -1,3 +1,3 @@
1
- import { NetRequestCoreFn, IRequestOptions } from "./type";
1
+ import { NetRequestCoreFn, IRequestOptions, IRequestBaseResponse } from "./type";
2
2
  import type { RequestGlobalConfig } from "./config";
3
- export declare function retryRequest(agent: NetRequestCoreFn, url: string, config: RequestGlobalConfig, options?: IRequestOptions, _try?: number): ReturnType<NetRequestCoreFn>;
3
+ export declare function retryRequest(agent: NetRequestCoreFn, url: string, config: RequestGlobalConfig, options?: IRequestOptions, _try?: number): Promise<IRequestBaseResponse>;
@@ -34,6 +34,10 @@ export declare function isJsonLike(text: string): boolean;
34
34
  * @param baseURL 可选 baseURL
35
35
  */
36
36
  export declare function getFullURL(url: string, baseURL?: string): string;
37
+ /**
38
+ * 简版 Object.fromEntries
39
+ */
40
+ export declare function fromEntries(kv: [string, string | undefined][]): Record<string, string>;
37
41
  /**
38
42
  * JSON.parse的扩展,格式化错误则返回 null,第二个参数为可选 typeGuard
39
43
  */
@@ -1,4 +1,4 @@
1
1
  /// <reference types="node" />
2
- import type { IncomingMessage, ServerResponse } from "node:http";
2
+ 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>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seayoo-io/combo-sdk-node",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Combo SDK for Node.js",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -27,7 +27,7 @@
27
27
  "pub": "pnpm type-check && pnpm lint:fix && vitest run && pnpm build && npm publish"
28
28
  },
29
29
  "engines": {
30
- "node": ">=14.0.0"
30
+ "node": ">=12"
31
31
  },
32
32
  "simple-git-hooks": {
33
33
  "pre-commit": "pnpm lint && pnpm type-check"
@@ -44,19 +44,20 @@
44
44
  "@types/express": "^4.17.21",
45
45
  "@types/jsonwebtoken": "^9.0.5",
46
46
  "@types/koa": "^2.13.12",
47
- "@types/node": "^20.10.6",
48
- "@typescript-eslint/eslint-plugin": "^6.17.0",
49
- "@typescript-eslint/parser": "^6.17.0",
47
+ "@types/node": "12.0.0",
48
+ "@typescript-eslint/eslint-plugin": "^6.18.1",
49
+ "@typescript-eslint/parser": "^6.18.1",
50
50
  "eslint": "^8.56.0",
51
51
  "eslint-config-prettier": "^9.1.0",
52
52
  "eslint-plugin-prettier": "^5.1.2",
53
53
  "lint-staged": "^15.2.0",
54
54
  "microbundle": "^0.15.1",
55
- "msw": "^2.0.11",
55
+ "msw": "^2.0.12",
56
56
  "rimraf": "^5.0.5",
57
57
  "simple-git-hooks": "^2.9.0",
58
- "typescript": "^5.2.2",
59
- "vitest": "^1.1.2"
58
+ "typescript": "5.2.2",
59
+ "vite": "^3.2.7",
60
+ "vitest": "^0.34.6"
60
61
  },
61
62
  "dependencies": {
62
63
  "jsonwebtoken": "^9.0.2"