@deflectbot/deflect-sdk 1.1.1 → 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 +50 -133
- package/dist/types/index.d.ts +13 -18
- package/package.json +1 -1
- package/src/index.ts +21 -140
package/dist/index.js
CHANGED
|
@@ -1,133 +1,50 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
class Deflect {
|
|
11
|
-
constructor() {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
window.Deflect = window.Deflect ||
|
|
15
|
-
window.Deflect.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
}
|
|
104
|
-
solveChallenge() {
|
|
105
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
106
|
-
if (!window.Deflect.siteKey) {
|
|
107
|
-
throw new Error("API key (siteKey) is missing in configuration");
|
|
108
|
-
}
|
|
109
|
-
try {
|
|
110
|
-
yield this.scriptLoadPromise;
|
|
111
|
-
}
|
|
112
|
-
catch (error) {
|
|
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.");
|
|
120
|
-
}
|
|
121
|
-
try {
|
|
122
|
-
return yield window.Deflect.getToken(window.Deflect.siteKey, window.Deflect.sessionId);
|
|
123
|
-
}
|
|
124
|
-
catch (error) {
|
|
125
|
-
if (error instanceof Error) {
|
|
126
|
-
throw new Error("Error getting Deflect token: " + error.message);
|
|
127
|
-
}
|
|
128
|
-
throw new Error("Error getting Deflect token: " + String(error));
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
export default new Deflect();
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
class Deflect {
|
|
11
|
+
constructor() {
|
|
12
|
+
window.Deflect = window.Deflect || {};
|
|
13
|
+
window.Deflect.siteKey = window.Deflect.siteKey || "";
|
|
14
|
+
window.Deflect.sessionId = window.Deflect.sessionId || "";
|
|
15
|
+
window.Deflect.extraArgs = window.Deflect.extraArgs || {};
|
|
16
|
+
}
|
|
17
|
+
configure(params) {
|
|
18
|
+
if (!params.siteKey) {
|
|
19
|
+
throw new Error("siteKey is required in configuration");
|
|
20
|
+
}
|
|
21
|
+
window.Deflect.siteKey = params.siteKey;
|
|
22
|
+
}
|
|
23
|
+
solveChallenge() {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
if (!window.Deflect.siteKey) {
|
|
26
|
+
throw new Error("API key (siteKey) is missing in configuration");
|
|
27
|
+
}
|
|
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");
|
|
32
|
+
}
|
|
33
|
+
const sessionId = response.headers.get("session_id");
|
|
34
|
+
if (sessionId) {
|
|
35
|
+
window.Deflect.sessionId = sessionId;
|
|
36
|
+
}
|
|
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");
|
|
44
|
+
}
|
|
45
|
+
const token = yield window.Deflect.getToken();
|
|
46
|
+
return token;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export default new Deflect();
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,18 +1,13 @@
|
|
|
1
|
-
interface DeflectConfig {
|
|
2
|
-
siteKey: string;
|
|
3
|
-
extraArgs?: {
|
|
4
|
-
[key: string]: string;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
private injectScript;
|
|
15
|
-
solveChallenge(): Promise<string>;
|
|
16
|
-
}
|
|
17
|
-
declare const _default: Deflect;
|
|
18
|
-
export default _default;
|
|
1
|
+
interface DeflectConfig {
|
|
2
|
+
siteKey: string;
|
|
3
|
+
extraArgs?: {
|
|
4
|
+
[key: string]: string;
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
declare class Deflect {
|
|
8
|
+
constructor();
|
|
9
|
+
configure(params: DeflectConfig): void;
|
|
10
|
+
solveChallenge(): Promise<string>;
|
|
11
|
+
}
|
|
12
|
+
declare const _default: Deflect;
|
|
13
|
+
export default _default;
|
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
|
|