1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-23 17:02:25 +03:00

Improve PushProcessor::getPushRuleGlobRegex (#4764)

* Improve PushProcessor::getPushRuleGlobRegex

Fix cache key not taking non-pattern parameters into account
Use lookarounds to ensure the word boundary isn't treated as part of the match

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Add tests

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski
2025-03-21 10:57:14 +00:00
committed by GitHub
parent 8061fa924d
commit 1e92c13a75
2 changed files with 45 additions and 5 deletions

View File

@@ -313,19 +313,21 @@ export class PushProcessor {
* No cache invalidation is present currently,
* as this will be inherently bounded to the size of the user's own push rules.
* @param pattern - the glob pattern to convert to a RegExp
* @param alignToWordBoundary - whether to align the pattern to word boundaries, as specified for `content.body` matches
* @param alignToWordBoundary - whether to align the pattern to word boundaries,
* as specified for `content.body` matches, will use lookaround assertions to ensure the match only includes the pattern
* @param flags - the flags to pass to the RegExp constructor, defaults to case-insensitive
*/
public static getPushRuleGlobRegex(pattern: string, alignToWordBoundary = false, flags = "i"): RegExp {
const [prefix, suffix] = alignToWordBoundary ? ["(^|\\W)", "(\\W|$)"] : ["^", "$"];
const [prefix, suffix] = alignToWordBoundary ? ["(?<=^|\\W)", "(?=\\W|$)"] : ["^", "$"];
const cacheKey = `${alignToWordBoundary}-${flags}-${pattern}`;
if (!PushProcessor.cachedGlobToRegex[pattern]) {
PushProcessor.cachedGlobToRegex[pattern] = new RegExp(
if (!PushProcessor.cachedGlobToRegex[cacheKey]) {
PushProcessor.cachedGlobToRegex[cacheKey] = new RegExp(
prefix + "(" + globToRegexp(pattern) + ")" + suffix,
flags,
);
}
return PushProcessor.cachedGlobToRegex[pattern];
return PushProcessor.cachedGlobToRegex[cacheKey];
}
/**