| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5a7491b commit 027841c
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -79,7 +79,7 @@ const { BuiltinModule } = require('internal/bootstrap/loaders'); | |||
| 79 | 79 | const { | |
| 80 | 80 | maybeCacheSourceMap, | |
| 81 | 81 | } = require('internal/source_map/source_map_cache'); | |
| 82 | - const { pathToFileURL, fileURLToPath, isURLInstance } = require('internal/url'); | ||
| 82 | + const { pathToFileURL, fileURLToPath, isURL } = require('internal/url'); | ||
| 83 | 83 | const { | |
| 84 | 84 | deprecate, | |
| 85 | 85 | emitExperimentalWarning, | |
@@ -1396,7 +1396,7 @@ const createRequireError = 'must be a file URL object, file URL string, or ' + | |||
| 1396 | 1396 | function createRequire(filename) { | |
| 1397 | 1397 | let filepath; | |
| 1398 | 1398 | ||
| 1399 | - if (isURLInstance(filename) || | ||
| 1399 | + if (isURL(filename) || | ||
| 1400 | 1400 | (typeof filename === 'string' && !path.isAbsolute(filename))) { | |
| 1401 | 1401 | try { | |
| 1402 | 1402 | filepath = fileURLToPath(filename); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,7 +21,7 @@ const { | |||
| 21 | 21 | ERR_INVALID_RETURN_PROPERTY_VALUE, | |
| 22 | 22 | ERR_INVALID_RETURN_VALUE, | |
| 23 | 23 | } = require('internal/errors').codes; | |
| 24 | - const { isURLInstance, URL } = require('internal/url'); | ||
| 24 | + const { isURL, URL } = require('internal/url'); | ||
| 25 | 25 | const { | |
| 26 | 26 | isAnyArrayBuffer, | |
| 27 | 27 | isArrayBufferView, | |
@@ -263,7 +263,7 @@ class Hooks { | |||
| 263 | 263 | if ( | |
| 264 | 264 | !isMain && | |
| 265 | 265 | typeof parentURL !== 'string' && | |
| 266 | - !isURLInstance(parentURL) | ||
| 266 | + !isURL(parentURL) | ||
| 267 | 267 | ) { | |
| 268 | 268 | throw new ERR_INVALID_ARG_TYPE( | |
| 269 | 269 | 'parentURL', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,7 @@ const { | |||
| 7 | 7 | ArrayPrototypePush, | |
| 8 | 8 | ArrayPrototypeReduce, | |
| 9 | 9 | ArrayPrototypeSlice, | |
| 10 | + Boolean, | ||
| 10 | 11 | Int8Array, | |
| 11 | 12 | IteratorPrototype, | |
| 12 | 13 | Number, | |
@@ -15,7 +16,6 @@ const { | |||
| 15 | 16 | ObjectGetOwnPropertySymbols, | |
| 16 | 17 | ObjectGetPrototypeOf, | |
| 17 | 18 | ObjectKeys, | |
| 18 | - ObjectPrototypeHasOwnProperty, | ||
| 19 | 19 | ReflectGetOwnPropertyDescriptor, | |
| 20 | 20 | ReflectOwnKeys, | |
| 21 | 21 | RegExpPrototypeSymbolReplace, | |
@@ -534,15 +534,27 @@ ObjectDefineProperties(URLSearchParams.prototype, { | |||
| 534 | 534 | }, | |
| 535 | 535 | }); | |
| 536 | 536 | ||
| 537 | + /** | ||
| 538 | + * Checks if a value has the shape of a WHATWG URL object. | ||
| 539 | + * | ||
| 540 | + * Using a symbol or instanceof would not be able to recognize URL objects | ||
| 541 | + * coming from other implementations (e.g. in Electron), so instead we are | ||
| 542 | + * checking some well known properties for a lack of a better test. | ||
| 543 | + * | ||
| 544 | + * @param {*} self | ||
| 545 | + * @returns {self is URL} | ||
| 546 | + */ | ||
| 537 | 547 | function isURL(self) { | |
| 538 | - return self != null && ObjectPrototypeHasOwnProperty(self, context); | ||
| 548 | + return Boolean(self?.href && self.origin); | ||
| 539 | 549 | } | |
| 540 | 550 | ||
| 541 | 551 | class URL { | |
| 552 | + #context = new URLContext(); | ||
| 553 | + #searchParams; | ||
| 554 | + | ||
| 542 | 555 | constructor(input, base = undefined) { | |
| 543 | 556 | // toUSVString is not needed. | |
| 544 | 557 | input = `${input}`; | |
| 545 | - this[context] = new URLContext(); | ||
| 546 | 558 | ||
| 547 | 559 | if (base !== undefined) { | |
| 548 | 560 | base = `${base}`; | |
@@ -558,11 +570,6 @@ class URL { | |||
| 558 | 570 | } | |
| 559 | 571 | ||
| 560 | 572 | [inspect.custom](depth, opts) { | |
| 561 | - if (this == null || | ||
| 562 | - ObjectGetPrototypeOf(this[context]) !== URLContext.prototype) { | ||
| 563 | - throw new ERR_INVALID_THIS('URL'); | ||
| 564 | - } | ||
| 565 | - | ||
| 566 | 573 | if (typeof depth === 'number' && depth < 0) | |
| 567 | 574 | return this; | |
| 568 | 575 | ||
@@ -583,182 +590,133 @@ class URL { | |||
| 583 | 590 | obj.hash = this.hash; | |
| 584 | 591 | ||
| 585 | 592 | if (opts.showHidden) { | |
| 586 | - obj[context] = this[context]; | ||
| 593 | + obj[context] = this.#context; | ||
| 587 | 594 | } | |
| 588 | 595 | ||
| 589 | 596 | return `${constructor.name} ${inspect(obj, opts)}`; | |
| 590 | 597 | } | |
| 591 | 598 | ||
| 592 | 599 | #onParseComplete = (href, origin, protocol, hostname, pathname, | |
| 593 | 600 | search, username, password, port, hash) => { | |
| 594 | - const ctx = this[context]; | ||
| 595 | - ctx.href = href; | ||
| 596 | - ctx.origin = origin; | ||
| 597 | - ctx.protocol = protocol; | ||
| 598 | - ctx.hostname = hostname; | ||
| 599 | - ctx.pathname = pathname; | ||
| 600 | - ctx.search = search; | ||
| 601 | - ctx.username = username; | ||
| 602 | - ctx.password = password; | ||
| 603 | - ctx.port = port; | ||
| 604 | - ctx.hash = hash; | ||
| 605 | - if (this[searchParams]) { | ||
| 606 | - this[searchParams][searchParams] = parseParams(search); | ||
| 601 | + this.#context.href = href; | ||
| 602 | + this.#context.origin = origin; | ||
| 603 | + this.#context.protocol = protocol; | ||
| 604 | + this.#context.hostname = hostname; | ||
| 605 | + this.#context.pathname = pathname; | ||
| 606 | + this.#context.search = search; | ||
| 607 | + this.#context.username = username; | ||
| 608 | + this.#context.password = password; | ||
| 609 | + this.#context.port = port; | ||
| 610 | + this.#context.hash = hash; | ||
| 611 | + if (this.#searchParams) { | ||
| 612 | + this.#searchParams[searchParams] = parseParams(search); | ||
| 607 | 613 | } | |
| 608 | 614 | }; | |
| 609 | 615 | ||
| 610 | 616 | toString() { | |
| 611 | - if (!isURL(this)) | ||
| 612 | - throw new ERR_INVALID_THIS('URL'); | ||
| 613 | - return this[context].href; | ||
| 617 | + return this.#context.href; | ||
| 614 | 618 | } | |
| 615 | 619 | ||
| 616 | 620 | get href() { | |
| 617 | - if (!isURL(this)) | ||
| 618 | - throw new ERR_INVALID_THIS('URL'); | ||
| 619 | - return this[context].href; | ||
| 621 | + return this.#context.href; | ||
| 620 | 622 | } | |
| 621 | 623 | ||
| 622 | 624 | set href(value) { | |
| 623 | - if (!isURL(this)) | ||
| 624 | - throw new ERR_INVALID_THIS('URL'); | ||
| 625 | - const valid = updateUrl(this[context].href, updateActions.kHref, `${value}`, this.#onParseComplete); | ||
| 625 | + const valid = updateUrl(this.#context.href, updateActions.kHref, `${value}`, this.#onParseComplete); | ||
| 626 | 626 | if (!valid) { throw ERR_INVALID_URL(`${value}`); } | |
| 627 | 627 | } | |
| 628 | 628 | ||
| 629 | 629 | // readonly | |
| 630 | 630 | get origin() { | |
| 631 | - if (!isURL(this)) | ||
| 632 | - throw new ERR_INVALID_THIS('URL'); | ||
| 633 | - return this[context].origin; | ||
| 631 | + return this.#context.origin; | ||
| 634 | 632 | } | |
| 635 | 633 | ||
| 636 | 634 | get protocol() { | |
| 637 | - if (!isURL(this)) | ||
| 638 | - throw new ERR_INVALID_THIS('URL'); | ||
| 639 | - return this[context].protocol; | ||
| 635 | + return this.#context.protocol; | ||
| 640 | 636 | } | |
| 641 | 637 | ||
| 642 | 638 | set protocol(value) { | |
| 643 | - if (!isURL(this)) | ||
| 644 | - throw new ERR_INVALID_THIS('URL'); | ||
| 645 | - updateUrl(this[context].href, updateActions.kProtocol, `${value}`, this.#onParseComplete); | ||
| 639 | + updateUrl(this.#context.href, updateActions.kProtocol, `${value}`, this.#onParseComplete); | ||
| 646 | 640 | } | |
| 647 | 641 | ||
| 648 | 642 | get username() { | |
| 649 | - if (!isURL(this)) | ||
| 650 | - throw new ERR_INVALID_THIS('URL'); | ||
| 651 | - return this[context].username; | ||
| 643 | + return this.#context.username; | ||
| 652 | 644 | } | |
| 653 | 645 | ||
| 654 | 646 | set username(value) { | |
| 655 | - if (!isURL(this)) | ||
| 656 | - throw new ERR_INVALID_THIS('URL'); | ||
| 657 | - updateUrl(this[context].href, updateActions.kUsername, `${value}`, this.#onParseComplete); | ||
| 647 | + updateUrl(this.#context.href, updateActions.kUsername, `${value}`, this.#onParseComplete); | ||
| 658 | 648 | } | |
| 659 | 649 | ||
| 660 | 650 | get password() { | |
| 661 | - if (!isURL(this)) | ||
| 662 | - throw new ERR_INVALID_THIS('URL'); | ||
| 663 | - return this[context].password; | ||
| 651 | + return this.#context.password; | ||
| 664 | 652 | } | |
| 665 | 653 | ||
| 666 | 654 | set password(value) { | |
| 667 | - if (!isURL(this)) | ||
| 668 | - throw new ERR_INVALID_THIS('URL'); | ||
| 669 | - updateUrl(this[context].href, updateActions.kPassword, `${value}`, this.#onParseComplete); | ||
| 655 | + updateUrl(this.#context.href, updateActions.kPassword, `${value}`, this.#onParseComplete); | ||
| 670 | 656 | } | |
| 671 | 657 | ||
| 672 | 658 | get host() { | |
| 673 | - if (!isURL(this)) | ||
| 674 | - throw new ERR_INVALID_THIS('URL'); | ||
| 675 | - const port = this[context].port; | ||
| 659 | + const port = this.#context.port; | ||
| 676 | 660 | const suffix = port.length > 0 ? `:${port}` : ''; | |
| 677 | - return this[context].hostname + suffix; | ||
| 661 | + return this.#context.hostname + suffix; | ||
| 678 | 662 | } | |
| 679 | 663 | ||
| 680 | 664 | set host(value) { | |
| 681 | - if (!isURL(this)) | ||
| 682 | - throw new ERR_INVALID_THIS('URL'); | ||
| 683 | - updateUrl(this[context].href, updateActions.kHost, `${value}`, this.#onParseComplete); | ||
| 665 | + updateUrl(this.#context.href, updateActions.kHost, `${value}`, this.#onParseComplete); | ||
| 684 | 666 | } | |
| 685 | 667 | ||
| 686 | 668 | get hostname() { | |
| 687 | - if (!isURL(this)) | ||
| 688 | - throw new ERR_INVALID_THIS('URL'); | ||
| 689 | - return this[context].hostname; | ||
| 669 | + return this.#context.hostname; | ||
| 690 | 670 | } | |
| 691 | 671 | ||
| 692 | 672 | set hostname(value) { | |
| 693 | - if (!isURL(this)) | ||
| 694 | - throw new ERR_INVALID_THIS('URL'); | ||
| 695 | - updateUrl(this[context].href, updateActions.kHostname, `${value}`, this.#onParseComplete); | ||
| 673 | + updateUrl(this.#context.href, updateActions.kHostname, `${value}`, this.#onParseComplete); | ||
| 696 | 674 | } | |
| 697 | 675 | ||
| 698 | 676 | get port() { | |
| 699 | - if (!isURL(this)) | ||
| 700 | - throw new ERR_INVALID_THIS('URL'); | ||
| 701 | - return this[context].port; | ||
| 677 | + return this.#context.port; | ||
| 702 | 678 | } | |
| 703 | 679 | ||
| 704 | 680 | set port(value) { | |
| 705 | - if (!isURL(this)) | ||
| 706 | - throw new ERR_INVALID_THIS('URL'); | ||
| 707 | - updateUrl(this[context].href, updateActions.kPort, `${value}`, this.#onParseComplete); | ||
| 681 | + updateUrl(this.#context.href, updateActions.kPort, `${value}`, this.#onParseComplete); | ||
| 708 | 682 | } | |
| 709 | 683 | ||
| 710 | 684 | get pathname() { | |
| 711 | - if (!isURL(this)) | ||
| 712 | - throw new ERR_INVALID_THIS('URL'); | ||
| 713 | - return this[context].pathname; | ||
| 685 | + return this.#context.pathname; | ||
| 714 | 686 | } | |
| 715 | 687 | ||
| 716 | 688 | set pathname(value) { | |
| 717 | - if (!isURL(this)) | ||
| 718 | - throw new ERR_INVALID_THIS('URL'); | ||
| 719 | - updateUrl(this[context].href, updateActions.kPathname, `${value}`, this.#onParseComplete); | ||
| 689 | + updateUrl(this.#context.href, updateActions.kPathname, `${value}`, this.#onParseComplete); | ||
| 720 | 690 | } | |
| 721 | 691 | ||
| 722 | 692 | get search() { | |
| 723 | - if (!isURL(this)) | ||
| 724 | - throw new ERR_INVALID_THIS('URL'); | ||
| 725 | - return this[context].search; | ||
| 693 | + return this.#context.search; | ||
| 726 | 694 | } | |
| 727 | 695 | ||
| 728 | 696 | set search(value) { | |
| 729 | - if (!isURL(this)) | ||
| 730 | - throw new ERR_INVALID_THIS('URL'); | ||
| 731 | - updateUrl(this[context].href, updateActions.kSearch, toUSVString(value), this.#onParseComplete); | ||
| 697 | + updateUrl(this.#context.href, updateActions.kSearch, toUSVString(value), this.#onParseComplete); | ||
| 732 | 698 | } | |
| 733 | 699 | ||
| 734 | 700 | // readonly | |
| 735 | 701 | get searchParams() { | |
| 736 | - if (!isURL(this)) | ||
| 737 | - throw new ERR_INVALID_THIS('URL'); | ||
| 738 | 702 | // Create URLSearchParams on demand to greatly improve the URL performance. | |
| 739 | - if (this[searchParams] == null) { | ||
| 740 | - this[searchParams] = new URLSearchParams(this[context].search); | ||
| 741 | - this[searchParams][context] = this; | ||
| 703 | + if (this.#searchParams == null) { | ||
| 704 | + this.#searchParams = new URLSearchParams(this.#context.search); | ||
| 705 | + this.#searchParams[context] = this; | ||
| 742 | 706 | } | |
| 743 | - return this[searchParams]; | ||
| 707 | + return this.#searchParams; | ||
| 744 | 708 | } | |
| 745 | 709 | ||
| 746 | 710 | get hash() { | |
| 747 | - if (!isURL(this)) | ||
| 748 | - throw new ERR_INVALID_THIS('URL'); | ||
| 749 | - return this[context].hash; | ||
| 711 | + return this.#context.hash; | ||
| 750 | 712 | } | |
| 751 | 713 | ||
| 752 | 714 | set hash(value) { | |
| 753 | - if (!isURL(this)) | ||
| 754 | - throw new ERR_INVALID_THIS('URL'); | ||
| 755 | - updateUrl(this[context].href, updateActions.kHash, `${value}`, this.#onParseComplete); | ||
| 715 | + updateUrl(this.#context.href, updateActions.kHash, `${value}`, this.#onParseComplete); | ||
| 756 | 716 | } | |
| 757 | 717 | ||
| 758 | 718 | toJSON() { | |
| 759 | - if (!isURL(this)) | ||
| 760 | - throw new ERR_INVALID_THIS('URL'); | ||
| 761 | - return this[context].href; | ||
| 719 | + return this.#context.href; | ||
| 762 | 720 | } | |
| 763 | 721 | ||
| 764 | 722 | static createObjectURL(obj) { | |
@@ -1206,7 +1164,7 @@ function getPathFromURLPosix(url) { | |||
| 1206 | 1164 | function fileURLToPath(path) { | |
| 1207 | 1165 | if (typeof path === 'string') | |
| 1208 | 1166 | path = new URL(path); | |
| 1209 | - else if (!isURLInstance(path)) | ||
| 1167 | + else if (!isURL(path)) | ||
| 1210 | 1168 | throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path); | |
| 1211 | 1169 | if (path.protocol !== 'file:') | |
| 1212 | 1170 | throw new ERR_INVALID_URL_SCHEME('file'); | |
@@ -1282,12 +1240,8 @@ function pathToFileURL(filepath) { | |||
| 1282 | 1240 | return outURL; | |
| 1283 | 1241 | } | |
| 1284 | 1242 | ||
| 1285 | - function isURLInstance(fileURLOrPath) { | ||
| 1286 | - return fileURLOrPath != null && fileURLOrPath.href && fileURLOrPath.origin; | ||
| 1287 | - } | ||
| 1288 | - | ||
| 1289 | 1243 | function toPathIfFileURL(fileURLOrPath) { | |
| 1290 | - if (!isURLInstance(fileURLOrPath)) | ||
| 1244 | + if (!isURL(fileURLOrPath)) | ||
| 1291 | 1245 | return fileURLOrPath; | |
| 1292 | 1246 | return fileURLToPath(fileURLOrPath); | |
| 1293 | 1247 | } | |
@@ -1297,7 +1251,6 @@ module.exports = { | |||
| 1297 | 1251 | fileURLToPath, | |
| 1298 | 1252 | pathToFileURL, | |
| 1299 | 1253 | toPathIfFileURL, | |
| 1300 | - isURLInstance, | ||
| 1301 | 1254 | URL, | |
| 1302 | 1255 | URLSearchParams, | |
| 1303 | 1256 | domainToASCII, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,7 +55,7 @@ const { | |||
| 55 | 55 | WritableWorkerStdio, | |
| 56 | 56 | } = workerIo; | |
| 57 | 57 | const { deserializeError } = require('internal/error_serdes'); | |
| 58 | - const { fileURLToPath, isURLInstance, pathToFileURL } = require('internal/url'); | ||
| 58 | + const { fileURLToPath, isURL, pathToFileURL } = require('internal/url'); | ||
| 59 | 59 | const { kEmptyObject } = require('internal/util'); | |
| 60 | 60 | const { validateArray } = require('internal/validators'); | |
| 61 | 61 | ||
@@ -148,13 +148,13 @@ class Worker extends EventEmitter { | |||
| 148 | 148 | } | |
| 149 | 149 | url = null; | |
| 150 | 150 | doEval = 'classic'; | |
| 151 | - } else if (isURLInstance(filename) && filename.protocol === 'data:') { | ||
| 151 | + } else if (isURL(filename) && filename.protocol === 'data:') { | ||
| 152 | 152 | url = null; | |
| 153 | 153 | doEval = 'module'; | |
| 154 | 154 | filename = `import ${JSONStringify(`${filename}`)}`; | |
| 155 | 155 | } else { | |
| 156 | 156 | doEval = false; | |
| 157 | - if (isURLInstance(filename)) { | ||
| 157 | + if (isURL(filename)) { | ||
| 158 | 158 | url = filename; | |
| 159 | 159 | filename = fileURLToPath(filename); | |
| 160 | 160 | } else if (typeof filename !== 'string') { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -61,7 +61,7 @@ assert.strictEqual( | |||
| 61 | 61 | ||
| 62 | 62 | assert.strictEqual( | |
| 63 | 63 | util.inspect({ a: url }, { depth: 0 }), | |
| 64 | - '{ a: [URL] }'); | ||
| 64 | + '{ a: URL {} }'); | ||
| 65 | 65 | ||
| 66 | 66 | class MyURL extends URL {} | |
| 67 | 67 | assert(util.inspect(new MyURL(url.href)).startsWith('MyURL {')); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments