@kelnishi/satmouse-client 0.15.1 → 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,111 +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
- /**
266
- * @param signalingUrl — HTTP endpoint for SDP exchange (e.g., "http://127.0.0.1:18945/rtc/offer")
267
- */
268
- constructor(signalingUrl) {
269
- this.signalingUrl = signalingUrl;
263
+ messageHandler = null;
264
+ static isAvailable() {
265
+ return !!globalThis.__satmouseExtensionAvailable;
270
266
  }
271
267
  async connect() {
272
- if (typeof globalThis.RTCPeerConnection === "undefined") {
273
- throw new Error("RTCPeerConnection not available");
268
+ if (typeof globalThis.postMessage !== "function") {
269
+ throw new Error("postMessage not available");
274
270
  }
275
- this.pc = new RTCPeerConnection({
276
- iceServers: []
277
- // Local network, no STUN/TURN
278
- });
279
- this.pc.ondatachannel = (event) => {
280
- const channel = event.channel;
281
- if (channel.label === "spatial") {
282
- channel.binaryType = "arraybuffer";
283
- channel.onmessage = (e) => {
284
- if (e.data instanceof ArrayBuffer && e.data.byteLength >= 20) {
285
- const decoded = decodeBinaryFrame(e.data);
286
- this.onSpatialData?.(decoded);
287
- }
288
- };
289
- } else if (channel.label === "reliable") {
290
- channel.onmessage = (e) => {
291
- try {
292
- const text = typeof e.data === "string" ? e.data : new TextDecoder().decode(e.data);
293
- const msg = JSON.parse(text);
294
- if (msg.type === "buttonEvent") this.onButtonEvent?.(msg.data);
295
- else if (msg.type === "deviceStatus") this.onDeviceStatus?.(msg.data.event, msg.data.device);
296
- } catch {
297
- }
298
- };
299
- }
300
- };
301
- this.pc.onconnectionstatechange = () => {
302
- const state = this.pc?.connectionState;
303
- if (state === "disconnected" || state === "failed" || state === "closed") {
304
- this.onClose?.();
305
- }
306
- };
307
- const offer = await this.pc.createOffer();
308
- await this.pc.setLocalDescription(offer);
309
- await this.waitForICE();
310
- const response = await fetch(this.signalingUrl, {
311
- method: "POST",
312
- body: this.pc.localDescription.sdp,
313
- headers: { "Content-Type": "application/sdp" }
314
- });
315
- if (!response.ok) {
316
- throw new Error(`Signaling failed: ${response.status}`);
317
- }
318
- const answerSdp = await response.text();
319
- await this.pc.setRemoteDescription({ type: "answer", sdp: answerSdp });
320
- await new Promise((resolve, reject) => {
321
- const timeout = setTimeout(() => reject(new Error("WebRTC connection timeout")), 1e4);
322
- const check = () => {
323
- if (this.pc?.connectionState === "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) {
324
280
  clearTimeout(timeout);
325
281
  resolve();
326
- } else if (this.pc?.connectionState === "failed") {
327
- clearTimeout(timeout);
328
- reject(new Error("WebRTC connection failed"));
329
282
  }
330
- };
331
- this.pc.onconnectionstatechange = () => {
332
- check();
333
- const state = this.pc?.connectionState;
334
- if (state === "disconnected" || state === "failed" || state === "closed") {
283
+ if (msg.type === "disconnected") {
335
284
  this.onClose?.();
336
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);
294
+ }
337
295
  };
338
- check();
296
+ globalThis.addEventListener("message", this.messageHandler);
297
+ globalThis.postMessage({ target: "satmouse-extension", action: "connect" }, "*");
339
298
  });
340
299
  }
341
300
  close() {
342
- try {
343
- this.pc?.close();
344
- } catch {
301
+ if (this.messageHandler) {
302
+ globalThis.removeEventListener("message", this.messageHandler);
303
+ this.messageHandler = null;
345
304
  }
346
- this.pc = null;
347
- }
348
- waitForICE() {
349
- return new Promise((resolve) => {
350
- if (!this.pc) return resolve();
351
- if (this.pc.iceGatheringState === "complete") return resolve();
352
- const timeout = setTimeout(resolve, 2e3);
353
- this.pc.onicegatheringstatechange = () => {
354
- if (this.pc?.iceGatheringState === "complete") {
355
- clearTimeout(timeout);
356
- resolve();
357
- }
358
- };
359
- });
305
+ globalThis.postMessage({ target: "satmouse-extension", action: "disconnect" }, "*");
360
306
  }
361
307
  };
362
308
 
@@ -373,7 +319,7 @@ function parseSatMouseUri(uri) {
373
319
  };
374
320
  }
375
321
  var DEFAULT_OPTIONS = {
376
- transports: ["webtransport", "webrtc", "websocket"],
322
+ transports: ["webtransport", "extension", "websocket"],
377
323
  reconnectDelay: 2e3,
378
324
  maxRetries: 3,
379
325
  wsSubprotocol: "satmouse-json"
@@ -445,11 +391,10 @@ var SatMouseConnection = class extends TypedEmitter {
445
391
  continue;
446
392
  }
447
393
  }
448
- if (proto === "webrtc") {
394
+ if (proto === "extension") {
449
395
  try {
450
- if (typeof globalThis.RTCPeerConnection === "undefined") continue;
451
- const rtcUrl = this.options.rtcUrl ?? `http://127.0.0.1:18945/rtc/offer`;
452
- const adapter = new WebRTCAdapter(rtcUrl);
396
+ if (!ExtensionAdapter.isAvailable()) continue;
397
+ const adapter = new ExtensionAdapter();
453
398
  if (await this.tryTransport(adapter)) return;
454
399
  } catch {
455
400
  continue;