jj.js 0.13.0 → 0.14.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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # v0.14.0 / 2024-03-07
2
+ 1. 优化类型文件生成
3
+ 2. 修复优化url生成
4
+ 3. 自动加载功能,不缓存空文件
5
+ 4. 修复优化Resquest类请求方法判断
6
+ 5. 默认开启cookie签名机制
7
+
1
8
  # v0.13.0 / 2024-02-27
2
9
  1. 新增request系统类库
3
10
  2. response类isAjax方法迁移到request类
package/jsconfig.json CHANGED
@@ -5,5 +5,5 @@
5
5
  "resolveJsonModule": true,
6
6
  "checkJs": true
7
7
  },
8
- "exclude": ["node_modules", "docs", "./lib/ctx.js"]
9
- }
8
+ "exclude": ["node_modules", "docs"]
9
+ }
package/lib/app.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const path = require('path');
2
2
  const Koa = require('koa');
3
- const {app: cfg_app} = require('./config');
3
+ const {app: cfg_app, cookie: cfg_cookie} = require('./config');
4
4
  const Logger = require('./logger');
5
5
  const Response = require('./response');
6
6
  const router = require('./router');
@@ -54,6 +54,9 @@ class App extends Koa
54
54
  // router
55
55
  this.use(router.routes()).use(router.allowedMethods());
56
56
 
57
+ // cookie
58
+ cfg_cookie.keys && (this.keys = cfg_app.app_debug ? ['jj.js'] : cfg_cookie.keys);
59
+
57
60
  // types
