dsclaw 0.1.6 → 0.1.7

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.js CHANGED
@@ -29,14 +29,15 @@ import pino from "pino";
29
29
  function createLogger(module) {
30
30
  return rootLogger.child({ module });
31
31
  }
32
- var rootLogger;
32
+ var isPkg, rootLogger;
33
33
  var init_logger = __esm({
34
34
  "src/shared/logger.ts"() {
35
35
  "use strict";
36
36
  init_tracer();
37
+ isPkg = typeof process.pkg !== "undefined";
37
38
  rootLogger = pino({
38
39
  level: process.env["LOG_LEVEL"] ?? "info",
39
- transport: process.env["NODE_ENV"] !== "production" ? { target: "pino-pretty", options: { colorize: true } } : void 0,
40
+ transport: !isPkg && process.env["NODE_ENV"] !== "production" ? { target: "pino-pretty", options: { colorize: true } } : void 0,
40
41
  mixin() {
41
42
  const ctx = getTraceContext();
42
43
  return {
@@ -763,18 +764,20 @@ var CANDIDATES = [
763
764
  }
764
765
  }
765
766
  ];
766
- function findChromium() {
767
+ function findAllChromium() {
767
768
  const platform = process.platform;
769
+ const found = [];
768
770
  for (const candidate of CANDIDATES) {
769
771
  const platformPaths = candidate.paths[platform];
770
772
  if (!platformPaths) continue;
771
773
  for (const p of platformPaths) {
772
774
  if (existsSync5(p)) {
773
- return { name: candidate.name, executablePath: p };
775
+ found.push({ name: candidate.name, executablePath: p });
776
+ break;
774
777
  }
775
778
  }
776
779
  }
777
- return null;
780
+ return found;
778
781
  }
779
782
 
780
783
  // src/dsers/browser-auth.ts
@@ -811,15 +814,11 @@ function parseWsUrl(stderrChunk) {
811
814
  const match = stderrChunk.match(/DevTools listening on (ws:\/\/[^\s]+)/);
812
815
  return match?.[1] ?? null;
813
816
  }
814
- function launchBrowser() {
815
- const browser = findChromium();
816
- if (!browser) {
817
- throw new Error("No Chromium-based browser found. Please install Chrome, Edge, or Brave.");
818
- }
817
+ function tryLaunchBrowser(browser) {
819
818
  const tmpDir = mkdtempSync(join5(tmpdir(), "dsclaw-auth-"));
820
819
  activeTmpDir = tmpDir;
821
820
  log6.info({ browser: browser.name }, "Launching browser for DSers auth");
822
- const proc = spawn(browser.executablePath, [
821
+ const args = [
823
822
  `--app=${DSERS_LOGIN_URL}`,
824
823
  "--remote-debugging-port=0",
825
824
  `--user-data-dir=${tmpDir}`,
@@ -828,7 +827,15 @@ function launchBrowser() {
828
827
  "--disable-extensions",
829
828
  "--mute-audio",
830
829
  "--window-size=480,700"
831
- ], {
830
+ ];
831
+ if (process.platform === "win32") {
832
+ args.push(
833
+ "--disable-background-mode",
834
+ "--disable-backgrounding-occluded-windows",
835
+ "--disable-renderer-backgrounding"
836
+ );
837
+ }
838
+ const proc = spawn(browser.executablePath, args, {
832
839
  stdio: ["ignore", "ignore", "pipe"],
833
840
  detached: false
834
841
  });
@@ -947,32 +954,58 @@ function watchLoginResponse(wsUrl) {
947
954
  });
948
955
  });
949
956
  }
957
+ async function attemptLoginWithBrowser(browser) {
958
+ const { proc, wsUrlPromise } = tryLaunchBrowser(browser);
959
+ proc.on("exit", () => {
960
+ activeProcess = null;
961
+ });
962
+ const wsUrl = await wsUrlPromise;
963
+ log6.info({ wsUrl }, "DevTools endpoint acquired");
964
+ const httpUrl = wsUrl.replace("ws://", "http://").replace(/\/devtools\/browser\/.*/, "/json");
965
+ const resp = await fetch(httpUrl);
966
+ const tabs = await resp.json();
967
+ const dsersTab = tabs.find((t2) => t2.url.includes("dsers.com")) ?? tabs[0];
968
+ if (!dsersTab?.webSocketDebuggerUrl) {
969
+ throw new Error("Could not find browser tab");
970
+ }
971
+ log6.info({ tabUrl: dsersTab.url }, "Watching tab for login API response");
972
+ const result = await watchLoginResponse(dsersTab.webSocketDebuggerUrl);
973
+ cleanup();
974
+ return result;
975
+ }
950
976
  async function loginViaCDP() {
951
977
  if (isAuthInProgress()) {
952
978
  throw new Error("Auth already in progress");
953
979
  }
954
- try {
955
- const { proc, wsUrlPromise } = launchBrowser();
956
- proc.on("exit", () => {
957
- activeProcess = null;
958
- });
959
- const wsUrl = await wsUrlPromise;
960
- log6.info({ wsUrl }, "DevTools endpoint acquired");
961
- const httpUrl = wsUrl.replace("ws://", "http://").replace(/\/devtools\/browser\/.*/, "/json");
962
- const resp = await fetch(httpUrl);
963
- const tabs = await resp.json();
964
- const dsersTab = tabs.find((t2) => t2.url.includes("dsers.com")) ?? tabs[0];
965
- if (!dsersTab?.webSocketDebuggerUrl) {
966
- throw new Error("Could not find browser tab");
967
- }
968
- log6.info({ tabUrl: dsersTab.url }, "Watching tab for login API response");
969
- const result = await watchLoginResponse(dsersTab.webSocketDebuggerUrl);
970
- cleanup();
971
- return result;
972
- } catch (error) {
973
- cleanup();
974
- throw error;
980
+ const browsers = findAllChromium();
981
+ if (!browsers.length) {
982
+ throw new Error("No Chromium-based browser found. Please install Chrome, Edge, or Brave.");
983
+ }
984
+ for (let i = 0; i < browsers.length; i++) {
985
+ const browser = browsers[i];
986
+ const nextBrowser = browsers[i + 1];
987
+ try {
988
+ return await attemptLoginWithBrowser(browser);
989
+ } catch (error) {
990
+ cleanup();
991
+ const msg = error instanceof Error ? error.message : String(error);
992
+ const isLaunchFailure = msg.includes("Browser exited before DevTools was ready") || msg.includes("Timed out waiting for Chrome DevTools");
993
+ if (isLaunchFailure && nextBrowser) {
994
+ log6.warn(
995
+ { browser: browser.name, error: msg, fallback: nextBrowser.name },
996
+ "Browser launch failed, trying next browser"
997
+ );
998
+ continue;
999
+ }
1000
+ if (isLaunchFailure && process.platform === "win32") {
1001
+ throw new Error(
1002
+ `Could not launch browser for DSers login. This usually happens when ${browser.name} is already running. Please close all ${browser.name} windows completely (including the system tray icon) and try again.`
1003
+ );
1004
+ }
1005
+ throw error;
1006
+ }
975
1007
  }
1008
+ throw new Error("All browsers failed to launch for DSers auth.");
976
1009
  }
977
1010
 
978
1011
  // src/agents/core-agent.ts