express-response-middleware 2.1.3 → 2.2.0
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/LICENSE +1 -1
- package/dist/{cjs/index.d.cts → index.d.mts} +20 -16
- package/dist/{esm/index.d.ts → index.d.ts} +20 -16
- package/dist/index.js +207 -0
- package/dist/index.mjs +180 -0
- package/package.json +21 -25
- package/dist/cjs/index.cjs +0 -196
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/esm/index.mjs +0 -191
- package/dist/esm/index.mjs.map +0 -1
package/LICENSE
CHANGED
|
@@ -1,24 +1,28 @@
|
|
|
1
|
-
import { Request,
|
|
2
|
-
|
|
3
|
-
type TransformJson<T = unknown, R = T> = (body: T, request: Request, response: Response) => R | Promise<R>;
|
|
4
|
-
declare const jsonMiddleware: <T = unknown>(fn: TransformJson<T>) => RequestHandler;
|
|
1
|
+
import { Request, RequestHandler, Response } from "express";
|
|
5
2
|
|
|
3
|
+
//#region src/endMiddleware.d.ts
|
|
6
4
|
type TransformEnd<T = unknown> = (request: Request, response: Response) => T | Promise<T>;
|
|
7
5
|
declare const endMiddleware: <T = unknown>(fn: TransformEnd<T>) => RequestHandler;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/jsonMiddleware.d.ts
|
|
8
|
+
type TransformJson<T = unknown, R = T> = (body: T, request: Request, response: Response) => R | Promise<R>;
|
|
9
|
+
declare const jsonMiddleware: <T = unknown>(fn: TransformJson<T>) => RequestHandler;
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/jsonpMiddleware.d.ts
|
|
12
12
|
type TransformJsonp<T = unknown, R = T> = (body: T, request: Request, response: Response) => R | Promise<R>;
|
|
13
13
|
declare const jsonpMiddleware: <T = unknown>(fn: TransformJsonp<T>) => RequestHandler;
|
|
14
|
-
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/writeMiddleware.d.ts
|
|
16
|
+
type TransformChunk = (chunk: string | Buffer, encoding: string | null, request: Request, response: Response) => void | string | Buffer | Promise<void> | Promise<string> | Promise<Buffer>;
|
|
17
|
+
declare const writeMiddleware: (fn: TransformChunk) => RequestHandler;
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/index.d.ts
|
|
15
20
|
declare global {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
21
|
+
namespace Express {
|
|
22
|
+
interface Response {
|
|
23
|
+
__isEnd?: boolean;
|
|
20
24
|
}
|
|
25
|
+
}
|
|
21
26
|
}
|
|
22
|
-
|
|
23
|
-
export { endMiddleware, jsonMiddleware, jsonpMiddleware, writeMiddleware };
|
|
24
|
-
export type { TransformChunk, TransformEnd, TransformJson, TransformJsonp };
|
|
27
|
+
//#endregion
|
|
28
|
+
export { TransformChunk, TransformEnd, TransformJson, TransformJsonp, endMiddleware, jsonMiddleware, jsonpMiddleware, writeMiddleware };
|
|
@@ -1,24 +1,28 @@
|
|
|
1
|
-
import { Request,
|
|
2
|
-
|
|
3
|
-
type TransformJson<T = unknown, R = T> = (body: T, request: Request, response: Response) => R | Promise<R>;
|
|
4
|
-
declare const jsonMiddleware: <T = unknown>(fn: TransformJson<T>) => RequestHandler;
|
|
1
|
+
import { Request, RequestHandler, Response } from "express";
|
|
5
2
|
|
|
3
|
+
//#region src/endMiddleware.d.ts
|
|
6
4
|
type TransformEnd<T = unknown> = (request: Request, response: Response) => T | Promise<T>;
|
|
7
5
|
declare const endMiddleware: <T = unknown>(fn: TransformEnd<T>) => RequestHandler;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/jsonMiddleware.d.ts
|
|
8
|
+
type TransformJson<T = unknown, R = T> = (body: T, request: Request, response: Response) => R | Promise<R>;
|
|
9
|
+
declare const jsonMiddleware: <T = unknown>(fn: TransformJson<T>) => RequestHandler;
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/jsonpMiddleware.d.ts
|
|
12
12
|
type TransformJsonp<T = unknown, R = T> = (body: T, request: Request, response: Response) => R | Promise<R>;
|
|
13
13
|
declare const jsonpMiddleware: <T = unknown>(fn: TransformJsonp<T>) => RequestHandler;
|
|
14
|
-
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/writeMiddleware.d.ts
|
|
16
|
+
type TransformChunk = (chunk: string | Buffer, encoding: string | null, request: Request, response: Response) => void | string | Buffer | Promise<void> | Promise<string> | Promise<Buffer>;
|
|
17
|
+
declare const writeMiddleware: (fn: TransformChunk) => RequestHandler;
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/index.d.ts
|
|
15
20
|
declare global {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
21
|
+
namespace Express {
|
|
22
|
+
interface Response {
|
|
23
|
+
__isEnd?: boolean;
|
|
20
24
|
}
|
|
25
|
+
}
|
|
21
26
|
}
|
|
22
|
-
|
|
23
|
-
export { endMiddleware, jsonMiddleware, jsonpMiddleware, writeMiddleware };
|
|
24
|
-
export type { TransformChunk, TransformEnd, TransformJson, TransformJsonp };
|
|
27
|
+
//#endregion
|
|
28
|
+
export { TransformChunk, TransformEnd, TransformJson, TransformJsonp, endMiddleware, jsonMiddleware, jsonpMiddleware, writeMiddleware };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
let node_async_hooks = require("node:async_hooks");
|
|
25
|
+
node_async_hooks = __toESM(node_async_hooks);
|
|
26
|
+
|
|
27
|
+
//#region src/utils/errorHandler.ts
|
|
28
|
+
const errorHandler = (err, _req, res, _next) => {
|
|
29
|
+
res.status(500).set("content-language", "en").json({ message: err.message }).end();
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/endMiddleware.ts
|
|
34
|
+
const endMiddleware = (fn) => (req, res, next) => {
|
|
35
|
+
const originalEndFn = res.end;
|
|
36
|
+
res.end = function() {
|
|
37
|
+
if (res.headersSent) return originalEndFn.apply(this, arguments);
|
|
38
|
+
let mayBePromise;
|
|
39
|
+
try {
|
|
40
|
+
mayBePromise = fn(req, res);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
res.end = originalEndFn;
|
|
43
|
+
errorHandler(e, req, res, next);
|
|
44
|
+
return res;
|
|
45
|
+
}
|
|
46
|
+
if (mayBePromise instanceof Promise) {
|
|
47
|
+
mayBePromise.then(() => {
|
|
48
|
+
if (res.headersSent) {
|
|
49
|
+
console.error("sending response while in endMiddleware is undefined behaviour");
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
originalEndFn.apply(this, arguments);
|
|
53
|
+
}).catch((e) => {
|
|
54
|
+
res.end = originalEndFn;
|
|
55
|
+
errorHandler(e, req, res, next);
|
|
56
|
+
});
|
|
57
|
+
res.end = function() {
|
|
58
|
+
return res;
|
|
59
|
+
};
|
|
60
|
+
} else {
|
|
61
|
+
if (res.headersSent) {
|
|
62
|
+
console.error("sending response while in endMiddleware is undefined behaviour");
|
|
63
|
+
return res;
|
|
64
|
+
}
|
|
65
|
+
return originalEndFn.apply(this, arguments);
|
|
66
|
+
}
|
|
67
|
+
return res;
|
|
68
|
+
};
|
|
69
|
+
next();
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/utils/asyncLocalStorage.ts
|
|
74
|
+
const isInSend$1 = new node_async_hooks.AsyncLocalStorage();
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/jsonMiddleware.ts
|
|
78
|
+
const jsonMiddleware = (fn) => (req, res, next) => {
|
|
79
|
+
const originalJsonFn = res.json;
|
|
80
|
+
const originalSendFn = res.send;
|
|
81
|
+
res.send = function() {
|
|
82
|
+
return isInSend$1.run(true, () => originalSendFn.apply(this, arguments));
|
|
83
|
+
};
|
|
84
|
+
res.json = function(json) {
|
|
85
|
+
const originalEndFn = res.end;
|
|
86
|
+
if (res.headersSent) return res;
|
|
87
|
+
let mayBePromise;
|
|
88
|
+
try {
|
|
89
|
+
mayBePromise = fn(json, req, res);
|
|
90
|
+
} catch (e) {
|
|
91
|
+
res.json = originalJsonFn;
|
|
92
|
+
errorHandler(e, req, res, next);
|
|
93
|
+
return res;
|
|
94
|
+
}
|
|
95
|
+
if (mayBePromise instanceof Promise) {
|
|
96
|
+
mayBePromise.then((result) => {
|
|
97
|
+
if (res.headersSent) return;
|
|
98
|
+
res.end = originalEndFn;
|
|
99
|
+
originalJsonFn.call(this, result === void 0 ? json : result);
|
|
100
|
+
}).catch((e) => {
|
|
101
|
+
res.json = originalJsonFn;
|
|
102
|
+
res.end = originalEndFn;
|
|
103
|
+
errorHandler(e, req, res, next);
|
|
104
|
+
});
|
|
105
|
+
res.end = function() {
|
|
106
|
+
if (isInSend$1.getStore()) originalEndFn.apply(this, arguments);
|
|
107
|
+
return res;
|
|
108
|
+
};
|
|
109
|
+
} else {
|
|
110
|
+
const result = mayBePromise;
|
|
111
|
+
if (res.headersSent) return res;
|
|
112
|
+
originalJsonFn.call(this, result === void 0 ? json : result);
|
|
113
|
+
}
|
|
114
|
+
return res;
|
|
115
|
+
};
|
|
116
|
+
next();
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region src/jsonpMiddleware.ts
|
|
121
|
+
const isInSend = new node_async_hooks.AsyncLocalStorage();
|
|
122
|
+
const jsonpMiddleware = (fn) => (req, res, next) => {
|
|
123
|
+
const originalJsonpFn = res.jsonp;
|
|
124
|
+
const originalSendFn = res.send;
|
|
125
|
+
res.send = function() {
|
|
126
|
+
return isInSend.run(true, () => originalSendFn.apply(this, arguments));
|
|
127
|
+
};
|
|
128
|
+
res.jsonp = function(json) {
|
|
129
|
+
const originalEndFn = res.end;
|
|
130
|
+
if (res.headersSent) return res;
|
|
131
|
+
let mayBePromise;
|
|
132
|
+
try {
|
|
133
|
+
mayBePromise = fn(json, req, res);
|
|
134
|
+
} catch (e) {
|
|
135
|
+
res.json = originalJsonpFn;
|
|
136
|
+
errorHandler(e, req, res, next);
|
|
137
|
+
return res;
|
|
138
|
+
}
|
|
139
|
+
if (mayBePromise instanceof Promise) {
|
|
140
|
+
mayBePromise.then((result) => {
|
|
141
|
+
if (res.headersSent) return;
|
|
142
|
+
res.end = originalEndFn;
|
|
143
|
+
originalJsonpFn.call(this, result === void 0 ? json : result);
|
|
144
|
+
}).catch((e) => {
|
|
145
|
+
res.jsonp = originalJsonpFn;
|
|
146
|
+
res.end = originalEndFn;
|
|
147
|
+
errorHandler(e, req, res, next);
|
|
148
|
+
});
|
|
149
|
+
res.end = function() {
|
|
150
|
+
if (isInSend.getStore()) originalEndFn.apply(this, arguments);
|
|
151
|
+
return res;
|
|
152
|
+
};
|
|
153
|
+
} else {
|
|
154
|
+
const result = mayBePromise;
|
|
155
|
+
if (res.headersSent) return res;
|
|
156
|
+
originalJsonpFn.call(this, result === void 0 ? json : result);
|
|
157
|
+
}
|
|
158
|
+
return res;
|
|
159
|
+
};
|
|
160
|
+
next();
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
//#endregion
|
|
164
|
+
//#region src/writeMiddleware.ts
|
|
165
|
+
const writeMiddleware = (fn) => (req, res, next) => {
|
|
166
|
+
const originalWrite = res.write;
|
|
167
|
+
const originalEndFn = res.end;
|
|
168
|
+
res.write = function(chunk) {
|
|
169
|
+
if (res.writableEnded) return false;
|
|
170
|
+
let mayBePromise;
|
|
171
|
+
try {
|
|
172
|
+
mayBePromise = fn(chunk, typeof arguments[1] === "string" ? arguments[1] : null, req, res);
|
|
173
|
+
} catch (e) {
|
|
174
|
+
errorHandler(e, req, res, next);
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
if (mayBePromise instanceof Promise) {
|
|
178
|
+
mayBePromise.then((result$1) => {
|
|
179
|
+
res.end = originalEndFn;
|
|
180
|
+
if (res.writableEnded) return false;
|
|
181
|
+
arguments[0] = result$1 === void 0 ? chunk : result$1;
|
|
182
|
+
const writeResponse = originalWrite.apply(res, arguments);
|
|
183
|
+
if (res.__isEnd) res.end();
|
|
184
|
+
return writeResponse;
|
|
185
|
+
}).catch((e) => {
|
|
186
|
+
res.end = originalEndFn;
|
|
187
|
+
errorHandler(e, req, res, next);
|
|
188
|
+
});
|
|
189
|
+
res.end = function() {
|
|
190
|
+
this.__isEnd = true;
|
|
191
|
+
return res;
|
|
192
|
+
};
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
const result = mayBePromise;
|
|
196
|
+
if (res.writableEnded) return false;
|
|
197
|
+
arguments[0] = result === void 0 ? chunk : result;
|
|
198
|
+
return originalWrite.apply(res, arguments);
|
|
199
|
+
};
|
|
200
|
+
next();
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
//#endregion
|
|
204
|
+
exports.endMiddleware = endMiddleware;
|
|
205
|
+
exports.jsonMiddleware = jsonMiddleware;
|
|
206
|
+
exports.jsonpMiddleware = jsonpMiddleware;
|
|
207
|
+
exports.writeMiddleware = writeMiddleware;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/errorHandler.ts
|
|
4
|
+
const errorHandler = (err, _req, res, _next) => {
|
|
5
|
+
res.status(500).set("content-language", "en").json({ message: err.message }).end();
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/endMiddleware.ts
|
|
10
|
+
const endMiddleware = (fn) => (req, res, next) => {
|
|
11
|
+
const originalEndFn = res.end;
|
|
12
|
+
res.end = function() {
|
|
13
|
+
if (res.headersSent) return originalEndFn.apply(this, arguments);
|
|
14
|
+
let mayBePromise;
|
|
15
|
+
try {
|
|
16
|
+
mayBePromise = fn(req, res);
|
|
17
|
+
} catch (e) {
|
|
18
|
+
res.end = originalEndFn;
|
|
19
|
+
errorHandler(e, req, res, next);
|
|
20
|
+
return res;
|
|
21
|
+
}
|
|
22
|
+
if (mayBePromise instanceof Promise) {
|
|
23
|
+
mayBePromise.then(() => {
|
|
24
|
+
if (res.headersSent) {
|
|
25
|
+
console.error("sending response while in endMiddleware is undefined behaviour");
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
originalEndFn.apply(this, arguments);
|
|
29
|
+
}).catch((e) => {
|
|
30
|
+
res.end = originalEndFn;
|
|
31
|
+
errorHandler(e, req, res, next);
|
|
32
|
+
});
|
|
33
|
+
res.end = function() {
|
|
34
|
+
return res;
|
|
35
|
+
};
|
|
36
|
+
} else {
|
|
37
|
+
if (res.headersSent) {
|
|
38
|
+
console.error("sending response while in endMiddleware is undefined behaviour");
|
|
39
|
+
return res;
|
|
40
|
+
}
|
|
41
|
+
return originalEndFn.apply(this, arguments);
|
|
42
|
+
}
|
|
43
|
+
return res;
|
|
44
|
+
};
|
|
45
|
+
next();
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/utils/asyncLocalStorage.ts
|
|
50
|
+
const isInSend$1 = new AsyncLocalStorage();
|
|
51
|
+
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/jsonMiddleware.ts
|
|
54
|
+
const jsonMiddleware = (fn) => (req, res, next) => {
|
|
55
|
+
const originalJsonFn = res.json;
|
|
56
|
+
const originalSendFn = res.send;
|
|
57
|
+
res.send = function() {
|
|
58
|
+
return isInSend$1.run(true, () => originalSendFn.apply(this, arguments));
|
|
59
|
+
};
|
|
60
|
+
res.json = function(json) {
|
|
61
|
+
const originalEndFn = res.end;
|
|
62
|
+
if (res.headersSent) return res;
|
|
63
|
+
let mayBePromise;
|
|
64
|
+
try {
|
|
65
|
+
mayBePromise = fn(json, req, res);
|
|
66
|
+
} catch (e) {
|
|
67
|
+
res.json = originalJsonFn;
|
|
68
|
+
errorHandler(e, req, res, next);
|
|
69
|
+
return res;
|
|
70
|
+
}
|
|
71
|
+
if (mayBePromise instanceof Promise) {
|
|
72
|
+
mayBePromise.then((result) => {
|
|
73
|
+
if (res.headersSent) return;
|
|
74
|
+
res.end = originalEndFn;
|
|
75
|
+
originalJsonFn.call(this, result === void 0 ? json : result);
|
|
76
|
+
}).catch((e) => {
|
|
77
|
+
res.json = originalJsonFn;
|
|
78
|
+
res.end = originalEndFn;
|
|
79
|
+
errorHandler(e, req, res, next);
|
|
80
|
+
});
|
|
81
|
+
res.end = function() {
|
|
82
|
+
if (isInSend$1.getStore()) originalEndFn.apply(this, arguments);
|
|
83
|
+
return res;
|
|
84
|
+
};
|
|
85
|
+
} else {
|
|
86
|
+
const result = mayBePromise;
|
|
87
|
+
if (res.headersSent) return res;
|
|
88
|
+
originalJsonFn.call(this, result === void 0 ? json : result);
|
|
89
|
+
}
|
|
90
|
+
return res;
|
|
91
|
+
};
|
|
92
|
+
next();
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/jsonpMiddleware.ts
|
|
97
|
+
const isInSend = new AsyncLocalStorage();
|
|
98
|
+
const jsonpMiddleware = (fn) => (req, res, next) => {
|
|
99
|
+
const originalJsonpFn = res.jsonp;
|
|
100
|
+
const originalSendFn = res.send;
|
|
101
|
+
res.send = function() {
|
|
102
|
+
return isInSend.run(true, () => originalSendFn.apply(this, arguments));
|
|
103
|
+
};
|
|
104
|
+
res.jsonp = function(json) {
|
|
105
|
+
const originalEndFn = res.end;
|
|
106
|
+
if (res.headersSent) return res;
|
|
107
|
+
let mayBePromise;
|
|
108
|
+
try {
|
|
109
|
+
mayBePromise = fn(json, req, res);
|
|
110
|
+
} catch (e) {
|
|
111
|
+
res.json = originalJsonpFn;
|
|
112
|
+
errorHandler(e, req, res, next);
|
|
113
|
+
return res;
|
|
114
|
+
}
|
|
115
|
+
if (mayBePromise instanceof Promise) {
|
|
116
|
+
mayBePromise.then((result) => {
|
|
117
|
+
if (res.headersSent) return;
|
|
118
|
+
res.end = originalEndFn;
|
|
119
|
+
originalJsonpFn.call(this, result === void 0 ? json : result);
|
|
120
|
+
}).catch((e) => {
|
|
121
|
+
res.jsonp = originalJsonpFn;
|
|
122
|
+
res.end = originalEndFn;
|
|
123
|
+
errorHandler(e, req, res, next);
|
|
124
|
+
});
|
|
125
|
+
res.end = function() {
|
|
126
|
+
if (isInSend.getStore()) originalEndFn.apply(this, arguments);
|
|
127
|
+
return res;
|
|
128
|
+
};
|
|
129
|
+
} else {
|
|
130
|
+
const result = mayBePromise;
|
|
131
|
+
if (res.headersSent) return res;
|
|
132
|
+
originalJsonpFn.call(this, result === void 0 ? json : result);
|
|
133
|
+
}
|
|
134
|
+
return res;
|
|
135
|
+
};
|
|
136
|
+
next();
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
//#endregion
|
|
140
|
+
//#region src/writeMiddleware.ts
|
|
141
|
+
const writeMiddleware = (fn) => (req, res, next) => {
|
|
142
|
+
const originalWrite = res.write;
|
|
143
|
+
const originalEndFn = res.end;
|
|
144
|
+
res.write = function(chunk) {
|
|
145
|
+
if (res.writableEnded) return false;
|
|
146
|
+
let mayBePromise;
|
|
147
|
+
try {
|
|
148
|
+
mayBePromise = fn(chunk, typeof arguments[1] === "string" ? arguments[1] : null, req, res);
|
|
149
|
+
} catch (e) {
|
|
150
|
+
errorHandler(e, req, res, next);
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
if (mayBePromise instanceof Promise) {
|
|
154
|
+
mayBePromise.then((result$1) => {
|
|
155
|
+
res.end = originalEndFn;
|
|
156
|
+
if (res.writableEnded) return false;
|
|
157
|
+
arguments[0] = result$1 === void 0 ? chunk : result$1;
|
|
158
|
+
const writeResponse = originalWrite.apply(res, arguments);
|
|
159
|
+
if (res.__isEnd) res.end();
|
|
160
|
+
return writeResponse;
|
|
161
|
+
}).catch((e) => {
|
|
162
|
+
res.end = originalEndFn;
|
|
163
|
+
errorHandler(e, req, res, next);
|
|
164
|
+
});
|
|
165
|
+
res.end = function() {
|
|
166
|
+
this.__isEnd = true;
|
|
167
|
+
return res;
|
|
168
|
+
};
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
const result = mayBePromise;
|
|
172
|
+
if (res.writableEnded) return false;
|
|
173
|
+
arguments[0] = result === void 0 ? chunk : result;
|
|
174
|
+
return originalWrite.apply(res, arguments);
|
|
175
|
+
};
|
|
176
|
+
next();
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
//#endregion
|
|
180
|
+
export { endMiddleware, jsonMiddleware, jsonpMiddleware, writeMiddleware };
|
package/package.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-response-middleware",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Transform an express response",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
7
7
|
],
|
|
8
|
-
"main": "dist/
|
|
9
|
-
"module": "dist/
|
|
10
|
-
"types": "dist/
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"module": "dist/index.mjs",
|
|
10
|
+
"types": "dist/index.d.mts",
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
|
13
13
|
"require": {
|
|
14
|
-
"types": "./dist/
|
|
15
|
-
"default": "./dist/
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
16
|
},
|
|
17
17
|
"import": {
|
|
18
|
-
"types": "./dist/
|
|
19
|
-
"default": "./dist/
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"default": "./dist/index.mjs"
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
23
|
"typesVersions": {
|
|
24
24
|
"*": {
|
|
25
25
|
"*": [
|
|
26
|
-
"./dist/
|
|
26
|
+
"./dist/index.d.mts"
|
|
27
27
|
]
|
|
28
28
|
}
|
|
29
29
|
},
|
|
@@ -44,22 +44,18 @@
|
|
|
44
44
|
},
|
|
45
45
|
"homepage": "https://github.com/marklai1998/express-response-middleware#readme",
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@biomejs/biome": "
|
|
48
|
-
"@commitlint/cli": "
|
|
49
|
-
"@commitlint/config-conventional": "
|
|
50
|
-
"@types/express": "5.0.
|
|
51
|
-
"@types/node": "
|
|
47
|
+
"@biomejs/biome": "2.3.3",
|
|
48
|
+
"@commitlint/cli": "20.1.0",
|
|
49
|
+
"@commitlint/config-conventional": "20.0.0",
|
|
50
|
+
"@types/express": "5.0.5",
|
|
51
|
+
"@types/node": "24.10.0",
|
|
52
52
|
"@types/supertest": "6.0.3",
|
|
53
|
-
"@vitest/coverage-v8": "
|
|
54
|
-
"lefthook": "
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"rollup-plugin-peer-deps-external": "2.2.4",
|
|
60
|
-
"supertest": "7.1.1",
|
|
61
|
-
"typescript": "5.8.3",
|
|
62
|
-
"vitest": "3.1.4"
|
|
53
|
+
"@vitest/coverage-v8": "4.0.6",
|
|
54
|
+
"lefthook": "2.0.2",
|
|
55
|
+
"supertest": "7.1.4",
|
|
56
|
+
"tsdown": "0.15.12",
|
|
57
|
+
"typescript": "5.9.3",
|
|
58
|
+
"vitest": "4.0.6"
|
|
63
59
|
},
|
|
64
60
|
"peerDependencies": {
|
|
65
61
|
"express": "^4.0.0 || ^5.0.0"
|
|
@@ -68,7 +64,7 @@
|
|
|
68
64
|
"node": ">=18"
|
|
69
65
|
},
|
|
70
66
|
"scripts": {
|
|
71
|
-
"build": "
|
|
67
|
+
"build": "tsdown --format esm --format cjs",
|
|
72
68
|
"test": "vitest run --coverage"
|
|
73
69
|
}
|
|
74
70
|
}
|
package/dist/cjs/index.cjs
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var node_async_hooks = require('node:async_hooks');
|
|
4
|
-
|
|
5
|
-
const isInSend$1 = new node_async_hooks.AsyncLocalStorage();
|
|
6
|
-
|
|
7
|
-
const errorHandler = (err, _req, res, _next) => {
|
|
8
|
-
res.status(500).set("content-language", "en").json({ message: err.message }).end();
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const jsonMiddleware = (fn) => (req, res, next) => {
|
|
12
|
-
const originalJsonFn = res.json;
|
|
13
|
-
const originalSendFn = res.send;
|
|
14
|
-
res.send = function() {
|
|
15
|
-
return isInSend$1.run(
|
|
16
|
-
true,
|
|
17
|
-
() => originalSendFn.apply(this, arguments)
|
|
18
|
-
);
|
|
19
|
-
};
|
|
20
|
-
res.json = function(json) {
|
|
21
|
-
const originalEndFn = res.end;
|
|
22
|
-
if (res.headersSent) return res;
|
|
23
|
-
let mayBePromise;
|
|
24
|
-
try {
|
|
25
|
-
mayBePromise = fn(json, req, res);
|
|
26
|
-
} catch (e) {
|
|
27
|
-
res.json = originalJsonFn;
|
|
28
|
-
errorHandler(e, req, res);
|
|
29
|
-
return res;
|
|
30
|
-
}
|
|
31
|
-
if (mayBePromise instanceof Promise) {
|
|
32
|
-
mayBePromise.then((result) => {
|
|
33
|
-
if (res.headersSent) return;
|
|
34
|
-
res.end = originalEndFn;
|
|
35
|
-
originalJsonFn.call(this, result === void 0 ? json : result);
|
|
36
|
-
}).catch((e) => {
|
|
37
|
-
res.json = originalJsonFn;
|
|
38
|
-
res.end = originalEndFn;
|
|
39
|
-
errorHandler(e, req, res);
|
|
40
|
-
});
|
|
41
|
-
res.end = function() {
|
|
42
|
-
if (isInSend$1.getStore()) {
|
|
43
|
-
originalEndFn.apply(this, arguments);
|
|
44
|
-
}
|
|
45
|
-
return res;
|
|
46
|
-
};
|
|
47
|
-
} else {
|
|
48
|
-
const result = mayBePromise;
|
|
49
|
-
if (res.headersSent) return res;
|
|
50
|
-
originalJsonFn.call(this, result === void 0 ? json : result);
|
|
51
|
-
}
|
|
52
|
-
return res;
|
|
53
|
-
};
|
|
54
|
-
next();
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const endMiddleware = (fn) => (req, res, next) => {
|
|
58
|
-
const originalEndFn = res.end;
|
|
59
|
-
res.end = function() {
|
|
60
|
-
if (res.headersSent) return originalEndFn.apply(this, arguments);
|
|
61
|
-
let mayBePromise;
|
|
62
|
-
try {
|
|
63
|
-
mayBePromise = fn(req, res);
|
|
64
|
-
} catch (e) {
|
|
65
|
-
res.end = originalEndFn;
|
|
66
|
-
errorHandler(e, req, res);
|
|
67
|
-
return res;
|
|
68
|
-
}
|
|
69
|
-
if (mayBePromise instanceof Promise) {
|
|
70
|
-
mayBePromise.then(() => {
|
|
71
|
-
if (res.headersSent) {
|
|
72
|
-
console.error(
|
|
73
|
-
"sending response while in endMiddleware is undefined behaviour"
|
|
74
|
-
);
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
originalEndFn.apply(this, arguments);
|
|
78
|
-
}).catch((e) => {
|
|
79
|
-
res.end = originalEndFn;
|
|
80
|
-
errorHandler(e, req, res);
|
|
81
|
-
});
|
|
82
|
-
res.end = function() {
|
|
83
|
-
return res;
|
|
84
|
-
};
|
|
85
|
-
} else {
|
|
86
|
-
if (res.headersSent) {
|
|
87
|
-
console.error(
|
|
88
|
-
"sending response while in endMiddleware is undefined behaviour"
|
|
89
|
-
);
|
|
90
|
-
return res;
|
|
91
|
-
}
|
|
92
|
-
return originalEndFn.apply(this, arguments);
|
|
93
|
-
}
|
|
94
|
-
return res;
|
|
95
|
-
};
|
|
96
|
-
next();
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const writeMiddleware = (fn) => (req, res, next) => {
|
|
100
|
-
const originalWrite = res.write;
|
|
101
|
-
const originalEndFn = res.end;
|
|
102
|
-
res.write = function(chunk) {
|
|
103
|
-
if (res.writableEnded) return false;
|
|
104
|
-
let mayBePromise;
|
|
105
|
-
try {
|
|
106
|
-
mayBePromise = fn(
|
|
107
|
-
chunk,
|
|
108
|
-
// Since `encoding` is an optional argument to `res.write`,
|
|
109
|
-
// make sure it is a string and not actually the callback.
|
|
110
|
-
// biome-ignore lint/style/noArguments: pass argument type
|
|
111
|
-
typeof arguments[1] === "string" ? arguments[1] : null,
|
|
112
|
-
req,
|
|
113
|
-
res
|
|
114
|
-
);
|
|
115
|
-
} catch (e) {
|
|
116
|
-
errorHandler(e, req, res);
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
if (mayBePromise instanceof Promise) {
|
|
120
|
-
mayBePromise.then((result2) => {
|
|
121
|
-
res.end = originalEndFn;
|
|
122
|
-
if (res.writableEnded) return false;
|
|
123
|
-
arguments[0] = result2 === void 0 ? chunk : result2;
|
|
124
|
-
const writeResponse = originalWrite.apply(res, arguments);
|
|
125
|
-
if (res.__isEnd) res.end();
|
|
126
|
-
return writeResponse;
|
|
127
|
-
}).catch((e) => {
|
|
128
|
-
res.end = originalEndFn;
|
|
129
|
-
errorHandler(e, req, res);
|
|
130
|
-
});
|
|
131
|
-
res.end = function() {
|
|
132
|
-
this.__isEnd = true;
|
|
133
|
-
return res;
|
|
134
|
-
};
|
|
135
|
-
return false;
|
|
136
|
-
}
|
|
137
|
-
const result = mayBePromise;
|
|
138
|
-
if (res.writableEnded) return false;
|
|
139
|
-
arguments[0] = result === void 0 ? chunk : result;
|
|
140
|
-
return originalWrite.apply(res, arguments);
|
|
141
|
-
};
|
|
142
|
-
next();
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
const isInSend = new node_async_hooks.AsyncLocalStorage();
|
|
146
|
-
const jsonpMiddleware = (fn) => (req, res, next) => {
|
|
147
|
-
const originalJsonpFn = res.jsonp;
|
|
148
|
-
const originalSendFn = res.send;
|
|
149
|
-
res.send = function() {
|
|
150
|
-
return isInSend.run(
|
|
151
|
-
true,
|
|
152
|
-
() => originalSendFn.apply(this, arguments)
|
|
153
|
-
);
|
|
154
|
-
};
|
|
155
|
-
res.jsonp = function(json) {
|
|
156
|
-
const originalEndFn = res.end;
|
|
157
|
-
if (res.headersSent) return res;
|
|
158
|
-
let mayBePromise;
|
|
159
|
-
try {
|
|
160
|
-
mayBePromise = fn(json, req, res);
|
|
161
|
-
} catch (e) {
|
|
162
|
-
res.json = originalJsonpFn;
|
|
163
|
-
errorHandler(e, req, res);
|
|
164
|
-
return res;
|
|
165
|
-
}
|
|
166
|
-
if (mayBePromise instanceof Promise) {
|
|
167
|
-
mayBePromise.then((result) => {
|
|
168
|
-
if (res.headersSent) return;
|
|
169
|
-
res.end = originalEndFn;
|
|
170
|
-
originalJsonpFn.call(this, result === void 0 ? json : result);
|
|
171
|
-
}).catch((e) => {
|
|
172
|
-
res.jsonp = originalJsonpFn;
|
|
173
|
-
res.end = originalEndFn;
|
|
174
|
-
errorHandler(e, req, res);
|
|
175
|
-
});
|
|
176
|
-
res.end = function() {
|
|
177
|
-
if (isInSend.getStore()) {
|
|
178
|
-
originalEndFn.apply(this, arguments);
|
|
179
|
-
}
|
|
180
|
-
return res;
|
|
181
|
-
};
|
|
182
|
-
} else {
|
|
183
|
-
const result = mayBePromise;
|
|
184
|
-
if (res.headersSent) return res;
|
|
185
|
-
originalJsonpFn.call(this, result === void 0 ? json : result);
|
|
186
|
-
}
|
|
187
|
-
return res;
|
|
188
|
-
};
|
|
189
|
-
next();
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
exports.endMiddleware = endMiddleware;
|
|
193
|
-
exports.jsonMiddleware = jsonMiddleware;
|
|
194
|
-
exports.jsonpMiddleware = jsonpMiddleware;
|
|
195
|
-
exports.writeMiddleware = writeMiddleware;
|
|
196
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/utils/asyncLocalStorage.ts","../../src/utils/errorHandler.ts","../../src/jsonMiddleware.ts","../../src/endMiddleware.ts","../../src/writeMiddleware.ts","../../src/jsonpMiddleware.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport const isInSend = new AsyncLocalStorage<boolean>();\n","import type { ErrorRequestHandler } from 'express';\n\nexport const errorHandler: ErrorRequestHandler = (err, _req, res, _next) => {\n res\n .status(500)\n .set('content-language', 'en')\n .json({ message: err.message })\n .end();\n};\n","import type { Request, RequestHandler, Response } from 'express';\nimport { isInSend } from './utils/asyncLocalStorage.js';\nimport { errorHandler } from './utils/errorHandler.js';\n\nexport type TransformJson<T = unknown, R = T> = (\n body: T,\n request: Request,\n response: Response,\n) => R | Promise<R>;\n\nexport const jsonMiddleware =\n <T = unknown>(fn: TransformJson<T>): RequestHandler =>\n (req, res, next) => {\n const originalJsonFn = res.json;\n const originalSendFn = res.send;\n\n res.send = function (this: Response) {\n return isInSend.run(true, () =>\n originalSendFn.apply(this, arguments as any),\n );\n };\n\n res.json = function (this: Response, json) {\n const originalEndFn = res.end;\n\n if (res.headersSent) return res;\n\n let mayBePromise: T | Promise<T>;\n try {\n mayBePromise = fn(json, req, res);\n } catch (e) {\n res.json = originalJsonFn;\n errorHandler(e, req, res, next);\n return res;\n }\n\n if (mayBePromise instanceof Promise) {\n mayBePromise\n .then((result) => {\n if (res.headersSent) return;\n res.end = originalEndFn;\n\n originalJsonFn.call(this, result === undefined ? json : result);\n })\n .catch((e) => {\n res.json = originalJsonFn;\n res.end = originalEndFn;\n errorHandler(e, req, res, next);\n });\n\n // Prevent end being called wile promise still running\n res.end = function (this: Response) {\n // Res.send will call Res.end, when send is call inside the middleware, do it actually\n if (isInSend.getStore()) {\n originalEndFn.apply(this, arguments as any);\n }\n return res;\n };\n } else {\n const result = mayBePromise;\n\n if (res.headersSent) return res;\n\n originalJsonFn.call(this, result === undefined ? json : result);\n }\n\n return res;\n };\n\n next();\n };\n","import type { Request, RequestHandler, Response } from 'express';\nimport { errorHandler } from './utils/errorHandler.js';\n\nexport type TransformEnd<T = unknown> = (\n request: Request,\n response: Response,\n) => T | Promise<T>;\n\nexport const endMiddleware =\n <T = unknown>(fn: TransformEnd<T>): RequestHandler =>\n (req, res, next) => {\n const originalEndFn = res.end;\n\n res.end = function (this: Response) {\n if (res.headersSent) return originalEndFn.apply(this, arguments as any);\n\n let mayBePromise: T | Promise<T>;\n try {\n mayBePromise = fn(req, res);\n } catch (e) {\n res.end = originalEndFn;\n errorHandler(e, req, res, next);\n return res;\n }\n\n if (mayBePromise instanceof Promise) {\n mayBePromise\n .then(() => {\n if (res.headersSent) {\n console.error(\n 'sending response while in endMiddleware is undefined behaviour',\n );\n return;\n }\n\n originalEndFn.apply(this, arguments as any);\n })\n .catch((e) => {\n res.end = originalEndFn;\n errorHandler(e, req, res, next);\n });\n\n res.end = function (this: Response) {\n return res;\n };\n } else {\n if (res.headersSent) {\n console.error(\n 'sending response while in endMiddleware is undefined behaviour',\n );\n return res;\n }\n\n return originalEndFn.apply(this, arguments as any);\n }\n\n return res;\n };\n\n next();\n };\n","import type { Request, RequestHandler, Response } from 'express';\nimport { errorHandler } from './utils/errorHandler.js';\n\nexport type TransformChunk = (\n chunk: string | Buffer,\n encoding: string | null,\n request: Request,\n response: Response,\n) => void | string | Buffer | Promise<void> | Promise<string> | Promise<Buffer>;\n\nexport const writeMiddleware =\n (fn: TransformChunk): RequestHandler =>\n (req, res, next) => {\n const originalWrite = res.write;\n const originalEndFn = res.end;\n\n res.write = function (this: Response, chunk) {\n if (res.writableEnded) return false;\n\n let mayBePromise: ReturnType<TransformChunk>;\n try {\n mayBePromise = fn(\n chunk,\n // Since `encoding` is an optional argument to `res.write`,\n // make sure it is a string and not actually the callback.\n // biome-ignore lint/style/noArguments: pass argument type\n typeof arguments[1] === 'string' ? arguments[1] : null,\n req,\n res,\n );\n } catch (e) {\n errorHandler(e, req, res, next);\n return false;\n }\n\n if (mayBePromise instanceof Promise) {\n mayBePromise\n .then((result) => {\n res.end = originalEndFn;\n\n if (res.writableEnded) return false;\n\n // biome-ignore lint/style/noArguments: pass argument type\n arguments[0] = result === undefined ? chunk : result;\n\n // biome-ignore lint/style/noArguments: pass argument type\n const writeResponse = originalWrite.apply(res, arguments as any);\n\n if (res.__isEnd) res.end();\n\n return writeResponse;\n })\n .catch((e) => {\n res.end = originalEndFn;\n errorHandler(e, req, res, next);\n });\n\n res.end = function (this: Response) {\n this.__isEnd = true;\n return res;\n };\n return false;\n }\n\n const result = mayBePromise;\n if (res.writableEnded) return false;\n\n // biome-ignore lint/style/noArguments: pass argument type\n arguments[0] = result === undefined ? chunk : result;\n\n // biome-ignore lint/style/noArguments: pass argument type\n return originalWrite.apply(res, arguments as any);\n };\n\n next();\n };\n","import { AsyncLocalStorage } from 'node:async_hooks';\nimport type { Request, RequestHandler, Response } from 'express';\nimport { errorHandler } from './utils/errorHandler.js';\n\nexport type TransformJsonp<T = unknown, R = T> = (\n body: T,\n request: Request,\n response: Response,\n) => R | Promise<R>;\n\nconst isInSend = new AsyncLocalStorage<boolean>();\n\nexport const jsonpMiddleware =\n <T = unknown>(fn: TransformJsonp<T>): RequestHandler =>\n (req, res, next) => {\n const originalJsonpFn = res.jsonp;\n const originalSendFn = res.send;\n\n res.send = function (this: Response) {\n return isInSend.run(true, () =>\n originalSendFn.apply(this, arguments as any),\n );\n };\n\n res.jsonp = function (this: Response, json) {\n const originalEndFn = res.end;\n\n if (res.headersSent) return res;\n\n let mayBePromise: T | Promise<T>;\n try {\n mayBePromise = fn(json, req, res);\n } catch (e) {\n res.json = originalJsonpFn;\n errorHandler(e, req, res, next);\n return res;\n }\n\n if (mayBePromise instanceof Promise) {\n mayBePromise\n .then((result) => {\n if (res.headersSent) return;\n res.end = originalEndFn;\n\n originalJsonpFn.call(this, result === undefined ? json : result);\n })\n .catch((e) => {\n res.jsonp = originalJsonpFn;\n res.end = originalEndFn;\n errorHandler(e, req, res, next);\n });\n\n // Prevent end being called wile promise still running\n res.end = function (this: Response) {\n // Res.send will call Res.end, when send is call inside the middleware, do it actually\n if (isInSend.getStore()) {\n originalEndFn.apply(this, arguments as any);\n }\n return res;\n };\n } else {\n const result = mayBePromise;\n\n if (res.headersSent) return res;\n\n originalJsonpFn.call(this, result === undefined ? json : result);\n }\n\n return res;\n };\n\n next();\n };\n"],"names":["isInSend","AsyncLocalStorage","result"],"mappings":";;;;AAEa,MAAAA,UAAA,GAAW,IAAIC,kCAA2B,EAAA;;ACAhD,MAAM,YAAoC,GAAA,CAAC,GAAK,EAAA,IAAA,EAAM,KAAK,KAAU,KAAA;AAC1E,EAAA,GAAA,CACG,MAAO,CAAA,GAAG,CACV,CAAA,GAAA,CAAI,oBAAoB,IAAI,CAAA,CAC5B,IAAK,CAAA,EAAE,OAAS,EAAA,GAAA,CAAI,OAAQ,EAAC,EAC7B,GAAI,EAAA;AACT,CAAA;;ACEO,MAAM,iBACX,CAAc,EAAA,KACd,CAAC,GAAA,EAAK,KAAK,IAAS,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OAAO,MAAM,CAAA;AAAA;AAGjE,IAAO,OAAA,GAAA;AAAA,GACT;AAEA,EAAK,IAAA,EAAA;AACP;;;;;;;"}
|
package/dist/esm/index.mjs
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
-
|
|
3
|
-
const isInSend$1 = new AsyncLocalStorage();
|
|
4
|
-
|
|
5
|
-
const errorHandler = (err, _req, res, _next) => {
|
|
6
|
-
res.status(500).set("content-language", "en").json({ message: err.message }).end();
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
const jsonMiddleware = (fn) => (req, res, next) => {
|
|
10
|
-
const originalJsonFn = res.json;
|
|
11
|
-
const originalSendFn = res.send;
|
|
12
|
-
res.send = function() {
|
|
13
|
-
return isInSend$1.run(
|
|
14
|
-
true,
|
|
15
|
-
() => originalSendFn.apply(this, arguments)
|
|
16
|
-
);
|
|
17
|
-
};
|
|
18
|
-
res.json = function(json) {
|
|
19
|
-
const originalEndFn = res.end;
|
|
20
|
-
if (res.headersSent) return res;
|
|
21
|
-
let mayBePromise;
|
|
22
|
-
try {
|
|
23
|
-
mayBePromise = fn(json, req, res);
|
|
24
|
-
} catch (e) {
|
|
25
|
-
res.json = originalJsonFn;
|
|
26
|
-
errorHandler(e, req, res);
|
|
27
|
-
return res;
|
|
28
|
-
}
|
|
29
|
-
if (mayBePromise instanceof Promise) {
|
|
30
|
-
mayBePromise.then((result) => {
|
|
31
|
-
if (res.headersSent) return;
|
|
32
|
-
res.end = originalEndFn;
|
|
33
|
-
originalJsonFn.call(this, result === void 0 ? json : result);
|
|
34
|
-
}).catch((e) => {
|
|
35
|
-
res.json = originalJsonFn;
|
|
36
|
-
res.end = originalEndFn;
|
|
37
|
-
errorHandler(e, req, res);
|
|
38
|
-
});
|
|
39
|
-
res.end = function() {
|
|
40
|
-
if (isInSend$1.getStore()) {
|
|
41
|
-
originalEndFn.apply(this, arguments);
|
|
42
|
-
}
|
|
43
|
-
return res;
|
|
44
|
-
};
|
|
45
|
-
} else {
|
|
46
|
-
const result = mayBePromise;
|
|
47
|
-
if (res.headersSent) return res;
|
|
48
|
-
originalJsonFn.call(this, result === void 0 ? json : result);
|
|
49
|
-
}
|
|
50
|
-
return res;
|
|
51
|
-
};
|
|
52
|
-
next();
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const endMiddleware = (fn) => (req, res, next) => {
|
|
56
|
-
const originalEndFn = res.end;
|
|
57
|
-
res.end = function() {
|
|
58
|
-
if (res.headersSent) return originalEndFn.apply(this, arguments);
|
|
59
|
-
let mayBePromise;
|
|
60
|
-
try {
|
|
61
|
-
mayBePromise = fn(req, res);
|
|
62
|
-
} catch (e) {
|
|
63
|
-
res.end = originalEndFn;
|
|
64
|
-
errorHandler(e, req, res);
|
|
65
|
-
return res;
|
|
66
|
-
}
|
|
67
|
-
if (mayBePromise instanceof Promise) {
|
|
68
|
-
mayBePromise.then(() => {
|
|
69
|
-
if (res.headersSent) {
|
|
70
|
-
console.error(
|
|
71
|
-
"sending response while in endMiddleware is undefined behaviour"
|
|
72
|
-
);
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
originalEndFn.apply(this, arguments);
|
|
76
|
-
}).catch((e) => {
|
|
77
|
-
res.end = originalEndFn;
|
|
78
|
-
errorHandler(e, req, res);
|
|
79
|
-
});
|
|
80
|
-
res.end = function() {
|
|
81
|
-
return res;
|
|
82
|
-
};
|
|
83
|
-
} else {
|
|
84
|
-
if (res.headersSent) {
|
|
85
|
-
console.error(
|
|
86
|
-
"sending response while in endMiddleware is undefined behaviour"
|
|
87
|
-
);
|
|
88
|
-
return res;
|
|
89
|
-
}
|
|
90
|
-
return originalEndFn.apply(this, arguments);
|
|
91
|
-
}
|
|
92
|
-
return res;
|
|
93
|
-
};
|
|
94
|
-
next();
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const writeMiddleware = (fn) => (req, res, next) => {
|
|
98
|
-
const originalWrite = res.write;
|
|
99
|
-
const originalEndFn = res.end;
|
|
100
|
-
res.write = function(chunk) {
|
|
101
|
-
if (res.writableEnded) return false;
|
|
102
|
-
let mayBePromise;
|
|
103
|
-
try {
|
|
104
|
-
mayBePromise = fn(
|
|
105
|
-
chunk,
|
|
106
|
-
// Since `encoding` is an optional argument to `res.write`,
|
|
107
|
-
// make sure it is a string and not actually the callback.
|
|
108
|
-
// biome-ignore lint/style/noArguments: pass argument type
|
|
109
|
-
typeof arguments[1] === "string" ? arguments[1] : null,
|
|
110
|
-
req,
|
|
111
|
-
res
|
|
112
|
-
);
|
|
113
|
-
} catch (e) {
|
|
114
|
-
errorHandler(e, req, res);
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
if (mayBePromise instanceof Promise) {
|
|
118
|
-
mayBePromise.then((result2) => {
|
|
119
|
-
res.end = originalEndFn;
|
|
120
|
-
if (res.writableEnded) return false;
|
|
121
|
-
arguments[0] = result2 === void 0 ? chunk : result2;
|
|
122
|
-
const writeResponse = originalWrite.apply(res, arguments);
|
|
123
|
-
if (res.__isEnd) res.end();
|
|
124
|
-
return writeResponse;
|
|
125
|
-
}).catch((e) => {
|
|
126
|
-
res.end = originalEndFn;
|
|
127
|
-
errorHandler(e, req, res);
|
|
128
|
-
});
|
|
129
|
-
res.end = function() {
|
|
130
|
-
this.__isEnd = true;
|
|
131
|
-
return res;
|
|
132
|
-
};
|
|
133
|
-
return false;
|
|
134
|
-
}
|
|
135
|
-
const result = mayBePromise;
|
|
136
|
-
if (res.writableEnded) return false;
|
|
137
|
-
arguments[0] = result === void 0 ? chunk : result;
|
|
138
|
-
return originalWrite.apply(res, arguments);
|
|
139
|
-
};
|
|
140
|
-
next();
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
const isInSend = new AsyncLocalStorage();
|
|
144
|
-
const jsonpMiddleware = (fn) => (req, res, next) => {
|
|
145
|
-
const originalJsonpFn = res.jsonp;
|
|
146
|
-
const originalSendFn = res.send;
|
|
147
|
-
res.send = function() {
|
|
148
|
-
return isInSend.run(
|
|
149
|
-
true,
|
|
150
|
-
() => originalSendFn.apply(this, arguments)
|
|
151
|
-
);
|
|
152
|
-
};
|
|
153
|
-
res.jsonp = function(json) {
|
|
154
|
-
const originalEndFn = res.end;
|
|
155
|
-
if (res.headersSent) return res;
|
|
156
|
-
let mayBePromise;
|
|
157
|
-
try {
|
|
158
|
-
mayBePromise = fn(json, req, res);
|
|
159
|
-
} catch (e) {
|
|
160
|
-
res.json = originalJsonpFn;
|
|
161
|
-
errorHandler(e, req, res);
|
|
162
|
-
return res;
|
|
163
|
-
}
|
|
164
|
-
if (mayBePromise instanceof Promise) {
|
|
165
|
-
mayBePromise.then((result) => {
|
|
166
|
-
if (res.headersSent) return;
|
|
167
|
-
res.end = originalEndFn;
|
|
168
|
-
originalJsonpFn.call(this, result === void 0 ? json : result);
|
|
169
|
-
}).catch((e) => {
|
|
170
|
-
res.jsonp = originalJsonpFn;
|
|
171
|
-
res.end = originalEndFn;
|
|
172
|
-
errorHandler(e, req, res);
|
|
173
|
-
});
|
|
174
|
-
res.end = function() {
|
|
175
|
-
if (isInSend.getStore()) {
|
|
176
|
-
originalEndFn.apply(this, arguments);
|
|
177
|
-
}
|
|
178
|
-
return res;
|
|
179
|
-
};
|
|
180
|
-
} else {
|
|
181
|
-
const result = mayBePromise;
|
|
182
|
-
if (res.headersSent) return res;
|
|
183
|
-
originalJsonpFn.call(this, result === void 0 ? json : result);
|
|
184
|
-
}
|
|
185
|
-
return res;
|
|
186
|
-
};
|
|
187
|
-
next();
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
export { endMiddleware, jsonMiddleware, jsonpMiddleware, writeMiddleware };
|
|
191
|
-
//# sourceMappingURL=index.mjs.map
|
package/dist/esm/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../src/utils/asyncLocalStorage.ts","../../src/utils/errorHandler.ts","../../src/jsonMiddleware.ts","../../src/endMiddleware.ts","../../src/writeMiddleware.ts","../../src/jsonpMiddleware.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport const isInSend = new AsyncLocalStorage<boolean>();\n","import type { ErrorRequestHandler } from 'express';\n\nexport const errorHandler: ErrorRequestHandler = (err, _req, res, _next) => {\n res\n .status(500)\n .set('content-language', 'en')\n .json({ message: err.message })\n .end();\n};\n","import type { Request, RequestHandler, Response } from 'express';\nimport { isInSend } from './utils/asyncLocalStorage.js';\nimport { errorHandler } from './utils/errorHandler.js';\n\nexport type TransformJson<T = unknown, R = T> = (\n body: T,\n request: Request,\n response: Response,\n) => R | Promise<R>;\n\nexport const jsonMiddleware =\n <T = unknown>(fn: TransformJson<T>): RequestHandler =>\n (req, res, next) => {\n const originalJsonFn = res.json;\n const originalSendFn = res.send;\n\n res.send = function (this: Response) {\n return isInSend.run(true, () =>\n originalSendFn.apply(this, arguments as any),\n );\n };\n\n res.json = function (this: Response, json) {\n const originalEndFn = res.end;\n\n if (res.headersSent) return res;\n\n let mayBePromise: T | Promise<T>;\n try {\n mayBePromise = fn(json, req, res);\n } catch (e) {\n res.json = originalJsonFn;\n errorHandler(e, req, res, next);\n return res;\n }\n\n if (mayBePromise instanceof Promise) {\n mayBePromise\n .then((result) => {\n if (res.headersSent) return;\n res.end = originalEndFn;\n\n originalJsonFn.call(this, result === undefined ? json : result);\n })\n .catch((e) => {\n res.json = originalJsonFn;\n res.end = originalEndFn;\n errorHandler(e, req, res, next);\n });\n\n // Prevent end being called wile promise still running\n res.end = function (this: Response) {\n // Res.send will call Res.end, when send is call inside the middleware, do it actually\n if (isInSend.getStore()) {\n originalEndFn.apply(this, arguments as any);\n }\n return res;\n };\n } else {\n const result = mayBePromise;\n\n if (res.headersSent) return res;\n\n originalJsonFn.call(this, result === undefined ? json : result);\n }\n\n return res;\n };\n\n next();\n };\n","import type { Request, RequestHandler, Response } from 'express';\nimport { errorHandler } from './utils/errorHandler.js';\n\nexport type TransformEnd<T = unknown> = (\n request: Request,\n response: Response,\n) => T | Promise<T>;\n\nexport const endMiddleware =\n <T = unknown>(fn: TransformEnd<T>): RequestHandler =>\n (req, res, next) => {\n const originalEndFn = res.end;\n\n res.end = function (this: Response) {\n if (res.headersSent) return originalEndFn.apply(this, arguments as any);\n\n let mayBePromise: T | Promise<T>;\n try {\n mayBePromise = fn(req, res);\n } catch (e) {\n res.end = originalEndFn;\n errorHandler(e, req, res, next);\n return res;\n }\n\n if (mayBePromise instanceof Promise) {\n mayBePromise\n .then(() => {\n if (res.headersSent) {\n console.error(\n 'sending response while in endMiddleware is undefined behaviour',\n );\n return;\n }\n\n originalEndFn.apply(this, arguments as any);\n })\n .catch((e) => {\n res.end = originalEndFn;\n errorHandler(e, req, res, next);\n });\n\n res.end = function (this: Response) {\n return res;\n };\n } else {\n if (res.headersSent) {\n console.error(\n 'sending response while in endMiddleware is undefined behaviour',\n );\n return res;\n }\n\n return originalEndFn.apply(this, arguments as any);\n }\n\n return res;\n };\n\n next();\n };\n","import type { Request, RequestHandler, Response } from 'express';\nimport { errorHandler } from './utils/errorHandler.js';\n\nexport type TransformChunk = (\n chunk: string | Buffer,\n encoding: string | null,\n request: Request,\n response: Response,\n) => void | string | Buffer | Promise<void> | Promise<string> | Promise<Buffer>;\n\nexport const writeMiddleware =\n (fn: TransformChunk): RequestHandler =>\n (req, res, next) => {\n const originalWrite = res.write;\n const originalEndFn = res.end;\n\n res.write = function (this: Response, chunk) {\n if (res.writableEnded) return false;\n\n let mayBePromise: ReturnType<TransformChunk>;\n try {\n mayBePromise = fn(\n chunk,\n // Since `encoding` is an optional argument to `res.write`,\n // make sure it is a string and not actually the callback.\n // biome-ignore lint/style/noArguments: pass argument type\n typeof arguments[1] === 'string' ? arguments[1] : null,\n req,\n res,\n );\n } catch (e) {\n errorHandler(e, req, res, next);\n return false;\n }\n\n if (mayBePromise instanceof Promise) {\n mayBePromise\n .then((result) => {\n res.end = originalEndFn;\n\n if (res.writableEnded) return false;\n\n // biome-ignore lint/style/noArguments: pass argument type\n arguments[0] = result === undefined ? chunk : result;\n\n // biome-ignore lint/style/noArguments: pass argument type\n const writeResponse = originalWrite.apply(res, arguments as any);\n\n if (res.__isEnd) res.end();\n\n return writeResponse;\n })\n .catch((e) => {\n res.end = originalEndFn;\n errorHandler(e, req, res, next);\n });\n\n res.end = function (this: Response) {\n this.__isEnd = true;\n return res;\n };\n return false;\n }\n\n const result = mayBePromise;\n if (res.writableEnded) return false;\n\n // biome-ignore lint/style/noArguments: pass argument type\n arguments[0] = result === undefined ? chunk : result;\n\n // biome-ignore lint/style/noArguments: pass argument type\n return originalWrite.apply(res, arguments as any);\n };\n\n next();\n };\n","import { AsyncLocalStorage } from 'node:async_hooks';\nimport type { Request, RequestHandler, Response } from 'express';\nimport { errorHandler } from './utils/errorHandler.js';\n\nexport type TransformJsonp<T = unknown, R = T> = (\n body: T,\n request: Request,\n response: Response,\n) => R | Promise<R>;\n\nconst isInSend = new AsyncLocalStorage<boolean>();\n\nexport const jsonpMiddleware =\n <T = unknown>(fn: TransformJsonp<T>): RequestHandler =>\n (req, res, next) => {\n const originalJsonpFn = res.jsonp;\n const originalSendFn = res.send;\n\n res.send = function (this: Response) {\n return isInSend.run(true, () =>\n originalSendFn.apply(this, arguments as any),\n );\n };\n\n res.jsonp = function (this: Response, json) {\n const originalEndFn = res.end;\n\n if (res.headersSent) return res;\n\n let mayBePromise: T | Promise<T>;\n try {\n mayBePromise = fn(json, req, res);\n } catch (e) {\n res.json = originalJsonpFn;\n errorHandler(e, req, res, next);\n return res;\n }\n\n if (mayBePromise instanceof Promise) {\n mayBePromise\n .then((result) => {\n if (res.headersSent) return;\n res.end = originalEndFn;\n\n originalJsonpFn.call(this, result === undefined ? json : result);\n })\n .catch((e) => {\n res.jsonp = originalJsonpFn;\n res.end = originalEndFn;\n errorHandler(e, req, res, next);\n });\n\n // Prevent end being called wile promise still running\n res.end = function (this: Response) {\n // Res.send will call Res.end, when send is call inside the middleware, do it actually\n if (isInSend.getStore()) {\n originalEndFn.apply(this, arguments as any);\n }\n return res;\n };\n } else {\n const result = mayBePromise;\n\n if (res.headersSent) return res;\n\n originalJsonpFn.call(this, result === undefined ? json : result);\n }\n\n return res;\n };\n\n next();\n };\n"],"names":["isInSend","result"],"mappings":";;AAEa,MAAAA,UAAA,GAAW,IAAI,iBAA2B,EAAA;;ACAhD,MAAM,YAAoC,GAAA,CAAC,GAAK,EAAA,IAAA,EAAM,KAAK,KAAU,KAAA;AAC1E,EAAA,GAAA,CACG,MAAO,CAAA,GAAG,CACV,CAAA,GAAA,CAAI,oBAAoB,IAAI,CAAA,CAC5B,IAAK,CAAA,EAAE,OAAS,EAAA,GAAA,CAAI,OAAQ,EAAC,EAC7B,GAAI,EAAA;AACT,CAAA;;ACEO,MAAM,iBACX,CAAc,EAAA,KACd,CAAC,GAAA,EAAK,KAAK,IAAS,KAAA;AAClB,EAAA,MAAM,iBAAiB,GAAI,CAAA,IAAA;AAC3B,EAAA,MAAM,iBAAiB,GAAI,CAAA,IAAA;AAE3B,EAAA,GAAA,CAAI,OAAO,WAA0B;AACnC,IAAA,OAAOA,UAAS,CAAA,GAAA;AAAA,MAAI,IAAA;AAAA,MAAM,MACxB,cAAA,CAAe,KAAM,CAAA,IAAA,EAAM,SAAgB;AAAA,KAC7C;AAAA,GACF;AAEA,EAAI,GAAA,CAAA,IAAA,GAAO,SAA0B,IAAM,EAAA;AACzC,IAAA,MAAM,gBAAgB,GAAI,CAAA,GAAA;AAE1B,IAAI,IAAA,GAAA,CAAI,aAAoB,OAAA,GAAA;AAE5B,IAAI,IAAA,YAAA;AACJ,IAAI,IAAA;AACF,MAAe,YAAA,GAAA,EAAA,CAAG,IAAM,EAAA,GAAA,EAAK,GAAG,CAAA;AAAA,aACzB,CAAG,EAAA;AACV,MAAA,GAAA,CAAI,IAAO,GAAA,cAAA;AACX,MAAa,YAAA,CAAA,CAAA,EAAG,GAAK,EAAA,GAAS,CAAA;AAC9B,MAAO,OAAA,GAAA;AA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