source: frontend/node_modules/encodeurl/README.md@ 9af201e

Last change on this file since 9af201e was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.1 KB
Line 
1# Encode URL
2
3Encode a URL to a percent-encoded form, excluding already-encoded sequences.
4
5## Installation
6
7```sh
8npm install encodeurl
9```
10
11## API
12
13```js
14var encodeUrl = require('encodeurl')
15```
16
17### encodeUrl(url)
18
19Encode a URL to a percent-encoded form, excluding already-encoded sequences.
20
21This function accepts a URL and encodes all the non-URL code points (as UTF-8 byte sequences). It will not encode the "%" character unless it is not part of a valid sequence (`%20` will be left as-is, but `%foo` will be encoded as `%25foo`).
22
23This encode is meant to be "safe" and does not throw errors. It will try as hard as it can to properly encode the given URL, including replacing any raw, unpaired surrogate pairs with the Unicode replacement character prior to encoding.
24
25## Examples
26
27### Encode a URL containing user-controlled data
28
29```js
30var encodeUrl = require('encodeurl')
31var escapeHtml = require('escape-html')
32
33http.createServer(function onRequest (req, res) {
34 // get encoded form of inbound url
35 var url = encodeUrl(req.url)
36
37 // create html message
38 var body = '<p>Location ' + escapeHtml(url) + ' not found</p>'
39
40 // send a 404
41 res.statusCode = 404
42 res.setHeader('Content-Type', 'text/html; charset=UTF-8')
43 res.setHeader('Content-Length', String(Buffer.byteLength(body, 'utf-8')))
44 res.end(body, 'utf-8')
45})
46```
47
48### Encode a URL for use in a header field
49
50```js
51var encodeUrl = require('encodeurl')
52var escapeHtml = require('escape-html')
53var url = require('url')
54
55http.createServer(function onRequest (req, res) {
56 // parse inbound url
57 var href = url.parse(req)
58
59 // set new host for redirect
60 href.host = 'localhost'
61 href.protocol = 'https:'
62 href.slashes = true
63
64 // create location header
65 var location = encodeUrl(url.format(href))
66
67 // create html message
68 var body = '<p>Redirecting to new site: ' + escapeHtml(location) + '</p>'
69
70 // send a 301
71 res.statusCode = 301
72 res.setHeader('Content-Type', 'text/html; charset=UTF-8')
73 res.setHeader('Content-Length', String(Buffer.byteLength(body, 'utf-8')))
74 res.setHeader('Location', location)
75 res.end(body, 'utf-8')
76})
77```
78
79## Similarities
80
81This function is _similar_ to the intrinsic function `encodeURI`. However, it will not encode:
82
83* The `\`, `^`, or `|` characters
84* The `%` character when it's part of a valid sequence
85* `[` and `]` (for IPv6 hostnames)
86* Replaces raw, unpaired surrogate pairs with the Unicode replacement character
87
88As a result, the encoding aligns closely with the behavior in the [WHATWG URL specification][whatwg-url]. However, this package only encodes strings and does not do any URL parsing or formatting.
89
90It is expected that any output from `new URL(url)` will not change when used with this package, as the output has already been encoded. Additionally, if we were to encode before `new URL(url)`, we do not expect the before and after encoded formats to be parsed any differently.
91
92## Testing
93
94```sh
95$ npm test
96$ npm run lint
97```
98
99## References
100
101- [RFC 3986: Uniform Resource Identifier (URI): Generic Syntax][rfc-3986]
102- [WHATWG URL Living Standard][whatwg-url]
103
104[rfc-3986]: https://tools.ietf.org/html/rfc3986
105[whatwg-url]: https://url.spec.whatwg.org/
106
107## License
108
109[MIT](LICENSE)
Note: See TracBrowser for help on using the repository browser.