Skip to content

Commit 371b07f

Browse files
committed
Remove punycode package
We can get the same functionality with the URL constructor
1 parent 4a4a8c7 commit 371b07f

File tree

7 files changed

+13
-27
lines changed

7 files changed

+13
-27
lines changed

package-lock.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@
6464
},
6565
"dependencies": {
6666
"is-ip": "^3.1.0",
67-
"node-fetch": "^2.6.1",
68-
"punycode": "^2.1.1"
67+
"node-fetch": "^2.6.1"
6968
},
7069
"devDependencies": {
7170
"@types/jest": "^27.4.0",

serialized-tries/info.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/psl/build-tries.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { toASCII } from "punycode";
21
import { buildTries } from "./build-tries";
32
import {
43
PUBLIC_SUFFIX_MARKER_ICANN_START,
54
PUBLIC_SUFFIX_MARKER_ICANN_END,
65
PUBLIC_SUFFIX_MARKER_PRIVATE_START,
76
PUBLIC_SUFFIX_MARKER_PRIVATE_END,
87
} from "../config";
8+
import { toASCII } from "../punycode";
99

1010
describe("buildTries()", () => {
1111
test("normalizes rules (to punycode, to lower case)", () => {

src/psl/build-tries.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { toASCII } from "punycode";
21
import {
32
PUBLIC_SUFFIX_MARKER_ICANN_START,
43
PUBLIC_SUFFIX_MARKER_ICANN_END,
54
PUBLIC_SUFFIX_MARKER_PRIVATE_START,
65
PUBLIC_SUFFIX_MARKER_PRIVATE_END,
76
} from "../config";
7+
import { toASCII } from "../punycode";
88
import { createTrieFromList } from "../trie/create-trie";
99

1010
const matchNewLine = /\r?\n/u;
@@ -34,22 +34,6 @@ const extractByMarkers = (
3434
const containsRule = (line: string) =>
3535
matchComment.test(line) === false && matchWhitespace.test(line) === false;
3636

37-
const normalizeRule = (rule: string) =>
38-
/*
39-
Users of parse-domain should only pass parsed hostnames (e.g. via new URL("...").hostname)
40-
to parseDomain(). This frees us from the burden of URL parsing.
41-
See also https://github.com/peerigon/parse-domain/issues/14
42-
43-
The problem is that new URL("...").hostname will also translate all non-ASCII characters
44-
into punycode (e.g. 大分.jp becomes xn--kbrq7o.jp). This means for us that our serialized
45-
trie needs to use punyencoded hostnames.
46-
47-
The downside of this is that punycode hostnames are bigger in terms of file size.
48-
However, that's still better than including a punycode library into the runtime code of parse-domain.
49-
Gzip is also very effective in removing duplicate characters.
50-
*/
51-
toASCII(rule).toLowerCase();
52-
5337
const parsePsl = (listContent: string) => {
5438
return {
5539
icann: extractByMarkers(
@@ -59,15 +43,15 @@ const parsePsl = (listContent: string) => {
5943
)
6044
.split(matchNewLine)
6145
.filter(containsRule)
62-
.map(normalizeRule),
46+
.map(toASCII),
6347
private: extractByMarkers(
6448
listContent,
6549
PUBLIC_SUFFIX_MARKER_PRIVATE_START,
6650
PUBLIC_SUFFIX_MARKER_PRIVATE_END
6751
)
6852
.split(matchNewLine)
6953
.filter(containsRule)
70-
.map(normalizeRule),
54+
.map(toASCII),
7155
};
7256
};
7357

src/publicsuffix.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { toASCII } from "punycode";
21
import { parseDomain, ParseResult, ParseResultType } from "./main";
2+
import { toASCII } from "./punycode";
33

44
describe("Official test suite from https://raw.githubusercontent.com/publicsuffix/list/main/tests/test_psl.txt", () => {
55
const canonical = (hostname: string) =>

src/punycode.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const toASCII = (hostname: string) => {
2+
return new URL(`http://${hostname}`).hostname;
3+
};

0 commit comments

Comments
 (0)