@deflectbot/deflect-sdk 1.3.3 → 1.3.5

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.
Files changed (3) hide show
  1. package/README.md +2 -2
  2. package/dist/index.js +194 -194
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -14,13 +14,13 @@ See the [documentation](https://docs.deflect.bot/) for how to use this SDK with
14
14
  #### yarn
15
15
 
16
16
  ```bash
17
- yarn add @castleio/sdk
17
+ yarn add @deflectbot/deflect-sdk
18
18
  ```
19
19
 
20
20
  #### npm
21
21
 
22
22
  ```bash
23
- npm install --save @castleio/sdk
23
+ npm install --save @deflectbot/deflect-sdk
24
24
  ```
25
25
 
26
26
  #### CDN
package/dist/index.js CHANGED
@@ -1,194 +1,194 @@
1
- "use strict";
2
- (() => {
3
- var __defProp = Object.defineProperty;
4
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
-
6
- // src/index.ts
7
- var Deflect = class {
8
- constructor() {
9
- this.config = null;
10
- this.scriptCache = null;
11
- this.isWarmupInProgress = false;
12
- this.initializeGlobalState();
13
- this.setupAutomaticWarmup();
14
- }
15
- static {
16
- __name(this, "Deflect");
17
- }
18
- initializeGlobalState() {
19
- if (typeof window === "undefined") return;
20
- window.Deflect = window.Deflect || {};
21
- }
22
- setupAutomaticWarmup() {
23
- if (typeof window === "undefined") return;
24
- if (document.readyState === "loading") {
25
- document.addEventListener("DOMContentLoaded", () => this.tryWarmup());
26
- } else {
27
- setTimeout(() => this.tryWarmup(), 100);
28
- }
29
- }
30
- async tryWarmup() {
31
- if (!this.config?.actionId || this.isWarmupInProgress || this.scriptCache) {
32
- return;
33
- }
34
- this.isWarmupInProgress = true;
35
- try {
36
- this.scriptCache = await this.fetchScript();
37
- } catch {
38
- } finally {
39
- this.isWarmupInProgress = false;
40
- }
41
- }
42
- buildScriptUrl(actionId) {
43
- const baseUrl = this.config?.scriptUrl || "https://js.deflect.bot/main.js";
44
- const nonce = Date.now().toString();
45
- return `${baseUrl}?action_id=${actionId}&_=${nonce}`;
46
- }
47
- async fetchScript() {
48
- if (!this.config?.actionId) {
49
- throw new Error("actionId is required");
50
- }
51
- const url = this.buildScriptUrl(this.config.actionId);
52
- const response = await fetch(url, { cache: "no-store" });
53
- if (!response.ok) {
54
- throw new Error(`Failed to fetch script: ${response.status}`);
55
- }
56
- return {
57
- content: await response.text(),
58
- sessionId: response.headers.get("session_id") || void 0
59
- };
60
- }
61
- async executeScript(script) {
62
- if (script.sessionId && typeof window !== "undefined") {
63
- window.Deflect.sessionId = script.sessionId;
64
- }
65
- const readyPromise = this.createReadyPromise();
66
- const blobUrl = this.createScriptBlob(script.content);
67
- const scriptElement = await this.loadScriptElement(blobUrl);
68
- try {
69
- await readyPromise;
70
- const token = await this.getTokenFromScript();
71
- this.prefetchNextScript();
72
- return token;
73
- } finally {
74
- this.cleanup(blobUrl, scriptElement);
75
- }
76
- }
77
- prefetchNextScript() {
78
- this.tryWarmup().catch(() => {
79
- });
80
- }
81
- createReadyPromise() {
82
- return new Promise((resolve) => {
83
- if (typeof window !== "undefined") {
84
- window.Deflect.ready = {
85
- promise: new Promise((innerResolve) => {
86
- window.Deflect.ready.resolve = innerResolve;
87
- }),
88
- resolve: /* @__PURE__ */ __name(() => resolve(), "resolve")
89
- };
90
- }
91
- });
92
- }
93
- createScriptBlob(content) {
94
- const blob = new Blob([content], { type: "text/javascript" });
95
- return URL.createObjectURL(blob);
96
- }
97
- async loadScriptElement(blobUrl) {
98
- return new Promise((resolve, reject) => {
99
- const script = document.createElement("script");
100
- script.type = "module";
101
- script.src = blobUrl;
102
- script.onload = () => resolve(script);
103
- script.onerror = () => reject(new Error("Script failed to load"));
104
- document.head.appendChild(script);
105
- });
106
- }
107
- async getTokenFromScript() {
108
- if (typeof window === "undefined" || typeof window.Deflect?.getToken !== "function") {
109
- throw new Error("Script did not load properly - getToken not available");
110
- }
111
- try {
112
- return await window.Deflect.getToken();
113
- } catch (error) {
114
- throw new Error(`Script execution failed: ${error}`);
115
- }
116
- }
117
- cleanup(blobUrl, scriptElement) {
118
- URL.revokeObjectURL(blobUrl);
119
- scriptElement.remove();
120
- if (typeof window !== "undefined" && window.Deflect?.getToken) {
121
- delete window.Deflect.getToken;
122
- }
123
- }
124
- configure(params) {
125
- if (!params.actionId?.trim()) {
126
- throw new Error("actionId is required and cannot be empty");
127
- }
128
- this.config = { ...params };
129
- if (typeof window !== "undefined") {
130
- window.Deflect.actionId = params.actionId;
131
- }
132
- this.tryWarmup();
133
- }
134
- // Deprecated name kept for backward compatibility
135
- async solveChallenge() {
136
- return this.getToken();
137
- }
138
- async getToken() {
139
- if (!this.config?.actionId) {
140
- throw new Error("Must call configure() before solveChallenge()");
141
- }
142
- let script;
143
- if (this.scriptCache && !this.isWarmupInProgress) {
144
- script = this.scriptCache;
145
- this.scriptCache = null;
146
- } else {
147
- script = await this.fetchScript();
148
- }
149
- return this.executeScript(script);
150
- }
151
- async warmup() {
152
- if (!this.config?.actionId) {
153
- return false;
154
- }
155
- try {
156
- await this.tryWarmup();
157
- return this.scriptCache !== null;
158
- } catch {
159
- return false;
160
- }
161
- }
162
- clearCache() {
163
- this.scriptCache = null;
164
- }
165
- /**
166
- * Inject a fresh token as a hidden input into a form. Only accepts a submit event from onsubmit.
167
- * Usage: <form ... onsubmit="return Deflect.injectToken(event)">
168
- * Returns false to prevent double submit.
169
- */
170
- async injectToken(event) {
171
- if (!event || !event.target || !(event.target instanceof HTMLFormElement)) {
172
- throw new Error("injectToken: must be called from a form submit event");
173
- }
174
- event.preventDefault();
175
- const form = event.target;
176
- const token = await this.getToken();
177
- Array.from(form.querySelectorAll('input[name="deflect_token"]')).forEach(
178
- (el) => el.remove()
179
- );
180
- const hidden = document.createElement("input");
181
- hidden.type = "hidden";
182
- hidden.name = "deflect_token";
183
- hidden.value = token;
184
- form.appendChild(hidden);
185
- form.submit();
186
- return false;
187
- }
188
- };
189
- var DeflectInstance = new Deflect();
190
- if (typeof window !== "undefined") {
191
- window.Deflect = DeflectInstance;
192
- }
193
- var src_default = DeflectInstance;
194
- })();
1
+ "use strict";
2
+ (() => {
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+
6
+ // src/index.ts
7
+ var Deflect = class {
8
+ constructor() {
9
+ this.config = null;
10
+ this.scriptCache = null;
11
+ this.isWarmupInProgress = false;
12
+ this.initializeGlobalState();
13
+ this.setupAutomaticWarmup();
14
+ }
15
+ static {
16
+ __name(this, "Deflect");
17
+ }
18
+ initializeGlobalState() {
19
+ if (typeof window === "undefined") return;
20
+ window.Deflect = window.Deflect || {};
21
+ }
22
+ setupAutomaticWarmup() {
23
+ if (typeof window === "undefined") return;
24
+ if (document.readyState === "loading") {
25
+ document.addEventListener("DOMContentLoaded", () => this.tryWarmup());
26
+ } else {
27
+ setTimeout(() => this.tryWarmup(), 100);
28
+ }
29
+ }
30
+ async tryWarmup() {
31
+ if (!this.config?.actionId || this.isWarmupInProgress || this.scriptCache) {
32
+ return;
33
+ }
34
+ this.isWarmupInProgress = true;
35
+ try {
36
+ this.scriptCache = await this.fetchScript();
37
+ } catch {
38
+ } finally {
39
+ this.isWarmupInProgress = false;
40
+ }
41
+ }
42
+ buildScriptUrl(actionId) {
43
+ const baseUrl = this.config?.scriptUrl || "https://js.deflect.bot/main.js";
44
+ const nonce = Date.now().toString();
45
+ return `${baseUrl}?action_id=${actionId}&_=${nonce}`;
46
+ }
47
+ async fetchScript() {
48
+ if (!this.config?.actionId) {
49
+ throw new Error("actionId is required");
50
+ }
51
+ const url = this.buildScriptUrl(this.config.actionId);
52
+ const response = await fetch(url, { cache: "no-store" });
53
+ if (!response.ok) {
54
+ throw new Error(`Failed to fetch script: ${response.status}`);
55
+ }
56
+ return {
57
+ content: await response.text(),
58
+ sessionId: response.headers.get("session_id") || void 0
59
+ };
60
+ }
61
+ async executeScript(script) {
62
+ if (script.sessionId && typeof window !== "undefined") {
63
+ window.Deflect.sessionId = script.sessionId;
64
+ }
65
+ const readyPromise = this.createReadyPromise();
66
+ const blobUrl = this.createScriptBlob(script.content);
67
+ const scriptElement = await this.loadScriptElement(blobUrl);
68
+ try {
69
+ await readyPromise;
70
+ const token = await this.getTokenFromScript();
71
+ this.prefetchNextScript();
72
+ return token;
73
+ } finally {
74
+ this.cleanup(blobUrl, scriptElement);
75
+ }
76
+ }
77
+ prefetchNextScript() {
78
+ this.tryWarmup().catch(() => {
79
+ });
80
+ }
81
+ createReadyPromise() {
82
+ return new Promise((resolve) => {
83
+ if (typeof window !== "undefined") {
84
+ window.Deflect.ready = {
85
+ promise: new Promise((innerResolve) => {
86
+ window.Deflect.ready.resolve = innerResolve;
87
+ }),
88
+ resolve: /* @__PURE__ */ __name(() => resolve(), "resolve")
89
+ };
90
+ }
91
+ });
92
+ }
93
+ createScriptBlob(content) {
94
+ const blob = new Blob([content], { type: "text/javascript" });
95
+ return URL.createObjectURL(blob);
96
+ }
97
+ async loadScriptElement(blobUrl) {
98
+ return new Promise((resolve, reject) => {
99
+ const script = document.createElement("script");
100
+ script.type = "module";
101
+ script.src = blobUrl;
102
+ script.onload = () => resolve(script);
103
+ script.onerror = () => reject(new Error("Script failed to load"));
104
+ document.head.appendChild(script);
105
+ });
106
+ }
107
+ async getTokenFromScript() {
108
+ if (typeof window === "undefined" || typeof window.Deflect?.getToken !== "function") {
109
+ throw new Error("Script did not load properly - getToken not available");
110
+ }
111
+ try {
112
+ return await window.Deflect.getToken();
113
+ } catch (error) {
114
+ throw new Error(`Script execution failed: ${error}`);
115
+ }
116
+ }
117
+ cleanup(blobUrl, scriptElement) {
118
+ URL.revokeObjectURL(blobUrl);
119
+ scriptElement.remove();
120
+ if (typeof window !== "undefined" && window.Deflect?.getToken) {
121
+ delete window.Deflect.getToken;
122
+ }
123
+ }
124
+ configure(params) {
125
+ if (!params.actionId?.trim()) {
126
+ throw new Error("actionId is required and cannot be empty");
127
+ }
128
+ this.config = { ...params };
129
+ if (typeof window !== "undefined") {
130
+ window.Deflect.actionId = params.actionId;
131
+ }
132
+ this.tryWarmup();
133
+ }
134
+ // Deprecated name kept for backward compatibility
135
+ async solveChallenge() {
136
+ return this.getToken();
137
+ }
138
+ async getToken() {
139
+ if (!this.config?.actionId) {
140
+ throw new Error("Must call configure() before solveChallenge()");
141
+ }
142
+ let script;
143
+ if (this.scriptCache && !this.isWarmupInProgress) {
144
+ script = this.scriptCache;
145
+ this.scriptCache = null;
146
+ } else {
147
+ script = await this.fetchScript();
148
+ }
149
+ return this.executeScript(script);
150
+ }
151
+ async warmup() {
152
+ if (!this.config?.actionId) {
153
+ return false;
154
+ }
155
+ try {
156
+ await this.tryWarmup();
157
+ return this.scriptCache !== null;
158
+ } catch {
159
+ return false;
160
+ }
161
+ }
162
+ clearCache() {
163
+ this.scriptCache = null;
164
+ }
165
+ /**
166
+ * Inject a fresh token as a hidden input into a form. Only accepts a submit event from onsubmit.
167
+ * Usage: <form ... onsubmit="return Deflect.injectToken(event)">
168
+ * Returns false to prevent double submit.
169
+ */
170
+ async injectToken(event) {
171
+ if (!event || !event.target || !(event.target instanceof HTMLFormElement)) {
172
+ throw new Error("injectToken: must be called from a form submit event");
173
+ }
174
+ event.preventDefault();
175
+ const form = event.target;
176
+ const token = await this.getToken();
177
+ Array.from(form.querySelectorAll('input[name="deflect_token"]')).forEach(
178
+ (el) => el.remove()
179
+ );
180
+ const hidden = document.createElement("input");
181
+ hidden.type = "hidden";
182
+ hidden.name = "deflect_token";
183
+ hidden.value = token;
184
+ form.appendChild(hidden);
185
+ form.submit();
186
+ return false;
187
+ }
188
+ };
189
+ var DeflectInstance = new Deflect();
190
+ if (typeof window !== "undefined") {
191
+ window.Deflect = DeflectInstance;
192
+ }
193
+ var src_default = DeflectInstance;
194
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deflectbot/deflect-sdk",
3
- "version": "1.3.3",
3
+ "version": "1.3.5",
4
4
  "description": "SDK for deflect.bot - Use it for seamless captcha integration on any website.",
5
5
  "main": "dist/index.min.js",
6
6
  "unpkg": "dist/index.min.js",