@@ -22,9 +22,23 @@ export const DEFAULT_SKIP_NAMES = new Set([
2222 "tmp" ,
2323] ) ;
2424
25- function assertPositiveInteger ( value , label ) {
26- if ( ! Number . isSafeInteger ( value ) || value <= 0 ) {
27- throw new Error ( `${ label } must be a positive safe integer` ) ;
25+ function isRecord ( value ) {
26+ return value !== null && typeof value === "object" && ! Array . isArray ( value ) ;
27+ }
28+
29+ function readCleanOption ( options , key , defaultValue ) {
30+ return Object . hasOwn ( options , key ) ? options [ key ] : defaultValue ;
31+ }
32+
33+ function assertBoolean ( value , label ) {
34+ if ( typeof value !== "boolean" ) {
35+ throw new Error ( `${ label } must be a boolean` ) ;
36+ }
37+ }
38+
39+ function assertPositiveIntegerAtMost ( value , label , maximum ) {
40+ if ( ! Number . isSafeInteger ( value ) || value <= 0 || value > maximum ) {
41+ throw new Error ( `${ label } must be a positive safe integer no greater than ${ maximum } ` ) ;
2842 }
2943}
3044
@@ -88,28 +102,41 @@ function assertCleanSuffixes(value, label) {
88102}
89103
90104function resolveCleanOptions ( options ) {
105+ if ( ! isRecord ( options ) ) {
106+ throw new Error ( "clean options must be an object" ) ;
107+ }
108+
91109 return {
92- removableNames : options . removableNames ?? DEFAULT_REMOVABLE_NAMES ,
93- removableSuffixes : options . removableSuffixes ?? DEFAULT_REMOVABLE_SUFFIXES ,
94- skipNames : options . skipNames ?? DEFAULT_SKIP_NAMES ,
95- verifyRoot : options . verifyRoot ?? true ,
96- maxDirectories : options . maxDirectories ?? MAX_CLEAN_DIRECTORIES ,
97- maxFiles : options . maxFiles ?? MAX_CLEAN_FILES ,
98- maxDirectoryEntries : options . maxDirectoryEntries ?? MAX_CLEAN_DIRECTORY_ENTRIES ,
99- maxRemovals : options . maxRemovals ?? MAX_CLEAN_REMOVALS ,
100- maxPathBytes : options . maxPathBytes ?? MAX_CLEAN_PATH_BYTES ,
110+ removableNames : readCleanOption ( options , "removableNames" , DEFAULT_REMOVABLE_NAMES ) ,
111+ removableSuffixes : readCleanOption ( options , "removableSuffixes" , DEFAULT_REMOVABLE_SUFFIXES ) ,
112+ skipNames : readCleanOption ( options , "skipNames" , DEFAULT_SKIP_NAMES ) ,
113+ verifyRoot : readCleanOption ( options , "verifyRoot" , true ) ,
114+ maxDirectories : readCleanOption ( options , "maxDirectories" , MAX_CLEAN_DIRECTORIES ) ,
115+ maxFiles : readCleanOption ( options , "maxFiles" , MAX_CLEAN_FILES ) ,
116+ maxDirectoryEntries : readCleanOption (
117+ options ,
118+ "maxDirectoryEntries" ,
119+ MAX_CLEAN_DIRECTORY_ENTRIES ,
120+ ) ,
121+ maxRemovals : readCleanOption ( options , "maxRemovals" , MAX_CLEAN_REMOVALS ) ,
122+ maxPathBytes : readCleanOption ( options , "maxPathBytes" , MAX_CLEAN_PATH_BYTES ) ,
101123 } ;
102124}
103125
104126function assertCleanOptions ( options ) {
105127 assertCleanNameSet ( options . removableNames , "removableNames" ) ;
106128 assertCleanSuffixes ( options . removableSuffixes , "removableSuffixes" ) ;
107129 assertCleanNameSet ( options . skipNames , "skipNames" ) ;
108- assertPositiveInteger ( options . maxDirectories , "maxDirectories" ) ;
109- assertPositiveInteger ( options . maxFiles , "maxFiles" ) ;
110- assertPositiveInteger ( options . maxDirectoryEntries , "maxDirectoryEntries" ) ;
111- assertPositiveInteger ( options . maxRemovals , "maxRemovals" ) ;
112- assertPositiveInteger ( options . maxPathBytes , "maxPathBytes" ) ;
130+ assertBoolean ( options . verifyRoot , "verifyRoot" ) ;
131+ assertPositiveIntegerAtMost ( options . maxDirectories , "maxDirectories" , MAX_CLEAN_DIRECTORIES ) ;
132+ assertPositiveIntegerAtMost ( options . maxFiles , "maxFiles" , MAX_CLEAN_FILES ) ;
133+ assertPositiveIntegerAtMost (
134+ options . maxDirectoryEntries ,
135+ "maxDirectoryEntries" ,
136+ MAX_CLEAN_DIRECTORY_ENTRIES ,
137+ ) ;
138+ assertPositiveIntegerAtMost ( options . maxRemovals , "maxRemovals" , MAX_CLEAN_REMOVALS ) ;
139+ assertPositiveIntegerAtMost ( options . maxPathBytes , "maxPathBytes" , MAX_CLEAN_PATH_BYTES ) ;
113140}
114141
115142function assertPathWithinLimit ( root , absolutePath , maxPathBytes ) {
0 commit comments