2019-04-26 20:30:33 +00:00
|
|
|
/* global AUTH_CONFIG */
|
|
|
|
import { browserName, locale } from './utils';
|
2018-09-18 23:23:58 +00:00
|
|
|
|
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) {
|
2022-09-04 10:26:12 +00:00
|
|
|
try {
|
|
|
|
window.asmCrypto = await import('asmcrypto.js');
|
|
|
|
await import('@dannycoates/webcrypto-liner/build/shim');
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 10:26:12 +00:00
|
|
|
async function polyfillStreams() {
|
|
|
|
try {
|
|
|
|
await import('@mattiasbuelens/web-streams-polyfill');
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 19:12:06 +00:00
|
|
|
export default async function getCapabilities() {
|
2019-08-26 17:49:19 +00:00
|
|
|
const browser = browserName();
|
2020-07-31 15:36:27 +00:00
|
|
|
const isMobile = /mobi|android/i.test(navigator.userAgent);
|
2019-08-26 17:49:19 +00:00
|
|
|
const serviceWorker = 'serviceWorker' in navigator && browser !== 'edge';
|
2018-11-26 18:36:49 +00:00
|
|
|
let crypto = await checkCrypto();
|
2018-07-31 18:29:26 +00:00
|
|
|
const nativeStreams = checkStreams();
|
2022-09-04 10:26:12 +00:00
|
|
|
let polyStreams = false;
|
|
|
|
if (!nativeStreams) {
|
|
|
|
polyStreams = await polyfillStreams();
|
|
|
|
}
|
2018-09-19 19:53:23 +00:00
|
|
|
let account = typeof AUTH_CONFIG !== 'undefined';
|
2018-09-17 19:05:03 +00:00
|
|
|
try {
|
2019-02-25 19:44:44 +00:00
|
|
|
account = account && !!localStorage;
|
2018-09-17 19:05:03 +00:00
|
|
|
} catch (e) {
|
2018-11-26 18:36:49 +00:00
|
|
|
account = false;
|
2018-09-17 19:05:03 +00:00
|
|
|
}
|
2019-03-10 04:40:06 +00:00
|
|
|
const share =
|
2020-07-31 15:36:27 +00:00
|
|
|
isMobile &&
|
|
|
|
typeof navigator.share === 'function' &&
|
|
|
|
locale().startsWith('en'); // en until strings merge
|
2018-07-31 18:29:26 +00:00
|
|
|
|
2019-03-13 17:30:03 +00:00
|
|
|
const standalone =
|
|
|
|
window.matchMedia('(display-mode: standalone)').matches ||
|
|
|
|
navigator.standalone;
|
|
|
|
|
2020-07-31 15:36:27 +00:00
|
|
|
const mobileFirefox = browser === 'firefox' && isMobile;
|
2019-08-26 17:49:19 +00:00
|
|
|
|
2018-07-31 18:29:26 +00:00
|
|
|
return {
|
2018-09-17 19:05:03 +00:00
|
|
|
account,
|
2018-07-31 18:29:26 +00:00
|
|
|
crypto,
|
2018-12-10 20:47:23 +00:00
|
|
|
serviceWorker,
|
2022-09-04 10:26:12 +00:00
|
|
|
streamUpload: nativeStreams || polyStreams,
|
2018-07-31 18:29:26 +00:00
|
|
|
streamDownload:
|
2019-08-26 17:49:19 +00:00
|
|
|
nativeStreams && serviceWorker && browser !== 'safari' && !mobileFirefox,
|
2022-09-04 10:26:12 +00:00
|
|
|
multifile: nativeStreams || polyStreams,
|
2019-03-13 17:30:03 +00:00
|
|
|
share,
|
|
|
|
standalone
|
2018-07-31 18:29:26 +00:00
|
|
|
};
|
|
|
|
}
|