* Coerce a URL string to a filename. This is used by the ESM loader * to map ESM URLs to entries in the CJS module cache on a best-effort basis. * TODO(joyeecheung): this can be rather expensive, cache the result on the * ModuleWrap wherever we can. * @param {string|undefined} url URL to convert
(url)
| 358 | * @returns {string|undefined} |
| 359 | */ |
| 360 | function urlToFilename(url) { |
| 361 | if (url && StringPrototypeStartsWith(url, 'file://')) { |
| 362 | let urlObj; |
| 363 | try { |
| 364 | urlObj = new URL(url); |
| 365 | } catch { |
| 366 | // Not a proper URL, return as-is as the cache key. |
| 367 | return url; |
| 368 | } |
| 369 | try { |
| 370 | return fileURLToPath(urlObj); |
| 371 | } catch { |
| 372 | // This is generally only possible when the URL is provided by a custom loader. |
| 373 | // Just use the path and ignore whether it's absolute or not as there's no such |
| 374 | // requirement for CJS cache. |
| 375 | return urlObj.pathname; |
| 376 | } |
| 377 | } |
| 378 | // Not a file URL, return as-is. |
| 379 | return url; |
| 380 | } |
| 381 | |
| 382 | // Whether we have started executing any user-provided CJS code. |
| 383 | // This is set right before we call the wrapped CJS code (not after, |
no test coverage detected
searching dependent graphs…