Skip to content

Commit

Permalink
updated url.js
Browse files Browse the repository at this point in the history
  • Loading branch information
suryaansh001 committed Dec 20, 2024
1 parent 953889d commit 169876b
Showing 1 changed file with 53 additions and 39 deletions.
92 changes: 53 additions & 39 deletions packages/react-native/Libraries/Blob/URL.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ if (
// $FlowFixMe[unsafe-addition]
BLOB_URL_PREFIX = constants.BLOB_URI_SCHEME + ':';
if (typeof constants.BLOB_URI_HOST === 'string') {
BLOB_URL_PREFIX += `//${constants.BLOB_URI_HOST}/`;
BLOB_URL_PREFIX += //${constants.BLOB_URI_HOST}/;
}
}

/*
* To allow Blobs be accessed via `content://` URIs,
* you need to register `BlobProvider` as a ContentProvider in your app's `AndroidManifest.xml`:
* To allow Blobs be accessed via content:// URIs,
* you need to register BlobProvider as a ContentProvider in your app's AndroidManifest.xml:
*
* ```xml
*
xml
* <manifest>
* <application>
* <provider
Expand All @@ -41,15 +42,18 @@ if (
* />
* </application>
* </manifest>
* ```
* And then define the `blob_provider_authority` string in `res/values/strings.xml`.
*
* And then define the blob_provider_authority string in res/values/strings.xml.
* Use a dotted name that's entirely unique to your app:
*
* ```xml
*
xml
* <resources>
* <string name="blob_provider_authority">your.app.package.blobs</string>
* </resources>
* ```
*
*/

export {URLSearchParams} from './URLSearchParams';
Expand All @@ -61,22 +65,22 @@ function validateBaseUrl(url: string) {
);
}


export class URL {
_url: string;
_searchParamsInstance: ?URLSearchParams = null;

static createObjectURL(blob: Blob): string {
if (BLOB_URL_PREFIX === null) {
throw new Error('Cannot create URL for blob!');
_parsedUrl: URL | null = null;

// Utility to parse the URL once and reuse the parsed object
_ensureParsed() {
if (!this._parsedUrl) {
try {
this._parsedUrl = new window.URL(this._url);
} catch (error) {
throw new Error(Invalid URL: ${this._url});
}
}
return `${BLOB_URL_PREFIX}${blob.data.blobId}?offset=${blob.data.offset}&size=${blob.size}`;
}

static revokeObjectURL(url: string) {
// Do nothing.
}

// $FlowFixMe[missing-local-annot]
constructor(url: string, base: string | URL) {
let baseUrl = null;
if (!base || validateBaseUrl(url)) {
Expand All @@ -88,7 +92,7 @@ export class URL {
if (typeof base === 'string') {
baseUrl = base;
if (!validateBaseUrl(baseUrl)) {
throw new TypeError(`Invalid base URL: ${baseUrl}`);
throw new TypeError(Invalid base URL: ${baseUrl});
}
} else {
baseUrl = base.toString();
Expand All @@ -97,62 +101,77 @@ export class URL {
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
}
if (!url.startsWith('/')) {
url = `/${url}`;
url = /${url};
}
if (baseUrl.endsWith(url)) {
url = '';
}
this._url = `${baseUrl}${url}`;
this._url = ${baseUrl}${url};
}
}

get hash(): string {
throw new Error('URL.hash is not implemented');
this._ensureParsed();
return this._parsedUrl.hash;
}

get host(): string {
throw new Error('URL.host is not implemented');
this._ensureParsed();
return this._parsedUrl.host;
}

get hostname(): string {
throw new Error('URL.hostname is not implemented');
this._ensureParsed();
return this._parsedUrl.hostname;
}

get href(): string {
return this.toString();
}

get origin(): string {
throw new Error('URL.origin is not implemented');
this._ensureParsed();
return this._parsedUrl.origin;
}

get password(): string {
throw new Error('URL.password is not implemented');
this._ensureParsed();
return this._parsedUrl.password;
}

get pathname(): string {
throw new Error('URL.pathname not implemented');
this._ensureParsed();
return this._parsedUrl.pathname;
}

get port(): string {
throw new Error('URL.port is not implemented');
this._ensureParsed();
return this._parsedUrl.port;
}

get protocol(): string {
throw new Error('URL.protocol is not implemented');
this._ensureParsed();
return this._parsedUrl.protocol;
}

get search(): string {
throw new Error('URL.search is not implemented');
this._ensureParsed();
return this._parsedUrl.search;
}

get searchParams(): URLSearchParams {
if (this._searchParamsInstance == null) {
this._searchParamsInstance = new URLSearchParams();
if (!this._searchParamsInstance) {
this._ensureParsed();
this._searchParamsInstance = new URLSearchParams(this._parsedUrl.searchParams);
}
return this._searchParamsInstance;
}

get username(): string {
this._ensureParsed();
return this._parsedUrl.username;
}

toJSON(): string {
return this.toString();
}
Expand All @@ -161,13 +180,8 @@ export class URL {
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 {
throw new Error('URL.username is not implemented');
}
}
}

0 comments on commit 169876b

Please sign in to comment.