@deflectbot/deflect-sdk 1.1.4 → 1.1.6
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 +20 -4
- package/eslint.config.mjs +11 -11
- package/package.json +1 -1
- package/src/index.ts +18 -5
package/dist/index.js
CHANGED
|
@@ -10,9 +10,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
class Deflect {
|
|
11
11
|
constructor() {
|
|
12
12
|
window.Deflect = window.Deflect || {};
|
|
13
|
-
window.Deflect.siteKey = window.Deflect.siteKey || "";
|
|
14
|
-
window.Deflect.sessionId = window.Deflect.sessionId || "";
|
|
15
13
|
window.Deflect.extraArgs = window.Deflect.extraArgs || {};
|
|
14
|
+
// Setup readiness handshake
|
|
15
|
+
if (!window.Deflect.ready) {
|
|
16
|
+
let resolveFn;
|
|
17
|
+
const promise = new Promise((resolve) => {
|
|
18
|
+
resolveFn = resolve;
|
|
19
|
+
});
|
|
20
|
+
window.Deflect.ready = {
|
|
21
|
+
promise,
|
|
22
|
+
resolve: () => resolveFn(),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
16
25
|
}
|
|
17
26
|
configure(params) {
|
|
18
27
|
if (!params.siteKey) {
|
|
@@ -22,6 +31,7 @@ class Deflect {
|
|
|
22
31
|
}
|
|
23
32
|
solveChallenge() {
|
|
24
33
|
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
var _a;
|
|
25
35
|
if (!window.Deflect.siteKey) {
|
|
26
36
|
throw new Error("API key (siteKey) is missing in configuration");
|
|
27
37
|
}
|
|
@@ -34,10 +44,16 @@ class Deflect {
|
|
|
34
44
|
if (sessionId) {
|
|
35
45
|
window.Deflect.sessionId = sessionId;
|
|
36
46
|
}
|
|
37
|
-
const scriptText = yield response.text();
|
|
38
47
|
const scriptEl = document.createElement("script");
|
|
39
|
-
scriptEl.
|
|
48
|
+
scriptEl.type = "module";
|
|
49
|
+
const blob = new Blob([yield response.text()], { type: "text/javascript" });
|
|
50
|
+
scriptEl.src = URL.createObjectURL(blob);
|
|
40
51
|
document.head.appendChild(scriptEl);
|
|
52
|
+
yield new Promise((resolve, reject) => {
|
|
53
|
+
scriptEl.onload = () => resolve();
|
|
54
|
+
scriptEl.onerror = () => reject("Failed to load the Deflect script");
|
|
55
|
+
});
|
|
56
|
+
yield ((_a = window.Deflect.ready) === null || _a === void 0 ? void 0 : _a.promise);
|
|
41
57
|
if (typeof window.Deflect === "undefined" ||
|
|
42
58
|
typeof window.Deflect.getToken !== "function") {
|
|
43
59
|
throw new Error("Deflect script did not load properly");
|
package/eslint.config.mjs
CHANGED
|
@@ -1,12 +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,
|
|
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
12
|
];
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -6,9 +6,19 @@ interface DeflectConfig {
|
|
|
6
6
|
class Deflect {
|
|
7
7
|
constructor() {
|
|
8
8
|
window.Deflect = window.Deflect || {};
|
|
9
|
-
window.Deflect.siteKey = window.Deflect.siteKey || "";
|
|
10
|
-
window.Deflect.sessionId = window.Deflect.sessionId || "";
|
|
11
9
|
window.Deflect.extraArgs = window.Deflect.extraArgs || {};
|
|
10
|
+
|
|
11
|
+
// Setup readiness handshake
|
|
12
|
+
if (!window.Deflect.ready) {
|
|
13
|
+
let resolveFn: () => void;
|
|
14
|
+
const promise = new Promise<void>((resolve) => {
|
|
15
|
+
resolveFn = resolve;
|
|
16
|
+
});
|
|
17
|
+
window.Deflect.ready = {
|
|
18
|
+
promise,
|
|
19
|
+
resolve: () => resolveFn(),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
12
22
|
}
|
|
13
23
|
|
|
14
24
|
configure(params: DeflectConfig): void {
|
|
@@ -24,11 +34,12 @@ class Deflect {
|
|
|
24
34
|
}
|
|
25
35
|
|
|
26
36
|
const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}`;
|
|
27
|
-
|
|
28
37
|
const response = await fetch(scriptUrl);
|
|
38
|
+
|
|
29
39
|
if (!response.ok) {
|
|
30
40
|
throw new Error("Failed to fetch the Deflect script");
|
|
31
41
|
}
|
|
42
|
+
|
|
32
43
|
const sessionId = response.headers.get("session_id");
|
|
33
44
|
if (sessionId) {
|
|
34
45
|
window.Deflect.sessionId = sessionId;
|
|
@@ -36,9 +47,9 @@ class Deflect {
|
|
|
36
47
|
|
|
37
48
|
const scriptEl = document.createElement("script");
|
|
38
49
|
scriptEl.type = "module";
|
|
50
|
+
|
|
39
51
|
const blob = new Blob([await response.text()], { type: "text/javascript" });
|
|
40
|
-
|
|
41
|
-
scriptEl.src = blobUrl;
|
|
52
|
+
scriptEl.src = URL.createObjectURL(blob);
|
|
42
53
|
|
|
43
54
|
document.head.appendChild(scriptEl);
|
|
44
55
|
|
|
@@ -46,6 +57,8 @@ class Deflect {
|
|
|
46
57
|
scriptEl.onload = () => resolve();
|
|
47
58
|
scriptEl.onerror = () => reject("Failed to load the Deflect script");
|
|
48
59
|
});
|
|
60
|
+
|
|
61
|
+
await window.Deflect.ready?.promise;
|
|
49
62
|
|
|
50
63
|
if (
|
|
51
64
|
typeof window.Deflect === "undefined" ||
|