@deflectbot/deflect-sdk 1.2.1 → 1.2.3
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/dist/index.js +89 -159
- package/dist/types/index.d.ts +10 -17
- package/package.json +1 -1
- package/src/index.ts +91 -161
package/dist/index.js
CHANGED
|
@@ -9,188 +9,118 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
class Deflect {
|
|
11
11
|
constructor() {
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
this.initializeGlobalState();
|
|
16
|
-
this.setupAutomaticWarmup();
|
|
17
|
-
}
|
|
18
|
-
initializeGlobalState() {
|
|
19
|
-
if (typeof window === "undefined")
|
|
20
|
-
return;
|
|
12
|
+
this._prefetchedScriptText = null;
|
|
13
|
+
this._hasUsedPrefetch = false;
|
|
14
|
+
this._customScriptUrl = null;
|
|
21
15
|
window.Deflect = window.Deflect || {};
|
|
22
16
|
window.Deflect.extraArgs = window.Deflect.extraArgs || {};
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
setTimeout(() => this.tryWarmup(), 100);
|
|
17
|
+
if (typeof window !== "undefined") {
|
|
18
|
+
window.addEventListener("load", () => {
|
|
19
|
+
setTimeout(() => {
|
|
20
|
+
this._warmupScript();
|
|
21
|
+
}, 500);
|
|
22
|
+
});
|
|
32
23
|
}
|
|
33
24
|
}
|
|
34
|
-
|
|
35
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
-
var _a;
|
|
37
|
-
if (!((_a = this.config) === null || _a === void 0 ? void 0 : _a.actionId) || this.isWarmupInProgress || this.scriptCache) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
this.isWarmupInProgress = true;
|
|
41
|
-
try {
|
|
42
|
-
this.scriptCache = yield this.fetchScript();
|
|
43
|
-
}
|
|
44
|
-
catch (_b) {
|
|
45
|
-
}
|
|
46
|
-
finally {
|
|
47
|
-
this.isWarmupInProgress = false;
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
buildScriptUrl(actionId) {
|
|
52
|
-
var _a, _b;
|
|
53
|
-
const baseUrl = ((_a = this.config) === null || _a === void 0 ? void 0 : _a.scriptUrl) || "https://js.deflect.bot/main.js";
|
|
54
|
-
if ((_b = this.config) === null || _b === void 0 ? void 0 : _b.scriptUrl) {
|
|
55
|
-
return baseUrl;
|
|
56
|
-
}
|
|
25
|
+
_getScriptUrl() {
|
|
57
26
|
const nonce = Date.now().toString();
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (!((_a = this.config) === null || _a === void 0 ? void 0 : _a.actionId)) {
|
|
64
|
-
throw new Error("actionId is required");
|
|
65
|
-
}
|
|
66
|
-
const url = this.buildScriptUrl(this.config.actionId);
|
|
67
|
-
const response = yield fetch(url, { cache: "no-store" });
|
|
68
|
-
if (!response.ok) {
|
|
69
|
-
throw new Error(`Failed to fetch script: ${response.status}`);
|
|
70
|
-
}
|
|
71
|
-
return {
|
|
72
|
-
content: yield response.text(),
|
|
73
|
-
sessionId: response.headers.get("session_id") || undefined,
|
|
74
|
-
};
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
executeScript(script) {
|
|
78
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
-
if (script.sessionId && typeof window !== "undefined") {
|
|
80
|
-
window.Deflect.sessionId = script.sessionId;
|
|
81
|
-
}
|
|
82
|
-
const readyPromise = this.createReadyPromise();
|
|
83
|
-
const blobUrl = this.createScriptBlob(script.content);
|
|
84
|
-
const scriptElement = yield this.loadScriptElement(blobUrl);
|
|
85
|
-
try {
|
|
86
|
-
yield readyPromise;
|
|
87
|
-
const token = yield this.getTokenFromScript();
|
|
88
|
-
return token;
|
|
89
|
-
}
|
|
90
|
-
finally {
|
|
91
|
-
this.cleanup(blobUrl, scriptElement);
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
createReadyPromise() {
|
|
96
|
-
return new Promise((resolve) => {
|
|
97
|
-
if (typeof window !== "undefined") {
|
|
98
|
-
window.Deflect.ready = {
|
|
99
|
-
promise: new Promise((innerResolve) => {
|
|
100
|
-
window.Deflect.ready.resolve = innerResolve;
|
|
101
|
-
}),
|
|
102
|
-
resolve: () => resolve(),
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
createScriptBlob(content) {
|
|
108
|
-
const blob = new Blob([content], { type: "text/javascript" });
|
|
109
|
-
return URL.createObjectURL(blob);
|
|
110
|
-
}
|
|
111
|
-
loadScriptElement(blobUrl) {
|
|
112
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
113
|
-
return new Promise((resolve, reject) => {
|
|
114
|
-
const script = document.createElement("script");
|
|
115
|
-
script.type = "module";
|
|
116
|
-
script.src = blobUrl;
|
|
117
|
-
script.onload = () => resolve(script);
|
|
118
|
-
script.onerror = () => reject(new Error("Script failed to load"));
|
|
119
|
-
document.head.appendChild(script);
|
|
120
|
-
});
|
|
121
|
-
});
|
|
27
|
+
if (this._customScriptUrl) {
|
|
28
|
+
const separator = this._customScriptUrl.includes("?") ? "&" : "?";
|
|
29
|
+
return `${this._customScriptUrl}${separator}action_id=${window.Deflect.actionId}&_=${nonce}`;
|
|
30
|
+
}
|
|
31
|
+
return `https://js.deflect.bot/main.js?action_id=${window.Deflect.actionId}&_=${nonce}`;
|
|
122
32
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
catch
|
|
134
|
-
|
|
135
|
-
|
|
33
|
+
_warmupScript() {
|
|
34
|
+
if (!window.Deflect.actionId)
|
|
35
|
+
return;
|
|
36
|
+
const scriptUrl = this._getScriptUrl();
|
|
37
|
+
fetch(scriptUrl, { cache: "no-store" })
|
|
38
|
+
.then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
if (!res.ok)
|
|
40
|
+
return;
|
|
41
|
+
this._prefetchedScriptText = yield res.text();
|
|
42
|
+
}))
|
|
43
|
+
.catch(() => { });
|
|
44
|
+
}
|
|
45
|
+
_refetchScript() {
|
|
46
|
+
this._hasUsedPrefetch = false;
|
|
47
|
+
this._prefetchedScriptText = null;
|
|
48
|
+
setTimeout(() => {
|
|
49
|
+
this._warmupScript();
|
|
50
|
+
}, 100);
|
|
51
|
+
}
|
|
52
|
+
setupReady() {
|
|
53
|
+
let resolveFn;
|
|
54
|
+
const promise = new Promise((resolve) => {
|
|
55
|
+
resolveFn = resolve;
|
|
136
56
|
});
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
scriptElement.remove();
|
|
142
|
-
if (typeof window !== "undefined" && ((_a = window.Deflect) === null || _a === void 0 ? void 0 : _a.getToken)) {
|
|
143
|
-
delete window.Deflect.getToken;
|
|
144
|
-
}
|
|
57
|
+
window.Deflect.ready = {
|
|
58
|
+
promise,
|
|
59
|
+
resolve: () => resolveFn(),
|
|
60
|
+
};
|
|
145
61
|
}
|
|
146
62
|
configure(params) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
throw new Error("actionId is required and cannot be empty");
|
|
63
|
+
if (!params.actionId) {
|
|
64
|
+
throw new Error("actionId is required in configuration");
|
|
150
65
|
}
|
|
151
|
-
|
|
152
|
-
if (
|
|
153
|
-
|
|
154
|
-
if (params.extraArgs) {
|
|
155
|
-
window.Deflect.extraArgs = Object.assign({}, params.extraArgs);
|
|
156
|
-
}
|
|
66
|
+
window.Deflect.actionId = params.actionId;
|
|
67
|
+
if (params.scriptUrl) {
|
|
68
|
+
this._customScriptUrl = params.scriptUrl;
|
|
157
69
|
}
|
|
158
|
-
this.tryWarmup();
|
|
159
70
|
}
|
|
160
71
|
solveChallenge() {
|
|
161
72
|
return __awaiter(this, void 0, void 0, function* () {
|
|
162
73
|
var _a;
|
|
163
|
-
if (!
|
|
164
|
-
throw new Error("
|
|
74
|
+
if (!window.Deflect.actionId) {
|
|
75
|
+
throw new Error("actionId is missing in configuration");
|
|
165
76
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
77
|
+
this.setupReady();
|
|
78
|
+
let scriptText;
|
|
79
|
+
let sessionId = null;
|
|
80
|
+
if (this._prefetchedScriptText && !this._hasUsedPrefetch) {
|
|
81
|
+
scriptText = this._prefetchedScriptText;
|
|
82
|
+
this._hasUsedPrefetch = true;
|
|
170
83
|
}
|
|
171
84
|
else {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
if (
|
|
181
|
-
|
|
85
|
+
const scriptUrl = this._getScriptUrl();
|
|
86
|
+
const response = yield fetch(scriptUrl, { cache: "no-store" });
|
|
87
|
+
if (!response.ok) {
|
|
88
|
+
throw new Error("Failed to fetch the Deflect script");
|
|
89
|
+
}
|
|
90
|
+
sessionId = response.headers.get("session_id");
|
|
91
|
+
scriptText = yield response.text();
|
|
92
|
+
}
|
|
93
|
+
if (sessionId) {
|
|
94
|
+
window.Deflect.sessionId = sessionId;
|
|
95
|
+
}
|
|
96
|
+
const blob = new Blob([scriptText], { type: "text/javascript" });
|
|
97
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
98
|
+
const scriptEl = document.createElement("script");
|
|
99
|
+
scriptEl.type = "module";
|
|
100
|
+
scriptEl.src = blobUrl;
|
|
101
|
+
document.head.appendChild(scriptEl);
|
|
102
|
+
yield new Promise((resolve, reject) => {
|
|
103
|
+
scriptEl.onload = () => resolve();
|
|
104
|
+
scriptEl.onerror = () => reject("Failed to load the Deflect script");
|
|
105
|
+
});
|
|
106
|
+
yield ((_a = window.Deflect.ready) === null || _a === void 0 ? void 0 : _a.promise);
|
|
107
|
+
if (typeof window.Deflect === "undefined" ||
|
|
108
|
+
typeof window.Deflect.getToken !== "function") {
|
|
109
|
+
throw new Error("Deflect script did not load properly");
|
|
182
110
|
}
|
|
111
|
+
let token;
|
|
183
112
|
try {
|
|
184
|
-
yield
|
|
185
|
-
return this.scriptCache !== null;
|
|
113
|
+
token = yield window.Deflect.getToken();
|
|
186
114
|
}
|
|
187
|
-
catch (
|
|
188
|
-
|
|
115
|
+
catch (error) {
|
|
116
|
+
throw new Error(`Deflect script execution failed: ${error}`);
|
|
189
117
|
}
|
|
118
|
+
this._refetchScript();
|
|
119
|
+
URL.revokeObjectURL(blobUrl);
|
|
120
|
+
scriptEl.remove();
|
|
121
|
+
delete window.Deflect.getToken;
|
|
122
|
+
return token;
|
|
190
123
|
});
|
|
191
124
|
}
|
|
192
|
-
clearCache() {
|
|
193
|
-
this.scriptCache = null;
|
|
194
|
-
}
|
|
195
125
|
}
|
|
196
126
|
export default new Deflect();
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,28 +1,21 @@
|
|
|
1
1
|
interface DeflectConfig {
|
|
2
2
|
actionId: string;
|
|
3
3
|
scriptUrl?: string;
|
|
4
|
-
extraArgs?:
|
|
4
|
+
extraArgs?: {
|
|
5
|
+
[key: string]: string;
|
|
6
|
+
};
|
|
5
7
|
}
|
|
6
8
|
declare class Deflect {
|
|
7
|
-
private
|
|
8
|
-
private
|
|
9
|
-
private
|
|
9
|
+
private _prefetchedScriptText;
|
|
10
|
+
private _hasUsedPrefetch;
|
|
11
|
+
private _customScriptUrl;
|
|
10
12
|
constructor();
|
|
11
|
-
private
|
|
12
|
-
private
|
|
13
|
-
private
|
|
14
|
-
private
|
|
15
|
-
private fetchScript;
|
|
16
|
-
private executeScript;
|
|
17
|
-
private createReadyPromise;
|
|
18
|
-
private createScriptBlob;
|
|
19
|
-
private loadScriptElement;
|
|
20
|
-
private getTokenFromScript;
|
|
21
|
-
private cleanup;
|
|
13
|
+
private _getScriptUrl;
|
|
14
|
+
private _warmupScript;
|
|
15
|
+
private _refetchScript;
|
|
16
|
+
private setupReady;
|
|
22
17
|
configure(params: DeflectConfig): void;
|
|
23
18
|
solveChallenge(): Promise<string>;
|
|
24
|
-
warmup(): Promise<boolean>;
|
|
25
|
-
clearCache(): void;
|
|
26
19
|
}
|
|
27
20
|
declare const _default: Deflect;
|
|
28
21
|
export default _default;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,217 +1,147 @@
|
|
|
1
1
|
interface DeflectConfig {
|
|
2
2
|
actionId: string;
|
|
3
3
|
scriptUrl?: string;
|
|
4
|
-
extraArgs?:
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
interface DeflectScript {
|
|
8
|
-
sessionId?: string;
|
|
9
|
-
content: string;
|
|
4
|
+
extraArgs?: { [key: string]: string };
|
|
10
5
|
}
|
|
11
6
|
|
|
12
7
|
class Deflect {
|
|
13
|
-
private
|
|
14
|
-
private
|
|
15
|
-
private
|
|
8
|
+
private _prefetchedScriptText: string | null = null;
|
|
9
|
+
private _hasUsedPrefetch = false;
|
|
10
|
+
private _customScriptUrl: string | null = null;
|
|
16
11
|
|
|
17
12
|
constructor() {
|
|
18
|
-
this.initializeGlobalState();
|
|
19
|
-
this.setupAutomaticWarmup();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
private initializeGlobalState(): void {
|
|
23
|
-
if (typeof window === "undefined") return;
|
|
24
|
-
|
|
25
13
|
window.Deflect = window.Deflect || {};
|
|
26
14
|
window.Deflect.extraArgs = window.Deflect.extraArgs || {};
|
|
27
|
-
}
|
|
28
15
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
setTimeout(() => this.tryWarmup(), 100);
|
|
16
|
+
if (typeof window !== "undefined") {
|
|
17
|
+
window.addEventListener("load", () => {
|
|
18
|
+
setTimeout(() => {
|
|
19
|
+
this._warmupScript();
|
|
20
|
+
}, 500);
|
|
21
|
+
});
|
|
36
22
|
}
|
|
37
23
|
}
|
|
38
24
|
|
|
39
|
-
private
|
|
40
|
-
|
|
41
|
-
|
|
25
|
+
private _getScriptUrl(): string {
|
|
26
|
+
const nonce = Date.now().toString();
|
|
27
|
+
if (this._customScriptUrl) {
|
|
28
|
+
const separator = this._customScriptUrl.includes("?") ? "&" : "?";
|
|
29
|
+
return `${this._customScriptUrl}${separator}action_id=${window.Deflect.actionId}&_=${nonce}`;
|
|
42
30
|
}
|
|
31
|
+
return `https://js.deflect.bot/main.js?action_id=${window.Deflect.actionId}&_=${nonce}`;
|
|
32
|
+
}
|
|
43
33
|
|
|
44
|
-
|
|
34
|
+
private _warmupScript(): void {
|
|
35
|
+
if (!window.Deflect.actionId) return;
|
|
45
36
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
37
|
+
const scriptUrl = this._getScriptUrl();
|
|
38
|
+
fetch(scriptUrl, { cache: "no-store" })
|
|
39
|
+
.then(async (res) => {
|
|
40
|
+
if (!res.ok) return;
|
|
41
|
+
this._prefetchedScriptText = await res.text();
|
|
42
|
+
})
|
|
43
|
+
.catch(() => {});
|
|
53
44
|
}
|
|
54
45
|
|
|
55
|
-
private
|
|
56
|
-
|
|
46
|
+
private _refetchScript(): void {
|
|
47
|
+
this._hasUsedPrefetch = false;
|
|
48
|
+
this._prefetchedScriptText = null;
|
|
57
49
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
50
|
+
setTimeout(() => {
|
|
51
|
+
this._warmupScript();
|
|
52
|
+
}, 100);
|
|
53
|
+
}
|
|
61
54
|
|
|
62
|
-
|
|
63
|
-
|
|
55
|
+
private setupReady(): void {
|
|
56
|
+
let resolveFn: () => void;
|
|
57
|
+
const promise = new Promise<void>((resolve) => {
|
|
58
|
+
resolveFn = resolve;
|
|
59
|
+
});
|
|
60
|
+
window.Deflect.ready = {
|
|
61
|
+
promise,
|
|
62
|
+
resolve: () => resolveFn(),
|
|
63
|
+
};
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
if (!
|
|
68
|
-
throw new Error("actionId is required");
|
|
66
|
+
configure(params: DeflectConfig): void {
|
|
67
|
+
if (!params.actionId) {
|
|
68
|
+
throw new Error("actionId is required in configuration");
|
|
69
69
|
}
|
|
70
|
+
window.Deflect.actionId = params.actionId;
|
|
70
71
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (!response.ok) {
|
|
75
|
-
throw new Error(`Failed to fetch script: ${response.status}`);
|
|
72
|
+
if (params.scriptUrl) {
|
|
73
|
+
this._customScriptUrl = params.scriptUrl;
|
|
76
74
|
}
|
|
77
|
-
|
|
78
|
-
return {
|
|
79
|
-
content: await response.text(),
|
|
80
|
-
sessionId: response.headers.get("session_id") || undefined,
|
|
81
|
-
};
|
|
82
75
|
}
|
|
83
76
|
|
|
84
|
-
|
|
85
|
-
if (
|
|
86
|
-
|
|
77
|
+
async solveChallenge(): Promise<string> {
|
|
78
|
+
if (!window.Deflect.actionId) {
|
|
79
|
+
throw new Error("actionId is missing in configuration");
|
|
87
80
|
}
|
|
88
81
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const blobUrl = this.createScriptBlob(script.content);
|
|
92
|
-
const scriptElement = await this.loadScriptElement(blobUrl);
|
|
82
|
+
this.setupReady();
|
|
93
83
|
|
|
94
|
-
|
|
95
|
-
|
|
84
|
+
let scriptText: string;
|
|
85
|
+
let sessionId: string | null = null;
|
|
96
86
|
|
|
97
|
-
|
|
87
|
+
if (this._prefetchedScriptText && !this._hasUsedPrefetch) {
|
|
88
|
+
scriptText = this._prefetchedScriptText;
|
|
89
|
+
this._hasUsedPrefetch = true;
|
|
90
|
+
} else {
|
|
91
|
+
const scriptUrl = this._getScriptUrl();
|
|
92
|
+
const response = await fetch(scriptUrl, { cache: "no-store" });
|
|
98
93
|
|
|
99
|
-
|
|
94
|
+
if (!response.ok) {
|
|
95
|
+
throw new Error("Failed to fetch the Deflect script");
|
|
96
|
+
}
|
|
100
97
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
this.cleanup(blobUrl, scriptElement);
|
|
98
|
+
sessionId = response.headers.get("session_id");
|
|
99
|
+
scriptText = await response.text();
|
|
104
100
|
}
|
|
105
|
-
}
|
|
106
101
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
102
|
+
if (sessionId) {
|
|
103
|
+
window.Deflect.sessionId = sessionId;
|
|
104
|
+
}
|
|
110
105
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (typeof window !== "undefined") {
|
|
114
|
-
window.Deflect.ready = {
|
|
115
|
-
promise: new Promise<void>((innerResolve) => {
|
|
116
|
-
window.Deflect.ready.resolve = innerResolve;
|
|
117
|
-
}),
|
|
118
|
-
resolve: () => resolve(),
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
}
|
|
106
|
+
const blob = new Blob([scriptText], { type: "text/javascript" });
|
|
107
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
123
108
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
109
|
+
const scriptEl = document.createElement("script");
|
|
110
|
+
scriptEl.type = "module";
|
|
111
|
+
scriptEl.src = blobUrl;
|
|
128
112
|
|
|
129
|
-
|
|
130
|
-
return new Promise((resolve, reject) => {
|
|
131
|
-
const script = document.createElement("script");
|
|
132
|
-
script.type = "module";
|
|
133
|
-
script.src = blobUrl;
|
|
113
|
+
document.head.appendChild(scriptEl);
|
|
134
114
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
document.head.appendChild(script);
|
|
115
|
+
await new Promise<void>((resolve, reject) => {
|
|
116
|
+
scriptEl.onload = () => resolve();
|
|
117
|
+
scriptEl.onerror = () => reject("Failed to load the Deflect script");
|
|
139
118
|
});
|
|
140
|
-
}
|
|
141
119
|
|
|
142
|
-
|
|
120
|
+
await window.Deflect.ready?.promise;
|
|
121
|
+
|
|
143
122
|
if (
|
|
144
|
-
typeof window === "undefined" ||
|
|
145
|
-
typeof window.Deflect
|
|
123
|
+
typeof window.Deflect === "undefined" ||
|
|
124
|
+
typeof window.Deflect.getToken !== "function"
|
|
146
125
|
) {
|
|
147
|
-
throw new Error("
|
|
126
|
+
throw new Error("Deflect script did not load properly");
|
|
148
127
|
}
|
|
149
128
|
|
|
129
|
+
let token: string;
|
|
130
|
+
|
|
150
131
|
try {
|
|
151
|
-
|
|
132
|
+
token = await window.Deflect.getToken();
|
|
152
133
|
} catch (error) {
|
|
153
|
-
throw new Error(`
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
private cleanup(blobUrl: string, scriptElement: HTMLScriptElement): void {
|
|
158
|
-
URL.revokeObjectURL(blobUrl);
|
|
159
|
-
scriptElement.remove();
|
|
160
|
-
|
|
161
|
-
if (typeof window !== "undefined" && window.Deflect?.getToken) {
|
|
162
|
-
delete window.Deflect.getToken;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
public configure(params: DeflectConfig): void {
|
|
167
|
-
if (!params.actionId?.trim()) {
|
|
168
|
-
throw new Error("actionId is required and cannot be empty");
|
|
134
|
+
throw new Error(`Deflect script execution failed: ${error}`);
|
|
169
135
|
}
|
|
170
136
|
|
|
171
|
-
this.
|
|
172
|
-
|
|
173
|
-
if (typeof window !== "undefined") {
|
|
174
|
-
window.Deflect.actionId = params.actionId;
|
|
175
|
-
if (params.extraArgs) {
|
|
176
|
-
window.Deflect.extraArgs = { ...params.extraArgs };
|
|
177
|
-
}
|
|
178
|
-
}
|
|
137
|
+
this._refetchScript();
|
|
179
138
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
public async solveChallenge(): Promise<string> {
|
|
184
|
-
if (!this.config?.actionId) {
|
|
185
|
-
throw new Error("Must call configure() before solveChallenge()");
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
let script: DeflectScript;
|
|
189
|
-
|
|
190
|
-
if (this.scriptCache && !this.isWarmupInProgress) {
|
|
191
|
-
script = this.scriptCache;
|
|
192
|
-
this.scriptCache = null;
|
|
193
|
-
} else {
|
|
194
|
-
script = await this.fetchScript();
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return this.executeScript(script);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
public async warmup(): Promise<boolean> {
|
|
201
|
-
if (!this.config?.actionId) {
|
|
202
|
-
return false;
|
|
203
|
-
}
|
|
139
|
+
URL.revokeObjectURL(blobUrl);
|
|
140
|
+
scriptEl.remove();
|
|
204
141
|
|
|
205
|
-
|
|
206
|
-
await this.tryWarmup();
|
|
207
|
-
return this.scriptCache !== null;
|
|
208
|
-
} catch {
|
|
209
|
-
return false;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
142
|
+
delete window.Deflect.getToken;
|
|
212
143
|
|
|
213
|
-
|
|
214
|
-
this.scriptCache = null;
|
|
144
|
+
return token;
|
|
215
145
|
}
|
|
216
146
|
}
|
|
217
147
|
|