@deflectbot/deflect-sdk 1.0.7 → 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 +2 -2
- package/README.md +39 -1
- package/dist/index.js +42 -42
- package/dist/types/index.d.ts +10 -10
- package/package.json +21 -18
- package/src/index.ts +121 -27
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
|
-
|
|
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();
|
package/dist/types/index.d.ts
CHANGED
|
@@ -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.
|
|
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,55 +1,149 @@
|
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
window.Deflect.
|
|
11
|
-
window.Deflect.sessionId = "";
|
|
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 || {};
|
|
12
15
|
}
|
|
13
16
|
|
|
14
|
-
configure(params: DeflectConfig): void {
|
|
17
|
+
configure(params: DeflectConfig, forceRefresh = false): void {
|
|
15
18
|
if (!params.siteKey) {
|
|
16
19
|
throw new Error("siteKey is required in configuration");
|
|
17
20
|
}
|
|
21
|
+
|
|
18
22
|
window.Deflect.siteKey = params.siteKey;
|
|
19
|
-
|
|
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");
|
|
20
29
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
30
|
+
this.scriptLoadPromise = null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!this.scriptLoadPromise) {
|
|
34
|
+
this.scriptLoadPromise = this.loadDeflectScript();
|
|
24
35
|
}
|
|
36
|
+
}
|
|
25
37
|
|
|
26
|
-
|
|
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("&");
|
|
27
42
|
|
|
28
|
-
const
|
|
29
|
-
if (!response.ok) {
|
|
30
|
-
throw new Error("Failed to fetch the Deflect script");
|
|
31
|
-
}
|
|
43
|
+
const scriptUrl = `https://js.deflect.bot/main.js?site=${encodeURIComponent(window.Deflect.siteKey)}${extraArgs ? `&${extraArgs}` : ""}`;
|
|
32
44
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
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;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
console.log("⏳ Fetching new Deflect script...");
|
|
57
|
+
|
|
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
|
+
`;
|
|
75
|
+
|
|
76
|
+
const blob = new Blob([workerCode], { type: "application/javascript" });
|
|
77
|
+
const worker = new Worker(URL.createObjectURL(blob));
|
|
37
78
|
|
|
38
|
-
|
|
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
|
+
};
|
|
39
96
|
|
|
97
|
+
worker.onerror = function (event: ErrorEvent) {
|
|
98
|
+
reject(new Error("Worker encountered an error: " + event.message));
|
|
99
|
+
worker.terminate();
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
worker.postMessage({ url: scriptUrl });
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private injectScript(scriptText: string, resolve: () => void, reject: (error: Error) => void) {
|
|
40
107
|
const scriptEl = document.createElement("script");
|
|
41
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
|
+
};
|
|
42
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
|
+
}
|
|
43
137
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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));
|
|
49
145
|
}
|
|
50
146
|
|
|
51
|
-
const token = await window.Deflect.getToken();
|
|
52
|
-
return token;
|
|
53
147
|
}
|
|
54
148
|
}
|
|
55
149
|
|