@yume-chan/adb 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.
Files changed (196) hide show
  1. package/CHANGELOG.json +33 -0
  2. package/CHANGELOG.md +18 -1
  3. package/LICENSE +21 -21
  4. package/README.md +247 -247
  5. package/esm/adb.d.ts +15 -3
  6. package/esm/adb.d.ts.map +1 -1
  7. package/esm/adb.js +18 -6
  8. package/esm/adb.js.map +1 -1
  9. package/esm/auth.d.ts.map +1 -1
  10. package/esm/auth.js.map +1 -1
  11. package/esm/backend.d.ts +2 -2
  12. package/esm/backend.d.ts.map +1 -1
  13. package/esm/commands/base.d.ts.map +1 -1
  14. package/esm/commands/base.js.map +1 -1
  15. package/esm/commands/framebuffer.d.ts +17 -1
  16. package/esm/commands/framebuffer.d.ts.map +1 -1
  17. package/esm/commands/framebuffer.js +4 -15
  18. package/esm/commands/framebuffer.js.map +1 -1
  19. package/esm/commands/install.d.ts +2 -2
  20. package/esm/commands/install.d.ts.map +1 -1
  21. package/esm/commands/install.js +3 -2
  22. package/esm/commands/install.js.map +1 -1
  23. package/esm/commands/power.d.ts +1 -1
  24. package/esm/commands/power.js +1 -1
  25. package/esm/commands/reverse.d.ts +3 -3
  26. package/esm/commands/reverse.d.ts.map +1 -1
  27. package/esm/commands/reverse.js +9 -9
  28. package/esm/commands/reverse.js.map +1 -1
  29. package/esm/commands/subprocess/command.d.ts +52 -0
  30. package/esm/commands/subprocess/command.d.ts.map +1 -0
  31. package/esm/commands/subprocess/command.js +83 -0
  32. package/esm/commands/subprocess/command.js.map +1 -0
  33. package/esm/commands/subprocess/index.d.ts +1 -53
  34. package/esm/commands/subprocess/index.d.ts.map +1 -1
  35. package/esm/commands/subprocess/index.js +1 -85
  36. package/esm/commands/subprocess/index.js.map +1 -1
  37. package/esm/commands/subprocess/protocols/none.d.ts +5 -4
  38. package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
  39. package/esm/commands/subprocess/protocols/none.js +11 -8
  40. package/esm/commands/subprocess/protocols/none.js.map +1 -1
  41. package/esm/commands/subprocess/protocols/shell.d.ts +4 -4
  42. package/esm/commands/subprocess/protocols/shell.d.ts.map +1 -1
  43. package/esm/commands/subprocess/protocols/shell.js +6 -6
  44. package/esm/commands/subprocess/protocols/shell.js.map +1 -1
  45. package/esm/commands/subprocess/protocols/types.d.ts +7 -7
  46. package/esm/commands/subprocess/protocols/types.d.ts.map +1 -1
  47. package/esm/commands/sync/index.d.ts +1 -1
  48. package/esm/commands/sync/index.d.ts.map +1 -1
  49. package/esm/commands/sync/index.js +1 -1
  50. package/esm/commands/sync/index.js.map +1 -1
  51. package/esm/commands/sync/list.d.ts +2 -2
  52. package/esm/commands/sync/list.d.ts.map +1 -1
  53. package/esm/commands/sync/list.js +15 -43
  54. package/esm/commands/sync/list.js.map +1 -1
  55. package/esm/commands/sync/pull.d.ts +3 -2
  56. package/esm/commands/sync/pull.d.ts.map +1 -1
  57. package/esm/commands/sync/pull.js +15 -17
  58. package/esm/commands/sync/pull.js.map +1 -1
  59. package/esm/commands/sync/push.d.ts +2 -2
  60. package/esm/commands/sync/push.d.ts.map +1 -1
  61. package/esm/commands/sync/push.js +2 -5
  62. package/esm/commands/sync/push.js.map +1 -1
  63. package/esm/commands/sync/request.d.ts +1 -1
  64. package/esm/commands/sync/request.d.ts.map +1 -1
  65. package/esm/commands/sync/request.js +1 -1
  66. package/esm/commands/sync/request.js.map +1 -1
  67. package/esm/commands/sync/response.d.ts +4 -9
  68. package/esm/commands/sync/response.d.ts.map +1 -1
  69. package/esm/commands/sync/response.js +32 -23
  70. package/esm/commands/sync/response.js.map +1 -1
  71. package/esm/commands/sync/stat.d.ts +3 -3
  72. package/esm/commands/sync/stat.d.ts.map +1 -1
  73. package/esm/commands/sync/stat.js +13 -30
  74. package/esm/commands/sync/stat.js.map +1 -1
  75. package/esm/commands/sync/sync.d.ts +2 -2
  76. package/esm/commands/sync/sync.d.ts.map +1 -1
  77. package/esm/commands/sync/sync.js +7 -5
  78. package/esm/commands/sync/sync.js.map +1 -1
  79. package/esm/crypto.js +1 -1
  80. package/esm/crypto.js.map +1 -1
  81. package/esm/index.d.ts +0 -1
  82. package/esm/index.d.ts.map +1 -1
  83. package/esm/index.js +0 -1
  84. package/esm/index.js.map +1 -1
  85. package/esm/packet.d.ts +1 -1
  86. package/esm/packet.d.ts.map +1 -1
  87. package/esm/packet.js +1 -1
  88. package/esm/packet.js.map +1 -1
  89. package/esm/socket/dispatcher.d.ts +11 -3
  90. package/esm/socket/dispatcher.d.ts.map +1 -1
  91. package/esm/socket/dispatcher.js +19 -11
  92. package/esm/socket/dispatcher.js.map +1 -1
  93. package/esm/socket/index.d.ts +1 -1
  94. package/esm/socket/index.d.ts.map +1 -1
  95. package/esm/socket/index.js +1 -1
  96. package/esm/socket/index.js.map +1 -1
  97. package/esm/socket/socket.d.ts +9 -4
  98. package/esm/socket/socket.d.ts.map +1 -1
  99. package/esm/socket/socket.js +20 -10
  100. package/esm/socket/socket.js.map +1 -1
  101. package/esm/utils/base64.d.ts +23 -0
  102. package/esm/utils/base64.d.ts.map +1 -1
  103. package/esm/utils/base64.js +6 -0
  104. package/esm/utils/base64.js.map +1 -1
  105. package/package.json +13 -14
  106. package/src/adb.ts +285 -273
  107. package/src/auth.ts +174 -173
  108. package/src/backend.ts +12 -11
  109. package/src/commands/base.ts +12 -11
  110. package/src/commands/framebuffer.ts +78 -73
  111. package/src/commands/index.ts +8 -8
  112. package/src/commands/install.ts +32 -30
  113. package/src/commands/power.ts +54 -54
  114. package/src/commands/reverse.ts +141 -140
  115. package/src/commands/subprocess/command.ts +131 -0
  116. package/src/commands/subprocess/index.ts +3 -139
  117. package/src/commands/subprocess/protocols/none.ts +73 -68
  118. package/src/commands/subprocess/protocols/shell.ts +191 -190
  119. package/src/commands/subprocess/protocols/types.ts +60 -59
  120. package/src/commands/subprocess/utils.ts +20 -20
  121. package/src/commands/sync/index.ts +7 -7
  122. package/src/commands/sync/list.ts +54 -86
  123. package/src/commands/sync/pull.ts +45 -46
  124. package/src/commands/sync/push.ts +38 -41
  125. package/src/commands/sync/request.ts +51 -50
  126. package/src/commands/sync/response.ts +68 -64
  127. package/src/commands/sync/stat.ts +129 -150
  128. package/src/commands/sync/sync.ts +182 -181
  129. package/src/commands/tcpip.ts +21 -21
  130. package/src/crypto.ts +296 -296
  131. package/src/features.ts +9 -9
  132. package/src/index.ts +10 -11
  133. package/src/packet.ts +80 -80
  134. package/src/socket/dispatcher.ts +310 -304
  135. package/src/socket/index.ts +2 -2
  136. package/src/socket/socket.ts +156 -144
  137. package/src/utils/auto-reset-event.ts +41 -41
  138. package/src/utils/base64.ts +329 -306
  139. package/src/utils/index.ts +3 -3
  140. package/tsconfig.build.json +3 -3
  141. package/tsconfig.build.tsbuildinfo +1 -1
  142. package/esm/commands/subprocess/legacy.d.ts +0 -35
  143. package/esm/commands/subprocess/legacy.d.ts.map +0 -1
  144. package/esm/commands/subprocess/legacy.js +0 -53
  145. package/esm/commands/subprocess/legacy.js.map +0 -1
  146. package/esm/commands/subprocess/protocol.d.ts +0 -39
  147. package/esm/commands/subprocess/protocol.d.ts.map +0 -1
  148. package/esm/commands/subprocess/protocol.js +0 -157
  149. package/esm/commands/subprocess/protocol.js.map +0 -1
  150. package/esm/commands/subprocess/types.d.ts +0 -50
  151. package/esm/commands/subprocess/types.d.ts.map +0 -1
  152. package/esm/commands/subprocess/types.js +0 -2
  153. package/esm/commands/subprocess/types.js.map +0 -1
  154. package/esm/socket/controller.d.ts +0 -37
  155. package/esm/socket/controller.d.ts.map +0 -1
  156. package/esm/socket/controller.js +0 -66
  157. package/esm/socket/controller.js.map +0 -1
  158. package/esm/stream/buffered.d.ts +0 -35
  159. package/esm/stream/buffered.d.ts.map +0 -1
  160. package/esm/stream/buffered.js +0 -123
  161. package/esm/stream/buffered.js.map +0 -1
  162. package/esm/stream/detect.bak.d.ts +0 -240
  163. package/esm/stream/detect.bak.d.ts.map +0 -1
  164. package/esm/stream/detect.bak.js +0 -60
  165. package/esm/stream/detect.bak.js.map +0 -1
  166. package/esm/stream/detect.d.ts +0 -2
  167. package/esm/stream/detect.d.ts.map +0 -1
  168. package/esm/stream/detect.js +0 -5
  169. package/esm/stream/detect.js.map +0 -1
  170. package/esm/stream/detect.native.d.ts +0 -240
  171. package/esm/stream/detect.native.d.ts.map +0 -1
  172. package/esm/stream/detect.native.js +0 -60
  173. package/esm/stream/detect.native.js.map +0 -1
  174. package/esm/stream/detect.polyfill.d.ts +0 -18
  175. package/esm/stream/detect.polyfill.d.ts.map +0 -1
  176. package/esm/stream/detect.polyfill.js +0 -9
  177. package/esm/stream/detect.polyfill.js.map +0 -1
  178. package/esm/stream/index.d.ts +0 -4
  179. package/esm/stream/index.d.ts.map +0 -1
  180. package/esm/stream/index.js +0 -4
  181. package/esm/stream/index.js.map +0 -1
  182. package/esm/stream/transform.d.ts +0 -104
  183. package/esm/stream/transform.d.ts.map +0 -1
  184. package/esm/stream/transform.js +0 -373
  185. package/esm/stream/transform.js.map +0 -1
  186. package/esm/utils/encoding.d.ts +0 -3
  187. package/esm/utils/encoding.d.ts.map +0 -1
  188. package/esm/utils/encoding.js +0 -11
  189. package/esm/utils/encoding.js.map +0 -1
  190. package/src/stream/buffered.ts +0 -147
  191. package/src/stream/detect.native.ts +0 -362
  192. package/src/stream/detect.polyfill.ts +0 -28
  193. package/src/stream/detect.ts +0 -5
  194. package/src/stream/index.ts +0 -3
  195. package/src/stream/transform.ts +0 -447
  196. package/tsconfig.test.json +0 -10
