forked from shimondoodkin/nodejs-mongodb-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachedfolder_page.js
executable file
·339 lines (328 loc) · 10.8 KB
/
cachedfolder_page.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
var fs = require('fs'); // allaws to open files
var sys=require('sys');
var Buffer=require('buffer').Buffer;
function recource_dir(parse_file_list_dirname,addcallback)
{
var parse_file_list1 = function(dir, files)
{
for (var i=0;i<files.length;i++)
{
var file = dir+'/'+files[i];
(function()
{
var file_on_callback = file;
//sys.puts('file assigned: '+ file_on_callback);
fs.stat(file_on_callback,
function(err,stats)
{
//sys.puts('stats returned: '+ file);
if (err)
{
// do nothing
// sometimes linked files are missing
//sys.puts('auto restart - cannot read file1 : '+ file_on_callback);
}
else
{
if (stats.isDirectory())
fs.readdir(file_on_callback,function(err,files){
if (err)
{
// do nothing
// sometimes linked files are missing
//sys.puts('auto restart - cannot read file2 : '+ file_on_callback);
}
else
parse_file_list1(file_on_callback, files);
});
else if (stats.isFile() )
{
addcallback(file_on_callback);
//eval("f= function(){restart_server('"+file_on_callback+"');};");fs.watchFile(file_on_callback, f); //probably may consume resources , but also tells whitch file
//fs.watchFile(file_on_callback, restart_server); //this one consumes less resiurces
}
}
});
}
)(); // This creates and executes a new function with its own scope.
}
};
fs.readdir(parse_file_list_dirname,function(err,files)
{
if (err)
{
// do nothing
// sometimes linked files are missing
//sys.puts('auto restart -cannot read file3: '+ parse_file_list_dirname);
}
else
parse_file_list1(parse_file_list_dirname, files);
});
}
var contentTypes = { // types from node paperboy
"aiff": "audio/x-aiff",
"arj": "application/x-arj-compressed",
"asf": "video/x-ms-asf",
"asx": "video/x-ms-asx",
"au": "audio/ulaw",
"avi": "video/x-msvideo",
"bcpio": "application/x-bcpio",
"ccad": "application/clariscad",
"cod": "application/vnd.rim.cod",
"com": "application/x-msdos-program",
"cpio": "application/x-cpio",
"cpt": "application/mac-compactpro",
"csh": "application/x-csh",
"css": "text/css",
"deb": "application/x-debian-package",
"dl": "video/dl",
"doc": "application/msword",
"drw": "application/drafting",
"dvi": "application/x-dvi",
"dwg": "application/acad",
"dxf": "application/dxf",
"dxr": "application/x-director",
"etx": "text/x-setext",
"ez": "application/andrew-inset",
"fli": "video/x-fli",
"flv": "video/x-flv",
"gif": "image/gif",
"gl": "video/gl",
"gtar": "application/x-gtar",
"gz": "application/x-gzip",
"hdf": "application/x-hdf",
"hqx": "application/mac-binhex40",
"html": "text/html",
"ico": "image/x-icon",
"ice": "x-conference/x-cooltalk",
"ief": "image/ief",
"igs": "model/iges",
"ips": "application/x-ipscript",
"ipx": "application/x-ipix",
"jad": "text/vnd.sun.j2me.app-descriptor",
"jar": "application/java-archive",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "text/javascript",
"json": "application/json",
"latex": "application/x-latex",
"lsp": "application/x-lisp",
"lzh": "application/octet-stream",
"m": "text/plain",
"m3u": "audio/x-mpegurl",
"man": "application/x-troff-man",
"me": "application/x-troff-me",
"midi": "audio/midi",
"mif": "application/x-mif",
"mime": "www/mime",
"movie": "video/x-sgi-movie",
"mp4": "video/mp4",
"mpg": "video/mpeg",
"mpga": "audio/mpeg",
"ms": "application/x-troff-ms",
"nc": "application/x-netcdf",
"oda": "application/oda",
"ogm": "application/ogg",
"pbm": "image/x-portable-bitmap",
"pdf": "application/pdf",
"pgm": "image/x-portable-graymap",
"pgn": "application/x-chess-pgn",
"pgp": "application/pgp",
"pm": "application/x-perl",
"png": "image/png",
"pnm": "image/x-portable-anymap",
"ppm": "image/x-portable-pixmap",
"ppz": "application/vnd.ms-powerpoint",
"pre": "application/x-freelance",
"prt": "application/pro_eng",
"ps": "application/postscript",
"qt": "video/quicktime",
"ra": "audio/x-realaudio",
"rar": "application/x-rar-compressed",
"ras": "image/x-cmu-raster",
"rgb": "image/x-rgb",
"rm": "audio/x-pn-realaudio",
"rpm": "audio/x-pn-realaudio-plugin",
"rtf": "text/rtf",
"rtx": "text/richtext",
"scm": "application/x-lotusscreencam",
"set": "application/set",
"sgml": "text/sgml",
"sh": "application/x-sh",
"shar": "application/x-shar",
"silo": "model/mesh",
"sit": "application/x-stuffit",
"skt": "application/x-koan",
"smil": "application/smil",
"snd": "audio/basic",
"sol": "application/solids",
"spl": "application/x-futuresplash",
"src": "application/x-wais-source",
"stl": "application/SLA",
"stp": "application/STEP",
"sv4cpio": "application/x-sv4cpio",
"sv4crc": "application/x-sv4crc",
"swf": "application/x-shockwave-flash",
"tar": "application/x-tar",
"tcl": "application/x-tcl",
"tex": "application/x-tex",
"texinfo": "application/x-texinfo",
"tgz": "application/x-tar-gz",
"tiff": "image/tiff",
"tr": "application/x-troff",
"tsi": "audio/TSP-audio",
"tsp": "application/dsptype",
"tsv": "text/tab-separated-values",
"txt": "text/plain",
"unv": "application/i-deas",
"ustar": "application/x-ustar",
"vcd": "application/x-cdlink",
"vda": "application/vda",
"vivo": "video/vnd.vivo",
"vrm": "x-world/x-vrml",
"wav": "audio/x-wav",
"wax": "audio/x-ms-wax",
"wma": "audio/x-ms-wma",
"wmv": "video/x-ms-wmv",
"wmx": "video/x-ms-wmx",
"wrl": "model/vrml",
"wvx": "video/x-ms-wvx",
"xbm": "image/x-xbitmap",
"xlw": "application/vnd.ms-excel",
"xml": "text/xml",
"xpm": "image/x-xpixmap",
"xwd": "image/x-xwindowdump",
"xyz": "chemical/x-pdb",
"zip": "application/zip"
};
var charsets = {
'text/javascript': 'UTF-8',
'text/html': 'UTF-8',
'text/css': 'UTF-8'
};
this.page=function(app,url,dirpath,reg_include,reg_exclude,hotreload,debugit)
{
if(typeof debugit==='undefined')debugit=false;
var page=
{
pagefilename:__filename,
pageurl:url, //prefix name
urlmatch:'pathbegins', //urlmatch: path / pathbegins
load_templates:{}, //dummy
prepeare_templates: {}, //dummy
prepere_data:function (page,template_name,callback){}, //dummy
etag:(new Date).getTime() , //my etag is the last time i loaded the application - last cache
main:function (shared,route_i)
{
var req=shared.req,res=shared.res,page=shared.page;
//sys.puts('cachefile page main ');
if(false&&req.headers['if-none-match'] == this.etag)
{
res.writeHead(304, { 'Content-Length': 0 });
res.end();
}
else
{
var relative=req.parsedurl.pathname.substring(this.pageurl.length+2,req.parsedurl.pathname.length);
if(typeof this.files[relative]==='undefined')
{
// res.writeHead(404, { 'Content-Type': 'text/html', 'ETag':this.etag, 'Cache-control': 'max-age=172800', 'Expires': expires.toUTCString(), });
// res.write( "Not Found" );
// sys.puts('cachefile page ' + url + ' relative ' + relative + ' file not found... "' + req.parsedurl.pathname + '"');
// res.end();
return false;
}
else
{
//sys.puts('cachefile page ' + url + ' relative ' + relative + ' served... "' + req.parsedurl.pathname + '"');
var file=this.files[relative];
var expires = new Date;
expires.setTime( expires.getTime() + -172800 ); // two days in seconds 172800
res.writeHead(200, { 'Content-Type': file[0] , 'Content-Length': file[1], 'ETag':this.etag, 'Cache-control': 'max-age=0, must-revalidate', 'Expires': expires.toUTCString() });
res.write( file[2] );
res.end();
delete expires;
}
}
return true;
},
files:
{
},
};
var dir=__dirname+'/'+dirpath,dirlen=dir.length+1;
recource_dir(dir,
function (filename)
{
var relateive=filename.substring(dirlen,filename.length),reg_inc=(reg_include!=false),reg_exc=(reg_exclude!=false);
if (reg_exc?reg_exclude.test(relateive):false) // if regex not defined ignore it
{
//if(debugit)sys.puts('cache folder: not loading '+relateive);
return;
}
if (reg_inc?reg_include.test(relateive):true) // if regex not defined ignore it
{
process.nextTick(function ()
{
fs.readFile(filename, function (err, data)
{
if (err)
{
//sys.puts('error loading ' + relateive + ' ' + err);
err=null;
return;
}
else
{
var ext=relateive.substring(relateive.lastIndexOf('.')+1,relateive.length);
var contenttype=contentTypes[ext] || 'application/octet-stream';
var charset=charsets[contenttype]?charsets[contenttype]:false;
//if(debugit) sys.puts('cache folder: loading '+relateive);
page.files[relateive] = [
contenttype+(charset?'; charset=' + charset:''), //0
data.length, //1
data//, //2
//ext, //
];
//page.files[relateive].push(page.files[relateive].length);
}
});
if(hotreload)
{
//console.log((new Date).toString()+' watch cached file: '+filename);
fs.watchFile(filename, function ()
{
console.log((new Date).toString()+' reload cached file: '+filename);
page.etag=(new Date).getTime() , //my etag is the last time i loaded the application - last cache
// it is change etag and the function that above
fs.readFile(filename, function (err, data)
{
if (err)
{
//sys.puts('error loading ' + relateive + ' ' + err);
err=null;
return;
}
else
{
var ext=relateive.substring(relateive.lastIndexOf('.')+1,relateive.length);
var contenttype=contentTypes[ext] || 'application/octet-stream';
var charset=charsets[contenttype]?charsets[contenttype]:false;
//sys.puts('cache folder: loading '+relateive);
page.files[relateive] = [
contenttype+(charset?'; charset=' + charset:''), //0
data.length, //1
data//, //2
//ext, //
];
//page.files[relateive].push(page.files[relateive].length);
}
});
});
}
});
}
}
);
return page;
} ;