@velmlabs/unstuck-core 0.2.4 → 0.2.6

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/dist/index.d.ts CHANGED
@@ -80,7 +80,7 @@ declare function probeCapabilities(cdp: CdpClient, opts?: {
80
80
  }): Promise<Capabilities>;
81
81
 
82
82
  /** Replays one viewer input message into the page via CDP Input.* */
83
- declare function applyInput(cdp: CdpClient, msg: ViewerInput, caps?: Capabilities): Promise<void>;
83
+ declare function applyInput(cdp: Pick<CdpClient, 'send'>, msg: ViewerInput, caps?: Capabilities): Promise<void>;
84
84
 
85
85
  interface FrameSink {
86
86
  send(b: Uint8Array): void;
package/dist/index.js CHANGED
@@ -709,6 +709,8 @@ var HELLO_TIMEOUT_MS = 1e4;
709
709
  var MAX_CODE_ATTEMPTS = 5;
710
710
  var DRAIN_INTERVAL_MS = 50;
711
711
  var STOP_TIMEOUT_MS = 2e3;
712
+ var MAX_PENDING_INPUT = 256;
713
+ var DROPPED = new Error("input dropped");
712
714
  var FINAL_EVENT = {
713
715
  resolved: "done",
714
716
  cancelled: "cancel",
@@ -762,7 +764,11 @@ function runTakeover(deps, cdp, opts) {
762
764
  } };
763
765
  let offPopups = () => {
764
766
  };
765
- let inputChain = Promise.resolve();
767
+ const pendingInput = [];
768
+ let pumping = false;
769
+ let inFlight = null;
770
+ let inputDropped = 0;
771
+ let pressed = null;
766
772
  const helloTimers = /* @__PURE__ */ new Set();
767
773
  let resolveResult;
768
774
  const result = new Promise((r) => resolveResult = r);
@@ -814,20 +820,83 @@ function runTakeover(deps, cdp, opts) {
814
820
  sendJson(viewer, { t: "state", status });
815
821
  viewer.close(1e3, status);
816
822
  }
823
+ inputDropped += pendingInput.splice(0).length;
824
+ const lastDispatch = inFlight;
817
825
  const durationS = Math.round(now() - startedAt) / 1e3;
818
826
  void (async () => {
827
+ await bounded((async () => {
828
+ await lastDispatch;
829
+ await releasePressed();
830
+ })());
819
831
  await stopStream();
820
- let unlockTimer;
821
- await Promise.race([Promise.all(locks.map((l) => l?.stop())), new Promise((r) => unlockTimer = setTimeout(r, STOP_TIMEOUT_MS))]);
822
- clearTimeout(unlockTimer);
832
+ await bounded(Promise.all(locks.map((l) => l?.stop())));
823
833
  if (published) await transport.unpublish(sessionId).catch(() => {
824
834
  });
825
- emit("finished", { status: final, durationS, inputCount, ...logged ?? (detail ? { detail } : {}) });
835
+ emit("finished", { status: final, durationS, inputCount, ...inputDropped ? { inputDropped } : {}, ...logged ?? (detail ? { detail } : {}) });
826
836
  const fallbacks = [!caps.screencast && "screenshots", !caps.insertText && "per-key typing"].filter(Boolean).join(", ");
827
837
  const shown = detail ?? (fallbacks ? `fallback: ${fallbacks}` : void 0);
828
838
  resolveResult({ status: final, sessionId, url, durationS, ...shown ? { detail: shown } : {} });
829
839
  })();
830
840
  }
