Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logger)!: Align logger with Pino API #858

Merged
merged 14 commits into from
Jun 6, 2024
43 changes: 27 additions & 16 deletions analyze/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logger from "@arcjet/logger";
import type { ArcjetLogger } from "@arcjet/protocol";

import * as core from "./wasm/arcjet_analyze_js_req.component.js";
import type {
Expand All @@ -8,6 +8,10 @@ import type {
BotType,
} from "./wasm/arcjet_analyze_js_req.component.js";

interface AnalyzeContext {
log: ArcjetLogger;
}

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

Expand Down Expand Up @@ -73,22 +77,24 @@ async function moduleFromPath(path: string): Promise<WebAssembly.Module> {
throw new Error(`Unknown path: ${path}`);
}

const coreImports: ImportObject = {
"arcjet:js-req/logger": {
debug(msg) {
logger.debug(msg);
async function init(context: AnalyzeContext) {
const { log } = context;

const coreImports: ImportObject = {
"arcjet:js-req/logger": {
debug(msg) {
log.debug(msg);
},
error(msg) {
log.error(msg);
},
},
error(msg) {
logger.error(msg);
},
},
};
};

async function init() {
try {
return core.instantiate(moduleFromPath, coreImports);
} catch {
logger.debug("WebAssembly is not supported in this runtime");
log.debug("WebAssembly is not supported in this runtime");
}
}

Expand Down Expand Up @@ -116,12 +122,15 @@ export {
* @param ip - The IP address of the client.
* @returns A SHA-256 string fingerprint.
*/
export async function generateFingerprint(ip: string): Promise<string> {
export async function generateFingerprint(
context: AnalyzeContext,
ip: string,
): Promise<string> {
if (ip == "") {
return "";
}

const analyze = await init();
const analyze = await init(context);

if (typeof analyze !== "undefined") {
return analyze.generateFingerprint(ip);
Expand Down Expand Up @@ -155,10 +164,11 @@ export async function generateFingerprint(ip: string): Promise<string> {
}

export async function isValidEmail(
context: AnalyzeContext,
candidate: string,
options?: EmailValidationConfig,
) {
const analyze = await init();
const analyze = await init(context);

if (typeof analyze !== "undefined") {
return analyze.isValidEmail(candidate, options);
Expand All @@ -169,11 +179,12 @@ export async function isValidEmail(
}

export async function detectBot(
context: AnalyzeContext,
headers: string,
patterns_add: string,
patterns_remove: string,
): Promise<BotDetectionResult> {
const analyze = await init();
const analyze = await init(context);

if (typeof analyze !== "undefined") {
return analyze.detectBot(headers, patterns_add, patterns_remove);
Expand Down
2 changes: 1 addition & 1 deletion analyze/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"./wasm/arcjet_analyze_js_req_bg.js"
],
"dependencies": {
"@arcjet/logger": "1.0.0-alpha.13"
"@arcjet/protocol": "1.0.0-alpha.13"
},
"devDependencies": {
"@arcjet/eslint-config": "1.0.0-alpha.13",
Expand Down
Loading
Loading