Skip to content

Serve sketch preview from subdomain #1894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 53 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
c684281
Start creating preview server
catarak Feb 4, 2021
c051f0c
Merge develop, start to render preview from subdomain
catarak Feb 10, 2021
ab536ef
Merge branch 'develop' into chore/sandbox
catarak Feb 17, 2021
4993898
More work on sandbox rendering
catarak Feb 19, 2021
0a5a863
Merge branch 'develop' into chore/sandbox
catarak Feb 24, 2021
1a057fc
Render iframe from preview server
catarak Feb 24, 2021
1c10250
Render nested iframes
catarak Feb 24, 2021
1e3a13b
Start and stop messages work
catarak Feb 25, 2021
b59d735
Start passing files from editor to sandboxed iframe
catarak Mar 2, 2021
416c52d
Merge develop
catarak Mar 12, 2021
4e76e57
Merge branch 'develop' into chore/sandbox
catarak Mar 31, 2021
2dc6529
Add function syncFileContent, load previewScripts in EmbedFrame
catarak Mar 31, 2021
06cee37
Connect console messages from iframe to editor frame
catarak Apr 6, 2021
e7f5fad
Enable blob urls for text files in rendering
catarak Apr 7, 2021
b8f1192
Add new variables to .env.example
catarak Apr 7, 2021
263f48c
Merge develop
catarak Apr 7, 2021
d31916f
Render FullView with EmbedFrame
catarak Apr 8, 2021
2e9878a
Move embed/present routes to previewServer
catarak Apr 13, 2021
d912502
Fix full view by waiting until frame registers
catarak Apr 13, 2021
2252989
Move PreviewFrame to styled components
catarak Apr 13, 2021
5141186
Fix ConsoleInput to work with EmbedFrame
catarak Apr 14, 2021
cf8f0ed
Fix loading assets at sketch runtime
catarak Apr 14, 2021
c397fd4
Update webpack config so that preview server works in prod
catarak Apr 15, 2021
73ca77a
Fix propTypes for EmbedFrame, add sandbox/allow to PreviewFrame
catarak Apr 15, 2021
bbd8503
Merge branch 'develop' into chore/sandbox
catarak May 11, 2021
808e7b1
Render all linked JS files with blobUrl
catarak May 12, 2021
9240558
Add staging kubernetes config
catarak May 19, 2021
2d4c028
New commit to deploy to release
catarak May 25, 2021
ce60942
Fix index.integration.test
catarak May 25, 2021
d01c905
Merge branch 'develop' into chore/sandbox
catarak May 27, 2021
f640da8
Initialize files with path, update console errors to use file path
catarak Jun 8, 2021
a9d0852
Highlight runtime errors, select correct file
catarak Jun 8, 2021
d1b3f12
Handle errors in index.html
catarak Jun 9, 2021
c7b5e47
Expand console on runtime errors
catarak Jun 9, 2021
efeab9f
Fix autorefresh
catarak Jun 9, 2021
672f23e
Merge branch 'develop' into chore/sandbox
catarak Jun 14, 2021
c0def07
Update srcdoc-polyfill, fix rendering on Safari
catarak Jun 14, 2021
7df219c
Render index.html using blobUrl, update error stack printing
catarak Jun 15, 2021
448f0c6
Fix error parsing, fix fetching sektch runtime urls
catarak Jun 16, 2021
68897b7
Continue to update error parsing
catarak Jun 16, 2021
a2c3843
Remove unused code, update prod webpack build for previewScripts
catarak Jun 16, 2021
49a1e0b
Merge branch 'develop' into chore/sandbox
catarak Jun 29, 2021
913de33
Handle missing routes in previewServer
catarak Jun 29, 2021
1337767
Update present view baseUrl
catarak Jun 29, 2021
fe1f989
Add connection to MongoDB to previewServer
catarak Jun 29, 2021
8c490c9
Update p5.js and p5.sound.js version to 1.4.0
catarak Jun 30, 2021
787b8d4
Remove runtime error highlight after changing file content
catarak Jul 1, 2021
7a79098
Monkeypatch p5._report function to resolve file names
catarak Jul 1, 2021
9686236
Add p5.accessibility, remove sound output, update line offset for ind…
catarak Jul 6, 2021
21b787e
Fix sketch <base> when usaved, handle nonstring messages
catarak Jul 6, 2021
8d930ce
Update onerror for previewEntry
catarak Jul 6, 2021
07078c0
Upate onerror to work cross browser
catarak Jul 6, 2021
14513b9
Add cors to previewServier, enable cors for Release
catarak Jul 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Connect console messages from iframe to editor frame
  • Loading branch information
