@geophrase/core 1.0.6 → 1.2.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +31 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geophrase/core",
3
- "version": "1.0.6",
3
+ "version": "1.2.0",
4
4
  "description": "The core vanilla JavaScript library for Geophrase Connect.",
5
5
  "homepage": "https://business.geophrase.com/docs",
6
6
  "repository": {
package/src/index.js CHANGED
@@ -1,8 +1,11 @@
1
1
  class Geophrase {
2
2
  constructor(options = {}) {
3
- if (!options.key || typeof document === 'undefined') {
4
- if (typeof document !== 'undefined') console.error("Geophrase: 'key' is required.");
5
- return;
3
+ // 1. Strict Constructor Validation
4
+ if (!options.key) {
5
+ throw new Error("Geophrase: 'key' is required.");
6
+ }
7
+ if (typeof document === 'undefined') {
8
+ return; // SSR safe no-op
6
9
  }
7
10
 
8
11
  this.apiKey = options.key;
@@ -21,7 +24,12 @@ class Geophrase {
21
24
 
22
25
  this._boundHandleMessage = this._handleMessage.bind(this);
23
26
 
24
- this._injectDOM();
27
+ // 2. Pre-compute the URL, but DO NOT inject or load it yet
28
+ const url = new URL(this.widgetOrigin);
29
+ url.searchParams.append('api-key', this.apiKey);
30
+ if (this.orderId) url.searchParams.append('order-id', this.orderId);
31
+ if (this.phone) url.searchParams.append('phone', this.phone);
32
+ this.widgetUrl = url.toString();
25
33
  }
26
34
 
27
35
  _injectDOM() {
@@ -56,19 +64,22 @@ class Geophrase {
56
64
  iframe.id = this.iframeId;
57
65
  iframe.allow = "geolocation";
58
66
 
59
- const url = new URL(this.widgetOrigin);
60
- url.searchParams.append('api-key', this.apiKey);
61
- if (this.orderId) url.searchParams.append('order-id', this.orderId);
62
- if (this.phone) url.searchParams.append('phone', this.phone);
63
- iframe.src = url.toString();
64
-
65
67
  overlay.appendChild(iframe);
66
68
  document.body.appendChild(overlay);
67
69
 
70
+ // Prevent duplicate listeners
71
+ window.removeEventListener('message', this._boundHandleMessage);
68
72
  window.addEventListener('message', this._boundHandleMessage);
69
73
  }
70
74
 
71
75
  open() {
76
+ // 3. Lazy-load the DOM and iframe src exactly when clicked
77
+ this._injectDOM();
78
+ const iframe = document.getElementById(this.iframeId);
79
+ if (iframe && !iframe.hasAttribute('src')) {
80
+ iframe.src = this.widgetUrl;
81
+ }
82
+
72
83
  document.body.style.overflow = 'hidden';
73
84
  document.getElementById(this.overlayId)?.classList.add('geophrase-active');
74
85
  }
@@ -78,7 +89,7 @@ class Geophrase {
78
89
  document.getElementById(this.overlayId)?.classList.remove('geophrase-active');
79
90
 
80
91
  const iframe = document.getElementById(this.iframeId);
81
- if (iframe) iframe.src = iframe.src;
92
+ if (iframe) iframe.removeAttribute('src'); // 4. Kill the background session safely
82
93
  }
83
94
 
84
95
  destroy() {
@@ -113,11 +124,17 @@ class Geophrase {
113
124
 
114
125
  if (data?.type === 'GEOPHRASE_RESOLUTION_TOKEN') {
115
126
  this.close();
127
+
128
+ // 5. Critical API Timeout protection
129
+ const controller = new AbortController();
130
+ const timeoutId = setTimeout(() => controller.abort(), 15000);
131
+
116
132
  try {
117
133
  const response = await fetch(`${this.apiBase}/business/resolve/`, {
118
134
  method: "POST",
119
135
  headers: { "X-API-Key": this.apiKey, "Content-Type": "application/json" },
120
136
  body: JSON.stringify({ token: data.token }),
137
+ signal: controller.signal
121
138
  });
122
139
 
123
140
  if (!response.ok) {
@@ -140,9 +157,11 @@ class Geophrase {
140
157
  if (this.onError) {
141
158
  this.onError({
142
159
  type: 'NETWORK_ERROR',
143
- message: error.message || 'Failed to connect to Geophrase API'
160
+ message: error.name === 'AbortError' ? 'Geophrase API request timed out' : error.message
144
161
  });
145
162
  }
163
+ } finally {
164
+ clearTimeout(timeoutId);
146
165
  }
147
166
  }
148
167
  }