koa-files 3.1.6 → 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 CHANGED
@@ -18,7 +18,7 @@ $ npm install koa-files
18
18
 
19
19
  ```ts
20
20
  import { Middleware } from 'koa';
21
- import { createReadStream, stat, Stats } from 'fs';
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
- readonly stat: typeof stat;
37
- readonly createReadStream: typeof createReadStream;
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
- ##### `headers`
68
+ ##### `defer`
66
69
 
67
- - Defaults to `undefined`.
68
- - Set headers to be sent.
69
- - See docs: [Headers in MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers).
70
+ - Defaults to `false`.
71
+ - If true, serves after `await next()`.
72
+ - Allowing any downstream middleware to respond first.
70
73
 
71
- ##### `acceptRanges`
74
+ ##### `etag`
72
75
 
73
76
  - Defaults to `true`.
74
- - Enable or disable accepting ranged requests.
75
- - Disabling this will not send Accept-Ranges and ignore the contents of the Range request header.
77
+ - Enable or disable etag generation.
78
+ - Use weak etag internally.
76
79
  - Can be overridden by the `headers`.
77
80
 
78
- ##### `etag`
81
+ ##### `acceptRanges`
79
82
 
80
83
  - Defaults to `true`.
81
- - Enable or disable etag generation.
82
- - Use weak etag internally.
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
- ##### `defer`
105
+ ##### `headers`
103
106
 
104
- - Defaults to `false`.
105
- - If true, serves after `await next()`.
106
- - Allowing any downstream middleware to respond first.
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.6
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('./utils/fs.cjs');
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: fs$1, highWaterMark } = this.options;
193
+ const { fs, highWaterMark } = this.options;
192
194
  // Set response body.
193
- context.body = new stream.FileReadStream(path$2, ranges, { fs: fs$1, highWaterMark });
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.6
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 config = Object.assign({ fs: fs__default.default }, options);
31
- const service = new Service(root, config);
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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 3.1.6
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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 3.1.6
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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 3.1.6
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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 3.1.6
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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 3.1.6
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 = 65536 } = options;
27
+ const { fs, encoding, highWaterMark } = options;
28
28
  super({ encoding, highWaterMark });
29
29
  this.bytesRead = 0;
30
30
  this.fd = null;
@@ -126,7 +126,7 @@ class FileReadStream extends stream.Readable {
126
126
  const position = range.offset + bytesRead;
127
127
  const buffer$1 = buffer.Buffer.allocUnsafeSlow(Math.min(size, range.length - bytesRead));
128
128
  // Read file range.
129
- this.fs.read(fd, buffer$1, 0, buffer$1.length, position, (readError, bytesRead, buffer) => {
129
+ this.fs.read(fd, buffer$1, 0, buffer$1.length, position, (readError, bytesRead, buffer$1) => {
130
130
  this.reading = false;
131
131
  // Tell ._destroy() that it's safe to close the fd now.
132
132
  if (this.destroyed) {
@@ -136,7 +136,15 @@ class FileReadStream extends stream.Readable {
136
136
  this.destroy(readError);
137
137
  } else {
138
138
  if (bytesRead > 0) {
139
- this.push(buffer.subarray(0, bytesRead));
139
+ if (bytesRead !== buffer$1.length) {
140
+ // Slow path. Shrink to fit.
141
+ // Copy instead of slice so that we don't retain
142
+ // large backing buffer for small reads.
143
+ const chunk = buffer.Buffer.allocUnsafeSlow(bytesRead);
144
+ buffer$1.copy(chunk, 0, 0, bytesRead);
145
+ buffer$1 = chunk;
146
+ }
147
+ this.push(buffer$1);
140
148
  this.bytesRead += bytesRead;
141
149
  } else {
142
150
  this.bytesRead = 0;
package/esm/Service.js CHANGED
@@ -1,13 +1,14 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 3.1.6
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.6
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 config = Object.assign({ fs }, options);
23
- const service = new Service(root, config);
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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 3.1.6
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
package/esm/utils/hash.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 3.1.6
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
package/esm/utils/http.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 3.1.6
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
package/esm/utils/path.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 3.1.6
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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 3.1.6
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 = 65536 } = options;
25
+ const { fs, encoding, highWaterMark } = options;
26
26
  super({ encoding, highWaterMark });
27
27
  this.bytesRead = 0;
28
28
  this.fd = null;
@@ -134,7 +134,15 @@ class FileReadStream extends Readable {
134
134
  this.destroy(readError);
135
135
  } else {
136
136
  if (bytesRead > 0) {
137
- this.push(buffer.subarray(0, bytesRead));
137
+ if (bytesRead !== buffer.length) {
138
+ // Slow path. Shrink to fit.
139
+ // Copy instead of slice so that we don't retain
140
+ // large backing buffer for small reads.
141
+ const chunk = Buffer.allocUnsafeSlow(bytesRead);
142
+ buffer.copy(chunk, 0, 0, bytesRead);
143
+ buffer = chunk;
144
+ }
145
+ this.push(buffer);
138
146
  this.bytesRead += bytesRead;
139
147
  } else {
140
148
  this.bytesRead = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koa-files",
3
- "version": "3.1.6",
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": {
@@ -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: FileSystem;
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: 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 Omit<ServiceOptions, 'fs'> {
8
- fs?: FileSystem;
6
+ export interface Options extends ServiceOptions {
9
7
  defer?: boolean;
10
8
  }
11
9
  /**