@kya-os/checkpoint-wasm-runtime 1.0.0
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/CHANGELOG.md +58 -0
- package/dist/adapters.d.mts +257 -0
- package/dist/adapters.d.ts +257 -0
- package/dist/adapters.js +603 -0
- package/dist/adapters.js.map +1 -0
- package/dist/adapters.mjs +586 -0
- package/dist/adapters.mjs.map +1 -0
- package/dist/dynamic-loader-cS-pUisw.d.ts +65 -0
- package/dist/dynamic-loader-qGJacfEC.d.mts +65 -0
- package/dist/edge.d.mts +22 -0
- package/dist/edge.d.ts +22 -0
- package/dist/edge.js +1403 -0
- package/dist/edge.js.map +1 -0
- package/dist/edge.mjs +1391 -0
- package/dist/edge.mjs.map +1 -0
- package/dist/engine-edge.d.mts +58 -0
- package/dist/engine-edge.d.ts +58 -0
- package/dist/engine-edge.js +537 -0
- package/dist/engine-edge.js.map +1 -0
- package/dist/engine-edge.mjs +533 -0
- package/dist/engine-edge.mjs.map +1 -0
- package/dist/engine.d.mts +34 -0
- package/dist/engine.d.ts +34 -0
- package/dist/engine.js +11 -0
- package/dist/engine.js.map +1 -0
- package/dist/engine.mjs +9 -0
- package/dist/engine.mjs.map +1 -0
- package/dist/index.d.mts +58 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.js +1652 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1637 -0
- package/dist/index.mjs.map +1 -0
- package/dist/node.d.mts +26 -0
- package/dist/node.d.ts +26 -0
- package/dist/node.js +972 -0
- package/dist/node.js.map +1 -0
- package/dist/node.mjs +960 -0
- package/dist/node.mjs.map +1 -0
- package/dist/orchestrator-edge.d.mts +243 -0
- package/dist/orchestrator-edge.d.ts +243 -0
- package/dist/orchestrator-edge.js +1076 -0
- package/dist/orchestrator-edge.js.map +1 -0
- package/dist/orchestrator-edge.mjs +1065 -0
- package/dist/orchestrator-edge.mjs.map +1 -0
- package/dist/orchestrator.d.mts +50 -0
- package/dist/orchestrator.d.ts +50 -0
- package/dist/orchestrator.js +1185 -0
- package/dist/orchestrator.js.map +1 -0
- package/dist/orchestrator.mjs +1172 -0
- package/dist/orchestrator.mjs.map +1 -0
- package/dist/rules-detector-DjbTJ1-Q.d.mts +470 -0
- package/dist/rules-detector-DjbTJ1-Q.d.ts +470 -0
- package/dist/static-loader-C1hUlksK.d.ts +72 -0
- package/dist/static-loader-Ds4iNw7c.d.mts +72 -0
- package/dist/types-D0j85fF0.d.mts +163 -0
- package/dist/types-D0j85fF0.d.ts +163 -0
- package/package.json +141 -0
- package/wasm/agentshield_wasm.d.ts +485 -0
- package/wasm/agentshield_wasm.js +1551 -0
- package/wasm/agentshield_wasm_bg.wasm +0 -0
- package/wasm/agentshield_wasm_bg.wasm.d.ts +97 -0
- package/wasm/kya-os-engine/kya_os_engine.d.ts +24 -0
- package/wasm/kya-os-engine/kya_os_engine.js +517 -0
- package/wasm/kya-os-engine/kya_os_engine_bg.wasm +0 -0
- package/wasm/kya-os-engine/kya_os_engine_bg.wasm.d.ts +8 -0
- package/wasm/kya-os-engine-web/kya_os_engine.d.ts +56 -0
- package/wasm/kya-os-engine-web/kya_os_engine.js +574 -0
- package/wasm/kya-os-engine-web/kya_os_engine_bg.wasm +0 -0
- package/wasm/kya-os-engine-web/kya_os_engine_bg.wasm.d.ts +8 -0
- package/wasm/package.json +30 -0
package/dist/edge.js
ADDED
|
@@ -0,0 +1,1403 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var checkpointShared = require('@kya-os/checkpoint-shared');
|
|
4
|
+
|
|
5
|
+
// src/types.ts
|
|
6
|
+
var CONFIDENCE = {
|
|
7
|
+
/** Minimum confidence for isAgent=true */
|
|
8
|
+
THRESHOLD_AGENT: 30,
|
|
9
|
+
/** Cryptographic signature verified */
|
|
10
|
+
SIGNATURE_VERIFIED: 100,
|
|
11
|
+
/** Signature header present but not verified */
|
|
12
|
+
SIGNATURE_PRESENT: 85,
|
|
13
|
+
/** Strong pattern match */
|
|
14
|
+
PATTERN_HIGH: 85,
|
|
15
|
+
/** Moderate pattern match */
|
|
16
|
+
PATTERN_MEDIUM: 60,
|
|
17
|
+
/** Weak pattern match */
|
|
18
|
+
PATTERN_LOW: 40,
|
|
19
|
+
/** Cloud IP detection only */
|
|
20
|
+
CLOUD_IP: 30
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// src/wasm-detector.ts
|
|
24
|
+
function parseVerificationMethod(method) {
|
|
25
|
+
switch (method.toLowerCase()) {
|
|
26
|
+
case "signature":
|
|
27
|
+
return "signature";
|
|
28
|
+
case "pattern":
|
|
29
|
+
return "pattern";
|
|
30
|
+
case "behavioral":
|
|
31
|
+
return "behavioral";
|
|
32
|
+
case "network":
|
|
33
|
+
return "network";
|
|
34
|
+
case "mcp_i_handshake":
|
|
35
|
+
return "mcp_i_handshake";
|
|
36
|
+
default:
|
|
37
|
+
return "none";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function determineForgeabilityRisk(method, confidence) {
|
|
41
|
+
if (method === "signature" && confidence >= 90) {
|
|
42
|
+
return "low";
|
|
43
|
+
}
|
|
44
|
+
if (confidence >= 80) {
|
|
45
|
+
return "medium";
|
|
46
|
+
}
|
|
47
|
+
return "high";
|
|
48
|
+
}
|
|
49
|
+
function determineDetectionClass(isAgent, agentName, confidence) {
|
|
50
|
+
if (!isAgent || confidence < CONFIDENCE.THRESHOLD_AGENT) {
|
|
51
|
+
return { type: "Human" };
|
|
52
|
+
}
|
|
53
|
+
if (agentName) {
|
|
54
|
+
const aiAgentPatterns = [
|
|
55
|
+
"chatgpt",
|
|
56
|
+
"gpt",
|
|
57
|
+
"openai",
|
|
58
|
+
"claude",
|
|
59
|
+
"anthropic",
|
|
60
|
+
"perplexity",
|
|
61
|
+
"gemini",
|
|
62
|
+
"google ai",
|
|
63
|
+
"copilot",
|
|
64
|
+
"bing chat"
|
|
65
|
+
];
|
|
66
|
+
const lowerName = agentName.toLowerCase();
|
|
67
|
+
if (aiAgentPatterns.some((pattern) => lowerName.includes(pattern))) {
|
|
68
|
+
return { type: "AiAgent", agentType: agentName };
|
|
69
|
+
}
|
|
70
|
+
const botPatterns = ["bot", "crawler", "spider", "scraper"];
|
|
71
|
+
if (botPatterns.some((pattern) => lowerName.includes(pattern))) {
|
|
72
|
+
return { type: "Bot", botType: agentName };
|
|
73
|
+
}
|
|
74
|
+
const automationPatterns = ["selenium", "puppeteer", "playwright", "cypress"];
|
|
75
|
+
if (automationPatterns.some((pattern) => lowerName.includes(pattern))) {
|
|
76
|
+
return { type: "Automation", toolType: agentName };
|
|
77
|
+
}
|
|
78
|
+
return { type: "AiAgent", agentType: agentName };
|
|
79
|
+
}
|
|
80
|
+
return { type: "Unknown" };
|
|
81
|
+
}
|
|
82
|
+
var WasmDetector = class {
|
|
83
|
+
/**
|
|
84
|
+
* Create a new WasmDetector
|
|
85
|
+
* @param loader - WASM loader (static for Edge, dynamic for Node.js)
|
|
86
|
+
* @param policyLoader - Optional policy loader for API key support
|
|
87
|
+
* @param options - Detector configuration options
|
|
88
|
+
*/
|
|
89
|
+
constructor(loader, policyLoader, options = {}) {
|
|
90
|
+
this.loader = loader;
|
|
91
|
+
this.policyLoader = policyLoader;
|
|
92
|
+
this.options = options;
|
|
93
|
+
}
|
|
94
|
+
ready = false;
|
|
95
|
+
loadPromise = null;
|
|
96
|
+
/**
|
|
97
|
+
* Analyze a request and detect if it's from an agent
|
|
98
|
+
*/
|
|
99
|
+
async detect(input) {
|
|
100
|
+
await this.ensureReady();
|
|
101
|
+
try {
|
|
102
|
+
const bindings = this.loader.getBindings();
|
|
103
|
+
const metadata = {
|
|
104
|
+
user_agent: input.userAgent || null,
|
|
105
|
+
ip_address: input.ipAddress || null,
|
|
106
|
+
headers: JSON.stringify(input.headers || {}),
|
|
107
|
+
timestamp: (input.timestamp || /* @__PURE__ */ new Date()).toISOString(),
|
|
108
|
+
url: input.url || null,
|
|
109
|
+
method: input.method || null,
|
|
110
|
+
client_fingerprint: input.clientFingerprint || null,
|
|
111
|
+
free: () => {
|
|
112
|
+
}
|
|
113
|
+
// No-op for JS
|
|
114
|
+
};
|
|
115
|
+
const wasmResult = bindings.detect_agent(metadata);
|
|
116
|
+
const verificationMethod = parseVerificationMethod(wasmResult.verification_method);
|
|
117
|
+
const forgeabilityRisk = determineForgeabilityRisk(verificationMethod, wasmResult.confidence);
|
|
118
|
+
const detectionClass = determineDetectionClass(
|
|
119
|
+
wasmResult.is_agent,
|
|
120
|
+
wasmResult.agent,
|
|
121
|
+
wasmResult.confidence
|
|
122
|
+
);
|
|
123
|
+
let result = {
|
|
124
|
+
isAgent: wasmResult.is_agent,
|
|
125
|
+
confidence: wasmResult.confidence,
|
|
126
|
+
// Already 0-100, no conversion!
|
|
127
|
+
detectionClass,
|
|
128
|
+
detectedAgent: wasmResult.agent ? {
|
|
129
|
+
type: this.inferAgentType(wasmResult.agent),
|
|
130
|
+
name: wasmResult.agent
|
|
131
|
+
} : void 0,
|
|
132
|
+
verificationMethod,
|
|
133
|
+
forgeabilityRisk,
|
|
134
|
+
reasons: this.extractReasons(wasmResult),
|
|
135
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
136
|
+
};
|
|
137
|
+
if (this.policyLoader && this.options.apiKey) {
|
|
138
|
+
result = await this.applyPolicy(result);
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (this.options.debug) {
|
|
143
|
+
console.error("[WasmDetector] Detection error:", error);
|
|
144
|
+
}
|
|
145
|
+
return this.createDefaultResult();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Check if the detector is ready
|
|
150
|
+
*/
|
|
151
|
+
isReady() {
|
|
152
|
+
return this.ready;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Ensure the detector is initialized
|
|
156
|
+
*/
|
|
157
|
+
async ensureReady() {
|
|
158
|
+
if (this.ready) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (this.loadPromise) {
|
|
162
|
+
return this.loadPromise;
|
|
163
|
+
}
|
|
164
|
+
this.loadPromise = this.initialize();
|
|
165
|
+
return this.loadPromise;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Get detector version
|
|
169
|
+
*/
|
|
170
|
+
async getVersion() {
|
|
171
|
+
await this.ensureReady();
|
|
172
|
+
return this.loader.getBindings().get_version();
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Initialize the detector
|
|
176
|
+
*/
|
|
177
|
+
async initialize() {
|
|
178
|
+
try {
|
|
179
|
+
await this.loader.load();
|
|
180
|
+
this.ready = true;
|
|
181
|
+
if (this.options.debug) {
|
|
182
|
+
const version2 = this.loader.getBindings().get_version();
|
|
183
|
+
console.log(
|
|
184
|
+
`[WasmDetector] Initialized with WASM v${version2} (${this.loader.getStrategy()})`
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
} catch (error) {
|
|
188
|
+
this.loadPromise = null;
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Apply customer policy to detection result
|
|
194
|
+
*/
|
|
195
|
+
async applyPolicy(result) {
|
|
196
|
+
if (!this.policyLoader || !this.options.apiKey) {
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
try {
|
|
200
|
+
let policy = this.policyLoader.getCachedPolicy(this.options.apiKey);
|
|
201
|
+
if (!policy) {
|
|
202
|
+
policy = await this.policyLoader.loadPolicy(this.options.apiKey);
|
|
203
|
+
}
|
|
204
|
+
return this.applyPolicyRules(result, policy);
|
|
205
|
+
} catch (error) {
|
|
206
|
+
if (this.options.debug) {
|
|
207
|
+
console.warn("[WasmDetector] Failed to load policy:", error);
|
|
208
|
+
}
|
|
209
|
+
return result;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Check if agent name matches a policy list entry
|
|
214
|
+
* Uses exact match or word-boundary prefix match to avoid false positives
|
|
215
|
+
* e.g., "gpt" matches "ChatGPT" and "GPT-4" but not "EgyptBot"
|
|
216
|
+
*/
|
|
217
|
+
matchesPolicyEntry(agentName, policyEntry) {
|
|
218
|
+
const lowerAgent = agentName.toLowerCase();
|
|
219
|
+
const lowerEntry = policyEntry.toLowerCase();
|
|
220
|
+
if (lowerAgent === lowerEntry) {
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
const wordBoundaryRegex = new RegExp(
|
|
224
|
+
`(^|[^a-z0-9])${this.escapeRegex(lowerEntry)}($|[^a-z0-9])`,
|
|
225
|
+
"i"
|
|
226
|
+
);
|
|
227
|
+
return wordBoundaryRegex.test(lowerAgent);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Escape special regex characters in a string
|
|
231
|
+
*/
|
|
232
|
+
escapeRegex(str) {
|
|
233
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Apply policy rules to detection result
|
|
237
|
+
*/
|
|
238
|
+
applyPolicyRules(result, policy) {
|
|
239
|
+
if (policy.denyList && result.detectedAgent) {
|
|
240
|
+
const agentName = result.detectedAgent.name;
|
|
241
|
+
const isDenied = policy.denyList.some((denied) => this.matchesPolicyEntry(agentName, denied));
|
|
242
|
+
if (isDenied) {
|
|
243
|
+
return {
|
|
244
|
+
...result,
|
|
245
|
+
shouldBlock: true,
|
|
246
|
+
blockReason: "deny_list"
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (policy.allowList && policy.allowList.length > 0 && result.isAgent) {
|
|
251
|
+
const agentName = result.detectedAgent?.name;
|
|
252
|
+
if (!agentName) ; else {
|
|
253
|
+
const isAllowed = policy.allowList.some(
|
|
254
|
+
(allowed) => this.matchesPolicyEntry(agentName, allowed)
|
|
255
|
+
);
|
|
256
|
+
if (!isAllowed) {
|
|
257
|
+
return {
|
|
258
|
+
...result,
|
|
259
|
+
shouldBlock: true,
|
|
260
|
+
blockReason: "not_in_allow_list"
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
...result,
|
|
265
|
+
shouldBlock: false
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
if (policy.blockThreshold !== void 0 && result.isAgent && result.confidence >= policy.blockThreshold) {
|
|
270
|
+
return {
|
|
271
|
+
...result,
|
|
272
|
+
shouldBlock: true,
|
|
273
|
+
blockReason: "threshold"
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
return result;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Infer agent type from name
|
|
280
|
+
*/
|
|
281
|
+
inferAgentType(agentName) {
|
|
282
|
+
const lowerName = agentName.toLowerCase();
|
|
283
|
+
if (lowerName.includes("chatgpt") || lowerName.includes("openai") || lowerName.includes("gpt")) {
|
|
284
|
+
return "openai";
|
|
285
|
+
}
|
|
286
|
+
if (lowerName.includes("claude") || lowerName.includes("anthropic")) {
|
|
287
|
+
return "anthropic";
|
|
288
|
+
}
|
|
289
|
+
if (lowerName.includes("perplexity")) {
|
|
290
|
+
return "perplexity";
|
|
291
|
+
}
|
|
292
|
+
if (lowerName.includes("gemini") || lowerName.includes("google")) {
|
|
293
|
+
return "google";
|
|
294
|
+
}
|
|
295
|
+
if (lowerName.includes("copilot") || lowerName.includes("bing")) {
|
|
296
|
+
return "microsoft";
|
|
297
|
+
}
|
|
298
|
+
return "unknown";
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Extract reasons from WASM result
|
|
302
|
+
*/
|
|
303
|
+
extractReasons(wasmResult) {
|
|
304
|
+
const reasons = [];
|
|
305
|
+
if (wasmResult.verification_method) {
|
|
306
|
+
reasons.push(`method:${wasmResult.verification_method}`);
|
|
307
|
+
}
|
|
308
|
+
if (wasmResult.agent) {
|
|
309
|
+
reasons.push(`agent:${wasmResult.agent.toLowerCase()}`);
|
|
310
|
+
}
|
|
311
|
+
return reasons;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Create default result (assumed human)
|
|
315
|
+
*/
|
|
316
|
+
createDefaultResult() {
|
|
317
|
+
return {
|
|
318
|
+
isAgent: false,
|
|
319
|
+
confidence: 0,
|
|
320
|
+
detectionClass: { type: "Human" },
|
|
321
|
+
verificationMethod: "none",
|
|
322
|
+
forgeabilityRisk: "high",
|
|
323
|
+
reasons: ["detection_failed"],
|
|
324
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
// src/wasm-bindgen/agentshield_wasm.js
|
|
330
|
+
var wasm;
|
|
331
|
+
var cachedTextDecoder = typeof TextDecoder !== "undefined" ? new TextDecoder("utf-8", { ignoreBOM: true, fatal: true }) : {
|
|
332
|
+
decode: () => {
|
|
333
|
+
throw Error("TextDecoder not available");
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
if (typeof TextDecoder !== "undefined") {
|
|
337
|
+
cachedTextDecoder.decode();
|
|
338
|
+
}
|
|
339
|
+
var cachedUint8ArrayMemory0 = null;
|
|
340
|
+
function getUint8ArrayMemory0() {
|
|
341
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
342
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
343
|
+
}
|
|
344
|
+
return cachedUint8ArrayMemory0;
|
|
345
|
+
}
|
|
346
|
+
function getStringFromWasm0(ptr, len) {
|
|
347
|
+
ptr = ptr >>> 0;
|
|
348
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
349
|
+
}
|
|
350
|
+
function logError(f, args) {
|
|
351
|
+
try {
|
|
352
|
+
return f.apply(this, args);
|
|
353
|
+
} catch (e) {
|
|
354
|
+
let error = (function() {
|
|
355
|
+
try {
|
|
356
|
+
return e instanceof Error ? `${e.message}
|
|
357
|
+
|
|
358
|
+
Stack:
|
|
359
|
+
${e.stack}` : e.toString();
|
|
360
|
+
} catch (_) {
|
|
361
|
+
return "<failed to stringify thrown value>";
|
|
362
|
+
}
|
|
363
|
+
})();
|
|
364
|
+
console.error(
|
|
365
|
+
"wasm-bindgen: imported JS function that was not marked as `catch` threw an error:",
|
|
366
|
+
error
|
|
367
|
+
);
|
|
368
|
+
throw e;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
var WASM_VECTOR_LEN = 0;
|
|
372
|
+
var cachedTextEncoder = typeof TextEncoder !== "undefined" ? new TextEncoder("utf-8") : {
|
|
373
|
+
encode: () => {
|
|
374
|
+
throw Error("TextEncoder not available");
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
var encodeString = typeof cachedTextEncoder.encodeInto === "function" ? function(arg, view) {
|
|
378
|
+
return cachedTextEncoder.encodeInto(arg, view);
|
|
379
|
+
} : function(arg, view) {
|
|
380
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
381
|
+
view.set(buf);
|
|
382
|
+
return {
|
|
383
|
+
read: arg.length,
|
|
384
|
+
written: buf.length
|
|
385
|
+
};
|
|
386
|
+
};
|
|
387
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
388
|
+
if (typeof arg !== "string") throw new Error(`expected a string argument, found ${typeof arg}`);
|
|
389
|
+
if (realloc === void 0) {
|
|
390
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
391
|
+
const ptr2 = malloc(buf.length, 1) >>> 0;
|
|
392
|
+
getUint8ArrayMemory0().subarray(ptr2, ptr2 + buf.length).set(buf);
|
|
393
|
+
WASM_VECTOR_LEN = buf.length;
|
|
394
|
+
return ptr2;
|
|
395
|
+
}
|
|
396
|
+
let len = arg.length;
|
|
397
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
398
|
+
const mem = getUint8ArrayMemory0();
|
|
399
|
+
let offset = 0;
|
|
400
|
+
for (; offset < len; offset++) {
|
|
401
|
+
const code = arg.charCodeAt(offset);
|
|
402
|
+
if (code > 127) break;
|
|
403
|
+
mem[ptr + offset] = code;
|
|
404
|
+
}
|
|
405
|
+
if (offset !== len) {
|
|
406
|
+
if (offset !== 0) {
|
|
407
|
+
arg = arg.slice(offset);
|
|
408
|
+
}
|
|
409
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
410
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
411
|
+
const ret = encodeString(arg, view);
|
|
412
|
+
if (ret.read !== arg.length) throw new Error("failed to pass whole string");
|
|
413
|
+
offset += ret.written;
|
|
414
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
415
|
+
}
|
|
416
|
+
WASM_VECTOR_LEN = offset;
|
|
417
|
+
return ptr;
|
|
418
|
+
}
|
|
419
|
+
var cachedDataViewMemory0 = null;
|
|
420
|
+
function getDataViewMemory0() {
|
|
421
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || cachedDataViewMemory0.buffer.detached === void 0 && cachedDataViewMemory0.buffer !== wasm.memory.buffer) {
|
|
422
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
423
|
+
}
|
|
424
|
+
return cachedDataViewMemory0;
|
|
425
|
+
}
|
|
426
|
+
function _assertNum(n) {
|
|
427
|
+
if (typeof n !== "number") throw new Error(`expected a number argument, found ${typeof n}`);
|
|
428
|
+
}
|
|
429
|
+
function _assertBoolean(n) {
|
|
430
|
+
if (typeof n !== "boolean") {
|
|
431
|
+
throw new Error(`expected a boolean argument, found ${typeof n}`);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
function isLikeNone(x) {
|
|
435
|
+
return x === void 0 || x === null;
|
|
436
|
+
}
|
|
437
|
+
function _assertClass(instance, klass) {
|
|
438
|
+
if (!(instance instanceof klass)) {
|
|
439
|
+
throw new Error(`expected instance of ${klass.name}`);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
function takeFromExternrefTable0(idx) {
|
|
443
|
+
const value = wasm.__wbindgen_export_3.get(idx);
|
|
444
|
+
wasm.__externref_table_dealloc(idx);
|
|
445
|
+
return value;
|
|
446
|
+
}
|
|
447
|
+
function detect_agent(metadata) {
|
|
448
|
+
_assertClass(metadata, JsRequestMetadata);
|
|
449
|
+
if (metadata.__wbg_ptr === 0) {
|
|
450
|
+
throw new Error("Attempt to use a moved value");
|
|
451
|
+
}
|
|
452
|
+
const ret = wasm.detect_agent(metadata.__wbg_ptr);
|
|
453
|
+
if (ret[2]) {
|
|
454
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
455
|
+
}
|
|
456
|
+
return JsDetectionResult.__wrap(ret[0]);
|
|
457
|
+
}
|
|
458
|
+
function version() {
|
|
459
|
+
let deferred1_0;
|
|
460
|
+
let deferred1_1;
|
|
461
|
+
try {
|
|
462
|
+
const ret = wasm.version();
|
|
463
|
+
deferred1_0 = ret[0];
|
|
464
|
+
deferred1_1 = ret[1];
|
|
465
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
466
|
+
} finally {
|
|
467
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
var JsDetectionResultFinalization = typeof FinalizationRegistry === "undefined" ? { register: () => {
|
|
471
|
+
}, unregister: () => {
|
|
472
|
+
} } : new FinalizationRegistry((ptr) => wasm.__wbg_jsdetectionresult_free(ptr >>> 0, 1));
|
|
473
|
+
var JsDetectionResult = class _JsDetectionResult {
|
|
474
|
+
constructor() {
|
|
475
|
+
throw new Error("cannot invoke `new` directly");
|
|
476
|
+
}
|
|
477
|
+
static __wrap(ptr) {
|
|
478
|
+
ptr = ptr >>> 0;
|
|
479
|
+
const obj = Object.create(_JsDetectionResult.prototype);
|
|
480
|
+
obj.__wbg_ptr = ptr;
|
|
481
|
+
JsDetectionResultFinalization.register(obj, obj.__wbg_ptr, obj);
|
|
482
|
+
return obj;
|
|
483
|
+
}
|
|
484
|
+
__destroy_into_raw() {
|
|
485
|
+
const ptr = this.__wbg_ptr;
|
|
486
|
+
this.__wbg_ptr = 0;
|
|
487
|
+
JsDetectionResultFinalization.unregister(this);
|
|
488
|
+
return ptr;
|
|
489
|
+
}
|
|
490
|
+
free() {
|
|
491
|
+
const ptr = this.__destroy_into_raw();
|
|
492
|
+
wasm.__wbg_jsdetectionresult_free(ptr, 0);
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Whether the request was identified as coming from an agent
|
|
496
|
+
* @returns {boolean}
|
|
497
|
+
*/
|
|
498
|
+
get is_agent() {
|
|
499
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
500
|
+
_assertNum(this.__wbg_ptr);
|
|
501
|
+
const ret = wasm.__wbg_get_jsdetectionresult_is_agent(this.__wbg_ptr);
|
|
502
|
+
return ret !== 0;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Whether the request was identified as coming from an agent
|
|
506
|
+
* @param {boolean} arg0
|
|
507
|
+
*/
|
|
508
|
+
set is_agent(arg0) {
|
|
509
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
510
|
+
_assertNum(this.__wbg_ptr);
|
|
511
|
+
_assertBoolean(arg0);
|
|
512
|
+
wasm.__wbg_set_jsdetectionresult_is_agent(this.__wbg_ptr, arg0);
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Confidence score (0.0 to 1.0)
|
|
516
|
+
* @returns {number}
|
|
517
|
+
*/
|
|
518
|
+
get confidence() {
|
|
519
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
520
|
+
_assertNum(this.__wbg_ptr);
|
|
521
|
+
const ret = wasm.__wbg_get_jsdetectionresult_confidence(this.__wbg_ptr);
|
|
522
|
+
return ret;
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Confidence score (0.0 to 1.0)
|
|
526
|
+
* @param {number} arg0
|
|
527
|
+
*/
|
|
528
|
+
set confidence(arg0) {
|
|
529
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
530
|
+
_assertNum(this.__wbg_ptr);
|
|
531
|
+
wasm.__wbg_set_jsdetectionresult_confidence(this.__wbg_ptr, arg0);
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Get the detected agent name
|
|
535
|
+
* @returns {string | undefined}
|
|
536
|
+
*/
|
|
537
|
+
get agent() {
|
|
538
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
539
|
+
_assertNum(this.__wbg_ptr);
|
|
540
|
+
const ret = wasm.jsdetectionresult_agent(this.__wbg_ptr);
|
|
541
|
+
let v1;
|
|
542
|
+
if (ret[0] !== 0) {
|
|
543
|
+
v1 = getStringFromWasm0(ret[0], ret[1]).slice();
|
|
544
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
545
|
+
}
|
|
546
|
+
return v1;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Get the verification method as a string
|
|
550
|
+
* @returns {string}
|
|
551
|
+
*/
|
|
552
|
+
get verification_method() {
|
|
553
|
+
let deferred1_0;
|
|
554
|
+
let deferred1_1;
|
|
555
|
+
try {
|
|
556
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
557
|
+
_assertNum(this.__wbg_ptr);
|
|
558
|
+
const ret = wasm.jsdetectionresult_verification_method(this.__wbg_ptr);
|
|
559
|
+
deferred1_0 = ret[0];
|
|
560
|
+
deferred1_1 = ret[1];
|
|
561
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
562
|
+
} finally {
|
|
563
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Get the risk level as a string
|
|
568
|
+
* @returns {string}
|
|
569
|
+
*/
|
|
570
|
+
get risk_level() {
|
|
571
|
+
let deferred1_0;
|
|
572
|
+
let deferred1_1;
|
|
573
|
+
try {
|
|
574
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
575
|
+
_assertNum(this.__wbg_ptr);
|
|
576
|
+
const ret = wasm.jsdetectionresult_risk_level(this.__wbg_ptr);
|
|
577
|
+
deferred1_0 = ret[0];
|
|
578
|
+
deferred1_1 = ret[1];
|
|
579
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
580
|
+
} finally {
|
|
581
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Get the timestamp as a string
|
|
586
|
+
* @returns {string}
|
|
587
|
+
*/
|
|
588
|
+
get timestamp() {
|
|
589
|
+
let deferred1_0;
|
|
590
|
+
let deferred1_1;
|
|
591
|
+
try {
|
|
592
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
593
|
+
_assertNum(this.__wbg_ptr);
|
|
594
|
+
const ret = wasm.jsdetectionresult_timestamp(this.__wbg_ptr);
|
|
595
|
+
deferred1_0 = ret[0];
|
|
596
|
+
deferred1_1 = ret[1];
|
|
597
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
598
|
+
} finally {
|
|
599
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
var JsRequestMetadataFinalization = typeof FinalizationRegistry === "undefined" ? { register: () => {
|
|
604
|
+
}, unregister: () => {
|
|
605
|
+
} } : new FinalizationRegistry((ptr) => wasm.__wbg_jsrequestmetadata_free(ptr >>> 0, 1));
|
|
606
|
+
var JsRequestMetadata = class {
|
|
607
|
+
__destroy_into_raw() {
|
|
608
|
+
const ptr = this.__wbg_ptr;
|
|
609
|
+
this.__wbg_ptr = 0;
|
|
610
|
+
JsRequestMetadataFinalization.unregister(this);
|
|
611
|
+
return ptr;
|
|
612
|
+
}
|
|
613
|
+
free() {
|
|
614
|
+
const ptr = this.__destroy_into_raw();
|
|
615
|
+
wasm.__wbg_jsrequestmetadata_free(ptr, 0);
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Constructor for JsRequestMetadata
|
|
619
|
+
* @param {string | null | undefined} user_agent
|
|
620
|
+
* @param {string | null | undefined} ip_address
|
|
621
|
+
* @param {string} headers
|
|
622
|
+
* @param {string} timestamp
|
|
623
|
+
* @param {string | null} [url]
|
|
624
|
+
* @param {string | null} [method]
|
|
625
|
+
* @param {string | null} [client_fingerprint]
|
|
626
|
+
*/
|
|
627
|
+
constructor(user_agent, ip_address, headers, timestamp, url, method, client_fingerprint) {
|
|
628
|
+
var ptr0 = isLikeNone(user_agent) ? 0 : passStringToWasm0(user_agent, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
629
|
+
var len0 = WASM_VECTOR_LEN;
|
|
630
|
+
var ptr1 = isLikeNone(ip_address) ? 0 : passStringToWasm0(ip_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
631
|
+
var len1 = WASM_VECTOR_LEN;
|
|
632
|
+
const ptr2 = passStringToWasm0(headers, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
633
|
+
const len2 = WASM_VECTOR_LEN;
|
|
634
|
+
const ptr3 = passStringToWasm0(timestamp, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
635
|
+
const len3 = WASM_VECTOR_LEN;
|
|
636
|
+
var ptr4 = isLikeNone(url) ? 0 : passStringToWasm0(url, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
637
|
+
var len4 = WASM_VECTOR_LEN;
|
|
638
|
+
var ptr5 = isLikeNone(method) ? 0 : passStringToWasm0(method, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
639
|
+
var len5 = WASM_VECTOR_LEN;
|
|
640
|
+
var ptr6 = isLikeNone(client_fingerprint) ? 0 : passStringToWasm0(client_fingerprint, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
641
|
+
var len6 = WASM_VECTOR_LEN;
|
|
642
|
+
const ret = wasm.jsrequestmetadata_new(
|
|
643
|
+
ptr0,
|
|
644
|
+
len0,
|
|
645
|
+
ptr1,
|
|
646
|
+
len1,
|
|
647
|
+
ptr2,
|
|
648
|
+
len2,
|
|
649
|
+
ptr3,
|
|
650
|
+
len3,
|
|
651
|
+
ptr4,
|
|
652
|
+
len4,
|
|
653
|
+
ptr5,
|
|
654
|
+
len5,
|
|
655
|
+
ptr6,
|
|
656
|
+
len6
|
|
657
|
+
);
|
|
658
|
+
this.__wbg_ptr = ret >>> 0;
|
|
659
|
+
JsRequestMetadataFinalization.register(this, this.__wbg_ptr, this);
|
|
660
|
+
return this;
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Get the user agent
|
|
664
|
+
* @returns {string | undefined}
|
|
665
|
+
*/
|
|
666
|
+
get user_agent() {
|
|
667
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
668
|
+
_assertNum(this.__wbg_ptr);
|
|
669
|
+
const ret = wasm.jsrequestmetadata_user_agent(this.__wbg_ptr);
|
|
670
|
+
let v1;
|
|
671
|
+
if (ret[0] !== 0) {
|
|
672
|
+
v1 = getStringFromWasm0(ret[0], ret[1]).slice();
|
|
673
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
674
|
+
}
|
|
675
|
+
return v1;
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Get the IP address
|
|
679
|
+
* @returns {string | undefined}
|
|
680
|
+
*/
|
|
681
|
+
get ip_address() {
|
|
682
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
683
|
+
_assertNum(this.__wbg_ptr);
|
|
684
|
+
const ret = wasm.jsrequestmetadata_ip_address(this.__wbg_ptr);
|
|
685
|
+
let v1;
|
|
686
|
+
if (ret[0] !== 0) {
|
|
687
|
+
v1 = getStringFromWasm0(ret[0], ret[1]).slice();
|
|
688
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
689
|
+
}
|
|
690
|
+
return v1;
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Get the headers as JSON string
|
|
694
|
+
* @returns {string}
|
|
695
|
+
*/
|
|
696
|
+
get headers() {
|
|
697
|
+
let deferred1_0;
|
|
698
|
+
let deferred1_1;
|
|
699
|
+
try {
|
|
700
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
701
|
+
_assertNum(this.__wbg_ptr);
|
|
702
|
+
const ret = wasm.jsrequestmetadata_headers(this.__wbg_ptr);
|
|
703
|
+
deferred1_0 = ret[0];
|
|
704
|
+
deferred1_1 = ret[1];
|
|
705
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
706
|
+
} finally {
|
|
707
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Get the timestamp
|
|
712
|
+
* @returns {string}
|
|
713
|
+
*/
|
|
714
|
+
get timestamp() {
|
|
715
|
+
let deferred1_0;
|
|
716
|
+
let deferred1_1;
|
|
717
|
+
try {
|
|
718
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
719
|
+
_assertNum(this.__wbg_ptr);
|
|
720
|
+
const ret = wasm.jsrequestmetadata_timestamp(this.__wbg_ptr);
|
|
721
|
+
deferred1_0 = ret[0];
|
|
722
|
+
deferred1_1 = ret[1];
|
|
723
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
724
|
+
} finally {
|
|
725
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* Get the URL
|
|
730
|
+
* @returns {string | undefined}
|
|
731
|
+
*/
|
|
732
|
+
get url() {
|
|
733
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
734
|
+
_assertNum(this.__wbg_ptr);
|
|
735
|
+
const ret = wasm.jsrequestmetadata_url(this.__wbg_ptr);
|
|
736
|
+
let v1;
|
|
737
|
+
if (ret[0] !== 0) {
|
|
738
|
+
v1 = getStringFromWasm0(ret[0], ret[1]).slice();
|
|
739
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
740
|
+
}
|
|
741
|
+
return v1;
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
* Get the method
|
|
745
|
+
* @returns {string | undefined}
|
|
746
|
+
*/
|
|
747
|
+
get method() {
|
|
748
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
749
|
+
_assertNum(this.__wbg_ptr);
|
|
750
|
+
const ret = wasm.jsrequestmetadata_method(this.__wbg_ptr);
|
|
751
|
+
let v1;
|
|
752
|
+
if (ret[0] !== 0) {
|
|
753
|
+
v1 = getStringFromWasm0(ret[0], ret[1]).slice();
|
|
754
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
755
|
+
}
|
|
756
|
+
return v1;
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* Get the client fingerprint
|
|
760
|
+
* @returns {string | undefined}
|
|
761
|
+
*/
|
|
762
|
+
get client_fingerprint() {
|
|
763
|
+
if (this.__wbg_ptr == 0) throw new Error("Attempt to use a moved value");
|
|
764
|
+
_assertNum(this.__wbg_ptr);
|
|
765
|
+
const ret = wasm.jsrequestmetadata_client_fingerprint(this.__wbg_ptr);
|
|
766
|
+
let v1;
|
|
767
|
+
if (ret[0] !== 0) {
|
|
768
|
+
v1 = getStringFromWasm0(ret[0], ret[1]).slice();
|
|
769
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
770
|
+
}
|
|
771
|
+
return v1;
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
function __wbg_get_imports() {
|
|
775
|
+
const imports = {};
|
|
776
|
+
imports.wbg = {};
|
|
777
|
+
imports.wbg.__wbg_error_7534b8e9a36f1ab4 = function() {
|
|
778
|
+
return logError(function(arg0, arg1) {
|
|
779
|
+
let deferred0_0;
|
|
780
|
+
let deferred0_1;
|
|
781
|
+
try {
|
|
782
|
+
deferred0_0 = arg0;
|
|
783
|
+
deferred0_1 = arg1;
|
|
784
|
+
console.error(getStringFromWasm0(arg0, arg1));
|
|
785
|
+
} finally {
|
|
786
|
+
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
|
|
787
|
+
}
|
|
788
|
+
}, arguments);
|
|
789
|
+
};
|
|
790
|
+
imports.wbg.__wbg_getTime_46267b1c24877e30 = function() {
|
|
791
|
+
return logError(function(arg0) {
|
|
792
|
+
const ret = arg0.getTime();
|
|
793
|
+
return ret;
|
|
794
|
+
}, arguments);
|
|
795
|
+
};
|
|
796
|
+
imports.wbg.__wbg_log_c222819a41e063d3 = function() {
|
|
797
|
+
return logError(function(arg0) {
|
|
798
|
+
console.log(arg0);
|
|
799
|
+
}, arguments);
|
|
800
|
+
};
|
|
801
|
+
imports.wbg.__wbg_new_31a97dac4f10fab7 = function() {
|
|
802
|
+
return logError(function(arg0) {
|
|
803
|
+
const ret = new Date(arg0);
|
|
804
|
+
return ret;
|
|
805
|
+
}, arguments);
|
|
806
|
+
};
|
|
807
|
+
imports.wbg.__wbg_new_8a6f238a6ece86ea = function() {
|
|
808
|
+
return logError(function() {
|
|
809
|
+
const ret = new Error();
|
|
810
|
+
return ret;
|
|
811
|
+
}, arguments);
|
|
812
|
+
};
|
|
813
|
+
imports.wbg.__wbg_now_807e54c39636c349 = function() {
|
|
814
|
+
return logError(function() {
|
|
815
|
+
const ret = Date.now();
|
|
816
|
+
return ret;
|
|
817
|
+
}, arguments);
|
|
818
|
+
};
|
|
819
|
+
imports.wbg.__wbg_stack_0ed75d68575b0f3c = function() {
|
|
820
|
+
return logError(function(arg0, arg1) {
|
|
821
|
+
const ret = arg1.stack;
|
|
822
|
+
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
823
|
+
const len1 = WASM_VECTOR_LEN;
|
|
824
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
825
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
826
|
+
}, arguments);
|
|
827
|
+
};
|
|
828
|
+
imports.wbg.__wbindgen_init_externref_table = function() {
|
|
829
|
+
const table = wasm.__wbindgen_export_3;
|
|
830
|
+
const offset = table.grow(4);
|
|
831
|
+
table.set(0, void 0);
|
|
832
|
+
table.set(offset + 0, void 0);
|
|
833
|
+
table.set(offset + 1, null);
|
|
834
|
+
table.set(offset + 2, true);
|
|
835
|
+
table.set(offset + 3, false);
|
|
836
|
+
};
|
|
837
|
+
imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
|
|
838
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
839
|
+
return ret;
|
|
840
|
+
};
|
|
841
|
+
imports.wbg.__wbindgen_throw = function(arg0, arg1) {
|
|
842
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
843
|
+
};
|
|
844
|
+
return imports;
|
|
845
|
+
}
|
|
846
|
+
function __wbg_finalize_init(instance, module) {
|
|
847
|
+
wasm = instance.exports;
|
|
848
|
+
cachedDataViewMemory0 = null;
|
|
849
|
+
cachedUint8ArrayMemory0 = null;
|
|
850
|
+
wasm.__wbindgen_start();
|
|
851
|
+
return wasm;
|
|
852
|
+
}
|
|
853
|
+
function initSync(module) {
|
|
854
|
+
if (wasm !== void 0) return wasm;
|
|
855
|
+
if (typeof module !== "undefined") {
|
|
856
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
857
|
+
({ module } = module);
|
|
858
|
+
} else {
|
|
859
|
+
console.warn("using deprecated parameters for `initSync()`; pass a single object instead");
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
const imports = __wbg_get_imports();
|
|
863
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
864
|
+
module = new WebAssembly.Module(module);
|
|
865
|
+
}
|
|
866
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
867
|
+
return __wbg_finalize_init(instance);
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
// src/loaders/static-loader.ts
|
|
871
|
+
var StaticWasmLoader = class {
|
|
872
|
+
/**
|
|
873
|
+
* Create a new StaticWasmLoader
|
|
874
|
+
* @param wasmModule - Pre-compiled WebAssembly.Module from static import
|
|
875
|
+
*/
|
|
876
|
+
constructor(wasmModule) {
|
|
877
|
+
this.wasmModule = wasmModule;
|
|
878
|
+
if (!wasmModule) {
|
|
879
|
+
throw new Error(
|
|
880
|
+
"StaticWasmLoader requires a WebAssembly.Module. Import with: import wasmModule from './wasm.wasm?module'"
|
|
881
|
+
);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
bindings = null;
|
|
885
|
+
loadPromise = null;
|
|
886
|
+
wasmExports = null;
|
|
887
|
+
/**
|
|
888
|
+
* Load and instantiate the WASM module
|
|
889
|
+
*/
|
|
890
|
+
async load() {
|
|
891
|
+
if (this.bindings) {
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
if (this.loadPromise) {
|
|
895
|
+
return this.loadPromise;
|
|
896
|
+
}
|
|
897
|
+
this.loadPromise = this.doLoad();
|
|
898
|
+
return this.loadPromise;
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* Internal load implementation using wasm-bindgen initSync
|
|
902
|
+
*/
|
|
903
|
+
async doLoad() {
|
|
904
|
+
try {
|
|
905
|
+
this.wasmExports = initSync({ module: this.wasmModule });
|
|
906
|
+
this.bindings = this.createBindings();
|
|
907
|
+
} catch (error) {
|
|
908
|
+
this.loadPromise = null;
|
|
909
|
+
throw new Error(
|
|
910
|
+
`Failed to instantiate WASM module: ${error instanceof Error ? error.message : String(error)}`
|
|
911
|
+
);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
/**
|
|
915
|
+
* Get the WASM bindings after loading
|
|
916
|
+
*/
|
|
917
|
+
getBindings() {
|
|
918
|
+
if (!this.bindings) {
|
|
919
|
+
throw new Error("WASM not loaded. Call load() first.");
|
|
920
|
+
}
|
|
921
|
+
return this.bindings;
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* Check if WASM is loaded
|
|
925
|
+
*/
|
|
926
|
+
isLoaded() {
|
|
927
|
+
return this.bindings !== null;
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
* Get the loading strategy name
|
|
931
|
+
*/
|
|
932
|
+
getStrategy() {
|
|
933
|
+
return "static-import";
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* Create bindings wrapper using wasm-bindgen exports
|
|
937
|
+
*/
|
|
938
|
+
createBindings() {
|
|
939
|
+
return {
|
|
940
|
+
detect_agent: (metadata) => {
|
|
941
|
+
const wasmMetadata = new JsRequestMetadata(
|
|
942
|
+
metadata.user_agent,
|
|
943
|
+
metadata.ip_address,
|
|
944
|
+
metadata.headers,
|
|
945
|
+
metadata.timestamp,
|
|
946
|
+
metadata.url,
|
|
947
|
+
metadata.method,
|
|
948
|
+
metadata.client_fingerprint
|
|
949
|
+
);
|
|
950
|
+
let result = null;
|
|
951
|
+
try {
|
|
952
|
+
result = detect_agent(wasmMetadata);
|
|
953
|
+
return {
|
|
954
|
+
is_agent: result.is_agent,
|
|
955
|
+
confidence: result.confidence,
|
|
956
|
+
agent: result.agent ?? null,
|
|
957
|
+
// Convert undefined to null
|
|
958
|
+
verification_method: result.verification_method,
|
|
959
|
+
risk_level: result.risk_level,
|
|
960
|
+
timestamp: result.timestamp
|
|
961
|
+
};
|
|
962
|
+
} finally {
|
|
963
|
+
result?.free();
|
|
964
|
+
wasmMetadata.free();
|
|
965
|
+
}
|
|
966
|
+
},
|
|
967
|
+
get_version: () => {
|
|
968
|
+
try {
|
|
969
|
+
return version();
|
|
970
|
+
} catch {
|
|
971
|
+
return "0.1.0-static";
|
|
972
|
+
}
|
|
973
|
+
},
|
|
974
|
+
get_build_info: () => {
|
|
975
|
+
return JSON.stringify({ version: "0.1.0", strategy: "static-import" });
|
|
976
|
+
}
|
|
977
|
+
};
|
|
978
|
+
}
|
|
979
|
+
};
|
|
980
|
+
function createStaticLoader(wasmModule) {
|
|
981
|
+
return new StaticWasmLoader(wasmModule);
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
// src/policy/policy-loader.ts
|
|
985
|
+
var DEFAULT_CONFIG = {
|
|
986
|
+
apiUrl: "https://api.agentshield.io",
|
|
987
|
+
cacheTTL: 5 * 60 * 1e3,
|
|
988
|
+
// 5 minutes
|
|
989
|
+
maxCacheSize: 100,
|
|
990
|
+
backgroundRefresh: true,
|
|
991
|
+
timeout: 5e3
|
|
992
|
+
};
|
|
993
|
+
var PolicyLoader = class {
|
|
994
|
+
cache = /* @__PURE__ */ new Map();
|
|
995
|
+
config;
|
|
996
|
+
constructor(config = {}) {
|
|
997
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* Load policy for an API key
|
|
1001
|
+
*/
|
|
1002
|
+
async loadPolicy(apiKey) {
|
|
1003
|
+
const cached = this.getCachedPolicy(apiKey);
|
|
1004
|
+
if (cached) {
|
|
1005
|
+
if (this.config.backgroundRefresh && this.shouldRefresh(apiKey)) {
|
|
1006
|
+
this.refreshInBackground(apiKey);
|
|
1007
|
+
}
|
|
1008
|
+
return cached;
|
|
1009
|
+
}
|
|
1010
|
+
return this.fetchPolicy(apiKey);
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Get cached policy if available and not expired
|
|
1014
|
+
*/
|
|
1015
|
+
getCachedPolicy(apiKey) {
|
|
1016
|
+
const entry = this.cache.get(apiKey);
|
|
1017
|
+
if (!entry) {
|
|
1018
|
+
return null;
|
|
1019
|
+
}
|
|
1020
|
+
if (this.isExpired(entry)) {
|
|
1021
|
+
this.cache.delete(apiKey);
|
|
1022
|
+
return null;
|
|
1023
|
+
}
|
|
1024
|
+
return entry.policy;
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* Invalidate cached policy
|
|
1028
|
+
*/
|
|
1029
|
+
invalidateCache(apiKey) {
|
|
1030
|
+
this.cache.delete(apiKey);
|
|
1031
|
+
}
|
|
1032
|
+
/**
|
|
1033
|
+
* Fetch policy from API and cache it
|
|
1034
|
+
*/
|
|
1035
|
+
async fetchPolicy(apiKey) {
|
|
1036
|
+
const policy = await this.fetchPolicyFromApi(apiKey);
|
|
1037
|
+
this.cachePolicy(apiKey, policy);
|
|
1038
|
+
return policy;
|
|
1039
|
+
}
|
|
1040
|
+
/**
|
|
1041
|
+
* Fetch policy from API without caching
|
|
1042
|
+
* Used internally for both direct fetches and background refreshes
|
|
1043
|
+
*/
|
|
1044
|
+
async fetchPolicyFromApi(apiKey) {
|
|
1045
|
+
const controller = new AbortController();
|
|
1046
|
+
const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
|
|
1047
|
+
try {
|
|
1048
|
+
const response = await fetch(`${this.config.apiUrl}/api/v1/policy`, {
|
|
1049
|
+
method: "GET",
|
|
1050
|
+
headers: {
|
|
1051
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1052
|
+
"Content-Type": "application/json",
|
|
1053
|
+
"User-Agent": "agentshield-wasm-runtime/0.1.0"
|
|
1054
|
+
},
|
|
1055
|
+
signal: controller.signal
|
|
1056
|
+
});
|
|
1057
|
+
clearTimeout(timeoutId);
|
|
1058
|
+
if (!response.ok) {
|
|
1059
|
+
if (response.status === 401) {
|
|
1060
|
+
throw new PolicyLoadError("Invalid API key", "INVALID_API_KEY");
|
|
1061
|
+
}
|
|
1062
|
+
if (response.status === 404) {
|
|
1063
|
+
return this.getDefaultPolicy(apiKey);
|
|
1064
|
+
}
|
|
1065
|
+
throw new PolicyLoadError(`Failed to load policy: ${response.status}`, "API_ERROR");
|
|
1066
|
+
}
|
|
1067
|
+
return await response.json();
|
|
1068
|
+
} catch (error) {
|
|
1069
|
+
clearTimeout(timeoutId);
|
|
1070
|
+
if (error instanceof PolicyLoadError) {
|
|
1071
|
+
throw error;
|
|
1072
|
+
}
|
|
1073
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
1074
|
+
throw new PolicyLoadError("Policy request timeout", "TIMEOUT");
|
|
1075
|
+
}
|
|
1076
|
+
throw new PolicyLoadError(
|
|
1077
|
+
`Failed to load policy: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
1078
|
+
"NETWORK_ERROR"
|
|
1079
|
+
);
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
/**
|
|
1083
|
+
* Cache a policy
|
|
1084
|
+
*/
|
|
1085
|
+
cachePolicy(apiKey, policy) {
|
|
1086
|
+
if (this.cache.size >= this.config.maxCacheSize) {
|
|
1087
|
+
const oldestKey = this.cache.keys().next().value;
|
|
1088
|
+
if (oldestKey) {
|
|
1089
|
+
this.cache.delete(oldestKey);
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
this.cache.set(apiKey, {
|
|
1093
|
+
policy,
|
|
1094
|
+
loadedAt: Date.now(),
|
|
1095
|
+
refreshing: false
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
/**
|
|
1099
|
+
* Check if cached entry is expired
|
|
1100
|
+
*/
|
|
1101
|
+
isExpired(entry) {
|
|
1102
|
+
return Date.now() - entry.loadedAt > this.config.cacheTTL;
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* Check if cache entry should be refreshed
|
|
1106
|
+
*/
|
|
1107
|
+
shouldRefresh(apiKey) {
|
|
1108
|
+
const entry = this.cache.get(apiKey);
|
|
1109
|
+
if (!entry) {
|
|
1110
|
+
return false;
|
|
1111
|
+
}
|
|
1112
|
+
const refreshThreshold = this.config.cacheTTL * 0.8;
|
|
1113
|
+
return Date.now() - entry.loadedAt > refreshThreshold && !entry.refreshing;
|
|
1114
|
+
}
|
|
1115
|
+
/**
|
|
1116
|
+
* Refresh policy in background
|
|
1117
|
+
*/
|
|
1118
|
+
async refreshInBackground(apiKey) {
|
|
1119
|
+
const entry = this.cache.get(apiKey);
|
|
1120
|
+
if (!entry || entry.refreshing) {
|
|
1121
|
+
return;
|
|
1122
|
+
}
|
|
1123
|
+
entry.refreshing = true;
|
|
1124
|
+
try {
|
|
1125
|
+
const policy = await this.fetchPolicyFromApi(apiKey);
|
|
1126
|
+
this.cachePolicy(apiKey, policy);
|
|
1127
|
+
} catch {
|
|
1128
|
+
} finally {
|
|
1129
|
+
const currentEntry = this.cache.get(apiKey);
|
|
1130
|
+
if (currentEntry && currentEntry.refreshing) {
|
|
1131
|
+
currentEntry.refreshing = false;
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
/**
|
|
1136
|
+
* Get default policy for a project
|
|
1137
|
+
*/
|
|
1138
|
+
getDefaultPolicy(apiKey) {
|
|
1139
|
+
return {
|
|
1140
|
+
projectId: apiKey.split("_")[1] || "unknown",
|
|
1141
|
+
blockThreshold: 90
|
|
1142
|
+
};
|
|
1143
|
+
}
|
|
1144
|
+
};
|
|
1145
|
+
var PolicyLoadError = class extends Error {
|
|
1146
|
+
constructor(message, code) {
|
|
1147
|
+
super(message);
|
|
1148
|
+
this.code = code;
|
|
1149
|
+
this.name = "PolicyLoadError";
|
|
1150
|
+
}
|
|
1151
|
+
};
|
|
1152
|
+
function createPolicyLoader(config) {
|
|
1153
|
+
return new PolicyLoader(config);
|
|
1154
|
+
}
|
|
1155
|
+
var RulesDetector = class {
|
|
1156
|
+
rules = null;
|
|
1157
|
+
ready = false;
|
|
1158
|
+
/**
|
|
1159
|
+
* Analyze a request and detect if it's from an agent
|
|
1160
|
+
*/
|
|
1161
|
+
async detect(input) {
|
|
1162
|
+
await this.ensureReady();
|
|
1163
|
+
const reasons = [];
|
|
1164
|
+
let confidence = 0;
|
|
1165
|
+
let detectedAgentName = null;
|
|
1166
|
+
let verificationMethod = "none";
|
|
1167
|
+
const userAgent = input.userAgent || input.headers["user-agent"] || "";
|
|
1168
|
+
const normalizedHeaders = this.normalizeHeaders(input.headers);
|
|
1169
|
+
if (userAgent && this.rules) {
|
|
1170
|
+
const uaMatch = this.matchUserAgent(userAgent);
|
|
1171
|
+
if (uaMatch) {
|
|
1172
|
+
confidence = Math.max(confidence, uaMatch.confidence);
|
|
1173
|
+
detectedAgentName = uaMatch.agentName;
|
|
1174
|
+
verificationMethod = "pattern";
|
|
1175
|
+
reasons.push(`pattern:${uaMatch.agentKey}`);
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
if (this.rules) {
|
|
1179
|
+
const headerMatch = this.matchHeaders(normalizedHeaders);
|
|
1180
|
+
if (headerMatch.confidence > 0) {
|
|
1181
|
+
confidence = Math.max(confidence, headerMatch.confidence);
|
|
1182
|
+
reasons.push(`headers:${headerMatch.count}`);
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
if (this.hasSignatureHeaders(normalizedHeaders)) {
|
|
1186
|
+
confidence = Math.max(confidence, CONFIDENCE.SIGNATURE_PRESENT);
|
|
1187
|
+
reasons.push("signature_headers_present");
|
|
1188
|
+
verificationMethod = "pattern";
|
|
1189
|
+
}
|
|
1190
|
+
const isAgent = confidence > CONFIDENCE.THRESHOLD_AGENT;
|
|
1191
|
+
const detectionClass = this.determineDetectionClass(isAgent, detectedAgentName, confidence);
|
|
1192
|
+
return {
|
|
1193
|
+
isAgent,
|
|
1194
|
+
confidence,
|
|
1195
|
+
detectionClass,
|
|
1196
|
+
detectedAgent: detectedAgentName ? {
|
|
1197
|
+
type: this.inferAgentType(detectedAgentName),
|
|
1198
|
+
name: detectedAgentName
|
|
1199
|
+
} : void 0,
|
|
1200
|
+
verificationMethod,
|
|
1201
|
+
forgeabilityRisk: "high",
|
|
1202
|
+
// JS fallback is always high risk
|
|
1203
|
+
reasons,
|
|
1204
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
/**
|
|
1208
|
+
* Check if the detector is ready
|
|
1209
|
+
*/
|
|
1210
|
+
isReady() {
|
|
1211
|
+
return this.ready;
|
|
1212
|
+
}
|
|
1213
|
+
/**
|
|
1214
|
+
* Ensure the detector is initialized
|
|
1215
|
+
*/
|
|
1216
|
+
async ensureReady() {
|
|
1217
|
+
if (this.ready) {
|
|
1218
|
+
return;
|
|
1219
|
+
}
|
|
1220
|
+
try {
|
|
1221
|
+
this.rules = checkpointShared.loadRulesSync();
|
|
1222
|
+
this.ready = true;
|
|
1223
|
+
} catch (error) {
|
|
1224
|
+
console.warn("[RulesDetector] Failed to load rules:", error);
|
|
1225
|
+
this.ready = true;
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Get detector version
|
|
1230
|
+
*/
|
|
1231
|
+
async getVersion() {
|
|
1232
|
+
return "0.1.0-js-fallback";
|
|
1233
|
+
}
|
|
1234
|
+
/**
|
|
1235
|
+
* Normalize headers to lowercase keys
|
|
1236
|
+
*/
|
|
1237
|
+
normalizeHeaders(headers) {
|
|
1238
|
+
const normalized = {};
|
|
1239
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
1240
|
+
normalized[key.toLowerCase()] = value;
|
|
1241
|
+
}
|
|
1242
|
+
return normalized;
|
|
1243
|
+
}
|
|
1244
|
+
/**
|
|
1245
|
+
* Match user agent against rules
|
|
1246
|
+
*/
|
|
1247
|
+
matchUserAgent(userAgent) {
|
|
1248
|
+
if (!this.rules) {
|
|
1249
|
+
return null;
|
|
1250
|
+
}
|
|
1251
|
+
const userAgents = this.rules.rules.userAgents;
|
|
1252
|
+
const genericKeys = ["generic_bot", "dev_tools", "automation_tools"];
|
|
1253
|
+
const entries = Object.entries(userAgents).sort((a, b) => {
|
|
1254
|
+
const aIsGeneric = genericKeys.includes(a[0]);
|
|
1255
|
+
const bIsGeneric = genericKeys.includes(b[0]);
|
|
1256
|
+
if (aIsGeneric && !bIsGeneric) return 1;
|
|
1257
|
+
if (!aIsGeneric && bIsGeneric) return -1;
|
|
1258
|
+
return 0;
|
|
1259
|
+
});
|
|
1260
|
+
for (const [agentKey, rule] of entries) {
|
|
1261
|
+
for (const pattern of rule.patterns) {
|
|
1262
|
+
try {
|
|
1263
|
+
const regex = new RegExp(pattern, "i");
|
|
1264
|
+
if (regex.test(userAgent)) {
|
|
1265
|
+
return {
|
|
1266
|
+
// Convert 0-1 rule confidence to 0-100 scale
|
|
1267
|
+
confidence: rule.confidence * 100,
|
|
1268
|
+
agentName: this.getAgentName(agentKey),
|
|
1269
|
+
agentKey
|
|
1270
|
+
};
|
|
1271
|
+
}
|
|
1272
|
+
} catch {
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
return null;
|
|
1277
|
+
}
|
|
1278
|
+
/**
|
|
1279
|
+
* Match headers against suspicious header rules
|
|
1280
|
+
*/
|
|
1281
|
+
matchHeaders(headers) {
|
|
1282
|
+
if (!this.rules) {
|
|
1283
|
+
return { confidence: 0, count: 0 };
|
|
1284
|
+
}
|
|
1285
|
+
const suspicious = this.rules.rules.headers.suspicious || [];
|
|
1286
|
+
let maxConfidence = 0;
|
|
1287
|
+
let count = 0;
|
|
1288
|
+
for (const headerRule of suspicious) {
|
|
1289
|
+
if (headers[headerRule.name.toLowerCase()]) {
|
|
1290
|
+
maxConfidence = Math.max(maxConfidence, headerRule.confidence * 100);
|
|
1291
|
+
count++;
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
return { confidence: maxConfidence, count };
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* Check if signature headers are present
|
|
1298
|
+
*/
|
|
1299
|
+
hasSignatureHeaders(headers) {
|
|
1300
|
+
return !!(headers["signature"] || headers["signature-input"] || headers["signature-agent"]);
|
|
1301
|
+
}
|
|
1302
|
+
/**
|
|
1303
|
+
* Get human-readable agent name from rule key
|
|
1304
|
+
*/
|
|
1305
|
+
getAgentName(agentKey) {
|
|
1306
|
+
const nameMap = {
|
|
1307
|
+
openai_gptbot: "ChatGPT/GPTBot",
|
|
1308
|
+
anthropic_claude: "Claude",
|
|
1309
|
+
perplexity_bot: "Perplexity",
|
|
1310
|
+
google_ai: "Google AI",
|
|
1311
|
+
microsoft_ai: "Microsoft Copilot",
|
|
1312
|
+
meta_ai: "Meta AI",
|
|
1313
|
+
cohere_bot: "Cohere",
|
|
1314
|
+
huggingface_bot: "HuggingFace",
|
|
1315
|
+
generic_bot: "Generic Bot",
|
|
1316
|
+
dev_tools: "Development Tool",
|
|
1317
|
+
automation_tools: "Automation Tool"
|
|
1318
|
+
};
|
|
1319
|
+
return nameMap[agentKey] || agentKey;
|
|
1320
|
+
}
|
|
1321
|
+
/**
|
|
1322
|
+
* Infer agent type from name
|
|
1323
|
+
*/
|
|
1324
|
+
inferAgentType(agentName) {
|
|
1325
|
+
const lowerName = agentName.toLowerCase();
|
|
1326
|
+
if (lowerName.includes("chatgpt") || lowerName.includes("gpt")) return "openai";
|
|
1327
|
+
if (lowerName.includes("claude")) return "anthropic";
|
|
1328
|
+
if (lowerName.includes("perplexity")) return "perplexity";
|
|
1329
|
+
if (lowerName.includes("google")) return "google";
|
|
1330
|
+
if (lowerName.includes("copilot") || lowerName.includes("bing")) return "microsoft";
|
|
1331
|
+
return "unknown";
|
|
1332
|
+
}
|
|
1333
|
+
/**
|
|
1334
|
+
* Determine detection class
|
|
1335
|
+
*/
|
|
1336
|
+
determineDetectionClass(isAgent, agentName, confidence) {
|
|
1337
|
+
if (!isAgent || confidence < CONFIDENCE.THRESHOLD_AGENT) {
|
|
1338
|
+
return { type: "Human" };
|
|
1339
|
+
}
|
|
1340
|
+
if (agentName) {
|
|
1341
|
+
const lowerName = agentName.toLowerCase();
|
|
1342
|
+
const aiPatterns = ["chatgpt", "gpt", "claude", "perplexity", "gemini", "copilot"];
|
|
1343
|
+
if (aiPatterns.some((p) => lowerName.includes(p))) {
|
|
1344
|
+
return { type: "AiAgent", agentType: agentName };
|
|
1345
|
+
}
|
|
1346
|
+
if (lowerName.includes("bot") || lowerName.includes("crawler")) {
|
|
1347
|
+
return { type: "Bot", botType: agentName };
|
|
1348
|
+
}
|
|
1349
|
+
if (lowerName.includes("automation") || lowerName.includes("tool")) {
|
|
1350
|
+
return { type: "Automation", toolType: agentName };
|
|
1351
|
+
}
|
|
1352
|
+
return { type: "AiAgent", agentType: agentName };
|
|
1353
|
+
}
|
|
1354
|
+
return { type: "Unknown" };
|
|
1355
|
+
}
|
|
1356
|
+
};
|
|
1357
|
+
function createRulesDetector() {
|
|
1358
|
+
return new RulesDetector();
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
// src/edge.ts
|
|
1362
|
+
function createEdgeDetector(wasmModule, options = {}) {
|
|
1363
|
+
const loader = new StaticWasmLoader(wasmModule);
|
|
1364
|
+
let policyLoader;
|
|
1365
|
+
if (options.apiKey) {
|
|
1366
|
+
policyLoader = new PolicyLoader({
|
|
1367
|
+
apiUrl: options.policyApiUrl,
|
|
1368
|
+
cacheTTL: options.policyTTL
|
|
1369
|
+
});
|
|
1370
|
+
}
|
|
1371
|
+
return new WasmDetector(loader, policyLoader, options);
|
|
1372
|
+
}
|
|
1373
|
+
function createFallbackDetector() {
|
|
1374
|
+
return new RulesDetector();
|
|
1375
|
+
}
|
|
1376
|
+
function extractInputFromRequest(request) {
|
|
1377
|
+
const headers = {};
|
|
1378
|
+
request.headers.forEach((value, key) => {
|
|
1379
|
+
headers[key] = value;
|
|
1380
|
+
});
|
|
1381
|
+
const url = new URL(request.url);
|
|
1382
|
+
return {
|
|
1383
|
+
userAgent: request.headers.get("user-agent") || void 0,
|
|
1384
|
+
ipAddress: request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || request.headers.get("x-real-ip") || void 0,
|
|
1385
|
+
headers,
|
|
1386
|
+
url: url.pathname + url.search,
|
|
1387
|
+
method: request.method
|
|
1388
|
+
};
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
exports.CONFIDENCE = CONFIDENCE;
|
|
1392
|
+
exports.PolicyLoader = PolicyLoader;
|
|
1393
|
+
exports.RulesDetector = RulesDetector;
|
|
1394
|
+
exports.StaticWasmLoader = StaticWasmLoader;
|
|
1395
|
+
exports.WasmDetector = WasmDetector;
|
|
1396
|
+
exports.createEdgeDetector = createEdgeDetector;
|
|
1397
|
+
exports.createFallbackDetector = createFallbackDetector;
|
|
1398
|
+
exports.createPolicyLoader = createPolicyLoader;
|
|
1399
|
+
exports.createRulesDetector = createRulesDetector;
|
|
1400
|
+
exports.createStaticLoader = createStaticLoader;
|
|
1401
|
+
exports.extractInputFromRequest = extractInputFromRequest;
|
|
1402
|
+
//# sourceMappingURL=edge.js.map
|
|
1403
|
+
//# sourceMappingURL=edge.js.map
|