@deflectbot/deflect-sdk 1.0.8 → 1.0.10
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/.gitattributes +2 -2
- package/dist/index.js +133 -42
- package/dist/types/index.d.ts +17 -10
- package/eslint.config.mjs +12 -0
- package/package.json +6 -2
- package/src/index.ts +45 -20
package/.gitattributes
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
# Auto detect text files and perform LF normalization
|
|
2
|
-
* text=auto
|
|
1
|
+
# Auto detect text files and perform LF normalization
|
|
2
|
+
* text=auto
|
package/dist/index.js
CHANGED
|
@@ -1,42 +1,133 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
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
|
+
this.scriptLoadPromise = null;
|
|
13
|
+
this.cacheTTL = 10 * 60 * 1000;
|
|
14
|
+
window.Deflect = window.Deflect || {};
|
|
15
|
+
window.Deflect.siteKey = window.Deflect.siteKey || "";
|
|
16
|
+
window.Deflect.sessionId = window.Deflect.sessionId || "";
|
|
17
|
+
window.Deflect.extraArgs = window.Deflect.extraArgs || {};
|
|
18
|
+
}
|
|
19
|
+
configure(params, forceRefresh = false) {
|
|
20
|
+
if (!params.siteKey) {
|
|
21
|
+
throw new Error("siteKey is required in configuration");
|
|
22
|
+
}
|
|
23
|
+
window.Deflect.siteKey = params.siteKey;
|
|
24
|
+
window.Deflect.extraArgs = params.extraArgs || {};
|
|
25
|
+
if (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?site=${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
|
+
}
|
|
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();
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
interface DeflectConfig {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
interface DeflectConfig {
|
|
2
|
+
siteKey: string;
|
|
3
|
+
extraArgs?: {
|
|
4
|
+
[key: string]: string;
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
declare class Deflect {
|
|
8
|
+
private scriptLoadPromise;
|
|
9
|
+
private cacheTTL;
|
|
10
|
+
constructor();
|
|
11
|
+
configure(params: DeflectConfig, forceRefresh?: boolean): void;
|
|
12
|
+
private loadDeflectScript;
|
|
13
|
+
private injectScript;
|
|
14
|
+
solveChallenge(): Promise<string>;
|
|
15
|
+
}
|
|
16
|
+
declare const _default: Deflect;
|
|
17
|
+
export default _default;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import globals from "globals";
|
|
2
|
+
import pluginJs from "@eslint/js";
|
|
3
|
+
import tseslint from "typescript-eslint";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/** @type {import('eslint').Linter.Config[]} */
|
|
7
|
+
export default [
|
|
8
|
+
{files: ["**/*.{js,mjs,cjs,ts}"]},
|
|
9
|
+
{languageOptions: { globals: globals.browser }},
|
|
10
|
+
pluginJs.configs.recommended,
|
|
11
|
+
...tseslint.configs.recommended,
|
|
12
|
+
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deflectbot/deflect-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/types/index.d.ts",
|
|
@@ -10,7 +10,11 @@
|
|
|
10
10
|
"author": "Fredrik Rafn",
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"
|
|
13
|
+
"@eslint/js": "^9.22.0",
|
|
14
|
+
"eslint": "^9.22.0",
|
|
15
|
+
"globals": "^16.0.0",
|
|
16
|
+
"typescript": "^5.7.3",
|
|
17
|
+
"typescript-eslint": "^8.27.0"
|
|
14
18
|
},
|
|
15
19
|
"publishConfig": {
|
|
16
20
|
"access": "public"
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
interface DeflectConfig {
|
|
2
2
|
siteKey: string;
|
|
3
3
|
extraArgs?: { [key: string]: string };
|
|
4
|
+
forceRefresh?: boolean;
|
|
4
5
|
}
|
|
5
6
|
|
|
6
7
|
class Deflect {
|
|
@@ -14,22 +15,22 @@ class Deflect {
|
|
|
14
15
|
window.Deflect.extraArgs = window.Deflect.extraArgs || {};
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
configure(params: DeflectConfig
|
|
18
|
+
configure(params: DeflectConfig): void {
|
|
18
19
|
if (!params.siteKey) {
|
|
19
20
|
throw new Error("siteKey is required in configuration");
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
+
|
|
22
23
|
window.Deflect.siteKey = params.siteKey;
|
|
23
|
-
window.Deflect.extraArgs = params.extraArgs || {};
|
|
24
|
-
|
|
25
|
-
if (forceRefresh) {
|
|
24
|
+
window.Deflect.extraArgs = params.extraArgs || {};
|
|
25
|
+
|
|
26
|
+
if (params.forceRefresh) {
|
|
26
27
|
console.log("Main.js force refreshed");
|
|
27
28
|
sessionStorage.removeItem("deflect_script");
|
|
28
29
|
localStorage.removeItem("deflect_script_timestamp");
|
|
29
30
|
|
|
30
31
|
this.scriptLoadPromise = null;
|
|
31
32
|
}
|
|
32
|
-
|
|
33
|
+
|
|
33
34
|
if (!this.scriptLoadPromise) {
|
|
34
35
|
this.scriptLoadPromise = this.loadDeflectScript();
|
|
35
36
|
}
|
|
@@ -37,17 +38,28 @@ class Deflect {
|
|
|
37
38
|
|
|
38
39
|
private loadDeflectScript(): Promise<void> {
|
|
39
40
|
const extraArgs = Object.keys(window.Deflect.extraArgs || {})
|
|
40
|
-
.map(
|
|
41
|
+
.map(
|
|
42
|
+
(key) =>
|
|
43
|
+
`${encodeURIComponent(key)}=${encodeURIComponent(
|
|
44
|
+
window.Deflect.extraArgs[key]
|
|
45
|
+
)}`
|
|
46
|
+
)
|
|
41
47
|
.join("&");
|
|
42
48
|
|
|
43
|
-
const scriptUrl = `https://js.deflect.bot/main.js?site=${encodeURIComponent(
|
|
49
|
+
const scriptUrl = `https://js.deflect.bot/main.js?site=${encodeURIComponent(
|
|
50
|
+
window.Deflect.siteKey
|
|
51
|
+
)}${extraArgs ? `&${extraArgs}` : ""}`;
|
|
44
52
|
|
|
45
53
|
return new Promise((resolve, reject) => {
|
|
46
54
|
const cachedScript = sessionStorage.getItem("deflect_script");
|
|
47
55
|
const lastFetchTime = localStorage.getItem("deflect_script_timestamp");
|
|
48
56
|
|
|
49
57
|
// Check if we have a valid cache (within 10 minutes)
|
|
50
|
-
if (
|
|
58
|
+
if (
|
|
59
|
+
cachedScript &&
|
|
60
|
+
lastFetchTime &&
|
|
61
|
+
Date.now() - parseInt(lastFetchTime) < this.cacheTTL
|
|
62
|
+
) {
|
|
51
63
|
console.log("✅ Using cached Deflect script");
|
|
52
64
|
this.injectScript(cachedScript, resolve, reject);
|
|
53
65
|
return;
|
|
@@ -76,23 +88,29 @@ class Deflect {
|
|
|
76
88
|
const blob = new Blob([workerCode], { type: "application/javascript" });
|
|
77
89
|
const worker = new Worker(URL.createObjectURL(blob));
|
|
78
90
|
|
|
79
|
-
worker.onmessage = (
|
|
91
|
+
worker.onmessage = (
|
|
92
|
+
event: MessageEvent<{
|
|
93
|
+
scriptText?: string;
|
|
94
|
+
sessionId?: string;
|
|
95
|
+
error?: string;
|
|
96
|
+
}>
|
|
97
|
+
) => {
|
|
80
98
|
if (event.data.error) {
|
|
81
99
|
reject(new Error(event.data.error));
|
|
82
100
|
worker.terminate();
|
|
83
101
|
return;
|
|
84
102
|
}
|
|
85
|
-
|
|
86
|
-
|
|
103
|
+
|
|
104
|
+
const scriptText = event.data.scriptText || "";
|
|
87
105
|
window.Deflect.sessionId = event.data.sessionId || "";
|
|
88
|
-
|
|
106
|
+
|
|
89
107
|
// Cache script info
|
|
90
108
|
sessionStorage.setItem("deflect_script", scriptText);
|
|
91
109
|
localStorage.setItem("deflect_script_timestamp", Date.now().toString());
|
|
92
|
-
|
|
110
|
+
|
|
93
111
|
this.injectScript(scriptText, resolve, reject);
|
|
94
112
|
worker.terminate();
|
|
95
|
-
};
|
|
113
|
+
};
|
|
96
114
|
|
|
97
115
|
worker.onerror = function (event: ErrorEvent) {
|
|
98
116
|
reject(new Error("Worker encountered an error: " + event.message));
|
|
@@ -103,7 +121,11 @@ class Deflect {
|
|
|
103
121
|
});
|
|
104
122
|
}
|
|
105
123
|
|
|
106
|
-
private injectScript(
|
|
124
|
+
private injectScript(
|
|
125
|
+
scriptText: string,
|
|
126
|
+
resolve: () => void,
|
|
127
|
+
reject: (error: Error) => void
|
|
128
|
+
) {
|
|
107
129
|
const scriptEl = document.createElement("script");
|
|
108
130
|
scriptEl.textContent = scriptText;
|
|
109
131
|
scriptEl.onload = () => {
|
|
@@ -129,21 +151,24 @@ class Deflect {
|
|
|
129
151
|
}
|
|
130
152
|
throw new Error("Deflect script failed to load: " + String(error));
|
|
131
153
|
}
|
|
132
|
-
|
|
133
154
|
|
|
134
155
|
if (typeof window.Deflect.getToken !== "function") {
|
|
135
|
-
throw new Error(
|
|
156
|
+
throw new Error(
|
|
157
|
+
"Deflect script did not load properly or is not exposing getToken."
|
|
158
|
+
);
|
|
136
159
|
}
|
|
137
160
|
|
|
138
161
|
try {
|
|
139
|
-
return await window.Deflect.getToken(
|
|
162
|
+
return await window.Deflect.getToken(
|
|
163
|
+
window.Deflect.siteKey,
|
|
164
|
+
window.Deflect.sessionId
|
|
165
|
+
);
|
|
140
166
|
} catch (error: unknown) {
|
|
141
167
|
if (error instanceof Error) {
|
|
142
168
|
throw new Error("Error getting Deflect token: " + error.message);
|
|
143
169
|
}
|
|
144
170
|
throw new Error("Error getting Deflect token: " + String(error));
|
|
145
171
|
}
|
|
146
|
-
|
|
147
172
|
}
|
|
148
173
|
}
|
|
149
174
|
|