Add loop-protect

Remove webworkers
This commit is contained in:
Berkeley Martinez
2015-11-30 14:27:39 -08:00
parent 063d16383f
commit 3a299daa37
15 changed files with 597 additions and 513 deletions

View File

@ -1,16 +1,17 @@
// what are the executeChallenge functions?
// Should be responsible for starting after a submit action
// Should not be responsible for displaying results
// Should return results
// should grab editor value
// depends on main editor
window.common = (function(global) {
const {
ga,
Rx: { Observable },
common = { init: [] }
} = global;
const {
addLoopProtect,
detectUnsafeCode$,
updatePreview$,
challengeType,
challengeTypes
} = common;
let attempts = 0;
common.executeChallenge$ = function executeChallenge$() {
@ -18,67 +19,39 @@ window.common = (function(global) {
const originalCode = code;
const head = common.arrayToNewLineString(common.head);
const tail = common.arrayToNewLineString(common.tail);
const combinedCode = head + code + tail;
attempts++;
ga('send', 'event', 'Challenge', 'ran-code', common.challengeName);
// run checks for unsafe code
return common.detectUnsafeCode$(code)
return detectUnsafeCode$(code)
// add head and tail and detect loops
.map(code => head + code + tail)
.flatMap(code => {
if (common.challengeType === common.challengeTypes.HTML) {
if (common.hasJs(code)) {
// html has a script code
// add faux code and test in webworker
return common.addFaux$(code)
.flatMap(code => common.detectLoops$(code))
.flatMap(({ err }) => {
if (err) {
return Observable.throw(err);
}
return common.updatePreview$(code)
.flatMap(() => common.runPreviewTests$({
code,
tests: common.tests.slice()
}));
});
}
// no script code detected in html code
// Update preview and run tests in iframe
return common.updatePreview$(code)
.flatMap(code => common.runPreviewTests$({
code,
tests: common.tests.slice()
}));
.map(() => {
if (challengeType !== challengeTypes.HTML) {
return `<script>;${addLoopProtect(combinedCode)}/**/</script>`;
}
// js challenge
// remove comments and add tests to string
return Observable.just(common.addTestsToString(Object.assign(
{
code: common.removeComments(code),
tests: common.tests.slice()
}
)))
.flatMap(common.detectLoops$)
.flatMap(({ err, code, data, userTests }) => {
if (err) {
return Observable.throw(err);
}
return addLoopProtect(combinedCode);
})
.flatMap(code => updatePreview$(code))
.flatMap(code => {
let output;
// run tests
// for now these are running in the browser
return common.runTests$({
data,
code,
userTests,
originalCode,
output: data.output.replace(/\\\"/gi, '')
});
if (
challengeType === challengeTypes.HTML &&
common.hasJs(code)
) {
output = common.getJsOutput(common.getJsFromHtml(code));
} else if (challengeType !== challengeTypes.HTML) {
output = common.getJsOutput(combinedCode);
}
return common.runPreviewTests$({
tests: common.tests.slice(),
originalCode,
output
});
});
};