express-response-middleware 1.0.0 → 2.0.1
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/.github/workflows/release.yml +17 -3
- package/CHANGELOG.md +50 -0
- package/README.md +121 -57
- package/dist/cjs/index.cjs +199 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.ts +24 -0
- package/dist/esm/index.d.ts +24 -0
- package/dist/esm/index.mjs +194 -0
- package/dist/esm/index.mjs.map +1 -0
- package/docs/MIGRATE_FROM_MUNG.md +153 -0
- package/docs/V1_README.md +115 -0
- package/package.json +26 -14
- package/renovate.json +6 -0
- package/rollup.config.mjs +49 -0
- package/src/endMiddleware.ts +61 -0
- package/src/index.ts +13 -0
- package/src/jsonMiddleware.ts +71 -0
- package/src/jsonpMiddleware.ts +73 -0
- package/src/sendMiddleware.ts +50 -0
- package/src/tests/endMiddleware.test.ts +92 -0
- package/src/tests/jsonMiddleware.test.ts +209 -0
- package/src/tests/jsonpMiddleware.test.ts +191 -0
- package/src/tests/mixAndMatch.test.ts +253 -0
- package/src/tests/sendMiddleware.test.ts +187 -0
- package/src/tests/testHelpers/sleep.ts +1 -0
- package/src/tests/writeMiddleware.test.ts +224 -0
- package/src/utils/asyncLocalStorage.ts +3 -0
- package/src/utils/errorHandler.ts +10 -0
- package/src/writeMiddleware.ts +70 -0
- package/tsconfig.json +28 -0
- package/vitest.config.ts +14 -0
- package/index.js +0 -203
- package/test/headers.js +0 -66
- package/test/json.js +0 -192
- package/test/jsonAsync.js +0 -170
- package/test/write.js +0 -160
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Request, RequestHandler, Response } from 'express'
|
|
2
|
+
import { errorHandler } from './utils/errorHandler'
|
|
3
|
+
import { isInSend } from './utils/asyncLocalStorage'
|
|
4
|
+
|
|
5
|
+
export type TransformJson<T = {}> = (
|
|
6
|
+
body: T,
|
|
7
|
+
request: Request,
|
|
8
|
+
response: Response
|
|
9
|
+
) => unknown | Promise<unknown>
|
|
10
|
+
|
|
11
|
+
export const jsonMiddleware =
|
|
12
|
+
(fn: TransformJson): RequestHandler =>
|
|
13
|
+
(req, res, next) => {
|
|
14
|
+
const originalJsonFn = res.json
|
|
15
|
+
const originalSendFn = res.send
|
|
16
|
+
|
|
17
|
+
res.send = function (this: Response) {
|
|
18
|
+
return isInSend.run(true, () =>
|
|
19
|
+
originalSendFn.apply(this, arguments as any)
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
res.json = function (this: Response, json) {
|
|
24
|
+
const originalEndFn = res.end
|
|
25
|
+
|
|
26
|
+
if (res.headersSent) return res
|
|
27
|
+
|
|
28
|
+
let mayBePromise
|
|
29
|
+
try {
|
|
30
|
+
mayBePromise = fn(json, req, res)
|
|
31
|
+
} catch (e) {
|
|
32
|
+
res.json = originalJsonFn
|
|
33
|
+
errorHandler(e, req, res, next)
|
|
34
|
+
return res
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (mayBePromise instanceof Promise) {
|
|
38
|
+
mayBePromise
|
|
39
|
+
.then(result => {
|
|
40
|
+
if (res.headersSent) return
|
|
41
|
+
res.end = originalEndFn
|
|
42
|
+
|
|
43
|
+
originalJsonFn.call(this, result === undefined ? json : result)
|
|
44
|
+
})
|
|
45
|
+
.catch(e => {
|
|
46
|
+
res.json = originalJsonFn
|
|
47
|
+
res.end = originalEndFn
|
|
48
|
+
errorHandler(e, req, res, next)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
// Prevent end being called wile promise still running
|
|
52
|
+
res.end = function (this: Response) {
|
|
53
|
+
// Res.send will call Res.end, when send is call inside the middleware, do it actually
|
|
54
|
+
if (isInSend.getStore()) {
|
|
55
|
+
originalEndFn.apply(this, arguments as any)
|
|
56
|
+
}
|
|
57
|
+
return res
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
const result = mayBePromise
|
|
61
|
+
|
|
62
|
+
if (res.headersSent) return res
|
|
63
|
+
|
|
64
|
+
originalJsonFn.call(this, result === undefined ? json : result)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return res
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
next()
|
|
71
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Request, RequestHandler, Response } from 'express'
|
|
2
|
+
import { errorHandler } from './utils/errorHandler'
|
|
3
|
+
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
4
|
+
|
|
5
|
+
export type TransformJsonp<T = {}> = (
|
|
6
|
+
body: T,
|
|
7
|
+
request: Request,
|
|
8
|
+
response: Response
|
|
9
|
+
) => unknown | Promise<unknown>
|
|
10
|
+
|
|
11
|
+
const isInSend = new AsyncLocalStorage<boolean>()
|
|
12
|
+
|
|
13
|
+
export const jsonpMiddleware =
|
|
14
|
+
(fn: TransformJsonp): RequestHandler =>
|
|
15
|
+
(req, res, next) => {
|
|
16
|
+
const originalJsonpFn = res.jsonp
|
|
17
|
+
const originalSendFn = res.send
|
|
18
|
+
|
|
19
|
+
res.send = function (this: Response) {
|
|
20
|
+
return isInSend.run(true, () =>
|
|
21
|
+
originalSendFn.apply(this, arguments as any)
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
res.jsonp = function (this: Response, json) {
|
|
26
|
+
const originalEndFn = res.end
|
|
27
|
+
|
|
28
|
+
if (res.headersSent) return res
|
|
29
|
+
|
|
30
|
+
let mayBePromise
|
|
31
|
+
try {
|
|
32
|
+
mayBePromise = fn(json, req, res)
|
|
33
|
+
} catch (e) {
|
|
34
|
+
res.json = originalJsonpFn
|
|
35
|
+
errorHandler(e, req, res, next)
|
|
36
|
+
return res
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (mayBePromise instanceof Promise) {
|
|
40
|
+
mayBePromise
|
|
41
|
+
.then(result => {
|
|
42
|
+
if (res.headersSent) return
|
|
43
|
+
res.end = originalEndFn
|
|
44
|
+
|
|
45
|
+
originalJsonpFn.call(this, result === undefined ? json : result)
|
|
46
|
+
})
|
|
47
|
+
.catch(e => {
|
|
48
|
+
res.jsonp = originalJsonpFn
|
|
49
|
+
res.end = originalEndFn
|
|
50
|
+
errorHandler(e, req, res, next)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
// Prevent end being called wile promise still running
|
|
54
|
+
res.end = function (this: Response) {
|
|
55
|
+
// Res.send will call Res.end, when send is call inside the middleware, do it actually
|
|
56
|
+
if (isInSend.getStore()) {
|
|
57
|
+
originalEndFn.apply(this, arguments as any)
|
|
58
|
+
}
|
|
59
|
+
return res
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
const result = mayBePromise
|
|
63
|
+
|
|
64
|
+
if (res.headersSent) return res
|
|
65
|
+
|
|
66
|
+
originalJsonpFn.call(this, result === undefined ? json : result)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return res
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
next()
|
|
73
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Request, RequestHandler, Response } from 'express'
|
|
2
|
+
import { errorHandler } from './utils/errorHandler'
|
|
3
|
+
|
|
4
|
+
export type TransformSend<T = unknown> = (
|
|
5
|
+
body: T,
|
|
6
|
+
request: Request,
|
|
7
|
+
response: Response
|
|
8
|
+
) => unknown | Promise<unknown>
|
|
9
|
+
|
|
10
|
+
export const sendMiddleware =
|
|
11
|
+
(fn: TransformSend): RequestHandler =>
|
|
12
|
+
(req, res, next) => {
|
|
13
|
+
const originalSendFn = res.send
|
|
14
|
+
|
|
15
|
+
res.send = function (this: Response, payload) {
|
|
16
|
+
if (res.headersSent) return res
|
|
17
|
+
|
|
18
|
+
let mayBePromise
|
|
19
|
+
try {
|
|
20
|
+
mayBePromise = fn(payload, req, res)
|
|
21
|
+
} catch (e) {
|
|
22
|
+
res.send = originalSendFn
|
|
23
|
+
errorHandler(e, req, res, next)
|
|
24
|
+
return res
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (mayBePromise instanceof Promise) {
|
|
28
|
+
mayBePromise
|
|
29
|
+
.then(result => {
|
|
30
|
+
if (res.headersSent) return
|
|
31
|
+
|
|
32
|
+
originalSendFn.call(this, result === undefined ? payload : result)
|
|
33
|
+
})
|
|
34
|
+
.catch(e => {
|
|
35
|
+
res.send = originalSendFn
|
|
36
|
+
errorHandler(e, req, res, next)
|
|
37
|
+
})
|
|
38
|
+
} else {
|
|
39
|
+
const result = mayBePromise
|
|
40
|
+
|
|
41
|
+
if (res.headersSent) return res
|
|
42
|
+
|
|
43
|
+
originalSendFn.call(this, result === undefined ? payload : result)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return res
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
next()
|
|
50
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import express from 'express'
|
|
2
|
+
import request from 'supertest'
|
|
3
|
+
import { expect } from 'vitest'
|
|
4
|
+
import { endMiddleware, TransformEnd } from '../index'
|
|
5
|
+
import { sleep } from './testHelpers/sleep'
|
|
6
|
+
|
|
7
|
+
describe('endMiddleware', () => {
|
|
8
|
+
const header: TransformEnd = (_req, res) => {
|
|
9
|
+
res.set('x-inspected-by', 'me')
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const headerAsync: TransformEnd = async (_req, res) => {
|
|
13
|
+
await sleep()
|
|
14
|
+
res.set('x-inspected-by', 'me')
|
|
15
|
+
await sleep()
|
|
16
|
+
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const header2: TransformEnd = (_req, res) => {
|
|
21
|
+
res.set('x-inspected-by-2', 'him')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const headerAsync2: TransformEnd = async (_req, res) => {
|
|
25
|
+
await sleep()
|
|
26
|
+
res.set('x-inspected-by-2', 'him')
|
|
27
|
+
await sleep()
|
|
28
|
+
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const error: TransformEnd = req => {
|
|
33
|
+
;(req as any).hopefully_fails()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const errorAsync: TransformEnd = async req => {
|
|
37
|
+
await sleep()
|
|
38
|
+
;(req as any).hopefully_fails()
|
|
39
|
+
await sleep()
|
|
40
|
+
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
it.each([header, headerAsync])('should return the headers', async handler => {
|
|
45
|
+
const server = express()
|
|
46
|
+
.use(endMiddleware(handler))
|
|
47
|
+
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
48
|
+
const response = await request(server).get('/')
|
|
49
|
+
|
|
50
|
+
expect(response.status).toStrictEqual(200)
|
|
51
|
+
expect(response.body).toStrictEqual({ a: 'a' })
|
|
52
|
+
expect(response.headers).toStrictEqual(
|
|
53
|
+
expect.objectContaining({
|
|
54
|
+
'x-inspected-by': 'me',
|
|
55
|
+
})
|
|
56
|
+
)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it.each([
|
|
60
|
+
[header, header2],
|
|
61
|
+
[header, headerAsync2],
|
|
62
|
+
[headerAsync, header2],
|
|
63
|
+
[headerAsync, headerAsync2],
|
|
64
|
+
])('should work with multiple middleware', async (handler, handler2) => {
|
|
65
|
+
const server = express()
|
|
66
|
+
.use(endMiddleware(handler))
|
|
67
|
+
.use(endMiddleware(handler2))
|
|
68
|
+
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
69
|
+
const response = await request(server).get('/')
|
|
70
|
+
|
|
71
|
+
expect(response.status).toStrictEqual(200)
|
|
72
|
+
expect(response.body).toStrictEqual({ a: 'a' })
|
|
73
|
+
expect(response.headers).toStrictEqual(
|
|
74
|
+
expect.objectContaining({
|
|
75
|
+
'x-inspected-by': 'me',
|
|
76
|
+
'x-inspected-by-2': 'him',
|
|
77
|
+
})
|
|
78
|
+
)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it.each([error, errorAsync])(
|
|
82
|
+
'should 500 on a synchronous exception',
|
|
83
|
+
async handler => {
|
|
84
|
+
const server = express()
|
|
85
|
+
.use(endMiddleware(handler))
|
|
86
|
+
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
87
|
+
const response = await request(server).get('/')
|
|
88
|
+
|
|
89
|
+
expect(response.status).toStrictEqual(500)
|
|
90
|
+
}
|
|
91
|
+
)
|
|
92
|
+
})
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import express, { ErrorRequestHandler } from 'express'
|
|
2
|
+
import request from 'supertest'
|
|
3
|
+
import { jsonMiddleware, TransformJson } from '../index'
|
|
4
|
+
import { sleep } from './testHelpers/sleep'
|
|
5
|
+
|
|
6
|
+
describe('jsonMiddleware', () => {
|
|
7
|
+
const noop: TransformJson = () => {}
|
|
8
|
+
|
|
9
|
+
const noopAsync: TransformJson = async json => {
|
|
10
|
+
await sleep()
|
|
11
|
+
return json
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const inspect: TransformJson<any> = json => {
|
|
15
|
+
json.inspected_by = 'me'
|
|
16
|
+
return json
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const inspect2: TransformJson<any> = json => {
|
|
20
|
+
json.inspected_by_2 = 'him'
|
|
21
|
+
return json
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const inspectAsync: TransformJson = async json => {
|
|
25
|
+
await sleep()
|
|
26
|
+
;(json as any).inspected_by = 'me'
|
|
27
|
+
await sleep()
|
|
28
|
+
|
|
29
|
+
return json
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const inspectAsync2: TransformJson = async json => {
|
|
33
|
+
await sleep()
|
|
34
|
+
;(json as any).inspected_by_2 = 'him'
|
|
35
|
+
await sleep()
|
|
36
|
+
|
|
37
|
+
return json
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const error: TransformJson<any> = json => {
|
|
41
|
+
json.foo.bar.hopefully.fails()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const errorAsync: TransformJson<any> = async json => {
|
|
45
|
+
await sleep()
|
|
46
|
+
json.foo.bar.hopefully.fails()
|
|
47
|
+
await sleep()
|
|
48
|
+
|
|
49
|
+
return json
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const noPermission: TransformJson = (_json, _req, res) => {
|
|
53
|
+
res.status(403).send('no permissions')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const noPermissionAsync: TransformJson = async (json, _req, res) => {
|
|
57
|
+
await sleep()
|
|
58
|
+
res.status(403).send('no permissions')
|
|
59
|
+
await sleep()
|
|
60
|
+
|
|
61
|
+
return Promise.resolve(json)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
it.each([inspect, inspectAsync])(
|
|
65
|
+
'should return the JSON result',
|
|
66
|
+
async handler => {
|
|
67
|
+
const server = express()
|
|
68
|
+
.use(jsonMiddleware(handler))
|
|
69
|
+
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
70
|
+
const response = await request(server).get('/')
|
|
71
|
+
|
|
72
|
+
const expected = { a: 'a', inspected_by: 'me' }
|
|
73
|
+
|
|
74
|
+
expect(response.status).toStrictEqual(200)
|
|
75
|
+
expect(response.body).toStrictEqual(expected)
|
|
76
|
+
expect(response.headers['content-length']).toStrictEqual(
|
|
77
|
+
JSON.stringify(expected).length.toString()
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
it.each([
|
|
83
|
+
[inspect, inspect2],
|
|
84
|
+
[inspect, inspectAsync2],
|
|
85
|
+
[inspectAsync, inspect2],
|
|
86
|
+
[inspectAsync, inspectAsync2],
|
|
87
|
+
])('should work with multiple middleware', async (handler, handler2) => {
|
|
88
|
+
const server = express()
|
|
89
|
+
.use(jsonMiddleware(handler))
|
|
90
|
+
.use(jsonMiddleware(handler2))
|
|
91
|
+
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
92
|
+
const response = await request(server).get('/')
|
|
93
|
+
|
|
94
|
+
const expected = { a: 'a', inspected_by: 'me', inspected_by_2: 'him' }
|
|
95
|
+
|
|
96
|
+
expect(response.status).toStrictEqual(200)
|
|
97
|
+
expect(response.body).toStrictEqual(expected)
|
|
98
|
+
expect(response.headers['content-length']).toStrictEqual(
|
|
99
|
+
JSON.stringify(expected).length.toString()
|
|
100
|
+
)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it.each([inspect, inspectAsync])(
|
|
104
|
+
'should call callback with an error',
|
|
105
|
+
async handler => {
|
|
106
|
+
const server = express()
|
|
107
|
+
.use(jsonMiddleware(handler))
|
|
108
|
+
.get('/', (_req, res) => res.status(404).json({ a: 'a' }).end())
|
|
109
|
+
const response = await request(server).get('/')
|
|
110
|
+
|
|
111
|
+
const expected = { a: 'a', inspected_by: 'me' }
|
|
112
|
+
|
|
113
|
+
expect(response.status).toStrictEqual(404)
|
|
114
|
+
expect(response.body).toStrictEqual(expected)
|
|
115
|
+
expect(response.headers['content-length']).toStrictEqual(
|
|
116
|
+
JSON.stringify(expected).length.toString()
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
it.each([inspect, inspectAsync])(
|
|
122
|
+
'should return the JSON result from a res.send',
|
|
123
|
+
async handler => {
|
|
124
|
+
const server = express()
|
|
125
|
+
.use(jsonMiddleware(handler))
|
|
126
|
+
.get('/', (_req, res) => res.status(200).send({ a: 'a' }).end())
|
|
127
|
+
const response = await request(server).get('/')
|
|
128
|
+
|
|
129
|
+
let expected = { a: 'a', inspected_by: 'me' }
|
|
130
|
+
|
|
131
|
+
expect(response.status).toStrictEqual(200)
|
|
132
|
+
expect(response.body).toStrictEqual(expected)
|
|
133
|
+
expect(response.headers['content-length']).toStrictEqual(
|
|
134
|
+
JSON.stringify(expected).length.toString()
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
it.each([noop, noopAsync])(
|
|
140
|
+
'should return a as application/json',
|
|
141
|
+
async handler => {
|
|
142
|
+
const server = express()
|
|
143
|
+
.use(jsonMiddleware(handler))
|
|
144
|
+
.get('/', (_req, res) => res.status(200).json(42).end())
|
|
145
|
+
const response = await request(server).get('/')
|
|
146
|
+
|
|
147
|
+
expect(response.status).toStrictEqual(200)
|
|
148
|
+
expect(response.text).toStrictEqual('42')
|
|
149
|
+
expect(response.headers).toStrictEqual(
|
|
150
|
+
expect.objectContaining({
|
|
151
|
+
'content-type': 'application/json; charset=utf-8',
|
|
152
|
+
})
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
it.each([noPermission, noPermissionAsync])(
|
|
158
|
+
'should abort if a response is sent',
|
|
159
|
+
async handler => {
|
|
160
|
+
const server = express()
|
|
161
|
+
.use(jsonMiddleware(handler))
|
|
162
|
+
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
163
|
+
const response = await request(server).get('/')
|
|
164
|
+
|
|
165
|
+
expect(response.status).toStrictEqual(403)
|
|
166
|
+
expect(response.text).toStrictEqual('no permissions')
|
|
167
|
+
expect(response.headers).toStrictEqual(
|
|
168
|
+
expect.objectContaining({
|
|
169
|
+
'content-type': 'text/html; charset=utf-8',
|
|
170
|
+
})
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
it.each([error, errorAsync])(
|
|
176
|
+
'should 500 on a synchronous exception',
|
|
177
|
+
async handler => {
|
|
178
|
+
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) =>
|
|
179
|
+
res.status(500).send(err.message).end()
|
|
180
|
+
const server = express()
|
|
181
|
+
.use(errorHandler)
|
|
182
|
+
.use(jsonMiddleware(handler))
|
|
183
|
+
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
184
|
+
|
|
185
|
+
const response = await request(server).get('/')
|
|
186
|
+
|
|
187
|
+
expect(response.status).toStrictEqual(500)
|
|
188
|
+
}
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
it.each([error, errorAsync])(
|
|
192
|
+
'should 500 on an asynchronous exception',
|
|
193
|
+
async handler => {
|
|
194
|
+
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) =>
|
|
195
|
+
res.status(500).send(err.message).end()
|
|
196
|
+
const server = express()
|
|
197
|
+
.use(errorHandler)
|
|
198
|
+
.use(jsonMiddleware(handler))
|
|
199
|
+
.get('/', (_req, res) => {
|
|
200
|
+
process.nextTick(() => {
|
|
201
|
+
res.status(200).json({ a: 'a' }).end()
|
|
202
|
+
})
|
|
203
|
+
})
|
|
204
|
+
const response = await request(server).get('/')
|
|
205
|
+
|
|
206
|
+
expect(response.status).toStrictEqual(500)
|
|
207
|
+
}
|
|
208
|
+
)
|
|
209
|
+
})
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import express, { ErrorRequestHandler } from 'express'
|
|
2
|
+
import request from 'supertest'
|
|
3
|
+
import { jsonpMiddleware, TransformJsonp } from '../index'
|
|
4
|
+
import { sleep } from './testHelpers/sleep'
|
|
5
|
+
|
|
6
|
+
describe('jsonpMiddleware', () => {
|
|
7
|
+
const noop: TransformJsonp = () => {}
|
|
8
|
+
|
|
9
|
+
const noopAsync: TransformJsonp = async json => {
|
|
10
|
+
await sleep()
|
|
11
|
+
return json
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const inspect: TransformJsonp<any> = json => {
|
|
15
|
+
json.inspected_by = 'me'
|
|
16
|
+
return json
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const inspect2: TransformJsonp<any> = json => {
|
|
20
|
+
json.inspected_by_2 = 'him'
|
|
21
|
+
return json
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const inspectAsync: TransformJsonp = async json => {
|
|
25
|
+
await sleep()
|
|
26
|
+
;(json as any).inspected_by = 'me'
|
|
27
|
+
await sleep()
|
|
28
|
+
|
|
29
|
+
return json
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const inspectAsync2: TransformJsonp = async json => {
|
|
33
|
+
await sleep()
|
|
34
|
+
;(json as any).inspected_by_2 = 'him'
|
|
35
|
+
await sleep()
|
|
36
|
+
|
|
37
|
+
return json
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const error: TransformJsonp<any> = json => {
|
|
41
|
+
json.foo.bar.hopefully.fails()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const errorAsync: TransformJsonp<any> = async json => {
|
|
45
|
+
await sleep()
|
|
46
|
+
json.foo.bar.hopefully.fails()
|
|
47
|
+
await sleep()
|
|
48
|
+
|
|
49
|
+
return json
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const noPermission: TransformJsonp = (_json, _req, res) => {
|
|
53
|
+
res.status(403).send('no permissions')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const noPermissionAsync: TransformJsonp = async (json, _req, res) => {
|
|
57
|
+
await sleep()
|
|
58
|
+
res.status(403).send('no permissions')
|
|
59
|
+
await sleep()
|
|
60
|
+
|
|
61
|
+
return Promise.resolve(json)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
it.each([inspect, inspectAsync])(
|
|
65
|
+
'should return the JSON result',
|
|
66
|
+
async handler => {
|
|
67
|
+
const server = express()
|
|
68
|
+
.use(jsonpMiddleware(handler))
|
|
69
|
+
.get('/', (_req, res) => res.status(200).jsonp({ a: 'a' }).end())
|
|
70
|
+
const response = await request(server).get('/')
|
|
71
|
+
|
|
72
|
+
const expected = { a: 'a', inspected_by: 'me' }
|
|
73
|
+
|
|
74
|
+
expect(response.status).toStrictEqual(200)
|
|
75
|
+
expect(response.body).toStrictEqual(expected)
|
|
76
|
+
expect(response.headers['content-length']).toStrictEqual(
|
|
77
|
+
JSON.stringify(expected).length.toString()
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
it.each([
|
|
83
|
+
[inspect, inspect2],
|
|
84
|
+
[inspect, inspectAsync2],
|
|
85
|
+
[inspectAsync, inspect2],
|
|
86
|
+
[inspectAsync, inspectAsync2],
|
|
87
|
+
])('should work with multiple middleware', async (handler, handler2) => {
|
|
88
|
+
const server = express()
|
|
89
|
+
.use(jsonpMiddleware(handler))
|
|
90
|
+
.use(jsonpMiddleware(handler2))
|
|
91
|
+
.get('/', (_req, res) => res.status(200).jsonp({ a: 'a' }).end())
|
|
92
|
+
const response = await request(server).get('/')
|
|
93
|
+
|
|
94
|
+
const expected = { a: 'a', inspected_by: 'me', inspected_by_2: 'him' }
|
|
95
|
+
|
|
96
|
+
expect(response.status).toStrictEqual(200)
|
|
97
|
+
expect(response.body).toStrictEqual(expected)
|
|
98
|
+
expect(response.headers['content-length']).toStrictEqual(
|
|
99
|
+
JSON.stringify(expected).length.toString()
|
|
100
|
+
)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it.each([inspect, inspectAsync])(
|
|
104
|
+
'should call callback with an error',
|
|
105
|
+
async handler => {
|
|
106
|
+
const server = express()
|
|
107
|
+
.use(jsonpMiddleware(handler))
|
|
108
|
+
.get('/', (_req, res) => res.status(404).jsonp({ a: 'a' }).end())
|
|
109
|
+
const response = await request(server).get('/')
|
|
110
|
+
|
|
111
|
+
const expected = { a: 'a', inspected_by: 'me' }
|
|
112
|
+
|
|
113
|
+
expect(response.status).toStrictEqual(404)
|
|
114
|
+
expect(response.body).toStrictEqual(expected)
|
|
115
|
+
expect(response.headers['content-length']).toStrictEqual(
|
|
116
|
+
JSON.stringify(expected).length.toString()
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
it.each([noop, noopAsync])(
|
|
122
|
+
'should return a as application/json',
|
|
123
|
+
async handler => {
|
|
124
|
+
const server = express()
|
|
125
|
+
.use(jsonpMiddleware(handler))
|
|
126
|
+
.get('/', (_req, res) => res.status(200).jsonp(42).end())
|
|
127
|
+
const response = await request(server).get('/')
|
|
128
|
+
|
|
129
|
+
expect(response.status).toStrictEqual(200)
|
|
130
|
+
expect(response.text).toStrictEqual('42')
|
|
131
|
+
expect(response.headers).toStrictEqual(
|
|
132
|
+
expect.objectContaining({
|
|
133
|
+
'content-type': 'application/json; charset=utf-8',
|
|
134
|
+
})
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
it.each([noPermission, noPermissionAsync])(
|
|
140
|
+
'should abort if a response is sent',
|
|
141
|
+
async handler => {
|
|
142
|
+
const server = express()
|
|
143
|
+
.use(jsonpMiddleware(handler))
|
|
144
|
+
.get('/', (_req, res) => res.status(200).jsonp({ a: 'a' }).end())
|
|
145
|
+
const response = await request(server).get('/')
|
|
146
|
+
|
|
147
|
+
expect(response.status).toStrictEqual(403)
|
|
148
|
+
expect(response.text).toStrictEqual('no permissions')
|
|
149
|
+
expect(response.headers).toStrictEqual(
|
|
150
|
+
expect.objectContaining({
|
|
151
|
+
'content-type': 'text/html; charset=utf-8',
|
|
152
|
+
})
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
it.each([error, errorAsync])(
|
|
158
|
+
'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()
|
|
162
|
+
const server = express()
|
|
163
|
+
.use(errorHandler)
|
|
164
|
+
.use(jsonpMiddleware(handler))
|
|
165
|
+
.get('/', (_req, res) => res.status(200).jsonp({ a: 'a' }).end())
|
|
166
|
+
|
|
167
|
+
const response = await request(server).get('/')
|
|
168
|
+
|
|
169
|
+
expect(response.status).toStrictEqual(500)
|
|
170
|
+
}
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
it.each([error, errorAsync])(
|
|
174
|
+
'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()
|
|
178
|
+
const server = express()
|
|
179
|
+
.use(errorHandler)
|
|
180
|
+
.use(jsonpMiddleware(handler))
|
|
181
|
+
.get('/', (_req, res) => {
|
|
182
|
+
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
|
+
})
|