Skip to content
CLDMVPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

12 Commits

Folders and files

Repository files navigation

@cldmv/droidsock

DroidSock Banner

DroidSock is a complete, from-scratch implementation of the Android Debug Bridge (ADB) protocol in Node.js. This library provides full ADB functionality including device connection, RSA authentication, shell command execution, and file transfers - eliminating clicking sounds on Android TV devices!

It talks to devices directly over TCP, with no adb binary or ADB server in between, and composes its protocol layers into a single API tree with @cldmv/slothlet, so every connected device becomes its own persistent API leaf.

ADB over the wire, in pure Node.js - no adb binary required.

npm version npm downloads GitHub downloads Last commit npm last update coverage

Note

Current status:

  • Shell + streaming: Stable - command execution, interactive shells, and log / process streaming all work over the real ADB protocol.
  • File transfer: mkdir / remove / move / copy / chmod / diskUsage / find / stat work today via shell commands. list prefers a binary-safe SYNC-based implementation with automatic shell fallback.
  • Experimental: push / pull / pushV2 / pullV2 / listSync / listV2 / statV2 (real ADB SYNC sub-protocol usage, both the legacy 32-bit and newer 64-bit variants), device.reboot(), device.forward() / device.reverse(), device.install() (both the classic push-then-install and modern streaming install paths), and pairing.pair() (Wi-Fi pairing) are all implemented - built from the ADB protocol spec and covered by unit tests (several exercised against real loopback TCP / TLS servers, not purely mocks) - but none of them have been run against a real device yet. See #1.

Contributors Sponsor shinrai


✨ What's New

