bare-url 0.3.5 → 0.3.6

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 (3) hide show
  1. package/index.js +1 -1
  2. package/lib/parse.js +65 -6
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -228,7 +228,7 @@ exports.pathToFileURL = function pathToFileURL (pathname) {
228
228
  if (pathname[pathname.length - 1] === '/') {
229
229
  resolved += '/'
230
230
  } else if (os.platform() === 'win32' && pathname[pathname.length - 1 === '\\']) {
231
- resolved += '/'
231
+ resolved += '\\'
232
232
  }
233
233
 
234
234
  resolved = resolved
package/lib/parse.js CHANGED
@@ -224,10 +224,12 @@ module.exports = function parse (
224
224
  continue
225
225
  }
226
226
 
227
+ const encodedCodePoints = UTF8PercentEncode(buffer[i], userinfoPercentEncodeSet)
228
+
227
229
  if (passwordTokenSeen) {
228
- url.password += encodeURIComponent(buffer[i])
230
+ url.password += encodedCodePoints
229
231
  } else {
230
- url.username += encodeURIComponent(buffer[i])
232
+ url.username += encodedCodePoints
231
233
  }
232
234
  }
233
235
 
@@ -463,7 +465,7 @@ module.exports = function parse (
463
465
  state = constants.STATE_FRAGMENT
464
466
  }
465
467
  } else {
466
- buffer += encodeURIComponent(String.fromCharCode(c))
468
+ buffer += UTF8PercentEncode(String.fromCharCode(c), pathPercentEncodeSet)
467
469
  }
468
470
  break
469
471
 
@@ -477,7 +479,7 @@ module.exports = function parse (
477
479
  state = constants.STATE_FRAGMENT
478
480
  } else {
479
481
  if (c !== -1) {
480
- url.path += encodeURIComponent(String.fromCharCode(c))
482
+ url.path += UTF8PercentEncode(String.fromCharCode(c), c0ControlPercentEncodeSet)
481
483
  }
482
484
  }
483
485
  break
@@ -485,7 +487,9 @@ module.exports = function parse (
485
487
  // https://url.spec.whatwg.org/#query-state
486
488
  case constants.STATE_QUERY:
487
489
  if ((!stateOverride && c === 0x23) || c === -1) {
488
- url.query += encodeURI(buffer)
490
+ const percentEncodeSet = isSpecial(url) ? specialQueryPercentEncodeSet : queryPercentEncodeSet
491
+
492
+ url.query += UTF8PercentEncode(buffer, percentEncodeSet)
489
493
  buffer = ''
490
494
 
491
495
  if (c === 0x23) {
@@ -500,7 +504,7 @@ module.exports = function parse (
500
504
  // https://url.spec.whatwg.org/#fragment-state
501
505
  case constants.STATE_FRAGMENT:
502
506
  if (c !== -1) {
503
- url.fragment += encodeURI(String.fromCharCode(c))
507
+ url.fragment += UTF8PercentEncode(String.fromCharCode(c), fragmentPercentEncodeSet)
504
508
  }
505
509
  }
506
510
  }
@@ -600,3 +604,58 @@ function isSingleDotPathSegment (segment) {
600
604
  function isDoubleDotPathSegment (segment) {
601
605
  return segment === '..' || decodeURIComponent(segment) === '..'
602
606
  }
607
+
608
+ // https://url.spec.whatwg.org/#percent-encode
609
+ function percentEncode (byte) {
610
+ return `%${byte.toString(16).toUpperCase().padStart(2, '0')}`
611
+ }
612
+
613
+ // https://url.spec.whatwg.org/#string-percent-encode-after-encoding
614
+ function percentEncodeAfterEncoding (bytes, percentEncodeSet) {
615
+ let output = ''
616
+
617
+ for (const byte of bytes) {
618
+ if (percentEncodeSet(byte)) {
619
+ output += percentEncode(byte)
620
+ } else {
621
+ output += String.fromCharCode(byte)
622
+ }
623
+ }
624
+
625
+ return output
626
+ }
627
+
628
+ // https://url.spec.whatwg.org/#string-utf-8-percent-encode
629
+ function UTF8PercentEncode (input, percentEncodeSet) {
630
+ return percentEncodeAfterEncoding(Buffer.from(input, 'utf8'), percentEncodeSet)
631
+ }
632
+
633
+ // https://url.spec.whatwg.org/#c0-control-percent-encode-set
634
+ function c0ControlPercentEncodeSet (c) {
635
+ return c <= 0x1f || c > 0x7e
636
+ }
637
+
638
+ // https://url.spec.whatwg.org/#fragment-percent-encode-set
639
+ function fragmentPercentEncodeSet (c) {
640
+ return c0ControlPercentEncodeSet(c) || c === 0x20 || c === 0x22 || c === 0x3c || c === 0x3e || c === 0x60
641
+ }
642
+
643
+ // https://url.spec.whatwg.org/#query-percent-encode-set
644
+ function queryPercentEncodeSet (c) {
645
+ return c0ControlPercentEncodeSet(c) || c === 0x20 || c === 0x22 || c === 0x23 || c === 0x3c || c === 0x3e
646
+ }
647
+
648
+ // https://url.spec.whatwg.org/#special-query-percent-encode-set
649
+ function specialQueryPercentEncodeSet (c) {
650
+ return queryPercentEncodeSet(c) || c === 0x27
651
+ }
652
+
653
+ // https://url.spec.whatwg.org/#path-percent-encode-set
654
+ function pathPercentEncodeSet (c) {
655
+ return queryPercentEncodeSet(c) || c === 0x3f || c === 0x60 || c === 0x7b || c === 0x7d
656
+ }
657
+
658
+ // https://url.spec.whatwg.org/#userinfo-percent-encode-set
659
+ function userinfoPercentEncodeSet (c) {
660
+ return pathPercentEncodeSet(c) || c === 0x2f || c === 0x3a || c === 0x3b || c === 0x3d || c === 0x40 || c === 0x5b || c === 0x5c || c === 0x5d || c === 0x5e || c === 0x7c
661
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-url",
3
- "version": "0.3.5",
3
+ "version": "0.3.6",
4
4
  "description": "URL parser for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [