import * as http from "http" ; import * as url from "url" ; import * as fs from "fs" ; import * as path from "path" ; import { format , promisify } from "util" ; import * as JSON5 from "json5" ; import * as send from "send" ; import * as WebSocket from "ws" ; import { Stats , appendFileSync } from "fs" ; import { gzip , createGzip } from "zlib" ; import { Writable , Stream } from "stream" ; import * as https from "https" ; import { networkInterfaces , NetworkInterfaceInfo } from "os" ; import * as ipcalc from "./ipcalc" ; import { NewTreeOptions , NewTreePathOptions_Auth , NewTreePathOptions_Index , ServerConfig , ServerConfigBase , ServerConfigSchema , ServerConfig_AccessOptions , ServerConfig_BindInfo , normalizeSettings , ConvertSettings , NewTreeOptionsObject , Config , OptionsConfig , } from "./server-config" ; import { checkServerConfigSchema } from "./interface-checker" ; import { StateObject } from "./state-object" ; import { tryParseJSON } from "./utils-functions" ; import { colors } from "./utils" ; type DebugFunc = ( level : number , str : string | NodeJS . ErrnoException , ... args : any [ ] ) => any ; export function loadSettings ( settingsFile , assetsFolder , routeKeys ) { let debug = ( level , str , ... args ) => StateObject . DebugLoggerInner ( level , "startup" , str , args , process . stderr ) ; const settingsString = fs . readFileSync ( settingsFile , "utf8" ) . replace ( /\t/gi , "    " ) . replace ( /\r\n/gi , "\n" ) ; let settingsObjSource = tryParseJSON < ServerConfigSchema > ( settingsString , e => { debug ( 4 , colors . FgRed + "The settings file could not be parsed: %s" + colors . Reset , e . originalError . message ) ; debug ( 4 , e . errorPosition ) ; throw "The settings file could not be parsed: Invalid JSON" ; } ) ; let [ sourceOK , sourceErrors ] = checkServerConfigSchema ( settingsObjSource ) ; if ( ! sourceOK ) console . log ( sourceErrors ) ; if ( ! settingsObjSource . tree ) throw "tree is not specified in the settings file" ; let settingshttps = settingsObjSource . bindInfo && settingsObjSource . bindInfo . https ; let settingsObj = normalizeSettings ( settingsObjSource , settingsFile , assetsFolder ) ; if ( ( settingsObjSource as any ) . logging ) { debug ( 4 , "Logging to file is no longer supported. Please remove the logging property from your config file. The debugLevel property is now a top level property (sibling to the tree property)." ) ; throw "Logging to file is no longer supported" ; } if ( typeof settingsObj . tree === "object" ) { let keys = [ ] ; settingsObj . tree ; let conflict = keys . filter ( k => routeKeys . indexOf ( k ) > - 1 ) ; if ( conflict . length ) debug ( 2 , "The following tree items are reserved for use by TiddlyServer: %s" , conflict . map ( e => '"' + e + '"' ) . join ( ", " ) ) ; } return { settings : settingsObj , settingshttps } ; }