@polyguard/sdk 1.0.3 → 1.0.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/sdk.js CHANGED
@@ -11006,13 +11006,13 @@
11006
11006
  <button id="polyguard-modal-cancel" style="background: #7be7c2; color: #222; font-weight: 600; border-radius: 8px; border: none; padding: 10px 32px; font-size: 16px; cursor: pointer; margin-top: 8px; width: 100%; max-width: 240px;">Cancel</button>
11007
11007
  </div>
11008
11008
  `;
11009
- document.body.appendChild(modal);
11010
11009
  function cleanup() {
11011
11010
  if (modal.parentNode) modal.parentNode.removeChild(modal);
11012
11011
  }
11013
11012
  return new Promise(async (resolve, reject) => {
11014
11013
  let ws = null;
11015
11014
  let closed = false;
11015
+ let modalShown = false;
11016
11016
  function showError(msg) {
11017
11017
  const errDiv = modal.querySelector("#polyguard-error");
11018
11018
  if (errDiv) {
@@ -11030,8 +11030,11 @@
11030
11030
  cleanup();
11031
11031
  reject(new Error("User cancelled"));
11032
11032
  }
11033
- modal.querySelector("#polyguard-modal-close").onclick = handleClose;
11034
- modal.querySelector("#polyguard-modal-cancel").onclick = handleClose;
11033
+ function showModal() {
11034
+ document.body.appendChild(modal);
11035
+ modal.querySelector("#polyguard-modal-close").onclick = handleClose;
11036
+ modal.querySelector("#polyguard-modal-cancel").onclick = handleClose;
11037
+ }
11035
11038
  try {
11036
11039
  clearError();
11037
11040
  const wsProtocol = window.location.protocol === "https:" ? "wss" : "ws";
@@ -11063,7 +11066,15 @@
11063
11066
  window.location.assign(data.url);
11064
11067
  return;
11065
11068
  } else if (data && data.qr_url) {
11066
- console.log("qr_url", data.qr_url);
11069
+ if (!modalShown) {
11070
+ showModal();
11071
+ modalShown = true;
11072
+ }
11073
+ const pcre = data.qr_url.match(/pcre=([^&]*)/);
11074
+ console.log("pcre", pcre);
11075
+ if (pcre) {
11076
+ ws.send(JSON.stringify({ type: "pong", seq: pcre[1] }));
11077
+ }
11067
11078
  const qrDiv = modal.querySelector("#polyguard-qr");
11068
11079
  if (!qrDiv) return;
11069
11080
  const isMobile = /Mobi|Android/i.test(navigator.userAgent);
@@ -11080,9 +11091,12 @@
11080
11091
  instructionList.children[1].textContent = "If you do not have the Polyguard app, you will be redirected to download it.";
11081
11092
  }
11082
11093
  } else {
11094
+ const startTime = Date.now();
11095
+ console.log("time before qr code", startTime);
11083
11096
  import_qrcode.default.toString(data.qr_url, { type: "svg" }, (err, svg) => {
11084
11097
  if (!err) qrDiv.innerHTML = svg;
11085
11098
  });
11099
+ console.log("time to generate qr code", Date.now() - startTime);
11086
11100
  }
11087
11101
  return;
11088
11102
  } else if (data && data.jwt) {
@@ -11093,14 +11107,16 @@
11093
11107
  } else if (data && data.status) {
11094
11108
  return;
11095
11109
  } else {
11110
+ console.error("Unknown message type from server", data);
11096
11111
  showError(`Unknown message type from server`);
11097
11112
  }
11098
11113
  } catch (e) {
11114
+ console.error("Invalid message format from server", e);
11099
11115
  showError("Invalid message format from server");
11100
- ws.close();
11101
11116
  }
11102
11117
  });
11103
11118
  ws.addEventListener("error", () => {
11119
+ console.error("WebSocket error");
11104
11120
  showError("WebSocket error");
11105
11121
  });
11106
11122
  ws.addEventListener("close", () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polyguard/sdk",
3
- "version": "1.0.3",
3
+ "version": "1.0.7",
4
4
  "main": "dist/sdk.js",
5
5
  "module": "dist/sdk.js",
6
6
  "browser": "dist/sdk.js",
@@ -64,15 +64,18 @@ export class PolyguardWebsocketClientImpl {
64
64
  <button id="polyguard-modal-cancel" style="background: #7be7c2; color: #222; font-weight: 600; border-radius: 8px; border: none; padding: 10px 32px; font-size: 16px; cursor: pointer; margin-top: 8px; width: 100%; max-width: 240px;">Cancel</button>
65
65
  </div>
66
66
  `;
