expressify: Do not send response when already sent
This commit is contained in:
parent
a8d603dd59
commit
ee29307e97
@ -26,24 +26,46 @@ describe('expressify', () => {
|
|||||||
expressify(
|
expressify(
|
||||||
createMiddleware(ctx => (ctx.req as any).id = 'test')))
|
createMiddleware(ctx => (ctx.req as any).id = 'test')))
|
||||||
app.get('/test', expressify(ctx => (ctx.req as any).id))
|
app.get('/test', expressify(ctx => (ctx.req as any).id))
|
||||||
request(app)
|
await request(app)
|
||||||
.get('/test')
|
.get('/test')
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.expect('"test"')
|
.expect('"test"')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('can send a response using a custom fn', () => {
|
it('can send a response using a custom fn', async () => {
|
||||||
const app = express()
|
const app = express()
|
||||||
app.use(
|
app.use(
|
||||||
expressify(
|
expressify(
|
||||||
createMiddleware(ctx => (ctx.req as any).id = 'test')))
|
createMiddleware(ctx => (ctx.req as any).id = 'test')))
|
||||||
app.get('/test',
|
app.get('/test',
|
||||||
expressify(ctx => (ctx.req as any).id, (res, value) => res.send(value)))
|
expressify(ctx => (ctx.req as any).id, (res, value) => res.send(value)))
|
||||||
request(app)
|
await request(app)
|
||||||
.get('/test')
|
.get('/test')
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.expect('test')
|
.expect('test')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('handles errors', async () => {
|
||||||
|
const app = express()
|
||||||
|
app.use(
|
||||||
|
expressify(
|
||||||
|
createMiddleware(ctx => { throw new Error('test') })))
|
||||||
|
await request(app)
|
||||||
|
.get('/test')
|
||||||
|
.expect(500)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not crash nor hang when middleware sends a response', async () => {
|
||||||
|
const app = express()
|
||||||
|
app.use(
|
||||||
|
expressify(
|
||||||
|
createMiddleware(ctx => ctx.res.end('test'))))
|
||||||
|
await request(app)
|
||||||
|
.get('/test')
|
||||||
|
.expect(200)
|
||||||
|
.expect('test')
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
import { NextFunction, Request, Response } from 'express'
|
import { NextFunction, Request, Response } from 'express'
|
||||||
import { IncomingMessage, ServerResponse } from 'http'
|
|
||||||
import { Middleware } from './Middleware'
|
import { Middleware } from './Middleware'
|
||||||
|
|
||||||
export const expressify = (
|
export const expressify = (
|
||||||
@ -13,9 +12,7 @@ export const expressify = (
|
|||||||
) => {
|
) => {
|
||||||
let result: unknown
|
let result: unknown
|
||||||
try {
|
try {
|
||||||
const r: IncomingMessage = req
|
result = await handleMiddleware({req, res})
|
||||||
const rr: ServerResponse = res
|
|
||||||
result = await handleMiddleware({req: r, res: rr})
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
next(err)
|
next(err)
|
||||||
return
|
return
|
||||||
@ -24,5 +21,7 @@ export const expressify = (
|
|||||||
next()
|
next()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (res.writable) {
|
||||||
sendResponse(res, result)
|
sendResponse(res, result)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user