@vuu-ui/vuu-data-remote 0.13.73 → 0.13.75

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.
@@ -41,6 +41,8 @@ const _ConnectionManager = class _ConnectionManager extends vuuUtils.EventEmitte
41
41
  `[ConnectionManager] ${message.type} message received, viewport not found`
42
42
  );
43
43
  }
44
+ } else if (message.type === "websocket-closed") {
45
+ this.emit("session-status", message);
44
46
  } else if (vuuUtils.isLoginResponse(message)) {
45
47
  this.emit("session-status", message);
46
48
  } else if (WebSocketConnection.isWebSocketConnectionMessage(message)) {
@@ -132,10 +134,12 @@ const _ConnectionManager = class _ConnectionManager extends vuuUtils.EventEmitte
132
134
  * @param serverUrl
133
135
  * @param token
134
136
  */
135
- async connect(options) {
137
+ async connect(options, throwOnRejected = false) {
136
138
  const result = await __privateGet(this, _worker).connect(options);
137
139
  if (result === "connected") {
138
140
  __privateGet(this, _deferredServerAPI).resolve(this.connectedServerAPI);
141
+ } else if (result === "rejected" && throwOnRejected) {
142
+ throw Error("[ConnectionManager] connection rejected");
139
143
  }
140
144
  return result;
141
145
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ConnectionManager.js","sources":["../../../packages/vuu-data-remote/src/ConnectionManager.ts"],"sourcesContent":["import {\n ConnectOptions,\n DataSourceCallbackMessage,\n ServerAPI,\n ServerProxySubscribeMessage,\n TableSchema,\n VuuUIMessageIn,\n} from \"@vuu-ui/vuu-data-types\";\nimport {\n SelectRequest,\n SelectResponse,\n VuuCreateVisualLink,\n VuuLoginResponse,\n VuuRemoveVisualLink,\n VuuRpcMenuRequest,\n VuuRpcServiceRequest,\n VuuTableList,\n VuuTableListRequest,\n VuuTableMetaRequest,\n} from \"@vuu-ui/vuu-protocol-types\";\nimport {\n DeferredPromise,\n EventEmitter,\n isConnectionQualityMetrics,\n isLoginResponse,\n isRequestResponse,\n isTableSchemaMessage,\n messageHasResult,\n uuid,\n} from \"@vuu-ui/vuu-utils\";\nimport {\n WebSocketConnectionEvents,\n WebSocketConnectionState,\n isWebSocketConnectionMessage,\n} from \"./WebSocketConnection\";\nimport { DedicatedWorker } from \"./DedicatedWorker\";\nimport { shouldMessageBeRoutedToDataSource } from \"./data-source\";\n\nimport { ConnectionQualityMetrics } from \"@vuu-ui/vuu-data-types\";\n\nexport type PostMessageToClientCallback = (\n msg: DataSourceCallbackMessage,\n) => void;\n\nexport type ConnectionEvents = WebSocketConnectionEvents & {\n \"connection-metrics\": (message: ConnectionQualityMetrics) => void;\n \"session-status\": (loginResponse: VuuLoginResponse) => void;\n};\n\ntype RegisteredViewport = {\n postMessageToClientDataSource: PostMessageToClientCallback;\n request: ServerProxySubscribeMessage;\n status: \"subscribing\";\n};\n\nclass ConnectionManager extends EventEmitter<ConnectionEvents> {\n #connectionState: WebSocketConnectionState = {\n connectionPhase: \"connecting\",\n connectionStatus: \"closed\",\n retryAttemptsTotal: -1,\n retryAttemptsRemaining: -1,\n secondsToNextRetry: -1,\n };\n static #instance: ConnectionManager;\n #deferredServerAPI = new DeferredPromise<ServerAPI>();\n #pendingRequests = new Map();\n #viewports = new Map<string, RegisteredViewport>();\n // #worker?: Worker;\n #worker: DedicatedWorker;\n\n private constructor() {\n super();\n this.#worker = new DedicatedWorker(this.handleMessageFromWorker);\n }\n\n public static get instance(): ConnectionManager {\n if (!ConnectionManager.#instance) {\n ConnectionManager.#instance = new ConnectionManager();\n }\n return ConnectionManager.#instance;\n }\n\n /**\n * Open a connection to the VuuServer. This method opens the websocket connection\n * and logs in. It can be called from whichever client code has access to the auth\n * token (eg. the login page, or just a hardcoded login script in a sample).\n * This will unblock any DataSources which may have already tried to subscribe to data,\n * but lacked access to the auth token.\n *\n * @param serverUrl\n * @param token\n */\n async connect(options: ConnectOptions) {\n const result = await this.#worker.connect(options);\n if (result === \"connected\") {\n this.#deferredServerAPI.resolve(this.connectedServerAPI);\n }\n return result;\n }\n\n private handleMessageFromWorker = (\n message: VuuUIMessageIn | DataSourceCallbackMessage,\n ) => {\n if (shouldMessageBeRoutedToDataSource(message)) {\n const viewport = this.#viewports.get(message.clientViewportId);\n if (viewport) {\n viewport.postMessageToClientDataSource(message);\n } else {\n console.error(\n `[ConnectionManager] ${message.type} message received, viewport not found`,\n );\n }\n } else if (isLoginResponse(message)) {\n this.emit(\"session-status\", message);\n } else if (isWebSocketConnectionMessage(message)) {\n this.#connectionState = message;\n this.emit(\"connection-status\", message);\n } else if (isConnectionQualityMetrics(message)) {\n this.emit(\"connection-metrics\", message);\n } else if (isRequestResponse(message)) {\n const { requestId } = message;\n if (this.#pendingRequests.has(requestId)) {\n const { resolve } = this.#pendingRequests.get(requestId);\n this.#pendingRequests.delete(requestId);\n const { requestId: _, ...messageWithoutRequestId } = message;\n\n if (messageHasResult(message)) {\n resolve(message.result);\n } else if (\n message.type === \"VP_EDIT_RPC_RESPONSE\" ||\n message.type === \"VP_EDIT_RPC_REJECT\"\n ) {\n resolve(message);\n } else if (isTableSchemaMessage(message)) {\n resolve(message.tableSchema);\n } else {\n resolve(messageWithoutRequestId);\n }\n } else {\n console.warn(\n \"%cConnectionManager Unexpected message from the worker\",\n \"color:red;font-weight:bold;\",\n );\n }\n }\n };\n\n get connectionStatus() {\n return this.#connectionState.connectionStatus;\n }\n\n get serverAPI() {\n return this.#deferredServerAPI.promise;\n }\n\n private connectedServerAPI: ServerAPI = {\n subscribe: (message, callback) => {\n if (this.#viewports.get(message.viewport)) {\n throw Error(\n `ConnectionManager attempting to subscribe with an existing viewport id`,\n );\n }\n // TODO we never use this status\n this.#viewports.set(message.viewport, {\n status: \"subscribing\",\n request: message,\n postMessageToClientDataSource: callback,\n });\n this.#worker.send({ type: \"subscribe\", ...message });\n },\n\n unsubscribe: (viewport) => {\n this.#worker.send({ type: \"unsubscribe\", viewport });\n },\n\n send: (message) => {\n this.#worker.send(message);\n },\n\n destroy: (viewportId?: string) => {\n if (viewportId && this.#viewports.has(viewportId)) {\n this.#viewports.delete(viewportId);\n }\n },\n\n rpcCall: async <T = unknown>(\n message:\n | VuuRpcServiceRequest\n | VuuRpcMenuRequest\n | VuuCreateVisualLink\n | VuuRemoveVisualLink,\n ) => this.asyncRequest<T>(message),\n\n select: async (selectRequest: SelectRequest) =>\n this.asyncRequest<SelectResponse>(selectRequest),\n\n getTableList: async () =>\n this.asyncRequest<VuuTableList>({ type: \"GET_TABLE_LIST\" }),\n\n getTableSchema: async (table) =>\n this.asyncRequest<TableSchema>({\n type: \"GET_TABLE_META\",\n table,\n }),\n };\n\n private asyncRequest = <T = unknown>(\n msg:\n | VuuRpcServiceRequest\n | VuuRpcMenuRequest\n | VuuTableListRequest\n | VuuTableMetaRequest\n | VuuCreateVisualLink\n | VuuRemoveVisualLink\n | SelectRequest,\n ): Promise<T> => {\n const requestId = uuid();\n this.#worker.send({\n requestId,\n ...msg,\n });\n return new Promise((resolve, reject) => {\n this.#pendingRequests.set(requestId, { resolve, reject });\n });\n };\n\n async disconnect() {\n try {\n this.#worker.send({ type: \"disconnect\" });\n this.#deferredServerAPI = new DeferredPromise<ServerAPI>();\n return \"disconnected\";\n } catch (err: unknown) {\n return \"rejected\";\n }\n }\n\n destroy() {\n this.#worker.terminate();\n }\n}\n\nexport default ConnectionManager.instance;\n"],"names":["EventEmitter","DeferredPromise","shouldMessageBeRoutedToDataSource","isLoginResponse","isWebSocketConnectionMessage","isConnectionQualityMetrics","isRequestResponse","messageHasResult","isTableSchemaMessage","uuid","DedicatedWorker"],"mappings":";;;;;;;;;;;;;;;;;AAAA,IAAA,gBAAA,EAAA,SAAA,EAAA,kBAAA,EAAA,gBAAA,EAAA,UAAA,EAAA,OAAA;AAuDA,MAAM,kBAAA,GAAN,MAAM,kBAAA,SAA0BA,qBAA+B,CAAA;AAAA,EAerD,WAAc,GAAA;AACpB,IAAM,KAAA,EAAA;AAfR,IAA6C,YAAA,CAAA,IAAA,EAAA,gBAAA,EAAA;AAAA,MAC3C,eAAiB,EAAA,YAAA;AAAA,MACjB,gBAAkB,EAAA,QAAA;AAAA,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AAA;AACjC,EAyEA,MAAM,UAAa,GAAA;AACjB,IAAI,IAAA;AACF,MAAA,YAAA,CAAA,IAAA,EAAK,OAAQ,CAAA,CAAA,IAAA,CAAK,EAAE,IAAA,EAAM,cAAc,CAAA;AACxC,MAAK,YAAA,CAAA,IAAA,EAAA,kBAAA,EAAqB,IAAIT,wBAA2B,EAAA,CAAA;AACzD,MAAO,OAAA,cAAA;AAAA,aACA,GAAc,EAAA;AACrB,MAAO,OAAA,UAAA;AAAA;AACT;AACF,EAEA,OAAU,GAAA;AACR,IAAA,YAAA,CAAA,IAAA,EAAK,SAAQ,SAAU,EAAA;AAAA;AAE3B,CAAA;AAvLE,gBAAA,GAAA,IAAA,OAAA,EAAA;AAOO,SAAA,GAAA,IAAA,OAAA,EAAA;AACP,kBAAA,GAAA,IAAA,OAAA,EAAA;AACA,gBAAA,GAAA,IAAA,OAAA,EAAA;AACA,UAAA,GAAA,IAAA,OAAA,EAAA;AAEA,OAAA,GAAA,IAAA,OAAA,EAAA;AALA,YAAA,CARI,kBAQG,EAAA,SAAA,CAAA;AART,IAAM,iBAAN,GAAA,kBAAA;AA0LA,0BAAe,iBAAkB,CAAA,QAAA;;;;"}
1
+ {"version":3,"file":"ConnectionManager.js","sources":["../../../packages/vuu-data-remote/src/ConnectionManager.ts"],"sourcesContent":["import {\n ConnectOptions,\n DataSourceCallbackMessage,\n ServerAPI,\n ServerProxySubscribeMessage,\n TableSchema,\n VuuUIMessageIn,\n} from \"@vuu-ui/vuu-data-types\";\nimport {\n SelectRequest,\n SelectResponse,\n VuuCreateVisualLink,\n VuuLoginResponse,\n VuuRemoveVisualLink,\n VuuRpcMenuRequest,\n VuuRpcServiceRequest,\n VuuTableList,\n VuuTableListRequest,\n VuuTableMetaRequest,\n} from \"@vuu-ui/vuu-protocol-types\";\nimport {\n DeferredPromise,\n EventEmitter,\n isConnectionQualityMetrics,\n isLoginResponse,\n isRequestResponse,\n isTableSchemaMessage,\n messageHasResult,\n uuid,\n} from \"@vuu-ui/vuu-utils\";\nimport {\n WebSocketCloseMessage,\n WebSocketConnectionEvents,\n WebSocketConnectionState,\n isWebSocketConnectionMessage,\n} from \"./WebSocketConnection\";\nimport { DedicatedWorker } from \"./DedicatedWorker\";\nimport { shouldMessageBeRoutedToDataSource } from \"./data-source\";\n\nimport { ConnectionQualityMetrics } from \"@vuu-ui/vuu-data-types\";\n\nexport type PostMessageToClientCallback = (\n msg: DataSourceCallbackMessage,\n) => void;\n\nexport type ConnectionEvents = WebSocketConnectionEvents & {\n \"connection-metrics\": (message: ConnectionQualityMetrics) => void;\n \"session-status\": (\n loginResponse: VuuLoginResponse | WebSocketCloseMessage,\n ) => void;\n};\n\ntype RegisteredViewport = {\n postMessageToClientDataSource: PostMessageToClientCallback;\n request: ServerProxySubscribeMessage;\n status: \"subscribing\";\n};\n\nclass ConnectionManager extends EventEmitter<ConnectionEvents> {\n #connectionState: WebSocketConnectionState = {\n connectionPhase: \"connecting\",\n connectionStatus: \"closed\",\n retryAttemptsTotal: -1,\n retryAttemptsRemaining: -1,\n secondsToNextRetry: -1,\n };\n static #instance: ConnectionManager;\n #deferredServerAPI = new DeferredPromise<ServerAPI>();\n #pendingRequests = new Map();\n #viewports = new Map<string, RegisteredViewport>();\n // #worker?: Worker;\n #worker: DedicatedWorker;\n\n private constructor() {\n super();\n this.#worker = new DedicatedWorker(this.handleMessageFromWorker);\n }\n\n public static get instance(): ConnectionManager {\n if (!ConnectionManager.#instance) {\n ConnectionManager.#instance = new ConnectionManager();\n }\n return ConnectionManager.#instance;\n }\n\n /**\n * Open a connection to the VuuServer. This method opens the websocket connection\n * and logs in. It can be called from whichever client code has access to the auth\n * token (eg. the login page, or just a hardcoded login script in a sample).\n * This will unblock any DataSources which may have already tried to subscribe to data,\n * but lacked access to the auth token.\n *\n * @param serverUrl\n * @param token\n */\n async connect(options: ConnectOptions, throwOnRejected = false) {\n const result = await this.#worker.connect(options);\n if (result === \"connected\") {\n this.#deferredServerAPI.resolve(this.connectedServerAPI);\n } else if (result === \"rejected\" && throwOnRejected) {\n throw Error(\"[ConnectionManager] connection rejected\");\n }\n return result;\n }\n\n private handleMessageFromWorker = (\n message: VuuUIMessageIn | DataSourceCallbackMessage | WebSocketCloseMessage,\n ) => {\n if (shouldMessageBeRoutedToDataSource(message)) {\n const viewport = this.#viewports.get(message.clientViewportId);\n if (viewport) {\n viewport.postMessageToClientDataSource(message);\n } else {\n console.error(\n `[ConnectionManager] ${message.type} message received, viewport not found`,\n );\n }\n } else if (message.type === \"websocket-closed\") {\n this.emit(\"session-status\", message);\n } else if (isLoginResponse(message)) {\n this.emit(\"session-status\", message);\n } else if (isWebSocketConnectionMessage(message)) {\n this.#connectionState = message;\n this.emit(\"connection-status\", message);\n } else if (isConnectionQualityMetrics(message)) {\n this.emit(\"connection-metrics\", message);\n } else if (isRequestResponse(message)) {\n const { requestId } = message;\n if (this.#pendingRequests.has(requestId)) {\n const { resolve } = this.#pendingRequests.get(requestId);\n this.#pendingRequests.delete(requestId);\n const { requestId: _, ...messageWithoutRequestId } = message;\n\n if (messageHasResult(message)) {\n resolve(message.result);\n } else if (\n message.type === \"VP_EDIT_RPC_RESPONSE\" ||\n message.type === \"VP_EDIT_RPC_REJECT\"\n ) {\n resolve(message);\n } else if (isTableSchemaMessage(message)) {\n resolve(message.tableSchema);\n } else {\n resolve(messageWithoutRequestId);\n }\n } else {\n console.warn(\n \"%cConnectionManager Unexpected message from the worker\",\n \"color:red;font-weight:bold;\",\n );\n }\n }\n };\n\n get connectionStatus() {\n return this.#connectionState.connectionStatus;\n }\n\n get serverAPI() {\n return this.#deferredServerAPI.promise;\n }\n\n private connectedServerAPI: ServerAPI = {\n subscribe: (message, callback) => {\n if (this.#viewports.get(message.viewport)) {\n throw Error(\n `ConnectionManager attempting to subscribe with an existing viewport id`,\n );\n }\n // TODO we never use this status\n this.#viewports.set(message.viewport, {\n status: \"subscribing\",\n request: message,\n postMessageToClientDataSource: callback,\n });\n this.#worker.send({ type: \"subscribe\", ...message });\n },\n\n unsubscribe: (viewport) => {\n this.#worker.send({ type: \"unsubscribe\", viewport });\n },\n\n send: (message) => {\n this.#worker.send(message);\n },\n\n destroy: (viewportId?: string) => {\n if (viewportId && this.#viewports.has(viewportId)) {\n this.#viewports.delete(viewportId);\n }\n },\n\n rpcCall: async <T = unknown>(\n message:\n | VuuRpcServiceRequest\n | VuuRpcMenuRequest\n | VuuCreateVisualLink\n | VuuRemoveVisualLink,\n ) => this.asyncRequest<T>(message),\n\n select: async (selectRequest: SelectRequest) =>\n this.asyncRequest<SelectResponse>(selectRequest),\n\n getTableList: async () =>\n this.asyncRequest<VuuTableList>({ type: \"GET_TABLE_LIST\" }),\n\n getTableSchema: async (table) =>\n this.asyncRequest<TableSchema>({\n type: \"GET_TABLE_META\",\n table,\n }),\n };\n\n private asyncRequest = <T = unknown>(\n msg:\n | VuuRpcServiceRequest\n | VuuRpcMenuRequest\n | VuuTableListRequest\n | VuuTableMetaRequest\n | VuuCreateVisualLink\n | VuuRemoveVisualLink\n | SelectRequest,\n ): Promise<T> => {\n const requestId = uuid();\n this.#worker.send({\n requestId,\n ...msg,\n });\n return new Promise((resolve, reject) => {\n this.#pendingRequests.set(requestId, { resolve, reject });\n });\n };\n\n async disconnect() {\n try {\n this.#worker.send({ type: \"disconnect\" });\n this.#deferredServerAPI = new DeferredPromise<ServerAPI>();\n return \"disconnected\";\n } catch (err: unknown) {\n return \"rejected\";\n }\n }\n\n destroy() {\n this.#worker.terminate();\n }\n}\n\nexport default ConnectionManager.instance;\n"],"names":["EventEmitter","DeferredPromise","shouldMessageBeRoutedToDataSource","isLoginResponse","isWebSocketConnectionMessage","isConnectionQualityMetrics","isRequestResponse","messageHasResult","isTableSchemaMessage","uuid","DedicatedWorker"],"mappings":";;;;;;;;;;;;;;;;;AAAA,IAAA,gBAAA,EAAA,SAAA,EAAA,kBAAA,EAAA,gBAAA,EAAA,UAAA,EAAA,OAAA;AA0DA,MAAM,kBAAA,GAAN,MAAM,kBAAA,SAA0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Connection)?.resolve("connected");
35
35
  } else if (message.type === "connection-failed") {
36
36
  __privateGet(this, _deferredConnection)?.resolve("rejected");
37
+ } else if (message.type === "websocket-closed") {
38
+ if (message.reason === "token-expired") {
39
+ console.log(`websocket closed ${message.reason}`);
40
+ onMessage(message);
41
+ }
37
42
  } else {
38
43
  onMessage(message);
39
44
  }
@@ -1 +1 @@
1
- {"version":3,"file":"DedicatedWorker.js","sources":["../../../packages/vuu-data-remote/src/DedicatedWorker.ts"],"sourcesContent":["import {\n ConnectOptions,\n VuuUIMessageIn,\n VuuUIMessageOut,\n WithRequestId,\n} from \"@vuu-ui/vuu-data-types\";\nimport { DeferredPromise, getLoggingConfigForWorker } from \"@vuu-ui/vuu-utils\";\n\n// Note: inlined-worker is a generated file, it must be built\nimport { workerSourceCode } from \"./inlined-worker\";\nimport {\n SelectRequest,\n VuuCreateVisualLink,\n VuuRemoveVisualLink,\n VuuRpcMenuRequest,\n VuuRpcServiceRequest,\n} from \"@vuu-ui/vuu-protocol-types\";\n\nconst workerBlob = new Blob([getLoggingConfigForWorker() + workerSourceCode], {\n type: \"text/javascript\",\n});\nconst workerBlobUrl = URL.createObjectURL(workerBlob);\n\nexport class DedicatedWorker {\n #deferredConnection?: DeferredPromise<\n \"connected\" | \"reconnected\" | \"rejected\"\n >;\n #worker: Promise<Worker>;\n\n constructor(onMessage: (msg: VuuUIMessageIn) => void) {\n const deferredWorker = new DeferredPromise<Worker>();\n this.#worker = deferredWorker.promise;\n const worker = new Worker(workerBlobUrl);\n const timer: number | null = window.setTimeout(() => {\n console.warn(\"timed out waiting for worker to load\");\n }, 1000);\n worker.onmessage = (msg: MessageEvent<VuuUIMessageIn>) => {\n const { data: message } = msg;\n if (message.type === \"ready\") {\n window.clearTimeout(timer);\n deferredWorker.resolve(worker);\n } else if (message.type === \"connected\") {\n // how do we detect reconnected\n this.#deferredConnection?.resolve(\"connected\");\n } else if (message.type === \"connection-failed\") {\n this.#deferredConnection?.resolve(\"rejected\");\n // this.#deferredConnection?.reject(message.reason);\n } else {\n onMessage(message);\n }\n };\n }\n\n async connect(options: ConnectOptions) {\n this.#deferredConnection = new DeferredPromise<\n \"connected\" | \"reconnected\" | \"rejected\"\n >();\n this.send({\n ...options,\n type: \"connect\",\n });\n return this.#deferredConnection.promise;\n }\n\n async send(\n message:\n | VuuUIMessageOut\n | WithRequestId<\n | VuuCreateVisualLink\n | VuuRemoveVisualLink\n | VuuRpcServiceRequest\n | VuuRpcMenuRequest\n | SelectRequest\n >,\n ) {\n (await this.#worker).postMessage(message);\n }\n\n async terminate() {\n (await this.#worker).terminate();\n }\n}\n"],"names":["getLoggingConfigForWorker","workerSourceCode","DeferredPromise"],"mappings":";;;;;;;;;;;;AAAA,IAAA,mBAAA,EAAA,OAAA;AAkBA,MAAM,aAAa,IAAI,IAAA,CAAK,CAACA,kCAA0B,EAAA,GAAIC,8BAAgB,CAAG,EAAA;AAAA,EAC5E,IAAM,EAAA;AACR,CAAC,CAAA;AACD,MAAM,aAAA,GAAgB,GAAI,CAAA,eAAA,CAAgB,UAAU,CAAA;AAE7C,MAAM,eAAgB,CAAA;AAAA,EAM3B,YAAY,SAA0C,EAAA;AALtD,IAAA,YAAA,CAAA,IAAA,EAAA,mBAAA,CAAA;AAGA,IAAA,YAAA,CAAA,IAAA,EAAA,OAAA,CAAA;AAGE,IAAM,MAAA,cAAA,GAAiB,IAAIC,wBAAwB,EAAA;AACnD,IAAA,YAAA,CAAA,IAAA,EAAK,SAAU,cAAe,CAAA,OAAA,CAAA;AAC9B,IAAM,MAAA,MAAA,GAAS,IAAI,MAAA,CAAO,aAAa,CAAA;AACvC,IAAM,MAAA,KAAA,GAAuB,MAAO,CAAA,UAAA,CAAW,MAAM;AACnD,MAAA,OAAA,CAAQ,KAAK,sCAAsC,CAAA;AAAA,OAClD,GAAI,CAAA;AACP,IAAO,MAAA,CAAA,SAAA,GAAY,CAAC,GAAsC,KAAA;AACxD,MAAM,MAAA,EAAE,IAAM,EAAA,OAAA,EAAY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IMessageOut,\n WithRequestId,\n} from \"@vuu-ui/vuu-data-types\";\nimport { DeferredPromise, getLoggingConfigForWorker } from \"@vuu-ui/vuu-utils\";\n\n// Note: inlined-worker is a generated file, it must be built\nimport { workerSourceCode } from \"./inlined-worker\";\nimport {\n SelectRequest,\n VuuCreateVisualLink,\n VuuRemoveVisualLink,\n VuuRpcMenuRequest,\n VuuRpcServiceRequest,\n} from \"@vuu-ui/vuu-protocol-types\";\nimport { WebSocketCloseMessage } from \"./WebSocketConnection\";\n\nconst workerBlob = new Blob([getLoggingConfigForWorker() + workerSourceCode], {\n type: \"text/javascript\",\n});\nconst workerBlobUrl = URL.createObjectURL(workerBlob);\n\nexport class DedicatedWorker {\n #deferredConnection?: DeferredPromise<\n \"connected\" | \"reconnected\" | \"rejected\"\n >;\n #worker: Promise<Worker>;\n\n constructor(\n onMessage: (msg: VuuUIMessageIn | WebSocketCloseMessage) => void,\n ) {\n const deferredWorker = new DeferredPromise<Worker>();\n this.#worker = deferredWorker.promise;\n const worker = new Worker(workerBlobUrl);\n const timer: number | null = window.setTimeout(() => {\n console.warn(\"timed out waiting for worker to load\");\n }, 1000);\n worker.onmessage = (\n msg: MessageEvent<VuuUIMessageIn | WebSocketCloseMessage>,\n ) => {\n const { data: message } = msg;\n if (message.type === \"ready\") {\n window.clearTimeout(timer);\n deferredWorker.resolve(worker);\n } else if (message.type === \"connected\") {\n // how do we detect reconnected\n this.#deferredConnection?.resolve(\"connected\");\n } else if (message.type === \"connection-failed\") {\n this.#deferredConnection?.resolve(\"rejected\");\n // this.#deferredConnection?.reject(message.reason);\n } else if (message.type === \"websocket-closed\") {\n if (message.reason === \"token-expired\") {\n console.log(`websocket closed ${message.reason}`);\n onMessage(message);\n }\n } else {\n onMessage(message);\n }\n };\n }\n\n async connect(options: ConnectOptions) {\n this.#deferredConnection = new DeferredPromise<\n \"connected\" | \"reconnected\" | \"rejected\"\n >();\n this.send({\n ...options,\n type: \"connect\",\n });\n return this.#deferredConnection.promise;\n }\n\n async send(\n message:\n | VuuUIMessageOut\n | WithRequestId<\n | VuuCreateVisualLink\n | VuuRemoveVisualLink\n | VuuRpcServiceRequest\n | VuuRpcMenuRequest\n | SelectRequest\n >,\n ) {\n (await this.#worker).postMessage(message);\n }\n\n async terminate() {\n (await this.#worker).terminate();\n }\n}\n"],"names":["getLoggingConfigForWorker","workerSourceCode","DeferredPromise"],"mappings":";;;;;;;;;;;;AAAA,IAAA,mBAAA,EAAA,OAAA;AAmBA,MAAM,aAAa,IAAI,IAAA,CAAK,CAACA,kCAA0B,EAAA,GAAIC,8BAAgB,CAAG,EAAA;AAAA,EAC5E,IAAM,EAAA;AACR,CAAC,CAAA;AACD,MAAM,aAAA,GAAgB,GAAI,CAAA,eAAA,CAAgB,UAAU,CAAA;AAE7C,MAAM,eAAgB,CAAA;AAAA,EAM3B,YACE,SACA,EAAA;AAPF,IAAA,YAAA,CAAA,IAAA,EAAA,mBAAA,CAAA;AAGA,IAAA,YAAA,CAAA,IAAA,EAAA,OAAA,CAAA;AAKE,IAAM,MAAA,cAAA,GAAiB,IAAIC,wBAAwB,EAAA;AACnD,IAAA,YAAA,CAAA,IAAA,EAAK,SAAU,cAAe,CAAA,OAAA,CAAA;AAC9B,IAAM,MAAA,MAAA,GAAS,IAAI,MAAA,CAAO,aAAa,CAAA;AACvC,IAAM,MAAA,KAAA,GAAuB,MAAO,CAAA,UAAA,CAAW,MAAM;AACnD,MAAA,OAAA,CAAQ,KAAK,sCAAsC,CAAA;AAAA,OAClD,GAAI,CAAA;AACP,IAAO,MAAA,CAAA,SAAA,GAAY,CACjB,GACG,KAAA;AACH,MAAM,MAAA,EAAE,IAAM,EAAA,OAAA,EAAY,GAAA,GAAA;AAC1B,MAAI,IAAA,OAAA,CAAQ,SAAS,OAAS,EAAA;AAC5B,QAAA,MAAA,CAAO,aAAa,KAAK,CAAA;AACzB,QAAA,cAAA,CAAe,QAAQ,MAAM,CAAA;AAAA,OAC/B,MAAA,IAAW,OAAQ,CAAA,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uu-data-remote/src/WebSocketConnection.ts"],"sourcesContent":["import { WebSocketProtocol } from \"@vuu-ui/vuu-data-types\";\nimport { VuuClientMessage, VuuServerMessage } from \"@vuu-ui/vuu-protocol-types\";\nimport { DeferredPromise, EventEmitter, logger } from \"@vuu-ui/vuu-utils\";\n\nexport type ConnectingStatus = \"connecting\" | \"reconnecting\";\nexport type ConnectedStatus = \"connected\" | \"reconnected\";\nexport type ConnectionStatus =\n | ConnectedStatus\n | \"closed\"\n | \"connection-open-awaiting-session\"\n | \"disconnected\"\n | \"failed\"\n | \"inactive\";\n\ntype InternalConnectionStatus = ConnectionStatus | ConnectingStatus;\n\ntype ReconnectAttempts = {\n retryAttemptsTotal: number;\n retryAttemptsRemaining: number;\n secondsToNextRetry: number;\n};\n\nexport interface WebSocketConnectionState<\n T extends InternalConnectionStatus = ConnectionStatus,\n> extends ReconnectAttempts {\n connectionPhase: ConnectingStatus;\n connectionStatus: T;\n}\n\nconst { debug, debugEnabled, info } = logger(\"WebSocketConnection\");\n\nconst isNotConnecting = (\n connectionState: WebSocketConnectionState<InternalConnectionStatus>,\n): connectionState is WebSocketConnectionState<ConnectionStatus> =>\n connectionState.connectionStatus !== \"connecting\" &&\n connectionState.connectionStatus !== \"reconnecting\";\n\nexport const isWebSocketConnectionMessage = (\n msg: object | WebSocketConnectionState,\n): msg is WebSocketConnectionState => {\n if (\"connectionStatus\" in msg) {\n return [\n \"connecting\",\n \"connected\",\n \"connection-open-awaiting-session\",\n \"reconnecting\",\n \"reconnected\",\n \"disconnected\",\n \"closed\",\n \"failed\",\n ].includes(msg.connectionStatus);\n } else {\n return false;\n }\n};\n\nexport const isConnected = (\n status: ConnectionStatus,\n): status is ConnectedStatus =>\n status === \"connected\" || status === \"reconnected\";\n\nexport type VuuServerMessageCallback = (msg: VuuServerMessage) => void;\n\nexport type RetryLimits = {\n connect: number;\n reconnect: number;\n};\n\nexport type WebSocketConnectionConfig = {\n url: string;\n protocols: WebSocketProtocol;\n callback: VuuServerMessageCallback;\n connectionTimeout?: number;\n retryLimits?: RetryLimits;\n};\n\nconst DEFAULT_RETRY_LIMITS: RetryLimits = {\n connect: 5,\n reconnect: 8,\n};\n\nconst DEFAULT_CONNECTION_TIMEOUT = 10000;\n\nconst ConnectingEndState: Record<ConnectingStatus, ConnectedStatus> = {\n connecting: \"connected\",\n reconnecting: \"reconnected\",\n} as const;\n\nconst parseWebSocketMessage = (message: string): VuuServerMessage => {\n try {\n return JSON.parse(message) as VuuServerMessage;\n } catch (e) {\n throw Error(`Error parsing JSON response from server ${message}`);\n }\n};\n\nexport type WebSocketConnectionCloseReason = \"failure\" | \"shutdown\";\nexport type WebSocketConnectionEvents = {\n closed: (reason: WebSocketConnectionCloseReason) => void;\n connected: () => void;\n \"connection-status\": (message: WebSocketConnectionState) => void;\n reconnected: () => void;\n};\n\nexport class WebSocketConnection extends EventEmitter<WebSocketConnectionEvents> {\n #callback;\n /**\n We are not confirmedOpen until we receive the first message from the\n server. If we get an unexpected close event before that, we consider\n the reconnect attempts as still within the connection phase, not true\n reconnection. This can happen e.g. when connecting to remote host via\n a proxy.\n */\n #confirmedOpen = false;\n #connectionState: WebSocketConnectionState<InternalConnectionStatus>;\n #connectionTimeout;\n #deferredConnection?: DeferredPromise;\n #protocols;\n #reconnectAttempts: ReconnectAttempts;\n #requiresLogin = true;\n #url;\n #ws?: WebSocket;\n\n constructor({\n callback,\n connectionTimeout = DEFAULT_CONNECTION_TIMEOUT,\n protocols,\n retryLimits = DEFAULT_RETRY_LIMITS,\n url,\n }: WebSocketConnectionConfig) {\n super();\n\n this.#callback = callback;\n this.#connectionTimeout = connectionTimeout;\n this.#url = url;\n this.#protocols = protocols;\n\n this.#reconnectAttempts = {\n retryAttemptsTotal: retryLimits.reconnect,\n retryAttemptsRemaining: retryLimits.reconnect,\n secondsToNextRetry: 1,\n };\n\n /**\n * Initial retryAttempts are for the 'connecting' phase. These will\n * be replaced with 'reconnecting' phase retry attempts only once\n * initial connection succeeds.\n */\n this.#connectionState = {\n connectionPhase: \"connecting\",\n connectionStatus: \"closed\",\n retryAttemptsTotal: retryLimits.connect,\n retryAttemptsRemaining: retryLimits.connect,\n secondsToNextRetry: 1,\n };\n }\n\n get connectionTimeout() {\n return this.#connectionTimeout;\n }\n\n get protocols() {\n return this.#protocols;\n }\n\n get requiresLogin() {\n return this.#requiresLogin;\n }\n\n get isClosed() {\n return this.status === \"closed\";\n }\n get isDisconnected() {\n return this.status === \"disconnected\";\n }\n\n get isConnecting() {\n return this.#connectionState.connectionPhase === \"connecting\";\n }\n\n get status() {\n return this.#connectionState.connectionStatus;\n }\n\n private set status(connectionStatus: InternalConnectionStatus) {\n this.#connectionState = {\n ...this.#connectionState,\n connectionStatus,\n };\n // we don't publish the connecting states. They have little meaning for clients\n // and are will generally be very short-lived.\n if (isNotConnecting(this.#connectionState)) {\n this.emit(\"connection-status\", this.#connectionState);\n }\n }\n\n get connectionState() {\n return this.#connectionState;\n }\n\n private get hasConnectionAttemptsRemaining() {\n return this.#connectionState.retryAttemptsRemaining > 0;\n }\n\n private get confirmedOpen() {\n return this.#confirmedOpen;\n }\n\n /**\n * We are 'confirmedOpen' when we see the first message transmitted\n * from the server. This ensures that even if we have one or more\n * proxies in our route to the endPoint, all connections have been\n * opened successfully.\n * First time in here (on our initial successful connection) we switch\n * from 'connect' phase to 'reconnect' phase. We may have different\n * retry configurations for these two phases.\n */\n private set confirmedOpen(confirmedOpen: boolean) {\n this.#confirmedOpen = confirmedOpen;\n\n if (confirmedOpen && this.isConnecting) {\n this.#connectionState = {\n ...this.#connectionState,\n connectionPhase: \"reconnecting\",\n ...this.#reconnectAttempts,\n };\n } else if (confirmedOpen) {\n // we have successfully reconnected after a failure.\n // Reset the retry attempts, ready for next failure\n // Note: this retry is shared with 'disconnected' status\n this.#connectionState = {\n ...this.#connectionState,\n ...this.#reconnectAttempts,\n };\n }\n }\n\n get url() {\n return this.#url;\n }\n\n async connect(clientCall = true) {\n const state = this.#connectionState;\n if (this.isConnecting && this.#deferredConnection === undefined) {\n // We block on the first connecting call, this will be the\n // initial connect call from app. Any other calls will be\n // reconnect attempts. The initial connecting call returns a promise.\n // This promise is resolved either on that initial call or on a\n // subsequent successful retry attempt within nthat same initial\n // connecting phase.\n this.#deferredConnection = new DeferredPromise();\n }\n const { connectionTimeout, protocols, url } = this;\n this.status = state.connectionPhase;\n const timer = setTimeout(() => {\n throw Error(\n `Failed to open WebSocket connection to ${url}, timed out after ${connectionTimeout}ms`,\n );\n }, connectionTimeout);\n\n const ws = (this.#ws = new WebSocket(url, protocols));\n\n ws.onopen = () => {\n const connectedStatus = ConnectingEndState[state.connectionPhase];\n this.status = connectedStatus;\n clearTimeout(timer);\n if (this.#deferredConnection) {\n this.#deferredConnection.resolve(undefined);\n this.#deferredConnection = undefined;\n }\n if (this.isConnecting) {\n this.emit(\"connected\");\n } else {\n this.emit(\"reconnected\");\n }\n };\n ws.onerror = () => {\n clearTimeout(timer);\n };\n\n ws.onclose = () => {\n if (!this.isClosed) {\n this.confirmedOpen = false;\n this.status = \"disconnected\";\n if (this.hasConnectionAttemptsRemaining) {\n this.reconnect();\n } else {\n this.close(\"failure\");\n }\n }\n };\n\n ws.onmessage = (evt) => {\n if (!this.confirmedOpen) {\n // Now that we are confirmedOpen any subsequent close events\n // will be treated as part of a reconnection phase.\n this.confirmedOpen = true;\n }\n this.receive(evt);\n };\n\n if (clientCall) {\n return this.#deferredConnection?.promise;\n }\n }\n\n private reconnect() {\n const { retryAttemptsRemaining, secondsToNextRetry } =\n this.#connectionState;\n setTimeout(() => {\n this.#connectionState = {\n ...this.#connectionState,\n retryAttemptsRemaining: retryAttemptsRemaining - 1,\n secondsToNextRetry: secondsToNextRetry * 2,\n };\n this.connect(false);\n }, secondsToNextRetry * 1000);\n }\n\n private receive = (evt: MessageEvent) => {\n if (evt.data === \"Invalid token\") {\n throw Error(\"[WebSocketConnection] Invalid token\");\n } else {\n const vuuMessageFromServer = parseWebSocketMessage(evt.data);\n if (vuuMessageFromServer.body.type === \"CHANGE_VP_RANGE_SUCCESS\") {\n info?.(`CHANGE_VP_RANGE_SUCCESS<#${vuuMessageFromServer.requestId}>`);\n }\n if (debugEnabled) {\n if (vuuMessageFromServer.body.type !== \"HB\") {\n debug(`${vuuMessageFromServer.body.type}`);\n if (vuuMessageFromServer.body.type === \"CHANGE_VP_SUCCESS\") {\n debug(JSON.stringify(vuuMessageFromServer.body));\n }\n }\n }\n this.#callback(vuuMessageFromServer);\n }\n };\n\n send = (msg: VuuClientMessage) => {\n if (msg.body.type === \"CHANGE_VP_RANGE\") {\n info?.(\n `CHANGE_VP_RANGE<#${msg.requestId}> ${msg.body.from}-${msg.body.to}`,\n );\n }\n this.#ws?.send(JSON.stringify(msg));\n };\n\n close(reason: WebSocketConnectionCloseReason = \"shutdown\") {\n this.status = \"closed\";\n if (reason === \"failure\") {\n if (this.#deferredConnection) {\n this.#deferredConnection.reject(Error(\"connection failed\"));\n this.#deferredConnection = undefined;\n }\n } else {\n this.#ws?.close();\n }\n this.emit(\"closed\", reason);\n this.#ws = undefined;\n }\n}\n"],"names":["logger"],"mappings":";;;;AA6BsCA,gBAAO,qBAAqB;AAQrD,MAAA,4BAAA,GAA+B,CAC1C,GACoC,KAAA;AACpC,EAAA,IAAI,sBAAsB,GAAK,EAAA;AAC7B,IAAO,OAAA;AAAA,MACL,YAAA;AAAA,MACA,WAAA;AAAA,MACA,kCAAA;AAAA,MACA,cAAA;AAAA,MACA,aAAA;AAAA,MACA,cAAA;AAAA,MACA,QAAA;AAAA,MACA;AAAA,KACF,CAAE,QAAS,CAAA,GAAA,CAAI,gBAAgB,CAAA;AAAA,GAC1B,MAAA;AACL,IAAO,OAAA,KAAA;AAAA;AAEX;AAEO,MAAM,WAAc,GAAA,CACzB,MAEA,KAAA,MAAA,KAAW,eAAe,MAAW,KAAA;;;;;"}
1
+ {"version":3,"file":"WebSocketConnection.js","sources":["../../../packages/vuu-data-remote/src/WebSocketConnection.ts"],"sourcesContent":["import { WebSocketProtocol } from \"@vuu-ui/vuu-data-types\";\nimport { VuuClientMessage, VuuServerMessage } from \"@vuu-ui/vuu-protocol-types\";\nimport { DeferredPromise, EventEmitter, logger } from \"@vuu-ui/vuu-utils\";\n\nexport type ConnectingStatus = \"connecting\" | \"reconnecting\";\nexport type ConnectedStatus = \"connected\" | \"reconnected\";\nexport type ConnectionStatus =\n | ConnectedStatus\n | \"closed\"\n | \"connection-open-awaiting-session\"\n | \"disconnected\"\n | \"failed\"\n | \"inactive\";\n\ntype InternalConnectionStatus = ConnectionStatus | ConnectingStatus;\n\ntype ReconnectAttempts = {\n retryAttemptsTotal: number;\n retryAttemptsRemaining: number;\n secondsToNextRetry: number;\n};\n\nexport interface WebSocketConnectionState<\n T extends InternalConnectionStatus = ConnectionStatus,\n> extends ReconnectAttempts {\n connectionPhase: ConnectingStatus;\n connectionStatus: T;\n}\n\nconst { debug, debugEnabled, info } = logger(\"WebSocketConnection\");\n\nconst isNotConnecting = (\n connectionState: WebSocketConnectionState<InternalConnectionStatus>,\n): connectionState is WebSocketConnectionState<ConnectionStatus> =>\n connectionState.connectionStatus !== \"connecting\" &&\n connectionState.connectionStatus !== \"reconnecting\";\n\nexport const isWebSocketConnectionMessage = (\n msg: object | WebSocketConnectionState,\n): msg is WebSocketConnectionState => {\n if (\"connectionStatus\" in msg) {\n return [\n \"connecting\",\n \"connected\",\n \"connection-open-awaiting-session\",\n \"reconnecting\",\n \"reconnected\",\n \"disconnected\",\n \"closed\",\n \"failed\",\n ].includes(msg.connectionStatus);\n } else {\n return false;\n }\n};\n\nexport const isConnected = (\n status: ConnectionStatus,\n): status is ConnectedStatus =>\n status === \"connected\" || status === \"reconnected\";\n\nexport type VuuServerMessageCallback = (msg: VuuServerMessage) => void;\n\nexport type RetryLimits = {\n connect: number;\n reconnect: number;\n};\n\nexport type WebSocketConnectionConfig = {\n url: string;\n protocols: WebSocketProtocol;\n callback: VuuServerMessageCallback;\n connectionTimeout?: number;\n retryLimits?: RetryLimits;\n};\n\nconst DEFAULT_RETRY_LIMITS: RetryLimits = {\n connect: 5,\n reconnect: 8,\n};\n\nconst DEFAULT_CONNECTION_TIMEOUT = 10000;\n\nconst ConnectingEndState: Record<ConnectingStatus, ConnectedStatus> = {\n connecting: \"connected\",\n reconnecting: \"reconnected\",\n} as const;\n\nconst parseWebSocketMessage = (message: string): VuuServerMessage => {\n try {\n return JSON.parse(message) as VuuServerMessage;\n } catch (e) {\n throw Error(`Error parsing JSON response from server ${message}`);\n }\n};\n\nexport type WebSocketCloseMessage = {\n type: \"websocket-closed\";\n reason: WebSocketConnectionCloseReason;\n};\n\nexport type WebSocketConnectionCloseReason =\n | \"failure\"\n | \"shutdown\"\n | \"invalid-token\"\n | \"token-expired\";\nexport type WebSocketConnectionEvents = {\n closed: (message: WebSocketCloseMessage) => void;\n connected: () => void;\n \"connection-status\": (message: WebSocketConnectionState) => void;\n reconnected: () => void;\n};\n\nexport class WebSocketConnection extends EventEmitter<WebSocketConnectionEvents> {\n #callback;\n /**\n We are not confirmedOpen until we receive the first message from the\n server. If we get an unexpected close event before that, we consider\n the reconnect attempts as still within the connection phase, not true\n reconnection. This can happen e.g. when connecting to remote host via\n a proxy.\n */\n #confirmedOpen = false;\n #connectionState: WebSocketConnectionState<InternalConnectionStatus>;\n #connectionTimeout;\n #deferredConnection?: DeferredPromise;\n #protocols;\n #reconnectAttempts: ReconnectAttempts;\n #requiresLogin = true;\n #url;\n #ws?: WebSocket;\n\n constructor({\n callback,\n connectionTimeout = DEFAULT_CONNECTION_TIMEOUT,\n protocols,\n retryLimits = DEFAULT_RETRY_LIMITS,\n url,\n }: WebSocketConnectionConfig) {\n super();\n\n this.#callback = callback;\n this.#connectionTimeout = connectionTimeout;\n this.#url = url;\n this.#protocols = protocols;\n\n this.#reconnectAttempts = {\n retryAttemptsTotal: retryLimits.reconnect,\n retryAttemptsRemaining: retryLimits.reconnect,\n secondsToNextRetry: 1,\n };\n\n /**\n * Initial retryAttempts are for the 'connecting' phase. These will\n * be replaced with 'reconnecting' phase retry attempts only once\n * initial connection succeeds.\n */\n this.#connectionState = {\n connectionPhase: \"connecting\",\n connectionStatus: \"closed\",\n retryAttemptsTotal: retryLimits.connect,\n retryAttemptsRemaining: retryLimits.connect,\n secondsToNextRetry: 1,\n };\n }\n\n get connectionTimeout() {\n return this.#connectionTimeout;\n }\n\n get protocols() {\n return this.#protocols;\n }\n\n get requiresLogin() {\n return this.#requiresLogin;\n }\n\n get isClosed() {\n return this.status === \"closed\";\n }\n get isDisconnected() {\n return this.status === \"disconnected\";\n }\n\n get isConnecting() {\n return this.#connectionState.connectionPhase === \"connecting\";\n }\n\n get status() {\n return this.#connectionState.connectionStatus;\n }\n\n private set status(connectionStatus: InternalConnectionStatus) {\n this.#connectionState = {\n ...this.#connectionState,\n connectionStatus,\n };\n // we don't publish the connecting states. They have little meaning for clients\n // and are will generally be very short-lived.\n if (isNotConnecting(this.#connectionState)) {\n this.emit(\"connection-status\", this.#connectionState);\n }\n }\n\n get connectionState() {\n return this.#connectionState;\n }\n\n private get hasConnectionAttemptsRemaining() {\n return this.#connectionState.retryAttemptsRemaining > 0;\n }\n\n private get confirmedOpen() {\n return this.#confirmedOpen;\n }\n\n /**\n * We are 'confirmedOpen' when we see the first message transmitted\n * from the server. This ensures that even if we have one or more\n * proxies in our route to the endPoint, all connections have been\n * opened successfully.\n * First time in here (on our initial successful connection) we switch\n * from 'connect' phase to 'reconnect' phase. We may have different\n * retry configurations for these two phases.\n */\n private set confirmedOpen(confirmedOpen: boolean) {\n this.#confirmedOpen = confirmedOpen;\n\n if (confirmedOpen && this.isConnecting) {\n this.#connectionState = {\n ...this.#connectionState,\n connectionPhase: \"reconnecting\",\n ...this.#reconnectAttempts,\n };\n } else if (confirmedOpen) {\n // we have successfully reconnected after a failure.\n // Reset the retry attempts, ready for next failure\n // Note: this retry is shared with 'disconnected' status\n this.#connectionState = {\n ...this.#connectionState,\n ...this.#reconnectAttempts,\n };\n }\n }\n\n get url() {\n return this.#url;\n }\n\n async connect(clientCall = true) {\n const state = this.#connectionState;\n if (this.isConnecting && this.#deferredConnection === undefined) {\n // We block on the first connecting call, this will be the\n // initial connect call from app. Any other calls will be\n // reconnect attempts. The initial connecting call returns a promise.\n // This promise is resolved either on that initial call or on a\n // subsequent successful retry attempt within nthat same initial\n // connecting phase.\n this.#deferredConnection = new DeferredPromise();\n }\n const { connectionTimeout, protocols, url } = this;\n this.status = state.connectionPhase;\n const timer = setTimeout(() => {\n throw Error(\n `Failed to open WebSocket connection to ${url}, timed out after ${connectionTimeout}ms`,\n );\n }, connectionTimeout);\n\n const ws = (this.#ws = new WebSocket(url, protocols));\n\n ws.onopen = () => {\n const connectedStatus = ConnectingEndState[state.connectionPhase];\n this.status = connectedStatus;\n clearTimeout(timer);\n if (this.#deferredConnection) {\n this.#deferredConnection.resolve(undefined);\n this.#deferredConnection = undefined;\n }\n if (this.isConnecting) {\n this.emit(\"connected\");\n } else {\n this.emit(\"reconnected\");\n }\n };\n ws.onerror = () => {\n clearTimeout(timer);\n };\n\n ws.onclose = () => {\n if (!this.isClosed) {\n this.confirmedOpen = false;\n this.status = \"disconnected\";\n if (this.hasConnectionAttemptsRemaining) {\n this.reconnect();\n } else {\n this.close(\"failure\");\n }\n }\n };\n\n ws.onmessage = (evt) => {\n if (!this.confirmedOpen) {\n // Now that we are confirmedOpen any subsequent close events\n // will be treated as part of a reconnection phase.\n this.confirmedOpen = true;\n }\n this.receive(evt);\n };\n\n if (clientCall) {\n return this.#deferredConnection?.promise;\n }\n }\n\n private reconnect() {\n const { retryAttemptsRemaining, secondsToNextRetry } =\n this.#connectionState;\n setTimeout(() => {\n this.#connectionState = {\n ...this.#connectionState,\n retryAttemptsRemaining: retryAttemptsRemaining - 1,\n secondsToNextRetry: secondsToNextRetry * 2,\n };\n this.connect(false);\n }, secondsToNextRetry * 1000);\n }\n\n private receive = (evt: MessageEvent) => {\n if (evt.data === \"Invalid token\" || evt.data === \"Token has expired\") {\n const closeReason: WebSocketConnectionCloseReason =\n evt.data === \"Invalid token\" ? \"invalid-token\" : \"token-expired\";\n this.close(closeReason);\n } else {\n const vuuMessageFromServer = parseWebSocketMessage(evt.data);\n if (vuuMessageFromServer.body.type === \"CHANGE_VP_RANGE_SUCCESS\") {\n info?.(`CHANGE_VP_RANGE_SUCCESS<#${vuuMessageFromServer.requestId}>`);\n }\n if (debugEnabled) {\n if (vuuMessageFromServer.body.type !== \"HB\") {\n debug(`${vuuMessageFromServer.body.type}`);\n if (vuuMessageFromServer.body.type === \"CHANGE_VP_SUCCESS\") {\n debug(JSON.stringify(vuuMessageFromServer.body));\n }\n }\n }\n this.#callback(vuuMessageFromServer);\n }\n };\n\n send = (msg: VuuClientMessage) => {\n if (msg.body.type === \"CHANGE_VP_RANGE\") {\n info?.(\n `CHANGE_VP_RANGE<#${msg.requestId}> ${msg.body.from}-${msg.body.to}`,\n );\n }\n this.#ws?.send(JSON.stringify(msg));\n };\n\n close(reason: WebSocketConnectionCloseReason = \"shutdown\") {\n this.status = \"closed\";\n if (reason === \"failure\") {\n if (this.#deferredConnection) {\n this.#deferredConnection.reject(Error(\"connection failed\"));\n this.#deferredConnection = undefined;\n }\n } else {\n this.#ws?.close();\n }\n this.emit(\"closed\", { type: \"websocket-closed\", reason });\n this.#ws = undefined;\n }\n}\n"],"names":["logger"],"mappings":";;;;AA6BsCA,gBAAO,qBAAqB;AAQrD,MAAA,4BAAA,GAA+B,CAC1C,GACoC,KAAA;AACpC,EAAA,IAAI,sBAAsB,GAAK,EAAA;AAC7B,IAAO,OAAA;AAAA,MACL,YAAA;AAAA,MACA,WAAA;AAAA,MACA,kCAAA;AAAA,MACA,cAAA;AAAA,MACA,aAAA;AAAA,MACA,cAAA;AAAA,MACA,QAAA;AAAA,MACA;AAAA,KACF,CAAE,QAAS,CAAA,GAAA,CAAI,gBAAgB,CAAA;AAAA,GAC1B,MAAA;AACL,IAAO,OAAA,KAAA;AAAA;AAEX;AAEO,MAAM,WAAc,GAAA,CACzB,MAEA,KAAA,MAAA,KAAW,eAAe,MAAW,KAAA;;;;;"}
@@ -1,28 +1,49 @@
1
1
  'use strict';
