@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.
@@ -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 function compressionMiddleware(): (req: FFRequest, res: FFResponse, next: () => void) => void;
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 = (chunk, encoding) => {
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, encoding);
50
+ return compressionStream.write(chunk, enc, callback);
36
51
  };
37
52
  // @ts-ignore
38
- res.end = (chunk, encoding) => {
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 (chunk) {
43
- // @ts-ignore
44
- res.write(chunk, encoding);
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 function compressionMiddleware() {
51
- return (req, res, next) => {
52
- compression(req, res);
53
- next();
54
- };
55
- }
77
+ export const compressionMiddleware = (req, res, next) => {
78
+ compression(req, res);
79
+ next();
80
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.5.8",
3
+ "version": "0.5.9",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",