From fb210ee232198d7108e65af986bb24845510d7a9 Mon Sep 17 00:00:00 2001 From: Angel Date: Wed, 14 Aug 2024 20:19:48 -0500 Subject: [PATCH] Url host hotnamame and hash password and userna --- packages/react-native/Libraries/Blob/URL.js | 51 ++++++++++++++----- .../Libraries/Blob/__tests__/URL-test.js | 33 ++++++++++++ 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/packages/react-native/Libraries/Blob/URL.js b/packages/react-native/Libraries/Blob/URL.js index db4bb2792a1e..8188bd3474a6 100644 --- a/packages/react-native/Libraries/Blob/URL.js +++ b/packages/react-native/Libraries/Blob/URL.js @@ -11,7 +11,29 @@ import type Blob from './Blob'; import NativeBlobModule from './NativeBlobModule'; - +import {match} from "hermes-transform/dist/traverse/esquery"; +import {tsThisType} from "@babel/types"; +function getDefaultPort(protocol) { + switch (protocol) { + case 'http': + return 80; + case 'https': + return 443; + case 'ftp': + return 21; + case 'smtp': + return 25; + case 'pop3': + return 110; + case 'imap': + return 143; + case 'mysql': + return 3306; + // Add more protocols and their default ports as needed + default: + return null; // Unknown protocol + } +} let BLOB_URL_PREFIX = null; if ( @@ -81,7 +103,7 @@ export class URL { let baseUrl = null; if (!base || validateBaseUrl(url)) { this._url = url; - if (!this._url.endsWith('/')) { + if (!this._url.endsWith('/') && this.search==="") { this._url += '/'; } } else { @@ -107,15 +129,15 @@ export class URL { } get hash(): string { - throw new Error('URL.hash is not implemented'); + return this.href.match(/#(.*)$/g)?this.href.match(/#(.*)$/g)[0]:"" } get host(): string { - throw new Error('URL.host is not implemented'); + return this.hostname+(this.port!==""?(":"+this.port):"") } get hostname(): string { - throw new Error('URL.hostname is not implemented'); + return this.href.match(/^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/(?:[^@]*@)?([^\/:]+)/)?this.href.match(/^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/(?:[^@]*@)?([^\/:]+)/)[1]:"" } get href(): string { @@ -123,27 +145,32 @@ export class URL { } get origin(): string { - throw new Error('URL.origin is not implemented'); + return this.protocol+"://"+this.host + } get password(): string { - throw new Error('URL.password is not implemented'); + return this.href.match( /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/[^:]+:([^@]+)@/)?this.href.match( /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/[^:]+:([^@]+)@/)[1]:""; } get pathname(): string { - throw new Error('URL.pathname not implemented'); + return this.href.match(/^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/[^\/]+(\/[^?#]*)/)?this.href.match(/^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/[^\/]+(\/[^?#]*)/)[1]:"" } get port(): string { - throw new Error('URL.port is not implemented'); + let match = this.href.match(/:[0-9]+/g) + if (match===null || parseInt(match[0].slice(1))===getDefaultPort(this.protocol)){ + return "" + } + return match[0].slice(1) } get protocol(): string { - throw new Error('URL.protocol is not implemented'); + return this.href.match(/^([a-z]+):/g)?this.href.match(/^([a-z]+):/g)[0].slice(0,-1):"" } get search(): string { - throw new Error('URL.search is not implemented'); + return this.href.match(/\?(.)+$/g)?this.href.match(/\?(.)+$/g)[0]:""; } get searchParams(): URLSearchParams { @@ -168,6 +195,6 @@ export class URL { } get username(): string { - throw new Error('URL.username is not implemented'); + return this.href.match(/^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/([^:]+)@/)?this.href.match(/^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/([^:]+)@/)[1]:""; } } diff --git a/packages/react-native/Libraries/Blob/__tests__/URL-test.js b/packages/react-native/Libraries/Blob/__tests__/URL-test.js index c317217c8fe7..85cee2cfcbcf 100644 --- a/packages/react-native/Libraries/Blob/__tests__/URL-test.js +++ b/packages/react-native/Libraries/Blob/__tests__/URL-test.js @@ -41,4 +41,37 @@ describe('URL', function () { const k = new URL('en-US/docs', 'https://developer.mozilla.org'); expect(k.href).toBe('https://developer.mozilla.org/en-US/docs'); }); + it("hash",()=>{ + const l = new URL("https://developer.mozilla.org"); + // expect(l.hostname).toBe("developer.mozilla.org") + const m = new URL('en-US/docs/#end',l); + expect(m.href).toBe("https://developer.mozilla.org/en-US/docs/#end") + expect(m.hash).toBe("#end") + }) + it("host is a hostname and port",()=>{ + // The port is not provided + const httpPort = "123"; + const protocol = "https" + const hostname = 'developer.mozilla.org' + const pathName = '/en-US/docs/Web/API/URL/host' + const n = new URL(`${protocol}://${hostname}:${httpPort}${pathName}`); + expect(n.hostname).toBe(`${hostname}`) + expect(n.origin).toBe(`${protocol}://${hostname}:${httpPort}`) + // the 443 is default https + const httpPort2 = "443"; + const r = new URL(`${protocol}://${hostname}:${httpPort2}${pathName}`); + expect(r.protocol).toBe(protocol) + expect(r.port).toBe("") + expect(r.host).toBe("developer.mozilla.org") + // the port is other + const p = new URL("https://developer.mozilla.org:4097/en-US/docs/Web/API/URL/host"); + expect(p.host).toBe("developer.mozilla.org:4097") + expect(p.pathname).toBe("/en-US/docs/Web/API/URL/host/") + const q = new URL("https://developer.mozilla.org:4097/en-US/docs/Web/API/URL/host?q=dad"); + expect(q.search).toBe("?q=dad") + expect(q.href).toBe("https://developer.mozilla.org:4097/en-US/docs/Web/API/URL/host?q=dad") + let u = new URL("https://username:password@developer.mozilla.org:443/en-US/docs/Web/API/URL/host") + expect(u.host).toBe("developer.mozilla.org") + expect(u.password).toBe("password") + }) });