Skip to content

Commit

Permalink
move conversions into their own file
Browse files Browse the repository at this point in the history
  • Loading branch information
e-moran committed Aug 14, 2024
1 parent e00da21 commit 3260b56
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 79 deletions.
4 changes: 1 addition & 3 deletions analyze/edge-light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import type {
ArcjetLogger,
ArcjetRequestDetails,
CustomDetect,
DetectedSensitiveInfoEntity,
DetectSensitiveInfoResult,
SensitiveInfoConfig,
SensitiveInfoEntity,
} from "@arcjet/protocol";

import * as core from "./wasm/arcjet_analyze_js_req.component.js";
Expand All @@ -24,7 +22,7 @@ import {
ConvertAnalyzeEntitiesToProtocolEntities,
ConvertDetectedSensitiveInfoEntityToAnalyzeEntity,
ConvertProtocolEntitiesToAnalyzeEntities,
} from "./index.js";
} from "./convert";

const FREE_EMAIL_PROVIDERS = [
"gmail.com",
Expand Down
78 changes: 5 additions & 73 deletions analyze/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type {
ArcjetLogger,
ArcjetRequestDetails,
SensitiveInfoEntity,
DetectedSensitiveInfoEntity,
DetectSensitiveInfoResult,
CustomDetect,
SensitiveInfoConfig,
Expand All @@ -16,6 +14,11 @@ import type {
BotType,
EmailValidationResult,
} from "./wasm/arcjet_analyze_js_req.component.js";
import {
ConvertAnalyzeEntitiesToProtocolEntities,
ConvertDetectedSensitiveInfoEntityToAnalyzeEntity,
ConvertProtocolEntitiesToAnalyzeEntities,
} from "./convert";

import { wasm as componentCoreWasm } from "./wasm/arcjet_analyze_js_req.component.core.wasm?js";
import { wasm as componentCore2Wasm } from "./wasm/arcjet_analyze_js_req.component.core2.wasm?js";
Expand All @@ -34,77 +37,6 @@ interface AnalyzeContext {
characteristics: string[];
}

export function ConvertProtocolEntitiesToAnalyzeEntities(
entity: SensitiveInfoEntity,
): core.SensitiveInfoEntity {
if (entity === "email") {
return { tag: "email" };
}

if (entity === "phone-number") {
return { tag: "phone-number" };
}

if (entity === "ip-address") {
return { tag: "ip-address" };
}

if (entity === "credit-card-number") {
return { tag: "credit-card-number" };
}

return {
tag: "custom",
val: "custom",
};
}

export function ConvertDetectedSensitiveInfoEntityToAnalyzeEntity(
entity?: DetectedSensitiveInfoEntity,
): core.SensitiveInfoEntity | undefined {
if (entity === "email") {
return { tag: "email" };
}

if (entity === "phone-number") {
return { tag: "phone-number" };
}

if (entity === "credit-card-number") {
return { tag: "credit-card-number" };
}

if (entity === "ip-address") {
return { tag: "ip-address" };
}

if (entity === "custom") {
return { tag: "custom", val: "custom" };
}
}

export function ConvertAnalyzeEntitiesToProtocolEntities(
entity: core.SensitiveInfoEntity,
): DetectedSensitiveInfoEntity {
if (entity.tag === "email") {
return "email";
}

if (entity.tag === "ip-address") {
return "ip-address";
}

if (entity.tag === "credit-card-number") {
return "credit-card-number";
}

if (entity.tag === "phone-number") {
return "phone-number";
}

return "custom";
}

// TODO: Do we actually need this wasmCache or does `import` cache correctly?
const wasmCache = new Map<string, WebAssembly.Module>();

Expand Down
4 changes: 1 addition & 3 deletions analyze/workerd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import type {
ArcjetLogger,
ArcjetRequestDetails,
CustomDetect,
DetectedSensitiveInfoEntity,
DetectSensitiveInfoResult,
SensitiveInfoConfig,
SensitiveInfoEntity,
} from "@arcjet/protocol";

import * as core from "./wasm/arcjet_analyze_js_req.component.js";
Expand All @@ -24,7 +22,7 @@ import {
ConvertAnalyzeEntitiesToProtocolEntities,
ConvertDetectedSensitiveInfoEntityToAnalyzeEntity,
ConvertProtocolEntitiesToAnalyzeEntities,
} from "./index.js";
} from "./convert";

const FREE_EMAIL_PROVIDERS = [
"gmail.com",
Expand Down

0 comments on commit 3260b56

Please sign in to comment.