File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -114,6 +114,12 @@ function cache (options) {
114114 return sharp . cache ( 0 , 0 , 0 ) ;
115115 }
116116 } else if ( is . object ( options ) ) {
117+ for ( const property of [ 'memory' , 'files' , 'items' ] ) {
118+ const value = options [ property ] ;
119+ if ( is . defined ( value ) && ! ( is . integer ( value ) && value >= 0 ) ) {
120+ throw is . invalidParameterError ( property , 'a positive integer' , value ) ;
121+ }
122+ }
117123 return sharp . cache ( options . memory , options . files , options . items ) ;
118124 } else {
119125 return sharp . cache ( ) ;
Original file line number Diff line number Diff line change @@ -23,15 +23,15 @@ Napi::Value cache(const Napi::CallbackInfo& info) {
2323
2424 // Set memory limit
2525 if (info[size_t (0 )].IsNumber ()) {
26- vips_cache_set_max_mem (info[size_t (0 )].As <Napi::Number>().Int32Value ( ) * 1048576 );
26+ vips_cache_set_max_mem (static_cast < size_t >( info[size_t (0 )].As <Napi::Number>().Uint32Value () ) * 1048576 );
2727 }
2828 // Set file limit
2929 if (info[size_t (1 )].IsNumber ()) {
30- vips_cache_set_max_files (info[size_t (1 )].As <Napi::Number>().Int32Value ());
30+ vips_cache_set_max_files (info[size_t (1 )].As <Napi::Number>().Uint32Value ());
3131 }
3232 // Set items limit
3333 if (info[size_t (2 )].IsNumber ()) {
34- vips_cache_set_max (info[size_t (2 )].As <Napi::Number>().Int32Value ());
34+ vips_cache_set_max (info[size_t (2 )].As <Napi::Number>().Uint32Value ());
3535 }
3636
3737 // Get memory stats
Original file line number Diff line number Diff line change @@ -57,6 +57,11 @@ suite('Utilities', () => {
5757 t . assert . strictEqual ( cache . files . max , 100 ) ;
5858 t . assert . strictEqual ( cache . items . max , 1000 ) ;
5959 } ) ;
60+ test ( 'Can be set to a memory maximum of 4096MB' , ( t ) => {
61+ t . plan ( 1 ) ;
62+ const cache = sharp . cache ( { memory : 4096 , files : 0 , items : 0 } ) ;
63+ t . assert . strictEqual ( cache . memory . max , 4096 ) ;
64+ } ) ;
6065 test ( 'Ignores invalid values' , ( t ) => {
6166 t . plan ( 3 ) ;
6267 sharp . cache ( true ) ;
@@ -65,6 +70,12 @@ suite('Utilities', () => {
6570 t . assert . strictEqual ( cache . files . max , 20 ) ;
6671 t . assert . strictEqual ( cache . items . max , 100 ) ;
6772 } ) ;
73+ test ( 'Rejects negative values' , ( t ) => {
74+ t . plan ( 3 ) ;
75+ t . assert . throws ( ( ) => sharp . cache ( { memory : - 1 } ) , / E x p e c t e d a p o s i t i v e i n t e g e r f o r m e m o r y b u t r e c e i v e d - 1 o f t y p e n u m b e r / ) ;
76+ t . assert . throws ( ( ) => sharp . cache ( { files : - 1 } ) , / E x p e c t e d a p o s i t i v e i n t e g e r f o r f i l e s b u t r e c e i v e d - 1 o f t y p e n u m b e r / ) ;
77+ t . assert . throws ( ( ) => sharp . cache ( { items : 1.5 } ) , / E x p e c t e d a p o s i t i v e i n t e g e r f o r i t e m s b u t r e c e i v e d 1 .5 o f t y p e n u m b e r / ) ;
78+ } ) ;
6879 } ) ;
6980
7081 suite ( 'Concurrency' , ( ) => {
You can’t perform that action at this time.
0 commit comments