Skip to content

Commit 2b2ca6b

Browse files
first implementation of fromReadStream with tests
fixes #8
1 parent 2aea226 commit 2b2ca6b

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

src/fromReadStream.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import fromAsyncIterable from './fromAsyncIterable' ;
2+
3+
async function* _fromReadStream ( readStream ) {
4+
5+
for await (const chunk of readStream) {
6+
for (const piece of chunk) yield piece ;
7+
}
8+
9+
}
10+
11+
/**
12+
* Converts a ReadStream object to a stream.
13+
* @param {ReadStream} readStream the ReadStream object to convert
14+
* @function
15+
*/
16+
export default readStream => fromAsyncIterable( _fromReadStream( readStream ) ) ;

src/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
11
import EOF from './EOF' ;
22
import StreamFromCallable from './StreamFromCallable' ;
33
import asyncIterableChain from './asyncIterableChain' ;
4-
import asyncIterableToArray from './asyncIterableToArray' ;
54
import asyncIterableMap from './asyncIterableMap' ;
5+
import asyncIterableToArray from './asyncIterableToArray' ;
66
import chain from './chain' ;
77
import exhaust from './exhaust' ;
88
import fromArray from './fromArray' ;
99
import fromAsyncIterable from './fromAsyncIterable' ;
1010
import fromCallable from './fromCallable' ;
1111
import fromIterable from './fromIterable' ;
1212
import fromIterator from './fromIterator' ;
13+
import fromReadStream from './fromReadStream' ;
1314
import fromString from './fromString' ;
1415
import ignore from './ignore' ;
1516
import map from './map' ;
1617
import skip from './skip' ;
1718
import split from './split' ;
1819
import toArray from './toArray' ;
19-
import toCallable from './toCallable' ;
2020
import toAsyncIterable from './toAsyncIterable' ;
21+
import toCallable from './toCallable' ;
2122
import toIterator from './toIterator' ;
2223
import toString from './toString' ;
2324

2425
export default {
2526
EOF ,
2627
StreamFromCallable ,
2728
asyncIterableChain ,
28-
asyncIterableToArray ,
2929
asyncIterableMap ,
30+
asyncIterableToArray ,
3031
chain ,
3132
exhaust ,
3233
fromArray ,
3334
fromAsyncIterable ,
3435
fromCallable ,
3536
fromIterable ,
3637
fromIterator ,
38+
fromReadStream ,
3739
fromString ,
3840
ignore ,
3941
map ,
4042
skip ,
4143
split ,
4244
toArray ,
43-
toCallable ,
4445
toAsyncIterable ,
46+
toCallable ,
4547
toIterator ,
4648
toString ,
4749
} ;
@@ -50,23 +52,24 @@ export {
5052
EOF ,
5153
StreamFromCallable ,
5254
asyncIterableChain ,
53-
asyncIterableToArray ,
5455
asyncIterableMap ,
56+
asyncIterableToArray ,
5557
chain ,
5658
exhaust ,
5759
fromArray ,
5860
fromAsyncIterable ,
5961
fromCallable ,
6062
fromIterable ,
6163
fromIterator ,
64+
fromReadStream ,
6265
fromString ,
6366
ignore ,
6467
map ,
6568
skip ,
6669
split ,
6770
toArray ,
68-
toCallable ,
6971
toAsyncIterable ,
72+
toCallable ,
7073
toIterator ,
7174
toString ,
7275
} ;

test/data/hello-world.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello, world

test/src/fromReadStream.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import test from 'ava' ;
2+
import fs from 'fs' ;
3+
4+
import stream from '../../src' ;
5+
6+
test( 'unread token' , async t => {
7+
8+
const filepath = 'test/data/hello-world.txt' ;
9+
const encoding = 'utf8' ;
10+
const expected = fs.readFileSync(filepath, encoding);
11+
12+
const readStream = fs.createReadStream(
13+
filepath ,
14+
{
15+
encoding: encoding ,
16+
highWaterMark: 1024 ,
17+
}
18+
) ;
19+
20+
const myStream = stream.fromReadStream( readStream ) ;
21+
22+
t.deepEqual(await stream.toString( myStream ), expected ) ;
23+
24+
} ) ;

0 commit comments

Comments
 (0)