-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Readable } from 'stream' | ||
import { randomBytes } from 'crypto' | ||
|
||
export default class RandomStream extends Readable { | ||
constructor(max) { | ||
super() | ||
this.remainingBytes = max | ||
} | ||
_read(requestedSize) { | ||
if (this.remainingBytes === 0) return this.push(null) | ||
|
||
const size = Math.min(this.remainingBytes, requestedSize) | ||
this.remainingBytes -= size | ||
|
||
randomBytes(size, (err, buf) => { | ||
if (err) return this.emit('error', err) | ||
this.push(buf) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as HashStream } from './HashStream' | ||
export { default as SizeStream } from './SizeStream' | ||
export { default as RandomStream } from './RandomStream' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import test from 'blue-tape' | ||
import concat from 'concat-stream' | ||
|
||
import { RandomStream } from '../src/' | ||
|
||
test('random stream', (t) => { | ||
const rand = new RandomStream(10) | ||
|
||
rand.pipe(concat((buf) => { | ||
t.equal(buf.length, 10) | ||
t.end() | ||
})) | ||
}) | ||
|
||
test('random stream #2', (t) => { | ||
const rand = new RandomStream(2000) | ||
|
||
rand.pipe(concat((buf) => { | ||
t.equal(buf.length, 2000) | ||
t.end() | ||
})) | ||
}) |