-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
249fb78
commit 5000cc8
Showing
18 changed files
with
388 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]> => []); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './getInstanceList'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": ["@rocket.chat/eslint-config"], | ||
"ignorePatterns": ["**/dist"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
preset: 'ts-jest', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './definition'; | ||
export * from './makeFunction'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.