@x-9lab/xlab 1.2.1 → 1.4.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.
@@ -168,6 +168,8 @@ declare global {
168
168
  indexPageCacheTime?: number;
169
169
  /**静态资源缓存时间 */
170
170
  staticResourceCacheTime?: number;
171
+ /**是否允许静态资源跨域访问 */
172
+ staticCros?: boolean;
171
173
  }
172
174
  /**业务错误数据对象 */
173
175
  interface ICodeItem {
package/README.md CHANGED
@@ -178,6 +178,7 @@ X-9lab 通用服务端
178
178
  |debug|`boolean`|false|是否开启 debug 模式|
179
179
  |staticMaxage|`number`|1800000|静态文件缓存时间|
180
180
  |staticHtmlFileMaxage|`number`|0|静态 html 文件缓存时间|
181
+ |staticCros|`boolean`|false|是否允许静态资源跨域访问|
181
182
  |enableComboCache|`boolean`|true|是否开启 Combo 缓存|
182
183
  |enableCron|`boolean`|false|是否开启定时任务|
183
184
  |middleware|`Array<string | (string | Record<string, any>)[]>`|[]|开启的中间件列表。因不方便合并替换,v1.1.0 开始建议使用 `middlewares` 来配置 |
@@ -19,6 +19,7 @@ const _utils = require("@x-drive/utils");
19
19
  const _nodeFetchWarper = require("@x-drive/node-fetch-warper");
20
20
  const _resolveUri = /*#__PURE__*/ _interopRequireDefault(require("./resolve-uri"));
21
21
  const _common = require("../common");
22
+ const _util = require("util");
22
23
  function _interopRequireDefault(obj) {
23
24
  return obj && obj.__esModule ? obj : {
24
25
  default: obj
@@ -120,7 +121,7 @@ const DEF_CONFIG = {
120
121
  }
121
122
  return null;
122
123
  } catch (e) {
123
- reqLog.error(e);
124
+ reqLog.error((0, _util.inspect)(e));
124
125
  if (retry && retry > 0) {
125
126
  if ((0, _utils.isNumber)(config.retryDelay) && config.retryDelay > 0) {
126
127
  await (0, _common.sleep)(config.retryDelay, reqLog);
package/dist/router.js CHANGED
@@ -16,6 +16,7 @@ function _interopRequireDefault(obj) {
16
16
  default: obj
17
17
  };
18
18
  }
19
+ /**API 服务目录 */ const API_BUSINESS_DIR = "business";
19
20
  /**生成 api */ function buildApi(api) {
20
21
  if (!api.startsWith("/")) {
21
22
  api = `/${api}`;
@@ -36,15 +37,18 @@ function _interopRequireDefault(obj) {
36
37
  if (stats.isDirectory() && item.startsWith("$")) {
37
38
  // 接口级别中间件
38
39
  let middlewaresFiles = _fs.default.readdirSync(tmpPath);
40
+ let mws = [];
39
41
  middlewaresFiles.forEach(function(name) {
40
42
  let tmpMwPath = _path.default.join(tmpPath, name);
41
43
  let fileStat = _fs.default.statSync(tmpMwPath);
42
44
  if (!fileStat.isDirectory() && !item.startsWith(".") && !item.endsWith(".d.ts")) {
43
- middlewares.push(tmpMwPath);
45
+ mws.push(tmpMwPath);
44
46
  }
45
47
  });
46
48
  // 简单做下排序
47
- middlewares.sort();
49
+ mws.sort();
50
+ const partPath = dir.split(API_BUSINESS_DIR)[1] || _path.default.posix.sep;
51
+ middlewares[partPath] = mws;
48
52
  }
49
53
  if (stats.isDirectory() && !item.startsWith("@") && !item.startsWith("$")) {
50
54
  walk(tmpPath, middlewares, callback);
@@ -60,11 +64,12 @@ function _interopRequireDefault(obj) {
60
64
  var mod = require(_path.default.resolve(modPath));
61
65
  if (mod) {
62
66
  mod = (0, _common.getRealDefaultMod)(mod);
63
- let api_path = modPath.split("business");
67
+ let api_path = modPath.split(API_BUSINESS_DIR);
64
68
  let api;
69
+ let apiPart;
65
70
  let modName;
66
71
  // 根据路径生成 url 规则
67
- api = api_path[1].replace(".js", "").split("/");
72
+ api = api_path[1].replace(".js", "").split(_path.default.posix.sep);
68
73
  // 文件名是 index
69
74
  const lastIsIndex = api[api.length - 1] === "index";
70
75
  // 文件名是 index 的则会被从 api 上先强行去掉
@@ -77,9 +82,11 @@ function _interopRequireDefault(obj) {
77
82
  if (!lastIsIndex) {
78
83
  api.pop();
79
84
  }
85
+ apiPart = api;
80
86
  api = buildApi(api.join("/"));
81
87
  mod.unshift(api);
82
88
  } else {
89
+ apiPart = api;
83
90
  // 不是数组则把文件当作正常的 api 地址
84
91
  api = buildApi(api.join("/"));
85
92
  }
@@ -87,6 +94,10 @@ function _interopRequireDefault(obj) {
87
94
  if ((0, _utils.isObject)(mod) && ((0, _utils.isFunction)(mod.handler) || (0, _utils.isAsyncFunction)(mod.handler))) {
88
95
  modName = mod.method || "get";
89
96
  api = mod.api || api;
97
+ apiPart = api.split("/");
98
+ if (apiPart[0] !== "") {
99
+ apiPart.unshift("");
100
+ }
90
101
  // 保证一定是以 api 开头的
91
102
  if (!IS_API_REGEXP.test(api)) {
92
103
  api = buildApi(api);
@@ -104,6 +115,15 @@ function _interopRequireDefault(obj) {
104
115
  ];
105
116
  }
106
117
  }
118
+ var dirMws = [];
119
+ var prevPart = "";
120
+ for(let i = 2; i < apiPart.length; i++){
121
+ const part = `${prevPart}${_path.default.posix.sep}${apiPart[i]}`;
122
+ if (middlewares[part]) {
123
+ dirMws = dirMws.concat(middlewares[part]);
124
+ }
125
+ prevPart = part;
126
+ }
107
127
  // 直接返回函数则直接绑定
108
128
  if ((0, _utils.isFunction)(mod) || (0, _utils.isAsyncFunction)(mod)) {
109
129
  mod = [
@@ -117,8 +137,8 @@ function _interopRequireDefault(obj) {
117
137
  modName = "get";
118
138
  }
119
139
  }
120
- if (middlewares && middlewares.length) {
121
- let mws = middlewares.map((name)=>(0, _common.getRealDefaultMod)(require(name)));
140
+ if (dirMws && dirMws.length) {
141
+ let mws = dirMws.map((name)=>(0, _common.getRealDefaultMod)(require(name)));
122
142
  mod.splice.apply(mod, [
123
143
  1,
124
144
  0,
@@ -131,12 +151,12 @@ function _interopRequireDefault(obj) {
131
151
  }
132
152
  };
133
153
  // 业务基本目录
134
- var _p = _path.default.resolve(__dirname, "business");
135
- walk(_p, [], processBusiness);
154
+ var _p = _path.default.resolve(__dirname, API_BUSINESS_DIR);
155
+ walk(_p, {}, processBusiness);
136
156
  // 支持自定义 api
137
- _p = _path.default.resolve(process.cwd(), _defaultXConfig.default.businessDir, "business");
157
+ _p = _path.default.resolve(process.cwd(), _defaultXConfig.default.businessDir, API_BUSINESS_DIR);
138
158
  if (process.cwd() !== __dirname && (0, _common.checkFileStat)(_p)) {
139
- walk(_p, [], processBusiness);
159
+ walk(_p, {}, processBusiness);
140
160
  }
141
161
  processBusiness = null;
142
162
  console.log("");
package/dist/server.js CHANGED
@@ -104,13 +104,20 @@ app.use(koaRouter.routes());
104
104
  var staticServeOpts = {};
105
105
  if (_config.default.isProd) {
106
106
  staticServeOpts.maxage = _config.default.staticMaxage;
107
- const staticHtmlFileMaxage = _config.default.staticHtmlFileMaxage;
108
- staticServeOpts.setHeaders = function(res, path) {
109
- if (staticHtmlFileMaxage && path.endsWith(".html")) {
110
- res.setHeader("Cache-Control", `max-age=${staticHtmlFileMaxage}`);
111
- }
112
- };
113
107
  }
108
+ staticServeOpts.setHeaders = function(res, path) {
109
+ if (_config.default.isProd && _config.default.staticHtmlFileMaxage && path.endsWith(".html")) {
110
+ res.setHeader("Cache-Control", `max-age=${_config.default.staticHtmlFileMaxage}`);
111
+ }
112
+ if (_config.default.staticCros) {
113
+ const { origin } = res.req.headers;
114
+ if (origin) {
115
+ res.setHeader("X-Cros-Static", origin);
116
+ res.setHeader("Access-Control-Allow-Origin", origin);
117
+ res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
118
+ }
119
+ }
120
+ };
114
121
  app.use((0, _koaStatic.default)(_path.default.resolve(process.cwd(), _config.default.root), staticServeOpts));
115
122
  // 异常状态处理中间件
116
123
  app.use(_badRequest.default);
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@x-9lab/xlab",
3
- "version": "1.2.1",
3
+ "version": "1.4.0",
4
4
  "description": "9lab 服务端模块",
5
- "versionDesc": "业务 api 文件夹支持接口级别通用中间件定义",
5
+ "versionDesc": "静态文件服务支持跨域",
6
6
  "scripts": {
7
7
  "dev": "swc src -D ./src/bin --config-file .swcrc -d dist -w",
8
8
  "compile": "swc src -D ./src/bin --config-file .swcrc -d dist",