@yume-chan/adb 0.0.13 → 0.0.16

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 (116) hide show
  1. package/CHANGELOG.json +57 -0
  2. package/CHANGELOG.md +29 -1
  3. package/LICENSE +21 -21
  4. package/README.md +247 -247
  5. package/esm/adb.d.ts +12 -4
  6. package/esm/adb.d.ts.map +1 -1
  7. package/esm/adb.js +29 -19
  8. package/esm/adb.js.map +1 -1
  9. package/esm/commands/reverse.d.ts +9 -12
  10. package/esm/commands/reverse.d.ts.map +1 -1
  11. package/esm/commands/reverse.js +44 -36
  12. package/esm/commands/reverse.js.map +1 -1
  13. package/esm/commands/subprocess/protocols/none.d.ts +2 -1
  14. package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
  15. package/esm/commands/subprocess/protocols/none.js +10 -9
  16. package/esm/commands/subprocess/protocols/none.js.map +1 -1
  17. package/esm/commands/sync/pull.d.ts.map +1 -1
  18. package/esm/commands/sync/pull.js +6 -3
  19. package/esm/commands/sync/pull.js.map +1 -1
  20. package/esm/commands/sync/stat.d.ts.map +1 -1
  21. package/esm/commands/sync/stat.js +2 -2
  22. package/esm/commands/sync/stat.js.map +1 -1
  23. package/esm/commands/sync/sync.js +1 -1
  24. package/esm/commands/sync/sync.js.map +1 -1
  25. package/esm/socket/dispatcher.d.ts +25 -14
  26. package/esm/socket/dispatcher.d.ts.map +1 -1
  27. package/esm/socket/dispatcher.js +94 -63
  28. package/esm/socket/dispatcher.js.map +1 -1
  29. package/esm/socket/socket.d.ts +18 -4
  30. package/esm/socket/socket.d.ts.map +1 -1
  31. package/esm/socket/socket.js +36 -21
  32. package/esm/socket/socket.js.map +1 -1
  33. package/esm/stream/buffered.d.ts +7 -3
  34. package/esm/stream/buffered.d.ts.map +1 -1
  35. package/esm/stream/buffered.js +69 -49
  36. package/esm/stream/buffered.js.map +1 -1
  37. package/esm/stream/detect.js +1 -0
  38. package/esm/stream/detect.js.map +1 -1
  39. package/esm/stream/detect.polyfill.d.ts +1 -1
  40. package/esm/stream/detect.polyfill.d.ts.map +1 -1
  41. package/esm/stream/detect.polyfill.js +0 -5
  42. package/esm/stream/detect.polyfill.js.map +1 -1
  43. package/esm/stream/transform.d.ts +39 -13
  44. package/esm/stream/transform.d.ts.map +1 -1
  45. package/esm/stream/transform.js +67 -90
  46. package/esm/stream/transform.js.map +1 -1
  47. package/esm/utils/base64.js +5 -5
  48. package/esm/utils/base64.js.map +1 -1
  49. package/package.json +10 -10
  50. package/src/adb.ts +280 -270
  51. package/src/auth.ts +173 -173
  52. package/src/backend.ts +11 -11
  53. package/src/commands/base.ts +11 -11
  54. package/src/commands/framebuffer.ts +73 -73
  55. package/src/commands/index.ts +8 -8
  56. package/src/commands/install.ts +30 -30
  57. package/src/commands/power.ts +54 -54
  58. package/src/commands/reverse.ts +140 -136
  59. package/src/commands/subprocess/index.ts +139 -139
  60. package/src/commands/subprocess/protocols/none.ts +70 -68
  61. package/src/commands/subprocess/protocols/shell.ts +190 -190
  62. package/src/commands/subprocess/protocols/types.ts +59 -59
  63. package/src/commands/subprocess/utils.ts +20 -20
  64. package/src/commands/sync/index.ts +7 -7
  65. package/src/commands/sync/list.ts +86 -86
  66. package/src/commands/sync/pull.ts +46 -43
  67. package/src/commands/sync/push.ts +41 -41
  68. package/src/commands/sync/request.ts +50 -50
  69. package/src/commands/sync/response.ts +64 -64
  70. package/src/commands/sync/stat.ts +151 -150
  71. package/src/commands/sync/sync.ts +181 -181
  72. package/src/commands/tcpip.ts +21 -21
  73. package/src/crypto.ts +296 -296
  74. package/src/features.ts +9 -9
  75. package/src/index.ts +11 -11
  76. package/src/packet.ts +80 -80
  77. package/src/socket/dispatcher.ts +305 -270
  78. package/src/socket/index.ts +2 -2
  79. package/src/socket/socket.ts +155 -136
  80. package/src/stream/buffered.ts +168 -147
  81. package/src/stream/detect.native.ts +362 -362
  82. package/src/stream/detect.polyfill.ts +22 -28
  83. package/src/stream/detect.ts +7 -5
  84. package/src/stream/index.ts +3 -3
  85. package/src/stream/transform.ts +474 -483
  86. package/src/utils/auto-reset-event.ts +41 -41
  87. package/src/utils/base64.ts +306 -306
  88. package/src/utils/index.ts +3 -3
  89. package/tsconfig.build.json +3 -3
  90. package/tsconfig.build.tsbuildinfo +1 -1
  91. package/tsconfig.test.json +9 -10
  92. package/web-streams-polyfill-4.0.0-beta.2.tgz +0 -0
  93. package/esm/commands/subprocess/legacy.d.ts +0 -35
  94. package/esm/commands/subprocess/legacy.d.ts.map +0 -1
  95. package/esm/commands/subprocess/legacy.js +0 -53
  96. package/esm/commands/subprocess/legacy.js.map +0 -1
  97. package/esm/commands/subprocess/protocol.d.ts +0 -39
  98. package/esm/commands/subprocess/protocol.d.ts.map +0 -1
  99. package/esm/commands/subprocess/protocol.js +0 -157
  100. package/esm/commands/subprocess/protocol.js.map +0 -1
  101. package/esm/commands/subprocess/types.d.ts +0 -50
  102. package/esm/commands/subprocess/types.d.ts.map +0 -1
  103. package/esm/commands/subprocess/types.js +0 -2
  104. package/esm/commands/subprocess/types.js.map +0 -1
  105. package/esm/socket/controller.d.ts +0 -37
  106. package/esm/socket/controller.d.ts.map +0 -1
  107. package/esm/socket/controller.js +0 -66
  108. package/esm/socket/controller.js.map +0 -1
  109. package/esm/stream/detect.bak.d.ts +0 -240
  110. package/esm/stream/detect.bak.d.ts.map +0 -1
  111. package/esm/stream/detect.bak.js +0 -60
  112. package/esm/stream/detect.bak.js.map +0 -1
  113. package/esm/utils/encoding.d.ts +0 -3
  114. package/esm/utils/encoding.d.ts.map +0 -1
  115. package/esm/utils/encoding.js +0 -11
  116. package/esm/utils/encoding.js.map +0 -1