58
61
  if(cfg_app.app_debug) {
59
62
  const {isFileSync} = require('./utils/fs');
package/lib/config.js CHANGED
@@ -49,6 +49,11 @@ const cache = {
49
49
  clear_time: undefined // (undefined: 清理一次, 0: 关闭自动清理, >0: 为自动清理周期)
50
50
  }
51
51
 
52
+ const cookie = {
53
+ keys: [Math.random().toString(36).slice(-8)], // cookie密钥
54
+ signed: true // 默认开启签名
55
+ }
56
+
52
57
  const page = {
53
58
  page_key : 'page', // 默认分页标识
54
59
  key_origin : 'query', // query 或 params
@@ -94,6 +99,6 @@ module.exports = {
94
99
  cache: {...cache, ...base_config.cache},
95
100
  page: {...page, ...base_config.page},
96
101
  routes: base_config.routes,
97
- cookie: base_config.cookie,
102
+ cookie: {...cookie, ...base_config.cookie},
98
103
  tpl: {...tpl, ...base_config.tpl}
99
104
  };
package/lib/cookie.js CHANGED
@@ -30,20 +30,21 @@ class Cookie extends Context
30
30
  if(key === undefined) {
31
31
  return this.all();
32
32
  }
33
- return this.ctx.cookies.get(key, options);
33
+ return this.ctx.cookies.get(key, {signed: cfg_cookie.signed, ...options});
34
34
  }
35
35
 
36
36
  /**
37
37
  * 删除或清理cookie
38
38
  * @public
39
39
  * @param {*} [key] - 为空则清理所有cookie
40
+ * @param {import('cookies').GetOption} [options]
40
41
  * @returns {this}
41
42
  */
42
- delete(key) {
43
+ delete(key, options) {
43
44
  if(key === undefined) {
44
45
  this.clear();
45
46
  } else {
46
- this.ctx.cookies.set(key, '', {maxAge: 0});
47
+ this.ctx.cookies.set(key, '', {...cfg_cookie, ...options, maxAge: 0});
47
48
  }
48
49
  return this;
49
50
  }
package/lib/loader.js CHANGED
@@ -43,8 +43,8 @@ function loader(dir='./', ...args) {
43
43
  nodeType[_nodePath] = 'file';
44
44
  } else if(isDirSync(_nodePath)) {
45
45
  nodeType[_nodePath] = 'dir';
46
- } else{
47
- nodeType[_nodePath] = 'none';
46
+ } else {
47
+ // nodeType[_nodePath] = 'none';
48
48
  }
49
49
  }
50
50
  if(nodeType[_nodePath] == 'file') {
package/lib/request.js CHANGED
@@ -151,7 +151,7 @@ class Request extends Context
151
151
  * @returns {string}
152
152
  */
153
153
  method() {
154
- return this.ctx.method;
154
+ return this.ctx.method.toLowerCase();
155
155
  }
156
156
 
157
157
  /**
@@ -159,7 +159,7 @@ class Request extends Context
159
159
  * @returns {boolean}
160
160
  */
161
161
  isGet() {
162
- return this.ctx.method == 'get';
162
+ return this.method() == 'get';
163
163
  }
164
164
 
165
165
  /**
@@ -167,7 +167,7 @@ class Request extends Context
167
167
  * @returns {boolean}
168
168
  */
169
169
  isPost() {
170
- return this.ctx.method == 'post';
170
+ return this.method() == 'post';
171
171
  }
172
172
 
173
173
  /**
@@ -178,8 +178,7 @@ class Request extends Context
178
178
  isAjax() {
179
179
  return this.ctx.headers['x-requested-with'] && String(this.ctx.headers['x-requested-with']).toLowerCase() == 'xmlhttprequest'
180
180
  || this.ctx.request.type.toLowerCase() == 'application/json'
181
- || this.ctx.query.is_ajax
182
- || this.ctx.request.body && this.ctx.request.body.is_ajax
181
+ || this.query('is_ajax')
183
182
  ? true : false;
184
183
  }
185
184
 
package/lib/types.js CHANGED
@@ -57,15 +57,16 @@ function start() {
57
57
  */
58
58
  async function createFile(f) {
59
59
  if(f) {
60
+ require.cache[f] && delete(require.cache[f])
60
61
  if(f == this._f) {
61
- return require.cache[f] && delete(require.cache[f]);
62
+ return;
62
63
  }
63
64
 
64
65
  this._f && require.cache[this._f] && delete(require.cache[this._f]);
65
66
  this._f = f;
66
67
 
67
68
  const f_info = f.replace(cfg_app.base_dir + path.sep, '').split(path.sep);
68
- if(f_info[0] && f_info[1] && f_info[0] != cfg_app.common_app && !~f_info[0].indexOf('.')) {
69
+ if(cfg_app.app_multi && f_info[0] && f_info[1] && f_info[0] != cfg_app.common_app && f_info[0] != 'config' && !~f_info[0].indexOf('.')) {
69
70
  this._APP = f_info[0];
70
71
  }
71
72
  } else {
package/lib/url.js CHANGED
@@ -18,19 +18,28 @@ class Url extends Context
18
18
  * 编译生成url
19
19
  * @public
20
20
  * @param {string} [url] - 网址,支持智能解析
21
- * @param {object} [vars] - 网址参数
22
- * @param {string} [ext] - 网址后缀
23
- * @param {string} [domain] - 网址域名
21
+ * @param {object|string|boolean} [vars] - 网址参数
22
+ * @param {string|boolean} [ext] - 网址后缀
23
+ * @param {string|boolean} [domain] - 网址域名
24
24
  * @returns {string}
25
+ * @example - build(url, vars, ext, domain|true)
26
+ * @example - build(url, ext, domain|true)
27
+ * @example - build(url, domain|true)
25
28
  */
26
- build(url='', vars, ext='', domain='') {
27
- if(vars && typeof vars != 'object') {
29
+ build(url='', vars={}, ext='', domain='') {
30
+ if(typeof vars != 'object') {
28
31
  [vars, ext, domain] = [{}, vars, ext];
29
32
  }
33
+ if(ext === true || ext.slice(0, 4) == 'http') {
34
+ [ext, domain] = ['', ext];
35
+ }
36
+ if(domain === true) {
37
+ domain = this.ctx.protocol + '://' + this.ctx.host;
38
+ }
30
39
  const query = {...vars};
31
40
  let urls;
32
41
  if(url.slice(0, 1) == ':') {
33
- url = this.ruleUrl(url.substr(1), query);
42
+ url = this.ruleUrl(url.slice(0, 1), query);
34
43
  } else if(url.slice(0, 1) != '/' && url.slice(0, 4) != 'http') {
35
44
  urls = url.split('/').reverse().map(u => toLine(u));
36
45
  if(urls.length < 3) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jj.js",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "A super simple lightweight NodeJS MVC framework(一个超级简单轻量的NodeJS MVC框架)",
5
5
  "main": "jj.js",
6
6
  "scripts": {},
package/types.js CHANGED
@@ -171,6 +171,7 @@
171
171
 
172
172
  /**
173
173
  * @typedef {import('cookies').SetOption} CookieConfig - Cookie配置,一般不用设置
174
+ * @typedef {typeof import('koa').prototype.keys} Keygrip
174
175
  */
175
176
 
176
177
  /**
@@ -188,7 +189,7 @@
188
189
  * @property {CacheConfig} [cache]
189
190
  * @property {PageConfig} [page]
190
191
  * @property {RouteConfig} [routes]
191
- * @property {CookieConfig} [cookie]
192
+ * @property {CookieConfig & {keys?: String[] | Keygrip | undefined}} [cookie]
192
193
  * @property {TplConfig} [tpl]
193
194
  */
194
195