axbridge-api 0.0.0-stage → 0.1.0
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/LICENSE +22 -0
- package/README.md +47 -2
- package/axbridge.d.ts +51 -0
- package/axbridge.js +259 -0
- package/package.json +34 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Reiii3
|
|
4
|
+
Copyright (c) 2025 RiProG-id (original RiProG-ADB-Panel, https://github.com/RiProG-id)
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,48 @@
|
|
|
1
|
-
#
|
|
1
|
+
# axbridge-api
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Browser client for [AxBridge](https://github.com/Reiii3/axbridge), the AxManager plugin that lets a normal
|
|
4
|
+
browser (or a hosted https site) run shell commands on the same Android device.
|
|
5
|
+
|
|
6
|
+
The API has the same shape as the [`kernelsu`](https://www.npmjs.com/package/kernelsu) package, so code written for
|
|
7
|
+
it keeps working.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm i axbridge-api
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { exec, spawn, setToken, ping } from "axbridge-api";
|
|
15
|
+
|
|
16
|
+
if (!(await ping())) throw new Error("AxBridge is not running");
|
|
17
|
+
setToken("<token from the AxBridge WebUI>"); // saved in localStorage
|
|
18
|
+
|
|
19
|
+
const { errno, stdout } = await exec("getprop ro.product.model");
|
|
20
|
+
|
|
21
|
+
const child = spawn("logcat", ["-v", "brief"]);
|
|
22
|
+
child.stdout.on("data", (text) => console.log(text));
|
|
23
|
+
child.on("exit", (code) => console.log("exited", code));
|
|
24
|
+
// child.kill();
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Use it in place of `kernelsu` without touching your imports (Vite):
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
// vite.config.js
|
|
31
|
+
export default { resolve: { alias: { kernelsu: "axbridge-api" } } };
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
| Export | |
|
|
35
|
+
| --- | --- |
|
|
36
|
+
| `exec(cmd, { cwd?, env?, timeout? })` | `Promise<{ errno, stdout, stderr }>` |
|
|
37
|
+
| `spawn(cmd, args?, { cwd?, env?, timeout? })` | `ChildProcess` with `stdout`/`stderr` `on("data")`, `on("exit")`, `on("error")`, `kill()` |
|
|
38
|
+
| `fullScreen(enable)`, `toast(message)` | Browser stand-ins |
|
|
39
|
+
| `ping()`, `setToken()`, `getToken()`, `clearToken()`, `setBaseUrl()` | Connection helpers |
|
|
40
|
+
| `BridgeError` | `error.code`: `network`, `unauthorized`, `forbidden`, `http` |
|
|
41
|
+
|
|
42
|
+
This package is only the client. You also need the AxBridge plugin installed on the phone and your site added to its
|
|
43
|
+
**Allowed websites** list. Setup, security notes and the HTTP protocol are in the
|
|
44
|
+
[main README](https://github.com/Reiii3/axbridge#readme).
|
|
45
|
+
|
|
46
|
+
Requires a server with `/spawn` (plugin v0.1.6 or newer) for `spawn`.
|
|
47
|
+
|
|
48
|
+
MIT. Based on RiProG-ADB-Panel (MIT, © 2025 RiProG-id).
|
package/axbridge.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export interface ExecResult {
|
|
2
|
+
errno: number;
|
|
3
|
+
stdout: string;
|
|
4
|
+
stderr: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface ExecOptions {
|
|
8
|
+
cwd?: string;
|
|
9
|
+
env?: Record<string, string>;
|
|
10
|
+
/** seconds, default 30, max 120 */
|
|
11
|
+
timeout?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface SpawnOptions {
|
|
15
|
+
cwd?: string;
|
|
16
|
+
env?: Record<string, string>;
|
|
17
|
+
/** seconds, default 300, max 3600 */
|
|
18
|
+
timeout?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type BridgeErrorCode = "network" | "unauthorized" | "forbidden" | "http";
|
|
22
|
+
|
|
23
|
+
export class BridgeError extends Error {
|
|
24
|
+
code: BridgeErrorCode;
|
|
25
|
+
constructor(code: BridgeErrorCode, message: string);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface Output {
|
|
29
|
+
on(event: "data", listener: (text: string) => void): this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ChildProcess {
|
|
33
|
+
stdout: Output;
|
|
34
|
+
stderr: Output;
|
|
35
|
+
/** code is null when the process was killed or the connection was cut */
|
|
36
|
+
on(event: "exit", listener: (code: number | null) => void): this;
|
|
37
|
+
on(event: "error", listener: (error: BridgeError | Error) => void): this;
|
|
38
|
+
kill(): void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function exec(command: string, options?: ExecOptions): Promise<ExecResult>;
|
|
42
|
+
export function spawn(command: string, args?: string[], options?: SpawnOptions): ChildProcess;
|
|
43
|
+
export function spawn(command: string, options?: SpawnOptions): ChildProcess;
|
|
44
|
+
export function fullScreen(enable: boolean): Promise<void>;
|
|
45
|
+
export function toast(message: string): void;
|
|
46
|
+
|
|
47
|
+
export function setBaseUrl(url: string): void;
|
|
48
|
+
export function getToken(): string;
|
|
49
|
+
export function setToken(token: string): void;
|
|
50
|
+
export function clearToken(): void;
|
|
51
|
+
export function ping(): Promise<boolean>;
|
package/axbridge.js
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// AxBridge browser client (ES module, no dependencies).
|
|
2
|
+
//
|
|
3
|
+
// Mirrors the `kernelsu` package, so code written for it keeps working:
|
|
4
|
+
//
|
|
5
|
+
// import { exec, spawn, fullScreen, toast } from "axbridge";
|
|
6
|
+
//
|
|
7
|
+
// Extra, AxBridge only: setBaseUrl, getToken, setToken, clearToken, ping, BridgeError.
|
|
8
|
+
|
|
9
|
+
let BASE = "http://127.0.0.1:8787";
|
|
10
|
+
const TOKEN_KEY = "axbridge_token";
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Errors
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Thrown (or emitted as the 'error' event of spawn) when the bridge itself fails.
|
|
18
|
+
* `code` is one of: "network" | "unauthorized" | "forbidden" | "http".
|
|
19
|
+
* A command that runs and fails is NOT an error, it resolves with a non-zero `errno`.
|
|
20
|
+
*/
|
|
21
|
+
export class BridgeError extends Error {
|
|
22
|
+
constructor(code, message) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = "BridgeError";
|
|
25
|
+
this.code = code;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// Connection settings
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
/** Point the client at another address (default http://127.0.0.1:8787). */
|
|
34
|
+
export const setBaseUrl = (url) => {
|
|
35
|
+
BASE = url.replace(/\/+$/, "");
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// localStorage can be blocked (private mode, sandboxed iframe) or missing (Node), so keep a fallback.
|
|
39
|
+
let memoryToken = "";
|
|
40
|
+
|
|
41
|
+
export const getToken = () => {
|
|
42
|
+
try {
|
|
43
|
+
return localStorage.getItem(TOKEN_KEY) || memoryToken;
|
|
44
|
+
} catch {
|
|
45
|
+
return memoryToken;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const setToken = (token) => {
|
|
50
|
+
memoryToken = String(token).trim();
|
|
51
|
+
try {
|
|
52
|
+
localStorage.setItem(TOKEN_KEY, memoryToken);
|
|
53
|
+
} catch {
|
|
54
|
+
/* storage unavailable, the in-memory copy still works for this page load */
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const clearToken = () => {
|
|
59
|
+
memoryToken = "";
|
|
60
|
+
try {
|
|
61
|
+
localStorage.removeItem(TOKEN_KEY);
|
|
62
|
+
} catch {
|
|
63
|
+
/* nothing to clear */
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** true when the AxBridge server answers on this device. */
|
|
68
|
+
export async function ping() {
|
|
69
|
+
try {
|
|
70
|
+
return (await fetch(`${BASE}/health`)).ok;
|
|
71
|
+
} catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
// Requests
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
|
|
80
|
+
async function post(path, body, signal) {
|
|
81
|
+
let res;
|
|
82
|
+
try {
|
|
83
|
+
res = await fetch(`${BASE}${path}`, {
|
|
84
|
+
method: "POST",
|
|
85
|
+
headers: { "Content-Type": "application/json", Authorization: `Bearer ${getToken()}` },
|
|
86
|
+
body: JSON.stringify(body),
|
|
87
|
+
signal,
|
|
88
|
+
});
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (error && error.name === "AbortError") throw error;
|
|
91
|
+
throw new BridgeError("network", "AxBridge is not reachable");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (res.status === 401) throw new BridgeError("unauthorized", "Wrong or missing AxBridge token");
|
|
95
|
+
if (res.status === 403) throw new BridgeError("forbidden", "This website is not in AxBridge's allowed list");
|
|
96
|
+
if (!res.ok) throw new BridgeError("http", `AxBridge HTTP ${res.status}: ${await res.text()}`);
|
|
97
|
+
return res;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Runs a shell command and waits for it to finish.
|
|
102
|
+
* @param {string} command
|
|
103
|
+
* @param {{cwd?: string, env?: Record<string,string>, timeout?: number}} [options]
|
|
104
|
+
* timeout is in seconds (default 30, max 120).
|
|
105
|
+
* @returns {Promise<{errno: number, stdout: string, stderr: string}>}
|
|
106
|
+
*/
|
|
107
|
+
export async function exec(command, options = {}) {
|
|
108
|
+
const { cwd, env, timeout } = options;
|
|
109
|
+
const res = await post("/exec", { cmd: command, cwd, env, timeout });
|
|
110
|
+
return res.json();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ---------------------------------------------------------------------------
|
|
114
|
+
// spawn
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
|
|
117
|
+
/** Minimal event emitter, enough for 'data', 'exit' and 'error'. */
|
|
118
|
+
class Emitter {
|
|
119
|
+
constructor() {
|
|
120
|
+
this.listeners = {};
|
|
121
|
+
}
|
|
122
|
+
on(event, listener) {
|
|
123
|
+
(this.listeners[event] ||= []).push(listener);
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
emit(event, ...args) {
|
|
127
|
+
(this.listeners[event] || []).forEach((listener) => listener(...args));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
class ChildProcess extends Emitter {
|
|
132
|
+
constructor(abort) {
|
|
133
|
+
super();
|
|
134
|
+
this.stdout = new Emitter();
|
|
135
|
+
this.stderr = new Emitter();
|
|
136
|
+
this._abort = abort;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Stops the process (the server kills its whole process group). 'exit' fires with code null. */
|
|
140
|
+
kill() {
|
|
141
|
+
this._abort.abort();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Starts a process and streams its output.
|
|
147
|
+
*
|
|
148
|
+
* const child = spawn("ls", ["-lh", "/data"]);
|
|
149
|
+
* child.stdout.on("data", (text) => ...);
|
|
150
|
+
* child.stderr.on("data", (text) => ...);
|
|
151
|
+
* child.on("exit", (code) => ...); // code is null when killed or timed out by the client
|
|
152
|
+
* child.on("error", (error) => ...); // BridgeError, e.g. wrong token
|
|
153
|
+
*
|
|
154
|
+
* With a non-empty `args` array the command is executed directly (no shell, no quoting issues).
|
|
155
|
+
* Without args the command is run as a shell line, like exec().
|
|
156
|
+
*
|
|
157
|
+
* @param {string} command
|
|
158
|
+
* @param {string[] | object} [args] or the options object
|
|
159
|
+
* @param {{cwd?: string, env?: Record<string,string>, timeout?: number}} [options]
|
|
160
|
+
* timeout is in seconds (default 300, max 3600).
|
|
161
|
+
* @returns {ChildProcess}
|
|
162
|
+
*/
|
|
163
|
+
export function spawn(command, args, options) {
|
|
164
|
+
if (args !== undefined && !Array.isArray(args)) {
|
|
165
|
+
options = args;
|
|
166
|
+
args = undefined;
|
|
167
|
+
}
|
|
168
|
+
const { cwd, env, timeout } = options || {};
|
|
169
|
+
const body = { cmd: command, args: args && args.length > 0 ? args : undefined, cwd, env, timeout };
|
|
170
|
+
|
|
171
|
+
const abort = new AbortController();
|
|
172
|
+
const child = new ChildProcess(abort);
|
|
173
|
+
run(child, body, abort.signal);
|
|
174
|
+
return child;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function run(child, body, signal) {
|
|
178
|
+
const decoders = { out: new TextDecoder(), err: new TextDecoder() };
|
|
179
|
+
const streams = { out: child.stdout, err: child.stderr };
|
|
180
|
+
let exited = false;
|
|
181
|
+
|
|
182
|
+
const exit = (code) => {
|
|
183
|
+
if (exited) return;
|
|
184
|
+
exited = true;
|
|
185
|
+
child.emit("exit", code);
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
try {
|
|
189
|
+
const res = await post("/spawn", body, signal);
|
|
190
|
+
const reader = res.body.getReader();
|
|
191
|
+
// Cancelling the reader closes the connection right away; abort() alone can lag in some runtimes.
|
|
192
|
+
signal.addEventListener("abort", () => reader.cancel().catch(() => {}));
|
|
193
|
+
const lines = new TextDecoder();
|
|
194
|
+
let pending = "";
|
|
195
|
+
|
|
196
|
+
const handle = (line) => {
|
|
197
|
+
if (!line) return;
|
|
198
|
+
const event = JSON.parse(line);
|
|
199
|
+
|
|
200
|
+
if (event.t === "exit") return exit(event.code);
|
|
201
|
+
|
|
202
|
+
const bytes = Uint8Array.from(atob(event.b), (c) => c.charCodeAt(0));
|
|
203
|
+
// stream:true keeps a multi-byte character that was split across chunks intact
|
|
204
|
+
const text = decoders[event.t].decode(bytes, { stream: true });
|
|
205
|
+
if (text) streams[event.t].emit("data", text);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
for (;;) {
|
|
209
|
+
const { done, value } = await reader.read();
|
|
210
|
+
if (done) break;
|
|
211
|
+
pending += lines.decode(value, { stream: true });
|
|
212
|
+
const parts = pending.split("\n");
|
|
213
|
+
pending = parts.pop(); // last piece may be an unfinished line
|
|
214
|
+
parts.forEach(handle);
|
|
215
|
+
}
|
|
216
|
+
handle(pending);
|
|
217
|
+
exit(null); // stream ended without an exit line (connection cut)
|
|
218
|
+
} catch (error) {
|
|
219
|
+
if (error && error.name === "AbortError") return exit(null);
|
|
220
|
+
child.emit("error", error);
|
|
221
|
+
exit(null);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// ---------------------------------------------------------------------------
|
|
226
|
+
// Browser stand-ins for the WebView-only functions of kernelsu
|
|
227
|
+
// ---------------------------------------------------------------------------
|
|
228
|
+
|
|
229
|
+
/** Enters or leaves browser full screen. Resolves quietly when the browser refuses. */
|
|
230
|
+
export async function fullScreen(enable) {
|
|
231
|
+
try {
|
|
232
|
+
if (enable) await document.documentElement.requestFullscreen();
|
|
233
|
+
else if (document.fullscreenElement) await document.exitFullscreen();
|
|
234
|
+
} catch {
|
|
235
|
+
/* needs a user gesture, or the browser does not allow it */
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const TOAST_MS = 2500;
|
|
240
|
+
let toastTimer;
|
|
241
|
+
|
|
242
|
+
/** Shows a short message at the bottom of the page (a plain DOM element, no CSS needed). */
|
|
243
|
+
export function toast(message) {
|
|
244
|
+
let el = document.getElementById("axbridge-toast");
|
|
245
|
+
if (!el) {
|
|
246
|
+
el = document.createElement("div");
|
|
247
|
+
el.id = "axbridge-toast";
|
|
248
|
+
el.setAttribute("role", "status");
|
|
249
|
+
el.style.cssText =
|
|
250
|
+
"position:fixed;left:50%;bottom:24px;transform:translateX(-50%);max-width:80vw;" +
|
|
251
|
+
"padding:10px 16px;border-radius:12px;background:#2e3135;color:#f0f0f4;" +
|
|
252
|
+
"font:14px/1.4 system-ui,sans-serif;z-index:2147483647;pointer-events:none;";
|
|
253
|
+
document.body.append(el);
|
|
254
|
+
}
|
|
255
|
+
el.textContent = String(message);
|
|
256
|
+
el.hidden = false;
|
|
257
|
+
clearTimeout(toastTimer);
|
|
258
|
+
toastTimer = setTimeout(() => (el.hidden = true), TOAST_MS);
|
|
259
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "axbridge-api",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Browser client for AxBridge: run AxManager shell commands from a normal browser, with a kernelsu-compatible exec/spawn API.",
|
|
5
|
+
"keywords": ["axmanager", "axbridge", "android", "kernelsu", "shell", "webui", "root"],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "<YOUR NAME>",
|
|
8
|
+
"homepage": "https://github.com/Reiii3/axbridge#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/Reiii3/axbridge.git",
|
|
12
|
+
"directory": "client"
|
|
13
|
+
},
|
|
14
|
+
"bugs": "https://github.com/Reiii3/axbridge/issues",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"main": "./axbridge.js",
|
|
18
|
+
"module": "./axbridge.js",
|
|
19
|
+
"types": "./axbridge.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./axbridge.d.ts",
|
|
23
|
+
"default": "./axbridge.js"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"axbridge.js",
|
|
29
|
+
"axbridge.d.ts",
|
|
30
|
+
"README.md",
|
|
31
|
+
"LICENSE"
|
|
32
|
+
],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
}
|
|
36
|
+
}
|