resolve-email 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Bret Comnes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # resolve-email
2
+ [![latest version](https://img.shields.io/npm/v/resolve-email.svg)](https://img.shields.io/npm/v/resolve-email.svg)
3
+ [![Actions Status](https://github.com/bcomnes/resolve-email/workflows/tests/badge.svg)](https://github.com/bcomnes/resolve-email/actions)
4
+ [![Coverage Status](https://coveralls.io/repos/github/bcomnes/resolve-email/badge.svg?branch=master)](https://coveralls.io/github/bcomnes/resolve-email?branch=master)
5
+ [![downloads](https://img.shields.io/npm/dm/resolve-email.svg)](https://npmtrends.com/resolve-email)
6
+ [![Socket Badge](https://socket.dev/api/badge/npm/package/resolve-email)](https://socket.dev/npm/package/resolve-email)
7
+
8
+ Resolve the domain of a syntactically valid email address to see if there is even a chance of deliverability.
9
+
10
+ ```
11
+ npm install resolve-email
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ``` js
17
+ import { resolveEmail } from 'resolve-email'
18
+
19
+ // Validate the email address before passing it in here:
20
+ const results = await resolveEmail('person@gmailc.om')
21
+
22
+ console.log(results)
23
+ // results.emailResolves true/false
24
+ // results.mxRecords [array of mx records and priorities]
25
+ // results.error any errors that may have occurred.
26
+ ```
27
+
28
+ ## See also
29
+
30
+ This module was adapted from [nodemailer/nodemailer-direct-transport](https://github.com/nodemailer/nodemailer-direct-transport/blob/v3.3.2/lib/direct-transport.js#L438)
31
+
32
+ ## License
33
+
34
+ MIT
package/index.js ADDED
@@ -0,0 +1,64 @@
1
+ import { isIP } from 'node:net'
2
+ import { resolveMx, resolve4, resolve6 } from 'node:dns/promises'
3
+
4
+ export async function _resolveMx (email, opts) {
5
+ opts = {
6
+ allowIps: false,
7
+ ...opts
8
+ }
9
+ const domain = (email.split('@').pop() || '').toLowerCase().trim().replace(/^\[(ipv6:)?|\]$/gi, '')
10
+
11
+ if (isIP(domain)) {
12
+ if (opts.allowIps) {
13
+ return [{
14
+ priority: 0,
15
+ exchange: domain
16
+ }]
17
+ } else {
18
+ throw new Error('An email address with an IP address for the domain was is disallowed')
19
+ }
20
+ }
21
+
22
+ try {
23
+ const resolved = await resolveMx(domain)
24
+ return resolved.sort((a, b) => (a?.priority ?? 0) - (b?.priority ?? 0))
25
+ } catch (err) {
26
+ if (!['ENODATA', 'ENOTFOUND'].includes(err.code)) {
27
+ throw err
28
+ }
29
+
30
+ try {
31
+ return await ipFallback(domain, resolve4)
32
+ } catch (err) {
33
+ if (!['ENODATA', 'ENOTFOUND'].includes(err.code)) {
34
+ throw err
35
+ }
36
+ return await ipFallback(domain, resolve6)
37
+ }
38
+ }
39
+ }
40
+
41
+ async function ipFallback (domain, resolver) {
42
+ const aList = await resolver(domain)
43
+
44
+ // return the first resolved IP with priority 0
45
+ return [...aList].map(entry => ({
46
+ priority: 0,
47
+ exchange: entry
48
+ })).slice(0, 1)
49
+ }
50
+
51
+ export async function resolveEmail (domain, opts) {
52
+ try {
53
+ const entries = await _resolveMx(domain, opts)
54
+ return {
55
+ emailResolves: entries.length > 0,
56
+ mxRecords: entries
57
+ }
58
+ } catch (err) {
59
+ return {
60
+ emailResolves: false,
61
+ error: err
62
+ }
63
+ }
64
+ }
package/index.test.js ADDED
@@ -0,0 +1,22 @@
1
+ import test from 'node:test'
2
+ import assert from 'node:assert'
3
+ import { resolveEmail } from './index.js'
4
+
5
+ const inputs = [
6
+ {
7
+ in: 'bcomnes@gmail.com',
8
+ expect: true
9
+ },
10
+ {
11
+ in: 'bcomnes@gmailc.om',
12
+ expect: false
13
+ }
14
+ ]
15
+
16
+ for (const i of inputs) {
17
+ test(`${i.in} ${i.expect ? 'resolves' : 'does not resolve'}`, async (t) => {
18
+ const results = await resolveEmail(i.in)
19
+
20
+ assert.strictEqual(results.emailResolves, i.expect, `${i.in} ${i.expect ? 'resolves' : 'does not resolve'}`)
21
+ })
22
+ }
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "resolve-email",
3
+ "description": "Resolve the domain of an email address to see if it even has a chance of delivering",
4
+ "files": [
5
+ "*.js",
6
+ "lib/*.js"
7
+ ],
8
+ "version": "1.0.0",
9
+ "author": "Bret Comnes <bcomnes@gmail.com> (https://bret.io)",
10
+ "bugs": {
11
+ "url": "https://github.com/bcomnes/resolve-email/issues"
12
+ },
13
+ "dependencies": {},
14
+ "devDependencies": {
15
+ "standard": "^17.0.0",
16
+ "npm-run-all2": "^6.0.0",
17
+ "auto-changelog": "^2.0.0",
18
+ "gh-release": "^7.0.0",
19
+ "c8": "^8.0.0",
20
+ "dependency-cruiser": "^13.0.0"
21
+ },
22
+ "engines": {
23
+ "node": ">=18.0.0",
24
+ "npm": ">=8.0.0"
25
+ },
26
+ "homepage": "https://github.com/bcomnes/resolve-email",
27
+ "keywords": [],
28
+ "license": "MIT",
29
+ "type": "module",
30
+ "module": "index.js",
31
+ "exports": {
32
+ "import": "./index.js"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/bcomnes/resolve-email.git"
37
+ },
38
+ "scripts": {
39
+ "prepublishOnly": "git push --follow-tags && gh-release -y",
40
+ "test": "run-s test:*",
41
+ "test:deptree": "depcruise --validate .dependency-cruiser.json .",
42
+ "test:standard": "standard",
43
+ "test:node-test": "METRICS=0 c8 node --test --test-reporter spec",
44
+ "version": "run-s version:*",
45
+ "version:changelog": "auto-changelog -p --template keepachangelog auto-changelog --breaking-pattern 'BREAKING CHANGE:'",
46
+ "version:git": "git add CHANGELOG.md",
47
+ "deps": "depcruise --exclude '^node_modules' --output-type dot . | dot -T svg | depcruise-wrap-stream-in-html > dependency-graph.html"
48
+ },
49
+ "standard": {
50
+ "ignore": [
51
+ "dist"
52
+ ]
53
+ },
54
+ "funding": {
55
+ "type": "individual",
56
+ "url": "https://github.com/sponsors/bcomnes"
57
+ },
58
+ "c8": {
59
+ "reporter": [
60
+ "lcov",
61
+ "text"
62
+ ]
63
+ }
64
+ }