@deflectbot/deflect-sdk 1.1.12 → 1.1.13

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 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", method: "GET" }).catch(() => { });
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
- const nonce = Date.now().toString();
54
- const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
55
- const response = yield fetch(scriptUrl, { cache: "no-store" });
56
- if (!response.ok) {
57
- throw new Error("Failed to fetch the Deflect script");
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 text = yield response.text();
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";
@@ -5,6 +5,8 @@ interface DeflectConfig {
5
5
  };
6
6
  }
7
7
  declare class Deflect {
8
+ private _prefetchedScriptText;
9
+ private _hasUsedPrefetch;
8
10
  constructor();
9
11
  private _warmupScript;
10
12
  private setupReady;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deflectbot/deflect-sdk",
3
- "version": "1.1.12",
3
+ "version": "1.1.13",
4
4
  "description": "SDK for deflect.bot - Use it for seamless captcha integration on any website.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",
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", method: "GET" }).catch(() => {});
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
- const nonce = Date.now().toString();
55
- const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
56
- const response = await fetch(scriptUrl, { cache: "no-store" });
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" });
72
+
73
+ if (!response.ok) {
74
+ throw new Error("Failed to fetch the Deflect script");
75
+ }
57
76
 
58
- if (!response.ok) {
59
- throw new Error("Failed to fetch the Deflect script");
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 text = await response.text();
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");