@deflectbot/deflect-sdk 1.1.12 → 1.1.14
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 +33 -10
- package/dist/types/index.d.ts +2 -0
- package/package.json +1 -1
- package/src/index.ts +34 -10
package/dist/index.js
CHANGED
|
@@ -9,6 +9,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
class Deflect {
|
|
11
11
|
constructor() {
|
|
12
|
+
this._prefetchedScriptText = null;
|
|
13
|
+
this._hasUsedPrefetch = false;
|
|
12
14
|
window.Deflect = window.Deflect || {};
|
|
13
15
|
window.Deflect.extraArgs = window.Deflect.extraArgs || {};
|
|
14
16
|
if (typeof window !== "undefined") {
|
|
@@ -24,7 +26,13 @@ class Deflect {
|
|
|
24
26
|
return;
|
|
25
27
|
const nonce = Date.now().toString();
|
|
26
28
|
const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
|
|
27
|
-
fetch(scriptUrl, { cache: "no-store"
|
|
29
|
+
fetch(scriptUrl, { cache: "no-store" })
|
|
30
|
+
.then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
if (!res.ok)
|
|
32
|
+
return;
|
|
33
|
+
this._prefetchedScriptText = yield res.text();
|
|
34
|
+
}))
|
|
35
|
+
.catch(() => { });
|
|
28
36
|
}
|
|
29
37
|
setupReady() {
|
|
30
38
|
let resolveFn;
|
|
@@ -50,18 +58,26 @@ class Deflect {
|
|
|
50
58
|
throw new Error("API key (siteKey) is missing in configuration");
|
|
51
59
|
}
|
|
52
60
|
this.setupReady();
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
61
|
+
let scriptText;
|
|
62
|
+
let sessionId = null;
|
|
63
|
+
if (this._prefetchedScriptText && !this._hasUsedPrefetch) {
|
|
64
|
+
scriptText = this._prefetchedScriptText;
|
|
65
|
+
this._hasUsedPrefetch = true;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const nonce = Date.now().toString();
|
|
69
|
+
const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
|
|
70
|
+
const response = yield fetch(scriptUrl, { cache: "no-store" });
|
|
71
|
+
if (!response.ok) {
|
|
72
|
+
throw new Error("Failed to fetch the Deflect script");
|
|
73
|
+
}
|
|
74
|
+
sessionId = response.headers.get("session_id");
|
|
75
|
+
scriptText = yield response.text();
|
|
58
76
|
}
|
|
59
|
-
const sessionId = response.headers.get("session_id");
|
|
60
77
|
if (sessionId) {
|
|
61
78
|
window.Deflect.sessionId = sessionId;
|
|
62
79
|
}
|
|
63
|
-
const
|
|
64
|
-
const blob = new Blob([text], { type: "text/javascript" });
|
|
80
|
+
const blob = new Blob([scriptText], { type: "text/javascript" });
|
|
65
81
|
const blobUrl = URL.createObjectURL(blob);
|
|
66
82
|
const scriptEl = document.createElement("script");
|
|
67
83
|
scriptEl.type = "module";
|
|
@@ -76,7 +92,14 @@ class Deflect {
|
|
|
76
92
|
typeof window.Deflect.getToken !== "function") {
|
|
77
93
|
throw new Error("Deflect script did not load properly");
|
|
78
94
|
}
|
|
79
|
-
|
|
95
|
+
let token;
|
|
96
|
+
try {
|
|
97
|
+
token = yield window.Deflect.getToken();
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
console.error("Error during Deflect script execution:", error);
|
|
101
|
+
throw new Error("Deflect script execution failed");
|
|
102
|
+
}
|
|
80
103
|
URL.revokeObjectURL(blobUrl);
|
|
81
104
|
scriptEl.remove();
|
|
82
105
|
delete window.Deflect.getToken;
|
package/dist/types/index.d.ts
CHANGED
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -4,6 +4,9 @@ interface DeflectConfig {
|
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
class Deflect {
|
|
7
|
+
private _prefetchedScriptText: string | null = null;
|
|
8
|
+
private _hasUsedPrefetch = false;
|
|
9
|
+
|
|
7
10
|
constructor() {
|
|
8
11
|
window.Deflect = window.Deflect || {};
|
|
9
12
|
window.Deflect.extraArgs = window.Deflect.extraArgs || {};
|
|
@@ -22,7 +25,12 @@ class Deflect {
|
|
|
22
25
|
|
|
23
26
|
const nonce = Date.now().toString();
|
|
24
27
|
const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
|
|
25
|
-
fetch(scriptUrl, { cache: "no-store"
|
|
28
|
+
fetch(scriptUrl, { cache: "no-store" })
|
|
29
|
+
.then(async (res) => {
|
|
30
|
+
if (!res.ok) return;
|
|
31
|
+
this._prefetchedScriptText = await res.text();
|
|
32
|
+
})
|
|
33
|
+
.catch(() => {});
|
|
26
34
|
}
|
|
27
35
|
|
|
28
36
|
private setupReady(): void {
|
|
@@ -51,21 +59,30 @@ class Deflect {
|
|
|
51
59
|
|
|
52
60
|
this.setupReady();
|
|
53
61
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
62
|
+
let scriptText: string;
|
|
63
|
+
let sessionId: string | null = null;
|
|
64
|
+
|
|
65
|
+
if (this._prefetchedScriptText && !this._hasUsedPrefetch) {
|
|
66
|
+
scriptText = this._prefetchedScriptText;
|
|
67
|
+
this._hasUsedPrefetch = true;
|
|
68
|
+
} else {
|
|
69
|
+
const nonce = Date.now().toString();
|
|
70
|
+
const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
|
|
71
|
+
const response = await fetch(scriptUrl, { cache: "no-store" });
|
|
57
72
|
|
|
58
|
-
|
|
59
|
-
|
|
73
|
+
if (!response.ok) {
|
|
74
|
+
throw new Error("Failed to fetch the Deflect script");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
sessionId = response.headers.get("session_id");
|
|
78
|
+
scriptText = await response.text();
|
|
60
79
|
}
|
|
61
80
|
|
|
62
|
-
const sessionId = response.headers.get("session_id");
|
|
63
81
|
if (sessionId) {
|
|
64
82
|
window.Deflect.sessionId = sessionId;
|
|
65
83
|
}
|
|
66
84
|
|
|
67
|
-
const
|
|
68
|
-
const blob = new Blob([text], { type: "text/javascript" });
|
|
85
|
+
const blob = new Blob([scriptText], { type: "text/javascript" });
|
|
69
86
|
const blobUrl = URL.createObjectURL(blob);
|
|
70
87
|
|
|
71
88
|
const scriptEl = document.createElement("script");
|
|
@@ -88,7 +105,14 @@ class Deflect {
|
|
|
88
105
|
throw new Error("Deflect script did not load properly");
|
|
89
106
|
}
|
|
90
107
|
|
|
91
|
-
|
|
108
|
+
let token: string;
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
token = await window.Deflect.getToken();
|
|
112
|
+
} catch (error) {
|
|
113
|
+
console.error("Error during Deflect script execution:", error);
|
|
114
|
+
throw new Error("Deflect script execution failed");
|
|
115
|
+
}
|
|
92
116
|
|
|
93
117
|
URL.revokeObjectURL(blobUrl);
|
|
94
118
|
scriptEl.remove();
|