koa-files 4.0.7 → 4.1.1

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 CHANGED
@@ -26,7 +26,11 @@ interface Headers {
26
26
  }
27
27
 
28
28
  interface IgnoreFunction {
29
- (path: string): boolean | Promise<boolean>;
29
+ (path: string): Promise<boolean> | boolean;
30
+ }
31
+
32
+ interface HighWaterMarkFunction {
33
+ (path: string, stats: Stats): Promise<number> | number;
30
34
  }
31
35
 
32
36
  interface HeadersFunction {
@@ -41,14 +45,14 @@ export interface FileSystem {
41
45
  }
42
46
 
43
47
  export interface Options {
44
- fs?: FileSystem;
45
- defer?: boolean;
46
48
  etag?: boolean;
49
+ defer?: boolean;
50
+ fs?: FileSystem;
47
51
  acceptRanges?: boolean;
48
52
  lastModified?: boolean;
49
- highWaterMark?: number;
50
53
  ignore?: IgnoreFunction;
51
54
  headers?: Headers | HeadersFunction;
55
+ highWaterMark?: number | HighWaterMarkFunction;
52
56
  }
53
57
 
54
58
  export function server(root: string, options?: Options): Middleware;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
@@ -128,13 +128,13 @@ class ReadStream extends stream.Readable {
128
128
  const buffer$1 = buffer.Buffer.allocUnsafeSlow(bytesToRead);
129
129
  const position = range.offset + bytesRead;
130
130
  // Read file range.
131
- this.fs.read(fd, buffer$1, 0, bytesToRead, position, (error, bytesRead, buffer) => {
131
+ this.fs.read(fd, buffer$1, 0, bytesToRead, position, (readError, bytesRead, buffer) => {
132
132
  this.reading = false;
133
133
  // Tell ._destroy() that it's safe to close the fd now.
134
134
  if (this.destroyed) {
135
- this.emit(DISPOSE_EVENT, error);
136
- } else if (error != null) {
137
- this.destroy(error);
135
+ this.emit(DISPOSE_EVENT, readError);
136
+ } else if (readError != null) {
137
+ this.destroy(readError);
138
138
  } else if (bytesRead !== bytesToRead) {
139
139
  this.destroy(new RangeError('invalid read operation'));
140
140
  } else {
@@ -2,11 +2,12 @@
2
2
  * @module ReadStream
3
3
  */
4
4
  import { PathLike } from 'fs';
5
+ import { Readable } from 'stream';
5
6
  import { Range } from './utils/http.cjs';
6
7
  import { FileSystem } from './utils/fs.cjs';
7
- import { Readable, ReadableOptions } from 'stream';
8
- export interface Options extends Pick<ReadableOptions, 'highWaterMark'> {
8
+ export interface Options {
9
9
  fs: FileSystem;
10
+ highWaterMark?: number;
10
11
  }
11
12
  interface Callback {
12
13
  (error?: Error | null): void;
package/cjs/Service.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
@@ -38,20 +38,16 @@ class Service {
38
38
  * @param root The file service root.
39
39
  * @param options The file service options.
40
40
  */
41
- constructor(root, options) {
41
+ constructor(root, options = {}) {
42
+ var _a;
42
43
  this.root = path.unixify(path$1.resolve(root));
43
- this.options = Object.assign({ fs: fs__default.default, highWaterMark: 65536 }, options);
44
- }
45
- /**
46
- * @private
47
- * @method isIgnore
48
- * @description Check if path is ignore.
49
- * @param path The path to check.
50
- */
51
- async isIgnore(path) {
52
- const { ignore } = this.options;
53
- const isIgnored = type.isFunction(ignore) ? await ignore(path) : false;
54
- return isIgnored === true;
44
+ const { ignore, headers, highWaterMark = 65536 } = options;
45
+ this.options = Object.assign(Object.assign({}, options), {
46
+ fs: (_a = options.fs) !== null && _a !== void 0 ? _a : fs__default.default,
47
+ ignore: type.isFunction(ignore) ? ignore : () => false,
48
+ headers: type.isFunction(headers) ? headers : () => headers,
49
+ highWaterMark: type.isFunction(highWaterMark) ? highWaterMark : () => highWaterMark
50
+ });
55
51
  }
56
52
  /**
57
53
  * @private
@@ -63,21 +59,15 @@ class Service {
63
59
  */
64
60
  async setupHeaders({ response }, path, stats) {
65
61
  const { options } = this;
66
- const { headers } = options;
67
62
  // Set status.
68
63
  response.status = 200;
69
64
  // Set Content-Type.
70
65
  response.type = path$1.extname(path);
71
- // Set headers.
66
+ // Get headers.
67
+ const headers = await options.headers(path, stats);
68
+ // If headers not empty, set headers.
72
69
  if (headers) {
73
- if (type.isFunction(headers)) {
74
- const fields = await headers(path, stats);
75
- if (fields) {
76
- response.set(fields);
77
- }
78
- } else {
79
- response.set(headers);
80
- }
70
+ response.set(headers);
81
71
  }
82
72
  // Accept-Ranges.
83
73
  if (options.acceptRanges === false) {
@@ -131,12 +121,12 @@ class Service {
131
121
  if (path.isOutRoot(path$2, root)) {
132
122
  return false;
133
123
  }
124
+ // Get options.
125
+ const { options } = this;
134
126
  // Is ignore path or file (403).
135
- if (await this.isIgnore(path$2)) {
127
+ if (await options.ignore(path$2)) {
136
128
  return false;
137
129
  }
138
- // Get options.
139
- const { options } = this;
140
130
  // File stats.
141
131
  const stats = await fs$1.stat(options.fs, path$2);
142
132
  // Check file stats.
@@ -193,7 +183,10 @@ class Service {
193
183
  return context.throw(400);
194
184
  }
195
185
  // Set response body.
196
- response.body = new ReadStream.ReadStream(path$2, ranges, options);
186
+ response.body = new ReadStream.ReadStream(path$2, ranges, {
187
+ fs: options.fs,
188
+ highWaterMark: await options.highWaterMark(path$2, stats)
189
+ });
197
190
  // File found.
198
191
  return true;
199
192
  }
package/cjs/Service.d.cts CHANGED
@@ -8,19 +8,22 @@ interface Headers {
8
8
  [key: string]: string | string[];
9
9
  }
10
10
  interface IgnoreFunction {
11
- (path: string): boolean | Promise<boolean>;
11
+ (path: string): Promise<boolean> | boolean;
12
+ }
13
+ interface HighWaterMarkFunction {
14
+ (path: string, stats: Stats): Promise<number> | number;
12
15
  }
13
16
  interface HeadersFunction {
14
17
  (path: string, stats: Stats): Promise<Headers | void> | Headers | void;
15
18
  }
16
19
  export interface Options {
17
- fs?: FileSystem;
18
20
  etag?: boolean;
21
+ fs?: FileSystem;
19
22
  acceptRanges?: boolean;
20
23
  lastModified?: boolean;
21
- highWaterMark?: number;
22
24
  ignore?: IgnoreFunction;
23
25
  headers?: Headers | HeadersFunction;
26
+ highWaterMark?: number | HighWaterMarkFunction;
24
27
  }
25
28
  /**
26
29
  * @class Service
@@ -35,13 +38,6 @@ export declare class Service {
35
38
  * @param options The file service options.
36
39
  */
37
40
  constructor(root: string, options?: Options);
38
- /**
39
- * @private
40
- * @method isIgnore
41
- * @description Check if path is ignore.
42
- * @param path The path to check.
43
- */
44
- private isIgnore;
45
41
  /**
46
42
  * @private
47
43
  * @method setupHeaders
package/cjs/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
package/cjs/utils/fs.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
@@ -2,11 +2,12 @@
2
2
  * @module ReadStream
3
3
  */
4
4
  import { PathLike } from 'fs';
5
+ import { Readable } from 'stream';
5
6
  import { Range } from './utils/http.js';
6
7
  import { FileSystem } from './utils/fs.js';
7
- import { Readable, ReadableOptions } from 'stream';
8
- export interface Options extends Pick<ReadableOptions, 'highWaterMark'> {
8
+ export interface Options {
9
9
  fs: FileSystem;
10
+ highWaterMark?: number;
10
11
  }
11
12
  interface Callback {
12
13
  (error?: Error | null): void;
package/esm/ReadStream.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
@@ -126,13 +126,13 @@ class ReadStream extends Readable {
126
126
  const buffer = Buffer.allocUnsafeSlow(bytesToRead);
127
127
  const position = range.offset + bytesRead;
128
128
  // Read file range.
129
- this.fs.read(fd, buffer, 0, bytesToRead, position, (error, bytesRead, buffer) => {
129
+ this.fs.read(fd, buffer, 0, bytesToRead, position, (readError, bytesRead, buffer) => {
130
130
  this.reading = false;
131
131
  // Tell ._destroy() that it's safe to close the fd now.
132
132
  if (this.destroyed) {
133
- this.emit(DISPOSE_EVENT, error);
134
- } else if (error != null) {
135
- this.destroy(error);
133
+ this.emit(DISPOSE_EVENT, readError);
134
+ } else if (readError != null) {
135
+ this.destroy(readError);
136
136
  } else if (bytesRead !== bytesToRead) {
137
137
  this.destroy(new RangeError('invalid read operation'));
138
138
  } else {
package/esm/Service.d.ts CHANGED
@@ -8,19 +8,22 @@ interface Headers {
8
8
  [key: string]: string | string[];
9
9
  }
10
10
  interface IgnoreFunction {
11
- (path: string): boolean | Promise<boolean>;
11
+ (path: string): Promise<boolean> | boolean;
12
+ }
13
+ interface HighWaterMarkFunction {
14
+ (path: string, stats: Stats): Promise<number> | number;
12
15
  }
13
16
  interface HeadersFunction {
14
17
  (path: string, stats: Stats): Promise<Headers | void> | Headers | void;
15
18
  }
16
19
  export interface Options {
17
- fs?: FileSystem;
18
20
  etag?: boolean;
21
+ fs?: FileSystem;
19
22
  acceptRanges?: boolean;
20
23
  lastModified?: boolean;
21
- highWaterMark?: number;
22
24
  ignore?: IgnoreFunction;
23
25
  headers?: Headers | HeadersFunction;
26
+ highWaterMark?: number | HighWaterMarkFunction;
24
27
  }
25
28
  /**
26
29
  * @class Service
@@ -35,13 +38,6 @@ export declare class Service {
35
38
  * @param options The file service options.
36
39
  */
37
40
  constructor(root: string, options?: Options);
38
- /**
39
- * @private
40
- * @method isIgnore
41
- * @description Check if path is ignore.
42
- * @param path The path to check.
43
- */
44
- private isIgnore;
45
41
  /**
46
42
  * @private
47
43
  * @method setupHeaders
package/esm/Service.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
@@ -29,20 +29,16 @@ class Service {
29
29
  * @param root The file service root.
30
30
  * @param options The file service options.
31
31
  */
32
- constructor(root, options) {
32
+ constructor(root, options = {}) {
33
+ var _a;
33
34
  this.root = unixify(resolve(root));
34
- this.options = Object.assign({ fs, highWaterMark: 65536 }, options);
35
- }
36
- /**
37
- * @private
38
- * @method isIgnore
39
- * @description Check if path is ignore.
40
- * @param path The path to check.
41
- */
42
- async isIgnore(path) {
43
- const { ignore } = this.options;
44
- const isIgnored = isFunction(ignore) ? await ignore(path) : false;
45
- return isIgnored === true;
35
+ const { ignore, headers, highWaterMark = 65536 } = options;
36
+ this.options = Object.assign(Object.assign({}, options), {
37
+ fs: (_a = options.fs) !== null && _a !== void 0 ? _a : fs,
38
+ ignore: isFunction(ignore) ? ignore : () => false,
39
+ headers: isFunction(headers) ? headers : () => headers,
40
+ highWaterMark: isFunction(highWaterMark) ? highWaterMark : () => highWaterMark
41
+ });
46
42
  }
47
43
  /**
48
44
  * @private
@@ -54,21 +50,15 @@ class Service {
54
50
  */
55
51
  async setupHeaders({ response }, path, stats) {
56
52
  const { options } = this;
57
- const { headers } = options;
58
53
  // Set status.
59
54
  response.status = 200;
60
55
  // Set Content-Type.
61
56
  response.type = extname(path);
62
- // Set headers.
57
+ // Get headers.
58
+ const headers = await options.headers(path, stats);
59
+ // If headers not empty, set headers.
63
60
  if (headers) {
64
- if (isFunction(headers)) {
65
- const fields = await headers(path, stats);
66
- if (fields) {
67
- response.set(fields);
68
- }
69
- } else {
70
- response.set(headers);
71
- }
61
+ response.set(headers);
72
62
  }
73
63
  // Accept-Ranges.
74
64
  if (options.acceptRanges === false) {
@@ -122,12 +112,12 @@ class Service {
122
112
  if (isOutRoot(path, root)) {
123
113
  return false;
124
114
  }
115
+ // Get options.
116
+ const { options } = this;
125
117
  // Is ignore path or file (403).
126
- if (await this.isIgnore(path)) {
118
+ if (await options.ignore(path)) {
127
119
  return false;
128
120
  }
129
- // Get options.
130
- const { options } = this;
131
121
  // File stats.
132
122
  const stats = await stat(options.fs, path);
133
123
  // Check file stats.
@@ -184,7 +174,10 @@ class Service {
184
174
  return context.throw(400);
185
175
  }
186
176
  // Set response body.
187
- response.body = new ReadStream(path, ranges, options);
177
+ response.body = new ReadStream(path, ranges, {
178
+ fs: options.fs,
179
+ highWaterMark: await options.highWaterMark(path, stats)
180
+ });
188
181
  // File found.
189
182
  return true;
190
183
  }
package/esm/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
package/esm/utils/fs.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
package/esm/utils/hash.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
package/esm/utils/http.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
package/esm/utils/path.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
package/esm/utils/type.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.7
4
+ * @version 4.1.1
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koa-files",
3
- "version": "4.0.7",
3
+ "version": "4.1.1",
4
4
  "description": "A static files serving middleware for koa.",
5
5
  "type": "module",
6
6
  "main": "cjs/index.cjs",
@@ -54,11 +54,11 @@
54
54
  "@types/node": "^24.1.0",
55
55
  "@types/range-parser": "^1.2.7",
56
56
  "dts-paths": "^1.1.10",
57
- "koa": "^3.0.0",
57
+ "koa": "^3.0.1",
58
58
  "magic-string": "^0.30.17",
59
59
  "prettier": "^3.6.2",
60
60
  "rimraf": "^6.0.1",
61
- "rollup": "^4.45.1",
61
+ "rollup": "^4.46.1",
62
62
  "typescript": "^5.8.3"
63
63
  },
64
64
  "peerDependencies": {