@wxn0brp/falcon-frame 0.5.8 → 0.5.9
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/compress.d.ts +2 -2
- package/dist/compress.js +38 -13
- package/package.json +1 -1
package/dist/compress.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { FFResponse } from "./res.js";
|
|
2
|
-
import { FFRequest } from "./types.js";
|
|
2
|
+
import { FFRequest, RouteHandler } from "./types.js";
|
|
3
3
|
export declare function compression(req: FFRequest, res: FFResponse): void;
|
|
4
|
-
export declare
|
|
4
|
+
export declare const compressionMiddleware: RouteHandler;
|
package/dist/compress.js
CHANGED
|
@@ -27,29 +27,54 @@ export function compression(req, res) {
|
|
|
27
27
|
compressionStream.on("end", () => {
|
|
28
28
|
originalEnd.call(res);
|
|
29
29
|
});
|
|
30
|
+
compressionStream.on("error", (err) => {
|
|
31
|
+
if (!res.headersSent) {
|
|
32
|
+
res.statusCode = 500;
|
|
33
|
+
originalEnd.call(res);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
res.destroy(err);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
res.on("close", () => {
|
|
40
|
+
compressionStream.destroy();
|
|
41
|
+
});
|
|
30
42
|
// @ts-ignore
|
|
31
|
-
res.write = (
|
|
43
|
+
res.write = (...args) => {
|
|
44
|
+
const [chunk, encoding, cb] = args;
|
|
45
|
+
const callback = typeof encoding === "function" ? encoding : cb;
|
|
46
|
+
const enc = typeof encoding === "function" ? undefined : encoding;
|
|
32
47
|
if (!res.headersSent) {
|
|
33
48
|
res.writeHead(res.statusCode);
|
|
34
49
|
}
|
|
35
|
-
return compressionStream.write(chunk,
|
|
50
|
+
return compressionStream.write(chunk, enc, callback);
|
|
36
51
|
};
|
|
37
52
|
// @ts-ignore
|
|
38
|
-
res.end = (
|
|
39
|
-
if (res.writableEnded)
|
|
53
|
+
res.end = (...args) => {
|
|
54
|
+
if (res.writableEnded)
|
|
40
55
|
return res;
|
|
56
|
+
const [chunk, encoding, cb] = args;
|
|
57
|
+
let finalChunk = chunk;
|
|
58
|
+
let finalCb = cb;
|
|
59
|
+
let finalEncoding = encoding;
|
|
60
|
+
if (typeof chunk === "function") {
|
|
61
|
+
finalCb = chunk;
|
|
62
|
+
finalChunk = undefined;
|
|
63
|
+
}
|
|
64
|
+
else if (typeof encoding === "function") {
|
|
65
|
+
finalCb = encoding;
|
|
66
|
+
finalEncoding = undefined;
|
|
41
67
|
}
|
|
42
|
-
if (
|
|
43
|
-
|
|
44
|
-
|
|
68
|
+
if (finalCb)
|
|
69
|
+
res.once("finish", finalCb);
|
|
70
|
+
if (finalChunk) {
|
|
71
|
+
res.write(finalChunk, finalEncoding);
|
|
45
72
|
}
|
|
46
73
|
compressionStream.end();
|
|
47
74
|
return res;
|
|
48
75
|
};
|
|
49
76
|
}
|
|
50
|
-
export
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
};
|
|
55
|
-
}
|
|
77
|
+
export const compressionMiddleware = (req, res, next) => {
|
|
78
|
+
compression(req, res);
|
|
79
|
+
next();
|
|
80
|
+
};
|