1
0
mirror of https://gitlab.com/timvisee/send.git synced 2024-11-15 07:32:48 +01:00
send/app/capabilities.js

117 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-04-26 22:30:33 +02:00
/* global AUTH_CONFIG */
import { browserName, locale } 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 {
window.asmCrypto = await import('asmcrypto.js');
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;
}
}
async function polyfillStreams() {
2018-07-31 20:29:26 +02:00
try {
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() {
const browser = browserName();
const isMobile = /mobi|android/i.test(navigator.userAgent);
const serviceWorker = 'serviceWorker' in navigator && browser !== 'edge';
2018-11-26 19:36:49 +01:00
let crypto = await checkCrypto();
2018-07-31 20:29:26 +02:00
const nativeStreams = checkStreams();
let polyStreams = false;
if (!nativeStreams) {
polyStreams = await polyfillStreams();
}
let account = typeof AUTH_CONFIG !== 'undefined';
try {
2019-02-25 20:44:44 +01:00
account = account && !!localStorage;
} catch (e) {
2018-11-26 19:36:49 +01:00
account = false;
}
2019-03-10 05:40:06 +01:00
const share =
isMobile &&
typeof navigator.share === 'function' &&
locale().startsWith('en'); // en until strings merge
2018-07-31 20:29:26 +02:00
2019-03-13 18:30:03 +01:00
const standalone =
window.matchMedia('(display-mode: standalone)').matches ||
navigator.standalone;
const mobileFirefox = browser === 'firefox' && isMobile;
2018-07-31 20:29:26 +02:00
return {
account,
2018-07-31 20:29:26 +02:00
crypto,
serviceWorker,
2018-07-31 20:29:26 +02:00
streamUpload: nativeStreams || polyStreams,
streamDownload:
nativeStreams && serviceWorker && browser !== 'safari' && !mobileFirefox,
2019-03-10 02:59:06 +01:00
multifile: nativeStreams || polyStreams,
2019-03-13 18:30:03 +01:00
share,
standalone
2018-07-31 20:29:26 +02:00
};
}