playcanvas-agent-bridge-cli 0.2.1 → 0.3.0

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.
@@ -15,10 +15,56 @@
15
15
 
16
16
  let ws = null;
17
17
  let reconnectTimer = null;
18
+ let heartbeatTimer = null;
19
+ let stopped = false;
18
20
  let config = DEFAULT_CONFIG;
19
21
  let tabInfo = {};
22
+ let extensionVersion = null;
20
23
  const pending = new Map();
21
24
 
25
+ function isExtensionContextInvalidated(error) {
26
+ return String((error && error.message) || error)
27
+ .toLowerCase()
28
+ .includes("extension context invalidated");
29
+ }
30
+
31
+ function stopBridge() {
32
+ if (stopped) return;
33
+ stopped = true;
34
+ if (reconnectTimer) {
35
+ clearTimeout(reconnectTimer);
36
+ reconnectTimer = null;
37
+ }
38
+ if (heartbeatTimer) {
39
+ clearInterval(heartbeatTimer);
40
+ heartbeatTimer = null;
41
+ }
42
+ for (const waiter of pending.values()) {
43
+ clearTimeout(waiter.timer);
44
+ waiter.reject(new Error("Extension context invalidated."));
45
+ }
46
+ pending.clear();
47
+ if (ws) {
48
+ try {
49
+ ws.close();
50
+ } catch {
51
+ // The extension context is already gone.
52
+ }
53
+ ws = null;
54
+ }
55
+ }
56
+
57
+ function getExtensionVersion() {
58
+ if (extensionVersion) return extensionVersion;
59
+ try {
60
+ extensionVersion = chrome.runtime.getManifest().version;
61
+ return extensionVersion;
62
+ } catch (error) {
63
+ if (isExtensionContextInvalidated(error)) stopBridge();
64
+ return null;
65
+ }
66
+ }
67
+
22
68
  function requestId() {
23
69
  if (crypto && crypto.randomUUID) return crypto.randomUUID();
24
70
  return "req-" + Date.now().toString(36) + "-" + Math.random().toString(36).slice(2);
@@ -36,11 +82,28 @@
36
82
 
37
83
  function requestRuntime(message) {
38
84
  return new Promise((resolve) => {
85
+ if (stopped) {
86
+ resolve(null);
87
+ return;
88
+ }
39
89
  try {
40
90
  chrome.runtime.sendMessage(message, (response) => {
91
+ try {
92
+ const lastError = chrome.runtime.lastError;
93
+ if (lastError) {
94
+ if (isExtensionContextInvalidated(lastError)) stopBridge();
95
+ resolve(null);
96
+ return;
97
+ }
98
+ } catch (error) {
99
+ if (isExtensionContextInvalidated(error)) stopBridge();
100
+ resolve(null);
101
+ return;
102
+ }
41
103
  resolve(response || null);
42
104
  });
43
- } catch {
105
+ } catch (error) {
106
+ if (isExtensionContextInvalidated(error)) stopBridge();
44
107
  resolve(null);
45
108
  }
46
109
  });
@@ -76,6 +139,13 @@
76
139
  const message = event.data;
77
140
  if (!message || message.channel !== CHANNEL || message.side !== "main") return;
78
141
 
142
+ if (message.type === "ready") {
143
+ sendTargetUpdate().catch((error) => {
144
+ if (isExtensionContextInvalidated(error)) stopBridge();
145
+ });
146
+ return;
147
+ }
148
+
79
149
  if (message.type === "response" && message.id) {
80
150
  const waiter = pending.get(message.id);
81
151
  if (!waiter) return;
@@ -111,6 +181,7 @@
111
181
  clientId,
112
182
  tabId: tabInfo.tabId,
113
183
  windowId: tabInfo.windowId,
184
+ extensionVersion: getExtensionVersion(),
114
185
  ...(response.data || {}),
115
186
  url: (response.data && response.data.url) || location.href,
116
187
  title: (response.data && response.data.title) || document.title || "",
@@ -121,6 +192,7 @@
121
192
  clientId,
122
193
  tabId: tabInfo.tabId,
123
194
  windowId: tabInfo.windowId,
195
+ extensionVersion: getExtensionVersion(),
124
196
  url: location.href,
125
197
  title: document.title || "",
126
198
  ready: false
@@ -129,16 +201,21 @@
129
201
  }
130
202
 
131
203
  async function sendTargetUpdate() {
132
- if (!ws || ws.readyState !== WebSocket.OPEN) return;
133
- ws.send(
134
- JSON.stringify({
135
- type: "target:update",
136
- target: await describeTarget()
137
- })
138
- );
204
+ if (stopped || !ws || ws.readyState !== WebSocket.OPEN) return;
205
+ try {
206
+ ws.send(
207
+ JSON.stringify({
208
+ type: "target:update",
209
+ target: await describeTarget()
210
+ })
211
+ );
212
+ } catch (error) {
213
+ if (isExtensionContextInvalidated(error)) stopBridge();
214
+ }
139
215
  }
140
216
 
141
217
  function scheduleReconnect() {
218
+ if (stopped) return;
142
219
  if (reconnectTimer) return;
143
220
  reconnectTimer = setTimeout(() => {
144
221
  reconnectTimer = null;
@@ -147,6 +224,7 @@
147
224
  }
148
225
 
149
226
  function connect() {
227
+ if (stopped) return;
150
228
  if (ws && (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING)) {
151
229
  return;
152
230
  }
@@ -164,7 +242,9 @@
164
242
 
165
243
  ws.addEventListener("open", () => {
166
244
  console.info("[pcbridge] connected to local daemon");
167
- sendTargetUpdate();
245
+ sendTargetUpdate().catch((error) => {
246
+ if (isExtensionContextInvalidated(error)) stopBridge();
247
+ });
168
248
  });
169
249
 
170
250
  ws.addEventListener("message", async (event) => {
@@ -179,24 +259,32 @@
179
259
 
180
260
  try {
181
261
  const response = await callMain(message.method, message.params || {}, message.timeoutMs);
182
- ws.send(
183
- JSON.stringify({
184
- type: "response",
185
- id: message.id,
186
- ok: true,
187
- data: response.data,
188
- meta: response.meta || {}
189
- })
190
- );
262
+ if (!stopped && ws && ws.readyState === WebSocket.OPEN) {
263
+ ws.send(
264
+ JSON.stringify({
265
+ type: "response",
266
+ id: message.id,
267
+ ok: true,
268
+ data: response.data,
269
+ meta: response.meta || {}
270
+ })
271
+ );
272
+ }
191
273
  } catch (error) {
192
- ws.send(
193
- JSON.stringify({
194
- type: "response",
195
- id: message.id,
196
- ok: false,
197
- error: serializeError(error)
198
- })
199
- );
274
+ if (isExtensionContextInvalidated(error)) {
275
+ stopBridge();
276
+ return;
277
+ }
278
+ if (!stopped && ws && ws.readyState === WebSocket.OPEN) {
279
+ ws.send(
280
+ JSON.stringify({
281
+ type: "response",
282
+ id: message.id,
283
+ ok: false,
284
+ error: serializeError(error)
285
+ })
286
+ );
287
+ }
200
288
  }
201
289
  });
202
290
 
@@ -215,10 +303,20 @@
215
303
 
216
304
  async function start() {
217
305
  config = await loadConfig();
306
+ if (stopped) return;
307
+ extensionVersion = getExtensionVersion();
308
+ if (stopped) return;
218
309
  tabInfo = await getTabInfo();
310
+ if (stopped) return;
219
311
  connect();
220
- setInterval(sendTargetUpdate, 2000);
312
+ heartbeatTimer = setInterval(() => {
313
+ sendTargetUpdate().catch((error) => {
314
+ if (isExtensionContextInvalidated(error)) stopBridge();
315
+ });
316
+ }, 2000);
221
317
  }
222
318
 
223
- start();
319
+ start().catch((error) => {
320
+ if (isExtensionContextInvalidated(error)) stopBridge();
321
+ });
224
322
  })();