diff --git a/core/client.js b/core/client.js index 788da5ca..e23ba3a3 100644 --- a/core/client.js +++ b/core/client.js @@ -126,7 +126,7 @@ function edit() { function load() { var request = new XMLHttpRequest(); request.addEventListener("loadend", function() { - if (request.status == 200) { + if (request.status == 200 || request.status == 404) { document.getElementById("editPane").style.display = 'flex'; if (!gEditor) { gEditor = CodeMirror.fromTextArea(document.getElementById("editor"), { @@ -138,9 +138,15 @@ function load() { 'showTrailingSpace': true, }); } - gEditor.setValue(request.responseText); + var text; + if (request.status == 200) { + text = request.responseText; + } else { + text = '// New script\nterminal.print("Hello, world!");\n'; + } + gEditor.setValue(text); gEditor.focus(); - gBackup = request.responseText; + gBackup = text; } }); request.addEventListener("error", function() { diff --git a/core/terminal.js b/core/terminal.js index 576fd309..14dc7360 100644 --- a/core/terminal.js +++ b/core/terminal.js @@ -258,8 +258,13 @@ async function handler(request, response, packageOwner, packageName, uri) { var process; if (uri === "/view") { var data = File.readFile("packages/" + packageOwner + "/" + packageName + "/" + packageName + ".js"); - response.writeHead(200, {"Content-Type": "text/javascript; charset=utf-8", "Content-Length": data.length}); - response.end(data); + if (data) { + response.writeHead(200, {"Content-Type": "text/javascript; charset=utf-8", "Content-Length": data.length}); + response.end(data); + } else { + response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": "File not found".length}); + response.end("File not found"); + } } else if (uri == "/save") { var credentials = auth.query(request.headers); var userName = credentials && credentials.session && credentials.session.name ? credentials.session.name : "guest";