http-cache-semantics icon indicating copy to clipboard operation
http-cache-semantics copied to clipboard

breaks if Cache-Control is array

Open gajus opened this issue 5 years ago • 2 comments

curl -v https://www.reservaentradas.com/cines
< HTTP/1.1 200 OK
< Date: Wed, 05 Feb 2020 18:51:26 GMT
< Server: Apache/2.2.22 (Debian)
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Vary: Accept-Encoding
< Cache-Control: max-age=0, no-cache
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=utf-8
<

Produces error:

TypeError: header.trim is not a function
          at parseCacheControl (/Users/gajus/Documents/dev/rayroute/rayman/node_modules/http-cache-semantics/index.js:62:26)
          at new CachePolicy (/Users/gajus/Documents/dev/rayroute/rayman/node_modules/http-cache-semantics/index.js:119:23)
          at action (/Users/gajus/Documents/dev/rayroute/rayman/src/interceptors/createCacheInterceptor/index.js:68:27)
          at Object.responseActions (/Users/gajus/Documents/dev/rayroute/rayman/node_modules/@rayroute/raygun/src/factories/createInterceptorActions.js:152:22)
          at processTicksAndRejections (internal/process/task_queues.js:97:5)
          at handleRequest (/Users/gajus/Documents/dev/rayroute/rayman/node_modules/@rayroute/raygun/src/factories/createRequestHandler.js:69:28)

gajus avatar Feb 05 '20 18:02 gajus

Oops, indeed!

Could you make it detect non-string header value and run join(',') on it?

kornelski avatar Feb 05 '20 18:02 kornelski

That is indeed how I have patched it locally.

+const normalizeHeaders = (headers: HeadersType): HeadersType => {
+  const normalizedHeaders = {
+    ...headers,
+  };
+
+  // @see https://github.com/kornelski/http-cache-semantics/issues/28
+  if (Array.isArray(normalizedHeaders['cache-control'])) {
+    normalizedHeaders['cache-control'] = normalizedHeaders['cache-control'].join(', ');
+  }
+
+  return normalizedHeaders;
+};
+

gajus avatar Feb 05 '20 18:02 gajus