@x-9lab/xlab 1.6.1 → 2.0.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.
Files changed (72) hide show
  1. package/@types/cluster.d.ts +15 -3
  2. package/@types/components/common/index.d.ts +1 -2
  3. package/@types/components/cron/index.d.ts +4 -4
  4. package/@types/components/log/index.d.ts +37 -2
  5. package/@types/config/index.d.ts +9 -5
  6. package/@types/custom.d.ts +25 -3
  7. package/@types/dot-file.d.ts +6 -0
  8. package/@types/global.d.ts +44 -56
  9. package/@types/index.d.ts +18 -0
  10. package/@types/init-env.d.ts +3 -1
  11. package/@types/middleware/handle-pre-dir.d.ts +1 -5
  12. package/@types/middleware/request-filter.d.ts +0 -1
  13. package/@types/middleware/service-mark.d.ts +1 -6
  14. package/@types/router.d.ts +25 -0
  15. package/@types/server.d.ts +28 -6
  16. package/MIGRATION.md +182 -0
  17. package/README.md +105 -86
  18. package/dist/@config/config.dev.js +7 -3
  19. package/dist/@config/config.js +7 -4
  20. package/dist/@config/config.sand.js +7 -3
  21. package/dist/bin/watch.js +36 -16
  22. package/dist/cluster.js +121 -44
  23. package/dist/components/assets/index.js +9 -5
  24. package/dist/components/cluster/index.js +17 -4
  25. package/dist/components/common/index.js +65 -45
  26. package/dist/components/cron/index.js +50 -27
  27. package/dist/components/header/index.js +32 -17
  28. package/dist/components/header/time.js +6 -2
  29. package/dist/components/index.js +1 -1
  30. package/dist/components/log/index.js +91 -87
  31. package/dist/components/mime/index.js +6 -2
  32. package/dist/components/return-code/index.js +33 -18
  33. package/dist/components/uuid/index.js +22 -13
  34. package/dist/config/getEnv.js +9 -5
  35. package/dist/config/index.js +98 -66
  36. package/dist/config/process-custom-config-files.js +26 -22
  37. package/dist/config/process-def-config-file.js +15 -11
  38. package/dist/custom.js +77 -28
  39. package/dist/default-x-config.js +9 -5
  40. package/dist/dot-file.js +42 -17
  41. package/dist/global.js +37 -31
  42. package/dist/index.js +100 -0
  43. package/dist/init-env.js +17 -9
  44. package/dist/middleware/@bin/html-filter.js +19 -7
  45. package/dist/middleware/bad-request.js +12 -8
  46. package/dist/middleware/compress.js +11 -7
  47. package/dist/middleware/cors.js +20 -7
  48. package/dist/middleware/fresh-filter.js +13 -7
  49. package/dist/middleware/handle-pre-dir.js +22 -17
  50. package/dist/middleware/request-filter.js +39 -25
  51. package/dist/middleware/service-mark.js +22 -25
  52. package/dist/middlewares.js +22 -20
  53. package/dist/router.js +174 -141
  54. package/dist/server.js +202 -93
  55. package/package.json +25 -29
  56. package/@types/business/utils/cookie/1.1.0/clean/index.d.ts +0 -3
  57. package/@types/business/utils/cookie/1.1.0/clean/js/index.d.ts +0 -2
  58. package/@types/components/cache/index.d.ts +0 -2
  59. package/@types/components/cache/lru.d.ts +0 -21
  60. package/@types/components/html-processor/index.d.ts +0 -9
  61. package/@types/components/injection/index.d.ts +0 -25
  62. package/@types/components/js-processor/index.d.ts +0 -5
  63. package/@types/components/md5/index.d.ts +0 -2
  64. package/@types/components/platform/index.d.ts +0 -21
  65. package/@types/components/proxy/index.d.ts +0 -26
  66. package/@types/components/querystring/index.d.ts +0 -25
  67. package/@types/components/redirect/index.d.ts +0 -11
  68. package/@types/components/request/helper.d.ts +0 -3
  69. package/@types/components/request/index.d.ts +0 -51
  70. package/@types/components/request/resolve-uri.d.ts +0 -6
  71. package/@types/components/version/index.d.ts +0 -26
  72. package/@types/env.d.ts +0 -1
