From 25d97e5e3b5ae22547fcdd24d38bd57127b0b246 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 30 Apr 2016 10:50:43 +0000 Subject: [PATCH] Move most of the specification of terminal API client-side, so that the terminal can be changed without restarting the core. Add descriptions to some packages. Other minor improvements. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3222 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/client.js | 18 ++++++++--- core/core.js | 16 +++++----- core/terminal.js | 45 ++++++++-------------------- packages/cory/index/index.js | 5 +++- packages/cory/mmoturtle/mmoturtle.js | 2 ++ packages/cory/ui/ui.js | 4 +-- packages/cory/xmpp/xmpp.js | 2 +- 7 files changed, 44 insertions(+), 48 deletions(-) diff --git a/core/client.js b/core/client.js index f15ea3876..d03930f1d 100644 --- a/core/client.js +++ b/core/client.js @@ -133,18 +133,18 @@ function receive(data) { if (window.Notification) { new Notification(line[0].title, line[0].options); } - } else if (line && line[0] && line[0].action == "title") { + } else if (line && line[0] && line[0].action == "setTitle") { window.document.title = line[0].value; - } else if (line && line[0] && line[0].action == "prompt") { + } else if (line && line[0] && line[0].action == "setPrompt") { var prompt = document.getElementById("prompt"); while (prompt.firstChild) { prompt.removeChild(prompt.firstChild); } prompt.appendChild(document.createTextNode(line[0].value)); - } else if (line && line[0] && line[0].action == "password") { + } else if (line && line[0] && line[0].action == "setPassword") { var prompt = document.getElementById("input"); prompt.setAttribute("type", line[0].value ? "password" : "text"); - } else if (line && line[0] && line[0].action == "hash") { + } else if (line && line[0] && line[0].action == "setHash") { window.location.hash = line[0].value; } else if (line && line[0] && line[0].action == "update") { document.getElementById("update").setAttribute("Style", "display: inline"); @@ -436,6 +436,16 @@ window.addEventListener("load", function() { gSocket.send(JSON.stringify({ action: "hello", path: window.location.pathname, + terminalApi: [ + ['clear'], + ['notify', 'title', 'options'], + ['postMessageToIframe', 'name', 'message'], + ['setHash', 'value'], + ['setPassword', 'value'], + ['setPrompt', 'value'], + ['setTitle', 'value'], + ['split', 'options'], + ], })); } gSocket.onmessage = function(event) { diff --git a/core/core.js b/core/core.js index 2ad2660ce..edea7a64a 100644 --- a/core/core.js +++ b/core/core.js @@ -289,20 +289,18 @@ function getProcess(packageOwner, packageName, key, options) { if (options.terminal) { imports.terminal = { 'print': process.terminal.print.bind(process.terminal), - 'clear': process.terminal.clear.bind(process.terminal), 'readLine': process.terminal.readLine.bind(process.terminal), - 'notify': process.terminal.notify.bind(process.terminal), 'setEcho': process.terminal.setEcho.bind(process.terminal), - 'setTitle': process.terminal.setTitle.bind(process.terminal), - 'setPrompt': process.terminal.setPrompt.bind(process.terminal), - 'setPassword': process.terminal.setPassword.bind(process.terminal), - 'setHash': process.terminal.setHash.bind(process.terminal), - 'split': process.terminal.split.bind(process.terminal), 'select': process.terminal.select.bind(process.terminal), - 'postMessageToIframe': process.terminal.postMessageToIframe.bind(process.terminal), 'cork': process.terminal.cork.bind(process.terminal), 'uncork': process.terminal.uncork.bind(process.terminal), }; + if (options.terminalApi) { + for (let i in options.terminalApi) { + let api = options.terminalApi[i]; + imports.terminal[api[0]] = process.terminal.makeFunction(api); + } + } } if (manifest && manifest.permissions @@ -416,6 +414,8 @@ httpd.all("", function(request, response) { return terminal.handler(request, response, null, null, match[1]); } else if (match = /^\/\~([^\/]+)\/([^\/]+)(.*)/.exec(request.uri)) { return terminal.handler(request, response, match[1], match[2], match[3]); + } else if (request.uri == "/robots.txt") { + return terminal.handler(request, response, null, null, request.uri); } else { var data = "File not found."; response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": data.length.toString()}); diff --git a/core/terminal.js b/core/terminal.js index d5b8105bf..a1d4b9a61 100644 --- a/core/terminal.js +++ b/core/terminal.js @@ -8,6 +8,7 @@ var kStaticFiles = [ {uri: '/client.js', path: 'client.js', type: 'text/javascript; charset=utf-8'}, {uri: '/editor.js', path: 'editor.js', type: 'text/javascript; charset=utf-8'}, {uri: '/agplv3-88x31.png', path: 'agplv3-88x31.png', type: 'image/png'}, + {uri: '/robots.txt', path: 'robots.txt', type: 'text/plain; charset=utf-8'}, ]; var auth = require('auth'); @@ -69,46 +70,14 @@ Terminal.prototype.print = function() { this._lastWrite = new Date(); } -Terminal.prototype.notify = function(title, options) { - this.print({action: "notify", title: title, options: options}); -} - -Terminal.prototype.setTitle = function(value) { - this.print({action: "title", value: value}); -} - -Terminal.prototype.setPrompt = function(value) { - this.print({action: "prompt", value: value}); -} - -Terminal.prototype.setPassword = function(value) { - this.print({action: "password", value: value}); -} - -Terminal.prototype.setHash = function(value) { - this.print({action: "hash", value: value}); -} - Terminal.prototype.notifyUpdate = function() { this.print({action: "update"}); } -Terminal.prototype.split = function(options) { - this.print({action: "split", options: options}); -} - Terminal.prototype.select = function(name) { this._selected = name; } -Terminal.prototype.postMessageToIframe = function(name, message) { - this.print({action: "postMessageToIframe", name: name, message: message}); -} - -Terminal.prototype.clear = function() { - this.print({action: "clear"}); -} - Terminal.prototype.ping = function() { this.dispatch({action: "ping"}); } @@ -137,6 +106,17 @@ Terminal.prototype.uncork = function() { } } +Terminal.prototype.makeFunction = function(api) { + let self = this; + return function() { + let message = {action: api[0]}; + for (let i = 1; i < api.length; i++) { + message[api[i]] = arguments[i - 1]; + } + self.print(message); + } +} + function invoke(handlers, argv) { var promises = []; if (handlers) { @@ -177,6 +157,7 @@ function socket(request, response, client) { var sessionId = makeSessionId(); response.send(JSON.stringify({lines: [{action: "session", sessionId: sessionId, credentials: credentials}]}), 0x1); + options.terminalApi = message.terminalApi || []; process = getSessionProcess(packageOwner, packageName, sessionId, options); process.terminal.readOutput(function(message) { response.send(JSON.stringify(message), 0x1); diff --git a/packages/cory/index/index.js b/packages/cory/index/index.js index acd11f5b7..0ff199444 100644 --- a/packages/cory/index/index.js +++ b/packages/cory/index/index.js @@ -1,5 +1,7 @@ "use strict"; +//! {"description": "A list of all packages and connected users"} + core.register("onSessionBegin", index); core.register("onSessionEnd", index); @@ -64,7 +66,8 @@ function index() { terminal.print( "* ", {href: "/~" + app.owner + "/" + app.name}, - message); + message, + app.manifest && app.manifest.description ? " - " + app.manifest.description.toString() : ""); }); terminal.uncork(); }); diff --git a/packages/cory/mmoturtle/mmoturtle.js b/packages/cory/mmoturtle/mmoturtle.js index 5178862c5..7874611fe 100644 --- a/packages/cory/mmoturtle/mmoturtle.js +++ b/packages/cory/mmoturtle/mmoturtle.js @@ -1,5 +1,7 @@ "use strict"; +//! {"description": "Massively multiplayer online Turtle Graphics"} + // This script runs server-side, once for each client session. if (imports.terminal) { diff --git a/packages/cory/ui/ui.js b/packages/cory/ui/ui.js index 5b0f44650..5939756d4 100644 --- a/packages/cory/ui/ui.js +++ b/packages/cory/ui/ui.js @@ -118,12 +118,12 @@ function testEdit(event) { }); } -if (imports.terminal) { +/*if (imports.terminal) { fileList({ title: "Test File List", prefix: "fileList_", edit: testEdit, }); -} +}*/ exports.fileList = fileList; \ No newline at end of file diff --git a/packages/cory/xmpp/xmpp.js b/packages/cory/xmpp/xmpp.js index e862347d6..cbf19c2fc 100644 --- a/packages/cory/xmpp/xmpp.js +++ b/packages/cory/xmpp/xmpp.js @@ -783,7 +783,7 @@ function schedulePing(socket) { terminal.split([ {type: "horizontal", children: [ {name: "terminal", grow: 1}, - {name: "users", basis: "2in", grow: 0, shrink: 0}, + {name: "users", basis: "2in", grow: "0", shrink: "0"}, ]}, ]); terminal.select("terminal");