express-response-middleware 2.1.3 → 2.3.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/README.md +34 -41
- package/dist/index.cjs +229 -0
- package/dist/index.d.cts +11 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.mjs +224 -0
- package/package.json +41 -43
- package/dist/cjs/index.cjs +0 -196
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index.d.cts +0 -24
- package/dist/esm/index.d.ts +0 -24
- package/dist/esm/index.mjs +0 -191
- package/dist/esm/index.mjs.map +0 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
Middleware for express responses. Fork of [express-mung](https://www.npmjs.com/package/express-mung)
|
|
9
9
|
|
|
10
|
-
This package allows synchronous and asynchronous transformation of an express response. This is a similar concept to the express middleware for a request but for a response. Note that the middleware is executed in LIFO order. It is implemented by monkey patching (hooking) the `res.end`, `res.json`, or `res.write` methods.
|
|
10
|
+
This package allows synchronous and asynchronous transformation of an express response. This is a similar concept to the express middleware for a request but for a response. Note that the middleware is executed in LIFO order. It is implemented by monkey patching (hooking) the `res.end`, `res.json`, `res.jsonp`, `res.send`, or `res.write` methods.
|
|
11
11
|
|
|
12
12
|
  [](https://conventionalcommits.org) [](https://www.typescriptlang.org/)
|
|
13
13
|
|
|
@@ -40,20 +40,20 @@ pnpm i express-response-middleware
|
|
|
40
40
|
Sample middleware (redact.js) to remove classified information.
|
|
41
41
|
|
|
42
42
|
```javascript
|
|
43
|
-
import { jsonMiddleware } from 'express-response-middleware'
|
|
43
|
+
import { jsonMiddleware } from 'express-response-middleware';
|
|
44
44
|
|
|
45
45
|
/* Remove any classified information from the response. */
|
|
46
46
|
export const hideSecretMiddleware = jsonMiddleware((body, req, res) => {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
})
|
|
47
|
+
if (body.secret) body.secret = '****';
|
|
48
|
+
// ...
|
|
49
|
+
return body;
|
|
50
|
+
});
|
|
51
51
|
```
|
|
52
52
|
|
|
53
53
|
then add to your `app.js` file (before the route handling middleware)
|
|
54
54
|
|
|
55
55
|
```javascript
|
|
56
|
-
app.use(hideSecretMiddleware)
|
|
56
|
+
app.use(hideSecretMiddleware);
|
|
57
57
|
```
|
|
58
58
|
|
|
59
59
|
## 💻 API
|
|
@@ -61,8 +61,8 @@ app.use(hideSecretMiddleware)
|
|
|
61
61
|
- [Json Middleware](#jsonmiddleware)
|
|
62
62
|
- [Jsonp Middleware](#jsonpmiddleware)
|
|
63
63
|
- [Send Middleware](#sendmiddleware)
|
|
64
|
-
- [End Middleware](#
|
|
65
|
-
- [Write Middleware](#
|
|
64
|
+
- [End Middleware](#endmiddleware)
|
|
65
|
+
- [Write Middleware](#writemiddleware)
|
|
66
66
|
- Legacy docs
|
|
67
67
|
- [V1](docs/V1_README.md)
|
|
68
68
|
|
|
@@ -71,12 +71,12 @@ app.use(hideSecretMiddleware)
|
|
|
71
71
|
Intercept `res.json`, allow transform the JSON body of the response.
|
|
72
72
|
|
|
73
73
|
```ts
|
|
74
|
-
import { jsonMiddleware } from 'express-response-middleware'
|
|
74
|
+
import { jsonMiddleware } from 'express-response-middleware';
|
|
75
75
|
|
|
76
76
|
const myMiddleware = jsonMiddleware((json, req, res) => {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
})
|
|
77
|
+
// your code here
|
|
78
|
+
return json;
|
|
79
|
+
});
|
|
80
80
|
```
|
|
81
81
|
|
|
82
82
|
### `jsonpMiddleware`
|
|
@@ -84,12 +84,12 @@ const myMiddleware = jsonMiddleware((json, req, res) => {
|
|
|
84
84
|
Intercept `res.jsonp`, allow transform the JSON body of the response.
|
|
85
85
|
|
|
86
86
|
```ts
|
|
87
|
-
import { jsonpMiddleware } from 'express-response-middleware'
|
|
87
|
+
import { jsonpMiddleware } from 'express-response-middleware';
|
|
88
88
|
|
|
89
89
|
const myMiddleware = jsonpMiddleware((json, req, res) => {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
})
|
|
90
|
+
// your code here
|
|
91
|
+
return json;
|
|
92
|
+
});
|
|
93
93
|
```
|
|
94
94
|
|
|
95
95
|
### `sendMiddleware`
|
|
@@ -97,24 +97,24 @@ const myMiddleware = jsonpMiddleware((json, req, res) => {
|
|
|
97
97
|
Intercept `res.send`, allow transform the body of the response.
|
|
98
98
|
|
|
99
99
|
```ts
|
|
100
|
-
import { sendMiddleware } from 'express-response-middleware'
|
|
100
|
+
import { sendMiddleware } from 'express-response-middleware';
|
|
101
101
|
|
|
102
102
|
const myMiddleware = sendMiddleware((payload, req, res) => {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
})
|
|
103
|
+
// your code here
|
|
104
|
+
return payload;
|
|
105
|
+
});
|
|
106
106
|
```
|
|
107
107
|
|
|
108
108
|
### `endMiddleware`
|
|
109
109
|
|
|
110
|
-
Intercept `end
|
|
110
|
+
Intercept `res.end`, allow transform the HTTP headers of the response.
|
|
111
111
|
|
|
112
112
|
```ts
|
|
113
|
-
import { endMiddleware } from 'express-response-middleware'
|
|
113
|
+
import { endMiddleware } from 'express-response-middleware';
|
|
114
114
|
|
|
115
115
|
const myMiddleware = endMiddleware((req, res) => {
|
|
116
|
-
|
|
117
|
-
})
|
|
116
|
+
// your code here
|
|
117
|
+
});
|
|
118
118
|
```
|
|
119
119
|
|
|
120
120
|
> [!CAUTION]
|
|
@@ -122,23 +122,22 @@ const myMiddleware = endMiddleware((req, res) => {
|
|
|
122
122
|
|
|
123
123
|
### `writeMiddleware`
|
|
124
124
|
|
|
125
|
-
Intercept `
|
|
125
|
+
Intercept `res.write` and `res.end`, allow transform each chunk of the body. Chunks are piped through a Node.js `Transform` stream, so async callbacks are processed in order and backpressure is respected.
|
|
126
126
|
|
|
127
127
|
```ts
|
|
128
|
-
import { writeMiddleware } from 'express-response-middleware'
|
|
128
|
+
import { writeMiddleware } from 'express-response-middleware';
|
|
129
129
|
|
|
130
130
|
const myMiddleware = writeMiddleware((chunk, encoding, req, res) => {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
})
|
|
131
|
+
// your code here
|
|
132
|
+
return chunk;
|
|
133
|
+
});
|
|
134
134
|
```
|
|
135
135
|
|
|
136
|
-
> [!CAUTION]
|
|
137
|
-
> Promise callback support is limited, it doesn't resolve multiple write call yet [Code](src/writeMiddleware.ts)
|
|
138
|
-
|
|
139
136
|
- When `writeMiddleware` detects that a response has completed (i.e. if `res.end` has been called), it will abort.
|
|
140
|
-
- Calling `res.json` or `res.send` from `writeMiddleware`
|
|
141
|
-
-
|
|
137
|
+
- Calling `res.json` or `res.send` from `writeMiddleware` ends the response; the remaining chunks are dropped.
|
|
138
|
+
- Writes are flushed asynchronously, so `res.headersSent` may still be `false` right after `res.write`.
|
|
139
|
+
- `Content-Length` is removed since the transformed body length is unknown; the response uses chunked encoding.
|
|
140
|
+
- Chunk boundaries are not guaranteed: chunks may be merged as they pass through the stream.
|
|
142
141
|
- `res.end` after `res.write` would not trigger endMiddleware, as header already sent
|
|
143
142
|
|
|
144
143
|
## Exception handling
|
|
@@ -147,12 +146,6 @@ const myMiddleware = writeMiddleware((chunk, encoding, req, res) => {
|
|
|
147
146
|
|
|
148
147
|
## 🤝 Contributing
|
|
149
148
|
|
|
150
|
-
### TODO
|
|
151
|
-
|
|
152
|
-
Current state project is up to modern standard and support all of the `express-mung` use case, here is the list that I think can improve on.
|
|
153
|
-
|
|
154
|
-
- [ ] Support multiple write call with `writeMiddleware` + async handler
|
|
155
|
-
|
|
156
149
|
### Development
|
|
157
150
|
|
|
158
151
|
#### Local Development
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let node_async_hooks = require("node:async_hooks");
|
|
3
|
+
let node_stream = require("node:stream");
|
|
4
|
+
// src/utils/errorHandler.ts
|
|
5
|
+
const errorHandler = (err, _req, res, _next) => {
|
|
6
|
+
res.status(500).set("content-language", "en").json({ message: err.message }).end();
|
|
7
|
+
};
|
|
8
|
+
// src/endMiddleware.ts
|
|
9
|
+
const endMiddleware = (fn) => (req, res, next) => {
|
|
10
|
+
const originalEndFn = res.end;
|
|
11
|
+
res.end = function() {
|
|
12
|
+
if (res.headersSent) return originalEndFn.apply(this, arguments);
|
|
13
|
+
let mayBePromise;
|
|
14
|
+
try {
|
|
15
|
+
mayBePromise = fn(req, res);
|
|
16
|
+
} catch (e) {
|
|
17
|
+
res.end = originalEndFn;
|
|
18
|
+
errorHandler(e, req, res, next);
|
|
19
|
+
return res;
|
|
20
|
+
}
|
|
21
|
+
if (mayBePromise instanceof Promise) {
|
|
22
|
+
mayBePromise.then(() => {
|
|
23
|
+
if (res.headersSent) {
|
|
24
|
+
console.error("sending response while in endMiddleware is undefined behaviour");
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
originalEndFn.apply(this, arguments);
|
|
28
|
+
}).catch((e) => {
|
|
29
|
+
res.end = originalEndFn;
|
|
30
|
+
errorHandler(e, req, res, next);
|
|
31
|
+
});
|
|
32
|
+
res.end = function() {
|
|
33
|
+
return res;
|
|
34
|
+
};
|
|
35
|
+
} else {
|
|
36
|
+
if (res.headersSent) {
|
|
37
|
+
console.error("sending response while in endMiddleware is undefined behaviour");
|
|
38
|
+
return res;
|
|
39
|
+
}
|
|
40
|
+
return originalEndFn.apply(this, arguments);
|
|
41
|
+
}
|
|
42
|
+
return res;
|
|
43
|
+
};
|
|
44
|
+
next();
|
|
45
|
+
};
|
|
46
|
+
// src/utils/asyncLocalStorage.ts
|
|
47
|
+
const isInSend = new node_async_hooks.AsyncLocalStorage();
|
|
48
|
+
// src/jsonMiddleware.ts
|
|
49
|
+
const jsonMiddleware = (fn) => (req, res, next) => {
|
|
50
|
+
const originalJsonFn = res.json;
|
|
51
|
+
const originalSendFn = res.send;
|
|
52
|
+
res.send = function() {
|
|
53
|
+
return isInSend.run(true, () => originalSendFn.apply(this, arguments));
|
|
54
|
+
};
|
|
55
|
+
res.json = function(json) {
|
|
56
|
+
const originalEndFn = res.end;
|
|
57
|
+
if (res.headersSent) return res;
|
|
58
|
+
let mayBePromise;
|
|
59
|
+
try {
|
|
60
|
+
mayBePromise = fn(json, req, res);
|
|
61
|
+
} catch (e) {
|
|
62
|
+
res.json = originalJsonFn;
|
|
63
|
+
errorHandler(e, req, res, next);
|
|
64
|
+
return res;
|
|
65
|
+
}
|
|
66
|
+
if (mayBePromise instanceof Promise) {
|
|
67
|
+
mayBePromise.then((result) => {
|
|
68
|
+
if (res.headersSent) return;
|
|
69
|
+
res.end = originalEndFn;
|
|
70
|
+
originalJsonFn.call(this, result === void 0 ? json : result);
|
|
71
|
+
}).catch((e) => {
|
|
72
|
+
res.json = originalJsonFn;
|
|
73
|
+
res.end = originalEndFn;
|
|
74
|
+
errorHandler(e, req, res, next);
|
|
75
|
+
});
|
|
76
|
+
res.end = function() {
|
|
77
|
+
if (isInSend.getStore()) originalEndFn.apply(this, arguments);
|
|
78
|
+
return res;
|
|
79
|
+
};
|
|
80
|
+
} else {
|
|
81
|
+
const result = mayBePromise;
|
|
82
|
+
if (res.headersSent) return res;
|
|
83
|
+
originalJsonFn.call(this, result === void 0 ? json : result);
|
|
84
|
+
}
|
|
85
|
+
return res;
|
|
86
|
+
};
|
|
87
|
+
next();
|
|
88
|
+
};
|
|
89
|
+
// src/jsonpMiddleware.ts
|
|
90
|
+
const jsonpMiddleware = (fn) => (req, res, next) => {
|
|
91
|
+
const originalJsonpFn = res.jsonp;
|
|
92
|
+
const originalSendFn = res.send;
|
|
93
|
+
res.send = function() {
|
|
94
|
+
return isInSend.run(true, () => originalSendFn.apply(this, arguments));
|
|
95
|
+
};
|
|
96
|
+
res.jsonp = function(json) {
|
|
97
|
+
const originalEndFn = res.end;
|
|
98
|
+
if (res.headersSent) return res;
|
|
99
|
+
let mayBePromise;
|
|
100
|
+
try {
|
|
101
|
+
mayBePromise = fn(json, req, res);
|
|
102
|
+
} catch (e) {
|
|
103
|
+
res.jsonp = originalJsonpFn;
|
|
104
|
+
errorHandler(e, req, res, next);
|
|
105
|
+
return res;
|
|
106
|
+
}
|
|
107
|
+
if (mayBePromise instanceof Promise) {
|
|
108
|
+
mayBePromise.then((result) => {
|
|
109
|
+
if (res.headersSent) return;
|
|
110
|
+
res.end = originalEndFn;
|
|
111
|
+
originalJsonpFn.call(this, result === void 0 ? json : result);
|
|
112
|
+
}).catch((e) => {
|
|
113
|
+
res.jsonp = originalJsonpFn;
|
|
114
|
+
res.end = originalEndFn;
|
|
115
|
+
errorHandler(e, req, res, next);
|
|
116
|
+
});
|
|
117
|
+
res.end = function() {
|
|
118
|
+
if (isInSend.getStore()) originalEndFn.apply(this, arguments);
|
|
119
|
+
return res;
|
|
120
|
+
};
|
|
121
|
+
} else {
|
|
122
|
+
const result = mayBePromise;
|
|
123
|
+
if (res.headersSent) return res;
|
|
124
|
+
originalJsonpFn.call(this, result === void 0 ? json : result);
|
|
125
|
+
}
|
|
126
|
+
return res;
|
|
127
|
+
};
|
|
128
|
+
next();
|
|
129
|
+
};
|
|
130
|
+
// src/sendMiddleware.ts
|
|
131
|
+
const sendMiddleware = (fn) => (req, res, next) => {
|
|
132
|
+
const originalSendFn = res.send;
|
|
133
|
+
res.send = function(payload) {
|
|
134
|
+
if (res.headersSent) return res;
|
|
135
|
+
let mayBePromise;
|
|
136
|
+
try {
|
|
137
|
+
mayBePromise = fn(payload, req, res);
|
|
138
|
+
} catch (e) {
|
|
139
|
+
res.send = originalSendFn;
|
|
140
|
+
errorHandler(e, req, res, next);
|
|
141
|
+
return res;
|
|
142
|
+
}
|
|
143
|
+
if (mayBePromise instanceof Promise) mayBePromise.then((result) => {
|
|
144
|
+
if (res.headersSent) return;
|
|
145
|
+
originalSendFn.call(this, result === void 0 ? payload : result);
|
|
146
|
+
}).catch((e) => {
|
|
147
|
+
res.send = originalSendFn;
|
|
148
|
+
errorHandler(e, req, res, next);
|
|
149
|
+
});
|
|
150
|
+
else {
|
|
151
|
+
const result = mayBePromise;
|
|
152
|
+
if (res.headersSent) return res;
|
|
153
|
+
originalSendFn.call(this, result === void 0 ? payload : result);
|
|
154
|
+
}
|
|
155
|
+
return res;
|
|
156
|
+
};
|
|
157
|
+
next();
|
|
158
|
+
};
|
|
159
|
+
// src/writeMiddleware.ts
|
|
160
|
+
const writeMiddleware = (fn) => (req, res, next) => {
|
|
161
|
+
const originalWrite = res.write;
|
|
162
|
+
const originalEnd = res.end;
|
|
163
|
+
const callWrite = (args) => originalWrite.apply(res, args);
|
|
164
|
+
const callEnd = (args) => originalEnd.apply(res, args);
|
|
165
|
+
const inTransform = new node_async_hooks.AsyncLocalStorage();
|
|
166
|
+
let endCallback;
|
|
167
|
+
const transform = new node_stream.Transform({
|
|
168
|
+
decodeStrings: false,
|
|
169
|
+
transform(chunk, encoding, callback) {
|
|
170
|
+
const stringEncoding = encoding === "buffer" ? null : encoding;
|
|
171
|
+
inTransform.run(true, async () => fn(chunk, stringEncoding, req, res)).then((result) => {
|
|
172
|
+
const output = result === void 0 ? chunk : result;
|
|
173
|
+
if (typeof output === "string" && stringEncoding) this.push(output, stringEncoding);
|
|
174
|
+
else this.push(output);
|
|
175
|
+
callback();
|
|
176
|
+
}, (e) => callback(e));
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
transform.on("data", (chunk) => {
|
|
180
|
+
if (res.writableEnded) return;
|
|
181
|
+
if (!res.headersSent) res.removeHeader("Content-Length");
|
|
182
|
+
if (!callWrite([chunk])) transform.pause();
|
|
183
|
+
});
|
|
184
|
+
res.on("drain", () => transform.resume());
|
|
185
|
+
transform.on("drain", () => res.emit("drain"));
|
|
186
|
+
transform.on("end", () => {
|
|
187
|
+
if (res.writableEnded) return;
|
|
188
|
+
callEnd(endCallback ? [endCallback] : []);
|
|
189
|
+
});
|
|
190
|
+
transform.on("error", (e) => {
|
|
191
|
+
res.write = originalWrite;
|
|
192
|
+
res.end = originalEnd;
|
|
193
|
+
if (res.headersSent) res.destroy(e);
|
|
194
|
+
else errorHandler(e, req, res, next);
|
|
195
|
+
});
|
|
196
|
+
res.on("close", () => {
|
|
197
|
+
if (!transform.destroyed) transform.destroy();
|
|
198
|
+
});
|
|
199
|
+
res.write = function(chunk, ...args) {
|
|
200
|
+
if (inTransform.getStore()) return callWrite([chunk, ...args]);
|
|
201
|
+
if (transform.writableEnded || res.writableEnded) return false;
|
|
202
|
+
const encoding = typeof args[0] === "string" ? args[0] : void 0;
|
|
203
|
+
const callback = typeof args[0] === "function" ? args[0] : args[1];
|
|
204
|
+
return transform.write(chunk, encoding, callback);
|
|
205
|
+
};
|
|
206
|
+
res.end = function(...args) {
|
|
207
|
+
if (inTransform.getStore()) return callEnd(args);
|
|
208
|
+
if (transform.writableEnded || res.writableEnded) return res;
|
|
209
|
+
const [chunk, encoding, callback] = typeof args[0] === "function" ? [
|
|
210
|
+
void 0,
|
|
211
|
+
void 0,
|
|
212
|
+
args[0]
|
|
213
|
+
] : typeof args[1] === "function" ? [
|
|
214
|
+
args[0],
|
|
215
|
+
void 0,
|
|
216
|
+
args[1]
|
|
217
|
+
] : args;
|
|
218
|
+
endCallback = callback;
|
|
219
|
+
if (chunk != null) transform.end(chunk, encoding);
|
|
220
|
+
else transform.end();
|
|
221
|
+
return res;
|
|
222
|
+
};
|
|
223
|
+
next();
|
|
224
|
+
};
|
|
225
|
+
exports.endMiddleware = endMiddleware;
|
|
226
|
+
exports.jsonMiddleware = jsonMiddleware;
|
|
227
|
+
exports.jsonpMiddleware = jsonpMiddleware;
|
|
228
|
+
exports.sendMiddleware = sendMiddleware;
|
|
229
|
+
exports.writeMiddleware = writeMiddleware;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Request, RequestHandler, Response } from "express";
|
|
2
|
+
export type TransformEnd<T = unknown> = (request: Request, response: Response) => T | Promise<T>;
|
|
3
|
+
export declare const endMiddleware: <T = unknown>(fn: TransformEnd<T>) => RequestHandler;
|
|
4
|
+
export type TransformJson<T = unknown, R = T> = (body: T, request: Request, response: Response) => R | Promise<R>;
|
|
5
|
+
export declare const jsonMiddleware: <T = unknown>(fn: TransformJson<T>) => RequestHandler;
|
|
6
|
+
export type TransformJsonp<T = unknown, R = T> = (body: T, request: Request, response: Response) => R | Promise<R>;
|
|
7
|
+
export declare const jsonpMiddleware: <T = unknown>(fn: TransformJsonp<T>) => RequestHandler;
|
|
8
|
+
export type TransformSend<T = unknown> = (body: T, request: Request, response: Response) => unknown | Promise<unknown>;
|
|
9
|
+
export declare const sendMiddleware: (fn: TransformSend) => RequestHandler;
|
|
10
|
+
export type TransformChunk = (chunk: string | Buffer, encoding: string | null, request: Request, response: Response) => void | string | Buffer | Promise<void> | Promise<string> | Promise<Buffer>;
|
|
11
|
+
export declare const writeMiddleware: (fn: TransformChunk) => RequestHandler;
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Request, RequestHandler, Response } from "express";
|
|
2
|
+
export type TransformEnd<T = unknown> = (request: Request, response: Response) => T | Promise<T>;
|
|
3
|
+
export declare const endMiddleware: <T = unknown>(fn: TransformEnd<T>) => RequestHandler;
|
|
4
|
+
export type TransformJson<T = unknown, R = T> = (body: T, request: Request, response: Response) => R | Promise<R>;
|
|
5
|
+
export declare const jsonMiddleware: <T = unknown>(fn: TransformJson<T>) => RequestHandler;
|
|
6
|
+
export type TransformJsonp<T = unknown, R = T> = (body: T, request: Request, response: Response) => R | Promise<R>;
|
|
7
|
+
export declare const jsonpMiddleware: <T = unknown>(fn: TransformJsonp<T>) => RequestHandler;
|
|
8
|
+
export type TransformSend<T = unknown> = (body: T, request: Request, response: Response) => unknown | Promise<unknown>;
|
|
9
|
+
export declare const sendMiddleware: (fn: TransformSend) => RequestHandler;
|
|
10
|
+
export type TransformChunk = (chunk: string | Buffer, encoding: string | null, request: Request, response: Response) => void | string | Buffer | Promise<void> | Promise<string> | Promise<Buffer>;
|
|
11
|
+
export declare const writeMiddleware: (fn: TransformChunk) => RequestHandler;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { Transform } from "node:stream";
|
|
3
|
+
// 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
|
+
// src/endMiddleware.ts
|
|
8
|
+
const endMiddleware = (fn) => (req, res, next) => {
|
|
9
|
+
const originalEndFn = res.end;
|
|
10
|
+
res.end = function() {
|
|
11
|
+
if (res.headersSent) return originalEndFn.apply(this, arguments);
|
|
12
|
+
let mayBePromise;
|
|
13
|
+
try {
|
|
14
|
+
mayBePromise = fn(req, res);
|
|
15
|
+
} catch (e) {
|
|
16
|
+
res.end = originalEndFn;
|
|
17
|
+
errorHandler(e, req, res, next);
|
|
18
|
+
return res;
|
|
19
|
+
}
|
|
20
|
+
if (mayBePromise instanceof Promise) {
|
|
21
|
+
mayBePromise.then(() => {
|
|
22
|
+
if (res.headersSent) {
|
|
23
|
+
console.error("sending response while in endMiddleware is undefined behaviour");
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
originalEndFn.apply(this, arguments);
|
|
27
|
+
}).catch((e) => {
|
|
28
|
+
res.end = originalEndFn;
|
|
29
|
+
errorHandler(e, req, res, next);
|
|
30
|
+
});
|
|
31
|
+
res.end = function() {
|
|
32
|
+
return res;
|
|
33
|
+
};
|
|
34
|
+
} else {
|
|
35
|
+
if (res.headersSent) {
|
|
36
|
+
console.error("sending response while in endMiddleware is undefined behaviour");
|
|
37
|
+
return res;
|
|
38
|
+
}
|
|
39
|
+
return originalEndFn.apply(this, arguments);
|
|
40
|
+
}
|
|
41
|
+
return res;
|
|
42
|
+
};
|
|
43
|
+
next();
|
|
44
|
+
};
|
|
45
|
+
// src/utils/asyncLocalStorage.ts
|
|
46
|
+
const isInSend = new AsyncLocalStorage();
|
|
47
|
+
// src/jsonMiddleware.ts
|
|
48
|
+
const jsonMiddleware = (fn) => (req, res, next) => {
|
|
49
|
+
const originalJsonFn = res.json;
|
|
50
|
+
const originalSendFn = res.send;
|
|
51
|
+
res.send = function() {
|
|
52
|
+
return isInSend.run(true, () => originalSendFn.apply(this, arguments));
|
|
53
|
+
};
|
|
54
|
+
res.json = function(json) {
|
|
55
|
+
const originalEndFn = res.end;
|
|
56
|
+
if (res.headersSent) return res;
|
|
57
|
+
let mayBePromise;
|
|
58
|
+
try {
|
|
59
|
+
mayBePromise = fn(json, req, res);
|
|
60
|
+
} catch (e) {
|
|
61
|
+
res.json = originalJsonFn;
|
|
62
|
+
errorHandler(e, req, res, next);
|
|
63
|
+
return res;
|
|
64
|
+
}
|
|
65
|
+
if (mayBePromise instanceof Promise) {
|
|
66
|
+
mayBePromise.then((result) => {
|
|
67
|
+
if (res.headersSent) return;
|
|
68
|
+
res.end = originalEndFn;
|
|
69
|
+
originalJsonFn.call(this, result === void 0 ? json : result);
|
|
70
|
+
}).catch((e) => {
|
|
71
|
+
res.json = originalJsonFn;
|
|
72
|
+
res.end = originalEndFn;
|
|
73
|
+
errorHandler(e, req, res, next);
|
|
74
|
+
});
|
|
75
|
+
res.end = function() {
|
|
76
|
+
if (isInSend.getStore()) originalEndFn.apply(this, arguments);
|
|
77
|
+
return res;
|
|
78
|
+
};
|
|
79
|
+
} else {
|
|
80
|
+
const result = mayBePromise;
|
|
81
|
+
if (res.headersSent) return res;
|
|
82
|
+
originalJsonFn.call(this, result === void 0 ? json : result);
|
|
83
|
+
}
|
|
84
|
+
return res;
|
|
85
|
+
};
|
|
86
|
+
next();
|
|
87
|
+
};
|
|
88
|
+
// src/jsonpMiddleware.ts
|
|
89
|
+
const jsonpMiddleware = (fn) => (req, res, next) => {
|
|
90
|
+
const originalJsonpFn = res.jsonp;
|
|
91
|
+
const originalSendFn = res.send;
|
|
92
|
+
res.send = function() {
|
|
93
|
+
return isInSend.run(true, () => originalSendFn.apply(this, arguments));
|
|
94
|
+
};
|
|
95
|
+
res.jsonp = function(json) {
|
|
96
|
+
const originalEndFn = res.end;
|
|
97
|
+
if (res.headersSent) return res;
|
|
98
|
+
let mayBePromise;
|
|
99
|
+
try {
|
|
100
|
+
mayBePromise = fn(json, req, res);
|
|
101
|
+
} catch (e) {
|
|
102
|
+
res.jsonp = originalJsonpFn;
|
|
103
|
+
errorHandler(e, req, res, next);
|
|
104
|
+
return res;
|
|
105
|
+
}
|
|
106
|
+
if (mayBePromise instanceof Promise) {
|
|
107
|
+
mayBePromise.then((result) => {
|
|
108
|
+
if (res.headersSent) return;
|
|
109
|
+
res.end = originalEndFn;
|
|
110
|
+
originalJsonpFn.call(this, result === void 0 ? json : result);
|
|
111
|
+
}).catch((e) => {
|
|
112
|
+
res.jsonp = originalJsonpFn;
|
|
113
|
+
res.end = originalEndFn;
|
|
114
|
+
errorHandler(e, req, res, next);
|
|
115
|
+
});
|
|
116
|
+
res.end = function() {
|
|
117
|
+
if (isInSend.getStore()) originalEndFn.apply(this, arguments);
|
|
118
|
+
return res;
|
|
119
|
+
};
|
|
120
|
+
} else {
|
|
121
|
+
const result = mayBePromise;
|
|
122
|
+
if (res.headersSent) return res;
|
|
123
|
+
originalJsonpFn.call(this, result === void 0 ? json : result);
|
|
124
|
+
}
|
|
125
|
+
return res;
|
|
126
|
+
};
|
|
127
|
+
next();
|
|
128
|
+
};
|
|
129
|
+
// src/sendMiddleware.ts
|
|
130
|
+
const sendMiddleware = (fn) => (req, res, next) => {
|
|
131
|
+
const originalSendFn = res.send;
|
|
132
|
+
res.send = function(payload) {
|
|
133
|
+
if (res.headersSent) return res;
|
|
134
|
+
let mayBePromise;
|
|
135
|
+
try {
|
|
136
|
+
mayBePromise = fn(payload, req, res);
|
|
137
|
+
} catch (e) {
|
|
138
|
+
res.send = originalSendFn;
|
|
139
|
+
errorHandler(e, req, res, next);
|
|
140
|
+
return res;
|
|
141
|
+
}
|
|
142
|
+
if (mayBePromise instanceof Promise) mayBePromise.then((result) => {
|
|
143
|
+
if (res.headersSent) return;
|
|
144
|
+
originalSendFn.call(this, result === void 0 ? payload : result);
|
|
145
|
+
}).catch((e) => {
|
|
146
|
+
res.send = originalSendFn;
|
|
147
|
+
errorHandler(e, req, res, next);
|
|
148
|
+
});
|
|
149
|
+
else {
|
|
150
|
+
const result = mayBePromise;
|
|
151
|
+
if (res.headersSent) return res;
|
|
152
|
+
originalSendFn.call(this, result === void 0 ? payload : result);
|
|
153
|
+
}
|
|
154
|
+
return res;
|
|
155
|
+
};
|
|
156
|
+
next();
|
|
157
|
+
};
|
|
158
|
+
// src/writeMiddleware.ts
|
|
159
|
+
const writeMiddleware = (fn) => (req, res, next) => {
|
|
160
|
+
const originalWrite = res.write;
|
|
161
|
+
const originalEnd = res.end;
|
|
162
|
+
const callWrite = (args) => originalWrite.apply(res, args);
|
|
163
|
+
const callEnd = (args) => originalEnd.apply(res, args);
|
|
164
|
+
const inTransform = new AsyncLocalStorage();
|
|
165
|
+
let endCallback;
|
|
166
|
+
const transform = new Transform({
|
|
167
|
+
decodeStrings: false,
|
|
168
|
+
transform(chunk, encoding, callback) {
|
|
169
|
+
const stringEncoding = encoding === "buffer" ? null : encoding;
|
|
170
|
+
inTransform.run(true, async () => fn(chunk, stringEncoding, req, res)).then((result) => {
|
|
171
|
+
const output = result === void 0 ? chunk : result;
|
|
172
|
+
if (typeof output === "string" && stringEncoding) this.push(output, stringEncoding);
|
|
173
|
+
else this.push(output);
|
|
174
|
+
callback();
|
|
175
|
+
}, (e) => callback(e));
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
transform.on("data", (chunk) => {
|
|
179
|
+
if (res.writableEnded) return;
|
|
180
|
+
if (!res.headersSent) res.removeHeader("Content-Length");
|
|
181
|
+
if (!callWrite([chunk])) transform.pause();
|
|
182
|
+
});
|
|
183
|
+
res.on("drain", () => transform.resume());
|
|
184
|
+
transform.on("drain", () => res.emit("drain"));
|
|
185
|
+
transform.on("end", () => {
|
|
186
|
+
if (res.writableEnded) return;
|
|
187
|
+
callEnd(endCallback ? [endCallback] : []);
|
|
188
|
+
});
|
|
189
|
+
transform.on("error", (e) => {
|
|
190
|
+
res.write = originalWrite;
|
|
191
|
+
res.end = originalEnd;
|
|
192
|
+
if (res.headersSent) res.destroy(e);
|
|
193
|
+
else errorHandler(e, req, res, next);
|
|
194
|
+
});
|
|
195
|
+
res.on("close", () => {
|
|
196
|
+
if (!transform.destroyed) transform.destroy();
|
|
197
|
+
});
|
|
198
|
+
res.write = function(chunk, ...args) {
|
|
199
|
+
if (inTransform.getStore()) return callWrite([chunk, ...args]);
|
|
200
|
+
if (transform.writableEnded || res.writableEnded) return false;
|
|
201
|
+
const encoding = typeof args[0] === "string" ? args[0] : void 0;
|
|
202
|
+
const callback = typeof args[0] === "function" ? args[0] : args[1];
|
|
203
|
+
return transform.write(chunk, encoding, callback);
|
|
204
|
+
};
|
|
205
|
+
res.end = function(...args) {
|
|
206
|
+
if (inTransform.getStore()) return callEnd(args);
|
|
207
|
+
if (transform.writableEnded || res.writableEnded) return res;
|
|
208
|
+
const [chunk, encoding, callback] = typeof args[0] === "function" ? [
|
|
209
|
+
void 0,
|
|
210
|
+
void 0,
|
|
211
|
+
args[0]
|
|
212
|
+
] : typeof args[1] === "function" ? [
|
|
213
|
+
args[0],
|
|
214
|
+
void 0,
|
|
215
|
+
args[1]
|
|
216
|
+
] : args;
|
|
217
|
+
endCallback = callback;
|
|
218
|
+
if (chunk != null) transform.end(chunk, encoding);
|
|
219
|
+
else transform.end();
|
|
220
|
+
return res;
|
|
221
|
+
};
|
|
222
|
+
next();
|
|
223
|
+
};
|
|
224
|
+
export { endMiddleware, jsonMiddleware, jsonpMiddleware, sendMiddleware, writeMiddleware };
|
package/package.json
CHANGED
|
@@ -1,65 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-response-middleware",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "Transform an express response",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"mung",
|
|
7
|
+
"middleware",
|
|
8
|
+
"transform",
|
|
9
|
+
"response",
|
|
10
|
+
"express"
|
|
7
11
|
],
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
|
|
12
|
+
"homepage": "https://github.com/marklai1998/express-response-middleware#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/marklai1998/express-response-middleware/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/marklai1998/express-response-middleware"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
11
21
|
"exports": {
|
|
12
22
|
".": {
|
|
13
23
|
"require": {
|
|
14
|
-
"types": "./dist/
|
|
15
|
-
"default": "./dist/
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
16
26
|
},
|
|
17
27
|
"import": {
|
|
18
|
-
"types": "./dist/
|
|
19
|
-
"default": "./dist/
|
|
28
|
+
"types": "./dist/index.d.mts",
|
|
29
|
+
"default": "./dist/index.mjs"
|
|
20
30
|
}
|
|
21
31
|
}
|
|
22
32
|
},
|
|
33
|
+
"main": "dist/index.js",
|
|
34
|
+
"module": "dist/index.mjs",
|
|
35
|
+
"types": "dist/index.d.mts",
|
|
23
36
|
"typesVersions": {
|
|
24
37
|
"*": {
|
|
25
38
|
"*": [
|
|
26
|
-
"./dist/
|
|
39
|
+
"./dist/index.d.mts"
|
|
27
40
|
]
|
|
28
41
|
}
|
|
29
42
|
},
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"url": "git+https://github.com/marklai1998/express-response-middleware"
|
|
33
|
-
},
|
|
34
|
-
"keywords": [
|
|
35
|
-
"mung",
|
|
36
|
-
"middleware",
|
|
37
|
-
"transform",
|
|
38
|
-
"response",
|
|
39
|
-
"express"
|
|
43
|
+
"files": [
|
|
44
|
+
"dist"
|
|
40
45
|
],
|
|
41
|
-
"license": "MIT",
|
|
42
|
-
"bugs": {
|
|
43
|
-
"url": "https://github.com/marklai1998/express-response-middleware/issues"
|
|
44
|
-
},
|
|
45
|
-
"homepage": "https://github.com/marklai1998/express-response-middleware#readme",
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@biomejs/biome": "
|
|
48
|
-
"@commitlint/cli": "
|
|
49
|
-
"@
|
|
50
|
-
"@
|
|
51
|
-
"@
|
|
52
|
-
"@types/
|
|
53
|
-
"@
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"typescript": "5.8.3",
|
|
62
|
-
"vitest": "3.1.4"
|
|
47
|
+
"@biomejs/biome": "2.6.2",
|
|
48
|
+
"@commitlint/cli": "21.2.4",
|
|
49
|
+
"@marklai/shared-config-biome": "1.0.2",
|
|
50
|
+
"@marklai/shared-config-commitlint": "1.1.2",
|
|
51
|
+
"@marklai/shared-config-typescript": "1.1.2",
|
|
52
|
+
"@types/express": "5.0.6",
|
|
53
|
+
"@types/node": "26.6.5",
|
|
54
|
+
"@types/supertest": "7.2.1",
|
|
55
|
+
"@vitest/coverage-v8": "5.1.0",
|
|
56
|
+
"lefthook": "2.2.2",
|
|
57
|
+
"supertest": "7.3.1",
|
|
58
|
+
"tsdown": "0.23.1",
|
|
59
|
+
"typescript": "7.0.2",
|
|
60
|
+
"vitest": "5.1.0"
|
|
63
61
|
},
|
|
64
62
|
"peerDependencies": {
|
|
65
63
|
"express": "^4.0.0 || ^5.0.0"
|
|
@@ -68,7 +66,7 @@
|
|
|
68
66
|
"node": ">=18"
|
|
69
67
|
},
|
|
70
68
|
"scripts": {
|
|
71
|
-
"build": "
|
|
69
|
+
"build": "tsdown --format esm --format cjs",
|
|
72
70
|
"test": "vitest run --coverage"
|
|
73
71
|
}
|
|
74
72
|
}
|
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,KAAA;AAClB,EAAA,MAAM,iBAAiB,GAAI,CAAA,IAAA;AAC3B,EAAA,MAAM,iBAAiB,GAAI,CAAA,IAAA;AAE3B,EAAA,GAAA,CAAI,OAAO,WAA0B;AACnC,IAAA,OAAOD,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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AAK,CAAA,IAAA,EAAM,MAAW,KAAA,MAAA,GAAY,OAAO,MAAM,CAAA;AAAA,OAChE,CAAA,CACA,KAAM,CAAA,CAAC,CAAM,KAAA;AACZ,QAAA,GAAA,CAAI,KAAQ,GAAA,eAAA;AACZ,QAAA,GAAA,CAAI,GAAM,GAAA,aAAA;AACV,QAAa,YAAA,CAAA,CAAA,EAAG,GAAK,EAAA,GAAS,CAAA;AAAA,OAC/B,CAAA;AAGH,MAAA,GAAA,CAAI,MAAM,WAA0B;AAElC,QAAI,IAAA,QAAA,CAAS,UAAY,EAAA;AACvB,UAAc,aAAA,CAAA,KAAA,CAAM,MAAM,SAAgB,CAAA;AAAA;AAE5C,QAAO,OAAA,GAAA;AAAA,OACT;AAAA,KACK,MAAA;AACL,MAAA,MAAM,MAAS,GAAA,YAAA;AAEf,MAAI,IAAA,GAAA,CAAI,aAAoB,OAAA,GAAA;AAE5B,MAAA,eAAA,CAAgB,IAAK,CAAA,IAAA,EAAM,MAAW,KAAA,MAAA,GAAY,OAAO,MAAM,CAAA;AAAA;AAGjE,IAAO,OAAA,GAAA;AAAA,GACT;AAEA,EAAK,IAAA,EAAA;AACP;;;;;;;"}
|
package/dist/cjs/index.d.cts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Request, Response, RequestHandler } from 'express';
|
|
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;
|
|
5
|
-
|
|
6
|
-
type TransformEnd<T = unknown> = (request: Request, response: Response) => T | Promise<T>;
|
|
7
|
-
declare const endMiddleware: <T = unknown>(fn: TransformEnd<T>) => RequestHandler;
|
|
8
|
-
|
|
9
|
-
type TransformChunk = (chunk: string | Buffer, encoding: string | null, request: Request, response: Response) => void | string | Buffer | Promise<void> | Promise<string> | Promise<Buffer>;
|
|
10
|
-
declare const writeMiddleware: (fn: TransformChunk) => RequestHandler;
|
|
11
|
-
|
|
12
|
-
type TransformJsonp<T = unknown, R = T> = (body: T, request: Request, response: Response) => R | Promise<R>;
|
|
13
|
-
declare const jsonpMiddleware: <T = unknown>(fn: TransformJsonp<T>) => RequestHandler;
|
|
14
|
-
|
|
15
|
-
declare global {
|
|
16
|
-
namespace Express {
|
|
17
|
-
interface Response {
|
|
18
|
-
__isEnd?: boolean;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export { endMiddleware, jsonMiddleware, jsonpMiddleware, writeMiddleware };
|
|
24
|
-
export type { TransformChunk, TransformEnd, TransformJson, TransformJsonp };
|
package/dist/esm/index.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Request, Response, RequestHandler } from 'express';
|
|
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;
|
|
5
|
-
|
|
6
|
-
type TransformEnd<T = unknown> = (request: Request, response: Response) => T | Promise<T>;
|
|
7
|
-
declare const endMiddleware: <T = unknown>(fn: TransformEnd<T>) => RequestHandler;
|
|
8
|
-
|
|
9
|
-
type TransformChunk = (chunk: string | Buffer, encoding: string | null, request: Request, response: Response) => void | string | Buffer | Promise<void> | Promise<string> | Promise<Buffer>;
|
|
10
|
-
declare const writeMiddleware: (fn: TransformChunk) => RequestHandler;
|
|
11
|
-
|
|
12
|
-
type TransformJsonp<T = unknown, R = T> = (body: T, request: Request, response: Response) => R | Promise<R>;
|
|
13
|
-
declare const jsonpMiddleware: <T = unknown>(fn: TransformJsonp<T>) => RequestHandler;
|
|
14
|
-
|
|
15
|
-
declare global {
|
|
16
|
-
namespace Express {
|
|
17
|
-
interface Response {
|
|
18
|
-
__isEnd?: boolean;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export { endMiddleware, jsonMiddleware, jsonpMiddleware, writeMiddleware };
|
|
24
|
-
export type { TransformChunk, TransformEnd, TransformJson, TransformJsonp };
|
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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