Skip to content

Commit 40c333d

Browse files
gyszalaiGyula Szalai
andauthored
Use types from fastify-reply-from. fix #102 (#103)
* Use types from fastify-reply-from. fix #102 * Remove http2 option from FastifyHttpProxyOptions interface as it is inherited from FastifyReplyFromOptions Co-authored-by: Gyula Szalai <[email protected]@gmail.com>
1 parent 5868c19 commit 40c333d

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

index.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import {
55
preHandlerHookHandler
66
} from "fastify";
77

8-
export interface FastifyHttpProxyOptions {
8+
import {
9+
FastifyReplyFromOptions
10+
} from "fastify-reply-from"
11+
12+
export interface FastifyHttpProxyOptions extends FastifyReplyFromOptions {
913
upstream: string;
1014
prefix?: string;
1115
rewritePrefix?: string;
12-
http2?: boolean;
1316
proxyPayloads?: boolean;
1417
preHandler?: preHandlerHookHandler;
1518
beforeHandler?: preHandlerHookHandler;

test/index.test-d.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,21 @@ app.register(fastifyHttpProxy, {
2222
beforeHandler: (request, reply) => {
2323
expectType<RawRequestDefaultExpression>(request.raw);
2424
expectType<RawReplyDefaultExpression>(reply.raw);
25-
}
25+
},
26+
base: 'whatever',
27+
cacheURLs: 10,
28+
undici: { dummy: true }, // undici has no TS declarations yet
29+
http: {
30+
agentOptions: {
31+
keepAliveMsecs: 10 * 60 * 1000
32+
},
33+
requestOptions: {
34+
timeout: 20000
35+
}
36+
},
37+
keepAliveMsecs: 60000,
38+
maxFreeSockets: 10,
39+
maxSockets: 20,
40+
rejectUnauthorized: true,
41+
sessionTimeout: 30000
2642
});

test/test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ async function run () {
2929
return 'this is /api2/a'
3030
})
3131

32+
origin.get('/timeout', async (request, reply) => {
33+
await new Promise((resolve) => setTimeout(resolve, 600))
34+
return 'this is never received'
35+
})
36+
3237
await origin.listen(0)
3338

3439
tearDown(origin.close.bind(origin))
@@ -373,6 +378,33 @@ async function run () {
373378
)
374379
t.equal(location, '/something')
375380
})
381+
382+
test('proxy request timeout', async t => {
383+
const server = Fastify()
384+
server.register(proxy, {
385+
upstream: `http://localhost:${origin.server.address().port}`,
386+
http: {
387+
requestOptions: {
388+
timeout: 300
389+
}
390+
}
391+
})
392+
393+
await server.listen(0)
394+
t.tearDown(server.close.bind(server))
395+
396+
try {
397+
await got(
398+
`http://localhost:${server.server.address().port}/timeout`,
399+
{ retry: 0 }
400+
)
401+
} catch (err) {
402+
t.equal(err.response.statusCode, 504)
403+
t.equal(err.response.statusMessage, 'Gateway Timeout')
404+
return
405+
}
406+
t.fail()
407+
})
376408
}
377409

378410
run()

0 commit comments

Comments
 (0)