feature: client: self signed certs on Chrome (#393)

This commit is contained in:
cintolas 2023-05-17 00:52:52 -07:00 committed by coderaiser
parent 895d19a153
commit ef6088531a
2 changed files with 28 additions and 1 deletions

View file

@ -1,5 +1,7 @@
'use strict';
const tryToCatch = require('try-to-catch');
module.exports.registerSW = registerSW;
module.exports.unregisterSW = unregisterSW;
@ -17,7 +19,14 @@ async function registerSW(prefix) {
if (!isHTTPS && !isLocalhost)
return;
return await navigator.serviceWorker.register(`${prefix}/sw.js`);
const {serviceWorker} = navigator;
const register = serviceWorker.register.bind(serviceWorker);
const [e, sw] = await tryToCatch(register,`${prefix}/sw.js`);
if (e)
return null;
return sw;
}
async function unregisterSW(prefix) {

View file

@ -70,6 +70,24 @@ test('sw: register: registerSW: http', async (t, {location, navigator}) => {
t.end();
});
test('sw: register: registerSW: https self-signed', async (t, {location, navigator}) => {
Object.assign(location, {
protocol: 'https',
hostname: 'self-signed.badssl.com',
});
const {register} = navigator.serviceWorker;
register.throws(Error('Cannot register service worker!'));
const {registerSW} = reRequire('./register');
const result = await registerSW();
t.notOk(result, 'should not throw');
t.end();
});
test('sw: register: registerSW', async (t, {location, navigator}) => {
location.hostname = 'localhost';