1616
1717 var versionNumber = require ( 'electron' ) . remote . getGlobal ( 'sharedObject' ) . appVersionNumber ;
1818
19- console . log ( "DatPart Server Version " + versionNumber ) ;
19+ logToScreen ( "DatPart Server Version " + versionNumber ) ;
2020
2121 var datMap = { } ;
2222
6666 "otf" : "font/opentype" ,
6767 "eot" : "application/vnd.ms-fontobject" ,
6868 //video formats
69- "av1" : "video/av1" ,
7069 "webm" : "video/webm" ,
7170 "mp4" : "video/mp4" ,
7271 "m4v" : "video/m4v" ,
160159 }
161160
162161const requestHandler = ( request , response ) => {
163- console . log ( request . url ) ;
164- console . log ( request ) ;
162+ logToScreen ( request . url ) ;
163+ logToScreen ( request ) ;
165164
166165 var uri = url . parse ( request . url ) . pathname ,
167166 filename = path . join ( process . cwd ( ) , uri ) ;
@@ -178,20 +177,21 @@ const requestHandler = (request, response) => {
178177
179178 var datPath = url . parse ( request . url ) . pathname ;
180179
181- console . log ( datPath ) ;
180+ logToScreen ( datPath ) ;
182181
183182if ( request . method == 'GET' && currentTLD == 'dat_site' || request . method == 'GET' && currentTLD == 'datsite' ) {
184183 datMap [ currentURLhostNoTLD ] = { } ;
185184dat ( __dirname + '/../../dats/' + currentURLhostNoTLD , {
186185 // 2. Tell Dat what link I want
187186 key : currentURLhostNoTLD , temp : true , sparse : true // (a 64 character hash from above)
188187} , function ( err , dat ) {
189- if ( err ) { throw err ; console . log ( err ) ; }
188+ if ( err ) { throw err ; logToScreen ( err ) ; }
190189
191190 var stats = dat . trackStats ( ) ;
192- console . log ( currentURLhostNoTLD + " " + dat . stats . get ( ) ) ;
193- console . log ( dat . stats . get ( ) ) ;
194- console . log ( "Dat Version: " + dat . stats . get ( ) . version ) ;
191+ logToScreen ( currentURLhostNoTLD + " " + dat . stats . get ( ) ) ;
192+ logToScreen ( dat . stats . get ( ) ) ;
193+ logToScreen ( "Dat Version: " + dat . stats . get ( ) . version ) ;
194+ logToScreen ( "Number of files: " + dat . stats . get ( ) . files ) ;
195195
196196 var fourOhFourPage = null ;
197197
@@ -215,70 +215,70 @@ dat( __dirname + '/../../dats/'+currentURLhostNoTLD, {
215215 /*
216216 dat.archive.readFile(datPath+'/dat.json', function (err, content) {
217217 if (content != null) {
218- console.log ("Got dat.json for "+ currentURLhostNoTLD);
219- console.log (datMap[currentURLhostNoTLD]);
218+ logToScreen ("Got dat.json for "+ currentURLhostNoTLD);
219+ logToScreen (datMap[currentURLhostNoTLD]);
220220
221221 if (JSON.parse(content.toString()).content_security_policy != null || JSON.parse(content.toString()).content_security_policy != undefined) {
222222 datMap[currentURLhostNoTLD].contentSecurityPolicy = JSON.parse(content.toString()).content_security_policy;
223- console.log (JSON.parse(content.toString()).content_security_policy);
223+ logToScreen (JSON.parse(content.toString()).content_security_policy);
224224 } else {
225225 datMap[currentURLhostNoTLD].contentSecurityPolicy = "";
226226 }
227227
228228 if (JSON.parse(content.toString()).fallback_page != null || JSON.parse(content.toString()).fallback_page != undefined) {
229229 datMap[currentURLhostNoTLD].fallback_page = JSON.parse(content.toString()).fallback_page;
230- console.log (JSON.parse(content.toString()).fallback_page);
230+ logToScreen (JSON.parse(content.toString()).fallback_page);
231231 } else {
232232 datMap[currentURLhostNoTLD].fallback_page = "";
233233 }
234234
235235 } else {
236- console.log ("dat.json not found for "+ currentURLhostNoTLD);
236+ logToScreen ("dat.json not found for "+ currentURLhostNoTLD);
237237
238238 }
239- if (err) {throw err; console.log (err);}
239+ if (err) {throw err; logToScreen (err);}
240240 });
241241 */
242242
243243/*
244244 datMap[currentURLhostNoTLD].fourOhFourFallback = new Promise(function(resolve, reject) {
245245 dat.archive.readFile('/dat.json', function (err, content) {
246246
247- console.log ("Grabbing JSON for fallback_page "+JSON.stringify(content));
247+ logToScreen ("Grabbing JSON for fallback_page "+JSON.stringify(content));
248248
249249 dat.archive.readFile(content.fallback_page, function (err, fallbackContent) {
250250 if (fallbackContent != null) {
251- console.log ("Got fallback page for dat://"+ currentURLhostNoTLD +" " + content.fallback_page);
251+ logToScreen ("Got fallback page for dat://"+ currentURLhostNoTLD +" " + content.fallback_page);
252252 resolve(fallbackContent);
253253 } else {
254- console.log ("fallback_page not found for dat://"+ currentURLhostNoTLD);
254+ logToScreen ("fallback_page not found for dat://"+ currentURLhostNoTLD);
255255 reject("fallback_page not found for dat://"+ currentURLhostNoTLD);
256256 }
257257 });
258258
259259 }, function(err) {
260- console.log (err);
260+ logToScreen (err);
261261 });
262262 });
263263 */
264264/*
265265 datMap[currentURLhostNoTLD].contentSecurityPolicy = new Promise(function(resolve, reject) {
266266 datMap[currentURLhostNoTLD].datJSON.then(function(result) {
267267
268- console.log ("Grabbing JSON for content_security_policy "+JSON.stringify(result));
268+ logToScreen ("Grabbing JSON for content_security_policy "+JSON.stringify(result));
269269
270270 dat.archive.readFile(result.content_security_policy, function (err, cspContent) {
271271 if (cspContent != null) {
272- console.log ("Got fallback page for dat://"+ currentURLhostNoTLD +" " + result.content_security_policy);
272+ logToScreen ("Got fallback page for dat://"+ currentURLhostNoTLD +" " + result.content_security_policy);
273273 resolve(cspContent);
274274 } else {
275- console.log ("content_security_policy not found for dat://"+ currentURLhostNoTLD);
275+ logToScreen ("content_security_policy not found for dat://"+ currentURLhostNoTLD);
276276 reject("content_security_policy not found for dat://"+ currentURLhostNoTLD);
277277 }
278278 });
279279
280280 }, function(err) {
281- console.log (err);
281+ logToScreen (err);
282282 });
283283 });
284284*/
@@ -288,7 +288,7 @@ if (lastChar == '/') { // If the last character is not a slash
288288 dat . archive . readFile ( datPath + '/index.html' , function ( err , content ) {
289289
290290 if ( content != null ) {
291- console . log ( datPath ) ;
291+ logToScreen ( datPath ) ;
292292 var newHeaders = HTTPheaders ;
293293 delete newHeaders [ "X-Frame-Options" ] ;
294294 delete newHeaders [ "Location" ] ;
@@ -302,7 +302,7 @@ if (lastChar == '/') { // If the last character is not a slash
302302 response . writeHead ( 200 , newHeaders ) ;
303303 response . end ( content ) ;
304304 } else {
305- console . log ( "File " + datPath + " not found!" ) ;
305+ logToScreen ( "File " + datPath + " not found!" ) ;
306306
307307 var newHeaders = HTTPheaders ;
308308 delete newHeaders [ "X-Frame-Options" ] ;
@@ -311,7 +311,7 @@ if (lastChar == '/') { // If the last character is not a slash
311311 newHeaders [ "X-Frame-Options" ] = "DENY" ;
312312
313313 if ( datMap [ currentURLhostNoTLD ] . fourOhFourFallback != null ) {
314- console . log ( result ) ;
314+ logToScreen ( result ) ;
315315
316316 newHeaders [ "Alt-Svc" ] = "dat='dat://" + currentURLhostNoTLD + datPath + "'" ;
317317 newHeaders [ "Dat-Url" ] = "dat://" + currentURLhostNoTLD + datPath ;
@@ -321,21 +321,21 @@ if (lastChar == '/') { // If the last character is not a slash
321321 response . writeHead ( 404 , newHeaders ) ;
322322 response . end ( result ) ;
323323 } else {
324- console . log ( err ) ;
324+ logToScreen ( err ) ;
325325 response . writeHead ( 204 , newHeaders ) ;
326326 response . end ( "Nothing" ) ;
327327 }
328328
329329 }
330- if ( err ) { throw err ; console . log ( err ) ; }
330+ if ( err ) { throw err ; logToScreen ( err ) ; }
331331 } ) ;
332332
333333} else {
334334
335335 dat . archive . readFile ( datPath , function ( err , content ) {
336336
337337 if ( content != null ) {
338- console . log ( datPath ) ;
338+ logToScreen ( datPath ) ;
339339
340340 var newHeaders = HTTPheaders ;
341341 delete newHeaders [ "X-Frame-Options" ] ;
@@ -354,7 +354,7 @@ if (lastChar == '/') { // If the last character is not a slash
354354 response . writeHead ( 200 , newHeaders ) ;
355355 response . end ( content ) ;
356356 } else {
357- console . log ( "File " + datPath + " not found!" ) ;
357+ logToScreen ( "File " + datPath + " not found!" ) ;
358358
359359 var newHeaders = HTTPheaders ;
360360 delete newHeaders [ "X-Frame-Options" ] ;
@@ -363,7 +363,7 @@ if (lastChar == '/') { // If the last character is not a slash
363363 newHeaders [ "X-Frame-Options" ] = "DENY" ;
364364
365365 if ( datMap [ currentURLhostNoTLD ] . fourOhFourFallback != null ) {
366- console . log ( result ) ;
366+ logToScreen ( result ) ;
367367
368368 newHeaders [ "Alt-Svc" ] = "dat='dat://" + currentURLhostNoTLD + datPath + "'" ;
369369 newHeaders [ "Dat-Url" ] = "dat://" + currentURLhostNoTLD + datPath ;
@@ -373,13 +373,13 @@ if (lastChar == '/') { // If the last character is not a slash
373373 response . writeHead ( 404 , newHeaders ) ;
374374 response . end ( result ) ;
375375 } else {
376- console . log ( err ) ;
376+ logToScreen ( err ) ;
377377 response . writeHead ( 204 , newHeaders ) ;
378378 response . end ( "Nothing" ) ;
379379 }
380380
381381 }
382- if ( err ) { throw err ; console . log ( err ) ; }
382+ if ( err ) { throw err ; logToScreen ( err ) ; }
383383 } ) ;
384384
385385}
@@ -390,8 +390,8 @@ if (lastChar == '/') { // If the last character is not a slash
390390
391391} else if ( request . method == 'GET' && currentTLD != 'dat_site' && currentTLD != 'datsite' ) {
392392
393- console . log ( "Non Dat Site request " + request ) ;
394- console . log ( request ) ;
393+ logToScreen ( "Non Dat Site request " + request ) ;
394+ logToScreen ( request ) ;
395395
396396 //var newHeaders;
397397 //newHeaders["location"] = "http://0.0.0.0/";
@@ -400,22 +400,22 @@ if (lastChar == '/') { // If the last character is not a slash
400400
401401} else if ( fs . existsSync ( __dirname + "/../../dats/" ) ) {
402402
403- console . log ( request . url ) ;
403+ logToScreen ( request . url ) ;
404404
405405 var currentTLD = url . parse ( request . url ) . hostname . split ( "." ) . pop ( ) ;
406406
407407 var currentURLhostNoTLD = url . parse ( request . url ) . hostname . split ( "." ) [ 0 ] ;
408408
409409 var datPath = url . parse ( request . url ) . pathname ;
410410
411- console . log ( datPath ) ;
411+ logToScreen ( datPath ) ;
412412
413- console . log ( "TLD: " + currentTLD + " Hash: " + currentURLhostNoTLD ) ;
413+ logToScreen ( "TLD: " + currentTLD + " Hash: " + currentURLhostNoTLD ) ;
414414
415415} else {
416416
417417 fs . mkdirSync ( appPath + "/dats/" ) ;
418- console . log ( request . url ) ;
418+ logToScreen ( request . url ) ;
419419
420420}
421421
@@ -425,8 +425,8 @@ const server = http.createServer(requestHandler);
425425
426426server . listen ( port , ( err ) => {
427427 if ( err ) {
428- return console . log ( 'something bad happened' , err ) ;
428+ return logToScreen ( 'something bad happened' , err ) ;
429429 }
430430
431- console . log ( `server is listening on ${ port } ` ) ;
431+ logToScreen ( `server is listening on ${ port } ` ) ;
432432} ) ;
0 commit comments