@yume-chan/adb 3.0.0-beta.1 → 3.0.0-beta.2

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 (118) hide show
  1. package/esm/adb.d.ts +7 -2
  2. package/esm/adb.d.ts.map +1 -1
  3. package/esm/adb.js +2 -2
  4. package/esm/adb.js.map +1 -1
  5. package/esm/banner.spec.d.ts +2 -0
  6. package/esm/banner.spec.d.ts.map +1 -0
  7. package/esm/daemon/auth/packet-processor.spec.d.ts +2 -0
  8. package/esm/daemon/auth/packet-processor.spec.d.ts.map +1 -0
  9. package/esm/daemon/auth/public-key.spec.d.ts +2 -0
  10. package/esm/daemon/auth/public-key.spec.d.ts.map +1 -0
  11. package/esm/daemon/crypto.spec.d.ts +2 -0
  12. package/esm/daemon/crypto.spec.d.ts.map +1 -0
  13. package/esm/server/client.spec.d.ts +2 -0
  14. package/esm/server/client.spec.d.ts.map +1 -0
  15. package/esm/server/transport.d.ts +1 -1
  16. package/esm/server/transport.d.ts.map +1 -1
  17. package/esm/server/transport.js +2 -2
  18. package/esm/server/transport.js.map +1 -1
  19. package/esm/service/reverse/response.spec.d.ts +2 -0
  20. package/esm/service/reverse/response.spec.d.ts.map +1 -0
  21. package/esm/service/subprocess/none/process.spec.d.ts +2 -0
  22. package/esm/service/subprocess/none/process.spec.d.ts.map +1 -0
  23. package/esm/service/subprocess/none/spawner.spec.d.ts +2 -0
  24. package/esm/service/subprocess/none/spawner.spec.d.ts.map +1 -0
  25. package/esm/service/subprocess/shell/process.spec.d.ts +2 -0
  26. package/esm/service/subprocess/shell/process.spec.d.ts.map +1 -0
  27. package/esm/service/subprocess/shell/shared.spec.d.ts +2 -0
  28. package/esm/service/subprocess/shell/shared.spec.d.ts.map +1 -0
  29. package/esm/service/subprocess/shell/spawner.spec.d.ts +2 -0
  30. package/esm/service/subprocess/shell/spawner.spec.d.ts.map +1 -0
  31. package/esm/service/subprocess/utils.spec.d.ts +2 -0
  32. package/esm/service/subprocess/utils.spec.d.ts.map +1 -0
  33. package/esm/service/sync/compression/adapters.d.ts +16 -0
  34. package/esm/service/sync/compression/adapters.d.ts.map +1 -0
  35. package/esm/service/sync/compression/adapters.js +84 -0
  36. package/esm/service/sync/compression/adapters.js.map +1 -0
  37. package/esm/service/sync/compression/format-ns.d.ts.map +1 -1
  38. package/esm/service/sync/compression/format-ns.js +1 -0
  39. package/esm/service/sync/compression/format-ns.js.map +1 -1
  40. package/esm/service/sync/compression/format.d.ts +1 -0
  41. package/esm/service/sync/compression/format.d.ts.map +1 -1
  42. package/esm/service/sync/compression/format.js +1 -0
  43. package/esm/service/sync/compression/format.js.map +1 -1
  44. package/esm/service/sync/compression/index.d.ts +1 -1
  45. package/esm/service/sync/compression/index.d.ts.map +1 -1
  46. package/esm/service/sync/compression/index.js +1 -1
  47. package/esm/service/sync/compression/index.js.map +1 -1
  48. package/esm/service/sync/compression/mode.d.ts +6 -0
  49. package/esm/service/sync/compression/mode.d.ts.map +1 -0
  50. package/esm/service/sync/compression/mode.js +5 -0
  51. package/esm/service/sync/compression/mode.js.map +1 -0
  52. package/esm/service/sync/compression/native.d.ts +8 -0
  53. package/esm/service/sync/compression/native.d.ts.map +1 -0
  54. package/esm/service/sync/compression/native.js +53 -0
  55. package/esm/service/sync/compression/native.js.map +1 -0
  56. package/esm/service/sync/id/request.d.ts +1 -0
  57. package/esm/service/sync/id/request.d.ts.map +1 -1
  58. package/esm/service/sync/id/request.js +1 -0
  59. package/esm/service/sync/id/request.js.map +1 -1
  60. package/esm/service/sync/request/flag.d.ts +9 -0
  61. package/esm/service/sync/request/flag.d.ts.map +1 -0
  62. package/esm/service/sync/request/flag.js +8 -0
  63. package/esm/service/sync/request/flag.js.map +1 -0
  64. package/esm/service/sync/request/pull.d.ts +25 -2
  65. package/esm/service/sync/request/pull.d.ts.map +1 -1
  66. package/esm/service/sync/request/pull.js +76 -16
  67. package/esm/service/sync/request/pull.js.map +1 -1
  68. package/esm/service/sync/request/push.d.ts +36 -14
  69. package/esm/service/sync/request/push.d.ts.map +1 -1
  70. package/esm/service/sync/request/push.js +176 -60
  71. package/esm/service/sync/request/push.js.map +1 -1
  72. package/esm/service/sync/service.d.ts +32 -16
  73. package/esm/service/sync/service.d.ts.map +1 -1
  74. package/esm/service/sync/service.js +58 -13
  75. package/esm/service/sync/service.js.map +1 -1
  76. package/esm/service/sync/socket-pool.d.ts +1 -0
  77. package/esm/service/sync/socket-pool.d.ts.map +1 -1
  78. package/esm/service/sync/socket-pool.js +45 -9
  79. package/esm/service/sync/socket-pool.js.map +1 -1
  80. package/esm/service/sync/socket-pool.spec.d.ts +2 -0
  81. package/esm/service/sync/socket-pool.spec.d.ts.map +1 -0
  82. package/esm/service/sync/socket.d.ts +4 -0
  83. package/esm/service/sync/socket.d.ts.map +1 -1
  84. package/esm/service/sync/socket.js +16 -0
  85. package/esm/service/sync/socket.js.map +1 -1
  86. package/esm/service/sync/socket.spec.d.ts +2 -0
  87. package/esm/service/sync/socket.spec.d.ts.map +1 -0
  88. package/esm/utils/base64.spec.d.ts +2 -0
  89. package/esm/utils/base64.spec.d.ts.map +1 -0
  90. package/esm/utils/md5.spec.d.ts +2 -0
  91. package/esm/utils/md5.spec.d.ts.map +1 -0
  92. package/esm/utils/ref.d.ts +3 -5
  93. package/esm/utils/ref.d.ts.map +1 -1
  94. package/esm/utils/ref.js +12 -6
  95. package/esm/utils/ref.js.map +1 -1
  96. package/package.json +5 -5
  97. package/src/adb.ts +15 -3
  98. package/src/server/transport.ts +5 -1
  99. package/src/service/sync/compression/adapters.ts +137 -0
  100. package/src/service/sync/compression/format-ns.ts +2 -0
  101. package/src/service/sync/compression/format.ts +5 -0
  102. package/src/service/sync/compression/index.ts +1 -1
  103. package/src/service/sync/compression/mode.ts +6 -0
  104. package/src/service/sync/compression/native.ts +73 -0
  105. package/src/service/sync/id/request.ts +1 -0
  106. package/src/service/sync/request/flag.ts +9 -0
  107. package/src/service/sync/request/pull.ts +135 -27
  108. package/src/service/sync/request/push.ts +241 -91
  109. package/src/service/sync/service.ts +87 -27
  110. package/src/service/sync/socket-pool.ts +50 -9
  111. package/src/service/sync/socket.ts +23 -1
  112. package/src/utils/ref.ts +17 -7
  113. package/yume-chan-adb-3.0.0-beta.0.tgz +0 -0
  114. package/esm/service/sync/compression/compression.d.ts +0 -17
  115. package/esm/service/sync/compression/compression.d.ts.map +0 -1
  116. package/esm/service/sync/compression/compression.js +0 -92
  117. package/esm/service/sync/compression/compression.js.map +0 -1
  118. package/src/service/sync/compression/compression.ts +0 -126