package/dist/server.js CHANGED
@@ -2,120 +2,229 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- exports.default = exports.exit = exports.boot = exports.app = void 0;
6
- require("./global");
7
- require("./init-env");
8
- var _log = require("./components/log");
9
- var _cron = require("./components/cron");
10
- var _defaultXConfig = _interopRequireDefault(require("./default-x-config"));
11
- var _dotFile = require("./dot-file");
12
- var _koaStatic = _interopRequireDefault(require("koa-static"));
13
- var _koaRouter = _interopRequireDefault(require("koa-router"));
14
- var _path = _interopRequireDefault(require("path"));
15
- var _koa = _interopRequireDefault(require("koa"));
16
- var _config = _interopRequireWildcard(require("./config"));
17
- var _custom = _interopRequireDefault(require("./custom"));
18
- var _router = _interopRequireDefault(require("./router"));
19
- var _cluster = _interopRequireDefault(require("./cluster"));
20
- var _middlewares = _interopRequireDefault(require("./middlewares"));
21
- var _badRequest = _interopRequireDefault(require("./middleware/bad-request"));
22
- function _interopRequireDefault(obj) {
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: Object.getOwnPropertyDescriptor(all, name).get
9
+ });
10
+ }
11
+ _export(exports, {
12
+ get app () {
13
+ return app;
14
+ },
15
+ get boot () {
16
+ return boot;
17
+ },
18
+ get default () {
19
+ return _default;
20
+ },
21
+ get exit () {
22
+ return exit;
23
+ },
24
+ get getApp () {
25
+ return getApp;
26
+ },
27
+ get shutdown () {
28
+ return shutdown;
29
+ }
30
+ });
31
+ const _log = require("./components/log");
32
+ const _cron = require("./components/cron");
33
+ const _defaultxconfig = /*#__PURE__*/ _interop_require_default(require("./default-x-config"));
34
+ const _dotfile = require("./dot-file");
35
+ const _config = /*#__PURE__*/ _interop_require_wildcard(require("./config"));
36
+ const _custom = require("./custom");
37
+ const _initenv = /*#__PURE__*/ _interop_require_default(require("./init-env"));
38
+ const _cluster = /*#__PURE__*/ _interop_require_wildcard(require("./cluster"));
39
+ const _router = /*#__PURE__*/ _interop_require_default(require("./router"));
40
+ const _middlewares = /*#__PURE__*/ _interop_require_default(require("./middlewares"));
41
+ const _badrequest = /*#__PURE__*/ _interop_require_default(require("./middleware/bad-request"));
42
+ const _utils = require("@x-drive/utils");
43
+ const _koastatic = /*#__PURE__*/ _interop_require_default(require("koa-static"));
44
+ const _koarouter = /*#__PURE__*/ _interop_require_default(require("koa-router"));
45
+ const _path = /*#__PURE__*/ _interop_require_default(require("path"));
46
+ const _koa = /*#__PURE__*/ _interop_require_default(require("koa"));
47
+ function _interop_require_default(obj) {
23
48
  return obj && obj.__esModule ? obj : {
24
49
  default: obj
25
50
  };
26
51
  }
