@kya-os/agentshield-nextjs 0.1.35 → 0.1.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -373,6 +373,21 @@ var wasmInstance = null;
373
373
  var wasmExports = null;
374
374
  var initPromise = null;
375
375
  var WASM_PATH = "/wasm/agentshield_wasm_bg.wasm";
376
+ var baseUrl = null;
377
+ function setWasmBaseUrl(url) {
378
+ baseUrl = url;
379
+ }
380
+ function getWasmUrl() {
381
+ if (baseUrl) {
382
+ try {
383
+ const url = new URL(baseUrl);
384
+ return `${url.origin}${WASM_PATH}`;
385
+ } catch {
386
+ return WASM_PATH;
387
+ }
388
+ }
389
+ return WASM_PATH;
390
+ }
376
391
  async function initWasm() {
377
392
  if (wasmExports) return true;
378
393
  if (initPromise) {
@@ -383,7 +398,8 @@ async function initWasm() {
383
398
  try {
384
399
  if (typeof WebAssembly.instantiateStreaming === "function") {
385
400
  try {
386
- const response2 = await fetch(WASM_PATH);
401
+ const wasmUrl2 = getWasmUrl();
402
+ const response2 = await fetch(wasmUrl2);
387
403
  if (!response2.ok) {
388
404
  throw new Error(`Failed to fetch WASM: ${response2.status}`);
389
405
  }
@@ -409,7 +425,8 @@ async function initWasm() {
409
425
  console.log("[AgentShield] Streaming compilation failed, falling back to standard compilation");
410
426
  }
411
427
  }
412
- const response = await fetch(WASM_PATH);
428
+ const wasmUrl = getWasmUrl();
429
+ const response = await fetch(wasmUrl);
413
430
  if (!response.ok) {
414
431
  throw new Error(`Failed to fetch WASM: ${response.status}`);
415
432
  }
@@ -508,6 +525,14 @@ var EdgeAgentDetectorWithWasm = class {
508
525
  }
509
526
  wasmEnabled = false;
510
527
  initPromise = null;
528
+ baseUrl = null;
529
+ /**
530
+ * Set the base URL for WASM loading in Edge Runtime
531
+ */
532
+ setBaseUrl(url) {
533
+ this.baseUrl = url;
534
+ setWasmBaseUrl(url);
535
+ }
511
536
  /**
512
537
  * Initialize the detector (including WASM if enabled)
513
538
  */
@@ -681,6 +706,12 @@ var EdgeAgentDetectorWrapperWithWasm = class {
681
706
  events = /* @__PURE__ */ new Map();
682
707
  constructor(config) {
683
708
  this.detector = new EdgeAgentDetectorWithWasm(config?.enableWasm ?? true);
709
+ if (config?.baseUrl) {
710
+ this.detector.setBaseUrl(config.baseUrl);
711
+ }
712
+ }
713
+ setBaseUrl(url) {
714
+ this.detector.setBaseUrl(url);
684
715
  }
685
716
  async analyze(input) {
686
717
  const result = await this.detector.analyze(input);
@@ -1061,18 +1092,9 @@ async function checkWasmAvailability() {
1061
1092
  if (typeof WebAssembly === "undefined") {
1062
1093
  return false;
1063
1094
  }
1064
- const wasmCode = new Uint8Array([
1065
- 0,
1066
- 97,
1067
- 115,
1068
- 109,
1069
- 1,
1070
- 0,
1071
- 0,
1072
- 0
1073
- ]);
1074
- const module = await WebAssembly.compile(wasmCode);
1075
- await WebAssembly.instantiate(module);
1095
+ if (!WebAssembly.instantiate || !WebAssembly.Module) {
1096
+ return false;
1097
+ }
1076
1098
  return true;
1077
1099
  } catch {
1078
1100
  return false;
@@ -1380,10 +1402,18 @@ function createEnhancedAgentShieldMiddleware(config = {}) {
1380
1402
  };
1381
1403
  let detector = null;
1382
1404
  let detectorInitPromise = null;
1383
- const getDetector = async () => {
1384
- if (detector) return detector;
1405
+ const getDetector = async (requestUrl) => {
1406
+ if (detector) {
1407
+ if (requestUrl && "setBaseUrl" in detector) {
1408
+ detector.setBaseUrl(requestUrl);
1409
+ }
1410
+ return detector;
1411
+ }
1385
1412
  if (detectorInitPromise) {
1386
1413
  await detectorInitPromise;
1414
+ if (requestUrl && detector && "setBaseUrl" in detector) {
1415
+ detector.setBaseUrl(requestUrl);
1416
+ }
1387
1417
  return detector;
1388
1418
  }
1389
1419
  detectorInitPromise = (async () => {
@@ -1402,6 +1432,9 @@ function createEnhancedAgentShieldMiddleware(config = {}) {
1402
1432
  }
1403
1433
  })();
1404
1434
  await detectorInitPromise;
1435
+ if (requestUrl && detector && "setBaseUrl" in detector) {
1436
+ detector.setBaseUrl(requestUrl);
1437
+ }
1405
1438
  return detector;
1406
1439
  };
1407
1440
  const sessionManager = new SessionManager();
@@ -1423,7 +1456,7 @@ function createEnhancedAgentShieldMiddleware(config = {}) {
1423
1456
  method: request.method,
1424
1457
  timestamp: /* @__PURE__ */ new Date()
1425
1458
  };
1426
- const activeDetector = await getDetector();
1459
+ const activeDetector = await getDetector(request.url);
1427
1460
  const result = await activeDetector.analyze(context);
1428
1461
  let finalConfidence = result.confidence;
1429
1462
  let verificationMethod = result.verificationMethod || "pattern";