@vuu-ui/vuu-data-remote 0.13.106 → 0.13.108

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 (47) hide show
  1. package/cjs/ConnectionManager.js +24 -23
  2. package/cjs/ConnectionManager.js.map +1 -1
  3. package/cjs/DedicatedWorker.js +6 -1
  4. package/cjs/DedicatedWorker.js.map +1 -1
  5. package/cjs/WebSocketConnection.js +17 -11
  6. package/cjs/WebSocketConnection.js.map +1 -1
  7. package/cjs/authenticate.js +0 -15
  8. package/cjs/authenticate.js.map +1 -1
  9. package/cjs/index.js +0 -9
  10. package/cjs/index.js.map +1 -1
  11. package/cjs/inlined-worker.js +343 -259
  12. package/cjs/inlined-worker.js.map +1 -1
  13. package/esm/ConnectionManager.js +26 -25
  14. package/esm/ConnectionManager.js.map +1 -1
  15. package/esm/DedicatedWorker.js +6 -1
  16. package/esm/DedicatedWorker.js.map +1 -1
  17. package/esm/WebSocketConnection.js +17 -11
  18. package/esm/WebSocketConnection.js.map +1 -1
  19. package/esm/authenticate.js +1 -15
  20. package/esm/authenticate.js.map +1 -1
  21. package/esm/index.js +1 -4
  22. package/esm/index.js.map +1 -1
  23. package/esm/inlined-worker.js +343 -259
  24. package/esm/inlined-worker.js.map +1 -1
  25. package/package.json +7 -7
  26. package/types/ConnectionManager.d.ts +6 -5
  27. package/types/DedicatedWorker.d.ts +2 -1
  28. package/types/WebSocketConnection.d.ts +38 -19
  29. package/types/authenticate.d.ts +0 -4
  30. package/types/index.d.ts +2 -5
  31. package/types/inlined-worker.d.ts +1 -1
  32. package/types/server-proxy/server-proxy.d.ts +2 -2
  33. package/cjs/LostConnectionHandler.js +0 -80
  34. package/cjs/LostConnectionHandler.js.map +0 -1
  35. package/cjs/VuuAuthProvider.js +0 -87
  36. package/cjs/VuuAuthProvider.js.map +0 -1
  37. package/cjs/VuuAuthenticator.js +0 -50
  38. package/cjs/VuuAuthenticator.js.map +0 -1
  39. package/esm/LostConnectionHandler.js +0 -76
  40. package/esm/LostConnectionHandler.js.map +0 -1
  41. package/esm/VuuAuthProvider.js +0 -85
  42. package/esm/VuuAuthProvider.js.map +0 -1
  43. package/esm/VuuAuthenticator.js +0 -47
  44. package/esm/VuuAuthenticator.js.map +0 -1
  45. package/types/LostConnectionHandler.d.ts +0 -28
  46. package/types/VuuAuthProvider.d.ts +0 -38
  47. package/types/VuuAuthenticator.d.ts +0 -21
@@ -407,18 +407,7 @@ var MENU_RPC_TYPES = [
407
407
  "VIEW_PORT_MENU_ROW_RPC",
408
408
  "VIEW_PORT_MENU_CELL_RPC"
409
409
  ];
410
- var INVALID_SESSION = "Invalid session";
411
- var SESSION_LIMIT_EXCEEDED = "User session limit exceeded";
412
- var INVALID_TOKEN = "Invalid token";
413
- var TOKEN_EXPIRED = "Token has expired";
414
- var InvalidLoginMessages = [
415
- INVALID_SESSION,
416
- SESSION_LIMIT_EXCEEDED,
417
- INVALID_TOKEN,
418
- TOKEN_EXPIRED
419
- ];
420
- var isLoginErrorMessage = (message) => typeof message === "string" && InvalidLoginMessages.includes(message);
421
- var isSelectRequest = (message) => message && typeof message === "object" && "type" in message && (message.type === "SELECT_ROW" || message.type === "DESELECT_ROW" || message.type === "SELECT_ROW_RANGE" || message.type === "SELECT_ALL" || message.type === "DESELECT_ALL");
410
+ var isSelectRequest = (message) => "type" in message && (message.type === "SELECT_ROW" || message.type === "DESELECT_ROW" || message.type === "SELECT_ROW_RANGE" || message.type === "SELECT_ALL" || message.type === "DESELECT_ALL");
422
411
  var isRpcServiceRequest = (message) => message.type === "RPC_REQUEST";
423
412
  var hasViewPortContext = (message) => message.context.type === "VIEWPORT_CONTEXT";
424
413
  var isVuuMenuRpcRequest = (message) => MENU_RPC_TYPES.includes(message["type"]);
@@ -1450,195 +1439,9 @@ var toClientRowTree = ({ rowIndex, rowKey, sel: isSelected, data, ts }, keys) =>
1450
1439
  ].concat(rest);
1451
1440
  };
1452
1441
 
