Skip to content

Finish implementing URL accessors like host, hostname, username, password #45030

@kross-italk

Description

Description

The window.URL object is available in Hermes but is otherwise un-implemented. All of the accessors have todo Errors.

constructor(url: string, base: string | URL) {
let baseUrl = null;
if (!base || validateBaseUrl(url)) {
this._url = url;
if (!this._url.endsWith('/')) {
this._url += '/';
}
} else {
if (typeof base === 'string') {
baseUrl = base;
if (!validateBaseUrl(baseUrl)) {
throw new TypeError(`Invalid base URL: ${baseUrl}`);
}
} else {
baseUrl = base.toString();
}
if (baseUrl.endsWith('/')) {
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
}
if (!url.startsWith('/')) {
url = `/${url}`;
}
if (baseUrl.endsWith(url)) {
url = '';
}
this._url = `${baseUrl}${url}`;
}
}
get hash(): string {
throw new Error('URL.hash is not implemented');
}
get host(): string {
throw new Error('URL.host is not implemented');
}
get hostname(): string {
throw new Error('URL.hostname is not implemented');
}
get href(): string {
return this.toString();
}
get origin(): string {
throw new Error('URL.origin is not implemented');
}
get password(): string {
throw new Error('URL.password is not implemented');
}
get pathname(): string {
throw new Error('URL.pathname not implemented');
}
get port(): string {
throw new Error('URL.port is not implemented');
}
get protocol(): string {
throw new Error('URL.protocol is not implemented');
}
get search(): string {
throw new Error('URL.search is not implemented');
}
get searchParams(): URLSearchParams {
if (this._searchParamsInstance == null) {
this._searchParamsInstance = new URLSearchParams();
}
return this._searchParamsInstance;
}
toJSON(): string {
return this.toString();
}
toString(): string {
if (this._searchParamsInstance === null) {
return this._url;
}
// $FlowFixMe[incompatible-use]
const instanceString = this._searchParamsInstance.toString();
const separator = this._url.indexOf('?') > -1 ? '&' : '?';
return this._url + separator + instanceString;
}
get username(): string {

Steps to reproduce

const urlObj = new window.URL( validUrlString );
urlObj.hostname
// Error: URL.hostname is not implemented

React Native Version

0.73.6

Affected Platforms

Runtime - Android

Output of npx react-native info

Reproducible on all systems. The code has a baked-in Error and no implementation.

Stacktrace or Logs

This error is located at:
    in SecureContext (created by Connect(SecureContext))
    in Connect(SecureContext) (created by App)
    in RCTView (created by View)
    in View (created by App)
    in App (created by RenderApp)
    in Provider (created by RenderApp)
    in RenderApp
    in RCTView (created by View)
    in View (created by AppContainer)
    in RCTView (created by View)
    in View (created by AppContainer)
    in AppContainer
    in app(RootComponent), js engine: hermes

Reproducer

https://snack.expo.dev/O0xP_moxLBNEHK9bXAXKl
It can't be reproduced in a browser folks. It can only be seen on hermes/Android so this is moot.

Screenshots and Videos

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions