more
This commit is contained in:
dopeuni444
2025-07-31 12:23:33 +04:00
parent 20b46678b7
commit b5a22951ae
3401 changed files with 331100 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
'use strict';
// Adapted from:
// Copyright (c) 2017-2019 Justin Ridgewell, MIT Licensed, https://github.com/jridgewell/safe-decode-string-component
// Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>, MIT Licensed, http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
const internals = {};
exports.decode = function (string) {
let percentPos = string.indexOf('%');
if (percentPos === -1) {
return string;
}
let decoded = '';
let last = 0;
let codepoint = 0;
let startOfOctets = percentPos;
let state = internals.utf8.accept;
while (percentPos > -1 &&
percentPos < string.length) {
const high = internals.resolveHex(string[percentPos + 1], 4);
const low = internals.resolveHex(string[percentPos + 2], 0);
const byte = high | low;
const type = internals.utf8.data[byte];
state = internals.utf8.data[256 + state + type];
codepoint = (codepoint << 6) | (byte & internals.utf8.data[364 + type]);
if (state === internals.utf8.accept) {
decoded += string.slice(last, startOfOctets);
decoded += codepoint <= 0xFFFF
? String.fromCharCode(codepoint)
: String.fromCharCode(0xD7C0 + (codepoint >> 10), 0xDC00 + (codepoint & 0x3FF));
codepoint = 0;
last = percentPos + 3;
percentPos = string.indexOf('%', last);
startOfOctets = percentPos;
continue;
}
if (state === internals.utf8.reject) {
return null;
}
percentPos += 3;
if (percentPos >= string.length ||
string[percentPos] !== '%') {
return null;
}
}
return decoded + string.slice(last);
};
internals.resolveHex = function (char, shift) {
const i = internals.hex[char];
return i === undefined ? 255 : i << shift;
};
internals.hex = {
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4,
'5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
'a': 10, 'A': 10, 'b': 11, 'B': 11, 'c': 12,
'C': 12, 'd': 13, 'D': 13, 'e': 14, 'E': 14,
'f': 15, 'F': 15
};
internals.utf8 = {
accept: 12,
reject: 0,
data: [
// Maps bytes to character to a transition
0, 0, 0, 0, 0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0, 0, 48, 48, 0, 0, 0, 0, 0, 0, 0, 0,
0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// Maps the current transition to a mask that needs to apply to the byte
0x7F, 0x3F, 0x3F, 0x3F, 0x00, 0x1F, 0x0F, 0x0F, 0x0F, 0x07, 0x07, 0x07
]
};

View File

