-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (25 loc) · 634 Bytes
/
Copy pathindex.js
File metadata and controls
31 lines (25 loc) · 634 Bytes
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
import {Readable as ReadableStream} from 'node:stream';
import {randomBytes} from 'node:crypto';
export default function randomBytesReadableStream({size = Number.POSITIVE_INFINITY} = {}) {
let producedSize = 0;
return new ReadableStream({
read(readSize) {
let shouldEnd = false;
if ((producedSize + readSize) >= size) {
readSize = size - producedSize;
shouldEnd = true;
}
randomBytes(readSize, (error, buffer) => {
if (error) {
this.emit('error', error);
return;
}
producedSize += readSize;
this.push(buffer);
if (shouldEnd) {
this.push(null);
}
});
},
});
}