Skip to content

Instantly share code, notes, and snippets.

@tniessen
Created May 12, 2021 12:39
Show Gist options
  • Save tniessen/9d63df7dc9860e2f1514cd701a932891 to your computer and use it in GitHub Desktop.
Save tniessen/9d63df7dc9860e2f1514cd701a932891 to your computer and use it in GitHub Desktop.
Tests the behavior of instanceof on CryptoKey instances shared across frames
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<iframe src="iframe.html" width="100%" height="300"></iframe>
<pre id="parent"></pre>
<script type="text/javascript" src="test.js"></script>
<script type="text/javascript">
window.addEventListener('message', (e) => {
const { testValuesFromWorker: v } = window;
document.querySelector('#parent').textContent = summarizeTestValues(v);
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Frame</title>
</head>
<body>
<pre id="child"></pre>
<script type="text/javascript" src="test.js"></script>
<script type="text/javascript">
generateTestValues().then((v) => {
document.querySelector('#child').textContent = summarizeTestValues(v);
window.parent.testValuesFromWorker = v;
parent.postMessage('doStuff');
});
</script>
</body>
</html>
'use strict';
async function generateTestValues() {
const array = [1, 2, 3];
const cryptoKey = await crypto.subtle.generateKey({
name: 'AES-CTR',
length: 128
}, true, ['encrypt']);
return { array, cryptoKey };
}
function summarizeTestValues({ array, cryptoKey }) {
return [
` array instanceof Array === ${array instanceof Array}`,
` Array.isArray(array) === ${Array.isArray(array)}`,
`cryptoKey instanceof CryptoKey === ${cryptoKey instanceof CryptoKey}`
].join('\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment