98 lines
4.3 KiB
JavaScript
98 lines
4.3 KiB
JavaScript
"use strict";
|
||
/** Guards for heavy analytics/report queries — prefer slow responses over process crashes. */
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.MAX_PDF_EXPORT_ROWS = exports.MAX_SELECTED_IDS = exports.MAX_EXPORT_ROWS = exports.MAX_REPORT_OFFSET = exports.MAX_REPORT_PAGE_SIZE = exports.EXPORT_OPERATION_TIMEOUT_MS = exports.ANALYTICS_SECTION_TIMEOUT_MS = exports.DB_STATEMENT_TIMEOUT_MS = void 0;
|
||
exports.clampReportPagination = clampReportPagination;
|
||
exports.isQueryTimeoutError = isQueryTimeoutError;
|
||
exports.isOutOfMemoryError = isOutOfMemoryError;
|
||
exports.mapResilienceErrorToHttp = mapResilienceErrorToHttp;
|
||
exports.withTimeout = withTimeout;
|
||
exports.DB_STATEMENT_TIMEOUT_MS = Math.max(0, parseInt(process.env.DB_STATEMENT_TIMEOUT_MS ?? '180000', 10));
|
||
/** Per dashboard subsection — fail fast so other sections can still load. */
|
||
exports.ANALYTICS_SECTION_TIMEOUT_MS = Math.max(5000, parseInt(process.env.ANALYTICS_SECTION_TIMEOUT_MS ?? '90000', 10));
|
||
exports.EXPORT_OPERATION_TIMEOUT_MS = Math.max(10000, parseInt(process.env.EXPORT_OPERATION_TIMEOUT_MS ?? '120000', 10));
|
||
exports.MAX_REPORT_PAGE_SIZE = 100;
|
||
exports.MAX_REPORT_OFFSET = 100000;
|
||
exports.MAX_EXPORT_ROWS = Math.min(10000, Math.max(100, parseInt(process.env.MAX_REPORT_EXPORT_ROWS ?? '10000', 10)));
|
||
exports.MAX_SELECTED_IDS = 500;
|
||
exports.MAX_PDF_EXPORT_ROWS = Math.min(exports.MAX_EXPORT_ROWS, Math.max(100, parseInt(process.env.MAX_PDF_EXPORT_ROWS ?? '3000', 10)));
|
||
function clampReportPagination(limitRaw, offsetRaw) {
|
||
const warnings = [];
|
||
let limit = Number.isFinite(limitRaw) ? Math.floor(limitRaw) : 50;
|
||
let offset = Number.isFinite(offsetRaw) ? Math.floor(offsetRaw) : 0;
|
||
if (limit > exports.MAX_REPORT_PAGE_SIZE) {
|
||
warnings.push(`Page size limited to ${exports.MAX_REPORT_PAGE_SIZE} rows.`);
|
||
limit = exports.MAX_REPORT_PAGE_SIZE;
|
||
}
|
||
if (limit < 1)
|
||
limit = 1;
|
||
if (offset > exports.MAX_REPORT_OFFSET) {
|
||
warnings.push(`Pagination offset capped at ${exports.MAX_REPORT_OFFSET.toLocaleString()} for stability.`);
|
||
offset = exports.MAX_REPORT_OFFSET;
|
||
}
|
||
if (offset < 0)
|
||
offset = 0;
|
||
return { limit, offset, warnings };
|
||
}
|
||
function isQueryTimeoutError(err) {
|
||
if (!err || typeof err !== 'object')
|
||
return false;
|
||
const e = err;
|
||
return (e.code === '57014' ||
|
||
(typeof e.message === 'string' &&
|
||
(e.message.includes('statement timeout') ||
|
||
e.message.includes('canceling statement') ||
|
||
e.message.includes('Query read timeout'))));
|
||
}
|
||
function isOutOfMemoryError(err) {
|
||
if (!err || typeof err !== 'object')
|
||
return false;
|
||
const e = err;
|
||
return (e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
|
||
(typeof e.message === 'string' &&
|
||
(e.message.includes('heap out of memory') || e.message.includes('allocation failed'))));
|
||
}
|
||
function mapResilienceErrorToHttp(err) {
|
||
if (isQueryTimeoutError(err)) {
|
||
return {
|
||
status: 503,
|
||
message: 'La requête a pris trop de temps (volume de données élevé). Réduisez la période ou affinez les filtres.',
|
||
};
|
||
}
|
||
if (err instanceof Error &&
|
||
(err.message.includes('operation timed out') || err.message.includes('timed out after'))) {
|
||
return {
|
||
status: 503,
|
||
message: 'L’opération a pris trop de temps (volume de données élevé). Réduisez la période ou affinez les filtres.',
|
||
};
|
||
}
|
||
if (isOutOfMemoryError(err)) {
|
||
return {
|
||
status: 503,
|
||
message: 'Export trop volumineux pour être traité en une fois. Réduisez les filtres ou exportez par pages.',
|
||
};
|
||
}
|
||
return {
|
||
status: 400,
|
||
message: err instanceof Error ? err.message : 'Operation failed',
|
||
};
|
||
}
|
||
function withTimeout(promise, timeoutMs, label) {
|
||
if (timeoutMs <= 0)
|
||
return promise;
|
||
return new Promise((resolve, reject) => {
|
||
const timer = setTimeout(() => {
|
||
reject(new Error(`${label}: operation timed out after ${timeoutMs}ms`));
|
||
}, timeoutMs);
|
||
promise
|
||
.then((v) => {
|
||
clearTimeout(timer);
|
||
resolve(v);
|
||
})
|
||
.catch((e) => {
|
||
clearTimeout(timer);
|
||
reject(e);
|
||
});
|
||
});
|
||
}
|
||
//# sourceMappingURL=queryResilience.js.map
|