From ec18df2c2ae3a2f870e4ed23b3ad31c03b2d4c9c Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 5 Dec 2018 18:01:12 -0700 Subject: [PATCH 1/2] Move glob regex utilities out of the pushprocessor and into a more generic place --- src/pushprocessor.js | 23 +++-------------------- src/utils.js | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/pushprocessor.js b/src/pushprocessor.js index 1fdbdfe8414..712c4539939 100644 --- a/src/pushprocessor.js +++ b/src/pushprocessor.js @@ -14,6 +14,9 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ + +import {escapeRegExp, globToRegexp} from "./utils"; + /** * @module pushprocessor */ @@ -26,10 +29,6 @@ const RULEKINDS_IN_ORDER = ['override', 'content', 'room', 'sender', 'underride' * @param {Object} client The Matrix client object to use */ function PushProcessor(client) { - const escapeRegExp = function(string) { - return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); - }; - const cachedGlobToRegex = { // $glob: RegExp, }; @@ -244,22 +243,6 @@ function PushProcessor(client) { return cachedGlobToRegex[glob]; }; - const globToRegexp = function(glob) { - // From - // https://github.com/matrix-org/synapse/blob/abbee6b29be80a77e05730707602f3bbfc3f38cb/synapse/push/__init__.py#L132 - // Because micromatch is about 130KB with dependencies, - // and minimatch is not much better. - let pat = escapeRegExp(glob); - pat = pat.replace(/\\\*/g, '.*'); - pat = pat.replace(/\?/g, '.'); - pat = pat.replace(/\\\[(!|)(.*)\\]/g, function(match, p1, p2, offset, string) { - const first = p1 && '^' || ''; - const second = p2.replace(/\\\-/, '-'); - return '[' + first + second + ']'; - }); - return pat; - }; - const valueForDottedKey = function(key, ev) { const parts = key.split('.'); let val; diff --git a/src/utils.js b/src/utils.js index 1587c64a373..ac3bd741e21 100644 --- a/src/utils.js +++ b/src/utils.js @@ -672,3 +672,27 @@ module.exports.removeHiddenChars = function(str) { return str.normalize('NFD').replace(removeHiddenCharsRegex, ''); }; const removeHiddenCharsRegex = /[\u200B-\u200D\u0300-\u036f\uFEFF\s]/g; + +function escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); +} +module.exports.escapeRegExp = escapeRegExp; + +module.exports.globToRegexp = function(glob, extended) { + extended = typeof(extended) === 'boolean' ? extended : true; + // From + // https://github.com/matrix-org/synapse/blob/abbee6b29be80a77e05730707602f3bbfc3f38cb/synapse/push/__init__.py#L132 + // Because micromatch is about 130KB with dependencies, + // and minimatch is not much better. + let pat = escapeRegExp(glob); + pat = pat.replace(/\\\*/g, '.*'); + pat = pat.replace(/\?/g, '.'); + if (extended) { + pat = pat.replace(/\\\[(!|)(.*)\\]/g, function (match, p1, p2, offset, string) { + const first = p1 && '^' || ''; + const second = p2.replace(/\\\-/, '-'); + return '[' + first + second + ']'; + }); + } + return pat; +}; \ No newline at end of file From 84ab0fde51c67e22bb441297eb8b354dcac62571 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 5 Dec 2018 18:13:28 -0700 Subject: [PATCH 2/2] Appease the linter --- src/utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils.js b/src/utils.js index ac3bd741e21..8ee81ed3e11 100644 --- a/src/utils.js +++ b/src/utils.js @@ -688,11 +688,11 @@ module.exports.globToRegexp = function(glob, extended) { pat = pat.replace(/\\\*/g, '.*'); pat = pat.replace(/\?/g, '.'); if (extended) { - pat = pat.replace(/\\\[(!|)(.*)\\]/g, function (match, p1, p2, offset, string) { + pat = pat.replace(/\\\[(!|)(.*)\\]/g, function(match, p1, p2, offset, string) { const first = p1 && '^' || ''; const second = p2.replace(/\\\-/, '-'); return '[' + first + second + ']'; }); } return pat; -}; \ No newline at end of file +};