@yume-chan/android-bin 0.0.15 → 0.0.17

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/src/bug-report.ts CHANGED
@@ -1,155 +1,156 @@
1
- // cspell: ignore bugreport
2
- // cspell: ignore bugreportz
3
-
4
- import { AdbCommandBase, AdbSubprocessShellProtocol, DecodeUtf8Stream, PushReadableStream, ReadableStream, SplitLineStream, WrapReadableStream, WritableStream } from "@yume-chan/adb";
5
-
6
- export interface BugReportZVersion {
7
- major: number;
8
- minor: number;
9
-
10
- supportProgress: boolean;
11
- supportStream: boolean;
12
- }
13
-
14
- export class BugReportZ extends AdbCommandBase {
15
- public static VERSION_REGEX = /(\d+)\.(\d+)/;
16
-
17
- public static BEGIN_REGEX = /BEGIN:(.*)/;
18
-
19
- public static PROGRESS_REGEX = /PROGRESS:(.*)\/(.*)/;
20
-
21
- public static OK_REGEX = /OK:(.*)/;
22
-
23
- public static FAIL_REGEX = /FAIL:(.*)/;
24
-
25
- /**
26
- * Retrieve the version of bugreportz.
27
- *
28
- * @returns a `BugReportVersion` object, or `undefined` if `bugreportz` is not available.
29
- */
30
- public async version(): Promise<BugReportZVersion | undefined> {
31
- // bugreportz requires shell protocol
32
- if (!AdbSubprocessShellProtocol.isSupported(this.adb)) {
33
- return undefined;
34
- }
35
-
36
- const { stderr, exitCode } = await this.adb.subprocess.spawnAndWait(['bugreportz', '-v']);
37
- if (exitCode !== 0 || stderr === '') {
38
- return undefined;
39
- }
40
-
41
- const match = stderr.match(BugReportZ.VERSION_REGEX);
42
- if (!match) {
43
- return undefined;
44
- }
45
-
46
- const major = parseInt(match[1]!, 10);
47
- const minor = parseInt(match[2]!, 10);
48
- return {
49
- major,
50
- minor,
51
-
52
- supportProgress: this.supportProgress(major, minor),
53
- supportStream: this.supportStream(major, minor),
54
- };
55
- }
56
-
57
- public supportProgress(major: number, minor: number): boolean {
58
- return major > 1 || minor >= 1;
59
- }
60
-
61
- /**
62
- * Create a zipped bugreport file.
63
- *
64
- * Compare to `stream`, this method will write the output to a file on device.
65
- * You can pull it later using sync protocol.
66
- *
67
- * @param onProgress Progress callback. Only specify this if `supportsProgress` is `true`.
68
- * @returns The path of the bugreport file.
69
- */
70
- public async generate(onProgress?: (progress: string, total: string) => void): Promise<string> {
71
- const process = await this.adb.subprocess.spawn([
72
- 'bugreportz',
73
- ...(onProgress ? ['-p'] : []),
74
- ]);
75
-
76
- let filename: string | undefined;
77
- let error: string | undefined;
78
-
79
- await process.stdout
80
- .pipeThrough(new DecodeUtf8Stream())
81
- .pipeThrough(new SplitLineStream())
82
- .pipeTo(new WritableStream<string>({
83
- write(line) {
84
- // `BEGIN:` and `PROGRESS:` only appear when `-p` is specified.
85
- let match = line.match(BugReportZ.PROGRESS_REGEX);
86
- if (match) {
87
- onProgress?.(match[1]!, match[2]!);
88
- }
89
-
90
- match = line.match(BugReportZ.BEGIN_REGEX);
91
- if (match) {
92
- filename = match[1]!;
93
- }
94
-
95
- match = line.match(BugReportZ.OK_REGEX);
96
- if (match) {
97
- filename = match[1];
98
- }
99
-
100
- match = line.match(BugReportZ.FAIL_REGEX);
101
- if (match) {
102
- // Don't report error now
103
- // We want to gather all output.
104
- error = match[1];
105
- }
106
- }
107
- }));
108
-
109
- if (error) {
110
- throw new Error(error);
111
- }
112
-
113
- if (!filename) {
114
- throw new Error('bugreportz did not return file name');
115
- }
116
-
117
- // Design choice: we don't automatically pull the file to avoid more dependency on `@yume-chan/adb`
118
- return filename;
119
- }
120
-
121
- public supportStream(major: number, minor: number): boolean {
122
- return major > 1 || minor >= 2;
123
- }
124
-
125
- public stream(): ReadableStream<Uint8Array> {
126
- return new PushReadableStream(async (controller) => {
127
- const process = await this.adb.subprocess.spawn(['bugreportz', '-s']);
128
- process.stdout
129
- .pipeTo(new WritableStream({
130
- async write(chunk) {
131
- await controller.enqueue(chunk);
132
- },
133
- }));
134
- process.stderr
135
- .pipeThrough(new DecodeUtf8Stream())
136
- .pipeTo(new WritableStream({
137
- async write(chunk) {
138
- controller.error(new Error(chunk));
139
- }
140
- }));
141
- await process.exit;
142
- controller.close();
143
- });
144
- }
145
- }
146
-
147
- // https://cs.android.com/android/platform/superproject/+/master:frameworks/native/cmds/bugreport/bugreport.cpp;drc=9b73bf07d73dbab5b792632e1e233edbad77f5fd;bpv=0;bpt=0
148
- export class BugReport extends AdbCommandBase {
149
- public generate(): ReadableStream<Uint8Array> {
150
- return new WrapReadableStream(async () => {
151
- const process = await this.adb.subprocess.spawn(['bugreport']);
152
- return process.stdout;
153
- });
154
- }
155
- }
1
+ // cspell: ignore bugreport
2
+ // cspell: ignore bugreportz
3
+
4
+ import { AdbCommandBase, AdbSubprocessShellProtocol } from '@yume-chan/adb';
5
+ import { DecodeUtf8Stream, PushReadableStream, ReadableStream, SplitStringStream, WrapReadableStream, WritableStream } from '@yume-chan/stream-extra';
6
+
7
+ export interface BugReportZVersion {
8
+ major: number;
9
+ minor: number;
10
+
11
+ supportProgress: boolean;
12
+ supportStream: boolean;
13
+ }
14
+
15
+ export class BugReportZ extends AdbCommandBase {
16
+ public static VERSION_REGEX = /(\d+)\.(\d+)/;
17
+
18
+ public static BEGIN_REGEX = /BEGIN:(.*)/;
19
+
20
+ public static PROGRESS_REGEX = /PROGRESS:(.*)\/(.*)/;
21
+
22
+ public static OK_REGEX = /OK:(.*)/;
23
+
24
+ public static FAIL_REGEX = /FAIL:(.*)/;
25
+
26
+ /**
27
+ * Retrieve the version of bugreportz.
28
+ *
29
+ * @returns a `BugReportVersion` object, or `undefined` if `bugreportz` is not available.
30
+ */
31
+ public async version(): Promise<BugReportZVersion | undefined> {
32
+ // bugreportz requires shell protocol
33
+ if (!AdbSubprocessShellProtocol.isSupported(this.adb)) {
34
+ return undefined;
35
+ }
36
+
37
+ const { stderr, exitCode } = await this.adb.subprocess.spawnAndWait(['bugreportz', '-v']);
38
+ if (exitCode !== 0 || stderr === '') {
39
+ return undefined;
40
+ }
41
+
42
+ const match = stderr.match(BugReportZ.VERSION_REGEX);
43
+ if (!match) {
44
+ return undefined;
45
+ }
46
+
47
+ const major = parseInt(match[1]!, 10);
48
+ const minor = parseInt(match[2]!, 10);
49
+ return {
50
+ major,
51
+ minor,
52
+
53
+ supportProgress: this.supportProgress(major, minor),
54
+ supportStream: this.supportStream(major, minor),
55
+ };
56
+ }
57
+
58
+ public supportProgress(major: number, minor: number): boolean {
59
+ return major > 1 || minor >= 1;
60
+ }
61
+
62
+ /**
63
+ * Create a zipped bugreport file.
64
+ *
65
+ * Compare to `stream`, this method will write the output to a file on device.
66
+ * You can pull it later using sync protocol.
67
+ *
68
+ * @param onProgress Progress callback. Only specify this if `supportsProgress` is `true`.
69
+ * @returns The path of the bugreport file.
70
+ */
71
+ public async generate(onProgress?: (progress: string, total: string) => void): Promise<string> {
72
+ const process = await this.adb.subprocess.spawn([
73
+ 'bugreportz',
74
+ ...(onProgress ? ['-p'] : []),
75
+ ]);
76
+
77
+ let filename: string | undefined;
78
+ let error: string | undefined;
79
+
80
+ await process.stdout
81
+ .pipeThrough(new DecodeUtf8Stream())
82
+ .pipeThrough(new SplitStringStream('\n'))
83
+ .pipeTo(new WritableStream<string>({
84
+ write(line) {
85
+ // `BEGIN:` and `PROGRESS:` only appear when `-p` is specified.
86
+ let match = line.match(BugReportZ.PROGRESS_REGEX);
87
+ if (match) {
88
+ onProgress?.(match[1]!, match[2]!);
89
+ }
90
+
91
+ match = line.match(BugReportZ.BEGIN_REGEX);
92
+ if (match) {
93
+ filename = match[1]!;
94
+ }
95
+
96
+ match = line.match(BugReportZ.OK_REGEX);
97
+ if (match) {
98
+ filename = match[1];
99
+ }
100
+
101
+ match = line.match(BugReportZ.FAIL_REGEX);
102
+ if (match) {
103
+ // Don't report error now
104
+ // We want to gather all output.
105
+ error = match[1];
106
+ }
107
+ }
108
+ }));
109
+
110
+ if (error) {
111
+ throw new Error(error);
112
+ }
113
+
114
+ if (!filename) {
115
+ throw new Error('bugreportz did not return file name');
116
+ }
117
+
118
+ // Design choice: we don't automatically pull the file to avoid more dependency on `@yume-chan/adb`
119
+ return filename;
120
+ }
121
+
122
+ public supportStream(major: number, minor: number): boolean {
123
+ return major > 1 || minor >= 2;
124
+ }
125
+
126
+ public stream(): ReadableStream<Uint8Array> {
127
+ return new PushReadableStream(async (controller) => {
128
+ const process = await this.adb.subprocess.spawn(['bugreportz', '-s']);
129
+ process.stdout
130
+ .pipeTo(new WritableStream({
131
+ async write(chunk) {
132
+ await controller.enqueue(chunk);
133
+ },
134
+ }));
135
+ process.stderr
136
+ .pipeThrough(new DecodeUtf8Stream())
137
+ .pipeTo(new WritableStream({
138
+ async write(chunk) {
139
+ controller.error(new Error(chunk));
140
+ }
141
+ }));
142
+ await process.exit;
143
+ controller.close();
144
+ });
145
+ }
146
+ }
147
+
148
+ // https://cs.android.com/android/platform/superproject/+/master:frameworks/native/cmds/bugreport/bugreport.cpp;drc=9b73bf07d73dbab5b792632e1e233edbad77f5fd;bpv=0;bpt=0
149
+ export class BugReport extends AdbCommandBase {
150
+ public generate(): ReadableStream<Uint8Array> {
151
+ return new WrapReadableStream(async () => {
152
+ const process = await this.adb.subprocess.spawn(['bugreport']);
153
+ return process.stdout;
154
+ });
155
+ }
156
+ }