From 5bae7cace6c2dd4bfd623fd98514ea84cdfbde6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=27rysiek=27=20Wo=C5=BAniak?= Date: Wed, 28 Feb 2024 02:58:28 +0000 Subject: [PATCH] minor improvements, including fetch plugin now returning 4xx errors directly as Response objects (ref. #36) --- plugins/delay/index.js | 5 +++++ plugins/fetch/index.js | 7 ++++--- service-worker.js | 2 ++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/plugins/delay/index.js b/plugins/delay/index.js index aa36553..c5632d6 100644 --- a/plugins/delay/index.js +++ b/plugins/delay/index.js @@ -35,6 +35,11 @@ // merge the defaults with settings from init let config = {...defaultConfig, ...init} + // reality check: if no wrapped plugin configured, or more than one, complain + if (config.uses.length != 1) { + throw new Error(`Expected exactly one plugin to wrap, but ${config.uses.length} configured.`) + } + /** * getting content using regular HTTP(S) fetch() */ diff --git a/plugins/fetch/index.js b/plugins/fetch/index.js index d7fe153..d240e6f 100644 --- a/plugins/fetch/index.js +++ b/plugins/fetch/index.js @@ -32,13 +32,14 @@ // run built-in regular fetch() return fetch(url, init) .then((response) => { - // 4xx? 5xx? that's a paddlin' - if (response.status >= 400) { + // 5xx? that's a paddlin' + // we do want to pass 3xx and 4xx on back to the client though! + if (response.status >= 500) { // throw an Error to fall back to LibResilient: throw new Error('HTTP Error: ' + response.status + ' ' + response.statusText); } // all good, it seems - LR.log(pluginName, `fetched successfully: ${response.url}`); + LR.log(pluginName, `fetched:\n+-- url: ${response.url}\n+-- http status: ${response.status} (${response.statusText})`); // we need to create a new Response object // with all the headers added explicitly, diff --git a/service-worker.js b/service-worker.js index c7f010f..293cf00 100644 --- a/service-worker.js +++ b/service-worker.js @@ -820,6 +820,8 @@ let LibResilientClient = class { constructor(clientId) { + self.log('service-worker', `new client: ${clientId}`) + // we often get the clientId long before // we are able to get a valid client out of it //