X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=blobdiff_plain;f=router.js;h=35a2433b9b1c35169a095469304c8fbc7001f844;hp=9d283f60b7c1a78a6e4b2f87bdb19e8c62c7dde7;hb=1d35c77069d852bb59753559d56e2183275b1df4;hpb=0cdde9df5603fd5ecc4c5c37e813735bf9955d9b diff --git a/router.js b/router.js index 9d283f6..35a2433 100644 --- a/router.js +++ b/router.js @@ -15,6 +15,11 @@ exports = { */ validRoutes: /[a-zA-Z][a-zA-Z0-9\-_]*/, + /** A map of routes + * @prop {Object.Map} routes - all the routes! + */ + routes: {}, + /** Parameters set on bootup (startHttpServer) * @prop {string[2]} skelPage - html document split in twain for JS injection * @prop {string} clientJS - jerverscripps to inject in skelPage for clients @@ -28,8 +33,6 @@ exports = { httpdRoot: undefined, bindJail: undefined, - - /** @func * @summary Start main HTTP server * @desc starts up an HTTP or HTTPS server used for routing @@ -61,13 +64,14 @@ exports = { }) this.httpd = require('https').createServer(this.httpsOpts, this.httpdListener) + .listen(conf.port) }) - this.httpd.listen(conf.port) - this.httpdRoot = - conf.httpdRoot ? require('path').normalize(conf.httpdRoot) : undefined - while (this.httpdRoot[this.httpdRoot.length - 1] == require('path').sep) - this.httpdRoot = this.httpdRoot.slice(0,-1) - this.syncReads(conf.skelFile, conf.clientJS, conf.hostJS) + if (conf.httpdRoot) { + this.httpdRoot = require('path').normalize(conf.httpdRoot) + while (this.httpdRoot[this.httpdRoot.length - 1] == require('path').sep) + this.httpdRoot = this.httpdRoot.slice(0,-1) + } + this.syncReads([conf.skelFile, conf.clientJS, conf.hostJS]) .then((results) => { this.skelPage = results[conf.skelFile].split('') this.clientJS = results[conf.clientJS] @@ -99,7 +103,7 @@ exports = { && this.bindJail != path) throw new Error(`${routeName}:${path} jailed to ${this.bindJail}`) if (require('fs').existsSync(path)) { - this.route[routeName] = { + this.routes[routeName] = { bind: { path: path, dir: require('fs').lstatSync(path).isDirectory() @@ -302,12 +306,19 @@ exports = { * and responds to either the host or the client, or both. Commands * are whitespace separated strings. * Commands: + * Forward Payload to Client) * < clientKey payload [header] * Route 'payload' to the client identified by 'clientKey'. * The optional 'header' argument is a stringified JSON object, * which will be written to the HTTP response header * In case of multiple requests from a single client, the * oldest request will be serviced on arrival of message + * Translate SDP and Forward to Client) + * ^ clientKey sdp [header] + * Route the JSON object 'sdp' to the client, after translating + * for interop between browsers using planB or Unified. Other + * than the interop step, this is identical to the '<' command + * Error) * ! errorMessage errorCode [offendingMessage] * Notify host that an error has occured, providing a message * and error code. 'offendingMessage', if present, is the @@ -321,6 +332,17 @@ exports = { argv = argv.slice(1) dlog(`Received host message from ${route.name}: ${command}`) switch (command) { + case '^': + if (argv.length < 2) { + dlog(`Malformed '${command}' command from ${route.host}`) + route.socket.send(`! "Insufficient arguments" 0 ${message}`) + break + } + argv[1] = JSON.parse(argv[1]) + //TODO: interop step + argv[1] = JSON.stringify(argv[1]) + //TODO: argv[1] = encryptForClient(argv[0], argv[1]) + /* Fallthrough to '<' behavior after translating argv[1] */ case '<': const response = route.pendingResponses.get(argv[0]).shift() if (!response) @@ -342,6 +364,10 @@ exports = { argv[0] += `\nIn message: ${argv[2]}` console.log(`Error[${route.host}|${argv[1]}]:${argv[0]}`) break + default: + route.socket.send(`! "Unknown command '${command}'" 0 ${message}`) + dlog(`Host ${route.host} send unknown command: ${message}`) + break } }, @@ -376,7 +402,7 @@ exports = { * @arg {Object} [readOpts] - options to pass to fs.readFile() */ syncReads: (files, readOpts) => new Promise((resolve,reject) => { - dlog(`syncReads: ${files}`) + dlog(`syncing reads from ${files}`) let count = 0 let results = {} const read_cb = (fileName) => (err, data) => { @@ -392,5 +418,6 @@ exports = { files.forEach((file) => require('fs').readFile(file, readOpts, read_cb(file))) }) - } + +module.exports = exports