@yume-chan/adb 0.0.16 → 0.0.18

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 (177) hide show
  1. package/CHANGELOG.json +18 -0
  2. package/CHANGELOG.md +13 -1
  3. package/LICENSE +1 -1
  4. package/esm/adb.d.ts +13 -8
  5. package/esm/adb.d.ts.map +1 -1
  6. package/esm/adb.js +63 -38
  7. package/esm/adb.js.map +1 -1
  8. package/esm/auth.d.ts +4 -4
  9. package/esm/auth.d.ts.map +1 -1
  10. package/esm/auth.js +6 -6
  11. package/esm/auth.js.map +1 -1
  12. package/esm/backend.d.ts +3 -3
  13. package/esm/backend.d.ts.map +1 -1
  14. package/esm/commands/base.d.ts +2 -2
  15. package/esm/commands/base.d.ts.map +1 -1
  16. package/esm/commands/base.js +1 -1
  17. package/esm/commands/base.js.map +1 -1
  18. package/esm/commands/framebuffer.d.ts +22 -6
  19. package/esm/commands/framebuffer.d.ts.map +1 -1
  20. package/esm/commands/framebuffer.js +33 -45
  21. package/esm/commands/framebuffer.js.map +1 -1
  22. package/esm/commands/install.d.ts +2 -2
  23. package/esm/commands/install.d.ts.map +1 -1
  24. package/esm/commands/install.js +12 -5
  25. package/esm/commands/install.js.map +1 -1
  26. package/esm/commands/power.d.ts.map +1 -1
  27. package/esm/commands/power.js +13 -9
  28. package/esm/commands/power.js.map +1 -1
  29. package/esm/commands/reverse.d.ts +5 -5
  30. package/esm/commands/reverse.d.ts.map +1 -1
  31. package/esm/commands/reverse.js +20 -18
  32. package/esm/commands/reverse.js.map +1 -1
  33. package/esm/commands/subprocess/command.d.ts +52 -0
  34. package/esm/commands/subprocess/command.d.ts.map +1 -0
  35. package/esm/commands/subprocess/command.js +81 -0
  36. package/esm/commands/subprocess/command.js.map +1 -0
  37. package/esm/commands/subprocess/index.d.ts +1 -53
  38. package/esm/commands/subprocess/index.d.ts.map +1 -1
  39. package/esm/commands/subprocess/index.js +1 -85
  40. package/esm/commands/subprocess/index.js.map +1 -1
  41. package/esm/commands/subprocess/protocols/none.d.ts +5 -5
  42. package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
  43. package/esm/commands/subprocess/protocols/none.js +5 -3
  44. package/esm/commands/subprocess/protocols/none.js.map +1 -1
  45. package/esm/commands/subprocess/protocols/shell.d.ts +4 -4
  46. package/esm/commands/subprocess/protocols/shell.d.ts.map +1 -1
  47. package/esm/commands/subprocess/protocols/shell.js +28 -20
  48. package/esm/commands/subprocess/protocols/shell.js.map +1 -1
  49. package/esm/commands/subprocess/protocols/types.d.ts +7 -7
  50. package/esm/commands/subprocess/protocols/types.d.ts.map +1 -1
  51. package/esm/commands/sync/index.d.ts +1 -1
  52. package/esm/commands/sync/index.d.ts.map +1 -1
  53. package/esm/commands/sync/index.js +1 -1
  54. package/esm/commands/sync/index.js.map +1 -1
  55. package/esm/commands/sync/list.d.ts +7 -7
  56. package/esm/commands/sync/list.d.ts.map +1 -1
  57. package/esm/commands/sync/list.js +34 -50
  58. package/esm/commands/sync/list.js.map +1 -1
  59. package/esm/commands/sync/pull.d.ts +5 -4
  60. package/esm/commands/sync/pull.d.ts.map +1 -1
  61. package/esm/commands/sync/pull.js +21 -22
  62. package/esm/commands/sync/pull.js.map +1 -1
  63. package/esm/commands/sync/push.d.ts +4 -4
  64. package/esm/commands/sync/push.d.ts.map +1 -1
  65. package/esm/commands/sync/push.js +7 -11
  66. package/esm/commands/sync/push.js.map +1 -1
  67. package/esm/commands/sync/request.d.ts +4 -4
  68. package/esm/commands/sync/request.d.ts.map +1 -1
  69. package/esm/commands/sync/request.js +6 -6
  70. package/esm/commands/sync/request.js.map +1 -1
  71. package/esm/commands/sync/response.d.ts +5 -10
  72. package/esm/commands/sync/response.d.ts.map +1 -1
  73. package/esm/commands/sync/response.js +35 -26
  74. package/esm/commands/sync/response.js.map +1 -1
  75. package/esm/commands/sync/stat.d.ts +7 -7
  76. package/esm/commands/sync/stat.d.ts.map +1 -1
  77. package/esm/commands/sync/stat.js +48 -55
  78. package/esm/commands/sync/stat.js.map +1 -1
  79. package/esm/commands/sync/sync.d.ts +7 -7
  80. package/esm/commands/sync/sync.d.ts.map +1 -1
  81. package/esm/commands/sync/sync.js +25 -22
  82. package/esm/commands/sync/sync.js.map +1 -1
  83. package/esm/crypto.d.ts.map +1 -1
  84. package/esm/crypto.js +26 -16
  85. package/esm/crypto.js.map +1 -1
  86. package/esm/index.d.ts +9 -10
  87. package/esm/index.d.ts.map +1 -1
  88. package/esm/index.js +9 -10
  89. package/esm/index.js.map +1 -1
  90. package/esm/packet.d.ts +8 -8
  91. package/esm/packet.d.ts.map +1 -1
  92. package/esm/packet.js +14 -12
  93. package/esm/packet.js.map +1 -1
  94. package/esm/socket/dispatcher.d.ts +11 -6
  95. package/esm/socket/dispatcher.d.ts.map +1 -1
  96. package/esm/socket/dispatcher.js +28 -20
  97. package/esm/socket/dispatcher.js.map +1 -1
  98. package/esm/socket/index.d.ts +1 -1
  99. package/esm/socket/index.d.ts.map +1 -1
  100. package/esm/socket/index.js +1 -1
  101. package/esm/socket/index.js.map +1 -1
  102. package/esm/socket/socket.d.ts +5 -5
  103. package/esm/socket/socket.d.ts.map +1 -1
  104. package/esm/socket/socket.js +37 -17
  105. package/esm/socket/socket.js.map +1 -1
  106. package/esm/utils/auto-reset-event.d.ts +1 -1
  107. package/esm/utils/auto-reset-event.d.ts.map +1 -1
  108. package/esm/utils/auto-reset-event.js +2 -2
  109. package/esm/utils/base64.d.ts +23 -0
  110. package/esm/utils/base64.d.ts.map +1 -1
  111. package/esm/utils/base64.js +36 -29
  112. package/esm/utils/base64.js.map +1 -1
  113. package/package.json +19 -17
  114. package/src/adb.ts +140 -86
  115. package/src/auth.ts +27 -18
  116. package/src/backend.ts +7 -4
  117. package/src/commands/base.ts +3 -2
  118. package/src/commands/framebuffer.ts +48 -47
  119. package/src/commands/install.ts +20 -10
  120. package/src/commands/power.ts +14 -10
  121. package/src/commands/reverse.ts +53 -33
  122. package/src/commands/subprocess/command.ts +134 -0
  123. package/src/commands/subprocess/index.ts +1 -137
  124. package/src/commands/subprocess/protocols/none.ts +10 -6
  125. package/src/commands/subprocess/protocols/shell.ts +72 -39
  126. package/src/commands/subprocess/protocols/types.ts +12 -8
  127. package/src/commands/sync/index.ts +1 -1
  128. package/src/commands/sync/list.ts +63 -68
  129. package/src/commands/sync/pull.ts +53 -37
  130. package/src/commands/sync/push.ts +26 -15
  131. package/src/commands/sync/request.ts +20 -21
  132. package/src/commands/sync/response.ts +63 -53
  133. package/src/commands/sync/stat.ts +91 -90
  134. package/src/commands/sync/sync.ts +56 -34
  135. package/src/crypto.ts +61 -33
  136. package/src/features.ts +1 -1
  137. package/src/index.ts +9 -11
  138. package/src/packet.ts +40 -30
  139. package/src/socket/dispatcher.ts +119 -77
  140. package/src/socket/index.ts +1 -1
  141. package/src/socket/socket.ts +68 -26
  142. package/src/utils/auto-reset-event.ts +3 -3
  143. package/src/utils/base64.ts +75 -42
  144. package/tsconfig.build.json +1 -1
  145. package/tsconfig.build.tsbuildinfo +1 -1
  146. package/esm/stream/buffered.d.ts +0 -39
  147. package/esm/stream/buffered.d.ts.map +0 -1
  148. package/esm/stream/buffered.js +0 -143
  149. package/esm/stream/buffered.js.map +0 -1
  150. package/esm/stream/detect.d.ts +0 -2
  151. package/esm/stream/detect.d.ts.map +0 -1
  152. package/esm/stream/detect.js +0 -6
  153. package/esm/stream/detect.js.map +0 -1
  154. package/esm/stream/detect.native.d.ts +0 -240
  155. package/esm/stream/detect.native.d.ts.map +0 -1
  156. package/esm/stream/detect.native.js +0 -60
  157. package/esm/stream/detect.native.js.map +0 -1
  158. package/esm/stream/detect.polyfill.d.ts +0 -18
  159. package/esm/stream/detect.polyfill.d.ts.map +0 -1
  160. package/esm/stream/detect.polyfill.js +0 -4
  161. package/esm/stream/detect.polyfill.js.map +0 -1
  162. package/esm/stream/index.d.ts +0 -4
  163. package/esm/stream/index.d.ts.map +0 -1
  164. package/esm/stream/index.js +0 -4
  165. package/esm/stream/index.js.map +0 -1
  166. package/esm/stream/transform.d.ts +0 -124
  167. package/esm/stream/transform.d.ts.map +0 -1
  168. package/esm/stream/transform.js +0 -379
  169. package/esm/stream/transform.js.map +0 -1
  170. package/src/stream/buffered.ts +0 -168
  171. package/src/stream/detect.native.ts +0 -362
  172. package/src/stream/detect.polyfill.ts +0 -22
  173. package/src/stream/detect.ts +0 -7
  174. package/src/stream/index.ts +0 -3
  175. package/src/stream/transform.ts +0 -474
  176. package/tsconfig.test.json +0 -9
  177. package/web-streams-polyfill-4.0.0-beta.2.tgz +0 -0
@@ -1,474 +0,0 @@
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
- }
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "./tsconfig.build.json",
3
- "compilerOptions": {
4
- "types": [
5
- "node"
6
- ],
7
- },
8
- "exclude": []
9
- }