@webdecoy/node 0.12.0 → 0.14.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.
@@ -0,0 +1,113 @@
1
+ import {
2
+ WebDecoy,
3
+ silentLogger
4
+ } from "./chunk-4QKKW7GN.mjs";
5
+
6
+ // src/testing.ts
7
+ function request(over = {}) {
8
+ return {
9
+ method: "GET",
10
+ path: "/",
11
+ ip: "203.0.113.1",
12
+ user_agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36",
13
+ headers: {},
14
+ timestamp: Date.now(),
15
+ ...over
16
+ };
17
+ }
18
+ function get(path, over = {}) {
19
+ const q = path.indexOf("?");
20
+ return request({
21
+ method: "GET",
22
+ path: q === -1 ? path : path.slice(0, q),
23
+ query: q === -1 ? void 0 : path.slice(q + 1),
24
+ ...over
25
+ });
26
+ }
27
+ function post(path, body, over = {}) {
28
+ return { ...get(path, over), method: "POST", body };
29
+ }
30
+ function botRequest(userAgent, over = {}) {
31
+ return request({ user_agent: userAgent, ...over });
32
+ }
33
+ function createTestHarness(options = {}) {
34
+ const { allowNetwork = false, ...config } = options;
35
+ return new WebDecoy({
36
+ ...config,
37
+ apiKey: allowNetwork ? config.apiKey : void 0,
38
+ logger: config.logger ?? silentLogger
39
+ });
40
+ }
41
+ function describeDecision(decision) {
42
+ const rules = decision.results.map((r) => `${r.rule}=${r.conclusion}${r.state === "RUN" ? "" : `(${r.state})`}`).join(", ");
43
+ return [
44
+ `conclusion=${decision.conclusion}`,
45
+ decision.reason ? `reason="${decision.reason}"` : null,
46
+ rules ? `rules[${rules}]` : "rules[none]"
47
+ ].filter(Boolean).join(" ");
48
+ }
49
+ var AssertionError = class extends Error {
50
+ constructor(message) {
51
+ super(message);
52
+ this.name = "WebDecoyAssertionError";
53
+ }
54
+ };
55
+ function expectDenied(decision, expected = {}) {
56
+ if (decision.conclusion !== "DENY") {
57
+ throw new AssertionError(
58
+ `Expected the request to be denied, but ${describeDecision(decision)}`
59
+ );
60
+ }
61
+ if (expected.rule && !decision.deniedBy(expected.rule)) {
62
+ throw new AssertionError(
63
+ `Expected ${expected.rule} to deny, but ${describeDecision(decision)}`
64
+ );
65
+ }
66
+ if (expected.reason !== void 0) {
67
+ const reason = decision.reason ?? "";
68
+ const ok = typeof expected.reason === "string" ? reason.includes(expected.reason) : expected.reason.test(reason);
69
+ if (!ok) {
70
+ throw new AssertionError(
71
+ `Expected the reason to match ${String(expected.reason)}, got "${reason}"`
72
+ );
73
+ }
74
+ }
75
+ }
76
+ function expectAllowed(decision) {
77
+ if (decision.conclusion !== "ALLOW") {
78
+ throw new AssertionError(
79
+ `Expected the request to be allowed, but ${describeDecision(decision)}`
80
+ );
81
+ }
82
+ }
83
+ function expectRuleState(decision, rule, state) {
84
+ const outcome = decision.results.find((r) => r.rule === rule);
85
+ if (!outcome) {
86
+ throw new AssertionError(
87
+ `No rule named ${rule} ran. ${describeDecision(decision)}`
88
+ );
89
+ }
90
+ if (outcome.state !== state) {
91
+ throw new AssertionError(
92
+ `Expected ${rule} to be ${state}, got ${outcome.state}. ${describeDecision(decision)}`
93
+ );
94
+ }
95
+ }
96
+ async function protectMany(sdk, metadata, count) {
97
+ const out = [];
98
+ for (let i = 0; i < count; i++) {
99
+ out.push(await sdk.protect(typeof metadata === "function" ? metadata() : { ...metadata }));
100
+ }
101
+ return out;
102
+ }
103
+ export {
104
+ botRequest,
105
+ createTestHarness,
106
+ expectAllowed,
107
+ expectDenied,
108
+ expectRuleState,
109
+ get,
110
+ post,
111
+ protectMany,
112
+ request
113
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/node",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "Web Decoy SDK for Node.js - Bot detection with TLS fingerprinting",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -9,14 +9,19 @@
9
9
  "require": "./dist/index.js",
10
10
  "import": "./dist/index.mjs",
11
11
  "types": "./dist/index.d.ts"
12
+ },
13
+ "./testing": {
14
+ "require": "./dist/testing.js",
15
+ "import": "./dist/testing.mjs",
16
+ "types": "./dist/testing.d.ts"
12
17
  }
13
18
  },
14
19
  "files": [
15
20
  "dist"
16
21
  ],
17
22
  "scripts": {
18
- "build": "tsup src/index.ts --format cjs,esm --dts --clean",
19
- "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
23
+ "build": "tsup src/index.ts src/testing.ts --format cjs,esm --dts --clean",
24
+ "dev": "tsup src/index.ts src/testing.ts --format cjs,esm --dts --watch",
20
25
  "test": "jest",
21
26
  "lint": "eslint src --max-warnings 9",
22
27
  "clean": "rm -rf dist",