chore: use indirect eval, avoid exposing stuff to the global scope (#11646)

* chore: use indirect eval, avoid exposing stuff to the global scope

* prettier
pull/11645/head
Rich Harris 1 year ago committed by GitHub
parent c131e6f494
commit 2e7e399160
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -85,7 +85,7 @@
if (action === 'eval') { if (action === 'eval') {
try { try {
const { script } = ev.data.args; const { script } = ev.data.args;
eval(script); (0, eval)(script);
send_ok(); send_ok();
} catch (e) { } catch (e) {
send_error(e.message, e.stack); send_error(e.message, e.stack);
@ -140,160 +140,168 @@
window.addEventListener('unhandledrejection', (event) => { window.addEventListener('unhandledrejection', (event) => {
parent.postMessage({ action: 'unhandledrejection', value: event.reason }, '*'); parent.postMessage({ action: 'unhandledrejection', value: event.reason }, '*');
}); });
}).call(this);
let previous = { level: null, args: null }; let previous = { level: null, args: null };
['clear', 'log', 'info', 'dir', 'warn', 'error', 'table'].forEach((level) => {
const original = console[level];
console[level] = (...args) => {
const stringifiedArgs = stringify(args);
if (previous.level === level && previous.args && previous.args === stringifiedArgs) {
parent.postMessage({ action: 'console', level, duplicate: true }, '*');
} else {
previous = { level, args: stringifiedArgs };
try {
parent.postMessage({ action: 'console', level, args }, '*');
} catch (err) {
parent.postMessage({ action: 'console', level: 'unclonable' }, '*');
}
}
['clear', 'log', 'info', 'dir', 'warn', 'error', 'table'].forEach((level) => { original(...args);
const original = console[level]; };
console[level] = (...args) => { });
const stringifiedArgs = stringify(args);
if (previous.level === level && previous.args && previous.args === stringifiedArgs) {
parent.postMessage({ action: 'console', level, duplicate: true }, '*');
} else {
previous = { level, args: stringifiedArgs };
try { [
parent.postMessage({ action: 'console', level, args }, '*'); { method: 'group', action: 'console_group' },
} catch (err) { { method: 'groupEnd', action: 'console_group_end' },
parent.postMessage({ action: 'console', level: 'unclonable' }, '*'); { method: 'groupCollapsed', action: 'console_group_collapsed' }
} ].forEach((group_action) => {
} const original = console[group_action.method];
console[group_action.method] = (label) => {
parent.postMessage({ action: group_action.action, label }, '*');
original(label);
};
});
const timers = new Map();
const original_time = console.time;
const original_timelog = console.timeLog;
const original_timeend = console.timeEnd;
original(...args); console.time = (label = 'default') => {
original_time(label);
timers.set(label, performance.now());
}; };
}); console.timeLog = (label = 'default') => {
original_timelog(label);
[ const now = performance.now();
{ method: 'group', action: 'console_group' }, if (timers.has(label)) {
{ method: 'groupEnd', action: 'console_group_end' }, parent.postMessage(
{ method: 'groupCollapsed', action: 'console_group_collapsed' } {
].forEach((group_action) => { action: 'console',
const original = console[group_action.method]; level: 'system-log',
console[group_action.method] = (label) => { args: [`${label}: ${now - timers.get(label)}ms`]
parent.postMessage({ action: group_action.action, label }, '*'); },
'*'
original(label); );
} else {
parent.postMessage(
{
action: 'console',
level: 'system-warn',
args: [`Timer '${label}' does not exist`]
},
'*'
);
}
}; };
}); console.timeEnd = (label = 'default') => {
original_timeend(label);
const timers = new Map(); const now = performance.now();
const original_time = console.time; if (timers.has(label)) {
const original_timelog = console.timeLog; parent.postMessage(
const original_timeend = console.timeEnd; {
action: 'console',
console.time = (label = 'default') => { level: 'system-log',
original_time(label); args: [`${label}: ${now - timers.get(label)}ms`]
timers.set(label, performance.now()); },
}; '*'
console.timeLog = (label = 'default') => { );
original_timelog(label); } else {
const now = performance.now(); parent.postMessage(
if (timers.has(label)) { {
parent.postMessage( action: 'console',
{ level: 'system-warn',
action: 'console', args: [`Timer '${label}' does not exist`]
level: 'system-log', },
args: [`${label}: ${now - timers.get(label)}ms`] '*'
}, );
'*' }
); timers.delete(label);
} else { };
parent.postMessage(
{ action: 'console', level: 'system-warn', args: [`Timer '${label}' does not exist`] }, const original_assert = console.assert;
'*' console.assert = (condition, ...args) => {
); if (condition) {
} const stack = new Error().stack;
}; parent.postMessage({ action: 'console', level: 'assert', args, stack }, '*');
console.timeEnd = (label = 'default') => { }
original_timeend(label); original_assert(condition, ...args);
const now = performance.now(); };
if (timers.has(label)) {
parent.postMessage( const counter = new Map();
{ const original_count = console.count;
action: 'console', const original_countreset = console.countReset;
level: 'system-log',
args: [`${label}: ${now - timers.get(label)}ms`] console.count = (label = 'default') => {
}, counter.set(label, (counter.get(label) || 0) + 1);
'*'
);
} else {
parent.postMessage( parent.postMessage(
{ action: 'console', level: 'system-warn', args: [`Timer '${label}' does not exist`] }, { action: 'console', level: 'system-log', args: `${label}: ${counter.get(label)}` },
'*' '*'
); );
} original_count(label);
timers.delete(label); };
};
console.countReset = (label = 'default') => {
if (counter.has(label)) {
counter.set(label, 0);
} else {
parent.postMessage(
{
action: 'console',
level: 'system-warn',
args: `Count for '${label}' does not exist`
},
'*'
);
}
original_countreset(label);
};
const original_trace = console.trace;
const original_assert = console.assert; console.trace = (...args) => {
console.assert = (condition, ...args) => {
if (condition) {
const stack = new Error().stack; const stack = new Error().stack;
parent.postMessage({ action: 'console', level: 'assert', args, stack }, '*'); parent.postMessage({ action: 'console', level: 'trace', args, stack }, '*');
} original_trace(...args);
original_assert(condition, ...args); };
};
function stringify(args) {
const counter = new Map(); try {
const original_count = console.count; return JSON.stringify(args, (key, value) => {
const original_countreset = console.countReset; // if we don't do this, our Set/Map from svelte/reactivity would show up wrong in the console
if (value instanceof Map) {
console.count = (label = 'default') => { return {
counter.set(label, (counter.get(label) || 0) + 1); type: 'Map',
parent.postMessage( value
{ action: 'console', level: 'system-log', args: `${label}: ${counter.get(label)}` }, };
'*' }
); if (value instanceof Set) {
original_count(label); return {
}; type: 'Set',
value
console.countReset = (label = 'default') => { };
if (counter.has(label)) { }
counter.set(label, 0); return value;
} else { });
parent.postMessage( } catch (error) {
{ return null;
action: 'console', }
level: 'system-warn',
args: `Count for '${label}' does not exist`
},
'*'
);
}
original_countreset(label);
};
const original_trace = console.trace;
console.trace = (...args) => {
const stack = new Error().stack;
parent.postMessage({ action: 'console', level: 'trace', args, stack }, '*');
original_trace(...args);
};
function stringify(args) {
try {
return JSON.stringify(args, (key, value) => {
// if we don't do this, our Set/Map from svelte/reactivity would show up wrong in the console
if (value instanceof Map) {
return {
type: 'Map',
value
};
}
if (value instanceof Set) {
return {
type: 'Set',
value
};
}
return value;
});
} catch (error) {
return null;
} }
} })();
</script> </script>
</head> </head>
<body></body> <body></body>

Loading…
Cancel
Save