Skip to content
This repository has been archived by the owner on Jun 23, 2018. It is now read-only.

Commit

Permalink
Merge pull request #126 from joelmccracken/fake-save-functionality
Browse files Browse the repository at this point in the history
Fake save functionality
  • Loading branch information
amasad committed Dec 13, 2014
2 parents 85180c0 + e0f2f8e commit 646b69f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -51,6 +51,7 @@ Dependencies

Using npm:

npm install coffee-script
npm install -g coffee-script

#### [Pygments](http://pygments.org/)
Expand Down
56 changes: 55 additions & 1 deletion server.js
Expand Up @@ -28,16 +28,52 @@ function textResponse(res, code, txt) {
res.end(txt);
}

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 = {};

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[uriFirstPiece]) {
restoreFakeSession(uriFirstPiece, res);
}
var filename = path.join(process.cwd(), uri);;

var m;
Expand Down Expand Up @@ -74,6 +110,24 @@ var httpCb = function (req, res) {
return;
}

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) {

Expand Down

0 comments on commit 646b69f

Please sign in to comment.