@kya-os/agentshield-nextjs
Version:
Next.js middleware for AgentShield AI agent detection
68 lines (66 loc) • 2.54 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { setWasmModule } from '@kya-os/agentshield';
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined") return require.apply(this, arguments);
throw Error('Dynamic require of "' + x + '" is not supported');
});
var wasmInitialized = false;
var wasmModule = null;
async function loadWasmNodejs() {
if (wasmInitialized) {
return true;
}
try {
const possiblePaths = [
// In node_modules (most likely)
path.join(process.cwd(), "node_modules", "@kya-os", "agentshield", "dist", "wasm", "agentshield_wasm_bg.wasm"),
// In project root (if user copied it)
path.join(process.cwd(), "agentshield_wasm_bg.wasm"),
// Relative to current file
path.join(__dirname, "..", "..", "..", "agentshield", "dist", "wasm", "agentshield_wasm_bg.wasm")
];
let wasmBuffer = null;
let loadedPath = null;
for (const wasmPath of possiblePaths) {
try {
if (fs.existsSync(wasmPath)) {
wasmBuffer = fs.readFileSync(wasmPath);
loadedPath = wasmPath;
break;
}
} catch (e) {
continue;
}
}
if (!wasmBuffer) {
console.warn("AgentShield: WASM file not found in any expected location");
return false;
}
const bytes = new Uint8Array(wasmBuffer);
wasmModule = await WebAssembly.compile(bytes);
setWasmModule(wasmModule);
wasmInitialized = true;
console.log(`\u2705 AgentShield: WASM loaded successfully from ${loadedPath} (Node.js runtime)`);
console.log("\u{1F510} Cryptographic verification enabled (95-100% confidence)");
return true;
} catch (error) {
console.warn("\u26A0\uFE0F AgentShield: Failed to load WASM in Node.js runtime:", error);
console.log("\u{1F4CA} Falling back to pattern detection (85% confidence)");
return false;
}
}
function isNodejsRuntime() {
return typeof process !== "undefined" && typeof process.versions !== "undefined" && typeof process.versions.node !== "undefined" && typeof __require !== "undefined";
}
function getWasmModule() {
return wasmModule;
}
function isWasmInitialized() {
return wasmInitialized;
}
export { getWasmModule, isNodejsRuntime, isWasmInitialized, loadWasmNodejs };
//# sourceMappingURL=nodejs-wasm-loader.mjs.map
//# sourceMappingURL=nodejs-wasm-loader.mjs.map