2
2
 
3
3
  const defaultAuthUrl = "api/authn";
4
- const authenticate = async (username, password, authUrl = defaultAuthUrl) => fetch(authUrl, {
5
- method: "POST",
6
- credentials: "include",
7
- headers: {
8
- "Content-Type": "application/json",
9
- "access-control-allow-origin": location.host
10
- },
11
- body: JSON.stringify({ username, password })
12
- }).then((response) => {
13
- if (response.ok) {
14
- const authToken = response.headers.get("vuu-auth-token");
15
- if (typeof authToken === "string" && authToken.length > 0) {
16
- return authToken;
17
- } else {
18
- throw Error(`Authentication failed auth token not returned by server`);
19
- }
4
+ const isValidVuuUser = (response) => typeof response === "object" && response !== null && "name" in response && "authorizations" in response && typeof response.name === "string" && Array.isArray(response.authorizations);
5
+ const parseVuuUserFromToken = (token) => {
6
+ const [base64EncodedVuuUser] = token.split(".");
7
+ const jsonString = atob(base64EncodedVuuUser);
8
+ const response = JSON.parse(jsonString);
9
+ if (isValidVuuUser(response)) {
10
+ return response;
20
11
  } else {
21
- throw Error(
22
- `Authentication failed ${response.status} ${response.statusText}`
23
- );
12
+ throw Error(`auth token does not containe VuuUser`);
24
13
  }
25
- });
14
+ };
15
+ const authenticate = async (username, password, authUrl = defaultAuthUrl) => {
16
+ return fetch(authUrl, {
17
+ method: "POST",
18
+ credentials: "include",
19
+ headers: {
20
+ "Content-Type": "application/json",
21
+ "access-control-allow-origin": location.host
22
+ },
23
+ body: JSON.stringify({ username, password })
24
+ }).then((response) => {
25
+ if (response.ok) {
26
+ const authToken = response.headers.get("vuu-auth-token");
27
+ if (typeof authToken === "string" && authToken.length > 0) {
28
+ try {
29
+ return {
30
+ token: authToken,
31
+ user: parseVuuUserFromToken(authToken)
32
+ };
33
+ } catch (e) {
34
+ throw Error(`Authentication error: vuu auth token decoding failed.`);
35
+ }
36
+ } else {
37
+ throw Error(`Authentication failed auth token not returned by server`);
38
+ }
39
+ } else {
40
+ throw Error(
41
+ `Authentication failed ${response.status} ${response.statusText}`
42
+ );
43
+ }
44
+ });
45
+ };
26
46
 
27
47
  exports.authenticate = authenticate;
48
+ exports.parseVuuUserFromToken = parseVuuUserFromToken;
28
49
  //# sourceMappingURL=authenticate.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"authenticate.js","sources":["../../../packages/vuu-data-remote/src/authenticate.ts"],"sourcesContent":["const defaultAuthUrl = \"api/authn\";\n\nexport const authenticate = async (\n username: string,\n password: string,\n authUrl = defaultAuthUrl\n): Promise<string | void> =>\n fetch(authUrl, {\n method: \"POST\",\n credentials: \"include\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"access-control-allow-origin\": location.host,\n },\n body: JSON.stringify({ username, password }),\n }).then((response) => {\n if (response.ok) {\n const authToken = response.headers.get(\"vuu-auth-token\");\n if (typeof authToken === \"string\" && authToken.length > 0) {\n return authToken;\n } else {\n throw Error(`Authentication failed auth token not returned by server`);\n }\n } else {\n throw Error(\n `Authentication failed ${response.status} ${response.statusText}`\n );\n }\n });\n"],"names":[],"mappings":";;AAAA,MAAM,cAAiB,GAAA,WAAA;AAEhB,MAAM,eAAe,OAC1B,QAAA,EACA,UACA,OAAU,GAAA,cAAA,KAEV,MAAM,OAAS,EAAA;AAAA,EACb,MAAQ,EAAA,MAAA;AAAA,EACR,WAAa,EAAA,SAAA;AAAA,EACb,OAAS,EAAA;AAAA,IACP,cAAgB,EAAA,kBAAA;AAAA,IAChB,+BAA+B,QAAS,CAAA;AAAA,GAC1C;AAAA,EACA,MAAM,IAAK,CAAA,SAAA,CAAU,EAAE,QAAA,EAAU,UAAU;AAC7C,CAAC,CAAA,CAAE,IAAK,CAAA,CAAC,QAAa,KAAA;AACpB,EAAA,IAAI,SAAS,EAAI,EAAA;AACf,IAAA,MAAM,SAAY,GAAA,QAAA,CAAS,OAAQ,CAAA,GAAA,CAAI,gBAAgB,CAAA;AACvD,IAAA,IAAI,OAAO,SAAA,KAAc,QAAY,IAAA,SAAA,CAAU,SAAS,CAAG,EAAA;AACzD,MAAO,OAAA,SAAA;AAAA,KACF,MAAA;AACL,MAAA,MAAM,MAAM,CAAyD,uDAAA,CAAA,CAAA;AAAA;AACvE,GACK,MAAA;AACL,IAAM,MAAA,KAAA;AAAA,MACJ,CAAyB,sBAAA,EAAA,QAAA,CAAS,MAAM,CAAA,CAAA,EAAI,SAAS,UAAU,CAAA;AAAA,KACjE;AAAA;AAEJ,CAAC;;;;"}
1
+ {"version":3,"file":"authenticate.js","sources":["../../../packages/vuu-data-remote/src/authenticate.ts"],"sourcesContent":["import type { VuuUser } from \"@vuu-ui/vuu-protocol-types\";\n\nconst defaultAuthUrl = \"api/authn\";\n\nconst isValidVuuUser = (response: unknown): response is VuuUser =>\n typeof response === \"object\" &&\n response !== null &&\n \"name\" in response &&\n \"authorizations\" in response &&\n typeof response.name === \"string\" &&\n Array.isArray(response.authorizations);\n\n/**\n * The auth token returned from authn call encodes (Base64) a VuuUser\n * within the first part of the token.\n */\nexport const parseVuuUserFromToken = (token: string) => {\n const [base64EncodedVuuUser] = token.split(\".\");\n const jsonString = atob(base64EncodedVuuUser);\n const response = JSON.parse(jsonString);\n if (isValidVuuUser(response)) {\n return response;\n } else {\n throw Error(`auth token does not containe VuuUser`);\n }\n};\n\nexport type AuthenticationResponse = {\n token: string;\n user: VuuUser;\n};\n\nexport const authenticate = async (\n username: string,\n password: string,\n authUrl = defaultAuthUrl,\n): Promise<AuthenticationResponse> => {\n return fetch(authUrl, {\n method: \"POST\",\n credentials: \"include\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"access-control-allow-origin\": location.host,\n },\n body: JSON.stringify({ username, password }),\n }).then((response) => {\n if (response.ok) {\n const authToken = response.headers.get(\"vuu-auth-token\");\n if (typeof authToken === \"string\" && authToken.length > 0) {\n try {\n return {\n token: authToken,\n user: parseVuuUserFromToken(authToken),\n };\n } catch (e) {\n throw Error(`Authentication error: vuu auth token decoding failed.`);\n }\n } else {\n throw Error(`Authentication failed auth token not returned by server`);\n }\n } else {\n throw Error(\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,KAAc,QAAY,IAAA,SAAA,CAAU,SAAS,CAAG,EAAA;AACzD,QAAI,IAAA;AACF,UAAO,OAAA;AAAA,YACL,KAAO,EAAA,SAAA;AAAA,YACP,IAAA,EAAM,sBAAsB,SAAS;AAAA,WACvC;AAAA,iBACO,CAAG,EAAA;AACV,UAAA,MAAM,MAAM,CAAwD,sDAAA,CAAA,CAAA;AAAA;AACtE,OACK,MAAA;AACL,QAAA,MAAM,MAAM,CAAyD,uDAAA,CAAA,CAAA;AAAA;AACvE,KACK,MAAA;AACL,MAAM,MAAA,KAAA;AAAA,QACJ,CAAyB,sBAAA,EAAA,QAAA,CAAS,MAAM,CAAA,CAAA,EAAI,SAAS,UAAU,CAAA;AAAA,OACjE;AAAA;AACF,GACD,CAAA;AACH;;;;;"}
package/cjs/index.js CHANGED
@@ -11,6 +11,7 @@ var WebSocketConnection = require('./WebSocketConnection.js');
11
11
 
12
12
 
13
13
  exports.authenticate = authenticate.authenticate;
14
+ exports.parseVuuUserFromToken = authenticate.parseVuuUserFromToken;
14
15
  exports.ConnectionManager = ConnectionManager;
15
16
  exports.connectionId = constants.connectionId;
16
17
  exports.msgType = constants.msgType;
package/cjs/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1588,7 +1588,6 @@ var ServerProxy = class {
1588
1588
  __publicField(this, "viewports");
1589
1589
  __publicField(this, "mapClientToServerViewport");
1590
1590
  __publicField(this, "authToken", "");
1591
- __publicField(this, "user", "user");
1592
1591
  __publicField(this, "pendingLogin");
1593
1592
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1594
1593
  __publicField(this, "pendingRequests", /* @__PURE__ */ new Map());
@@ -1597,9 +1596,6 @@ var ServerProxy = class {
1597
1596
  __publicField(this, "cachedTableMetaRequests", /* @__PURE__ */ new Map());
1598
1597
  __publicField(this, "cachedTableSchemas", /* @__PURE__ */ new Map());
1599
1598
  __publicField(this, "tableList");
1600
- __publicField(this, "connectionClosed", (reason) => {
1601
- console.log(\`[ServerProxy] connectionClosed reason \${reason}\`);
1602
- });
1603
1599
  __publicField(this, "connectionStatusChanged", (message) => {
1604
1600
  if (message.connectionStatus === "disconnected") {
1605
1601
  this.clearAllViewports();
@@ -1643,13 +1639,11 @@ var ServerProxy = class {
1643
1639
  this.viewports = /* @__PURE__ */ new Map();
1644
1640
  this.mapClientToServerViewport = /* @__PURE__ */ new Map();
1645
1641
  connection.on("reconnected", this.reconnect);
1646
- connection.on("closed", this.connectionClosed);
1647
1642
  connection.on("connection-status", this.connectionStatusChanged);
1648
1643
  }
1649
- async login(authToken, user = "user") {
1644
+ async login(authToken) {
1650
1645
  if (authToken) {
1651
1646
  this.authToken = authToken;
1652
- this.user = user;
1653
1647
  return new Promise((resolve, reject) => {
1654
1648
  this.sendMessageToServer({ type: "LOGIN", token: this.authToken }, "");
1655
1649
  this.pendingLogin = { resolve, reject };
@@ -2620,8 +2614,9 @@ var WebSocketConnection = class extends EventEmitter {
2620
2614
  __privateAdd(this, _url);
2621
2615
  __privateAdd(this, _ws);
2622
2616
  __publicField(this, "receive", (evt) => {
2623
- if (evt.data === "Invalid token") {
2624
- throw Error("[WebSocketConnection] Invalid token");
2617
+ if (evt.data === "Invalid token" || evt.data === "Token has expired") {
2618
+ const closeReason = evt.data === "Invalid token" ? "invalid-token" : "token-expired";
2619
+ this.close(closeReason);
2625
2620
  } else {
2626
2621
  const vuuMessageFromServer = parseWebSocketMessage(evt.data);
2627
2622
  if (vuuMessageFromServer.body.type === "CHANGE_VP_RANGE_SUCCESS") {
@@ -2804,7 +2799,7 @@ var WebSocketConnection = class extends EventEmitter {
2804
2799
  } else {
2805
2800
  (_a = __privateGet(this, _ws)) == null ? void 0 : _a.close();
2806
2801
  }
2807
- this.emit("closed", reason);
2802
+ this.emit("closed", { type: "websocket-closed", reason });
2808
2803
  __privateSet(this, _ws, void 0);
2809
2804
  }
2810
2805
  };
@@ -2844,7 +2839,7 @@ var ws;
2844
2839
  var sendMessageToClient = (message) => {
2845
2840
  postMessage(message);
2846
2841
  };
2847
- async function connectToServer(url, protocols, token, username, retryLimitDisconnect, retryLimitStartup) {
2842
+ async function connectToServer(url, protocols, token, retryLimitDisconnect, retryLimitStartup) {
2848
2843
  const websocketConnection = ws = new WebSocketConnection({
2849
2844
  callback: (msg) => {
2850
2845
  if (isConnectionQualityMetrics(msg)) {
@@ -2860,10 +2855,11 @@ async function connectToServer(url, protocols, token, username, retryLimitDiscon
2860
2855
  url
2861
2856
  });
2862
2857
  websocketConnection.on("connection-status", postMessage);
2858
+ websocketConnection.on("closed", postMessage);
2863
2859
  await websocketConnection.connect();
2864
2860
  server = new ServerProxy(websocketConnection, sendMessageToClient);
2865
2861
  if (websocketConnection.requiresLogin) {
2866
- await server.login(token, username);
2862
+ await server.login(token);
2867
2863
  }
2868
2864
  }
2869
2865
  var handleMessageFromClient = async ({
@@ -2876,7 +2872,6 @@ var handleMessageFromClient = async ({
2876
2872
  message.url,
2877
2873
  message.protocol,
2878
2874
  message.token,
2879
- message.username,
2880
2875
  message.retryLimitDisconnect,
2881
2876
  message.retryLimitStartup
2882
2877
  );