drop.chapril.org-firefoxsend/app/capabilities.js
AaronDewes 64644b57e3 Remove some polyfills
This should reduce the runtime size & load times somewhat by removing polyfills for older browsers which are not used much
2022-08-09 15:17:38 +00:00

98 lines
2.1 KiB
JavaScript

/* global AUTH_CONFIG */
import { browserName, locale } from './utils';
async function checkCrypto() {
try {
const key = await crypto.subtle.generateKey(
{
name: 'AES-GCM',
length: 128
},
true,
['encrypt', 'decrypt']
);
await crypto.subtle.exportKey('raw', key);
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']
);
await crypto.subtle.generateKey(
{
name: 'ECDH',
namedCurve: 'P-256'
},
true,
['deriveBits']
);
return true;
} catch (err) {
return false;
}
}
function checkStreams() {
try {
new ReadableStream({
pull() {}
});
return true;
} catch (e) {
return false;
}
}
export default async function getCapabilities() {
const browser = browserName();
const isMobile = /mobi|android/i.test(navigator.userAgent);
const serviceWorker = 'serviceWorker' in navigator && browser !== 'edge';
let crypto = await checkCrypto();
const nativeStreams = checkStreams();
let account = typeof AUTH_CONFIG !== 'undefined';
try {
account = account && !!localStorage;
} catch (e) {
account = false;
}
const share =
isMobile &&
typeof navigator.share === 'function' &&
locale().startsWith('en'); // en until strings merge
const standalone =
window.matchMedia('(display-mode: standalone)').matches ||
navigator.standalone;
const mobileFirefox = browser === 'firefox' && isMobile;
return {
account,
crypto,
serviceWorker,
streamUpload: nativeStreams,
streamDownload:
nativeStreams && serviceWorker && browser !== 'safari' && !mobileFirefox,
multifile: nativeStreams,
share,
standalone
};
}