1453
- // src/WebSocketConnection.ts
1454
- var { debug: debug3, debugEnabled: debugEnabled3, info: info2 } = logger("WebSocketConnection");
1455
- var isLoginRejectedMessage = (message) => message !== null && "type" in message && message.type === "LOGIN_REJECTED";
1456
- var DEFAULT_CONNECTION_TIMEOUT = 1e4;
1457
- var parseWebSocketMessage = (message) => {
1458
- try {
1459
- return JSON.parse(message);
1460
- } catch (e) {
1461
- throw Error(\`Error parsing JSON response from server \${message}\`);
1462
- }
1463
- };
1464
- var _callback, _confirmedOpen, _connectionPhase, _connectionStatus, _connectionTimeout, _deferredOpen, _protocols, _url, _ws;
1465
- var WebSocketConnection = class extends EventEmitter {
1466
- constructor({
1467
- callback,
1468
- connectionTimeout = DEFAULT_CONNECTION_TIMEOUT,
1469
- protocols,
1470
- url
1471
- }) {
1472
- super();
1473
- __privateAdd(this, _callback);
1474
- /**
1475
- We are not confirmedOpen until we receive the first message from the
1476
- server. If we get an unexpected close event before that, we consider
1477
- the reconnect attempts as still within the connection phase, not true
1478
- reconnection. This can happen e.g. when connecting to remote host via
1479
- a proxy.
1480
- */
1481
- __privateAdd(this, _confirmedOpen, false);
1482
- __privateAdd(this, _connectionPhase, "initial-connection");
1483
- __privateAdd(this, _connectionStatus, "closed");
1484
- __privateAdd(this, _connectionTimeout);
1485
- __privateAdd(this, _deferredOpen);
1486
- __privateAdd(this, _protocols);
1487
- __privateAdd(this, _url);
1488
- __privateAdd(this, _ws);
1489
- __publicField(this, "receive", (evt) => {
1490
- if (isLoginErrorMessage(evt.data)) {
1491
- console.log(\`[WebSocketConnection] closed because of login issue\`);
1492
- if (__privateGet(this, _deferredOpen)) {
1493
- console.log(\`... and qwe have a deferred connection\`);
1494
- }
1495
- __privateGet(this, _callback).call(this, {
1496
- type: "LOGIN_REJECTED",
1497
- reason: evt.data
1498
- });
1499
- this.close(evt.data);
1500
- } else {
1501
- const vuuMessageFromServer = parseWebSocketMessage(evt.data);
1502
- if (debugEnabled3) {
1503
- if (vuuMessageFromServer.body.type !== "HB") {
1504
- debug3(\`\${vuuMessageFromServer.body.type}\`);
1505
- if (vuuMessageFromServer.body.type === "CHANGE_VP_SUCCESS") {
1506
- debug3(JSON.stringify(vuuMessageFromServer.body));
1507
- }
1508
- }
1509
- }
1510
- __privateGet(this, _callback).call(this, vuuMessageFromServer);
1511
- if (!this.confirmedOpen) {
1512
- if (vuuMessageFromServer.body.type === "LOGIN_SUCCESS") {
1513
- this.connectionStatus = __privateGet(this, _connectionPhase) === "initial-connection" ? "connected" : "reconnected";
1514
- this.confirmedOpen = true;
1515
- }
1516
- }
1517
- }
1518
- });
1519
- __publicField(this, "send", (msg) => {
1520
- var _a;
1521
- if (msg.body.type === "CHANGE_VP_RANGE") {
1522
- info2 == null ? void 0 : info2(
1523
- \`CHANGE_VP_RANGE<#\${msg.requestId}> \${msg.body.from}-\${msg.body.to}\`
1524
- );
1525
- }
1526
- (_a = __privateGet(this, _ws)) == null ? void 0 : _a.send(JSON.stringify(msg));
1527
- });
1528
- __privateSet(this, _callback, callback);
1529
- __privateSet(this, _connectionTimeout, connectionTimeout);
1530
- __privateSet(this, _url, url);
1531
- __privateSet(this, _protocols, protocols);
1532
- }
1533
- get connectionTimeout() {
1534
- return __privateGet(this, _connectionTimeout);
1535
- }
1536
- get protocols() {
1537
- return __privateGet(this, _protocols);
1538
- }
1539
- get isClosed() {
1540
- return __privateGet(this, _connectionStatus) === "closed";
1541
- }
1542
- get isDisconnected() {
1543
- return __privateGet(this, _connectionStatus) === "disconnected";
1544
- }
1545
- get connectionPhase() {
1546
- return __privateGet(this, _connectionPhase);
1547
- }
1548
- get connectionStatus() {
1549
- return __privateGet(this, _connectionStatus);
1550
- }
1551
- set connectionStatus(connectionStatus) {
1552
- if (connectionStatus !== "connecting" && connectionStatus !== "reconnecting") {
1553
- __privateSet(this, _connectionStatus, connectionStatus);
1554
- this.emit("connection-status", __privateGet(this, _connectionStatus));
1555
- }
1556
- }
1557
- get confirmedOpen() {
1558
- return __privateGet(this, _confirmedOpen);
1559
- }
1560
- /**
1561
- * We are 'confirmedOpen' when we see the first message transmitted
1562
- * from the server. This ensures that even if we have one or more
1563
- * proxies in our route to the endPoint, all connections have been
1564
- * opened successfully.
1565
- * First time in here (on our initial successful connection) we switch
1566
- * from 'connect' phase to 'reconnect' phase. We may have different
1567
- * retry configurations for these two phases.
1568
- */
1569
- set confirmedOpen(confirmedOpen) {
1570
- __privateSet(this, _confirmedOpen, confirmedOpen);
1571
- if (confirmedOpen && __privateGet(this, _connectionPhase) === "initial-connection") {
1572
- __privateSet(this, _connectionPhase, "post-disconnect-reconnection");
1573
- }
1574
- }
1575
- get url() {
1576
- return __privateGet(this, _url);
1577
- }
1578
- async openWebSocket() {
1579
- var _a;
1580
- const initialConnect = __privateGet(this, _connectionPhase) === "initial-connection";
1581
- if (__privateGet(this, _deferredOpen) === void 0) {
1582
- __privateSet(this, _deferredOpen, new DeferredPromise());
1583
- }
1584
- const { connectionTimeout, protocols, url } = this;
1585
- __privateSet(this, _connectionStatus, initialConnect ? "connecting" : "reconnecting");
1586
- const timer = setTimeout(() => {
1587
- throw Error(
1588
- \`Failed to open WebSocket connection to \${url}, timed out after \${connectionTimeout}ms\`
1589
- );
1590
- }, connectionTimeout);
1591
- const ws = __privateSet(this, _ws, new WebSocket(url, protocols));
1592
- ws.onopen = () => {
1593
- this.connectionStatus = "websocket-open";
1594
- clearTimeout(timer);
1595
- if (__privateGet(this, _deferredOpen)) {
1596
- __privateGet(this, _deferredOpen).resolve(void 0);
1597
- __privateSet(this, _deferredOpen, void 0);
1598
- }
1599
- };
1600
- ws.onerror = () => {
1601
- clearTimeout(timer);
1602
- };
1603
- ws.onclose = () => {
1604
- if (!this.isClosed) {
1605
- this.confirmedOpen = false;
1606
- this.connectionStatus = "disconnected";
1607
- this.close("failure");
1608
- }
1609
- };
1610
- ws.onmessage = (evt) => {
1611
- this.receive(evt);
1612
- };
1613
- return (_a = __privateGet(this, _deferredOpen)) == null ? void 0 : _a.promise;
1614
- }
1615
- close(reason = "shutdown") {
1616
- var _a;
1617
- this.connectionStatus = "closed";
1618
- if (reason === "failure") {
1619
- if (__privateGet(this, _deferredOpen)) {
1620
- __privateGet(this, _deferredOpen).reject(Error("connection failed"));
1621
- __privateSet(this, _deferredOpen, void 0);
1622
- }
1623
- } else {
1624
- (_a = __privateGet(this, _ws)) == null ? void 0 : _a.close();
1625
- }
1626
- __privateSet(this, _ws, void 0);
1627
- }
1628
- };
1629
- _callback = new WeakMap();
1630
- _confirmedOpen = new WeakMap();
1631
- _connectionPhase = new WeakMap();
1632
- _connectionStatus = new WeakMap();
1633
- _connectionTimeout = new WeakMap();
1634
- _deferredOpen = new WeakMap();
1635
- _protocols = new WeakMap();
1636
- _url = new WeakMap();
1637
- _ws = new WeakMap();
1638
-
1639
1442
  // src/server-proxy/server-proxy.ts
1640
1443
  var _requestId = 1;
1641
- var { debug: debug4, debugEnabled: debugEnabled4, error: error2, info: info3, infoEnabled: infoEnabled2, warn: warn2 } = logger("ServerProxy");
1444
+ var { debug: debug3, debugEnabled: debugEnabled3, error: error2, info: info2, infoEnabled: infoEnabled2, warn: warn2 } = logger("ServerProxy");
1642
1445
  var nextRequestId = () => \`\${_requestId++}\`;
1643
1446
  var DEFAULT_OPTIONS = {};
1644
1447
  var isActiveViewport = (viewPort) => viewPort.disabled !== true && viewPort.suspended !== true;
@@ -1675,15 +1478,13 @@ var ServerProxy = class {
1675
1478
  __publicField(this, "cachedTableMetaRequests", /* @__PURE__ */ new Map());
1676
1479
  __publicField(this, "cachedTableSchemas", /* @__PURE__ */ new Map());
1677
1480
  __publicField(this, "tableList");
1678
- __publicField(this, "connectionStatusChanged", (status) => {
1679
- if (status === "disconnected") {
1680
- this.sessionId = void 0;
1481
+ __publicField(this, "connectionStatusChanged", (message) => {
1482
+ if (message.connectionStatus === "disconnected") {
1681
1483
  this.clearAllViewports();
1682
- } else if (status === "reconnected") {
1683
- this.reconnect();
1684
1484
  }
1685
1485
  });
1686
1486
  __publicField(this, "reconnect", async () => {
1487
+ await this.login(this.authToken);
1687
1488
  const [activeViewports, inactiveViewports] = partition(
1688
1489
  Array.from(this.viewports.values()),
1689
1490
  isActiveViewport
@@ -1719,6 +1520,7 @@ var ServerProxy = class {
1719
1520
  this.postMessageToClient = callback;
1720
1521
  this.viewports = /* @__PURE__ */ new Map();
1721
1522
  this.mapClientToServerViewport = /* @__PURE__ */ new Map();
1523
+ connection.on("reconnected", this.reconnect);
1722
1524
  connection.on("connection-status", this.connectionStatusChanged);
1723
1525
  }
1724
1526
  async login(authToken) {
@@ -1774,8 +1576,8 @@ var ServerProxy = class {
1774
1576
  );
1775
1577
  if (clientResponse) {
1776
1578
  this.postMessageToClient(clientResponse);
1777
- if (debugEnabled4) {
1778
- debug4(
1579
+ if (debugEnabled3) {
1580
+ debug3(
1779
1581
  \`post DataSourceSubscribedMessage to client: \${JSON.stringify(
1780
1582
  clientResponse
1781
1583
  )}\`
@@ -1849,7 +1651,7 @@ var ServerProxy = class {
1849
1651
  unsubscribe(clientViewportId) {
1850
1652
  const serverViewportId = this.mapClientToServerViewport.get(clientViewportId);
1851
1653
  if (serverViewportId) {
1852
- info3 == null ? void 0 : info3(
1654
+ info2 == null ? void 0 : info2(
1853
1655
  \`Unsubscribe Message (Client to Server):
1854
1656
  \${serverViewportId}\`
1855
1657
  );
@@ -1891,26 +1693,26 @@ var ServerProxy = class {
1891
1693
  /**********************************************************************/
1892
1694
  setViewRange(viewport, message) {
1893
1695
  const requestId = nextRequestId();
1894
- infoEnabled2 && info3(\`setViewRange (\${message.range.from}:\${message.range.to})\`);
1696
+ infoEnabled2 && info2(\`setViewRange (\${message.range.from}:\${message.range.to})\`);
1895
1697
  const [serverRequest, rows, debounceRequest] = viewport.rangeRequest(
1896
1698
  requestId,
1897
1699
  message.range
1898
1700
  );
1899
1701
  if (viewport.status === "subscribed") {
1900
- info3 == null ? void 0 : info3(\`setViewRange \${message.range.from} - \${message.range.to}\`);
1702
+ info2 == null ? void 0 : info2(\`setViewRange \${message.range.from} - \${message.range.to}\`);
1901
1703
  if (serverRequest) {
1902
1704
  if (true) {
1903
- info3 == null ? void 0 : info3(
1705
+ info2 == null ? void 0 : info2(
1904
1706
  \`CHANGE_VP_RANGE (\${message.range.from}-\${message.range.to}) => (\${serverRequest.from}-\${serverRequest.to})\`
1905
1707
  );
1906
1708
  }
1907
- infoEnabled2 && info3(
1709
+ infoEnabled2 && info2(
1908
1710
  \`setViewRange send CHANGE_VP_RANGE<#\${requestId}> (\${serverRequest.from}-\${serverRequest.to})\`
1909
1711
  );
1910
1712
  this.sendMessageToServer(serverRequest, requestId);
1911
1713
  }
1912
1714
  if (rows) {
1913
- info3 == null ? void 0 : info3(\`setViewRange \${rows.length} rows returned from cache\`);
1715
+ info2 == null ? void 0 : info2(\`setViewRange \${rows.length} rows returned from cache\`);
1914
1716
  this.postMessageToClient({
1915
1717
  mode: "update",
1916
1718
  type: "viewport-update",
@@ -1974,19 +1776,19 @@ var ServerProxy = class {
1974
1776
  viewport.suspend();
1975
1777
  if (escalateToDisable) {
1976
1778
  viewport.suspendTimer = setTimeout(() => {
1977
- info3 == null ? void 0 : info3("suspendTimer expired, escalate suspend to disable");
1779
+ info2 == null ? void 0 : info2("suspendTimer expired, escalate suspend to disable");
1978
1780
  this.disableViewport(viewport);
1979
1781
  }, escalateDelay);
1980
1782
  }
1981
1783
  }
1982
1784
  resumeViewport(viewport) {
1983
1785
  if (viewport.suspendTimer) {
1984
- debug4 == null ? void 0 : debug4("clear suspend timer");
1786
+ debug3 == null ? void 0 : debug3("clear suspend timer");
1985
1787
  clearTimeout(viewport.suspendTimer);
1986
1788
  viewport.suspendTimer = null;
1987
1789
  }
1988
1790
  const [size, rows] = viewport.resume();
1989
- debug4 == null ? void 0 : debug4(\`resumeViewport size \${size}, \${rows.length} rows sent to client\`);
1791
+ debug3 == null ? void 0 : debug3(\`resumeViewport size \${size}, \${rows.length} rows sent to client\`);
1990
1792
  this.postMessageToClient({
1991
1793
  clientViewportId: viewport.clientViewportId,
1992
1794
  mode: "update",
@@ -2236,14 +2038,6 @@ var ServerProxy = class {
2236
2038
  }
2237
2039
  handleMessageFromServer(message) {
2238
2040
  var _a, _b;
2239
- if (isLoginRejectedMessage(message)) {
2240
- if (this.pendingLogin) {
2241
- this.pendingLogin.reject(message.reason);
2242
- this.pendingLogin = void 0;
2243
- this.authToken = "";
2244
- }
2245
- return;
2246
- }
2247
2041
  const { body, requestId, sessionId } = message;
2248
2042
  const pendingRequest = this.pendingRequests.get(requestId);
2249
2043
  if (pendingRequest) {
@@ -2273,6 +2067,9 @@ var ServerProxy = class {
2273
2067
  throw Error("LOGIN_SUCCESS did not provide sessionId");
2274
2068
  }
2275
2069
  break;
2070
+ case "LOGIN_FAIL":
2071
+ this.postMessageToClient(body);
2072
+ break;
2276
2073
  case "REMOVE_VP_SUCCESS":
2277
2074
  {
2278
2075
  const viewport = viewports.get(body.viewPortId);
@@ -2319,8 +2116,8 @@ var ServerProxy = class {
2319
2116
  const response = viewport.completeOperation(requestId);
2320
2117
  if (response !== void 0) {
2321
2118
  this.postMessageToClient(response);
2322
- if (debugEnabled4) {
2323
- debug4(\`postMessageToClient \${JSON.stringify(response)}\`);
2119
+ if (debugEnabled3) {
2120
+ debug3(\`postMessageToClient \${JSON.stringify(response)}\`);
2324
2121
  }
2325
2122
  }
2326
2123
  }
@@ -2370,28 +2167,28 @@ var ServerProxy = class {
2370
2167
  case "TABLE_ROW":
2371
2168
  {
2372
2169
  const viewportRowMap = groupRowsByViewport(body.rows);
2373
- if (debugEnabled4) {
2170
+ if (debugEnabled3) {
2374
2171
  const [firstRow] = body.rows;
2375
2172
  if (body.rows.length === 0) {
2376
- infoEnabled2 && info3("handleMessageFromServer TABLE_ROW 0 rows");
2173
+ infoEnabled2 && info2("handleMessageFromServer TABLE_ROW 0 rows");
2377
2174
  } else if ((firstRow == null ? void 0 : firstRow.rowIndex) === -1) {
2378
2175
  if (body.rows.length === 1) {
2379
2176
  if (firstRow.updateType === "SIZE") {
2380
- infoEnabled2 && info3(
2177
+ infoEnabled2 && info2(
2381
2178
  \`handleMessageFromServer [\${firstRow.viewPortId}] TABLE_ROW SIZE ONLY \${firstRow.vpSize}\`
2382
2179
  );
2383
2180
  } else {
2384
- infoEnabled2 && info3(
2181
+ infoEnabled2 && info2(
2385
2182
  \`handleMessageFromServer [\${firstRow.viewPortId}] TABLE_ROW SIZE \${firstRow.vpSize} rowIdx \${firstRow.rowIndex}\`
2386
2183
  );
2387
2184
  }
2388
2185
  } else {
2389
- infoEnabled2 && info3(
2186
+ infoEnabled2 && info2(
2390
2187
  \`handleMessageFromServer TABLE_ROW \${body.rows.length} rows, SIZE \${firstRow.vpSize}, [\${body.rows.map((r) => r.rowIndex).join(",")}]\`
2391
2188
  );
2392
2189
  }
2393
2190
  } else {
2394
- infoEnabled2 && info3(
2191
+ infoEnabled2 && info2(
2395
2192
  \`handleMessageFromServer TABLE_ROW \${body.rows.length} rows [\${body.rows.map((r) => r.rowIndex).join(",")}]\`
2396
2193
  );
2397
2194
  }
@@ -2414,7 +2211,7 @@ var ServerProxy = class {
2414
2211
  const viewport = this.viewports.get(body.viewPortId);
2415
2212
  if (viewport) {
2416
2213
  const { from, to } = body;
2417
- infoEnabled2 && info3(\`CHANGE_VP_RANGE_SUCCESS<#\${requestId}> \${from} - \${to}\`);
2214
+ infoEnabled2 && info2(\`CHANGE_VP_RANGE_SUCCESS<#\${requestId}> \${from} - \${to}\`);
2418
2215
  viewport.completeOperation(requestId, from, to);
2419
2216
  }
2420
2217
  }
@@ -2559,7 +2356,7 @@ var ServerProxy = class {
2559
2356
  error2(body.msg);
2560
2357
  break;
2561
2358
  default:
2562
- infoEnabled2 && info3(\`handleMessageFromServer \${body["type"]}.\`);
2359
+ infoEnabled2 && info2(\`handleMessageFromServer \${body["type"]}.\`);
2563
2360
  }
2564
2361
  }
2565
2362
  cacheTableMeta(messageBody) {
@@ -2598,7 +2395,7 @@ var ServerProxy = class {
2598
2395
  const [rows, mode] = result;
2599
2396
  const size = viewport.getNewRowCount();
2600
2397
  if (size !== void 0 || rows && rows.length > 0) {
2601
- debugEnabled4 && debug4(
2398
+ debugEnabled3 && debug3(
2602
2399
  \`postMessageToClient #\${viewport.clientViewportId} viewport-update \${mode}, \${(_a = rows == null ? void 0 : rows.length) != null ? _a : "no"} rows, size \${size}\`
2603
2400
  );
2604
2401
  if (mode) {
@@ -2617,31 +2414,316 @@ var ServerProxy = class {
2617
2414
  }
2618
2415
  };
2619
2416
 
2417
+ // src/WebSocketConnection.ts
2418
+ var { debug: debug4, debugEnabled: debugEnabled4, info: info3 } = logger("WebSocketConnection");
2419
+ var isNotConnecting = (connectionState) => connectionState.connectionStatus !== "connecting" && connectionState.connectionStatus !== "reconnecting";
2420
+ var isWebSocketConnectionMessage = (msg) => {
2421
+ if ("connectionStatus" in msg) {
2422
+ return [
2423
+ "connecting",
2424
+ "connected",
2425
+ "connection-open-awaiting-session",
2426
+ "reconnecting",
2427
+ "reconnected",
2428
+ "disconnected",
2429
+ "closed",
2430
+ "failed"
2431
+ ].includes(msg.connectionStatus);
2432
+ } else {
2433
+ return false;
2434
+ }
2435
+ };
2436
+ var DEFAULT_RETRY_LIMITS = {
2437
+ connect: 5,
2438
+ reconnect: 8
2439
+ };
2440
+ var DEFAULT_CONNECTION_TIMEOUT = 1e4;
2441
+ var ConnectingEndState = {
2442
+ connecting: "connected",
2443
+ reconnecting: "reconnected"
2444
+ };
2445
+ var parseWebSocketMessage = (message) => {
2446
+ try {
2447
+ return JSON.parse(message);
2448
+ } catch (e) {
2449
+ throw Error(\`Error parsing JSON response from server \${message}\`);
2450
+ }
2451
+ };
2452
+ var _callback, _confirmedOpen, _connectionState, _connectionTimeout, _deferredConnection, _protocols, _reconnectAttempts, _requiresLogin, _url, _ws;
2453
+ var WebSocketConnection = class extends EventEmitter {
2454
+ constructor({
2455
+ callback,
2456
+ connectionTimeout = DEFAULT_CONNECTION_TIMEOUT,
2457
+ protocols,
2458
+ retryLimits = DEFAULT_RETRY_LIMITS,
2459
+ url
2460
+ }) {
2461
+ super();
2462
+ __privateAdd(this, _callback);
2463
+ /**
2464
+ We are not confirmedOpen until we receive the first message from the
2465
+ server. If we get an unexpected close event before that, we consider
2466
+ the reconnect attempts as still within the connection phase, not true
2467
+ reconnection. This can happen e.g. when connecting to remote host via
2468
+ a proxy.
2469
+ */
2470
+ __privateAdd(this, _confirmedOpen, false);
2471
+ __privateAdd(this, _connectionState);
2472
+ __privateAdd(this, _connectionTimeout);
2473
+ __privateAdd(this, _deferredConnection);
2474
+ __privateAdd(this, _protocols);
2475
+ __privateAdd(this, _reconnectAttempts);
2476
+ __privateAdd(this, _requiresLogin, true);
2477
+ __privateAdd(this, _url);
2478
+ __privateAdd(this, _ws);
2479
+ __publicField(this, "receive", (evt) => {
2480
+ if (evt.data === "Invalid token" || evt.data === "Token has expired") {
2481
+ const closeReason = evt.data === "Invalid token" ? "invalid-token" : "token-expired";
2482
+ this.close(closeReason);
2483
+ } else {
2484
+ const vuuMessageFromServer = parseWebSocketMessage(evt.data);
2485
+ if (vuuMessageFromServer.body.type === "CHANGE_VP_RANGE_SUCCESS") {
2486
+ info3 == null ? void 0 : info3(\`CHANGE_VP_RANGE_SUCCESS<#\${vuuMessageFromServer.requestId}>\`);
2487
+ }
2488
+ if (debugEnabled4) {
2489
+ if (vuuMessageFromServer.body.type !== "HB") {
2490
+ debug4(\`\${vuuMessageFromServer.body.type}\`);
2491
+ if (vuuMessageFromServer.body.type === "CHANGE_VP_SUCCESS") {
2492
+ debug4(JSON.stringify(vuuMessageFromServer.body));
2493
+ }
2494
+ }
2495
+ }
2496
+ __privateGet(this, _callback).call(this, vuuMessageFromServer);
2497
+ }
2498
+ });
2499
+ __publicField(this, "send", (msg) => {
2500
+ var _a;
2501
+ if (msg.body.type === "CHANGE_VP_RANGE") {
2502
+ info3 == null ? void 0 : info3(
2503
+ \`CHANGE_VP_RANGE<#\${msg.requestId}> \${msg.body.from}-\${msg.body.to}\`
2504
+ );
2505
+ }
2506
+ (_a = __privateGet(this, _ws)) == null ? void 0 : _a.send(JSON.stringify(msg));
2507
+ });
2508
+ __privateSet(this, _callback, callback);
2509
+ __privateSet(this, _connectionTimeout, connectionTimeout);
2510
+ __privateSet(this, _url, url);
2511
+ __privateSet(this, _protocols, protocols);
2512
+ __privateSet(this, _reconnectAttempts, {
2513
+ retryAttemptsTotal: retryLimits.reconnect,
2514
+ retryAttemptsRemaining: retryLimits.reconnect,
2515
+ secondsToNextRetry: 1
2516
+ });
2517
+ __privateSet(this, _connectionState, {
2518
+ connectionPhase: "connecting",
2519
+ connectionStatus: "closed",
2520
+ retryAttemptsTotal: retryLimits.connect,
2521
+ retryAttemptsRemaining: retryLimits.connect,
2522
+ secondsToNextRetry: 1
2523
+ });
2524
+ }
2525
+ get connectionTimeout() {
2526
+ return __privateGet(this, _connectionTimeout);
2527
+ }
2528
+ get protocols() {
2529
+ return __privateGet(this, _protocols);
2530
+ }
2531
+ get requiresLogin() {
2532
+ return __privateGet(this, _requiresLogin);
2533
+ }
2534
+ get isClosed() {
2535
+ return this.status === "closed";
2536
+ }
2537
+ get isDisconnected() {
2538
+ return this.status === "disconnected";
2539
+ }
2540
+ get isConnecting() {
2541
+ return __privateGet(this, _connectionState).connectionPhase === "connecting";
2542
+ }
2543
+ get status() {
2544
+ return __privateGet(this, _connectionState).connectionStatus;
2545
+ }
2546
+ set status(connectionStatus) {
2547
+ __privateSet(this, _connectionState, {
2548
+ ...__privateGet(this, _connectionState),
2549
+ connectionStatus
2550
+ });
2551
+ if (isNotConnecting(__privateGet(this, _connectionState))) {
2552
+ this.emit("connection-status", __privateGet(this, _connectionState));
2553
+ }
2554
+ }
2555
+ get connectionState() {
2556
+ return __privateGet(this, _connectionState);
2557
+ }
2558
+ get hasConnectionAttemptsRemaining() {
2559
+ return __privateGet(this, _connectionState).retryAttemptsRemaining > 0;
2560
+ }
2561
+ get confirmedOpen() {
2562
+ return __privateGet(this, _confirmedOpen);
2563
+ }
2564
+ /**
2565
+ * We are 'confirmedOpen' when we see the first message transmitted
2566
+ * from the server. This ensures that even if we have one or more
2567
+ * proxies in our route to the endPoint, all connections have been
2568
+ * opened successfully.
2569
+ * First time in here (on our initial successful connection) we switch
2570
+ * from 'connect' phase to 'reconnect' phase. We may have different
2571
+ * retry configurations for these two phases.
2572
+ */
2573
+ set confirmedOpen(confirmedOpen) {
2574
+ __privateSet(this, _confirmedOpen, confirmedOpen);
2575
+ if (confirmedOpen && this.isConnecting) {
2576
+ __privateSet(this, _connectionState, {
2577
+ ...__privateGet(this, _connectionState),
2578
+ connectionPhase: "reconnecting",
2579
+ ...__privateGet(this, _reconnectAttempts)
2580
+ });
2581
+ } else if (confirmedOpen) {
2582
+ __privateSet(this, _connectionState, {
2583
+ ...__privateGet(this, _connectionState),
2584
+ ...__privateGet(this, _reconnectAttempts)
2585
+ });
2586
+ }
2587
+ }
2588
+ get url() {
2589
+ return __privateGet(this, _url);
2590
+ }
2591
+ async connect(clientCall = true) {
2592
+ var _a;
2593
+ const state = __privateGet(this, _connectionState);
2594
+ if (this.isConnecting && __privateGet(this, _deferredConnection) === void 0) {
2595
+ __privateSet(this, _deferredConnection, new DeferredPromise());
2596
+ }
2597
+ const { connectionTimeout, protocols, url } = this;
2598
+ this.status = state.connectionPhase;
2599
+ const timer = setTimeout(() => {
2600
+ throw Error(
2601
+ \`Failed to open WebSocket connection to \${url}, timed out after \${connectionTimeout}ms\`
2602
+ );
2603
+ }, connectionTimeout);
2604
+ const ws2 = __privateSet(this, _ws, new WebSocket(url, protocols));
2605
+ ws2.onopen = () => {
2606
+ const connectedStatus = ConnectingEndState[state.connectionPhase];
2607
+ this.status = connectedStatus;
2608
+ clearTimeout(timer);
2609
+ if (__privateGet(this, _deferredConnection)) {
2610
+ __privateGet(this, _deferredConnection).resolve(void 0);
2611
+ __privateSet(this, _deferredConnection, void 0);
2612
+ }
2613
+ if (this.isConnecting) {
2614
+ this.emit("connected");
2615
+ } else {
2616
+ this.emit("reconnected");
2617
+ }
2618
+ };
2619
+ ws2.onerror = () => {
2620
+ clearTimeout(timer);
2621
+ };
2622
+ ws2.onclose = () => {
2623
+ if (!this.isClosed) {
2624
+ this.confirmedOpen = false;
2625
+ this.status = "disconnected";
2626
+ if (this.hasConnectionAttemptsRemaining) {
2627
+ this.reconnect();
2628
+ } else {
2629
+ this.close("failure");
2630
+ }
2631
+ }
2632
+ };
2633
+ ws2.onmessage = (evt) => {
2634
+ if (!this.confirmedOpen) {
2635
+ this.confirmedOpen = true;
2636
+ }
2637
+ this.receive(evt);
2638
+ };
2639
+ if (clientCall) {
2640
+ return (_a = __privateGet(this, _deferredConnection)) == null ? void 0 : _a.promise;
2641
+ }
2642
+ }
2643
+ reconnect() {
2644
+ const { retryAttemptsRemaining, secondsToNextRetry } = __privateGet(this, _connectionState);
2645
+ setTimeout(() => {
2646
+ __privateSet(this, _connectionState, {
2647
+ ...__privateGet(this, _connectionState),
2648
+ retryAttemptsRemaining: retryAttemptsRemaining - 1,
2649
+ secondsToNextRetry: secondsToNextRetry * 2
2650
+ });
2651
+ this.connect(false);
2652
+ }, secondsToNextRetry * 1e3);
2653
+ }
2654
+ close(reason = "shutdown") {
2655
+ var _a;
2656
+ this.status = "closed";
2657
+ if (reason === "failure") {
2658
+ if (__privateGet(this, _deferredConnection)) {
2659
+ __privateGet(this, _deferredConnection).reject(Error("connection failed"));
2660
+ __privateSet(this, _deferredConnection, void 0);
2661
+ }
2662
+ } else {
2663
+ (_a = __privateGet(this, _ws)) == null ? void 0 : _a.close();
2664
+ }
2665
+ this.emit("closed", { type: "websocket-closed", reason });
2666
+ __privateSet(this, _ws, void 0);
2667
+ }
2668
+ };
2669
+ _callback = new WeakMap();
2670
+ _confirmedOpen = new WeakMap();
2671
+ _connectionState = new WeakMap();
2672
+ _connectionTimeout = new WeakMap();
2673
+ _deferredConnection = new WeakMap();
2674
+ _protocols = new WeakMap();
2675
+ _reconnectAttempts = new WeakMap();
2676
+ _requiresLogin = new WeakMap();
2677
+ _url = new WeakMap();
2678
+ _ws = new WeakMap();
2679
+
2620
2680
  // src/worker.ts
2621
- var serverProxy;
2622
- var webSocketConnection;
2681
+ var server;
2623
2682
  var { info: info4, infoEnabled: infoEnabled3 } = logger("worker");
2683
+ var getRetryLimits = (retryLimitDisconnect, retryLimitStartup) => {
2684
+ if (retryLimitDisconnect !== void 0 && retryLimitStartup !== void 0) {
2685
+ return {
2686
+ connect: retryLimitStartup,
2687
+ reconnect: retryLimitDisconnect
2688
+ };
2689
+ } else if (retryLimitDisconnect !== void 0) {
2690
+ return {
2691
+ connect: retryLimitDisconnect,
2692
+ reconnect: retryLimitDisconnect
2693
+ };
2694
+ } else if (retryLimitStartup !== void 0) {
2695
+ return {
2696
+ connect: retryLimitStartup,
2697
+ reconnect: retryLimitStartup
2698
+ };
2699
+ }
2700
+ };
2701
+ var ws;
2624
2702
  var sendMessageToClient = (message) => {
2625
2703
  postMessage(message);
2626
2704
  };
2627
- async function connectToServer(url, protocols, token) {
2628
- if (webSocketConnection === void 0 && serverProxy === void 0) {
2629
- webSocketConnection = new WebSocketConnection({
2630
- callback: (msg) => {
2631
- if (isConnectionQualityMetrics(msg)) {
2632
- postMessage({ type: "connection-metrics", messages: msg });
2633
- } else {
2634
- serverProxy.handleMessageFromServer(msg);
2635
- }
2636
- },
2637
- protocols,
2638
- url
2639
- });
2640
- webSocketConnection.on("connection-status", postMessage);
2641
- serverProxy = new ServerProxy(webSocketConnection, sendMessageToClient);
2705
+ async function connectToServer(url, protocols, token, retryLimitDisconnect, retryLimitStartup) {
2706
+ const websocketConnection = ws = new WebSocketConnection({
2707
+ callback: (msg) => {
2708
+ if (isConnectionQualityMetrics(msg)) {
2709
+ postMessage({ type: "connection-metrics", messages: msg });
2710
+ } else if (isWebSocketConnectionMessage(msg)) {
2711
+ postMessage(msg);
2712
+ } else {
2713
+ server.handleMessageFromServer(msg);
2714
+ }
2715
+ },
2716
+ protocols,
2717
+ retryLimits: getRetryLimits(retryLimitStartup, retryLimitDisconnect),
2718
+ url
2719
+ });
2720
+ websocketConnection.on("connection-status", postMessage);
2721
+ websocketConnection.on("closed", postMessage);
2722
+ await websocketConnection.connect();
2723
+ server = new ServerProxy(websocketConnection, sendMessageToClient);
2724
+ if (websocketConnection.requiresLogin) {
2725
+ return await server.login(token);
2642
2726
  }
2643
- await webSocketConnection.openWebSocket();
2644
- return serverProxy.login(token);
2645
2727
  }
2646
2728
  var handleMessageFromClient = async ({
2647
2729
  data: message
@@ -2652,7 +2734,9 @@ var handleMessageFromClient = async ({
2652
2734
  const sessionId = await connectToServer(
2653
2735
  message.url,
2654
2736
  message.protocol,
2655
- message.token
2737
+ message.token,
2738
+ message.retryLimitDisconnect,
2739
+ message.retryLimitStartup
2656
2740
  );
2657
2741
  postMessage({ type: "connected", sessionId });
2658
2742
  } catch (err) {
@@ -2662,20 +2746,20 @@ var handleMessageFromClient = async ({
2662
2746
  // If any of the messages below are received BEFORE we have connected and created
2663
2747
  // the server - handle accordingly
2664
2748
  case "disconnect":
2665
- serverProxy.disconnect();
2666
- webSocketConnection == null ? void 0 : webSocketConnection.close();
2749
+ server.disconnect();
2750
+ ws == null ? void 0 : ws.close();
2667
2751
  break;
2668
2752
  case "subscribe":
2669
2753
  infoEnabled3 && info4(\`client subscribe: \${JSON.stringify(message)}\`);
2670
- serverProxy.subscribe(message);
2754
+ server.subscribe(message);
2671
2755
  break;
2672
2756
  case "unsubscribe":
2673
2757
  infoEnabled3 && info4(\`client unsubscribe: \${JSON.stringify(message)}\`);
2674
- serverProxy.unsubscribe(message.viewport);
2758
+ server.unsubscribe(message.viewport);
2675
2759
  break;
2676
2760
  default:
2677
2761
  infoEnabled3 && info4(\`client message: \${JSON.stringify(message)}\`);
2678
- serverProxy.handleMessageFromClient(message);
2762
+ server.handleMessageFromClient(message);
2679
2763
  }
2680
2764
  };
2681
2765
  self.addEventListener("message", handleMessageFromClient);