file-routing-expressjs
Version:
A dependency-free, flexible, system-based file routing for Express.
1 lines • 2.19 kB
Source Map (JSON)
{"version":3,"sources":["../../src/plugins/timeout.ts"],"sourcesContent":["import { withTimeout } from \"../helpers/async\";\nimport { isNotDefined } from \"../helpers/validators\";\nimport { Plugin, TimeoutConfig } from \"../types\";\nimport { TimeoutError } from \"../types/exceptions\";\n\nconst timeoutPlugin: Plugin<TimeoutConfig> = {\n name: \"timeout\",\n\n validateConfig(config) {\n if (!config || typeof config.ms !== \"number\" || config.ms <= 0) { \n throw new Error(`[timeout] \"ms\" must be > 0`); \n }\n },\n\n wrap(handler, config) {\n return async (req, res, next) => {\n if (isNotDefined(config)) {\n return handler(req, res, next);\n }\n\n const exec = async () => handler(req, res, next);\n const abortController = new AbortController();\n req.signal = abortController.signal;\n\n try {\n await withTimeout(exec(), config.ms, abortController);\n } catch (err) {\n if (err instanceof TimeoutError) {\n if (!res.headersSent) {\n res.status(504).send(\"Request timeout\");\n }\n return;\n }\n\n throw err;\n } finally {\n delete req.signal;\n }\n };\n }\n};\n\nexport default timeoutPlugin;"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,SAAS,mBAAmB;AAC5B,SAAS,oBAAoB;AAE7B,SAAS,oBAAoB;AAE7B,MAAM,gBAAuC;AAAA,EACzC,MAAM;AAAA,EAEN,eAAe,QAAQ;AACnB,QAAI,CAAC,UAAU,OAAO,OAAO,OAAO,YAAY,OAAO,MAAM,GAAG;AAC5D,YAAM,IAAI,MAAM,4BAA4B;AAAA,IAChD;AAAA,EACJ;AAAA,EAEA,KAAK,SAAS,QAAQ;AAClB,WAAO,CAAO,KAAK,KAAK,SAAS;AAC7B,UAAI,aAAa,MAAM,GAAG;AACtB,eAAO,QAAQ,KAAK,KAAK,IAAI;AAAA,MACjC;AAEA,YAAM,OAAO,MAAS;AAAG,uBAAQ,KAAK,KAAK,IAAI;AAAA;AAC/C,YAAM,kBAAkB,IAAI,gBAAgB;AAC5C,UAAI,SAAS,gBAAgB;AAE7B,UAAI;AACA,cAAM,YAAY,KAAK,GAAG,OAAO,IAAI,eAAe;AAAA,MACxD,SAAS,KAAK;AACV,YAAI,eAAe,cAAc;AAC7B,cAAI,CAAC,IAAI,aAAa;AAClB,gBAAI,OAAO,GAAG,EAAE,KAAK,iBAAiB;AAAA,UAC1C;AACA;AAAA,QACJ;AAEA,cAAM;AAAA,MACV,UAAE;AACE,eAAO,IAAI;AAAA,MACf;AAAA,IACJ;AAAA,EACJ;AACJ;AAEA,IAAO,kBAAQ;","names":[]}