@laplace.live/event-bridge-sdk 0.2.3 → 1.0.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 +1 -1
- package/index.ts +24 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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(
|
|
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;let w=(()=>{let z=new URL(q);if(z.searchParams.has("token"))z.searchParams.set("token","***");return z.toString()})();console.log(`Connected to LAPLACE Event Bridge: ${w}`),k()},this.ws.onmessage=(w)=>{try{let z=JSON.parse(w.data);if(z.type==="ping"){this.ws?.send(JSON.stringify({type:"pong",timestamp:Date.now(),respondingTo:z.timestamp}));return}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
|
@@ -99,7 +99,17 @@ export class LaplaceEventBridgeClient {
|
|
|
99
99
|
this.ws.onopen = () => {
|
|
100
100
|
this.setConnectionState(ConnectionState.CONNECTED)
|
|
101
101
|
this.reconnectAttempts = 0
|
|
102
|
-
|
|
102
|
+
|
|
103
|
+
// Create a display URL that masks the token if present
|
|
104
|
+
const displayUrl = (() => {
|
|
105
|
+
const urlObj = new URL(url)
|
|
106
|
+
if (urlObj.searchParams.has('token')) {
|
|
107
|
+
urlObj.searchParams.set('token', '***')
|
|
108
|
+
}
|
|
109
|
+
return urlObj.toString()
|
|
110
|
+
})()
|
|
111
|
+
|
|
112
|
+
console.log(`Connected to LAPLACE Event Bridge: ${displayUrl}`)
|
|
103
113
|
resolve()
|
|
104
114
|
}
|
|
105
115
|
|
|
@@ -107,6 +117,19 @@ export class LaplaceEventBridgeClient {
|
|
|
107
117
|
try {
|
|
108
118
|
const data = JSON.parse(event.data)
|
|
109
119
|
|
|
120
|
+
// Handle ping from server
|
|
121
|
+
if (data.type === 'ping') {
|
|
122
|
+
// Respond with pong
|
|
123
|
+
this.ws?.send(
|
|
124
|
+
JSON.stringify({
|
|
125
|
+
type: 'pong',
|
|
126
|
+
timestamp: Date.now(),
|
|
127
|
+
respondingTo: data.timestamp,
|
|
128
|
+
})
|
|
129
|
+
)
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
|
|
110
133
|
// Store client ID from the established message
|
|
111
134
|
if (data.type === 'established' && data.clientId) {
|
|
112
135
|
this.clientId = data.clientId
|