@deflectbot/deflect-sdk 1.1.2 → 1.1.4
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 +32 -144
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,157 +15,49 @@ 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
|
-
}
|
|
38
|
-
|
|
39
|
-
private loadDeflectScript(): Promise<void> {
|
|
40
|
-
const extraArgs = Object.keys(window.Deflect.extraArgs || {})
|
|
41
|
-
.map(
|
|
42
|
-
(key) =>
|
|
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 || "";
|
|
106
|
-
|
|
107
|
-
// Cache script info
|
|
108
|
-
sessionStorage.setItem("deflect_script", scriptText);
|
|
109
|
-
localStorage.setItem("deflect_script_timestamp", Date.now().toString());
|
|
110
|
-
|
|
111
|
-
this.injectScript(scriptText, resolve, reject);
|
|
112
|
-
worker.terminate();
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
worker.onerror = function (event: ErrorEvent) {
|
|
116
|
-
reject(new Error("Worker encountered an error: " + event.message));
|
|
117
|
-
worker.terminate();
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
worker.postMessage({ url: scriptUrl });
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
private injectScript(
|
|
125
|
-
scriptText: string,
|
|
126
|
-
resolve: () => void,
|
|
127
|
-
reject: (error: Error) => void
|
|
128
|
-
) {
|
|
129
|
-
const scriptEl = document.createElement("script");
|
|
130
|
-
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
|
-
document.head.appendChild(scriptEl);
|
|
139
19
|
}
|
|
140
20
|
|
|
141
21
|
async solveChallenge(): Promise<string> {
|
|
142
22
|
if (!window.Deflect.siteKey) {
|
|
143
23
|
throw new Error("API key (siteKey) is missing in configuration");
|
|
144
24
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
throw new Error("Deflect script failed to load: " + String(error));
|
|
25
|
+
|
|
26
|
+
const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}`;
|
|
27
|
+
|
|
28
|
+
const response = await fetch(scriptUrl);
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
throw new Error("Failed to fetch the Deflect script");
|
|
153
31
|
}
|
|
154
|
-
|
|
155
|
-
if (
|
|
156
|
-
|
|
157
|
-
"Deflect script did not load properly or is not exposing getToken."
|
|
158
|
-
);
|
|
32
|
+
const sessionId = response.headers.get("session_id");
|
|
33
|
+
if (sessionId) {
|
|
34
|
+
window.Deflect.sessionId = sessionId;
|
|
159
35
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
36
|
+
|
|
37
|
+
const scriptEl = document.createElement("script");
|
|
38
|
+
scriptEl.type = "module";
|
|
39
|
+
const blob = new Blob([await response.text()], { type: "text/javascript" });
|
|
40
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
41
|
+
scriptEl.src = blobUrl;
|
|
42
|
+
|
|
43
|
+
document.head.appendChild(scriptEl);
|
|
44
|
+
|
|
45
|
+
await new Promise<void>((resolve, reject) => {
|
|
46
|
+
scriptEl.onload = () => resolve();
|
|
47
|
+
scriptEl.onerror = () => reject("Failed to load the Deflect script");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (
|
|
51
|
+
typeof window.Deflect === "undefined" ||
|
|
52
|
+
typeof window.Deflect.getToken !== "function"
|
|
53
|
+
) {
|
|
54
|
+
throw new Error("Deflect script did not load properly");
|
|
171
55
|
}
|
|
56
|
+
|
|
57
|
+
const token = await window.Deflect.getToken();
|
|
58
|
+
return token;
|
|
172
59
|
}
|
|
60
|
+
|
|
173
61
|
}
|
|
174
62
|
|
|
175
63
|
export default new Deflect();
|