express-response-middleware 2.1.2 → 2.1.3
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/package.json +6 -5
- package/.commitlintrc.json +0 -3
- package/.github/workflows/release.yml +0 -42
- package/.github/workflows/runTest.yml +0 -59
- package/.husky/commit-msg +0 -1
- package/.husky/pre-commit +0 -1
- package/.husky/pre-push +0 -1
- package/.node-version +0 -1
- package/CHANGELOG.md +0 -88
- package/biome.json +0 -36
- package/docs/MIGRATE_FROM_MUNG.md +0 -153
- package/docs/V1_README.md +0 -115
- package/pnpm-workspace.yaml +0 -3
- package/renovate.json +0 -9
- package/rollup.config.js +0 -52
- package/src/endMiddleware.ts +0 -61
- package/src/index.ts +0 -12
- package/src/jsonMiddleware.ts +0 -71
- package/src/jsonpMiddleware.ts +0 -73
- package/src/sendMiddleware.ts +0 -50
- package/src/tests/endMiddleware.test.ts +0 -101
- package/src/tests/jsonMiddleware.test.ts +0 -253
- package/src/tests/jsonpMiddleware.test.ts +0 -233
- package/src/tests/mixAndMatch.test.ts +0 -261
- package/src/tests/sendMiddleware.test.ts +0 -219
- package/src/tests/testHelpers/sleep.ts +0 -2
- package/src/tests/writeMiddleware.test.ts +0 -267
- package/src/utils/asyncLocalStorage.ts +0 -3
- package/src/utils/errorHandler.ts +0 -9
- package/src/writeMiddleware.ts +0 -76
- package/tsconfig.json +0 -28
- package/vitest.config.ts +0 -14
|
@@ -1,267 +0,0 @@
|
|
|
1
|
-
import express, { type ErrorRequestHandler } from 'express';
|
|
2
|
-
import request from 'supertest';
|
|
3
|
-
import { expect } from 'vitest';
|
|
4
|
-
import { type TransformChunk, writeMiddleware } from '../index.js';
|
|
5
|
-
import { sleep } from './testHelpers/sleep.js';
|
|
6
|
-
|
|
7
|
-
describe('write', () => {
|
|
8
|
-
const appendText: TransformChunk = (chunk) => {
|
|
9
|
-
return `${chunk} with more content`;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const appendTextAsync: TransformChunk = async (chunk) => {
|
|
13
|
-
await sleep();
|
|
14
|
-
return `${chunk} with more content`;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const doNothing: TransformChunk = () => {};
|
|
18
|
-
|
|
19
|
-
const doNothingAsync: TransformChunk = async () => {
|
|
20
|
-
await sleep();
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const appendText2: TransformChunk = (chunk) => {
|
|
24
|
-
return `${chunk} with more content2`;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const appendTextAsync2: TransformChunk = async (chunk) => {
|
|
28
|
-
await sleep();
|
|
29
|
-
return `${chunk} with more content2`;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const inspectJson: TransformChunk = (chunk) => {
|
|
33
|
-
try {
|
|
34
|
-
const json = JSON.parse(String(chunk));
|
|
35
|
-
json.inspected_by = 'me';
|
|
36
|
-
return JSON.stringify({ ...json, inspected_by: 'me' });
|
|
37
|
-
} catch (e) {
|
|
38
|
-
console.log('JSON parse error');
|
|
39
|
-
throw e;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const inspectJsonAsync: TransformChunk = (chunk) => {
|
|
44
|
-
try {
|
|
45
|
-
sleep();
|
|
46
|
-
const json = JSON.parse(String(chunk));
|
|
47
|
-
json.inspected_by = 'me';
|
|
48
|
-
sleep();
|
|
49
|
-
return JSON.stringify({ ...json, inspected_by: 'me' });
|
|
50
|
-
} catch (e) {
|
|
51
|
-
console.log('JSON parse error');
|
|
52
|
-
throw e;
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const error: TransformChunk = (chunk) => {
|
|
57
|
-
(chunk as any).foo.bar.hopefully.fails();
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const errorAsync: TransformChunk = async (chunk) => {
|
|
61
|
-
await sleep();
|
|
62
|
-
(chunk as any).foo.bar.hopefully.fails();
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const error403: TransformChunk = (_chunk, _encoding, _req, res) => {
|
|
66
|
-
res.status(403).json({ foo: 'bar ' });
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
const error403Async: TransformChunk = (_chunk, _encoding, _req, res) => {
|
|
70
|
-
res.status(403).json({ foo: 'bar ' });
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
it.each([appendText, appendTextAsync])(
|
|
74
|
-
'should return the text result',
|
|
75
|
-
async (handler) => {
|
|
76
|
-
const server = express()
|
|
77
|
-
.use(writeMiddleware(handler))
|
|
78
|
-
.get('/', (_req, res) => {
|
|
79
|
-
res.status(200).write('This is the response body', 'utf-8');
|
|
80
|
-
res.end();
|
|
81
|
-
});
|
|
82
|
-
const response = await request(server).get('/');
|
|
83
|
-
|
|
84
|
-
expect(response.status).toStrictEqual(200);
|
|
85
|
-
expect(response.text).toStrictEqual(
|
|
86
|
-
'This is the response body with more content',
|
|
87
|
-
);
|
|
88
|
-
},
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
it.each([appendText, appendTextAsync])(
|
|
92
|
-
'should not call if header is already sent',
|
|
93
|
-
async (handler) => {
|
|
94
|
-
const handlerSpy = vi.fn(handler);
|
|
95
|
-
|
|
96
|
-
const server = express()
|
|
97
|
-
.use(writeMiddleware(handlerSpy))
|
|
98
|
-
.get('/', (_req, res) => {
|
|
99
|
-
res.end();
|
|
100
|
-
res.status(200).write('This is the response body');
|
|
101
|
-
});
|
|
102
|
-
const response = await request(server).get('/');
|
|
103
|
-
|
|
104
|
-
expect(handlerSpy).not.toHaveBeenCalled();
|
|
105
|
-
expect(response.status).toStrictEqual(200);
|
|
106
|
-
},
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
it.each([doNothing, doNothingAsync])(
|
|
110
|
-
'should return the text result if callback return void',
|
|
111
|
-
async (handler) => {
|
|
112
|
-
const server = express()
|
|
113
|
-
.use(writeMiddleware(handler))
|
|
114
|
-
.get('/', (_req, res) => {
|
|
115
|
-
res.status(200).write(Buffer.from('This is the response body'));
|
|
116
|
-
res.end();
|
|
117
|
-
});
|
|
118
|
-
const response = await request(server).get('/');
|
|
119
|
-
|
|
120
|
-
expect(response.status).toStrictEqual(200);
|
|
121
|
-
expect(response.text).toStrictEqual('This is the response body');
|
|
122
|
-
},
|
|
123
|
-
);
|
|
124
|
-
|
|
125
|
-
it.each([
|
|
126
|
-
[
|
|
127
|
-
appendText,
|
|
128
|
-
appendText2,
|
|
129
|
-
'This is the response body with more content2 with more content',
|
|
130
|
-
],
|
|
131
|
-
[
|
|
132
|
-
appendText,
|
|
133
|
-
appendTextAsync2,
|
|
134
|
-
'This is the response body with more content2 with more content',
|
|
135
|
-
],
|
|
136
|
-
[
|
|
137
|
-
appendTextAsync,
|
|
138
|
-
appendText2,
|
|
139
|
-
'This is the response body with more content2 with more content',
|
|
140
|
-
],
|
|
141
|
-
[
|
|
142
|
-
appendTextAsync,
|
|
143
|
-
appendTextAsync2,
|
|
144
|
-
'This is the response body with more content2 with more content',
|
|
145
|
-
],
|
|
146
|
-
])(
|
|
147
|
-
'should work with multiple middleware',
|
|
148
|
-
async (handler, handler2, expectStr) => {
|
|
149
|
-
const server = express()
|
|
150
|
-
.use(writeMiddleware(handler))
|
|
151
|
-
.use(writeMiddleware(handler2))
|
|
152
|
-
.get('/', (_req, res) => {
|
|
153
|
-
res.status(200).write('This is the response body');
|
|
154
|
-
res.end();
|
|
155
|
-
});
|
|
156
|
-
const response = await request(server).get('/');
|
|
157
|
-
|
|
158
|
-
expect(response.status).toStrictEqual(200);
|
|
159
|
-
expect(response.text).toStrictEqual(expectStr);
|
|
160
|
-
},
|
|
161
|
-
);
|
|
162
|
-
|
|
163
|
-
it.each([inspectJson, inspectJsonAsync])(
|
|
164
|
-
'should return a `body` when the content type is application/json',
|
|
165
|
-
async (handler) => {
|
|
166
|
-
const server = express()
|
|
167
|
-
.use(writeMiddleware(handler))
|
|
168
|
-
.get('/', (_req, res) => {
|
|
169
|
-
res
|
|
170
|
-
.set('Content-Type', 'application/json')
|
|
171
|
-
.status(200)
|
|
172
|
-
.write(
|
|
173
|
-
JSON.stringify({
|
|
174
|
-
a: 'a',
|
|
175
|
-
}),
|
|
176
|
-
);
|
|
177
|
-
res.end();
|
|
178
|
-
});
|
|
179
|
-
const response = await request(server).get('/');
|
|
180
|
-
|
|
181
|
-
expect(response.status).toStrictEqual(200);
|
|
182
|
-
expect(response.body).toStrictEqual({ a: 'a', inspected_by: 'me' });
|
|
183
|
-
},
|
|
184
|
-
);
|
|
185
|
-
|
|
186
|
-
it.each([appendText, appendTextAsync])(
|
|
187
|
-
'should call callback an error response',
|
|
188
|
-
async (handler) => {
|
|
189
|
-
const server = express()
|
|
190
|
-
.use(writeMiddleware(handler))
|
|
191
|
-
.get('/', (_req, res) => {
|
|
192
|
-
res.status(404).write('This is the response body');
|
|
193
|
-
res.end();
|
|
194
|
-
});
|
|
195
|
-
const response = await request(server).get('/');
|
|
196
|
-
|
|
197
|
-
expect(response.status).toStrictEqual(404);
|
|
198
|
-
expect(response.text).toStrictEqual(
|
|
199
|
-
'This is the response body with more content',
|
|
200
|
-
);
|
|
201
|
-
},
|
|
202
|
-
);
|
|
203
|
-
|
|
204
|
-
it.each([error403, error403Async])(
|
|
205
|
-
'should abort if a response is sent',
|
|
206
|
-
async (handler) => {
|
|
207
|
-
const server = express()
|
|
208
|
-
.use(writeMiddleware(handler))
|
|
209
|
-
.get('/', (_req, res) => {
|
|
210
|
-
res
|
|
211
|
-
.set('Content-Type', 'application/json')
|
|
212
|
-
.status(200)
|
|
213
|
-
.write('This is the response body');
|
|
214
|
-
res.end();
|
|
215
|
-
});
|
|
216
|
-
const response = await request(server).get('/');
|
|
217
|
-
|
|
218
|
-
expect(response.status).to.equal(403);
|
|
219
|
-
},
|
|
220
|
-
);
|
|
221
|
-
|
|
222
|
-
it.each([error, errorAsync])(
|
|
223
|
-
'should 500 on a synchronous exception',
|
|
224
|
-
async (handler) => {
|
|
225
|
-
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) => {
|
|
226
|
-
res.status(500).send(err.message).end();
|
|
227
|
-
};
|
|
228
|
-
const server = express()
|
|
229
|
-
.use(errorHandler)
|
|
230
|
-
.use(writeMiddleware(handler))
|
|
231
|
-
.get('/', (_req, res) => {
|
|
232
|
-
res
|
|
233
|
-
.set('Content-Type', 'application/json')
|
|
234
|
-
.status(200)
|
|
235
|
-
.write('This is the response body');
|
|
236
|
-
res.end();
|
|
237
|
-
});
|
|
238
|
-
const response = await request(server).get('/');
|
|
239
|
-
|
|
240
|
-
expect(response.status).toStrictEqual(500);
|
|
241
|
-
},
|
|
242
|
-
);
|
|
243
|
-
|
|
244
|
-
it.each([error, errorAsync])(
|
|
245
|
-
'should 500 on an asynchronous exception',
|
|
246
|
-
async (handler) => {
|
|
247
|
-
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) => {
|
|
248
|
-
res.status(500).send(err.message).end();
|
|
249
|
-
};
|
|
250
|
-
const server = express()
|
|
251
|
-
.use(errorHandler)
|
|
252
|
-
.use(writeMiddleware(handler))
|
|
253
|
-
.get('/', (_req, res) => {
|
|
254
|
-
process.nextTick(() => {
|
|
255
|
-
res
|
|
256
|
-
.set('Content-Type', 'application/json')
|
|
257
|
-
.status(200)
|
|
258
|
-
.write('This is the response body');
|
|
259
|
-
res.end();
|
|
260
|
-
});
|
|
261
|
-
});
|
|
262
|
-
const response = await request(server).get('/');
|
|
263
|
-
|
|
264
|
-
expect(response.status).toStrictEqual(500);
|
|
265
|
-
},
|
|
266
|
-
);
|
|
267
|
-
});
|
package/src/writeMiddleware.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import type { Request, RequestHandler, Response } from 'express';
|
|
2
|
-
import { errorHandler } from './utils/errorHandler.js';
|
|
3
|
-
|
|
4
|
-
export type TransformChunk = (
|
|
5
|
-
chunk: string | Buffer,
|
|
6
|
-
encoding: string | null,
|
|
7
|
-
request: Request,
|
|
8
|
-
response: Response,
|
|
9
|
-
) => void | string | Buffer | Promise<void> | Promise<string> | Promise<Buffer>;
|
|
10
|
-
|
|
11
|
-
export const writeMiddleware =
|
|
12
|
-
(fn: TransformChunk): RequestHandler =>
|
|
13
|
-
(req, res, next) => {
|
|
14
|
-
const originalWrite = res.write;
|
|
15
|
-
const originalEndFn = res.end;
|
|
16
|
-
|
|
17
|
-
res.write = function (this: Response, chunk) {
|
|
18
|
-
if (res.writableEnded) return false;
|
|
19
|
-
|
|
20
|
-
let mayBePromise: ReturnType<TransformChunk>;
|
|
21
|
-
try {
|
|
22
|
-
mayBePromise = fn(
|
|
23
|
-
chunk,
|
|
24
|
-
// Since `encoding` is an optional argument to `res.write`,
|
|
25
|
-
// make sure it is a string and not actually the callback.
|
|
26
|
-
// biome-ignore lint/style/noArguments: pass argument type
|
|
27
|
-
typeof arguments[1] === 'string' ? arguments[1] : null,
|
|
28
|
-
req,
|
|
29
|
-
res,
|
|
30
|
-
);
|
|
31
|
-
} catch (e) {
|
|
32
|
-
errorHandler(e, req, res, next);
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (mayBePromise instanceof Promise) {
|
|
37
|
-
mayBePromise
|
|
38
|
-
.then((result) => {
|
|
39
|
-
res.end = originalEndFn;
|
|
40
|
-
|
|
41
|
-
if (res.writableEnded) return false;
|
|
42
|
-
|
|
43
|
-
// biome-ignore lint/style/noArguments: pass argument type
|
|
44
|
-
arguments[0] = result === undefined ? chunk : result;
|
|
45
|
-
|
|
46
|
-
// biome-ignore lint/style/noArguments: pass argument type
|
|
47
|
-
const writeResponse = originalWrite.apply(res, arguments as any);
|
|
48
|
-
|
|
49
|
-
if (res.__isEnd) res.end();
|
|
50
|
-
|
|
51
|
-
return writeResponse;
|
|
52
|
-
})
|
|
53
|
-
.catch((e) => {
|
|
54
|
-
res.end = originalEndFn;
|
|
55
|
-
errorHandler(e, req, res, next);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
res.end = function (this: Response) {
|
|
59
|
-
this.__isEnd = true;
|
|
60
|
-
return res;
|
|
61
|
-
};
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const result = mayBePromise;
|
|
66
|
-
if (res.writableEnded) return false;
|
|
67
|
-
|
|
68
|
-
// biome-ignore lint/style/noArguments: pass argument type
|
|
69
|
-
arguments[0] = result === undefined ? chunk : result;
|
|
70
|
-
|
|
71
|
-
// biome-ignore lint/style/noArguments: pass argument type
|
|
72
|
-
return originalWrite.apply(res, arguments as any);
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
next();
|
|
76
|
-
};
|
package/tsconfig.json
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"types": ["vitest/globals", "node"],
|
|
4
|
-
|
|
5
|
-
"target": "ESNext",
|
|
6
|
-
"lib": ["ESNext"],
|
|
7
|
-
"module": "NodeNext",
|
|
8
|
-
"moduleResolution": "nodenext",
|
|
9
|
-
|
|
10
|
-
"allowJs": false,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"esModuleInterop": false,
|
|
13
|
-
"resolveJsonModule": true,
|
|
14
|
-
"isolatedModules": true,
|
|
15
|
-
"noEmit": true,
|
|
16
|
-
|
|
17
|
-
"strict": true,
|
|
18
|
-
"allowSyntheticDefaultImports": true,
|
|
19
|
-
"forceConsistentCasingInFileNames": true,
|
|
20
|
-
"allowUnreachableCode": false,
|
|
21
|
-
"allowUnusedLabels": false,
|
|
22
|
-
"noImplicitOverride": true,
|
|
23
|
-
"noImplicitReturns": true,
|
|
24
|
-
"noUnusedLocals": true,
|
|
25
|
-
"noUnusedParameters": true
|
|
26
|
-
},
|
|
27
|
-
"include": ["src"]
|
|
28
|
-
}
|
package/vitest.config.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from 'vitest/config';
|
|
2
|
-
|
|
3
|
-
export default defineConfig({
|
|
4
|
-
test: {
|
|
5
|
-
globals: true,
|
|
6
|
-
environment: 'node',
|
|
7
|
-
coverage: {
|
|
8
|
-
provider: 'v8',
|
|
9
|
-
reporter: ['lcov', 'text'],
|
|
10
|
-
include: ['src'],
|
|
11
|
-
exclude: ['**/tests/**', '**/*.d.ts'],
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
});
|