koa-files 3.1.1 → 3.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 3.1.1
4
+ * @version 3.1.2
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 3.1.1
4
+ * @version 3.1.2
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 3.1.1
4
+ * @version 3.1.2
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.1
4
+ * @version 3.1.2
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.1
4
+ * @version 3.1.2
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.1
4
+ * @version 3.1.2
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.1
4
+ * @version 3.1.2
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
@@ -84,7 +84,7 @@ class FileReadStream extends stream.Readable {
84
84
  */
85
85
  _construct(callback) {
86
86
  this.fs.open(this.path, 'r', (openError, fd) => {
87
- if (openError === null) {
87
+ if (openError == null) {
88
88
  this.fd = fd;
89
89
  }
90
90
  callback(openError);
@@ -121,34 +121,32 @@ class FileReadStream extends stream.Readable {
121
121
  let bytesRead = 0;
122
122
  const padding = this.getPadding(range);
123
123
  const hasRangePadding = padding != null;
124
- const length = hasRangePadding ? padding.length : 0;
125
- if (hasRangePadding && length > 0) {
124
+ if (hasRangePadding) {
125
+ const { length } = padding;
126
126
  const begin = this.bytesRead;
127
- bytesRead = Math.min(size, length - begin);
128
- const end = begin + bytesRead;
129
- this.push(padding.subarray(begin, end));
130
- this.bytesRead = end;
127
+ if (length > 0 && begin < length) {
128
+ bytesRead = Math.min(size, length - begin);
129
+ const end = begin + bytesRead;
130
+ this.push(padding.subarray(begin, end));
131
+ this.bytesRead = end;
132
+ }
131
133
  }
132
- if (!hasRangePadding || this.bytesRead >= length) {
134
+ if (bytesRead <= 0) {
133
135
  this.bytesRead = 0;
134
- const hasBytesRead = bytesRead > 0;
135
- switch (this.readState) {
136
+ const { readState } = this;
137
+ switch (readState) {
136
138
  case 0 /* ReadState.PREFIX */:
137
139
  this.readState = 1 /* ReadState.RANGE */;
138
- if (!hasBytesRead) {
139
- this.readFileRange(fd, range, size);
140
- }
140
+ this.readFileRange(fd, range, size);
141
141
  break;
142
142
  case 2 /* ReadState.SUFFIX */:
143
143
  this.currentRangeIndex++;
144
144
  this.readState = 0 /* ReadState.PREFIX */;
145
- if (!hasBytesRead) {
146
- const range = this.getRange();
147
- if (range == null) {
148
- this.push(null);
149
- } else {
150
- this.readFilePadding(fd, range, size);
151
- }
145
+ const nextRange = this.getRange();
146
+ if (nextRange == null) {
147
+ this.push(null);
148
+ } else {
149
+ this.readFilePadding(fd, nextRange, size);
152
150
  }
153
151
  break;
154
152
  }
@@ -167,7 +165,9 @@ class FileReadStream extends stream.Readable {
167
165
  const position = range.offset + bytesRead;
168
166
  const buffer$1 = buffer.Buffer.alloc(Math.min(size, range.length - bytesRead));
169
167
  this.fs.read(fd, buffer$1, 0, buffer$1.length, position, (readError, bytesRead, buffer) => {
170
- if (readError === null) {
168
+ if (readError != null) {
169
+ this.destroy(readError);
170
+ } else {
171
171
  if (bytesRead > 0) {
172
172
  this.push(buffer.subarray(0, bytesRead));
173
173
  this.bytesRead += bytesRead;
@@ -176,8 +176,6 @@ class FileReadStream extends stream.Readable {
176
176
  this.readState = 2 /* ReadState.SUFFIX */;
177
177
  this.readFilePadding(fd, range, size);
178
178
  }
179
- } else {
180
- this.destroy(readError);
181
179
  }
182
180
  this.reading = false;
183
181
  });
@@ -191,8 +189,11 @@ class FileReadStream extends stream.Readable {
191
189
  if (!this.reading) {
192
190
  const { fd } = this;
193
191
  const range = this.getRange();
194
- if (fd != null && range != null) {
195
- switch (this.readState) {
192
+ if (fd == null || range == null) {
193
+ this.push(null);
194
+ } else {
195
+ const { readState } = this;
196
+ switch (readState) {
196
197
  case 0 /* ReadState.PREFIX */:
197
198
  case 2 /* ReadState.SUFFIX */:
198
199
  this.readFilePadding(fd, range, size);
@@ -200,11 +201,7 @@ class FileReadStream extends stream.Readable {
200
201
  case 1 /* ReadState.RANGE */:
201
202
  this.readFileRange(fd, range, size);
202
203
  break;
203
- default:
204
- throw new Error(`invalid read state: ${this.readState}`);
205
204
  }
206
- } else {
207
- this.push(null);
208
205
  }
209
206
  }
210
207
  }
package/esm/Service.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package koa-files
3
3
  * @license MIT
4
- * @version 3.1.1
4
+ * @version 3.1.2
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 3.1.1
4
+ * @version 3.1.2
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 3.1.1
4
+ * @version 3.1.2
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.1
4
+ * @version 3.1.2
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.1
4
+ * @version 3.1.2
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.1
4
+ * @version 3.1.2
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.1
4
+ * @version 3.1.2
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
@@ -82,7 +82,7 @@ class FileReadStream extends Readable {
82
82
  */
83
83
  _construct(callback) {
84
84
  this.fs.open(this.path, 'r', (openError, fd) => {
85
- if (openError === null) {
85
+ if (openError == null) {
86
86
  this.fd = fd;
87
87
  }
88
88
  callback(openError);
@@ -119,34 +119,32 @@ class FileReadStream extends Readable {
119
119
  let bytesRead = 0;
120
120
  const padding = this.getPadding(range);
121
121
  const hasRangePadding = padding != null;
122
- const length = hasRangePadding ? padding.length : 0;
123
- if (hasRangePadding && length > 0) {
122
+ if (hasRangePadding) {
123
+ const { length } = padding;
124
124
  const begin = this.bytesRead;
125
- bytesRead = Math.min(size, length - begin);
126
- const end = begin + bytesRead;
127
- this.push(padding.subarray(begin, end));
128
- this.bytesRead = end;
125
+ if (length > 0 && begin < length) {
126
+ bytesRead = Math.min(size, length - begin);
127
+ const end = begin + bytesRead;
128
+ this.push(padding.subarray(begin, end));
129
+ this.bytesRead = end;
130
+ }
129
131
  }
130
- if (!hasRangePadding || this.bytesRead >= length) {
132
+ if (bytesRead <= 0) {
131
133
  this.bytesRead = 0;
132
- const hasBytesRead = bytesRead > 0;
133
- switch (this.readState) {
134
+ const { readState } = this;
135
+ switch (readState) {
134
136
  case 0 /* ReadState.PREFIX */:
135
137
  this.readState = 1 /* ReadState.RANGE */;
136
- if (!hasBytesRead) {
137
- this.readFileRange(fd, range, size);
138
- }
138
+ this.readFileRange(fd, range, size);
139
139
  break;
140
140
  case 2 /* ReadState.SUFFIX */:
141
141
  this.currentRangeIndex++;
142
142
  this.readState = 0 /* ReadState.PREFIX */;
143
- if (!hasBytesRead) {
144
- const range = this.getRange();
145
- if (range == null) {
146
- this.push(null);
147
- } else {
148
- this.readFilePadding(fd, range, size);
149
- }
143
+ const nextRange = this.getRange();
144
+ if (nextRange == null) {
145
+ this.push(null);
146
+ } else {
147
+ this.readFilePadding(fd, nextRange, size);
150
148
  }
151
149
  break;
152
150
  }
@@ -165,7 +163,9 @@ class FileReadStream extends Readable {
165
163
  const position = range.offset + bytesRead;
166
164
  const buffer = Buffer.alloc(Math.min(size, range.length - bytesRead));
167
165
  this.fs.read(fd, buffer, 0, buffer.length, position, (readError, bytesRead, buffer) => {
168
- if (readError === null) {
166
+ if (readError != null) {
167
+ this.destroy(readError);
168
+ } else {
169
169
  if (bytesRead > 0) {
170
170
  this.push(buffer.subarray(0, bytesRead));
171
171
  this.bytesRead += bytesRead;
@@ -174,8 +174,6 @@ class FileReadStream extends Readable {
174
174
  this.readState = 2 /* ReadState.SUFFIX */;
175
175
  this.readFilePadding(fd, range, size);
176
176
  }
177
- } else {
178
- this.destroy(readError);
179
177
  }
180
178
  this.reading = false;
181
179
  });
@@ -189,8 +187,11 @@ class FileReadStream extends Readable {
189
187
  if (!this.reading) {
190
188
  const { fd } = this;
191
189
  const range = this.getRange();
192
- if (fd != null && range != null) {
193
- switch (this.readState) {
190
+ if (fd == null || range == null) {
191
+ this.push(null);
192
+ } else {
193
+ const { readState } = this;
194
+ switch (readState) {
194
195
  case 0 /* ReadState.PREFIX */:
195
196
  case 2 /* ReadState.SUFFIX */:
196
197
  this.readFilePadding(fd, range, size);
@@ -198,11 +199,7 @@ class FileReadStream extends Readable {
198
199
  case 1 /* ReadState.RANGE */:
199
200
  this.readFileRange(fd, range, size);
200
201
  break;
201
- default:
202
- throw new Error(`invalid read state: ${this.readState}`);
203
202
  }
204
- } else {
205
- this.push(null);
206
203
  }
207
204
  }
208
205
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koa-files",
3
- "version": "3.1.1",
3
+ "version": "3.1.2",
4
4
  "description": "A static files serving middleware for koa.",
5
5
  "type": "module",
6
6
  "main": "cjs/index.cjs",
@@ -56,7 +56,7 @@
56
56
  "dependencies": {
57
57
  "etag": "^1.8.1",
58
58
  "range-parser": "^1.2.1",
59
- "tslib": "^2.7.0"
59
+ "tslib": "^2.8.0"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@rollup/plugin-typescript": "^12.1.0",