@laplace.live/event-bridge-sdk 0.1.7 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- var w;((q)=>{q.DISCONNECTED="disconnected";q.CONNECTING="connecting";q.CONNECTED="connected";q.RECONNECTING="reconnecting"})(w||={});class z{ws=null;eventHandlers=new Map;anyEventHandlers=[];connectionStateHandlers=[];reconnectTimer=null;reconnectAttempts=0;clientId=null;connectionState="disconnected";options={url:"ws://localhost:9696",token:"",reconnect:!0,reconnectInterval:3000,maxReconnectAttempts:10};constructor(b={}){this.options={...this.options,...b}}connect(){return new Promise((b,k)=>{try{if(this.ws)this.ws.close();this.setConnectionState("connecting");let m=[];if(this.options.token)m.push("laplace-event-bridge-role-client",this.options.token);this.ws=new WebSocket(this.options.url,m),this.ws.onopen=()=>{this.setConnectionState("connected"),this.reconnectAttempts=0,console.log("Connected to LAPLACE Event Bridge"),b()},this.ws.onmessage=(u)=>{try{let q=JSON.parse(u.data);if(q.type==="established"&&q.clientId)this.clientId=q.clientId,console.log(`Connection established with client ID: ${this.clientId}`);this.processEvent(q)}catch(q){console.error("Failed to parse event data:",q)}},this.ws.onerror=(u)=>{console.error("WebSocket error:",u),k(u)},this.ws.onclose=()=>{if(console.log("Disconnected from LAPLACE Event Bridge"),this.options.reconnect&&this.reconnectAttempts<this.options.maxReconnectAttempts)this.reconnectAttempts++,this.setConnectionState("reconnecting"),console.log(`Attempting to reconnect (${this.reconnectAttempts}/${this.options.maxReconnectAttempts})...`),this.reconnectTimer=setTimeout(()=>{this.connect().catch((u)=>{console.error("Reconnection failed:",u)})},this.options.reconnectInterval);else this.setConnectionState("disconnected")}}catch(m){this.setConnectionState("disconnected"),k(m)}})}disconnect(){if(this.reconnectTimer)clearTimeout(this.reconnectTimer),this.reconnectTimer=null;if(this.ws)this.ws.close(),this.ws=null;this.setConnectionState("disconnected"),this.clientId=null}on(b,k){if(!this.eventHandlers.has(b))this.eventHandlers.set(b,[]);this.eventHandlers.get(b).push(k)}onAny(b){this.anyEventHandlers.push(b)}onConnectionStateChange(b){this.connectionStateHandlers.push(b),b(this.connectionState)}off(b,k){if(!this.eventHandlers.has(b))return;let m=this.eventHandlers.get(b),u=m.indexOf(k);if(u!==-1)m.splice(u,1);if(m.length===0)this.eventHandlers.delete(b)}offAny(b){let k=this.anyEventHandlers.indexOf(b);if(k!==-1)this.anyEventHandlers.splice(k,1)}offConnectionStateChange(b){let k=this.connectionStateHandlers.indexOf(b);if(k!==-1)this.connectionStateHandlers.splice(k,1)}isConnectedToBridge(){return this.connectionState==="connected"}getConnectionState(){return this.connectionState}getClientId(){return this.clientId}send(b){if(!this.ws||this.ws.readyState!==WebSocket.OPEN)throw new Error("Not connected to LAPLACE Event Bridge");this.ws.send(JSON.stringify(b))}setConnectionState(b){if(this.connectionState!==b){this.connectionState=b;for(let k of this.connectionStateHandlers)try{k(b)}catch(m){console.error("Error in connection state change handler:",m)}}}processEvent(b){if(this.eventHandlers.has(b.type))for(let k of this.eventHandlers.get(b.type))try{k(b)}catch(m){console.error(`Error in event handler for type ${b.type}:`,m)}for(let k of this.anyEventHandlers)try{k(b)}catch(m){console.error("Error in any event handler:",m)}}}export{z as LaplaceEventBridgeClient,w as ConnectionState};
1
+ var B;((w)=>{w.DISCONNECTED="disconnected";w.CONNECTING="connecting";w.CONNECTED="connected";w.RECONNECTING="reconnecting"})(B||={});class D{ws=null;eventHandlers=new Map;anyEventHandlers=[];connectionStateHandlers=[];reconnectTimer=null;reconnectAttempts=0;clientId=null;connectionState="disconnected";options={url:"ws://localhost:9696",token:"",reconnect:!0,reconnectInterval:3000,maxReconnectAttempts:10};constructor(k={}){this.options={...this.options,...k}}connect(){return new Promise((k,m)=>{try{if(this.ws)this.ws.close();this.setConnectionState("connecting");let q=this.options.url,A=[];if(this.options.token){A.push("laplace-event-bridge-role-client",this.options.token);let w=new URL(q);w.searchParams.set("token",this.options.token),q=w.toString()}this.ws=new WebSocket(q,A),this.ws.onopen=()=>{this.setConnectionState("connected"),this.reconnectAttempts=0,console.log("Connected to LAPLACE Event Bridge"),k()},this.ws.onmessage=(w)=>{try{let z=JSON.parse(w.data);if(z.type==="established"&&z.clientId)this.clientId=z.clientId,console.log(`Connection established with client ID: ${this.clientId}`);this.processEvent(z)}catch(z){console.error("Failed to parse event data:",z)}},this.ws.onerror=(w)=>{console.error("WebSocket error:",w),m(w)},this.ws.onclose=()=>{if(console.log("Disconnected from LAPLACE Event Bridge"),this.options.reconnect&&this.reconnectAttempts<this.options.maxReconnectAttempts)this.reconnectAttempts++,this.setConnectionState("reconnecting"),console.log(`Attempting to reconnect (${this.reconnectAttempts}/${this.options.maxReconnectAttempts})...`),this.reconnectTimer=setTimeout(()=>{this.connect().catch((w)=>{console.error("Reconnection failed:",w)})},this.options.reconnectInterval);else this.setConnectionState("disconnected")}}catch(q){this.setConnectionState("disconnected"),m(q)}})}disconnect(){if(this.reconnectTimer)clearTimeout(this.reconnectTimer),this.reconnectTimer=null;if(this.ws)this.ws.close(),this.ws=null;this.setConnectionState("disconnected"),this.clientId=null}on(k,m){if(!this.eventHandlers.has(k))this.eventHandlers.set(k,[]);this.eventHandlers.get(k).push(m)}onAny(k){this.anyEventHandlers.push(k)}onConnectionStateChange(k){this.connectionStateHandlers.push(k),k(this.connectionState)}off(k,m){if(!this.eventHandlers.has(k))return;let q=this.eventHandlers.get(k),A=q.indexOf(m);if(A!==-1)q.splice(A,1);if(q.length===0)this.eventHandlers.delete(k)}offAny(k){let m=this.anyEventHandlers.indexOf(k);if(m!==-1)this.anyEventHandlers.splice(m,1)}offConnectionStateChange(k){let m=this.connectionStateHandlers.indexOf(k);if(m!==-1)this.connectionStateHandlers.splice(m,1)}isConnectedToBridge(){return this.connectionState==="connected"}getConnectionState(){return this.connectionState}getClientId(){return this.clientId}send(k){if(!this.ws||this.ws.readyState!==WebSocket.OPEN)throw new Error("Not connected to LAPLACE Event Bridge");this.ws.send(JSON.stringify(k))}setConnectionState(k){if(this.connectionState!==k){this.connectionState=k;for(let m of this.connectionStateHandlers)try{m(k)}catch(q){console.error("Error in connection state change handler:",q)}}}processEvent(k){if(this.eventHandlers.has(k.type))for(let m of this.eventHandlers.get(k.type))try{m(k)}catch(q){console.error(`Error in event handler for type ${k.type}:`,q)}for(let m of this.anyEventHandlers)try{m(k)}catch(q){console.error("Error in any event handler:",q)}}}export{D as LaplaceEventBridgeClient,B as ConnectionState};
package/index.ts CHANGED
@@ -82,13 +82,19 @@ export class LaplaceEventBridgeClient {
82
82
 
83
83
  this.setConnectionState(ConnectionState.CONNECTING)
84
84
 
85
+ let url = this.options.url
85
86
  const protocols: string[] = []
86
87
  if (this.options.token) {
87
88
  // Add the token as the second protocol parameter
88
89
  protocols.push('laplace-event-bridge-role-client', this.options.token)
90
+
91
+ // Also add token as a query parameter
92
+ const urlObj = new URL(url)
93
+ urlObj.searchParams.set('token', this.options.token)
94
+ url = urlObj.toString()
89
95
  }
90
96
 
91
- this.ws = new WebSocket(this.options.url, protocols)
97
+ this.ws = new WebSocket(url, protocols)
92
98
 
93
99
  this.ws.onopen = () => {
94
100
  this.setConnectionState(ConnectionState.CONNECTED)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@laplace.live/event-bridge-sdk",
3
3
  "description": "LAPLACE Event Bridge SDK",
4
- "version": "0.1.7",
4
+ "version": "0.2.1",
5
5
  "module": "index.ts",
6
6
  "types": "index.ts",
7
7
  "license": "MIT",
@@ -22,7 +22,7 @@
22
22
  "LICENSE"
23
23
  ],
24
24
  "dependencies": {
25
- "@laplace.live/event-types": "^2.0.4"
25
+ "@laplace.live/event-types": "^2.0.9"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "@laplace.live/event-types": "^2.0.4"