Skip to content

Commit d8ff9fd

Browse files
fix(sharing): reset lastIndex before testing a request filter
1 parent 385f33f commit d8ff9fd

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

‎lib/sharing/utils.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,12 @@ const matchesRequestFilters = (remainder, include, exclude) => {
440440
* @param {string | RegExp} pattern request filter
441441
* @returns {boolean} true, when the remainder matches
442442
*/
443-
const matches = (pattern) =>
444-
typeof pattern === "string"
445-
? remainder === pattern
446-
: pattern.test(remainder);
443+
const matches = (pattern) => {
444+
if (typeof pattern === "string") return remainder === pattern;
445+
// a global or sticky RegExp would resume from the previous match
446+
pattern.lastIndex = 0;
447+
return pattern.test(remainder);
448+
};
447449
if (include && include.request !== undefined && !matches(include.request)) {
448450
return false;
449451
}

‎test/configCases/sharing/share-filter/webpack.config.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ module.exports = {
1717
exclude: { version: "<2.0.0" }
1818
},
1919
"lib/": {
20-
include: { request: /^[ab]$/ },
20+
// "g" keeps lastIndex, but every request must be tested afresh
21+
include: { request: /^[ab]$/g },
2122
exclude: { request: "b" }
2223
},
2324
"./local": {

0 commit comments

Comments
 (0)