@@ -21,7 +21,15 @@ import { ResponseId } from "./id/index.js";
21
21
 
22
22
  const NumberRequest = struct({ id: u32, arg: u32 }, { littleEndian: true });
23
23
 
24
- class AdbSyncError extends Error {}
24
+ const AdbSyncErrorBrand = Symbol.for("AdbSyncError.brand");
25
+
26
+ class AdbSyncError extends Error {
27
+ [AdbSyncErrorBrand] = true;
28
+
29
+ static override [Symbol.hasInstance](value: unknown) {
30
+ return !!(value as AdbSyncError | undefined)?.[AdbSyncErrorBrand];
31
+ }
32
+ }
25
33
  export { AdbSyncError as Error };
26
34
 
27
35
  const FailResponse = struct(
@@ -38,10 +46,13 @@ function i32ToHex(i: number) {
38
46
  return "0x" + i.toString(16).padStart(8, "0");
39
47
  }
40
48
 
49
+ let NextId = 0;
50
+
41
51
  export class Socket implements AsyncExactReadable {
42
52
  static readonly NumberRequest = NumberRequest;
43
53
  static readonly FailResponse = FailResponse;
44
54
 
55
+ #id: number;
45
56
  readonly #socket: Adb.Socket;
46
57
  readonly #writer: WritableStreamDefaultWriter<MaybeConsumable<Uint8Array>>;
47
58
  readonly #readable: BufferedReadableStream;
@@ -49,6 +60,10 @@ export class Socket implements AsyncExactReadable {
49
60
  readonly #combiner: BufferCombiner;
50
61
  #idleTimeoutId: ReturnType<typeof setTimeout> | null = null;
51
62
 
63
+ get id() {
64
+ return this.#id;
65
+ }
66
+
52
67
  get position() {
53
68
  return this.#readable.position;
54
69
  }
@@ -58,6 +73,9 @@ export class Socket implements AsyncExactReadable {
58
73
  }
59
74
 
60
75
  constructor(socket: Adb.Socket, bufferSize: number) {
76
+ this.#id = NextId;
77
+ NextId += 1;
78
+
61
79
  this.#socket = socket;
62
80
  this.#writer = socket.writable.getWriter();
63
81
  this.#readable = new BufferedReadableStream(socket.readable);
@@ -73,6 +91,10 @@ export class Socket implements AsyncExactReadable {
73
91
  startIdleTimer(timeout: number, callback: () => void) {
74
92
  this.clearIdleTimer();
75
93
  this.#idleTimeoutId = setTimeout(callback, timeout);
94
+ if (typeof this.#idleTimeoutId === "object") {
95
+ // Node.js: unref the timer so that it won't keep the process alive
96
+ (this.#idleTimeoutId as { unref?: () => void }).unref?.();
97
+ }
76
98
  }
77
99
 
78
100
  /**
package/src/utils/ref.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { Disposable } from "@yume-chan/event";
2
+
1
3
  interface GlobalExtension {
2
4
  setInterval: (callback: () => void, delay: number) => number;
3
5
  clearInterval: (id: number) => void;
@@ -9,14 +11,12 @@ const { setInterval, clearInterval } = globalThis as unknown as GlobalExtension;
9
11
  * An object to keep current Node.js process alive even when no code is running.
10
12
  *
11
13
  * Does nothing in Web environments.
12
- *
13
- * Note that it does't have reference counting. Calling `unref` will
14
- * remove the ref no matter how many times `ref` has been previously called, and vice versa.
15
- * This is the same as how Node.js works.
16
14
  */
17
- export class Ref {
15
+ export class Ref implements Disposable {
18
16
  #intervalId: number | undefined;
19
17
 
18
+ #count = 0;
19
+
20
20
  constructor(options?: { unref?: boolean | undefined }) {
21
21
  if (!options?.unref) {
22
22
  this.ref();
@@ -24,11 +24,21 @@ export class Ref {
24
24
  }
25
25
 
26
26
  ref() {
27
- // `setInterval` can keep current Node.js alive, the delay value doesn't matter
28
- this.#intervalId = setInterval(() => {}, 60 * 1000);
27
+ if (this.#count === 0) {
28
+ // `setInterval` can keep current Node.js alive, the delay value doesn't matter
29
+ this.#intervalId = setInterval(() => {}, 60 * 1000);
30
+ }
31
+ this.#count += 1;
29
32
  }
30
33
 
31
34
  unref() {
35
+ this.#count -= 1;
36
+ if (this.#count === 0) {
37
+ this.dispose();
38
+ }
39
+ }
40
+
41
+ dispose(): void {
32
42
  if (this.#intervalId) {
33
43
  clearInterval(this.#intervalId);
34
44
  this.#intervalId = undefined;
Binary file
@@ -1,17 +0,0 @@
1
- import { CompressionStream } from "@yume-chan/stream-extra";
2
- import type { Adb } from "../../../adb.js";
3
- import { Format } from "./format.js";
4
- export { Format };
5
- export declare const Mode: {
6
- readonly Compress: 0;
7
- readonly Decompress: 1;
8
- };
9
- export type Mode = (typeof Mode)[keyof typeof Mode];
10
- export declare function hasRuntimeSupport(format: Format, mode: Mode): boolean;
11
- export declare function canUseBrotli(adb: Adb, mode: Mode): boolean;
12
- export declare function canUseLz4(adb: Adb, mode: Mode): boolean;
13
- export declare function canUseZstd(adb: Adb, mode: Mode): boolean;
14
- export declare function canUseFormat(adb: Adb, format: Format, mode: Mode): boolean;
15
- export declare function chooseFormat(adb: Adb, mode: Mode): 0 | 3 | 4 | 2;
16
- export declare function createCompressionStream(format: Format): CompressionStream;
17
- //# sourceMappingURL=compression.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"compression.d.ts","sourceRoot":"","sources":["../../../../src/service/sync/compression/compression.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,iBAAiB,EAGpB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAI3C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,MAAM,EAAE,CAAC;AAElB,eAAO,MAAM,IAAI;;;CAGP,CAAC;AAEX,MAAM,MAAM,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,CAAC,MAAM,OAAO,IAAI,CAAC,CAAC;AAepD,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,WA4B3D;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,WAKhD;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,WAK7C;AAED,wBAAgB,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,WAK9C;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,WAahE;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,iBAiBhD;AAED,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,MAAM,GAAG,iBAAiB,CAMzE"}
@@ -1,92 +0,0 @@
1
- import { CompressionStream, DecompressionStream, TransformStream, } from "@yume-chan/stream-extra";
2
- import { AdbFeature } from "../../../features.js";
3
- import { NOOP } from "../../../utils/no-op.js";
4
- import { Format } from "./format.js";
5
- export { Format };
6
- export const Mode = {
7
- Compress: 0,
8
- Decompress: 1,
9
- };
10
- const FormatMap = {
11
- [Format.None]: "",
12
- // https://github.com/whatwg/compression/pull/80
13
- [Format.Zstd]: "zstd",
14
- // placeholder
15
- [Format.Lz4]: "lz4",
16
- // https://github.com/whatwg/compression/pull/80
17
- [Format.Brotli]: "brotli",
18
- };
19
- const CompressionFormatCache = new Map();
20
- const DecompressionFormatCache = new Map();
21
- export function hasRuntimeSupport(format, mode) {
22
- if (format === Format.None) {
23
- return true;
24
- }
25
- const Cache = mode === Mode.Compress
26
- ? CompressionFormatCache
27
- : DecompressionFormatCache;
28
- if (Cache.has(format)) {
29
- return Cache.get(format);
30
- }
31
- const Stream = mode === Mode.Compress ? CompressionStream : DecompressionStream;
32
- if (!Stream) {
33
- return false;
34
- }
35
- try {
36
- const stream = new Stream(FormatMap[format]);
37
- void stream.writable.abort().catch(NOOP);
38
- Cache.set(format, true);
39
- return true;
40
- }
41
- catch {
42
- Cache.set(format, false);
43
- return false;
44
- }
45
- }
46
- export function canUseBrotli(adb, mode) {
47
- return (adb.canUseFeature(AdbFeature.SendReceive2Brotli) &&
48
- hasRuntimeSupport(Format.Brotli, mode));
49
- }
50
- export function canUseLz4(adb, mode) {
51
- return (adb.canUseFeature(AdbFeature.SendReceive2Lz4) &&
52
- hasRuntimeSupport(Format.Lz4, mode));
53
- }
54
- export function canUseZstd(adb, mode) {
55
- return (adb.canUseFeature(AdbFeature.SendReceive2Zstd) &&
56
- hasRuntimeSupport(Format.Zstd, mode));
57
- }
58
- export function canUseFormat(adb, format, mode) {
59
- switch (format) {
60
- case Format.None:
61
- return true;
62
- case Format.Brotli:
63
- return canUseBrotli(adb, mode);
64
- case Format.Lz4:
65
- return canUseLz4(adb, mode);
66
- case Format.Zstd:
67
- return canUseZstd(adb, mode);
68
- default:
69
- return false;
70
- }
71
- }
72
- export function chooseFormat(adb, mode) {
73
- // The order follows
74
- // https://android.googlesource.com/platform/packages/modules/adb/+/3da39565cad412a743a58b94b875a43ed3c640d3/client/file_sync_client.cpp#277
75
- if (canUseZstd(adb, mode)) {
76
- return Format.Zstd;
77
- }
78
- if (canUseLz4(adb, mode)) {
79
- return Format.Lz4;
80
- }
81
- if (canUseBrotli(adb, mode)) {
82
- return Format.Brotli;
83
- }
84
- return Format.None;
85
- }
86
- export function createCompressionStream(format) {
87
- if (format === Format.None) {
88
- return new TransformStream();
89
- }
90
- return new CompressionStream(FormatMap[format]);
91
- }
92
- //# sourceMappingURL=compression.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"compression.js","sourceRoot":"","sources":["../../../../src/service/sync/compression/compression.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,iBAAiB,EACjB,mBAAmB,EACnB,eAAe,GAClB,MAAM,yBAAyB,CAAC;AAGjC,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAE/C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CA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C;YACI,OAAO,KAAK,CAAC;IACrB,CAAC;AACL,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,GAAQ,EAAE,IAAU;IAC7C,oBAAoB;IACpB,4IAA4I;IAE5I,IAAI,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,CAAC;QACxB,OAAO,MAAM,CAAC,IAAI,CAAC;IACvB,CAAC;IAED,IAAI,SAAS,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,CAAC;QACvB,OAAO,MAAM,CAAC,GAAG,CAAC;IACtB,CAAC;IAED,IAAI,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,CAAC;QAC1B,OAAO,MAAM,CAAC,MAAM,CAAC;IACzB,CAAC;IAED,OAAO,MAAM,CAAC,IAAI,CAAC;AACvB,CAAC;AAED,MAAM,UAAU,uBAAuB,CAAC,MAAc;IAClD,IAAI,MAAM,KAAK,MAAM,CAAC,IAAI,EAAE,CAAC;QACzB,OAAO,IAAI,eAAe,EAAE,CAAC;IACjC,CAAC;IAED,OAAO,IAAI,iBAAiB,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;AACpD,CAAC"}
@@ -1,126 +0,0 @@
1
- import {
2
- CompressionStream,
3
- DecompressionStream,
4
- TransformStream,
5
- } from "@yume-chan/stream-extra";
6
-
7
- import type { Adb } from "../../../adb.js";
8
- import { AdbFeature } from "../../../features.js";
9
- import { NOOP } from "../../../utils/no-op.js";
10
-
11
- import { Format } from "./format.js";
12
-
13
- export { Format };
14
-
15
- export const Mode = {
16
- Compress: 0,
17
- Decompress: 1,
18
- } as const;
19
-
20
- export type Mode = (typeof Mode)[keyof typeof Mode];
21
-
22
- const FormatMap: Record<Format, string> = {
23
- [Format.None]: "",
24
- // https://github.com/whatwg/compression/pull/80
25
- [Format.Zstd]: "zstd",
26
- // placeholder
27
- [Format.Lz4]: "lz4",
28
- // https://github.com/whatwg/compression/pull/80
29
- [Format.Brotli]: "brotli",
30
- };
31
-
32
- const CompressionFormatCache = new Map<Format, boolean>();
33
- const DecompressionFormatCache = new Map<Format, boolean>();
34
-
35
- export function hasRuntimeSupport(format: Format, mode: Mode) {
36
- if (format === Format.None) {
37
- return true;
38
- }
39
-
40
- const Cache =
41
- mode === Mode.Compress
42
- ? CompressionFormatCache
43
- : DecompressionFormatCache;
44
- if (Cache.has(format)) {
45
- return Cache.get(format)!;
46
- }
47
-
48
- const Stream =
49
- mode === Mode.Compress ? CompressionStream : DecompressionStream;
50
- if (!Stream) {
51
- return false;
52
- }
53
-
54
- try {
55
- const stream = new Stream(FormatMap[format]);
56
- void stream.writable.abort().catch(NOOP);
57
- Cache.set(format, true);
58
- return true;
59
- } catch {
60
- Cache.set(format, false);
61
- return false;
62
- }
63
- }
64
-
65
- export function canUseBrotli(adb: Adb, mode: Mode) {
66
- return (
67
- adb.canUseFeature(AdbFeature.SendReceive2Brotli) &&
68
- hasRuntimeSupport(Format.Brotli, mode)
69
- );
70
- }
71
-
72
- export function canUseLz4(adb: Adb, mode: Mode) {
73
- return (
74
- adb.canUseFeature(AdbFeature.SendReceive2Lz4) &&
75
- hasRuntimeSupport(Format.Lz4, mode)
76
- );
77
- }
78
-
79
- export function canUseZstd(adb: Adb, mode: Mode) {
80
- return (
81
- adb.canUseFeature(AdbFeature.SendReceive2Zstd) &&
82
- hasRuntimeSupport(Format.Zstd, mode)
83
- );
84
- }
85
-
86
- export function canUseFormat(adb: Adb, format: Format, mode: Mode) {
87
- switch (format) {
88
- case Format.None:
89
- return true;
90
- case Format.Brotli:
91
- return canUseBrotli(adb, mode);
92
- case Format.Lz4:
93
- return canUseLz4(adb, mode);
94
- case Format.Zstd:
95
- return canUseZstd(adb, mode);
96
- default:
97
- return false;
98
- }
99
- }
100
-
101
- export function chooseFormat(adb: Adb, mode: Mode) {
102
- // The order follows
103
- // https://android.googlesource.com/platform/packages/modules/adb/+/3da39565cad412a743a58b94b875a43ed3c640d3/client/file_sync_client.cpp#277
104
-
105
- if (canUseZstd(adb, mode)) {
106
- return Format.Zstd;
107
- }
108
-
109
- if (canUseLz4(adb, mode)) {
110
- return Format.Lz4;
111
- }
112
-
113
- if (canUseBrotli(adb, mode)) {
114
- return Format.Brotli;
115
- }
116
-
117
- return Format.None;
118
- }
119
-
120
- export function createCompressionStream(format: Format): CompressionStream {
121
- if (format === Format.None) {
122
- return new TransformStream();
123
- }
124
-
125
- return new CompressionStream(FormatMap[format]);
126
- }