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 d67ecf8
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 79 deletions.
14 changes: 14 additions & 0 deletions analyze/convert.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as core from "./wasm/arcjet_analyze_js_req.component.js";

Check failure on line 1 in analyze/convert.d.ts

View check run for this annotation

Trunk.io / Trunk Check

prettier

Incorrect formatting, autoformat by running 'trunk fmt'
import {
DetectedSensitiveInfoEntity,
SensitiveInfoEntity,
} from "@arcjet/protocol";
export declare function ConvertProtocolEntitiesToAnalyzeEntities(
entity: SensitiveInfoEntity,
): core.SensitiveInfoEntity;
export declare function ConvertDetectedSensitiveInfoEntityToAnalyzeEntity(
entity?: DetectedSensitiveInfoEntity,
): core.SensitiveInfoEntity | undefined;
export declare function ConvertAnalyzeEntitiesToProtocolEntities(
entity: core.SensitiveInfoEntity,
): DetectedSensitiveInfoEntity;
56 changes: 56 additions & 0 deletions analyze/convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function ConvertProtocolEntitiesToAnalyzeEntities(entity) {

Check failure on line 1 in analyze/convert.js

View check run for this annotation

Trunk.io / Trunk Check

prettier

Incorrect formatting, autoformat by running 'trunk fmt'
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",
};
}
function ConvertDetectedSensitiveInfoEntityToAnalyzeEntity(entity) {
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" };
}
}
function ConvertAnalyzeEntitiesToProtocolEntities(entity) {
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";
}

export {
ConvertAnalyzeEntitiesToProtocolEntities,
ConvertDetectedSensitiveInfoEntityToAnalyzeEntity,
ConvertProtocolEntitiesToAnalyzeEntities,
};
76 changes: 76 additions & 0 deletions analyze/convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import * as core from "./wasm/arcjet_analyze_js_req.component.js";
import {
DetectedSensitiveInfoEntity,
SensitiveInfoEntity,
} from "@arcjet/protocol";

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";
}
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 d67ecf8

Please sign in to comment.