encodeurl 1.0.2 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +26 -31
  2. package/index.js +6 -11
  3. package/package.json +16 -12
  4. package/HISTORY.md +0 -14
package/README.md CHANGED
@@ -1,21 +1,16 @@
1
- # encodeurl
1
+ # Encode URL
2
2
 
3
3
  [![NPM Version][npm-image]][npm-url]
4
4
  [![NPM Downloads][downloads-image]][downloads-url]
5
- [![Node.js Version][node-version-image]][node-version-url]
6
- [![Build Status][travis-image]][travis-url]
5
+ [![Build Status][ci-image]][ci-url]
7
6
  [![Test Coverage][coveralls-image]][coveralls-url]
8
7
 
9
- Encode a URL to a percent-encoded form, excluding already-encoded sequences
8
+ Encode a URL to a percent-encoded form, excluding already-encoded sequences.
10
9
 
11
10
  ## Installation
12
11
 
13
- This is a [Node.js](https://nodejs.org/en/) module available through the
14
- [npm registry](https://www.npmjs.com/). Installation is done using the
15
- [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
16
-
17
12
  ```sh
18
- $ npm install encodeurl
13
+ npm install encodeurl
19
14
  ```
20
15
 
21
16
  ## API
@@ -28,24 +23,13 @@ var encodeUrl = require('encodeurl')
28
23
 
29
24
  Encode a URL to a percent-encoded form, excluding already-encoded sequences.
30
25
 
31
- This function will take an already-encoded URL and encode all the non-URL
32
- code points (as UTF-8 byte sequences). This function will not encode the
33
- "%" character unless it is not part of a valid sequence (`%20` will be
34
- left as-is, but `%foo` will be encoded as `%25foo`).
26
+ This 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`).
35
27
 
36
- This encode is meant to be "safe" and does not throw errors. It will try as
37
- hard as it can to properly encode the given URL, including replacing any raw,
38
- unpaired surrogate pairs with the Unicode replacement character prior to
39
- encoding.
40
-
41
- This function is _similar_ to the intrinsic function `encodeURI`, except it
42
- will not encode the `%` character if that is part of a valid sequence, will
43
- not encode `[` and `]` (for IPv6 hostnames) and will replace raw, unpaired
44
- surrogate pairs with the Unicode replacement character (instead of throwing).
28
+ This 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.
45
29
 
46
30
  ## Examples
47
31
 
48
- ### Encode a URL containing user-controled data
32
+ ### Encode a URL containing user-controlled data
49
33
 
50
34
  ```js
51
35
  var encodeUrl = require('encodeurl')
@@ -97,6 +81,19 @@ http.createServer(function onRequest (req, res) {
97
81
  })
98
82
  ```
99
83
 
84
+ ## Similarities
85
+
86
+ This function is _similar_ to the intrinsic function `encodeURI`. However, it will not encode:
87
+
88
+ * The `\`, `^`, or `|` characters
89
+ * The `%` character when it's part of a valid sequence
90
+ * `[` and `]` (for IPv6 hostnames)
91
+ * Replaces raw, unpaired surrogate pairs with the Unicode replacement character
92
+
93
+ As 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.
94
+
95
+ It 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.
96
+
100
97
  ## Testing
101
98
 
102
99
  ```sh
@@ -116,13 +113,11 @@ $ npm run lint
116
113
 
117
114
  [MIT](LICENSE)
118
115
 
119
- [npm-image]: https://img.shields.io/npm/v/encodeurl.svg
116
+ [npm-image]: https://img.shields.io/npm/v/encodeurl
120
117
  [npm-url]: https://npmjs.org/package/encodeurl
121
- [node-version-image]: https://img.shields.io/node/v/encodeurl.svg
122
- [node-version-url]: https://nodejs.org/en/download
123
- [travis-image]: https://img.shields.io/travis/pillarjs/encodeurl.svg
124
- [travis-url]: https://travis-ci.org/pillarjs/encodeurl
125
- [coveralls-image]: https://img.shields.io/coveralls/pillarjs/encodeurl.svg
118
+ [downloads-image]: https://img.shields.io/npm/dm/encodeurl
119
+ [downloads-url]: https://npmcharts.com/compare/encodeurl?minimal=true
120
+ [ci-image]: https://img.shields.io/github/actions/workflow/status/pillarjs/encodeurl/ci.yml?branch=master
121
+ [ci-url]: https://github.com/pillarjs/encodeurl/actions/workflows/ci.yml
122
+ [coveralls-image]: https://img.shields.io/coverallsCoverage/github/pillarjs/encodeurl?branch=master
126
123
  [coveralls-url]: https://coveralls.io/r/pillarjs/encodeurl?branch=master
127
- [downloads-image]: https://img.shields.io/npm/dm/encodeurl.svg
128
- [downloads-url]: https://npmjs.org/package/encodeurl
package/index.js CHANGED
@@ -19,21 +19,14 @@ module.exports = encodeUrl
19
19
  * @private
20
20
  */
21
21
 
22
- var ENCODE_CHARS_REGEXP = /(?:[^\x21\x25\x26-\x3B\x3D\x3F-\x5B\x5D\x5F\x61-\x7A\x7E]|%(?:[^0-9A-Fa-f]|[0-9A-Fa-f][^0-9A-Fa-f]|$))+/g
22
+ var ENCODE_CHARS_REGEXP = /(?:[^\x21\x23-\x3B\x3D\x3F-\x5F\x61-\x7A\x7C\x7E]|%(?:[^0-9A-Fa-f]|[0-9A-Fa-f][^0-9A-Fa-f]|$))+/g
23
23
 
24
24
  /**
25
- * RegExp to match unmatched surrogate pair.
25
+ * RegExp to match surrogate pairs and unpaired surrogates.
26
26
  * @private
27
27
  */
28
28
 
29
- var UNMATCHED_SURROGATE_PAIR_REGEXP = /(^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/g
30
-
31
- /**
32
- * String to replace unmatched surrogate pair with.
33
- * @private
34
- */
35
-
36
- var UNMATCHED_SURROGATE_PAIR_REPLACE = '$1\uFFFD$2'
29
+ var SURROGATE_REGEXP = /([\uD800-\uDBFF][\uDC00-\uDFFF])|[\uD800-\uDFFF]/g
37
30
 
38
31
  /**
39
32
  * Encode a URL to a percent-encoded form, excluding already-encoded sequences.
@@ -55,6 +48,8 @@ var UNMATCHED_SURROGATE_PAIR_REPLACE = '$1\uFFFD$2'
55
48
 
56
49
  function encodeUrl (url) {
57
50
  return String(url)
58
- .replace(UNMATCHED_SURROGATE_PAIR_REGEXP, UNMATCHED_SURROGATE_PAIR_REPLACE)
51
+ .replace(SURROGATE_REGEXP, function (_, pair) {
52
+ return pair || '\uFFFD'
53
+ })
59
54
  .replace(ENCODE_CHARS_REGEXP, encodeURI)
60
55
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "encodeurl",
3
3
  "description": "Encode a URL to a percent-encoded form, excluding already-encoded sequences",
4
- "version": "1.0.2",
4
+ "version": "2.0.1",
5
5
  "contributors": [
6
6
  "Douglas Christopher Wilson <doug@somethingdoug.com>"
7
7
  ],
@@ -12,15 +12,19 @@
12
12
  "url"
13
13
  ],
14
14
  "repository": "pillarjs/encodeurl",
15
+ "funding": {
16
+ "type": "opencollective",
17
+ "url": "https://opencollective.com/express"
18
+ },
15
19
  "devDependencies": {
16
- "eslint": "3.19.0",
17
- "eslint-config-standard": "10.2.1",
18
- "eslint-plugin-import": "2.8.0",
19
- "eslint-plugin-node": "5.2.1",
20
- "eslint-plugin-promise": "3.6.0",
21
- "eslint-plugin-standard": "3.0.1",
22
- "istanbul": "0.4.5",
23
- "mocha": "2.5.3"
20
+ "eslint": "5.11.1",
21
+ "eslint-config-standard": "12.0.0",
22
+ "eslint-plugin-import": "2.14.0",
23
+ "eslint-plugin-node": "7.0.1",
24
+ "eslint-plugin-promise": "4.0.1",
25
+ "eslint-plugin-standard": "4.0.0",
26
+ "mocha": "~11.7.0",
27
+ "nyc": "~17.1.0"
24
28
  },
25
29
  "files": [
26
30
  "LICENSE",
@@ -33,8 +37,8 @@
33
37
  },
34
38
  "scripts": {
35
39
  "lint": "eslint .",
36
- "test": "mocha --reporter spec --bail --check-leaks test/",
37
- "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
38
- "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
40
+ "test": "mocha --check-leaks --reporter spec",
41
+ "test-ci": "nyc --reporter=lcov --reporter=text npm test",
42
+ "test-cov": "nyc --reporter=html --reporter=text npm test"
39
43
  }
40
44
  }
package/HISTORY.md DELETED
@@ -1,14 +0,0 @@
1
- 1.0.2 / 2018-01-21
2
- ==================
3
-
4
- * Fix encoding `%` as last character
5
-
6
- 1.0.1 / 2016-06-09
7
- ==================
8
-
9
- * Fix encoding unpaired surrogates at start/end of string
10
-
11
- 1.0.0 / 2016-06-08
12
- ==================
13
-
14
- * Initial release