1
0
mirror of https://gitlab.com/timvisee/send.git synced 2024-11-10 13:13:00 +01:00
send/frontend/src/utils.js

129 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-06-28 20:30:14 +02:00
function arrayToHex(iv) {
2017-06-02 05:59:27 +02:00
let hexStr = '';
// eslint-disable-next-line prefer-const
2017-07-27 16:46:28 +02:00
for (let i in iv) {
2017-06-02 05:59:27 +02:00
if (iv[i] < 16) {
hexStr += '0' + iv[i].toString(16);
} else {
hexStr += iv[i].toString(16);
}
}
return hexStr;
}
2017-06-28 20:30:14 +02:00
function hexToArray(str) {
const iv = new Uint8Array(str.length / 2);
2017-06-02 05:59:27 +02:00
for (let i = 0; i < str.length; i += 2) {
iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
}
return iv;
}
2017-06-21 22:23:36 +02:00
function notify(str) {
2017-07-26 20:24:58 +02:00
return str;
/* TODO: enable once we have an opt-in ui element
if (!('Notification' in window)) {
2017-06-21 22:23:36 +02:00
return;
} else if (Notification.permission === 'granted') {
new Notification(str);
2017-06-21 22:23:36 +02:00
} else if (Notification.permission !== 'denied') {
Notification.requestPermission(function(permission) {
if (permission === 'granted') new Notification(str);
});
2017-06-21 22:23:36 +02:00
}
2017-07-26 20:24:58 +02:00
*/
2017-06-21 22:23:36 +02:00
}
function gcmCompliant() {
try {
2017-07-12 19:53:29 +02:00
return window.crypto.subtle
.generateKey(
{
name: 'AES-GCM',
2017-07-12 19:53:29 +02:00
length: 128
},
2017-07-12 19:53:29 +02:00
true,
['encrypt', 'decrypt']
)
2017-07-12 19:53:29 +02:00
.then(key => {
return window.crypto.subtle
.encrypt(
{
name: 'AES-GCM',
iv: window.crypto.getRandomValues(new Uint8Array(12)),
additionalData: window.crypto.getRandomValues(new Uint8Array(6)),
tagLength: 128
},
key,
new ArrayBuffer(8)
)
.then(() => {
return Promise.resolve();
});
})
.catch(err => {
return loadShim();
2017-07-12 19:53:29 +02:00
});
} catch (err) {
return loadShim();
}
function loadShim() {
return new Promise((resolve, reject) => {
const shim = document.createElement('script');
shim.src = '/cryptofill.js';
shim.addEventListener('load', resolve);
shim.addEventListener('error', reject);
document.head.appendChild(shim);
});
}
}
2017-07-21 00:16:00 +02:00
function findMetric(href) {
2017-07-22 02:01:26 +02:00
switch (href) {
2017-07-21 00:16:00 +02:00
case 'https://www.mozilla.org/':
return 'mozilla';
case 'https://www.mozilla.org/about/legal':
return 'legal';
case 'https://testpilot.firefox.com/about':
return 'about';
case 'https://testpilot.firefox.com/privacy':
return 'privacy';
case 'https://testpilot.firefox.com/terms':
return 'terms';
case 'https://www.mozilla.org/privacy/websites/#cookies':
2017-07-21 00:16:00 +02:00
return 'cookies';
case 'https://github.com/mozilla/send':
return 'github';
case 'https://twitter.com/FxTestPilot':
return 'twitter';
case 'https://www.mozilla.org/firefox/new/?scene=2':
return 'download-firefox';
default:
return 'other';
}
}
function isFile(id) {
return /^[0-9a-fA-F]{10}$/.test(id);
}
2017-07-21 22:25:08 +02:00
function sendEvent() {
2017-07-22 02:01:26 +02:00
return window.analytics.sendEvent
.apply(window.analytics, arguments)
.catch(() => 0);
2017-07-21 22:25:08 +02:00
}
const ONE_DAY_IN_MS = 86400000;
2017-06-02 05:59:27 +02:00
module.exports = {
2017-06-28 20:30:14 +02:00
arrayToHex,
hexToArray,
notify,
2017-07-21 00:16:00 +02:00
gcmCompliant,
findMetric,
isFile,
2017-07-21 22:25:08 +02:00
sendEvent,
ONE_DAY_IN_MS
2017-06-02 05:59:27 +02:00
};