-
Notifications
You must be signed in to change notification settings - Fork 7
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
Allow sharing of the REPL #25
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,34 @@ | ||
export const getCodeSizeInBytes = code => { | ||
return new Blob([code], { type: 'text/plain' }).size; | ||
}; | ||
|
||
const base64UrlDecodeChars = {'-': '+', '_': '/', '.': '='}; | ||
const base64UrlDecode = input => { | ||
try { | ||
return atob(input.replace(/[-_.]/g, c => base64UrlDecodeChars[c])); | ||
} catch { | ||
return null; | ||
} | ||
} | ||
|
||
const base64UrlEncodeChars = {'+': '-', '/': '_', '=': '.'}; | ||
const base64UrlEncode = input => { | ||
try { | ||
return btoa(input).replace(/[+/=]/g, c => base64UrlEncodeChars[c]); | ||
} catch { | ||
return null; | ||
} | ||
} | ||
|
||
export const saveState = state => { | ||
const base64 = base64UrlEncode(JSON.stringify(state)); | ||
window.history.replaceState(state, '', `#${base64}`); | ||
}; | ||
|
||
export const loadState = () => { | ||
try { | ||
const base64 = window.location.hash.slice(1); | ||
return JSON.parse(base64UrlDecode(base64)); | ||
} catch {} | ||
return null; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,7 +141,15 @@ const options = `// edit terser options | |
rename: {}, | ||
}`; | ||
|
||
/* eslint-disable-next-line no-eval */ | ||
export const evalOptions = (opts) => eval(`(${opts||options})`) | ||
export const evalOptions = (opts) => { | ||
opts = opts || options; | ||
// Strip line comments | ||
opts = opts.replace(/\/\/.*/g, ''); | ||
// Trim trailing commas | ||
opts = opts.replace(/,\s*([\]}])/g, '$1'); | ||
// Quote property names | ||
opts = opts.replace(/^\s*(\w+):/gm, '"$1":'); | ||
return JSON.parse(opts); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see why this is something we need to do (avoiding a self xss and whatnot), however there are some Terser options that are regexps. For instance you can use There are a few options that take functions too (which would be a nightmare) so I'm not mentioning that part. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's possible to avoid this by disabling sharing for options that include RegExps and functions. Then you can do eval freely, and only do JSON.parse in loadState |
||
|
||
export default options; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Smart!