@deflectbot/deflect-sdk 1.2.2 → 1.2.3

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
@@ -1,108 +1,126 @@
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._prefetchedScriptText = null;
13
- this._hasUsedPrefetch = false;
14
- window.Deflect = window.Deflect || {};
15
- window.Deflect.extraArgs = window.Deflect.extraArgs || {};
16
- if (typeof window !== "undefined") {
17
- window.addEventListener("load", () => {
18
- setTimeout(() => {
19
- this._warmupScript();
20
- }, 500);
21
- });
22
- }
23
- }
24
- _warmupScript() {
25
- if (!window.Deflect.actionId)
26
- return;
27
- const nonce = Date.now().toString();
28
- const scriptUrl = `https://js.deflect.bot/main.js?action_id=${window.Deflect.actionId}&_=${nonce}`;
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(() => { });
36
- }
37
- setupReady() {
38
- let resolveFn;
39
- const promise = new Promise((resolve) => {
40
- resolveFn = resolve;
41
- });
42
- window.Deflect.ready = {
43
- promise,
44
- resolve: () => resolveFn(),
45
- };
46
- }
47
- configure(params) {
48
- if (!params.actionId) {
49
- throw new Error("actionId is required in configuration");
50
- }
51
- window.Deflect.actionId = params.actionId;
52
- }
53
- solveChallenge() {
54
- return __awaiter(this, void 0, void 0, function* () {
55
- var _a;
56
- if (!window.Deflect.actionId) {
57
- throw new Error("actionId is missing in configuration");
58
- }
59
- this.setupReady();
60
- let scriptText;
61
- let sessionId = null;
62
- if (this._prefetchedScriptText && !this._hasUsedPrefetch) {
63
- scriptText = this._prefetchedScriptText;
64
- this._hasUsedPrefetch = true;
65
- }
66
- else {
67
- const nonce = Date.now().toString();
68
- const scriptUrl = `https://js.deflect.bot/main.js?action_id=${window.Deflect.actionId}&_=${nonce}`;
69
- const response = yield fetch(scriptUrl, { cache: "no-store" });
70
- if (!response.ok) {
71
- throw new Error("Failed to fetch the Deflect script");
72
- }
73
- sessionId = response.headers.get("session_id");
74
- scriptText = yield response.text();
75
- }
76
- if (sessionId) {
77
- window.Deflect.sessionId = sessionId;
78
- }
79
- const blob = new Blob([scriptText], { type: "text/javascript" });
80
- const blobUrl = URL.createObjectURL(blob);
81
- const scriptEl = document.createElement("script");
82
- scriptEl.type = "module";
83
- scriptEl.src = blobUrl;
84
- document.head.appendChild(scriptEl);
85
- yield new Promise((resolve, reject) => {
86
- scriptEl.onload = () => resolve();
87
- scriptEl.onerror = () => reject("Failed to load the Deflect script");
88
- });
89
- yield ((_a = window.Deflect.ready) === null || _a === void 0 ? void 0 : _a.promise);
90
- if (typeof window.Deflect === "undefined" ||
91
- typeof window.Deflect.getToken !== "function") {
92
- throw new Error("Deflect script did not load properly");
93
- }
94
- let token;
95
- try {
96
- token = yield window.Deflect.getToken();
97
- }
98
- catch (error) {
99
- throw new Error(`Deflect script execution failed: ${error}`);
100
- }
101
- URL.revokeObjectURL(blobUrl);
102
- scriptEl.remove();
103
- delete window.Deflect.getToken;
104
- return token;
105
- });
106
- }
107
- }
108
- export default new Deflect();
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._prefetchedScriptText = null;
13
+ this._hasUsedPrefetch = false;
14
+ this._customScriptUrl = null;
15
+ window.Deflect = window.Deflect || {};
16
+ window.Deflect.extraArgs = window.Deflect.extraArgs || {};
17
+ if (typeof window !== "undefined") {
18
+ window.addEventListener("load", () => {
19
+ setTimeout(() => {
20
+ this._warmupScript();
21
+ }, 500);
22
+ });
23
+ }
24
+ }
25
+ _getScriptUrl() {
26
+ const nonce = Date.now().toString();
27
+ if (this._customScriptUrl) {
28
+ const separator = this._customScriptUrl.includes("?") ? "&" : "?";
29
+ return `${this._customScriptUrl}${separator}action_id=${window.Deflect.actionId}&_=${nonce}`;
30
+ }
31
+ return `https://js.deflect.bot/main.js?action_id=${window.Deflect.actionId}&_=${nonce}`;
32
+ }
33
+ _warmupScript() {
34
+ if (!window.Deflect.actionId)
35
+ return;
36
+ const scriptUrl = this._getScriptUrl();
37
+ fetch(scriptUrl, { cache: "no-store" })
38
+ .then((res) => __awaiter(this, void 0, void 0, function* () {
39
+ if (!res.ok)
40
+ return;
41
+ this._prefetchedScriptText = yield res.text();
42
+ }))
43
+ .catch(() => { });
44
+ }
45
+ _refetchScript() {
46
+ this._hasUsedPrefetch = false;
47
+ this._prefetchedScriptText = null;
48
+ setTimeout(() => {
49
+ this._warmupScript();
50
+ }, 100);
51
+ }
52
+ setupReady() {
53
+ let resolveFn;
54
+ const promise = new Promise((resolve) => {
55
+ resolveFn = resolve;
56
+ });
57
+ window.Deflect.ready = {
58
+ promise,
59
+ resolve: () => resolveFn(),
60
+ };
61
+ }
62
+ configure(params) {
63
+ if (!params.actionId) {
64
+ throw new Error("actionId is required in configuration");
65
+ }
66
+ window.Deflect.actionId = params.actionId;
67
+ if (params.scriptUrl) {
68
+ this._customScriptUrl = params.scriptUrl;
69
+ }
70
+ }
71
+ solveChallenge() {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ var _a;
74
+ if (!window.Deflect.actionId) {
75
+ throw new Error("actionId is missing in configuration");
76
+ }
77
+ this.setupReady();
78
+ let scriptText;
79
+ let sessionId = null;
80
+ if (this._prefetchedScriptText && !this._hasUsedPrefetch) {
81
+ scriptText = this._prefetchedScriptText;
82
+ this._hasUsedPrefetch = true;
83
+ }
84
+ else {
85
+ const scriptUrl = this._getScriptUrl();
86
+ const response = yield fetch(scriptUrl, { cache: "no-store" });
87
+ if (!response.ok) {
88
+ throw new Error("Failed to fetch the Deflect script");
89
+ }
90
+ sessionId = response.headers.get("session_id");
91
+ scriptText = yield response.text();
92
+ }
93
+ if (sessionId) {
94
+ window.Deflect.sessionId = sessionId;
95
+ }
96
+ const blob = new Blob([scriptText], { type: "text/javascript" });
97
+ const blobUrl = URL.createObjectURL(blob);
98
+ const scriptEl = document.createElement("script");
99
+ scriptEl.type = "module";
100
+ scriptEl.src = blobUrl;
101
+ document.head.appendChild(scriptEl);
102
+ yield new Promise((resolve, reject) => {
103
+ scriptEl.onload = () => resolve();
104
+ scriptEl.onerror = () => reject("Failed to load the Deflect script");
105
+ });
106
+ yield ((_a = window.Deflect.ready) === null || _a === void 0 ? void 0 : _a.promise);
107
+ if (typeof window.Deflect === "undefined" ||
108
+ typeof window.Deflect.getToken !== "function") {
109
+ throw new Error("Deflect script did not load properly");
110
+ }
111
+ let token;
112
+ try {
113
+ token = yield window.Deflect.getToken();
114
+ }
115
+ catch (error) {
116
+ throw new Error(`Deflect script execution failed: ${error}`);
117
+ }
118
+ this._refetchScript();
119
+ URL.revokeObjectURL(blobUrl);
120
+ scriptEl.remove();
121
+ delete window.Deflect.getToken;
122
+ return token;
123
+ });
124
+ }
125
+ }
126
+ export default new Deflect();
@@ -1,17 +1,21 @@
1
- interface DeflectConfig {
2
- actionId: string;
3
- extraArgs?: {
4
- [key: string]: string;
5
- };
6
- }
7
- declare class Deflect {
8
- private _prefetchedScriptText;
9
- private _hasUsedPrefetch;
10
- constructor();
11
- private _warmupScript;
12
- private setupReady;
13
- configure(params: DeflectConfig): void;
14
- solveChallenge(): Promise<string>;
15
- }
16
- declare const _default: Deflect;
17
- export default _default;
1
+ interface DeflectConfig {
2
+ actionId: string;
3
+ scriptUrl?: string;
4
+ extraArgs?: {
5
+ [key: string]: string;
6
+ };
7
+ }
8
+ declare class Deflect {
9
+ private _prefetchedScriptText;
10
+ private _hasUsedPrefetch;
11
+ private _customScriptUrl;
12
+ constructor();
13
+ private _getScriptUrl;
14
+ private _warmupScript;
15
+ private _refetchScript;
16
+ private setupReady;
17
+ configure(params: DeflectConfig): void;
18
+ solveChallenge(): Promise<string>;
19
+ }
20
+ declare const _default: Deflect;
21
+ export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deflectbot/deflect-sdk",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
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",