koa-files 4.0.5 → 4.0.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.
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.5
4
+ * @version 4.0.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
@@ -70,20 +70,19 @@ class ReadStream extends stream.Readable {
70
70
  }
71
71
  /**
72
72
  * @private
73
- * @method readFilePadding
73
+ * @method readPadding
74
74
  * @param fd The file descriptor.
75
75
  * @param range The current range.
76
76
  * @param size The number of bytes to read.
77
77
  */
78
- readFilePadding(fd, range, size) {
78
+ readPadding(fd, range, size) {
79
79
  let bytesRead = 0;
80
80
  const padding = this.getPadding(range);
81
- const hasRangePadding = padding != null;
82
81
  // If padding exists.
83
- if (hasRangePadding) {
82
+ if (padding != null) {
84
83
  const { length } = padding;
85
84
  const begin = this.bytesRead;
86
- if (length > 0 && begin < length) {
85
+ if (begin < length) {
87
86
  bytesRead = Math.min(size, length - begin);
88
87
  const end = begin + bytesRead;
89
88
  this.push(padding.subarray(begin, end));
@@ -91,14 +90,13 @@ class ReadStream extends stream.Readable {
91
90
  }
92
91
  }
93
92
  // If no padding or read completed.
94
- if (bytesRead <= 0) {
93
+ if (bytesRead < size) {
95
94
  this.bytesRead = 0;
96
- const { readState } = this;
97
95
  // Change read state.
98
- switch (readState) {
96
+ switch (this.readState) {
99
97
  case 0 /* ReadState.PREFIX */:
100
98
  this.readState = 1 /* ReadState.RANGE */;
101
- this.readFileRange(fd, range, size);
99
+ this.readFileRange(fd, range, size - bytesRead);
102
100
  break;
103
101
  case 2 /* ReadState.SUFFIX */:
104
102
  this.currentRangeIndex++;
@@ -107,7 +105,7 @@ class ReadStream extends stream.Readable {
107
105
  if (nextRange == null) {
108
106
  this.push(null);
109
107
  } else {
110
- this.readFilePadding(fd, nextRange, size);
108
+ this.readPadding(fd, nextRange, size - bytesRead);
111
109
  }
112
110
  break;
113
111
  }
@@ -121,39 +119,40 @@ class ReadStream extends stream.Readable {
121
119
  * @param size The number of bytes to read.
122
120
  */
123
121
  readFileRange(fd, range, size) {
124
- this.reading = true;
122
+ const { length } = range;
125
123
  const { bytesRead } = this;
126
- const position = range.offset + bytesRead;
127
- const buffer$1 = buffer.Buffer.allocUnsafeSlow(Math.min(size, range.length - bytesRead));
128
- // Read file range.
129
- this.fs.read(fd, buffer$1, 0, buffer$1.length, position, (readError, bytesRead, buffer$1) => {
130
- this.reading = false;
131
- // Tell ._destroy() that it's safe to close the fd now.
132
- if (this.destroyed) {
133
- this.emit(DISPOSE_EVENT, readError);
134
- } else {
135
- if (readError != null) {
136
- this.destroy(readError);
124
+ // File range not finished.
125
+ if (bytesRead < length) {
126
+ this.reading = true;
127
+ const bytesToRead = Math.min(size, length - bytesRead);
128
+ const buffer$1 = buffer.Buffer.allocUnsafeSlow(bytesToRead);
129
+ const position = range.offset + bytesRead;
130
+ // Read file range.
131
+ this.fs.read(fd, buffer$1, 0, bytesToRead, position, (error, bytesRead, buffer) => {
132
+ this.reading = false;
133
+ // Tell ._destroy() that it's safe to close the fd now.
134
+ if (this.destroyed) {
135
+ this.emit(DISPOSE_EVENT, error);
136
+ } else if (error != null) {
137
+ this.destroy(error);
138
+ } else if (bytesRead !== bytesToRead) {
139
+ this.destroy(new RangeError('invalid read operation'));
137
140
  } else {
138
- if (bytesRead > 0) {
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);
148
- this.bytesRead += bytesRead;
149
- } else {
141
+ this.push(buffer);
142
+ this.bytesRead += bytesRead;
143
+ // file range read completed.
144
+ if (bytesRead < size) {
150
145
  this.bytesRead = 0;
151
146
  this.readState = 2 /* ReadState.SUFFIX */;
152
- this.readFilePadding(fd, range, size);
147
+ this.readPadding(fd, range, size - bytesRead);
153
148
  }
154
149
  }
155
- }
156
- });
150
+ });
151
+ } else {
152
+ this.bytesRead = 0;
153
+ this.readState = 2 /* ReadState.SUFFIX */;
154
+ this.readPadding(fd, range, size);
155
+ }
157
156
  }
158
157
  /**
159
158
  * @override
@@ -168,12 +167,11 @@ class ReadStream extends stream.Readable {
168
167
  if (fd == null || range == null) {
169
168
  this.push(null);
170
169
  } else {
171
- const { readState } = this;
172
170
  // Read bytes from range.
173
- switch (readState) {
171
+ switch (this.readState) {
174
172
  case 0 /* ReadState.PREFIX */:
175
173
  case 2 /* ReadState.SUFFIX */:
176
- this.readFilePadding(fd, range, size);
174
+ this.readPadding(fd, range, size);
177
175
  break;
178
176
  case 1 /* ReadState.RANGE */:
179
177
  this.readFileRange(fd, range, size);
@@ -46,12 +46,12 @@ export declare class ReadStream extends Readable {
46
46
  private getPadding;
47
47
  /**
48
48
  * @private
49
- * @method readFilePadding
49
+ * @method readPadding
50
50
  * @param fd The file descriptor.
51
51
  * @param range The current range.
52
52
  * @param size The number of bytes to read.
53
53
  */
54
- private readFilePadding;
54
+ private readPadding;
55
55
  /**
56
56
  * @private
57
57
  * @method readFileRange
package/cjs/Service.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.5
4
+ * @version 4.0.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
package/cjs/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.5
4
+ * @version 4.0.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
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.5
4
+ * @version 4.0.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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.5
4
+ * @version 4.0.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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.5
4
+ * @version 4.0.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
@@ -34,4 +34,4 @@ export declare function isPreconditionFailed({ request, response }: Context): bo
34
34
  * @param context The koa context.
35
35
  * @param stats The file stats.
36
36
  */
37
- export declare function parseRanges(context: Context, stats: Stats): Range[] | -1 | -2;
37
+ export declare function parseRanges(context: Context, stats: Stats): -1 | -2 | Range[];
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.5
4
+ * @version 4.0.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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.5
4
+ * @version 4.0.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
@@ -46,12 +46,12 @@ export declare class ReadStream extends Readable {
46
46
  private getPadding;
47
47
  /**
48
48
  * @private
49
- * @method readFilePadding
49
+ * @method readPadding
50
50
  * @param fd The file descriptor.
51
51
  * @param range The current range.
52
52
  * @param size The number of bytes to read.
53
53
  */
54
- private readFilePadding;
54
+ private readPadding;
55
55
  /**
56
56
  * @private
57
57
  * @method readFileRange
package/esm/ReadStream.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.5
4
+ * @version 4.0.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
@@ -68,20 +68,19 @@ class ReadStream extends Readable {
68
68
  }
69
69
  /**
70
70
  * @private
71
- * @method readFilePadding
71
+ * @method readPadding
72
72
  * @param fd The file descriptor.
73
73
  * @param range The current range.
74
74
  * @param size The number of bytes to read.
75
75
  */
76
- readFilePadding(fd, range, size) {
76
+ readPadding(fd, range, size) {
77
77
  let bytesRead = 0;
78
78
  const padding = this.getPadding(range);
79
- const hasRangePadding = padding != null;
80
79
  // If padding exists.
81
- if (hasRangePadding) {
80
+ if (padding != null) {
82
81
  const { length } = padding;
83
82
  const begin = this.bytesRead;
84
- if (length > 0 && begin < length) {
83
+ if (begin < length) {
85
84
  bytesRead = Math.min(size, length - begin);
86
85
  const end = begin + bytesRead;
87
86
  this.push(padding.subarray(begin, end));
@@ -89,14 +88,13 @@ class ReadStream extends Readable {
89
88
  }
90
89
  }
91
90
  // If no padding or read completed.
92
- if (bytesRead <= 0) {
91
+ if (bytesRead < size) {
93
92
  this.bytesRead = 0;
94
- const { readState } = this;
95
93
  // Change read state.
96
- switch (readState) {
94
+ switch (this.readState) {
97
95
  case 0 /* ReadState.PREFIX */:
98
96
  this.readState = 1 /* ReadState.RANGE */;
99
- this.readFileRange(fd, range, size);
97
+ this.readFileRange(fd, range, size - bytesRead);
100
98
  break;
101
99
  case 2 /* ReadState.SUFFIX */:
102
100
  this.currentRangeIndex++;
@@ -105,7 +103,7 @@ class ReadStream extends Readable {
105
103
  if (nextRange == null) {
106
104
  this.push(null);
107
105
  } else {
108
- this.readFilePadding(fd, nextRange, size);
106
+ this.readPadding(fd, nextRange, size - bytesRead);
109
107
  }
110
108
  break;
111
109
  }
@@ -119,39 +117,40 @@ class ReadStream extends Readable {
119
117
  * @param size The number of bytes to read.
120
118
  */
121
119
  readFileRange(fd, range, size) {
122
- this.reading = true;
120
+ const { length } = range;
123
121
  const { bytesRead } = this;
124
- const position = range.offset + bytesRead;
125
- const buffer = Buffer.allocUnsafeSlow(Math.min(size, range.length - bytesRead));
126
- // Read file range.
127
- this.fs.read(fd, buffer, 0, buffer.length, position, (readError, bytesRead, buffer) => {
128
- this.reading = false;
129
- // Tell ._destroy() that it's safe to close the fd now.
130
- if (this.destroyed) {
131
- this.emit(DISPOSE_EVENT, readError);
132
- } else {
133
- if (readError != null) {
134
- this.destroy(readError);
122
+ // File range not finished.
123
+ if (bytesRead < length) {
124
+ this.reading = true;
125
+ const bytesToRead = Math.min(size, length - bytesRead);
126
+ const buffer = Buffer.allocUnsafeSlow(bytesToRead);
127
+ const position = range.offset + bytesRead;
128
+ // Read file range.
129
+ this.fs.read(fd, buffer, 0, bytesToRead, position, (error, bytesRead, buffer) => {
130
+ this.reading = false;
131
+ // Tell ._destroy() that it's safe to close the fd now.
132
+ if (this.destroyed) {
133
+ this.emit(DISPOSE_EVENT, error);
134
+ } else if (error != null) {
135
+ this.destroy(error);
136
+ } else if (bytesRead !== bytesToRead) {
137
+ this.destroy(new RangeError('invalid read operation'));
135
138
  } else {
136
- if (bytesRead > 0) {
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);
146
- this.bytesRead += bytesRead;
147
- } else {
139
+ this.push(buffer);
140
+ this.bytesRead += bytesRead;
141
+ // file range read completed.
142
+ if (bytesRead < size) {
148
143
  this.bytesRead = 0;
149
144
  this.readState = 2 /* ReadState.SUFFIX */;
150
- this.readFilePadding(fd, range, size);
145
+ this.readPadding(fd, range, size - bytesRead);
151
146
  }
152
147
  }
153
- }
154
- });
148
+ });
149
+ } else {
150
+ this.bytesRead = 0;
151
+ this.readState = 2 /* ReadState.SUFFIX */;
152
+ this.readPadding(fd, range, size);
153
+ }
155
154
  }
156
155
  /**
157
156
  * @override
@@ -166,12 +165,11 @@ class ReadStream extends Readable {
166
165
  if (fd == null || range == null) {
167
166
  this.push(null);
168
167
  } else {
169
- const { readState } = this;
170
168
  // Read bytes from range.
171
- switch (readState) {
169
+ switch (this.readState) {
172
170
  case 0 /* ReadState.PREFIX */:
173
171
  case 2 /* ReadState.SUFFIX */:
174
- this.readFilePadding(fd, range, size);
172
+ this.readPadding(fd, range, size);
175
173
  break;
176
174
  case 1 /* ReadState.RANGE */:
177
175
  this.readFileRange(fd, range, size);
package/esm/Service.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.5
4
+ * @version 4.0.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
package/esm/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 4.0.5
4
+ * @version 4.0.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
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.5
4
+ * @version 4.0.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
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.5
4
+ * @version 4.0.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
@@ -34,4 +34,4 @@ export declare function isPreconditionFailed({ request, response }: Context): bo
34
34
  * @param context The koa context.
35
35
  * @param stats The file stats.
36
36
  */
37
- export declare function parseRanges(context: Context, stats: Stats): Range[] | -1 | -2;
37
+ export declare function parseRanges(context: Context, stats: Stats): -1 | -2 | Range[];
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.5
4
+ * @version 4.0.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
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.5
4
+ * @version 4.0.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
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.5
4
+ * @version 4.0.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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koa-files",
3
- "version": "4.0.5",
3
+ "version": "4.0.7",
4
4
  "description": "A static files serving middleware for koa.",
5
5
  "type": "module",
6
6
  "main": "cjs/index.cjs",
@@ -42,38 +42,37 @@
42
42
  "url": "https://github.com/nuintun/koa-files/issues"
43
43
  },
44
44
  "homepage": "https://github.com/nuintun/koa-files#readme",
45
- "scripts": {
46
- "lint": "tsc --noEmit",
47
- "test": "node tests/index.js",
48
- "clean": "node tools/clean.js",
49
- "prepublishOnly": "npm run build",
50
- "build:cjs": "rollup -c tools/rollup.cjs.js",
51
- "build:esm": "rollup -c tools/rollup.esm.js",
52
- "build:fix-types": "node tools/fix-types.js",
53
- "format": "prettier --write . --ignore-path .prettierignore",
54
- "build": "npm run clean && npm run build:main && npm run format",
55
- "build:main": "npm run build:cjs && npm run build:esm && npm run build:fix-types"
56
- },
57
45
  "dependencies": {
58
46
  "etag": "^1.8.1",
59
47
  "range-parser": "^1.2.1",
60
48
  "tslib": "^2.8.1"
61
49
  },
62
50
  "devDependencies": {
63
- "@rollup/plugin-typescript": "^12.1.2",
64
- "@types/etag": "^1.8.3",
51
+ "@rollup/plugin-typescript": "^12.1.4",
52
+ "@types/etag": "^1.8.4",
65
53
  "@types/koa": "^2.15.0",
66
- "@types/node": "^22.15.3",
54
+ "@types/node": "^24.1.0",
67
55
  "@types/range-parser": "^1.2.7",
68
- "dts-paths": "^1.1.6",
56
+ "dts-paths": "^1.1.10",
69
57
  "koa": "^3.0.0",
70
58
  "magic-string": "^0.30.17",
71
- "prettier": "^3.5.3",
59
+ "prettier": "^3.6.2",
72
60
  "rimraf": "^6.0.1",
73
- "rollup": "^4.40.1",
61
+ "rollup": "^4.45.1",
74
62
  "typescript": "^5.8.3"
75
63
  },
76
64
  "peerDependencies": {
77
65
  "koa": ">=2.0.0"
66
+ },
67
+ "scripts": {
68
+ "lint": "tsc --noEmit",
69
+ "test": "node tests/index.js",
70
+ "clean": "node tools/clean.js",
71
+ "build:cjs": "rollup -c tools/rollup.cjs.js",
72
+ "build:esm": "rollup -c tools/rollup.esm.js",
73
+ "build:fix-types": "node tools/fix-types.js",
74
+ "build": "pnpm clean && pnpm build:main && pnpm format",
75
+ "format": "prettier --write . --ignore-path .prettierignore",
76
+ "build:main": "pnpm build:cjs && pnpm build:esm && pnpm build:fix-types"
78
77
  }
79
- }
78
+ }