From 9f61795eb05fcd1dd8e3d6befcc89ae8fff28c34 Mon Sep 17 00:00:00 2001 From: "David I. Lehn" Date: Fri, 3 Jun 2022 21:57:01 -0400 Subject: [PATCH] Fix immutable response header bug. Node 18 responses have an immutable location header. Refactor logic to avoid issue. --- lib/documentLoaders/node.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/documentLoaders/node.js b/lib/documentLoaders/node.js index 9de3b7ba..61ad6b30 100644 --- a/lib/documentLoaders/node.js +++ b/lib/documentLoaders/node.js @@ -80,6 +80,7 @@ module.exports = ({ url, headers, strictSSL, httpAgent, httpsAgent }); doc = {contextUrl: null, documentUrl: url, document: body || null}; + // handle error const statusText = http.STATUS_CODES[res.status]; if(res.status >= 400) { @@ -92,7 +93,9 @@ module.exports = ({ }); } const link = res.headers.get('link'); + let location = res.headers.get('location'); const contentType = res.headers.get('content-type'); + // handle Link Header if(link && contentType !== 'application/ld+json') { // only 1 related link header permitted @@ -115,10 +118,10 @@ module.exports = ({ alternate.type == 'application/ld+json' && !(contentType || '') .match(/^application\/(\w*\+)?json$/)) { - res.headers.set('location', prependBase(url, alternate.target)); + location = prependBase(url, alternate.target); } } - const location = res.headers.get('location'); + // handle redirect if((alternate || res.status >= 300 && res.status < 400) && location) {