@@ -1,59 +1,60 @@
1
- import type { ValueOrPromise } from "@yume-chan/struct";
2
- import type { Adb } from "../../../adb.js";
3
- import type { AdbSocket } from "../../../socket/index.js";
4
- import type { ReadableStream, WritableStream } from "../../../stream/index.js";
5
-
6
- export interface AdbSubprocessProtocol {
7
- /**
8
- * A WritableStream that writes to the `stdin` pipe.
9
- */
10
- readonly stdin: WritableStream<Uint8Array>;
11
-
12
- /**
13
- * The `stdout` pipe of the process.
14
- */
15
- readonly stdout: ReadableStream<Uint8Array>;
16
-
17
- /**
18
- * The `stderr` pipe of the process.
19
- *
20
- * Note: Some `AdbSubprocessProtocol` doesn't separate `stdout` and `stderr`,
21
- * All output will be sent to `stdout`.
22
- */
23
- readonly stderr: ReadableStream<Uint8Array>;
24
-
25
- /**
26
- * A `Promise` that resolves to the exit code of the process.
27
- *
28
- * Note: Some `AdbSubprocessProtocol` doesn't support exit code,
29
- * They will always resolve it with `0`.
30
- */
31
- readonly exit: Promise<number>;
32
-
33
- /**
34
- * Resizes the current shell.
35
- *
36
- * Some `AdbSubprocessProtocol`s may not support resizing
37
- * and will ignore calls to this method.
38
- */
39
- resize(rows: number, cols: number): ValueOrPromise<void>;
40
-
41
- /**
42
- * Kills the current process.
43
- */
44
- kill(): ValueOrPromise<void>;
45
- }
46
-
47
- export interface AdbSubprocessProtocolConstructor {
48
- /** Returns `true` if the `adb` instance supports this shell */
49
- isSupported(adb: Adb): ValueOrPromise<boolean>;
50
-
51
- /** Spawns an executable in PTY (interactive) mode. */
52
- pty(adb: Adb, command: string): ValueOrPromise<AdbSubprocessProtocol>;
53
-
54
- /** Spawns an executable and pipe the output. */
55
- raw(adb: Adb, command: string): ValueOrPromise<AdbSubprocessProtocol>;
56
-
57
- /** Creates a new `AdbShell` by attaching to an exist `AdbSocket` */
58
- new(socket: AdbSocket): AdbSubprocessProtocol;
59
- }
1
+ import type { ReadableStream, WritableStream } from '@yume-chan/stream-extra';
2
+ import type { ValueOrPromise } from '@yume-chan/struct';
3
+
4
+ import type { Adb } from '../../../adb.js';
5
+ import type { AdbSocket } from '../../../socket/index.js';
6
+
7
+ export interface AdbSubprocessProtocol {
8
+ /**
9
+ * A WritableStream that writes to the `stdin` stream.
10
+ */
11
+ readonly stdin: WritableStream<Uint8Array>;
12
+
13
+ /**
14
+ * The `stdout` stream of the process.
15
+ */
16
+ readonly stdout: ReadableStream<Uint8Array>;
17
+
18
+ /**
19
+ * The `stderr` stream of the process.
20
+ *
21
+ * Note: Some `AdbSubprocessProtocol` doesn't separate `stdout` and `stderr`,
22
+ * All output will be sent to `stdout`.
23
+ */
24
+ readonly stderr: ReadableStream<Uint8Array>;
25
+
26
+ /**
27
+ * A `Promise` that resolves to the exit code of the process.
28
+ *
29
+ * Note: Some `AdbSubprocessProtocol` doesn't support exit code,
30
+ * They will always resolve it with `0`.
31
+ */
32
+ readonly exit: Promise<number>;
33
+
34
+ /**
35
+ * Resizes the current shell.
36
+ *
37
+ * Some `AdbSubprocessProtocol`s may not support resizing
38
+ * and will ignore calls to this method.
39
+ */
40
+ resize(rows: number, cols: number): ValueOrPromise<void>;
41
+
42
+ /**
43
+ * Kills the current process.
44
+ */
45
+ kill(): ValueOrPromise<void>;
46
+ }
47
+
48
+ export interface AdbSubprocessProtocolConstructor {
49
+ /** Returns `true` if the `adb` instance supports this shell */
50
+ isSupported(adb: Adb): ValueOrPromise<boolean>;
51
+
52
+ /** Spawns an executable in PTY (interactive) mode. */
53
+ pty(adb: Adb, command: string): ValueOrPromise<AdbSubprocessProtocol>;
54
+
55
+ /** Spawns an executable and pipe the output. */
56
+ raw(adb: Adb, command: string): ValueOrPromise<AdbSubprocessProtocol>;
57
+
58
+ /** Creates a new `AdbShell` by attaching to an exist `AdbSocket` */
59
+ new(socket: AdbSocket): AdbSubprocessProtocol;
60
+ }
@@ -1,20 +1,20 @@
1
- export function escapeArg(s: string) {
2
- let result = '';
3
- result += `'`;
4
-
5
- let base = 0;
6
- while (true) {
7
- const found = s.indexOf(`'`, base);
8
- if (found === -1) {
9
- result += s.substring(base);
10
- break;
11
- }
12
- result += s.substring(base, found);
13
- // a'b becomes a'\'b
14
- result += String.raw`'\''`;
15
- base = found + 1;
16
- }
17
-
18
- result += `'`;
19
- return result;
20
- }
1
+ export function escapeArg(s: string) {
2
+ let result = '';
3
+ result += `'`;
4
+
5
+ let base = 0;
6
+ while (true) {
7
+ const found = s.indexOf(`'`, base);
8
+ if (found === -1) {
9
+ result += s.substring(base);
10
+ break;
11
+ }
12
+ result += s.substring(base, found);
13
+ // a'b becomes a'\'b
14
+ result += String.raw`'\''`;
15
+ base = found + 1;
16
+ }
17
+
18
+ result += `'`;
19
+ return result;
20
+ }
@@ -1,7 +1,7 @@
1
- export * from './list.js';
2
- export * from './pull.js';
3
- export * from './request.js';
4
- export * from './response.js';
5
- export * from './push.js';
6
- export * from './stat.js';
7
- export * from './sync.js';
1
+ export * from './list.js';
2
+ export * from './pull.js';
3
+ export * from './push.js';
4
+ export * from './request.js';
5
+ export * from './response.js';
6
+ export * from './stat.js';
7
+ export * from './sync.js';
@@ -1,86 +1,54 @@
1
- import Struct from '@yume-chan/struct';
2
- import type { AdbBufferedStream, WritableStreamDefaultWriter } from '../../stream/index.js';
3
- import { AdbSyncRequestId, adbSyncWriteRequest } from './request.js';
4
- import { AdbSyncDoneResponse, adbSyncReadResponse, AdbSyncResponseId } from './response.js';
5
- import { AdbSyncLstatResponse, AdbSyncStatResponse, type AdbSyncStat } from './stat.js';
6
-
7
- export interface AdbSyncEntry extends AdbSyncStat {
8
- name: string;
9
- }
10
-
11
- export const AdbSyncEntryResponse =
12
- new Struct({ littleEndian: true })
13
- .fields(AdbSyncLstatResponse)
14
- .uint32('nameLength')
15
- .string('name', { lengthField: 'nameLength' })
16
- .extra({ id: AdbSyncResponseId.Entry as const });
17
-
18
- export type AdbSyncEntryResponse = typeof AdbSyncEntryResponse['TDeserializeResult'];
19
-
20
- export const AdbSyncEntry2Response =
21
- new Struct({ littleEndian: true })
22
- .fields(AdbSyncStatResponse)
23
- .uint32('nameLength')
24
- .string('name', { lengthField: 'nameLength' })
25
- .extra({ id: AdbSyncResponseId.Entry2 as const });
26
-
27
- export type AdbSyncEntry2Response = typeof AdbSyncEntry2Response['TDeserializeResult'];
28
-
29
- const LIST_V1_RESPONSE_TYPES = {
30
- [AdbSyncResponseId.Entry]: AdbSyncEntryResponse,
31
- [AdbSyncResponseId.Done]: new AdbSyncDoneResponse(AdbSyncEntryResponse.size),
32
- };
33
-
34
- const LIST_V2_RESPONSE_TYPES = {
35
- [AdbSyncResponseId.Entry2]: AdbSyncEntry2Response,
36
- [AdbSyncResponseId.Done]: new AdbSyncDoneResponse(AdbSyncEntry2Response.size),
37
- };
38
-
39
- export async function* adbSyncOpenDir(
40
- stream: AdbBufferedStream,
41
- writer: WritableStreamDefaultWriter<Uint8Array>,
42
- path: string,
43
- v2: boolean,
44
- ): AsyncGenerator<AdbSyncEntry, void, void> {
45
- let requestId: AdbSyncRequestId.List | AdbSyncRequestId.List2;
46
- let responseType: typeof LIST_V1_RESPONSE_TYPES | typeof LIST_V2_RESPONSE_TYPES;
47
-
48
- if (v2) {
49
- requestId = AdbSyncRequestId.List2;
50
- responseType = LIST_V2_RESPONSE_TYPES;
51
- } else {
52
- requestId = AdbSyncRequestId.List;
53
- responseType = LIST_V1_RESPONSE_TYPES;
54
- }
55
-
56
- await adbSyncWriteRequest(writer, requestId, path);
57
-
58
- while (true) {
59
- const response = await adbSyncReadResponse(stream, responseType);
60
- switch (response.id) {
61
- case AdbSyncResponseId.Entry:
62
- yield {
63
- mode: response.mode,
64
- size: BigInt(response.size),
65
- mtime: BigInt(response.mtime),
66
- get type() { return response.type; },
67
- get permission() { return response.permission; },
68
- name: response.name,
69
- };
70
- break;
71
- case AdbSyncResponseId.Entry2:
72
- // `LST2` can return error codes for failed `lstat` calls.
73
- // `LIST` just ignores them.
74
- // But they only contain `name` so still pretty useless.
75
- if (response.error !== 0) {
76
- continue;
77
- }
78
- yield response;
79
- break;
80
- case AdbSyncResponseId.Done:
81
- return;
82
- default:
83
- throw new Error('Unexpected response id');
84
- }
85
- }
86
- }
1
+ import type { BufferedReadableStream, WritableStreamDefaultWriter } from '@yume-chan/stream-extra';
2
+ import Struct from '@yume-chan/struct';
3
+
4
+ import { AdbSyncRequestId, adbSyncWriteRequest } from './request.js';
5
+ import { adbSyncReadResponses, AdbSyncResponseId } from './response.js';
6
+ import { AdbSyncLstatResponse, AdbSyncStatResponse, type AdbSyncStat } from './stat.js';
7
+
8
+ export interface AdbSyncEntry extends AdbSyncStat {
9
+ name: string;
10
+ }
11
+
12
+ export const AdbSyncEntryResponse =
13
+ new Struct({ littleEndian: true })
14
+ .fields(AdbSyncLstatResponse)
15
+ .uint32('nameLength')
16
+ .string('name', { lengthField: 'nameLength' })
17
+ .extra({ id: AdbSyncResponseId.Entry as const });
18
+
19
+ export type AdbSyncEntryResponse = typeof AdbSyncEntryResponse['TDeserializeResult'];
20
+
21
+ export const AdbSyncEntry2Response =
22
+ new Struct({ littleEndian: true })
23
+ .fields(AdbSyncStatResponse)
24
+ .uint32('nameLength')
25
+ .string('name', { lengthField: 'nameLength' })
26
+ .extra({ id: AdbSyncResponseId.Entry2 as const });
27
+
28
+ export type AdbSyncEntry2Response = typeof AdbSyncEntry2Response['TDeserializeResult'];
29
+
30
+ export async function* adbSyncOpenDir(
31
+ stream: BufferedReadableStream,
32
+ writer: WritableStreamDefaultWriter<Uint8Array>,
33
+ path: string,
34
+ v2: boolean,
35
+ ): AsyncGenerator<AdbSyncEntry, void, void> {
36
+ if (v2) {
37
+ await adbSyncWriteRequest(writer, AdbSyncRequestId.List2, path);
38
+ yield* adbSyncReadResponses(stream, AdbSyncResponseId.Entry2, AdbSyncEntry2Response);
39
+ } else {
40
+ await adbSyncWriteRequest(writer, AdbSyncRequestId.List, path);
41
+ for await (const item of adbSyncReadResponses(stream, AdbSyncResponseId.Entry, AdbSyncEntryResponse)) {
42
+ // Convert to same format as `AdbSyncEntry2Response` for easier consumption.
43
+ // However it will add some overhead.
44
+ yield {
45
+ mode: item.mode,
46
+ size: BigInt(item.size),
47
+ mtime: BigInt(item.mtime),
48
+ get type() { return item.type; },
49
+ get permission() { return item.permission; },
50
+ name: item.name,
51
+ };
52
+ }
53
+ }
54
+ }
@@ -1,46 +1,45 @@
1
- import Struct from '@yume-chan/struct';
2
- import { AdbBufferedStream, ReadableStream, WritableStreamDefaultWriter } from '../../stream/index.js';
3
- import { AdbSyncRequestId, adbSyncWriteRequest } from './request.js';
4
- import { AdbSyncDoneResponse, adbSyncReadResponse, AdbSyncResponseId } from './response.js';
5
-
6
- export const AdbSyncDataResponse =
7
- new Struct({ littleEndian: true })
8
- .uint32('dataLength')
9
- .uint8Array('data', { lengthField: 'dataLength' })
10
- .extra({ id: AdbSyncResponseId.Data as const });
11
-
12
- const RESPONSE_TYPES = {
13
- [AdbSyncResponseId.Data]: AdbSyncDataResponse,
14
- [AdbSyncResponseId.Done]: new AdbSyncDoneResponse(AdbSyncDataResponse.size),
15
- };
16
-
17
- export function adbSyncPull(
18
- stream: AdbBufferedStream,
19
- writer: WritableStreamDefaultWriter<Uint8Array>,
20
- path: string,
21
- ): ReadableStream<Uint8Array> {
22
- return new ReadableStream<Uint8Array>({
23
- async start() {
24
- await adbSyncWriteRequest(writer, AdbSyncRequestId.Receive, path);
25
- },
26
- async pull(controller) {
27
- const response = await adbSyncReadResponse(stream, RESPONSE_TYPES);
28
- switch (response.id) {
29
- case AdbSyncResponseId.Data:
30
- controller.enqueue(response.data!);
31
- break;
32
- case AdbSyncResponseId.Done:
33
- controller.close();
34
- break;
35
- default:
36
- throw new Error('Unexpected response id');
37
- }
38
- },
39
- cancel() {
40
- throw new Error(`Sync commands don't support cancel.`);
41
- },
42
- }, {
43
- highWaterMark: 16 * 1024,
44
- size(chunk) { return chunk.byteLength; }
45
- });
46
- }
1
+ import { BufferedReadableStream, ReadableStream, WritableStreamDefaultWriter } from '@yume-chan/stream-extra';
2
+ import Struct from '@yume-chan/struct';
3
+
4
+ import { AdbSyncRequestId, adbSyncWriteRequest } from './request.js';
5
+ import { adbSyncReadResponses, AdbSyncResponseId } from './response.js';
6
+
7
+ export const AdbSyncDataResponse =
8
+ new Struct({ littleEndian: true })
9
+ .uint32('dataLength')
10
+ .uint8Array('data', { lengthField: 'dataLength' })
11
+ .extra({ id: AdbSyncResponseId.Data as const });
12
+
13
+ export type AdbSyncDataResponse = typeof AdbSyncDataResponse['TDeserializeResult'];
14
+
15
+ export function adbSyncPull(
16
+ stream: BufferedReadableStream,
17
+ writer: WritableStreamDefaultWriter<Uint8Array>,
18
+ path: string,
19
+ ): ReadableStream<Uint8Array> {
20
+ let generator!: AsyncGenerator<AdbSyncDataResponse, void, void>;
21
+ return new ReadableStream<Uint8Array>({
22
+ async start() {
23
+ // TODO: If `ReadableStream.from(AsyncGenerator)` is added to spec, use it instead.
24
+ await adbSyncWriteRequest(writer, AdbSyncRequestId.Receive, path);
25
+ generator = adbSyncReadResponses(stream, AdbSyncResponseId.Data, AdbSyncDataResponse);
26
+ },
27
+ async pull(controller) {
28
+ const { done, value } = await generator.next();
29
+ if (done) {
30
+ controller.close();
31
+ return;
32
+ }
33
+ controller.enqueue(value.data);
34
+ },
35
+ cancel() {
36
+ try {
37
+ generator.return();
38
+ } catch { }
39
+ throw new Error(`Sync commands can't be canceled.`);
40
+ },
41
+ }, {
42
+ highWaterMark: 16 * 1024,
43
+ size(chunk) { return chunk.byteLength; }
44
+ });
45
+ }
@@ -1,41 +1,38 @@
1
- import Struct from '@yume-chan/struct';
2
- import { AdbBufferedStream, ChunkStream, pipeFrom, WritableStream, WritableStreamDefaultWriter } from '../../stream/index.js';
3
- import { AdbSyncRequestId, adbSyncWriteRequest } from './request.js';
4
- import { adbSyncReadResponse, AdbSyncResponseId } from './response.js';
5
- import { LinuxFileType } from './stat.js';
6
-
7
- export const AdbSyncOkResponse =
8
- new Struct({ littleEndian: true })
9
- .uint32('unused');
10
-
11
- const ResponseTypes = {
12
- [AdbSyncResponseId.Ok]: AdbSyncOkResponse,
13
- };
14
-
15
- export const ADB_SYNC_MAX_PACKET_SIZE = 64 * 1024;
16
-
17
- export function adbSyncPush(
18
- stream: AdbBufferedStream,
19
- writer: WritableStreamDefaultWriter<Uint8Array>,
20
- filename: string,
21
- mode: number = (LinuxFileType.File << 12) | 0o666,
22
- mtime: number = (Date.now() / 1000) | 0,
23
- packetSize: number = ADB_SYNC_MAX_PACKET_SIZE,
24
- ): WritableStream<Uint8Array> {
25
- return pipeFrom(
26
- new WritableStream<Uint8Array>({
27
- async start() {
28
- const pathAndMode = `${filename},${mode.toString()}`;
29
- await adbSyncWriteRequest(writer, AdbSyncRequestId.Send, pathAndMode);
30
- },
31
- async write(chunk) {
32
- await adbSyncWriteRequest(writer, AdbSyncRequestId.Data, chunk);
33
- },
34
- async close() {
35
- await adbSyncWriteRequest(writer, AdbSyncRequestId.Done, mtime);
36
- await adbSyncReadResponse(stream, ResponseTypes);
37
- },
38
- }),
39
- new ChunkStream(packetSize)
40
- );
41
- }
1
+ import { BufferedReadableStream, ChunkStream, pipeFrom, WritableStream, WritableStreamDefaultWriter } from '@yume-chan/stream-extra';
2
+ import Struct from '@yume-chan/struct';
3
+
4
+ import { AdbSyncRequestId, adbSyncWriteRequest } from './request.js';
5
+ import { adbSyncReadResponse, AdbSyncResponseId } from './response.js';
6
+ import { LinuxFileType } from './stat.js';
7
+
8
+ export const AdbSyncOkResponse =
9
+ new Struct({ littleEndian: true })
10
+ .uint32('unused');
11
+
12
+ export const ADB_SYNC_MAX_PACKET_SIZE = 64 * 1024;
13
+
14
+ export function adbSyncPush(
15
+ stream: BufferedReadableStream,
16
+ writer: WritableStreamDefaultWriter<Uint8Array>,
17
+ filename: string,
18
+ mode: number = (LinuxFileType.File << 12) | 0o666,
19
+ mtime: number = (Date.now() / 1000) | 0,
20
+ packetSize: number = ADB_SYNC_MAX_PACKET_SIZE,
21
+ ): WritableStream<Uint8Array> {
22
+ return pipeFrom(
23
+ new WritableStream<Uint8Array>({
24
+ async start() {
25
+ const pathAndMode = `${filename},${mode.toString()}`;
26
+ await adbSyncWriteRequest(writer, AdbSyncRequestId.Send, pathAndMode);
27
+ },
28
+ async write(chunk) {
29
+ await adbSyncWriteRequest(writer, AdbSyncRequestId.Data, chunk);
30
+ },
31
+ async close() {
32
+ await adbSyncWriteRequest(writer, AdbSyncRequestId.Done, mtime);
33
+ await adbSyncReadResponse(stream, AdbSyncResponseId.Ok, AdbSyncOkResponse);
34
+ },
35
+ }),
36
+ new ChunkStream(packetSize)
37
+ );
38
+ }
@@ -1,50 +1,51 @@
1
- import Struct from '@yume-chan/struct';
2
- import type { WritableStreamDefaultWriter } from "../../stream/index.js";
3
- import { encodeUtf8 } from "../../utils/index.js";
4
-
5
- export enum AdbSyncRequestId {
6
- List = 'LIST',
7
- List2 = 'LIS2',
8
- Send = 'SEND',
9
- Lstat = 'STAT',
10
- Stat = 'STA2',
11
- Lstat2 = 'LST2',
12
- Data = 'DATA',
13
- Done = 'DONE',
14
- Receive = 'RECV',
15
- }
16
-
17
- export const AdbSyncNumberRequest =
18
- new Struct({ littleEndian: true })
19
- .string('id', { length: 4 })
20
- .uint32('arg');
21
-
22
- export const AdbSyncDataRequest =
23
- new Struct({ littleEndian: true })
24
- .fields(AdbSyncNumberRequest)
25
- .uint8Array('data', { lengthField: 'arg' });
26
-
27
- export async function adbSyncWriteRequest(
28
- writer: WritableStreamDefaultWriter<Uint8Array>,
29
- id: AdbSyncRequestId | string,
30
- value: number | string | Uint8Array
31
- ): Promise<void> {
32
- let buffer: Uint8Array;
33
- if (typeof value === 'number') {
34
- buffer = AdbSyncNumberRequest.serialize({
35
- id,
36
- arg: value,
37
- });
38
- } else if (typeof value === 'string') {
39
- buffer = AdbSyncDataRequest.serialize({
40
- id,
41
- data: encodeUtf8(value),
42
- });
43
- } else {
44
- buffer = AdbSyncDataRequest.serialize({
45
- id,
46
- data: value,
47
- });
48
- }
49
- await writer.write(buffer);
50
- }
1
+ import type { WritableStreamDefaultWriter } from '@yume-chan/stream-extra';
2
+ import Struct from '@yume-chan/struct';
3
+
4
+ import { encodeUtf8 } from '../../utils/index.js';
5
+
6
+ export enum AdbSyncRequestId {
7
+ List = 'LIST',
8
+ List2 = 'LIS2',
9
+ Send = 'SEND',
10
+ Lstat = 'STAT',
11
+ Stat = 'STA2',
12
+ Lstat2 = 'LST2',
13
+ Data = 'DATA',
14
+ Done = 'DONE',
15
+ Receive = 'RECV',
16
+ }
17
+
18
+ export const AdbSyncNumberRequest =
19
+ new Struct({ littleEndian: true })
20
+ .string('id', { length: 4 })
21
+ .uint32('arg');
22
+
23
+ export const AdbSyncDataRequest =
24
+ new Struct({ littleEndian: true })
25
+ .fields(AdbSyncNumberRequest)
26
+ .uint8Array('data', { lengthField: 'arg' });
27
+
28
+ export async function adbSyncWriteRequest(
29
+ writer: WritableStreamDefaultWriter<Uint8Array>,
30
+ id: AdbSyncRequestId | string,
31
+ value: number | string | Uint8Array
32
+ ): Promise<void> {
33
+ let buffer: Uint8Array;
34
+ if (typeof value === 'number') {
35
+ buffer = AdbSyncNumberRequest.serialize({
36
+ id,
37
+ arg: value,
38
+ });
39
+ } else if (typeof value === 'string') {
40
+ buffer = AdbSyncDataRequest.serialize({
41
+ id,
42
+ data: encodeUtf8(value),
43
+ });
44
+ } else {
45
+ buffer = AdbSyncDataRequest.serialize({
46
+ id,
47
+ data: value,
48
+ });
49
+ }
50
+ await writer.write(buffer);
51
+ }