+const dlog = (msg) => console.log(msg)
+
+exports = {
+ /** Regular expression for valid routes
+ * @var {Object.RegEx} validRoutes - matches valid route names
+ */
+ validRoutes: /[a-zA-Z][a-zA-Z0-9\-_]*/,
+
+ /** @func
+ * @summary Synchronize Reading Multiple Files
+ * @desc reads an array of files into an object, whose keys are the
+ * input filenames, and values are the data read
+ * @arg {string[]} files - array of file names to read
+ * @arg {Object} [readOpts] - options to pass to fs.readFile()
+ */
+ syncReads: (files, readOpts) => new Promise((resolve,reject) => {
+ dlog(`syncReads: ${files}`)
+ let count = 0
+ let results = {}
+ const read_cb = (fileName) => (err, data) => {
+ if (err)
+ reject(err)
+ else
+ results[fileName] = data
+ if (++count == files.length)
+ resolve(results)
+ }
+ if (readOpts == undefined)
+ readOpts = { encoding: 'utf8' }
+ files.forEach((file) => fs.readFile(file, readOpts, read_cb(file)))
+ }),
+
+ /** @func
+ * @summary Main router listener
+ * @desc listens for client requests and services routes/files
+ * @arg {http.ClientRequest} request
+ * @arg {http.ServerResponse} response
+ */
+ listener: function (request,response) {
+ dlog(`Received request ${request.method} ${request.url}`)
+ const htArgv = request.url.slice(1).split('?')
+ let routePath = htArgv[0].split('/')[0]
+ let routeName = routePath[0]
+ let route = this.routes[routeName]
+ if (route) {
+ if (route.host == (request.headers['x-forwarded-for'] ||
+ request.connection.remoteAddress))
+ this.serveHost(response, route, htArgv)
+ else
+ this.serveRoute(response, route)
+ }
+ else if (this.validRoutes.test(routeName)) {
+ route = this.createRoute(routeName, this.httpsOpts)
+ this.serveHost(response, route, htArgv)
+ }
+ else {
+ this.serveFile(response, routePath)
+ }
+ },
+
+ /** @func
+ * @summary Serve a route to an http client
+ * @desc routes may be bound to the filesystem, or to an outgoing host
+ * @arg {http.ServerResponse} response - response object to use
+ * @arg {string} routeName - name of the route to follow
+ */
+ serveRoute: function (response, routeName) {
+ },
+
+ /** @func
+ * @summary Create a new route
+ * @desc makes a new route for the given route name
+ * @arg {string} routeName - name of the new route
+ * @arg {string} host - Origin address from the request that made this
+ * route (for security verification on the socket)
+ * @arg {Object} [httpsOpts] - key and cert for tls
+ */
+ createRoute: function (routeName, host, httpsOpts) {
+ dlog(`Creating ${httpsOpts ? 'TLS ' : ''}route ${routeName} from ${host}`)
+ if (routeName in this.routes)
+ throw new Error(`route ${routeName} already exists`)
+ const httpd = httpsOpts
+ ? require('https').createServer(httpsOpts)
+ : require('http').createServer()
+ const wsd = new require('ws').Server({
+ server: httpd,
+ verifyClient: (info) => info.origin == host && (info.secure || !httpsOpts)
+ })
+ const route = {
+ pendingResponses: new Map([]),
+ host: host,
+ httpd: httpd,
+ wsd: wsd,
+ name: routeName,
+ socket: undefined
+ }
+ wsd.on('connection', (socket) =>
+ socket.on('message', (msg) =>
+ this.hostMessage(msg,route)))
+ this.routes[routeName] = route
+ return route
+ },
+
+ /** @func
+ * @summary handle host message
+ * @desc receives a message from a host, handles the command (first character),
+ * and responds to either the host or the client, or both. Commands
+ * are whitespace separated strings.
+ * Commands:
+ * < clientKey requestID payload
+ * Route 'payload' to the client identified by 'clientKey', in
+ * response to the request identified by 'requestID'
+ * ! errorMessage errorCode [offendingMessage]
+ * Notify host that an error has occured, providing a message
+ * and error code. 'offendingMessage', if present, is the
+ * message received from the remote that triggered the error.
+ * @arg {string} message - raw string from the host
+ * @arg {Object} route - the route over
+ */
+ hostMessage: function (message, route) {
+ const argv = message.split(' ')
+ const command = argv[0][0]
+ argv = argv.slice(1)
+ dlog(`Received host message from ${route.name}: ${command}`)
+ switch (command) {
+ case '<':
+ if (argv.length == 3) {
+ const response = route.pendingResponses.get(argv[0] + argv[1])
+ response.writeHead(200, { 'Content-Type': 'application/octet-stream' })
+ response.write(argv[2])
+ response.end()
+ }
+ else {
+ route.socket.send(`! "Insufficient arguments" 0 ${message}`)
+ }
+ break
+ case '!':
+ if (argv.length == 3)
+ argv[0] += `\nIn message: ${argv[2]}`
+ console.log(`Error[${route.host}|${argv[1]}]:${argv[0]}`)
+ break
+ }
+ },
+
+ /** @func
+ * @summary Serve a file to an http client after a request
+ * @desc reads files from the system to be distributed to clients, and
+ * buffers recently accessed files
+ * @arg {http.ServerResponse} response - the response object to use
+ * @arg {string} filePath - location of the file on disk to service
+ */
+ serveFile: function (response, filePath) {
+ },
+
+ /** @func
+ * @summary Start main HTTP server
+ * @desc starts up an HTTP or HTTPS server used for routing
+ * @arg {number|string} port - local system port to bind to
+ * @arg {Object} [tls] - if present, startHttpServer will start in tls
+ * mode. supported properties:
+ * 'certfile': certificate file location
+ * 'keyfile': key file location
+ */
+ startHttpServer: function (port, tls) {
+ if ('httpd' in this)
+ throw new Error('httpd already running')
+ if (tls == undefined)
+ this.httpd = require('http').createServer(this.listener)
+ else if (!('key' in tls) || !('cert' in tls))
+ throw new Error('HTTPS requires a valid key and cert')
+ else
+ this.syncReads([tls.keyfile, tls.certfile]).then((results) => {
+ Object.defineProperty(this, 'httpsOpts', {
+ value: {
+ key: results[tls.keyfile],
+ cert: results[tls.certfile]
+ }
+ })
+ this.httpd = require('https').createServer(httpsOpts, this.listener)
+ })
+ this.httpd.listen(port)
+ console.log(`HTTP${(tls == undefined) ? 'S' : ''} ` +
+ `Server Started on Port ${port}`)