@kya-os/agentshield-nextjs 0.1.11 → 0.1.12
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/wasm-setup.d.mts +43 -0
- package/dist/wasm-setup.d.ts +43 -0
- package/dist/wasm-setup.js +52 -0
- package/dist/wasm-setup.js.map +1 -0
- package/dist/wasm-setup.mjs +48 -0
- package/dist/wasm-setup.mjs.map +1 -0
- package/package.json +7 -2
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM Setup for AgentShield in Next.js Edge Runtime
|
|
3
|
+
*
|
|
4
|
+
* This module handles WASM initialization for cryptographic signature verification.
|
|
5
|
+
* It automatically detects the environment and only loads WASM in production.
|
|
6
|
+
*
|
|
7
|
+
* Usage in middleware.ts:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { setupWasm } from '@kya-os/agentshield-nextjs/wasm-setup';
|
|
10
|
+
* import { createAgentShieldMiddleware } from '@kya-os/agentshield-nextjs';
|
|
11
|
+
*
|
|
12
|
+
* // Initialize WASM (safe to call in all environments)
|
|
13
|
+
* await setupWasm();
|
|
14
|
+
*
|
|
15
|
+
* const agentShieldMiddleware = createAgentShieldMiddleware({...});
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Initialize WASM module for AgentShield
|
|
20
|
+
*
|
|
21
|
+
* This function:
|
|
22
|
+
* - Loads WASM in production/Edge Runtime for cryptographic verification
|
|
23
|
+
* - Skips WASM in test environments (Jest) automatically
|
|
24
|
+
* - Is safe to call multiple times (idempotent)
|
|
25
|
+
* - Handles errors gracefully with fallback to pattern detection
|
|
26
|
+
*
|
|
27
|
+
* @returns Promise that resolves when initialization is complete
|
|
28
|
+
*/
|
|
29
|
+
declare function setupWasm(): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Check if WASM has been initialized
|
|
32
|
+
*
|
|
33
|
+
* @returns true if WASM setup has been attempted (success or failure)
|
|
34
|
+
*/
|
|
35
|
+
declare function isWasmInitialized(): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Reset WASM initialization state (mainly for testing)
|
|
38
|
+
*
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
declare function resetWasmState(): void;
|
|
42
|
+
|
|
43
|
+
export { isWasmInitialized, resetWasmState, setupWasm };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM Setup for AgentShield in Next.js Edge Runtime
|
|
3
|
+
*
|
|
4
|
+
* This module handles WASM initialization for cryptographic signature verification.
|
|
5
|
+
* It automatically detects the environment and only loads WASM in production.
|
|
6
|
+
*
|
|
7
|
+
* Usage in middleware.ts:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { setupWasm } from '@kya-os/agentshield-nextjs/wasm-setup';
|
|
10
|
+
* import { createAgentShieldMiddleware } from '@kya-os/agentshield-nextjs';
|
|
11
|
+
*
|
|
12
|
+
* // Initialize WASM (safe to call in all environments)
|
|
13
|
+
* await setupWasm();
|
|
14
|
+
*
|
|
15
|
+
* const agentShieldMiddleware = createAgentShieldMiddleware({...});
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Initialize WASM module for AgentShield
|
|
20
|
+
*
|
|
21
|
+
* This function:
|
|
22
|
+
* - Loads WASM in production/Edge Runtime for cryptographic verification
|
|
23
|
+
* - Skips WASM in test environments (Jest) automatically
|
|
24
|
+
* - Is safe to call multiple times (idempotent)
|
|
25
|
+
* - Handles errors gracefully with fallback to pattern detection
|
|
26
|
+
*
|
|
27
|
+
* @returns Promise that resolves when initialization is complete
|
|
28
|
+
*/
|
|
29
|
+
declare function setupWasm(): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Check if WASM has been initialized
|
|
32
|
+
*
|
|
33
|
+
* @returns true if WASM setup has been attempted (success or failure)
|
|
34
|
+
*/
|
|
35
|
+
declare function isWasmInitialized(): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Reset WASM initialization state (mainly for testing)
|
|
38
|
+
*
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
declare function resetWasmState(): void;
|
|
42
|
+
|
|
43
|
+
export { isWasmInitialized, resetWasmState, setupWasm };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/wasm-setup.ts
|
|
4
|
+
var wasmInitialized = false;
|
|
5
|
+
var initPromise = null;
|
|
6
|
+
async function setupWasm() {
|
|
7
|
+
if (wasmInitialized) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (initPromise) {
|
|
11
|
+
return initPromise;
|
|
12
|
+
}
|
|
13
|
+
initPromise = doSetupWasm();
|
|
14
|
+
return initPromise;
|
|
15
|
+
}
|
|
16
|
+
async function doSetupWasm() {
|
|
17
|
+
try {
|
|
18
|
+
if (typeof process !== "undefined" && process.env.NODE_ENV === "test") {
|
|
19
|
+
console.log("AgentShield: Skipping WASM in test environment");
|
|
20
|
+
wasmInitialized = true;
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (typeof process !== "undefined" && !process.env.NEXT_RUNTIME) {
|
|
24
|
+
wasmInitialized = true;
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const { setWasmModule } = await import('@kya-os/agentshield');
|
|
28
|
+
const wasmModule = await import(
|
|
29
|
+
/* webpackIgnore: true */
|
|
30
|
+
'@kya-os/agentshield/dist/wasm/agentshield_wasm_bg.wasm?module'
|
|
31
|
+
);
|
|
32
|
+
setWasmModule(wasmModule.default || wasmModule);
|
|
33
|
+
console.log("\u2705 AgentShield: WASM module loaded for cryptographic verification");
|
|
34
|
+
wasmInitialized = true;
|
|
35
|
+
} catch (error) {
|
|
36
|
+
console.warn("\u26A0\uFE0F AgentShield: WASM not available, using pattern-based detection", error);
|
|
37
|
+
wasmInitialized = true;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function isWasmInitialized() {
|
|
41
|
+
return wasmInitialized;
|
|
42
|
+
}
|
|
43
|
+
function resetWasmState() {
|
|
44
|
+
wasmInitialized = false;
|
|
45
|
+
initPromise = null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
exports.isWasmInitialized = isWasmInitialized;
|
|
49
|
+
exports.resetWasmState = resetWasmState;
|
|
50
|
+
exports.setupWasm = setupWasm;
|
|
51
|
+
//# sourceMappingURL=wasm-setup.js.map
|
|
52
|
+
//# sourceMappingURL=wasm-setup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/wasm-setup.ts"],"names":[],"mappings":";;;AAkBA,IAAI,eAAA,GAAkB,KAAA;AACtB,IAAI,WAAA,GAAoC,IAAA;AAaxC,eAAsB,SAAA,GAA2B;AAE/C,EAAA,IAAI,eAAA,EAAiB;AACnB,IAAA;AAAA,EACF;AAGA,EAAA,IAAI,WAAA,EAAa;AACf,IAAA,OAAO,WAAA;AAAA,EACT;AAGA,EAAA,WAAA,GAAc,WAAA,EAAY;AAC1B,EAAA,OAAO,WAAA;AACT;AAEA,eAAe,WAAA,GAA6B;AAC1C,EAAA,IAAI;AAEF,IAAA,IAAI,OAAO,OAAA,KAAY,WAAA,IAAe,OAAA,CAAQ,GAAA,CAAI,aAAa,MAAA,EAAQ;AACrE,MAAA,OAAA,CAAQ,IAAI,gDAAgD,CAAA;AAC5D,MAAA,eAAA,GAAkB,IAAA;AAClB,MAAA;AAAA,IACF;AAGA,IAAA,IAAI,OAAO,OAAA,KAAY,WAAA,IAAe,CAAC,OAAA,CAAQ,IAAI,YAAA,EAAc;AAE/D,MAAA,eAAA,GAAkB,IAAA;AAClB,MAAA;AAAA,IACF;AAGA,IAAA,MAAM,EAAE,aAAA,EAAc,GAAI,MAAM,OAAO,qBAAqB,CAAA;AAI5D,IAAA,MAAM,aAAa,MAAM;AAAA;AAAA,MAEvB;AAAA,KACF;AAGA,IAAA,aAAA,CAAc,UAAA,CAAW,WAAW,UAAU,CAAA;AAE9C,IAAA,OAAA,CAAQ,IAAI,uEAAkE,CAAA;AAC9E,IAAA,eAAA,GAAkB,IAAA;AAAA,EACpB,SAAS,KAAA,EAAO;AAGd,IAAA,OAAA,CAAQ,IAAA,CAAK,+EAAqE,KAAK,CAAA;AACvF,IAAA,eAAA,GAAkB,IAAA;AAAA,EACpB;AACF;AAOO,SAAS,iBAAA,GAA6B;AAC3C,EAAA,OAAO,eAAA;AACT;AAOO,SAAS,cAAA,GAAuB;AACrC,EAAA,eAAA,GAAkB,KAAA;AAClB,EAAA,WAAA,GAAc,IAAA;AAChB","file":"wasm-setup.js","sourcesContent":["/**\n * WASM Setup for AgentShield in Next.js Edge Runtime\n * \n * This module handles WASM initialization for cryptographic signature verification.\n * It automatically detects the environment and only loads WASM in production.\n * \n * Usage in middleware.ts:\n * ```typescript\n * import { setupWasm } from '@kya-os/agentshield-nextjs/wasm-setup';\n * import { createAgentShieldMiddleware } from '@kya-os/agentshield-nextjs';\n * \n * // Initialize WASM (safe to call in all environments)\n * await setupWasm();\n * \n * const agentShieldMiddleware = createAgentShieldMiddleware({...});\n * ```\n */\n\nlet wasmInitialized = false;\nlet initPromise: Promise<void> | null = null;\n\n/**\n * Initialize WASM module for AgentShield\n * \n * This function:\n * - Loads WASM in production/Edge Runtime for cryptographic verification\n * - Skips WASM in test environments (Jest) automatically\n * - Is safe to call multiple times (idempotent)\n * - Handles errors gracefully with fallback to pattern detection\n * \n * @returns Promise that resolves when initialization is complete\n */\nexport async function setupWasm(): Promise<void> {\n // Already initialized, return immediately\n if (wasmInitialized) {\n return;\n }\n\n // Initialization in progress, return the existing promise\n if (initPromise) {\n return initPromise;\n }\n\n // Start initialization\n initPromise = doSetupWasm();\n return initPromise;\n}\n\nasync function doSetupWasm(): Promise<void> {\n try {\n // Skip WASM in test environments\n if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {\n console.log('AgentShield: Skipping WASM in test environment');\n wasmInitialized = true;\n return;\n }\n\n // Skip if not in Edge Runtime (e.g., Node.js build time)\n if (typeof process !== 'undefined' && !process.env.NEXT_RUNTIME) {\n // We're in Node.js at build time, skip WASM\n wasmInitialized = true;\n return;\n }\n\n // Dynamic import to avoid syntax errors in CommonJS environments\n const { setWasmModule } = await import('@kya-os/agentshield');\n \n // Import WASM module with ?module suffix for Edge Runtime\n // TypeScript doesn't understand this import, but it works at runtime\n const wasmModule = await import(\n /* webpackIgnore: true */\n '@kya-os/agentshield/dist/wasm/agentshield_wasm_bg.wasm?module' as any\n );\n \n // Initialize WASM module for cryptographic signature verification\n setWasmModule(wasmModule.default || wasmModule);\n \n console.log('✅ AgentShield: WASM module loaded for cryptographic verification');\n wasmInitialized = true;\n } catch (error) {\n // This is fine - WASM loading can fail in some environments\n // The middleware will automatically fall back to EdgeAgentDetector\n console.warn('⚠️ AgentShield: WASM not available, using pattern-based detection', error);\n wasmInitialized = true; // Mark as initialized even on failure to prevent retries\n }\n}\n\n/**\n * Check if WASM has been initialized\n * \n * @returns true if WASM setup has been attempted (success or failure)\n */\nexport function isWasmInitialized(): boolean {\n return wasmInitialized;\n}\n\n/**\n * Reset WASM initialization state (mainly for testing)\n * \n * @internal\n */\nexport function resetWasmState(): void {\n wasmInitialized = false;\n initPromise = null;\n}"]}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// src/wasm-setup.ts
|
|
2
|
+
var wasmInitialized = false;
|
|
3
|
+
var initPromise = null;
|
|
4
|
+
async function setupWasm() {
|
|
5
|
+
if (wasmInitialized) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
if (initPromise) {
|
|
9
|
+
return initPromise;
|
|
10
|
+
}
|
|
11
|
+
initPromise = doSetupWasm();
|
|
12
|
+
return initPromise;
|
|
13
|
+
}
|
|
14
|
+
async function doSetupWasm() {
|
|
15
|
+
try {
|
|
16
|
+
if (typeof process !== "undefined" && process.env.NODE_ENV === "test") {
|
|
17
|
+
console.log("AgentShield: Skipping WASM in test environment");
|
|
18
|
+
wasmInitialized = true;
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (typeof process !== "undefined" && !process.env.NEXT_RUNTIME) {
|
|
22
|
+
wasmInitialized = true;
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const { setWasmModule } = await import('@kya-os/agentshield');
|
|
26
|
+
const wasmModule = await import(
|
|
27
|
+
/* webpackIgnore: true */
|
|
28
|
+
'@kya-os/agentshield/dist/wasm/agentshield_wasm_bg.wasm?module'
|
|
29
|
+
);
|
|
30
|
+
setWasmModule(wasmModule.default || wasmModule);
|
|
31
|
+
console.log("\u2705 AgentShield: WASM module loaded for cryptographic verification");
|
|
32
|
+
wasmInitialized = true;
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.warn("\u26A0\uFE0F AgentShield: WASM not available, using pattern-based detection", error);
|
|
35
|
+
wasmInitialized = true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function isWasmInitialized() {
|
|
39
|
+
return wasmInitialized;
|
|
40
|
+
}
|
|
41
|
+
function resetWasmState() {
|
|
42
|
+
wasmInitialized = false;
|
|
43
|
+
initPromise = null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { isWasmInitialized, resetWasmState, setupWasm };
|
|
47
|
+
//# sourceMappingURL=wasm-setup.mjs.map
|
|
48
|
+
//# sourceMappingURL=wasm-setup.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/wasm-setup.ts"],"names":[],"mappings":";AAkBA,IAAI,eAAA,GAAkB,KAAA;AACtB,IAAI,WAAA,GAAoC,IAAA;AAaxC,eAAsB,SAAA,GAA2B;AAE/C,EAAA,IAAI,eAAA,EAAiB;AACnB,IAAA;AAAA,EACF;AAGA,EAAA,IAAI,WAAA,EAAa;AACf,IAAA,OAAO,WAAA;AAAA,EACT;AAGA,EAAA,WAAA,GAAc,WAAA,EAAY;AAC1B,EAAA,OAAO,WAAA;AACT;AAEA,eAAe,WAAA,GAA6B;AAC1C,EAAA,IAAI;AAEF,IAAA,IAAI,OAAO,OAAA,KAAY,WAAA,IAAe,OAAA,CAAQ,GAAA,CAAI,aAAa,MAAA,EAAQ;AACrE,MAAA,OAAA,CAAQ,IAAI,gDAAgD,CAAA;AAC5D,MAAA,eAAA,GAAkB,IAAA;AAClB,MAAA;AAAA,IACF;AAGA,IAAA,IAAI,OAAO,OAAA,KAAY,WAAA,IAAe,CAAC,OAAA,CAAQ,IAAI,YAAA,EAAc;AAE/D,MAAA,eAAA,GAAkB,IAAA;AAClB,MAAA;AAAA,IACF;AAGA,IAAA,MAAM,EAAE,aAAA,EAAc,GAAI,MAAM,OAAO,qBAAqB,CAAA;AAI5D,IAAA,MAAM,aAAa,MAAM;AAAA;AAAA,MAEvB;AAAA,KACF;AAGA,IAAA,aAAA,CAAc,UAAA,CAAW,WAAW,UAAU,CAAA;AAE9C,IAAA,OAAA,CAAQ,IAAI,uEAAkE,CAAA;AAC9E,IAAA,eAAA,GAAkB,IAAA;AAAA,EACpB,SAAS,KAAA,EAAO;AAGd,IAAA,OAAA,CAAQ,IAAA,CAAK,+EAAqE,KAAK,CAAA;AACvF,IAAA,eAAA,GAAkB,IAAA;AAAA,EACpB;AACF;AAOO,SAAS,iBAAA,GAA6B;AAC3C,EAAA,OAAO,eAAA;AACT;AAOO,SAAS,cAAA,GAAuB;AACrC,EAAA,eAAA,GAAkB,KAAA;AAClB,EAAA,WAAA,GAAc,IAAA;AAChB","file":"wasm-setup.mjs","sourcesContent":["/**\n * WASM Setup for AgentShield in Next.js Edge Runtime\n * \n * This module handles WASM initialization for cryptographic signature verification.\n * It automatically detects the environment and only loads WASM in production.\n * \n * Usage in middleware.ts:\n * ```typescript\n * import { setupWasm } from '@kya-os/agentshield-nextjs/wasm-setup';\n * import { createAgentShieldMiddleware } from '@kya-os/agentshield-nextjs';\n * \n * // Initialize WASM (safe to call in all environments)\n * await setupWasm();\n * \n * const agentShieldMiddleware = createAgentShieldMiddleware({...});\n * ```\n */\n\nlet wasmInitialized = false;\nlet initPromise: Promise<void> | null = null;\n\n/**\n * Initialize WASM module for AgentShield\n * \n * This function:\n * - Loads WASM in production/Edge Runtime for cryptographic verification\n * - Skips WASM in test environments (Jest) automatically\n * - Is safe to call multiple times (idempotent)\n * - Handles errors gracefully with fallback to pattern detection\n * \n * @returns Promise that resolves when initialization is complete\n */\nexport async function setupWasm(): Promise<void> {\n // Already initialized, return immediately\n if (wasmInitialized) {\n return;\n }\n\n // Initialization in progress, return the existing promise\n if (initPromise) {\n return initPromise;\n }\n\n // Start initialization\n initPromise = doSetupWasm();\n return initPromise;\n}\n\nasync function doSetupWasm(): Promise<void> {\n try {\n // Skip WASM in test environments\n if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {\n console.log('AgentShield: Skipping WASM in test environment');\n wasmInitialized = true;\n return;\n }\n\n // Skip if not in Edge Runtime (e.g., Node.js build time)\n if (typeof process !== 'undefined' && !process.env.NEXT_RUNTIME) {\n // We're in Node.js at build time, skip WASM\n wasmInitialized = true;\n return;\n }\n\n // Dynamic import to avoid syntax errors in CommonJS environments\n const { setWasmModule } = await import('@kya-os/agentshield');\n \n // Import WASM module with ?module suffix for Edge Runtime\n // TypeScript doesn't understand this import, but it works at runtime\n const wasmModule = await import(\n /* webpackIgnore: true */\n '@kya-os/agentshield/dist/wasm/agentshield_wasm_bg.wasm?module' as any\n );\n \n // Initialize WASM module for cryptographic signature verification\n setWasmModule(wasmModule.default || wasmModule);\n \n console.log('✅ AgentShield: WASM module loaded for cryptographic verification');\n wasmInitialized = true;\n } catch (error) {\n // This is fine - WASM loading can fail in some environments\n // The middleware will automatically fall back to EdgeAgentDetector\n console.warn('⚠️ AgentShield: WASM not available, using pattern-based detection', error);\n wasmInitialized = true; // Mark as initialized even on failure to prevent retries\n }\n}\n\n/**\n * Check if WASM has been initialized\n * \n * @returns true if WASM setup has been attempted (success or failure)\n */\nexport function isWasmInitialized(): boolean {\n return wasmInitialized;\n}\n\n/**\n * Reset WASM initialization state (mainly for testing)\n * \n * @internal\n */\nexport function resetWasmState(): void {\n wasmInitialized = false;\n initPromise = null;\n}"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kya-os/agentshield-nextjs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"description": "Next.js middleware for AgentShield AI agent detection",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"nextjs",
|
|
@@ -36,6 +36,11 @@
|
|
|
36
36
|
"import": "./dist/middleware.mjs",
|
|
37
37
|
"require": "./dist/middleware.js"
|
|
38
38
|
},
|
|
39
|
+
"./wasm-setup": {
|
|
40
|
+
"types": "./dist/wasm-setup.d.ts",
|
|
41
|
+
"import": "./dist/wasm-setup.mjs",
|
|
42
|
+
"require": "./dist/wasm-setup.js"
|
|
43
|
+
},
|
|
39
44
|
"./package.json": "./package.json"
|
|
40
45
|
},
|
|
41
46
|
"files": [
|
|
@@ -85,6 +90,6 @@
|
|
|
85
90
|
},
|
|
86
91
|
"sideEffects": false,
|
|
87
92
|
"dependencies": {
|
|
88
|
-
"@kya-os/agentshield": "^0.1.
|
|
93
|
+
"@kya-os/agentshield": "^0.1.12"
|
|
89
94
|
}
|
|
90
95
|
}
|