@kya-os/agentshield-nextjs
Version:
Next.js middleware for AgentShield AI agent detection
148 lines (144 loc) • 4.98 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { setWasmModule } from '@kya-os/agentshield';
var __defProp = Object.defineProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __esm = (fn, res) => function __init() {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
// src/nodejs-wasm-loader.ts
var nodejs_wasm_loader_exports = {};
__export(nodejs_wasm_loader_exports, {
getWasmModule: () => getWasmModule,
isNodejsRuntime: () => isNodejsRuntime,
isWasmInitialized: () => isWasmInitialized,
loadWasmNodejs: () => loadWasmNodejs
});
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;
}
var wasmInitialized, wasmModule;
var init_nodejs_wasm_loader = __esm({
"src/nodejs-wasm-loader.ts"() {
wasmInitialized = false;
wasmModule = null;
}
});
// src/wasm-setup.ts
var wasmInitialized2 = false;
var initPromise = null;
var initAttempted = false;
async function setupWasm() {
if (wasmInitialized2) {
return;
}
if (initPromise) {
return initPromise;
}
initPromise = doSetupWasm();
return initPromise;
}
async function doSetupWasm() {
if (initAttempted) {
return;
}
initAttempted = true;
try {
if (typeof process !== "undefined" && process.env.NODE_ENV === "test") {
wasmInitialized2 = true;
return;
}
const isNodejs = typeof process !== "undefined" && typeof __require !== "undefined" && process.env.NEXT_RUNTIME === "nodejs";
if (isNodejs) {
try {
const { loadWasmNodejs: loadWasmNodejs2 } = await Promise.resolve().then(() => (init_nodejs_wasm_loader(), nodejs_wasm_loader_exports));
const loaded = await loadWasmNodejs2();
wasmInitialized2 = true;
if (loaded) {
console.log("\u{1F680} AgentShield: Running with full WASM support (Node.js runtime)");
} else {
console.log("\u{1F4CA} AgentShield: Using pattern detection (WASM not found)");
}
return;
} catch (error) {
console.warn("\u26A0\uFE0F AgentShield: Node.js WASM loader failed:", error);
}
}
wasmInitialized2 = true;
if (process.env.NEXT_RUNTIME === "edge") {
console.log("\u26A1 AgentShield: Edge Runtime detected - using pattern detection (85% confidence)");
console.log("\u2705 AgentShield: Ready for AI agent detection");
}
} catch (error) {
wasmInitialized2 = true;
}
}
function isWasmInitialized2() {
return wasmInitialized2;
}
function resetWasmState() {
wasmInitialized2 = false;
initPromise = null;
}
export { isWasmInitialized2 as isWasmInitialized, resetWasmState, setupWasm };
//# sourceMappingURL=wasm-setup.mjs.map
//# sourceMappingURL=wasm-setup.mjs.map