From 6a2818a1dc07347d6b8e6a002821e6f7d5336388 Mon Sep 17 00:00:00 2001 From: Joel McCracken Date: Wed, 20 Aug 2014 20:49:30 -0400 Subject: [PATCH 1/6] Adds instructions to install coffeescript in repo Otherwise, I get "Cannot find module 'coffee-script'" --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 77abe81..77f09cf 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ Dependencies Using npm: + npm install coffee-script npm install -g coffee-script #### [Pygments](http://pygments.org/) From 2b3b2b32059450f29a18b0b13cd7f5d41c7666ca Mon Sep 17 00:00:00 2001 From: Joel McCracken Date: Fri, 12 Dec 2014 19:29:55 -0500 Subject: [PATCH 2/6] implements fake 'save' backend There is a separate, not-open-source server component that does the saving. This implements an in-memory save that can serve for development purposes. --- server.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/server.js b/server.js index f05a9bc..1b6742b 100755 --- a/server.js +++ b/server.js @@ -28,6 +28,10 @@ function textResponse(res, code, txt) { res.end(txt); } +function genRandomString() { + return Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0,5); +} + var waiting = {}; var httpCb = function (req, res) { var uri = url.parse(req.url).pathname; @@ -74,6 +78,26 @@ var httpCb = function (req, res) { return; } + + var inMemorySaved = {} + if(m = uri.match(/save/)){ + var thisRandom = genRandomString(); + var dataParts = []; + req.on('data', function(data){ + dataParts.push(data); + }); + req.on('end', function(){ + inMemorySaved[thisRandom] = queryString.parse(dataParts.join('')); + res.writeHead(200, {'Content-Type': CONTENT_TYPES.json, 'max-age': '0'}); + var responseString = JSON.stringify({ session_id: String(thisRandom), + // not attempting to support multiple revisions + revision_id: '1' + }); + res.end(responseString); + }); + return; + }; + fs.exists(filename, function (exists) { if (!exists) { From f2779aa32a1f8c9cf3c6b6d15636cb3fc512b0b8 Mon Sep 17 00:00:00 2001 From: Joel McCracken Date: Fri, 12 Dec 2014 19:43:38 -0500 Subject: [PATCH 3/6] fixes inMemorySaved variable location The scoping was wrong, so it was being re-created on each request. --- server.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 1b6742b..6bcd16a 100755 --- a/server.js +++ b/server.js @@ -33,6 +33,8 @@ function genRandomString() { } var waiting = {}; +var inMemorySaved = {}; + var httpCb = function (req, res) { var uri = url.parse(req.url).pathname; if (uri.split('/')[1] in { @@ -78,9 +80,7 @@ var httpCb = function (req, res) { return; } - - var inMemorySaved = {} - if(m = uri.match(/save/)){ + if(m = uri.match(/save/)) { var thisRandom = genRandomString(); var dataParts = []; req.on('data', function(data){ From 819a399b29dd6e153fd935a2b27ac02d470826e9 Mon Sep 17 00:00:00 2001 From: Joel McCracken Date: Fri, 12 Dec 2014 19:44:18 -0500 Subject: [PATCH 4/6] Render index when saved url is requested Theoretically, this could be a problem, if say the random function save something to a url that is otherwise valid... but idc about that really. --- server.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server.js b/server.js index 6bcd16a..5c4c9db 100755 --- a/server.js +++ b/server.js @@ -44,6 +44,10 @@ var httpCb = function (req, res) { , examples: 1 , workspace: 1 }) { uri = '/index.html'; } + if(inMemorySaved[uri.split('/')[1]]) { + uri = '/index.html'; + } + var filename = path.join(process.cwd(), uri);; var m; From d50ac01fb50d85d43b4438e6efaaea6fe0496475 Mon Sep 17 00:00:00 2001 From: Joel McCracken Date: Fri, 12 Dec 2014 20:26:42 -0500 Subject: [PATCH 5/6] factors duplicated split functionality into variable Getting the special first key is used multiple times, extract variable. --- server.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/server.js b/server.js index 5c4c9db..e1228d4 100755 --- a/server.js +++ b/server.js @@ -37,17 +37,18 @@ var inMemorySaved = {}; var httpCb = function (req, res) { var uri = url.parse(req.url).pathname; - if (uri.split('/')[1] in { + var uriFirstPiece = uri.split('/')[1] + if (uriFirstPiece in { languages: 1 , help: 1 , about: 1 , examples: 1 , workspace: 1 }) { uri = '/index.html'; } - if(inMemorySaved[uri.split('/')[1]]) { - uri = '/index.html'; - } + if(inMemorySaved[uriFirstPiece]) { + restoreFakeSession(uriFirstPiece, res); + } var filename = path.join(process.cwd(), uri);; var m; From e0f2f8e29efae3c9d9ad57517340fbd9deb5f1cf Mon Sep 17 00:00:00 2001 From: Joel McCracken Date: Fri, 12 Dec 2014 20:25:10 -0500 Subject: [PATCH 6/6] adds method to restore fake sessions These set the data in the preferred session format, reads index, replaces the magic SESSION_PLACEHOLDER with the required contents, and returns the corrected index file. --- server.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/server.js b/server.js index e1228d4..8f24c6e 100755 --- a/server.js +++ b/server.js @@ -32,6 +32,31 @@ function genRandomString() { return Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0,5); } +function restoreFakeSession(pieceName, res) { + fs.readFile('index.html', String, function (err, file) { + if (err) { + textResponse(res, 500, err + '\n'); + return; + } + + var savedData = inMemorySaved[pieceName] + var sessionReturnData = {session_id: pieceName, + revision_id: "1", + eval_history: JSON.parse(savedData.eval_history), + editor_text: savedData.editor_text, + language: savedData.language, + console_dump: savedData.console_dump }; + + file = String(file).replace('SESSION_PLACEHOLDER', 'REPLIT_DATA='+JSON.stringify(sessionReturnData)); + + res.writeHead(200, {'Content-Type': CONTENT_TYPES['html'], 'max-age': '0'}); + res.write(file); + + res.end(); + }); +} + + var waiting = {}; var inMemorySaved = {};