-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhierarchy.js
309 lines (254 loc) · 10.4 KB
/
hierarchy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// hierarchy module - website inheritance
// (c)copyright 2014-2015 by Gerald Wodni <[email protected]>
"use strict";
var async = require('async');
var path = require("path");
var fs = require("fs");
var _ = require("underscore");
module.exports = function _hierarchy( k ) {
var routes = {};
var websitesRoot = k.kernOpts.websitesRoot;
/* locate by hierarchy: cut subdomains, then check 'default' folder */
function lookupFile( website, filename, opts ) {
opts = opts || {};
/* file found */
var filePath = opts.unlockRoot ? path.join( website, filename ) : path.join( websitesRoot, website, filename )
//console.log( "LookUp:", filePath );
if( fs.existsSync( filePath ) )
return filePath;
/* check for route */
if( website in routes )
return lookupFile( routes[website], filename );
/* cut next subdomain */
var firstDot = website.indexOf(".");
if( firstDot >= 0 )
return lookupFile( website.substring( firstDot + 1 ), filename );
/* if we are at TLD, check default */
if( website !== "default" )
return lookupFile( "default", filename );
/* nothing in default, just fail */
return null;
}
function lookupFileThrow( website, filename ) {
var filePath = lookupFile( website, filename );
if( filePath == null ) {
var err = new Error( "Not Found: '" + filename + "'" );
err.status = 404;
throw err;
}
return filePath;
}
/* locate by hierarchy: cut subdomains, then check 'default' folder */
function up( website ) {
if( website in routes )
return routes[website];
/* cut next subdomain */
var firstDot = website.indexOf(".");
if( firstDot >= 0 )
return website.substring( firstDot + 1 );
/* if we are at TLD, check default */
if( website !== "default" )
return "default";
/* nothing in default, just fail */
return null;
}
function upParts( website ) {
var parts = [ website ];
var part = website;
while( part = up( part ) )
parts.push( part );
return parts;
}
/* go up until directory exists */
function upExists( website, dir ) {
while( true ) {
website = up( website );
if( website == null )
return null;
var dirPath = path.join( websitesRoot, website, dir || '' );
if( fs.existsSync( dirPath ) )
return website;
}
}
function paths( website, dir ) {
var paths = []
/* prepend dummy to include website itself */
website = "dummy." + website;
while( true ) {
website = upExists( website, dir );
if( website == null )
break;
paths.push( path.join( websitesRoot, website, dir || '' ) );
}
return paths;
}
function websiteDir( site ) {
return path.join( process.cwd(), websitesRoot, website( site ) );
}
/* get website without any subdomain */
function website( website ) {
return upExists( "dummy." + website );
}
/* add custom route to hierarchy */
function addRoute( source, target ) {
routes[ source ] = target;
}
/* stream wrappers */
function createReadStream( website, filename ) {
var filepath = lookupFile( website, filename );
return fs.createReadStream( filepath || "" );
}
function createWriteStream( _website, filename, opts ) {
opts = opts || {};
var filepath = opts.unlockRoot ? path.join( _website, filename ) : path.join( websitesRoot, website( _website ), filename );
return fs.createWriteStream( filepath );
}
function createHideShowFilter( hideFilters, showFilters ) {
return function( name ) {
/* every hide filter must be negative */
if( !_.every( hideFilters || [ /^\./g ], function( hideFilter ) {
hideFilter.lastIndex = 0; // reset regex's internal match offset
return !hideFilter.test( name );
}) )
return false;
/* any show filter must be positive */
return _.some( showFilters || [ /.*/ ], function( showFilter ) {
showFilter.lastIndex = 0;
return showFilter.test( name );
});
};
}
/* check if directory name is allowed i.e. when creating a new directory */
function checkDirname( website, dirname, opts ) {
opts = createDirFileFilters( opts );
var dir = path.normalize( opts.unlockRoot ? path.join( website, dirname ) : path.join( websitesRoot, website, dirname ) );
if( opts.dirnameFilter( dirname )
&& ( opts.unlockRoot || dir.indexOf( websitesRoot ) == 0)
&& (!opts.lockWebsite || dir.indexOf( path.join( websitesRoot, website ) ) == 0 ) )
return dir;
else
return null;
}
function checkFilename( website, filename, opts ) {
opts = createDirFileFilters( opts );
var info = path.parse( filename );
var file = path.normalize( opts.unlockRoot ? path.join( website, filename ) : path.join( websitesRoot, website, filename ) );
if ( opts.dirnameFilter ( info.dir )
&& opts.filenameFilter( info.base )
&& ( opts.unlockRoot || file.indexOf( websitesRoot ) == 0 ) // lock to websites
&& (!opts.lockWebsite || file.indexOf( path.join( websitesRoot, website ) ) == 0 ) ) // lock to given website
return file;
else
return null;
}
function checkFilters( website, filename, opts ) {
opts = createDirFileFilters( opts );
var info = path.parse( filename );
var file = lookupFile( website, filename, opts )
if ( opts.dirnameFilter ( info.dir )
&& opts.filenameFilter( info.base )
&& file != null
&& (!opts.lockWebsite || file.indexOf( path.join( websitesRoot, website ) ) == 0 ) )
return file;
else
return null;
}
function createDirFileFilters( opts ) {
return _.extend( {}, {
dirnameFilter: createHideShowFilter( opts.dirHideFilters, opts.dirShowFilters ),
filenameFilter: createHideShowFilter( opts.fileHideFilters, opts.fileShowFilters )
}, opts );
}
function readTree( opts, callback ) {
var tree = { dirs: {}, files: [] };
opts = createDirFileFilters( opts );
/* queue worker */
var treeQueue = async.queue( function( task, next ) {
/* directory contents */
fs.readdir( task.dirpath, function( err, filenames ) {
if( err )
return next( err );
/* run stat for content */
async.mapSeries( filenames, function( filename, d ) {
var filepath = path.join( task.dirpath, filename );
fs.stat( filepath, function( err, stat ) {
if( err )
return d( err );
//console.log( stat.isDirectory(), task.prefix );
/* spawn new worker for every directory */
if( stat.isDirectory() ) {
var prefix = path.join( task.prefix, filename );
if( opts.dirnameFilter( prefix ) ) {
var newTree = { dirs: {}, files: [], prefix: task.prefix, path: prefix };
task.tree.dirs[ filename ] = newTree;
treeQueue.push( { dirpath: filepath, tree: newTree, prefix: prefix } );
}
}
/* just add file */
else if( opts.dirnameFilter( task.prefix ) && opts.filenameFilter( filename ) ) {
var link = path.join( task.prefix, filename );
task.tree.files.push( { name: filename, link: link } );
}
d();
});
}, next );
});
});
/* all done, callback */
treeQueue.drain = function( err ) {
callback( err, tree );
};
treeQueue.push( { dirpath: opts.dirpath, tree: tree, prefix: opts.prefix || "" } );
}
function readHierarchyTree( website, dirpath, opts, callback ) {
if( typeof callback === "undefined" ) {
callback = opts;
opts = {};
}
var filepath = opts.unlockRoot ? lookupFile( "", dirpath, opts ) : lookupFile( website, dirpath );
if( filepath == null )
return callback( new Error( "No Directory" ) );
opts.dirpath = filepath;
readTree( opts, callback );
}
function flattenTree( tree, opts ) {
var items = [];
/* add files */
if( !opts.foldersOnly )
_.each( tree.files, function( file, name ) {
items.push( file.link );
});
/* add dirs and recurse */
_.each( tree.dirs, function( dir, name ) {
if( !opts.filesOnly )
items.push( dir.path );
items = items.concat( flattenTree( dir, opts ) );
});
return items;
}
function readFlatHierarchyTree( website, dirpath, opts, callback ) {
readHierarchyTree( website, dirpath, opts, function( err, tree ) {
if( err ) return callback( err );
return callback( null, flattenTree( tree, opts ) );
});
}
return {
addRoute: addRoute,
checkDirname: checkDirname,
checkFilename: checkFilename,
checkFilters: checkFilters,
createReadStream: createReadStream,
createWriteStream: createWriteStream,
lookupFile: lookupFile,
lookupFileThrow: lookupFileThrow,
readFlatHierarchyTree: readFlatHierarchyTree,
readTree: readTree,
readHierarchyTree: readHierarchyTree,
up: up,
upParts: upParts,
upExists: upExists,
paths: paths,
website: website,
websiteDir: websiteDir
}
}