@mcesystems/adb-kit 1.0.22 → 1.0.24

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
@@ -6155,7 +6155,7 @@ var require_has_flag = __commonJS({
6155
6155
  var require_supports_color = __commonJS({
6156
6156
  "../../node_modules/.pnpm/supports-color@7.2.0/node_modules/supports-color/index.js"(exports2, module2) {
6157
6157
  "use strict";
6158
- var os = require("os");
6158
+ var os2 = require("os");
6159
6159
  var tty = require("tty");
6160
6160
  var hasFlag = require_has_flag();
6161
6161
  var { env: env2 } = process;
@@ -6203,7 +6203,7 @@ var require_supports_color = __commonJS({
6203
6203
  return min;
6204
6204
  }
6205
6205
  if (process.platform === "win32") {
6206
- const osRelease = os.release().split(".");
6206
+ const osRelease = os2.release().split(".");
6207
6207
  if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
6208
6208
  return Number(osRelease[2]) >= 14931 ? 3 : 2;
6209
6209
  }
@@ -29717,13 +29717,13 @@ var require_client2 = __commonJS({
29717
29717
  var server_1 = __importDefault(require_server());
29718
29718
  var DeviceClient_1 = __importDefault(require_DeviceClient());
29719
29719
  var Client = class extends events_1.EventEmitter {
29720
- constructor({ host = "127.0.0.1", port = 5037, bin = "adb", timeout = 0 } = { port: 5037 }) {
29720
+ constructor({ host = "127.0.0.1", port = 5037, bin = "adb", timeout: timeout2 = 0 } = { port: 5037 }) {
29721
29721
  super();
29722
29722
  this.host = host;
29723
29723
  this.port = port;
29724
29724
  this.bin = bin;
29725
- this.timeout = timeout;
29726
- this.options = { host, port, bin, timeout };
29725
+ this.timeout = timeout2;
29726
+ this.options = { host, port, bin, timeout: timeout2 };
29727
29727
  }
29728
29728
  createTcpUsbBridge(serial, options) {
29729
29729
  return new server_1.default(this, serial, options);
@@ -30715,6 +30715,141 @@ module.exports = __toCommonJS(index_exports);
30715
30715
  // src/logic/adbDeviceKit.ts
30716
30716
  var adbkit = __toESM(require_dist());
30717
30717
 
30718
+ // ../../node_modules/.pnpm/get-port@7.1.0/node_modules/get-port/index.js
30719
+ var import_node_net = __toESM(require("node:net"), 1);
30720
+ var import_node_os = __toESM(require("node:os"), 1);
30721
+ var Locked = class extends Error {
30722
+ constructor(port) {
30723
+ super(`${port} is locked`);
30724
+ }
30725
+ };
30726
+ var lockedPorts = {
30727
+ old: /* @__PURE__ */ new Set(),
30728
+ young: /* @__PURE__ */ new Set()
30729
+ };
30730
+ var releaseOldLockedPortsIntervalMs = 1e3 * 15;
30731
+ var minPort = 1024;
30732
+ var maxPort = 65535;
30733
+ var timeout;
30734
+ var getLocalHosts = () => {
30735
+ const interfaces = import_node_os.default.networkInterfaces();
30736
+ const results = /* @__PURE__ */ new Set([void 0, "0.0.0.0"]);
30737
+ for (const _interface of Object.values(interfaces)) {
30738
+ for (const config of _interface) {
30739
+ results.add(config.address);
30740
+ }
30741
+ }
30742
+ return results;
30743
+ };
30744
+ var checkAvailablePort = (options) => new Promise((resolve, reject) => {
30745
+ const server = import_node_net.default.createServer();
30746
+ server.unref();
30747
+ server.on("error", reject);
30748
+ server.listen(options, () => {
30749
+ const { port } = server.address();
30750
+ server.close(() => {
30751
+ resolve(port);
30752
+ });
30753
+ });
30754
+ });
30755
+ var getAvailablePort = async (options, hosts) => {
30756
+ if (options.host || options.port === 0) {
30757
+ return checkAvailablePort(options);
30758
+ }
30759
+ for (const host of hosts) {
30760
+ try {
30761
+ await checkAvailablePort({ port: options.port, host });
30762
+ } catch (error) {
30763
+ if (!["EADDRNOTAVAIL", "EINVAL"].includes(error.code)) {
30764
+ throw error;
30765
+ }
30766
+ }
30767
+ }
30768
+ return options.port;
30769
+ };
30770
+ var portCheckSequence = function* (ports) {
30771
+ if (ports) {
30772
+ yield* ports;
30773
+ }
30774
+ yield 0;
30775
+ };
30776
+ async function getPorts(options) {
30777
+ let ports;
30778
+ let exclude = /* @__PURE__ */ new Set();
30779
+ if (options) {
30780
+ if (options.port) {
30781
+ ports = typeof options.port === "number" ? [options.port] : options.port;
30782
+ }
30783
+ if (options.exclude) {
30784
+ const excludeIterable = options.exclude;
30785
+ if (typeof excludeIterable[Symbol.iterator] !== "function") {
30786
+ throw new TypeError("The `exclude` option must be an iterable.");
30787
+ }
30788
+ for (const element of excludeIterable) {
30789
+ if (typeof element !== "number") {
30790
+ throw new TypeError("Each item in the `exclude` option must be a number corresponding to the port you want excluded.");
30791
+ }
30792
+ if (!Number.isSafeInteger(element)) {
30793
+ throw new TypeError(`Number ${element} in the exclude option is not a safe integer and can't be used`);
30794
+ }
30795
+ }
30796
+ exclude = new Set(excludeIterable);
30797
+ }
30798
+ }
30799
+ if (timeout === void 0) {
30800
+ timeout = setTimeout(() => {
30801
+ timeout = void 0;
30802
+ lockedPorts.old = lockedPorts.young;
30803
+ lockedPorts.young = /* @__PURE__ */ new Set();
30804
+ }, releaseOldLockedPortsIntervalMs);
30805
+ if (timeout.unref) {
30806
+ timeout.unref();
30807
+ }
30808
+ }
30809
+ const hosts = getLocalHosts();
30810
+ for (const port of portCheckSequence(ports)) {
30811
+ try {
30812
+ if (exclude.has(port)) {
30813
+ continue;
30814
+ }
30815
+ let availablePort = await getAvailablePort({ ...options, port }, hosts);
30816
+ while (lockedPorts.old.has(availablePort) || lockedPorts.young.has(availablePort)) {
30817
+ if (port !== 0) {
30818
+ throw new Locked(port);
30819
+ }
30820
+ availablePort = await getAvailablePort({ ...options, port }, hosts);
30821
+ }
30822
+ lockedPorts.young.add(availablePort);
30823
+ return availablePort;
30824
+ } catch (error) {
30825
+ if (!["EADDRINUSE", "EACCES"].includes(error.code) && !(error instanceof Locked)) {
30826
+ throw error;
30827
+ }
30828
+ }
30829
+ }
30830
+ throw new Error("No available ports found");
30831
+ }
30832
+ function portNumbers(from, to) {
30833
+ if (!Number.isInteger(from) || !Number.isInteger(to)) {
30834
+ throw new TypeError("`from` and `to` must be integer numbers");
30835
+ }
30836
+ if (from < minPort || from > maxPort) {
30837
+ throw new RangeError(`'from' must be between ${minPort} and ${maxPort}`);
30838
+ }
30839
+ if (to < minPort || to > maxPort) {
30840
+ throw new RangeError(`'to' must be between ${minPort} and ${maxPort}`);
30841
+ }
30842
+ if (from > to) {
30843
+ throw new RangeError("`to` must be greater than or equal to `from`");
30844
+ }
30845
+ const generator = function* (from2, to2) {
30846
+ for (let port = from2; port <= to2; port++) {
30847
+ yield port;
30848
+ }
30849
+ };
30850
+ return generator(from, to);
30851
+ }
30852
+
30718
30853
  // src/utils/adbPath.ts
30719
30854
  var import_node_fs = require("node:fs");
30720
30855
  var import_node_path = __toESM(require("node:path"));
@@ -30819,6 +30954,7 @@ var AdbDeviceKit = class {
30819
30954
  client;
30820
30955
  device;
30821
30956
  deviceId;
30957
+ devicePort = null;
30822
30958
  async listDevices() {
30823
30959
  const devices = await this.client.listDevices();
30824
30960
  return devices;
@@ -30833,7 +30969,8 @@ var AdbDeviceKit = class {
30833
30969
  chunks.push(chunk);
30834
30970
  }
30835
30971
  return Buffer.concat(chunks).toString().trim();
30836
- } catch {
30972
+ } catch (error) {
30973
+ logError(`Error getting property ${property} for device ${this.deviceId}: ${error}`);
30837
30974
  return "";
30838
30975
  }
30839
30976
  }
@@ -30886,7 +31023,7 @@ var AdbDeviceKit = class {
30886
31023
  const devices = await this.listDevices();
30887
31024
  return !!devices.find((device) => device.id === this.deviceId);
30888
31025
  }
30889
- async waitForUsbDebugging(timeout = 12e4) {
31026
+ async waitForUsbDebugging(timeout2 = 12e4) {
30890
31027
  if (await this.hasUsbDebugging()) {
30891
31028
  return true;
30892
31029
  }
@@ -30894,7 +31031,7 @@ var AdbDeviceKit = class {
30894
31031
  return new Promise((resolve, reject) => {
30895
31032
  const timeoutId = setTimeout(() => {
30896
31033
  reject(new Error("Timeout waiting for USB debugging"));
30897
- }, timeout);
31034
+ }, timeout2);
30898
31035
  tracker.on("remove", (_) => {
30899
31036
  clearTimeout(timeoutId);
30900
31037
  });
@@ -30914,6 +31051,16 @@ var AdbDeviceKit = class {
30914
31051
  });
30915
31052
  });
30916
31053
  }
31054
+ async startPortForward(serviceName) {
31055
+ if (this.devicePort) {
31056
+ return this.devicePort;
31057
+ }
31058
+ const port = await getPorts({ port: portNumbers(3e4, 31e3) });
31059
+ if (await this.device.forward(`tcp:${port}`, serviceName)) {
31060
+ this.devicePort = port;
31061
+ }
31062
+ return this.devicePort;
31063
+ }
30917
31064
  getDeviceId() {
30918
31065
  return this.deviceId;
30919
31066
  }