snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
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 2s

This commit is contained in:
upstream-mirror
2026-09-16 01:21:02 +05:00
commit 30810eecd7
10161 changed files with 987110 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
# @rocket.chat/patch-injection
## 0.0.2
### Patch Changes
- ([#38989](https://github.com/RocketChat/Rocket.Chat/pull/38989)) chore(eslint): Upgrades ESLint and its configuration
## 0.0.2-rc.0
### Patch Changes
- ([#38989](https://github.com/RocketChat/Rocket.Chat/pull/38989)) chore(eslint): Upgrades ESLint and its configuration

View File

@@ -0,0 +1,6 @@
import server from '@rocket.chat/jest-presets/server';
import type { Config } from 'jest';
export default {
preset: server.preset,
} satisfies Config;

View File

@@ -0,0 +1,30 @@
{
"name": "@rocket.chat/patch-injection",
"version": "0.0.2",
"private": true,
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"files": [
"/dist"
],
"scripts": {
"build": "rm -rf dist && tsc",
"dev": "tsc --watch --preserveWatchOutput",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"test": "jest",
"testunit": "jest",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@rocket.chat/jest-presets": "workspace:~",
"@rocket.chat/tsconfig": "workspace:*",
"@types/jest": "~30.0.0",
"eslint": "~9.39.5",
"jest": "~30.2.0",
"typescript": "~5.9.3"
},
"volta": {
"extends": "../../package.json"
}
}

View File

@@ -0,0 +1,17 @@
import type { BaseFunction, PatchData, PatchFunction } from './definition';
import { getFunctionPatches } from './getFunctionPatches';
export const addPatch = <T extends BaseFunction>(baseFunction: T, patch: PatchFunction<T>, condition?: () => boolean) => {
const patches = getFunctionPatches(baseFunction);
const patchData: PatchData<T> = {
patchFunction: patch,
condition,
};
patches.add(patchData);
return () => {
patches.delete(patchData);
};
};

View File

@@ -0,0 +1,4 @@
import type { BaseFunction, PatchData } from './definition';
export const functions = new Map<BaseFunction, Set<PatchData<BaseFunction>>>();
export const calledFunctions = new Set<BaseFunction>();

View File

@@ -0,0 +1,13 @@
export type BaseFunction = (...args: any[]) => any;
export type PatchFunction<T extends BaseFunction> = (next: T, ...args: Parameters<T>) => ReturnType<T>;
export type PatchData<T extends BaseFunction> = {
patchFunction: PatchFunction<T>;
condition?: () => boolean;
};
export type PatchedFunction<T extends BaseFunction> = T & {
patch: (patch: PatchFunction<T>, condition?: () => boolean) => () => void;
originalSignature: T;
patchSignature: PatchFunction<T>;
};
export type OriginalSignature<P extends PatchedFunction<BaseFunction>> = P['originalSignature'];
export type PatchFor<P extends PatchedFunction<BaseFunction>> = PatchFunction<OriginalSignature<P>>;

View File

@@ -0,0 +1,15 @@
import { calledFunctions, functions } from './data';
import type { BaseFunction, PatchData } from './definition';
export const getFunctionPatches = <T extends BaseFunction>(baseFunction: T): Set<PatchData<T>> => {
if (calledFunctions.has(baseFunction)) {
throw new Error('Patching a function that was already used.');
}
const patches = functions.get(baseFunction) as Set<PatchData<T>> | undefined;
if (!patches) {
throw new Error('Specified function can not be patched');
}
return patches;
};

View File

@@ -0,0 +1,2 @@
export * from './makeFunction';
export * from './midleware';

View File

@@ -0,0 +1,236 @@
import { makeFunction } from './makeFunction';
describe('Make a simple function', () => {
it('should execute the function passed as argument', () => {
const fn = makeFunction(() => {
throw new Error('function was called');
});
expect(fn).toThrow('function was called');
});
it('should return the result of the internal function', () => {
const fn = makeFunction(() => 15);
expect(fn()).toBe(15);
});
});
describe('Replace a simple function', () => {
it('should execute the patched function', () => {
const fn = makeFunction(() => {
throw new Error('function was called');
});
fn.patch(() => {
throw new Error('patch was called');
});
expect(fn).toThrow('patch was called');
});
it('should return the result of the patched function', () => {
const fn = makeFunction(() => 15);
fn.patch(() => 20);
expect(fn()).toBe(20);
});
});
describe('Remove a patch', () => {
it('should execute the function passed as argument', () => {
const fn = makeFunction(() => {
throw new Error('function was called');
});
const remove = fn.patch(() => {
throw new Error('patch was called');
});
expect(fn).toThrow('patch was called');
remove();
expect(fn).toThrow('function was called');
});
it('should return the result of the internal function', () => {
const fn = makeFunction(() => 15);
const remove = fn.patch(() => 20);
expect(fn()).toBe(20);
remove();
expect(fn()).toBe(15);
});
});
describe('Patch Condition', () => {
it('should execute either function depending if the patch is enabled or not', () => {
const fn = makeFunction(() => {
throw new Error('function was called');
});
let enabled = false;
fn.patch(
() => {
throw new Error('patch was called');
},
() => enabled,
);
expect(fn).toThrow('function was called');
enabled = true;
expect(fn).toThrow('patch was called');
enabled = false;
expect(fn).toThrow('function was called');
});
it('should return the value of the right function based on the condition', () => {
const fn = makeFunction(() => 15);
let enabled = false;
fn.patch(
() => 20,
() => enabled,
);
expect(fn()).toBe(15);
enabled = true;
expect(fn()).toBe(20);
enabled = false;
expect(fn()).toBe(15);
});
});
describe('Chained calls', () => {
it('Should call the inner function', () => {
const fn = makeFunction(() => {
throw new Error('function was called');
});
fn.patch((next) => {
next();
throw new Error('patch was called');
});
expect(fn).toThrow('function was called');
});
it('should return the sum of values', () => {
const fn = makeFunction(() => 15);
fn.patch((next) => 20 + next());
expect(fn()).toBe(35);
});
it('should send the parameters in the correct order every time', () => {
const fn = makeFunction((a: string, b: string) => `3=${[a, b].join('')}`);
fn.patch((next, a, b) => `2=${[a, b].join('')},${next('E', 'F')}`);
fn.patch((next, a, b) => `1=${[a, b].join('')},${next('C', 'D')}`);
expect(fn('A', 'B')).toBe('1=AB,2=CD,3=EF');
});
});
describe('Multiple patches', () => {
it('Should call the right version of the function', () => {
const fn = makeFunction(() => {
throw new Error('function was called');
});
const remove = fn.patch(() => {
throw new Error('patch was called');
});
const remove2 = fn.patch(() => {
throw new Error('second patch was called');
});
expect(fn).toThrow('second patch was called');
remove2();
expect(fn).toThrow('patch was called');
remove();
expect(fn).toThrow('function was called');
});
it('Should respect conditions and removals', () => {
const fn = makeFunction(() => {
throw new Error('function was called');
});
let enabled = true;
let enabled2 = true;
const remove = fn.patch(
() => {
throw new Error('patch was called');
},
() => enabled,
);
const remove2 = fn.patch(
() => {
throw new Error('second patch was called');
},
() => enabled2,
);
expect(fn).toThrow('second patch was called');
enabled2 = false;
expect(fn).toThrow('patch was called');
enabled = false;
expect(fn).toThrow('function was called');
enabled2 = true;
expect(fn).toThrow('second patch was called');
enabled = true;
remove2();
expect(fn).toThrow('patch was called');
remove();
expect(fn).toThrow('function was called');
});
it('should chain on the right order', () => {
const fn = makeFunction(() => [1]);
let enabled2 = true;
let enabled3 = true;
let enabled4 = true;
fn.patch(
(next) => [2].concat(next()),
() => enabled2,
);
const remove3 = fn.patch(
(next) => [3].concat(next()),
() => enabled3,
);
fn.patch(
(next) => [4].concat(next()),
() => enabled4,
);
expect(fn()).toStrictEqual([4, 3, 2, 1]);
enabled2 = false;
expect(fn()).toStrictEqual([4, 3, 1]);
enabled4 = false;
expect(fn()).toStrictEqual([3, 1]);
enabled3 = false;
expect(fn()).toStrictEqual([1]);
enabled3 = true;
expect(fn()).toStrictEqual([3, 1]);
remove3();
expect(fn()).toStrictEqual([1]);
enabled2 = true;
enabled4 = true;
expect(fn()).toStrictEqual([4, 2, 1]);
});
});

View File

@@ -0,0 +1,26 @@
import type { BaseFunction, PatchFunction, PatchedFunction } from './definition';
import { withMiddleware } from './midleware';
export const makeFunction = <T extends BaseFunction>(fn: T): PatchedFunction<T> => {
const wrapped = withMiddleware(fn);
const patch = (fn: PatchFunction<T>, condition?: () => boolean) => {
return wrapped.use((ctx, next) => {
if (!condition || condition()) {
return fn(next as unknown as T, ...ctx);
}
return next(...ctx);
});
};
const originalSignature = (() => {
throw new Error('OriginalSignature of patched functions is not meant to be executed directly.');
}) as unknown as T;
const patchSignature = (() => {
throw new Error('PatchSignature of patched functions is not meant to be executed directly.');
}) as unknown as PatchFunction<T>;
return Object.assign(wrapped, {
patch,
originalSignature,
patchSignature,
}) as PatchedFunction<T>;
};

View File

@@ -0,0 +1,36 @@
type Middleware<F extends (...args: any[]) => any> = (ctx: Parameters<F>, next: NextFunction<F>) => ReturnType<F>;
type NextFunction<F extends (...args: any[]) => any> = (...args: Parameters<F> | []) => ReturnType<F>;
export function withMiddleware<F extends (...args: any[]) => any>(fn: F) {
const middlewares: Middleware<F>[] = [];
const buildRunner = (): ((ctx: Parameters<F>) => ReturnType<F>) => {
return middlewares.reduce(
(next, middleware) => {
return (ctx) => middleware(ctx, (...args) => next(args.length ? (args as Parameters<F>) : ctx));
},
(ctx: Parameters<F>) => fn(...ctx),
);
};
let runner = buildRunner();
const use = (middleware: Middleware<F>) => {
middlewares.push(middleware);
runner = buildRunner();
return () => {
const index = middlewares.indexOf(middleware);
if (index > -1) {
middlewares.splice(index, 1);
}
runner = buildRunner();
};
};
const run = (...args: Parameters<F>): ReturnType<F> => {
return runner(args);
};
return Object.assign(run as F, { use, fn });
}

View File

@@ -0,0 +1,10 @@
{
"extends": "@rocket.chat/tsconfig/server.json",
"compilerOptions": {
"module": "esnext",
"declaration": true,
"rootDir": "./src",
"outDir": "./dist"
},
"include": ["./src/**/*"]
}