89
89
*/
90
90
91
91
const { UnifileError} = require ( './error.js' ) ;
92
-
92
+ const { Transform } = require ( 'stream' ) ;
93
93
/**
94
94
* Tells if a method needs authentification
95
95
* @param {string } methodName - Name of the method to test
@@ -104,10 +104,14 @@ function isAuthentifiedFunction(methodName) {
104
104
105
105
const HOOKS = {
106
106
ON_READ : 'onRead' ,
107
- ON_WRITE : 'onWrite'
107
+ ON_READSTREAM : 'onReadStream' ,
108
+ ON_WRITE : 'onWrite' ,
109
+ ON_WRITESTREAM : 'onWriteStream'
108
110
} ;
109
111
110
- const applyPlugins = ( plugins , content ) => plugins . reduce ( ( data , action ) => action ( data ) , content ) ;
112
+ const applyPlugin = ( plugin , content ) => {
113
+ return plugin ? plugin ( content ) : content ;
114
+ } ;
111
115
112
116
const connectors = Symbol ( 'connectors' ) ;
113
117
@@ -126,10 +130,7 @@ class Unifile {
126
130
*/
127
131
constructor ( ) {
128
132
this [ connectors ] = new Map ( ) ;
129
- this . plugins = Object . keys ( HOOKS ) . reduce ( ( memo , key ) => {
130
- memo [ HOOKS [ key ] ] = [ ] ;
131
- return memo ;
132
- } , { } ) ;
133
+ this . plugins = { } ;
133
134
}
134
135
135
136
/**
@@ -149,8 +150,8 @@ class Unifile {
149
150
* @param {Plugin } plugin - A plugin with at least one hook implemented
150
151
*/
151
152
ext ( plugin ) {
152
- Object . keys ( HOOKS ) . map ( ( key ) => HOOKS [ key ] ) . forEach ( ( hook ) => {
153
- if ( plugin . hasOwnProperty ( hook ) ) this . plugins [ hook ] . push ( plugin [ hook ] ) ;
153
+ Object . values ( HOOKS ) . forEach ( ( hook ) => {
154
+ if ( plugin . hasOwnProperty ( hook ) ) this . plugins [ hook ] = plugin [ hook ] ;
154
155
} ) ;
155
156
}
156
157
@@ -272,7 +273,7 @@ class Unifile {
272
273
* @return {external:Promise<null> } an empty promise.
273
274
*/
274
275
writeFile ( session , connectorName , path , content ) {
275
- const input = applyPlugins ( this . plugins [ HOOKS . ON_WRITE ] , content ) ;
276
+ const input = applyPlugin ( this . plugins [ HOOKS . ON_WRITE ] , content ) ;
276
277
return this . callMethod ( connectorName , session , 'writeFile' , path , input ) ;
277
278
}
278
279
@@ -284,7 +285,13 @@ class Unifile {
284
285
* @return {external:WritableStream } a writable stream into the file
285
286
*/
286
287
createWriteStream ( session , connectorName , path ) {
287
- return this . callMethod ( connectorName , session , 'createWriteStream' , path ) ;
288
+ const content = this . callMethod ( connectorName , session , 'createWriteStream' , path ) ;
289
+ const plugin = this . plugins [ HOOKS . ON_WRITESTREAM ] ;
290
+ if ( plugin instanceof Transform ) {
291
+ plugin . pipe ( content ) ;
292
+ return plugin ;
293
+ }
294
+ return content ;
288
295
}
289
296
290
297
/**
@@ -296,7 +303,7 @@ class Unifile {
296
303
*/
297
304
readFile ( session , connectorName , path ) {
298
305
return this . callMethod ( connectorName , session , 'readFile' , path )
299
- . then ( ( content ) => applyPlugins ( this . plugins [ HOOKS . ON_READ ] , content ) ) ;
306
+ . then ( ( content ) => applyPlugin ( this . plugins [ HOOKS . ON_READ ] , content ) ) ;
300
307
}
301
308
302
309
/**
@@ -307,7 +314,12 @@ class Unifile {
307
314
* @return {external:ReadableStream } a readable stream from the file
308
315
*/
309
316
createReadStream ( session , connectorName , path ) {
310
- return this . callMethod ( connectorName , session , 'createReadStream' , path ) ;
317
+ const content = this . callMethod ( connectorName , session , 'createReadStream' , path ) ;
318
+ const plugin = this . plugins [ HOOKS . ON_READSTREAM ] ;
319
+ if ( plugin instanceof Transform ) {
320
+ return content . pipe ( plugin ) ;
321
+ }
322
+ return content ;
311
323
}
312
324
313
325
/**
0 commit comments