Files
upstream-rocketchat/apps/meteor/server/lib/deprecationWarningLogger.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

184 lines
6.7 KiB
TypeScript

import { Logger } from '@rocket.chat/logger';
import type { PathPattern } from '@rocket.chat/rest-typings';
import semver from 'semver';
import { metrics } from './metrics/lib/metrics';
const deprecationLogger = new Logger('DeprecationWarning');
type MessageFn<T> = (params: { parameter: string; version: string } & T) => string;
const throwErrorsForVersionsUnder = process.env.ROCKET_CHAT_DEPRECATION_THROW_ERRORS_FOR_VERSIONS_UNDER;
const writeDeprecationHeader = (res: Response | undefined, type: string, message: string, version: string) => {
if (res) {
res.headers.set('x-deprecation-type', type);
res.headers.set('x-deprecation-message', message);
res.headers.set('x-deprecation-version', version);
}
};
const compareVersions = (version: string, message: string) => {
if (throwErrorsForVersionsUnder && semver.lte(version, throwErrorsForVersionsUnder)) {
throw new Error(message);
}
};
export type DeprecationLoggerNextPlannedVersion = '9.0.0';
// `TEST_MODE=true` makes the logger throw so accidental usage of a deprecated
// method/endpoint surfaces immediately in the test run. The API end-to-end
// suite sets `TEST_MODE=api` instead — every other test-mode behavior stays on
// (rate limiter bypass, short cache TTLs, etc.) but the deprecation logger
// only logs because the suite intentionally exercises deprecated DDP methods
// through `/v1/method.call/:method` and the streamer.
const shouldThrowOnDeprecation = process.env.TEST_MODE === 'true';
export const apiDeprecationLogger = ((logger) => {
return {
endpoint: (endpoint: string, version: DeprecationLoggerNextPlannedVersion, res: Response, info = '') => {
const message = `The endpoint "${endpoint}" is deprecated and will be removed on version ${version}${info ? ` (${info})` : ''}`;
if (shouldThrowOnDeprecation) {
throw new Error(message);
}
compareVersions(version, message);
writeDeprecationHeader(res, 'endpoint-deprecation', message, version);
metrics.deprecations.inc({ type: 'deprecation', kind: 'endpoint', name: endpoint });
metrics.deprecationsTotal.inc({ type: 'deprecation', kind: 'endpoint', name: endpoint });
logger.warn({ msg: message, endpoint, version, info });
},
parameter: (
endpoint: string,
parameter: string,
version: DeprecationLoggerNextPlannedVersion,
res: Response,
messageGenerator?: MessageFn<{ endpoint: string }>,
) => {
const message =
messageGenerator?.({
parameter,
endpoint,
version,
}) ?? `The parameter "${parameter}" in the endpoint "${endpoint}" is deprecated and will be removed on version ${version}`;
compareVersions(version, message);
if (shouldThrowOnDeprecation) {
throw new Error(message);
}
metrics.deprecations.inc({ type: 'parameter-deprecation', kind: 'endpoint', name: endpoint, params: parameter });
metrics.deprecationsTotal.inc({ type: 'parameter-deprecation', kind: 'endpoint', name: endpoint, params: parameter });
writeDeprecationHeader(res, 'parameter-deprecation', message, version);
logger.warn({ msg: message, endpoint, parameter, version });
},
deprecatedParameterUsage: (
endpoint: string,
parameter: string,
version: DeprecationLoggerNextPlannedVersion,
res: Response,
messageGenerator?: MessageFn<{
endpoint: string;
}>,
) => {
const message =
messageGenerator?.({
parameter,
endpoint,
version,
}) ?? `The usage of the endpoint "${endpoint}" is deprecated and will be removed on version ${version}`;
if (shouldThrowOnDeprecation) {
throw new Error(message);
}
compareVersions(version, message);
metrics.deprecations.inc({ type: 'invalid-usage', kind: 'endpoint', name: endpoint, params: parameter });
metrics.deprecationsTotal.inc({ type: 'invalid-usage', kind: 'endpoint', name: endpoint, params: parameter });
writeDeprecationHeader(res, 'invalid-usage', message, version);
logger.warn({ msg: message, endpoint, parameter, version });
},
};
})(deprecationLogger.section('API'));
type DeprecationPath<T> = T extends `/${string}` ? (T extends PathPattern ? T : never) : string;
type DeprecationInfo<T> = DeprecationPath<T> | Array<DeprecationPath<T>>;
export const methodDeprecationLogger = ((logger) => {
return {
method: <T extends string | PathPattern>(method: string, version: DeprecationLoggerNextPlannedVersion, info: DeprecationInfo<T>) => {
const infoArray = Array.isArray(info) ? info : [info];
const paths = infoArray.map((p) => (typeof p === 'string' && p.startsWith('/') ? `"${p}"` : p)).join(' or ');
const replacement = infoArray.length > 0 ? `Use the ${paths} endpoint${infoArray.length > 1 ? 's' : ''} instead` : '';
const message = `The method "${method}" is deprecated and will be removed on version ${version}${replacement ? ` (${replacement})` : ''}`;
if (shouldThrowOnDeprecation) {
throw new Error(message);
}
compareVersions(version, message);
metrics.deprecations.inc({ type: 'deprecation', name: method, kind: 'method' });
metrics.deprecationsTotal.inc({ type: 'deprecation', name: method, kind: 'method' });
logger.warn({ msg: message, method, version, replacement });
},
parameter: (method: string, parameter: string, version: DeprecationLoggerNextPlannedVersion) => {
const message = `The parameter "${parameter}" in the method "${method}" is deprecated and will be removed on version ${version}`;
if (shouldThrowOnDeprecation) {
throw new Error(message);
}
metrics.deprecations.inc({ type: 'parameter-deprecation', name: method, params: parameter });
metrics.deprecationsTotal.inc({ type: 'parameter-deprecation', name: method, params: parameter });
compareVersions(version, message);
logger.warn({ msg: message, method, parameter, version });
},
deprecatedParameterUsage: (
method: string,
parameter: string,
version: DeprecationLoggerNextPlannedVersion,
messageGenerator?: MessageFn<{
method: string;
}>,
) => {
const message =
messageGenerator?.({
parameter,
method,
version,
}) ?? `The usage of the method "${method}" is deprecated and will be removed on version ${version}`;
if (shouldThrowOnDeprecation) {
throw new Error(message);
}
compareVersions(version, message);
metrics.deprecations.inc({ type: 'invalid-usage', name: method, params: parameter, kind: 'method' });
metrics.deprecationsTotal.inc({ type: 'invalid-usage', name: method, params: parameter, kind: 'method' });
logger.warn({ msg: message, method, parameter, version });
},
/** @deprecated */
warn: (message: string) => {
if (shouldThrowOnDeprecation) {
throw new Error(message);
}
compareVersions('0.0.0', message);
logger.warn({ msg: message });
},
};
})(deprecationLogger.section('METHOD'));
export const streamDeprecationLogger = deprecationLogger.section('STREAM');