wok-server 0.1.4 → 0.1.5
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/dist/mvc/render/file.js +22 -5
- package/package.json +1 -1
package/dist/mvc/render/file.js
CHANGED
|
@@ -5,6 +5,7 @@ const fs_1 = require("fs");
|
|
|
5
5
|
const promises_1 = require("fs/promises");
|
|
6
6
|
const json_1 = require("./json");
|
|
7
7
|
const path_1 = require("path");
|
|
8
|
+
const zlib_1 = require("zlib");
|
|
8
9
|
/**
|
|
9
10
|
* 常用的 content-type 对照表
|
|
10
11
|
*/
|
|
@@ -143,7 +144,7 @@ async function renderFile(request, response, filePath, download = false) {
|
|
|
143
144
|
const rangeHeader = request.headers['range'];
|
|
144
145
|
if (!rangeHeader) {
|
|
145
146
|
response.setHeader('Last-Modified', statRes.mtime.toUTCString());
|
|
146
|
-
return streamFile(filePath, response);
|
|
147
|
+
return streamFile(filePath, request, response);
|
|
147
148
|
}
|
|
148
149
|
// 解析,range 示例:bytes=200-1000, 2000-6576, 19000-
|
|
149
150
|
// 多段的情况,暂时不做支持,非常麻烦,段数多还可能会有效率问题
|
|
@@ -151,11 +152,11 @@ async function renderFile(request, response, filePath, download = false) {
|
|
|
151
152
|
const ranges = rangeHeader.split(',');
|
|
152
153
|
let range = ranges.length ? ranges[0] : undefined;
|
|
153
154
|
if (!range) {
|
|
154
|
-
return streamFile(filePath, response);
|
|
155
|
+
return streamFile(filePath, request, response);
|
|
155
156
|
}
|
|
156
157
|
range = range.trim();
|
|
157
158
|
if (!range.startsWith('bytes=')) {
|
|
158
|
-
return streamFile(filePath, response);
|
|
159
|
+
return streamFile(filePath, request, response);
|
|
159
160
|
}
|
|
160
161
|
range = range.substring(6);
|
|
161
162
|
const strs = range.split('-');
|
|
@@ -178,10 +179,10 @@ async function renderFile(request, response, filePath, download = false) {
|
|
|
178
179
|
// 注:Range 和 Content-Range 还有 createReadStream 中的字节范围,都是前后全包含的
|
|
179
180
|
// Content-Range: bytes 42-1233/1234
|
|
180
181
|
response.setHeader('Content-Range', `bytes ${start}-${end}/${statRes.size}`);
|
|
181
|
-
return streamFile(filePath, response, { start, end });
|
|
182
|
+
return streamFile(filePath, request, response, { start, end });
|
|
182
183
|
}
|
|
183
184
|
exports.renderFile = renderFile;
|
|
184
|
-
function streamFile(filePath, response, opts) {
|
|
185
|
+
function streamFile(filePath, request, response, opts) {
|
|
185
186
|
return new Promise((res, rej) => {
|
|
186
187
|
if (opts && typeof opts.start === 'number') {
|
|
187
188
|
// 部分返回 206
|
|
@@ -191,6 +192,22 @@ function streamFile(filePath, response, opts) {
|
|
|
191
192
|
// 全部返回 200
|
|
192
193
|
response.statusCode = 200;
|
|
193
194
|
}
|
|
195
|
+
// 支持 gzip
|
|
196
|
+
const acceptEncoding = request.headers['accept-encoding'];
|
|
197
|
+
if (acceptEncoding) {
|
|
198
|
+
// Accept-Encoding: br;q=1.0, gzip;q=0.8, *;q=0.1
|
|
199
|
+
const acceptEncodings = acceptEncoding
|
|
200
|
+
.trim()
|
|
201
|
+
.split(',')
|
|
202
|
+
.map(item => item.trim())
|
|
203
|
+
.map(item => item.split(';')[0]);
|
|
204
|
+
if (acceptEncodings.includes('gzip') || acceptEncodings.includes('*')) {
|
|
205
|
+
response.setHeader('Content-Encoding', 'gzip');
|
|
206
|
+
(0, fs_1.createReadStream)(filePath, opts).pipe((0, zlib_1.createGzip)()).pipe(response);
|
|
207
|
+
response.once('finish', res).once('error', rej);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
194
211
|
(0, fs_1.createReadStream)(filePath, opts).pipe(response);
|
|
195
212
|
response.once('finish', res).once('error', rej);
|
|
196
213
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wok-server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"packageManager": "pnpm@8.9.0",
|
|
5
5
|
"description": "一个基于 NodeJs 和 TypeScript 的后端框架,轻量级、克制、简洁。A lightweight, restrained, and concise backend framework based on Node.js and TypeScript.",
|
|
6
6
|
"scripts": {
|