@@ -1,483 +1,474 @@
1
- import { PromiseResolver } from "@yume-chan/async";
2
- import type Struct from "@yume-chan/struct";
3
- import type { StructValueType, ValueOrPromise } from "@yume-chan/struct";
4
- import { decodeUtf8 } from "../utils/index.js";
5
- import { BufferedStream, BufferedStreamEndedError } from "./buffered.js";
6
- import { AbortController, AbortSignal, ReadableStream, ReadableStreamDefaultReader, TransformStream, WritableStream, WritableStreamDefaultWriter, type QueuingStrategy, type ReadableStreamDefaultController, type ReadableWritablePair, type UnderlyingSink, type UnderlyingSource } from "./detect.js";
7
-
8
- export interface DuplexStreamFactoryOptions {
9
- preventCloseReadableStreams?: boolean | undefined;
10
-
11
- close?: (() => void | Promise<void>) | undefined;
12
- }
13
-
14
- /**
15
- * A factory for creating a duplex stream.
16
- *
17
- * It can create multiple `ReadableStream`s and `WritableStream`s,
18
- * when any of them is closed, all other streams will be closed as well.
19
- */
20
- export class DuplexStreamFactory<R, W> {
21
- private readableControllers: ReadableStreamDefaultController<R>[] = [];
22
- private pushReadableControllers: PushReadableStreamController<R>[] = [];
23
-
24
- private _closed = new PromiseResolver<void>();
25
- public get closed() { return this._closed.promise; }
26
-
27
- private options: DuplexStreamFactoryOptions;
28
-
29
- private _closeRequestedByReadable = false;
30
- private _writableClosed = false;
31
-
32
- public constructor(options?: DuplexStreamFactoryOptions) {
33
- this.options = options ?? {};
34
- }
35
-
36
- public createPushReadable(source: PushReadableStreamSource<R>, strategy?: QueuingStrategy<R>): PushReadableStream<R> {
37
- return new PushReadableStream<R>(controller => {
38
- this.pushReadableControllers.push(controller);
39
-
40
- controller.abortSignal.addEventListener('abort', async () => {
41
- this._closeRequestedByReadable = true;
42
- await this.close();
43
- });
44
-
45
- source({
46
- abortSignal: controller.abortSignal,
47
- async enqueue(chunk) {
48
- await controller.enqueue(chunk);
49
- },
50
- close: async () => {
51
- // The source signals stream ended,
52
- // usually means the other end closed the connection first.
53
- controller.close();
54
- this._closeRequestedByReadable = true;
55
- await this.close();
56
- },
57
- error: async (e?: any) => {
58
- controller.error(e);
59
- this._closeRequestedByReadable = true;
60
- await this.close();
61
- },
62
- });
63
- }, strategy);
64
- };
65
-
66
- public createWrapReadable(wrapper: ReadableStream<R> | WrapReadableStreamStart<R> | ReadableStreamWrapper<R>): WrapReadableStream<R> {
67
- return new WrapReadableStream<R>({
68
- async start() {
69
- return getWrappedReadableStream(wrapper);
70
- },
71
- close: async () => {
72
- if ('close' in wrapper) {
73
- await wrapper.close?.();
74
- }
75
- this._closeRequestedByReadable = true;
76
- await this.close();
77
- },
78
- });
79
- }
80
-
81
- public createReadable(source?: UnderlyingSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R> {
82
- return new ReadableStream<R>({
83
- start: async (controller) => {
84
- this.readableControllers.push(controller);
85
- await source?.start?.(controller);
86
- },
87
- pull: (controller) => {
88
- return source?.pull?.(controller);
89
- },
90
- cancel: async (reason) => {
91
- await source?.cancel?.(reason);
92
- this._closeRequestedByReadable = true;
93
- await this.close();
94
- },
95
- }, strategy);
96
- }
97
-
98
- public createWritable(sink: UnderlyingSink<W>, strategy?: QueuingStrategy<W>): WritableStream<W> {
99
- return new WritableStream<W>({
100
- start: async (controller) => {
101
- await sink.start?.(controller);
102
- },
103
- write: async (chunk, controller) => {
104
- if (this._writableClosed) {
105
- throw new Error("stream is closed");
106
- }
107
-
108
- await sink.write?.(chunk, controller);
109
- },
110
- close: async () => {
111
- await sink.close?.();
112
- this.close();
113
- },
114
- abort: async (reason) => {
115
- await sink.abort?.(reason);
116
- await this.close();
117
- },
118
- }, strategy);
119
- }
120
-
121
- public async closeReadableStreams() {
122
- this._closed.resolve();
123
- await this.options.close?.();
124
-
125
- for (const controller of this.readableControllers) {
126
- try {
127
- controller.close();
128
- } catch { }
129
- }
130
-
131
- for (const controller of this.pushReadableControllers) {
132
- try {
133
- controller.close();
134
- } catch { }
135
- }
136
- }
137
-
138
- public async close() {
139
- this._writableClosed = true;
140
-
141
- if (this._closeRequestedByReadable ||
142
- !this.options.preventCloseReadableStreams) {
143
- await this.closeReadableStreams();
144
- }
145
- }
146
- }
147
-
148
- export class DecodeUtf8Stream extends TransformStream<Uint8Array, string>{
149
- public constructor() {
150
- super({
151
- transform(chunk, controller) {
152
- controller.enqueue(decodeUtf8(chunk));
153
- },
154
- });
155
- }
156
- }
157
-
158
- export class GatherStringStream extends WritableStream<string>{
159
- // Optimization: rope (concat strings) is faster than `[].join('')`
160
- private _result = '';
161
- public get result() { return this._result; }
162
-
163
- public constructor() {
164
- super({
165
- write: (chunk) => {
166
- this._result += chunk;
167
- },
168
- });
169
- }
170
- }
171
-
172
- // TODO: StructTransformStream: Looking for better implementation
173
- export class StructDeserializeStream<T extends Struct<any, any, any, any>>
174
- implements ReadableWritablePair<Uint8Array, StructValueType<T>>{
175
- private _readable: ReadableStream<StructValueType<T>>;
176
- public get readable() { return this._readable; }
177
-
178
- private _writable: WritableStream<Uint8Array>;
179
- public get writable() { return this._writable; }
180
-
181
- public constructor(struct: T) {
182
- // Convert incoming chunks to a `BufferedStream`
183
- let incomingStreamController!: PushReadableStreamController<Uint8Array>;
184
- const incomingStream = new BufferedStream(
185
- new PushReadableStream<Uint8Array>(
186
- controller => incomingStreamController = controller,
187
- )
188
- );
189
-
190
- this._readable = new ReadableStream<StructValueType<T>>({
191
- async pull(controller) {
192
- try {
193
- const value = await struct.deserialize(incomingStream);
194
- controller.enqueue(value);
195
- } catch (e) {
196
- if (e instanceof BufferedStreamEndedError) {
197
- controller.close();
198
- return;
199
- }
200
- throw e;
201
- }
202
- }
203
- });
204
-
205
- this._writable = new WritableStream({
206
- async write(chunk) {
207
- await incomingStreamController.enqueue(chunk);
208
- },
209
- abort() {
210
- incomingStreamController.close();
211
- },
212
- close() {
213
- incomingStreamController.close();
214
- },
215
- });
216
- }
217
- }
218
-
219
- export class StructSerializeStream<T extends Struct<any, any, any, any>>
220
- extends TransformStream<T['TInit'], Uint8Array>{
221
- constructor(struct: T) {
222
- super({
223
- transform(chunk, controller) {
224
- controller.enqueue(struct.serialize(chunk));
225
- },
226
- });
227
- }
228
- }
229
-
230
- export type WrapWritableStreamStart<T> = () => ValueOrPromise<WritableStream<T>>;
231
-
232
- export interface WritableStreamWrapper<T> {
233
- start: WrapWritableStreamStart<T>;
234
- close?(): Promise<void>;
235
- }
236
-
237
- async function getWrappedWritableStream<T>(
238
- wrapper: WritableStream<T> | WrapWritableStreamStart<T> | WritableStreamWrapper<T>
239
- ) {
240
- if ('start' in wrapper) {
241
- return await wrapper.start();
242
- } else if (typeof wrapper === 'function') {
243
- return await wrapper();
244
- } else {
245
- // Can't use `wrapper instanceof WritableStream`
246
- // Because we want to be compatible with any WritableStream-like objects
247
- return wrapper;
248
- }
249
- }
250
-
251
- export class WrapWritableStream<T> extends WritableStream<T> {
252
- public writable!: WritableStream<T>;
253
-
254
- private writer!: WritableStreamDefaultWriter<T>;
255
-
256
- public constructor(wrapper: WritableStream<T> | WrapWritableStreamStart<T> | WritableStreamWrapper<T>) {
257
- super({
258
- start: async () => {
259
- // `start` is invoked before `ReadableStream`'s constructor finish,
260
- // so using `this` synchronously causes
261
- // "Must call super constructor in derived class before accessing 'this' or returning from derived constructor".
262
- // Queue a microtask to avoid this.
263
- await Promise.resolve();
264
-
265
- this.writable = await getWrappedWritableStream(wrapper);
266
- this.writer = this.writable.getWriter();
267
- },
268
- write: async (chunk) => {
269
- // Maintain back pressure
270
- await this.writer.ready;
271
- await this.writer.write(chunk);
272
- },
273
- abort: async (reason) => {
274
- await this.writer.abort(reason);
275
- if ('close' in wrapper) {
276
- await wrapper.close?.();
277
- }
278
- },
279
- close: async () => {
280
- // Close the inner stream first.
281
- // Usually the inner stream is a logical sub-stream over the outer stream,
282
- // closing the outer stream first will make the inner stream incapable of
283
- // sending data in its `close` handler.
284
- await this.writer.close();
285
- if ('close' in wrapper) {
286
- await wrapper.close?.();
287
- }
288
- },
289
- });
290
- }
291
- }
292
-
293
- export type WrapReadableStreamStart<T> = () => ValueOrPromise<ReadableStream<T>>;
294
-
295
- export interface ReadableStreamWrapper<T> {
296
- start: WrapReadableStreamStart<T>;
297
- close?(): Promise<void>;
298
- }
299
-
300
- function getWrappedReadableStream<T>(
301
- wrapper: ReadableStream<T> | WrapReadableStreamStart<T> | ReadableStreamWrapper<T>
302
- ) {
303
- if ('start' in wrapper) {
304
- return wrapper.start();
305
- } else if (typeof wrapper === 'function') {
306
- return wrapper();
307
- } else {
308
- // Can't use `wrapper instanceof ReadableStream`
309
- // Because we want to be compatible with any ReadableStream-like objects
310
- return wrapper;
311
- }
312
- }
313
-
314
- export class WrapReadableStream<T> extends ReadableStream<T>{
315
- public readable!: ReadableStream<T>;
316
-
317
- private reader!: ReadableStreamDefaultReader<T>;
318
-
319
- public constructor(wrapper: ReadableStream<T> | WrapReadableStreamStart<T> | ReadableStreamWrapper<T>) {
320
- super({
321
- start: async () => {
322
- // `start` is invoked before `ReadableStream`'s constructor finish,
323
- // so using `this` synchronously causes
324
- // "Must call super constructor in derived class before accessing 'this' or returning from derived constructor".
325
- // Queue a microtask to avoid this.
326
- await Promise.resolve();
327
-
328
- this.readable = await getWrappedReadableStream(wrapper);
329
- this.reader = this.readable.getReader();
330
- },
331
- cancel: async (reason) => {
332
- await this.reader.cancel(reason);
333
- if ('close' in wrapper) {
334
- await wrapper.close?.();
335
- }
336
- },
337
- pull: async (controller) => {
338
- const result = await this.reader.read();
339
- if (result.done) {
340
- controller.close();
341
- if ('close' in wrapper) {
342
- await wrapper.close?.();
343
- }
344
- } else {
345
- controller.enqueue(result.value);
346
- }
347
- }
348
- });
349
- }
350
- }
351
-
352
- export class ChunkStream extends TransformStream<Uint8Array, Uint8Array>{
353
- public constructor(size: number) {
354
- super({
355
- transform(chunk, controller) {
356
- for (let start = 0; start < chunk.byteLength;) {
357
- const end = start + size;
358
- controller.enqueue(chunk.subarray(start, end));
359
- start = end;
360
- }
361
- }
362
- });
363
- }
364
- }
365
-
366
- function* splitLines(text: string): Generator<string, void, void> {
367
- let start = 0;
368
-
369
- while (true) {
370
- const index = text.indexOf('\n', start);
371
- if (index === -1) {
372
- return;
373
- }
374
-
375
- const line = text.substring(start, index);
376
- yield line;
377
-
378
- start = index + 1;
379
- }
380
- }
381
-
382
- export class SplitLineStream extends TransformStream<string, string> {
383
- public constructor() {
384
- super({
385
- transform(chunk, controller) {
386
- for (const line of splitLines(chunk)) {
387
- controller.enqueue(line);
388
- }
389
- }
390
- });
391
- }
392
- }
393
-
394
- /**
395
- * Create a new `WritableStream` that, when written to, will write that chunk to
396
- * `pair.writable`, when pipe `pair.readable` to `writable`.
397
- *
398
- * It's the opposite of `ReadableStream.pipeThrough`.
399
- *
400
- * @param writable The `WritableStream` to write to.
401
- * @param pair A `TransformStream` that converts chunks.
402
- * @returns A new `WritableStream`.
403
- */
404
- export function pipeFrom<W, T>(writable: WritableStream<W>, pair: ReadableWritablePair<W, T>) {
405
- const writer = pair.writable.getWriter();
406
- const pipe = pair.readable
407
- .pipeTo(writable);
408
- return new WritableStream<T>({
409
- async write(chunk) {
410
- await writer.ready;
411
- await writer.write(chunk);
412
- },
413
- async close() {
414
- await writer.close();
415
- await pipe;
416
- }
417
- });
418
- }
419
-
420
- export class InspectStream<T> extends TransformStream<T, T> {
421
- constructor(callback: (value: T) => void) {
422
- super({
423
- transform(chunk, controller) {
424
- callback(chunk);
425
- controller.enqueue(chunk);
426
- }
427
- });
428
- }
429
- }
430
-
431
- export interface PushReadableStreamController<T> {
432
- abortSignal: AbortSignal;
433
-
434
- enqueue(chunk: T): Promise<void>;
435
-
436
- close(): void;
437
-
438
- error(e?: any): void;
439
- }
440
-
441
- export type PushReadableStreamSource<T> = (controller: PushReadableStreamController<T>) => void;
442
-
443
- export class PushReadableStream<T> extends ReadableStream<T> {
444
- public constructor(source: PushReadableStreamSource<T>, strategy?: QueuingStrategy<T>) {
445
- let waterMarkLow: PromiseResolver<void> | undefined;
446
- const canceled: AbortController = new AbortController();
447
-
448
- super({
449
- start: (controller) => {
450
- source({
451
- abortSignal: canceled.signal,
452
- async enqueue(chunk) {
453
- // Only when the stream is errored, `desiredSize` will be `null`.
454
- // But since `null <= 0` is `true`
455
- // (`null <= 0` is evaluated as `!(null > 0)` => `!false` => `true`),
456
- // not handling it will cause a deadlock.
457
- if ((controller.desiredSize ?? 1) <= 0) {
458
- waterMarkLow = new PromiseResolver<void>();
459
- await waterMarkLow.promise;
460
- }
461
-
462
- // `controller.enqueue` will throw error for us
463
- // if the stream is already errored.
464
- controller.enqueue(chunk);
465
- },
466
- close() {
467
- controller.close();
468
- },
469
- error(e) {
470
- controller.error(e);
471
- },
472
- });
473
- },
474
- pull: () => {
475
- waterMarkLow?.resolve();
476
- },
477
- cancel: async (reason) => {
478
- canceled.abort();
479
- waterMarkLow?.reject(reason);
480
- },
481
- }, strategy);
482
- }
483
- }
1
+ import { PromiseResolver } from "@yume-chan/async";
2
+ import type Struct from "@yume-chan/struct";
3
+ import type { StructValueType, ValueOrPromise } from "@yume-chan/struct";
4
+ import { decodeUtf8 } from "../utils/index.js";
5
+ import { BufferedStream, BufferedStreamEndedError } from "./buffered.js";
6
+ import { AbortController, AbortSignal, ReadableStream, ReadableStreamDefaultReader, TransformStream, WritableStream, WritableStreamDefaultWriter, type QueuingStrategy, type ReadableStreamDefaultController, type ReadableWritablePair } from "./detect.js";
7
+
8
+ export interface DuplexStreamFactoryOptions {
9
+ /**
10
+ * Callback when any `ReadableStream` is cancelled (the user doesn't need any more data),
11
+ * or `WritableStream` is ended (the user won't produce any more data),
12
+ * or `DuplexStreamFactory#close` is called.
13
+ *
14
+ * Usually you want to let the other peer know that the duplex stream should be clsoed.
15
+ *
16
+ * `dispose` will automatically be called after `close` completes,
17
+ * but if you want to wait another peer for a close confirmation and call
18
+ * `DuplexStreamFactory#dispose` yourself, you can return `false`
19
+ * (or a `Promise` that resolves to `false`) to disable the automatic call.
20
+ */
21
+ close?: (() => ValueOrPromise<boolean | void>) | undefined;
22
+
23
+ /**
24
+ * Callback when any `ReadableStream` is closed (the other peer doesn't produce any more data),
25
+ * or `WritableStream` is aborted (the other peer can't receive any more data),
26
+ * or `DuplexStreamFactory#abort` is called.
27
+ *
28
+ * Usually indicates the other peer has closed the duplex stream. You can clean up
29
+ * any resources you have allocated now.
30
+ */
31
+ dispose?: (() => void | Promise<void>) | undefined;
32
+ }
33
+
34
+ /**
35
+ * A factory for creating a duplex stream.
36
+ *
37
+ * It can create multiple `ReadableStream`s and `WritableStream`s,
38
+ * when any of them is closed, all other streams will be closed as well.
39
+ */
40
+ export class DuplexStreamFactory<R, W> {
41
+ private readableControllers: ReadableStreamDefaultController<R>[] = [];
42
+ private writers: WritableStreamDefaultWriter<W>[] = [];
43
+
44
+ private _writableClosed = false;
45
+ public get writableClosed() { return this._writableClosed; }
46
+
47
+ private _closed = new PromiseResolver<void>();
48
+ public get closed() { return this._closed.promise; }
49
+
50
+ private options: DuplexStreamFactoryOptions;
51
+
52
+ public constructor(options?: DuplexStreamFactoryOptions) {
53
+ this.options = options ?? {};
54
+ }
55
+
56
+ public wrapReadable(readable: ReadableStream<R>): WrapReadableStream<R> {
57
+ return new WrapReadableStream<R>({
58
+ start: (controller) => {
59
+ this.readableControllers.push(controller);
60
+ return readable;
61
+ },
62
+ cancel: async () => {
63
+ // cancel means the local peer closes the connection first.
64
+ await this.close();
65
+ },
66
+ close: async () => {
67
+ // stream end means the remote peer closed the connection first.
68
+ await this.dispose();
69
+ },
70
+ });
71
+ }
72
+
73
+ public createWritable(stream: WritableStream<W>): WritableStream<W> {
74
+ const writer = stream.getWriter();
75
+ this.writers.push(writer);
76
+
77
+ // `WritableStream` has no way to tell if the remote peer has closed the connection.
78
+ // So it only triggers `close`.
79
+ return new WritableStream<W>({
80
+ write: async (chunk) => {
81
+ await writer.ready;
82
+ await writer.write(chunk);
83
+ },
84
+ abort: async (reason) => {
85
+ await writer.abort(reason);
86
+ await this.close();
87
+ },
88
+ close: async () => {
89
+ try { await writer.close(); } catch { }
90
+ await this.close();
91
+ },
92
+ });
93
+ }
94
+
95
+ public async close() {
96
+ if (this._writableClosed) {
97
+ return;
98
+ }
99
+ this._writableClosed = true;
100
+
101
+ // Call `close` first, so it can still write data to `WritableStream`s.
102
+ if (await this.options.close?.() !== false) {
103
+ // `close` can return `false` to disable automatic `dispose`.
104
+ await this.dispose();
105
+ }
106
+
107
+ for (const writer of this.writers) {
108
+ try { await writer.close(); } catch { }
109
+ }
110
+ }
111
+
112
+ public async dispose() {
113
+ this._writableClosed = true;
114
+ this._closed.resolve();
115
+
116
+ for (const controller of this.readableControllers) {
117
+ try { controller.close(); } catch { }
118
+ }
119
+
120
+ await this.options.dispose?.();
121
+ }
122
+ }
123
+
124
+ export class DecodeUtf8Stream extends TransformStream<Uint8Array, string>{
125
+ public constructor() {
126
+ super({
127
+ transform(chunk, controller) {
128
+ controller.enqueue(decodeUtf8(chunk));
129
+ },
130
+ });
131
+ }
132
+ }
133
+
134
+ export class GatherStringStream extends WritableStream<string>{
135
+ // Optimization: rope (concat strings) is faster than `[].join('')`
136
+ private _result = '';
137
+ public get result() { return this._result; }
138
+
139
+ public constructor() {
140
+ super({
141
+ write: (chunk) => {
142
+ this._result += chunk;
143
+ },
144
+ });
145
+ }
146
+ }
147
+
148
+ // TODO: StructTransformStream: Looking for better implementation
149
+ export class StructDeserializeStream<T extends Struct<any, any, any, any>>
150
+ implements ReadableWritablePair<Uint8Array, StructValueType<T>>{
151
+ private _readable: ReadableStream<StructValueType<T>>;
152
+ public get readable() { return this._readable; }
153
+
154
+ private _writable: WritableStream<Uint8Array>;
155
+ public get writable() { return this._writable; }
156
+
157
+ public constructor(struct: T) {
158
+ // Convert incoming chunks to a `BufferedStream`
159
+ let incomingStreamController!: PushReadableStreamController<Uint8Array>;
160
+ const incomingStream = new BufferedStream(
161
+ new PushReadableStream<Uint8Array>(
162
+ controller => incomingStreamController = controller,
163
+ )
164
+ );
165
+
166
+ this._readable = new ReadableStream<StructValueType<T>>({
167
+ async pull(controller) {
168
+ try {
169
+ const value = await struct.deserialize(incomingStream);
170
+ controller.enqueue(value);
171
+ } catch (e) {
172
+ if (e instanceof BufferedStreamEndedError) {
173
+ controller.close();
174
+ return;
175
+ }
176
+ throw e;
177
+ }
178
+ }
179
+ });
180
+
181
+ this._writable = new WritableStream({
182
+ async write(chunk) {
183
+ await incomingStreamController.enqueue(chunk);
184
+ },
185
+ abort() {
186
+ incomingStreamController.close();
187
+ },
188
+ close() {
189
+ incomingStreamController.close();
190
+ },
191
+ });
192
+ }
193
+ }
194
+
195
+ export class StructSerializeStream<T extends Struct<any, any, any, any>>
196
+ extends TransformStream<T['TInit'], Uint8Array>{
197
+ constructor(struct: T) {
198
+ super({
199
+ transform(chunk, controller) {
200
+ controller.enqueue(struct.serialize(chunk));
201
+ },
202
+ });
203
+ }
204
+ }
205
+
206
+ export type WrapWritableStreamStart<T> = () => ValueOrPromise<WritableStream<T>>;
207
+
208
+ export interface WritableStreamWrapper<T> {
209
+ start: WrapWritableStreamStart<T>;
210
+ close?(): Promise<void>;
211
+ }
212
+
213
+ async function getWrappedWritableStream<T>(
214
+ wrapper: WritableStream<T> | WrapWritableStreamStart<T> | WritableStreamWrapper<T>
215
+ ) {
216
+ if ('start' in wrapper) {
217
+ return await wrapper.start();
218
+ } else if (typeof wrapper === 'function') {
219
+ return await wrapper();
220
+ } else {
221
+ // Can't use `wrapper instanceof WritableStream`
222
+ // Because we want to be compatible with any WritableStream-like objects
223
+ return wrapper;
224
+ }
225
+ }
226
+
227
+ export class WrapWritableStream<T> extends WritableStream<T> {
228
+ public writable!: WritableStream<T>;
229
+
230
+ private writer!: WritableStreamDefaultWriter<T>;
231
+
232
+ public constructor(wrapper: WritableStream<T> | WrapWritableStreamStart<T> | WritableStreamWrapper<T>) {
233
+ super({
234
+ start: async () => {
235
+ // `start` is invoked before `ReadableStream`'s constructor finish,
236
+ // so using `this` synchronously causes
237
+ // "Must call super constructor in derived class before accessing 'this' or returning from derived constructor".
238
+ // Queue a microtask to avoid this.
239
+ await Promise.resolve();
240
+
241
+ this.writable = await getWrappedWritableStream(wrapper);
242
+ this.writer = this.writable.getWriter();
243
+ },
244
+ write: async (chunk) => {
245
+ // Maintain back pressure
246
+ await this.writer.ready;
247
+ await this.writer.write(chunk);
248
+ },
249
+ abort: async (reason) => {
250
+ await this.writer.abort(reason);
251
+ if ('close' in wrapper) {
252
+ await wrapper.close?.();
253
+ }
254
+ },
255
+ close: async () => {
256
+ // Close the inner stream first.
257
+ // Usually the inner stream is a logical sub-stream over the outer stream,
258
+ // closing the outer stream first will make the inner stream incapable of
259
+ // sending data in its `close` handler.
260
+ await this.writer.close();
261
+ if ('close' in wrapper) {
262
+ await wrapper.close?.();
263
+ }
264
+ },
265
+ });
266
+ }
267
+ }
268
+
269
+ export type WrapReadableStreamStart<T> = (controller: ReadableStreamDefaultController<T>) => ValueOrPromise<ReadableStream<T>>;
270
+
271
+ export interface ReadableStreamWrapper<T> {
272
+ start: WrapReadableStreamStart<T>;
273
+ cancel?(reason?: any): ValueOrPromise<void>;
274
+ close?(): ValueOrPromise<void>;
275
+ }
276
+
277
+ function getWrappedReadableStream<T>(
278
+ wrapper: ReadableStream<T> | WrapReadableStreamStart<T> | ReadableStreamWrapper<T>,
279
+ controller: ReadableStreamDefaultController<T>
280
+ ) {
281
+ if ('start' in wrapper) {
282
+ return wrapper.start(controller);
283
+ } else if (typeof wrapper === 'function') {
284
+ return wrapper(controller);
285
+ } else {
286
+ // Can't use `wrapper instanceof ReadableStream`
287
+ // Because we want to be compatible with any ReadableStream-like objects
288
+ return wrapper;
289
+ }
290
+ }
291
+
292
+ /**
293
+ * This class has multiple usages:
294
+ *
295
+ * 1. Get notified when the stream is cancelled or closed.
296
+ * 2. Synchronously create a `ReadableStream` by asynchronously return another `ReadableStream`.
297
+ * 3. Convert native `ReadableStream`s to polyfilled ones so they can `pipe` between.
298
+ */
299
+ export class WrapReadableStream<T> extends ReadableStream<T>{
300
+ public readable!: ReadableStream<T>;
301
+
302
+ private reader!: ReadableStreamDefaultReader<T>;
303
+
304
+ public constructor(wrapper: ReadableStream<T> | WrapReadableStreamStart<T> | ReadableStreamWrapper<T>) {
305
+ super({
306
+ start: async (controller) => {
307
+ // `start` is invoked before `ReadableStream`'s constructor finish,
308
+ // so using `this` synchronously causes
309
+ // "Must call super constructor in derived class before accessing 'this' or returning from derived constructor".
310
+ // Queue a microtask to avoid this.
311
+ await Promise.resolve();
312
+
313
+ this.readable = await getWrappedReadableStream(wrapper, controller);
314
+ this.reader = this.readable.getReader();
315
+ },
316
+ cancel: async (reason) => {
317
+ await this.reader.cancel(reason);
318
+ if ('cancel' in wrapper) {
319
+ await wrapper.cancel?.(reason);
320
+ }
321
+ },
322
+ pull: async (controller) => {
323
+ const result = await this.reader.read();
324
+ if (result.done) {
325
+ controller.close();
326
+ if ('close' in wrapper) {
327
+ await wrapper.close?.();
328
+ }
329
+ } else {
330
+ controller.enqueue(result.value);
331
+ }
332
+ }
333
+ });
334
+ }
335
+ }
336
+
337
+ export class ChunkStream extends TransformStream<Uint8Array, Uint8Array>{
338
+ public constructor(size: number) {
339
+ super({
340
+ transform(chunk, controller) {
341
+ for (let start = 0; start < chunk.byteLength;) {
342
+ const end = start + size;
343
+ controller.enqueue(chunk.subarray(start, end));
344
+ start = end;
345
+ }
346
+ }
347
+ });
348
+ }
349
+ }
350
+
351
+ function* splitLines(text: string): Generator<string, void, void> {
352
+ let start = 0;
353
+
354
+ while (true) {
355
+ const index = text.indexOf('\n', start);
356
+ if (index === -1) {
357
+ return;
358
+ }
359
+
360
+ const line = text.substring(start, index);
361
+ yield line;
362
+
363
+ start = index + 1;
364
+ }
365
+ }
366
+
367
+ export class SplitLineStream extends TransformStream<string, string> {
368
+ public constructor() {
369
+ super({
370
+ transform(chunk, controller) {
371
+ for (const line of splitLines(chunk)) {
372
+ controller.enqueue(line);
373
+ }
374
+ }
375
+ });
376
+ }
377
+ }
378
+
379
+ /**
380
+ * Create a new `WritableStream` that, when written to, will write that chunk to
381
+ * `pair.writable`, when pipe `pair.readable` to `writable`.
382
+ *
383
+ * It's the opposite of `ReadableStream.pipeThrough`.
384
+ *
385
+ * @param writable The `WritableStream` to write to.
386
+ * @param pair A `TransformStream` that converts chunks.
387
+ * @returns A new `WritableStream`.
388
+ */
389
+ export function pipeFrom<W, T>(writable: WritableStream<W>, pair: ReadableWritablePair<W, T>) {
390
+ const writer = pair.writable.getWriter();
391
+ const pipe = pair.readable
392
+ .pipeTo(writable);
393
+ return new WritableStream<T>({
394
+ async write(chunk) {
395
+ await writer.ready;
396
+ await writer.write(chunk);
397
+ },
398
+ async close() {
399
+ await writer.close();
400
+ await pipe;
401
+ }
402
+ });
403
+ }
404
+
405
+ export class InspectStream<T> extends TransformStream<T, T> {
406
+ constructor(callback: (value: T) => void) {
407
+ super({
408
+ transform(chunk, controller) {
409
+ callback(chunk);
410
+ controller.enqueue(chunk);
411
+ }
412
+ });
413
+ }
414
+ }
415
+
416
+ export interface PushReadableStreamController<T> {
417
+ abortSignal: AbortSignal;
418
+
419
+ enqueue(chunk: T): Promise<void>;
420
+
421
+ close(): void;
422
+
423
+ error(e?: any): void;
424
+ }
425
+
426
+ export type PushReadableStreamSource<T> = (controller: PushReadableStreamController<T>) => void;
427
+
428
+ export class PushReadableStream<T> extends ReadableStream<T> {
429
+ public constructor(source: PushReadableStreamSource<T>, strategy?: QueuingStrategy<T>) {
430
+ let waterMarkLow: PromiseResolver<void> | undefined;
431
+ const canceled: AbortController = new AbortController();
432
+
433
+ super({
434
+ start: (controller) => {
435
+ source({
436
+ abortSignal: canceled.signal,
437
+ async enqueue(chunk) {
438
+ if (canceled.signal.aborted) {
439
+ // If the stream is already cancelled,
440
+ // throw immediately.
441
+ throw canceled.signal.reason ?? new Error('Aborted');
442
+ }
443
+
444
+ // Only when the stream is errored, `desiredSize` will be `null`.
445
+ // But since `null <= 0` is `true`
446
+ // (`null <= 0` is evaluated as `!(null > 0)` => `!false` => `true`),
447
+ // not handling it will cause a deadlock.
448
+ if ((controller.desiredSize ?? 1) <= 0) {
449
+ waterMarkLow = new PromiseResolver<void>();
450
+ await waterMarkLow.promise;
451
+ }
452
+
453
+ // `controller.enqueue` will throw error for us
454
+ // if the stream is already errored.
455
+ controller.enqueue(chunk);
456
+ },
457
+ close() {
458
+ controller.close();
459
+ },
460
+ error(e) {
461
+ controller.error(e);
462
+ },
463
+ });
464
+ },
465
+ pull: () => {
466
+ waterMarkLow?.resolve();
467
+ },
468
+ cancel: async (reason) => {
469
+ canceled.abort(reason);
470
+ waterMarkLow?.reject(reason);
471
+ },
472
+ }, strategy);
473
+ }
474
+ }