file-routing-expressjs
Version:
A dependency-free, flexible, system-based file routing for Express.
61 lines • 1.75 kB
JavaScript
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
import { withTimeout } from "../helpers/async";
import { isNotDefined } from "../helpers/validators";
import { TimeoutError } from "../types/exceptions";
const timeoutPlugin = {
name: "timeout",
validateConfig(config) {
if (!config || typeof config.ms !== "number" || config.ms <= 0) {
throw new Error(`[timeout] "ms" must be > 0`);
}
},
wrap(handler, config) {
return (req, res, next) => __async(this, null, function* () {
if (isNotDefined(config)) {
return handler(req, res, next);
}
const exec = () => __async(this, null, function* () {
return handler(req, res, next);
});
const abortController = new AbortController();
req.signal = abortController.signal;
try {
yield withTimeout(exec(), config.ms, abortController);
} catch (err) {
if (err instanceof TimeoutError) {
if (!res.headersSent) {
res.status(504).send("Request timeout");
}
return;
}
throw err;
} finally {
delete req.signal;
}
});
}
};
var timeout_default = timeoutPlugin;
export {
timeout_default as default
};
//# sourceMappingURL=timeout.mjs.map