koa-files 2.3.6 → 2.3.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/cjs/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 2.3.6
4
+ * @version 2.3.7
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A static files serving middleware for koa.
7
7
  * @see https://github.com/nuintun/koa-files#readme
@@ -22,18 +22,18 @@ const Send = require('./Send.cjs');
22
22
  * @param {Options} options
23
23
  */
24
24
  function server(root, options) {
25
- const send = new Send(root, options);
26
- if (options && options.defer) {
25
+ const send = new Send(root, options);
26
+ if (options && options.defer) {
27
+ return async (context, next) => {
28
+ await next();
29
+ await send.response(context);
30
+ };
31
+ }
27
32
  return async (context, next) => {
28
- await next();
29
- await send.response(context);
33
+ if (!(await send.response(context))) {
34
+ await next();
35
+ }
30
36
  };
31
- }
32
- return async (context, next) => {
33
- if (!(await send.response(context))) {
34
- await next();
35
- }
36
- };
37
37
  }
38
38
 
39
39
  module.exports = server;
package/cjs/utils.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 2.3.6
4
+ * @version 2.3.7
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A static files serving middleware for koa.
7
7
  * @see https://github.com/nuintun/koa-files#readme
@@ -12,11 +12,9 @@
12
12
  const fs = require('fs');
13
13
  const path = require('path');
14
14
 
15
- function _interopDefault(e) {
16
- return e && e.__esModule ? e : { default: e };
17
- }
15
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
18
16
 
19
- const fs__default = /*#__PURE__*/ _interopDefault(fs);
17
+ const fs__default = /*#__PURE__*/_interopDefault(fs);
20
18
 
21
19
  /**
22
20
  * @module utils
@@ -32,8 +30,8 @@ const CHARS = Array.from('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr
32
30
  * @returns {boolean}
33
31
  */
34
32
  function isOutRoot(path$1, root) {
35
- path$1 = path.relative(root, path$1);
36
- return /\.\.(?:[\\/]|$)/.test(path$1) || path.isAbsolute(path$1);
33
+ path$1 = path.relative(root, path$1);
34
+ return /\.\.(?:[\\/]|$)/.test(path$1) || path.isAbsolute(path$1);
37
35
  }
38
36
  /**
39
37
  * @function unixify
@@ -42,7 +40,7 @@ function isOutRoot(path$1, root) {
42
40
  * @returns {string}
43
41
  */
44
42
  function unixify(path) {
45
- return path.replace(/\\/g, '/');
43
+ return path.replace(/\\/g, '/');
46
44
  }
47
45
  /**
48
46
  * @function decodeURI
@@ -51,11 +49,12 @@ function unixify(path) {
51
49
  * @returns {string|-1}
52
50
  */
53
51
  function decodeURI(URI) {
54
- try {
55
- return decodeURIComponent(URI);
56
- } catch (error) {
57
- return -1;
58
- }
52
+ try {
53
+ return decodeURIComponent(URI);
54
+ }
55
+ catch (error) {
56
+ return -1;
57
+ }
59
58
  }
60
59
  /**
61
60
  * @function boundaryGenerator
@@ -63,13 +62,13 @@ function decodeURI(URI) {
63
62
  * @returns {string}
64
63
  */
65
64
  function boundaryGenerator() {
66
- let boundary = '';
67
- // Create boundary
68
- for (let i = 0; i < 38; i++) {
69
- boundary += CHARS[Math.floor(Math.random() * 62)];
70
- }
71
- // Return boundary
72
- return boundary;
65
+ let boundary = '';
66
+ // Create boundary
67
+ for (let i = 0; i < 38; i++) {
68
+ boundary += CHARS[Math.floor(Math.random() * 62)];
69
+ }
70
+ // Return boundary
71
+ return boundary;
73
72
  }
74
73
  /**
75
74
  * @function fstat
@@ -77,11 +76,11 @@ function boundaryGenerator() {
77
76
  * @returns {Promise<Stats>}
78
77
  */
79
78
  function fstat(path) {
80
- return new Promise((resolve, reject) => {
81
- fs__default.default.stat(path, (error, stats) => {
82
- error ? reject(error) : resolve(stats);
79
+ return new Promise((resolve, reject) => {
80
+ fs__default.default.stat(path, (error, stats) => {
81
+ error ? reject(error) : resolve(stats);
82
+ });
83
83
  });
84
- });
85
84
  }
86
85
  /**
87
86
  * @function hasTrailingSlash
@@ -89,7 +88,7 @@ function fstat(path) {
89
88
  * @returns {boolean}
90
89
  */
91
90
  function hasTrailingSlash(path) {
92
- return /\/$/.test(path);
91
+ return /\/$/.test(path);
93
92
  }
94
93
  /**
95
94
  * @function parseTokens
@@ -97,31 +96,31 @@ function hasTrailingSlash(path) {
97
96
  * @param {string[]} value
98
97
  */
99
98
  function parseTokens(value) {
100
- let end = 0;
101
- let start = 0;
102
- let tokens = [];
103
- // gather tokens
104
- for (let i = 0, length = value.length; i < length; i++) {
105
- switch (value.charCodeAt(i)) {
106
- case 0x20:
107
- // ' '
108
- if (start === end) {
109
- start = end = i + 1;
99
+ let end = 0;
100
+ let start = 0;
101
+ let tokens = [];
102
+ // gather tokens
103
+ for (let i = 0, length = value.length; i < length; i++) {
104
+ switch (value.charCodeAt(i)) {
105
+ case 0x20:
106
+ // ' '
107
+ if (start === end) {
108
+ start = end = i + 1;
109
+ }
110
+ break;
111
+ case 0x2c:
112
+ // ','
113
+ tokens.push(value.substring(start, end));
114
+ start = end = i + 1;
115
+ break;
116
+ default:
117
+ end = i + 1;
118
+ break;
110
119
  }
111
- break;
112
- case 0x2c:
113
- // ','
114
- tokens.push(value.substring(start, end));
115
- start = end = i + 1;
116
- break;
117
- default:
118
- end = i + 1;
119
- break;
120
120
  }
121
- }
122
- // final token
123
- tokens.push(value.substring(start, end));
124
- return tokens;
121
+ // final token
122
+ tokens.push(value.substring(start, end));
123
+ return tokens;
125
124
  }
126
125
  /**
127
126
  * @function isETagFresh
@@ -130,9 +129,9 @@ function parseTokens(value) {
130
129
  * @returns {boolean}
131
130
  */
132
131
  function isETagFresh(match, etag) {
133
- return parseTokens(match).some(match => {
134
- return match === etag || match === 'W/' + etag || 'W/' + match === etag;
135
- });
132
+ return parseTokens(match).some(match => {
133
+ return match === etag || match === 'W/' + etag || 'W/' + match === etag;
134
+ });
136
135
  }
137
136
  /**
138
137
  * @function isETag
@@ -140,7 +139,7 @@ function isETagFresh(match, etag) {
140
139
  * @returns {boolean}
141
140
  */
142
141
  function isETag(etag) {
143
- return /(?:W\/)?"(?:[ !#-\x7E\x80-\xFF]*|\r\n[\t ]|\\.)*"/.test(etag);
142
+ return /(?:W\/)?"(?:[ !#-\x7E\x80-\xFF]*|\r\n[\t ]|\\.)*"/.test(etag);
144
143
  }
145
144
 
146
145
  exports.boundaryGenerator = boundaryGenerator;