@deflectbot/deflect-sdk 1.0.6 → 1.0.8

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/.gitattributes CHANGED
@@ -1,2 +1,2 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
package/README.md CHANGED
@@ -5,8 +5,46 @@
5
5
 
6
6
  Deflect is an antibot solution that works with Vue, React, NextJS, and other JavaScript frameworks.
7
7
 
8
- ## Installation
8
+ ---
9
+
10
+ ## CDN Install (Auto-Updates)
11
+
12
+ ```bash
13
+ Include in your project:
14
+ <script src="https://cdn.jsdelivr.net/npm/@deflectbot/deflect-sdk/dist/index.min.js"></script>
15
+ ```
16
+
17
+ ---
18
+
19
+ ## NPM Install (Requires Manual Updates)
9
20
 
10
21
  ```bash
11
22
  npm install deflect
12
23
  ```
24
+
25
+ ---
26
+
27
+ ## Setup
28
+
29
+ ```ts
30
+ import Deflect from "@deflectbot/deflect-sdk";
31
+
32
+ Deflect.configure({
33
+ siteKey: "YOUR_SITE_KEY",
34
+ extraArgs: { mode: "strict" }, // Optional
35
+ forceRefresh: true, // Optional
36
+ });
37
+ ```
38
+
39
+ ---
40
+
41
+ ## Solve Challenge
42
+
43
+ ```ts
44
+ try {
45
+ const token = await Deflect.solveChallenge();
46
+ console.log("✅ Token received:", token);
47
+ } catch (error) {
48
+ console.error("❌ Failed to solve challenge:", error);
49
+ }
50
+ ```
package/dist/index.js CHANGED
@@ -1,42 +1,42 @@
1
- class Deflect {
2
- constructor() {
3
- if (typeof window.Deflect === "undefined") {
4
- window.Deflect = {};
5
- }
6
- window.Deflect.siteKey = "";
7
- }
8
- configure(params) {
9
- if (!params.siteKey) {
10
- throw new Error("siteKey is required in configuration");
11
- }
12
- window.Deflect.siteKey = params.siteKey;
13
- }
14
- solveChallenge() {
15
- return new Promise((resolve, reject) => {
16
- if (!window.Deflect.siteKey) {
17
- return reject("API key (siteKey) is missing in configuration");
18
- }
19
- const scriptUrl = `https://js.deflect.bot/main.js?site=${window.Deflect.siteKey}`;
20
- const script = document.createElement("script");
21
- script.src = scriptUrl;
22
- script.onload = () => {
23
- if (typeof window.Deflect === "undefined" ||
24
- typeof window.Deflect.getToken !== "function") {
25
- return reject("Deflect script did not load properly");
26
- }
27
- window.Deflect.getToken()
28
- .then((token) => {
29
- resolve(token);
30
- })
31
- .catch((err) => {
32
- reject(`Failed to get token: ${err.message}`);
33
- });
34
- };
35
- script.onerror = () => {
36
- reject("Failed to load the Deflect script");
37
- };
38
- document.head.appendChild(script);
39
- });
40
- }
41
- }
42
- export default new Deflect();
1
+ class Deflect {
2
+ constructor() {
3
+ if (typeof window.Deflect === "undefined") {
4
+ window.Deflect = {};
5
+ }
6
+ window.Deflect.siteKey = "";
7
+ }
8
+ configure(params) {
9
+ if (!params.siteKey) {
10
+ throw new Error("siteKey is required in configuration");
11
+ }
12
+ window.Deflect.siteKey = params.siteKey;
13
+ }
14
+ solveChallenge() {
15
+ return new Promise((resolve, reject) => {
16
+ if (!window.Deflect.siteKey) {
17
+ return reject("API key (siteKey) is missing in configuration");
18
+ }
19
+ const scriptUrl = `https://js.deflect.bot/main.js?site=${window.Deflect.siteKey}`;
20
+ const script = document.createElement("script");
21
+ script.src = scriptUrl;
22
+ script.onload = () => {
23
+ if (typeof window.Deflect === "undefined" ||
24
+ typeof window.Deflect.getToken !== "function") {
25
+ return reject("Deflect script did not load properly");
26
+ }
27
+ window.Deflect.getToken()
28
+ .then((token) => {
29
+ resolve(token);
30
+ })
31
+ .catch((err) => {
32
+ reject(`Failed to get token: ${err.message}`);
33
+ });
34
+ };
35
+ script.onerror = () => {
36
+ reject("Failed to load the Deflect script");
37
+ };
38
+ document.head.appendChild(script);
39
+ });
40
+ }
41
+ }
42
+ export default new Deflect();
@@ -1,10 +1,10 @@
1
- interface DeflectConfig {
2
- siteKey: string;
3
- }
4
- declare class Deflect {
5
- constructor();
6
- configure(params: DeflectConfig): void;
7
- solveChallenge(): Promise<string>;
8
- }
9
- declare const _default: Deflect;
10
- export default _default;
1
+ interface DeflectConfig {
2
+ siteKey: string;
3
+ }
4
+ declare class Deflect {
5
+ constructor();
6
+ configure(params: DeflectConfig): void;
7
+ solveChallenge(): Promise<string>;
8
+ }
9
+ declare const _default: Deflect;
10
+ export default _default;
package/package.json CHANGED
@@ -1,18 +1,21 @@
1
- {
2
- "name": "@deflectbot/deflect-sdk",
3
- "version": "1.0.6",
4
- "description": "",
5
- "main": "dist/index.js",
6
- "types": "dist/types/index.d.ts",
7
- "scripts": {
8
- "build": "tsc"
9
- },
10
- "author": "Fredrik Rafn",
11
- "license": "MIT",
12
- "devDependencies": {
13
- "typescript": "^5.7.3"
14
- },
15
- "publishConfig": {
16
- "access": "public"
17
- }
18
- }
1
+ {
2
+ "name": "@deflectbot/deflect-sdk",
3
+ "version": "1.0.8",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/types/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc"
9
+ },
10
+ "author": "Fredrik Rafn",
11
+ "license": "MIT",
12
+ "devDependencies": {
13
+ "typescript": "^5.7.3"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "dependencies": {
19
+ "@deflectbot/deflect-sdk": "file:"
20
+ }
21
+ }
package/src/index.ts CHANGED
@@ -1,57 +1,150 @@
1
1
  interface DeflectConfig {
2
2
  siteKey: string;
3
+ extraArgs?: { [key: string]: string };
3
4
  }
