### Install react-native-url-polyfill Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Install the library using yarn or npm. ```bash yarn add react-native-url-polyfill # or npm install react-native-url-polyfill ``` -------------------------------- ### Start Metro Bundler Source: https://github.com/charpeni/react-native-url-polyfill/blob/main/platforms/react-native/0.81/README.md Run this command from the root of your React Native project to start the Metro dev server. This is essential for building and running your app. ```sh # Using npm npm start # OR using Yarn yarn start ``` -------------------------------- ### Manually setup URL polyfill Source: https://github.com/charpeni/react-native-url-polyfill/blob/main/README.md Import and call setupURLPolyfill() in your code when you are ready to apply the polyfill. This offers more control over when the polyfill is activated. ```javascript import { setupURLPolyfill } from 'react-native-url-polyfill'; setupURLPolyfill(); ``` -------------------------------- ### Install react-native-url-polyfill with Yarn Source: https://github.com/charpeni/react-native-url-polyfill/blob/main/README.md Use this command to add the polyfill to your project dependencies. ```bash yarn add react-native-url-polyfill ``` -------------------------------- ### Build and Run iOS App Source: https://github.com/charpeni/react-native-url-polyfill/blob/main/platforms/react-native/0.81/README.md Use this command to build and run the iOS application. Ensure Metro is running and CocoaPods dependencies are installed. ```sh # Using npm npm run ios # OR using Yarn yarn ios ``` -------------------------------- ### Install CocoaPods Dependencies for iOS Source: https://github.com/charpeni/react-native-url-polyfill/blob/main/platforms/react-native/0.81/README.md Before running the iOS app, install CocoaPods dependencies. Run 'bundle install' once to install CocoaPods itself, and 'bundle exec pod install' every time native dependencies are updated. ```sh bundle install ``` ```sh bundle exec pod install ``` -------------------------------- ### URL.createObjectURL(blob) Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Creates a Blob URL from a Blob object. This URL can be used to reference blob data, for example, in media elements. Requires specific Android configurations for content:// URI support. ```APIDOC ## `URL.createObjectURL(blob)` — Blob URL creation Implemented by reading the native `BlobModule` constants (`BLOB_URI_SCHEME`, `BLOB_URI_HOST`) from React Native's `NativeModules`. Constructs a Blob URL in the form `:///?offset=&size=`. Requires `BlobProvider` to be registered as an Android `ContentProvider` for `content://` URI support. ```javascript // android/app/src/main/AndroidManifest.xml — required for content:// Blob URLs on Android: // // android/app/src/main/res/values/strings.xml: // com.myapp.blobs import { URL } from 'react-native-url-polyfill'; async function uploadAndGetBlobUrl(uri) { const response = await fetch(uri); const blob = await response.blob(); try { const blobUrl = URL.createObjectURL(blob); // blobUrl => e.g., "blob://localhost/abc-123?offset=0&size=4096" console.log(blobUrl); // Use blobUrl in an Image, Video, WebView, etc. // When done, revoke (no-op in this polyfill but good practice): URL.revokeObjectURL(blobUrl); } catch (e) { // Throws if BlobModule is unavailable console.error('Blob URLs not supported:', e.message); } } ``` ``` -------------------------------- ### URLSearchParams Encoding Behavior Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Illustrates how `URLSearchParams` automatically encodes special characters in parameter values, such as the '+' sign in phone numbers. The decoded value is retrieved correctly using `get()`. ```javascript // Encoding behavior const enc = new URLSearchParams(); enc.set('phone', '+15555555555'); console.log(enc.toString()); // => "phone=%2B15555555555" console.log(enc.get('phone')); // => "+15555555555" ``` -------------------------------- ### Manual Global Polyfill with setupURLPolyfill() Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Call the setupURLPolyfill() function from 'react-native-url-polyfill' to manually patch globalThis.URL and globalThis.URLSearchParams. This is useful for conditional or deferred polyfill application. Confirm the polyfill is active by checking globalThis.REACT_NATIVE_URL_POLYFILL. ```javascript import { setupURLPolyfill } from 'react-native-url-polyfill'; // Called lazily — e.g., before making the first network request function initApp() { setupURLPolyfill(); // Confirm polyfill is active console.log(globalThis.REACT_NATIVE_URL_POLYfill); // => "react-native-url-polyfill@3.0.0" // Global URL now works correctly const base = new URL('https://api.example.com'); console.log(base.href); // => "https://api.example.com/" // React Native regression fix: relative URL resolution const about = new URL('about', 'https://www.mozilla.org'); console.log(about.href); // => "https://www.mozilla.org/about" } ``` -------------------------------- ### Construct and Access URL Properties Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Demonstrates basic URL construction and accessing its various properties. Ensure the `URL` class is imported from `react-native-url-polyfill`. ```javascript import { URL } from 'react-native-url-polyfill'; // Basic construction and property access const url = new URL('https://user:pass@api.example.com:8080/v2/items?sort=asc&limit=10#results'); console.log(url.protocol); // => "https:" console.log(url.username); // => "user" console.log(url.password); // => "pass" console.log(url.hostname); // => "api.example.com" console.log(url.port); // => "8080" console.log(url.pathname); // => "/v2/items" console.log(url.search); // => "?sort=asc&limit=10" console.log(url.hash); // => "#results" console.log(url.origin); // => "https://api.example.com:8080" ``` -------------------------------- ### TypeScript Support with URL Polyfill Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Provides TypeScript types for URL and URLSearchParams, bundled with the library. No extra @types packages are needed. The setupURLPolyfill function can be used for deferred global patching. ```typescript // No extra @types needed — types are bundled import { URL, URLSearchParams, setupURLPolyfill } from 'react-native-url-polyfill'; setupURLPolyfill(); // void const url: URL = new URL('https://api.example.com/data'); const params: URLSearchParams = new URLSearchParams('key=value'); // Or with the /auto import (no explicit type import needed): // import 'react-native-url-polyfill/auto'; // const url = new URL('https://...'); // globalThis.URL is typed via lib.dom.d.ts ``` -------------------------------- ### TypeScript Support Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt The library includes TypeScript definitions, allowing for type-safe usage of URL and URLSearchParams without additional type packages. It also provides a `setupURLPolyfill` function for deferred global patching. ```APIDOC ## TypeScript support The library ships a `index.d.ts` that re-exports `URL` and `URLSearchParams` types from Node's built-in `url` module and declares the `setupURLPolyfill` function. Consumption is type-safe without extra `@types` packages. ```typescript // No extra @types needed — types are bundled import { URL, URLSearchParams, setupURLPolyfill } from 'react-native-url-polyfill'; setupURLPolyfill(); // void const url: URL = new URL('https://api.example.com/data'); const params: URLSearchParams = new URLSearchParams('key=value'); // Or with the /auto import (no explicit type import needed): // import 'react-native-url-polyfill/auto'; // const url = new URL('https://...'); // globalThis.URL is typed via lib.dom.d.ts ``` ``` -------------------------------- ### Serialize URL to String Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Demonstrates different methods for serializing a URL object back into a string representation. ```javascript // Serialization console.log(url.toString()); // same as url.href console.log(url.toJSON()); // same as url.href console.log(JSON.stringify(url)); // => '"https://user:pass@api.example.com:8080/v3/items?sort=desc#results"' ``` -------------------------------- ### Run Tests After Updating WPT Data Source: https://github.com/charpeni/react-native-url-polyfill/blob/main/js/__tests__/wpt/README.md This command-line instruction executes the test suite after updating the test data. It runs Jest tests, which will automatically incorporate new tests, flag Unicode-related failures as skipped, and identify non-Unicode failures as needing implementation work. ```bash yarn test ``` -------------------------------- ### Iterate Over URLSearchParams Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Shows how to iterate over `URLSearchParams` using `forEach`, `keys`, `values`, and `entries`. The `Symbol.iterator` is also supported. ```javascript // Iteration const p = new URLSearchParams('a=1&b=2&c=3'); p.forEach((value, key) => console.log(`${key}=${value}`)); // => a=1, b=2, c=3 console.log([...p.keys()]); // => ["a", "b", "c"] console.log([...p.values()]); // => ["1", "2", "3"] console.log([...p.entries()]); // => [["a","1"],["b","2"],["c","3"]] ``` -------------------------------- ### Handle Invalid URL Construction Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Demonstrates how the `URL` constructor throws a `TypeError` when provided with an invalid URL string. ```javascript // Error on invalid URL try { new URL('not a valid url'); } catch (e) { console.log(e instanceof TypeError); // => true } ``` -------------------------------- ### Automatically apply URL polyfill Source: https://github.com/charpeni/react-native-url-polyfill/blob/main/README.md Import 'react-native-url-polyfill/auto' at the top of your JavaScript entry-point file (e.g., index.js) to automatically apply the polyfill globally. ```javascript import 'react-native-url-polyfill/auto'; ``` -------------------------------- ### Build and Run Android App Source: https://github.com/charpeni/react-native-url-polyfill/blob/main/platforms/react-native/0.81/README.md Execute this command in a separate terminal from your React Native project root to build and run the Android application. Ensure Metro is running. ```sh # Using npm npm run android # OR using Yarn yarn android ``` -------------------------------- ### WPT Reference File Change Warning Source: https://github.com/charpeni/react-native-url-polyfill/blob/main/js/__tests__/wpt/README.md This warning is displayed when upstream reference files (e.g., *.any.js) have changed, indicating that manual porting of tests to URLSearchParams-test.js or URL-test.js is required. Review the diff and update local test files accordingly. ```bash ⚠ The following reference files changed upstream: These are manually ported — review the diff and update URLSearchParams-test.js (or URL-test.js) accordingly. urlsearchparams-sort.any.js urlsearchparams-size.any.js ``` -------------------------------- ### Update WPT Test Data Source: https://github.com/charpeni/react-native-url-polyfill/blob/main/js/__tests__/wpt/README.md This command-line instruction is used to pull the latest test data from the Web Platform Tests repository. It executes a script that downloads files from a WPT mirror, ensuring the local test suite is up-to-date with the canonical tests. ```bash yarn update-wpt ``` -------------------------------- ### Create Blob URL with URL.createObjectURL Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Constructs a Blob URL using the native BlobModule. Requires BlobProvider to be registered as an Android ContentProvider for content:// URI support. Use URL.revokeObjectURL when done. ```xml // android/app/src/main/AndroidManifest.xml — required for content:// Blob URLs on Android: // // android/app/src/main/res/values/strings.xml: // com.myapp.blobs ``` ```javascript import { URL } from 'react-native-url-polyfill'; async function uploadAndGetBlobUrl(uri) { const response = await fetch(uri); const blob = await response.blob(); try { const blobUrl = URL.createObjectURL(blob); // blobUrl => e.g., "blob://localhost/abc-123?offset=0&size=4096" console.log(blobUrl); // Use blobUrl in an Image, Video, WebView, etc. // When done, revoke (no-op in this polyfill but good practice): URL.revokeObjectURL(blobUrl); } catch (e) { // Throws if BlobModule is unavailable console.error('Blob URLs not supported:', e.message); } } ``` -------------------------------- ### Use URL and URLSearchParams as ponyfills Source: https://github.com/charpeni/react-native-url-polyfill/blob/main/README.md Import URL and URLSearchParams directly from 'react-native-url-polyfill' to use them as ponyfills. This approach avoids modifying the global URL object and allows selective usage. ```javascript import { URL, URLSearchParams } from 'react-native-url-polyfill'; const url = new URL('https://github.com'); const searchParams = new URLSearchParams('q=GitHub'); ``` -------------------------------- ### Construct URLSearchParams from String Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Initializes `URLSearchParams` from a query string. The leading '?' is automatically removed. ```javascript import { URLSearchParams, URL } from 'react-native-url-polyfill'; // Construction from string (leading "?" stripped automatically) const params = new URLSearchParams('?sort=asc&limit=10&tag=js&tag=ts'); console.log(params.get('sort')); // => "asc" console.log(params.get('limit')); // => "10" console.log(params.getAll('tag')); // => ["js", "ts"] console.log(params.has('missing')); // => false ``` -------------------------------- ### URL Static Methods (No-op) Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Shows the usage of `URL.revokeObjectURL`. Note that this method is a no-op in this specific polyfill. ```javascript // Static: URL.revokeObjectURL (no-op in this polyfill) URL.revokeObjectURL('blob:some-url'); // no error, no effect ``` -------------------------------- ### Construct URLSearchParams from Array of Pairs Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Initializes `URLSearchParams` using an array where each element is a key-value pair (an array of two strings). ```javascript // Construction from array of pairs const fromArr = new URLSearchParams([['a', '1'], ['b', '2']]); console.log(fromArr.toString()); // => "a=1&b=2" ``` -------------------------------- ### Check URL Parsability with `URL.canParse()` Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Uses the static `URL.canParse()` method to check if a given input can be parsed as a URL. It accepts a URL string or a URL string and a base URL. ```javascript // Static: URL.canParse() console.log(URL.canParse('https://example.com')); // => true console.log(URL.canParse('a', 'https://b/')); // => true console.log(URL.canParse('https://test:test')); // => false console.log(URL.canParse(undefined)); // => false ``` -------------------------------- ### Bidirectional Sync Between URL and URLSearchParams Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Demonstrates how changes made to `URLSearchParams` are reflected in the `URL` object's search string, and vice versa. This sync is bidirectional. ```javascript // Bidirectional sync with URL const url = new URL('http://example.org/file?a=b&c=d'); const sp = url.searchParams; sp.set('a', 'updated'); console.log(url.search); // => "?a=updated&c=d" sp.append('e', 'f'); console.log(url.href); // => "http://example.org/file?a=updated&c=d&e=f" url.search = 'x=1'; console.log(sp.toString()); // => "x=1" ``` -------------------------------- ### Resolve Relative URLs Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Shows how to resolve relative URLs against a base URL. This fixes known issues in React Native. ```javascript // Relative URL resolution (fixes React Native issue #25717) const resolved = new URL('about', 'https://www.mozilla.org'); console.log(resolved.href); // => "https://www.mozilla.org/about" ``` -------------------------------- ### Construct URLSearchParams from Object Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Creates a `URLSearchParams` instance from a JavaScript object. ```javascript // Construction from object const fromObj = new URLSearchParams({ foo: 'bar', baz: 'qux' }); console.log(fromObj.toString()); // => "foo=bar&baz=qux" ``` -------------------------------- ### Mutate URL Properties Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Illustrates how to modify URL properties like `pathname` and `search` after construction. ```javascript // Property mutation url.pathname = '/v3/items'; url.search = '?sort=desc'; console.log(url.href); // => "https://user:pass@api.example.com:8080/v3/items?sort=desc#results" ``` -------------------------------- ### URL Class Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt The URL class provides a WHATWG-compliant implementation for parsing and manipulating URLs. It supports standard property getters/setters and static methods like canParse(). ```APIDOC ## `URL` — WHATWG-compliant URL class (ponyfill) A named export of the WHATWG URL implementation. Supports full URL parsing, all standard property getters/setters (`href`, `protocol`, `username`, `password`, `host`, `hostname`, `port`, `pathname`, `search`, `hash`, `origin`), as well as static methods `URL.canParse()` and `URL.createObjectURL()` / `URL.revokeObjectURL()`. Non-ASCII hostnames are not supported (Unicode stripped for bundle size). ### Constructor `new URL(url: string, base?: string)` ### Properties - `href` (string): The full URL. - `protocol` (string): The URL protocol (e.g., "https:"). - `username` (string): The username for authentication. - `password` (string): The password for authentication. - `host` (string): The host and port (e.g., "api.example.com:8080"). - `hostname` (string): The host without the port (e.g., "api.example.com"). - `port` (string): The port number. - `pathname` (string): The path of the URL. - `search` (string): The query string, including the leading '?'. - `hash` (string): The fragment identifier, including the leading '#'. - `origin` (string): The origin of the URL. ### Static Methods - `URL.canParse(url: string, base?: string): boolean`: Checks if a URL can be parsed. - `URL.createObjectURL(blob: Blob): string`: Creates a blob URL (no-op in this polyfill). - `URL.revokeObjectURL(url: string): void`: Revokes a blob URL (no-op in this polyfill). ### Example ```javascript import { URL } from 'react-native-url-polyfill'; const url = new URL('https://user:pass@api.example.com:8080/v2/items?sort=asc#results'); console.log(url.hostname); // => "api.example.com" url.pathname = '/v3/items'; console.log(url.href); // => "https://user:pass@api.example.com:8080/v3/items?sort=asc#results" console.log(URL.canParse('https://example.com')); // => true ``` ``` -------------------------------- ### Sort URLSearchParams Keys Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Sorts the parameter keys lexicographically in place using the `sort()` method. ```javascript // Sorting params.sort(); console.log([...params.keys()]); // keys sorted lexicographically ``` -------------------------------- ### URLSearchParams Class Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt The URLSearchParams class provides a WHATWG-compliant way to work with URL query parameters. It supports various construction methods and mutation operations. ```APIDOC ## `URLSearchParams` — WHATWG-compliant query parameter class (ponyfill) A named export backed by `whatwg-url-without-unicode`. Accepts string, object, array-of-pairs, or another `URLSearchParams` instance as constructor input. Provides full WHATWG-spec methods: `append`, `delete`, `get`, `getAll`, `has`, `set`, `sort`, `forEach`, `keys`, `values`, `entries`, `toString`, and `Symbol.iterator`. Bidirectionally synced with `URL.searchParams`. ### Constructor `new URLSearchParams(init?: string | object | Array<[string, string]> | URLSearchParams)` ### Methods - `append(name: string, value: string): void`: Appends a new key-value pair. - `delete(name: string): void`: Deletes all key-value pairs with the given name. - `get(name: string): string | null`: Returns the first value associated with the given name, or null if not found. - `getAll(name: string): string[]`: Returns all values associated with the given name. - `has(name: string): boolean`: Returns true if a value with the given name exists. - `set(name: string, value: string): void`: Sets the value associated with a given name to the given value, discarding all previous values. - `sort(): void`: Sorts all key-value pairs lexicographically by name. - `forEach(callbackfn: (value: string, key: string) => void, thisArg?: any): void`: Iterates over each key-value pair. - `keys(): IterableIterator`: Returns an iterator for the keys. - `values(): IterableIterator`: Returns an iterator for the values. - `entries(): IterableIterator<[string, string]>`: Returns an iterator for the key-value pairs. - `toString(): string`: Returns a serialized string of the parameters. - `[Symbol.iterator](): IterableIterator<[string, string]>`: Allows iteration over entries. ### Example ```javascript import { URLSearchParams, URL } from 'react-native-url-polyfill'; const params = new URLSearchParams('?sort=asc&tag=js&tag=ts'); console.log(params.getAll('tag')); // => ["js", "ts"] params.append('tag', 'react'); console.log(params.toString()); // => "sort=asc&tag=js&tag=ts&tag=react" const url = new URL('http://example.org/file?a=b'); url.searchParams.set('a', 'updated'); console.log(url.href); // => "http://example.org/file?a=updated" ``` ``` -------------------------------- ### Mutate URLSearchParams: append, set, delete Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Demonstrates the `append`, `set`, and `delete` methods for modifying `URLSearchParams`. `append` adds a new value for an existing key, `set` overwrites all values for a key, and `delete` removes a key and its values. ```javascript // Mutation methods params.append('tag', 'react'); console.log(params.getAll('tag')); // => ["js", "ts", "react"] params.set('sort', 'desc'); console.log(params.get('sort')); // => "desc" params.delete('limit'); console.log(params.has('limit')); // => false ``` -------------------------------- ### Generate Jest Tests from URL Test Data Source: https://github.com/charpeni/react-native-url-polyfill/blob/main/js/__tests__/wpt/README.md This code demonstrates how JSON test data is consumed by Jest to generate individual test cases for URL parsing. Each entry in the `urltestdata.json` file is iterated over to create a Jest `it()` call, dynamically creating tests without manual code changes for each case. ```javascript const urlTestData = require('./wpt/urltestdata.json'); for (const expected of testCases) { it(`Parsing: <${expected.input}>`, () => { const url = new URL(expected.input, base); expect(url.href).toBe(expected.href); // ...all other properties }); } ``` -------------------------------- ### Remove All URLSearchParams Source: https://context7.com/charpeni/react-native-url-polyfill/llms.txt Shows how deleting all parameters from `URLSearchParams` results in the `URL` object's search string becoming empty and the trailing '?' being removed from the URL. ```javascript // Deleting all params removes "?" from URL url.searchParams.delete('x'); console.log(url.href); // => "http://example.org/file" console.log(url.search); // => "" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.