@@ -2,13 +2,19 @@ const helpers = require('./../helpers');
22const config = require ( 'config' ) ;
33const filesize = require ( 'filesize' ) ;
44const consts = require ( './../constants' ) ;
5- const Duplex = require ( 'stream' ) . Duplex ;
5+ const { Duplex, Writable } = require ( 'stream' ) ;
66const { promisify } = require ( 'util' ) ;
77
88const kSource = Symbol ( "source" ) ;
99const kCache = Symbol ( "cache" ) ;
1010const kSendFileQueue = Symbol ( "sendFileQueue" ) ;
1111
12+ class NullStream extends Writable {
13+ _write ( chunk , encoding , cb ) {
14+ setImmediate ( cb ) ;
15+ }
16+ }
17+
1218class CommandProcessor extends Duplex {
1319
1420 /**
@@ -24,7 +30,6 @@ class CommandProcessor extends Duplex {
2430 putStream : this . _handleWrite . bind ( this ) ,
2531 command : this . _handleCommand . bind ( this ) ,
2632 version : this . _handleVersion . bind ( this ) ,
27- none : ( ) => Promise . resolve ( )
2833 } ;
2934
3035 this . _writeHandler = this . _writeHandlers . version ;
@@ -40,6 +45,7 @@ class CommandProcessor extends Duplex {
4045 this . _putWhitelist = this . _options . putWhitelist ;
4146 this . _whitelistEmpty = ( ! Array . isArray ( this . _putWhitelist ) || ! this . _putWhitelist . length ) ;
4247
48+ this . _nullStream = new NullStream ( ) ;
4349 this . _putStream = null ;
4450 this . _putSize = 0 ;
4551 this . _putSent = 0 ;
@@ -344,14 +350,14 @@ class CommandProcessor extends Duplex {
344350
345351 if ( this . _isWhitelisted ( this . _trx . clientAddress ) ) {
346352 this . _putStream = await this . _trx . getWriteStream ( type , size ) ;
347- this . _putStream . promiseWrite = promisify ( this . _putStream . write ) . bind ( this . _putStream ) ;
348- this . _putSize = size ;
349- this . _writeHandler = this . _writeHandlers . putStream ;
350- }
351- else {
352- this . _writeHandler = this . _writeHandlers . none ;
353+ } else {
354+ this . _putStream = this . _nullStream ;
353355 helpers . log ( consts . LOG_DBG , `PUT rejected from non-whitelisted IP: ${ this . _trx . clientAddress } ` ) ;
354356 }
357+
358+ this . _putStream . promiseWrite = promisify ( this . _putStream . write ) . bind ( this . _putStream ) ;
359+ this . _putSize = size ;
360+ this . _writeHandler = this . _writeHandlers . putStream ;
355361 }
356362}
357363
0 commit comments