koa-files 3.1.7 → 3.1.8
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/README.md +22 -19
- package/cjs/Service.cjs +8 -6
- package/cjs/index.cjs +3 -11
- 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/cjs/utils/stream.cjs +2 -2
- package/esm/Service.js +3 -2
- package/esm/index.js +3 -5
- 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/esm/utils/stream.js +2 -2
- package/package.json +2 -1
- package/types/Service.d.ts +3 -3
- package/types/index.d.ts +1 -3
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ $ npm install koa-files
|
|
|
18
18
|
|
|
19
19
|
```ts
|
|
20
20
|
import { Middleware } from 'koa';
|
|
21
|
-
import
|
|
21
|
+
import fs, { Stats } from 'node:fs';
|
|
22
22
|
|
|
23
23
|
interface IgnoreFunction {
|
|
24
24
|
(path: string): boolean;
|
|
@@ -32,17 +32,20 @@ interface HeaderFunction {
|
|
|
32
32
|
(path: string, stats: Stats): Headers | void;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
interface FileSystem {
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
export interface FileSystem {
|
|
36
|
+
stat: typeof fs.stat;
|
|
37
|
+
open: typeof fs.open;
|
|
38
|
+
read: typeof fs.read;
|
|
39
|
+
close: typeof fs.close;
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
export interface Options {
|
|
41
|
-
etag?: boolean;
|
|
42
43
|
fs?: FileSystem;
|
|
43
44
|
defer?: boolean;
|
|
45
|
+
etag?: boolean;
|
|
44
46
|
acceptRanges?: boolean;
|
|
45
47
|
lastModified?: boolean;
|
|
48
|
+
highWaterMark?: number;
|
|
46
49
|
ignore?: IgnoreFunction;
|
|
47
50
|
headers?: Headers | HeaderFunction;
|
|
48
51
|
}
|
|
@@ -62,24 +65,24 @@ export default function server(root: string, options?: Options): Middleware;
|
|
|
62
65
|
- Defaults to `node:fs`.
|
|
63
66
|
- The file system to used.
|
|
64
67
|
|
|
65
|
-
##### `
|
|
68
|
+
##### `defer`
|
|
66
69
|
|
|
67
|
-
- Defaults to `
|
|
68
|
-
-
|
|
69
|
-
-
|
|
70
|
+
- Defaults to `false`.
|
|
71
|
+
- If true, serves after `await next()`.
|
|
72
|
+
- Allowing any downstream middleware to respond first.
|
|
70
73
|
|
|
71
|
-
##### `
|
|
74
|
+
##### `etag`
|
|
72
75
|
|
|
73
76
|
- Defaults to `true`.
|
|
74
|
-
- Enable or disable
|
|
75
|
-
-
|
|
77
|
+
- Enable or disable etag generation.
|
|
78
|
+
- Use weak etag internally.
|
|
76
79
|
- Can be overridden by the `headers`.
|
|
77
80
|
|
|
78
|
-
##### `
|
|
81
|
+
##### `acceptRanges`
|
|
79
82
|
|
|
80
83
|
- Defaults to `true`.
|
|
81
|
-
- Enable or disable
|
|
82
|
-
-
|
|
84
|
+
- Enable or disable accepting ranged requests.
|
|
85
|
+
- Disabling this will not send Accept-Ranges and ignore the contents of the Range request header.
|
|
83
86
|
- Can be overridden by the `headers`.
|
|
84
87
|
|
|
85
88
|
##### `lastModified`
|
|
@@ -99,11 +102,11 @@ export default function server(root: string, options?: Options): Middleware;
|
|
|
99
102
|
- Defaults to `undefined`.
|
|
100
103
|
- Function that determines if a file should be ignored.
|
|
101
104
|
|
|
102
|
-
##### `
|
|
105
|
+
##### `headers`
|
|
103
106
|
|
|
104
|
-
- Defaults to `
|
|
105
|
-
-
|
|
106
|
-
-
|
|
107
|
+
- Defaults to `undefined`.
|
|
108
|
+
- Set headers to be sent.
|
|
109
|
+
- See docs: [Headers in MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers).
|
|
107
110
|
|
|
108
111
|
## Example
|
|
109
112
|
|
package/cjs/Service.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 3.1.
|
|
4
|
+
* @version 3.1.8
|
|
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
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
'use strict';
|
|
11
11
|
|
|
12
12
|
const createETag = require('etag');
|
|
13
|
-
const fs = require('
|
|
13
|
+
const fs = require('fs');
|
|
14
|
+
const fs$1 = require('./utils/fs.cjs');
|
|
14
15
|
const path$1 = require('path');
|
|
15
16
|
const stream = require('./utils/stream.cjs');
|
|
16
17
|
const path = require('./utils/path.cjs');
|
|
@@ -21,6 +22,7 @@ function _interopDefault(e) {
|
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
const createETag__default = /*#__PURE__*/ _interopDefault(createETag);
|
|
25
|
+
const fs__default = /*#__PURE__*/ _interopDefault(fs);
|
|
24
26
|
|
|
25
27
|
/**
|
|
26
28
|
* @module Service
|
|
@@ -44,8 +46,8 @@ class Service {
|
|
|
44
46
|
* @param options The file service options.
|
|
45
47
|
*/
|
|
46
48
|
constructor(root, options) {
|
|
47
|
-
this.options = options;
|
|
48
49
|
this.root = path.unixify(path$1.resolve(root));
|
|
50
|
+
this.options = Object.assign({ fs: fs__default.default, highWaterMark: 65536 }, options);
|
|
49
51
|
}
|
|
50
52
|
/**
|
|
51
53
|
* @private
|
|
@@ -135,7 +137,7 @@ class Service {
|
|
|
135
137
|
return false;
|
|
136
138
|
}
|
|
137
139
|
// File stats.
|
|
138
|
-
const stats = await fs.stat(this.options.fs, path$2);
|
|
140
|
+
const stats = await fs$1.stat(this.options.fs, path$2);
|
|
139
141
|
// Check file stats.
|
|
140
142
|
if (
|
|
141
143
|
// File not exist (404 | 500).
|
|
@@ -188,9 +190,9 @@ class Service {
|
|
|
188
190
|
return context.throw(400);
|
|
189
191
|
}
|
|
190
192
|
// Get stream options.
|
|
191
|
-
const { fs
|
|
193
|
+
const { fs, highWaterMark } = this.options;
|
|
192
194
|
// Set response body.
|
|
193
|
-
context.body = new stream.FileReadStream(path$2, ranges, { fs
|
|
195
|
+
context.body = new stream.FileReadStream(path$2, ranges, { fs, highWaterMark });
|
|
194
196
|
// File found.
|
|
195
197
|
return true;
|
|
196
198
|
}
|
package/cjs/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 3.1.
|
|
4
|
+
* @version 3.1.8
|
|
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
|
|
@@ -9,15 +9,8 @@
|
|
|
9
9
|
|
|
10
10
|
'use strict';
|
|
11
11
|
|
|
12
|
-
const fs = require('fs');
|
|
13
12
|
const Service = require('./Service.cjs');
|
|
14
13
|
|
|
15
|
-
function _interopDefault(e) {
|
|
16
|
-
return e && e.__esModule ? e : { default: e };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const fs__default = /*#__PURE__*/ _interopDefault(fs);
|
|
20
|
-
|
|
21
14
|
/**
|
|
22
15
|
* @module index
|
|
23
16
|
*/
|
|
@@ -27,9 +20,8 @@ const fs__default = /*#__PURE__*/ _interopDefault(fs);
|
|
|
27
20
|
* @param {Options} options
|
|
28
21
|
*/
|
|
29
22
|
function server(root, options) {
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
if (config.defer) {
|
|
23
|
+
const service = new Service(root, options);
|
|
24
|
+
if (options === null || options === void 0 ? void 0 : options.defer) {
|
|
33
25
|
return async (context, next) => {
|
|
34
26
|
await next();
|
|
35
27
|
await service.respond(context);
|
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/cjs/utils/stream.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 3.1.
|
|
4
|
+
* @version 3.1.8
|
|
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,7 +24,7 @@ class FileReadStream extends stream.Readable {
|
|
|
24
24
|
* @param options The stream options.
|
|
25
25
|
*/
|
|
26
26
|
constructor(path, ranges, options) {
|
|
27
|
-
const { fs, encoding, highWaterMark
|
|
27
|
+
const { fs, encoding, highWaterMark } = options;
|
|
28
28
|
super({ encoding, highWaterMark });
|
|
29
29
|
this.bytesRead = 0;
|
|
30
30
|
this.fd = null;
|
package/esm/Service.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 3.1.
|
|
4
|
+
* @version 3.1.8
|
|
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
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import createETag from 'etag';
|
|
11
|
+
import fs from 'fs';
|
|
11
12
|
import { stat } from './utils/fs.js';
|
|
12
13
|
import { resolve, extname, join } from 'path';
|
|
13
14
|
import { FileReadStream } from './utils/stream.js';
|
|
@@ -36,8 +37,8 @@ class Service {
|
|
|
36
37
|
* @param options The file service options.
|
|
37
38
|
*/
|
|
38
39
|
constructor(root, options) {
|
|
39
|
-
this.options = options;
|
|
40
40
|
this.root = unixify(resolve(root));
|
|
41
|
+
this.options = Object.assign({ fs, highWaterMark: 65536 }, options);
|
|
41
42
|
}
|
|
42
43
|
/**
|
|
43
44
|
* @private
|
package/esm/index.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 3.1.
|
|
4
|
+
* @version 3.1.8
|
|
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
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import fs from 'fs';
|
|
11
10
|
import Service from './Service.js';
|
|
12
11
|
|
|
13
12
|
/**
|
|
@@ -19,9 +18,8 @@ import Service from './Service.js';
|
|
|
19
18
|
* @param {Options} options
|
|
20
19
|
*/
|
|
21
20
|
function server(root, options) {
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
if (config.defer) {
|
|
21
|
+
const service = new Service(root, options);
|
|
22
|
+
if (options === null || options === void 0 ? void 0 : options.defer) {
|
|
25
23
|
return async (context, next) => {
|
|
26
24
|
await next();
|
|
27
25
|
await service.respond(context);
|
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/esm/utils/stream.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 3.1.
|
|
4
|
+
* @version 3.1.8
|
|
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,7 +22,7 @@ class FileReadStream extends Readable {
|
|
|
22
22
|
* @param options The stream options.
|
|
23
23
|
*/
|
|
24
24
|
constructor(path, ranges, options) {
|
|
25
|
-
const { fs, encoding, highWaterMark
|
|
25
|
+
const { fs, encoding, highWaterMark } = options;
|
|
26
26
|
super({ encoding, highWaterMark });
|
|
27
27
|
this.bytesRead = 0;
|
|
28
28
|
this.fd = null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koa-files",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.8",
|
|
4
4
|
"description": "A static files serving middleware for koa.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "cjs/index.cjs",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"server",
|
|
28
28
|
"static",
|
|
29
29
|
"service",
|
|
30
|
+
"multipart",
|
|
30
31
|
"middleware"
|
|
31
32
|
],
|
|
32
33
|
"engines": {
|
package/types/Service.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Service
|
|
3
3
|
*/
|
|
4
|
-
import { Stats } from 'fs';
|
|
5
4
|
import { Context } from 'koa';
|
|
5
|
+
import { Stats } from 'fs';
|
|
6
6
|
import { FileSystem } from './utils/fs';
|
|
7
7
|
interface IgnoreFunction {
|
|
8
8
|
(path: string): boolean;
|
|
@@ -14,7 +14,7 @@ interface HeaderFunction {
|
|
|
14
14
|
(path: string, stats: Stats): Headers | void;
|
|
15
15
|
}
|
|
16
16
|
export interface Options {
|
|
17
|
-
fs
|
|
17
|
+
fs?: FileSystem;
|
|
18
18
|
etag?: boolean;
|
|
19
19
|
acceptRanges?: boolean;
|
|
20
20
|
lastModified?: boolean;
|
|
@@ -34,7 +34,7 @@ export default class Service {
|
|
|
34
34
|
* @param root The file service root.
|
|
35
35
|
* @param options The file service options.
|
|
36
36
|
*/
|
|
37
|
-
constructor(root: string, options
|
|
37
|
+
constructor(root: string, options?: Options);
|
|
38
38
|
/**
|
|
39
39
|
* @private
|
|
40
40
|
* @method isIgnore
|
package/types/index.d.ts
CHANGED
|
@@ -2,10 +2,8 @@
|
|
|
2
2
|
* @module index
|
|
3
3
|
*/
|
|
4
4
|
import { Middleware } from 'koa';
|
|
5
|
-
import { FileSystem } from './utils/fs';
|
|
6
5
|
import { Options as ServiceOptions } from './Service';
|
|
7
|
-
export interface Options extends
|
|
8
|
-
fs?: FileSystem;
|
|
6
|
+
export interface Options extends ServiceOptions {
|
|
9
7
|
defer?: boolean;
|
|
10
8
|
}
|
|
11
9
|
/**
|