diff --git a/lib/util/cache.js b/lib/util/cache.js index 2ea4cb26b20..d51c202765e 100644 --- a/lib/util/cache.js +++ b/lib/util/cache.js @@ -13,6 +13,24 @@ function makeCacheKey (opts) { throw new Error('opts.origin is undefined') } + /** @type {Record} */ + let headers + if (opts.headers == null) { + headers = {} + } else if (typeof opts.headers[Symbol.iterator] === 'function') { + headers = {} + for (const [key, val] of opts.headers) { + if (typeof key !== 'string' || typeof val !== 'string') { + throw new Error('opts.headers is not a valid header map') + } + headers[key] = val + } + } else if (typeof opts.headers === 'object') { + headers = opts.headers + } else { + throw new Error('opts.headers is not an object') + } + /** * @type {import('../../types/cache-interceptor.d.ts').default.CacheKey} */ @@ -20,7 +38,7 @@ function makeCacheKey (opts) { origin: opts.origin.toString(), method: opts.method, path: opts.path, - headers: opts.headers + headers } return cacheKey diff --git a/types/dispatcher.d.ts b/types/dispatcher.d.ts index 8b9e633d730..069fed63ac4 100644 --- a/types/dispatcher.d.ts +++ b/types/dispatcher.d.ts @@ -103,7 +103,7 @@ declare namespace Dispatcher { /** Default: `null` */ body?: string | Buffer | Uint8Array | Readable | null | FormData; /** Default: `null` */ - headers?: IncomingHttpHeaders | string[] | Iterable<[string, string | string[] | undefined]> | null; + headers?: Record | IncomingHttpHeaders | string[] | Iterable<[string, string | string[] | undefined]> | null; /** Query string params to be embedded in the request URL. Default: `null` */ query?: Record; /** Whether the requests can be safely retried or not. If `false` the request won't be sent until all preceding requests in the pipeline have completed. Default: `true` if `method` is `HEAD` or `GET`. */