Latest: v2.3.0 (November 2026)

  • CommonJS entry is the generic shim - require("@cldmv/droidsock") returns the same createDroidSock function as before, still carrying the createDroidSock, DroidSock, ADB and AndroidDebugBridge properties, and now .default as well, so destructuring keeps working (#118, #122).
  • Superseded CI runs are cancelled - ci.yml adopts the v4.46.0 concurrency block (#119).
  • View full v2.3.0 Changelog

Recent Releases

  • v2.2.1 (November 2026) - @cldmv/slothlet kept below 3.24.0 so device.connect() no longer throws TypeError: Illegal invocation; npm run test:dist runs the suite against the build (Changelog)
  • v2.2.0 (November 2026) - CLDMV library layout (src/index.mjs / src/index.cjs entries, tsup-built dist/); @cldmv/droidsock/main is a deprecated alias of the root (Changelog)
  • v2.1.0 (October 2026) - device lifecycle events, opt-in auto-reconnect with backoff, TCP keepalive and an optional heartbeat; engines.node rose to >=22.15.0 (Changelog)
  • v2.0.3 (October 2026) - index.cjs loads the ESM entry with a plain require() and fails clearly without require(esm); @cldmv/droidsock/devcheck is no longer exported or published (Changelog)

πŸ“š For complete version history and detailed release notes, see the docs/changelog/ folder.


πŸš€ Key Features

  • βœ… Complete ADB Protocol: TCP connection, CNXN / AUTH handshake, and stream multiplexing implemented from scratch
  • βœ… RSA Authentication: Automatic key generation and ADB-specific signature / public-key formatting
  • βœ… Stream Multiplexing: Multiple concurrent operations over a single connection
  • βœ… Shell Commands: Execute commands, stream output, interactive sessions
  • βœ… File Operations: Shell-based mkdir / remove / move / copy / chmod / diskUsage / find, plus binary-safe SYNC-based list, and experimental push / pull (legacy 32-bit) / pushV2 / pullV2 / listV2 / statV2 (64-bit)
  • βœ… Reboot (experimental): Real reboot: service, including bootloader / recovery / sideload modes
  • βœ… Port Forwarding (experimental): adb forward/adb reverse-equivalent TCP tunneling, both directions
  • βœ… APK Install (experimental): adb install-equivalent local APK installation - classic push-then-install and modern streaming (exec:cmd package install) paths
  • βœ… Wi-Fi Pairing (experimental): adb pair-equivalent PIN-based pairing (SPAKE2-over-Ed25519 + TLS 1.3) for Android 11+ wireless debugging
  • βœ… Device Discovery (experimental): discover.subnet() CIDR sweep and discover.mdns() for devices advertising wireless debugging, plus support for multiple devices via configuration
  • βœ… Error Handling: Robust error handling and connection recovery

πŸ“¦ Installation

Requirements

  • Node.js v22.15.0 or higher (the package's engines.node floor, set by its @cldmv/slothlet dependency)
  • Both import and require() are supported. require() loads the ESM entry through Node's synchronous require(esm), which every supported Node.js version (22.15+) provides; on older versions, load the package with import() instead.
  • Developing this package needs Node.js 22.22.0 or later (devEngines).

Install

npm install @cldmv/droidsock

πŸš€ Quick Start

import droidsock from "@cldmv/droidsock";

// Create the API instance
const api = await droidsock();

// Connect to a device
const device = await api.device.connect("10.6.0.108", 5555);

// Execute a shell command
const output = await device.shell("ls -la");
console.log(output);

// Convenience getters
const model = await device.getModel();
const version = await device.getAndroidVersion();

// Stream commands
const logcat = device.logcat({
	onData: (data) => console.log(data)
});

// Clean up
await device.disconnect();

CommonJS works the same way: const droidsock = require("@cldmv/droidsock") returns the same function, so droidsock(...) works directly. The createDroidSock, DroidSock, ADB and AndroidDebugBridge names and .default are set on it, so const { ADB } = require("@cldmv/droidsock") works too.

@cldmv/droidsock/main is a deprecated alias of @cldmv/droidsock: it exports the same functions and prints a DeprecationWarning (code CLDMV_DROIDSOCK_MAIN) once per process. Import @cldmv/droidsock instead; the alias is removed in the next major version.


βš™οΈ Device Configuration

The example scripts read device addresses from references/devices.json. The folder is git-ignored, so create the file yourself:

{
	"livingroom": {
		"name": "Living Room TV",
		"host": "10.6.0.108",
		"port": 5555,
		"description": "Main living room Android TV"
	},
	"bedroom": {
		"name": "Master Bedroom TV",
		"host": "10.6.0.118",
		"port": 5555,
		"description": "Master bedroom Android TV"
	},
	"default": "livingroom"
}

πŸ“˜ API Reference

droidsock(options) (also createDroidSock) creates the API instance; api.device.connect(host, port, options) connects to a device (IPv4 or IPv6) and returns its live leaf - also reachable afterward at api.devices["<host>_<port>"] (a . becomes _, a : becomes __) - exposing connection state, shell execution / streaming, file operations (push / pull / list / stat), reboot, port forwarding, and APK install. api.device.disconnect(host, port) tears down one device's connection without forgetting it - reconnect later with connect() on the same host:port, no need to re-supply options; api.device.remove(host, port) forgets it entirely. api.devices.list() / disconnect() (all) / remove() (all) / get(idOrLeaf) manage the set of known devices as a whole.

πŸ“š See docs/API.md for the full method reference, including every option and the experimental / scope caveats on push / pull / list / forward / reverse / install.


πŸ’‘ Examples

Basic Usage

# Run basic example with default device
node --conditions=droidsock-dev examples/basic-usage.mjs

# Run with specific device
node --conditions=droidsock-dev examples/basic-usage.mjs livingroom

The examples import @cldmv/droidsock by name. --conditions=droidsock-dev runs them against src/; without it they load the build in dist/ (run npm run build first). See Development.

Streaming Commands

# Stream logcat
node --conditions=droidsock-dev examples/streaming-example.mjs logcat

# Stream top command
node --conditions=droidsock-dev examples/streaming-example.mjs top

# File transfer demo
node --conditions=droidsock-dev examples/streaming-example.mjs files

πŸ—οΈ Architecture

src/index.mjs is the package entry (src/index.cjs is the CommonJS entry, which loads it through require(esm)) and re-exports the factory from src/droidsock.mjs, which composes the layers below into a single api tree via @cldmv/slothlet:

  1. Connection Layer (src/api/connection.mjs): TCP socket + CNXN / AUTH handshake
  2. Authentication Layer (src/api/auth.mjs): RSA key management and ADB signature / public-key formatting
  3. Stream Layer (src/api/stream.mjs): ADB stream multiplexing (OPEN / WRTE / OKAY / CLSE)
  4. Shell Layer (src/api/shell.mjs): Command execution, streaming, and interactive shell APIs
  5. Files Layer (src/api/files.mjs): Shell-based file operations, a binary-safe SYNC LIST implementation with automatic shell fallback, and an experimental ADB SYNC sub-protocol implementation for real binary transfer (push / pull) - not yet validated against a real device
  6. Reboot Layer (src/api/reboot.mjs): Real ADB reboot: service
  7. Forward Layer (src/api/forward.mjs): TCP port forwarding (host β†’ device) via the tcp: service
  8. Reverse Layer (src/api/reverse.mjs): TCP port forwarding (device β†’ host) via reverse:forward:/reverse:killforward: and the Stream layer's device-initiated stream handling
  9. Install Layer (src/api/install.mjs): Local APK install, composed from the Files and Shell layers
  10. Pairing Layer (src/api/pairing.mjs): Wi-Fi pairing (adb pair equivalent) - a separate TLS 1.3 + SPAKE2 protocol reusing the Authentication layer's persistent RSA identity, not composed with any of the layers above
  11. Device / Devices Layers (src/api/device.mjs, src/api/devices.mjs): High-level per-device API composing the layers above, split by single-target (device.connect/disconnect/remove) vs. collection-wide (devices.list/disconnect/remove/get) operations. Each device is a real, persistent slothlet leaf at api.devices.<sanitized host_port>, assigned there by connect() rather than held in a private module variable, so its methods keep working self/context access exactly like any other leaf - the leaf outlives any one connection, and only remove() unmounts it
  12. Config / Log Layers (src/api/config.mjs, src/api/log.mjs): Shared configuration and logging

πŸ“š See docs/PROTOCOL.md for wire-level protocol details (packet structure, auth flow, SYNC sub-protocol framing, reboot / forward service usage).


πŸ›  Troubleshooting

Connection Issues

  • Ensure device is on same network
  • Enable "ADB over network" in developer options
  • Check firewall settings
  • Verify IP address and port

Authentication Issues

  • Delete existing keys to force re-authorization: rm -rf ~/.adb
  • Ensure device shows authorization dialog
  • Check device storage permissions

Common Errors

  • "Command timeout": Increase timeout in options
  • "Stream not open": Ensure connection is established
  • "File not found": Check paths and permissions

πŸ§ͺ Development

The implementation is built directly from the public ADB protocol documentation (AOSP SYNC.TXT and the wire-protocol references), cross-checked against Google's own reference client (google/python-adb) where the public docs are ambiguous, and covered by a mocked Vitest suite. The core connection / shell / stream-multiplexing path has real device usage behind it; the newer SYNC-protocol and service additions (push / pull / listSync / reboot / forward / install) have not yet been run against a real device - see the status note at the top of this README and #1.

The source lives in src/: src/index.mjs is the ESM entry and src/index.cjs the CommonJS entry. npm run build (tsup) writes dist/, which mirrors src/ file by file because slothlet scans dist/api/ at runtime; dist/index.cjs is a verbatim copy of src/index.cjs. Only dist/ and types/ are published.

In a checkout, @cldmv/droidsock resolves to dist/ by default. The droidsock-dev export condition switches it to src/:

Run Resolves to
node app.mjs dist/index.mjs (ERR_MODULE_NOT_FOUND until npm run build)
node --conditions=droidsock-dev app.mjs src/index.mjs
require("@cldmv/droidsock") with the condition src/index.cjs β†’ src/index.mjs

Set it once per shell with export NODE_OPTIONS=--conditions=droidsock-dev. npm test runs the Vitest suite against src/ (the Vitest config passes the condition) and then npm run test:cjs, which builds dist/ and checks it with node --test, and npm run test:dist builds dist/ and runs the Vitest suite against it (npm test includes both).


πŸ“š Documentation

  • API Reference - every method and option, with the experimental and scope caveats
  • Protocol Details - packet structure, auth flow, SYNC sub-protocol framing, reboot / forward service usage
  • Changelog - per-version release notes

CodeFactor OpenSSF Scorecard npms.io score npm unpacked size Repo size


🀝 Contributing

This is a complete implementation of the ADB protocol. For improvements or bug fixes, please submit issues or pull requests.

Contributors Sponsor shinrai


πŸ”— Links


πŸ“„ License

GitHub license npm license

Apache-2.0 - see LICENSE for details.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages