@deflectbot/deflect-sdk 1.0.8 → 1.0.9
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 +42 -18
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.9",
|
|
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
|
@@ -18,10 +18,10 @@ class Deflect {
|
|
|
18
18
|
if (!params.siteKey) {
|
|
19
19
|
throw new Error("siteKey is required in configuration");
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
window.Deflect.siteKey = params.siteKey;
|
|
23
|
-
window.Deflect.extraArgs = params.extraArgs || {};
|
|
24
|
-
|
|
23
|
+
window.Deflect.extraArgs = params.extraArgs || {};
|
|
24
|
+
|
|
25
25
|
if (forceRefresh) {
|
|
26
26
|
console.log("Main.js force refreshed");
|
|
27
27
|
sessionStorage.removeItem("deflect_script");
|
|
@@ -29,7 +29,7 @@ class Deflect {
|
|
|
29
29
|
|
|
30
30
|
this.scriptLoadPromise = null;
|
|
31
31
|
}
|
|
32
|
-
|
|
32
|
+
|
|
33
33
|
if (!this.scriptLoadPromise) {
|
|
34
34
|
this.scriptLoadPromise = this.loadDeflectScript();
|
|
35
35
|
}
|
|
@@ -37,17 +37,28 @@ class Deflect {
|
|
|
37
37
|
|
|
38
38
|
private loadDeflectScript(): Promise<void> {
|
|
39
39
|
const extraArgs = Object.keys(window.Deflect.extraArgs || {})
|
|
40
|
-
.map(
|
|
40
|
+
.map(
|
|
41
|
+
(key) =>
|
|
42
|
+
`${encodeURIComponent(key)}=${encodeURIComponent(
|
|
43
|
+
window.Deflect.extraArgs[key]
|
|
44
|
+
)}`
|
|
45
|
+
)
|
|
41
46
|
.join("&");
|
|
42
47
|
|
|
43
|
-
const scriptUrl = `https://js.deflect.bot/main.js?site=${encodeURIComponent(
|
|
48
|
+
const scriptUrl = `https://js.deflect.bot/main.js?site=${encodeURIComponent(
|
|
49
|
+
window.Deflect.siteKey
|
|
50
|
+
)}${extraArgs ? `&${extraArgs}` : ""}`;
|
|
44
51
|
|
|
45
52
|
return new Promise((resolve, reject) => {
|
|
46
53
|
const cachedScript = sessionStorage.getItem("deflect_script");
|
|
47
54
|
const lastFetchTime = localStorage.getItem("deflect_script_timestamp");
|
|
48
55
|
|
|
49
56
|
// Check if we have a valid cache (within 10 minutes)
|
|
50
|
-
if (
|
|
57
|
+
if (
|
|
58
|
+
cachedScript &&
|
|
59
|
+
lastFetchTime &&
|
|
60
|
+
Date.now() - parseInt(lastFetchTime) < this.cacheTTL
|
|
61
|
+
) {
|
|
51
62
|
console.log("✅ Using cached Deflect script");
|
|
52
63
|
this.injectScript(cachedScript, resolve, reject);
|
|
53
64
|
return;
|
|
@@ -76,23 +87,29 @@ class Deflect {
|
|
|
76
87
|
const blob = new Blob([workerCode], { type: "application/javascript" });
|
|
77
88
|
const worker = new Worker(URL.createObjectURL(blob));
|
|
78
89
|
|
|
79
|
-
worker.onmessage = (
|
|
90
|
+
worker.onmessage = (
|
|
91
|
+
event: MessageEvent<{
|
|
92
|
+
scriptText?: string;
|
|
93
|
+
sessionId?: string;
|
|
94
|
+
error?: string;
|
|
95
|
+
}>
|
|
96
|
+
) => {
|
|
80
97
|
if (event.data.error) {
|
|
81
98
|
reject(new Error(event.data.error));
|
|
82
99
|
worker.terminate();
|
|
83
100
|
return;
|
|
84
101
|
}
|
|
85
|
-
|
|
86
|
-
|
|
102
|
+
|
|
103
|
+
const scriptText = event.data.scriptText || "";
|
|
87
104
|
window.Deflect.sessionId = event.data.sessionId || "";
|
|
88
|
-
|
|
105
|
+
|
|
89
106
|
// Cache script info
|
|
90
107
|
sessionStorage.setItem("deflect_script", scriptText);
|
|
91
108
|
localStorage.setItem("deflect_script_timestamp", Date.now().toString());
|
|
92
|
-
|
|
109
|
+
|
|
93
110
|
this.injectScript(scriptText, resolve, reject);
|
|
94
111
|
worker.terminate();
|
|
95
|
-
};
|
|
112
|
+
};
|
|
96
113
|
|
|
97
114
|
worker.onerror = function (event: ErrorEvent) {
|
|
98
115
|
reject(new Error("Worker encountered an error: " + event.message));
|
|
@@ -103,7 +120,11 @@ class Deflect {
|
|
|
103
120
|
});
|
|
104
121
|
}
|
|
105
122
|
|
|
106
|
-
private injectScript(
|
|
123
|
+
private injectScript(
|
|
124
|
+
scriptText: string,
|
|
125
|
+
resolve: () => void,
|
|
126
|
+
reject: (error: Error) => void
|
|
127
|
+
) {
|
|
107
128
|
const scriptEl = document.createElement("script");
|
|
108
129
|
scriptEl.textContent = scriptText;
|
|
109
130
|
scriptEl.onload = () => {
|
|
@@ -129,21 +150,24 @@ class Deflect {
|
|
|
129
150
|
}
|
|
130
151
|
throw new Error("Deflect script failed to load: " + String(error));
|
|
131
152
|
}
|
|
132
|
-
|
|
133
153
|
|
|
134
154
|
if (typeof window.Deflect.getToken !== "function") {
|
|
135
|
-
throw new Error(
|
|
155
|
+
throw new Error(
|
|
156
|
+
"Deflect script did not load properly or is not exposing getToken."
|
|
157
|
+
);
|
|
136
158
|
}
|
|
137
159
|
|
|
138
160
|
try {
|
|
139
|
-
return await window.Deflect.getToken(
|
|
161
|
+
return await window.Deflect.getToken(
|
|
162
|
+
window.Deflect.siteKey,
|
|
163
|
+
window.Deflect.sessionId
|
|
164
|
+
);
|
|
140
165
|
} catch (error: unknown) {
|
|
141
166
|
if (error instanceof Error) {
|
|
142
167
|
throw new Error("Error getting Deflect token: " + error.message);
|
|
143
168
|
}
|
|
144
169
|
throw new Error("Error getting Deflect token: " + String(error));
|
|
145
170
|
}
|
|
146
|
-
|
|
147
171
|
}
|
|
148
172
|
}
|
|
149
173
|
|