Skip to content

Commit

Permalink
chore: use indirect eval, avoid exposing stuff to the global scope (#…
Browse files Browse the repository at this point in the history
…11646)

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

* prettier
  • Loading branch information
Rich-Harris committed May 16, 2024
1 parent c131e6f commit 2e7e399
Showing 1 changed file with 151 additions and 143 deletions.
294 changes: 151 additions & 143 deletions sites/svelte-5-preview/src/lib/Output/srcdoc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
if (action === 'eval') {
try {
const { script } = ev.data.args;
eval(script);
(0, eval)(script);
send_ok();
} catch (e) {
send_error(e.message, e.stack);
Expand Down Expand Up @@ -140,160 +140,168 @@
window.addEventListener('unhandledrejection', (event) => {
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) => {
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 };
original(...args);
};
});

try {
parent.postMessage({ action: 'console', level, args }, '*');
} catch (err) {
parent.postMessage({ action: 'console', level: 'unclonable' }, '*');
}
}
[
{ method: 'group', action: 'console_group' },
{ method: 'groupEnd', action: 'console_group_end' },
{ 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());
};
});

[
{ method: 'group', action: 'console_group' },
{ method: 'groupEnd', action: 'console_group_end' },
{ 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);
console.timeLog = (label = 'default') => {
original_timelog(label);
const now = performance.now();
if (timers.has(label)) {
parent.postMessage(
{
action: 'console',
level: 'system-log',
args: [`${label}: ${now - timers.get(label)}ms`]
},
'*'
);
} else {
parent.postMessage(
{
action: 'console',
level: 'system-warn',
args: [`Timer '${label}' does not exist`]
},
'*'
);
}
};
});

const timers = new Map();
const original_time = console.time;
const original_timelog = console.timeLog;
const original_timeend = console.timeEnd;

console.time = (label = 'default') => {
original_time(label);
timers.set(label, performance.now());
};
console.timeLog = (label = 'default') => {
original_timelog(label);
const now = performance.now();
if (timers.has(label)) {
parent.postMessage(
{
action: 'console',
level: 'system-log',
args: [`${label}: ${now - timers.get(label)}ms`]
},
'*'
);
} else {
parent.postMessage(
{ action: 'console', level: 'system-warn', args: [`Timer '${label}' does not exist`] },
'*'
);
}
};
console.timeEnd = (label = 'default') => {
original_timeend(label);
const now = performance.now();
if (timers.has(label)) {
parent.postMessage(
{
action: 'console',
level: 'system-log',
args: [`${label}: ${now - timers.get(label)}ms`]
},
'*'
);
} else {
console.timeEnd = (label = 'default') => {
original_timeend(label);
const now = performance.now();
if (timers.has(label)) {
parent.postMessage(
{
action: 'console',
level: 'system-log',
args: [`${label}: ${now - timers.get(label)}ms`]
},
'*'
);
} else {
parent.postMessage(
{
action: 'console',
level: 'system-warn',
args: [`Timer '${label}' does not exist`]
},
'*'
);
}
timers.delete(label);
};

const original_assert = console.assert;
console.assert = (condition, ...args) => {
if (condition) {
const stack = new Error().stack;
parent.postMessage({ action: 'console', level: 'assert', args, stack }, '*');
}
original_assert(condition, ...args);
};

const counter = new Map();
const original_count = console.count;
const original_countreset = console.countReset;

console.count = (label = 'default') => {
counter.set(label, (counter.get(label) || 0) + 1);
parent.postMessage(
{ action: 'console', level: 'system-warn', args: [`Timer '${label}' does not exist`] },
{ action: 'console', level: 'system-log', args: `${label}: ${counter.get(label)}` },
'*'
);
}
timers.delete(label);
};
original_count(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.assert = (condition, ...args) => {
if (condition) {
console.trace = (...args) => {
const stack = new Error().stack;
parent.postMessage({ action: 'console', level: 'assert', args, stack }, '*');
}
original_assert(condition, ...args);
};

const counter = new Map();
const original_count = console.count;
const original_countreset = console.countReset;

console.count = (label = 'default') => {
counter.set(label, (counter.get(label) || 0) + 1);
parent.postMessage(
{ action: 'console', level: 'system-log', args: `${label}: ${counter.get(label)}` },
'*'
);
original_count(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;

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;
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>
</head>
<body></body>
Expand Down

0 comments on commit 2e7e399

Please sign in to comment.