@kelnishi/satmouse-client 0.15.2 → 0.16.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.
@@ -252,133 +252,57 @@ var WebSocketAdapter = class {
252
252
  }
253
253
  };
254
254
 
255
- // src/core/transports/webrtc.ts
256
- var WebRTCAdapter = class {
257
- protocol = "webrtc";
255
+ // src/core/transports/extension.ts
256
+ var ExtensionAdapter = class {
257
+ protocol = "extension";
258
258
  onSpatialData = null;
259
259
  onButtonEvent = null;
260
260
  onDeviceStatus = null;
261
261
  onClose = null;
262
262
  onError = null;
263
- pc = null;
264
- signalingUrl;
265
- constructor(signalingUrl) {
266
- this.signalingUrl = signalingUrl;
263
+ messageHandler = null;
264
+ static isAvailable() {
265
+ return !!globalThis.__satmouseExtensionAvailable;
267
266
  }
268
267
  async connect() {
269
- if (typeof globalThis.RTCPeerConnection === "undefined") {
270
- throw new Error("RTCPeerConnection not available");
268
+ if (typeof globalThis.postMessage !== "function") {
269
+ throw new Error("postMessage not available");
271
270
  }
272
- this.pc = new RTCPeerConnection({ iceServers: [] });
273
- this.pc.ondatachannel = (event) => {
274
- const channel = event.channel;
275
- if (channel.label === "spatial") {
276
- channel.binaryType = "arraybuffer";
277
- channel.onmessage = (e) => {
278
- if (e.data instanceof ArrayBuffer && e.data.byteLength >= 20) {
279
- this.onSpatialData?.(decodeBinaryFrame(e.data));
280
- }
281
- };
282
- } else if (channel.label === "reliable") {
283
- channel.onmessage = (e) => {
284
- try {
285
- const text = typeof e.data === "string" ? e.data : new TextDecoder().decode(e.data);
286
- const msg = JSON.parse(text);
287
- if (msg.type === "buttonEvent") this.onButtonEvent?.(msg.data);
288
- else if (msg.type === "deviceStatus") this.onDeviceStatus?.(msg.data.event, msg.data.device);
289
- } catch {
290
- }
291
- };
292
- }
293
- };
294
- this.pc.onconnectionstatechange = () => {
295
- const state = this.pc?.connectionState;
296
- if (state === "disconnected" || state === "failed" || state === "closed") {
297
- this.onClose?.();
298
- }
299
- };
300
- const offer = await this.pc.createOffer();
301
- await this.pc.setLocalDescription(offer);
302
- await this.waitForICE();
303
- const answerSdp = await this.exchangeSDP(this.pc.localDescription.sdp);
304
- await this.pc.setRemoteDescription({ type: "answer", sdp: answerSdp });
305
- await new Promise((resolve, reject) => {
306
- const timeout = setTimeout(() => reject(new Error("WebRTC connection timeout")), 1e4);
307
- const check = () => {
308
- const state = this.pc?.connectionState;
309
- if (state === "connected") {
271
+ return new Promise((resolve, reject) => {
272
+ const timeout = setTimeout(() => {
273
+ this.close();
274
+ reject(new Error("Extension connection timeout"));
275
+ }, 5e3);
276
+ this.messageHandler = (event) => {
277
+ if (event.data?.source !== "satmouse-extension") return;
278
+ const msg = event.data;
279
+ if ((msg.type === "connected" || msg.type === "bridgeConnected") && timeout) {
310
280
  clearTimeout(timeout);
311
281
  resolve();
312
- } else if (state === "failed") {
313
- clearTimeout(timeout);
314
- reject(new Error("WebRTC connection failed"));
282
+ }
283
+ if (msg.type === "disconnected") {
284
+ this.onClose?.();
285
+ }
286
+ if (msg.type === "spatialData" && msg.data) {
287
+ this.onSpatialData?.(msg.data);
288
+ }
289
+ if (msg.type === "buttonEvent" && msg.data) {
290
+ this.onButtonEvent?.(msg.data);
291
+ }
292
+ if (msg.type === "deviceStatus" && msg.data) {
293
+ this.onDeviceStatus?.(msg.data.event, msg.data.device);
315
294
  }
316
295
  };
317
- this.pc.onconnectionstatechange = () => {
318
- check();
319
- const state = this.pc?.connectionState;
320
- if (state === "disconnected" || state === "failed" || state === "closed") this.onClose?.();
321
- };
322
- check();
296
+ globalThis.addEventListener("message", this.messageHandler);
297
+ globalThis.postMessage({ target: "satmouse-extension", action: "connect" }, "*");
323
298
  });
324
299
  }
325
300
  close() {
326
- try {
327
- this.pc?.close();
328
- } catch {
329
- }
330
- this.pc = null;
331
- }
332
- /** Exchange SDP: try fetch POST, fall back to popup window */
333
- async exchangeSDP(offerSdp) {
334
- try {
335
- const res = await fetch(this.signalingUrl, {
336
- method: "POST",
337
- body: offerSdp,
338
- headers: { "Content-Type": "application/sdp" }
339
- });
340
- if (res.ok) return await res.text();
341
- } catch {
301
+ if (this.messageHandler) {
302
+ globalThis.removeEventListener("message", this.messageHandler);
303
+ this.messageHandler = null;
342
304
  }
343
- return this.exchangeSDPViaPopup(offerSdp);
344
- }
345
- exchangeSDPViaPopup(offerSdp) {
346
- return new Promise((resolve, reject) => {
347
- const offerB64 = btoa(offerSdp);
348
- const baseUrl = this.signalingUrl.replace("/rtc/offer", "/rtc/connect-popup");
349
- const url = `${baseUrl}?offer=${encodeURIComponent(offerB64)}`;
350
- const popup = globalThis.open(url, "satmouse-rtc", "width=1,height=1,left=-100,top=-100");
351
- if (!popup) {
352
- reject(new Error("Popup blocked \u2014 user interaction required"));
353
- return;
354
- }
355
- const timeout = setTimeout(() => {
356
- popup.close();
357
- reject(new Error("WebRTC signaling timeout"));
358
- }, 1e4);
359
- const onMessage = (event) => {
360
- if (event.data?.type === "satmouse-rtc-answer") {
361
- clearTimeout(timeout);
362
- globalThis.removeEventListener("message", onMessage);
363
- popup.close();
364
- resolve(event.data.answer);
365
- }
366
- };
367
- globalThis.addEventListener("message", onMessage);
368
- });
369
- }
370
- waitForICE() {
371
- return new Promise((resolve) => {
372
- if (!this.pc) return resolve();
373
- if (this.pc.iceGatheringState === "complete") return resolve();
374
- const timeout = setTimeout(resolve, 2e3);
375
- this.pc.onicegatheringstatechange = () => {
376
- if (this.pc?.iceGatheringState === "complete") {
377
- clearTimeout(timeout);
378
- resolve();
379
- }
380
- };
381
- });
305
+ globalThis.postMessage({ target: "satmouse-extension", action: "disconnect" }, "*");
382
306
  }
383
307
  };
384
308
 
@@ -395,7 +319,7 @@ function parseSatMouseUri(uri) {
395
319
  };
396
320
  }
397
321
  var DEFAULT_OPTIONS = {
398
- transports: ["webtransport", "webrtc", "websocket"],
322
+ transports: ["webtransport", "extension", "websocket"],
399
323
  reconnectDelay: 2e3,
400
324
  maxRetries: 3,
401
325
  wsSubprotocol: "satmouse-json"
@@ -467,11 +391,10 @@ var SatMouseConnection = class extends TypedEmitter {
467
391
  continue;
468
392
  }
469
393
  }
470
- if (proto === "webrtc") {
394
+ if (proto === "extension") {
471
395
  try {
472
- if (typeof globalThis.RTCPeerConnection === "undefined") continue;
473
- const rtcUrl = this.options.rtcUrl ?? `http://127.0.0.1:18945/rtc/offer`;
474
- const adapter = new WebRTCAdapter(rtcUrl);
396
+ if (!ExtensionAdapter.isAvailable()) continue;
397
+ const adapter = new ExtensionAdapter();
475
398
  if (await this.tryTransport(adapter)) return;
476
399
  } catch {
477
400
  continue;