841
+ async function bounded(p) {
842
+ let timer;
843
+ await Promise.race([p, new Promise((r) => timer = setTimeout(r, STOP_TIMEOUT_MS))]);
844
+ clearTimeout(timer);
845
+ }
846
+ async function releasePressed() {
847
+ const p = pressed;
848
+ pressed = null;
849
+ if (!p) return;
850
+ const { x, y, button, clickCount } = p;
851
+ await p.target.cdp.send("Input.dispatchMouseEvent", { type: "mouseReleased", x, y, button, clickCount, modifiers: 0 }).catch(() => {
852
+ });
853
+ }
854
+ function enqueueInput(conn, input) {
855
+ if (isTerminal(status) || conn !== holder) return void inputDropped++;
856
+ if (pendingInput.length >= MAX_PENDING_INPUT) {
857
+ let i = pendingInput.findIndex((p) => p.input.t === "wheel" || p.input.t === "mouse" && p.input.action === "move");
858
+ if (i < 0 && isRelease(input)) i = pendingInput.findIndex((p) => !isRelease(p.input));
859
+ inputDropped++;
860
+ if (i < 0) return;
861
+ pendingInput.splice(i, 1);
862
+ }
863
+ pendingInput.push({ conn, input });
864
+ if (!pumping) void pumpInput();
865
+ }
866
+ async function pumpInput() {
867
+ pumping = true;
868
+ for (let next = pendingInput.shift(); next; next = pendingInput.shift()) {
869
+ inFlight = dispatchInput(next.conn, next.input);
870
+ await inFlight;
871
+ }
872
+ inFlight = null;
873
+ pumping = false;
874
+ }
875
+ async function dispatchInput(conn, input) {
876
+ const live = () => !isTerminal(status) && conn === holder;
877
+ if (!live()) return void inputDropped++;
878
+ const target = active();
879
+ const guarded = {
880
+ send: (method, params) => live() ? target.cdp.send(method, params) : Promise.reject(DROPPED)
881
+ };
882
+ inputCount++;
883
+ try {
884
+ const msgs = input.t === "key" ? translateShortcut(input, await browserPlatformIsMac(target.cdp)) : [input];
885
+ for (const m of msgs) {
886
+ await applyInput(guarded, m, target.caps);
887
+ if (m.t === "mouse") trackButton(target, m);
888
+ }
889
+ } catch (e) {
890
+ if (e === DROPPED) return;
891
+ return onTargetError(target, e);
892
+ }
893
+ if (live()) fieldFeed.onInput(input);
894
+ }
895
+ function trackButton(target, m) {
896
+ if (m.action === "down") pressed = { target, x: m.x, y: m.y, button: m.button, clickCount: m.clickCount };
897
+ else if (m.action === "up") pressed = null;
898
+ else if (pressed) pressed = { ...pressed, x: m.x, y: m.y };
899
+ }
831
900
  async function attachViewer(conn, vn) {
832
901
  const previous = holder;
833
902
  holder = conn;
@@ -992,18 +1061,7 @@ function runTakeover(deps, cdp, opts) {
992
1061
  }
993
1062
  default: {
994
1063
  const safe = safeInput(msg);
995
- if (!safe) return;
996
- inputCount++;
997
- inputChain = inputChain.then(async () => {
998
- const target = active();
999
- try {
1000
- const msgs = safe.t === "key" ? translateShortcut(safe, await browserPlatformIsMac(target.cdp)) : [safe];
1001
- for (const m of msgs) await applyInput(target.cdp, m, target.caps);
1002
- } catch (e) {
1003
- return onTargetError(target, e);
1004
- }
1005
- fieldFeed.onInput(safe);
1006
- });
1064
+ if (safe) enqueueInput(conn, safe);
1007
1065
  }
1008
1066
  }
1009
1067
  }
@@ -1090,6 +1148,9 @@ function runTakeover(deps, cdp, opts) {
1090
1148
  }
1091
1149
  return { result, cancel: (detail) => finish("cancelled", detail), fail, joined };
1092
1150
  }
1151
+ function isRelease(m) {
1152
+ return m.t === "mouse" && m.action === "up";
1153
+ }
1093
1154
  function newCode() {
1094
1155
  const buf = new Uint32Array(1);
1095
1156
  do
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@velmlabs/unstuck-core",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "The Unstuck takeover engine core: screencast, input, sessions and transports, usable in Node or Cloudflare Workers.",
5
5
  "keywords": [
6
6
  "ai",
@@ -47,7 +47,7 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "tldts": "^7.5.2",
50
- "@velmlabs/unstuck-protocol": "0.2.4"
50
+ "@velmlabs/unstuck-protocol": "0.2.6"
51
51
  },
52
52
  "publishConfig": {
53
53
  "access": "public"