kernelsu 1.0.1 → 1.0.4

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.
Files changed (3) hide show
  1. package/README.md +111 -0
  2. package/index.js +82 -23
  3. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # Library for KernelSU's module WebUI
2
+
3
+ ## Install
4
+
5
+ ```sh
6
+ yarn add kernelsu
7
+ ```
8
+
9
+ ## API
10
+
11
+ ### exec
12
+
13
+ Spawns a **root** shell and runs a command within that shell, passing the `stdout` and `stderr` to a Promise when complete.
14
+
15
+ - `command` `<string>` The command to run, with space-separated arguments.
16
+ - `options` `<Object>`
17
+ - `cwd` - Current working directory of the child process
18
+ - `env` - Environment key-value pairs
19
+
20
+ ```javascript
21
+ import { exec } from 'kernelsu';
22
+
23
+ const { errno, stdout, stderr } = await exec('ls -l', { cwd: '/tmp' });
24
+ if (errno === 0) {
25
+ // success
26
+ console.log(stdout);
27
+ }
28
+ ```
29
+
30
+ ### spawn
31
+
32
+ Spawns a new process using the given `command` in **root** shell, with command-line arguments in `args`. If omitted, `args` defaults to an empty array.
33
+
34
+ Returns a `ChildProcess`, Instances of the ChildProcess represent spawned child processes.
35
+
36
+ - `command` `<string>` The command to run.
37
+ - `args` `<string[]>` List of string arguments.
38
+ - `options` `<Object>`:
39
+ - `cwd` `<string>` - Current working directory of the child process
40
+ - `env` `<Object>` - Environment key-value pairs
41
+
42
+ Example of running `ls -lh /data`, capturing `stdout`, `stderr`, and the exit code:
43
+
44
+ ```javascript
45
+ import { spawn } from 'kernelsu';
46
+
47
+ const ls = spawn('ls', ['-lh', '/data']);
48
+
49
+ ls.stdout.on('data', (data) => {
50
+ console.log(`stdout: ${data}`);
51
+ });
52
+
53
+ ls.stderr.on('data', (data) => {
54
+ console.log(`stderr: ${data}`);
55
+ });
56
+
57
+ ls.on('exit', (code) => {
58
+ console.log(`child process exited with code ${code}`);
59
+ });
60
+ ```
61
+
62
+ #### ChildProcess
63
+
64
+ ##### Event 'exit'
65
+
66
+ - `code` `<number>` The exit code if the child exited on its own.
67
+
68
+ The `'exit'` event is emitted after the child process ends. If the process exited, `code` is the final exit code of the process, otherwise null
69
+
70
+ ##### Event 'error'
71
+
72
+ - `err` `<Error>` The error.
73
+
74
+ The `'error'` event is emitted whenever:
75
+
76
+ - The process could not be spawned.
77
+ - The process could not be killed.
78
+
79
+ ##### `stdout`
80
+
81
+ A `Readable Stream` that represents the child process's `stdout`.
82
+
83
+ ```javascript
84
+ const subprocess = spawn('ls');
85
+
86
+ subprocess.stdout.on('data', (data) => {
87
+ console.log(`Received chunk ${data}`);
88
+ });
89
+ ```
90
+
91
+ #### `stderr`
92
+
93
+ A `Readable Stream` that represents the child process's `stderr`.
94
+
95
+ ### fullScreen
96
+
97
+ Request the WebView enter/exit full screen.
98
+
99
+ ```javascript
100
+ import { fullScreen } from 'kernelsu';
101
+ fullScreen(true);
102
+ ```
103
+
104
+ ### toast
105
+
106
+ Show a toast message.
107
+
108
+ ```javascript
109
+ import { toast } from 'kernelsu';
110
+ toast('Hello, world!');
111
+ ```
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  let callbackCounter = 0;
2
- function getUniqueCallbackName() {
3
- return `_callback_${Date.now()}_${callbackCounter++}`;
2
+ function getUniqueCallbackName(prefix) {
3
+ return `${prefix}_callback_${Date.now()}_${callbackCounter++}`;
4
4
  }
5
5
 
6
6
  export function exec(command, options) {
@@ -10,39 +10,98 @@ export function exec(command, options) {
10
10
 
11
11
  return new Promise((resolve, reject) => {
12
12
  // Generate a unique callback function name
13
- const callbackFuncName = getUniqueCallbackName();
13
+ const callbackFuncName = getUniqueCallbackName("exec");
14
14
 
15
15
  // Define the success callback function
16
- window[callbackFuncName] = (stdout, stderr) => {
17
- resolve({ stdout, stderr });
16
+ window[callbackFuncName] = (errno, stdout, stderr) => {
17
+ resolve({ errno, stdout, stderr });
18
18
  cleanup(callbackFuncName);
19
19
  };
20
20
 
21
- // Define the failure callback function
22
- const errorFuncName = callbackFuncName + "_error";
23
- window[errorFuncName] = (error) => {
24
- reject(error);
25
- cleanup(callbackFuncName, errorFuncName);
26
- };
27
-
28
- // Cleanup function to remove the callbacks
29
- function cleanup(successName, errorName = successName) {
21
+ function cleanup(successName) {
30
22
  delete window[successName];
31
- if (errorName) {
32
- delete window[errorName];
33
- }
34
23
  }
35
24
 
36
25
  try {
37
- ksu.exec(
26
+ ksu.exec(command, JSON.stringify(options), callbackFuncName);
27
+ } catch (error) {
28
+ reject(error);
29
+ cleanup(callbackFuncName);
30
+ }
31
+ });
32
+ }
33
+
34
+ function Stdio() {
35
+ this.listeners = {};
36
+ }
37
+
38
+ Stdio.prototype.on = function (event, listener) {
39
+ if (!this.listeners[event]) {
40
+ this.listeners[event] = [];
41
+ }
42
+ this.listeners[event].push(listener);
43
+ };
44
+
45
+ Stdio.prototype.emit = function (event, ...args) {
46
+ if (this.listeners[event]) {
47
+ this.listeners[event].forEach((listener) => listener(...args));
48
+ }
49
+ };
50
+
51
+ function ChildProcess() {
52
+ this.listeners = {};
53
+ this.stdin = new Stdio();
54
+ this.stdout = new Stdio();
55
+ this.stderr = new Stdio();
56
+ }
57
+
58
+ ChildProcess.prototype.on = function (event, listener) {
59
+ if (!this.listeners[event]) {
60
+ this.listeners[event] = [];
61
+ }
62
+ this.listeners[event].push(listener);
63
+ };
64
+
65
+ ChildProcess.prototype.emit = function (event, ...args) {
66
+ if (this.listeners[event]) {
67
+ this.listeners[event].forEach((listener) => listener(...args));
68
+ }
69
+ };
70
+
71
+ export function spawn(command, args, options) {
72
+ if (typeof args === "undefined") {
73
+ args = [];
74
+ }
75
+
76
+ if (typeof options === "undefined") {
77
+ options = {};
78
+ }
79
+
80
+ const child = new ChildProcess();
81
+ const childCallbackName = getUniqueCallbackName("spawn");
82
+ window[childCallbackName] = child;
83
+
84
+ function cleanup(name) {
85
+ delete window[name];
86
+ }
87
+ try {
88
+ ksu.spawn(
38
89
  command,
90
+ JSON.stringify(args),
39
91
  JSON.stringify(options),
40
- callbackFuncName,
41
- errorFuncName
92
+ childCallbackName
42
93
  );
43
94
  } catch (error) {
44
- reject(error);
45
- cleanup(callbackFuncName, errorFuncName);
95
+ child.emit("error", error);
96
+ cleanup(childCallbackName);
46
97
  }
47
- });
98
+ return child;
99
+ }
100
+
101
+ export function fullScreen(isFullScreen) {
102
+ ksu.fullScreen(isFullScreen);
103
+ }
104
+
105
+ export function toast(message) {
106
+ ksu.toast(message);
48
107
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kernelsu",
3
- "version": "1.0.1",
3
+ "version": "1.0.4",
4
4
  "description": "Library for KernelSU's module WebUI",
5
5
  "main": "index.js",
6
6
  "scripts": {