UNPKG

3.76 kBMarkdownView Raw
1# @fastly/as-url
2
3![npm version](https://img.shields.io/npm/v/@fastly/as-url) ![npm downloads per month](https://img.shields.io/npm/dm/@fastly/as-url)
4
5AssemblyScript 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).
6
7## Installation
8
9`@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:
10
11`npm install --save @fastly/as-url`
12
13## Quick Start
14
15This 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.
16
17```
18import { URL } from '@fastly/as-url';
19
20// Parse a complex absolute URL
21const myComplexUrlString = 'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash';
22const url = new URL(myComplexUrlString);
23
24let protocol = url.protocol; // "https:"
25let username = url.username; // "user"
26let password = url.password; // "pass"
27let hostname = url.hostname; // "sub.example.com"
28let port = url.port; // "8080"
29let host = url.host; // "sub.example.com:8080"
30let origin = url.origin // "https://sub.example.com:8080"
31let pathname = url.pathname; // "/p/a/t/h"
32let search = url.search; // "?query=string"
33let hash = url.hash; // "#hash"
34let href = url.href; // "https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash"
35
36// Parse a relative URL
37const relativeUrl = new URL(
38 "../../p/a/t/h?query=string#hash",
39 "https://fastly.com/path1/path2"
40);
41
42let relativeUrlHref = relativeUrl.href; // https://fastly.com/p/a/t/h?query=string#hash
43```
44
45Also, feel free to look through the [tests for additional examples](https://unpkg.com/@fastly/as-url/assembly/__tests__/).
46
47## Reference API
48
49The Reference API documentation can be found in the [`reference-docs/`](https://unpkg.com/@fastly/as-url/) directory.
50
51## Changelog
52
53The changelog can be found [here](https://unpkg.com/@fastly/as-url/CHANGELOG.md).
54
55## Security
56
57If 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`
58
59We 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).
60
61## Motivation
62
63This library was built to be used as a dependency for [`@fastly/as-compute`](https://www.npmjs.com/package/@fastly/as-compute).
64
65This 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.
66
67However, 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.
68
69## License
70
71[Apache-2.0 WITH LLVM-exception](./LICENSE)