@@ -7,6 +7,8 @@ var Immutable = require("immutable");
77var http = require ( "http" ) ;
88var https = require ( "https" ) ;
99var Map = require ( "immutable" ) . Map ;
10+ var fromJS = require ( "immutable" ) . fromJS ;
11+ var List = require ( "immutable" ) . List ;
1012var snippet = require ( "./../snippet" ) . utils ;
1113var _ = require ( "./../../lodash.custom" ) ;
1214var serveStatic = require ( "serve-static" ) ;
@@ -86,18 +88,18 @@ var utils = {
8688 if ( options . get ( "serveStatic" ) ) {
8789
8890 var ssMiddlewares = utils . getServeStaticMiddlewares ( options . get ( "serveStatic" ) , options . get ( "serveStaticOptions" , Immutable . Map ( { } ) ) . toJS ( ) ) ;
89- var withErrors = ssMiddlewares . filter ( function ( x ) { return x . errors . length > 0 } ) ;
90- var withoutErrors = ssMiddlewares . filter ( function ( x ) { return x . errors . length === 0 } ) ;
91+ var withErrors = ssMiddlewares . filter ( function ( x ) { return x . get ( " errors" ) . size > 0 } ) ;
92+ var withoutErrors = ssMiddlewares . filter ( function ( x ) { return x . get ( " errors" ) . size === 0 } ) ;
9193
9294 if ( withErrors . size ) {
9395 withErrors . forEach ( function ( item ) {
94- logger . logger . error ( "{red:Warning!} %s" , item . errors [ 0 ] . data . message ) ;
96+ logger . logger . error ( "{red:Warning!} %s" , item . getIn ( [ "errors" , 0 , " data" , " message" ] ) ) ;
9597 } ) ;
9698 }
9799
98100 if ( withoutErrors . size ) {
99101 withoutErrors . forEach ( function ( item ) {
100- defaultMiddlewares . push . apply ( defaultMiddlewares , item . items ) ;
102+ defaultMiddlewares . push . apply ( defaultMiddlewares , item . get ( " items" ) . toJS ( ) ) ;
101103 } ) ;
102104 }
103105 }
@@ -174,82 +176,134 @@ var utils = {
174176
175177 return ssOption . map ( function ( dir , i ) {
176178
177- if ( Immutable . Map . isMap ( dir ) ) {
178-
179- var ssOptions = ( function ( ) {
180- if ( dir . get ( "options" ) ) {
181- return dir . get ( "options" ) . toJS ( ) ;
182- }
183- return { }
184- } ) ( ) ;
185-
186- var route = dir . get ( "route" ) ;
187- var _dir = dir . get ( "dir" ) ;
188-
189- if ( ! isValidOption ( route ) || ! isValidOption ( _dir ) ) {
190- return {
191- items : [ ] ,
192- errors : [ {
193- type : "Invalid Object" ,
194- data : {
195- message : "Serve Static requires both 'route' and 'dir' options when using an Object"
196- }
197- } ]
198- }
199- }
200-
201- var ssItems = ( function ( ) {
202- if ( _ . isString ( route ) ) {
203- return [ {
204- id : "Serve static " + i ,
205- route : getRoute ( route ) ,
206- handle : serveStatic ( _dir , ssOptions )
207- } ]
208- }
209- return route . map ( function ( item , j ) {
210- return {
211- id : "Serve static " + i + "." + j ,
212- route : getRoute ( item ) ,
213- handle : serveStatic ( _dir , ssOptions )
214- }
215- } ) . toJS ( )
216- } ) ( ) ;
217- return {
218- items : ssItems ,
219- errors : [ ]
220- } ;
179+ /**
180+ * When a user gives a plain string only, eg:
181+ * serveStatic: ['./temp']
182+ * ->
183+ * This means a middleware will be created with
184+ * route: ''
185+ * handle: serveStatic('./temp', options)
186+ */
187+ if ( _ . isString ( dir ) ) {
188+ return getFromString ( dir )
221189 }
222190
223- if ( _ . isString ( dir ) ) {
224- return {
225- items : [
226- {
227- id : "Serve static " + i ,
228- route : "" ,
229- handle : serveStatic ( dir , serveStaticOptions )
230- }
231- ] ,
232- errors : [ ]
233- }
191+ /**
192+ * If a user gave an object eg:
193+ * serveStatic: [{route: "", dir: ["test", "./tmp"]}]
194+ * ->
195+ * This means we need to create a middle for each route + dir combo
196+ */
197+ if ( Immutable . Map . isMap ( dir ) ) {
198+ return getFromMap ( dir , i ) ;
234199 }
235200
236- return {
201+ /**
202+ * At this point, an item in the serveStatic array was not a string
203+ * or an object so we return an error that can be logged
204+ */
205+ return fromJS ( {
237206 items : [ ] ,
238207 errors : [ {
239208 type : "Invalid Type" ,
240209 data : {
241210 message : "Only strings and Objects (with route+dir) are supported for the ServeStatic option"
242211 }
243212 } ]
244- }
213+ } )
245214 } ) ;
246- function isValidOption ( x ) {
247- return _ . isString ( x ) || ( _ . isArray ( x ) && x . length > 0 ) || ( Immutable . List . isList ( x ) && x . size > 0 ) ;
248- }
249215 function getRoute ( x ) {
250- if ( x === '' ) return '' ;
216+ if ( x === "" ) return "" ;
251217 return x [ 0 ] === "/" ? x : "/" + x ;
252218 }
219+ function getFromString ( dir ) {
220+ return fromJS ( {
221+ items : [
222+ {
223+ route : "" ,
224+ handle : serveStatic ( dir , serveStaticOptions )
225+ }
226+ ] ,
227+ errors : [ ]
228+ } )
229+ }
230+ function getFromMap ( dir ) {
231+
232+ var ssOptions = ( function ( ) {
233+ if ( dir . get ( "options" ) ) {
234+ return dir . get ( "options" ) . toJS ( ) ;
235+ }
236+ return { }
237+ } ) ( ) ;
238+
239+ var route = Immutable . List ( [ ] ) . concat ( dir . get ( "route" ) ) . filter ( _ . isString ) ;
240+ var _dir = Immutable . List ( [ ] ) . concat ( dir . get ( "dir" ) ) . filter ( _ . isString ) ;
241+
242+ if ( _dir . size === 0 ) {
243+
244+ return fromJS ( {
245+ items : [ ] ,
246+ errors : [ {
247+ type : "Invalid Object" ,
248+ data : {
249+ message : "Serve Static requires a 'dir' property when using an Object"
250+ }
251+ } ]
252+ } )
253+ }
254+
255+ var ssItems = ( function ( ) {
256+
257+ /**
258+ * iterate over every 'route' item
259+ * @type {Immutable.List<any>|Immutable.List<*>|Immutable.List<any>|* }
260+ */
261+ var routeItems = ( function ( ) {
262+
263+ /**
264+ * If no 'route' was given, assume we want to match all
265+ * paths
266+ */
267+ if ( route . size === 0 ) {
268+ return _dir . map ( function ( dirString ) {
269+ return Map ( {
270+ route : "" ,
271+ dir : dirString
272+ } ) ;
273+ } ) ;
274+ }
275+
276+ return route . reduce ( function ( acc , routeString ) {
277+ /**
278+ * For each 'route' item, also iterate through 'dirs'
279+ * @type {Immutable.Iterable<K, M> }
280+ */
281+ var perDir = _dir . map ( function ( dirString ) {
282+ return Map ( {
283+ route : getRoute ( routeString ) ,
284+ dir : dirString
285+ } )
286+ } ) ;
287+ return acc . concat ( perDir ) ;
288+
289+ } , List ( [ ] ) ) ;
290+ } ) ( ) ;
291+
292+ /**
293+ * Now create a serverStatic Middleware for each item
294+ */
295+ return routeItems . map ( function ( routeItem ) {
296+ return routeItem . merge ( {
297+ handle : serveStatic ( routeItem . get ( "dir" ) , ssOptions )
298+ } ) ;
299+ } ) ;
300+ } ) ( ) ;
301+
302+ return fromJS ( {
303+ items : ssItems ,
304+ errors : [ ]
305+ } ) ;
306+ }
253307 }
254308} ;
255309
0 commit comments