@deflectbot/deflect-sdk 1.1.5 → 1.1.7

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
@@ -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,10 +31,12 @@ 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
  }
28
- const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}`;
38
+ const nonce = Date.now().toString();
39
+ const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
29
40
  const response = yield fetch(scriptUrl);
30
41
  if (!response.ok) {
31
42
  throw new Error("Failed to fetch the Deflect script");
@@ -37,13 +48,13 @@ class Deflect {
37
48
  const scriptEl = document.createElement("script");
38
49
  scriptEl.type = "module";
39
50
  const blob = new Blob([yield response.text()], { type: "text/javascript" });
40
- const blobUrl = URL.createObjectURL(blob);
41
- scriptEl.src = blobUrl;
51
+ scriptEl.src = URL.createObjectURL(blob);
42
52
  document.head.appendChild(scriptEl);
43
53
  yield new Promise((resolve, reject) => {
44
54
  scriptEl.onload = () => resolve();
45
55
  scriptEl.onerror = () => reject("Failed to load the Deflect script");
46
56
  });
57
+ yield ((_a = window.Deflect.ready) === null || _a === void 0 ? void 0 : _a.promise);
47
58
  if (typeof window.Deflect === "undefined" ||
48
59
  typeof window.Deflect.getToken !== "function") {
49
60
  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
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@deflectbot/deflect-sdk",
3
- "version": "1.1.5",
4
- "description": "",
3
+ "version": "1.1.7",
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",
7
7
  "scripts": {
8
8
  "build": "tsc"
9
9
  },
10
- "author": "Fredrik Rafn",
10
+ "author": "Deflect",
11
11
  "license": "MIT",
12
12
  "devDependencies": {
13
13
  "@eslint/js": "^9.22.0",
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 {
@@ -23,12 +33,14 @@ class Deflect {
23
33
  throw new Error("API key (siteKey) is missing in configuration");
24
34
  }
25
35
 
26
- const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}`;
27
-
36
+ const nonce = Date.now().toString();
37
+ const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
28
38
  const response = await fetch(scriptUrl);
39
+
29
40
  if (!response.ok) {
30
41
  throw new Error("Failed to fetch the Deflect script");
31
42
  }
43
+
32
44
  const sessionId = response.headers.get("session_id");
33
45
  if (sessionId) {
34
46
  window.Deflect.sessionId = sessionId;
@@ -36,9 +48,9 @@ class Deflect {
36
48
 
37
49
  const scriptEl = document.createElement("script");
38
50
  scriptEl.type = "module";
51
+
39
52
  const blob = new Blob([await response.text()], { type: "text/javascript" });
40
- const blobUrl = URL.createObjectURL(blob);
41
- scriptEl.src = blobUrl;
53
+ scriptEl.src = URL.createObjectURL(blob);
42
54
 
43
55
  document.head.appendChild(scriptEl);
44
56
 
@@ -46,6 +58,8 @@ class Deflect {
46
58
  scriptEl.onload = () => resolve();
47
59
  scriptEl.onerror = () => reject("Failed to load the Deflect script");
48
60
  });
61
+
62
+ await window.Deflect.ready?.promise;
49
63
 
50
64
  if (
51
65
  typeof window.Deflect === "undefined" ||