From d40cf65714005ef849ab4b7411c923867a4e8fef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=27rysiek=27=20Wo=C5=BAniak?= Date: Wed, 31 Jan 2024 15:41:58 +0000 Subject: [PATCH] loading external MIME sniffing library moved to executeConfig() (ref. #48) --- service-worker.js | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/service-worker.js b/service-worker.js index 7928148..bb4cf94 100644 --- a/service-worker.js +++ b/service-worker.js @@ -381,6 +381,26 @@ let executeConfig = (pluginsConfig) => { } + // finally -- do we want to use MIME type guessing based on content? + // dealing with this at the very end so that we know we can safely set detectMimeFromBuffer + // and not need to re-set it back in case anything fails + if (self.LibResilientConfig.useMimeSniffingLibrary === true) { + // we do not want to hit a NetworkError and end up using the default config + // much better to end up not using the fancy MIME type detection in such a case + try { + // we do! load the external lib + self.importScripts(`./lib/file-type.js`) + } catch (e) { + self.log('service-worker', `error when fetching external MIME sniffing library: ${e.message}`) + } + if (typeof fileType !== 'undefined' && "fileTypeFromBuffer" in fileType) { + detectMimeFromBuffer = fileType.fileTypeFromBuffer + self.log('service-worker', 'loaded external MIME sniffing library') + } else { + self.log('service-worker', 'failed to load external MIME sniffing library!') + } + } + } @@ -447,24 +467,6 @@ let initServiceWorker = async () => { self.log('service-worker', 'config loading failed, using defaults; error:', e) } - // first let's deal with the easy part -- do we want to use MIME type guessing based on content? - if (self.LibResilientConfig.useMimeSniffingLibrary === true) { - // we do not want to hit a NetworkError and end up using the default config - // much better to end up not using the fancy MIME type detection in such a case - try { - // we do! load the external lib - self.importScripts(`./lib/file-type.js`) - } catch (e) { - self.log('service-worker', `error when fetching external MIME sniffing library: ${e.message}`) - } - if (typeof fileType !== 'undefined' && "fileTypeFromBuffer" in fileType) { - detectMimeFromBuffer = fileType.fileTypeFromBuffer - self.log('service-worker', 'loaded external MIME sniffing library') - } else { - self.log('service-worker', 'failed to load external MIME sniffing library!') - } - } - // create the LibResilientPluginConstructors map // the global... hack is here so that we can run tests; not the most elegant // TODO: find a better way