UNPKG

@fastly/as-url

Version:

![npm version](https://img.shields.io/npm/v/@fastly/as-url) ![npm downloads per month](https://img.shields.io/npm/dm/@fastly/as-url)

72 lines (45 loc) 3.76 kB
# @fastly/as-url ![npm version](https://img.shields.io/npm/v/@fastly/as-url) ![npm downloads per month](https://img.shields.io/npm/dm/@fastly/as-url) AssemblyScript library to implement the [WHATWG URL Standard](https://url.spec.whatwg.org/), similar to the [JavaScript URL Web API](https://developer.mozilla.org/en-US/docs/Web/API/URL). ## Installation `@fastly/as-url` is available as a [npm package](https://www.npmjs.com/package/@fastly/as-url). You can install `@fastly/as-url` in your AssemblyScript project by running: `npm install --save @fastly/as-url` ## Quick Start This package attempts to be an implementation of the [JavaScript URL Web API](https://developer.mozilla.org/en-US/docs/Web/API/URL). Thus, the [JavaScript URL Web API documentation](https://developer.mozilla.org/en-US/docs/Web/API/URL) could also act as a helpful getting started guide. However, while most features are implemented, some features are still in development (e.g `URLSearchParams`). Please see the [reference documentation in the `reference-docs` directory](https://unpkg.com/@fastly/as-url/) for the full documentation. ``` import { URL } from '@fastly/as-url'; // Parse a complex absolute URL const myComplexUrlString = 'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'; const url = new URL(myComplexUrlString); let protocol = url.protocol; // "https:" let username = url.username; // "user" let password = url.password; // "pass" let hostname = url.hostname; // "sub.example.com" let port = url.port; // "8080" let host = url.host; // "sub.example.com:8080" let origin = url.origin // "https://sub.example.com:8080" let pathname = url.pathname; // "/p/a/t/h" let search = url.search; // "?query=string" let hash = url.hash; // "#hash" let href = url.href; // "https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash" // Parse a relative URL const relativeUrl = new URL( "../../p/a/t/h?query=string#hash", "https://fastly.com/path1/path2" ); let relativeUrlHref = relativeUrl.href; // https://fastly.com/p/a/t/h?query=string#hash ``` Also, feel free to look through the [tests for additional examples](https://unpkg.com/@fastly/as-url/assembly/__tests__/). ## Reference API The Reference API documentation can be found in the [`reference-docs/`](https://unpkg.com/@fastly/as-url/) directory. ## Changelog The changelog can be found [here](https://unpkg.com/@fastly/as-url/CHANGELOG.md). ## Security If you happen to find any security issues, please see the [Fastly Security Reporting Page](https://www.fastly.com/security/report-security-issue), or feel free to send an email to: `security@fastly.com` We plan to disclose any found security vulnerabilites per the [npm security reporting guidelines](https://docs.npmjs.com/reporting-a-vulnerability-in-an-npm-package). Note that communications related to security issues in Fastly-maintained OSS as described here are distinct from [Fastly Security Advisories](https://www.fastly.com/security-advisories). ## Motivation This library was built to be used as a dependency for [`@fastly/as-compute`](https://www.npmjs.com/package/@fastly/as-compute). This library is based on the [WHATWG URL Standard](https://url.spec.whatwg.org/) and strives to match the [JavaScript URL Web API](https://developer.mozilla.org/en-US/docs/Web/API/URL) that [Node.js 's URL library](https://nodejs.org/api/url.html#url_the_whatwg_url_api) implements. However, the goal of this library is not to be a 1-to-1 implementation of the Node URL API. The goal of this library is to enable sharing very similar, if not identical, URL code between Fastly's Compute@Edge platform and popular JavaScript platforms like Web Browsers and Node. ## License [Apache-2.0 WITH LLVM-exception](./LICENSE)