UNPKG

@sparser/email-address-parser

Version:

An RFC 5322, and RFC 6532 compliant email address parser.

450 lines (402 loc) 13.5 kB
let imports = {}; imports['__wbindgen_placeholder__'] = module.exports; let wasm; const { TextDecoder, TextEncoder } = require(`util`); const heap = new Array(32).fill(undefined); heap.push(undefined, null, true, false); function getObject(idx) { return heap[idx]; } let heap_next = heap.length; function dropObject(idx) { if (idx < 36) return; heap[idx] = heap_next; heap_next = idx; } function takeObject(idx) { const ret = getObject(idx); dropObject(idx); return ret; } let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); cachedTextDecoder.decode(); let cachedUint8Memory0 = new Uint8Array(); function getUint8Memory0() { if (cachedUint8Memory0.byteLength === 0) { cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); } return cachedUint8Memory0; } function getStringFromWasm0(ptr, len) { return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); } let WASM_VECTOR_LEN = 0; let cachedTextEncoder = new TextEncoder('utf-8'); const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' ? function (arg, view) { return cachedTextEncoder.encodeInto(arg, view); } : function (arg, view) { const buf = cachedTextEncoder.encode(arg); view.set(buf); return { read: arg.length, written: buf.length }; }); function passStringToWasm0(arg, malloc, realloc) { if (realloc === undefined) { const buf = cachedTextEncoder.encode(arg); const ptr = malloc(buf.length); getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); WASM_VECTOR_LEN = buf.length; return ptr; } let len = arg.length; let ptr = malloc(len); const mem = getUint8Memory0(); let offset = 0; for (; offset < len; offset++) { const code = arg.charCodeAt(offset); if (code > 0x7F) break; mem[ptr + offset] = code; } if (offset !== len) { if (offset !== 0) { arg = arg.slice(offset); } ptr = realloc(ptr, len, len = offset + arg.length * 3); const view = getUint8Memory0().subarray(ptr + offset, ptr + len); const ret = encodeString(arg, view); offset += ret.written; } WASM_VECTOR_LEN = offset; return ptr; } function isLikeNone(x) { return x === undefined || x === null; } function _assertClass(instance, klass) { if (!(instance instanceof klass)) { throw new Error(`expected instance of ${klass.name}`); } return instance.ptr; } let cachedInt32Memory0 = new Int32Array(); function getInt32Memory0() { if (cachedInt32Memory0.byteLength === 0) { cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); } return cachedInt32Memory0; } function addHeapObject(obj) { if (heap_next === heap.length) heap.push(heap.length + 1); const idx = heap_next; heap_next = heap[idx]; heap[idx] = obj; return idx; } /** * Email address struct. * * # Examples * ``` * use email_address_parser::EmailAddress; * * assert!(EmailAddress::parse("foo@-bar.com", None).is_none()); * let email = EmailAddress::parse("foo@bar.com", None); * assert!(email.is_some()); * let email = email.unwrap(); * assert_eq!(email.get_local_part(), "foo"); * assert_eq!(email.get_domain(), "bar.com"); * assert_eq!(format!("{}", email), "foo@bar.com"); * ``` */ class EmailAddress { static __wrap(ptr) { const obj = Object.create(EmailAddress.prototype); obj.ptr = ptr; return obj; } __destroy_into_raw() { const ptr = this.ptr; this.ptr = 0; return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_emailaddress_free(ptr); } /** * This is a WASM wrapper over EmailAddress::new that panics. * If you are using this lib from Rust then consider using EmailAddress::new. * * # Examples * ``` * use email_address_parser::EmailAddress; * * let email = EmailAddress::_new("foo", "bar.com", None); * ``` * * # Panics * * This method panics if the local part or domain is invalid. * * ```rust,should_panic * use email_address_parser::EmailAddress; * * EmailAddress::_new("foo", "-bar.com", None); * ``` * @param {string} local_part * @param {string} domain * @param {ParsingOptions | undefined} options */ constructor(local_part, domain, options) { const ptr0 = passStringToWasm0(local_part, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ptr1 = passStringToWasm0(domain, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; let ptr2 = 0; if (!isLikeNone(options)) { _assertClass(options, ParsingOptions); ptr2 = options.ptr; options.ptr = 0; } const ret = wasm.emailaddress__new(ptr0, len0, ptr1, len1, ptr2); return EmailAddress.__wrap(ret); } /** * Parses a given string as an email address. * * Accessible from WASM. * * Returns `Some(EmailAddress)` if the parsing is successful, else `None`. * # Examples * ``` * use email_address_parser::*; * * // strict parsing * let email = EmailAddress::parse("foo@bar.com", None); * assert!(email.is_some()); * let email = email.unwrap(); * assert_eq!(email.get_local_part(), "foo"); * assert_eq!(email.get_domain(), "bar.com"); * * // non-strict parsing * let email = EmailAddress::parse("\u{0d}\u{0a} \u{0d}\u{0a} test@iana.org", Some(ParsingOptions::new(true))); * assert!(email.is_some()); * * // parsing invalid address * let email = EmailAddress::parse("test@-iana.org", Some(ParsingOptions::new(true))); * assert!(email.is_none()); * let email = EmailAddress::parse("test@-iana.org", Some(ParsingOptions::new(true))); * assert!(email.is_none()); * let email = EmailAddress::parse("test", Some(ParsingOptions::new(true))); * assert!(email.is_none()); * let email = EmailAddress::parse("test", Some(ParsingOptions::new(true))); * assert!(email.is_none()); * ``` * @param {string} input * @param {ParsingOptions | undefined} options * @returns {EmailAddress | undefined} */ static parse(input, options) { const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; let ptr1 = 0; if (!isLikeNone(options)) { _assertClass(options, ParsingOptions); ptr1 = options.ptr; options.ptr = 0; } const ret = wasm.emailaddress_parse(ptr0, len0, ptr1); return ret === 0 ? undefined : EmailAddress.__wrap(ret); } /** * Validates if the given `input` string is an email address or not. * * Returns `true` if the `input` is valid, `false` otherwise. * Unlike the `parse` method, it does not instantiate an `EmailAddress`. * # Examples * ``` * use email_address_parser::*; * * // strict validation * assert!(EmailAddress::is_valid("foo@bar.com", None)); * * // non-strict validation * assert!(EmailAddress::is_valid("\u{0d}\u{0a} \u{0d}\u{0a} test@iana.org", Some(ParsingOptions::new(true)))); * * // invalid address * assert!(!EmailAddress::is_valid("test@-iana.org", Some(ParsingOptions::new(true)))); * assert!(!EmailAddress::is_valid("test@-iana.org", Some(ParsingOptions::new(true)))); * assert!(!EmailAddress::is_valid("test", Some(ParsingOptions::new(true)))); * assert!(!EmailAddress::is_valid("test", Some(ParsingOptions::new(true)))); * ``` * @param {string} input * @param {ParsingOptions | undefined} options * @returns {boolean} */ static isValid(input, options) { const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; let ptr1 = 0; if (!isLikeNone(options)) { _assertClass(options, ParsingOptions); ptr1 = options.ptr; options.ptr = 0; } const ret = wasm.emailaddress_isValid(ptr0, len0, ptr1); return ret !== 0; } /** * Returns the local part of the email address. * * Note that if you are using this library from rust, then consider using the `get_local_part` method instead. * This returns a cloned copy of the local part string, instead of a borrowed `&str`, and exists purely for WASM interoperability. * * # Examples * ``` * use email_address_parser::EmailAddress; * * let email = EmailAddress::new("foo", "bar.com", None).unwrap(); * assert_eq!(email.localPart(), "foo"); * * let email = EmailAddress::parse("foo@bar.com", None).unwrap(); * assert_eq!(email.localPart(), "foo"); * ``` * @returns {string} */ get localPart() { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.emailaddress_localPart(retptr, this.ptr); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; return getStringFromWasm0(r0, r1); } finally { wasm.__wbindgen_add_to_stack_pointer(16); wasm.__wbindgen_free(r0, r1); } } /** * Returns the domain of the email address. * * Note that if you are using this library from rust, then consider using the `get_domain` method instead. * This returns a cloned copy of the domain string, instead of a borrowed `&str`, and exists purely for WASM interoperability. * * # Examples * ``` * use email_address_parser::EmailAddress; * * let email = EmailAddress::new("foo", "bar.com", None).unwrap(); * assert_eq!(email.domain(), "bar.com"); * * let email = EmailAddress::parse("foo@bar.com", None).unwrap(); * assert_eq!(email.domain(), "bar.com"); * ``` * @returns {string} */ get domain() { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.emailaddress_domain(retptr, this.ptr); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; return getStringFromWasm0(r0, r1); } finally { wasm.__wbindgen_add_to_stack_pointer(16); wasm.__wbindgen_free(r0, r1); } } /** * Returns the formatted EmailAddress. * This exists purely for WASM interoperability. * @returns {string} */ toString() { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.emailaddress_toString(retptr, this.ptr); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; return getStringFromWasm0(r0, r1); } finally { wasm.__wbindgen_add_to_stack_pointer(16); wasm.__wbindgen_free(r0, r1); } } } module.exports.EmailAddress = EmailAddress; /** * Options for parsing. * * The is only one available option so far `is_lax` which can be set to * `true` or `false` to enable/disable obsolete parts parsing. * The default is `false`. */ class ParsingOptions { static __wrap(ptr) { const obj = Object.create(ParsingOptions.prototype); obj.ptr = ptr; return obj; } __destroy_into_raw() { const ptr = this.ptr; this.ptr = 0; return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_parsingoptions_free(ptr); } /** * @returns {boolean} */ get is_lax() { const ret = wasm.__wbg_get_parsingoptions_is_lax(this.ptr); return ret !== 0; } /** * @param {boolean} arg0 */ set is_lax(arg0) { wasm.__wbg_set_parsingoptions_is_lax(this.ptr, arg0); } /** * @param {boolean} is_lax */ constructor(is_lax) { const ret = wasm.parsingoptions_new(is_lax); return ParsingOptions.__wrap(ret); } } module.exports.ParsingOptions = ParsingOptions; module.exports.__wbg_new_693216e109162396 = function() { const ret = new Error(); return addHeapObject(ret); }; module.exports.__wbg_stack_0ddaca5d1abfb52f = function(arg0, arg1) { const ret = getObject(arg1).stack; const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; getInt32Memory0()[arg0 / 4 + 1] = len0; getInt32Memory0()[arg0 / 4 + 0] = ptr0; }; module.exports.__wbg_error_09919627ac0992f5 = function(arg0, arg1) { try { console.error(getStringFromWasm0(arg0, arg1)); } finally { wasm.__wbindgen_free(arg0, arg1); } }; module.exports.__wbindgen_object_drop_ref = function(arg0) { takeObject(arg0); }; module.exports.__wbindgen_throw = function(arg0, arg1) { throw new Error(getStringFromWasm0(arg0, arg1)); }; const path = require('path').join(__dirname, 'email_address_parser_bg.wasm'); const bytes = require('fs').readFileSync(path); const wasmModule = new WebAssembly.Module(bytes); const wasmInstance = new WebAssembly.Instance(wasmModule, imports); wasm = wasmInstance.exports; module.exports.__wasm = wasm;