@unito/integration-sdk
Version:
Integration SDK
19 lines (18 loc) • 769 B
JavaScript
import { TimeoutError } from '../httpErrors.js';
const OPERATION_DEADLINE_HEADER = 'X-Unito-Operation-Deadline';
function extractOperationDeadline(req, res, next) {
const operationDeadlineHeader = Number(req.header(OPERATION_DEADLINE_HEADER));
if (operationDeadlineHeader) {
// `operationDeadlineHeader` represents a timestamp in the future, in seconds.
// We need to convert it to a number of milliseconds.
const deadline = operationDeadlineHeader * 1000 - Date.now();
if (deadline > 0) {
res.locals.signal = AbortSignal.timeout(deadline);
}
else {
throw new TimeoutError('Request already timed out upon reception');
}
}
next();
}
export default extractOperationDeadline;