-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module to extract the domain name from a given hostname
- Loading branch information
Showing
6 changed files
with
8,466 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/node_modules/ | ||
/*.zip | ||
/node_modules/ | ||
/public_suffix_list.dat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/bin/bash | ||
|
||
set -o errexit -o nounset -o pipefail -o xtrace | ||
|
||
SCRIPT_DIRECTORY=$( cd "$( dirname "${BASH_SOURCE:-$0}" )" && pwd ) | ||
ROOT_DIRECTORY=$(realpath $SCRIPT_DIRECTORY/../) | ||
|
||
PSL_FILE="public_suffix_list.dat" | ||
PSL_RULES="pslrules.js" | ||
PSL_FILE_PATH=$ROOT_DIRECTORY/$PSL_FILE | ||
PSL_RULES_PATH=$ROOT_DIRECTORY/$PSL_RULES | ||
|
||
if [ ! -f $PSL_FILE_PATH ]; then | ||
wget --directory-prefix $ROOT_DIRECTORY https://publicsuffix.org/list/$PSL_FILE | ||
fi | ||
|
||
CONTENT=$(cat << END | ||
const pslrules = (function(root) { // eslint-disable-line no-unused-vars | ||
const EXCEPTION_ENTRIES = new Set([ | ||
$(cat $PSL_FILE_PATH | grep '^!' | sed -e 's/^!//' | awk '{print $1}' | sed -e 's/\(.*\)/ "\1",/') | ||
]); | ||
const WILDCARD_ENTRIES = new Set([ | ||
$(cat $PSL_FILE_PATH | grep '^*.' | sed -e 's/^\*\.//' | awk '{print $1}' | sed -e 's/\(.*\)/ "\1",/') | ||
]); | ||
const NORMAL_ENTRIES = new Set([ | ||
$(cat $PSL_FILE_PATH | grep -v '^//' | grep -v '^$' | grep -v '^!' | grep -v '^*.' | awk '{print $1}' | sed -e 's/\(.*\)/ "\1",/') | ||
]); | ||
root.EXCEPTION_ENTRIES = EXCEPTION_ENTRIES; | ||
root.WILDCARD_ENTRIES = WILDCARD_ENTRIES; | ||
root.NORMAL_ENTRIES = NORMAL_ENTRIES; | ||
return { | ||
EXCEPTION_ENTRIES: EXCEPTION_ENTRIES, | ||
WILDCARD_ENTRIES: WILDCARD_ENTRIES, | ||
NORMAL_ENTRIES: NORMAL_ENTRIES, | ||
}; | ||
})(this); | ||
END | ||
) | ||
|
||
echo "$CONTENT" > $PSL_RULES_PATH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* global pslrules */ | ||
const psl = (function(root) { // eslint-disable-line no-unused-vars | ||
|
||
if (typeof pslrules === "undefined") { | ||
pslrules = require("./pslrules"); // eslint-disable-line no-global-assign,no-unused-vars | ||
} | ||
|
||
function getDomain(hostname, previousHead=undefined) { | ||
if (!hostname) { | ||
return undefined; | ||
} | ||
|
||
if (pslrules.EXCEPTION_ENTRIES.has(hostname)) { | ||
return hostname; | ||
} | ||
|
||
let dotIndex = hostname.indexOf("."); | ||
if (dotIndex == -1) { | ||
return undefined; | ||
} | ||
|
||
let head = hostname.slice(0, dotIndex); | ||
let rest = hostname.slice(dotIndex + 1); | ||
|
||
if (pslrules.WILDCARD_ENTRIES.has(rest) && previousHead) { | ||
return [previousHead, head, rest].join("."); | ||
} | ||
if (pslrules.NORMAL_ENTRIES.has(rest)) { | ||
return [head, rest].join("."); | ||
} | ||
|
||
return getDomain(rest, head); | ||
} | ||
|
||
root.getDomain = getDomain; | ||
|
||
return { | ||
getDomain: getDomain, | ||
}; | ||
|
||
})(this); |
Oops, something went wrong.