2019-03-10 05:40:06 +01:00
|
|
|
/* global AUTH_CONFIG LOCALE */
|
2018-09-19 01:23:58 +02:00
|
|
|
import { browserName } from './utils';
|
|
|
|
|
2018-07-31 20:29:26 +02:00
|
|
|
async function checkCrypto() {
|
|
|
|
try {
|
|
|
|
const key = await crypto.subtle.generateKey(
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
length: 128
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['encrypt', 'decrypt']
|
|
|
|
);
|
2018-08-01 00:10:12 +02:00
|
|
|
await crypto.subtle.exportKey('raw', key);
|
2018-07-31 20:29:26 +02: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 19:36:49 +01:00
|
|
|
await crypto.subtle.generateKey(
|
|
|
|
{
|
|
|
|
name: 'ECDH',
|
|
|
|
namedCurve: 'P-256'
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['deriveBits']
|
|
|
|
);
|
2018-07-31 20:29:26 +02:00
|
|
|
return true;
|
|
|
|
} catch (err) {
|
2018-11-26 19:36:49 +01:00
|
|
|
try {
|
2019-02-21 21:22:12 +01:00
|
|
|
window.asmCrypto = await import('asmcrypto.js');
|
2019-02-22 00:20:28 +01:00
|
|
|
await import('@dannycoates/webcrypto-liner/build/shim');
|
2018-11-26 19:36:49 +01:00
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-31 20:29:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkStreams() {
|
|
|
|
try {
|
|
|
|
new ReadableStream({
|
|
|
|
pull() {}
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 15:50:59 +01:00
|
|
|
async function polyfillStreams() {
|
2018-07-31 20:29:26 +02:00
|
|
|
try {
|
2018-11-20 15:50:59 +01:00
|
|
|
await import('@mattiasbuelens/web-streams-polyfill');
|
2018-07-31 20:29:26 +02:00
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 20:12:06 +01:00
|
|
|
export default async function getCapabilities() {
|
2019-02-20 03:53:55 +01:00
|
|
|
const serviceWorker =
|
|
|
|
'serviceWorker' in navigator && browserName() !== 'edge';
|
2018-11-26 19:36:49 +01:00
|
|
|
let crypto = await checkCrypto();
|
2018-07-31 20:29:26 +02:00
|
|
|
const nativeStreams = checkStreams();
|
2018-11-20 15:50:59 +01:00
|
|
|
let polyStreams = false;
|
|
|
|
if (!nativeStreams) {
|
|
|
|
polyStreams = await polyfillStreams();
|
|
|
|
}
|
2018-09-19 21:53:23 +02:00
|
|
|
let account = typeof AUTH_CONFIG !== 'undefined';
|
2018-09-17 21:05:03 +02:00
|
|
|
try {
|
2019-02-25 20:44:44 +01:00
|
|
|
account = account && !!localStorage;
|
2018-09-17 21:05:03 +02:00
|
|
|
} catch (e) {
|
2018-11-26 19:36:49 +01:00
|
|
|
account = false;
|
2018-09-17 21:05:03 +02:00
|
|
|
}
|
2019-03-10 05:40:06 +01:00
|
|
|
const share =
|
|
|
|
typeof navigator.share === 'function' && LOCALE.startsWith('en'); // en until strings merge
|
2018-07-31 20:29:26 +02:00
|
|
|
|
|
|
|
return {
|
2018-09-17 21:05:03 +02:00
|
|
|
account,
|
2018-07-31 20:29:26 +02:00
|
|
|
crypto,
|
2018-12-10 21:47:23 +01:00
|
|
|
serviceWorker,
|
2018-07-31 20:29:26 +02:00
|
|
|
streamUpload: nativeStreams || polyStreams,
|
|
|
|
streamDownload:
|
2018-12-10 21:47:23 +01:00
|
|
|
nativeStreams && serviceWorker && browserName() !== 'safari',
|
2019-03-10 02:59:06 +01:00
|
|
|
multifile: nativeStreams || polyStreams,
|
|
|
|
share
|
2018-07-31 20:29:26 +02:00
|
|
|
};
|
|
|
|
}
|