11import { file , type Server } from "bun" ;
22import { parse as parseToml } from "@iarna/toml" ;
3+ import JSON5 from "json5" ;
34import { join , resolve } from "path" ;
4- import { existsSync , statSync } from "fs" ;
55import type { XmitConfig } from "./config" ;
66import { v4 as uuidv4 } from "./uuid" ;
77
@@ -11,14 +11,10 @@ interface ServeOptions {
1111 hostname ?: string ;
1212}
1313
14- function openFile ( path : string ) : string | null {
15- try {
16- const stat = statSync ( path ) ;
17- if ( ! stat . isDirectory ( ) ) {
18- return path ;
19- }
20- } catch {
21- // File doesn't exist or can't be accessed
14+ async function openFile ( path : string ) : Promise < string | null > {
15+ const f = file ( path ) ;
16+ if ( await f . exists ( ) && f . size > 0 ) {
17+ return path ;
2218 }
2319 return null ;
2420}
@@ -106,19 +102,25 @@ export async function createServer(options: ServeOptions): Promise<Server> {
106102 port,
107103 hostname,
108104 async fetch ( request : Request ) : Promise < Response > {
109- // Load xmit.toml configuration
105+ // Load xmit.json (JSON5) or xmit. toml configuration
110106 let config : XmitConfig = { } ;
111- const cfgPath = join ( absDirectory , "xmit.toml" ) ;
107+ const jsonPath = join ( absDirectory , "xmit.json" ) ;
108+ const tomlPath = join ( absDirectory , "xmit.toml" ) ;
109+
110+ const jsonFile = file ( jsonPath ) ;
111+ const tomlFile = file ( tomlPath ) ;
112112
113- try {
114- const cfgFile = file ( cfgPath ) ;
115- if ( await cfgFile . exists ( ) ) {
116- const cfgText = await cfgFile . text ( ) ;
117- config = parseToml ( cfgText ) as XmitConfig ;
113+ if ( await jsonFile . exists ( ) ) {
114+ try {
115+ config = JSON5 . parse ( await jsonFile . text ( ) ) as XmitConfig ;
116+ } catch ( err ) {
117+ console . warn ( `⚠️ ${ jsonPath } : ${ err } ` ) ;
118118 }
119- } catch ( err ) {
120- if ( existsSync ( cfgPath ) ) {
121- console . warn ( `⚠️ ${ cfgPath } : ${ err } ` ) ;
119+ } else if ( await tomlFile . exists ( ) ) {
120+ try {
121+ config = parseToml ( await tomlFile . text ( ) ) as XmitConfig ;
122+ } catch ( err ) {
123+ console . warn ( `⚠️ ${ tomlPath } : ${ err } ` ) ;
122124 }
123125 }
124126
@@ -197,18 +199,18 @@ export async function createServer(options: ServeOptions): Promise<Server> {
197199 const path = url . pathname . slice ( 1 ) ; // Remove leading /
198200 const originalPath = join ( absDirectory , path ) ;
199201 let realPath = originalPath ;
200- let foundFile = openFile ( realPath ) ;
202+ let foundFile = await openFile ( realPath ) ;
201203
202204 // Try index.html in directory
203205 if ( ! foundFile ) {
204206 realPath = join ( originalPath , "index.html" ) ;
205- foundFile = openFile ( realPath ) ;
207+ foundFile = await openFile ( realPath ) ;
206208 }
207209
208210 // Try .html extension
209211 if ( ! foundFile ) {
210212 realPath = originalPath + ".html" ;
211- foundFile = openFile ( realPath ) ;
213+ foundFile = await openFile ( realPath ) ;
212214 }
213215
214216 // Try redirects
@@ -230,15 +232,15 @@ export async function createServer(options: ServeOptions): Promise<Server> {
230232 // Try fallback
231233 if ( ! foundFile && config . fallback ) {
232234 realPath = join ( absDirectory , config . fallback ) ;
233- foundFile = openFile ( realPath ) ;
235+ foundFile = await openFile ( realPath ) ;
234236 }
235237
236238 // Try 404 page
237239 if ( ! foundFile ) {
238240 status = 404 ;
239241 if ( config [ "404" ] ) {
240242 realPath = join ( absDirectory , config [ "404" ] ) ;
241- foundFile = openFile ( realPath ) ;
243+ foundFile = await openFile ( realPath ) ;
242244 }
243245 }
244246
0 commit comments