67 lines
2.7 KiB
JavaScript
67 lines
2.7 KiB
JavaScript
![]() |
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.canonicalize = exports.canonicalize_quoted_string = exports.normalize = exports.normalize_dot_string = exports.parse = void 0;
|
||
|
// const punycode = require('punycode');
|
||
|
const nearley = require("nearley");
|
||
|
const grammar_1 = __importDefault(require("./grammar"));
|
||
|
grammar_1.default.ParserStart = "Mailbox";
|
||
|
const grammar = nearley.Grammar.fromCompiled(grammar_1.default);
|
||
|
// <https://tools.ietf.org/html/rfc5321#section-4.1.2>
|
||
|
function parse(address) {
|
||
|
const parser = new nearley.Parser(grammar);
|
||
|
parser.feed(address);
|
||
|
if (parser.results.length !== 1) {
|
||
|
throw new Error("address parsing failed: ambiguous grammar");
|
||
|
}
|
||
|
return parser.results[0];
|
||
|
}
|
||
|
exports.parse = parse;
|
||
|
/** Strip +something, strip '.'s, and map to lower case.
|
||
|
*/
|
||
|
function normalize_dot_string(dot_string) {
|
||
|
const tagless = (function () {
|
||
|
const plus_loc = dot_string.indexOf("+");
|
||
|
if (plus_loc === -1) {
|
||
|
return dot_string;
|
||
|
}
|
||
|
return dot_string.substr(0, plus_loc);
|
||
|
})();
|
||
|
const dotless = tagless.replace(/\./g, "");
|
||
|
return dotless.toLowerCase();
|
||
|
}
|
||
|
exports.normalize_dot_string = normalize_dot_string;
|
||
|
/** The G style address normalization.
|
||
|
*/
|
||
|
function normalize(address) {
|
||
|
var _a, _b;
|
||
|
const a = parse(address);
|
||
|
const domain = (_a = a.domainPart.AddressLiteral) !== null && _a !== void 0 ? _a : a.domainPart.DomainName.toLowerCase();
|
||
|
const local = (_b = a.localPart.QuotedString) !== null && _b !== void 0 ? _b : normalize_dot_string(a.localPart.DotString);
|
||
|
return `${local}@${domain}`;
|
||
|
}
|
||
|
exports.normalize = normalize;
|
||
|
function canonicalize_quoted_string(quoted_string) {
|
||
|
const unquoted = quoted_string.substr(1).substr(0, quoted_string.length - 2);
|
||
|
const unescaped = unquoted.replace(/(?:\\(.))/g, "$1");
|
||
|
const reescaped = unescaped.replace(/(?:(["\\]))/g, "\\$1");
|
||
|
return `"${reescaped}"`; // re-quote
|
||
|
}
|
||
|
exports.canonicalize_quoted_string = canonicalize_quoted_string;
|
||
|
/**
|
||
|
* Apply a canonicalization consistent with standards to support
|
||
|
* comparison as a string.
|
||
|
*/
|
||
|
function canonicalize(address) {
|
||
|
var _a;
|
||
|
const a = parse(address);
|
||
|
const domain = (_a = a.domainPart.AddressLiteral) !== null && _a !== void 0 ? _a : a.domainPart.DomainName.toLowerCase();
|
||
|
const local = a.localPart.QuotedString
|
||
|
? canonicalize_quoted_string(a.localPart.QuotedString)
|
||
|
: a.localPart.DotString;
|
||
|
return `${local}@${domain}`;
|
||
|
}
|
||
|
exports.canonicalize = canonicalize;
|
||
|
//# sourceMappingURL=index.js.map
|