@deflectbot/deflect-sdk 1.1.2 → 1.1.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 +16 -99
- package/dist/types/index.d.ts +0 -5
- package/package.json +1 -1
- package/src/index.ts +21 -140
package/dist/index.js
CHANGED
|
@@ -9,8 +9,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
class Deflect {
|
|
11
11
|
constructor() {
|
|
12
|
-
this.scriptLoadPromise = null;
|
|
13
|
-
this.cacheTTL = 10 * 60 * 1000;
|
|
14
12
|
window.Deflect = window.Deflect || {};
|
|
15
13
|
window.Deflect.siteKey = window.Deflect.siteKey || "";
|
|
16
14
|
window.Deflect.sessionId = window.Deflect.sessionId || "";
|
|
@@ -21,112 +19,31 @@ class Deflect {
|
|
|
21
19
|
throw new Error("siteKey is required in configuration");
|
|
22
20
|
}
|
|
23
21
|
window.Deflect.siteKey = params.siteKey;
|
|
24
|
-
window.Deflect.extraArgs = params.extraArgs || {};
|
|
25
|
-
if (params.forceRefresh) {
|
|
26
|
-
console.log("Main.js force refreshed");
|
|
27
|
-
sessionStorage.removeItem("deflect_script");
|
|
28
|
-
localStorage.removeItem("deflect_script_timestamp");
|
|
29
|
-
this.scriptLoadPromise = null;
|
|
30
|
-
}
|
|
31
|
-
if (!this.scriptLoadPromise) {
|
|
32
|
-
this.scriptLoadPromise = this.loadDeflectScript();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
loadDeflectScript() {
|
|
36
|
-
const extraArgs = Object.keys(window.Deflect.extraArgs || {})
|
|
37
|
-
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(window.Deflect.extraArgs[key])}`)
|
|
38
|
-
.join("&");
|
|
39
|
-
const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${encodeURIComponent(window.Deflect.siteKey)}${extraArgs ? `&${extraArgs}` : ""}`;
|
|
40
|
-
return new Promise((resolve, reject) => {
|
|
41
|
-
const cachedScript = sessionStorage.getItem("deflect_script");
|
|
42
|
-
const lastFetchTime = localStorage.getItem("deflect_script_timestamp");
|
|
43
|
-
// Check if we have a valid cache (within 10 minutes)
|
|
44
|
-
if (cachedScript &&
|
|
45
|
-
lastFetchTime &&
|
|
46
|
-
Date.now() - parseInt(lastFetchTime) < this.cacheTTL) {
|
|
47
|
-
console.log("✅ Using cached Deflect script");
|
|
48
|
-
this.injectScript(cachedScript, resolve, reject);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
console.log("⏳ Fetching new Deflect script...");
|
|
52
|
-
const workerCode = `
|
|
53
|
-
self.onmessage = async function(event) {
|
|
54
|
-
try {
|
|
55
|
-
const response = await fetch(event.data.url, { cache: "no-store" });
|
|
56
|
-
if (!response.ok) {
|
|
57
|
-
const errorText = await response.text();
|
|
58
|
-
self.postMessage({ error: 'Failed to fetch script. Status: ' + response.status + ' - ' + errorText });
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
const sessionId = response.headers.get("session_id") || "";
|
|
62
|
-
const scriptText = await response.text();
|
|
63
|
-
self.postMessage({ scriptText: scriptText, sessionId: sessionId });
|
|
64
|
-
} catch (err) {
|
|
65
|
-
self.postMessage({ error: 'Error in worker: ' + (err instanceof Error ? err.message : String(err)) });
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
`;
|
|
69
|
-
const blob = new Blob([workerCode], { type: "application/javascript" });
|
|
70
|
-
const worker = new Worker(URL.createObjectURL(blob));
|
|
71
|
-
worker.onmessage = (event) => {
|
|
72
|
-
if (event.data.error) {
|
|
73
|
-
reject(new Error(event.data.error));
|
|
74
|
-
worker.terminate();
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
const scriptText = event.data.scriptText || "";
|
|
78
|
-
window.Deflect.sessionId = event.data.sessionId || "";
|
|
79
|
-
// Cache script info
|
|
80
|
-
sessionStorage.setItem("deflect_script", scriptText);
|
|
81
|
-
localStorage.setItem("deflect_script_timestamp", Date.now().toString());
|
|
82
|
-
this.injectScript(scriptText, resolve, reject);
|
|
83
|
-
worker.terminate();
|
|
84
|
-
};
|
|
85
|
-
worker.onerror = function (event) {
|
|
86
|
-
reject(new Error("Worker encountered an error: " + event.message));
|
|
87
|
-
worker.terminate();
|
|
88
|
-
};
|
|
89
|
-
worker.postMessage({ url: scriptUrl });
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
injectScript(scriptText, resolve, reject) {
|
|
93
|
-
const scriptEl = document.createElement("script");
|
|
94
|
-
scriptEl.textContent = scriptText;
|
|
95
|
-
scriptEl.onload = () => {
|
|
96
|
-
console.log("✅ Deflect script loaded successfully.");
|
|
97
|
-
resolve();
|
|
98
|
-
};
|
|
99
|
-
scriptEl.onerror = () => {
|
|
100
|
-
reject(new Error("Failed to load Deflect script"));
|
|
101
|
-
};
|
|
102
|
-
document.head.appendChild(scriptEl);
|
|
103
22
|
}
|
|
104
23
|
solveChallenge() {
|
|
105
24
|
return __awaiter(this, void 0, void 0, function* () {
|
|
106
25
|
if (!window.Deflect.siteKey) {
|
|
107
26
|
throw new Error("API key (siteKey) is missing in configuration");
|
|
108
27
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (error instanceof Error) {
|
|
114
|
-
throw new Error("Deflect script failed to load: " + error.message);
|
|
115
|
-
}
|
|
116
|
-
throw new Error("Deflect script failed to load: " + String(error));
|
|
117
|
-
}
|
|
118
|
-
if (typeof window.Deflect.getToken !== "function") {
|
|
119
|
-
throw new Error("Deflect script did not load properly or is not exposing getToken.");
|
|
28
|
+
const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}`;
|
|
29
|
+
const response = yield fetch(scriptUrl);
|
|
30
|
+
if (!response.ok) {
|
|
31
|
+
throw new Error("Failed to fetch the Deflect script");
|
|
120
32
|
}
|
|
121
|
-
|
|
122
|
-
|
|
33
|
+
const sessionId = response.headers.get("session_id");
|
|
34
|
+
if (sessionId) {
|
|
35
|
+
window.Deflect.sessionId = sessionId;
|
|
123
36
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
37
|
+
const scriptText = yield response.text();
|
|
38
|
+
const scriptEl = document.createElement("script");
|
|
39
|
+
scriptEl.textContent = scriptText;
|
|
40
|
+
document.head.appendChild(scriptEl);
|
|
41
|
+
if (typeof window.Deflect === "undefined" ||
|
|
42
|
+
typeof window.Deflect.getToken !== "function") {
|
|
43
|
+
throw new Error("Deflect script did not load properly");
|
|
129
44
|
}
|
|
45
|
+
const token = yield window.Deflect.getToken();
|
|
46
|
+
return token;
|
|
130
47
|
});
|
|
131
48
|
}
|
|
132
49
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -3,15 +3,10 @@ interface DeflectConfig {
|
|
|
3
3
|
extraArgs?: {
|
|
4
4
|
[key: string]: string;
|
|
5
5
|
};
|
|
6
|
-
forceRefresh?: boolean;
|
|
7
6
|
}
|
|
8
7
|
declare class Deflect {
|
|
9
|
-
private scriptLoadPromise;
|
|
10
|
-
private cacheTTL;
|
|
11
8
|
constructor();
|
|
12
9
|
configure(params: DeflectConfig): void;
|
|
13
|
-
private loadDeflectScript;
|
|
14
|
-
private injectScript;
|
|
15
10
|
solveChallenge(): Promise<string>;
|
|
16
11
|
}
|
|
17
12
|
declare const _default: Deflect;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
interface DeflectConfig {
|
|
2
2
|
siteKey: string;
|
|
3
3
|
extraArgs?: { [key: string]: string };
|
|
4
|
-
forceRefresh?: boolean;
|
|
5
4
|
}
|
|
6
5
|
|
|
7
6
|
class Deflect {
|
|
8
|
-
private scriptLoadPromise: Promise<void> | null = null;
|
|
9
|
-
private cacheTTL: number = 10 * 60 * 1000;
|
|
10
|
-
|
|
11
7
|
constructor() {
|
|
12
8
|
window.Deflect = window.Deflect || {};
|
|
13
9
|
window.Deflect.siteKey = window.Deflect.siteKey || "";
|
|
@@ -19,156 +15,41 @@ class Deflect {
|
|
|
19
15
|
if (!params.siteKey) {
|
|
20
16
|
throw new Error("siteKey is required in configuration");
|
|
21
17
|
}
|
|
22
|
-
|
|
23
18
|
window.Deflect.siteKey = params.siteKey;
|
|
24
|
-
window.Deflect.extraArgs = params.extraArgs || {};
|
|
25
|
-
|
|
26
|
-
if (params.forceRefresh) {
|
|
27
|
-
console.log("Main.js force refreshed");
|
|
28
|
-
sessionStorage.removeItem("deflect_script");
|
|
29
|
-
localStorage.removeItem("deflect_script_timestamp");
|
|
30
|
-
|
|
31
|
-
this.scriptLoadPromise = null;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (!this.scriptLoadPromise) {
|
|
35
|
-
this.scriptLoadPromise = this.loadDeflectScript();
|
|
36
|
-
}
|
|
37
19
|
}
|
|
38
20
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
`${encodeURIComponent(key)}=${encodeURIComponent(
|
|
44
|
-
window.Deflect.extraArgs[key]
|
|
45
|
-
)}`
|
|
46
|
-
)
|
|
47
|
-
.join("&");
|
|
48
|
-
|
|
49
|
-
const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${encodeURIComponent(
|
|
50
|
-
window.Deflect.siteKey
|
|
51
|
-
)}${extraArgs ? `&${extraArgs}` : ""}`;
|
|
52
|
-
|
|
53
|
-
return new Promise((resolve, reject) => {
|
|
54
|
-
const cachedScript = sessionStorage.getItem("deflect_script");
|
|
55
|
-
const lastFetchTime = localStorage.getItem("deflect_script_timestamp");
|
|
56
|
-
|
|
57
|
-
// Check if we have a valid cache (within 10 minutes)
|
|
58
|
-
if (
|
|
59
|
-
cachedScript &&
|
|
60
|
-
lastFetchTime &&
|
|
61
|
-
Date.now() - parseInt(lastFetchTime) < this.cacheTTL
|
|
62
|
-
) {
|
|
63
|
-
console.log("✅ Using cached Deflect script");
|
|
64
|
-
this.injectScript(cachedScript, resolve, reject);
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
console.log("⏳ Fetching new Deflect script...");
|
|
69
|
-
|
|
70
|
-
const workerCode = `
|
|
71
|
-
self.onmessage = async function(event) {
|
|
72
|
-
try {
|
|
73
|
-
const response = await fetch(event.data.url, { cache: "no-store" });
|
|
74
|
-
if (!response.ok) {
|
|
75
|
-
const errorText = await response.text();
|
|
76
|
-
self.postMessage({ error: 'Failed to fetch script. Status: ' + response.status + ' - ' + errorText });
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
const sessionId = response.headers.get("session_id") || "";
|
|
80
|
-
const scriptText = await response.text();
|
|
81
|
-
self.postMessage({ scriptText: scriptText, sessionId: sessionId });
|
|
82
|
-
} catch (err) {
|
|
83
|
-
self.postMessage({ error: 'Error in worker: ' + (err instanceof Error ? err.message : String(err)) });
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
`;
|
|
87
|
-
|
|
88
|
-
const blob = new Blob([workerCode], { type: "application/javascript" });
|
|
89
|
-
const worker = new Worker(URL.createObjectURL(blob));
|
|
90
|
-
|
|
91
|
-
worker.onmessage = (
|
|
92
|
-
event: MessageEvent<{
|
|
93
|
-
scriptText?: string;
|
|
94
|
-
sessionId?: string;
|
|
95
|
-
error?: string;
|
|
96
|
-
}>
|
|
97
|
-
) => {
|
|
98
|
-
if (event.data.error) {
|
|
99
|
-
reject(new Error(event.data.error));
|
|
100
|
-
worker.terminate();
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const scriptText = event.data.scriptText || "";
|
|
105
|
-
window.Deflect.sessionId = event.data.sessionId || "";
|
|
21
|
+
async solveChallenge(): Promise<string> {
|
|
22
|
+
if (!window.Deflect.siteKey) {
|
|
23
|
+
throw new Error("API key (siteKey) is missing in configuration");
|
|
24
|
+
}
|
|
106
25
|
|
|
107
|
-
|
|
108
|
-
sessionStorage.setItem("deflect_script", scriptText);
|
|
109
|
-
localStorage.setItem("deflect_script_timestamp", Date.now().toString());
|
|
26
|
+
const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}`;
|
|
110
27
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
28
|
+
const response = await fetch(scriptUrl);
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
throw new Error("Failed to fetch the Deflect script");
|
|
31
|
+
}
|
|
114
32
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
33
|
+
const sessionId = response.headers.get("session_id");
|
|
34
|
+
if (sessionId) {
|
|
35
|
+
window.Deflect.sessionId = sessionId;
|
|
36
|
+
}
|
|
119
37
|
|
|
120
|
-
|
|
121
|
-
});
|
|
122
|
-
}
|
|
38
|
+
const scriptText = await response.text();
|
|
123
39
|
|
|
124
|
-
private injectScript(
|
|
125
|
-
scriptText: string,
|
|
126
|
-
resolve: () => void,
|
|
127
|
-
reject: (error: Error) => void
|
|
128
|
-
) {
|
|
129
40
|
const scriptEl = document.createElement("script");
|
|
130
41
|
scriptEl.textContent = scriptText;
|
|
131
|
-
scriptEl.onload = () => {
|
|
132
|
-
console.log("✅ Deflect script loaded successfully.");
|
|
133
|
-
resolve();
|
|
134
|
-
};
|
|
135
|
-
scriptEl.onerror = () => {
|
|
136
|
-
reject(new Error("Failed to load Deflect script"));
|
|
137
|
-
};
|
|
138
42
|
document.head.appendChild(scriptEl);
|
|
139
|
-
}
|
|
140
43
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
try {
|
|
147
|
-
await this.scriptLoadPromise;
|
|
148
|
-
} catch (error: unknown) {
|
|
149
|
-
if (error instanceof Error) {
|
|
150
|
-
throw new Error("Deflect script failed to load: " + error.message);
|
|
151
|
-
}
|
|
152
|
-
throw new Error("Deflect script failed to load: " + String(error));
|
|
44
|
+
if (
|
|
45
|
+
typeof window.Deflect === "undefined" ||
|
|
46
|
+
typeof window.Deflect.getToken !== "function"
|
|
47
|
+
) {
|
|
48
|
+
throw new Error("Deflect script did not load properly");
|
|
153
49
|
}
|
|
154
50
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
"Deflect script did not load properly or is not exposing getToken."
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
try {
|
|
162
|
-
return await window.Deflect.getToken(
|
|
163
|
-
window.Deflect.siteKey,
|
|
164
|
-
window.Deflect.sessionId
|
|
165
|
-
);
|
|
166
|
-
} catch (error: unknown) {
|
|
167
|
-
if (error instanceof Error) {
|
|
168
|
-
throw new Error("Error getting Deflect token: " + error.message);
|
|
169
|
-
}
|
|
170
|
-
throw new Error("Error getting Deflect token: " + String(error));
|
|
171
|
-
}
|
|
51
|
+
const token = await window.Deflect.getToken();
|
|
52
|
+
return token;
|
|
172
53
|
}
|
|
173
54
|
}
|
|
174
55
|
|