express-response-middleware 2.1.0 → 2.1.2
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/.commitlintrc.json +3 -0
- package/.github/workflows/release.yml +4 -4
- package/.github/workflows/runTest.yml +11 -8
- package/.husky/commit-msg +1 -0
- package/.husky/pre-commit +1 -0
- package/.husky/pre-push +1 -0
- package/CHANGELOG.md +14 -0
- package/README.md +3 -1
- package/biome.json +36 -0
- package/dist/cjs/index.cjs +17 -20
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +8 -8
- package/dist/esm/index.d.ts +8 -8
- package/dist/esm/index.mjs +17 -20
- package/dist/esm/index.mjs.map +1 -1
- package/package.json +17 -14
- package/pnpm-workspace.yaml +3 -0
- package/renovate.json +4 -1
- package/{rollup.config.mjs → rollup.config.js} +6 -5
- package/src/endMiddleware.ts +31 -31
- package/src/index.ts +5 -6
- package/src/jsonMiddleware.ts +38 -38
- package/src/jsonpMiddleware.ts +39 -39
- package/src/sendMiddleware.ts +25 -25
- package/src/tests/endMiddleware.test.ts +64 -55
- package/src/tests/jsonMiddleware.test.ts +166 -122
- package/src/tests/jsonpMiddleware.test.ts +154 -112
- package/src/tests/mixAndMatch.test.ts +119 -111
- package/src/tests/sendMiddleware.test.ts +144 -112
- package/src/tests/testHelpers/sleep.ts +2 -1
- package/src/tests/writeMiddleware.test.ts +157 -114
- package/src/utils/asyncLocalStorage.ts +2 -2
- package/src/utils/errorHandler.ts +3 -4
- package/src/writeMiddleware.ts +44 -38
- package/tsconfig.json +1 -1
- package/vitest.config.ts +2 -2
- package/.prettierrc.json +0 -8
- package/index.d.ts +0 -69
|
@@ -1,83 +1,113 @@
|
|
|
1
|
-
import express, { ErrorRequestHandler } from 'express'
|
|
2
|
-
import request from 'supertest'
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import express, { type ErrorRequestHandler } from 'express';
|
|
2
|
+
import request from 'supertest';
|
|
3
|
+
import { expect } from 'vitest';
|
|
4
|
+
import { type TransformJsonp, jsonpMiddleware } from '../index.js';
|
|
5
|
+
import { sleep } from './testHelpers/sleep.js';
|
|
5
6
|
|
|
6
7
|
describe('jsonpMiddleware', () => {
|
|
7
|
-
const noop: TransformJsonp = () => {}
|
|
8
|
+
const noop: TransformJsonp = () => {};
|
|
8
9
|
|
|
9
|
-
const noopAsync: TransformJsonp = async json => {
|
|
10
|
-
await sleep()
|
|
11
|
-
return json
|
|
12
|
-
}
|
|
10
|
+
const noopAsync: TransformJsonp = async (json) => {
|
|
11
|
+
await sleep();
|
|
12
|
+
return json;
|
|
13
|
+
};
|
|
13
14
|
|
|
14
|
-
const inspect: TransformJsonp<any> = json => {
|
|
15
|
-
json
|
|
16
|
-
|
|
17
|
-
}
|
|
15
|
+
const inspect: TransformJsonp<any> = (json) => {
|
|
16
|
+
return { ...json, inspected_by: 'me' };
|
|
17
|
+
};
|
|
18
18
|
|
|
19
|
-
const
|
|
20
|
-
json.
|
|
21
|
-
|
|
22
|
-
}
|
|
19
|
+
const inspectMutate: TransformJsonp<any> = (json) => {
|
|
20
|
+
json.inspected_by = 'me';
|
|
21
|
+
};
|
|
23
22
|
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
;
|
|
27
|
-
|
|
23
|
+
const inspect2: TransformJsonp<any> = (json) => {
|
|
24
|
+
json.inspected_by_2 = 'him';
|
|
25
|
+
return json;
|
|
26
|
+
};
|
|
28
27
|
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
const inspectMutateAsync: TransformJsonp = async (json) => {
|
|
29
|
+
await sleep();
|
|
30
|
+
(json as any).inspected_by = 'me';
|
|
31
|
+
await sleep();
|
|
32
|
+
};
|
|
31
33
|
|
|
32
|
-
const
|
|
33
|
-
await sleep()
|
|
34
|
-
|
|
35
|
-
await sleep()
|
|
34
|
+
const inspectAsync: TransformJsonp = async (json) => {
|
|
35
|
+
await sleep();
|
|
36
|
+
const res = { ...(json as object), inspected_by: 'me' };
|
|
37
|
+
await sleep();
|
|
36
38
|
|
|
37
|
-
return
|
|
38
|
-
}
|
|
39
|
+
return res;
|
|
40
|
+
};
|
|
39
41
|
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
const inspectAsync2: TransformJsonp = async (json) => {
|
|
43
|
+
await sleep();
|
|
44
|
+
(json as any).inspected_by_2 = 'him';
|
|
45
|
+
await sleep();
|
|
43
46
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
json.foo.bar.hopefully.fails()
|
|
47
|
-
await sleep()
|
|
47
|
+
return json;
|
|
48
|
+
};
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
const error: TransformJsonp<any> = (json) => {
|
|
51
|
+
json.foo.bar.hopefully.fails();
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const errorAsync: TransformJsonp<any> = async (json) => {
|
|
55
|
+
await sleep();
|
|
56
|
+
json.foo.bar.hopefully.fails();
|
|
57
|
+
await sleep();
|
|
58
|
+
|
|
59
|
+
return json;
|
|
60
|
+
};
|
|
51
61
|
|
|
52
62
|
const noPermission: TransformJsonp = (_json, _req, res) => {
|
|
53
|
-
res.status(403).send('no permissions')
|
|
54
|
-
}
|
|
63
|
+
res.status(403).send('no permissions');
|
|
64
|
+
};
|
|
55
65
|
|
|
56
66
|
const noPermissionAsync: TransformJsonp = async (json, _req, res) => {
|
|
57
|
-
await sleep()
|
|
58
|
-
res.status(403).send('no permissions')
|
|
59
|
-
await sleep()
|
|
67
|
+
await sleep();
|
|
68
|
+
res.status(403).send('no permissions');
|
|
69
|
+
await sleep();
|
|
60
70
|
|
|
61
|
-
return Promise.resolve(json)
|
|
62
|
-
}
|
|
71
|
+
return Promise.resolve(json);
|
|
72
|
+
};
|
|
63
73
|
|
|
64
|
-
it.each([inspect, inspectAsync])(
|
|
74
|
+
it.each([inspect, inspectAsync, inspectMutate, inspectMutateAsync])(
|
|
65
75
|
'should return the JSON result',
|
|
66
|
-
async handler => {
|
|
76
|
+
async (handler) => {
|
|
67
77
|
const server = express()
|
|
68
78
|
.use(jsonpMiddleware(handler))
|
|
69
|
-
.get('/', (_req, res) =>
|
|
70
|
-
|
|
79
|
+
.get('/', (_req, res) => {
|
|
80
|
+
res.status(200).jsonp({ a: 'a' }).end();
|
|
81
|
+
});
|
|
82
|
+
const response = await request(server).get('/');
|
|
71
83
|
|
|
72
|
-
const expected = { a: 'a', inspected_by: 'me' }
|
|
84
|
+
const expected = { a: 'a', inspected_by: 'me' };
|
|
73
85
|
|
|
74
|
-
expect(response.status).toStrictEqual(200)
|
|
75
|
-
expect(response.body).toStrictEqual(expected)
|
|
86
|
+
expect(response.status).toStrictEqual(200);
|
|
87
|
+
expect(response.body).toStrictEqual(expected);
|
|
76
88
|
expect(response.headers['content-length']).toStrictEqual(
|
|
77
|
-
JSON.stringify(expected).length.toString()
|
|
78
|
-
)
|
|
79
|
-
}
|
|
80
|
-
)
|
|
89
|
+
JSON.stringify(expected).length.toString(),
|
|
90
|
+
);
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
it.each([inspect, inspectAsync])(
|
|
95
|
+
'should not call if header is already sent',
|
|
96
|
+
async (handler) => {
|
|
97
|
+
const handlerSpy = vi.fn(handler);
|
|
98
|
+
|
|
99
|
+
const server = express()
|
|
100
|
+
.use(jsonpMiddleware(handlerSpy))
|
|
101
|
+
.get('/', (_req, res) => {
|
|
102
|
+
res.end();
|
|
103
|
+
res.status(200).jsonp({ a: 'a' }).end();
|
|
104
|
+
});
|
|
105
|
+
const response = await request(server).get('/');
|
|
106
|
+
|
|
107
|
+
expect(handlerSpy).not.toHaveBeenCalled();
|
|
108
|
+
expect(response.status).toStrictEqual(200);
|
|
109
|
+
},
|
|
110
|
+
);
|
|
81
111
|
|
|
82
112
|
it.each([
|
|
83
113
|
[inspect, inspect2],
|
|
@@ -88,104 +118,116 @@ describe('jsonpMiddleware', () => {
|
|
|
88
118
|
const server = express()
|
|
89
119
|
.use(jsonpMiddleware(handler))
|
|
90
120
|
.use(jsonpMiddleware(handler2))
|
|
91
|
-
.get('/', (_req, res) =>
|
|
92
|
-
|
|
121
|
+
.get('/', (_req, res) => {
|
|
122
|
+
res.status(200).jsonp({ a: 'a' }).end();
|
|
123
|
+
});
|
|
124
|
+
const response = await request(server).get('/');
|
|
93
125
|
|
|
94
|
-
const expected = { a: 'a', inspected_by: 'me', inspected_by_2: 'him' }
|
|
126
|
+
const expected = { a: 'a', inspected_by: 'me', inspected_by_2: 'him' };
|
|
95
127
|
|
|
96
|
-
expect(response.status).toStrictEqual(200)
|
|
97
|
-
expect(response.body).toStrictEqual(expected)
|
|
128
|
+
expect(response.status).toStrictEqual(200);
|
|
129
|
+
expect(response.body).toStrictEqual(expected);
|
|
98
130
|
expect(response.headers['content-length']).toStrictEqual(
|
|
99
|
-
JSON.stringify(expected).length.toString()
|
|
100
|
-
)
|
|
101
|
-
})
|
|
131
|
+
JSON.stringify(expected).length.toString(),
|
|
132
|
+
);
|
|
133
|
+
});
|
|
102
134
|
|
|
103
135
|
it.each([inspect, inspectAsync])(
|
|
104
136
|
'should call callback with an error',
|
|
105
|
-
async handler => {
|
|
137
|
+
async (handler) => {
|
|
106
138
|
const server = express()
|
|
107
139
|
.use(jsonpMiddleware(handler))
|
|
108
|
-
.get('/', (_req, res) =>
|
|
109
|
-
|
|
140
|
+
.get('/', (_req, res) => {
|
|
141
|
+
res.status(404).jsonp({ a: 'a' }).end();
|
|
142
|
+
});
|
|
143
|
+
const response = await request(server).get('/');
|
|
110
144
|
|
|
111
|
-
const expected = { a: 'a', inspected_by: 'me' }
|
|
145
|
+
const expected = { a: 'a', inspected_by: 'me' };
|
|
112
146
|
|
|
113
|
-
expect(response.status).toStrictEqual(404)
|
|
114
|
-
expect(response.body).toStrictEqual(expected)
|
|
147
|
+
expect(response.status).toStrictEqual(404);
|
|
148
|
+
expect(response.body).toStrictEqual(expected);
|
|
115
149
|
expect(response.headers['content-length']).toStrictEqual(
|
|
116
|
-
JSON.stringify(expected).length.toString()
|
|
117
|
-
)
|
|
118
|
-
}
|
|
119
|
-
)
|
|
150
|
+
JSON.stringify(expected).length.toString(),
|
|
151
|
+
);
|
|
152
|
+
},
|
|
153
|
+
);
|
|
120
154
|
|
|
121
155
|
it.each([noop, noopAsync])(
|
|
122
156
|
'should return a as application/json',
|
|
123
|
-
async handler => {
|
|
157
|
+
async (handler) => {
|
|
124
158
|
const server = express()
|
|
125
159
|
.use(jsonpMiddleware(handler))
|
|
126
|
-
.get('/', (_req, res) =>
|
|
127
|
-
|
|
160
|
+
.get('/', (_req, res) => {
|
|
161
|
+
res.status(200).jsonp(42).end();
|
|
162
|
+
});
|
|
163
|
+
const response = await request(server).get('/');
|
|
128
164
|
|
|
129
|
-
expect(response.status).toStrictEqual(200)
|
|
130
|
-
expect(response.text).toStrictEqual('42')
|
|
165
|
+
expect(response.status).toStrictEqual(200);
|
|
166
|
+
expect(response.text).toStrictEqual('42');
|
|
131
167
|
expect(response.headers).toStrictEqual(
|
|
132
168
|
expect.objectContaining({
|
|
133
169
|
'content-type': 'application/json; charset=utf-8',
|
|
134
|
-
})
|
|
135
|
-
)
|
|
136
|
-
}
|
|
137
|
-
)
|
|
170
|
+
}),
|
|
171
|
+
);
|
|
172
|
+
},
|
|
173
|
+
);
|
|
138
174
|
|
|
139
175
|
it.each([noPermission, noPermissionAsync])(
|
|
140
176
|
'should abort if a response is sent',
|
|
141
|
-
async handler => {
|
|
177
|
+
async (handler) => {
|
|
142
178
|
const server = express()
|
|
143
179
|
.use(jsonpMiddleware(handler))
|
|
144
|
-
.get('/', (_req, res) =>
|
|
145
|
-
|
|
180
|
+
.get('/', (_req, res) => {
|
|
181
|
+
res.status(200).jsonp({ a: 'a' }).end();
|
|
182
|
+
});
|
|
183
|
+
const response = await request(server).get('/');
|
|
146
184
|
|
|
147
|
-
expect(response.status).toStrictEqual(403)
|
|
148
|
-
expect(response.text).toStrictEqual('no permissions')
|
|
185
|
+
expect(response.status).toStrictEqual(403);
|
|
186
|
+
expect(response.text).toStrictEqual('no permissions');
|
|
149
187
|
expect(response.headers).toStrictEqual(
|
|
150
188
|
expect.objectContaining({
|
|
151
189
|
'content-type': 'text/html; charset=utf-8',
|
|
152
|
-
})
|
|
153
|
-
)
|
|
154
|
-
}
|
|
155
|
-
)
|
|
190
|
+
}),
|
|
191
|
+
);
|
|
192
|
+
},
|
|
193
|
+
);
|
|
156
194
|
|
|
157
195
|
it.each([error, errorAsync])(
|
|
158
196
|
'should 500 on a synchronous exception',
|
|
159
|
-
async handler => {
|
|
160
|
-
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) =>
|
|
161
|
-
res.status(500).send(err.message).end()
|
|
197
|
+
async (handler) => {
|
|
198
|
+
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) => {
|
|
199
|
+
res.status(500).send(err.message).end();
|
|
200
|
+
};
|
|
162
201
|
const server = express()
|
|
163
202
|
.use(errorHandler)
|
|
164
203
|
.use(jsonpMiddleware(handler))
|
|
165
|
-
.get('/', (_req, res) =>
|
|
204
|
+
.get('/', (_req, res) => {
|
|
205
|
+
res.status(200).jsonp({ a: 'a' }).end();
|
|
206
|
+
});
|
|
166
207
|
|
|
167
|
-
const response = await request(server).get('/')
|
|
208
|
+
const response = await request(server).get('/');
|
|
168
209
|
|
|
169
|
-
expect(response.status).toStrictEqual(500)
|
|
170
|
-
}
|
|
171
|
-
)
|
|
210
|
+
expect(response.status).toStrictEqual(500);
|
|
211
|
+
},
|
|
212
|
+
);
|
|
172
213
|
|
|
173
214
|
it.each([error, errorAsync])(
|
|
174
215
|
'should 500 on an asynchronous exception',
|
|
175
|
-
async handler => {
|
|
176
|
-
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) =>
|
|
177
|
-
res.status(500).send(err.message).end()
|
|
216
|
+
async (handler) => {
|
|
217
|
+
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) => {
|
|
218
|
+
res.status(500).send(err.message).end();
|
|
219
|
+
};
|
|
178
220
|
const server = express()
|
|
179
221
|
.use(errorHandler)
|
|
180
222
|
.use(jsonpMiddleware(handler))
|
|
181
223
|
.get('/', (_req, res) => {
|
|
182
224
|
process.nextTick(() => {
|
|
183
|
-
res.status(200).jsonp({ a: 'a' }).end()
|
|
184
|
-
})
|
|
185
|
-
})
|
|
186
|
-
const response = await request(server).get('/')
|
|
187
|
-
|
|
188
|
-
expect(response.status).toStrictEqual(500)
|
|
189
|
-
}
|
|
190
|
-
)
|
|
191
|
-
})
|
|
225
|
+
res.status(200).jsonp({ a: 'a' }).end();
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
const response = await request(server).get('/');
|
|
229
|
+
|
|
230
|
+
expect(response.status).toStrictEqual(500);
|
|
231
|
+
},
|
|
232
|
+
);
|
|
233
|
+
});
|