@deflectbot/deflect-sdk 1.1.8 → 1.1.9

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
@@ -1,50 +1,50 @@
1
- # Deflect -
2
-
3
- ![npm](https://img.shields.io/npm/v/@deflectbot/deflect-sdk)
4
- ![downloads](https://img.shields.io/npm/dm/@deflectbot/deflect-sdk)
5
-
6
- Deflect is an antibot solution that works with Vue, React, NextJS, and other JavaScript frameworks.
7
-
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)
20
-
21
- ```bash
22
- npm install deflect
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
- }
1
+ # Deflect -
2
+
3
+ ![npm](https://img.shields.io/npm/v/@deflectbot/deflect-sdk)
4
+ ![downloads](https://img.shields.io/npm/dm/@deflectbot/deflect-sdk)
5
+
6
+ Deflect is an antibot solution that works with Vue, React, NextJS, and other JavaScript frameworks.
7
+
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)
20
+
21
+ ```bash
22
+ npm install deflect
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
50
  ```
package/dist/index.js CHANGED
@@ -9,19 +9,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  class Deflect {
11
11
  constructor() {
12
+ this._prefetchedBlobUrl = null;
12
13
  window.Deflect = window.Deflect || {};
13
14
  window.Deflect.extraArgs = window.Deflect.extraArgs || {};
14
- // Setup readiness handshake
15
- if (!window.Deflect.ready) {
16
- let resolveFn;
17
- const promise = new Promise((resolve) => {
18
- resolveFn = resolve;
19
- });
20
- window.Deflect.ready = {
21
- promise,
22
- resolve: () => resolveFn(),
23
- };
24
- }
15
+ }
16
+ setupReady() {
17
+ let resolveFn;
18
+ const promise = new Promise((resolve) => {
19
+ resolveFn = resolve;
20
+ });
21
+ window.Deflect.ready = {
22
+ promise,
23
+ resolve: () => resolveFn(),
24
+ };
25
25
  }
26
26
  configure(params) {
27
27
  if (!params.siteKey) {
@@ -29,28 +29,50 @@ class Deflect {
29
29
  }
30
30
  window.Deflect.siteKey = params.siteKey;
31
31
  }
32
+ prefetchScript() {
33
+ if (!window.Deflect.siteKey || this._prefetchedBlobUrl)
34
+ return;
35
+ const nonce = Date.now().toString();
36
+ const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
37
+ fetch(scriptUrl, { cache: "no-store" })
38
+ .then((res) => __awaiter(this, void 0, void 0, function* () {
39
+ if (!res.ok)
40
+ return;
41
+ const text = yield res.text();
42
+ const blob = new Blob([text], { type: "text/javascript" });
43
+ this._prefetchedBlobUrl = URL.createObjectURL(blob);
44
+ }))
45
+ .catch(() => { });
46
+ }
32
47
  solveChallenge() {
33
48
  return __awaiter(this, void 0, void 0, function* () {
34
49
  var _a;
35
50
  if (!window.Deflect.siteKey) {
36
51
  throw new Error("API key (siteKey) is missing in configuration");
37
52
  }
38
- const nonce = Date.now().toString();
39
- const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
40
- const response = yield fetch(scriptUrl, {
41
- cache: "no-store",
42
- });
43
- if (!response.ok) {
44
- throw new Error("Failed to fetch the Deflect script");
53
+ this.setupReady();
54
+ let blobUrl;
55
+ let sessionId = null;
56
+ if (this._prefetchedBlobUrl) {
57
+ blobUrl = this._prefetchedBlobUrl;
58
+ }
59
+ else {
60
+ const nonce = Date.now().toString();
61
+ const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
62
+ const response = yield fetch(scriptUrl, { cache: "no-store" });
63
+ if (!response.ok) {
64
+ throw new Error("Failed to fetch the Deflect script");
65
+ }
66
+ sessionId = response.headers.get("session_id");
67
+ const text = yield response.text();
68
+ const blob = new Blob([text], { type: "text/javascript" });
69
+ blobUrl = URL.createObjectURL(blob);
45
70
  }
46
- const sessionId = response.headers.get("session_id");
47
71
  if (sessionId) {
48
72
  window.Deflect.sessionId = sessionId;
49
73
  }
50
74
  const scriptEl = document.createElement("script");
51
75
  scriptEl.type = "module";
52
- const blob = new Blob([yield response.text()], { type: "text/javascript" });
53
- const blobUrl = URL.createObjectURL(blob);
54
76
  scriptEl.src = blobUrl;
55
77
  document.head.appendChild(scriptEl);
56
78
  yield new Promise((resolve, reject) => {
@@ -65,9 +87,8 @@ class Deflect {
65
87
  const token = yield window.Deflect.getToken();
66
88
  URL.revokeObjectURL(blobUrl);
67
89
  scriptEl.remove();
68
- // Optional: wipe memory-heavy fields
90
+ this._prefetchedBlobUrl = null;
69
91
  delete window.Deflect.getToken;
70
- delete window.Deflect.ready;
71
92
  return token;
72
93
  });
73
94
  }
@@ -5,8 +5,11 @@ interface DeflectConfig {
5
5
  };
6
6
  }
7
7
  declare class Deflect {
8
+ private _prefetchedBlobUrl;
8
9
  constructor();
10
+ private setupReady;
9
11
  configure(params: DeflectConfig): void;
12
+ prefetchScript(): void;
10
13
  solveChallenge(): Promise<string>;
11
14
  }
12
15
  declare const _default: Deflect;
package/package.json CHANGED
@@ -1,25 +1,25 @@
1
- {
2
- "name": "@deflectbot/deflect-sdk",
3
- "version": "1.1.8",
4
- "description": "SDK for deflect.bot - Use it for seamless captcha integration on any website.",
5
- "main": "dist/index.js",
6
- "types": "dist/types/index.d.ts",
7
- "scripts": {
8
- "build": "tsc"
9
- },
10
- "author": "Deflect",
11
- "license": "MIT",
12
- "devDependencies": {
13
- "@eslint/js": "^9.22.0",
14
- "eslint": "^9.22.0",
15
- "globals": "^16.0.0",
16
- "typescript": "^5.7.3",
17
- "typescript-eslint": "^8.27.0"
18
- },
19
- "publishConfig": {
20
- "access": "public"
21
- },
22
- "dependencies": {
23
- "@deflectbot/deflect-sdk": "file:"
24
- }
25
- }
1
+ {
2
+ "name": "@deflectbot/deflect-sdk",
3
+ "version": "1.1.9",
4
+ "description": "SDK for deflect.bot - Use it for seamless captcha integration on any website.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/types/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc"
9
+ },
10
+ "author": "Deflect",
11
+ "license": "MIT",
12
+ "devDependencies": {
13
+ "@eslint/js": "^9.22.0",
14
+ "eslint": "^9.22.0",
15
+ "globals": "^16.0.0",
16
+ "typescript": "^5.7.3",
17
+ "typescript-eslint": "^8.27.0"
18
+ },
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "dependencies": {
23
+ "@deflectbot/deflect-sdk": "file:"
24
+ }
25
+ }
package/src/index.ts CHANGED
@@ -1,89 +1,111 @@
1
- interface DeflectConfig {
2
- siteKey: string;
3
- extraArgs?: { [key: string]: string };
4
- }
5
-
6
- class Deflect {
7
- constructor() {
8
- window.Deflect = window.Deflect || {};
9
- window.Deflect.extraArgs = window.Deflect.extraArgs || {};
10
-
11
- // Setup readiness handshake
12
- if (!window.Deflect.ready) {
13
- let resolveFn: () => void;
14
- const promise = new Promise<void>((resolve) => {
15
- resolveFn = resolve;
16
- });
17
- window.Deflect.ready = {
18
- promise,
19
- resolve: () => resolveFn(),
20
- };
21
- }
22
- }
23
-
24
- configure(params: DeflectConfig): void {
25
- if (!params.siteKey) {
26
- throw new Error("siteKey is required in configuration");
27
- }
28
- window.Deflect.siteKey = params.siteKey;
29
- }
30
-
31
- async solveChallenge(): Promise<string> {
32
- if (!window.Deflect.siteKey) {
33
- throw new Error("API key (siteKey) is missing in configuration");
34
- }
35
-
36
- const nonce = Date.now().toString();
37
- const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
38
- const response = await fetch(scriptUrl,
39
- {
40
- cache: "no-store",
41
- }
42
- );
43
-
44
- if (!response.ok) {
45
- throw new Error("Failed to fetch the Deflect script");
46
- }
47
-
48
- const sessionId = response.headers.get("session_id");
49
- if (sessionId) {
50
- window.Deflect.sessionId = sessionId;
51
- }
52
-
53
- const scriptEl = document.createElement("script");
54
- scriptEl.type = "module";
55
-
56
- const blob = new Blob([await response.text()], { type: "text/javascript" });
57
- const blobUrl = URL.createObjectURL(blob);
58
- scriptEl.src = blobUrl;
59
-
60
- document.head.appendChild(scriptEl);
61
-
62
- await new Promise<void>((resolve, reject) => {
63
- scriptEl.onload = () => resolve();
64
- scriptEl.onerror = () => reject("Failed to load the Deflect script");
65
- });
66
-
67
- await window.Deflect.ready?.promise;
68
-
69
- if (
70
- typeof window.Deflect === "undefined" ||
71
- typeof window.Deflect.getToken !== "function"
72
- ) {
73
- throw new Error("Deflect script did not load properly");
74
- }
75
-
76
- const token = await window.Deflect.getToken();
77
- URL.revokeObjectURL(blobUrl);
78
- scriptEl.remove();
79
-
80
- // Optional: wipe memory-heavy fields
81
- delete window.Deflect.getToken;
82
- delete window.Deflect.ready;
83
-
84
- return token;
85
- }
86
-
87
- }
88
-
89
- export default new Deflect();
1
+ interface DeflectConfig {
2
+ siteKey: string;
3
+ extraArgs?: { [key: string]: string };
4
+ }
5
+
6
+ class Deflect {
7
+ private _prefetchedBlobUrl: string | null = null;
8
+
9
+ constructor() {
10
+ window.Deflect = window.Deflect || {};
11
+ window.Deflect.extraArgs = window.Deflect.extraArgs || {};
12
+ }
13
+
14
+ private setupReady(): void {
15
+ let resolveFn: () => void;
16
+ const promise = new Promise<void>((resolve) => {
17
+ resolveFn = resolve;
18
+ });
19
+ window.Deflect.ready = {
20
+ promise,
21
+ resolve: () => resolveFn(),
22
+ };
23
+ }
24
+
25
+ configure(params: DeflectConfig): void {
26
+ if (!params.siteKey) {
27
+ throw new Error("siteKey is required in configuration");
28
+ }
29
+ window.Deflect.siteKey = params.siteKey;
30
+ }
31
+
32
+ prefetchScript(): void {
33
+ if (!window.Deflect.siteKey || this._prefetchedBlobUrl) return;
34
+
35
+ const nonce = Date.now().toString();
36
+ const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
37
+
38
+ fetch(scriptUrl, { cache: "no-store" })
39
+ .then(async (res) => {
40
+ if (!res.ok) return;
41
+ const text = await res.text();
42
+ const blob = new Blob([text], { type: "text/javascript" });
43
+ this._prefetchedBlobUrl = URL.createObjectURL(blob);
44
+ })
45
+ .catch(() => {});
46
+ }
47
+
48
+ async solveChallenge(): Promise<string> {
49
+ if (!window.Deflect.siteKey) {
50
+ throw new Error("API key (siteKey) is missing in configuration");
51
+ }
52
+
53
+ this.setupReady();
54
+
55
+ let blobUrl: string;
56
+ let sessionId: string | null = null;
57
+
58
+ if (this._prefetchedBlobUrl) {
59
+ blobUrl = this._prefetchedBlobUrl;
60
+ } else {
61
+ const nonce = Date.now().toString();
62
+ const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
63
+ const response = await fetch(scriptUrl, { cache: "no-store" });
64
+
65
+ if (!response.ok) {
66
+ throw new Error("Failed to fetch the Deflect script");
67
+ }
68
+
69
+ sessionId = response.headers.get("session_id");
70
+ const text = await response.text();
71
+ const blob = new Blob([text], { type: "text/javascript" });
72
+ blobUrl = URL.createObjectURL(blob);
73
+ }
74
+
75
+ if (sessionId) {
76
+ window.Deflect.sessionId = sessionId;
77
+ }
78
+
79
+ const scriptEl = document.createElement("script");
80
+ scriptEl.type = "module";
81
+ scriptEl.src = blobUrl;
82
+
83
+ document.head.appendChild(scriptEl);
84
+
85
+ await new Promise<void>((resolve, reject) => {
86
+ scriptEl.onload = () => resolve();
87
+ scriptEl.onerror = () => reject("Failed to load the Deflect script");
88
+ });
89
+
90
+ await window.Deflect.ready?.promise;
91
+
92
+ if (
93
+ typeof window.Deflect === "undefined" ||
94
+ typeof window.Deflect.getToken !== "function"
95
+ ) {
96
+ throw new Error("Deflect script did not load properly");
97
+ }
98
+
99
+ const token = await window.Deflect.getToken();
100
+
101
+ URL.revokeObjectURL(blobUrl);
102
+ scriptEl.remove();
103
+ this._prefetchedBlobUrl = null;
104
+
105
+ delete window.Deflect.getToken;
106
+
107
+ return token;
108
+ }
109
+ }
110
+
111
+ export default new Deflect();
@@ -1,3 +1,3 @@
1
- interface Window {
2
- Deflect: any;
3
- }
1
+ interface Window {
2
+ Deflect: any;
3
+ }
package/tsconfig.json CHANGED
@@ -1,22 +1,22 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2015", // Set target to ES2015 or later
4
- "module": "ESNext", // Use ES module syntax
5
- "moduleResolution": "node", // Resolve modules like Node.js
6
- "strict": true, // Enable strict type-checking
7
- "declaration": true, // Enable generation of .d.ts files
8
- "declarationDir": "./dist/types", // Directory for .d.ts files
9
- "esModuleInterop": true, // Allow importing non-ESM modules
10
- "skipLibCheck": true, // Skip type checking of declaration files
11
- "outDir": "./dist", // Output directory for JavaScript files
12
- "lib": ["ES2015", "DOM"], // Add ES2015 and DOM libraries
13
- "jsx": "preserve" // For JSX compatibility in Vue
14
- },
15
- "include": [
16
- "src/**/*"
17
- ],
18
- "exclude": [
19
- "node_modules",
20
- "dist"
21
- ]
22
- }
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2015", // Set target to ES2015 or later
4
+ "module": "ESNext", // Use ES module syntax
5
+ "moduleResolution": "node", // Resolve modules like Node.js
6
+ "strict": true, // Enable strict type-checking
7
+ "declaration": true, // Enable generation of .d.ts files
8
+ "declarationDir": "./dist/types", // Directory for .d.ts files
9
+ "esModuleInterop": true, // Allow importing non-ESM modules
10
+ "skipLibCheck": true, // Skip type checking of declaration files
11
+ "outDir": "./dist", // Output directory for JavaScript files
12
+ "lib": ["ES2015", "DOM"], // Add ES2015 and DOM libraries
13
+ "jsx": "preserve" // For JSX compatibility in Vue
14
+ },
15
+ "include": [
16
+ "src/**/*"
17
+ ],
18
+ "exclude": [
19
+ "node_modules",
20
+ "dist"
21
+ ]
22
+ }