-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
797 lines (653 loc) · 22.2 KB
/
test.js
File metadata and controls
797 lines (653 loc) · 22.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
import {Readable} from 'node:stream';
import {
equal,
deepEqual,
ok,
throws,
rejects,
} from 'node:assert/strict';
import test from 'node:test';
import {chunk, chunkFrom, chunkFromAsync} from './index.js';
// Helper to collect all chunks from a generator
function collectChunks(generator) {
return [...generator];
}
// Helper to collect all chunks from an async generator
async function collectAsyncChunks(asyncGenerator) {
const chunks = [];
for await (const chunk of asyncGenerator) {
chunks.push(chunk);
}
return chunks;
}
test('chunk - splits Uint8Array into default 64KB chunks', () => {
const buffer = new Uint8Array(200_000);
const chunks = collectChunks(chunk(buffer, 65_536));
equal(chunks.length, 4);
equal(chunks[0].length, 65_536);
equal(chunks[1].length, 65_536);
equal(chunks[2].length, 65_536);
equal(chunks[3].length, 3392); // Remainder
});
test('chunk - splits Uint8Array with custom chunk size', () => {
const buffer = new Uint8Array(300_000);
const chunks = collectChunks(chunk(buffer, 100_000));
equal(chunks.length, 3);
equal(chunks[0].length, 100_000);
equal(chunks[1].length, 100_000);
equal(chunks[2].length, 100_000);
});
test('chunk - handles exact multiple of chunk size', () => {
const buffer = new Uint8Array(196_608); // Exactly 3 * 65536
const chunks = collectChunks(chunk(buffer, 65_536));
equal(chunks.length, 3);
equal(chunks[0].length, 65_536);
equal(chunks[1].length, 65_536);
equal(chunks[2].length, 65_536);
});
test('chunk - handles buffer smaller than chunk size', () => {
const buffer = new Uint8Array(1000);
const chunks = collectChunks(chunk(buffer, 65_536));
equal(chunks.length, 1);
equal(chunks[0].length, 1000);
});
test('chunk - handles empty buffer', () => {
const buffer = new Uint8Array(0);
const chunks = collectChunks(chunk(buffer, 65_536));
equal(chunks.length, 0);
});
test('chunk - preserves data integrity', () => {
const buffer = new Uint8Array(1000);
// Fill with sequential values
for (let index = 0; index < buffer.length; index++) {
buffer[index] = index % 256;
}
const chunks = collectChunks(chunk(buffer, 300));
// Reconstruct the buffer from chunks
const reconstructed = new Uint8Array(1000);
let offset = 0;
for (const chunk of chunks) {
reconstructed.set(chunk, offset);
offset += chunk.length;
}
deepEqual(reconstructed, buffer);
});
test('chunk - works with Uint16Array', () => {
const buffer = new Uint16Array(1000);
const chunks = collectChunks(chunk(buffer, 1000));
// Uint16Array has 2 bytes per element, so 1000 elements = 2000 bytes
equal(chunks.length, 2);
equal(chunks[0].length, 1000);
equal(chunks[1].length, 1000);
});
test('chunk - works with Int32Array', () => {
const buffer = new Int32Array(500);
const chunks = collectChunks(chunk(buffer, 1000));
// Int32Array has 4 bytes per element, so 500 elements = 2000 bytes
equal(chunks.length, 2);
equal(chunks[0].length, 1000);
equal(chunks[1].length, 1000);
});
test('chunk - chunks are views of original buffer', () => {
const buffer = new Uint8Array(1000);
buffer[0] = 42;
buffer[999] = 99;
const chunks = collectChunks(chunk(buffer, 300));
equal(chunks[0][0], 42);
equal(chunks.at(-1)[99], 99); // Last chunk, offset 99
});
test('chunk - throws on non-ArrayBufferView input', () => {
throws(() => {
collectChunks(chunk('not a buffer', 65_536));
}, {
name: 'TypeError',
message: 'Expected data to be ArrayBufferView',
});
throws(() => {
collectChunks(chunk([1, 2, 3], 65_536));
}, {
name: 'TypeError',
message: 'Expected data to be ArrayBufferView',
});
throws(() => {
collectChunks(chunk(undefined, 65_536));
}, {
name: 'TypeError',
message: 'Expected data to be ArrayBufferView',
});
});
test('chunk - throws on invalid chunk size', () => {
const buffer = new Uint8Array(100);
const error = {
name: 'TypeError',
message: 'Expected chunkSize to be a positive integer',
};
throws(() => {
collectChunks(chunk(buffer, 0));
}, error);
throws(() => {
collectChunks(chunk(buffer, -1));
}, error);
throws(() => {
collectChunks(chunk(buffer, 1.5));
}, error);
throws(() => {
collectChunks(chunk(buffer, 'invalid'));
}, error);
});
test('chunk - validates parameters in order', () => {
// When both parameters are invalid, should throw error for first parameter (data)
throws(() => {
collectChunks(chunk(null, -1));
}, {
name: 'TypeError',
message: 'Expected data to be ArrayBufferView',
});
});
test('chunkFromAsync - splits stream chunks into smaller pieces', async () => {
const buffer = new Uint8Array(200_000);
const stream = Readable.from([buffer]);
const chunks = await collectAsyncChunks(chunkFromAsync(stream, 65_536));
// Readable.from emits the whole buffer as one chunk, which gets split
ok(chunks.length > 1);
equal(chunks[0].length, 65_536);
equal(chunks.at(-1).length, 200_000 % 65_536);
// Verify total size
const totalSize = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
equal(totalSize, 200_000);
});
test('chunkFromAsync - works with custom chunk size', async () => {
const buffer = new Uint8Array(300_000);
const stream = Readable.from([buffer]);
const chunks = await collectAsyncChunks(chunkFromAsync(stream, 100_000));
equal(chunks.length, 3);
equal(chunks[0].length, 100_000);
equal(chunks[1].length, 100_000);
equal(chunks[2].length, 100_000);
});
test('chunkFromAsync - preserves data integrity', async () => {
const buffer = new Uint8Array(10_000);
// Fill with sequential values
for (let index = 0; index < buffer.length; index++) {
buffer[index] = index % 256;
}
const stream = Readable.from([buffer]);
const chunks = await collectAsyncChunks(chunkFromAsync(stream, 3000));
// Reconstruct the buffer from chunks
const reconstructed = new Uint8Array(10_000);
let offset = 0;
for (const chunk of chunks) {
reconstructed.set(chunk, offset);
offset += chunk.length;
}
deepEqual(reconstructed, buffer);
});
test('chunkFromAsync - handles empty stream', async () => {
const buffer = new Uint8Array(0);
const stream = Readable.from([buffer]);
const chunks = await collectAsyncChunks(chunkFromAsync(stream, 65_536));
equal(chunks.length, 0);
});
test('chunkFromAsync - works with stream that emits multiple chunks', async () => {
// Create a stream that emits multiple chunks
async function * multiChunkGenerator() {
yield new Uint8Array(100_000);
yield new Uint8Array(100_000);
yield new Uint8Array(100_000);
}
const chunks = await collectAsyncChunks(chunkFromAsync(multiChunkGenerator(), 50_000));
// Each 100KB chunk gets split into 2 50KB chunks = 6 total
equal(chunks.length, 6);
for (const chunk of chunks) {
equal(chunk.length, 50_000);
}
});
test('chunkFromAsync - works with sync iterable', async () => {
function * syncGenerator() {
yield new Uint8Array(100_000);
yield new Uint8Array(50_000);
}
const chunks = await collectAsyncChunks(chunkFromAsync(syncGenerator(), 40_000));
ok(chunks.length > 1);
const totalSize = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
equal(totalSize, 150_000);
});
test('chunkFromAsync - handles Uint16Array chunks', async () => {
async function * typedArrayGenerator() {
yield new Uint16Array(500); // 1000 bytes
yield new Uint16Array(500); // 1000 bytes
}
const chunks = await collectAsyncChunks(chunkFromAsync(typedArrayGenerator(), 500));
equal(chunks.length, 4);
for (const chunk of chunks) {
equal(chunk.length, 500);
}
});
test('chunkFromAsync - throws on non-iterable input', async () => {
await rejects(async () => {
for await (const _ of chunkFromAsync(null, 65_536)) {
// Should throw before entering loop
}
}, {
name: 'TypeError',
message: 'Expected iterable to be an async iterable or iterable',
});
await rejects(async () => {
for await (const _ of chunkFromAsync(123, 65_536)) {
// Should throw before entering loop
}
}, {
name: 'TypeError',
message: 'Expected iterable to be an async iterable or iterable',
});
await rejects(async () => {
for await (const _ of chunkFromAsync({}, 65_536)) {
// Should throw before entering loop
}
}, {
name: 'TypeError',
message: 'Expected iterable to be an async iterable or iterable',
});
});
test('chunkFromAsync - throws on invalid chunk size', async () => {
const stream = Readable.from(new Uint8Array(100));
await rejects(async () => {
for await (const _ of chunkFromAsync(stream, 0)) {
// Should throw before entering loop
}
}, {
name: 'TypeError',
message: 'Expected chunkSize to be a positive integer',
});
await rejects(async () => {
for await (const _ of chunkFromAsync(stream, -1)) {
// Should throw before entering loop
}
}, {
name: 'TypeError',
message: 'Expected chunkSize to be a positive integer',
});
});
test('chunkFromAsync - throws on non-TypedArray iterable chunks', async () => {
async function * invalidGenerator() {
yield 'not a buffer';
}
await rejects(async () => {
for await (const _ of chunkFromAsync(invalidGenerator(), 65_536)) {
// Should throw when processing first chunk
}
}, {
name: 'TypeError',
message: 'Expected iterable chunks to be Uint8Array or ArrayBufferView',
});
});
test('chunkFromAsync - handles large files efficiently', async () => {
// Simulate a 10MB file
const largeBuffer = new Uint8Array(10_000_000);
const stream = Readable.from([largeBuffer]);
const chunks = await collectAsyncChunks(chunkFromAsync(stream, 65_536));
// Verify we got proper chunking
ok(chunks.length > 100); // Should have many chunks
ok(chunks.every(chunk => chunk.length <= 65_536)); // All chunks <= 64KB
// Verify total size
const totalSize = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
equal(totalSize, 10_000_000);
});
test('chunkFromAsync - accumulates small chunks to reach desired size', async () => {
// Stream emits multiple small chunks that need accumulation
async function * smallChunkGenerator() {
yield new Uint8Array(300); // 300 bytes
yield new Uint8Array(300); // 300 bytes
yield new Uint8Array(300); // 300 bytes
}
const chunks = await collectAsyncChunks(chunkFromAsync(smallChunkGenerator(), 500));
// First two 300-byte chunks should combine into one 500-byte chunk (with 100 left over)
// Third 300-byte chunk combines with leftover 100 to make 400 bytes
equal(chunks.length, 2);
equal(chunks[0].length, 500);
equal(chunks[1].length, 400);
// Verify total size
const totalSize = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
equal(totalSize, 900);
});
test('chunkFromAsync - handles exact chunk size from single emit', async () => {
async function * exactGenerator() {
yield new Uint8Array(1000);
}
const chunks = await collectAsyncChunks(chunkFromAsync(exactGenerator(), 1000));
equal(chunks.length, 1);
equal(chunks[0].length, 1000);
});
test('chunk - handles exact chunk size', () => {
const buffer = new Uint8Array(1000);
const chunks = collectChunks(chunk(buffer, 1000));
equal(chunks.length, 1);
equal(chunks[0].length, 1000);
});
test('chunk - works with TypedArray view with offset', () => {
// Create a buffer with offset and limited length
const arrayBuffer = new ArrayBuffer(1000);
const view = new Uint16Array(arrayBuffer, 100, 200); // 200 elements starting at byte 100 = 400 bytes
// Fill with test data
for (let index = 0; index < view.length; index++) {
view[index] = index;
}
const chunks = collectChunks(chunk(view, 150));
// 400 bytes total, 150 byte chunks
equal(chunks.length, 3);
equal(chunks[0].length, 150);
equal(chunks[1].length, 150);
equal(chunks[2].length, 100);
// Verify total size
const totalSize = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
equal(totalSize, 400);
});
test('chunkFromAsync - works with TypedArray view with offset', async () => {
const arrayBuffer = new ArrayBuffer(1000);
const view = new Uint16Array(arrayBuffer, 100, 200); // 400 bytes
for (let index = 0; index < view.length; index++) {
view[index] = index;
}
async function * viewGenerator() {
yield view;
}
const chunks = await collectAsyncChunks(chunkFromAsync(viewGenerator(), 150));
equal(chunks.length, 3);
equal(chunks[0].length, 150);
equal(chunks[1].length, 150);
equal(chunks[2].length, 100);
});
test('chunkFromAsync - handles mixed small and large chunks', async () => {
async function * mixedGenerator() {
yield new Uint8Array(100); // Small
yield new Uint8Array(200); // Small
yield new Uint8Array(800); // Large - will split
yield new Uint8Array(150); // Small
}
const chunks = await collectAsyncChunks(chunkFromAsync(mixedGenerator(), 500));
// 100 + 200 = 300 (need more)
// 300 + 800 = 1100 -> emit 500, keep 600
// 600 -> emit 500, keep 100
// 100 + 150 = 250 (final chunk)
equal(chunks.length, 3);
equal(chunks[0].length, 500);
equal(chunks[1].length, 500);
equal(chunks[2].length, 250);
const totalSize = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
equal(totalSize, 1250);
});
test('chunk - handles chunkSize of 1', () => {
const buffer = new Uint8Array(10);
for (let index = 0; index < buffer.length; index++) {
buffer[index] = index;
}
const chunks = collectChunks(chunk(buffer, 1));
equal(chunks.length, 10);
for (const [index, chunk] of chunks.entries()) {
equal(chunk.length, 1);
equal(chunk[0], index);
}
});
// ChunkFrom tests
test('chunkFrom - works with iterable of buffers', () => {
const buffers = [new Uint8Array(1000), new Uint8Array(2000)];
const chunks = collectChunks(chunkFrom(buffers, 500));
// 1000 + 2000 = 3000 bytes total, split into 500-byte chunks
equal(chunks.length, 6);
for (const chunk of chunks) {
equal(chunk.length, 500);
}
});
test('chunkFrom - accumulates small chunks', () => {
const buffers = [new Uint8Array(300), new Uint8Array(300), new Uint8Array(300)];
const chunks = collectChunks(chunkFrom(buffers, 500));
// 300 + 300 = 600 -> emit 500, keep 100
// 100 + 300 = 400 -> final chunk
equal(chunks.length, 2);
equal(chunks[0].length, 500);
equal(chunks[1].length, 400);
});
test('chunkFrom - handles mixed small and large chunks', () => {
const buffers = [new Uint8Array(100), new Uint8Array(800), new Uint8Array(150)];
const chunks = collectChunks(chunkFrom(buffers, 500));
// 100 + 800 = 900 -> emit 500, keep 400
// 400 + 150 = 550 -> emit 500, keep 50
// 50 -> final chunk
equal(chunks.length, 3);
equal(chunks[0].length, 500);
equal(chunks[1].length, 500);
equal(chunks[2].length, 50);
});
test('chunkFrom - preserves data integrity', () => {
const buffer1 = new Uint8Array(500);
const buffer2 = new Uint8Array(500);
for (let index = 0; index < buffer1.length; index++) {
buffer1[index] = index % 256;
}
for (let index = 0; index < buffer2.length; index++) {
buffer2[index] = (index + 128) % 256;
}
const chunks = collectChunks(chunkFrom([buffer1, buffer2], 300));
// Reconstruct
const reconstructed = new Uint8Array(1000);
let offset = 0;
for (const chunk of chunks) {
reconstructed.set(chunk, offset);
offset += chunk.length;
}
deepEqual(reconstructed.subarray(0, 500), buffer1);
deepEqual(reconstructed.subarray(500, 1000), buffer2);
});
test('chunkFrom - works with generator', () => {
function * bufferGenerator() {
yield new Uint8Array(100);
yield new Uint8Array(200);
yield new Uint8Array(300);
}
const chunks = collectChunks(chunkFrom(bufferGenerator(), 250));
const totalSize = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
equal(totalSize, 600);
});
test('chunkFrom - throws on non-iterable input', () => {
throws(() => {
collectChunks(chunkFrom('not iterable', 100));
}, {
name: 'TypeError',
message: 'Expected iterable to be an Iterable<ArrayBufferView>',
});
throws(() => {
collectChunks(chunkFrom(123, 100));
}, {
name: 'TypeError',
message: 'Expected iterable to be an Iterable<ArrayBufferView>',
});
});
test('chunkFrom - throws on non-buffer chunks', () => {
throws(() => {
collectChunks(chunkFrom(['not a buffer'], 100));
}, {
name: 'TypeError',
message: 'Expected iterable chunks to be Uint8Array or ArrayBufferView',
});
});
test('chunkFrom - handles empty iterable', () => {
const chunks = collectChunks(chunkFrom([], 100));
equal(chunks.length, 0);
});
test('chunkFrom - validates parameters in order', () => {
// When both parameters are invalid, should throw error for first parameter (iterable)
throws(() => {
collectChunks(chunkFrom(null, -1));
}, {
name: 'TypeError',
message: 'Expected iterable to be an Iterable<ArrayBufferView>',
});
});
test('chunkFrom - works with TypedArray views', () => {
const buffers = [new Uint16Array(250), new Int32Array(125)];
const chunks = collectChunks(chunkFrom(buffers, 300));
// Uint16Array: 250 elements * 2 bytes = 500 bytes
// Int32Array: 125 elements * 4 bytes = 500 bytes
// Total: 1000 bytes -> 3 chunks (300, 300, 300, 100)
equal(chunks.length, 4);
equal(chunks[0].length, 300);
equal(chunks[1].length, 300);
equal(chunks[2].length, 300);
equal(chunks[3].length, 100);
});
test('chunkFromAsync - handles very large chunkSize', async () => {
async function * generator() {
yield new Uint8Array(100);
yield new Uint8Array(200);
yield new Uint8Array(300);
}
// ChunkSize much larger than total data
const chunks = await collectAsyncChunks(chunkFromAsync(generator(), 1_000_000));
// Should accumulate everything into one chunk
equal(chunks.length, 1);
equal(chunks[0].length, 600);
});
test('chunk - works with DataView input', () => {
const arrayBuffer = new ArrayBuffer(1000);
const dataView = new DataView(arrayBuffer, 100, 500); // 500 bytes starting at byte 100
const chunks = collectChunks(chunk(dataView, 200));
equal(chunks.length, 3);
equal(chunks[0].length, 200);
equal(chunks[1].length, 200);
equal(chunks[2].length, 100);
const totalSize = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
equal(totalSize, 500);
});
test('chunkFromAsync - works with DataView input', async () => {
const arrayBuffer = new ArrayBuffer(1000);
const dataView = new DataView(arrayBuffer, 100, 500);
async function * dataViewGenerator() {
yield dataView;
}
const chunks = await collectAsyncChunks(chunkFromAsync(dataViewGenerator(), 200));
equal(chunks.length, 3);
equal(chunks[0].length, 200);
equal(chunks[1].length, 200);
equal(chunks[2].length, 100);
});
test('chunk - yields views that reflect mutations to original buffer', () => {
const buffer = new Uint8Array(100);
buffer[0] = 42;
buffer[50] = 99;
buffer[99] = 123;
const chunks = collectChunks(chunk(buffer, 40));
// Chunks are views, so they should reflect the original buffer values
equal(chunks[0][0], 42);
equal(chunks[1][10], 99); // Index 50 in original is index 10 in second chunk (40-80)
equal(chunks[2][19], 123); // Index 99 in original is index 19 in third chunk (80-100)
// Mutating the original buffer should reflect in the chunks
buffer[0] = 255;
equal(chunks[0][0], 255);
});
test('chunk - throws on NaN chunkSize', () => {
const buffer = new Uint8Array(100);
throws(() => {
collectChunks(chunk(buffer, Number.NaN));
}, {
name: 'TypeError',
message: 'Expected chunkSize to be a positive integer',
});
});
test('chunk - throws on Infinity chunkSize', () => {
const buffer = new Uint8Array(100);
throws(() => {
collectChunks(chunk(buffer, Number.POSITIVE_INFINITY));
}, {
name: 'TypeError',
message: 'Expected chunkSize to be a positive integer',
});
});
test('chunk - throws on unsafe integer chunkSize', () => {
const buffer = new Uint8Array(100);
throws(() => {
collectChunks(chunk(buffer, Number.MAX_SAFE_INTEGER + 1));
}, {
name: 'TypeError',
message: 'Expected chunkSize to be a positive integer',
});
});
test('chunkFromAsync - throws on NaN chunkSize', async () => {
const stream = Readable.from([new Uint8Array(100)]);
await rejects(async () => {
for await (const _ of chunkFromAsync(stream, Number.NaN)) {
// Should throw before entering loop
}
}, {
name: 'TypeError',
message: 'Expected chunkSize to be a positive integer',
});
});
test('chunkFromAsync - throws on Infinity chunkSize', async () => {
const stream = Readable.from([new Uint8Array(100)]);
await rejects(async () => {
for await (const _ of chunkFromAsync(stream, Number.POSITIVE_INFINITY)) {
// Should throw before entering loop
}
}, {
name: 'TypeError',
message: 'Expected chunkSize to be a positive integer',
});
});
test('chunkFromAsync - throws on unsafe integer chunkSize', async () => {
const stream = Readable.from([new Uint8Array(100)]);
await rejects(async () => {
for await (const _ of chunkFromAsync(stream, Number.MAX_SAFE_INTEGER + 1)) {
// Should throw before entering loop
}
}, {
name: 'TypeError',
message: 'Expected chunkSize to be a positive integer',
});
});
test('chunkFromAsync - validates parameters in order', async () => {
// When both parameters are invalid, should throw error for first parameter (iterable)
await rejects(async () => {
for await (const _ of chunkFromAsync(null, -1)) {
// Should throw before entering loop
}
}, {
name: 'TypeError',
message: 'Expected iterable to be an async iterable or iterable',
});
});
test('chunkFrom - handles many tiny chunks efficiently (avoids O(n²))', () => {
// With fixed carry buffer, it should be fast
function * manyTinyChunks() {
// Emit 10,000 chunks of 1KB each = 10MB total
// Target chunk size is 1MB, so all chunks accumulate before first emit
for (let index = 0; index < 10_000; index++) {
yield new Uint8Array(1024);
}
}
const start = Date.now();
const chunks = collectChunks(chunkFrom(manyTinyChunks(), 1024 * 1024));
const duration = Date.now() - start;
// Should complete in well under 1 second
ok(duration < 1000, `Took ${duration}ms, expected < 1000ms`);
// Verify correctness: 10MB total should produce 10 chunks of 1MB each
equal(chunks.length, 10);
equal(chunks[0].length, 1024 * 1024);
const totalSize = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
equal(totalSize, 10_000 * 1024);
});
test('chunkFromAsync - handles many tiny chunks efficiently (avoids O(n²))', async () => {
// Same test as above but for async variant
async function * manyTinyChunks() {
for (let index = 0; index < 10_000; index++) {
yield new Uint8Array(1024);
}
}
const start = Date.now();
const chunks = await collectAsyncChunks(chunkFromAsync(manyTinyChunks(), 1024 * 1024));
const duration = Date.now() - start;
ok(duration < 1000, `Took ${duration}ms, expected < 1000ms`);
equal(chunks.length, 10);
equal(chunks[0].length, 1024 * 1024);
const totalSize = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
equal(totalSize, 10_000 * 1024);
});