kernelsu 1.0.2 → 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 +75 -7
  2. package/index.js +78 -27
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -10,20 +10,88 @@ yarn add kernelsu
10
10
 
11
11
  ### exec
12
12
 
13
- Execute a command in the **root** shell.
13
+ Spawns a **root** shell and runs a command within that shell, passing the `stdout` and `stderr` to a Promise when complete.
14
14
 
15
- options:
16
-
17
- - `cwd` - Current working directory of the child process
18
- - `env` - Environment key-value pairs
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
19
 
20
20
  ```javascript
21
21
  import { exec } from 'kernelsu';
22
22
 
23
- const { stdout, stderr } = await exec('ls -l', { cwd: '/tmp'});
24
- console.log(stdout);
23
+ const { errno, stdout, stderr } = await exec('ls -l', { cwd: '/tmp' });
24
+ if (errno === 0) {
25
+ // success
26
+ console.log(stdout);
27
+ }
25
28
  ```
26
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
+
27
95
  ### fullScreen
28
96
 
29
97
  Request the WebView enter/exit full screen.
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,47 +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(
38
- command,
39
- JSON.stringify(options),
40
- callbackFuncName,
41
- errorFuncName
42
- );
26
+ ksu.exec(command, JSON.stringify(options), callbackFuncName);
43
27
  } catch (error) {
44
28
  reject(error);
45
- cleanup(callbackFuncName, errorFuncName);
29
+ cleanup(callbackFuncName);
46
30
  }
47
31
  });
48
32
  }
49
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(
89
+ command,
90
+ JSON.stringify(args),
91
+ JSON.stringify(options),
92
+ childCallbackName
93
+ );
94
+ } catch (error) {
95
+ child.emit("error", error);
96
+ cleanup(childCallbackName);
97
+ }
98
+ return child;
99
+ }
100
+
50
101
  export function fullScreen(isFullScreen) {
51
- ksu.fullScreen(isFullScreen);
102
+ ksu.fullScreen(isFullScreen);
52
103
  }
53
104
 
54
105
  export function toast(message) {
55
- ksu.toast(message);
56
- }
106
+ ksu.toast(message);
107
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kernelsu",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Library for KernelSU's module WebUI",
5
5
  "main": "index.js",
6
6
  "scripts": {