1
1
import { delay } from '@httptoolkit/util' ;
2
- import { HttpHandler , HttpEndpoint } from '../http-index.js' ;
2
+ import { HttpHandler } from '../http-index.js' ;
3
3
import { buildHttpBinAnythingEndpoint } from '../../httpbin-compat.js' ;
4
4
5
5
const matchPath = ( path : string ) => path . startsWith ( '/delay/' ) ;
@@ -8,17 +8,26 @@ const defaultAnythingEndpoint = buildHttpBinAnythingEndpoint({
8
8
fieldFilter : [ "url" , "args" , "form" , "data" , "origin" , "headers" , "files" ]
9
9
} ) ;
10
10
11
- const handle : HttpHandler = async ( req , res , { path } ) => {
12
- const delayMs = parseFloat ( path . slice ( '/delay/' . length ) ) ;
11
+ const handle : HttpHandler = async ( req , res , { path, handleRequest } ) => {
12
+ const followingSlashIndex = path . indexOf ( '/' , '/delay/' . length ) ;
13
+ const followingUrl = followingSlashIndex !== - 1 ? path . slice ( followingSlashIndex ) : '' ;
14
+ const endOfDelay = followingSlashIndex === - 1 ? path . length : followingSlashIndex ;
15
+ const delayMs = parseFloat ( path . slice ( '/delay/' . length , endOfDelay ) ) ;
13
16
14
17
if ( isNaN ( delayMs ) ) {
15
18
res . writeHead ( 400 ) ;
16
19
res . end ( 'Invalid delay duration' ) ;
17
20
}
18
21
19
- await delay ( Math . min ( delayMs , 10 * 1000 ) ) ; // 10s max
22
+ await delay ( Math . min ( delayMs , 10 ) * 1000 ) ; // 10s max
20
23
21
- return defaultAnythingEndpoint ( req , res ) ;
24
+ if ( followingUrl ) {
25
+ req . url = followingUrl ;
26
+ handleRequest ( req , res ) ;
27
+ return ;
28
+ } else {
29
+ return defaultAnythingEndpoint ( req , res ) ;
30
+ }
22
31
}
23
32
24
33
export const delayEndpoint = {
0 commit comments