Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Avoid blocking work on the request path
Do not run heavy or blocking operations inside request handlers; offload to async jobs.
Bad example
| 1 | export async function generateReport(req: Request, res: Response) { |
| 2 | // BAD: heavy CPU/IO work blocks the request |
| 3 | const pdf = await reportService.generatePdf(req.body); |
| 4 |
|
| 5 | res.setHeader("Content-Type", "application/pdf"); |
| 6 | return res.send(pdf); |
| 7 | } |
Explanation (EN)
Heavy work in the request path increases latency and can cause timeouts under load.
Objašnjenje (HR)
Teski rad u request pathu povecava latenciju i moze uzrokovati timeout pod opterecenjem.
Good example
| 1 | export async function generateReport(req: Request, res: Response) { |
| 2 | const jobId = await queue.enqueue("generate-report", { |
| 3 | userId: req.user.id, |
| 4 | params: req.body, |
| 5 | }); |
| 6 |
|
| 7 | return res.status(202).json({ jobId }); |
| 8 | } |
Explanation (EN)
Offloading long-running work keeps the request fast and lets a worker handle heavy processing.
Objašnjenje (HR)
Premjestanje dugotrajnih poslova drzi zahtjev brzima, a worker obradjuje tezak posao.
Exceptions / Tradeoffs (EN)
Small, fast operations can run inline, but anything heavy should be queued or precomputed.
Iznimke / Tradeoffi (HR)
Male, brze operacije mogu ici inline, ali sve sto je tesko treba prebaciti u queue ili precompute.