2016-05-25 18:28:20 -07:00
|
|
|
import Rx, { Observable, Subject } from 'rx';
|
2016-06-23 20:05:30 -07:00
|
|
|
/* eslint-disable import/no-unresolved */
|
2016-05-27 17:11:25 -07:00
|
|
|
import loopProtect from 'loop-protect';
|
2016-06-23 20:05:30 -07:00
|
|
|
/* eslint-enable import/no-unresolved */
|
2016-08-16 15:59:23 -07:00
|
|
|
import { ofType } from '../../common/utils/get-actions-of-type';
|
2016-05-20 12:42:26 -07:00
|
|
|
import types from '../../common/app/routes/challenges/redux/types';
|
2016-05-25 18:28:20 -07:00
|
|
|
import {
|
2016-05-28 01:28:50 -07:00
|
|
|
updateOutput,
|
|
|
|
checkChallenge,
|
2016-05-25 18:28:20 -07:00
|
|
|
updateTests
|
|
|
|
} from '../../common/app/routes/challenges/redux/actions';
|
2016-05-20 12:42:26 -07:00
|
|
|
|
2017-01-26 21:07:22 -08:00
|
|
|
// we use two different frames to make them all essentially pure functions
|
|
|
|
// main iframe is responsible rendering the preview and is where we proxy the
|
|
|
|
// console.log
|
2016-05-20 12:42:26 -07:00
|
|
|
const mainId = 'fcc-main-frame';
|
2017-01-26 21:07:22 -08:00
|
|
|
// the test frame is responsible for running the assert tests
|
2016-05-20 12:42:26 -07:00
|
|
|
const testId = 'fcc-test-frame';
|
2016-05-25 18:28:20 -07:00
|
|
|
|
|
|
|
const createHeader = (id = mainId) => `
|
|
|
|
<script>
|
|
|
|
window.__frameId = '${id}';
|
2017-01-26 21:07:22 -08:00
|
|
|
window.onerror = function(msg, url, ln, col, err) {
|
|
|
|
window.__err = err;
|
|
|
|
return true;
|
|
|
|
};
|
2016-05-25 18:28:20 -07:00
|
|
|
</script>
|
|
|
|
`;
|
|
|
|
|
2016-05-20 12:42:26 -07:00
|
|
|
|
|
|
|
function createFrame(document, id = mainId) {
|
|
|
|
const frame = document.createElement('iframe');
|
|
|
|
frame.id = id;
|
|
|
|
frame.setAttribute('style', 'display: none');
|
|
|
|
document.body.appendChild(frame);
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshFrame(frame) {
|
|
|
|
frame.src = 'about:blank';
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFrameDocument(document, id = mainId) {
|
|
|
|
let frame = document.getElementById(id);
|
|
|
|
if (!frame) {
|
|
|
|
frame = createFrame(document, id);
|
|
|
|
}
|
2016-05-27 17:11:25 -07:00
|
|
|
frame.contentWindow.loopProtect = loopProtect;
|
|
|
|
return {
|
|
|
|
frame: frame.contentDocument || frame.contentWindow.document,
|
|
|
|
frameWindow: frame.contentWindow
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-01-26 21:07:22 -08:00
|
|
|
function buildProxyConsole(window, proxyLogger) {
|
2016-05-27 17:11:25 -07:00
|
|
|
const oldLog = window.console.log.bind(console);
|
|
|
|
window.__console = {};
|
|
|
|
window.__console.log = function proxyConsole(...args) {
|
2017-01-26 21:07:22 -08:00
|
|
|
proxyLogger.onNext(args);
|
2016-05-27 17:11:25 -07:00
|
|
|
return oldLog(...args);
|
|
|
|
};
|
2016-05-20 12:42:26 -07:00
|
|
|
}
|
|
|
|
|
2017-01-26 21:07:22 -08:00
|
|
|
function frameMain({ build } = {}, document, proxyLogger) {
|
2016-05-27 17:11:25 -07:00
|
|
|
const { frame: main, frameWindow } = getFrameDocument(document);
|
2016-05-25 18:28:20 -07:00
|
|
|
refreshFrame(main);
|
2017-01-26 21:07:22 -08:00
|
|
|
buildProxyConsole(frameWindow, proxyLogger);
|
2016-09-29 12:48:19 -07:00
|
|
|
main.Rx = Rx;
|
2016-05-20 12:42:26 -07:00
|
|
|
main.open();
|
2017-01-26 21:07:22 -08:00
|
|
|
main.write(createHeader() + build);
|
2016-05-20 12:42:26 -07:00
|
|
|
main.close();
|
|
|
|
}
|
|
|
|
|
2017-04-28 18:30:23 -07:00
|
|
|
function frameTests({ build, sources, checkChallengePayload } = {}, document) {
|
2016-05-27 17:11:25 -07:00
|
|
|
const { frame: tests } = getFrameDocument(document, testId);
|
2016-05-25 18:28:20 -07:00
|
|
|
refreshFrame(tests);
|
|
|
|
tests.Rx = Rx;
|
2017-04-28 18:30:23 -07:00
|
|
|
// default for classic challenges
|
|
|
|
// should not be used for modern
|
|
|
|
tests.__source = sources['index'] || '';
|
|
|
|
tests.__getUserInput = key => sources[key];
|
2017-01-26 21:07:22 -08:00
|
|
|
tests.__checkChallengePayload = checkChallengePayload;
|
2016-05-25 18:28:20 -07:00
|
|
|
tests.open();
|
|
|
|
tests.write(createHeader(testId) + build);
|
|
|
|
tests.close();
|
|
|
|
}
|
|
|
|
|
2017-01-26 21:07:22 -08:00
|
|
|
export default function frameEpic(actions, getState, { window, document }) {
|
|
|
|
// we attach a common place for the iframes to pull in functions from
|
|
|
|
// the main process
|
2016-05-20 12:42:26 -07:00
|
|
|
window.__common = {};
|
2016-05-27 17:11:25 -07:00
|
|
|
window.__common.shouldRun = () => true;
|
2017-01-26 21:07:22 -08:00
|
|
|
// this will proxy console.log calls
|
|
|
|
const proxyLogger = new Subject();
|
|
|
|
// frameReady will let us know when the test iframe is ready to run
|
|
|
|
const frameReady = window.__common[testId + 'Ready'] = new Subject();
|
|
|
|
const result = actions
|
|
|
|
::ofType(types.frameMain, types.frameTests)
|
2016-08-16 15:59:23 -07:00
|
|
|
// if isCodeLocked is true do not frame user code
|
|
|
|
.filter(() => !getState().challengesApp.isCodeLocked)
|
2016-05-20 12:42:26 -07:00
|
|
|
.map(action => {
|
2016-05-25 18:28:20 -07:00
|
|
|
if (action.type === types.frameMain) {
|
2017-01-26 21:07:22 -08:00
|
|
|
return frameMain(action.payload, document, proxyLogger);
|
2016-05-25 18:28:20 -07:00
|
|
|
}
|
2016-05-27 17:11:25 -07:00
|
|
|
return frameTests(action.payload, document);
|
2017-01-26 21:07:22 -08:00
|
|
|
})
|
|
|
|
.ignoreElements();
|
2016-05-25 18:28:20 -07:00
|
|
|
|
|
|
|
return Observable.merge(
|
2017-01-26 21:07:22 -08:00
|
|
|
proxyLogger.map(updateOutput),
|
|
|
|
frameReady.flatMap(({ checkChallengePayload }) => {
|
2016-05-27 17:11:25 -07:00
|
|
|
const { frame } = getFrameDocument(document, testId);
|
2016-05-25 18:28:20 -07:00
|
|
|
const { tests } = getState().challengesApp;
|
2016-05-28 01:28:50 -07:00
|
|
|
const postTests = Observable.of(
|
|
|
|
updateOutput('// tests completed'),
|
2017-01-26 21:07:22 -08:00
|
|
|
checkChallenge(checkChallengePayload)
|
2016-05-28 01:28:50 -07:00
|
|
|
).delay(250);
|
2017-01-26 21:07:22 -08:00
|
|
|
// run the tests within the test iframe
|
|
|
|
return frame.__runTests(tests)
|
|
|
|
.do(tests => {
|
|
|
|
tests.forEach(test => {
|
|
|
|
if (typeof test.message === 'string') {
|
|
|
|
proxyLogger.onNext(test.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
2016-05-27 17:11:25 -07:00
|
|
|
.map(updateTests)
|
2016-05-28 01:28:50 -07:00
|
|
|
.concat(postTests);
|
2016-05-25 18:28:20 -07:00
|
|
|
}),
|
2017-01-26 21:07:22 -08:00
|
|
|
result
|
2016-05-25 18:28:20 -07:00
|
|
|
);
|
2016-05-20 12:42:26 -07:00
|
|
|
}
|