koa-files 3.1.1 → 3.1.3

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.3
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
@@ -44,18 +44,6 @@ class Service {
44
44
  * @param options The file service options.
45
45
  */
46
46
  constructor(root, options) {
47
- Object.defineProperty(this, 'root', {
48
- enumerable: true,
49
- configurable: true,
50
- writable: true,
51
- value: void 0
52
- });
53
- Object.defineProperty(this, 'options', {
54
- enumerable: true,
55
- configurable: true,
56
- writable: true,
57
- value: void 0
58
- });
59
47
  this.options = options;
60
48
  this.root = path.unixify(path$1.resolve(root));
61
49
  }
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.3
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.3
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.3
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.3
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.3
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.3
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
@@ -25,54 +25,10 @@ class FileReadStream extends stream.Readable {
25
25
  constructor(path, ranges, options) {
26
26
  const { fs, encoding, highWaterMark } = options;
27
27
  super({ encoding, highWaterMark });
28
- Object.defineProperty(this, 'fd', {
29
- enumerable: true,
30
- configurable: true,
31
- writable: true,
32
- value: void 0
33
- });
34
- Object.defineProperty(this, 'fs', {
35
- enumerable: true,
36
- configurable: true,
37
- writable: true,
38
- value: void 0
39
- });
40
- Object.defineProperty(this, 'path', {
41
- enumerable: true,
42
- configurable: true,
43
- writable: true,
44
- value: void 0
45
- });
46
- Object.defineProperty(this, 'ranges', {
47
- enumerable: true,
48
- configurable: true,
49
- writable: true,
50
- value: void 0
51
- });
52
- Object.defineProperty(this, 'bytesRead', {
53
- enumerable: true,
54
- configurable: true,
55
- writable: true,
56
- value: 0
57
- });
58
- Object.defineProperty(this, 'reading', {
59
- enumerable: true,
60
- configurable: true,
61
- writable: true,
62
- value: false
63
- });
64
- Object.defineProperty(this, 'currentRangeIndex', {
65
- enumerable: true,
66
- configurable: true,
67
- writable: true,
68
- value: 0
69
- });
70
- Object.defineProperty(this, 'readState', {
71
- enumerable: true,
72
- configurable: true,
73
- writable: true,
74
- value: 0 /* ReadState.PREFIX */
75
- });
28
+ this.bytesRead = 0;
29
+ this.reading = false;
30
+ this.currentRangeIndex = 0;
31
+ this.readState = 0 /* ReadState.PREFIX */;
76
32
  this.fs = fs;
77
33
  this.path = path;
78
34
  this.ranges = ranges;
@@ -84,7 +40,7 @@ class FileReadStream extends stream.Readable {
84
40
  */
85
41
  _construct(callback) {
86
42
  this.fs.open(this.path, 'r', (openError, fd) => {
87
- if (openError === null) {
43
+ if (openError == null) {
88
44
  this.fd = fd;
89
45
  }
90
46
  callback(openError);
@@ -121,34 +77,32 @@ class FileReadStream extends stream.Readable {
121
77
  let bytesRead = 0;
122
78
  const padding = this.getPadding(range);
123
79
  const hasRangePadding = padding != null;
124
- const length = hasRangePadding ? padding.length : 0;
125
- if (hasRangePadding && length > 0) {
80
+ if (hasRangePadding) {
81
+ const { length } = padding;
126
82
  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;
83
+ if (length > 0 && begin < length) {
84
+ bytesRead = Math.min(size, length - begin);
85
+ const end = begin + bytesRead;
86
+ this.push(padding.subarray(begin, end));
87
+ this.bytesRead = end;
88
+ }
131
89
  }
132
- if (!hasRangePadding || this.bytesRead >= length) {
90
+ if (bytesRead <= 0) {
133
91
  this.bytesRead = 0;
134
- const hasBytesRead = bytesRead > 0;
135
- switch (this.readState) {
92
+ const { readState } = this;
93
+ switch (readState) {
136
94
  case 0 /* ReadState.PREFIX */:
137
95
  this.readState = 1 /* ReadState.RANGE */;
138
- if (!hasBytesRead) {
139
- this.readFileRange(fd, range, size);
140
- }
96
+ this.readFileRange(fd, range, size);
141
97
  break;
142
98
  case 2 /* ReadState.SUFFIX */:
143
99
  this.currentRangeIndex++;
144
100
  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
- }
101
+ const nextRange = this.getRange();
102
+ if (nextRange == null) {
103
+ this.push(null);
104
+ } else {
105
+ this.readFilePadding(fd, nextRange, size);
152
106
  }
153
107
  break;
154
108
  }
@@ -167,7 +121,9 @@ class FileReadStream extends stream.Readable {
167
121
  const position = range.offset + bytesRead;
168
122
  const buffer$1 = buffer.Buffer.alloc(Math.min(size, range.length - bytesRead));
169
123
  this.fs.read(fd, buffer$1, 0, buffer$1.length, position, (readError, bytesRead, buffer) => {
170
- if (readError === null) {
124
+ if (readError != null) {
125
+ this.destroy(readError);
126
+ } else {
171
127
  if (bytesRead > 0) {
172
128
  this.push(buffer.subarray(0, bytesRead));
173
129
  this.bytesRead += bytesRead;
@@ -176,8 +132,6 @@ class FileReadStream extends stream.Readable {
176
132
  this.readState = 2 /* ReadState.SUFFIX */;
177
133
  this.readFilePadding(fd, range, size);
178
134
  }
179
- } else {
180
- this.destroy(readError);
181
135
  }
182
136
  this.reading = false;
183
137
  });
@@ -191,8 +145,11 @@ class FileReadStream extends stream.Readable {
191
145
  if (!this.reading) {
192
146
  const { fd } = this;
193
147
  const range = this.getRange();
194
- if (fd != null && range != null) {
195
- switch (this.readState) {
148
+ if (fd == null || range == null) {
149
+ this.push(null);
150
+ } else {
151
+ const { readState } = this;
152
+ switch (readState) {
196
153
  case 0 /* ReadState.PREFIX */:
197
154
  case 2 /* ReadState.SUFFIX */:
198
155
  this.readFilePadding(fd, range, size);
@@ -200,11 +157,7 @@ class FileReadStream extends stream.Readable {
200
157
  case 1 /* ReadState.RANGE */:
201
158
  this.readFileRange(fd, range, size);
202
159
  break;
203
- default:
204
- throw new Error(`invalid read state: ${this.readState}`);
205
160
  }
206
- } else {
207
- this.push(null);
208
161
  }
209
162
  }
210
163
  }
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.3
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
@@ -36,18 +36,6 @@ class Service {
36
36
  * @param options The file service options.
37
37
  */
38
38
  constructor(root, options) {
39
- Object.defineProperty(this, 'root', {
40
- enumerable: true,
41
- configurable: true,
42
- writable: true,
43
- value: void 0
44
- });
45
- Object.defineProperty(this, 'options', {
46
- enumerable: true,
47
- configurable: true,
48
- writable: true,
49
- value: void 0
50
- });
51
39
  this.options = options;
52
40
  this.root = unixify(resolve(root));
53
41
  }
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.3
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.3
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.3
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.3
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.3
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.3
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
@@ -23,54 +23,10 @@ class FileReadStream extends Readable {
23
23
  constructor(path, ranges, options) {
24
24
  const { fs, encoding, highWaterMark } = options;
25
25
  super({ encoding, highWaterMark });
26
- Object.defineProperty(this, 'fd', {
27
- enumerable: true,
28
- configurable: true,
29
- writable: true,
30
- value: void 0
31
- });
32
- Object.defineProperty(this, 'fs', {
33
- enumerable: true,
34
- configurable: true,
35
- writable: true,
36
- value: void 0
37
- });
38
- Object.defineProperty(this, 'path', {
39
- enumerable: true,
40
- configurable: true,
41
- writable: true,
42
- value: void 0
43
- });
44
- Object.defineProperty(this, 'ranges', {
45
- enumerable: true,
46
- configurable: true,
47
- writable: true,
48
- value: void 0
49
- });
50
- Object.defineProperty(this, 'bytesRead', {
51
- enumerable: true,
52
- configurable: true,
53
- writable: true,
54
- value: 0
55
- });
56
- Object.defineProperty(this, 'reading', {
57
- enumerable: true,
58
- configurable: true,
59
- writable: true,
60
- value: false
61
- });
62
- Object.defineProperty(this, 'currentRangeIndex', {
63
- enumerable: true,
64
- configurable: true,
65
- writable: true,
66
- value: 0
67
- });
68
- Object.defineProperty(this, 'readState', {
69
- enumerable: true,
70
- configurable: true,
71
- writable: true,
72
- value: 0 /* ReadState.PREFIX */
73
- });
26
+ this.bytesRead = 0;
27
+ this.reading = false;
28
+ this.currentRangeIndex = 0;
29
+ this.readState = 0 /* ReadState.PREFIX */;
74
30
  this.fs = fs;
75
31
  this.path = path;
76
32
  this.ranges = ranges;
@@ -82,7 +38,7 @@ class FileReadStream extends Readable {
82
38
  */
83
39
  _construct(callback) {
84
40
  this.fs.open(this.path, 'r', (openError, fd) => {
85
- if (openError === null) {
41
+ if (openError == null) {
86
42
  this.fd = fd;
87
43
  }
88
44
  callback(openError);
@@ -119,34 +75,32 @@ class FileReadStream extends Readable {
119
75
  let bytesRead = 0;
120
76
  const padding = this.getPadding(range);
121
77
  const hasRangePadding = padding != null;
122
- const length = hasRangePadding ? padding.length : 0;
123
- if (hasRangePadding && length > 0) {
78
+ if (hasRangePadding) {
79
+ const { length } = padding;
124
80
  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;
81
+ if (length > 0 && begin < length) {
82
+ bytesRead = Math.min(size, length - begin);
83
+ const end = begin + bytesRead;
84
+ this.push(padding.subarray(begin, end));
85
+ this.bytesRead = end;
86
+ }
129
87
  }
130
- if (!hasRangePadding || this.bytesRead >= length) {
88
+ if (bytesRead <= 0) {
131
89
  this.bytesRead = 0;
132
- const hasBytesRead = bytesRead > 0;
133
- switch (this.readState) {
90
+ const { readState } = this;
91
+ switch (readState) {
134
92
  case 0 /* ReadState.PREFIX */:
135
93
  this.readState = 1 /* ReadState.RANGE */;
136
- if (!hasBytesRead) {
137
- this.readFileRange(fd, range, size);
138
- }
94
+ this.readFileRange(fd, range, size);
139
95
  break;
140
96
  case 2 /* ReadState.SUFFIX */:
141
97
  this.currentRangeIndex++;
142
98
  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
- }
99
+ const nextRange = this.getRange();
100
+ if (nextRange == null) {
101
+ this.push(null);
102
+ } else {
103
+ this.readFilePadding(fd, nextRange, size);
150
104
  }
151
105
  break;
152
106
  }
@@ -165,7 +119,9 @@ class FileReadStream extends Readable {
165
119
  const position = range.offset + bytesRead;
166
120
  const buffer = Buffer.alloc(Math.min(size, range.length - bytesRead));
167
121
  this.fs.read(fd, buffer, 0, buffer.length, position, (readError, bytesRead, buffer) => {
168
- if (readError === null) {
122
+ if (readError != null) {
123
+ this.destroy(readError);
124
+ } else {
169
125
  if (bytesRead > 0) {
170
126
  this.push(buffer.subarray(0, bytesRead));
171
127
  this.bytesRead += bytesRead;
@@ -174,8 +130,6 @@ class FileReadStream extends Readable {
174
130
  this.readState = 2 /* ReadState.SUFFIX */;
175
131
  this.readFilePadding(fd, range, size);
176
132
  }
177
- } else {
178
- this.destroy(readError);
179
133
  }
180
134
  this.reading = false;
181
135
  });
@@ -189,8 +143,11 @@ class FileReadStream extends Readable {
189
143
  if (!this.reading) {
190
144
  const { fd } = this;
191
145
  const range = this.getRange();
192
- if (fd != null && range != null) {
193
- switch (this.readState) {
146
+ if (fd == null || range == null) {
147
+ this.push(null);
148
+ } else {
149
+ const { readState } = this;
150
+ switch (readState) {
194
151
  case 0 /* ReadState.PREFIX */:
195
152
  case 2 /* ReadState.SUFFIX */:
196
153
  this.readFilePadding(fd, range, size);
@@ -198,11 +155,7 @@ class FileReadStream extends Readable {
198
155
  case 1 /* ReadState.RANGE */:
199
156
  this.readFileRange(fd, range, size);
200
157
  break;
201
- default:
202
- throw new Error(`invalid read state: ${this.readState}`);
203
158
  }
204
- } else {
205
- this.push(null);
206
159
  }
207
160
  }
208
161
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koa-files",
3
- "version": "3.1.1",
3
+ "version": "3.1.3",
4
4
  "description": "A static files serving middleware for koa.",
5
5
  "type": "module",
6
6
  "main": "cjs/index.cjs",
@@ -56,10 +56,10 @@
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
- "@rollup/plugin-typescript": "^12.1.0",
62
+ "@rollup/plugin-typescript": "^12.1.1",
63
63
  "@types/etag": "^1.8.3",
64
64
  "@types/koa": "^2.15.0",
65
65
  "@types/range-parser": "^1.2.7",