@mcesystems/adb-kit 1.0.68 → 1.0.69

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.mjs CHANGED
@@ -29851,7 +29851,7 @@ var require_adb = __commonJS({
29851
29851
  Object.defineProperty(exports2, "__esModule", { value: true });
29852
29852
  var client_1 = __importDefault(require_client2());
29853
29853
  var util_1 = __importDefault(require_util3());
29854
- var Adb = class {
29854
+ var Adb2 = class {
29855
29855
  static createClient(options = {}) {
29856
29856
  const opts = {
29857
29857
  bin: options.bin,
@@ -29868,8 +29868,8 @@ var require_adb = __commonJS({
29868
29868
  return new client_1.default(opts);
29869
29869
  }
29870
29870
  };
29871
- Adb.util = util_1.default;
29872
- exports2.default = Adb;
29871
+ Adb2.util = util_1.default;
29872
+ exports2.default = Adb2;
29873
29873
  }
29874
29874
  });
29875
29875
 
@@ -31281,6 +31281,23 @@ function ensureAdbPathFromResources() {
31281
31281
  logInfo(`Using ADB from resources: ${adbPath}`);
31282
31282
  }
31283
31283
  }
31284
+ function isConnectionErrorMessage(err) {
31285
+ const msg = err.message ?? "";
31286
+ const code = err.code ?? "";
31287
+ const name = err.name ?? "";
31288
+ return msg.includes("ECONNRESET") || msg.includes("EPIPE") || msg.includes("Premature end of stream") || code === "ECONNRESET" || code === "EPIPE" || name === "PrematureEOFError";
31289
+ }
31290
+ var connectionErrorHandlerInstalled = false;
31291
+ function installConnectionErrorHandler() {
31292
+ if (connectionErrorHandlerInstalled) return;
31293
+ connectionErrorHandlerInstalled = true;
31294
+ process.on("unhandledRejection", (reason) => {
31295
+ if (reason instanceof Error && isConnectionErrorMessage(reason)) {
31296
+ logDetail(`Suppressed expected ADB connection error: ${reason.message}`);
31297
+ return;
31298
+ }
31299
+ });
31300
+ }
31284
31301
  var deviceProps = {
31285
31302
  Manufacturer: "ro.product.manufacturer",
31286
31303
  Name: "ro.product.name",
@@ -31315,9 +31332,8 @@ var AdbDeviceKit = class {
31315
31332
  setLogLevel(process.env.LOG_LEVEL ?? "none");
31316
31333
  logNamespace(`adb:${deviceId}`);
31317
31334
  ensureAdbPathFromResources();
31318
- this.client = adbkit.default.createClient({
31319
- bin: process.env.ADB_PATH ?? getAdbBinaryPath() ?? "."
31320
- });
31335
+ installConnectionErrorHandler();
31336
+ this.client = this.connect();
31321
31337
  this.deviceId = deviceId.split("\\").pop() ?? deviceId;
31322
31338
  logInfo(`Device ID: ${this.deviceId}`);
31323
31339
  this.device = this.client.getDevice(this.deviceId);
@@ -31326,6 +31342,18 @@ var AdbDeviceKit = class {
31326
31342
  device;
31327
31343
  deviceId;
31328
31344
  devicePort = null;
31345
+ connect() {
31346
+ const adbBin = process.env.ADB_PATH ?? getAdbBinaryPath() ?? "adb";
31347
+ const client = adbkit.default.createClient({ bin: adbBin });
31348
+ const errorHandler = (err) => {
31349
+ logError(`ADB client connection error (e.g. device disconnected): ${err.message}`);
31350
+ client.removeListener("error", errorHandler);
31351
+ this.client = this.connect();
31352
+ this.device = this.client.getDevice(this.deviceId);
31353
+ };
31354
+ client.on("error", errorHandler);
31355
+ return client;
31356
+ }
31329
31357
  async listDevices() {
31330
31358
  const devices = await this.client.listDevices();
31331
31359
  return devices;
@@ -31378,39 +31406,84 @@ var AdbDeviceKit = class {
31378
31406
  const devices = await this.listDevices();
31379
31407
  return !!devices.find((device) => device.id === this.deviceId);
31380
31408
  }
31381
- async waitForUsbDebugging(timeout2 = 12e4, numberOfAllowedAttempts = 5) {
31409
+ /**
31410
+ * Creates an isolated ADB client for tracking operations.
31411
+ * This client is separate from the main client so connection errors
31412
+ * during tracking don't affect other operations.
31413
+ */
31414
+ createTrackingClient() {
31415
+ const adbBin = process.env.ADB_PATH ?? getAdbBinaryPath() ?? "adb";
31416
+ return adbkit.default.createClient({ bin: adbBin });
31417
+ }
31418
+ async waitForUsbDebugging(timeout2 = 12e4, _numberOfAllowedAttempts = 5) {
31382
31419
  if (await this.hasUsbDebugging()) {
31383
31420
  logDetail("USB debugging is already enabled");
31384
31421
  return true;
31385
31422
  }
31386
- let count = numberOfAllowedAttempts;
31387
- const tracker = await this.client.trackDevices();
31423
+ const trackingClient = this.createTrackingClient();
31424
+ const tracker = await trackingClient.trackDevices();
31425
+ let settled = false;
31388
31426
  return new Promise((resolve, reject) => {
31389
31427
  const timeoutId = setTimeout(() => {
31390
31428
  logError("Timeout waiting for USB debugging");
31391
- reject(new Error("Timeout waiting for USB debugging"));
31429
+ settle({ type: "reject", err: new Error("Timeout waiting for USB debugging") });
31392
31430
  }, timeout2);
31393
- tracker.on("remove", (_) => {
31431
+ const cleanupTrackingClient = () => {
31432
+ try {
31433
+ tracker.end();
31434
+ } catch {
31435
+ }
31436
+ trackingClient.on("error", () => {
31437
+ });
31438
+ };
31439
+ const settle = (result) => {
31440
+ if (settled) return;
31441
+ settled = true;
31394
31442
  clearTimeout(timeoutId);
31443
+ cleanupTrackingClient();
31444
+ if (result.type === "resolve") {
31445
+ resolve(result.value);
31446
+ } else {
31447
+ logError(result.err.message);
31448
+ reject(result.err);
31449
+ }
31450
+ };
31451
+ trackingClient.on("error", (err) => {
31452
+ if (isConnectionErrorMessage(err)) {
31453
+ logDetail(`Tracking client connection closed: ${err.message}`);
31454
+ settle({ type: "resolve", value: false });
31455
+ } else {
31456
+ logError(`Tracking client error: ${err.message}`);
31457
+ settle({ type: "reject", err });
31458
+ }
31459
+ });
31460
+ tracker.on("error", (err) => {
31461
+ if (isConnectionErrorMessage(err)) {
31462
+ logDetail(`Tracker connection closed: ${err.message}`);
31463
+ settle({ type: "resolve", value: false });
31464
+ } else {
31465
+ logError(`Tracker error: ${err.message}`);
31466
+ settle({ type: "reject", err });
31467
+ }
31468
+ });
31469
+ tracker.on("end", () => {
31470
+ logDetail("Device tracking ended");
31471
+ settle({ type: "resolve", value: false });
31472
+ });
31473
+ tracker.on("remove", (_) => {
31395
31474
  logError("Device removed from tracker");
31475
+ settle({ type: "reject", err: new Error("Device removed while waiting for USB debugging") });
31396
31476
  });
31397
31477
  tracker.on("add", (device) => {
31398
31478
  if (device.type === "device") {
31399
31479
  logDetail("Device added to tracker");
31400
- clearTimeout(timeoutId);
31401
- resolve(true);
31480
+ settle({ type: "resolve", value: true });
31402
31481
  }
31403
31482
  });
31404
31483
  tracker.on("change", (device) => {
31405
- count--;
31406
- if (device.type === "device" && count > 0) {
31407
- clearTimeout(timeoutId);
31484
+ if (device.type === "device") {
31408
31485
  logDetail("Device changed in tracker");
31409
- resolve(true);
31410
- } else if (count === 0) {
31411
- logDetail("No device found in tracker");
31412
- this.client.kill();
31413
- resolve(false);
31486
+ settle({ type: "resolve", value: true });
31414
31487
  }
31415
31488
  });
31416
31489
  });
@@ -31436,7 +31509,12 @@ var AdbDeviceKit = class {
31436
31509
  return this.device;
31437
31510
  }
31438
31511
  };
31512
+
31513
+ // src/index.ts
31514
+ var import_adbkit = __toESM(require_dist());
31515
+ var readAll = import_adbkit.default.util.readAll;
31439
31516
  export {
31440
- AdbDeviceKit
31517
+ AdbDeviceKit,
31518
+ readAll
31441
31519
  };
31442
31520
  //# sourceMappingURL=index.mjs.map