CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
README.md110 linesDownload Raw Back to encodeurl
1# Encode URL2 3Encode a URL to a percent-encoded form, excluding already-encoded sequences.4 5## Installation6 7```sh8npm install encodeurl9```10 11## API12 13```js14var 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## Examples26 27### Encode a URL containing user-controlled data28 29```js30var encodeUrl = require('encodeurl')31var escapeHtml = require('escape-html')32 33http.createServer(function onRequest (req, res) {34  // get encoded form of inbound url35  var url = encodeUrl(req.url)36 37  // create html message38  var body = '<p>Location ' + escapeHtml(url) + ' not found</p>'39 40  // send a 40441  res.statusCode = 40442  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 field49 50```js51var encodeUrl = require('encodeurl')52var escapeHtml = require('escape-html')53var url = require('url')54 55http.createServer(function onRequest (req, res) {56  // parse inbound url57  var href = url.parse(req)58 59  // set new host for redirect60  href.host = 'localhost'61  href.protocol = 'https:'62  href.slashes = true63 64  // create location header65  var location = encodeUrl(url.format(href))66 67  // create html message68  var body = '<p>Redirecting to new site: ' + escapeHtml(location) + '</p>'69 70  // send a 30171  res.statusCode = 30172  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## Similarities80 81This function is _similar_ to the intrinsic function `encodeURI`. However, it will not encode:82 83* The `\`, `^`, or `|` characters84* The `%` character when it's part of a valid sequence85* `[` and `]` (for IPv6 hostnames)86* Replaces raw, unpaired surrogate pairs with the Unicode replacement character87 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## Testing93 94```sh95$ npm test96$ npm run lint97```98 99## References100 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/rfc3986105[whatwg-url]: https://url.spec.whatwg.org/106 107## License108 109[MIT](LICENSE)110