koa-files 3.0.4 → 3.0.6
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/Service.cjs +31 -31
- package/cjs/index.cjs +3 -3
- package/cjs/utils/fs.cjs +1 -1
- package/cjs/utils/hash.cjs +1 -1
- package/cjs/utils/http.cjs +1 -1
- package/cjs/utils/path.cjs +1 -1
- package/esm/Service.js +31 -31
- package/esm/index.js +3 -3
- package/esm/utils/fs.js +1 -1
- package/esm/utils/hash.js +1 -1
- package/esm/utils/http.js +1 -1
- package/esm/utils/path.js +1 -1
- package/package.json +6 -6
- package/types/Service.d.ts +20 -20
package/cjs/Service.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 3.0.
|
|
4
|
+
* @version 3.0.6
|
|
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
|
|
@@ -42,8 +42,8 @@ class Service {
|
|
|
42
42
|
/**
|
|
43
43
|
* @constructor
|
|
44
44
|
* @description Create file service.
|
|
45
|
-
* @param root
|
|
46
|
-
* @param options
|
|
45
|
+
* @param root The file service root.
|
|
46
|
+
* @param options The file service options.
|
|
47
47
|
*/
|
|
48
48
|
constructor(root, options) {
|
|
49
49
|
this.options = options;
|
|
@@ -53,7 +53,7 @@ class Service {
|
|
|
53
53
|
* @private
|
|
54
54
|
* @method isIgnore
|
|
55
55
|
* @description Check if path is ignore.
|
|
56
|
-
* @param path
|
|
56
|
+
* @param path The path to check.
|
|
57
57
|
*/
|
|
58
58
|
isIgnore(path) {
|
|
59
59
|
const { ignore } = this.options;
|
|
@@ -62,10 +62,10 @@ class Service {
|
|
|
62
62
|
/**
|
|
63
63
|
* @private
|
|
64
64
|
* @method setupHeaders
|
|
65
|
-
* @description Setup headers
|
|
66
|
-
* @param context
|
|
67
|
-
* @param path
|
|
68
|
-
* @param stats
|
|
65
|
+
* @description Setup headers.
|
|
66
|
+
* @param context The koa context.
|
|
67
|
+
* @param path The file path.
|
|
68
|
+
* @param stats The file stats.
|
|
69
69
|
*/
|
|
70
70
|
setupHeaders(context, path, stats) {
|
|
71
71
|
const { options } = this;
|
|
@@ -111,14 +111,14 @@ class Service {
|
|
|
111
111
|
}
|
|
112
112
|
/**
|
|
113
113
|
* @private
|
|
114
|
-
* @method
|
|
115
|
-
* @description
|
|
116
|
-
* @param stream Destination stream.
|
|
114
|
+
* @method read
|
|
115
|
+
* @description Read file with range.
|
|
117
116
|
* @param path The file path to read.
|
|
118
117
|
* @param range The range to read.
|
|
119
|
-
* @param
|
|
118
|
+
* @param stream The destination stream.
|
|
119
|
+
* @param end The end flag after pipe to stream.
|
|
120
120
|
*/
|
|
121
|
-
|
|
121
|
+
read(path, range, stream, end) {
|
|
122
122
|
const { fs } = this.options;
|
|
123
123
|
return new Promise(resolve => {
|
|
124
124
|
// Range prefix and suffix.
|
|
@@ -166,24 +166,18 @@ class Service {
|
|
|
166
166
|
/**
|
|
167
167
|
* @private
|
|
168
168
|
* @method send
|
|
169
|
-
* @description Send file.
|
|
170
|
-
* @param
|
|
171
|
-
* @param
|
|
172
|
-
* @param
|
|
169
|
+
* @description Send file with ranges.
|
|
170
|
+
* @param path The file path to send.
|
|
171
|
+
* @param ranges The ranges to send.
|
|
172
|
+
* @param stream The destination stream.
|
|
173
173
|
*/
|
|
174
|
-
async send(
|
|
175
|
-
// Set stream body, highWaterMark 64kb.
|
|
176
|
-
const stream$1 = new stream.PassThrough({
|
|
177
|
-
highWaterMark: 65536
|
|
178
|
-
});
|
|
179
|
-
// Set response body.
|
|
180
|
-
context.body = stream$1;
|
|
174
|
+
async send(path, ranges, stream) {
|
|
181
175
|
// Ranges length.
|
|
182
176
|
let { length } = ranges;
|
|
183
177
|
// Write ranges to stream.
|
|
184
178
|
for (const range of ranges) {
|
|
185
179
|
// Write range.
|
|
186
|
-
const passed = await this.
|
|
180
|
+
const passed = await this.read(path, range, stream, --length === 0);
|
|
187
181
|
// If not passed, break.
|
|
188
182
|
if (!passed) {
|
|
189
183
|
break;
|
|
@@ -192,11 +186,11 @@ class Service {
|
|
|
192
186
|
}
|
|
193
187
|
/**
|
|
194
188
|
* @public
|
|
195
|
-
* @method
|
|
196
|
-
* @description
|
|
197
|
-
* @param context
|
|
189
|
+
* @method respond
|
|
190
|
+
* @description Respond file.
|
|
191
|
+
* @param context The koa context.
|
|
198
192
|
*/
|
|
199
|
-
async
|
|
193
|
+
async respond(context) {
|
|
200
194
|
const { root } = this;
|
|
201
195
|
// Only support GET and HEAD (405).
|
|
202
196
|
if (context.method !== 'GET' && context.method !== 'HEAD') {
|
|
@@ -270,8 +264,14 @@ class Service {
|
|
|
270
264
|
if (ranges === -2) {
|
|
271
265
|
return context.throw(400);
|
|
272
266
|
}
|
|
273
|
-
//
|
|
274
|
-
|
|
267
|
+
// Set stream body, highWaterMark 64kb.
|
|
268
|
+
const stream$1 = new stream.PassThrough({
|
|
269
|
+
highWaterMark: 65536
|
|
270
|
+
});
|
|
271
|
+
// Send file with ranges.
|
|
272
|
+
this.send(path$2, ranges, stream$1);
|
|
273
|
+
// Set response body.
|
|
274
|
+
context.body = stream$1;
|
|
275
275
|
// File found.
|
|
276
276
|
return true;
|
|
277
277
|
}
|
package/cjs/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 3.0.
|
|
4
|
+
* @version 3.0.6
|
|
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
|
|
@@ -32,11 +32,11 @@ function server(root, options) {
|
|
|
32
32
|
if (config.defer) {
|
|
33
33
|
return async (context, next) => {
|
|
34
34
|
await next();
|
|
35
|
-
await service.
|
|
35
|
+
await service.respond(context);
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
38
|
return async (context, next) => {
|
|
39
|
-
if (!(await service.
|
|
39
|
+
if (!(await service.respond(context))) {
|
|
40
40
|
await next();
|
|
41
41
|
}
|
|
42
42
|
};
|
package/cjs/utils/fs.cjs
CHANGED
package/cjs/utils/hash.cjs
CHANGED
package/cjs/utils/http.cjs
CHANGED
package/cjs/utils/path.cjs
CHANGED
package/esm/Service.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 3.0.
|
|
4
|
+
* @version 3.0.6
|
|
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
|
|
@@ -33,8 +33,8 @@ class Service {
|
|
|
33
33
|
/**
|
|
34
34
|
* @constructor
|
|
35
35
|
* @description Create file service.
|
|
36
|
-
* @param root
|
|
37
|
-
* @param options
|
|
36
|
+
* @param root The file service root.
|
|
37
|
+
* @param options The file service options.
|
|
38
38
|
*/
|
|
39
39
|
constructor(root, options) {
|
|
40
40
|
this.options = options;
|
|
@@ -44,7 +44,7 @@ class Service {
|
|
|
44
44
|
* @private
|
|
45
45
|
* @method isIgnore
|
|
46
46
|
* @description Check if path is ignore.
|
|
47
|
-
* @param path
|
|
47
|
+
* @param path The path to check.
|
|
48
48
|
*/
|
|
49
49
|
isIgnore(path) {
|
|
50
50
|
const { ignore } = this.options;
|
|
@@ -53,10 +53,10 @@ class Service {
|
|
|
53
53
|
/**
|
|
54
54
|
* @private
|
|
55
55
|
* @method setupHeaders
|
|
56
|
-
* @description Setup headers
|
|
57
|
-
* @param context
|
|
58
|
-
* @param path
|
|
59
|
-
* @param stats
|
|
56
|
+
* @description Setup headers.
|
|
57
|
+
* @param context The koa context.
|
|
58
|
+
* @param path The file path.
|
|
59
|
+
* @param stats The file stats.
|
|
60
60
|
*/
|
|
61
61
|
setupHeaders(context, path, stats) {
|
|
62
62
|
const { options } = this;
|
|
@@ -102,14 +102,14 @@ class Service {
|
|
|
102
102
|
}
|
|
103
103
|
/**
|
|
104
104
|
* @private
|
|
105
|
-
* @method
|
|
106
|
-
* @description
|
|
107
|
-
* @param stream Destination stream.
|
|
105
|
+
* @method read
|
|
106
|
+
* @description Read file with range.
|
|
108
107
|
* @param path The file path to read.
|
|
109
108
|
* @param range The range to read.
|
|
110
|
-
* @param
|
|
109
|
+
* @param stream The destination stream.
|
|
110
|
+
* @param end The end flag after pipe to stream.
|
|
111
111
|
*/
|
|
112
|
-
|
|
112
|
+
read(path, range, stream, end) {
|
|
113
113
|
const { fs } = this.options;
|
|
114
114
|
return new Promise(resolve => {
|
|
115
115
|
// Range prefix and suffix.
|
|
@@ -157,24 +157,18 @@ class Service {
|
|
|
157
157
|
/**
|
|
158
158
|
* @private
|
|
159
159
|
* @method send
|
|
160
|
-
* @description Send file.
|
|
161
|
-
* @param
|
|
162
|
-
* @param
|
|
163
|
-
* @param
|
|
160
|
+
* @description Send file with ranges.
|
|
161
|
+
* @param path The file path to send.
|
|
162
|
+
* @param ranges The ranges to send.
|
|
163
|
+
* @param stream The destination stream.
|
|
164
164
|
*/
|
|
165
|
-
async send(
|
|
166
|
-
// Set stream body, highWaterMark 64kb.
|
|
167
|
-
const stream = new PassThrough({
|
|
168
|
-
highWaterMark: 65536
|
|
169
|
-
});
|
|
170
|
-
// Set response body.
|
|
171
|
-
context.body = stream;
|
|
165
|
+
async send(path, ranges, stream) {
|
|
172
166
|
// Ranges length.
|
|
173
167
|
let { length } = ranges;
|
|
174
168
|
// Write ranges to stream.
|
|
175
169
|
for (const range of ranges) {
|
|
176
170
|
// Write range.
|
|
177
|
-
const passed = await this.
|
|
171
|
+
const passed = await this.read(path, range, stream, --length === 0);
|
|
178
172
|
// If not passed, break.
|
|
179
173
|
if (!passed) {
|
|
180
174
|
break;
|
|
@@ -183,11 +177,11 @@ class Service {
|
|
|
183
177
|
}
|
|
184
178
|
/**
|
|
185
179
|
* @public
|
|
186
|
-
* @method
|
|
187
|
-
* @description
|
|
188
|
-
* @param context
|
|
180
|
+
* @method respond
|
|
181
|
+
* @description Respond file.
|
|
182
|
+
* @param context The koa context.
|
|
189
183
|
*/
|
|
190
|
-
async
|
|
184
|
+
async respond(context) {
|
|
191
185
|
const { root } = this;
|
|
192
186
|
// Only support GET and HEAD (405).
|
|
193
187
|
if (context.method !== 'GET' && context.method !== 'HEAD') {
|
|
@@ -261,8 +255,14 @@ class Service {
|
|
|
261
255
|
if (ranges === -2) {
|
|
262
256
|
return context.throw(400);
|
|
263
257
|
}
|
|
264
|
-
//
|
|
265
|
-
|
|
258
|
+
// Set stream body, highWaterMark 64kb.
|
|
259
|
+
const stream = new PassThrough({
|
|
260
|
+
highWaterMark: 65536
|
|
261
|
+
});
|
|
262
|
+
// Send file with ranges.
|
|
263
|
+
this.send(path, ranges, stream);
|
|
264
|
+
// Set response body.
|
|
265
|
+
context.body = stream;
|
|
266
266
|
// File found.
|
|
267
267
|
return true;
|
|
268
268
|
}
|
package/esm/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 3.0.
|
|
4
|
+
* @version 3.0.6
|
|
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
|
|
@@ -24,11 +24,11 @@ function server(root, options) {
|
|
|
24
24
|
if (config.defer) {
|
|
25
25
|
return async (context, next) => {
|
|
26
26
|
await next();
|
|
27
|
-
await service.
|
|
27
|
+
await service.respond(context);
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
30
|
return async (context, next) => {
|
|
31
|
-
if (!(await service.
|
|
31
|
+
if (!(await service.respond(context))) {
|
|
32
32
|
await next();
|
|
33
33
|
}
|
|
34
34
|
};
|
package/esm/utils/fs.js
CHANGED
package/esm/utils/hash.js
CHANGED
package/esm/utils/http.js
CHANGED
package/esm/utils/path.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koa-files",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.6",
|
|
4
4
|
"description": "A static files serving middleware for koa.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "cjs/index.cjs",
|
|
@@ -57,21 +57,21 @@
|
|
|
57
57
|
"destroy": "^1.2.0",
|
|
58
58
|
"etag": "^1.8.1",
|
|
59
59
|
"range-parser": "^1.2.1",
|
|
60
|
-
"tslib": "^2.
|
|
60
|
+
"tslib": "^2.7.0"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
|
-
"@rollup/plugin-typescript": "^
|
|
63
|
+
"@rollup/plugin-typescript": "^12.1.0",
|
|
64
64
|
"@types/destroy": "^1.0.3",
|
|
65
65
|
"@types/etag": "^1.8.3",
|
|
66
66
|
"@types/koa": "^2.15.0",
|
|
67
67
|
"@types/range-parser": "^1.2.7",
|
|
68
68
|
"koa": "^2.15.3",
|
|
69
|
-
"magic-string": "^0.30.
|
|
69
|
+
"magic-string": "^0.30.11",
|
|
70
70
|
"prettier": "^3.3.3",
|
|
71
71
|
"rimraf": "^6.0.1",
|
|
72
|
-
"rollup": "^4.
|
|
72
|
+
"rollup": "^4.24.0",
|
|
73
73
|
"tsc-alias": "^1.8.10",
|
|
74
|
-
"typescript": "^5.
|
|
74
|
+
"typescript": "^5.6.3"
|
|
75
75
|
},
|
|
76
76
|
"peerDependencies": {
|
|
77
77
|
"koa": "^2.0.0"
|
package/types/Service.d.ts
CHANGED
|
@@ -30,51 +30,51 @@ export default class Service {
|
|
|
30
30
|
/**
|
|
31
31
|
* @constructor
|
|
32
32
|
* @description Create file service.
|
|
33
|
-
* @param root
|
|
34
|
-
* @param options
|
|
33
|
+
* @param root The file service root.
|
|
34
|
+
* @param options The file service options.
|
|
35
35
|
*/
|
|
36
36
|
constructor(root: string, options: Options);
|
|
37
37
|
/**
|
|
38
38
|
* @private
|
|
39
39
|
* @method isIgnore
|
|
40
40
|
* @description Check if path is ignore.
|
|
41
|
-
* @param path
|
|
41
|
+
* @param path The path to check.
|
|
42
42
|
*/
|
|
43
43
|
private isIgnore;
|
|
44
44
|
/**
|
|
45
45
|
* @private
|
|
46
46
|
* @method setupHeaders
|
|
47
|
-
* @description Setup headers
|
|
48
|
-
* @param context
|
|
49
|
-
* @param path
|
|
50
|
-
* @param stats
|
|
47
|
+
* @description Setup headers.
|
|
48
|
+
* @param context The koa context.
|
|
49
|
+
* @param path The file path.
|
|
50
|
+
* @param stats The file stats.
|
|
51
51
|
*/
|
|
52
52
|
private setupHeaders;
|
|
53
53
|
/**
|
|
54
54
|
* @private
|
|
55
|
-
* @method
|
|
56
|
-
* @description
|
|
57
|
-
* @param stream Destination stream.
|
|
55
|
+
* @method read
|
|
56
|
+
* @description Read file with range.
|
|
58
57
|
* @param path The file path to read.
|
|
59
58
|
* @param range The range to read.
|
|
60
|
-
* @param
|
|
59
|
+
* @param stream The destination stream.
|
|
60
|
+
* @param end The end flag after pipe to stream.
|
|
61
61
|
*/
|
|
62
|
-
private
|
|
62
|
+
private read;
|
|
63
63
|
/**
|
|
64
64
|
* @private
|
|
65
65
|
* @method send
|
|
66
|
-
* @description Send file.
|
|
67
|
-
* @param
|
|
68
|
-
* @param
|
|
69
|
-
* @param
|
|
66
|
+
* @description Send file with ranges.
|
|
67
|
+
* @param path The file path to send.
|
|
68
|
+
* @param ranges The ranges to send.
|
|
69
|
+
* @param stream The destination stream.
|
|
70
70
|
*/
|
|
71
71
|
private send;
|
|
72
72
|
/**
|
|
73
73
|
* @public
|
|
74
|
-
* @method
|
|
75
|
-
* @description
|
|
76
|
-
* @param context
|
|
74
|
+
* @method respond
|
|
75
|
+
* @description Respond file.
|
|
76
|
+
* @param context The koa context.
|
|
77
77
|
*/
|
|
78
|
-
|
|
78
|
+
respond(context: Context): Promise<boolean>;
|
|
79
79
|
}
|
|
80
80
|
export {};
|