1
1
'use strict' ;
2
2
3
- const PassThrough = require ( 'stream' ) . PassThrough ;
3
+ const { Readable , Writable , Transform , PassThrough} = require ( 'stream' ) ;
4
4
const chai = require ( 'chai' ) ;
5
5
chai . use ( require ( 'chai-as-promised' ) ) ;
6
6
const Promise = require ( 'bluebird' ) ;
@@ -86,6 +86,7 @@ describe('Unifile class', function() {
86
86
let unifile ;
87
87
let inMemoryFile = '' ;
88
88
beforeEach ( 'Instanciation' , function ( ) {
89
+ inMemoryFile = '' ;
89
90
unifile = new Unifile ( ) ;
90
91
unifile . use ( {
91
92
name : 'memory' ,
@@ -94,6 +95,23 @@ describe('Unifile class', function() {
94
95
writeFile : ( session , path , content ) => {
95
96
inMemoryFile = content ;
96
97
return Promise . resolve ( ) ;
98
+ } ,
99
+ createReadStream : ( session , path ) => {
100
+ return new Readable ( {
101
+ read ( size ) {
102
+ this . push ( 'hello' ) ;
103
+ this . push ( null ) ;
104
+ }
105
+ } ) ;
106
+ } ,
107
+ createWriteStream : ( session , path ) => {
108
+ //return require('fs').createWriteStream('./test.log');
109
+ return new Writable ( {
110
+ write ( chunk , encoding , callback ) {
111
+ inMemoryFile += chunk . toString ( ) ;
112
+ callback ( null ) ;
113
+ }
114
+ } ) ;
97
115
}
98
116
} ) ;
99
117
} ) ;
@@ -103,10 +121,8 @@ describe('Unifile class', function() {
103
121
onWrite : console . log ,
104
122
onRead : console . log
105
123
} ) ;
106
- expect ( unifile . plugins . onRead ) . to . have . lengthOf ( 1 ) ;
107
- expect ( unifile . plugins . onWrite ) . to . have . lengthOf ( 1 ) ;
108
- expect ( unifile . plugins . onRead [ 0 ] ) . to . equal ( console . log ) ;
109
- expect ( unifile . plugins . onWrite [ 0 ] ) . to . equal ( console . log ) ;
124
+ expect ( unifile . plugins . onRead ) . to . equal ( console . log ) ;
125
+ expect ( unifile . plugins . onWrite ) . to . equal ( console . log ) ;
110
126
} ) ;
111
127
112
128
it ( 'can modify the input on write action' , function ( ) {
@@ -126,18 +142,40 @@ describe('Unifile class', function() {
126
142
. should . eventually . equal ( 'aa' ) ;
127
143
} ) ;
128
144
129
- it ( 'follow a chain of action in order ' , function ( ) {
145
+ it ( 'can modify the input on write stream ' , function ( done ) {
130
146
unifile . ext ( {
131
- onRead : ( input ) => input . replace ( 'b' , 'a' ) ,
132
- onWrite : ( input ) => input . replace ( 'a' , 'b' )
147
+ onWriteStream : new Transform ( {
148
+ transform ( chunk , encoding , callback ) {
149
+ callback ( null , chunk . toString ( ) . toUpperCase ( ) ) ;
150
+ }
151
+ } )
152
+ } ) ;
153
+ const stream = unifile . createWriteStream ( { } , 'memory' , '' ) ;
154
+ stream . on ( 'end' , ( ) => {
155
+ expect ( inMemoryFile ) . to . equal ( 'HELLO' ) ;
156
+ done ( ) ;
133
157
} ) ;
158
+ stream . end ( 'hello' ) ;
159
+ } ) ;
160
+
161
+ it ( 'can modify the input on read stream' , function ( done ) {
162
+ inMemoryFile = 'hello' ;
134
163
unifile . ext ( {
135
- onRead : ( input ) => input . replace ( 'a' , 'c' ) ,
136
- onWrite : ( input ) => input . replace ( 'b' , 'd' )
164
+ onReadStream : new Transform ( {
165
+ transform ( chunk , encoding , callback ) {
166
+ callback ( null , chunk . toString ( ) . toUpperCase ( ) ) ;
167
+ }
168
+ } )
169
+ } ) ;
170
+ const stream = unifile . createReadStream ( { } , 'memory' , '' ) ;
171
+ const chunks = [ ] ;
172
+ stream . on ( 'data' , ( chunk ) => {
173
+ chunks . push ( chunk ) ;
174
+ } ) ;
175
+ stream . on ( 'end' , ( ) => {
176
+ expect ( Buffer . concat ( chunks ) . toString ( ) ) . to . equal ( 'HELLO' ) ;
177
+ done ( ) ;
137
178
} ) ;
138
- return unifile . writeFile ( { } , 'memory' , '' , 'ab' )
139
- . then ( ( ) => expect ( inMemoryFile ) . to . equal ( 'db' ) )
140
- . then ( ( ) => unifile . readFile ( { } , 'memory' , '' ) . should . eventually . equal ( 'dc' ) ) ;
141
179
} ) ;
142
180
} ) ;
143
181
0 commit comments