catarak committed Apr 6, 2021
commit 06cee3752bc4ceeaa31f11aa03e434bd35802819
16 changes: 6 additions & 10 deletions client/modules/Preview/EmbedFrame.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ import {
EXTERNAL_LINK_REGEX,
NOT_EXTERNAL_LINK_REGEX
} from '../../../server/utils/fileUtils';
import {
hijackConsoleErrorsScript,
startTag,
getAllScriptOffsets
} from '../../utils/consoleUtils';
import { startTag, getAllScriptOffsets } from '../../utils/consoleUtils';
import { registerFrame, MessageTypes } from '../../utils/dispatcher';

import { getHTMLFile } from '../IDE/reducers/files';
Expand Down Expand Up @@ -86,7 +82,6 @@ function resolvePathsForElementsWithAttribute(attr, sketchDoc, files) {

function resolveCSSLinksInString(content, files) {
let newContent = content;
console.log(content);
let cssFileStrings = content.match(STRING_REGEX);
cssFileStrings = cssFileStrings || [];
cssFileStrings.forEach((cssFileString) => {
Expand Down Expand Up @@ -293,11 +288,12 @@ function injectLocalFiles(files, htmlFile) {
const sketchDocString = `<!DOCTYPE HTML>\n${sketchDoc.documentElement.outerHTML}`;
scriptOffs = getAllScriptOffsets(sketchDocString);
const consoleErrorsScript = sketchDoc.createElement('script');
consoleErrorsScript.innerHTML = hijackConsoleErrorsScript(
JSON.stringify(scriptOffs)
);
consoleErrorsScript.innerHTML = `
window.offs = ${JSON.stringify(scriptOffs)};
window.editorOrigin = '${getConfig('EDITOR_URL')}';
`;
addLoopProtect(sketchDoc);
sketchDoc.head.insertBefore(consoleErrorsScript, sketchDoc.head.firstElement);
sketchDoc.head.prepend(consoleErrorsScript);

return `<!DOCTYPE HTML>\n${sketchDoc.documentElement.outerHTML}`;
}
Expand Down
1 change: 0 additions & 1 deletion client/modules/Preview/previewIndex.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const App = () => {
const [state, dispatch] = useReducer(filesReducer, [], initialState);
const [isPlaying, setIsPlaying] = useState(false);
function handleMessageEvent(message) {
console.log('embedframe received message event');
const { type, payload } = message;
switch (type) {
case MessageTypes.FILES:
Expand Down
62 changes: 1 addition & 61 deletions client/utils/consoleUtils.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,8 @@
import { EXTERNAL_LINK_REGEX } from '../../server/utils/fileUtils';

export const hijackConsoleErrorsScript = (offs) => {
const s = `
function getScriptOff(line) {
var offs = ${offs};
var l = 0;
var file = '';
for (var i=0; i<offs.length; i++) {
var n = offs[i][0];
if (n < line && n > l) {
l = n;
file = offs[i][1];
}
}
return [line - l, file];
}
// catch reference errors, via http://stackoverflow.com/a/12747364/2994108
window.onerror = function (msg, url, lineNumber, columnNo, error) {
var string = msg.toLowerCase();
var substring = "script error";
var data = {};
if (url.match(${EXTERNAL_LINK_REGEX}) !== null && error.stack){
var errorNum = error.stack.split('about:srcdoc:')[1].split(':')[0];
var fileInfo = getScriptOff(errorNum);
data = msg + ' (' + fileInfo[1] + ': line ' + fileInfo[0] + ')';
} else {
var fileInfo = getScriptOff(lineNumber);
data = msg + ' (' + fileInfo[1] + ': line ' + fileInfo[0] + ')';
}
window.parent.postMessage([{
log: [{
method: 'error',
data: [data],
id: Date.now().toString()
}],
source: fileInfo[1]
}], '*');
return false;
};
// catch rejected promises
window.onunhandledrejection = function (event) {
if (event.reason && event.reason.message && event.reason.stack){
var errorNum = event.reason.stack.split('about:srcdoc:')[1].split(':')[0];
var fileInfo = getScriptOff(errorNum);
var data = event.reason.message + ' (' + fileInfo[1] + ': line ' + fileInfo[0] + ')';
window.parent.postMessage([{
log: [{
method: 'error',
data: [data],
id: Date.now().toString()
}],
source: fileInfo[1]
}], '*');
}
};
`;
return s;
};

export const startTag = '@fs-';

export const getAllScriptOffsets = (htmlFile) => {
const offs = [];
const hijackConsoleErrorsScriptLength = 52;
const hijackConsoleErrorsScriptLength = 2;
const embeddedJSStart = 'script crossorigin=""';
let foundJSScript = true;
let foundEmbeddedJS = true;
Expand Down
92 changes: 89 additions & 3 deletions client/utils/previewEntry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import loopProtect from 'loop-protect';
import { Hook, Decode, Encode } from 'console-feed';
import evaluateExpression from './evaluateExpression';
import { EXTERNAL_LINK_REGEX } from '../../server/utils/fileUtils';

// should postMessage user the dispatcher? does the parent window need to
// be registered as a frame? or a just a listener?

// could maybe send these as a message idk
// const { editor } = window;
const editor = window.parent.parent;
const { editorOrigin } = window;
// const editorOrigin = '*';
// const editorOrigin = 'http://localhost:8000';
// so this works??
// maybe i have to pass the parent window??? idk man

window.loopProtect = loopProtect;

Expand All @@ -17,12 +30,13 @@ setInterval(() => {
messages: consoleBuffer,
source: 'sketch'
};
window.parent.postMessage(message, window.origin);
editor.postMessage(message, editorOrigin);
consoleBuffer.length = 0;
}
}, LOGWAIT);

function handleMessageEvent(e) {
// maybe don't need this?? idk!
if (window.origin !== e.origin) return;
const { data } = e;
const { source, messages } = data;
Expand All @@ -34,15 +48,87 @@ function handleMessageEvent(e) {
const resultMessages = [
{ log: Encode({ method: error ? 'error' : 'result', data: [result] }) }
];
window.parent.postMessage(
editor.postMessage(
{
messages: resultMessages,
source: 'sketch'
},
window.origin
editorOrigin
);
});
}
}

window.addEventListener('message', handleMessageEvent);

function getScriptOff(line) {
const { offs } = window;
let l = 0;
let file = '';
for (let i = 0; i < offs.length; i += 1) {
const n = offs[i][0];
if (n < line && n > l) {
l = n;
[, file] = offs[i];
}
}
return [line - l, file];
}
// catch reference errors, via http://stackoverflow.com/a/12747364/2994108
window.onerror = function onError(msg, url, lineNumber, columnNo, error) {
// var string = msg.toLowerCase();
// var substring = "script error";
let data = {};
let fileInfo;
if (url.match(EXTERNAL_LINK_REGEX) !== null && error.stack) {
const errorNum = error.stack.split('about:srcdoc:')[1].split(':')[0];
fileInfo = getScriptOff(errorNum);
data = `${msg} (${fileInfo[1]}: line ${fileInfo[0]})`;
} else {
fileInfo = getScriptOff(lineNumber);
data = `${msg} (${fileInfo[1]}: line ${fileInfo[0]})`;
}
editor.postMessage(
{
source: fileInfo[1],
messages: [
{
log: [
{
method: 'error',
data: [data],
id: Date.now().toString()
}
]
}
]
},
editorOrigin
);
return false;
};
// catch rejected promises
window.onunhandledrejection = function onUnhandledRejection(event) {
if (event.reason && event.reason.message && event.reason.stack) {
const errorNum = event.reason.stack.split('about:srcdoc:')[1].split(':')[0];
const fileInfo = getScriptOff(errorNum);
const data = `${event.reason.message} (${fileInfo[1]}: line ${fileInfo[0]})`;
editor.postMessage(
{
source: fileInfo[1],
messages: [
{
log: [
{
method: 'error',
data: [data],
id: Date.now().toString()
}
]
}
]
},
editorOrigin
);
}
};
1 change: 1 addition & 0 deletions server/views/previewIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function renderPreviewIndex() {
? `${assetsManifest['/previewScripts.js']}`
: '/previewScripts.js'
}';
window.process.env.EDITOR_URL = '${process.env.EDITOR_URL}';
</script>
</head>
<body>
Expand Down