-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #285 from thefrontside/foundation-delay
foundation simulator delay response API
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@simulacrum/foundation-simulator": minor:enhance | ||
--- | ||
|
||
Add API to configure a delay of all responses with a set interval. Using this in a simulator would enable a feel closer to a real external endpoint. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { Request, Response, NextFunction } from "express"; | ||
|
||
export function delayMiddleware( | ||
delayResponses?: number | { minimum: number; maximum: number } | ||
) { | ||
const delayMin = | ||
typeof delayResponses === "number" | ||
? delayResponses | ||
: delayResponses?.minimum; | ||
const delayMax = | ||
typeof delayResponses === "number" ? undefined : delayResponses?.maximum; | ||
|
||
return async function delayHandler( | ||
request: Request, | ||
response: Response, | ||
next: NextFunction | ||
) { | ||
if (delayMin || delayMax) { | ||
let timeoutDuration = calculateTimeoutDuration(delayMin, delayMax); | ||
await new Promise<void>((resolve) => { | ||
let timeoutHandle: NodeJS.Timeout | undefined = undefined; | ||
|
||
const done = () => { | ||
if (timeoutHandle) { | ||
clearTimeout(timeoutHandle); | ||
} | ||
resolve(); | ||
}; | ||
|
||
timeoutHandle = setTimeout(done, timeoutDuration); | ||
}); | ||
} | ||
next(); | ||
}; | ||
} | ||
|
||
function calculateTimeoutDuration( | ||
min: number | undefined, | ||
max: number | undefined | ||
): number { | ||
if (max && !min) { | ||
// sets the timeout at +/- 20% of configured max | ||
return max * 0.8 + max * 0.4 * Math.random(); | ||
} else if (min && !max) { | ||
// sets the timeout at +/- 20% of configured max | ||
return min * 0.8 + min * 0.4 * Math.random(); | ||
} else if (max && min) { | ||
const timeoutRange = max - min; | ||
return min + timeoutRange * Math.random(); | ||
} | ||
|
||
return 0; | ||
} |