Skip to content

Commit 1c64d0c

Browse files
committed
fix: resolve Minicap resource loading in ESM and CommonJS environments
1 parent 42ef4db commit 1c64d0c

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# CHANGELOG
22

3-
## V5.1.0 (2025-07-01)
3+
## V5.1.1 (2025-07-03)
4+
* fix Minicap resource loading issue in nodeJS using npm:import-meta-resolve
5+
6+
## V5.1.0 (2025-07-02)
47
* remove some dev depencences from depencences
58
* Allow authorization of known public keys in the tcp usb bridge [#540](https://github.com/DeviceFarmer/adbkit/pull/540)
69
* Add attach / detach commands [#534](https://github.com/DeviceFarmer/adbkit/pull/534)

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@u4/adbkit",
3-
"version": "5.1.0",
3+
"version": "5.1.1",
44
"description": "A Typescript client for the Android Debug Bridge.",
55
"type": "module",
66
"keywords": [
@@ -69,6 +69,7 @@
6969
"commander": "14.0.0",
7070
"debug": "~4.4.1",
7171
"get-port": "7.1.0",
72+
"import-meta-resolve": "^4.1.0",
7273
"node-forge": "^1.3.1",
7374
"promise-duplex": "^8.0.0",
7475
"promise-readable": "^8.0.1",
@@ -77,7 +78,6 @@
7778
},
7879
"devDependencies": {
7980
"@jest/globals": "^30.0.3",
80-
"jest": "^30.0.3",
8181
"@types/debug": "^4.1.12",
8282
"@types/jest": "^30.0.0",
8383
"@types/mocha": "^10.0.10",
@@ -89,6 +89,7 @@
8989
"@typescript-eslint/parser": "^8.35.1",
9090
"chai": "~5.2.0",
9191
"eslint": "^9.30.1",
92+
"jest": "^30.0.3",
9293
"mocha": "~11.7.1",
9394
"picocolors": "^1.1.1",
9495
"prettier": "^3.6.2",

src/adb/thirdparty/minicap/Minicap.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import PromiseDuplex from 'promise-duplex';
88
import ThirdUtils from "../ThirdUtils.js";
99
import Utils from '../../utils.js';
1010
import Stats from '../../sync/stats.js';
11+
import { resolve } from 'import-meta-resolve';
1112

1213
/**
1314
* Application binary interface known CPU
@@ -30,6 +31,23 @@ interface IEmissions {
3031
error: (error: Error) => void
3132
disconnect: (cause: string) => void
3233
}
34+
// fs.ReadStream |
35+
function getResource(path: string): string | null {
36+
try {
37+
return require.resolve(path);
38+
} catch (e) {
39+
try {
40+
if (e instanceof Error && e.message.includes('require is not defined')) {
41+
return resolve(path, import.meta.url);
42+
// .pathname;
43+
// return new URL(path, import.meta.url).pathname;
44+
}
45+
} catch (e) {
46+
return null;
47+
}
48+
return null;
49+
}
50+
}
3351

3452
export default class Minicap extends EventEmitter {
3553
private config: MinicapOptions;
@@ -133,29 +151,28 @@ export default class Minicap extends EventEmitter {
133151
const sdkLevel = parseInt(props['ro.build.version.sdk']);
134152
const minicapName = (sdkLevel >= 16) ? 'minicap' : 'minicap-nopie';
135153

136-
let binFile: string;
137-
let soFile = '';
154+
let binFile: string | null = null;
155+
let soFile: string | null = null;
138156

139-
try {
140-
binFile = require.resolve(`@devicefarmer/minicap-prebuilt/prebuilt/${abi}/bin/${minicapName}`);
141-
} catch (e) {
142-
throw Error(`minicap not found in @devicefarmer/minicap-prebuilt/prebuilt/${abi}/bin/ please install @devicefarmer/minicap-prebuilt to use minicap ${e}`);
157+
158+
binFile = getResource(`@devicefarmer/minicap-prebuilt/prebuilt/${abi}/bin/${minicapName}`);
159+
if (!binFile)
160+
throw Error(`minicap not found in @devicefarmer/minicap-prebuilt/prebuilt/${abi}/bin/ please install @devicefarmer/minicap-prebuilt to use minicap`);
161+
162+
if (sdkLevel === 32) {
163+
soFile = getResource(`@u4/minicap-prebuilt/prebuilt/${abi}/lib/android-${sdkLevel}/minicap.so`);
164+
} else {
165+
soFile = getResource(`@devicefarmer/minicap-prebuilt/prebuilt/${abi}/lib/android-${sdkLevel}/minicap.so`);
143166
}
144167

145-
try {
146-
if (sdkLevel === 32) {
147-
soFile = require.resolve(`@u4/minicap-prebuilt/prebuilt/${abi}/lib/android-${sdkLevel}/minicap.so`);
148-
} else {
149-
soFile = require.resolve(`@devicefarmer/minicap-prebuilt/prebuilt/${abi}/lib/android-${sdkLevel}/minicap.so`);
150-
}
151-
} catch (e) {
152-
throw Error(`minicap.so for your device check for @devicefarmer/minicap-prebuilt update that support android-${sdkLevel}, ${soFile} is missing ${e}`);
168+
if (!soFile) {
169+
throw Error(`minicap.so for your device check for @devicefarmer/minicap-prebuilt update that support android-${sdkLevel}, ${soFile} is missing`);
153170
}
154171

155172
// only upload minicap binary in tmp filder if file is missing
156173
try {
157174
await this.client.stat('/data/local/tmp/minicap');
158-
debug(`/data/local/tmp/minicap already presentin ${this.client.serial}`)
175+
debug(`/data/local/tmp/minicap already present in ${this.client.serial}`)
159176
} catch {
160177
debug(`pushing minicap binary to ${this.client.serial}`)
161178
const tr = await this.client.push(binFile, '/data/local/tmp/minicap', 0o755);

0 commit comments

Comments
 (0)