peer-term 1.2.10 → 1.2.11

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +38 -14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "peer-term",
3
- "version": "1.2.10",
3
+ "version": "1.2.11",
4
4
  "license": "MIT",
5
5
  "description": "Share your terminal instantly using a 6-digit code. Encrypted. No config.",
6
6
  "keywords": [
package/src/index.js CHANGED
@@ -648,13 +648,10 @@ class Session {
648
648
  try {
649
649
  const resizeJson = JSON.stringify({ type: 'resize', cols, rows });
650
650
  const payload = await encrypt(this.sharedKey, resizeJson);
651
- // Phase 4: Route through DataChannel if active, else relay
652
- if (this.useDataChannel && this.webrtc && this.webrtc.isActive()) {
653
- // Send resize as a tagged message so client can distinguish
654
- this.webrtc.send(JSON.stringify({ _meta: 'resize', payload }));
655
- } else if (this.ws && this.ws.readyState === WebSocket.OPEN) {
656
- this.ws.send(JSON.stringify({ type: 'data', payload, meta: 'resize' }));
657
- }
651
+ this._sendEncryptedToClient(payload, {
652
+ directPayload: JSON.stringify({ _meta: 'resize', payload }),
653
+ meta: 'resize',
654
+ });
658
655
  } catch {}
659
656
  }
660
657
 
@@ -746,14 +743,41 @@ class Session {
746
743
  const payload = await encrypt(sharedKey, data);
747
744
  if (this.destroyed || sharedKey !== this.sharedKey) return;
748
745
 
749
- // Phase 4: Route through DataChannel if active, else relay
750
- if (this.useDataChannel && this.webrtc && this.webrtc.isActive()) {
751
- this.webrtc.send(payload);
752
- } else if (this.ws && this.ws.readyState === WebSocket.OPEN) {
753
- // Backpressure: drop chunk if relay can't keep up (1.25 MB buffer limit)
754
- if (this.ws.bufferedAmount < 1.25 * 1024 * 1024) {
755
- this.ws.send(JSON.stringify({ type: 'data', payload }));
746
+ this._sendEncryptedToClient(payload, { enforceBackpressure: true });
747
+ }
748
+
749
+ _sendEncryptedToClient(payload, options = {}) {
750
+ const directPayload = options.directPayload || payload;
751
+
752
+ if (this.useDataChannel) {
753
+ if (this.webrtc && this.webrtc.isActive()) {
754
+ let sentDirect = false;
755
+ try {
756
+ sentDirect = this.webrtc.send(directPayload);
757
+ } catch {}
758
+
759
+ if (sentDirect) return true;
756
760
  }
761
+
762
+ this.useDataChannel = false;
763
+ this.log('DataChannel send failed — falling back to relay');
764
+ }
765
+
766
+ return this._sendViaRelay(payload, options);
767
+ }
768
+
769
+ _sendViaRelay(payload, { meta, enforceBackpressure = false } = {}) {
770
+ if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return false;
771
+ if (enforceBackpressure && this.ws.bufferedAmount >= 1.25 * 1024 * 1024) return false;
772
+
773
+ const msg = { type: 'data', payload };
774
+ if (meta) msg.meta = meta;
775
+
776
+ try {
777
+ this.ws.send(JSON.stringify(msg));
778
+ return true;
779
+ } catch {
780
+ return false;
757
781
  }
758
782
  }
759
783