67
- document.body.appendChild(modal);
67
+
68
68
  // Helper to cleanup modal
69
69
  function cleanup() {
70
70
  if (modal.parentNode) modal.parentNode.removeChild(modal);
71
71
  }
72
+
72
73
  // Promise for JWT
73
74
  return new Promise(async (resolve, reject) => {
74
75
  let ws = null;
75
76
  let closed = false;
77
+ let modalShown = false;
78
+
76
79
  function showError(msg) {
77
80
  const errDiv = modal.querySelector('#polyguard-error');
78
81
  if (errDiv) {
@@ -91,8 +94,15 @@ export class PolyguardWebsocketClientImpl {
91
94
  cleanup();
92
95
  reject(new Error('User cancelled'));
93
96
  }
94
- modal.querySelector('#polyguard-modal-close').onclick = handleClose;
95
- modal.querySelector('#polyguard-modal-cancel').onclick = handleClose;
97
+
98
+ // Helper to show modal (only called when QR code is received)
99
+ function showModal() {
100
+ document.body.appendChild(modal);
101
+ // Set up close/cancel handlers after modal is in DOM
102
+ modal.querySelector('#polyguard-modal-close').onclick = handleClose;
103
+ modal.querySelector('#polyguard-modal-cancel').onclick = handleClose;
104
+ }
105
+
96
106
  // Start ticket/ws flow
97
107
  try {
98
108
  clearError();
@@ -127,7 +137,17 @@ export class PolyguardWebsocketClientImpl {
127
137
  window.location.assign(data.url);
128
138
  return;
129
139
  } else if (data && data.qr_url) {
130
- console.log('qr_url', data.qr_url);
140
+ // Show modal only when QR code is received
141
+ if (!modalShown) {
142
+ showModal();
143
+ modalShown = true;
144
+ }
145
+
146
+ const pcre = data.qr_url.match(/pcre=([^&]*)/);
147
+ console.log('pcre', pcre);
148
+ if (pcre) {
149
+ ws.send(JSON.stringify({ type: 'pong', seq: pcre[1] }));
150
+ }
131
151
 
132
152
  const qrDiv = modal.querySelector('#polyguard-qr');
133
153
  if (!qrDiv) return;
@@ -150,10 +170,13 @@ export class PolyguardWebsocketClientImpl {
150
170
  instructionList.children[1].textContent = 'If you do not have the Polyguard app, you will be redirected to download it.';
151
171
  }
152
172
  } else {
173
+ const startTime = Date.now();
174
+ console.log('time before qr code', startTime);
153
175
  // For desktop, display the QR code
154
176
  QRCode.toString(data.qr_url, { type: 'svg' }, (err, svg) => {
155
177
  if (!err) qrDiv.innerHTML = svg;
156
178
  });
179
+ console.log('time to generate qr code', Date.now() - startTime);
157
180
  }
158
181
  return;
159
182
  } else if (data && data.jwt) {
@@ -165,14 +188,17 @@ export class PolyguardWebsocketClientImpl {
165
188
  // ignore
166
189
  return;
167
190
  } else {
191
+ console.error('Unknown message type from server', data);
168
192
  showError(`Unknown message type from server`);
169
193
  }
170
194
  } catch (e) {
195
+ console.error('Invalid message format from server', e);
171
196
  showError('Invalid message format from server');
172
- ws.close();
197
+ // ws.close();
173
198
  }
174
199
  });
175
200
  ws.addEventListener('error', () => {
201
+ console.error('WebSocket error');
176
202
  showError('WebSocket error');
177
203
  });
178
204
  ws.addEventListener('close', () => {