-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpull.test.ts
More file actions
992 lines (869 loc) · 32.2 KB
/
Copy pathpull.test.ts
File metadata and controls
992 lines (869 loc) · 32.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
/**
* Tests for Pull Pipeline - pull(), pullSync(), pipeTo(), pipeToSync()
*
* Requirements covered: See https://github.com/jasnell/new-streams/blob/main/docs/REQUIREMENTS.md Sections 3-4 (PULL-xxx, PIPE-xxx)
*/
import { describe, it } from 'node:test';
import * as assert from 'node:assert';
import { pull, pullSync, pipeTo, pipeToSync } from './pull.js';
import { fromSync, from } from './from.js';
import type { Writer, SyncWriter, Transform, SyncTransform } from './types.js';
import { concatBytes } from './utils.js';
// Helper to collect all bytes from an async iterable
async function collectBytes(source: AsyncIterable<Uint8Array[]>): Promise<Uint8Array> {
const chunks: Uint8Array[] = [];
for await (const batch of source) {
chunks.push(...batch);
}
return concatBytes(chunks);
}
// Helper to collect all bytes from a sync iterable
function collectBytesSync(source: Iterable<Uint8Array[]>): Uint8Array {
const chunks: Uint8Array[] = [];
for (const batch of source) {
chunks.push(...batch);
}
return concatBytes(chunks);
}
// Helper to decode Uint8Array to string
function decode(bytes: Uint8Array): string {
return new TextDecoder().decode(bytes);
}
// Create a mock sync writer for testing
function createMockSyncWriter(): SyncWriter & { chunks: Uint8Array[]; closed: boolean; failed: Error | undefined } {
const writer = {
chunks: [] as Uint8Array[],
closed: false,
failed: undefined as Error | undefined,
get desiredSize() {
return this.closed ? null : 10;
},
write(chunk: Uint8Array | string) {
if (this.closed) throw new Error('Writer is closed');
const data = typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk;
this.chunks.push(data);
},
writev(chunks: (Uint8Array | string)[]) {
for (const chunk of chunks) {
this.write(chunk);
}
},
end() {
this.closed = true;
return this.chunks.reduce((acc, c) => acc + c.byteLength, 0);
},
fail(reason?: Error) {
this.failed = reason;
this.closed = true;
},
};
return writer;
}
// Create a mock async writer for testing
function createMockWriter(): Writer & { chunks: Uint8Array[]; closed: boolean; failed: Error | undefined } {
const writer = {
chunks: [] as Uint8Array[],
closed: false,
failed: undefined as Error | undefined,
get desiredSize() {
return this.closed ? null : 10;
},
async write(chunk: Uint8Array | string) {
if (this.closed) throw new Error('Writer is closed');
const data = typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk;
this.chunks.push(data);
},
async writev(chunks: (Uint8Array | string)[]) {
for (const chunk of chunks) {
await this.write(chunk);
}
},
writeSync(chunk: Uint8Array | string) {
if (this.closed) return false;
const data = typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk;
this.chunks.push(data);
return true;
},
writevSync(chunks: (Uint8Array | string)[]) {
for (const chunk of chunks) {
if (!this.writeSync(chunk)) return false;
}
return true;
},
async end() {
this.closed = true;
return this.chunks.reduce((acc, c) => acc + c.byteLength, 0);
},
endSync() {
this.closed = true;
return this.chunks.reduce((acc, c) => acc + c.byteLength, 0);
},
async fail(reason?: Error) {
this.failed = reason;
this.closed = true;
},
failSync(reason?: Error) {
this.failed = reason;
this.closed = true;
return true;
},
};
return writer;
}
describe('pullSync()', () => {
describe('basic usage', () => {
it('should pass through source without transforms [PULL-003]', () => {
const source = fromSync('Hello, World!');
const output = pullSync(source);
const result = collectBytesSync(output);
assert.strictEqual(decode(result), 'Hello, World!');
});
it('should apply a single transform [PULL-006]', () => {
const source = fromSync('hello');
const toUpper: SyncTransform = (chunks) => {
if (chunks === null) return null;
return chunks.map((c) => new TextEncoder().encode(decode(c).toUpperCase()));
};
const output = pullSync(source, toUpper);
const result = collectBytesSync(output);
assert.strictEqual(decode(result), 'HELLO');
});
it('should chain multiple transforms [PULL-007]', () => {
const source = fromSync('hello');
const toUpper: SyncTransform = (chunks) => {
if (chunks === null) return null;
return chunks.map((c) => new TextEncoder().encode(decode(c).toUpperCase()));
};
const addExclaim: SyncTransform = (chunks) => {
if (chunks === null) return null;
return chunks.map((c) => new TextEncoder().encode(decode(c) + '!'));
};
const output = pullSync(source, toUpper, addExclaim);
const result = collectBytesSync(output);
assert.strictEqual(decode(result), 'HELLO!');
});
});
describe('transform output types', () => {
it('should handle transform returning Uint8Array[] [PULL-010]', () => {
const source = fromSync('test');
const transform: SyncTransform = (chunks) => {
if (chunks === null) return null;
return [new Uint8Array([65, 66, 67])]; // ABC
};
const output = pullSync(source, transform);
const result = collectBytesSync(output);
assert.strictEqual(decode(result), 'ABC');
});
it('should handle transform returning string (via generator) [PULL-011]', () => {
const source = fromSync('test');
const transform: SyncTransform = function* (chunks) {
if (chunks === null) return;
yield 'transformed';
};
const output = pullSync(source, transform);
const result = collectBytesSync(output);
assert.strictEqual(decode(result), 'transformed');
});
it('should handle transform returning nested iterables [PULL-012]', () => {
const source = fromSync('test');
const transform: SyncTransform = function* (chunks) {
if (chunks === null) return;
yield 'a';
yield (function* () {
yield 'b';
yield 'c';
})();
yield 'd';
};
const output = pullSync(source, transform);
const result = collectBytesSync(output);
assert.strictEqual(decode(result), 'abcd');
});
});
describe('stateful transforms', () => {
it('should support stateful transform object [PULL-030]', () => {
const source = fromSync(['chunk1', 'chunk2']);
// Object = stateful transform (receives entire source as iterable)
const transform: SyncTransform = {
transform: function* (sourceIter) {
let count = 0;
for (const chunks of sourceIter) {
if (chunks === null) {
yield `total:${count}`;
} else {
count += chunks.length;
for (const chunk of chunks) {
yield chunk;
}
}
}
},
};
const output = pullSync(source, transform);
const result = collectBytesSync(output);
// Note: Each array element becomes a separate batch, so count = 2
assert.ok(decode(result).includes('total:'));
});
it('should yield separate batches per transform yield, not buffer all output', () => {
const source = fromSync(['a', 'b', 'c']);
const transform: SyncTransform = {
transform: function* (sourceIter) {
for (const chunks of sourceIter) {
if (chunks === null) {
yield 'done';
} else {
for (const chunk of chunks) {
yield chunk;
}
}
}
},
};
const output = pullSync(source, transform);
// Count the number of batches yielded
const batches: Uint8Array[][] = [];
for (const batch of output) {
batches.push(batch);
}
// The transform yields 4 items (a, b, c, done) so we should get 4 batches,
// not 1 giant batch containing everything
assert.strictEqual(batches.length, 4);
assert.strictEqual(decode(batches[0][0]), 'a');
assert.strictEqual(decode(batches[1][0]), 'b');
assert.strictEqual(decode(batches[2][0]), 'c');
assert.strictEqual(decode(batches[3][0]), 'done');
});
});
describe('flush signal', () => {
it('should receive null flush signal at end [PULL-031]', () => {
const source = fromSync('test');
let flushed = false;
const transform: SyncTransform = (chunks) => {
if (chunks === null) {
flushed = true;
return null;
}
return chunks;
};
const output = pullSync(source, transform);
collectBytesSync(output);
assert.strictEqual(flushed, true);
});
});
});
describe('pull()', () => {
describe('basic usage', () => {
it('should pass through source without transforms [PULL-001]', async () => {
const source = from('Hello, World!');
const output = pull(source);
const result = await collectBytes(output);
assert.strictEqual(decode(result), 'Hello, World!');
});
it('should work with sync source [PULL-002]', async () => {
const source = fromSync('Sync Source');
const output = pull(source);
const result = await collectBytes(output);
assert.strictEqual(decode(result), 'Sync Source');
});
it('should apply a single transform [PULL-004]', async () => {
const source = from('hello');
const toUpper: Transform = (chunks) => {
if (chunks === null) return null;
return chunks.map((c) => new TextEncoder().encode(decode(c).toUpperCase()));
};
const output = pull(source, toUpper);
const result = await collectBytes(output);
assert.strictEqual(decode(result), 'HELLO');
});
it('should chain multiple transforms [PULL-005]', async () => {
const source = from('hello');
const toUpper: Transform = (chunks) => {
if (chunks === null) return null;
return chunks.map((c) => new TextEncoder().encode(decode(c).toUpperCase()));
};
const addExclaim: Transform = (chunks) => {
if (chunks === null) return null;
return chunks.map((c) => new TextEncoder().encode(decode(c) + '!'));
};
const output = pull(source, toUpper, addExclaim);
const result = await collectBytes(output);
assert.strictEqual(decode(result), 'HELLO!');
});
});
describe('async transforms', () => {
it('should handle async transform function [PULL-020]', async () => {
const source = from('test');
const asyncTransform: Transform = async (chunks) => {
await new Promise((resolve) => setTimeout(resolve, 10));
if (chunks === null) return null;
return chunks;
};
const output = pull(source, asyncTransform);
const result = await collectBytes(output);
assert.strictEqual(decode(result), 'test');
});
it('should handle transform returning promise [PULL-021]', async () => {
const source = from('test');
const transform: Transform = (chunks) => {
return Promise.resolve(chunks);
};
const output = pull(source, transform);
const result = await collectBytes(output);
assert.strictEqual(decode(result), 'test');
});
it('should handle transform returning async generator [PULL-022]', async () => {
const source = from('test');
const transform: Transform = async function* (chunks) {
if (chunks === null) return;
yield 'async';
await new Promise((resolve) => setTimeout(resolve, 5));
yield 'gen';
};
const output = pull(source, transform);
const result = await collectBytes(output);
assert.strictEqual(decode(result), 'asyncgen');
});
});
describe('options', () => {
it('should accept options as last argument [PULL-040]', async () => {
const source = from('test');
const transform: Transform = (chunks) => chunks;
const output = pull(source, transform, { signal: undefined });
const result = await collectBytes(output);
assert.strictEqual(decode(result), 'test');
});
it('should respect AbortSignal [PULL-041]', async () => {
const controller = new AbortController();
const source = from(
(async function* () {
yield 'chunk1';
await new Promise((resolve) => setTimeout(resolve, 100));
yield 'chunk2';
})()
);
const output = pull(source, { signal: controller.signal });
// Abort after first chunk
setTimeout(() => controller.abort(), 20);
await assert.rejects(async () => {
await collectBytes(output);
}, /Abort/);
});
it('should handle already-aborted signal [PULL-042]', async () => {
const controller = new AbortController();
controller.abort();
const source = from('test');
const output = pull(source, { signal: controller.signal });
await assert.rejects(async () => {
await collectBytes(output);
}, /Abort/);
});
});
describe('error handling', () => {
it('should fire signal on transforms when error occurs [PULL-032]', async () => {
let signalAborted = false;
const source = from('test');
// Object = stateful transform (receives entire source as async iterable)
const transform1: Transform = {
async *transform(source, { signal }) {
signal.addEventListener('abort', () => {
signalAborted = true;
}, { once: true });
for await (const chunks of source) {
if (chunks) yield chunks;
}
},
};
const transform2: Transform = () => {
throw new Error('Transform error');
};
const output = pull(source, transform1, transform2);
await assert.rejects(async () => {
await collectBytes(output);
}, /Transform error/);
assert.strictEqual(signalAborted, true);
});
});
describe('transform signal', () => {
it('should pass signal to stateless transforms', async () => {
let receivedSignal: AbortSignal | undefined;
const source = from('hello');
const transform: Transform = (chunks, { signal }) => {
receivedSignal = signal;
return chunks;
};
const output = pull(source, transform);
await collectBytes(output);
assert.ok(receivedSignal, 'Transform should receive a signal');
assert.ok(receivedSignal instanceof AbortSignal, 'Should be an AbortSignal');
});
it('should pass signal to stateful transforms', async () => {
let receivedSignal: AbortSignal | undefined;
const source = from('hello');
const transform: Transform = {
async *transform(source, { signal }) {
receivedSignal = signal;
for await (const chunks of source) {
if (chunks) yield chunks;
}
},
};
const output = pull(source, transform);
await collectBytes(output);
assert.ok(receivedSignal, 'Transform should receive a signal');
assert.ok(receivedSignal instanceof AbortSignal, 'Should be an AbortSignal');
});
it('should fire signal when user signal aborts', async () => {
let transformSignalAborted = false;
const controller = new AbortController();
const source = from((async function* () {
yield [new TextEncoder().encode('chunk1')];
await new Promise(resolve => setTimeout(resolve, 50));
yield [new TextEncoder().encode('chunk2')];
})());
const transform: Transform = (chunks, { signal }) => {
signal.addEventListener('abort', () => {
transformSignalAborted = true;
}, { once: true });
return chunks;
};
const output = pull(source, transform, { signal: controller.signal });
// Abort after first chunk
setTimeout(() => controller.abort(), 20);
await assert.rejects(async () => {
await collectBytes(output);
}, /Abort/);
assert.strictEqual(transformSignalAborted, true);
});
it('should fire signal when consumer breaks', async () => {
let transformSignalAborted = false;
const source = from((async function* () {
yield [new TextEncoder().encode('chunk1')];
yield [new TextEncoder().encode('chunk2')];
yield [new TextEncoder().encode('chunk3')];
})());
const transform: Transform = (chunks, { signal }) => {
if (!transformSignalAborted) {
signal.addEventListener('abort', () => {
transformSignalAborted = true;
}, { once: true });
}
return chunks;
};
const output = pull(source, transform);
// Only consume one batch, then break
for await (const _batch of output) {
break;
}
assert.strictEqual(transformSignalAborted, true);
});
it('should fire signal when source throws', async () => {
let transformSignalAborted = false;
const source = from((async function* () {
yield [new TextEncoder().encode('chunk1')];
throw new Error('Source error');
})());
const transform: Transform = (chunks, { signal }) => {
if (!transformSignalAborted) {
signal.addEventListener('abort', () => {
transformSignalAborted = true;
}, { once: true });
}
return chunks;
};
const output = pull(source, transform);
await assert.rejects(async () => {
await collectBytes(output);
}, /Source error/);
assert.strictEqual(transformSignalAborted, true);
});
it('should fire signal when a transform throws', async () => {
let transform1SignalAborted = false;
const source = from('test');
const transform1: Transform = {
async *transform(source, { signal }) {
signal.addEventListener('abort', () => {
transform1SignalAborted = true;
}, { once: true });
for await (const chunks of source) {
if (chunks) yield chunks;
}
},
};
const transform2: Transform = () => {
throw new Error('Transform 2 error');
};
const output = pull(source, transform1, transform2);
await assert.rejects(async () => {
await collectBytes(output);
}, /Transform 2 error/);
assert.strictEqual(transform1SignalAborted, true);
});
it('should NOT fire signal on normal completion', async () => {
let signalAborted = false;
const source = from('hello');
const transform: Transform = (chunks, { signal }) => {
signal.addEventListener('abort', () => {
signalAborted = true;
}, { once: true });
return chunks;
};
const output = pull(source, transform);
await collectBytes(output);
assert.strictEqual(signalAborted, false);
});
it('should allow transform to pass signal to sub-operation', async () => {
let subOperationAborted = false;
const controller = new AbortController();
const source = from((async function* () {
yield [new TextEncoder().encode('chunk1')];
await new Promise(resolve => setTimeout(resolve, 50));
yield [new TextEncoder().encode('chunk2')];
})());
const transform: Transform = async (chunks, { signal }) => {
if (chunks === null) return null;
// Simulate passing signal to a sub-operation
await new Promise<void>((resolve, reject) => {
if (signal.aborted) {
subOperationAborted = true;
reject(signal.reason);
return;
}
signal.addEventListener('abort', () => {
subOperationAborted = true;
reject(signal.reason);
}, { once: true });
// Resolve after a short delay (simulating work)
setTimeout(resolve, 5);
});
return chunks;
};
const output = pull(source, transform, { signal: controller.signal });
setTimeout(() => controller.abort(), 20);
await assert.rejects(async () => {
await collectBytes(output);
}, /Abort/);
assert.strictEqual(subOperationAborted, true);
});
it('should throw immediately with pre-aborted signal without calling transforms', async () => {
let transformCalled = false;
const controller = new AbortController();
controller.abort();
const source = from('test');
const transform: Transform = (chunks) => {
transformCalled = true;
return chunks;
};
const output = pull(source, transform, { signal: controller.signal });
await assert.rejects(async () => {
await collectBytes(output);
}, /Abort/);
assert.strictEqual(transformCalled, false);
});
it('should give each transform its own options object', async () => {
const options1: object[] = [];
const options2: object[] = [];
const source = from('hello');
const t1: Transform = (chunks, opts) => {
options1.push(opts);
return chunks;
};
const t2: Transform = (chunks, opts) => {
options2.push(opts);
return chunks;
};
const output = pull(source, t1, t2);
await collectBytes(output);
assert.ok(options1.length > 0, 'Transform 1 should have been called');
assert.ok(options2.length > 0, 'Transform 2 should have been called');
assert.notStrictEqual(options1[0], options2[0], 'Each transform should get its own options object');
});
});
});
describe('pipeToSync()', () => {
describe('basic usage', () => {
it('should write source to writer without transforms [WRITE-004]', () => {
const source = fromSync('Hello, World!');
const writer = createMockSyncWriter();
const bytesWritten = pipeToSync(source, writer);
assert.strictEqual(bytesWritten, 13);
assert.strictEqual(writer.closed, true);
assert.strictEqual(decode(concatBytes(writer.chunks)), 'Hello, World!');
});
it('should apply transforms before writing [WRITE-005]', () => {
const source = fromSync('hello');
const toUpper: SyncTransform = (chunks) => {
if (chunks === null) return null;
return chunks.map((c) => new TextEncoder().encode(decode(c).toUpperCase()));
};
const writer = createMockSyncWriter();
const bytesWritten = pipeToSync(source, toUpper, writer);
assert.strictEqual(bytesWritten, 5);
assert.strictEqual(decode(concatBytes(writer.chunks)), 'HELLO');
});
});
describe('options', () => {
it('should respect preventClose option [WRITE-010]', () => {
const source = fromSync('test');
const writer = createMockSyncWriter();
pipeToSync(source, writer, { preventClose: true });
assert.strictEqual(writer.closed, false);
});
it('should respect preventFail option [WRITE-011]', () => {
const source = {
*[Symbol.iterator]() {
yield [new TextEncoder().encode('test')];
throw new Error('Source error');
},
};
const writer = createMockSyncWriter();
assert.throws(() => {
pipeToSync(source, writer, { preventFail: true });
}, /Source error/);
assert.strictEqual(writer.failed, undefined);
});
});
describe('error handling', () => {
it('should fail writer on source error [WRITE-020]', () => {
const source = {
*[Symbol.iterator]() {
yield [new TextEncoder().encode('test')];
throw new Error('Source error');
},
};
const writer = createMockSyncWriter();
assert.throws(() => {
pipeToSync(source, writer);
}, /Source error/);
assert.ok(writer.failed);
});
it('should throw if no writer provided [WRITE-021]', () => {
const source = fromSync('test');
assert.throws(() => {
pipeToSync(source);
}, /pipeTo requires a writer argument/);
});
});
});
describe('pipeTo()', () => {
describe('basic usage', () => {
it('should write source to writer without transforms [WRITE-001]', async () => {
const source = from('Hello, World!');
const writer = createMockWriter();
const bytesWritten = await pipeTo(source, writer);
assert.strictEqual(bytesWritten, 13);
assert.strictEqual(writer.closed, true);
});
it('should work with sync source [WRITE-002]', async () => {
const source = fromSync('Sync Source');
const writer = createMockWriter();
const bytesWritten = await pipeTo(source, writer);
assert.strictEqual(bytesWritten, 11);
});
it('should apply transforms before writing [WRITE-003]', async () => {
const source = from('hello');
const toUpper: Transform = (chunks) => {
if (chunks === null) return null;
return chunks.map((c) => new TextEncoder().encode(decode(c).toUpperCase()));
};
const writer = createMockWriter();
const bytesWritten = await pipeTo(source, toUpper, writer);
assert.strictEqual(bytesWritten, 5);
assert.strictEqual(decode(concatBytes(writer.chunks)), 'HELLO');
});
});
describe('options', () => {
it('should respect preventClose option [WRITE-010]', async () => {
const source = from('test');
const writer = createMockWriter();
await pipeTo(source, writer, { preventClose: true });
assert.strictEqual(writer.closed, false);
});
it('should respect preventFail option [WRITE-011]', async () => {
const source = from(
(async function* () {
yield [new TextEncoder().encode('test')];
throw new Error('Source error');
})()
);
const writer = createMockWriter();
await assert.rejects(async () => {
await pipeTo(source, writer, { preventFail: true });
}, /Source error/);
assert.strictEqual(writer.failed, undefined);
});
it('should respect AbortSignal [WRITE-012]', async () => {
const controller = new AbortController();
const source = from(
(async function* () {
yield [new TextEncoder().encode('chunk1')];
await new Promise((resolve) => setTimeout(resolve, 100));
yield [new TextEncoder().encode('chunk2')];
})()
);
const writer = createMockWriter();
// Abort after first chunk
setTimeout(() => controller.abort(), 20);
await assert.rejects(async () => {
await pipeTo(source, writer, { signal: controller.signal });
}, /Abort/);
});
});
describe('error handling', () => {
it('should fail writer on source error [WRITE-020]', async () => {
const source = from(
(async function* () {
yield [new TextEncoder().encode('test')];
throw new Error('Source error');
})()
);
const writer = createMockWriter();
await assert.rejects(async () => {
await pipeTo(source, writer);
}, /Source error/);
assert.ok(writer.failed);
});
it('should throw if no writer provided [WRITE-021]', async () => {
const source = from('test');
await assert.rejects(async () => {
await pipeTo(source);
}, /pipeTo requires a writer argument/);
});
});
describe('signal propagation to writes', () => {
it('should pass signal to writer.write() and cancel blocked write [WRITE-025]', async () => {
// Create a writer that blocks on write until signal fires
let writeSignalReceived = false;
const writerChunks: Uint8Array[] = [];
const slowWriter: Writer = {
get desiredSize() { return 10; },
async write(chunk: Uint8Array | string, options?) {
const data = typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk;
if (options?.signal) {
// Simulate slow I/O that respects signal
await new Promise<void>((resolve, reject) => {
if (options.signal!.aborted) {
writeSignalReceived = true;
reject(options.signal!.reason);
return;
}
const onAbort = () => {
writeSignalReceived = true;
reject(options.signal!.reason);
};
options.signal!.addEventListener('abort', onAbort, { once: true });
// Simulate slow write that takes 200ms
setTimeout(() => {
options.signal!.removeEventListener('abort', onAbort);
writerChunks.push(data);
resolve();
}, 200);
});
} else {
writerChunks.push(data);
}
},
async writev(chunks, options?) {
for (const c of chunks) await this.write(c, options);
},
writeSync() { return false; },
writevSync() { return false; },
async end() { return writerChunks.reduce((a, c) => a + c.byteLength, 0); },
endSync() { return -1; },
async fail() {},
failSync() { return true; },
};
const controller = new AbortController();
const source = from((async function* () {
yield [new TextEncoder().encode('chunk1')];
})());
// Abort after a short delay — the write will be in-flight
setTimeout(() => controller.abort(), 30);
await assert.rejects(async () => {
await pipeTo(source, slowWriter, { signal: controller.signal });
}, /Abort/);
assert.strictEqual(writeSignalReceived, true);
});
it('should pass signal to writer.end() [WRITE-026]', async () => {
let endSignalReceived = false;
const writer: Writer = {
get desiredSize() { return 10; },
async write(chunk: Uint8Array | string) {},
async writev(chunks: (Uint8Array | string)[]) {},
writeSync() { return true; },
writevSync() { return true; },
async end(options?) {
endSignalReceived = options?.signal !== undefined;
return 0;
},
endSync() { return 0; },
async fail() {},
failSync() { return true; },
};
const source = from('test');
await pipeTo(source, writer, { signal: new AbortController().signal });
assert.strictEqual(endSignalReceived, true);
});
it('should not pass signal to writes when no user signal provided', async () => {
let writeOptionsReceived: unknown = 'not-called';
const writer: Writer = {
get desiredSize() { return 10; },
async write(chunk: Uint8Array | string, options?) {
writeOptionsReceived = options;
},
async writev(chunks: (Uint8Array | string)[]) {},
writeSync() { return true; },
writevSync() { return true; },
async end() { return 0; },
endSync() { return 0; },
async fail() {},
failSync() { return true; },
};
const source = from('test');
await pipeTo(source, writer);
assert.strictEqual(writeOptionsReceived, undefined);
});
});
describe('transform-writer', () => {
it('should handle writer that is also a transform [WRITE-030]', async () => {
const source = from('hello');
const hashes: string[] = [];
const writerChunks: Uint8Array[] = [];
// TransformObject = stateful transform, receives entire source as async iterable
const hashingWriter: Writer & { transform: (source: AsyncIterable<Uint8Array[] | null>) => AsyncGenerator<Uint8Array[]> } = {
async *transform(source) {
for await (const chunks of source) {
if (chunks) {
for (const c of chunks) {
hashes.push(`hash:${c.byteLength}`);
}
yield chunks;
}
}
},
get desiredSize() {
return 10;
},
async write(chunk: Uint8Array | string) {
const data = typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk;
writerChunks.push(data);
},
async writev(chunks: (Uint8Array | string)[]) {
for (const chunk of chunks) {
await this.write(chunk);
}
},
writeSync() { return true; },
writevSync() { return true; },
async end() {
return writerChunks.reduce((acc, c) => acc + c.byteLength, 0);
},
endSync() { return 0; },
async fail() {},
failSync() { return true; },
};
await pipeTo(source, hashingWriter);
assert.ok(hashes.length > 0);
assert.ok(hashes[0].startsWith('hash:'));
});
});
});