@brilab-mailer/utils 0.2.0 → 0.2.2

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.
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EmailField = EmailField;
4
+ const common_1 = require("@nestjs/common");
5
+ const class_validator_1 = require("class-validator");
6
+ const class_transformer_1 = require("class-transformer");
7
+ const is_real_email_decorator_js_1 = require("./is-real-email.decorator.js");
8
+ function EmailField(options) {
9
+ return (0, common_1.applyDecorators)((0, class_validator_1.IsEmail)(), (0, is_real_email_decorator_js_1.IsRealEmail)(), (0, class_validator_1.IsString)(), (0, class_validator_1.MaxLength)(255), (0, class_validator_1.Matches)(/^(?!\s*$).+/, { message: 'should not be only spaces' }), ...(options?.required !== false ? [(0, class_validator_1.IsNotEmpty)()] : []), (0, class_transformer_1.Transform)(({ value }) => (value === '' ? null : value)));
10
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./is-real-email.decorator.js"), exports);
18
+ __exportStar(require("./email-field.decorator.js"), exports);
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.IsRealEmailConstraint = void 0;
10
+ exports.IsRealEmail = IsRealEmail;
11
+ const class_validator_1 = require("class-validator");
12
+ const promises_1 = require("node:dns/promises");
13
+ const DEFAULTS = {
14
+ disabled: false,
15
+ servers: ['1.1.1.1', '8.8.8.8'],
16
+ timeoutMs: 3000,
17
+ cacheTtlMs: 5 * 60 * 1000,
18
+ failOpen: true,
19
+ };
20
+ const mxCache = new Map();
21
+ const TIMEOUT = Symbol('timeout');
22
+ let IsRealEmailConstraint = class IsRealEmailConstraint {
23
+ async validate(email, args) {
24
+ const opts = { ...DEFAULTS, ...(args?.constraints?.[0] ?? {}) };
25
+ if (opts.disabled)
26
+ return true;
27
+ if (typeof email !== 'string')
28
+ return false;
29
+ const domain = email.split('@').at(-1)?.trim().toLowerCase();
30
+ if (!domain)
31
+ return false;
32
+ const cached = mxCache.get(domain);
33
+ const now = Date.now();
34
+ if (cached && cached.expires > now)
35
+ return cached.result;
36
+ const result = await this.lookup(domain, opts);
37
+ // Only cache definitive answers, not fail-open timeouts.
38
+ if (result.cacheable) {
39
+ mxCache.set(domain, { result: result.value, expires: now + opts.cacheTtlMs });
40
+ }
41
+ return result.value;
42
+ }
43
+ async lookup(domain, opts) {
44
+ const resolver = new promises_1.Resolver();
45
+ if (opts.servers?.length)
46
+ resolver.setServers(opts.servers);
47
+ let timer;
48
+ const timeout = new Promise((resolve) => {
49
+ timer = setTimeout(() => resolve(TIMEOUT), opts.timeoutMs);
50
+ });
51
+ try {
52
+ const outcome = await Promise.race([resolver.resolveMx(domain), timeout]);
53
+ if (outcome === TIMEOUT)
54
+ return { value: opts.failOpen ?? true, cacheable: false };
55
+ return { value: Array.isArray(outcome) && outcome.length > 0, cacheable: true };
56
+ }
57
+ catch {
58
+ // Genuine resolution failure (NXDOMAIN/ENODATA): domain cannot receive mail.
59
+ return { value: false, cacheable: true };
60
+ }
61
+ finally {
62
+ if (timer)
63
+ clearTimeout(timer);
64
+ }
65
+ }
66
+ defaultMessage() {
67
+ return 'This email cannot receive messages (invalid domain)';
68
+ }
69
+ };
70
+ exports.IsRealEmailConstraint = IsRealEmailConstraint;
71
+ exports.IsRealEmailConstraint = IsRealEmailConstraint = __decorate([
72
+ (0, class_validator_1.ValidatorConstraint)({ async: true })
73
+ ], IsRealEmailConstraint);
74
+ function IsRealEmail(options) {
75
+ const { disabled, servers, timeoutMs, cacheTtlMs, failOpen, ...validationOptions } = options ?? {};
76
+ const constraintOptions = {
77
+ disabled,
78
+ servers,
79
+ timeoutMs,
80
+ cacheTtlMs,
81
+ failOpen,
82
+ };
83
+ return function (target, propertyKey) {
84
+ (0, class_validator_1.registerDecorator)({
85
+ name: 'IsRealEmail',
86
+ target: target.constructor,
87
+ propertyName: String(propertyKey),
88
+ options: validationOptions,
89
+ constraints: [constraintOptions],
90
+ validator: IsRealEmailConstraint,
91
+ });
92
+ };
93
+ }
package/cjs/index.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./decorators/index.js"), exports);
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@brilab-mailer/utils",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "author": "Bohdan Radchenko <radchenkobs@gmail.com>",
5
5
  "type": "module",
6
- "main": "./index.js",
6
+ "main": "./cjs/index.js",
7
7
  "module": "./index.js",
8
8
  "types": "./index.d.ts",
9
9
  "exports": {
@@ -11,7 +11,7 @@
11
11
  ".": {
12
12
  "types": "./index.d.ts",
13
13
  "import": "./index.js",
14
- "default": "./index.js"
14
+ "require": "./cjs/index.js"
15
15
  }
16
16
  },
17
17
  "files": [