4
5
 
5
6
  class Deflect {
7
+ private scriptLoadPromise: Promise<void> | null = null;
8
+ private cacheTTL: number = 10 * 60 * 1000;
9
+
6
10
  constructor() {
7
- if (typeof window.Deflect === "undefined") {
8
- window.Deflect = {};
9
- }
10
- window.Deflect.siteKey = "";
11
+ window.Deflect = window.Deflect || {};
12
+ window.Deflect.siteKey = window.Deflect.siteKey || "";
13
+ window.Deflect.sessionId = window.Deflect.sessionId || "";
14
+ window.Deflect.extraArgs = window.Deflect.extraArgs || {};
11
15
  }
12
16
 
13
- configure(params: DeflectConfig): void {
17
+ configure(params: DeflectConfig, forceRefresh = false): void {
14
18
  if (!params.siteKey) {
15
19
  throw new Error("siteKey is required in configuration");
16
20
  }
21
+
17
22
  window.Deflect.siteKey = params.siteKey;
23
+ window.Deflect.extraArgs = params.extraArgs || {};
24
+
25
+ if (forceRefresh) {
26
+ console.log("Main.js force refreshed");
27
+ sessionStorage.removeItem("deflect_script");
28
+ localStorage.removeItem("deflect_script_timestamp");
29
+
30
+ this.scriptLoadPromise = null;
31
+ }
32
+
33
+ if (!this.scriptLoadPromise) {
34
+ this.scriptLoadPromise = this.loadDeflectScript();
35
+ }
18
36
  }
19
37
 
20
- solveChallenge(): Promise<string> {
38
+ private loadDeflectScript(): Promise<void> {
39
+ const extraArgs = Object.keys(window.Deflect.extraArgs || {})
40
+ .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(window.Deflect.extraArgs[key])}`)
41
+ .join("&");
42
+
43
+ const scriptUrl = `https://js.deflect.bot/main.js?site=${encodeURIComponent(window.Deflect.siteKey)}${extraArgs ? `&${extraArgs}` : ""}`;
44
+
21
45
  return new Promise((resolve, reject) => {
22
- if (!window.Deflect.siteKey) {
23
- return reject("API key (siteKey) is missing in configuration");
46
+ const cachedScript = sessionStorage.getItem("deflect_script");
47
+ const lastFetchTime = localStorage.getItem("deflect_script_timestamp");
48
+
49
+ // Check if we have a valid cache (within 10 minutes)
50
+ if (cachedScript && lastFetchTime && Date.now() - parseInt(lastFetchTime) < this.cacheTTL) {
51
+ console.log("✅ Using cached Deflect script");
52
+ this.injectScript(cachedScript, resolve, reject);
53
+ return;
24
54
  }
25
55
 
26
- const scriptUrl = `https://js.deflect.bot/main.js?site=${window.Deflect.siteKey}`;
56
+ console.log("⏳ Fetching new Deflect script...");
27
57
 
28
- const script = document.createElement("script");
29
- script.src = scriptUrl;
58
+ const workerCode = `
59
+ self.onmessage = async function(event) {
60
+ try {
61
+ const response = await fetch(event.data.url, { cache: "no-store" });
62
+ if (!response.ok) {
63
+ const errorText = await response.text();
64
+ self.postMessage({ error: 'Failed to fetch script. Status: ' + response.status + ' - ' + errorText });
65
+ return;
66
+ }
67
+ const sessionId = response.headers.get("session_id") || "";
68
+ const scriptText = await response.text();
69
+ self.postMessage({ scriptText: scriptText, sessionId: sessionId });
70
+ } catch (err) {
71
+ self.postMessage({ error: 'Error in worker: ' + (err instanceof Error ? err.message : String(err)) });
72
+ }
73
+ };
74
+ `;
30
75
 
31
- script.onload = () => {
32
- if (
33
- typeof window.Deflect === "undefined" ||
34
- typeof window.Deflect.getToken !== "function"
35
- ) {
36
- return reject("Deflect script did not load properly");
37
- }
76
+ const blob = new Blob([workerCode], { type: "application/javascript" });
77
+ const worker = new Worker(URL.createObjectURL(blob));
38
78
 
39
- window.Deflect.getToken()
40
- .then((token: string) => {
41
- resolve(token);
42
- })
43
- .catch((err: Error) => {
44
- reject(`Failed to get token: ${err.message}`);
45
- });
46
- };
79
+ worker.onmessage = (event: MessageEvent<{ scriptText?: string; sessionId?: string; error?: string }>) => {
80
+ if (event.data.error) {
81
+ reject(new Error(event.data.error));
82
+ worker.terminate();
83
+ return;
84
+ }
85
+
86
+ var scriptText = event.data.scriptText || "";
87
+ window.Deflect.sessionId = event.data.sessionId || "";
88
+
89
+ // Cache script info
90
+ sessionStorage.setItem("deflect_script", scriptText);
91
+ localStorage.setItem("deflect_script_timestamp", Date.now().toString());
92
+
93
+ this.injectScript(scriptText, resolve, reject);
94
+ worker.terminate();
95
+ };
47
96
 
48
- script.onerror = () => {
49
- reject("Failed to load the Deflect script");
97
+ worker.onerror = function (event: ErrorEvent) {
98
+ reject(new Error("Worker encountered an error: " + event.message));
99
+ worker.terminate();
50
100
  };
51
101
 
52
- document.head.appendChild(script);
102
+ worker.postMessage({ url: scriptUrl });
53
103
  });
54
104
  }
105
+
106
+ private injectScript(scriptText: string, resolve: () => void, reject: (error: Error) => void) {
107
+ const scriptEl = document.createElement("script");
108
+ scriptEl.textContent = scriptText;
109
+ scriptEl.onload = () => {
110
+ console.log("✅ Deflect script loaded successfully.");
111
+ resolve();
112
+ };
113
+ scriptEl.onerror = () => {
114
+ reject(new Error("Failed to load Deflect script"));
115
+ };
116
+ document.head.appendChild(scriptEl);
117
+ }
118
+
119
+ async solveChallenge(): Promise<string> {
120
+ if (!window.Deflect.siteKey) {
121
+ throw new Error("API key (siteKey) is missing in configuration");
122
+ }
123
+
124
+ try {
125
+ await this.scriptLoadPromise;
126
+ } catch (error: unknown) {
127
+ if (error instanceof Error) {
128
+ throw new Error("Deflect script failed to load: " + error.message);
129
+ }
130
+ throw new Error("Deflect script failed to load: " + String(error));
131
+ }
132
+
133
+
134
+ if (typeof window.Deflect.getToken !== "function") {
135
+ throw new Error("Deflect script did not load properly or is not exposing getToken.");
136
+ }
137
+
138
+ try {
139
+ return await window.Deflect.getToken(window.Deflect.siteKey, window.Deflect.sessionId);
140
+ } catch (error: unknown) {
141
+ if (error instanceof Error) {
142
+ throw new Error("Error getting Deflect token: " + error.message);
143
+ }
144
+ throw new Error("Error getting Deflect token: " + String(error));
145
+ }
146
+
147
+ }
55
148
  }
56
149
 
57
150
  export default new Deflect();
package/src/index.js DELETED
@@ -1,46 +0,0 @@
1
- class Deflect {
2
- constructor() {
3
- window.Deflect = window.Deflect || {};
4
- }
5
-
6
- configure(params) {
7
- if (!params.siteKey) {
8
- throw new Error('siteKey is required in configuration');
9
- }
10
- window.Deflect.siteKey = params.siteKey;
11
- }
12
-
13
- solveChallenge() {
14
- return new Promise((resolve, reject) => {
15
- if (!window.Deflect.siteKey) {
16
- return reject("siteKey is missing in configuration");
17
- }
18
-
19
- const scriptUrl = `https://js.deflect.bot/main.js?site=${window.Deflect.siteKey}`;
20
-
21
- const script = document.createElement("script");
22
- script.src = scriptUrl;
23
- script.onload = () => {
24
- if (typeof window.Deflect === 'undefined' || typeof window.Deflect.getToken !== 'function') {
25
- return reject("Deflect script did not load properly");
26
- }
27
-
28
- window.Deflect.getToken()
29
- .then((token) => {
30
- resolve(token);
31
- })
32
- .catch((err) => {
33
- reject(`Failed to get token: ${err}`);
34
- });
35
- };
36
-
37
- script.onerror = () => {
38
- reject("Failed to load the Deflect script");
39
- };
40
-
41
- document.head.appendChild(script);
42
- });
43
- }
44
- }
45
-
46
- module.exports = new Deflect();