Skip to content

Commit

Permalink
chore: function patches (#31899)
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-lehnen-rc authored Apr 16, 2024
1 parent 249fb78 commit 5000cc8
Show file tree
Hide file tree
Showing 18 changed files with 388 additions and 4 deletions.
4 changes: 4 additions & 0 deletions apps/meteor/app/api/server/helpers/getInstanceList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { makeFunction } from '@rocket.chat/patch-injection';
import type { BrokerNode } from 'moleculer';

export const getInstanceList = makeFunction(async (): Promise<BrokerNode[]> => []);
8 changes: 4 additions & 4 deletions apps/meteor/app/api/server/v1/instances.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { InstanceStatus } from '@rocket.chat/models';

import { Instance as InstanceService } from '../../../../ee/server/sdk';
import { isRunningMs } from '../../../../server/lib/isRunningMs';
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
import { API } from '../api';
import { getInstanceList } from '../helpers/getInstanceList';

const getMatrixInstances = (() => {
const getConnections = (() => {
if (isRunningMs()) {
return () => [];
}

return () => InstanceService.getInstances();
return () => getInstanceList();
})();

API.v1.addRoute(
Expand All @@ -24,7 +24,7 @@ API.v1.addRoute(

const instanceRecords = await InstanceStatus.find().toArray();

const connections = await getMatrixInstances();
const connections = await getConnections();

const result = instanceRecords.map((instanceRecord) => {
const connection = connections.find((c) => c.id === instanceRecord._id);
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/ee/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ import './configuration/index';
import './local-services/ldap/service';
import './methods/getReadReceipts';
import './apps/startup';
import './patches';

export { registerEEBroker } from './startup';
4 changes: 4 additions & 0 deletions apps/meteor/ee/server/patches/getInstanceList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { getInstanceList } from '../../../app/api/server/helpers/getInstanceList';
import { Instance } from '../sdk';

getInstanceList.patch(() => Instance.getInstances());
1 change: 1 addition & 0 deletions apps/meteor/ee/server/patches/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './getInstanceList';
1 change: 1 addition & 0 deletions apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@
"@rocket.chat/omnichannel-services": "workspace:^",
"@rocket.chat/onboarding-ui": "~0.33.3",
"@rocket.chat/password-policies": "workspace:^",
"@rocket.chat/patch-injection": "workspace:^",
"@rocket.chat/pdf-worker": "workspace:^",
"@rocket.chat/poplib": "workspace:^",
"@rocket.chat/presence": "workspace:^",
Expand Down
4 changes: 4 additions & 0 deletions packages/patch-injection/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["@rocket.chat/eslint-config"],
"ignorePatterns": ["**/dist"]
}
3 changes: 3 additions & 0 deletions packages/patch-injection/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
preset: 'ts-jest',
};
24 changes: 24 additions & 0 deletions packages/patch-injection/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@rocket.chat/patch-injection",
"version": "0.0.1",
"private": true,
"devDependencies": {
"@types/jest": "~29.5.7",
"eslint": "~8.45.0",
"jest": "~29.6.4",
"ts-jest": "~29.1.1",
"typescript": "~5.3.3"
},
"scripts": {
"lint": "eslint --ext .js,.jsx,.ts,.tsx .",
"lint:fix": "eslint --ext .js,.jsx,.ts,.tsx . --fix",
"test": "jest",
"build": "rm -rf dist && tsc -p tsconfig.json",
"dev": "tsc -p tsconfig.json --watch --preserveWatchOutput"
},
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"files": [
"/dist"
]
}
17 changes: 17 additions & 0 deletions packages/patch-injection/src/addPatch.ts
Original file line number Diff line number Diff line change
@@ -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);
};
};
4 changes: 4 additions & 0 deletions packages/patch-injection/src/data.ts
Original file line number Diff line number Diff line change
@@ -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>();
9 changes: 9 additions & 0 deletions packages/patch-injection/src/definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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;
};
15 changes: 15 additions & 0 deletions packages/patch-injection/src/getFunctionPatches.ts
Original file line number Diff line number Diff line change
@@ -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;
};
2 changes: 2 additions & 0 deletions packages/patch-injection/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './definition';
export * from './makeFunction';
33 changes: 33 additions & 0 deletions packages/patch-injection/src/makeFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { addPatch } from './addPatch';
import { calledFunctions, functions } from './data';
import type { BaseFunction, PatchData, PatchFunction, PatchedFunction } from './definition';

export const makeFunction = <T extends BaseFunction>(fn: T): PatchedFunction<T> => {
const patches = new Set<PatchData<T>>();

patches.add({
patchFunction: (_next, ...args) => fn(...args),
});

const result = ((...args: Parameters<T>): ReturnType<T> => {
let newFn: T = fn;

for (const patch of patches) {
if (patch.condition && !patch.condition()) {
continue;
}

const nextFn = newFn;
newFn = ((...args: Parameters<T>) => patch.patchFunction(nextFn, ...args)) as T;
}

calledFunctions.add(result);
return newFn(...args);
}) as PatchedFunction<T>;

functions.set(result, patches as Set<PatchData<BaseFunction>>);

result.patch = (patch: PatchFunction<T>, condition?: () => boolean) => addPatch(result, patch, condition);

return result;
};
Loading

0 comments on commit 5000cc8

Please sign in to comment.