Skip to content

Commit

Permalink
feat: compact punycode support
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 16, 2020
1 parent a2ffa04 commit edaf5ca
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @ts-ignore
import { toASCII } from './punycode'

// Utils used from https://github.com/vuejs/vue-router-next/blob/master/src/encoding.ts (Author @posva)

const HASH_RE = /#/g // %23
Expand Down Expand Up @@ -123,3 +126,7 @@ export function encodeSearchParam (key: string, val: string | string[]) {

return `${encodeQueryKey(key)}=${encodeParam(val)}`
}

export function encodeHost (name: string = '') {
return toASCII(name)
}
4 changes: 4 additions & 0 deletions src/punycode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable */
// @ts-nocheck
// Tree-shaked + Minified version of [email protected]/punycode.es6.js#toASCII
const n = /[^\0-\x7E]/; const t = /[\x2E\u3002\uFF0E\uFF61]/g; const o = { overflow: 'Overflow Error', 'not-basic': 'Illegal Input', 'invalid-input': 'Invalid Input' }; const e = Math.floor; const r = String.fromCharCode; function s(n) { throw new RangeError(o[n]) } const c = function (n, t) { return n + 22 + 75 * (n < 26) - ((t != 0) << 5) }; const u = function (n, t, o) { let r = 0; for (n = o ? e(n / 700) : n >> 1, n += e(n / t); n > 455; r += 36) { n = e(n / 35) } return e(r + 36 * n / (n + 38)) }; export function toASCII (o) { return (function (n, o) { const e = n.split('@'); let r = ''; e.length > 1 && (r = e[0] + '@', n = e[1]); const s = (function (n, t) { const o = []; let e = n.length; for (; e--;) { o[e] = t(n[e]) } return o }((n = n.replace(t, '.')).split('.'), o)).join('.'); return r + s }(o, function (t) { return n.test(t) ? 'xn--' + (function (n) { const t = []; const o = (n = (function (n) { const t = []; let o = 0; const e = n.length; for (; o < e;) { const r = n.charCodeAt(o++); if (r >= 55296 && r <= 56319 && o < e) { const e = n.charCodeAt(o++); (64512 & e) == 56320 ? t.push(((1023 & r) << 10) + (1023 & e) + 65536) : (t.push(r), o--) } else { t.push(r) } } return t }(n))).length; let f = 128; let i = 0; let l = 72; for (const o of n) { o < 128 && t.push(r(o)) } const h = t.length; let p = h; for (h && t.push('-'); p < o;) { let o = 2147483647; for (const t of n) { t >= f && t < o && (o = t) } const a = p + 1; o - f > e((2147483647 - i) / a) && s('overflow'), i += (o - f) * a, f = o; for (const o of n) { if (o < f && ++i > 2147483647 && s('overflow'), o == f) { let n = i; for (let o = 36; ; o += 36) { const s = o <= l ? 1 : o >= l + 26 ? 26 : o - l; if (n < s) { break } const u = n - s; const f = 36 - s; t.push(r(c(s + u % f, 0))), n = e(u / f) } t.push(r(c(n, 0))), l = u(i, a, p == h), i = 0, ++p } } ++i, ++f } return t.join('') }(t)) : t })) }
6 changes: 3 additions & 3 deletions src/ufo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hasProtocol, parsePath, ParamsObject, parseURL } from './parse'
import { withoutLeadingSlash, withLeadingSlash, withTrailingSlash } from './utils'
import { encodeSearchParam, encodeHash, encode, encodePath, decode } from './encoding'
import { withoutLeadingSlash, withTrailingSlash } from './utils'
import { encodeSearchParam, encodeHash, encodePath, decode, encodeHost } from './encoding'

export class UFO implements URL {
params: ParamsObject = {}
Expand Down Expand Up @@ -57,7 +57,7 @@ export class UFO implements URL {
}

get host () {
return encode(this.hostname) + (this.port ? `:${this.port}` : '')
return encodeHost(this.hostname) + (this.port ? `:${this.port}` : '')
}

get origin (): string {
Expand Down
16 changes: 8 additions & 8 deletions test/normalize.perfect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('normalizeURL', () => {
'http://foo.com/blah_blah_(wikipedia)_(again)',
'http://www.example.com/wpstyle/?p=364',
'https://www.example.com/foo/?bar=baz&inga=42&quux',
// 'http://✪df.ws/123',
'http://✪df.ws/123',
'http://userid:[email protected]:8080',
'http://userid:[email protected]:8080/',
'http://[email protected]',
Expand All @@ -44,21 +44,21 @@ describe('normalizeURL', () => {
'http://userid:[email protected]/',
'http://142.42.1.1/',
'http://142.42.1.1:8080/',
// 'http://➡.ws/䨹',
// 'http://⌘.ws',
// 'http://⌘.ws/',
'http://➡.ws/䨹',
'http://⌘.ws',
'http://⌘.ws/',
'http://foo.com/blah_(wikipedia)#cite-1',
'http://foo.com/blah_(wikipedia)_blah#cite-1',
'http://foo.com/unicode_(✪)_in_parens',
'http://foo.com/(something)?after=parens',
// 'http://☺.damowmow.com/',
'http://☺.damowmow.com/',
'http://code.google.com/events/#&product=browser',
'http://j.mp',
'ftp://foo.bar/baz',
'http://foo.bar/?q=Test%20URL-encoded%20stuff',
// 'http://مثال.إختبار',
// 'http://例子.测试',
// 'http://उदाहरण.परीक्षा',
'http://مثال.إختبار',
'http://例子.测试',
'http://उदाहरण.परीक्षा',
// 'http://-.~_!$&\'() * +,;=:% 40: 80 % 2f:::::: @example.com',
'http://1337.net',
'http://a.b-c.de',
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,11 @@
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.1.5.tgz#b6ab3bba29e16b821d84e09ecfaded462b816b00"
integrity sha512-UEyp8LwZ4Dg30kVU2Q3amHHyTn1jEdhCIE59ANed76GaT1Vp76DD3ZWSAxgCrw6wJ0TqeoBpqmfUHiUDPs//HQ==

"@types/punycode@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@types/punycode/-/punycode-2.1.0.tgz#89e4f3d09b3f92e87a80505af19be7e0c31d4e83"
integrity sha512-PG5aLpW6PJOeV2fHRslP4IOMWn+G+Uq8CfnyJ+PDS8ndCbU+soO+fB3NKCKo0p/Jh2Y4aPaiQZsrOXFdzpcA6g==

"@types/[email protected]":
version "1.17.1"
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-1.17.1.tgz#3afd6ad8967c77e4376c598a82ddd58f46ec45d6"
Expand Down

0 comments on commit edaf5ca

Please sign in to comment.