@@ -66,18 +66,20 @@ function patchedModuleLoad(moduleName) {
6666 // However, when an ESM library imports a CommonJS package, our requireHook is triggered.
6767 // For native ESM libraries the iitmHook is triggered.
6868 if ( path . isAbsolute ( moduleName ) && [ '.node' , '.json' , '.ts' ] . indexOf ( path . extname ( moduleName ) ) === - 1 ) {
69+ // Normalize Windows paths (backslashes) to forward slashes for regex matching
70+ const normalizedModuleName = moduleName . replace ( / \\ / g, '/' ) ;
6971 // EDGE CASE for ESM: mysql2/promise.js
70- if ( moduleName . indexOf ( 'node_modules/mysql2/promise.js' ) !== - 1 ) {
72+ if ( normalizedModuleName . indexOf ( 'node_modules/mysql2/promise.js' ) !== - 1 ) {
7173 moduleName = 'mysql2/promise' ;
7274 } else {
7375 // e.g. path is node_modules/@elastic /elasicsearch/index.js
74- let match = moduleName . match ( / n o d e _ m o d u l e s \/ ( @ .* ?(? = \/ ) \/ .* ?(? = \/ ) ) / ) ;
76+ let match = normalizedModuleName . match ( / n o d e _ m o d u l e s \/ ( @ .* ?(? = \/ ) \/ .* ?(? = \/ ) ) / ) ;
7577
7678 if ( match && match . length > 1 ) {
7779 moduleName = match [ 1 ] ;
7880 } else {
7981 // e.g. path is node_modules/mysql/lib/index.js
80- match = moduleName . match ( / n o d e _ m o d u l e s \/ ( .* ?(? = \/ ) ) / ) ;
82+ match = normalizedModuleName . match ( / n o d e _ m o d u l e s \/ ( .* ?(? = \/ ) ) / ) ;
8183
8284 if ( match && match . length > 1 ) {
8385 moduleName = match [ 1 ] ;
@@ -145,8 +147,11 @@ function patchedModuleLoad(moduleName) {
145147 }
146148
147149 if ( ! cacheEntry . byFileNamePatternTransformersApplied ) {
150+ // Normalize Windows paths (backslashes) to forward slashes for regex pattern matching
151+ // This ensures patterns with forward slashes (like /\/mongodb-core\/lib\/connection\/pool\.js/) work on Windows
152+ const normalizedFilename = filename . replace ( / \\ / g, '/' ) ;
148153 for ( let i = 0 ; i < byFileNamePatternTransformers . length ; i ++ ) {
149- if ( byFileNamePatternTransformers [ i ] . pattern . test ( filename ) ) {
154+ if ( byFileNamePatternTransformers [ i ] . pattern . test ( normalizedFilename ) ) {
150155 cacheEntry . moduleExports =
151156 byFileNamePatternTransformers [ i ] . fn ( cacheEntry . moduleExports , filename ) || cacheEntry . moduleExports ;
152157 }
@@ -191,15 +196,27 @@ exports.buildFileNamePattern = function buildFileNamePattern(arr) {
191196 return new RegExp ( `${ arr . reduce ( buildFileNamePatternReducer , '' ) } $` ) ;
192197} ;
193198
199+ /**
200+ * Escapes special regex characters in a string
201+ * @param {string } str
202+ * @returns {string }
203+ */
204+ function escapeRegex ( str ) {
205+ return str . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ;
206+ }
207+
194208/**
195209 * @param {string } agg
196210 * @param {string } pathSegment
197211 * @returns {string }
198212 */
199213function buildFileNamePatternReducer ( agg , pathSegment ) {
200214 if ( agg . length > 0 ) {
201- agg += `\\${ path . sep } ` ;
215+ // Always use forward slashes in patterns since we normalize filenames to forward slashes
216+ // This ensures patterns work consistently on both Windows and Unix systems
217+ agg += '\\/' ;
202218 }
203- agg += pathSegment ;
219+ // Escape special regex characters in path segments (e.g., '.' in 'express.js' should be '\.')
220+ agg += escapeRegex ( pathSegment ) ;
204221 return agg ;
205222}
0 commit comments