| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,7 +59,26 @@ const userConditions = getOptionValue('--conditions'); | |||
| 59 | 59 | const DEFAULT_CONDITIONS = ObjectFreeze(['node', 'import', ...userConditions]); | |
| 60 | 60 | const DEFAULT_CONDITIONS_SET = new SafeSet(DEFAULT_CONDITIONS); | |
| 61 | 61 | ||
| 62 | + /** | ||
| 63 | + * @typedef {string | string[] | Record<string, unknown>} Exports | ||
| 64 | + * @typedef {'module' | 'commonjs'} PackageType | ||
| 65 | + * @typedef {{ | ||
| 66 | + * exports?: ExportConfig; | ||
| 67 | + * name?: string; | ||
| 68 | + * main?: string; | ||
| 69 | + * type?: PackageType; | ||
| 70 | + * }} PackageConfig | ||
| 71 | + */ | ||
| 72 | + | ||
| 62 | 73 | const emittedPackageWarnings = new SafeSet(); | |
| 74 | + | ||
| 75 | + /** | ||
| 76 | + * @param {string} match | ||
| 77 | + * @param {URL} pjsonUrl | ||
| 78 | + * @param {boolean} isExports | ||
| 79 | + * @param {string | URL | undefined} base | ||
| 80 | + * @returns {void} | ||
| 81 | + */ | ||
| 63 | 82 | function emitFolderMapDeprecation(match, pjsonUrl, isExports, base) { | |
| 64 | 83 | const pjsonPath = fileURLToPath(pjsonUrl); | |
| 65 | 84 | ||
@@ -76,6 +95,13 @@ function emitFolderMapDeprecation(match, pjsonUrl, isExports, base) { | |||
| 76 | 95 | ); | |
| 77 | 96 | } | |
| 78 | 97 | ||
| 98 | + /** | ||
| 99 | + * @param {URL} url | ||
| 100 | + * @param {URL} packageJSONUrl | ||
| 101 | + * @param {string | URL | undefined} base | ||
| 102 | + * @param {string} main | ||
| 103 | + * @returns | ||
| 104 | + */ | ||
| 79 | 105 | function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) { | |
| 80 | 106 | const { format } = defaultGetFormat(url); | |
| 81 | 107 | if (format !== 'module') | |
@@ -104,6 +130,10 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) { | |||
| 104 | 130 | ); | |
| 105 | 131 | } | |
| 106 | 132 | ||
| 133 | + /** | ||
| 134 | + * @param {string[]} [conditions] | ||
| 135 | + * @returns {Set<string>} | ||
| 136 | + */ | ||
| 107 | 137 | function getConditionsSet(conditions) { | |
| 108 | 138 | if (conditions !== undefined && conditions !== DEFAULT_CONDITIONS) { | |
| 109 | 139 | if (!ArrayIsArray(conditions)) { | |
@@ -118,9 +148,19 @@ function getConditionsSet(conditions) { | |||
| 118 | 148 | const realpathCache = new SafeMap(); | |
| 119 | 149 | const packageJSONCache = new SafeMap(); /* string -> PackageConfig */ | |
| 120 | 150 | ||
| 151 | + /** | ||
| 152 | + * @param {string | URL} path | ||
| 153 | + * @returns {import('fs').Stats} | ||
| 154 | + */ | ||
| 121 | 155 | const tryStatSync = | |
| 122 | 156 | (path) => statSync(path, { throwIfNoEntry: false }) ?? new Stats(); | |
| 123 | 157 | ||
| 158 | + /** | ||
| 159 | + * @param {string} path | ||
| 160 | + * @param {string} specifier | ||
| 161 | + * @param {string | URL | undefined} base | ||
| 162 | + * @returns {PackageConfig} | ||
| 163 | + */ | ||
| 124 | 164 | function getPackageConfig(path, specifier, base) { | |
| 125 | 165 | const existing = packageJSONCache.get(path); | |
| 126 | 166 | if (existing !== undefined) { | |
@@ -173,6 +213,10 @@ function getPackageConfig(path, specifier, base) { | |||
| 173 | 213 | return packageConfig; | |
| 174 | 214 | } | |
| 175 | 215 | ||
| 216 | + /** | ||
| 217 | + * @param {URL | string} resolved | ||
| 218 | + * @returns {PackageConfig} | ||
| 219 | + */ | ||
| 176 | 220 | function getPackageScopeConfig(resolved) { | |
| 177 | 221 | let packageJSONUrl = new URL('./package.json', resolved); | |
| 178 | 222 | while (true) { | |
@@ -205,19 +249,25 @@ function getPackageScopeConfig(resolved) { | |||
| 205 | 249 | } | |
| 206 | 250 | ||
| 207 | 251 | /** | |
| 208 | - * Legacy CommonJS main resolution: | ||
| 209 | - * 1. let M = pkg_url + (json main field) | ||
| 210 | - * 2. TRY(M, M.js, M.json, M.node) | ||
| 211 | - * 3. TRY(M/index.js, M/index.json, M/index.node) | ||
| 212 | - * 4. TRY(pkg_url/index.js, pkg_url/index.json, pkg_url/index.node) | ||
| 213 | - * 5. NOT_FOUND | ||
| 214 | 252 | * @param {string | URL} url | |
| 215 | 253 | * @returns {boolean} | |
| 216 | 254 | */ | |
| 217 | 255 | function fileExists(url) { | |
| 218 | 256 | return statSync(url, { throwIfNoEntry: false })?.isFile() ?? false; | |
| 219 | 257 | } | |
| 220 | 258 | ||
| 259 | + /** | ||
| 260 | + * Legacy CommonJS main resolution: | ||
| 261 | + * 1. let M = pkg_url + (json main field) | ||
| 262 | + * 2. TRY(M, M.js, M.json, M.node) | ||
| 263 | + * 3. TRY(M/index.js, M/index.json, M/index.node) | ||
| 264 | + * 4. TRY(pkg_url/index.js, pkg_url/index.json, pkg_url/index.node) | ||
| 265 | + * 5. NOT_FOUND | ||
| 266 | + * @param {URL} packageJSONUrl | ||
| 267 | + * @param {PackageConfig} packageConfig | ||
| 268 | + * @param {string | URL | undefined} base | ||
| 269 | + * @returns {URL} | ||
| 270 | + */ | ||
| 221 | 271 | function legacyMainResolve(packageJSONUrl, packageConfig, base) { | |
| 222 | 272 | let guess; | |
| 223 | 273 | if (packageConfig.main !== undefined) { | |
@@ -259,12 +309,21 @@ function legacyMainResolve(packageJSONUrl, packageConfig, base) { | |||
| 259 | 309 | fileURLToPath(new URL('.', packageJSONUrl)), fileURLToPath(base)); | |
| 260 | 310 | } | |
| 261 | 311 | ||
| 312 | + /** | ||
| 313 | + * @param {URL} search | ||
| 314 | + * @returns {URL | undefined} | ||
| 315 | + */ | ||
| 262 | 316 | function resolveExtensionsWithTryExactName(search) { | |
| 263 | 317 | if (fileExists(search)) return search; | |
| 264 | 318 | return resolveExtensions(search); | |
| 265 | 319 | } | |
| 266 | 320 | ||
| 267 | 321 | const extensions = ['.js', '.json', '.node', '.mjs']; | |
| 322 | + | ||
| 323 | + /** | ||
| 324 | + * @param {URL} search | ||
| 325 | + * @returns {URL | undefined} | ||
| 326 | + */ | ||
| 268 | 327 | function resolveExtensions(search) { | |
| 269 | 328 | for (let i = 0; i < extensions.length; i++) { | |
| 270 | 329 | const extension = extensions[i]; | |
@@ -274,6 +333,10 @@ function resolveExtensions(search) { | |||
| 274 | 333 | return undefined; | |
| 275 | 334 | } | |
| 276 | 335 | ||
| 336 | + /** | ||
| 337 | + * @param {URL} search | ||
| 338 | + * @returns {URL | undefined} | ||
| 339 | + */ | ||
| 277 | 340 | function resolveDirectoryEntry(search) { | |
| 278 | 341 | const dirPath = fileURLToPath(search); | |
| 279 | 342 | const pkgJsonPath = resolve(dirPath, 'package.json'); | |
@@ -291,6 +354,11 @@ function resolveDirectoryEntry(search) { | |||
| 291 | 354 | } | |
| 292 | 355 | ||
| 293 | 356 | const encodedSepRegEx = /%2F|%2C/i; | |
| 357 | + /** | ||
| 358 | + * @param {URL} resolved | ||
| 359 | + * @param {string | URL | undefined} base | ||
| 360 | + * @returns {URL | undefined} | ||
| 361 | + */ | ||
| 294 | 362 | function finalizeResolution(resolved, base) { | |
| 295 | 363 | if (RegExpPrototypeTest(encodedSepRegEx, resolved.pathname)) | |
| 296 | 364 | throw new ERR_INVALID_MODULE_SPECIFIER( | |
@@ -325,18 +393,35 @@ function finalizeResolution(resolved, base) { | |||
| 325 | 393 | return resolved; | |
| 326 | 394 | } | |
| 327 | 395 | ||
| 396 | + /** | ||
| 397 | + * @param {string} specifier | ||
| 398 | + * @param {URL} packageJSONUrl | ||
| 399 | + * @param {string | URL | undefined} base | ||
| 400 | + */ | ||
| 328 | 401 | function throwImportNotDefined(specifier, packageJSONUrl, base) { | |
| 329 | 402 | throw new ERR_PACKAGE_IMPORT_NOT_DEFINED( | |
| 330 | 403 | specifier, packageJSONUrl && fileURLToPath(new URL('.', packageJSONUrl)), | |
| 331 | 404 | fileURLToPath(base)); | |
| 332 | 405 | } | |
| 333 | 406 | ||
| 407 | + /** | ||
| 408 | + * @param {string} specifier | ||
| 409 | + * @param {URL} packageJSONUrl | ||
| 410 | + * @param {string | URL | undefined} base | ||
| 411 | + */ | ||
| 334 | 412 | function throwExportsNotFound(subpath, packageJSONUrl, base) { | |
| 335 | 413 | throw new ERR_PACKAGE_PATH_NOT_EXPORTED( | |
| 336 | 414 | fileURLToPath(new URL('.', packageJSONUrl)), subpath, | |
| 337 | 415 | base && fileURLToPath(base)); | |
| 338 | 416 | } | |
| 339 | 417 | ||
| 418 | + /** | ||
| 419 | + * | ||
| 420 | + * @param {string | URL} subpath | ||
| 421 | + * @param {URL} packageJSONUrl | ||
| 422 | + * @param {boolean} internal | ||
| 423 | + * @param {string | URL | undefined} base | ||
| 424 | + */ | ||
| 340 | 425 | function throwInvalidSubpath(subpath, packageJSONUrl, internal, base) { | |
| 341 | 426 | const reason = `request is not a valid subpath for the "${internal ? | |
| 342 | 427 | 'imports' : 'exports'}" resolution of ${fileURLToPath(packageJSONUrl)}`; | |
@@ -478,6 +563,13 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath, | |||
| 478 | 563 | base); | |
| 479 | 564 | } | |
| 480 | 565 | ||
| 566 | + /** | ||
| 567 | + * | ||
| 568 | + * @param {Exports} exports | ||
| 569 | + * @param {URL} packageJSONUrl | ||
| 570 | + * @param {string | URL | undefined} base | ||
| 571 | + * @returns | ||
| 572 | + */ | ||
| 481 | 573 | function isConditionalExportsMainSugar(exports, packageJSONUrl, base) { | |
| 482 | 574 | if (typeof exports === 'string' || ArrayIsArray(exports)) return true; | |
| 483 | 575 | if (typeof exports !== 'object' || exports === null) return false; | |
@@ -504,8 +596,8 @@ function isConditionalExportsMainSugar(exports, packageJSONUrl, base) { | |||
| 504 | 596 | /** | |
| 505 | 597 | * @param {URL} packageJSONUrl | |
| 506 | 598 | * @param {string} packageSubpath | |
| 507 | - * @param {object} packageConfig | ||
| 508 | - * @param {string} base | ||
| 599 | + * @param {PackageConfig} packageConfig | ||
| 600 | + * @param {string | URL | undefined} base | ||
| 509 | 601 | * @param {Set<string>} conditions | |
| 510 | 602 | * @returns {URL} | |
| 511 | 603 | */ | |
@@ -560,6 +652,12 @@ function packageExportsResolve( | |||
| 560 | 652 | throwExportsNotFound(packageSubpath, packageJSONUrl, base); | |
| 561 | 653 | } | |
| 562 | 654 | ||
| 655 | + /** | ||
| 656 | + * @param {string} name | ||
| 657 | + * @param {string | URL | undefined} base | ||
| 658 | + * @param {Set<string>} conditions | ||
| 659 | + * @returns | ||
| 660 | + */ | ||
| 563 | 661 | function packageImportsResolve(name, base, conditions) { | |
| 564 | 662 | if (name === '#' || StringPrototypeStartsWith(name, '#/')) { | |
| 565 | 663 | const reason = 'is not a valid internal imports specifier name'; | |
@@ -615,11 +713,20 @@ function packageImportsResolve(name, base, conditions) { | |||
| 615 | 713 | throwImportNotDefined(name, packageJSONUrl, base); | |
| 616 | 714 | } | |
| 617 | 715 | ||
| 716 | + /** | ||
| 717 | + * @param {URL} url | ||
| 718 | + * @returns {PackageType} | ||
| 719 | + */ | ||
| 618 | 720 | function getPackageType(url) { | |
| 619 | 721 | const packageConfig = getPackageScopeConfig(url); | |
| 620 | 722 | return packageConfig.type; | |
| 621 | 723 | } | |
| 622 | 724 | ||
| 725 | + /** | ||
| 726 | + * @param {string} specifier | ||
| 727 | + * @param {string | URL | undefined} base | ||
| 728 | + * @returns {{ packageName: string, packageSubpath: string, isScoped: boolean }} | ||
| 729 | + */ | ||
| 623 | 730 | function parsePackageName(specifier, base) { | |
| 624 | 731 | let separatorIndex = StringPrototypeIndexOf(specifier, '/'); | |
| 625 | 732 | let validPackageName = true; | |
@@ -659,7 +766,7 @@ function parsePackageName(specifier, base) { | |||
| 659 | 766 | ||
| 660 | 767 | /** | |
| 661 | 768 | * @param {string} specifier | |
| 662 | - * @param {URL} base | ||
| 769 | + * @param {string | URL | undefined} base | ||
| 663 | 770 | * @param {Set<string>} conditions | |
| 664 | 771 | * @returns {URL} | |
| 665 | 772 | */ | |
@@ -712,6 +819,10 @@ function packageResolve(specifier, base, conditions) { | |||
| 712 | 819 | throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base)); | |
| 713 | 820 | } | |
| 714 | 821 | ||
| 822 | + /** | ||
| 823 | + * @param {string} specifier | ||
| 824 | + * @returns {boolean} | ||
| 825 | + */ | ||
| 715 | 826 | function isBareSpecifier(specifier) { | |
| 716 | 827 | return specifier[0] && specifier[0] !== '/' && specifier[0] !== '.'; | |
| 717 | 828 | } | |
@@ -734,7 +845,7 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) { | |||
| 734 | 845 | ||
| 735 | 846 | /** | |
| 736 | 847 | * @param {string} specifier | |
| 737 | - * @param {URL} base | ||
| 848 | + * @param {string | URL | undefined} base | ||
| 738 | 849 | * @param {Set<string>} conditions | |
| 739 | 850 | * @returns {URL} | |
| 740 | 851 | */ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments