fix: fork qed-mail

This commit is contained in:
orion 2024-02-07 15:23:45 -06:00
parent 3d9aa86ffe
commit a91ed02d7d
Signed by: orion
GPG Key ID: 6D4165AE4C928719
19 changed files with 4201 additions and 0 deletions

130
qed-mail/.gitignore vendored Normal file
View File

@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

24
qed-mail/README.md Normal file
View File

@ -0,0 +1,24 @@
<br />
<h1 align="center">qed-mail</h1>
<h4 align="center">A NodeJS library for checking if an email address exists without sending any email.</h4>
<br /><br />
## Get Started
### Install
Use `npm` to install this package.
```bash
npm install --save-dev qed-mail
```
### Example
```ts
import qedmail from "qed-mail";
qedmail.checkEmail("example@example.com").then(console.log);
```

View File

@ -0,0 +1,25 @@
import { checkEmail } from "../src/index";
async function wait(ms: number) {
return new Promise((resolve) => {
setTimeout(() => resolve(null), ms);
});
}
async function main() {
const reachable: string[] = [];
for (let i = 100; i < 1000; i++) {
const resp = await checkEmail(`chotnt${i}@naver.com`);
if (resp.reachable) reachable.push(resp.email);
console.log(JSON.stringify(resp, null, 2));
await wait(300);
}
console.log("📮 REACHABLE > ", reachable);
}
main();
// 📮 REACHABLE > [ 'chotnt321@naver.com', 'chotnt741@naver.com', 'chotnt767@naver.com' ]

5
qed-mail/jest.config.js Normal file
View File

@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};

3773
qed-mail/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

40
qed-mail/package.json Normal file
View File

@ -0,0 +1,40 @@
{
"name": "qed-mail",
"version": "1.0.1",
"description": "📮 A NodeJS library for checking if an email address exists without sending any email.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"prettier": "prettier --write .",
"test": "jest"
},
"bugs": {
"url": "https://github.com/dontdonut/qed-mail/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/dontdonut/qed-mail"
},
"keywords": [
"email",
"disposable",
"email-validation",
"email-verify",
"check-email"
],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/jest": "^29.4.0",
"@types/node": "^18.14.1",
"jest": "^29.4.3",
"prettier": "^2.8.4",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
},
"dependencies": {
"smtp-fetch": "^1.0.5"
}
}

0
qed-mail/src/error.ts Normal file
View File

36
qed-mail/src/index.ts Normal file
View File

@ -0,0 +1,36 @@
import { checkMX } from "./mx";
import { checkSMTP } from "./smtp";
import { checkSyntax } from "./syntax";
import { Result } from "./types";
const DEFAULT_RESULT = {
reachable: false,
syntax: { valid: false },
mx: { valid: false },
smtp: { valid: false },
};
async function checkEmail(email: string): Promise<Result> {
const result: Result = { email, ...DEFAULT_RESULT };
const syntax = checkSyntax(email);
if (!syntax.valid) {
return { ...result, syntax };
}
const mx = await checkMX(syntax.domain!);
if (!mx.valid || !mx.mxRecords) {
return { ...result, syntax, mx };
}
const records = mx.mxRecords.sort((a, b) => a.priority - b.priority);
const smtp = await checkSMTP(email, records[0]!.exchange, 25);
if (!smtp.valid) {
return { ...result, syntax, mx, smtp };
}
return { ...result, reachable: true, syntax, mx, smtp };
}
export { checkEmail, checkMX, checkSMTP, checkSyntax };

1
qed-mail/src/mx/index.ts Normal file
View File

@ -0,0 +1 @@
export { default as checkMX } from "./verify";

16
qed-mail/src/mx/verify.ts Normal file
View File

@ -0,0 +1,16 @@
import dns from "dns";
import { MxResult } from "../types";
async function checkMX(domain: string): Promise<MxResult> {
return new Promise((resolve) => {
dns.resolveMx(domain, (err, mxRecords) => {
if (err || mxRecords.length === 0) {
return resolve({ valid: false, mxRecords: [] });
} else {
resolve({ valid: true, mxRecords });
}
});
});
}
export default checkMX;

View File

@ -0,0 +1 @@
export { default as checkSMTP } from "./verify";

View File

@ -0,0 +1,32 @@
import { Client } from "smtp-fetch";
import { SMTPResult } from "../types";
async function checkSMTP(
to: string,
host: string,
port: number,
timeout: number = 5000
): Promise<SMTPResult> {
const c = new Client(host, port);
try {
await c.connect({ timeout });
await c.mail("");
await c.rcpt(to);
await c.quit();
return {
valid: true,
};
} catch (err: any) {
return {
valid: false,
error: err.message,
};
} finally {
c.close();
}
}
export default checkSMTP;

View File

@ -0,0 +1 @@
export { default as checkSyntax } from "./verify";

View File

@ -0,0 +1,16 @@
import { SyntaxResult } from "../types";
const EMAIL_REGEX =
/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;
function checkSyntax(email: string): SyntaxResult {
if (!EMAIL_REGEX.test(email)) {
return { valid: false };
}
const [username, domain] = email.split("@");
return { username, domain, valid: true };
}
export default checkSyntax;

25
qed-mail/src/types.ts Normal file
View File

@ -0,0 +1,25 @@
import type { MxRecord } from "dns";
export type SyntaxResult = {
valid: boolean;
username?: string;
domain?: string;
};
export type MxResult = {
valid: boolean;
mxRecords?: MxRecord[];
};
export type SMTPResult = {
valid: boolean;
error?: string;
};
export type Result = {
email: string;
reachable: boolean;
syntax: SyntaxResult;
mx: MxResult;
smtp: SMTPResult;
};

13
qed-mail/test/lib.test.ts Normal file
View File

@ -0,0 +1,13 @@
import { checkEmail } from "../src/index";
describe("Check Email by multilayer pipelines", () => {
it("reachable", async () => {
const result = await checkEmail("chotnt741@gmail.com");
expect(result.reachable).toBe(true);
});
it("unreachable", async () => {
const result = await checkEmail("chotnt999@gmail.com");
expect(result.reachable).toBe(false);
});
});

5
qed-mail/test/mx.test.ts Normal file
View File

@ -0,0 +1,5 @@
describe("A DNS MX record directs email to a mail server.", () => {
it("If domain is unreachable, checkMX returns empty array", async () => {});
it("If domain is reachable, checkMX returns exchanges", async () => {});
});

View File

@ -0,0 +1,17 @@
import { checkSyntax } from "../src/syntax";
describe("Validate Email by REGEX", () => {
it("If email is validate, set SyntaxResult.valid as true", () => {
const syntax = checkSyntax("chotnt741@gmail.com");
expect(syntax.valid).toBe(true);
expect(syntax.username).toBe("chotnt741");
expect(syntax.domain).toBe("gmail.com");
});
it("If email is validate, set SyntaxResult.valid as false", () => {
const syntax = checkSyntax("chotnt741@gmailcom");
expect(syntax.valid).toBe(false);
});
});

41
qed-mail/tsconfig.json Normal file
View File

@ -0,0 +1,41 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
// Recommended Node options have been incorporated from https://github.com/tsconfig/bases/blob/master/bases/node14.json
"target": "ES5",
"module": "commonjs",
"esModuleInterop": true,
// Overrides default in order to remove "dom" because this package shouldn't assume the presence of browser APIs
"lib": ["ES2022", "DOM"],
// Emit location
"outDir": "dist",
// Emit sourcemaps
"declarationMap": true,
"sourceMap": true,
"inlineSources": true,
// Emit type definitions
"declaration": true,
// Strict mode
"strict": true,
// Allow import package.json
"resolveJsonModule": true,
// Linter style rules
"noUnusedLocals": false, // Disabled because we use eslint for this.
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"noPropertyAccessFromIndexSignature": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*", "src/index.d.tsd.tssrc/index.d.tsdex.d.ts"]
}