From d8d8b259fffbd01a03d5c5bf2768ee48797b68bb Mon Sep 17 00:00:00 2001 From: v1rtl Date: Tue, 16 Jul 2024 10:46:44 +0300 Subject: [PATCH 1/5] feat(wip): replace dequal, glob-to-regexp and bump path-to-regexp --- package-lock.json | 40 +++++++++++------------ packages/core/package.json | 6 ++-- packages/core/src/Matchers.js | 14 ++++---- packages/fetch-mock/package.json | 6 ++-- packages/fetch-mock/src/Route/matchers.js | 8 ++--- 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/package-lock.json b/package-lock.json index b43a6124..77cab102 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5333,7 +5333,7 @@ "version": "2.0.3", "resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.3.tgz", "integrity": "sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==", - "dev": true, + "license": "MIT", "engines": { "node": ">=6" } @@ -7338,11 +7338,6 @@ "node": ">=10.13.0" } }, - "node_modules/glob-to-regexp": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz", - "integrity": "sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw==" - }, "node_modules/glob/node_modules/brace-expansion": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", @@ -7410,6 +7405,12 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/globrex": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/globrex/-/globrex-0.1.2.tgz", + "integrity": "sha512-uHJgbwAMwNFf5mLst7IWLNg14x1CkeqglJb/K3doi4dw6q2IvAAmM/Y81kevy83wP+Sst+nutFTYOGg3d1lsxg==", + "license": "MIT" + }, "node_modules/gopd": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz", @@ -10283,11 +10284,6 @@ "integrity": "sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ==", "dev": true }, - "node_modules/lodash.isequal": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz", - "integrity": "sha512-pDo3lu8Jhfjqls6GkMgpahsF9kCyayhgykjyLMNFTKWrpVdAQtYyB4muAMWozBB4ig/dtWAmsMxLEI8wuz+DYQ==" - }, "node_modules/lodash.isplainobject": { "version": "4.0.6", "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", @@ -11449,9 +11445,13 @@ "dev": true }, "node_modules/path-to-regexp": { - "version": "2.4.0", - "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-2.4.0.tgz", - "integrity": "sha512-G6zHoVqC6GGTQkZwF4lkuEyMbVOjoBKAEybQUypI1WTkqinCOrq2x6U2+phkJ1XsEMTy4LjtwPI7HW+NVrRR2w==" + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-7.1.0.tgz", + "integrity": "sha512-ZToe+MbUF4lBqk6dV8GKot4DKfzrxXsplOddH8zN3YK+qw9/McvP7+4ICjZvOne0jQhN4eJwHsX6tT0Ns19fvw==", + "license": "MIT", + "engines": { + "node": ">=16" + } }, "node_modules/pathe": { "version": "1.1.2", @@ -14809,10 +14809,10 @@ "version": "0.1.0", "license": "ISC", "dependencies": { - "glob-to-regexp": "^0.4.1", + "dequal": "^2.0.3", + "globrex": "^0.1.2", "is-subset": "^0.1.1", - "lodash.isequal": "^4.5.0", - "path-to-regexp": "^2.4.0", + "path-to-regexp": "^7.1.0", "querystring": "^0.2.1" } }, @@ -14821,10 +14821,10 @@ "license": "MIT", "dependencies": { "debug": "^4.1.1", - "glob-to-regexp": "^0.4.0", + "dequal": "^2.0.3", + "globrex": "^0.1.2", "is-subset": "^0.1.1", - "lodash.isequal": "^4.5.0", - "path-to-regexp": "^2.2.1" + "path-to-regexp": "^7.1.0" }, "engines": { "node": ">=4.0.0" diff --git a/packages/core/package.json b/packages/core/package.json index 2b627cbe..4e776494 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -18,10 +18,10 @@ }, "homepage": "https://github.com/wheresrhys/fetch-mock#readme", "dependencies": { - "glob-to-regexp": "^0.4.1", + "dequal": "^2.0.3", + "globrex": "^0.1.2", "is-subset": "^0.1.1", - "lodash.isequal": "^4.5.0", - "path-to-regexp": "^2.4.0", + "path-to-regexp": "^7.1.0", "querystring": "^0.2.1" } } diff --git a/packages/core/src/Matchers.js b/packages/core/src/Matchers.js index dd23a534..54f2affd 100644 --- a/packages/core/src/Matchers.js +++ b/packages/core/src/Matchers.js @@ -2,11 +2,11 @@ /** @typedef {import('./Route').RouteConfig} RouteConfig */ /** @typedef {import('./RequestUtils').NormalizedRequestOptions} NormalizedRequestOptions */ /** @typedef {import('path-to-regexp').Key} Key */ -import glob from 'glob-to-regexp'; -import pathToRegexp from 'path-to-regexp'; +import glob from 'globrex'; +import { pathToRegexp } from 'path-to-regexp'; import querystring from 'querystring'; import isSubset from 'is-subset'; -import isEqual from 'lodash.isequal'; +import { dequal as isEqual } from 'dequal'; import { normalizeHeaders, getPath, @@ -54,7 +54,7 @@ const stringMatchers = { glob: (targetString) => { const urlRX = glob(targetString); - return (url) => urlRX.test(url); + return (url) => urlRX.regex.test(url); }, express: (targetString) => { const urlRX = pathToRegexp(targetString); @@ -129,14 +129,12 @@ const getParamsMatcher = ({ params: expectedParams, url: matcherUrl }) => { ); } const expectedKeys = Object.keys(expectedParams); - /** @type {Key[]} */ - const keys = []; - const re = pathToRegexp(matcherUrl.replace(/^express:/, ''), keys); + const re = pathToRegexp(matcherUrl.replace(/^express:/, '')); return (url) => { const vals = re.exec(getPath(url)) || []; vals.shift(); /** @type {Object.} */ - const params = keys.reduce( + const params = re.keys.reduce( (map, { name }, i) => vals[i] ? Object.assign(map, { [name]: vals[i] }) : map, {}, diff --git a/packages/fetch-mock/package.json b/packages/fetch-mock/package.json index f72107d4..a035f449 100644 --- a/packages/fetch-mock/package.json +++ b/packages/fetch-mock/package.json @@ -33,10 +33,10 @@ "homepage": "http://www.wheresrhys.co.uk/fetch-mock", "dependencies": { "debug": "^4.1.1", - "glob-to-regexp": "^0.4.0", + "dequal": "^2.0.3", + "globrex": "^0.1.2", "is-subset": "^0.1.1", - "lodash.isequal": "^4.5.0", - "path-to-regexp": "^2.2.1" + "path-to-regexp": "^7.1.0" }, "peerDependenciesMeta": { "node-fetch": { diff --git a/packages/fetch-mock/src/Route/matchers.js b/packages/fetch-mock/src/Route/matchers.js index db99bb1f..9598497d 100644 --- a/packages/fetch-mock/src/Route/matchers.js +++ b/packages/fetch-mock/src/Route/matchers.js @@ -1,7 +1,7 @@ -import glob from 'glob-to-regexp'; -import pathToRegexp from 'path-to-regexp'; +import glob from 'globrex'; +import { pathToRegexp } from 'path-to-regexp'; import isSubset from 'is-subset'; -import isEqual from 'lodash.isequal'; +import { dequal as isEqual } from 'dequal'; import { headers as headerUtils, getPath, @@ -24,7 +24,7 @@ const stringMatchers = { ), glob: (targetString) => { const urlRX = glob(targetString); - return debuggableUrlFunc((url) => urlRX.test(url)); + return debuggableUrlFunc((url) => urlRX.regex.test(url)); }, express: (targetString) => { const urlRX = pathToRegexp(targetString); From 65ef5678ba23c53d27f3b165fe25020d96c498db Mon Sep 17 00:00:00 2001 From: v1rtl Date: Tue, 16 Jul 2024 10:53:15 +0300 Subject: [PATCH 2/5] fix: failing tests --- packages/fetch-mock/src/Route/matchers.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/fetch-mock/src/Route/matchers.js b/packages/fetch-mock/src/Route/matchers.js index 9598497d..0c04b624 100644 --- a/packages/fetch-mock/src/Route/matchers.js +++ b/packages/fetch-mock/src/Route/matchers.js @@ -142,13 +142,12 @@ const getParamsMatcher = ({ params: expectedParams, url: matcherUrl }) => { } debug(' Expected path parameters:', expectedParams); const expectedKeys = Object.keys(expectedParams); - const keys = []; - const re = pathToRegexp(matcherUrl.replace(/^express:/, ''), keys); + const re = pathToRegexp(matcherUrl.replace(/^express:/, '')); return (url) => { debug('Attempting to match path parameters'); const vals = re.exec(getPath(url)) || []; vals.shift(); - const params = keys.reduce( + const params = re.keys.reduce( (map, { name }, i) => vals[i] ? Object.assign(map, { [name]: vals[i] }) : map, {}, From 4bf3e32f852ffc169ca354288eff86737e131480 Mon Sep 17 00:00:00 2001 From: v1rtl Date: Tue, 16 Jul 2024 11:06:55 +0300 Subject: [PATCH 3/5] fix: replace path-to-regexp with regexparam --- package-lock.json | 24 +++++++++++------------ packages/core/package.json | 4 ++-- packages/core/src/Matchers.js | 15 +++++++------- packages/fetch-mock/package.json | 2 +- packages/fetch-mock/src/Route/matchers.js | 14 ++++++------- 5 files changed, 29 insertions(+), 30 deletions(-) diff --git a/package-lock.json b/package-lock.json index 77cab102..491623fc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11444,15 +11444,6 @@ "integrity": "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==", "dev": true }, - "node_modules/path-to-regexp": { - "version": "7.1.0", - "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-7.1.0.tgz", - "integrity": "sha512-ZToe+MbUF4lBqk6dV8GKot4DKfzrxXsplOddH8zN3YK+qw9/McvP7+4ICjZvOne0jQhN4eJwHsX6tT0Ns19fvw==", - "license": "MIT", - "engines": { - "node": ">=16" - } - }, "node_modules/pathe": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/pathe/-/pathe-1.1.2.tgz", @@ -11940,6 +11931,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/regexparam": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/regexparam/-/regexparam-3.0.0.tgz", + "integrity": "sha512-RSYAtP31mvYLkAHrOlh25pCNQ5hWnT106VukGaaFfuJrZFkGRX5GhUAdPqpSDXxOhA2c4akmRuplv1mRqnBn6Q==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, "node_modules/regextras": { "version": "0.8.0", "resolved": "https://registry.npmjs.org/regextras/-/regextras-0.8.0.tgz", @@ -14812,8 +14812,8 @@ "dequal": "^2.0.3", "globrex": "^0.1.2", "is-subset": "^0.1.1", - "path-to-regexp": "^7.1.0", - "querystring": "^0.2.1" + "querystring": "^0.2.1", + "regexparam": "^3.0.0" } }, "packages/fetch-mock": { @@ -14824,7 +14824,7 @@ "dequal": "^2.0.3", "globrex": "^0.1.2", "is-subset": "^0.1.1", - "path-to-regexp": "^7.1.0" + "regexparam": "^3.0.0" }, "engines": { "node": ">=4.0.0" diff --git a/packages/core/package.json b/packages/core/package.json index 4e776494..9ee49285 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -21,7 +21,7 @@ "dequal": "^2.0.3", "globrex": "^0.1.2", "is-subset": "^0.1.1", - "path-to-regexp": "^7.1.0", - "querystring": "^0.2.1" + "querystring": "^0.2.1", + "regexparam": "^3.0.0" } } diff --git a/packages/core/src/Matchers.js b/packages/core/src/Matchers.js index 54f2affd..445bde6e 100644 --- a/packages/core/src/Matchers.js +++ b/packages/core/src/Matchers.js @@ -1,9 +1,8 @@ //@type-check /** @typedef {import('./Route').RouteConfig} RouteConfig */ /** @typedef {import('./RequestUtils').NormalizedRequestOptions} NormalizedRequestOptions */ -/** @typedef {import('path-to-regexp').Key} Key */ import glob from 'globrex'; -import { pathToRegexp } from 'path-to-regexp'; +import * as regexparam from 'regexparam'; import querystring from 'querystring'; import isSubset from 'is-subset'; import { dequal as isEqual } from 'dequal'; @@ -57,8 +56,8 @@ const stringMatchers = { return (url) => urlRX.regex.test(url); }, express: (targetString) => { - const urlRX = pathToRegexp(targetString); - return (url) => urlRX.test(getPath(url)); + const urlRX = regexparam.parse(targetString); + return (url) => urlRX.pattern.test(getPath(url)); }, path: (targetString) => (url) => getPath(url) === targetString, }; @@ -129,14 +128,14 @@ const getParamsMatcher = ({ params: expectedParams, url: matcherUrl }) => { ); } const expectedKeys = Object.keys(expectedParams); - const re = pathToRegexp(matcherUrl.replace(/^express:/, '')); + const re = regexparam.parse(matcherUrl.replace(/^express:/, '')); return (url) => { - const vals = re.exec(getPath(url)) || []; + const vals = re.pattern.exec(getPath(url)) || []; vals.shift(); /** @type {Object.} */ const params = re.keys.reduce( - (map, { name }, i) => - vals[i] ? Object.assign(map, { [name]: vals[i] }) : map, + (map, paramName, i) => + vals[i] ? Object.assign(map, { [paramName]: vals[i] }) : map, {}, ); return expectedKeys.every((key) => params[key] === expectedParams[key]); diff --git a/packages/fetch-mock/package.json b/packages/fetch-mock/package.json index a035f449..88be7e17 100644 --- a/packages/fetch-mock/package.json +++ b/packages/fetch-mock/package.json @@ -36,7 +36,7 @@ "dequal": "^2.0.3", "globrex": "^0.1.2", "is-subset": "^0.1.1", - "path-to-regexp": "^7.1.0" + "regexparam": "^3.0.0" }, "peerDependenciesMeta": { "node-fetch": { diff --git a/packages/fetch-mock/src/Route/matchers.js b/packages/fetch-mock/src/Route/matchers.js index 0c04b624..b9832c1e 100644 --- a/packages/fetch-mock/src/Route/matchers.js +++ b/packages/fetch-mock/src/Route/matchers.js @@ -1,5 +1,5 @@ import glob from 'globrex'; -import { pathToRegexp } from 'path-to-regexp'; +import * as regexparam from 'regexparam' import isSubset from 'is-subset'; import { dequal as isEqual } from 'dequal'; import { @@ -27,8 +27,8 @@ const stringMatchers = { return debuggableUrlFunc((url) => urlRX.regex.test(url)); }, express: (targetString) => { - const urlRX = pathToRegexp(targetString); - return debuggableUrlFunc((url) => urlRX.test(getPath(url))); + const urlRX = regexparam.parse(targetString); + return debuggableUrlFunc((url) => urlRX.pattern.test(getPath(url))); }, path: (targetString) => debuggableUrlFunc((url) => getPath(url) === targetString), @@ -142,14 +142,14 @@ const getParamsMatcher = ({ params: expectedParams, url: matcherUrl }) => { } debug(' Expected path parameters:', expectedParams); const expectedKeys = Object.keys(expectedParams); - const re = pathToRegexp(matcherUrl.replace(/^express:/, '')); + const re = regexparam.parse(matcherUrl.replace(/^express:/, '')); return (url) => { debug('Attempting to match path parameters'); - const vals = re.exec(getPath(url)) || []; + const vals = re.pattern.exec(getPath(url)) || []; vals.shift(); const params = re.keys.reduce( - (map, { name }, i) => - vals[i] ? Object.assign(map, { [name]: vals[i] }) : map, + (map, paramName, i) => + vals[i] ? Object.assign(map, { [paramName]: vals[i] }) : map, {}, ); debug(' Expected path parameters:', expectedParams); From ff9fee634db8b019f1384e44d13b4121bc2d62bb Mon Sep 17 00:00:00 2001 From: v1rtl Date: Tue, 16 Jul 2024 11:07:08 +0300 Subject: [PATCH 4/5] fix: wildcard import --- packages/fetch-mock/src/Route/matchers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fetch-mock/src/Route/matchers.js b/packages/fetch-mock/src/Route/matchers.js index b9832c1e..0a1f5d43 100644 --- a/packages/fetch-mock/src/Route/matchers.js +++ b/packages/fetch-mock/src/Route/matchers.js @@ -1,5 +1,5 @@ import glob from 'globrex'; -import * as regexparam from 'regexparam' +import * as regexparam from 'regexparam'; import isSubset from 'is-subset'; import { dequal as isEqual } from 'dequal'; import { From eb23636dd8e9a3d2c0bb60e2e0c4f0e9bdc727ae Mon Sep 17 00:00:00 2001 From: v1rtl Date: Wed, 17 Jul 2024 22:55:51 +0300 Subject: [PATCH 5/5] chore: bump engines --- package.json | 2 +- packages/fetch-mock/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index edd95820..8e3b32ca 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "homepage": "http://www.wheresrhys.co.uk/fetch-mock", "engines": { - "node": ">=4.0.0" + "node": ">=8.0.0" }, "workspaces": [ "packages/*", diff --git a/packages/fetch-mock/package.json b/packages/fetch-mock/package.json index 88be7e17..944f3406 100644 --- a/packages/fetch-mock/package.json +++ b/packages/fetch-mock/package.json @@ -44,6 +44,6 @@ } }, "engines": { - "node": ">=4.0.0" + "node": ">=8.0.0" } }