1- import express from ' express'
2- import basicAuth from ' express-basic-auth'
3- import http from ' node:http'
4- import { createBareServer } from ' @tomphttp/bare-server-node'
5- import path from ' node:path'
6- import cors from ' cors'
7- import config from ' ./config.js'
8-
9- const __dirname = process . cwd ( )
10- const server = http . createServer ( )
11- const app = express ( server )
12- const bareServer = createBareServer ( ' /o/' )
13- const PORT = process . env . PORT || 8080
1+ import express from " express" ;
2+ import basicAuth from " express-basic-auth" ;
3+ import http from " node:http" ;
4+ import { createBareServer } from " @tomphttp/bare-server-node" ;
5+ import path from " node:path" ;
6+ import cors from " cors" ;
7+ import config from " ./config.js" ;
8+
9+ const __dirname = process . cwd ( ) ;
10+ const server = http . createServer ( ) ;
11+ const app = express ( server ) ;
12+ const bareServer = createBareServer ( " /o/" ) ;
13+ const PORT = process . env . PORT || 8080 ;
1414
1515if ( config . challenge ) {
16- console . log ( ' Password protection is enabled. Usernames are: ' + Object . keys ( config . users ) )
17- console . log ( ' Passwords are: ' + Object . values ( config . users ) )
16+ console . log ( " Password protection is enabled. Usernames are: " + Object . keys ( config . users ) ) ;
17+ console . log ( " Passwords are: " + Object . values ( config . users ) ) ;
1818
1919 app . use (
2020 basicAuth ( {
2121 users : config . users ,
2222 challenge : true ,
2323 } )
24- )
24+ ) ;
2525}
2626
27- app . use ( express . json ( ) )
28- app . use ( express . urlencoded ( { extended : true } ) )
29- app . use ( cors ( ) )
30- app . use ( express . static ( path . join ( __dirname , ' static' ) ) )
27+ app . use ( express . json ( ) ) ;
28+ app . use ( express . urlencoded ( { extended : true } ) ) ;
29+ app . use ( cors ( ) ) ;
30+ app . use ( express . static ( path . join ( __dirname , " static" ) ) ) ;
3131
3232if ( config . routes !== false ) {
3333 const routes = [
34- { path : ' /ap' , file : ' apps.html' } ,
35- { path : '/g' , file : ' games.html' } ,
36- { path : '/s' , file : ' settings.html' } ,
37- { path : '/t' , file : ' tabs.html' } ,
38- { path : '/p' , file : ' go.html' } ,
39- { path : '/' , file : ' index.html' } ,
40- { path : ' /tos' , file : ' tos.html' } ,
41- ]
34+ { path : " /ap" , file : " apps.html" } ,
35+ { path : "/g" , file : " games.html" } ,
36+ { path : "/s" , file : " settings.html" } ,
37+ { path : "/t" , file : " tabs.html" } ,
38+ { path : "/p" , file : " go.html" } ,
39+ { path : "/" , file : " index.html" } ,
40+ { path : " /tos" , file : " tos.html" } ,
41+ ] ;
4242
4343 routes . forEach ( ( route ) => {
4444 app . get ( route . path , ( req , res ) => {
45- res . sendFile ( path . join ( __dirname , ' static' , route . file ) )
46- } )
47- } )
45+ res . sendFile ( path . join ( __dirname , " static" , route . file ) ) ;
46+ } ) ;
47+ } ) ;
4848}
4949
5050if ( config . local !== false ) {
51- app . get ( '/e/*' , ( req , res , next ) => {
52- const baseUrls = [
53- 'https://raw.githubusercontent.com/v-5x/x/fixy' ,
54- 'https://raw.githubusercontent.com/ypxa/y/main' ,
55- 'https://raw.githubusercontent.com/ypxa/w/master' ,
56- ]
57- fetchData ( req , res , next , baseUrls )
58- } )
51+ app . get ( "/e/*" , ( req , res , next ) => {
52+ const baseUrls = [ "https://raw.githubusercontent.com/v-5x/x/fixy" , "https://raw.githubusercontent.com/ypxa/y/main" , "https://raw.githubusercontent.com/ypxa/w/master" ] ;
53+ fetchData ( req , res , next , baseUrls ) ;
54+ } ) ;
5955}
6056
6157const fetchData = async ( req , res , next , baseUrls ) => {
6258 try {
63- const reqTarget = baseUrls . map ( ( baseUrl ) => `${ baseUrl } /${ req . params [ 0 ] } ` )
64- let data
65- let asset
59+ const reqTarget = baseUrls . map ( ( baseUrl ) => `${ baseUrl } /${ req . params [ 0 ] } ` ) ;
60+ let data ;
61+ let asset ;
6662
6763 for ( const target of reqTarget ) {
68- asset = await fetch ( target )
64+ asset = await fetch ( target ) ;
6965 if ( asset . ok ) {
70- data = await asset . arrayBuffer ( )
71- break
66+ data = await asset . arrayBuffer ( ) ;
67+ break ;
7268 }
7369 }
7470
7571 if ( data ) {
76- res . end ( Buffer . from ( data ) )
72+ res . end ( Buffer . from ( data ) ) ;
7773 } else {
78- res . status ( 404 ) . send ( )
74+ res . status ( 404 ) . send ( ) ;
7975 }
8076 } catch ( error ) {
81- console . error ( `Error fetching ${ req . url } :` , error )
82- res . status ( 500 ) . send ( )
77+ console . error ( `Error fetching ${ req . url } :` , error ) ;
78+ res . status ( 500 ) . send ( ) ;
8379 }
84- }
80+ } ;
8581
86- app . get ( '*' , ( req , res ) => {
82+ app . get ( "*" , ( req , res ) => {
8783 res . status ( 404 ) . send ( ) ;
8884} ) ;
8985
@@ -92,26 +88,26 @@ app.use((err, req, res, next) => {
9288 res . status ( 500 ) . send ( ) ;
9389} ) ;
9490
95- server . on ( ' request' , ( req , res ) => {
91+ server . on ( " request" , ( req , res ) => {
9692 if ( bareServer . shouldRoute ( req ) ) {
97- bareServer . routeRequest ( req , res )
93+ bareServer . routeRequest ( req , res ) ;
9894 } else {
99- app ( req , res )
95+ app ( req , res ) ;
10096 }
101- } )
97+ } ) ;
10298
103- server . on ( ' upgrade' , ( req , socket , head ) => {
99+ server . on ( " upgrade" , ( req , socket , head ) => {
104100 if ( bareServer . shouldRoute ( req ) ) {
105- bareServer . routeUpgrade ( req , socket , head )
101+ bareServer . routeUpgrade ( req , socket , head ) ;
106102 } else {
107- socket . end ( )
103+ socket . end ( ) ;
108104 }
109- } )
105+ } ) ;
110106
111- server . on ( ' listening' , ( ) => {
112- console . log ( `Running at http://localhost:${ PORT } ` )
113- } )
107+ server . on ( " listening" , ( ) => {
108+ console . log ( `Running at http://localhost:${ PORT } ` ) ;
109+ } ) ;
114110
115111server . listen ( {
116112 port : PORT ,
117- } )
113+ } ) ;
0 commit comments