aboutsummaryrefslogtreecommitdiffstats
path: root/vanilla/node_modules/tldts-core/src/is-valid.ts
diff options
context:
space:
mode:
authorAdam Mathes <adam@adammathes.com>2026-02-13 21:34:48 -0800
committerAdam Mathes <adam@adammathes.com>2026-02-13 21:34:48 -0800
commit76cb9c2a39d477a64824a985ade40507e3bbade1 (patch)
tree41e997aa9c6f538d3a136af61dae9424db2005a9 /vanilla/node_modules/tldts-core/src/is-valid.ts
parent819a39a21ac992b1393244a4c283bbb125208c69 (diff)
downloadneko-76cb9c2a39d477a64824a985ade40507e3bbade1.tar.gz
neko-76cb9c2a39d477a64824a985ade40507e3bbade1.tar.bz2
neko-76cb9c2a39d477a64824a985ade40507e3bbade1.zip
feat(vanilla): add testing infrastructure and tests (NK-wjnczv)
Diffstat (limited to 'vanilla/node_modules/tldts-core/src/is-valid.ts')
-rw-r--r--vanilla/node_modules/tldts-core/src/is-valid.ts79
1 files changed, 79 insertions, 0 deletions
diff --git a/vanilla/node_modules/tldts-core/src/is-valid.ts b/vanilla/node_modules/tldts-core/src/is-valid.ts
new file mode 100644
index 0000000..03cc384
--- /dev/null
+++ b/vanilla/node_modules/tldts-core/src/is-valid.ts
@@ -0,0 +1,79 @@
+/**
+ * Implements fast shallow verification of hostnames. This does not perform a
+ * struct check on the content of labels (classes of Unicode characters, etc.)
+ * but instead check that the structure is valid (number of labels, length of
+ * labels, etc.).
+ *
+ * If you need stricter validation, consider using an external library.
+ */
+
+function isValidAscii(code: number): boolean {
+ return (
+ (code >= 97 && code <= 122) || (code >= 48 && code <= 57) || code > 127
+ );
+}
+
+/**
+ * Check if a hostname string is valid. It's usually a preliminary check before
+ * trying to use getDomain or anything else.
+ *
+ * Beware: it does not check if the TLD exists.
+ */
+export default function (hostname: string): boolean {
+ if (hostname.length > 255) {
+ return false;
+ }
+
+ if (hostname.length === 0) {
+ return false;
+ }
+
+ if (
+ /*@__INLINE__*/ !isValidAscii(hostname.charCodeAt(0)) &&
+ hostname.charCodeAt(0) !== 46 && // '.' (dot)
+ hostname.charCodeAt(0) !== 95 // '_' (underscore)
+ ) {
+ return false;
+ }
+
+ // Validate hostname according to RFC
+ let lastDotIndex = -1;
+ let lastCharCode = -1;
+ const len = hostname.length;
+
+ for (let i = 0; i < len; i += 1) {
+ const code = hostname.charCodeAt(i);
+ if (code === 46 /* '.' */) {
+ if (
+ // Check that previous label is < 63 bytes long (64 = 63 + '.')
+ i - lastDotIndex > 64 ||
+ // Check that previous character was not already a '.'
+ lastCharCode === 46 ||
+ // Check that the previous label does not end with a '-' (dash)
+ lastCharCode === 45 ||
+ // Check that the previous label does not end with a '_' (underscore)
+ lastCharCode === 95
+ ) {
+ return false;
+ }
+
+ lastDotIndex = i;
+ } else if (
+ !(/*@__INLINE__*/ (isValidAscii(code) || code === 45 || code === 95))
+ ) {
+ // Check if there is a forbidden character in the label
+ return false;
+ }
+
+ lastCharCode = code;
+ }
+
+ return (
+ // Check that last label is shorter than 63 chars
+ len - lastDotIndex - 1 <= 63 &&
+ // Check that the last character is an allowed trailing label character.
+ // Since we already checked that the char is a valid hostname character,
+ // we only need to check that it's different from '-'.
+ lastCharCode !== 45
+ );
+}