Skip to content

Commit

Permalink
feat: update email bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
e-moran committed Jul 9, 2024
1 parent 6b2ccf0 commit fd19ae9
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 75 deletions.
40 changes: 37 additions & 3 deletions analyze/edge-light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
EmailValidationConfig,
BotDetectionResult,
BotType,
EmailValidationResult,
} from "./wasm/arcjet_analyze_js_req.component.js";

import componentCoreWasm from "./wasm/arcjet_analyze_js_req.component.core.wasm?module";
Expand Down Expand Up @@ -43,6 +44,31 @@ async function init(context: AnalyzeContext) {
log.error(msg);
},
},
"arcjet:js-req/email-validator-overrides": {
isFreeEmail(domain) {
if (
[
"gmail.com",
"yahoo.com",
"hotmail.com",
"aol.com",
"hotmail.co.uk",
].includes(domain)
) {
return "yes";
}
return "unknown";
},
isDisposableEmail() {
return "unknown";
},
hasMxRecords() {
return "unknown";
},
hasGravatar() {
return "unknown";
},
},
};

try {
Expand Down Expand Up @@ -97,14 +123,22 @@ export async function isValidEmail(
context: AnalyzeContext,
candidate: string,
options?: EmailValidationConfig,
) {
): Promise<EmailValidationResult> {
const analyze = await init(context);
const optionsOrDefault = options || {
requireTopLevelDomain: true,
allowDomainLiteral: false,
blockedEmails: [],
};

if (typeof analyze !== "undefined") {
return analyze.isValidEmail(candidate, options);
return analyze.isValidEmail(candidate, optionsOrDefault);
} else {
// TODO: Fallback to JS if we don't have WASM?
return true;
return {
validity: "valid",
blocked: [],
};
}
}

Expand Down
40 changes: 37 additions & 3 deletions analyze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
EmailValidationConfig,
BotDetectionResult,
BotType,
EmailValidationResult,
} from "./wasm/arcjet_analyze_js_req.component.js";

import { wasm as componentCoreWasm } from "./wasm/arcjet_analyze_js_req.component.core.wasm?js";
Expand Down Expand Up @@ -57,6 +58,31 @@ async function init(context: AnalyzeContext) {
log.error(msg);
},
},
"arcjet:js-req/email-validator-overrides": {
isFreeEmail(domain) {
if (
[
"gmail.com",
"yahoo.com",
"hotmail.com",
"aol.com",
"hotmail.co.uk",
].includes(domain)
) {
return "yes";
}
return "unknown";
},
isDisposableEmail() {
return "unknown";
},
hasMxRecords() {
return "unknown";
},
hasGravatar() {
return "unknown";
},
},
};

try {
Expand Down Expand Up @@ -111,14 +137,22 @@ export async function isValidEmail(
context: AnalyzeContext,
candidate: string,
options?: EmailValidationConfig,
) {
): Promise<EmailValidationResult> {
const analyze = await init(context);
const optionsOrDefault = options || {
requireTopLevelDomain: true,
allowDomainLiteral: false,
blockedEmails: [],
};

if (typeof analyze !== "undefined") {
return analyze.isValidEmail(candidate, options);
return analyze.isValidEmail(candidate, optionsOrDefault);
} else {
// TODO: Fallback to JS if we don't have WASM?
return true;
return {
validity: "valid",
blocked: [],
};
}
}

Expand Down
Binary file modified analyze/wasm/arcjet_analyze_js_req.component.core.wasm
Binary file not shown.
Binary file modified analyze/wasm/arcjet_analyze_js_req.component.core2.wasm
Binary file not shown.
Binary file modified analyze/wasm/arcjet_analyze_js_req.component.core3.wasm
Binary file not shown.
21 changes: 18 additions & 3 deletions analyze/wasm/arcjet_analyze_js_req.component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,33 @@ export interface BotDetectionResult {
botType: BotType,
botScore: number,
}
/**
* # Variants
*
* ## `"valid"`
*
* ## `"invalid"`
*/
export type EmailValidity = 'valid' | 'invalid';
export interface EmailValidationResult {
validity: EmailValidity,
blocked: string[],
}
export interface EmailValidationConfig {
requireTopLevelDomain?: boolean,
allowDomainLiteral?: boolean,
requireTopLevelDomain: boolean,
allowDomainLiteral: boolean,
blockedEmails: string[],
}
import { ArcjetJsReqEmailValidatorOverrides } from './interfaces/arcjet-js-req-email-validator-overrides.js';
import { ArcjetJsReqLogger } from './interfaces/arcjet-js-req-logger.js';
export interface ImportObject {
'arcjet:js-req/email-validator-overrides': typeof ArcjetJsReqEmailValidatorOverrides,
'arcjet:js-req/logger': typeof ArcjetJsReqLogger,
}
export interface Root {
detectBot(headers: string, patternsAdd: string, patternsRemove: string): BotDetectionResult,
generateFingerprint(request: string, characteristics: string[]): string,
isValidEmail(candidate: string, options: EmailValidationConfig | undefined): boolean,
isValidEmail(candidate: string, options: EmailValidationConfig): EmailValidationResult,
}

/**
Expand Down
Loading

0 comments on commit fd19ae9

Please sign in to comment.