27
- function _interopRequireWildcard(obj) {
28
- if (obj && obj.__esModule) {
29
- return obj;
30
- } else {
31
- var newObj = {};
32
- if (obj != null) {
33
- for(var key in obj){
34
- if (Object.prototype.hasOwnProperty.call(obj, key)) {
35
- var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {};
36
- if (desc.get || desc.set) {
37
- Object.defineProperty(newObj, key, desc);
38
- } else {
39
- newObj[key] = obj[key];
40
- }
41
- }
42
- }
52
+ function _getRequireWildcardCache(nodeInterop) {
53
+ if (typeof WeakMap !== "function") return null;
54
+ var cacheBabelInterop = new WeakMap();
55
+ var cacheNodeInterop = new WeakMap();
56
+ return (_getRequireWildcardCache = function(nodeInterop) {
57
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
58
+ })(nodeInterop);
59
+ }
60
+ function _interop_require_wildcard(obj, nodeInterop) {
61
+ if (!nodeInterop && obj && obj.__esModule) return obj;
62
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") return {
63
+ default: obj
64
+ };
65
+ var cache = _getRequireWildcardCache(nodeInterop);
66
+ if (cache && cache.has(obj)) return cache.get(obj);
67
+ var newObj = {
68
+ __proto__: null
69
+ };
70
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
71
+ for(var key in obj){
72
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
73
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
74
+ if (desc && (desc.get || desc.set)) Object.defineProperty(newObj, key, desc);
75
+ else newObj[key] = obj[key];
43
76
  }
44
- newObj.default = obj;
45
- return newObj;
46
77
  }
78
+ newObj.default = obj;
79
+ if (cache) cache.set(obj, newObj);
80
+ return newObj;
47
81
  }
48
- process.env.TZ = _config.default.timezone;
49
82
  const logger = _log.globalLog.getLogger("system");
50
- (0, _dotFile).processDotFile(_config.default.env, logger);
51
- // 只在 master 上输出的日志
52
- global.masterLog = _config.log;
53
- // 服务实例
54
- const app = new _koa.default();
55
- /**
56
- * 获取应用实例对象
57
- * @deprecated v1.7.0 后将弃用,请直接从模块中引用
58
- * @return 实例对象
59
- */ global.getApp = function() {
60
- return app;
61
- };
83
+ /**服务实例 */ const app = new _koa.default();
62
84
  // 不输出X-Powered-By
63
85
  // @ts-ignore
64
86
  app.poweredBy = false;
65
- if (_config.default.custom) {
66
- // 自定义初始化业务
67
- (0, _custom).default();
87
+ /**
88
+ * 获取应用实例对象
89
+ * @return 实例对象
90
+ */ function getApp() {
91
+ return app;
68
92
  }
69
- // 加载中间件
70
- (0, _middlewares).default(app);
71
- const koaRouter = new _koaRouter.default();
72
- // 路由中间件
73
- app.use(koaRouter.routes());
74
- // 具体路由
75
- (0, _router).default(koaRouter);
76
- // 静态文件服务
77
- var staticServeOpts = {};
78
- if (_config.default.isProd) {
79
- staticServeOpts.maxage = _config.default.staticMaxage;
93
+ /**http 服务实例, master fork 模式下为空 */ var server = null;
94
+ /**是否已启动 */ var booted = false;
95
+ /**是否正在退出 */ var shuttingDown = false;
96
+ /**
97
+ * 关闭 http 服务, 等待在途请求结束, 超时强制返回
98
+ * @param timeout 超时时间(毫秒)
99
+ */ function closeServer(timeout) {
100
+ return new Promise(function(resolve) {
101
+ const timer = setTimeout(function() {
102
+ logger.warn(`等待在途请求结束超时(${timeout}ms), 强制关闭`);
103
+ resolve();
104
+ }, timeout);
105
+ server.close(function() {
106
+ clearTimeout(timer);
107
+ resolve();
108
+ });
109
+ // 主动断开空闲的 keep-alive 连接, 避免拖住 close 回调
110
+ if ((0, _utils.isExecutable)(server.closeIdleConnections)) {
111
+ server.closeIdleConnections();
112
+ }
113
+ });
80
114
  }
81
- staticServeOpts.setHeaders = function(res, path) {
82
- if (_config.default.isProd && _config.default.staticHtmlFileMaxage && path.endsWith(".html")) {
83
- res.setHeader("Cache-Control", `max-age=${_config.default.staticHtmlFileMaxage}`);
115
+ /**
116
+ * 退出服务
117
+
118
+ * 停止定时任务 -> 通知 worker 退出(master) -> 等待在途请求 -> 执行 shutdown 钩子 -> 退出进程
119
+ * @param code 进程退出码
120
+ */ async function shutdown(code = 0) {
121
+ if (shuttingDown) {
122
+ return;
84
123
  }
85
- if (_config.default.staticCros) {
86
- const { origin } = res.req.headers;
87
- if (origin) {
88
- res.setHeader("X-Cros-Static", origin);
89
- res.setHeader("Access-Control-Allow-Origin", origin);
90
- res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
124
+ shuttingDown = true;
125
+ const config = (0, _config.get)();
126
+ (0, _config.log)("system", `Shutting down (pid ${process.pid}) ...`);
127
+ try {
128
+ (0, _cron.kill)();
129
+ if (config.isMaster && config.workers) {
130
+ await (0, _cluster.shutdownWorkers)(config.shutdownTimeout);
91
131
  }
132
+ if (server) {
133
+ await closeServer(config.shutdownTimeout || 10 * 1000);
134
+ }
135
+ await (0, _custom.runShutdown)();
136
+ } catch (e) {
137
+ logger.error("Shutdown error:", e);
92
138
  }
93
- };
94
- app.use((0, _koaStatic).default(_path.default.resolve(process.cwd(), _config.default.root), staticServeOpts));
95
- // 异常状态处理中间件
96
- app.use(_badRequest.default);
97
- if (_config.default.enableCron) {
98
- if (_config.default.isMaster || _config.default.enableWorkerCron) {
99
- // 定时任务
100
- (0, _cron).on(_path.default.resolve(process.cwd(), _defaultXConfig.default.businessDir, "cron"));
101
- }
139
+ process.exit(code);
140
+ }
141
+ /**
142
+ * 退出服务
143
+ * @deprecated 请使用 shutdown
144
+ */ function exit() {
145
+ shutdown();
102
146
  }
103
- /**启动服务 */ function boot() {
104
- (0, _cluster).default();
105
- process.on("uncaughtException", (err)=>{
147
+ /**
148
+ * 启动服务
149
+
150
+ * 按固定顺序执行启动阶段:
151
+ * initEnv -> loadConfig -> dotFile -> custom(setup) -> middleware
152
+ * -> router -> static/badRequest -> cron -> listen -> custom(ready)
153
+ */ async function boot() {
154
+ if (booted) {
155
+ return;
156
+ }
157
+ booted = true;
158
+ // 1. 业务环境预处理(@server/@env)
159
+ (0, _initenv.default)();
160
+ // 2. 配置装载
161
+ const config = (0, _config.default)();
162
+ process.env.TZ = config.timezone;
163
+ // 3. .xlab 环境数据
164
+ (0, _dotfile.processDotFile)(config.env, logger);
165
+ // 4. 自定义初始化(setup 钩子)
166
+ if (config.custom) {
167
+ await (0, _custom.loadCustom)();
168
+ }
169
+ // 5. 中间件
170
+ (0, _middlewares.default)(app);
171
+ // 6. 业务路由
172
+ const koaRouter = new _koarouter.default();
173
+ app.use(koaRouter.routes());
174
+ (0, _router.default)(koaRouter);
175
+ // 7. 静态文件服务
176
+ var staticServeOpts = {};
177
+ if (config.isProd) {
178
+ staticServeOpts.maxage = config.staticMaxage;
179
+ }
180
+ staticServeOpts.setHeaders = function(res, filePath) {
181
+ if (config.isProd && config.staticHtmlFileMaxage && filePath.endsWith(".html")) {
182
+ res.setHeader("Cache-Control", `max-age=${config.staticHtmlFileMaxage}`);
183
+ }
184
+ if (config.staticCros) {
185
+ const { origin } = res.req.headers;
186
+ if (origin) {
187
+ res.setHeader("X-Cros-Static", origin);
188
+ res.setHeader("Access-Control-Allow-Origin", origin);
189
+ res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
190
+ }
191
+ }
192
+ };
193
+ app.use((0, _koastatic.default)(_path.default.resolve(process.cwd(), config.root), staticServeOpts));
194
+ // 8. 异常状态处理中间件
195
+ app.use(_badrequest.default);
196
+ // 9. 定时任务
197
+ if (config.enableCron) {
198
+ if (config.isMaster || config.enableWorkerCron) {
199
+ (0, _cron.on)(_path.default.resolve(process.cwd(), _defaultxconfig.default.businessDir, "cron"));
200
+ }
201
+ }
202
+ // 10. 监听
203
+ server = (0, _cluster.default)(app);
204
+ // 11. 进程信号与异常处理
205
+ if (!config.isMaster) {
206
+ // master 异常退出(如被强杀)导致通道断开时, worker 自行优雅退出,
207
+ // 防止孤儿进程继续占用端口对外服务
208
+ process.on("disconnect", function() {
209
+ shutdown();
210
+ });
211
+ }
212
+ process.on("SIGTERM", function() {
213
+ shutdown();
214
+ }).on("SIGINT", function() {
215
+ shutdown();
216
+ }).on("uncaughtException", (err)=>{
106
217
  logger.error(`uncaughtException:${err.message}\n`, err.stack);
107
218
  }).on("unhandledRejection", (err)=>{
108
219
  logger.error(`unhandledRejection:${err.message}\n`, err.stack);
109
220
  });
221
+ // 12. ready 钩子
222
+ await (0, _custom.runReady)();
110
223
  }
111
- /**退出服务 */ function exit() {
112
- process.exit();
113
- }
114
- var _default = app;
115
- exports.default = _default;
224
+ const _default = app;
116
225
  if (require.main === module) {
117
- boot();
226
+ boot().catch(function(err) {
227
+ logger.error("Boot failed:", err);
228
+ process.exit(1);
229
+ });
118
230
  }
119
- exports.app = app;
120
- exports.boot = boot;
121
- exports.exit = exit;
package/package.json CHANGED
@@ -1,49 +1,45 @@
1
1
  {
2
2
  "name": "@x-9lab/xlab",
3
- "version": "1.6.1",
3
+ "version": "2.0.1",
4
4
  "description": "9lab 服务端模块",
5
- "versionDesc": "日志信息输出优化",
5
+ "versionDesc": "watch 退出信号级联与 worker 孤儿防护;移除 combo 中间件残留",
6
6
  "scripts": {
7
- "dev": "swc src -D ./src/bin --config-file .swcrc -d dist -w",
8
- "compile": "swc src -D ./src/bin --config-file .swcrc -d dist",
7
+ "dev": "swc src --copy-files --strip-leading-paths --config-file .swcrc -d dist -w",
8
+ "compile": "swc src --copy-files --strip-leading-paths --config-file .swcrc -d dist",
9
9
  "build": "yarn compile && yarn gen-declaration",
10
10
  "gen-declaration": "tsc --emitDeclarationOnly",
11
+ "test": "node --test",
11
12
  "changelog": "changelog"
12
13
  },
13
14
  "engines": {
14
- "node": ">10.0.0"
15
+ "node": ">=20.0.0"
15
16
  },
16
- "main": "./dist/server.js",
17
- "types": "./@types/global.d.ts",
17
+ "main": "./dist/index.js",
18
+ "types": "./@types/index.d.ts",
18
19
  "files": [
19
20
  "dist",
20
- "@types"
21
+ "@types",
22
+ "MIGRATION.md"
21
23
  ],
22
24
  "dependencies": {
23
- "koa": "3.1.2",
24
- "cron": "1.8.2",
25
- "log4js": "^1.1.1",
26
- "liftoff": "4.0.0",
27
- "minimist": "1.2.6",
28
- "koa-static": "5.0.0",
25
+ "@x-drive/utils": "1.1.32",
26
+ "koa": "3.2.1",
27
+ "koa-compress": "5.2.2",
29
28
  "koa-router": "14.0.0",
30
- "koa-compress": "5.2.1",
31
- "@x-drive/utils": "1.1.32"
29
+ "koa-static": "5.0.0",
30
+ "liftoff": "5.0.1",
31
+ "log4js": "^6.9.1",
32
+ "minimist": "1.2.8"
32
33
  },
33
34
  "devDependencies": {
34
- "tslib": "2.3.0",
35
- "colors": "1.4.0",
36
- "should": "11.2.1",
37
- "nodemon": "2.0.12",
38
- "chokidar": "3.5.3",
39
- "@swc/cli": "0.1.57",
40
- "typescript": "4.3.4",
41
- "@swc/core": "1.2.171",
42
- "@types/log4js": "2.3.5",
43
- "@types/node": "16.11.40",
44
- "@types/koa-static": "4.0.4",
35
+ "@swc/cli": "^0.7.7",
36
+ "@swc/core": "^1.13.2",
45
37
  "@types/koa-router": "7.4.9",
46
- "@x-drive/changelog": "1.1.6"
38
+ "@types/koa-static": "4.0.4",
39
+ "@types/node": "^20.19.0",
40
+ "@x-drive/changelog": "1.1.6",
41
+ "chokidar": "^3.6.0",
42
+ "typescript": "^5.9.2"
47
43
  },
48
44
  "bin": {
49
45
  "xlab": "dist/bin/xlab"
@@ -52,4 +48,4 @@
52
48
  "type": "git",
53
49
  "url": "git+https://github.com/x-9lab/xlab.git"
54
50
  }
55
- }
51
+ }
@@ -1,3 +0,0 @@
1
- import type Koa from "koa";
2
- declare function onClean(ctx: Koa.Context): void;
3
- export default onClean;
@@ -1,2 +0,0 @@
1
- declare function onJsClean(ctx: any): void;
2
- export default onJsClean;
@@ -1,2 +0,0 @@
1
- import { create } from "./lru";
2
- export { create as local };
@@ -1,21 +0,0 @@
1
- /**
2
- * 生成或获取一个缓存实例
3
- * @param cacheName 实例名称
4
- * @param conf 缓存配置,仅在生成的时候有效
5
- * @return 缓存实例
6
- */
7
- declare function create(cacheName: string, conf: Record<string, XLab.JsonValue>): any;
8
- export { create };
9
- /**
10
- * 获取一个缓存实例
11
- * @param name 缓存名称
12
- * @param conf 缓存配置
13
- */
14
- declare function get(name: string, conf?: Record<string, XLab.JsonValue>): any;
15
- export { get };
16
- /**
17
- * 获取当前缓存实例数量
18
- * @return 缓存数量
19
- */
20
- declare function len(): number;
21
- export { len };
@@ -1,9 +0,0 @@
1
- import type Koa from "koa";
2
- declare type RouteOption = Record<string, string | string[] | Function>;
3
- interface HtmlProcessorOptions {
4
- route?: RouteOption;
5
- }
6
- declare function htmlProcessor(pathname: string, options: HtmlProcessorOptions, context: Koa.Context): string;
7
- declare function init(): void;
8
- export { init };
9
- export { htmlProcessor };
@@ -1,25 +0,0 @@
1
- import type Koa from "koa";
2
- /**
3
- * 数据注入方法
4
- * @param html 待处理的字符串
5
- * @param req 请求对象
6
- * @return 处理完的字符串
7
- */
8
- declare function inject(html: string, req: Koa.Context): string;
9
- export { inject };
10
- /**
11
- * 添加一个字段到缓存对象
12
- * @param key 字段名
13
- * @param val 数据
14
- * @return 存储的数据
15
- */
16
- declare function add(key: string, val: XLab.JsonValue): any;
17
- export { add };
18
- /**
19
- * 修改一个字段
20
- * @param key 字段名
21
- * @param val 数据
22
- * @return 存储的数据
23
- */
24
- declare function modify(key: string, val: XLab.JsonValue): boolean;
25
- export { modify };
@@ -1,5 +0,0 @@
1
- import type Koa from "koa";
2
- declare function jsProcessor(pathname: string, context: Koa.Context): string;
3
- declare function init(): void;
4
- export { init };
5
- export { jsProcessor };
@@ -1,2 +0,0 @@
1
- declare function md5(str: string): string;
2
- export { md5 };
@@ -1,21 +0,0 @@
1
- interface IPlat {
2
- /**平台类型,mobile 或 desktop */
3
- is: string;
4
- /**是否是微信 */
5
- wechat?: boolean;
6
- /**是否是支付宝 */
7
- alipay?: boolean;
8
- /**是否是微博 */
9
- weibo?: boolean;
10
- /**是否是 Windows 系统 */
11
- win?: boolean;
12
- /**是否是 Mac 系统 */
13
- mac?: boolean;
14
- /**是否是 Linux 系统 */
15
- linux?: string;
16
- /**操作系统信息 */
17
- os?: string;
18
- }
19
- export type { IPlat };
20
- declare function check(ua: string): IPlat;
21
- export { check };
@@ -1,26 +0,0 @@
1
- import type Koa from "koa";
2
- /**代理配置项 */
3
- interface IProxyOption {
4
- /**代理目标 */
5
- target: string;
6
- /**代理域名 */
7
- host: string;
8
- }
9
- declare module "http" {
10
- interface IncomingMessage {
11
- /**代理类型 */
12
- proxyType: string;
13
- }
14
- }
15
- /**
16
- * 设置代理配置
17
- * @param type 代理配置名称
18
- * @param options 配置信息
19
- */
20
- declare function set(type: string, options: IProxyOption | string): IProxyOption;
21
- export { set };
22
- /**
23
- * web 代理
24
- */
25
- declare function web(reqObj: Koa.Context, headers: any): Promise<unknown>;
26
- export { web };
@@ -1,25 +0,0 @@
1
- /// <reference types="node" />
2
- import qs from "querystring";
3
- /**
4
- * 将 URL 参数转换为字符串
5
- * @example
6
- * ```ts
7
- * query2string({"pf":"145","ss":"320x240"});
8
- * query2string({"pf":"145","ss":"320x240"},["pf"]);
9
- * query2string({"pf":"145","ss":"320x240"},{"fr":"android"});
10
- * query2string({"pf":"145","ss":"320x240"},{"fr":"android"},["fr"]);
11
- * ```
12
- */
13
- declare function query2string(...args: Array<XLab.JsonObject | string[]>): string;
14
- export { query2string };
15
- /**
16
- * 字符串转成参数对象
17
- * @param qString 参数字符串
18
- * @return 处理完的对象
19
- * @example
20
- * ```ts
21
- * string2query("pf=145&ss=320x240");
22
- * ```
23
- */
24
- declare function string2query(qString: string): qs.ParsedUrlQuery;
25
- export { string2query };
@@ -1,11 +0,0 @@
1
- /// <reference types="node" />
2
- import type { ParsedUrlQuery } from "querystring";
3
- import type Koa from "koa";
4
- declare function by302(context: Koa.Context, url: string): void;
5
- export { by302 };
6
- declare function byJS(context: Koa.Context, url: string, msg: string, origin: string): void;
7
- export { byJS };
8
- declare function byMeta(context: Koa.Context, url: string): void;
9
- export { byMeta };
10
- declare function makeRedirectUrl(query: ParsedUrlQuery, url: string): string;
11
- export { makeRedirectUrl };
@@ -1,3 +0,0 @@
1
- import type Fetch from "node-fetch";
2
- declare function getFetch(): Promise<typeof Fetch>;
3
- export { getFetch };
@@ -1,51 +0,0 @@
1
- import type { RequestInit, Response } from "node-fetch";
2
- /**支持的返回数据类型 */
3
- declare type IFetchType = "text" | "json" | "blob" | "buffer" | "arrayBuffer";
4
- /**请求配置 */
5
- interface IFetchConfig extends Partial<Omit<RequestInit, "body" | "method">> {
6
- /**数据返回类型 */
7
- type?: IFetchType;
8
- /**是否强制同源 */
9
- sameOrigin?: boolean;
10
- /**请求自动重试次数 */
11
- retry?: number;
12
- /**自动重试间隔时间 */
13
- retryDelay?: number;
14
- /**是否自动追加随机数retry,默认 false */
15
- random?: boolean;
16
- /**数据返回时的钩子 */
17
- onResponse?: (res?: Response) => Promise<unknown>;
18
- }
19
- export type { IFetchConfig };
20
- /**给指定对象增加一个随机字符串 */
21
- declare function setRandomStr(query: any): void;
22
- export { setRandomStr };
23
- /**设置默认 Headers */
24
- declare function setDefHeaders(headers: Record<string, any>): void;
25
- export { setDefHeaders };
26
- /**
27
- * 获取数据
28
- * @param type 请求类型
29
- * @param uri 请求地址
30
- * @param query 请求参数
31
- * @param data 发送数据
32
- * @param config 请求配置
33
- * @param needProcessConfig 是否需要处理配置
34
- */
35
- declare function httpFetch<T = unknown>(type: string, uri: string, query?: Record<string, any>, data?: any, config?: IFetchConfig, retry?: number, needProcessConfig?: boolean): Promise<T>;
36
- export { httpFetch as fetch };
37
- /**
38
- * 发起一个 get 请求
39
- * @param uri 请求地址
40
- * @param query 请求参数
41
- */
42
- declare function requsetGet<T = unknown>(uri: string, query?: Record<string, any>, config?: IFetchConfig): Promise<T>;
43
- export { requsetGet as get };
44
- /**
45
- * 发起一个 post 请求
46
- * @param uri 请求地址
47
- * @param query 请求参数
48
- * @param data 请求数据
49
- */
50
- declare function requestPost<T>(uri: string, query?: any, data?: any, config?: IFetchConfig): Promise<T>;
51
- export { requestPost as post };
@@ -1,6 +0,0 @@
1
- /**
2
- * 解析请求别名
3
- * @param uri 请求地址
4
- */
5
- declare function resolve(uri: string): string;
6
- export default resolve;