dsh-relay-phone 0.18.1 → 0.18.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-relay-phone",
3
- "version": "0.18.1",
3
+ "version": "0.18.3",
4
4
  "description": "Remote-control your local DeepSeek Harness from a phone through your own zero-knowledge relay: phone-first page, QR pairing, end-to-end encryption, approvals, full desktop UI fallback.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -472,32 +472,44 @@ export function buildBootSource(id, gate) {
472
472
  try { userHandlers[i](ev) } catch (e) { console.error(e) }
473
473
  }
474
474
  }
475
- function deliverSealed(b64) {
475
+ // Binary is a real message kind here: the client sets binaryType="arraybuffer"
476
+ // and the account avatar arrives that way. Decoding those bytes as text both
477
+ // corrupts them and makes the host reject the frame, which is what killed the
478
+ // mux when the account panel opened.
479
+ function deliverSealed(b64, isBinary) {
476
480
  var bin = atob(b64)
477
481
  var bytes = new Uint8Array(bin.length)
478
482
  for (var i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i)
479
483
  var plain = openEnvelope(bytes)
480
484
  if (!plain) return
485
+ if (isBinary) {
486
+ var copy = new Uint8Array(plain.length)
487
+ copy.set(plain)
488
+ return deliverRaw(copy.buffer)
489
+ }
481
490
  deliverRaw(new TextDecoder().decode(plain))
482
491
  }
483
492
  target.addEventListener('message', function (ev) {
484
493
  if (typeof ev.data !== 'string') return deliverRaw(ev.data)
485
494
  try {
486
495
  var parsed = JSON.parse(ev.data)
487
- if (parsed && typeof parsed.__relay === 'string') return deliverSealed(parsed.__relay)
496
+ if (parsed && typeof parsed.__relay === 'string') {
497
+ return deliverSealed(parsed.__relay, parsed.__bin === 1)
498
+ }
488
499
  } catch (e) {}
489
500
  deliverRaw(ev.data)
490
501
  })
491
502
 
492
503
  var origSend = target.send.bind(target)
493
504
  target.send = function (data) {
494
- var bytes = typeof data === 'string'
495
- ? new TextEncoder().encode(data)
496
- : new Uint8Array(data)
505
+ var isBinary = typeof data !== 'string'
506
+ var bytes = isBinary ? new Uint8Array(data) : new TextEncoder().encode(data)
497
507
  var sealed = sealEnvelope(bytes)
498
508
  var bin = ''
499
509
  for (var i = 0; i < sealed.length; i++) bin += String.fromCharCode(sealed[i])
500
- origSend(JSON.stringify({ __relay: btoa(bin) }))
510
+ var wrapped = { __relay: btoa(bin) }
511
+ if (isBinary) wrapped.__bin = 1
512
+ origSend(JSON.stringify(wrapped))
501
513
  }
502
514
  Object.defineProperty(target, 'onmessage', {
503
515
  get: function () { return userListener },
package/src/pump.js CHANGED
@@ -992,6 +992,10 @@ export class PumpManager {
992
992
  upstream.setNoDelay(true)
993
993
  upstream.on('connect', () => {
994
994
  upstream.write(out)
995
+ // Frames are NOT flushed here: the host has not answered 101 yet, and a
996
+ // byte written before the switch looks like a pipelined HTTP request to
997
+ // it. Whether the browser's first frame beat the 101 was a race, which is
998
+ // why the mux sometimes sat silent and the page then retried.
995
999
  this.pumpPhoneWs(sid, stream)
996
1000
  })
997
1001
  upstream.on('data', (data) => {
@@ -1005,6 +1009,8 @@ export class PumpManager {
1005
1009
  this.client?.sendStream(sid, stream.wsServerBuf.subarray(0, idx + 4))
1006
1010
  stream.wsServerBuf = stream.wsServerBuf.subarray(idx + 4)
1007
1011
  stream.wsHandshakeDone = true
1012
+ // The switch is real now: release whatever the phone already sent.
1013
+ this.pumpPhoneWs(sid, stream)
1008
1014
  }
1009
1015
  this.pumpServerWs(sid, stream)
1010
1016
  })
@@ -1084,6 +1090,8 @@ export class PumpManager {
1084
1090
  // -- websocket sealing ---------------------------------------------------------------
1085
1091
  pumpPhoneWs(sid, stream) {
1086
1092
  if (!stream.upstream || stream.upstream.destroyed) return
1093
+ // Not before the host answered 101: see the comment at the upgrade handler.
1094
+ if (!stream.wsHandshakeDone) return
1087
1095
  for (;;) {
1088
1096
  const parsed = parseFrame(stream.wsPhoneBuf)
1089
1097
  if (!parsed) return
@@ -1092,10 +1100,12 @@ export class PumpManager {
1092
1100
  let out
1093
1101
  if (isDataFrame(frame.opcode)) {
1094
1102
  let plain = null
1103
+ let binary = false
1095
1104
  try {
1096
1105
  const json = JSON.parse(frame.payload.toString('utf8'))
1097
1106
  if (json && typeof json.__relay === 'string') {
1098
1107
  plain = this.cryptoBox.open(this.cryptoBox.envelopeKey, Buffer.from(json.__relay, 'base64'))
1108
+ binary = json.__bin === 1
1099
1109
  }
1100
1110
  } catch {}
1101
1111
  if (!plain) {
@@ -1109,10 +1119,10 @@ export class PumpManager {
1109
1119
  }
1110
1120
  plain = frame.payload
1111
1121
  }
1112
- // DSH 0.2's API gateway closes a binary frame with 1003 ("text
1113
- // messages required"). The boot patch always sends JSON text, so
1114
- // re-frame the decrypted payload as text upstream.
1115
- out = buildFrame(OPCODES.OP_TEXT, plain, { mask: true })
1122
+ // Text stays text (the JSON protocol requires it) and binary stays binary:
1123
+ // an ArrayBuffer payload forwarded as text is rejected by the host, and a
1124
+ // text payload forwarded as binary is invisible to it.
1125
+ out = buildFrame(binary ? OPCODES.OP_BINARY : OPCODES.OP_TEXT, plain, { mask: true })
1116
1126
  } else {
1117
1127
  out = buildFrame(frame.opcode, frame.payload, { mask: true, fin: frame.fin })
1118
1128
  }
@@ -1141,7 +1151,13 @@ export class PumpManager {
1141
1151
  : frame.payload
1142
1152
  stream.wsFragment = null
1143
1153
  const sealed = this.cryptoBox.seal(this.cryptoBox.envelopeKey, payload)
1144
- const json = Buffer.from(JSON.stringify({ __relay: sealed.toString('base64') }))
1154
+ // Carry the message kind across the seal. The client sets
1155
+ // binaryType="arraybuffer" and the account avatar arrives as binary;
1156
+ // decoding it as text corrupts the bytes and the app then breaks the
1157
+ // connection.
1158
+ const wrapped = { __relay: sealed.toString('base64') }
1159
+ if (frame.opcode === OPCODES.OP_BINARY) wrapped.__bin = 1
1160
+ const json = Buffer.from(JSON.stringify(wrapped))
1145
1161
  this.client?.sendStream(sid, buildFrame(OPCODES.OP_TEXT, json, { mask: false }))
1146
1162
  continue
1147
1163
  }