@@ -0,0 +1,123 @@
'use strict';
const Url = require('url');
const Errors = require('./errors');
const internals = {
minDomainSegments: 2,
nonAsciiRx: /[^\x00-\x7f]/,
domainControlRx: /[\x00-\x20@\:\/\\#!\$&\'\(\)\*\+,;=\?]/, // Control + space + separators
tldSegmentRx: /^[a-zA-Z](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$/,
domainSegmentRx: /^[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$/,
URL: Url.URL || URL // $lab:coverage:ignore$
};
exports.analyze = function (domain, options = {}) {
if (!domain) { // Catch null / undefined
return Errors.code('DOMAIN_NON_EMPTY_STRING');
}
if (typeof domain !== 'string') {
throw new Error('Invalid input: domain must be a string');
}
if (domain.length > 256) {
return Errors.code('DOMAIN_TOO_LONG');
}
const ascii = !internals.nonAsciiRx.test(domain);
if (!ascii) {
if (options.allowUnicode === false) { // Defaults to true
return Errors.code('DOMAIN_INVALID_UNICODE_CHARS');
}
domain = domain.normalize('NFC');
}
if (internals.domainControlRx.test(domain)) {
return Errors.code('DOMAIN_INVALID_CHARS');
}
domain = internals.punycode(domain);
// https://tools.ietf.org/html/rfc1035 section 2.3.1
if (options.allowFullyQualified &&
domain[domain.length - 1] === '.') {
domain = domain.slice(0, -1);
}
const minDomainSegments = options.minDomainSegments || internals.minDomainSegments;
const segments = domain.split('.');
if (segments.length < minDomainSegments) {
return Errors.code('DOMAIN_SEGMENTS_COUNT');
}
if (options.maxDomainSegments) {
if (segments.length > options.maxDomainSegments) {
return Errors.code('DOMAIN_SEGMENTS_COUNT_MAX');
}
}
const tlds = options.tlds;
if (tlds) {
const tld = segments[segments.length - 1].toLowerCase();
if (tlds.deny && tlds.deny.has(tld) ||
tlds.allow && !tlds.allow.has(tld)) {
return Errors.code('DOMAIN_FORBIDDEN_TLDS');
}
}
for (let i = 0; i < segments.length; ++i) {
const segment = segments[i];
if (!segment.length) {
return Errors.code('DOMAIN_EMPTY_SEGMENT');
}
if (segment.length > 63) {
return Errors.code('DOMAIN_LONG_SEGMENT');
}
if (i < segments.length - 1) {
if (!internals.domainSegmentRx.test(segment)) {
return Errors.code('DOMAIN_INVALID_CHARS');
}
}
else {
if (!internals.tldSegmentRx.test(segment)) {
return Errors.code('DOMAIN_INVALID_TLDS_CHARS');
}
}
}
return null;
};
exports.isValid = function (domain, options) {
return !exports.analyze(domain, options);
};
internals.punycode = function (domain) {
if (domain.includes('%')) {
domain = domain.replace(/%/g, '%25');
}
try {
return new internals.URL(`http://${domain}`).host;
}
catch (err) {
return domain;
}
};

View File

@@ -0,0 +1,170 @@
'use strict';
const Util = require('util');
const Domain = require('./domain');
const Errors = require('./errors');
const internals = {
nonAsciiRx: /[^\x00-\x7f]/,
encoder: new (Util.TextEncoder || TextEncoder)() // $lab:coverage:ignore$
};
exports.analyze = function (email, options) {
return internals.email(email, options);
};
exports.isValid = function (email, options) {
return !internals.email(email, options);
};
internals.email = function (email, options = {}) {
if (typeof email !== 'string') {
throw new Error('Invalid input: email must be a string');
}
if (!email) {
return Errors.code('EMPTY_STRING');
}
// Unicode
const ascii = !internals.nonAsciiRx.test(email);
if (!ascii) {
if (options.allowUnicode === false) { // Defaults to true
return Errors.code('FORBIDDEN_UNICODE');
}
email = email.normalize('NFC');
}
// Basic structure
const parts = email.split('@');
if (parts.length !== 2) {
return parts.length > 2 ? Errors.code('MULTIPLE_AT_CHAR') : Errors.code('MISSING_AT_CHAR');
}
const [local, domain] = parts;
if (!local) {
return Errors.code('EMPTY_LOCAL');
}
if (!options.ignoreLength) {
if (email.length > 254) { // http://tools.ietf.org/html/rfc5321#section-4.5.3.1.3
return Errors.code('ADDRESS_TOO_LONG');
}
if (internals.encoder.encode(local).length > 64) { // http://tools.ietf.org/html/rfc5321#section-4.5.3.1.1
return Errors.code('LOCAL_TOO_LONG');
}
}
// Validate parts
return internals.local(local, ascii) || Domain.analyze(domain, options);
};
internals.local = function (local, ascii) {
const segments = local.split('.');
for (const segment of segments) {
if (!segment.length) {
return Errors.code('EMPTY_LOCAL_SEGMENT');
}
if (ascii) {
if (!internals.atextRx.test(segment)) {
return Errors.code('INVALID_LOCAL_CHARS');
}
continue;
}
for (const char of segment) {
if (internals.atextRx.test(char)) {
continue;
}
const binary = internals.binary(char);
if (!internals.atomRx.test(binary)) {
return Errors.code('INVALID_LOCAL_CHARS');
}
}
}
};
internals.binary = function (char) {
return Array.from(internals.encoder.encode(char)).map((v) => String.fromCharCode(v)).join('');
};
/*
From RFC 5321:
Mailbox = Local-part "@" ( Domain / address-literal )
Local-part = Dot-string / Quoted-string
Dot-string = Atom *("." Atom)
Atom = 1*atext
atext = ALPHA / DIGIT / "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "/" / "=" / "?" / "^" / "_" / "`" / "{" / "|" / "}" / "~"
Domain = sub-domain *("." sub-domain)
sub-domain = Let-dig [Ldh-str]
Let-dig = ALPHA / DIGIT
Ldh-str = *( ALPHA / DIGIT / "-" ) Let-dig
ALPHA = %x41-5A / %x61-7A ; a-z, A-Z
DIGIT = %x30-39 ; 0-9
From RFC 6531:
sub-domain =/ U-label
atext =/ UTF8-non-ascii
UTF8-non-ascii = UTF8-2 / UTF8-3 / UTF8-4
UTF8-2 = %xC2-DF UTF8-tail
UTF8-3 = %xE0 %xA0-BF UTF8-tail /
%xE1-EC 2( UTF8-tail ) /
%xED %x80-9F UTF8-tail /
%xEE-EF 2( UTF8-tail )
UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) /
%xF1-F3 3( UTF8-tail ) /
%xF4 %x80-8F 2( UTF8-tail )
UTF8-tail = %x80-BF
Note: The following are not supported:
RFC 5321: address-literal, Quoted-string
RFC 5322: obs-*, CFWS
*/
internals.atextRx = /^[\w!#\$%&'\*\+\-/=\?\^`\{\|\}~]+$/; // _ included in \w
internals.atomRx = new RegExp([
// %xC2-DF UTF8-tail
'(?:[\\xc2-\\xdf][\\x80-\\xbf])',
// %xE0 %xA0-BF UTF8-tail %xE1-EC 2( UTF8-tail ) %xED %x80-9F UTF8-tail %xEE-EF 2( UTF8-tail )
'(?:\\xe0[\\xa0-\\xbf][\\x80-\\xbf])|(?:[\\xe1-\\xec][\\x80-\\xbf]{2})|(?:\\xed[\\x80-\\x9f][\\x80-\\xbf])|(?:[\\xee-\\xef][\\x80-\\xbf]{2})',
// %xF0 %x90-BF 2( UTF8-tail ) %xF1-F3 3( UTF8-tail ) %xF4 %x80-8F 2( UTF8-tail )
'(?:\\xf0[\\x90-\\xbf][\\x80-\\xbf]{2})|(?:[\\xf1-\\xf3][\\x80-\\xbf]{3})|(?:\\xf4[\\x80-\\x8f][\\x80-\\xbf]{2})'
].join('|'));

View File

@@ -0,0 +1,29 @@
'use strict';
exports.codes = {
EMPTY_STRING: 'Address must be a non-empty string',
FORBIDDEN_UNICODE: 'Address contains forbidden Unicode characters',
MULTIPLE_AT_CHAR: 'Address cannot contain more than one @ character',
MISSING_AT_CHAR: 'Address must contain one @ character',
EMPTY_LOCAL: 'Address local part cannot be empty',
ADDRESS_TOO_LONG: 'Address too long',
LOCAL_TOO_LONG: 'Address local part too long',
EMPTY_LOCAL_SEGMENT: 'Address local part contains empty dot-separated segment',
INVALID_LOCAL_CHARS: 'Address local part contains invalid character',
DOMAIN_NON_EMPTY_STRING: 'Domain must be a non-empty string',
DOMAIN_TOO_LONG: 'Domain too long',
DOMAIN_INVALID_UNICODE_CHARS: 'Domain contains forbidden Unicode characters',
DOMAIN_INVALID_CHARS: 'Domain contains invalid character',
DOMAIN_INVALID_TLDS_CHARS: 'Domain contains invalid tld character',
DOMAIN_SEGMENTS_COUNT: 'Domain lacks the minimum required number of segments',
DOMAIN_SEGMENTS_COUNT_MAX: 'Domain contains too many segments',
DOMAIN_FORBIDDEN_TLDS: 'Domain uses forbidden TLD',
DOMAIN_EMPTY_SEGMENT: 'Domain contains empty dot-separated segment',
DOMAIN_LONG_SEGMENT: 'Domain contains dot-separated segment that is too long'
};
exports.code = function (code) {
return { code, error: exports.codes[code] };
};