send/app/capabilities.js

106 lines
2.2 KiB
JavaScript
Raw Normal View History

/* global AUTH_CONFIG */
import { browserName } from './utils';
2018-07-31 18:29:26 +00:00
async function checkCrypto() {
try {
const key = await crypto.subtle.generateKey(
{
name: 'AES-GCM',
length: 128
},
true,
['encrypt', 'decrypt']
);
2018-07-31 22:10:12 +00:00
await crypto.subtle.exportKey('raw', key);
2018-07-31 18:29:26 +00:00
await crypto.subtle.encrypt(
{
name: 'AES-GCM',
iv: crypto.getRandomValues(new Uint8Array(12)),
tagLength: 128
},
key,
new ArrayBuffer(8)
);
await crypto.subtle.importKey(
'raw',
crypto.getRandomValues(new Uint8Array(16)),
'PBKDF2',
false,
['deriveKey']
);
await crypto.subtle.importKey(
'raw',
crypto.getRandomValues(new Uint8Array(16)),
'HKDF',
false,
['deriveKey']
);
2018-11-26 18:36:49 +00:00
await crypto.subtle.generateKey(
{
name: 'ECDH',
namedCurve: 'P-256'
},
true,
['deriveBits']
);
2018-07-31 18:29:26 +00:00
return true;
} catch (err) {
2018-11-26 18:36:49 +00:00
try {
window.asmCrypto = await import('asmcrypto.js');
await import('@dannycoates/webcrypto-liner/build/shim');
2018-11-26 18:36:49 +00:00
return true;
} catch (e) {
return false;
}
2018-07-31 18:29:26 +00:00
}
}
function checkStreams() {
try {
new ReadableStream({
pull() {}
});
return true;
} catch (e) {
return false;
}
}
async function polyfillStreams() {
2018-07-31 18:29:26 +00:00
try {
await import('@mattiasbuelens/web-streams-polyfill');
2018-07-31 18:29:26 +00:00
return true;
} catch (e) {
return false;
}
}
2018-12-13 19:12:06 +00:00
export default async function getCapabilities() {
2019-02-20 02:53:55 +00:00
const serviceWorker =
'serviceWorker' in navigator && browserName() !== 'edge';
2018-11-26 18:36:49 +00:00
let crypto = await checkCrypto();
2018-07-31 18:29:26 +00:00
const nativeStreams = checkStreams();
let polyStreams = false;
if (!nativeStreams) {
polyStreams = await polyfillStreams();
}
let account = typeof AUTH_CONFIG !== 'undefined';
try {
2019-02-25 19:44:44 +00:00
account = account && !!localStorage;
} catch (e) {
2018-11-26 18:36:49 +00:00
account = false;
}
2019-03-10 01:59:06 +00:00
const share = !!navigator.share;
2018-07-31 18:29:26 +00:00
return {
account,
2018-07-31 18:29:26 +00:00
crypto,
serviceWorker,
2018-07-31 18:29:26 +00:00
streamUpload: nativeStreams || polyStreams,
streamDownload:
nativeStreams && serviceWorker && browserName() !== 'safari',
2019-03-10 01:59:06 +00:00
multifile: nativeStreams || polyStreams,
share
2018-07-31 18:29:26 +00:00
};
}