mm_statics 1.4.2 → 1.4.4

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 (2) hide show
  1. package/index.js +127 -121
  2. package/package.json +1 -2
package/index.js CHANGED
@@ -3,152 +3,158 @@ const send = require('koa-send');
3
3
  const path = require('path');
4
4
  require('mm_es6_to_amd');
5
5
 
6
- /**
7
- * @description 创建静态文件访问器
8
- * @param {String} root 文件根目录
9
- * @param {Object} config 配置
10
- * @param {Object} es6_to_amd 将ES6的js、vue文件转AMD标准, 传入需要转换的后缀名
11
- */
12
- function statics(root, config, es6_to_amd) {
13
- /**
14
- * @description ES6的js、vue文件转AMD标准配置
6
+
7
+ class Statics {
8
+ /**
9
+ * 创建静态文件访问器
10
+ * @param {String} root 文件根目录
11
+ * @param {Object} config 配置
12
+ * @param {Object} es6_to_amd 将ES6的js、vue文件转AMD标准, 传入需要转换的后缀名
15
13
  */
16
- this.es6_to_amd = {
14
+ constructor(root, config, es6_to_amd) {
17
15
  /**
18
- * 处理转换的文件路径,当文件属于改路径下的才进行转换
16
+ * @description ES6的js、vue文件转AMD标准配置
19
17
  */
20
- path: '/src',
18
+ this.es6_to_amd = {
19
+ /**
20
+ * 处理转换的文件路径,当文件属于改路径下的才进行转换
21
+ */
22
+ path: '/src',
23
+ /**
24
+ * @param {Array} 需要转换的文件后缀名
25
+ */
26
+ files: ['.js', '.vue']
27
+ };
28
+
21
29
  /**
22
- * @param {Array} 需要转换的文件后缀名
30
+ * 配置参数
23
31
  */
24
- files: ['.js', '.vue']
25
- };
32
+ this.config = {
33
+ index: "index.html",
34
+ maxage: 7200 * 1000,
35
+ immutable: true,
36
+ hidden: false,
37
+ format: false,
38
+ extensions: false,
39
+ brotli: false,
40
+ gzip: false,
41
+ root: root ? root : './static'
42
+ };
26
43
 
27
- /**
28
- * 配置参数
29
- */
30
- this.config = {
31
- index: "index.html",
32
- maxage: 7200 * 1000,
33
- immutable: true,
34
- hidden: false,
35
- format: false,
36
- extensions: false,
37
- brotli: false,
38
- gzip: false,
39
- root: root ? root : './static'
40
- };
44
+ if (config) {
45
+ if (config.maxAge) {
46
+ config.maxage = config.maxAge * 1000;
47
+ delete config.maxAge;
48
+ } else if (config.maxage) {
49
+ config.maxage *= 1000;
50
+ }
51
+ $.push(this.config, config);
52
+ }
41
53
 
42
- if (config) {
43
- if (config.maxAge) {
44
- config.maxage = config.maxAge * 1000;
45
- delete config.maxAge;
46
- } else if (config.maxage) {
47
- config.maxage *= 1000;
54
+ if (es6_to_amd) {
55
+ $.push(this.es6_to_amd, es6_to_amd);
48
56
  }
49
- $.push(this.config, config);
50
57
  }
58
+ }
51
59
 
52
- /**
53
- * 执行静态文件处理 (转换为ES5标准)
54
- * @method run
55
- * @param {Object} ctx http请求上下文
56
- * @param {Function} next 跳过当前执行,先执行后面函数
57
- * @return {Boolean} 是否执行成功
58
- */
59
- async function main(ctx, next) {
60
- await next();
61
- if (ctx.method !== 'HEAD' && ctx.method !== 'GET')
62
- return;
63
- // 已处理响应
64
- if (ctx.body != null || ctx.status !== 404)
65
- return;
66
60
 
67
- var path = ctx.path;
61
+ /**
62
+ * 执行静态文件处理 (转换为ES5标准)
63
+ * @method run
64
+ * @param {Object} ctx http请求上下文
65
+ * @param {Function} next 跳过当前执行,先执行后面函数
66
+ * @return {Boolean} 是否执行成功
67
+ */
68
+ Statics.prototype.main = async function(ctx, next) {
69
+ await next();
70
+ if (ctx.method !== 'HEAD' && ctx.method !== 'GET')
71
+ return;
72
+ // 已处理响应
73
+ if (ctx.body != null || ctx.status !== 404)
74
+ return;
68
75
 
69
- if (path.startWith(this.es6_to_amd.path)) {
70
- // 是否需要转换
71
- var bl = false;
72
- var lt = this.es6_to_amd.files;
73
- var len = lt.length;
74
- for (var i = 0; i < len; i++) {
75
- if (path.endsWith(lt[i])) {
76
- bl = true;
77
- break;
78
- }
76
+ var path = ctx.path;
77
+
78
+ if (path.startWith(this.es6_to_amd.path)) {
79
+ // 是否需要转换
80
+ var bl = false;
81
+ var lt = this.es6_to_amd.files;
82
+ var len = lt.length;
83
+ for (var i = 0; i < len; i++) {
84
+ if (path.endsWith(lt[i])) {
85
+ bl = true;
86
+ break;
79
87
  }
80
- if (bl) {
81
- var file = this.config.root + path;
82
- if (!file.hasFile()) {
83
- return;
84
- }
85
- var code;
86
- try {
87
- var str = file.loadText();
88
- code = $.es6_to_amd(str);
89
- } catch (e) {
90
- throw e
88
+ }
89
+ if (bl) {
90
+ var file = this.config.root + path;
91
+ if (!file.hasFile()) {
92
+ return;
93
+ }
94
+ var code;
95
+ try {
96
+ var str = file.loadText();
97
+ code = $.es6_to_amd(str);
98
+ } catch (e) {
99
+ throw e
100
+ }
101
+ if (code) {
102
+ if (path.endsWith('.js')) {
103
+ ctx.response.type = "application/javascript; charset=utf-8";
91
104
  }
92
- if (code) {
93
- if (path.endsWith('.js')) {
94
- ctx.response.type = "application/javascript; charset=utf-8";
95
- }
96
- ctx.body = code;
97
- if (this.config.maxage) {
98
- if (this.config.immutable) {
99
- ctx.set('Cache-Control', 'max-age=' + (this.config.maxage / 1000) + ",immutable");
100
- } else {
101
- ctx.set('Cache-Control', 'max-age=' + (this.config.maxage / 1000));
102
- }
105
+ ctx.body = code;
106
+ if (this.config.maxage) {
107
+ if (this.config.immutable) {
108
+ ctx.set('Cache-Control', 'max-age=' + (this.config.maxage / 1000) + ",immutable");
109
+ } else {
110
+ ctx.set('Cache-Control', 'max-age=' + (this.config.maxage / 1000));
103
111
  }
104
- return file;
105
112
  }
113
+ return file;
106
114
  }
107
115
  }
108
- try {
109
- await send(ctx, path, this.config);
110
- } catch (err) {
111
- if (err.status !== 404) {
112
- throw err;
113
- }
116
+ }
117
+ try {
118
+ await send(ctx, path, this.config);
119
+ } catch (err) {
120
+ if (err.status !== 404) {
121
+ throw err;
114
122
  }
115
123
  }
124
+ }
116
125
 
117
- /**
118
- * 执行静态文件处理
119
- * @method run
120
- * @param {Object} ctx http请求上下文
121
- * @param {Function} next 跳过当前执行,先执行后面函数
122
- * @return {Boolean} 是否执行成功
123
- */
124
- async function run(ctx, next) {
125
- await next();
126
- if (ctx.method !== 'HEAD' && ctx.method !== 'GET')
127
- return;
128
- // response is already handled
129
- if (ctx.body != null || ctx.status !== 404)
130
- return; // eslint-disable-line
131
- try {
132
- await send(ctx, ctx.path, this.config);
133
- } catch (err) {
134
- if (err.status !== 404) {
135
- throw err;
136
- }
126
+ /**
127
+ * 执行静态文件处理
128
+ * @method run
129
+ * @param {Object} ctx http请求上下文
130
+ * @param {Function} next 跳过当前执行,先执行后面函数
131
+ * @return {Boolean} 是否执行成功
132
+ */
133
+ Statics.prototype.run = async function(ctx, next) {
134
+ await next();
135
+ if (ctx.method !== 'HEAD' && ctx.method !== 'GET')
136
+ return;
137
+ // response is already handled
138
+ if (ctx.body != null || ctx.status !== 404)
139
+ return; // eslint-disable-line
140
+ try {
141
+ await send(ctx, ctx.path, this.config);
142
+ } catch (err) {
143
+ if (err.status !== 404) {
144
+ throw err;
137
145
  }
138
- };
139
-
140
- if (es6_to_amd) {
141
- $.push(this.es6_to_amd, es6_to_amd);
142
- }
143
- if (this.es6_to_amd.files.length > 0) {
144
- return main;
145
- } else {
146
- return run;
147
146
  }
148
- }
147
+ };
149
148
 
150
149
  /**
151
150
  * 用于处理静态文件
152
151
  * @module statics
153
152
  */
154
- module.exports = statics;
153
+ module.exports = function(root, config, es6_to_amd) {
154
+ var static = new Statics(root, config, es6_to_amd);
155
+ if (es6_to_amd.files.length > 0) {
156
+ return static.main;
157
+ } else {
158
+ return static.run;
159
+ }
160
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_statics",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "这是超级美眉statics函数模块,用于web服务端statics缓存",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -32,7 +32,6 @@
32
32
  "dependencies": {
33
33
  "abstract-syntax-tree": "^2.22.0",
34
34
  "koa-send": "^5.0.1",
35
- "mm_cachebase": "^1.4.2",
36
35
  "mm_es6_to_amd": "^1.4.0"
37
36
  }
38
37
  }